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 | #define WANT_SYS_IOCTL_H
|
---|
9 | #include <slirp.h>
|
---|
10 | #include "ip_icmp.h"
|
---|
11 | #include "main.h"
|
---|
12 | #ifdef __sun__
|
---|
13 | #include <sys/filio.h>
|
---|
14 | #endif
|
---|
15 |
|
---|
16 | void
|
---|
17 | so_init()
|
---|
18 | {
|
---|
19 | /* Nothing yet */
|
---|
20 | }
|
---|
21 |
|
---|
22 |
|
---|
23 | struct socket *
|
---|
24 | solookup(head, laddr, lport, faddr, fport)
|
---|
25 | struct socket *head;
|
---|
26 | struct in_addr laddr;
|
---|
27 | u_int lport;
|
---|
28 | struct in_addr faddr;
|
---|
29 | u_int fport;
|
---|
30 | {
|
---|
31 | struct socket *so;
|
---|
32 |
|
---|
33 | for (so = head->so_next; so != head; so = so->so_next) {
|
---|
34 | if (so->so_lport == lport &&
|
---|
35 | so->so_laddr.s_addr == laddr.s_addr &&
|
---|
36 | so->so_faddr.s_addr == faddr.s_addr &&
|
---|
37 | so->so_fport == fport)
|
---|
38 | break;
|
---|
39 | }
|
---|
40 |
|
---|
41 | if (so == head)
|
---|
42 | return (struct socket *)NULL;
|
---|
43 | return so;
|
---|
44 |
|
---|
45 | }
|
---|
46 |
|
---|
47 | /*
|
---|
48 | * Create a new socket, initialise the fields
|
---|
49 | * It is the responsibility of the caller to
|
---|
50 | * insque() it into the correct linked-list
|
---|
51 | */
|
---|
52 | struct socket *
|
---|
53 | socreate()
|
---|
54 | {
|
---|
55 | struct socket *so;
|
---|
56 |
|
---|
57 | so = (struct socket *)malloc(sizeof(struct socket));
|
---|
58 | if(so) {
|
---|
59 | memset(so, 0, sizeof(struct socket));
|
---|
60 | so->so_state = SS_NOFDREF;
|
---|
61 | so->s = -1;
|
---|
62 | #if defined(VBOX_WITH_SIMPLEFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
|
---|
63 | so->hNetworkEvent = WSACreateEvent(); /*XXX: NOT correct place*/
|
---|
64 | AssertRelease(so->hNetworkEvent != WSA_INVALID_EVENT);
|
---|
65 | #endif
|
---|
66 | }
|
---|
67 | return(so);
|
---|
68 | }
|
---|
69 |
|
---|
70 | /*
|
---|
71 | * remque and free a socket, clobber cache
|
---|
72 | */
|
---|
73 | void
|
---|
74 | sofree(PNATState pData, struct socket *so)
|
---|
75 | {
|
---|
76 | if (so->so_emu==EMU_RSH && so->extra) {
|
---|
77 | sofree(pData, so->extra);
|
---|
78 | so->extra=NULL;
|
---|
79 | }
|
---|
80 | if (so == tcp_last_so)
|
---|
81 | tcp_last_so = &tcb;
|
---|
82 | else if (so == udp_last_so)
|
---|
83 | udp_last_so = &udb;
|
---|
84 |
|
---|
85 | m_free(pData, so->so_m);
|
---|
86 |
|
---|
87 | #if defined(VBOX_WITH_SIMPLEFIED_SLIRP_SYNC) && defined(RT_OS_WINDOWS)
|
---|
88 | WSACloseEvent(so->hNetworkEvent); /*XXX: NOT correct place*/
|
---|
89 | #endif
|
---|
90 |
|
---|
91 | if(so->so_next && so->so_prev)
|
---|
92 | remque(pData, so); /* crashes if so is not in a queue */
|
---|
93 |
|
---|
94 | free(so);
|
---|
95 | }
|
---|
96 |
|
---|
97 | /*
|
---|
98 | * Read from so's socket into sb_snd, updating all relevant sbuf fields
|
---|
99 | * NOTE: This will only be called if it is select()ed for reading, so
|
---|
100 | * a read() of 0 (or less) means it's disconnected
|
---|
101 | */
|
---|
102 | int
|
---|
103 | soread(PNATState pData, struct socket *so)
|
---|
104 | {
|
---|
105 | int n, nn, lss, total;
|
---|
106 | struct sbuf *sb = &so->so_snd;
|
---|
107 | int len = sb->sb_datalen - sb->sb_cc;
|
---|
108 | struct iovec iov[2];
|
---|
109 | int mss = so->so_tcpcb->t_maxseg;
|
---|
110 |
|
---|
111 | DEBUG_CALL("soread");
|
---|
112 | DEBUG_ARG("so = %lx", (long )so);
|
---|
113 |
|
---|
114 | /*
|
---|
115 | * No need to check if there's enough room to read.
|
---|
116 | * soread wouldn't have been called if there weren't
|
---|
117 | */
|
---|
118 |
|
---|
119 | len = sb->sb_datalen - sb->sb_cc;
|
---|
120 |
|
---|
121 | iov[0].iov_base = sb->sb_wptr;
|
---|
122 | iov[1].iov_base = 0;
|
---|
123 | iov[1].iov_len = 0;
|
---|
124 | if (sb->sb_wptr < sb->sb_rptr) {
|
---|
125 | iov[0].iov_len = sb->sb_rptr - sb->sb_wptr;
|
---|
126 | /* Should never succeed, but... */
|
---|
127 | if (iov[0].iov_len > len)
|
---|
128 | iov[0].iov_len = len;
|
---|
129 | if (iov[0].iov_len > mss)
|
---|
130 | iov[0].iov_len -= iov[0].iov_len%mss;
|
---|
131 | n = 1;
|
---|
132 | } else {
|
---|
133 | iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_wptr;
|
---|
134 | /* Should never succeed, but... */
|
---|
135 | if (iov[0].iov_len > len) iov[0].iov_len = len;
|
---|
136 | len -= iov[0].iov_len;
|
---|
137 | if (len) {
|
---|
138 | iov[1].iov_base = sb->sb_data;
|
---|
139 | iov[1].iov_len = sb->sb_rptr - sb->sb_data;
|
---|
140 | if(iov[1].iov_len > len)
|
---|
141 | iov[1].iov_len = len;
|
---|
142 | total = iov[0].iov_len + iov[1].iov_len;
|
---|
143 | if (total > mss) {
|
---|
144 | lss = total%mss;
|
---|
145 | if (iov[1].iov_len > lss) {
|
---|
146 | iov[1].iov_len -= lss;
|
---|
147 | n = 2;
|
---|
148 | } else {
|
---|
149 | lss -= iov[1].iov_len;
|
---|
150 | iov[0].iov_len -= lss;
|
---|
151 | n = 1;
|
---|
152 | }
|
---|
153 | } else
|
---|
154 | n = 2;
|
---|
155 | } else {
|
---|
156 | if (iov[0].iov_len > mss)
|
---|
157 | iov[0].iov_len -= iov[0].iov_len%mss;
|
---|
158 | n = 1;
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | #ifdef HAVE_READV
|
---|
163 | nn = readv(so->s, (struct iovec *)iov, n);
|
---|
164 | DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
|
---|
165 | #else
|
---|
166 | nn = recv(so->s, iov[0].iov_base, iov[0].iov_len,0);
|
---|
167 | #endif
|
---|
168 | if (nn <= 0) {
|
---|
169 | if (nn < 0 && (errno == EINTR || errno == EAGAIN))
|
---|
170 | return 0;
|
---|
171 | else {
|
---|
172 | DEBUG_MISC((dfd, " --- soread() disconnected, nn = %d, errno = %d-%s\n", nn, errno,strerror(errno)));
|
---|
173 | sofcantrcvmore(so);
|
---|
174 | tcp_sockclosed(pData, sototcpcb(so));
|
---|
175 | return -1;
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 | #ifndef HAVE_READV
|
---|
180 | /*
|
---|
181 | * If there was no error, try and read the second time round
|
---|
182 | * We read again if n = 2 (ie, there's another part of the buffer)
|
---|
183 | * and we read as much as we could in the first read
|
---|
184 | * We don't test for <= 0 this time, because there legitimately
|
---|
185 | * might not be any more data (since the socket is non-blocking),
|
---|
186 | * a close will be detected on next iteration.
|
---|
187 | * A return of -1 wont (shouldn't) happen, since it didn't happen above
|
---|
188 | */
|
---|
189 | if (n == 2 && nn == iov[0].iov_len) {
|
---|
190 | int ret;
|
---|
191 | ret = recv(so->s, iov[1].iov_base, iov[1].iov_len,0);
|
---|
192 | if (ret > 0)
|
---|
193 | nn += ret;
|
---|
194 | }
|
---|
195 |
|
---|
196 | DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
|
---|
197 | #endif
|
---|
198 |
|
---|
199 | /* Update fields */
|
---|
200 | sb->sb_cc += nn;
|
---|
201 | sb->sb_wptr += nn;
|
---|
202 | if (sb->sb_wptr >= (sb->sb_data + sb->sb_datalen))
|
---|
203 | sb->sb_wptr -= sb->sb_datalen;
|
---|
204 | return nn;
|
---|
205 | }
|
---|
206 |
|
---|
207 | /*
|
---|
208 | * Get urgent data
|
---|
209 | *
|
---|
210 | * When the socket is created, we set it SO_OOBINLINE,
|
---|
211 | * so when OOB data arrives, we soread() it and everything
|
---|
212 | * in the send buffer is sent as urgent data
|
---|
213 | */
|
---|
214 | void
|
---|
215 | sorecvoob(PNATState pData, struct socket *so)
|
---|
216 | {
|
---|
217 | struct tcpcb *tp = sototcpcb(so);
|
---|
218 |
|
---|
219 | DEBUG_CALL("sorecvoob");
|
---|
220 | DEBUG_ARG("so = %lx", (long)so);
|
---|
221 |
|
---|
222 | /*
|
---|
223 | * We take a guess at how much urgent data has arrived.
|
---|
224 | * In most situations, when urgent data arrives, the next
|
---|
225 | * read() should get all the urgent data. This guess will
|
---|
226 | * be wrong however if more data arrives just after the
|
---|
227 | * urgent data, or the read() doesn't return all the
|
---|
228 | * urgent data.
|
---|
229 | */
|
---|
230 | soread(pData, so);
|
---|
231 | tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
|
---|
232 | tp->t_force = 1;
|
---|
233 | tcp_output(pData, tp);
|
---|
234 | tp->t_force = 0;
|
---|
235 | }
|
---|
236 |
|
---|
237 | /*
|
---|
238 | * Send urgent data
|
---|
239 | * There's a lot duplicated code here, but...
|
---|
240 | */
|
---|
241 | int
|
---|
242 | sosendoob(so)
|
---|
243 | struct socket *so;
|
---|
244 | {
|
---|
245 | struct sbuf *sb = &so->so_rcv;
|
---|
246 | char buff[2048]; /* XXX Shouldn't be sending more oob data than this */
|
---|
247 |
|
---|
248 | int n, len;
|
---|
249 |
|
---|
250 | DEBUG_CALL("sosendoob");
|
---|
251 | DEBUG_ARG("so = %lx", (long)so);
|
---|
252 | DEBUG_ARG("sb->sb_cc = %d", sb->sb_cc);
|
---|
253 |
|
---|
254 | if (so->so_urgc > 2048)
|
---|
255 | so->so_urgc = 2048; /* XXXX */
|
---|
256 |
|
---|
257 | if (sb->sb_rptr < sb->sb_wptr) {
|
---|
258 | /* We can send it directly */
|
---|
259 | n = send(so->s, sb->sb_rptr, so->so_urgc, (MSG_OOB)); /* |MSG_DONTWAIT)); */
|
---|
260 | so->so_urgc -= n;
|
---|
261 |
|
---|
262 | DEBUG_MISC((dfd, " --- sent %d bytes urgent data, %d urgent bytes left\n", n, so->so_urgc));
|
---|
263 | } else {
|
---|
264 | /*
|
---|
265 | * Since there's no sendv or sendtov like writev,
|
---|
266 | * we must copy all data to a linear buffer then
|
---|
267 | * send it all
|
---|
268 | */
|
---|
269 | len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
|
---|
270 | if (len > so->so_urgc) len = so->so_urgc;
|
---|
271 | memcpy(buff, sb->sb_rptr, len);
|
---|
272 | so->so_urgc -= len;
|
---|
273 | if (so->so_urgc) {
|
---|
274 | n = sb->sb_wptr - sb->sb_data;
|
---|
275 | if (n > so->so_urgc) n = so->so_urgc;
|
---|
276 | memcpy((buff + len), sb->sb_data, n);
|
---|
277 | so->so_urgc -= n;
|
---|
278 | len += n;
|
---|
279 | }
|
---|
280 | n = send(so->s, buff, len, (MSG_OOB)); /* |MSG_DONTWAIT)); */
|
---|
281 | #ifdef DEBUG
|
---|
282 | if (n != len)
|
---|
283 | DEBUG_ERROR((dfd, "Didn't send all data urgently XXXXX\n"));
|
---|
284 | #endif
|
---|
285 | DEBUG_MISC((dfd, " ---2 sent %d bytes urgent data, %d urgent bytes left\n", n, so->so_urgc));
|
---|
286 | }
|
---|
287 |
|
---|
288 | sb->sb_cc -= n;
|
---|
289 | sb->sb_rptr += n;
|
---|
290 | if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
|
---|
291 | sb->sb_rptr -= sb->sb_datalen;
|
---|
292 |
|
---|
293 | return n;
|
---|
294 | }
|
---|
295 |
|
---|
296 | /*
|
---|
297 | * Write data from so_rcv to so's socket,
|
---|
298 | * updating all sbuf field as necessary
|
---|
299 | */
|
---|
300 | int
|
---|
301 | sowrite(PNATState pData, struct socket *so)
|
---|
302 | {
|
---|
303 | int n,nn;
|
---|
304 | struct sbuf *sb = &so->so_rcv;
|
---|
305 | int len = sb->sb_cc;
|
---|
306 | struct iovec iov[2];
|
---|
307 |
|
---|
308 | DEBUG_CALL("sowrite");
|
---|
309 | DEBUG_ARG("so = %lx", (long)so);
|
---|
310 |
|
---|
311 | if (so->so_urgc) {
|
---|
312 | sosendoob(so);
|
---|
313 | if (sb->sb_cc == 0)
|
---|
314 | return 0;
|
---|
315 | }
|
---|
316 |
|
---|
317 | /*
|
---|
318 | * No need to check if there's something to write,
|
---|
319 | * sowrite wouldn't have been called otherwise
|
---|
320 | */
|
---|
321 |
|
---|
322 | len = sb->sb_cc;
|
---|
323 |
|
---|
324 | iov[0].iov_base = sb->sb_rptr;
|
---|
325 | iov[1].iov_base = 0;
|
---|
326 | iov[1].iov_len = 0;
|
---|
327 | if (sb->sb_rptr < sb->sb_wptr) {
|
---|
328 | iov[0].iov_len = sb->sb_wptr - sb->sb_rptr;
|
---|
329 | /* Should never succeed, but... */
|
---|
330 | if (iov[0].iov_len > len) iov[0].iov_len = len;
|
---|
331 | n = 1;
|
---|
332 | } else {
|
---|
333 | iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
|
---|
334 | if (iov[0].iov_len > len) iov[0].iov_len = len;
|
---|
335 | len -= iov[0].iov_len;
|
---|
336 | if (len) {
|
---|
337 | iov[1].iov_base = sb->sb_data;
|
---|
338 | iov[1].iov_len = sb->sb_wptr - sb->sb_data;
|
---|
339 | if (iov[1].iov_len > len) iov[1].iov_len = len;
|
---|
340 | n = 2;
|
---|
341 | } else
|
---|
342 | n = 1;
|
---|
343 | }
|
---|
344 | /* Check if there's urgent data to send, and if so, send it */
|
---|
345 |
|
---|
346 | #ifdef HAVE_READV
|
---|
347 | nn = writev(so->s, (const struct iovec *)iov, n);
|
---|
348 |
|
---|
349 | DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", nn));
|
---|
350 | #else
|
---|
351 | nn = send(so->s, iov[0].iov_base, iov[0].iov_len,0);
|
---|
352 | #endif
|
---|
353 | /* This should never happen, but people tell me it does *shrug* */
|
---|
354 | if (nn < 0 && (errno == EAGAIN || errno == EINTR))
|
---|
355 | return 0;
|
---|
356 |
|
---|
357 | if (nn <= 0) {
|
---|
358 | DEBUG_MISC((dfd, " --- sowrite disconnected, so->so_state = %x, errno = %d\n",
|
---|
359 | so->so_state, errno));
|
---|
360 | sofcantsendmore(so);
|
---|
361 | tcp_sockclosed(pData, sototcpcb(so));
|
---|
362 | return -1;
|
---|
363 | }
|
---|
364 |
|
---|
365 | #ifndef HAVE_READV
|
---|
366 | if (n == 2 && nn == iov[0].iov_len) {
|
---|
367 | int ret;
|
---|
368 | ret = send(so->s, iov[1].iov_base, iov[1].iov_len,0);
|
---|
369 | if (ret > 0)
|
---|
370 | nn += ret;
|
---|
371 | }
|
---|
372 | DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", nn));
|
---|
373 | #endif
|
---|
374 |
|
---|
375 | /* Update sbuf */
|
---|
376 | sb->sb_cc -= nn;
|
---|
377 | sb->sb_rptr += nn;
|
---|
378 | if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
|
---|
379 | sb->sb_rptr -= sb->sb_datalen;
|
---|
380 |
|
---|
381 | /*
|
---|
382 | * If in DRAIN mode, and there's no more data, set
|
---|
383 | * it CANTSENDMORE
|
---|
384 | */
|
---|
385 | if ((so->so_state & SS_FWDRAIN) && sb->sb_cc == 0)
|
---|
386 | sofcantsendmore(so);
|
---|
387 |
|
---|
388 | return nn;
|
---|
389 | }
|
---|
390 |
|
---|
391 | /*
|
---|
392 | * recvfrom() a UDP socket
|
---|
393 | */
|
---|
394 | void
|
---|
395 | sorecvfrom(PNATState pData, struct socket *so)
|
---|
396 | {
|
---|
397 | struct sockaddr_in addr;
|
---|
398 | socklen_t addrlen = sizeof(struct sockaddr_in);
|
---|
399 |
|
---|
400 | DEBUG_CALL("sorecvfrom");
|
---|
401 | DEBUG_ARG("so = %lx", (long)so);
|
---|
402 |
|
---|
403 | if (so->so_type == IPPROTO_ICMP) { /* This is a "ping" reply */
|
---|
404 | char buff[256];
|
---|
405 | int len;
|
---|
406 |
|
---|
407 | len = recvfrom(so->s, buff, 256, 0,
|
---|
408 | (struct sockaddr *)&addr, &addrlen);
|
---|
409 | /* XXX Check if reply is "correct"? */
|
---|
410 |
|
---|
411 | if(len == -1 || len == 0) {
|
---|
412 | u_char code=ICMP_UNREACH_PORT;
|
---|
413 |
|
---|
414 | if(errno == EHOSTUNREACH) code=ICMP_UNREACH_HOST;
|
---|
415 | else if(errno == ENETUNREACH) code=ICMP_UNREACH_NET;
|
---|
416 |
|
---|
417 | DEBUG_MISC((dfd," udp icmp rx errno = %d-%s\n",
|
---|
418 | errno,strerror(errno)));
|
---|
419 | icmp_error(pData, so->so_m, ICMP_UNREACH,code, 0,strerror(errno));
|
---|
420 | } else {
|
---|
421 | icmp_reflect(pData, so->so_m);
|
---|
422 | so->so_m = 0; /* Don't m_free() it again! */
|
---|
423 | }
|
---|
424 | /* No need for this socket anymore, udp_detach it */
|
---|
425 | udp_detach(pData, so);
|
---|
426 | } else { /* A "normal" UDP packet */
|
---|
427 | struct mbuf *m;
|
---|
428 | int len, n;
|
---|
429 |
|
---|
430 | if (!(m = m_get(pData))) return;
|
---|
431 | m->m_data += if_maxlinkhdr;
|
---|
432 |
|
---|
433 | /*
|
---|
434 | * XXX Shouldn't FIONREAD packets destined for port 53,
|
---|
435 | * but I don't know the max packet size for DNS lookups
|
---|
436 | */
|
---|
437 | len = M_FREEROOM(m);
|
---|
438 | /* if (so->so_fport != htons(53)) { */
|
---|
439 | ioctlsocket(so->s, FIONREAD, &n);
|
---|
440 |
|
---|
441 | if (n > len) {
|
---|
442 | n = (m->m_data - m->m_dat) + m->m_len + n + 1;
|
---|
443 | m_inc(m, n);
|
---|
444 | len = M_FREEROOM(m);
|
---|
445 | }
|
---|
446 | /* } */
|
---|
447 |
|
---|
448 | m->m_len = recvfrom(so->s, m->m_data, len, 0,
|
---|
449 | (struct sockaddr *)&addr, &addrlen);
|
---|
450 | DEBUG_MISC((dfd, " did recvfrom %d, errno = %d-%s\n",
|
---|
451 | m->m_len, errno,strerror(errno)));
|
---|
452 | if(m->m_len<0) {
|
---|
453 | u_char code=ICMP_UNREACH_PORT;
|
---|
454 |
|
---|
455 | if(errno == EHOSTUNREACH) code=ICMP_UNREACH_HOST;
|
---|
456 | else if(errno == ENETUNREACH) code=ICMP_UNREACH_NET;
|
---|
457 |
|
---|
458 | DEBUG_MISC((dfd," rx error, tx icmp ICMP_UNREACH:%i\n", code));
|
---|
459 | icmp_error(pData, so->so_m, ICMP_UNREACH,code, 0,strerror(errno));
|
---|
460 | m_free(pData, m);
|
---|
461 | } else {
|
---|
462 | /*
|
---|
463 | * Hack: domain name lookup will be used the most for UDP,
|
---|
464 | * and since they'll only be used once there's no need
|
---|
465 | * for the 4 minute (or whatever) timeout... So we time them
|
---|
466 | * out much quicker (10 seconds for now...)
|
---|
467 | */
|
---|
468 | if (so->so_expire) {
|
---|
469 | if (so->so_fport == htons(53))
|
---|
470 | so->so_expire = curtime + SO_EXPIREFAST;
|
---|
471 | else
|
---|
472 | so->so_expire = curtime + SO_EXPIRE;
|
---|
473 | }
|
---|
474 |
|
---|
475 | /* if (m->m_len == len) {
|
---|
476 | * m_inc(m, MINCSIZE);
|
---|
477 | * m->m_len = 0;
|
---|
478 | * }
|
---|
479 | */
|
---|
480 |
|
---|
481 | /*
|
---|
482 | * If this packet was destined for CTL_ADDR,
|
---|
483 | * make it look like that's where it came from, done by udp_output
|
---|
484 | */
|
---|
485 | udp_output(pData, so, m, &addr);
|
---|
486 | } /* rx error */
|
---|
487 | } /* if ping packet */
|
---|
488 | }
|
---|
489 |
|
---|
490 | /*
|
---|
491 | * sendto() a socket
|
---|
492 | */
|
---|
493 | int
|
---|
494 | sosendto(PNATState pData, struct socket *so, struct mbuf *m)
|
---|
495 | {
|
---|
496 | int ret;
|
---|
497 | struct sockaddr_in addr;
|
---|
498 | #if 0
|
---|
499 | struct sockaddr_in host_addr;
|
---|
500 | #endif
|
---|
501 |
|
---|
502 | DEBUG_CALL("sosendto");
|
---|
503 | DEBUG_ARG("so = %lx", (long)so);
|
---|
504 | DEBUG_ARG("m = %lx", (long)m);
|
---|
505 |
|
---|
506 | addr.sin_family = AF_INET;
|
---|
507 | if ((so->so_faddr.s_addr & htonl(pData->netmask)) == special_addr.s_addr) {
|
---|
508 | /* It's an alias */
|
---|
509 | uint32_t last_byte = ntohl(so->so_faddr.s_addr) & ~pData->netmask;
|
---|
510 | switch(last_byte) {
|
---|
511 | #if 0
|
---|
512 | /* handle this case at 'default:' */
|
---|
513 | case CTL_BROADCAST:
|
---|
514 | addr.sin_addr.s_addr = INADDR_BROADCAST;
|
---|
515 | # if 0
|
---|
516 | /* Send the packet to host to fully emulate broadcast */
|
---|
517 | /** @todo r=klaus: on Linux host this causes the host to receive
|
---|
518 | * the packet twice for some reason. And I cannot find any place
|
---|
519 | * in the man pages which states that sending a broadcast does not
|
---|
520 | * reach the host itself. */
|
---|
521 | host_addr.sin_family = AF_INET;
|
---|
522 | host_addr.sin_port = so->so_fport;
|
---|
523 | host_addr.sin_addr = our_addr;
|
---|
524 | sendto(so->s, m->m_data, m->m_len, 0,
|
---|
525 | (struct sockaddr *)&host_addr, sizeof (struct sockaddr));
|
---|
526 | # endif
|
---|
527 | break;
|
---|
528 | #endif
|
---|
529 | case CTL_DNS:
|
---|
530 | if (!get_dns_addr(pData, &dns_addr))
|
---|
531 | addr.sin_addr = dns_addr;
|
---|
532 | else
|
---|
533 | addr.sin_addr = loopback_addr;
|
---|
534 | break;
|
---|
535 | case CTL_ALIAS:
|
---|
536 | default:
|
---|
537 | if (last_byte == ~pData->netmask)
|
---|
538 | addr.sin_addr.s_addr = INADDR_BROADCAST;
|
---|
539 | else
|
---|
540 | addr.sin_addr = loopback_addr;
|
---|
541 | break;
|
---|
542 | }
|
---|
543 | } else
|
---|
544 | addr.sin_addr = so->so_faddr;
|
---|
545 | addr.sin_port = so->so_fport;
|
---|
546 |
|
---|
547 | DEBUG_MISC((dfd, " sendto()ing, addr.sin_port=%d, addr.sin_addr.s_addr=%.16s\n", ntohs(addr.sin_port), inet_ntoa(addr.sin_addr)));
|
---|
548 |
|
---|
549 | /* Don't care what port we get */
|
---|
550 | ret = sendto(so->s, m->m_data, m->m_len, 0,
|
---|
551 | (struct sockaddr *)&addr, sizeof (struct sockaddr));
|
---|
552 | if (ret < 0)
|
---|
553 | return -1;
|
---|
554 |
|
---|
555 | /*
|
---|
556 | * Kill the socket if there's no reply in 4 minutes,
|
---|
557 | * but only if it's an expirable socket
|
---|
558 | */
|
---|
559 | if (so->so_expire)
|
---|
560 | so->so_expire = curtime + SO_EXPIRE;
|
---|
561 | so->so_state = SS_ISFCONNECTED; /* So that it gets select()ed */
|
---|
562 | return 0;
|
---|
563 | }
|
---|
564 |
|
---|
565 | /*
|
---|
566 | * XXX This should really be tcp_listen
|
---|
567 | */
|
---|
568 | struct socket *
|
---|
569 | solisten(PNATState pData, u_int port, u_int32_t laddr, u_int lport, int flags)
|
---|
570 | {
|
---|
571 | struct sockaddr_in addr;
|
---|
572 | struct socket *so;
|
---|
573 | socklen_t addrlen = sizeof(addr);
|
---|
574 | int s, opt = 1;
|
---|
575 |
|
---|
576 | DEBUG_CALL("solisten");
|
---|
577 | DEBUG_ARG("port = %d", port);
|
---|
578 | DEBUG_ARG("laddr = %x", laddr);
|
---|
579 | DEBUG_ARG("lport = %d", lport);
|
---|
580 | DEBUG_ARG("flags = %x", flags);
|
---|
581 |
|
---|
582 | if ((so = socreate()) == NULL) {
|
---|
583 | /* free(so); Not sofree() ??? free(NULL) == NOP */
|
---|
584 | return NULL;
|
---|
585 | }
|
---|
586 |
|
---|
587 | /* Don't tcp_attach... we don't need so_snd nor so_rcv */
|
---|
588 | if ((so->so_tcpcb = tcp_newtcpcb(pData, so)) == NULL) {
|
---|
589 | free(so);
|
---|
590 | return NULL;
|
---|
591 | }
|
---|
592 | insque(pData, so,&tcb);
|
---|
593 |
|
---|
594 | /*
|
---|
595 | * SS_FACCEPTONCE sockets must time out.
|
---|
596 | */
|
---|
597 | if (flags & SS_FACCEPTONCE)
|
---|
598 | so->so_tcpcb->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT*2;
|
---|
599 |
|
---|
600 | so->so_state = (SS_FACCEPTCONN|flags);
|
---|
601 | so->so_lport = lport; /* Kept in network format */
|
---|
602 | so->so_laddr.s_addr = laddr; /* Ditto */
|
---|
603 |
|
---|
604 | addr.sin_family = AF_INET;
|
---|
605 | addr.sin_addr.s_addr = INADDR_ANY;
|
---|
606 | addr.sin_port = port;
|
---|
607 |
|
---|
608 | if (((s = socket(AF_INET,SOCK_STREAM,0)) < 0) ||
|
---|
609 | (setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int)) < 0) ||
|
---|
610 | (bind(s,(struct sockaddr *)&addr, sizeof(addr)) < 0) ||
|
---|
611 | (listen(s,1) < 0)) {
|
---|
612 | #ifdef RT_OS_WINDOWS
|
---|
613 | int tmperrno = WSAGetLastError(); /* Don't clobber the real reason we failed */
|
---|
614 | closesocket(s);
|
---|
615 | sofree(pData, so);
|
---|
616 | /* Restore the real errno */
|
---|
617 | WSASetLastError(tmperrno);
|
---|
618 | #else
|
---|
619 | int tmperrno = errno; /* Don't clobber the real reason we failed */
|
---|
620 | close(s);
|
---|
621 | sofree(pData, so);
|
---|
622 | /* Restore the real errno */
|
---|
623 | errno = tmperrno;
|
---|
624 | #endif
|
---|
625 | return NULL;
|
---|
626 | }
|
---|
627 | setsockopt(s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(int));
|
---|
628 |
|
---|
629 | getsockname(s,(struct sockaddr *)&addr,&addrlen);
|
---|
630 | so->so_fport = addr.sin_port;
|
---|
631 | if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr)
|
---|
632 | so->so_faddr = alias_addr;
|
---|
633 | else
|
---|
634 | so->so_faddr = addr.sin_addr;
|
---|
635 |
|
---|
636 | so->s = s;
|
---|
637 | return so;
|
---|
638 | }
|
---|
639 |
|
---|
640 | /*
|
---|
641 | * Data is available in so_rcv
|
---|
642 | * Just write() the data to the socket
|
---|
643 | * XXX not yet...
|
---|
644 | */
|
---|
645 | void
|
---|
646 | sorwakeup(so)
|
---|
647 | struct socket *so;
|
---|
648 | {
|
---|
649 | /* sowrite(so); */
|
---|
650 | /* FD_CLR(so->s,&writefds); */
|
---|
651 | }
|
---|
652 |
|
---|
653 | /*
|
---|
654 | * Data has been freed in so_snd
|
---|
655 | * We have room for a read() if we want to
|
---|
656 | * For now, don't read, it'll be done in the main loop
|
---|
657 | */
|
---|
658 | void
|
---|
659 | sowwakeup(so)
|
---|
660 | struct socket *so;
|
---|
661 | {
|
---|
662 | /* Nothing, yet */
|
---|
663 | }
|
---|
664 |
|
---|
665 | /*
|
---|
666 | * Various session state calls
|
---|
667 | * XXX Should be #define's
|
---|
668 | * The socket state stuff needs work, these often get call 2 or 3
|
---|
669 | * times each when only 1 was needed
|
---|
670 | */
|
---|
671 | void
|
---|
672 | soisfconnecting(so)
|
---|
673 | register struct socket *so;
|
---|
674 | {
|
---|
675 | so->so_state &= ~(SS_NOFDREF|SS_ISFCONNECTED|SS_FCANTRCVMORE|
|
---|
676 | SS_FCANTSENDMORE|SS_FWDRAIN);
|
---|
677 | so->so_state |= SS_ISFCONNECTING; /* Clobber other states */
|
---|
678 | }
|
---|
679 |
|
---|
680 | void
|
---|
681 | soisfconnected(so)
|
---|
682 | register struct socket *so;
|
---|
683 | {
|
---|
684 | so->so_state &= ~(SS_ISFCONNECTING|SS_FWDRAIN|SS_NOFDREF);
|
---|
685 | so->so_state |= SS_ISFCONNECTED; /* Clobber other states */
|
---|
686 | }
|
---|
687 |
|
---|
688 | void
|
---|
689 | sofcantrcvmore(so)
|
---|
690 | struct socket *so;
|
---|
691 | {
|
---|
692 | if ((so->so_state & SS_NOFDREF) == 0) {
|
---|
693 | shutdown(so->s,0);
|
---|
694 | }
|
---|
695 | so->so_state &= ~(SS_ISFCONNECTING);
|
---|
696 | if (so->so_state & SS_FCANTSENDMORE)
|
---|
697 | so->so_state = SS_NOFDREF; /* Don't select it */ /* XXX close() here as well? */
|
---|
698 | else
|
---|
699 | so->so_state |= SS_FCANTRCVMORE;
|
---|
700 | }
|
---|
701 |
|
---|
702 | void
|
---|
703 | sofcantsendmore(so)
|
---|
704 | struct socket *so;
|
---|
705 | {
|
---|
706 | if ((so->so_state & SS_NOFDREF) == 0) {
|
---|
707 | shutdown(so->s,1); /* send FIN to fhost */
|
---|
708 | }
|
---|
709 | so->so_state &= ~(SS_ISFCONNECTING);
|
---|
710 | if (so->so_state & SS_FCANTRCVMORE)
|
---|
711 | so->so_state = SS_NOFDREF; /* as above */
|
---|
712 | else
|
---|
713 | so->so_state |= SS_FCANTSENDMORE;
|
---|
714 | }
|
---|
715 |
|
---|
716 | void
|
---|
717 | soisfdisconnected(so)
|
---|
718 | struct socket *so;
|
---|
719 | {
|
---|
720 | /* so->so_state &= ~(SS_ISFCONNECTING|SS_ISFCONNECTED); */
|
---|
721 | /* close(so->s); */
|
---|
722 | /* so->so_state = SS_ISFDISCONNECTED; */
|
---|
723 | /*
|
---|
724 | * XXX Do nothing ... ?
|
---|
725 | */
|
---|
726 | }
|
---|
727 |
|
---|
728 | /*
|
---|
729 | * Set write drain mode
|
---|
730 | * Set CANTSENDMORE once all data has been write()n
|
---|
731 | */
|
---|
732 | void
|
---|
733 | sofwdrain(so)
|
---|
734 | struct socket *so;
|
---|
735 | {
|
---|
736 | if (so->so_rcv.sb_cc)
|
---|
737 | so->so_state |= SS_FWDRAIN;
|
---|
738 | else
|
---|
739 | sofcantsendmore(so);
|
---|
740 | }
|
---|
741 |
|
---|