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