VirtualBox

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

Last change on this file since 16684 was 15791, checked in by vboxsync, 16 years ago

NAT: malloc/free/realloc/strdup => iprt functions; fixed VBOX_WITH_SLIRP_MEMORY_CHECK

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