Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 1 | #include <string.h> |
| 2 | #include "slip_common.h" |
Al Viro | 37185b3 | 2012-10-08 03:27:32 +0100 | [diff] [blame] | 3 | #include <net_user.h> |
Jeff Dike | a3c77c6 | 2005-06-13 15:52:18 -0700 | [diff] [blame] | 4 | |
| 5 | int slip_proto_read(int fd, void *buf, int len, struct slip_proto *slip) |
| 6 | { |
| 7 | int i, n, size, start; |
| 8 | |
| 9 | if(slip->more > 0){ |
| 10 | i = 0; |
| 11 | while(i < slip->more){ |
| 12 | size = slip_unesc(slip->ibuf[i++], slip->ibuf, |
| 13 | &slip->pos, &slip->esc); |
| 14 | if(size){ |
| 15 | memcpy(buf, slip->ibuf, size); |
| 16 | memmove(slip->ibuf, &slip->ibuf[i], |
| 17 | slip->more - i); |
| 18 | slip->more = slip->more - i; |
| 19 | return size; |
| 20 | } |
| 21 | } |
| 22 | slip->more = 0; |
| 23 | } |
| 24 | |
| 25 | n = net_read(fd, &slip->ibuf[slip->pos], |
| 26 | sizeof(slip->ibuf) - slip->pos); |
| 27 | if(n <= 0) |
| 28 | return n; |
| 29 | |
| 30 | start = slip->pos; |
| 31 | for(i = 0; i < n; i++){ |
| 32 | size = slip_unesc(slip->ibuf[start + i], slip->ibuf,&slip->pos, |
| 33 | &slip->esc); |
| 34 | if(size){ |
| 35 | memcpy(buf, slip->ibuf, size); |
| 36 | memmove(slip->ibuf, &slip->ibuf[start+i+1], |
| 37 | n - (i + 1)); |
| 38 | slip->more = n - (i + 1); |
| 39 | return size; |
| 40 | } |
| 41 | } |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | int slip_proto_write(int fd, void *buf, int len, struct slip_proto *slip) |
| 46 | { |
| 47 | int actual, n; |
| 48 | |
| 49 | actual = slip_esc(buf, slip->obuf, len); |
| 50 | n = net_write(fd, slip->obuf, actual); |
| 51 | if(n < 0) |
| 52 | return n; |
| 53 | else return len; |
| 54 | } |