2 * Copyright (c) 1995 Danny Gasparovski.
4 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
8 #include "qemu/osdep.h"
9 #include "qemu-common.h"
13 #include <sys/filio.h>
16 static void sofcantrcvmore(struct socket
*so
);
17 static void sofcantsendmore(struct socket
*so
);
19 struct socket
*solookup(struct socket
**last
, struct socket
*head
,
20 struct sockaddr_storage
*lhost
, struct sockaddr_storage
*fhost
)
22 struct socket
*so
= *last
;
25 if (so
!= head
&& sockaddr_equal(&(so
->lhost
.ss
), lhost
)
26 && (!fhost
|| sockaddr_equal(&so
->fhost
.ss
, fhost
))) {
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
))) {
38 return (struct socket
*)NULL
;
42 * Create a new socket, initialise the fields
43 * It is the responsibility of the caller to
44 * insque() it into the correct linked-list
47 socreate(Slirp
*slirp
)
51 so
= (struct socket
*)malloc(sizeof(struct socket
));
53 memset(so
, 0, sizeof(struct socket
));
54 so
->so_state
= SS_NOFDREF
;
63 * remque and free a socket, clobber cache
66 sofree(struct socket
*so
)
68 Slirp
*slirp
= so
->slirp
;
71 for (ifm
= (struct mbuf
*) slirp
->if_fastq
.qh_link
;
72 (struct quehead
*) ifm
!= &slirp
->if_fastq
;
73 ifm
= ifm
->ifq_next
) {
74 if (ifm
->ifq_so
== so
) {
79 for (ifm
= (struct mbuf
*) slirp
->if_batchq
.qh_link
;
80 (struct quehead
*) ifm
!= &slirp
->if_batchq
;
81 ifm
= ifm
->ifq_next
) {
82 if (ifm
->ifq_so
== so
) {
87 if (so
->so_emu
==EMU_RSH
&& so
->extra
) {
91 if (so
== slirp
->tcp_last_so
) {
92 slirp
->tcp_last_so
= &slirp
->tcb
;
93 } else if (so
== slirp
->udp_last_so
) {
94 slirp
->udp_last_so
= &slirp
->udb
;
95 } else if (so
== slirp
->icmp_last_so
) {
96 slirp
->icmp_last_so
= &slirp
->icmp
;
100 if(so
->so_next
&& so
->so_prev
)
101 remque(so
); /* crashes if so is not in a queue */
106 size_t sopreprbuf(struct socket
*so
, struct iovec
*iov
, int *np
)
109 struct sbuf
*sb
= &so
->so_snd
;
110 int len
= sb
->sb_datalen
- sb
->sb_cc
;
111 int mss
= so
->so_tcpcb
->t_maxseg
;
113 DEBUG_CALL("sopreprbuf");
114 DEBUG_ARG("so = %p", so
);
119 iov
[0].iov_base
= sb
->sb_wptr
;
120 iov
[1].iov_base
= NULL
;
122 if (sb
->sb_wptr
< sb
->sb_rptr
) {
123 iov
[0].iov_len
= sb
->sb_rptr
- sb
->sb_wptr
;
124 /* Should never succeed, but... */
125 if (iov
[0].iov_len
> len
)
126 iov
[0].iov_len
= len
;
127 if (iov
[0].iov_len
> mss
)
128 iov
[0].iov_len
-= iov
[0].iov_len
%mss
;
131 iov
[0].iov_len
= (sb
->sb_data
+ sb
->sb_datalen
) - sb
->sb_wptr
;
132 /* Should never succeed, but... */
133 if (iov
[0].iov_len
> len
) iov
[0].iov_len
= len
;
134 len
-= iov
[0].iov_len
;
136 iov
[1].iov_base
= sb
->sb_data
;
137 iov
[1].iov_len
= sb
->sb_rptr
- sb
->sb_data
;
138 if(iov
[1].iov_len
> len
)
139 iov
[1].iov_len
= len
;
140 total
= iov
[0].iov_len
+ iov
[1].iov_len
;
143 if (iov
[1].iov_len
> lss
) {
144 iov
[1].iov_len
-= lss
;
147 lss
-= iov
[1].iov_len
;
148 iov
[0].iov_len
-= lss
;
154 if (iov
[0].iov_len
> mss
)
155 iov
[0].iov_len
-= iov
[0].iov_len
%mss
;
162 return iov
[0].iov_len
+ (n
- 1) * iov
[1].iov_len
;
166 * Read from so's socket into sb_snd, updating all relevant sbuf fields
167 * NOTE: This will only be called if it is select()ed for reading, so
168 * a read() of 0 (or less) means it's disconnected
171 soread(struct socket
*so
)
174 struct sbuf
*sb
= &so
->so_snd
;
177 DEBUG_CALL("soread");
178 DEBUG_ARG("so = %p", so
);
181 * No need to check if there's enough room to read.
182 * soread wouldn't have been called if there weren't
184 sopreprbuf(so
, iov
, &n
);
187 nn
= readv(so
->s
, (struct iovec
*)iov
, n
);
188 DEBUG_MISC((dfd
, " ... read nn = %d bytes\n", nn
));
190 nn
= qemu_recv(so
->s
, iov
[0].iov_base
, iov
[0].iov_len
,0);
193 if (nn
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
197 socklen_t slen
= sizeof err
;
201 getsockopt(so
->s
, SOL_SOCKET
, SO_ERROR
,
205 DEBUG_MISC((dfd
, " --- soread() disconnected, nn = %d, errno = %d-%s\n", nn
, errno
,strerror(errno
)));
208 if (err
== ECONNRESET
|| err
== ECONNREFUSED
209 || err
== ENOTCONN
|| err
== EPIPE
) {
210 tcp_drop(sototcpcb(so
), err
);
212 tcp_sockclosed(sototcpcb(so
));
220 * If there was no error, try and read the second time round
221 * We read again if n = 2 (ie, there's another part of the buffer)
222 * and we read as much as we could in the first read
223 * We don't test for <= 0 this time, because there legitimately
224 * might not be any more data (since the socket is non-blocking),
225 * a close will be detected on next iteration.
226 * A return of -1 won't (shouldn't) happen, since it didn't happen above
228 if (n
== 2 && nn
== iov
[0].iov_len
) {
230 ret
= qemu_recv(so
->s
, iov
[1].iov_base
, iov
[1].iov_len
,0);
235 DEBUG_MISC((dfd
, " ... read nn = %d bytes\n", nn
));
241 if (sb
->sb_wptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
242 sb
->sb_wptr
-= sb
->sb_datalen
;
246 int soreadbuf(struct socket
*so
, const char *buf
, int size
)
248 int n
, nn
, copy
= size
;
249 struct sbuf
*sb
= &so
->so_snd
;
252 DEBUG_CALL("soreadbuf");
253 DEBUG_ARG("so = %p", so
);
256 * No need to check if there's enough room to read.
257 * soread wouldn't have been called if there weren't
259 if (sopreprbuf(so
, iov
, &n
) < size
)
262 nn
= MIN(iov
[0].iov_len
, copy
);
263 memcpy(iov
[0].iov_base
, buf
, nn
);
271 memcpy(iov
[1].iov_base
, buf
, copy
);
277 if (sb
->sb_wptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
278 sb
->sb_wptr
-= sb
->sb_datalen
;
283 tcp_sockclosed(sototcpcb(so
));
284 fprintf(stderr
, "soreadbuf buffer to small");
291 * When the socket is created, we set it SO_OOBINLINE,
292 * so when OOB data arrives, we soread() it and everything
293 * in the send buffer is sent as urgent data
296 sorecvoob(struct socket
*so
)
298 struct tcpcb
*tp
= sototcpcb(so
);
301 DEBUG_CALL("sorecvoob");
302 DEBUG_ARG("so = %p", so
);
305 * We take a guess at how much urgent data has arrived.
306 * In most situations, when urgent data arrives, the next
307 * read() should get all the urgent data. This guess will
308 * be wrong however if more data arrives just after the
309 * urgent data, or the read() doesn't return all the
314 tp
->snd_up
= tp
->snd_una
+ so
->so_snd
.sb_cc
;
325 * There's a lot duplicated code here, but...
328 sosendoob(struct socket
*so
)
330 struct sbuf
*sb
= &so
->so_rcv
;
331 char buff
[2048]; /* XXX Shouldn't be sending more oob data than this */
335 DEBUG_CALL("sosendoob");
336 DEBUG_ARG("so = %p", so
);
337 DEBUG_ARG("sb->sb_cc = %d", sb
->sb_cc
);
339 if (so
->so_urgc
> 2048)
340 so
->so_urgc
= 2048; /* XXXX */
342 if (sb
->sb_rptr
< sb
->sb_wptr
) {
343 /* We can send it directly */
344 n
= slirp_send(so
, sb
->sb_rptr
, so
->so_urgc
, (MSG_OOB
)); /* |MSG_DONTWAIT)); */
347 DEBUG_MISC((dfd
, " --- sent %d bytes urgent data, %d urgent bytes left\n", n
, so
->so_urgc
));
350 * Since there's no sendv or sendtov like writev,
351 * we must copy all data to a linear buffer then
354 len
= (sb
->sb_data
+ sb
->sb_datalen
) - sb
->sb_rptr
;
355 if (len
> so
->so_urgc
) len
= so
->so_urgc
;
356 memcpy(buff
, sb
->sb_rptr
, len
);
359 n
= sb
->sb_wptr
- sb
->sb_data
;
360 if (n
> so
->so_urgc
) n
= so
->so_urgc
;
361 memcpy((buff
+ len
), sb
->sb_data
, n
);
365 n
= slirp_send(so
, buff
, len
, (MSG_OOB
)); /* |MSG_DONTWAIT)); */
368 DEBUG_ERROR((dfd
, "Didn't send all data urgently XXXXX\n"));
370 DEBUG_MISC((dfd
, " ---2 sent %d bytes urgent data, %d urgent bytes left\n", n
, so
->so_urgc
));
375 if (sb
->sb_rptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
376 sb
->sb_rptr
-= sb
->sb_datalen
;
382 * Write data from so_rcv to so's socket,
383 * updating all sbuf field as necessary
386 sowrite(struct socket
*so
)
389 struct sbuf
*sb
= &so
->so_rcv
;
393 DEBUG_CALL("sowrite");
394 DEBUG_ARG("so = %p", so
);
403 * No need to check if there's something to write,
404 * sowrite wouldn't have been called otherwise
407 iov
[0].iov_base
= sb
->sb_rptr
;
408 iov
[1].iov_base
= NULL
;
410 if (sb
->sb_rptr
< sb
->sb_wptr
) {
411 iov
[0].iov_len
= sb
->sb_wptr
- sb
->sb_rptr
;
412 /* Should never succeed, but... */
413 if (iov
[0].iov_len
> len
) iov
[0].iov_len
= len
;
416 iov
[0].iov_len
= (sb
->sb_data
+ sb
->sb_datalen
) - sb
->sb_rptr
;
417 if (iov
[0].iov_len
> len
) iov
[0].iov_len
= len
;
418 len
-= iov
[0].iov_len
;
420 iov
[1].iov_base
= sb
->sb_data
;
421 iov
[1].iov_len
= sb
->sb_wptr
- sb
->sb_data
;
422 if (iov
[1].iov_len
> len
) iov
[1].iov_len
= len
;
427 /* Check if there's urgent data to send, and if so, send it */
430 nn
= writev(so
->s
, (const struct iovec
*)iov
, n
);
432 DEBUG_MISC((dfd
, " ... wrote nn = %d bytes\n", nn
));
434 nn
= slirp_send(so
, iov
[0].iov_base
, iov
[0].iov_len
,0);
436 /* This should never happen, but people tell me it does *shrug* */
437 if (nn
< 0 && (errno
== EAGAIN
|| errno
== EINTR
))
441 DEBUG_MISC((dfd
, " --- sowrite disconnected, so->so_state = %x, errno = %d\n",
442 so
->so_state
, errno
));
444 tcp_sockclosed(sototcpcb(so
));
449 if (n
== 2 && nn
== iov
[0].iov_len
) {
451 ret
= slirp_send(so
, iov
[1].iov_base
, iov
[1].iov_len
,0);
455 DEBUG_MISC((dfd
, " ... wrote nn = %d bytes\n", nn
));
461 if (sb
->sb_rptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
462 sb
->sb_rptr
-= sb
->sb_datalen
;
465 * If in DRAIN mode, and there's no more data, set
468 if ((so
->so_state
& SS_FWDRAIN
) && sb
->sb_cc
== 0)
475 * recvfrom() a UDP socket
478 sorecvfrom(struct socket
*so
)
480 struct sockaddr_storage addr
;
481 struct sockaddr_storage saddr
, daddr
;
482 socklen_t addrlen
= sizeof(struct sockaddr_storage
);
484 DEBUG_CALL("sorecvfrom");
485 DEBUG_ARG("so = %p", so
);
487 if (so
->so_type
== IPPROTO_ICMP
) { /* This is a "ping" reply */
491 len
= recvfrom(so
->s
, buff
, 256, 0,
492 (struct sockaddr
*)&addr
, &addrlen
);
493 /* XXX Check if reply is "correct"? */
495 if(len
== -1 || len
== 0) {
496 u_char code
=ICMP_UNREACH_PORT
;
498 if(errno
== EHOSTUNREACH
) code
=ICMP_UNREACH_HOST
;
499 else if(errno
== ENETUNREACH
) code
=ICMP_UNREACH_NET
;
501 DEBUG_MISC((dfd
," udp icmp rx errno = %d-%s\n",
502 errno
,strerror(errno
)));
503 icmp_send_error(so
->so_m
, ICMP_UNREACH
, code
, 0, strerror(errno
));
505 icmp_reflect(so
->so_m
);
506 so
->so_m
= NULL
; /* Don't m_free() it again! */
508 /* No need for this socket anymore, udp_detach it */
510 } else { /* A "normal" UDP packet */
519 m
= m_get(so
->slirp
);
523 switch (so
->so_ffamily
) {
525 m
->m_data
+= IF_MAXLINKHDR
+ sizeof(struct udpiphdr
);
528 m
->m_data
+= IF_MAXLINKHDR
+ sizeof(struct ip6
)
529 + sizeof(struct udphdr
);
532 g_assert_not_reached();
537 * XXX Shouldn't FIONREAD packets destined for port 53,
538 * but I don't know the max packet size for DNS lookups
541 /* if (so->so_fport != htons(53)) { */
542 ioctlsocket(so
->s
, FIONREAD
, &n
);
545 n
= (m
->m_data
- m
->m_dat
) + m
->m_len
+ n
+ 1;
551 m
->m_len
= recvfrom(so
->s
, m
->m_data
, len
, 0,
552 (struct sockaddr
*)&addr
, &addrlen
);
553 DEBUG_MISC((dfd
, " did recvfrom %d, errno = %d-%s\n",
554 m
->m_len
, errno
,strerror(errno
)));
556 /* Report error as ICMP */
557 switch (so
->so_lfamily
) {
560 code
= ICMP_UNREACH_PORT
;
562 if (errno
== EHOSTUNREACH
) {
563 code
= ICMP_UNREACH_HOST
;
564 } else if (errno
== ENETUNREACH
) {
565 code
= ICMP_UNREACH_NET
;
568 DEBUG_MISC((dfd
, " rx error, tx icmp ICMP_UNREACH:%i\n", code
));
569 icmp_send_error(so
->so_m
, ICMP_UNREACH
, code
, 0, strerror(errno
));
572 code
= ICMP6_UNREACH_PORT
;
574 if (errno
== EHOSTUNREACH
) {
575 code
= ICMP6_UNREACH_ADDRESS
;
576 } else if (errno
== ENETUNREACH
) {
577 code
= ICMP6_UNREACH_NO_ROUTE
;
580 DEBUG_MISC((dfd
, " rx error, tx icmp6 ICMP_UNREACH:%i\n", code
));
581 icmp6_send_error(so
->so_m
, ICMP6_UNREACH
, code
);
584 g_assert_not_reached();
590 * Hack: domain name lookup will be used the most for UDP,
591 * and since they'll only be used once there's no need
592 * for the 4 minute (or whatever) timeout... So we time them
593 * out much quicker (10 seconds for now...)
596 if (so
->so_fport
== htons(53))
597 so
->so_expire
= curtime
+ SO_EXPIREFAST
;
599 so
->so_expire
= curtime
+ SO_EXPIRE
;
603 * If this packet was destined for CTL_ADDR,
604 * make it look like that's where it came from
607 sotranslate_in(so
, &saddr
);
608 daddr
= so
->lhost
.ss
;
610 switch (so
->so_ffamily
) {
612 udp_output(so
, m
, (struct sockaddr_in
*) &saddr
,
613 (struct sockaddr_in
*) &daddr
,
617 udp6_output(so
, m
, (struct sockaddr_in6
*) &saddr
,
618 (struct sockaddr_in6
*) &daddr
);
621 g_assert_not_reached();
625 } /* if ping packet */
632 sosendto(struct socket
*so
, struct mbuf
*m
)
635 struct sockaddr_storage addr
;
637 DEBUG_CALL("sosendto");
638 DEBUG_ARG("so = %p", so
);
639 DEBUG_ARG("m = %p", m
);
642 DEBUG_CALL(" sendto()ing)");
643 sotranslate_out(so
, &addr
);
645 /* Don't care what port we get */
646 ret
= sendto(so
->s
, m
->m_data
, m
->m_len
, 0,
647 (struct sockaddr
*)&addr
, sockaddr_size(&addr
));
652 * Kill the socket if there's no reply in 4 minutes,
653 * but only if it's an expirable socket
656 so
->so_expire
= curtime
+ SO_EXPIRE
;
657 so
->so_state
&= SS_PERSISTENT_MASK
;
658 so
->so_state
|= SS_ISFCONNECTED
; /* So that it gets select()ed */
663 * Listen for incoming TCP connections
666 tcp_listen(Slirp
*slirp
, uint32_t haddr
, u_int hport
, uint32_t laddr
,
667 u_int lport
, int flags
)
669 struct sockaddr_in addr
;
672 socklen_t addrlen
= sizeof(addr
);
673 memset(&addr
, 0, addrlen
);
675 DEBUG_CALL("tcp_listen");
676 DEBUG_ARG("haddr = %x", haddr
);
677 DEBUG_ARG("hport = %d", hport
);
678 DEBUG_ARG("laddr = %x", laddr
);
679 DEBUG_ARG("lport = %d", lport
);
680 DEBUG_ARG("flags = %x", flags
);
682 so
= socreate(slirp
);
687 /* Don't tcp_attach... we don't need so_snd nor so_rcv */
688 if ((so
->so_tcpcb
= tcp_newtcpcb(so
)) == NULL
) {
692 insque(so
, &slirp
->tcb
);
695 * SS_FACCEPTONCE sockets must time out.
697 if (flags
& SS_FACCEPTONCE
)
698 so
->so_tcpcb
->t_timer
[TCPT_KEEP
] = TCPTV_KEEP_INIT
*2;
700 so
->so_state
&= SS_PERSISTENT_MASK
;
701 so
->so_state
|= (SS_FACCEPTCONN
| flags
);
702 so
->so_lfamily
= AF_INET
;
703 so
->so_lport
= lport
; /* Kept in network format */
704 so
->so_laddr
.s_addr
= laddr
; /* Ditto */
706 addr
.sin_family
= AF_INET
;
707 addr
.sin_addr
.s_addr
= haddr
;
708 addr
.sin_port
= hport
;
710 if (((s
= qemu_socket(AF_INET
,SOCK_STREAM
,0)) < 0) ||
711 (socket_set_fast_reuse(s
) < 0) ||
712 (bind(s
,(struct sockaddr
*)&addr
, sizeof(addr
)) < 0) ||
714 int tmperrno
= errno
; /* Don't clobber the real reason we failed */
718 /* Restore the real errno */
720 WSASetLastError(tmperrno
);
726 qemu_setsockopt(s
, SOL_SOCKET
, SO_OOBINLINE
, &opt
, sizeof(int));
728 getsockname(s
,(struct sockaddr
*)&addr
,&addrlen
);
729 so
->so_ffamily
= AF_INET
;
730 so
->so_fport
= addr
.sin_port
;
731 if (addr
.sin_addr
.s_addr
== 0 || addr
.sin_addr
.s_addr
== loopback_addr
.s_addr
)
732 so
->so_faddr
= slirp
->vhost_addr
;
734 so
->so_faddr
= addr
.sin_addr
;
741 * Various session state calls
742 * XXX Should be #define's
743 * The socket state stuff needs work, these often get call 2 or 3
744 * times each when only 1 was needed
747 soisfconnecting(struct socket
*so
)
749 so
->so_state
&= ~(SS_NOFDREF
|SS_ISFCONNECTED
|SS_FCANTRCVMORE
|
750 SS_FCANTSENDMORE
|SS_FWDRAIN
);
751 so
->so_state
|= SS_ISFCONNECTING
; /* Clobber other states */
755 soisfconnected(struct socket
*so
)
757 so
->so_state
&= ~(SS_ISFCONNECTING
|SS_FWDRAIN
|SS_NOFDREF
);
758 so
->so_state
|= SS_ISFCONNECTED
; /* Clobber other states */
762 sofcantrcvmore(struct socket
*so
)
764 if ((so
->so_state
& SS_NOFDREF
) == 0) {
767 so
->so_state
&= ~(SS_ISFCONNECTING
);
768 if (so
->so_state
& SS_FCANTSENDMORE
) {
769 so
->so_state
&= SS_PERSISTENT_MASK
;
770 so
->so_state
|= SS_NOFDREF
; /* Don't select it */
772 so
->so_state
|= SS_FCANTRCVMORE
;
777 sofcantsendmore(struct socket
*so
)
779 if ((so
->so_state
& SS_NOFDREF
) == 0) {
780 shutdown(so
->s
,1); /* send FIN to fhost */
782 so
->so_state
&= ~(SS_ISFCONNECTING
);
783 if (so
->so_state
& SS_FCANTRCVMORE
) {
784 so
->so_state
&= SS_PERSISTENT_MASK
;
785 so
->so_state
|= SS_NOFDREF
; /* as above */
787 so
->so_state
|= SS_FCANTSENDMORE
;
792 * Set write drain mode
793 * Set CANTSENDMORE once all data has been write()n
796 sofwdrain(struct socket
*so
)
798 if (so
->so_rcv
.sb_cc
)
799 so
->so_state
|= SS_FWDRAIN
;
805 * Translate addr in host addr when it is a virtual address
807 void sotranslate_out(struct socket
*so
, struct sockaddr_storage
*addr
)
809 Slirp
*slirp
= so
->slirp
;
810 struct sockaddr_in
*sin
= (struct sockaddr_in
*)addr
;
811 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)addr
;
813 switch (addr
->ss_family
) {
815 if ((so
->so_faddr
.s_addr
& slirp
->vnetwork_mask
.s_addr
) ==
816 slirp
->vnetwork_addr
.s_addr
) {
818 if (so
->so_faddr
.s_addr
== slirp
->vnameserver_addr
.s_addr
) {
819 if (get_dns_addr(&sin
->sin_addr
) < 0) {
820 sin
->sin_addr
= loopback_addr
;
823 sin
->sin_addr
= loopback_addr
;
827 DEBUG_MISC((dfd
, " addr.sin_port=%d, "
828 "addr.sin_addr.s_addr=%.16s\n",
829 ntohs(sin
->sin_port
), inet_ntoa(sin
->sin_addr
)));
833 if (in6_equal_net(&so
->so_faddr6
, &slirp
->vprefix_addr6
,
834 slirp
->vprefix_len
)) {
835 if (in6_equal(&so
->so_faddr6
, &slirp
->vnameserver_addr6
)) {
837 if (get_dns6_addr(&sin6
->sin6_addr
, &scope_id
) >= 0) {
838 sin6
->sin6_scope_id
= scope_id
;
840 sin6
->sin6_addr
= in6addr_loopback
;
843 sin6
->sin6_addr
= in6addr_loopback
;
853 void sotranslate_in(struct socket
*so
, struct sockaddr_storage
*addr
)
855 Slirp
*slirp
= so
->slirp
;
856 struct sockaddr_in
*sin
= (struct sockaddr_in
*)addr
;
857 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)addr
;
859 switch (addr
->ss_family
) {
861 if ((so
->so_faddr
.s_addr
& slirp
->vnetwork_mask
.s_addr
) ==
862 slirp
->vnetwork_addr
.s_addr
) {
863 uint32_t inv_mask
= ~slirp
->vnetwork_mask
.s_addr
;
865 if ((so
->so_faddr
.s_addr
& inv_mask
) == inv_mask
) {
866 sin
->sin_addr
= slirp
->vhost_addr
;
867 } else if (sin
->sin_addr
.s_addr
== loopback_addr
.s_addr
||
868 so
->so_faddr
.s_addr
!= slirp
->vhost_addr
.s_addr
) {
869 sin
->sin_addr
= so
->so_faddr
;
875 if (in6_equal_net(&so
->so_faddr6
, &slirp
->vprefix_addr6
,
876 slirp
->vprefix_len
)) {
877 if (in6_equal(&sin6
->sin6_addr
, &in6addr_loopback
)
878 || !in6_equal(&so
->so_faddr6
, &slirp
->vhost_addr6
)) {
879 sin6
->sin6_addr
= so
->so_faddr6
;
890 * Translate connections from localhost to the real hostname
892 void sotranslate_accept(struct socket
*so
)
894 Slirp
*slirp
= so
->slirp
;
896 switch (so
->so_ffamily
) {
898 if (so
->so_faddr
.s_addr
== INADDR_ANY
||
899 (so
->so_faddr
.s_addr
& loopback_mask
) ==
900 (loopback_addr
.s_addr
& loopback_mask
)) {
901 so
->so_faddr
= slirp
->vhost_addr
;
906 if (in6_equal(&so
->so_faddr6
, &in6addr_any
) ||
907 in6_equal(&so
->so_faddr6
, &in6addr_loopback
)) {
908 so
->so_faddr6
= slirp
->vhost_addr6
;