1 | /*
|
---|
2 | * Copyright (c) 1995 Danny Gasparovski.
|
---|
3 | *
|
---|
4 | * Please read the file COPYRIGHT for the
|
---|
5 | * terms and conditions of the copyright.
|
---|
6 | */
|
---|
7 |
|
---|
8 | #include <slirp.h>
|
---|
9 |
|
---|
10 | /* Done as a macro in socket.h */
|
---|
11 | /* int
|
---|
12 | * sbspace(struct sockbuff *sb)
|
---|
13 | * {
|
---|
14 | * return SB_DATALEN - sb->sb_cc;
|
---|
15 | * }
|
---|
16 | */
|
---|
17 |
|
---|
18 | void
|
---|
19 | sbfree(struct sbuf *sb)
|
---|
20 | {
|
---|
21 | RTMemFree(sb->sb_data);
|
---|
22 | }
|
---|
23 |
|
---|
24 | void
|
---|
25 | sbdrop(struct sbuf *sb, int num)
|
---|
26 | {
|
---|
27 | /*
|
---|
28 | * We can only drop how much we have
|
---|
29 | * This should never succeed
|
---|
30 | */
|
---|
31 | if (num > sb->sb_cc)
|
---|
32 | num = sb->sb_cc;
|
---|
33 | sb->sb_cc -= num;
|
---|
34 | sb->sb_rptr += num;
|
---|
35 | if (sb->sb_rptr >= sb->sb_data + sb->sb_datalen)
|
---|
36 | sb->sb_rptr -= sb->sb_datalen;
|
---|
37 |
|
---|
38 | }
|
---|
39 |
|
---|
40 | void
|
---|
41 | sbreserve(PNATState pData, struct sbuf *sb, int size)
|
---|
42 | {
|
---|
43 | if (sb->sb_data)
|
---|
44 | {
|
---|
45 | /* Already alloced, realloc if necessary */
|
---|
46 | if (sb->sb_datalen != size)
|
---|
47 | {
|
---|
48 | sb->sb_wptr =
|
---|
49 | sb->sb_rptr =
|
---|
50 | sb->sb_data = (char *)RTMemRealloc(sb->sb_data, size);
|
---|
51 | sb->sb_cc = 0;
|
---|
52 | if (sb->sb_wptr)
|
---|
53 | sb->sb_datalen = size;
|
---|
54 | else
|
---|
55 | sb->sb_datalen = 0;
|
---|
56 | }
|
---|
57 | }
|
---|
58 | else
|
---|
59 | {
|
---|
60 | sb->sb_wptr = sb->sb_rptr = sb->sb_data = (char *)RTMemAlloc(size);
|
---|
61 | sb->sb_cc = 0;
|
---|
62 | if (sb->sb_wptr)
|
---|
63 | sb->sb_datalen = size;
|
---|
64 | else
|
---|
65 | sb->sb_datalen = 0;
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | /*
|
---|
70 | * Try and write() to the socket, whatever doesn't get written
|
---|
71 | * append to the buffer... for a host with a fast net connection,
|
---|
72 | * this prevents an unnecessary copy of the data
|
---|
73 | * (the socket is non-blocking, so we won't hang)
|
---|
74 | */
|
---|
75 | void
|
---|
76 | sbappend(PNATState pData, struct socket *so, struct mbuf *m)
|
---|
77 | {
|
---|
78 | int ret = 0;
|
---|
79 | #ifdef VBOX_WITH_SLIRP_BSD_MBUF
|
---|
80 | int mlen = 0;
|
---|
81 | caddr_t buf = NULL;
|
---|
82 | #endif
|
---|
83 |
|
---|
84 | STAM_PROFILE_START(&pData->StatIOSBAppend_pf, a);
|
---|
85 | DEBUG_CALL("sbappend");
|
---|
86 | DEBUG_ARG("so = %lx", (long)so);
|
---|
87 | DEBUG_ARG("m = %lx", (long)m);
|
---|
88 | DEBUG_ARG("m->m_len = %d", m ? m->m_len : 0);
|
---|
89 |
|
---|
90 | STAM_COUNTER_INC(&pData->StatIOSBAppend);
|
---|
91 | /* Shouldn't happen, but... e.g. foreign host closes connection */
|
---|
92 | #ifndef VBOX_WITH_SLIRP_BSD_MBUF
|
---|
93 | if (m->m_len <= 0)
|
---|
94 | #else
|
---|
95 | mlen = m_length(m, NULL);
|
---|
96 | if (mlen <= 0)
|
---|
97 | #endif
|
---|
98 | {
|
---|
99 | STAM_COUNTER_INC(&pData->StatIOSBAppend_zm);
|
---|
100 | goto done;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /*
|
---|
104 | * If there is urgent data, call sosendoob
|
---|
105 | * if not all was sent, sowrite will take care of the rest
|
---|
106 | * (The rest of this function is just an optimisation)
|
---|
107 | */
|
---|
108 | if (so->so_urgc)
|
---|
109 | {
|
---|
110 | sbappendsb(pData, &so->so_rcv, m);
|
---|
111 | m_free(pData, m);
|
---|
112 | sosendoob(so);
|
---|
113 | return;
|
---|
114 | }
|
---|
115 |
|
---|
116 | /*
|
---|
117 | * We only write if there's nothing in the buffer,
|
---|
118 | * ottherwise it'll arrive out of order, and hence corrupt
|
---|
119 | */
|
---|
120 | #ifndef VBOX_WITH_SLIRP_BSD_MBUF
|
---|
121 | if(!so->so_rcv.sb_cc)
|
---|
122 | ret = send(so->s, m->m_data, m->m_len, 0);
|
---|
123 | #else
|
---|
124 | buf = RTMemAlloc(mlen);
|
---|
125 | if (buf == NULL)
|
---|
126 | {
|
---|
127 | ret = 0;
|
---|
128 | goto no_sent;
|
---|
129 | }
|
---|
130 | m_copydata(m, 0, mlen, buf);
|
---|
131 | if(!so->so_rcv.sb_cc)
|
---|
132 | ret = send(so->s, buf, mlen, 0);
|
---|
133 | RTMemFree(buf);
|
---|
134 | no_sent:
|
---|
135 | #endif
|
---|
136 |
|
---|
137 | if (ret <= 0)
|
---|
138 | {
|
---|
139 | STAM_COUNTER_INC(&pData->StatIOSBAppend_wf);
|
---|
140 | /*
|
---|
141 | * Nothing was written
|
---|
142 | * It's possible that the socket has closed, but
|
---|
143 | * we don't need to check because if it has closed,
|
---|
144 | * it will be detected in the normal way by soread()
|
---|
145 | */
|
---|
146 | sbappendsb(pData, &so->so_rcv, m);
|
---|
147 | STAM_PROFILE_STOP(&pData->StatIOSBAppend_pf_wf, a);
|
---|
148 | goto done;
|
---|
149 | }
|
---|
150 | #ifndef VBOX_WITH_SLIRP_BSD_MBUF
|
---|
151 | else if (ret != m->m_len)
|
---|
152 | #else
|
---|
153 | else if (ret != mlen)
|
---|
154 | #endif
|
---|
155 | {
|
---|
156 | STAM_COUNTER_INC(&pData->StatIOSBAppend_wp);
|
---|
157 | /*
|
---|
158 | * Something was written, but not everything..
|
---|
159 | * sbappendsb the rest
|
---|
160 | */
|
---|
161 | #ifndef VBOX_WITH_SLIRP_BSD_MBUF
|
---|
162 | m->m_len -= ret;
|
---|
163 | m->m_data += ret;
|
---|
164 | #else
|
---|
165 | m_adj(m, ret);
|
---|
166 | #endif
|
---|
167 | sbappendsb(pData, &so->so_rcv, m);
|
---|
168 | STAM_PROFILE_STOP(&pData->StatIOSBAppend_pf_wp, a);
|
---|
169 | goto done;
|
---|
170 | } /* else */
|
---|
171 | /* Whatever happened, we free the mbuf */
|
---|
172 | STAM_COUNTER_INC(&pData->StatIOSBAppend_wa);
|
---|
173 | STAM_PROFILE_STOP(&pData->StatIOSBAppend_pf_wa, a);
|
---|
174 | done:
|
---|
175 | m_free(pData, m);
|
---|
176 | }
|
---|
177 |
|
---|
178 | /*
|
---|
179 | * Copy the data from m into sb
|
---|
180 | * The caller is responsible to make sure there's enough room
|
---|
181 | */
|
---|
182 | void
|
---|
183 | sbappendsb(PNATState pData, struct sbuf *sb, struct mbuf *m)
|
---|
184 | {
|
---|
185 | int len, n, nn;
|
---|
186 |
|
---|
187 | #ifndef VBOX_WITH_SLIRP_BSD_MBUF
|
---|
188 | len = m->m_len;
|
---|
189 | #else
|
---|
190 | len = m_length(m, NULL);
|
---|
191 | #endif
|
---|
192 |
|
---|
193 | STAM_COUNTER_INC(&pData->StatIOSBAppendSB);
|
---|
194 | if (sb->sb_wptr < sb->sb_rptr)
|
---|
195 | {
|
---|
196 | STAM_COUNTER_INC(&pData->StatIOSBAppendSB_w_l_r);
|
---|
197 | n = sb->sb_rptr - sb->sb_wptr;
|
---|
198 | if (n > len)
|
---|
199 | n = len;
|
---|
200 | #ifndef VBOX_WITH_SLIRP_BSD_MBUF
|
---|
201 | memcpy(sb->sb_wptr, m->m_data, n);
|
---|
202 | #else
|
---|
203 | m_copydata(m, 0, n, sb->sb_wptr);
|
---|
204 | #endif
|
---|
205 | }
|
---|
206 | else
|
---|
207 | {
|
---|
208 | STAM_COUNTER_INC(&pData->StatIOSBAppendSB_w_ge_r);
|
---|
209 | /* Do the right edge first */
|
---|
210 | n = sb->sb_data + sb->sb_datalen - sb->sb_wptr;
|
---|
211 | if (n > len)
|
---|
212 | n = len;
|
---|
213 | #ifndef VBOX_WITH_SLIRP_BSD_MBUF
|
---|
214 | memcpy(sb->sb_wptr, m->m_data, n);
|
---|
215 | #else
|
---|
216 | m_copydata(m, 0, n, sb->sb_wptr);
|
---|
217 | #endif
|
---|
218 | len -= n;
|
---|
219 | if (len)
|
---|
220 | {
|
---|
221 | /* Now the left edge */
|
---|
222 | nn = sb->sb_rptr - sb->sb_data;
|
---|
223 | if (nn > len)
|
---|
224 | nn = len;
|
---|
225 | #ifndef VBOX_WITH_SLIRP_BSD_MBUF
|
---|
226 | memcpy(sb->sb_data, m->m_data+n, nn);
|
---|
227 | #else
|
---|
228 | m_copydata(m, n, nn, sb->sb_wptr);
|
---|
229 | #endif
|
---|
230 | n += nn;
|
---|
231 | }
|
---|
232 | }
|
---|
233 |
|
---|
234 | sb->sb_cc += n;
|
---|
235 | sb->sb_wptr += n;
|
---|
236 | if (sb->sb_wptr >= sb->sb_data + sb->sb_datalen)
|
---|
237 | {
|
---|
238 | STAM_COUNTER_INC(&pData->StatIOSBAppendSB_w_alter);
|
---|
239 | sb->sb_wptr -= sb->sb_datalen;
|
---|
240 | }
|
---|
241 | }
|
---|
242 |
|
---|
243 | /*
|
---|
244 | * Copy data from sbuf to a normal, straight buffer
|
---|
245 | * Don't update the sbuf rptr, this will be
|
---|
246 | * done in sbdrop when the data is acked
|
---|
247 | */
|
---|
248 | void
|
---|
249 | sbcopy(struct sbuf *sb, int off, int len, char *to)
|
---|
250 | {
|
---|
251 | char *from;
|
---|
252 |
|
---|
253 | from = sb->sb_rptr + off;
|
---|
254 | if (from >= sb->sb_data + sb->sb_datalen)
|
---|
255 | from -= sb->sb_datalen;
|
---|
256 |
|
---|
257 | if (from < sb->sb_wptr)
|
---|
258 | {
|
---|
259 | if (len > sb->sb_cc)
|
---|
260 | len = sb->sb_cc;
|
---|
261 | memcpy(to, from, len);
|
---|
262 | }
|
---|
263 | else
|
---|
264 | {
|
---|
265 | /* re-use off */
|
---|
266 | off = (sb->sb_data + sb->sb_datalen) - from;
|
---|
267 | if (off > len)
|
---|
268 | off = len;
|
---|
269 | memcpy(to, from, off);
|
---|
270 | len -= off;
|
---|
271 | if (len)
|
---|
272 | memcpy(to+off, sb->sb_data, len);
|
---|
273 | }
|
---|
274 | }
|
---|
275 |
|
---|