VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/slirp/sbuf.c@ 22984

Last change on this file since 22984 was 22576, checked in by vboxsync, 15 years ago

NAT: logging, warnings

  • Property svn:eol-style set to native
File size: 5.5 KB
Line 
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
18void
19sbfree(struct sbuf *sb)
20{
21 RTMemFree(sb->sb_data);
22}
23
24void
25sbdrop(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
40void
41sbreserve(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 */
75void
76sbappend(PNATState pData, struct socket *so, struct mbuf *m)
77{
78 int ret = 0;
79
80 STAM_PROFILE_START(&pData->StatIOSBAppend_pf, a);
81 DEBUG_CALL("sbappend");
82 DEBUG_ARG("so = %lx", (long)so);
83 DEBUG_ARG("m = %lx", (long)m);
84 DEBUG_ARG("m->m_len = %d", m ? m->m_len : 0);
85
86 STAM_COUNTER_INC(&pData->StatIOSBAppend);
87 /* Shouldn't happen, but... e.g. foreign host closes connection */
88 if (m->m_len <= 0)
89 {
90 STAM_COUNTER_INC(&pData->StatIOSBAppend_zm);
91 goto done;
92 }
93
94 /*
95 * If there is urgent data, call sosendoob
96 * if not all was sent, sowrite will take care of the rest
97 * (The rest of this function is just an optimisation)
98 */
99 if (so->so_urgc)
100 {
101 sbappendsb(pData, &so->so_rcv, m);
102 m_free(pData, m);
103 sosendoob(so);
104 return;
105 }
106
107 /*
108 * We only write if there's nothing in the buffer,
109 * ottherwise it'll arrive out of order, and hence corrupt
110 */
111 if (!so->so_rcv.sb_cc)
112 ret = send(so->s, m->m_data, m->m_len, 0);
113
114 if (ret <= 0)
115 {
116 STAM_COUNTER_INC(&pData->StatIOSBAppend_wf);
117 /*
118 * Nothing was written
119 * It's possible that the socket has closed, but
120 * we don't need to check because if it has closed,
121 * it will be detected in the normal way by soread()
122 */
123 sbappendsb(pData, &so->so_rcv, m);
124 STAM_PROFILE_STOP(&pData->StatIOSBAppend_pf_wf, a);
125 goto done;
126 }
127 else if (ret != m->m_len)
128 {
129 STAM_COUNTER_INC(&pData->StatIOSBAppend_wp);
130 /*
131 * Something was written, but not everything..
132 * sbappendsb the rest
133 */
134 m->m_len -= ret;
135 m->m_data += ret;
136 sbappendsb(pData, &so->so_rcv, m);
137 STAM_PROFILE_STOP(&pData->StatIOSBAppend_pf_wp, a);
138 goto done;
139 } /* else */
140 /* Whatever happened, we free the mbuf */
141 STAM_COUNTER_INC(&pData->StatIOSBAppend_wa);
142 STAM_PROFILE_STOP(&pData->StatIOSBAppend_pf_wa, a);
143done:
144 m_free(pData, m);
145}
146
147/*
148 * Copy the data from m into sb
149 * The caller is responsible to make sure there's enough room
150 */
151void
152sbappendsb(PNATState pData, struct sbuf *sb, struct mbuf *m)
153{
154 int len, n, nn;
155
156 len = m->m_len;
157
158 STAM_COUNTER_INC(&pData->StatIOSBAppendSB);
159 if (sb->sb_wptr < sb->sb_rptr)
160 {
161 STAM_COUNTER_INC(&pData->StatIOSBAppendSB_w_l_r);
162 n = sb->sb_rptr - sb->sb_wptr;
163 if (n > len)
164 n = len;
165 memcpy(sb->sb_wptr, m->m_data, n);
166 }
167 else
168 {
169 STAM_COUNTER_INC(&pData->StatIOSBAppendSB_w_ge_r);
170 /* Do the right edge first */
171 n = sb->sb_data + sb->sb_datalen - sb->sb_wptr;
172 if (n > len)
173 n = len;
174 memcpy(sb->sb_wptr, m->m_data, n);
175 len -= n;
176 if (len)
177 {
178 /* Now the left edge */
179 nn = sb->sb_rptr - sb->sb_data;
180 if (nn > len)
181 nn = len;
182 memcpy(sb->sb_data, m->m_data+n, nn);
183 n += nn;
184 }
185 }
186
187 sb->sb_cc += n;
188 sb->sb_wptr += n;
189 if (sb->sb_wptr >= sb->sb_data + sb->sb_datalen)
190 {
191 STAM_COUNTER_INC(&pData->StatIOSBAppendSB_w_alter);
192 sb->sb_wptr -= sb->sb_datalen;
193 }
194}
195
196/*
197 * Copy data from sbuf to a normal, straight buffer
198 * Don't update the sbuf rptr, this will be
199 * done in sbdrop when the data is acked
200 */
201void
202sbcopy(struct sbuf *sb, int off, int len, char *to)
203{
204 char *from;
205
206 from = sb->sb_rptr + off;
207 if (from >= sb->sb_data + sb->sb_datalen)
208 from -= sb->sb_datalen;
209
210 if (from < sb->sb_wptr)
211 {
212 if (len > sb->sb_cc)
213 len = sb->sb_cc;
214 memcpy(to, from, len);
215 }
216 else
217 {
218 /* re-use off */
219 off = (sb->sb_data + sb->sb_datalen) - from;
220 if (off > len)
221 off = len;
222 memcpy(to, from, off);
223 len -= off;
224 if (len)
225 memcpy(to+off, sb->sb_data, len);
226 }
227}
228
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette