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 | free(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(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 | */
|
---|
73 | void
|
---|
74 | sbappend(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 | */
|
---|
138 | void
|
---|
139 | sbappendsb(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) n = len;
|
---|
149 | memcpy(sb->sb_wptr, m->m_data, n);
|
---|
150 | }
|
---|
151 | else
|
---|
152 | {
|
---|
153 | /* Do the right edge first */
|
---|
154 | n = sb->sb_data + sb->sb_datalen - sb->sb_wptr;
|
---|
155 | if (n > len) n = len;
|
---|
156 | memcpy(sb->sb_wptr, m->m_data, n);
|
---|
157 | len -= n;
|
---|
158 | if (len)
|
---|
159 | {
|
---|
160 | /* Now the left edge */
|
---|
161 | nn = sb->sb_rptr - sb->sb_data;
|
---|
162 | if (nn > len)
|
---|
163 | nn = len;
|
---|
164 | memcpy(sb->sb_data,m->m_data+n,nn);
|
---|
165 | n += nn;
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | sb->sb_cc += n;
|
---|
170 | sb->sb_wptr += n;
|
---|
171 | if (sb->sb_wptr >= sb->sb_data + sb->sb_datalen)
|
---|
172 | sb->sb_wptr -= sb->sb_datalen;
|
---|
173 | }
|
---|
174 |
|
---|
175 | /*
|
---|
176 | * Copy data from sbuf to a normal, straight buffer
|
---|
177 | * Don't update the sbuf rptr, this will be
|
---|
178 | * done in sbdrop when the data is acked
|
---|
179 | */
|
---|
180 | void
|
---|
181 | sbcopy(struct sbuf *sb, int off, int len, char *to)
|
---|
182 | {
|
---|
183 | char *from;
|
---|
184 |
|
---|
185 | from = sb->sb_rptr + off;
|
---|
186 | if (from >= sb->sb_data + sb->sb_datalen)
|
---|
187 | from -= sb->sb_datalen;
|
---|
188 |
|
---|
189 | if (from < sb->sb_wptr)
|
---|
190 | {
|
---|
191 | if (len > sb->sb_cc) len = sb->sb_cc;
|
---|
192 | memcpy(to,from,len);
|
---|
193 | }
|
---|
194 | else
|
---|
195 | {
|
---|
196 | /* re-use off */
|
---|
197 | off = (sb->sb_data + sb->sb_datalen) - from;
|
---|
198 | if (off > len) off = len;
|
---|
199 | memcpy(to,from,off);
|
---|
200 | len -= off;
|
---|
201 | if (len)
|
---|
202 | memcpy(to+off,sb->sb_data,len);
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|