1
2
3
4
5
6
7
8#include "qemu/osdep.h"
9#include "qemu-common.h"
10#include "slirp.h"
11#include "ip_icmp.h"
12#ifdef __sun__
13#include <sys/filio.h>
14#endif
15
16static void sofcantrcvmore(struct socket *so);
17static void sofcantsendmore(struct socket *so);
18
19struct socket *solookup(struct socket **last, struct socket *head,
20 struct sockaddr_storage *lhost, struct sockaddr_storage *fhost)
21{
22 struct socket *so = *last;
23
24
25 if (so != head && sockaddr_equal(&(so->lhost.ss), lhost)
26 && (!fhost || sockaddr_equal(&so->fhost.ss, fhost))) {
27 return so;
28 }
29
30 for (so = head->so_next; so != head; so = so->so_next) {
31 if (sockaddr_equal(&(so->lhost.ss), lhost)
32 && (!fhost || sockaddr_equal(&so->fhost.ss, fhost))) {
33 *last = so;
34 return so;
35 }
36 }
37
38 return (struct socket *)NULL;
39}
40
41
42
43
44
45
46struct socket *
47socreate(Slirp *slirp)
48{
49 struct socket *so;
50
51 so = (struct socket *)malloc(sizeof(struct socket));
52 if(so) {
53 memset(so, 0, sizeof(struct socket));
54 so->so_state = SS_NOFDREF;
55 so->s = -1;
56 so->slirp = slirp;
57 so->pollfds_idx = -1;
58 }
59 return(so);
60}
61
62
63
64
65static void
66soqfree(struct socket *so, struct quehead *qh)
67{
68 struct mbuf *ifq;
69
70 for (ifq = (struct mbuf *) qh->qh_link;
71 (struct quehead *) ifq != qh;
72 ifq = ifq->ifq_next) {
73 if (ifq->ifq_so == so) {
74 struct mbuf *ifm;
75 ifq->ifq_so = NULL;
76 for (ifm = ifq->ifs_next; ifm != ifq; ifm = ifm->ifs_next) {
77 ifm->ifq_so = NULL;
78 }
79 }
80 }
81}
82
83
84
85
86void
87sofree(struct socket *so)
88{
89 Slirp *slirp = so->slirp;
90
91 soqfree(so, &slirp->if_fastq);
92 soqfree(so, &slirp->if_batchq);
93
94 if (so->so_emu==EMU_RSH && so->extra) {
95 sofree(so->extra);
96 so->extra=NULL;
97 }
98 if (so == slirp->tcp_last_so) {
99 slirp->tcp_last_so = &slirp->tcb;
100 } else if (so == slirp->udp_last_so) {
101 slirp->udp_last_so = &slirp->udb;
102 } else if (so == slirp->icmp_last_so) {
103 slirp->icmp_last_so = &slirp->icmp;
104 }
105 m_free(so->so_m);
106
107 if(so->so_next && so->so_prev)
108 remque(so);
109
110 if (so->so_tcpcb) {
111 free(so->so_tcpcb);
112 }
113 free(so);
114}
115
116size_t sopreprbuf(struct socket *so, struct iovec *iov, int *np)
117{
118 int n, lss, total;
119 struct sbuf *sb = &so->so_snd;
120 int len = sb->sb_datalen - sb->sb_cc;
121 int mss = so->so_tcpcb->t_maxseg;
122
123 DEBUG_CALL("sopreprbuf");
124 DEBUG_ARG("so = %p", so);
125
126 if (len <= 0)
127 return 0;
128
129 iov[0].iov_base = sb->sb_wptr;
130 iov[1].iov_base = NULL;
131 iov[1].iov_len = 0;
132 if (sb->sb_wptr < sb->sb_rptr) {
133 iov[0].iov_len = sb->sb_rptr - sb->sb_wptr;
134
135 if (iov[0].iov_len > len)
136 iov[0].iov_len = len;
137 if (iov[0].iov_len > mss)
138 iov[0].iov_len -= iov[0].iov_len%mss;
139 n = 1;
140 } else {
141 iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_wptr;
142
143 if (iov[0].iov_len > len) iov[0].iov_len = len;
144 len -= iov[0].iov_len;
145 if (len) {
146 iov[1].iov_base = sb->sb_data;
147 iov[1].iov_len = sb->sb_rptr - sb->sb_data;
148 if(iov[1].iov_len > len)
149 iov[1].iov_len = len;
150 total = iov[0].iov_len + iov[1].iov_len;
151 if (total > mss) {
152 lss = total%mss;
153 if (iov[1].iov_len > lss) {
154 iov[1].iov_len -= lss;
155 n = 2;
156 } else {
157 lss -= iov[1].iov_len;
158 iov[0].iov_len -= lss;
159 n = 1;
160 }
161 } else
162 n = 2;
163 } else {
164 if (iov[0].iov_len > mss)
165 iov[0].iov_len -= iov[0].iov_len%mss;
166 n = 1;
167 }
168 }
169 if (np)
170 *np = n;
171
172 return iov[0].iov_len + (n - 1) * iov[1].iov_len;
173}
174
175
176
177
178
179
180int
181soread(struct socket *so)
182{
183 int n, nn;
184 struct sbuf *sb = &so->so_snd;
185 struct iovec iov[2];
186
187 DEBUG_CALL("soread");
188 DEBUG_ARG("so = %p", so);
189
190
191
192
193
194 sopreprbuf(so, iov, &n);
195
196#ifdef HAVE_READV
197 nn = readv(so->s, (struct iovec *)iov, n);
198 DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
199#else
200 nn = qemu_recv(so->s, iov[0].iov_base, iov[0].iov_len,0);
201#endif
202 if (nn <= 0) {
203 if (nn < 0 && (errno == EINTR || errno == EAGAIN))
204 return 0;
205 else {
206 int err;
207 socklen_t slen = sizeof err;
208
209 err = errno;
210 if (nn == 0) {
211 getsockopt(so->s, SOL_SOCKET, SO_ERROR,
212 &err, &slen);
213 }
214
215 DEBUG_MISC((dfd, " --- soread() disconnected, nn = %d, errno = %d-%s\n", nn, errno,strerror(errno)));
216 sofcantrcvmore(so);
217
218 if (err == ECONNRESET || err == ECONNREFUSED
219 || err == ENOTCONN || err == EPIPE) {
220 tcp_drop(sototcpcb(so), err);
221 } else {
222 tcp_sockclosed(sototcpcb(so));
223 }
224 return -1;
225 }
226 }
227
228#ifndef HAVE_READV
229
230
231
232
233
234
235
236
237
238 if (n == 2 && nn == iov[0].iov_len) {
239 int ret;
240 ret = qemu_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
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 return nn;
254}
255
256int soreadbuf(struct socket *so, const char *buf, int size)
257{
258 int n, nn, copy = size;
259 struct sbuf *sb = &so->so_snd;
260 struct iovec iov[2];
261
262 DEBUG_CALL("soreadbuf");
263 DEBUG_ARG("so = %p", so);
264
265
266
267
268
269 if (sopreprbuf(so, iov, &n) < size)
270 goto err;
271
272 nn = MIN(iov[0].iov_len, copy);
273 memcpy(iov[0].iov_base, buf, nn);
274
275 copy -= nn;
276 buf += nn;
277
278 if (copy == 0)
279 goto done;
280
281 memcpy(iov[1].iov_base, buf, copy);
282
283done:
284
285 sb->sb_cc += size;
286 sb->sb_wptr += size;
287 if (sb->sb_wptr >= (sb->sb_data + sb->sb_datalen))
288 sb->sb_wptr -= sb->sb_datalen;
289 return size;
290err:
291
292 sofcantrcvmore(so);
293 tcp_sockclosed(sototcpcb(so));
294 fprintf(stderr, "soreadbuf buffer to small");
295 return -1;
296}
297
298
299
300
301
302
303
304
305int
306sorecvoob(struct socket *so)
307{
308 struct tcpcb *tp = sototcpcb(so);
309 int ret;
310
311 DEBUG_CALL("sorecvoob");
312 DEBUG_ARG("so = %p", so);
313
314
315
316
317
318
319
320
321
322 ret = soread(so);
323 if (ret > 0) {
324 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
325 tp->t_force = 1;
326 tcp_output(tp);
327 tp->t_force = 0;
328 }
329
330 return ret;
331}
332
333
334
335
336
337int
338sosendoob(struct socket *so)
339{
340 struct sbuf *sb = &so->so_rcv;
341 char buff[2048];
342
343 int n, len;
344
345 DEBUG_CALL("sosendoob");
346 DEBUG_ARG("so = %p", so);
347 DEBUG_ARG("sb->sb_cc = %d", sb->sb_cc);
348
349 if (so->so_urgc > 2048)
350 so->so_urgc = 2048;
351
352 if (sb->sb_rptr < sb->sb_wptr) {
353
354 n = slirp_send(so, sb->sb_rptr, so->so_urgc, (MSG_OOB));
355 } else {
356
357
358
359
360
361 uint32_t urgc = so->so_urgc;
362 len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
363 if (len > urgc) {
364 len = urgc;
365 }
366 memcpy(buff, sb->sb_rptr, len);
367 urgc -= len;
368 if (urgc) {
369 n = sb->sb_wptr - sb->sb_data;
370 if (n > urgc) {
371 n = urgc;
372 }
373 memcpy((buff + len), sb->sb_data, n);
374 len += n;
375 }
376 n = slirp_send(so, buff, len, (MSG_OOB));
377 }
378
379#ifdef DEBUG
380 if (n != len) {
381 DEBUG_ERROR((dfd, "Didn't send all data urgently XXXXX\n"));
382 }
383#endif
384 if (n < 0) {
385 return n;
386 }
387 so->so_urgc -= n;
388 DEBUG_MISC((dfd, " ---2 sent %d bytes urgent data, %d urgent bytes left\n", n, so->so_urgc));
389
390 sb->sb_cc -= n;
391 sb->sb_rptr += n;
392 if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
393 sb->sb_rptr -= sb->sb_datalen;
394
395 return n;
396}
397
398
399
400
401
402int
403sowrite(struct socket *so)
404{
405 int n,nn;
406 struct sbuf *sb = &so->so_rcv;
407 int len = sb->sb_cc;
408 struct iovec iov[2];
409
410 DEBUG_CALL("sowrite");
411 DEBUG_ARG("so = %p", so);
412
413 if (so->so_urgc) {
414 uint32_t expected = so->so_urgc;
415 if (sosendoob(so) < expected) {
416
417
418
419
420
421 goto err_disconnected;
422 }
423 if (sb->sb_cc == 0)
424 return 0;
425 }
426
427
428
429
430
431
432 iov[0].iov_base = sb->sb_rptr;
433 iov[1].iov_base = NULL;
434 iov[1].iov_len = 0;
435 if (sb->sb_rptr < sb->sb_wptr) {
436 iov[0].iov_len = sb->sb_wptr - sb->sb_rptr;
437
438 if (iov[0].iov_len > len) iov[0].iov_len = len;
439 n = 1;
440 } else {
441 iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
442 if (iov[0].iov_len > len) iov[0].iov_len = len;
443 len -= iov[0].iov_len;
444 if (len) {
445 iov[1].iov_base = sb->sb_data;
446 iov[1].iov_len = sb->sb_wptr - sb->sb_data;
447 if (iov[1].iov_len > len) iov[1].iov_len = len;
448 n = 2;
449 } else
450 n = 1;
451 }
452
453
454#ifdef HAVE_READV
455 nn = writev(so->s, (const struct iovec *)iov, n);
456
457 DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", nn));
458#else
459 nn = slirp_send(so, iov[0].iov_base, iov[0].iov_len,0);
460#endif
461
462 if (nn < 0 && (errno == EAGAIN || errno == EINTR))
463 return 0;
464
465 if (nn <= 0) {
466 goto err_disconnected;
467 }
468
469#ifndef HAVE_READV
470 if (n == 2 && nn == iov[0].iov_len) {
471 int ret;
472 ret = slirp_send(so, iov[1].iov_base, iov[1].iov_len,0);
473 if (ret > 0)
474 nn += ret;
475 }
476 DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", nn));
477#endif
478
479
480 sb->sb_cc -= nn;
481 sb->sb_rptr += nn;
482 if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
483 sb->sb_rptr -= sb->sb_datalen;
484
485
486
487
488
489 if ((so->so_state & SS_FWDRAIN) && sb->sb_cc == 0)
490 sofcantsendmore(so);
491
492 return nn;
493
494err_disconnected:
495 DEBUG_MISC((dfd, " --- sowrite disconnected, so->so_state = %x, errno = %d\n",
496 so->so_state, errno));
497 sofcantsendmore(so);
498 tcp_sockclosed(sototcpcb(so));
499 return -1;
500}
501
502
503
504
505void
506sorecvfrom(struct socket *so)
507{
508 struct sockaddr_storage addr;
509 struct sockaddr_storage saddr, daddr;
510 socklen_t addrlen = sizeof(struct sockaddr_storage);
511
512 DEBUG_CALL("sorecvfrom");
513 DEBUG_ARG("so = %p", so);
514
515 if (so->so_type == IPPROTO_ICMP) {
516 char buff[256];
517 int len;
518
519 len = recvfrom(so->s, buff, 256, 0,
520 (struct sockaddr *)&addr, &addrlen);
521
522
523 if(len == -1 || len == 0) {
524 u_char code=ICMP_UNREACH_PORT;
525
526 if(errno == EHOSTUNREACH) code=ICMP_UNREACH_HOST;
527 else if(errno == ENETUNREACH) code=ICMP_UNREACH_NET;
528
529 DEBUG_MISC((dfd," udp icmp rx errno = %d-%s\n",
530 errno,strerror(errno)));
531 icmp_send_error(so->so_m, ICMP_UNREACH, code, 0, strerror(errno));
532 } else {
533 icmp_reflect(so->so_m);
534 so->so_m = NULL;
535 }
536
537 udp_detach(so);
538 } else {
539 struct mbuf *m;
540 int len;
541#ifdef _WIN32
542 unsigned long n;
543#else
544 int n;
545#endif
546
547 m = m_get(so->slirp);
548 if (!m) {
549 return;
550 }
551 switch (so->so_ffamily) {
552 case AF_INET:
553 m->m_data += IF_MAXLINKHDR + sizeof(struct udpiphdr);
554 break;
555 case AF_INET6:
556 m->m_data += IF_MAXLINKHDR + sizeof(struct ip6)
557 + sizeof(struct udphdr);
558 break;
559 default:
560 g_assert_not_reached();
561 break;
562 }
563
564
565
566
567
568 len = M_FREEROOM(m);
569
570 ioctlsocket(so->s, FIONREAD, &n);
571
572 if (n > len) {
573 n = (m->m_data - m->m_dat) + m->m_len + n + 1;
574 m_inc(m, n);
575 len = M_FREEROOM(m);
576 }
577
578
579 m->m_len = recvfrom(so->s, m->m_data, len, 0,
580 (struct sockaddr *)&addr, &addrlen);
581 DEBUG_MISC((dfd, " did recvfrom %d, errno = %d-%s\n",
582 m->m_len, errno,strerror(errno)));
583 if(m->m_len<0) {
584
585 switch (so->so_lfamily) {
586 uint8_t code;
587 case AF_INET:
588 code = ICMP_UNREACH_PORT;
589
590 if (errno == EHOSTUNREACH) {
591 code = ICMP_UNREACH_HOST;
592 } else if (errno == ENETUNREACH) {
593 code = ICMP_UNREACH_NET;
594 }
595
596 DEBUG_MISC((dfd, " rx error, tx icmp ICMP_UNREACH:%i\n", code));
597 icmp_send_error(so->so_m, ICMP_UNREACH, code, 0, strerror(errno));
598 break;
599 case AF_INET6:
600 code = ICMP6_UNREACH_PORT;
601
602 if (errno == EHOSTUNREACH) {
603 code = ICMP6_UNREACH_ADDRESS;
604 } else if (errno == ENETUNREACH) {
605 code = ICMP6_UNREACH_NO_ROUTE;
606 }
607
608 DEBUG_MISC((dfd, " rx error, tx icmp6 ICMP_UNREACH:%i\n", code));
609 icmp6_send_error(so->so_m, ICMP6_UNREACH, code);
610 break;
611 default:
612 g_assert_not_reached();
613 break;
614 }
615 m_free(m);
616 } else {
617
618
619
620
621
622
623 if (so->so_expire) {
624 if (so->so_fport == htons(53))
625 so->so_expire = curtime + SO_EXPIREFAST;
626 else
627 so->so_expire = curtime + SO_EXPIRE;
628 }
629
630
631
632
633
634 saddr = addr;
635 sotranslate_in(so, &saddr);
636 daddr = so->lhost.ss;
637
638 switch (so->so_ffamily) {
639 case AF_INET:
640 udp_output(so, m, (struct sockaddr_in *) &saddr,
641 (struct sockaddr_in *) &daddr,
642 so->so_iptos);
643 break;
644 case AF_INET6:
645 udp6_output(so, m, (struct sockaddr_in6 *) &saddr,
646 (struct sockaddr_in6 *) &daddr);
647 break;
648 default:
649 g_assert_not_reached();
650 break;
651 }
652 }
653 }
654}
655
656
657
658
659int
660sosendto(struct socket *so, struct mbuf *m)
661{
662 int ret;
663 struct sockaddr_storage addr;
664
665 DEBUG_CALL("sosendto");
666 DEBUG_ARG("so = %p", so);
667 DEBUG_ARG("m = %p", m);
668
669 addr = so->fhost.ss;
670 DEBUG_CALL(" sendto()ing)");
671 sotranslate_out(so, &addr);
672
673
674 ret = sendto(so->s, m->m_data, m->m_len, 0,
675 (struct sockaddr *)&addr, sockaddr_size(&addr));
676 if (ret < 0)
677 return -1;
678
679
680
681
682
683 if (so->so_expire)
684 so->so_expire = curtime + SO_EXPIRE;
685 so->so_state &= SS_PERSISTENT_MASK;
686 so->so_state |= SS_ISFCONNECTED;
687 return 0;
688}
689
690
691
692
693struct socket *
694tcp_listen(Slirp *slirp, uint32_t haddr, u_int hport, uint32_t laddr,
695 u_int lport, int flags)
696{
697 struct sockaddr_in addr;
698 struct socket *so;
699 int s, opt = 1;
700 socklen_t addrlen = sizeof(addr);
701 memset(&addr, 0, addrlen);
702
703 DEBUG_CALL("tcp_listen");
704 DEBUG_ARG("haddr = %x", haddr);
705 DEBUG_ARG("hport = %d", hport);
706 DEBUG_ARG("laddr = %x", laddr);
707 DEBUG_ARG("lport = %d", lport);
708 DEBUG_ARG("flags = %x", flags);
709
710 so = socreate(slirp);
711 if (!so) {
712 return NULL;
713 }
714
715
716 if ((so->so_tcpcb = tcp_newtcpcb(so)) == NULL) {
717 free(so);
718 return NULL;
719 }
720 insque(so, &slirp->tcb);
721
722
723
724
725 if (flags & SS_FACCEPTONCE)
726 so->so_tcpcb->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT*2;
727
728 so->so_state &= SS_PERSISTENT_MASK;
729 so->so_state |= (SS_FACCEPTCONN | flags);
730 so->so_lfamily = AF_INET;
731 so->so_lport = lport;
732 so->so_laddr.s_addr = laddr;
733
734 addr.sin_family = AF_INET;
735 addr.sin_addr.s_addr = haddr;
736 addr.sin_port = hport;
737
738 if (((s = qemu_socket(AF_INET,SOCK_STREAM,0)) < 0) ||
739 (socket_set_fast_reuse(s) < 0) ||
740 (bind(s,(struct sockaddr *)&addr, sizeof(addr)) < 0) ||
741 (listen(s,1) < 0)) {
742 int tmperrno = errno;
743
744 if (s >= 0) {
745 closesocket(s);
746 }
747 sofree(so);
748
749#ifdef _WIN32
750 WSASetLastError(tmperrno);
751#else
752 errno = tmperrno;
753#endif
754 return NULL;
755 }
756 qemu_setsockopt(s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
757
758 getsockname(s,(struct sockaddr *)&addr,&addrlen);
759 so->so_ffamily = AF_INET;
760 so->so_fport = addr.sin_port;
761 if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr)
762 so->so_faddr = slirp->vhost_addr;
763 else
764 so->so_faddr = addr.sin_addr;
765
766 so->s = s;
767 return so;
768}
769
770
771
772
773
774
775
776void
777soisfconnecting(struct socket *so)
778{
779 so->so_state &= ~(SS_NOFDREF|SS_ISFCONNECTED|SS_FCANTRCVMORE|
780 SS_FCANTSENDMORE|SS_FWDRAIN);
781 so->so_state |= SS_ISFCONNECTING;
782}
783
784void
785soisfconnected(struct socket *so)
786{
787 so->so_state &= ~(SS_ISFCONNECTING|SS_FWDRAIN|SS_NOFDREF);
788 so->so_state |= SS_ISFCONNECTED;
789}
790
791static void
792sofcantrcvmore(struct socket *so)
793{
794 if ((so->so_state & SS_NOFDREF) == 0) {
795 shutdown(so->s,0);
796 }
797 so->so_state &= ~(SS_ISFCONNECTING);
798 if (so->so_state & SS_FCANTSENDMORE) {
799 so->so_state &= SS_PERSISTENT_MASK;
800 so->so_state |= SS_NOFDREF;
801 } else {
802 so->so_state |= SS_FCANTRCVMORE;
803 }
804}
805
806static void
807sofcantsendmore(struct socket *so)
808{
809 if ((so->so_state & SS_NOFDREF) == 0) {
810 shutdown(so->s,1);
811 }
812 so->so_state &= ~(SS_ISFCONNECTING);
813 if (so->so_state & SS_FCANTRCVMORE) {
814 so->so_state &= SS_PERSISTENT_MASK;
815 so->so_state |= SS_NOFDREF;
816 } else {
817 so->so_state |= SS_FCANTSENDMORE;
818 }
819}
820
821
822
823
824
825void
826sofwdrain(struct socket *so)
827{
828 if (so->so_rcv.sb_cc)
829 so->so_state |= SS_FWDRAIN;
830 else
831 sofcantsendmore(so);
832}
833
834
835
836
837void sotranslate_out(struct socket *so, struct sockaddr_storage *addr)
838{
839 Slirp *slirp = so->slirp;
840 struct sockaddr_in *sin = (struct sockaddr_in *)addr;
841 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
842
843 switch (addr->ss_family) {
844 case AF_INET:
845 if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) ==
846 slirp->vnetwork_addr.s_addr) {
847
848 if (so->so_faddr.s_addr == slirp->vnameserver_addr.s_addr) {
849 if (get_dns_addr(&sin->sin_addr) < 0) {
850 sin->sin_addr = loopback_addr;
851 }
852 } else {
853 sin->sin_addr = loopback_addr;
854 }
855 }
856
857 DEBUG_MISC((dfd, " addr.sin_port=%d, "
858 "addr.sin_addr.s_addr=%.16s\n",
859 ntohs(sin->sin_port), inet_ntoa(sin->sin_addr)));
860 break;
861
862 case AF_INET6:
863 if (in6_equal_net(&so->so_faddr6, &slirp->vprefix_addr6,
864 slirp->vprefix_len)) {
865 if (in6_equal(&so->so_faddr6, &slirp->vnameserver_addr6)) {
866 uint32_t scope_id;
867 if (get_dns6_addr(&sin6->sin6_addr, &scope_id) >= 0) {
868 sin6->sin6_scope_id = scope_id;
869 } else {
870 sin6->sin6_addr = in6addr_loopback;
871 }
872 } else {
873 sin6->sin6_addr = in6addr_loopback;
874 }
875 }
876 break;
877
878 default:
879 break;
880 }
881}
882
883void sotranslate_in(struct socket *so, struct sockaddr_storage *addr)
884{
885 Slirp *slirp = so->slirp;
886 struct sockaddr_in *sin = (struct sockaddr_in *)addr;
887 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
888
889 switch (addr->ss_family) {
890 case AF_INET:
891 if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) ==
892 slirp->vnetwork_addr.s_addr) {
893 uint32_t inv_mask = ~slirp->vnetwork_mask.s_addr;
894
895 if ((so->so_faddr.s_addr & inv_mask) == inv_mask) {
896 sin->sin_addr = slirp->vhost_addr;
897 } else if (sin->sin_addr.s_addr == loopback_addr.s_addr ||
898 so->so_faddr.s_addr != slirp->vhost_addr.s_addr) {
899 sin->sin_addr = so->so_faddr;
900 }
901 }
902 break;
903
904 case AF_INET6:
905 if (in6_equal_net(&so->so_faddr6, &slirp->vprefix_addr6,
906 slirp->vprefix_len)) {
907 if (in6_equal(&sin6->sin6_addr, &in6addr_loopback)
908 || !in6_equal(&so->so_faddr6, &slirp->vhost_addr6)) {
909 sin6->sin6_addr = so->so_faddr6;
910 }
911 }
912 break;
913
914 default:
915 break;
916 }
917}
918
919
920
921
922void sotranslate_accept(struct socket *so)
923{
924 Slirp *slirp = so->slirp;
925
926 switch (so->so_ffamily) {
927 case AF_INET:
928 if (so->so_faddr.s_addr == INADDR_ANY ||
929 (so->so_faddr.s_addr & loopback_mask) ==
930 (loopback_addr.s_addr & loopback_mask)) {
931 so->so_faddr = slirp->vhost_addr;
932 }
933 break;
934
935 case AF_INET6:
936 if (in6_equal(&so->so_faddr6, &in6addr_any) ||
937 in6_equal(&so->so_faddr6, &in6addr_loopback)) {
938 so->so_faddr6 = slirp->vhost_addr6;
939 }
940 break;
941
942 default:
943 break;
944 }
945}
946