VirtualBox

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

最後變更 在這個檔案從11284是 1076,由 vboxsync 提交於 18 年 前

Removed tons of ifdef VBOX conditionals to make slirp readable again

  • 屬性 svn:eol-style 設為 native
檔案大小: 4.0 KB
 
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(sb)
20 struct sbuf *sb;
21{
22 free(sb->sb_data);
23}
24
25void
26sbdrop(sb, num)
27 struct sbuf *sb;
28 int num;
29{
30 /*
31 * We can only drop how much we have
32 * This should never succeed
33 */
34 if(num > sb->sb_cc)
35 num = sb->sb_cc;
36 sb->sb_cc -= num;
37 sb->sb_rptr += num;
38 if(sb->sb_rptr >= sb->sb_data + sb->sb_datalen)
39 sb->sb_rptr -= sb->sb_datalen;
40
41}
42
43void
44sbreserve(sb, size)
45 struct sbuf *sb;
46 int size;
47{
48 if (sb->sb_data) {
49 /* Already alloced, realloc if necessary */
50 if (sb->sb_datalen != size) {
51 sb->sb_wptr = sb->sb_rptr = sb->sb_data = (char *)realloc(sb->sb_data, size);
52 sb->sb_cc = 0;
53 if (sb->sb_wptr)
54 sb->sb_datalen = size;
55 else
56 sb->sb_datalen = 0;
57 }
58 } else {
59 sb->sb_wptr = sb->sb_rptr = sb->sb_data = (char *)malloc(size);
60 sb->sb_cc = 0;
61 if (sb->sb_wptr)
62 sb->sb_datalen = size;
63 else
64 sb->sb_datalen = 0;
65 }
66}
67
68/*
69 * Try and write() to the socket, whatever doesn't get written
70 * append to the buffer... for a host with a fast net connection,
71 * this prevents an unnecessary copy of the data
72 * (the socket is non-blocking, so we won't hang)
73 */
74void
75sbappend(PNATState pData, struct socket *so, struct mbuf *m)
76{
77 int ret = 0;
78
79 DEBUG_CALL("sbappend");
80 DEBUG_ARG("so = %lx", (long)so);
81 DEBUG_ARG("m = %lx", (long)m);
82 DEBUG_ARG("m->m_len = %d", m->m_len);
83
84 /* Shouldn't happen, but... e.g. foreign host closes connection */
85 if (m->m_len <= 0) {
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 sbappendsb(&so->so_rcv, m);
97 m_free(pData, m);
98 sosendoob(so);
99 return;
100 }
101
102 /*
103 * We only write if there's nothing in the buffer,
104 * ottherwise it'll arrive out of order, and hence corrupt
105 */
106 if (!so->so_rcv.sb_cc)
107 ret = send(so->s, m->m_data, m->m_len, 0);
108
109 if (ret <= 0) {
110 /*
111 * Nothing was written
112 * It's possible that the socket has closed, but
113 * we don't need to check because if it has closed,
114 * it will be detected in the normal way by soread()
115 */
116 sbappendsb(&so->so_rcv, m);
117 } else if (ret != m->m_len) {
118 /*
119 * Something was written, but not everything..
120 * sbappendsb the rest
121 */
122 m->m_len -= ret;
123 m->m_data += ret;
124 sbappendsb(&so->so_rcv, m);
125 } /* else */
126 /* Whatever happened, we free the mbuf */
127 m_free(pData, m);
128}
129
130/*
131 * Copy the data from m into sb
132 * The caller is responsible to make sure there's enough room
133 */
134void
135sbappendsb(sb, m)
136 struct sbuf *sb;
137 struct mbuf *m;
138{
139 int len, n, nn;
140
141 len = m->m_len;
142
143 if (sb->sb_wptr < sb->sb_rptr) {
144 n = sb->sb_rptr - sb->sb_wptr;
145 if (n > len) n = len;
146 memcpy(sb->sb_wptr, m->m_data, n);
147 } else {
148 /* Do the right edge first */
149 n = sb->sb_data + sb->sb_datalen - sb->sb_wptr;
150 if (n > len) n = len;
151 memcpy(sb->sb_wptr, m->m_data, n);
152 len -= n;
153 if (len) {
154 /* Now the left edge */
155 nn = sb->sb_rptr - sb->sb_data;
156 if (nn > len) nn = len;
157 memcpy(sb->sb_data,m->m_data+n,nn);
158 n += nn;
159 }
160 }
161
162 sb->sb_cc += n;
163 sb->sb_wptr += n;
164 if (sb->sb_wptr >= sb->sb_data + sb->sb_datalen)
165 sb->sb_wptr -= sb->sb_datalen;
166}
167
168/*
169 * Copy data from sbuf to a normal, straight buffer
170 * Don't update the sbuf rptr, this will be
171 * done in sbdrop when the data is acked
172 */
173void
174sbcopy(sb, off, len, to)
175 struct sbuf *sb;
176 int off;
177 int len;
178 char *to;
179{
180 char *from;
181
182 from = sb->sb_rptr + off;
183 if (from >= sb->sb_data + sb->sb_datalen)
184 from -= sb->sb_datalen;
185
186 if (from < sb->sb_wptr) {
187 if (len > sb->sb_cc) len = sb->sb_cc;
188 memcpy(to,from,len);
189 } else {
190 /* re-use off */
191 off = (sb->sb_data + sb->sb_datalen) - from;
192 if (off > len) off = len;
193 memcpy(to,from,off);
194 len -= off;
195 if (len)
196 memcpy(to+off,sb->sb_data,len);
197 }
198}
199
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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