VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/slirp/socket.c@ 13710

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

mid point in replacing on VBOX_SLIRP_LOCK/UNLOCK macroces
resolved deadlock

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