VirtualBox

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

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

Removed tons of ifdef VBOX conditionals to make slirp readable again

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

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