VirtualBox

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

Last change on this file since 15682 was 15293, checked in by vboxsync, 16 years ago

slirp: style + comments

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