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 * Remove references to so from the given message queue.
66 soqfree(struct socket
*so
, struct quehead
*qh
)
70 for (ifq
= (struct mbuf
*) qh
->qh_link
;
71 (struct quehead
*) ifq
!= qh
;
72 ifq
= ifq
->ifq_next
) {
73 if (ifq
->ifq_so
== so
) {
76 for (ifm
= ifq
->ifs_next
; ifm
!= ifq
; ifm
= ifm
->ifs_next
) {
84 * remque and free a socket, clobber cache
87 sofree(struct socket
*so
)
89 Slirp
*slirp
= so
->slirp
;
91 soqfree(so
, &slirp
->if_fastq
);
92 soqfree(so
, &slirp
->if_batchq
);
94 if (so
->so_emu
==EMU_RSH
&& so
->extra
) {
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
;
107 if(so
->so_next
&& so
->so_prev
)
108 remque(so
); /* crashes if so is not in a queue */
116 size_t sopreprbuf(struct socket
*so
, struct iovec
*iov
, int *np
)
119 struct sbuf
*sb
= &so
->so_snd
;
120 int len
= sb
->sb_datalen
- sb
->sb_cc
;
121 int mss
= so
->so_tcpcb
->t_maxseg
;
123 DEBUG_CALL("sopreprbuf");
124 DEBUG_ARG("so = %p", so
);
129 iov
[0].iov_base
= sb
->sb_wptr
;
130 iov
[1].iov_base
= NULL
;
132 if (sb
->sb_wptr
< sb
->sb_rptr
) {
133 iov
[0].iov_len
= sb
->sb_rptr
- sb
->sb_wptr
;
134 /* Should never succeed, but... */
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
;
141 iov
[0].iov_len
= (sb
->sb_data
+ sb
->sb_datalen
) - sb
->sb_wptr
;
142 /* Should never succeed, but... */
143 if (iov
[0].iov_len
> len
) iov
[0].iov_len
= len
;
144 len
-= iov
[0].iov_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
;
153 if (iov
[1].iov_len
> lss
) {
154 iov
[1].iov_len
-= lss
;
157 lss
-= iov
[1].iov_len
;
158 iov
[0].iov_len
-= lss
;
164 if (iov
[0].iov_len
> mss
)
165 iov
[0].iov_len
-= iov
[0].iov_len
%mss
;
172 return iov
[0].iov_len
+ (n
- 1) * iov
[1].iov_len
;
176 * Read from so's socket into sb_snd, updating all relevant sbuf fields
177 * NOTE: This will only be called if it is select()ed for reading, so
178 * a read() of 0 (or less) means it's disconnected
181 soread(struct socket
*so
)
184 struct sbuf
*sb
= &so
->so_snd
;
187 DEBUG_CALL("soread");
188 DEBUG_ARG("so = %p", so
);
191 * No need to check if there's enough room to read.
192 * soread wouldn't have been called if there weren't
194 sopreprbuf(so
, iov
, &n
);
197 nn
= readv(so
->s
, (struct iovec
*)iov
, n
);
198 DEBUG_MISC((dfd
, " ... read nn = %d bytes\n", nn
));
200 nn
= qemu_recv(so
->s
, iov
[0].iov_base
, iov
[0].iov_len
,0);
203 if (nn
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
207 socklen_t slen
= sizeof err
;
211 getsockopt(so
->s
, SOL_SOCKET
, SO_ERROR
,
215 DEBUG_MISC((dfd
, " --- soread() disconnected, nn = %d, errno = %d-%s\n", nn
, errno
,strerror(errno
)));
218 if (err
== ECONNRESET
|| err
== ECONNREFUSED
219 || err
== ENOTCONN
|| err
== EPIPE
) {
220 tcp_drop(sototcpcb(so
), err
);
222 tcp_sockclosed(sototcpcb(so
));
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 won't (shouldn't) happen, since it didn't happen above
238 if (n
== 2 && nn
== iov
[0].iov_len
) {
240 ret
= qemu_recv(so
->s
, iov
[1].iov_base
, iov
[1].iov_len
,0);
245 DEBUG_MISC((dfd
, " ... read nn = %d bytes\n", nn
));
251 if (sb
->sb_wptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
252 sb
->sb_wptr
-= sb
->sb_datalen
;
256 int soreadbuf(struct socket
*so
, const char *buf
, int size
)
258 int n
, nn
, copy
= size
;
259 struct sbuf
*sb
= &so
->so_snd
;
262 DEBUG_CALL("soreadbuf");
263 DEBUG_ARG("so = %p", so
);
266 * No need to check if there's enough room to read.
267 * soread wouldn't have been called if there weren't
269 if (sopreprbuf(so
, iov
, &n
) < size
)
272 nn
= MIN(iov
[0].iov_len
, copy
);
273 memcpy(iov
[0].iov_base
, buf
, nn
);
281 memcpy(iov
[1].iov_base
, buf
, copy
);
287 if (sb
->sb_wptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
288 sb
->sb_wptr
-= sb
->sb_datalen
;
293 tcp_sockclosed(sototcpcb(so
));
294 fprintf(stderr
, "soreadbuf buffer to small");
301 * When the socket is created, we set it SO_OOBINLINE,
302 * so when OOB data arrives, we soread() it and everything
303 * in the send buffer is sent as urgent data
306 sorecvoob(struct socket
*so
)
308 struct tcpcb
*tp
= sototcpcb(so
);
311 DEBUG_CALL("sorecvoob");
312 DEBUG_ARG("so = %p", so
);
315 * We take a guess at how much urgent data has arrived.
316 * In most situations, when urgent data arrives, the next
317 * read() should get all the urgent data. This guess will
318 * be wrong however if more data arrives just after the
319 * urgent data, or the read() doesn't return all the
324 tp
->snd_up
= tp
->snd_una
+ so
->so_snd
.sb_cc
;
335 * There's a lot duplicated code here, but...
338 sosendoob(struct socket
*so
)
340 struct sbuf
*sb
= &so
->so_rcv
;
341 char buff
[2048]; /* XXX Shouldn't be sending more oob data than this */
345 DEBUG_CALL("sosendoob");
346 DEBUG_ARG("so = %p", so
);
347 DEBUG_ARG("sb->sb_cc = %d", sb
->sb_cc
);
349 if (so
->so_urgc
> 2048)
350 so
->so_urgc
= 2048; /* XXXX */
352 if (sb
->sb_rptr
< sb
->sb_wptr
) {
353 /* We can send it directly */
354 n
= slirp_send(so
, sb
->sb_rptr
, so
->so_urgc
, (MSG_OOB
)); /* |MSG_DONTWAIT)); */
357 * Since there's no sendv or sendtov like writev,
358 * we must copy all data to a linear buffer then
361 uint32_t urgc
= so
->so_urgc
;
362 len
= (sb
->sb_data
+ sb
->sb_datalen
) - sb
->sb_rptr
;
366 memcpy(buff
, sb
->sb_rptr
, len
);
369 n
= sb
->sb_wptr
- sb
->sb_data
;
373 memcpy((buff
+ len
), sb
->sb_data
, n
);
376 n
= slirp_send(so
, buff
, len
, (MSG_OOB
)); /* |MSG_DONTWAIT)); */
381 DEBUG_ERROR((dfd
, "Didn't send all data urgently XXXXX\n"));
388 DEBUG_MISC((dfd
, " ---2 sent %d bytes urgent data, %d urgent bytes left\n", n
, so
->so_urgc
));
392 if (sb
->sb_rptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
393 sb
->sb_rptr
-= sb
->sb_datalen
;
399 * Write data from so_rcv to so's socket,
400 * updating all sbuf field as necessary
403 sowrite(struct socket
*so
)
406 struct sbuf
*sb
= &so
->so_rcv
;
410 DEBUG_CALL("sowrite");
411 DEBUG_ARG("so = %p", so
);
414 uint32_t expected
= so
->so_urgc
;
415 if (sosendoob(so
) < expected
) {
416 /* Treat a short write as a fatal error too,
417 * rather than continuing on and sending the urgent
418 * data as if it were non-urgent and leaving the
419 * so_urgc count wrong.
421 goto err_disconnected
;
428 * No need to check if there's something to write,
429 * sowrite wouldn't have been called otherwise
432 iov
[0].iov_base
= sb
->sb_rptr
;
433 iov
[1].iov_base
= NULL
;
435 if (sb
->sb_rptr
< sb
->sb_wptr
) {
436 iov
[0].iov_len
= sb
->sb_wptr
- sb
->sb_rptr
;
437 /* Should never succeed, but... */
438 if (iov
[0].iov_len
> len
) iov
[0].iov_len
= len
;
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
;
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
;
452 /* Check if there's urgent data to send, and if so, send it */
455 nn
= writev(so
->s
, (const struct iovec
*)iov
, n
);
457 DEBUG_MISC((dfd
, " ... wrote nn = %d bytes\n", nn
));
459 nn
= slirp_send(so
, iov
[0].iov_base
, iov
[0].iov_len
,0);
461 /* This should never happen, but people tell me it does *shrug* */
462 if (nn
< 0 && (errno
== EAGAIN
|| errno
== EINTR
))
466 goto err_disconnected
;
470 if (n
== 2 && nn
== iov
[0].iov_len
) {
472 ret
= slirp_send(so
, iov
[1].iov_base
, iov
[1].iov_len
,0);
476 DEBUG_MISC((dfd
, " ... wrote nn = %d bytes\n", nn
));
482 if (sb
->sb_rptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
483 sb
->sb_rptr
-= sb
->sb_datalen
;
486 * If in DRAIN mode, and there's no more data, set
489 if ((so
->so_state
& SS_FWDRAIN
) && sb
->sb_cc
== 0)
495 DEBUG_MISC((dfd
, " --- sowrite disconnected, so->so_state = %x, errno = %d\n",
496 so
->so_state
, errno
));
498 tcp_sockclosed(sototcpcb(so
));
503 * recvfrom() a UDP socket
506 sorecvfrom(struct socket
*so
)
508 struct sockaddr_storage addr
;
509 struct sockaddr_storage saddr
, daddr
;
510 socklen_t addrlen
= sizeof(struct sockaddr_storage
);
512 DEBUG_CALL("sorecvfrom");
513 DEBUG_ARG("so = %p", so
);
515 if (so
->so_type
== IPPROTO_ICMP
) { /* This is a "ping" reply */
519 len
= recvfrom(so
->s
, buff
, 256, 0,
520 (struct sockaddr
*)&addr
, &addrlen
);
521 /* XXX Check if reply is "correct"? */
523 if(len
== -1 || len
== 0) {
524 u_char code
=ICMP_UNREACH_PORT
;
526 if(errno
== EHOSTUNREACH
) code
=ICMP_UNREACH_HOST
;
527 else if(errno
== ENETUNREACH
) code
=ICMP_UNREACH_NET
;
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
));
533 icmp_reflect(so
->so_m
);
534 so
->so_m
= NULL
; /* Don't m_free() it again! */
536 /* No need for this socket anymore, udp_detach it */
538 } else { /* A "normal" UDP packet */
547 m
= m_get(so
->slirp
);
551 switch (so
->so_ffamily
) {
553 m
->m_data
+= IF_MAXLINKHDR
+ sizeof(struct udpiphdr
);
556 m
->m_data
+= IF_MAXLINKHDR
+ sizeof(struct ip6
)
557 + sizeof(struct udphdr
);
560 g_assert_not_reached();
565 * XXX Shouldn't FIONREAD packets destined for port 53,
566 * but I don't know the max packet size for DNS lookups
569 /* if (so->so_fport != htons(53)) { */
570 ioctlsocket(so
->s
, FIONREAD
, &n
);
573 n
= (m
->m_data
- m
->m_dat
) + m
->m_len
+ n
+ 1;
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
)));
584 /* Report error as ICMP */
585 switch (so
->so_lfamily
) {
588 code
= ICMP_UNREACH_PORT
;
590 if (errno
== EHOSTUNREACH
) {
591 code
= ICMP_UNREACH_HOST
;
592 } else if (errno
== ENETUNREACH
) {
593 code
= ICMP_UNREACH_NET
;
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
));
600 code
= ICMP6_UNREACH_PORT
;
602 if (errno
== EHOSTUNREACH
) {
603 code
= ICMP6_UNREACH_ADDRESS
;
604 } else if (errno
== ENETUNREACH
) {
605 code
= ICMP6_UNREACH_NO_ROUTE
;
608 DEBUG_MISC((dfd
, " rx error, tx icmp6 ICMP_UNREACH:%i\n", code
));
609 icmp6_send_error(so
->so_m
, ICMP6_UNREACH
, code
);
612 g_assert_not_reached();
618 * Hack: domain name lookup will be used the most for UDP,
619 * and since they'll only be used once there's no need
620 * for the 4 minute (or whatever) timeout... So we time them
621 * out much quicker (10 seconds for now...)
624 if (so
->so_fport
== htons(53))
625 so
->so_expire
= curtime
+ SO_EXPIREFAST
;
627 so
->so_expire
= curtime
+ SO_EXPIRE
;
631 * If this packet was destined for CTL_ADDR,
632 * make it look like that's where it came from
635 sotranslate_in(so
, &saddr
);
636 daddr
= so
->lhost
.ss
;
638 switch (so
->so_ffamily
) {
640 udp_output(so
, m
, (struct sockaddr_in
*) &saddr
,
641 (struct sockaddr_in
*) &daddr
,
645 udp6_output(so
, m
, (struct sockaddr_in6
*) &saddr
,
646 (struct sockaddr_in6
*) &daddr
);
649 g_assert_not_reached();
653 } /* if ping packet */
660 sosendto(struct socket
*so
, struct mbuf
*m
)
663 struct sockaddr_storage addr
;
665 DEBUG_CALL("sosendto");
666 DEBUG_ARG("so = %p", so
);
667 DEBUG_ARG("m = %p", m
);
670 DEBUG_CALL(" sendto()ing)");
671 sotranslate_out(so
, &addr
);
673 /* Don't care what port we get */
674 ret
= sendto(so
->s
, m
->m_data
, m
->m_len
, 0,
675 (struct sockaddr
*)&addr
, sockaddr_size(&addr
));
680 * Kill the socket if there's no reply in 4 minutes,
681 * but only if it's an expirable socket
684 so
->so_expire
= curtime
+ SO_EXPIRE
;
685 so
->so_state
&= SS_PERSISTENT_MASK
;
686 so
->so_state
|= SS_ISFCONNECTED
; /* So that it gets select()ed */
691 * Listen for incoming TCP connections
694 tcp_listen(Slirp
*slirp
, uint32_t haddr
, u_int hport
, uint32_t laddr
,
695 u_int lport
, int flags
)
697 struct sockaddr_in addr
;
700 socklen_t addrlen
= sizeof(addr
);
701 memset(&addr
, 0, addrlen
);
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
);
710 so
= socreate(slirp
);
715 /* Don't tcp_attach... we don't need so_snd nor so_rcv */
716 if ((so
->so_tcpcb
= tcp_newtcpcb(so
)) == NULL
) {
720 insque(so
, &slirp
->tcb
);
723 * SS_FACCEPTONCE sockets must time out.
725 if (flags
& SS_FACCEPTONCE
)
726 so
->so_tcpcb
->t_timer
[TCPT_KEEP
] = TCPTV_KEEP_INIT
*2;
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
; /* Kept in network format */
732 so
->so_laddr
.s_addr
= laddr
; /* Ditto */
734 addr
.sin_family
= AF_INET
;
735 addr
.sin_addr
.s_addr
= haddr
;
736 addr
.sin_port
= hport
;
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) ||
742 int tmperrno
= errno
; /* Don't clobber the real reason we failed */
748 /* Restore the real errno */
750 WSASetLastError(tmperrno
);
756 qemu_setsockopt(s
, SOL_SOCKET
, SO_OOBINLINE
, &opt
, sizeof(int));
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
;
764 so
->so_faddr
= addr
.sin_addr
;
771 * Various session state calls
772 * XXX Should be #define's
773 * The socket state stuff needs work, these often get call 2 or 3
774 * times each when only 1 was needed
777 soisfconnecting(struct socket
*so
)
779 so
->so_state
&= ~(SS_NOFDREF
|SS_ISFCONNECTED
|SS_FCANTRCVMORE
|
780 SS_FCANTSENDMORE
|SS_FWDRAIN
);
781 so
->so_state
|= SS_ISFCONNECTING
; /* Clobber other states */
785 soisfconnected(struct socket
*so
)
787 so
->so_state
&= ~(SS_ISFCONNECTING
|SS_FWDRAIN
|SS_NOFDREF
);
788 so
->so_state
|= SS_ISFCONNECTED
; /* Clobber other states */
792 sofcantrcvmore(struct socket
*so
)
794 if ((so
->so_state
& SS_NOFDREF
) == 0) {
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
; /* Don't select it */
802 so
->so_state
|= SS_FCANTRCVMORE
;
807 sofcantsendmore(struct socket
*so
)
809 if ((so
->so_state
& SS_NOFDREF
) == 0) {
810 shutdown(so
->s
,1); /* send FIN to fhost */
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
; /* as above */
817 so
->so_state
|= SS_FCANTSENDMORE
;
822 * Set write drain mode
823 * Set CANTSENDMORE once all data has been write()n
826 sofwdrain(struct socket
*so
)
828 if (so
->so_rcv
.sb_cc
)
829 so
->so_state
|= SS_FWDRAIN
;
835 * Translate addr in host addr when it is a virtual address
837 void sotranslate_out(struct socket
*so
, struct sockaddr_storage
*addr
)
839 Slirp
*slirp
= so
->slirp
;
840 struct sockaddr_in
*sin
= (struct sockaddr_in
*)addr
;
841 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)addr
;
843 switch (addr
->ss_family
) {
845 if ((so
->so_faddr
.s_addr
& slirp
->vnetwork_mask
.s_addr
) ==
846 slirp
->vnetwork_addr
.s_addr
) {
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
;
853 sin
->sin_addr
= loopback_addr
;
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
)));
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
)) {
867 if (get_dns6_addr(&sin6
->sin6_addr
, &scope_id
) >= 0) {
868 sin6
->sin6_scope_id
= scope_id
;
870 sin6
->sin6_addr
= in6addr_loopback
;
873 sin6
->sin6_addr
= in6addr_loopback
;
883 void sotranslate_in(struct socket
*so
, struct sockaddr_storage
*addr
)
885 Slirp
*slirp
= so
->slirp
;
886 struct sockaddr_in
*sin
= (struct sockaddr_in
*)addr
;
887 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)addr
;
889 switch (addr
->ss_family
) {
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
;
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
;
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
;
920 * Translate connections from localhost to the real hostname
922 void sotranslate_accept(struct socket
*so
)
924 Slirp
*slirp
= so
->slirp
;
926 switch (so
->so_ffamily
) {
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
;
936 if (in6_equal(&so
->so_faddr6
, &in6addr_any
) ||
937 in6_equal(&so
->so_faddr6
, &in6addr_loopback
)) {
938 so
->so_faddr6
= slirp
->vhost_addr6
;