bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 1995 Danny Gasparovski. |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3 | * |
| 4 | * Please read the file COPYRIGHT for the |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 5 | * terms and conditions of the copyright. |
| 6 | */ |
| 7 | |
| 8 | #include <slirp.h> |
| 9 | |
blueswir1 | 9634d90 | 2007-10-26 19:01:16 +0000 | [diff] [blame] | 10 | static void sbappendsb(struct sbuf *sb, struct mbuf *m); |
| 11 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 12 | void |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 13 | sbfree(struct sbuf *sb) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 14 | { |
| 15 | free(sb->sb_data); |
| 16 | } |
| 17 | |
| 18 | void |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 19 | sbdrop(struct sbuf *sb, int num) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 20 | { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 21 | /* |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 22 | * We can only drop how much we have |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 23 | * This should never succeed |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 24 | */ |
| 25 | if(num > sb->sb_cc) |
| 26 | num = sb->sb_cc; |
| 27 | sb->sb_cc -= num; |
| 28 | sb->sb_rptr += num; |
| 29 | if(sb->sb_rptr >= sb->sb_data + sb->sb_datalen) |
| 30 | sb->sb_rptr -= sb->sb_datalen; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 31 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | void |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 35 | sbreserve(struct sbuf *sb, int size) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 36 | { |
| 37 | if (sb->sb_data) { |
| 38 | /* Already alloced, realloc if necessary */ |
| 39 | if (sb->sb_datalen != size) { |
| 40 | sb->sb_wptr = sb->sb_rptr = sb->sb_data = (char *)realloc(sb->sb_data, size); |
| 41 | sb->sb_cc = 0; |
| 42 | if (sb->sb_wptr) |
| 43 | sb->sb_datalen = size; |
| 44 | else |
| 45 | sb->sb_datalen = 0; |
| 46 | } |
| 47 | } else { |
| 48 | sb->sb_wptr = sb->sb_rptr = sb->sb_data = (char *)malloc(size); |
| 49 | sb->sb_cc = 0; |
| 50 | if (sb->sb_wptr) |
| 51 | sb->sb_datalen = size; |
| 52 | else |
| 53 | sb->sb_datalen = 0; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * Try and write() to the socket, whatever doesn't get written |
| 59 | * append to the buffer... for a host with a fast net connection, |
| 60 | * this prevents an unnecessary copy of the data |
| 61 | * (the socket is non-blocking, so we won't hang) |
| 62 | */ |
| 63 | void |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 64 | sbappend(struct socket *so, struct mbuf *m) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 65 | { |
| 66 | int ret = 0; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 67 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 68 | DEBUG_CALL("sbappend"); |
| 69 | DEBUG_ARG("so = %lx", (long)so); |
| 70 | DEBUG_ARG("m = %lx", (long)m); |
| 71 | DEBUG_ARG("m->m_len = %d", m->m_len); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 72 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 73 | /* Shouldn't happen, but... e.g. foreign host closes connection */ |
| 74 | if (m->m_len <= 0) { |
| 75 | m_free(m); |
| 76 | return; |
| 77 | } |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 78 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 79 | /* |
| 80 | * If there is urgent data, call sosendoob |
| 81 | * if not all was sent, sowrite will take care of the rest |
| 82 | * (The rest of this function is just an optimisation) |
| 83 | */ |
| 84 | if (so->so_urgc) { |
| 85 | sbappendsb(&so->so_rcv, m); |
| 86 | m_free(m); |
| 87 | sosendoob(so); |
| 88 | return; |
| 89 | } |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 90 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 91 | /* |
| 92 | * We only write if there's nothing in the buffer, |
| 93 | * ottherwise it'll arrive out of order, and hence corrupt |
| 94 | */ |
| 95 | if (!so->so_rcv.sb_cc) |
aliguori | e1c5a2b | 2009-01-08 19:18:21 +0000 | [diff] [blame] | 96 | ret = slirp_send(so, m->m_data, m->m_len, 0); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 97 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 98 | if (ret <= 0) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 99 | /* |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 100 | * Nothing was written |
| 101 | * It's possible that the socket has closed, but |
| 102 | * we don't need to check because if it has closed, |
| 103 | * it will be detected in the normal way by soread() |
| 104 | */ |
| 105 | sbappendsb(&so->so_rcv, m); |
| 106 | } else if (ret != m->m_len) { |
| 107 | /* |
| 108 | * Something was written, but not everything.. |
| 109 | * sbappendsb the rest |
| 110 | */ |
| 111 | m->m_len -= ret; |
| 112 | m->m_data += ret; |
| 113 | sbappendsb(&so->so_rcv, m); |
| 114 | } /* else */ |
| 115 | /* Whatever happened, we free the mbuf */ |
| 116 | m_free(m); |
| 117 | } |
| 118 | |
| 119 | /* |
| 120 | * Copy the data from m into sb |
| 121 | * The caller is responsible to make sure there's enough room |
| 122 | */ |
blueswir1 | 9634d90 | 2007-10-26 19:01:16 +0000 | [diff] [blame] | 123 | static void |
| 124 | sbappendsb(struct sbuf *sb, struct mbuf *m) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 125 | { |
| 126 | int len, n, nn; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 127 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 128 | len = m->m_len; |
| 129 | |
| 130 | if (sb->sb_wptr < sb->sb_rptr) { |
| 131 | n = sb->sb_rptr - sb->sb_wptr; |
| 132 | if (n > len) n = len; |
| 133 | memcpy(sb->sb_wptr, m->m_data, n); |
| 134 | } else { |
| 135 | /* Do the right edge first */ |
| 136 | n = sb->sb_data + sb->sb_datalen - sb->sb_wptr; |
| 137 | if (n > len) n = len; |
| 138 | memcpy(sb->sb_wptr, m->m_data, n); |
| 139 | len -= n; |
| 140 | if (len) { |
| 141 | /* Now the left edge */ |
| 142 | nn = sb->sb_rptr - sb->sb_data; |
| 143 | if (nn > len) nn = len; |
| 144 | memcpy(sb->sb_data,m->m_data+n,nn); |
| 145 | n += nn; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | sb->sb_cc += n; |
| 150 | sb->sb_wptr += n; |
| 151 | if (sb->sb_wptr >= sb->sb_data + sb->sb_datalen) |
| 152 | sb->sb_wptr -= sb->sb_datalen; |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * Copy data from sbuf to a normal, straight buffer |
| 157 | * Don't update the sbuf rptr, this will be |
| 158 | * done in sbdrop when the data is acked |
| 159 | */ |
| 160 | void |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 161 | sbcopy(struct sbuf *sb, int off, int len, char *to) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 162 | { |
| 163 | char *from; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 164 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 165 | from = sb->sb_rptr + off; |
| 166 | if (from >= sb->sb_data + sb->sb_datalen) |
| 167 | from -= sb->sb_datalen; |
| 168 | |
| 169 | if (from < sb->sb_wptr) { |
| 170 | if (len > sb->sb_cc) len = sb->sb_cc; |
| 171 | memcpy(to,from,len); |
| 172 | } else { |
| 173 | /* re-use off */ |
| 174 | off = (sb->sb_data + sb->sb_datalen) - from; |
| 175 | if (off > len) off = len; |
| 176 | memcpy(to,from,off); |
| 177 | len -= off; |
| 178 | if (len) |
| 179 | memcpy(to+off,sb->sb_data,len); |
| 180 | } |
| 181 | } |