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
;
70 if (so
->so_emu
==EMU_RSH
&& so
->extra
) {
74 if (so
== slirp
->tcp_last_so
) {
75 slirp
->tcp_last_so
= &slirp
->tcb
;
76 } else if (so
== slirp
->udp_last_so
) {
77 slirp
->udp_last_so
= &slirp
->udb
;
78 } else if (so
== slirp
->icmp_last_so
) {
79 slirp
->icmp_last_so
= &slirp
->icmp
;
83 if(so
->so_next
&& so
->so_prev
)
84 remque(so
); /* crashes if so is not in a queue */
89 size_t sopreprbuf(struct socket
*so
, struct iovec
*iov
, int *np
)
92 struct sbuf
*sb
= &so
->so_snd
;
93 int len
= sb
->sb_datalen
- sb
->sb_cc
;
94 int mss
= so
->so_tcpcb
->t_maxseg
;
96 DEBUG_CALL("sopreprbuf");
97 DEBUG_ARG("so = %p", so
);
102 iov
[0].iov_base
= sb
->sb_wptr
;
103 iov
[1].iov_base
= NULL
;
105 if (sb
->sb_wptr
< sb
->sb_rptr
) {
106 iov
[0].iov_len
= sb
->sb_rptr
- sb
->sb_wptr
;
107 /* Should never succeed, but... */
108 if (iov
[0].iov_len
> len
)
109 iov
[0].iov_len
= len
;
110 if (iov
[0].iov_len
> mss
)
111 iov
[0].iov_len
-= iov
[0].iov_len
%mss
;
114 iov
[0].iov_len
= (sb
->sb_data
+ sb
->sb_datalen
) - sb
->sb_wptr
;
115 /* Should never succeed, but... */
116 if (iov
[0].iov_len
> len
) iov
[0].iov_len
= len
;
117 len
-= iov
[0].iov_len
;
119 iov
[1].iov_base
= sb
->sb_data
;
120 iov
[1].iov_len
= sb
->sb_rptr
- sb
->sb_data
;
121 if(iov
[1].iov_len
> len
)
122 iov
[1].iov_len
= len
;
123 total
= iov
[0].iov_len
+ iov
[1].iov_len
;
126 if (iov
[1].iov_len
> lss
) {
127 iov
[1].iov_len
-= lss
;
130 lss
-= iov
[1].iov_len
;
131 iov
[0].iov_len
-= lss
;
137 if (iov
[0].iov_len
> mss
)
138 iov
[0].iov_len
-= iov
[0].iov_len
%mss
;
145 return iov
[0].iov_len
+ (n
- 1) * iov
[1].iov_len
;
149 * Read from so's socket into sb_snd, updating all relevant sbuf fields
150 * NOTE: This will only be called if it is select()ed for reading, so
151 * a read() of 0 (or less) means it's disconnected
154 soread(struct socket
*so
)
157 struct sbuf
*sb
= &so
->so_snd
;
160 DEBUG_CALL("soread");
161 DEBUG_ARG("so = %p", so
);
164 * No need to check if there's enough room to read.
165 * soread wouldn't have been called if there weren't
167 sopreprbuf(so
, iov
, &n
);
170 nn
= readv(so
->s
, (struct iovec
*)iov
, n
);
171 DEBUG_MISC((dfd
, " ... read nn = %d bytes\n", nn
));
173 nn
= qemu_recv(so
->s
, iov
[0].iov_base
, iov
[0].iov_len
,0);
176 if (nn
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
180 socklen_t slen
= sizeof err
;
184 getsockopt(so
->s
, SOL_SOCKET
, SO_ERROR
,
188 DEBUG_MISC((dfd
, " --- soread() disconnected, nn = %d, errno = %d-%s\n", nn
, errno
,strerror(errno
)));
191 if (err
== ECONNRESET
|| err
== ECONNREFUSED
192 || err
== ENOTCONN
|| err
== EPIPE
) {
193 tcp_drop(sototcpcb(so
), err
);
195 tcp_sockclosed(sototcpcb(so
));
203 * If there was no error, try and read the second time round
204 * We read again if n = 2 (ie, there's another part of the buffer)
205 * and we read as much as we could in the first read
206 * We don't test for <= 0 this time, because there legitimately
207 * might not be any more data (since the socket is non-blocking),
208 * a close will be detected on next iteration.
209 * A return of -1 wont (shouldn't) happen, since it didn't happen above
211 if (n
== 2 && nn
== iov
[0].iov_len
) {
213 ret
= qemu_recv(so
->s
, iov
[1].iov_base
, iov
[1].iov_len
,0);
218 DEBUG_MISC((dfd
, " ... read nn = %d bytes\n", nn
));
224 if (sb
->sb_wptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
225 sb
->sb_wptr
-= sb
->sb_datalen
;
229 int soreadbuf(struct socket
*so
, const char *buf
, int size
)
231 int n
, nn
, copy
= size
;
232 struct sbuf
*sb
= &so
->so_snd
;
235 DEBUG_CALL("soreadbuf");
236 DEBUG_ARG("so = %p", so
);
239 * No need to check if there's enough room to read.
240 * soread wouldn't have been called if there weren't
242 if (sopreprbuf(so
, iov
, &n
) < size
)
245 nn
= MIN(iov
[0].iov_len
, copy
);
246 memcpy(iov
[0].iov_base
, buf
, nn
);
254 memcpy(iov
[1].iov_base
, buf
, copy
);
260 if (sb
->sb_wptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
261 sb
->sb_wptr
-= sb
->sb_datalen
;
266 tcp_sockclosed(sototcpcb(so
));
267 fprintf(stderr
, "soreadbuf buffer to small");
274 * When the socket is created, we set it SO_OOBINLINE,
275 * so when OOB data arrives, we soread() it and everything
276 * in the send buffer is sent as urgent data
279 sorecvoob(struct socket
*so
)
281 struct tcpcb
*tp
= sototcpcb(so
);
284 DEBUG_CALL("sorecvoob");
285 DEBUG_ARG("so = %p", so
);
288 * We take a guess at how much urgent data has arrived.
289 * In most situations, when urgent data arrives, the next
290 * read() should get all the urgent data. This guess will
291 * be wrong however if more data arrives just after the
292 * urgent data, or the read() doesn't return all the
297 tp
->snd_up
= tp
->snd_una
+ so
->so_snd
.sb_cc
;
308 * There's a lot duplicated code here, but...
311 sosendoob(struct socket
*so
)
313 struct sbuf
*sb
= &so
->so_rcv
;
314 char buff
[2048]; /* XXX Shouldn't be sending more oob data than this */
318 DEBUG_CALL("sosendoob");
319 DEBUG_ARG("so = %p", so
);
320 DEBUG_ARG("sb->sb_cc = %d", sb
->sb_cc
);
322 if (so
->so_urgc
> 2048)
323 so
->so_urgc
= 2048; /* XXXX */
325 if (sb
->sb_rptr
< sb
->sb_wptr
) {
326 /* We can send it directly */
327 n
= slirp_send(so
, sb
->sb_rptr
, so
->so_urgc
, (MSG_OOB
)); /* |MSG_DONTWAIT)); */
330 DEBUG_MISC((dfd
, " --- sent %d bytes urgent data, %d urgent bytes left\n", n
, so
->so_urgc
));
333 * Since there's no sendv or sendtov like writev,
334 * we must copy all data to a linear buffer then
337 len
= (sb
->sb_data
+ sb
->sb_datalen
) - sb
->sb_rptr
;
338 if (len
> so
->so_urgc
) len
= so
->so_urgc
;
339 memcpy(buff
, sb
->sb_rptr
, len
);
342 n
= sb
->sb_wptr
- sb
->sb_data
;
343 if (n
> so
->so_urgc
) n
= so
->so_urgc
;
344 memcpy((buff
+ len
), sb
->sb_data
, n
);
348 n
= slirp_send(so
, buff
, len
, (MSG_OOB
)); /* |MSG_DONTWAIT)); */
351 DEBUG_ERROR((dfd
, "Didn't send all data urgently XXXXX\n"));
353 DEBUG_MISC((dfd
, " ---2 sent %d bytes urgent data, %d urgent bytes left\n", n
, so
->so_urgc
));
358 if (sb
->sb_rptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
359 sb
->sb_rptr
-= sb
->sb_datalen
;
365 * Write data from so_rcv to so's socket,
366 * updating all sbuf field as necessary
369 sowrite(struct socket
*so
)
372 struct sbuf
*sb
= &so
->so_rcv
;
376 DEBUG_CALL("sowrite");
377 DEBUG_ARG("so = %p", so
);
386 * No need to check if there's something to write,
387 * sowrite wouldn't have been called otherwise
390 iov
[0].iov_base
= sb
->sb_rptr
;
391 iov
[1].iov_base
= NULL
;
393 if (sb
->sb_rptr
< sb
->sb_wptr
) {
394 iov
[0].iov_len
= sb
->sb_wptr
- sb
->sb_rptr
;
395 /* Should never succeed, but... */
396 if (iov
[0].iov_len
> len
) iov
[0].iov_len
= len
;
399 iov
[0].iov_len
= (sb
->sb_data
+ sb
->sb_datalen
) - sb
->sb_rptr
;
400 if (iov
[0].iov_len
> len
) iov
[0].iov_len
= len
;
401 len
-= iov
[0].iov_len
;
403 iov
[1].iov_base
= sb
->sb_data
;
404 iov
[1].iov_len
= sb
->sb_wptr
- sb
->sb_data
;
405 if (iov
[1].iov_len
> len
) iov
[1].iov_len
= len
;
410 /* Check if there's urgent data to send, and if so, send it */
413 nn
= writev(so
->s
, (const struct iovec
*)iov
, n
);
415 DEBUG_MISC((dfd
, " ... wrote nn = %d bytes\n", nn
));
417 nn
= slirp_send(so
, iov
[0].iov_base
, iov
[0].iov_len
,0);
419 /* This should never happen, but people tell me it does *shrug* */
420 if (nn
< 0 && (errno
== EAGAIN
|| errno
== EINTR
))
424 DEBUG_MISC((dfd
, " --- sowrite disconnected, so->so_state = %x, errno = %d\n",
425 so
->so_state
, errno
));
427 tcp_sockclosed(sototcpcb(so
));
432 if (n
== 2 && nn
== iov
[0].iov_len
) {
434 ret
= slirp_send(so
, iov
[1].iov_base
, iov
[1].iov_len
,0);
438 DEBUG_MISC((dfd
, " ... wrote nn = %d bytes\n", nn
));
444 if (sb
->sb_rptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
445 sb
->sb_rptr
-= sb
->sb_datalen
;
448 * If in DRAIN mode, and there's no more data, set
451 if ((so
->so_state
& SS_FWDRAIN
) && sb
->sb_cc
== 0)
458 * recvfrom() a UDP socket
461 sorecvfrom(struct socket
*so
)
463 struct sockaddr_storage addr
;
464 struct sockaddr_storage saddr
, daddr
;
465 socklen_t addrlen
= sizeof(struct sockaddr_storage
);
467 DEBUG_CALL("sorecvfrom");
468 DEBUG_ARG("so = %p", so
);
470 if (so
->so_type
== IPPROTO_ICMP
) { /* This is a "ping" reply */
474 len
= recvfrom(so
->s
, buff
, 256, 0,
475 (struct sockaddr
*)&addr
, &addrlen
);
476 /* XXX Check if reply is "correct"? */
478 if(len
== -1 || len
== 0) {
479 u_char code
=ICMP_UNREACH_PORT
;
481 if(errno
== EHOSTUNREACH
) code
=ICMP_UNREACH_HOST
;
482 else if(errno
== ENETUNREACH
) code
=ICMP_UNREACH_NET
;
484 DEBUG_MISC((dfd
," udp icmp rx errno = %d-%s\n",
485 errno
,strerror(errno
)));
486 icmp_send_error(so
->so_m
, ICMP_UNREACH
, code
, 0, strerror(errno
));
488 icmp_reflect(so
->so_m
);
489 so
->so_m
= NULL
; /* Don't m_free() it again! */
491 /* No need for this socket anymore, udp_detach it */
493 } else { /* A "normal" UDP packet */
502 m
= m_get(so
->slirp
);
506 switch (so
->so_ffamily
) {
508 m
->m_data
+= IF_MAXLINKHDR
+ sizeof(struct udpiphdr
);
511 m
->m_data
+= IF_MAXLINKHDR
+ sizeof(struct ip6
)
512 + sizeof(struct udphdr
);
515 g_assert_not_reached();
520 * XXX Shouldn't FIONREAD packets destined for port 53,
521 * but I don't know the max packet size for DNS lookups
524 /* if (so->so_fport != htons(53)) { */
525 ioctlsocket(so
->s
, FIONREAD
, &n
);
528 n
= (m
->m_data
- m
->m_dat
) + m
->m_len
+ n
+ 1;
534 m
->m_len
= recvfrom(so
->s
, m
->m_data
, len
, 0,
535 (struct sockaddr
*)&addr
, &addrlen
);
536 DEBUG_MISC((dfd
, " did recvfrom %d, errno = %d-%s\n",
537 m
->m_len
, errno
,strerror(errno
)));
539 /* Report error as ICMP */
540 switch (so
->so_lfamily
) {
543 code
= ICMP_UNREACH_PORT
;
545 if (errno
== EHOSTUNREACH
) {
546 code
= ICMP_UNREACH_HOST
;
547 } else if (errno
== ENETUNREACH
) {
548 code
= ICMP_UNREACH_NET
;
551 DEBUG_MISC((dfd
, " rx error, tx icmp ICMP_UNREACH:%i\n", code
));
552 icmp_send_error(so
->so_m
, ICMP_UNREACH
, code
, 0, strerror(errno
));
555 code
= ICMP6_UNREACH_PORT
;
557 if (errno
== EHOSTUNREACH
) {
558 code
= ICMP6_UNREACH_ADDRESS
;
559 } else if (errno
== ENETUNREACH
) {
560 code
= ICMP6_UNREACH_NO_ROUTE
;
563 DEBUG_MISC((dfd
, " rx error, tx icmp6 ICMP_UNREACH:%i\n", code
));
564 icmp6_send_error(so
->so_m
, ICMP6_UNREACH
, code
);
567 g_assert_not_reached();
573 * Hack: domain name lookup will be used the most for UDP,
574 * and since they'll only be used once there's no need
575 * for the 4 minute (or whatever) timeout... So we time them
576 * out much quicker (10 seconds for now...)
579 if (so
->so_fport
== htons(53))
580 so
->so_expire
= curtime
+ SO_EXPIREFAST
;
582 so
->so_expire
= curtime
+ SO_EXPIRE
;
586 * If this packet was destined for CTL_ADDR,
587 * make it look like that's where it came from
590 sotranslate_in(so
, &saddr
);
591 daddr
= so
->lhost
.ss
;
593 switch (so
->so_ffamily
) {
595 udp_output(so
, m
, (struct sockaddr_in
*) &saddr
,
596 (struct sockaddr_in
*) &daddr
,
600 udp6_output(so
, m
, (struct sockaddr_in6
*) &saddr
,
601 (struct sockaddr_in6
*) &daddr
);
604 g_assert_not_reached();
608 } /* if ping packet */
615 sosendto(struct socket
*so
, struct mbuf
*m
)
618 struct sockaddr_storage addr
;
620 DEBUG_CALL("sosendto");
621 DEBUG_ARG("so = %p", so
);
622 DEBUG_ARG("m = %p", m
);
625 DEBUG_CALL(" sendto()ing)");
626 sotranslate_out(so
, &addr
);
628 /* Don't care what port we get */
629 ret
= sendto(so
->s
, m
->m_data
, m
->m_len
, 0,
630 (struct sockaddr
*)&addr
, sockaddr_size(&addr
));
635 * Kill the socket if there's no reply in 4 minutes,
636 * but only if it's an expirable socket
639 so
->so_expire
= curtime
+ SO_EXPIRE
;
640 so
->so_state
&= SS_PERSISTENT_MASK
;
641 so
->so_state
|= SS_ISFCONNECTED
; /* So that it gets select()ed */
646 * Listen for incoming TCP connections
649 tcp_listen(Slirp
*slirp
, uint32_t haddr
, u_int hport
, uint32_t laddr
,
650 u_int lport
, int flags
)
652 struct sockaddr_in addr
;
655 socklen_t addrlen
= sizeof(addr
);
656 memset(&addr
, 0, addrlen
);
658 DEBUG_CALL("tcp_listen");
659 DEBUG_ARG("haddr = %x", haddr
);
660 DEBUG_ARG("hport = %d", hport
);
661 DEBUG_ARG("laddr = %x", laddr
);
662 DEBUG_ARG("lport = %d", lport
);
663 DEBUG_ARG("flags = %x", flags
);
665 so
= socreate(slirp
);
670 /* Don't tcp_attach... we don't need so_snd nor so_rcv */
671 if ((so
->so_tcpcb
= tcp_newtcpcb(so
)) == NULL
) {
675 insque(so
, &slirp
->tcb
);
678 * SS_FACCEPTONCE sockets must time out.
680 if (flags
& SS_FACCEPTONCE
)
681 so
->so_tcpcb
->t_timer
[TCPT_KEEP
] = TCPTV_KEEP_INIT
*2;
683 so
->so_state
&= SS_PERSISTENT_MASK
;
684 so
->so_state
|= (SS_FACCEPTCONN
| flags
);
685 so
->so_lfamily
= AF_INET
;
686 so
->so_lport
= lport
; /* Kept in network format */
687 so
->so_laddr
.s_addr
= laddr
; /* Ditto */
689 addr
.sin_family
= AF_INET
;
690 addr
.sin_addr
.s_addr
= haddr
;
691 addr
.sin_port
= hport
;
693 if (((s
= qemu_socket(AF_INET
,SOCK_STREAM
,0)) < 0) ||
694 (socket_set_fast_reuse(s
) < 0) ||
695 (bind(s
,(struct sockaddr
*)&addr
, sizeof(addr
)) < 0) ||
697 int tmperrno
= errno
; /* Don't clobber the real reason we failed */
701 /* Restore the real errno */
703 WSASetLastError(tmperrno
);
709 qemu_setsockopt(s
, SOL_SOCKET
, SO_OOBINLINE
, &opt
, sizeof(int));
711 getsockname(s
,(struct sockaddr
*)&addr
,&addrlen
);
712 so
->so_ffamily
= AF_INET
;
713 so
->so_fport
= addr
.sin_port
;
714 if (addr
.sin_addr
.s_addr
== 0 || addr
.sin_addr
.s_addr
== loopback_addr
.s_addr
)
715 so
->so_faddr
= slirp
->vhost_addr
;
717 so
->so_faddr
= addr
.sin_addr
;
724 * Various session state calls
725 * XXX Should be #define's
726 * The socket state stuff needs work, these often get call 2 or 3
727 * times each when only 1 was needed
730 soisfconnecting(struct socket
*so
)
732 so
->so_state
&= ~(SS_NOFDREF
|SS_ISFCONNECTED
|SS_FCANTRCVMORE
|
733 SS_FCANTSENDMORE
|SS_FWDRAIN
);
734 so
->so_state
|= SS_ISFCONNECTING
; /* Clobber other states */
738 soisfconnected(struct socket
*so
)
740 so
->so_state
&= ~(SS_ISFCONNECTING
|SS_FWDRAIN
|SS_NOFDREF
);
741 so
->so_state
|= SS_ISFCONNECTED
; /* Clobber other states */
745 sofcantrcvmore(struct socket
*so
)
747 if ((so
->so_state
& SS_NOFDREF
) == 0) {
750 so
->so_state
&= ~(SS_ISFCONNECTING
);
751 if (so
->so_state
& SS_FCANTSENDMORE
) {
752 so
->so_state
&= SS_PERSISTENT_MASK
;
753 so
->so_state
|= SS_NOFDREF
; /* Don't select it */
755 so
->so_state
|= SS_FCANTRCVMORE
;
760 sofcantsendmore(struct socket
*so
)
762 if ((so
->so_state
& SS_NOFDREF
) == 0) {
763 shutdown(so
->s
,1); /* send FIN to fhost */
765 so
->so_state
&= ~(SS_ISFCONNECTING
);
766 if (so
->so_state
& SS_FCANTRCVMORE
) {
767 so
->so_state
&= SS_PERSISTENT_MASK
;
768 so
->so_state
|= SS_NOFDREF
; /* as above */
770 so
->so_state
|= SS_FCANTSENDMORE
;
775 * Set write drain mode
776 * Set CANTSENDMORE once all data has been write()n
779 sofwdrain(struct socket
*so
)
781 if (so
->so_rcv
.sb_cc
)
782 so
->so_state
|= SS_FWDRAIN
;
788 * Translate addr in host addr when it is a virtual address
790 void sotranslate_out(struct socket
*so
, struct sockaddr_storage
*addr
)
792 Slirp
*slirp
= so
->slirp
;
793 struct sockaddr_in
*sin
= (struct sockaddr_in
*)addr
;
794 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)addr
;
796 switch (addr
->ss_family
) {
798 if ((so
->so_faddr
.s_addr
& slirp
->vnetwork_mask
.s_addr
) ==
799 slirp
->vnetwork_addr
.s_addr
) {
801 if (so
->so_faddr
.s_addr
== slirp
->vnameserver_addr
.s_addr
) {
802 if (get_dns_addr(&sin
->sin_addr
) < 0) {
803 sin
->sin_addr
= loopback_addr
;
806 sin
->sin_addr
= loopback_addr
;
810 DEBUG_MISC((dfd
, " addr.sin_port=%d, "
811 "addr.sin_addr.s_addr=%.16s\n",
812 ntohs(sin
->sin_port
), inet_ntoa(sin
->sin_addr
)));
816 if (in6_equal_net(&so
->so_faddr6
, &slirp
->vprefix_addr6
,
817 slirp
->vprefix_len
)) {
818 if (in6_equal(&so
->so_faddr6
, &slirp
->vnameserver_addr6
)) {
819 /*if (get_dns_addr(&addr) < 0) {*/ /* TODO */
820 sin6
->sin6_addr
= in6addr_loopback
;
823 sin6
->sin6_addr
= in6addr_loopback
;
833 void sotranslate_in(struct socket
*so
, struct sockaddr_storage
*addr
)
835 Slirp
*slirp
= so
->slirp
;
836 struct sockaddr_in
*sin
= (struct sockaddr_in
*)addr
;
837 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)addr
;
839 switch (addr
->ss_family
) {
841 if ((so
->so_faddr
.s_addr
& slirp
->vnetwork_mask
.s_addr
) ==
842 slirp
->vnetwork_addr
.s_addr
) {
843 uint32_t inv_mask
= ~slirp
->vnetwork_mask
.s_addr
;
845 if ((so
->so_faddr
.s_addr
& inv_mask
) == inv_mask
) {
846 sin
->sin_addr
= slirp
->vhost_addr
;
847 } else if (sin
->sin_addr
.s_addr
== loopback_addr
.s_addr
||
848 so
->so_faddr
.s_addr
!= slirp
->vhost_addr
.s_addr
) {
849 sin
->sin_addr
= so
->so_faddr
;
855 if (in6_equal_net(&so
->so_faddr6
, &slirp
->vprefix_addr6
,
856 slirp
->vprefix_len
)) {
857 if (in6_equal(&sin6
->sin6_addr
, &in6addr_loopback
)
858 || !in6_equal(&so
->so_faddr6
, &slirp
->vhost_addr6
)) {
859 sin6
->sin6_addr
= so
->so_faddr6
;
870 * Translate connections from localhost to the real hostname
872 void sotranslate_accept(struct socket
*so
)
874 Slirp
*slirp
= so
->slirp
;
876 switch (so
->so_ffamily
) {
878 if (so
->so_faddr
.s_addr
== INADDR_ANY
||
879 (so
->so_faddr
.s_addr
& loopback_mask
) ==
880 (loopback_addr
.s_addr
& loopback_mask
)) {
881 so
->so_faddr
= slirp
->vhost_addr
;
886 if (in6_equal(&so
->so_faddr6
, &in6addr_any
) ||
887 in6_equal(&so
->so_faddr6
, &in6addr_loopback
)) {
888 so
->so_faddr6
= slirp
->vhost_addr6
;