2 * Copyright (c) 1995 Danny Gasparovski.
4 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
11 #include <sys/filio.h>
14 static void sofcantrcvmore(struct socket
*so
);
15 static void sofcantsendmore(struct socket
*so
);
17 struct socket
*solookup(struct socket
**last
, struct socket
*head
,
18 struct sockaddr_storage
*lhost
, struct sockaddr_storage
*fhost
)
20 struct socket
*so
= *last
;
23 if (so
!= head
&& sockaddr_equal(&(so
->lhost
.ss
), lhost
)
24 && (!fhost
|| sockaddr_equal(&so
->fhost
.ss
, fhost
))) {
28 for (so
= head
->so_next
; so
!= head
; so
= so
->so_next
) {
29 if (sockaddr_equal(&(so
->lhost
.ss
), lhost
)
30 && (!fhost
|| sockaddr_equal(&so
->fhost
.ss
, fhost
))) {
36 return (struct socket
*)NULL
;
40 * Create a new socket, initialise the fields
41 * It is the responsibility of the caller to
42 * insque() it into the correct linked-list
45 socreate(Slirp
*slirp
)
47 struct socket
*so
= g_new(struct socket
, 1);
49 memset(so
, 0, sizeof(struct socket
));
50 so
->so_state
= SS_NOFDREF
;
59 * Remove references to so from the given message queue.
62 soqfree(struct socket
*so
, struct quehead
*qh
)
66 for (ifq
= (struct mbuf
*) qh
->qh_link
;
67 (struct quehead
*) ifq
!= qh
;
68 ifq
= ifq
->ifq_next
) {
69 if (ifq
->ifq_so
== so
) {
72 for (ifm
= ifq
->ifs_next
; ifm
!= ifq
; ifm
= ifm
->ifs_next
) {
80 * remque and free a socket, clobber cache
83 sofree(struct socket
*so
)
85 Slirp
*slirp
= so
->slirp
;
87 soqfree(so
, &slirp
->if_fastq
);
88 soqfree(so
, &slirp
->if_batchq
);
90 if (so
== slirp
->tcp_last_so
) {
91 slirp
->tcp_last_so
= &slirp
->tcb
;
92 } else if (so
== slirp
->udp_last_so
) {
93 slirp
->udp_last_so
= &slirp
->udb
;
94 } else if (so
== slirp
->icmp_last_so
) {
95 slirp
->icmp_last_so
= &slirp
->icmp
;
99 if(so
->so_next
&& so
->so_prev
)
100 remque(so
); /* crashes if so is not in a queue */
108 size_t sopreprbuf(struct socket
*so
, struct iovec
*iov
, int *np
)
111 struct sbuf
*sb
= &so
->so_snd
;
112 int len
= sb
->sb_datalen
- sb
->sb_cc
;
113 int mss
= so
->so_tcpcb
->t_maxseg
;
115 DEBUG_CALL("sopreprbuf");
116 DEBUG_ARG("so = %p", so
);
121 iov
[0].iov_base
= sb
->sb_wptr
;
122 iov
[1].iov_base
= NULL
;
124 if (sb
->sb_wptr
< sb
->sb_rptr
) {
125 iov
[0].iov_len
= sb
->sb_rptr
- sb
->sb_wptr
;
126 /* Should never succeed, but... */
127 if (iov
[0].iov_len
> len
)
128 iov
[0].iov_len
= len
;
129 if (iov
[0].iov_len
> mss
)
130 iov
[0].iov_len
-= iov
[0].iov_len
%mss
;
133 iov
[0].iov_len
= (sb
->sb_data
+ sb
->sb_datalen
) - sb
->sb_wptr
;
134 /* Should never succeed, but... */
135 if (iov
[0].iov_len
> len
) iov
[0].iov_len
= len
;
136 len
-= iov
[0].iov_len
;
138 iov
[1].iov_base
= sb
->sb_data
;
139 iov
[1].iov_len
= sb
->sb_rptr
- sb
->sb_data
;
140 if(iov
[1].iov_len
> len
)
141 iov
[1].iov_len
= len
;
142 total
= iov
[0].iov_len
+ iov
[1].iov_len
;
145 if (iov
[1].iov_len
> lss
) {
146 iov
[1].iov_len
-= lss
;
149 lss
-= iov
[1].iov_len
;
150 iov
[0].iov_len
-= lss
;
156 if (iov
[0].iov_len
> mss
)
157 iov
[0].iov_len
-= iov
[0].iov_len
%mss
;
164 return iov
[0].iov_len
+ (n
- 1) * iov
[1].iov_len
;
168 * Read from so's socket into sb_snd, updating all relevant sbuf fields
169 * NOTE: This will only be called if it is select()ed for reading, so
170 * a read() of 0 (or less) means it's disconnected
173 soread(struct socket
*so
)
176 struct sbuf
*sb
= &so
->so_snd
;
179 DEBUG_CALL("soread");
180 DEBUG_ARG("so = %p", so
);
183 * No need to check if there's enough room to read.
184 * soread wouldn't have been called if there weren't
186 sopreprbuf(so
, iov
, &n
);
188 nn
= slirp_recv(so
->s
, iov
[0].iov_base
, iov
[0].iov_len
,0);
190 if (nn
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
194 socklen_t elen
= sizeof err
;
195 struct sockaddr_storage addr
;
196 struct sockaddr
*paddr
= (struct sockaddr
*) &addr
;
197 socklen_t alen
= sizeof addr
;
201 if (getpeername(so
->s
, paddr
, &alen
) < 0) {
204 slirp_getsockopt(so
->s
, SOL_SOCKET
, SO_ERROR
,
209 DEBUG_MISC(" --- soread() disconnected, nn = %d, errno = %d-%s",
210 nn
, errno
,strerror(errno
));
213 if (err
== ECONNRESET
|| err
== ECONNREFUSED
214 || err
== ENOTCONN
|| err
== EPIPE
) {
215 tcp_drop(sototcpcb(so
), err
);
217 tcp_sockclosed(sototcpcb(so
));
224 * If there was no error, try and read the second time round
225 * We read again if n = 2 (ie, there's another part of the buffer)
226 * and we read as much as we could in the first read
227 * We don't test for <= 0 this time, because there legitimately
228 * might not be any more data (since the socket is non-blocking),
229 * a close will be detected on next iteration.
230 * A return of -1 won't (shouldn't) happen, since it didn't happen above
232 if (n
== 2 && nn
== iov
[0].iov_len
) {
234 ret
= slirp_recv(so
->s
, iov
[1].iov_base
, iov
[1].iov_len
,0);
239 DEBUG_MISC(" ... read nn = %d bytes", nn
);
244 if (sb
->sb_wptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
245 sb
->sb_wptr
-= sb
->sb_datalen
;
249 int soreadbuf(struct socket
*so
, const char *buf
, int size
)
251 int n
, nn
, copy
= size
;
252 struct sbuf
*sb
= &so
->so_snd
;
255 DEBUG_CALL("soreadbuf");
256 DEBUG_ARG("so = %p", so
);
259 * No need to check if there's enough room to read.
260 * soread wouldn't have been called if there weren't
262 if (sopreprbuf(so
, iov
, &n
) < size
)
265 nn
= MIN(iov
[0].iov_len
, copy
);
266 memcpy(iov
[0].iov_base
, buf
, nn
);
274 memcpy(iov
[1].iov_base
, buf
, copy
);
280 if (sb
->sb_wptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
281 sb
->sb_wptr
-= sb
->sb_datalen
;
286 tcp_sockclosed(sototcpcb(so
));
287 g_critical("soreadbuf buffer too small");
294 * When the socket is created, we set it SO_OOBINLINE,
295 * so when OOB data arrives, we soread() it and everything
296 * in the send buffer is sent as urgent data
299 sorecvoob(struct socket
*so
)
301 struct tcpcb
*tp
= sototcpcb(so
);
304 DEBUG_CALL("sorecvoob");
305 DEBUG_ARG("so = %p", so
);
308 * We take a guess at how much urgent data has arrived.
309 * In most situations, when urgent data arrives, the next
310 * read() should get all the urgent data. This guess will
311 * be wrong however if more data arrives just after the
312 * urgent data, or the read() doesn't return all the
317 tp
->snd_up
= tp
->snd_una
+ so
->so_snd
.sb_cc
;
328 * There's a lot duplicated code here, but...
331 sosendoob(struct socket
*so
)
333 struct sbuf
*sb
= &so
->so_rcv
;
334 char buff
[2048]; /* XXX Shouldn't be sending more oob data than this */
338 DEBUG_CALL("sosendoob");
339 DEBUG_ARG("so = %p", so
);
340 DEBUG_ARG("sb->sb_cc = %d", sb
->sb_cc
);
342 if (so
->so_urgc
> 2048)
343 so
->so_urgc
= 2048; /* XXXX */
345 if (sb
->sb_rptr
< sb
->sb_wptr
) {
346 /* We can send it directly */
347 n
= slirp_send(so
, sb
->sb_rptr
, so
->so_urgc
, (MSG_OOB
)); /* |MSG_DONTWAIT)); */
350 * Since there's no sendv or sendtov like writev,
351 * we must copy all data to a linear buffer then
354 uint32_t urgc
= so
->so_urgc
;
355 int len
= (sb
->sb_data
+ sb
->sb_datalen
) - sb
->sb_rptr
;
359 memcpy(buff
, sb
->sb_rptr
, len
);
362 n
= sb
->sb_wptr
- sb
->sb_data
;
366 memcpy((buff
+ len
), sb
->sb_data
, n
);
369 n
= slirp_send(so
, buff
, len
, (MSG_OOB
)); /* |MSG_DONTWAIT)); */
372 DEBUG_ERROR("Didn't send all data urgently XXXXX");
381 DEBUG_MISC(" ---2 sent %d bytes urgent data, %d urgent bytes left", n
, so
->so_urgc
);
385 if (sb
->sb_rptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
386 sb
->sb_rptr
-= sb
->sb_datalen
;
392 * Write data from so_rcv to so's socket,
393 * updating all sbuf field as necessary
396 sowrite(struct socket
*so
)
399 struct sbuf
*sb
= &so
->so_rcv
;
403 DEBUG_CALL("sowrite");
404 DEBUG_ARG("so = %p", so
);
407 uint32_t expected
= so
->so_urgc
;
408 if (sosendoob(so
) < expected
) {
409 /* Treat a short write as a fatal error too,
410 * rather than continuing on and sending the urgent
411 * data as if it were non-urgent and leaving the
412 * so_urgc count wrong.
414 goto err_disconnected
;
421 * No need to check if there's something to write,
422 * sowrite wouldn't have been called otherwise
425 iov
[0].iov_base
= sb
->sb_rptr
;
426 iov
[1].iov_base
= NULL
;
428 if (sb
->sb_rptr
< sb
->sb_wptr
) {
429 iov
[0].iov_len
= sb
->sb_wptr
- sb
->sb_rptr
;
430 /* Should never succeed, but... */
431 if (iov
[0].iov_len
> len
) iov
[0].iov_len
= len
;
434 iov
[0].iov_len
= (sb
->sb_data
+ sb
->sb_datalen
) - sb
->sb_rptr
;
435 if (iov
[0].iov_len
> len
) iov
[0].iov_len
= len
;
436 len
-= iov
[0].iov_len
;
438 iov
[1].iov_base
= sb
->sb_data
;
439 iov
[1].iov_len
= sb
->sb_wptr
- sb
->sb_data
;
440 if (iov
[1].iov_len
> len
) iov
[1].iov_len
= len
;
445 /* Check if there's urgent data to send, and if so, send it */
447 nn
= slirp_send(so
, iov
[0].iov_base
, iov
[0].iov_len
,0);
448 /* This should never happen, but people tell me it does *shrug* */
449 if (nn
< 0 && (errno
== EAGAIN
|| errno
== EINTR
))
453 goto err_disconnected
;
456 if (n
== 2 && nn
== iov
[0].iov_len
) {
458 ret
= slirp_send(so
, iov
[1].iov_base
, iov
[1].iov_len
,0);
462 DEBUG_MISC(" ... wrote nn = %d bytes", nn
);
467 if (sb
->sb_rptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
468 sb
->sb_rptr
-= sb
->sb_datalen
;
471 * If in DRAIN mode, and there's no more data, set
474 if ((so
->so_state
& SS_FWDRAIN
) && sb
->sb_cc
== 0)
480 DEBUG_MISC(" --- sowrite disconnected, so->so_state = %x, errno = %d",
481 so
->so_state
, errno
);
483 tcp_sockclosed(sototcpcb(so
));
488 * recvfrom() a UDP socket
491 sorecvfrom(struct socket
*so
)
493 struct sockaddr_storage addr
;
494 struct sockaddr_storage saddr
, daddr
;
495 socklen_t addrlen
= sizeof(struct sockaddr_storage
);
497 DEBUG_CALL("sorecvfrom");
498 DEBUG_ARG("so = %p", so
);
500 if (so
->so_type
== IPPROTO_ICMP
) { /* This is a "ping" reply */
504 len
= recvfrom(so
->s
, buff
, 256, 0,
505 (struct sockaddr
*)&addr
, &addrlen
);
506 /* XXX Check if reply is "correct"? */
508 if(len
== -1 || len
== 0) {
509 uint8_t code
=ICMP_UNREACH_PORT
;
511 if(errno
== EHOSTUNREACH
) code
=ICMP_UNREACH_HOST
;
512 else if(errno
== ENETUNREACH
) code
=ICMP_UNREACH_NET
;
514 DEBUG_MISC(" udp icmp rx errno = %d-%s",
515 errno
,strerror(errno
));
516 icmp_send_error(so
->so_m
, ICMP_UNREACH
, code
, 0, strerror(errno
));
518 icmp_reflect(so
->so_m
);
519 so
->so_m
= NULL
; /* Don't m_free() it again! */
521 /* No need for this socket anymore, udp_detach it */
523 } else { /* A "normal" UDP packet */
532 m
= m_get(so
->slirp
);
536 switch (so
->so_ffamily
) {
538 m
->m_data
+= IF_MAXLINKHDR
+ sizeof(struct udpiphdr
);
541 m
->m_data
+= IF_MAXLINKHDR
+ sizeof(struct ip6
)
542 + sizeof(struct udphdr
);
545 g_assert_not_reached();
550 * XXX Shouldn't FIONREAD packets destined for port 53,
551 * but I don't know the max packet size for DNS lookups
554 /* if (so->so_fport != htons(53)) { */
555 slirp_ioctlsocket(so
->s
, FIONREAD
, &n
);
558 n
= (m
->m_data
- m
->m_dat
) + m
->m_len
+ n
+ 1;
564 m
->m_len
= recvfrom(so
->s
, m
->m_data
, len
, 0,
565 (struct sockaddr
*)&addr
, &addrlen
);
566 DEBUG_MISC(" did recvfrom %d, errno = %d-%s",
567 m
->m_len
, errno
,strerror(errno
));
569 /* Report error as ICMP */
570 switch (so
->so_lfamily
) {
573 code
= ICMP_UNREACH_PORT
;
575 if (errno
== EHOSTUNREACH
) {
576 code
= ICMP_UNREACH_HOST
;
577 } else if (errno
== ENETUNREACH
) {
578 code
= ICMP_UNREACH_NET
;
581 DEBUG_MISC(" rx error, tx icmp ICMP_UNREACH:%i", code
);
582 icmp_send_error(so
->so_m
, ICMP_UNREACH
, code
, 0, strerror(errno
));
585 code
= ICMP6_UNREACH_PORT
;
587 if (errno
== EHOSTUNREACH
) {
588 code
= ICMP6_UNREACH_ADDRESS
;
589 } else if (errno
== ENETUNREACH
) {
590 code
= ICMP6_UNREACH_NO_ROUTE
;
593 DEBUG_MISC(" rx error, tx icmp6 ICMP_UNREACH:%i", code
);
594 icmp6_send_error(so
->so_m
, ICMP6_UNREACH
, code
);
597 g_assert_not_reached();
603 * Hack: domain name lookup will be used the most for UDP,
604 * and since they'll only be used once there's no need
605 * for the 4 minute (or whatever) timeout... So we time them
606 * out much quicker (10 seconds for now...)
609 if (so
->so_fport
== htons(53))
610 so
->so_expire
= curtime
+ SO_EXPIREFAST
;
612 so
->so_expire
= curtime
+ SO_EXPIRE
;
616 * If this packet was destined for CTL_ADDR,
617 * make it look like that's where it came from
620 sotranslate_in(so
, &saddr
);
621 daddr
= so
->lhost
.ss
;
623 switch (so
->so_ffamily
) {
625 udp_output(so
, m
, (struct sockaddr_in
*) &saddr
,
626 (struct sockaddr_in
*) &daddr
,
630 udp6_output(so
, m
, (struct sockaddr_in6
*) &saddr
,
631 (struct sockaddr_in6
*) &daddr
);
634 g_assert_not_reached();
638 } /* if ping packet */
645 sosendto(struct socket
*so
, struct mbuf
*m
)
648 struct sockaddr_storage addr
;
650 DEBUG_CALL("sosendto");
651 DEBUG_ARG("so = %p", so
);
652 DEBUG_ARG("m = %p", m
);
655 DEBUG_CALL(" sendto()ing)");
656 sotranslate_out(so
, &addr
);
658 /* Don't care what port we get */
659 ret
= sendto(so
->s
, m
->m_data
, m
->m_len
, 0,
660 (struct sockaddr
*)&addr
, sockaddr_size(&addr
));
665 * Kill the socket if there's no reply in 4 minutes,
666 * but only if it's an expirable socket
669 so
->so_expire
= curtime
+ SO_EXPIRE
;
670 so
->so_state
&= SS_PERSISTENT_MASK
;
671 so
->so_state
|= SS_ISFCONNECTED
; /* So that it gets select()ed */
676 * Listen for incoming TCP connections
679 tcp_listen(Slirp
*slirp
, uint32_t haddr
, unsigned hport
, uint32_t laddr
,
680 unsigned lport
, int flags
)
682 struct sockaddr_in addr
;
685 socklen_t addrlen
= sizeof(addr
);
686 memset(&addr
, 0, addrlen
);
688 DEBUG_CALL("tcp_listen");
689 DEBUG_ARG("haddr = %s", inet_ntoa((struct in_addr
){.s_addr
= haddr
}));
690 DEBUG_ARG("hport = %d", ntohs(hport
));
691 DEBUG_ARG("laddr = %s", inet_ntoa((struct in_addr
){.s_addr
= laddr
}));
692 DEBUG_ARG("lport = %d", ntohs(lport
));
693 DEBUG_ARG("flags = %x", flags
);
695 so
= socreate(slirp
);
697 /* Don't tcp_attach... we don't need so_snd nor so_rcv */
698 if ((so
->so_tcpcb
= tcp_newtcpcb(so
)) == NULL
) {
702 insque(so
, &slirp
->tcb
);
705 * SS_FACCEPTONCE sockets must time out.
707 if (flags
& SS_FACCEPTONCE
)
708 so
->so_tcpcb
->t_timer
[TCPT_KEEP
] = TCPTV_KEEP_INIT
*2;
710 so
->so_state
&= SS_PERSISTENT_MASK
;
711 so
->so_state
|= (SS_FACCEPTCONN
| flags
);
712 so
->so_lfamily
= AF_INET
;
713 so
->so_lport
= lport
; /* Kept in network format */
714 so
->so_laddr
.s_addr
= laddr
; /* Ditto */
716 addr
.sin_family
= AF_INET
;
717 addr
.sin_addr
.s_addr
= haddr
;
718 addr
.sin_port
= hport
;
720 if (((s
= slirp_socket(AF_INET
,SOCK_STREAM
,0)) < 0) ||
721 (slirp_socket_set_fast_reuse(s
) < 0) ||
722 (bind(s
,(struct sockaddr
*)&addr
, sizeof(addr
)) < 0) ||
724 int tmperrno
= errno
; /* Don't clobber the real reason we failed */
727 slirp_closesocket(s
);
730 /* Restore the real errno */
732 WSASetLastError(tmperrno
);
738 slirp_setsockopt(s
, SOL_SOCKET
, SO_OOBINLINE
, &opt
, sizeof(int));
740 slirp_setsockopt(s
, IPPROTO_TCP
, TCP_NODELAY
, &opt
, sizeof(int));
742 getsockname(s
,(struct sockaddr
*)&addr
,&addrlen
);
743 so
->so_ffamily
= AF_INET
;
744 so
->so_fport
= addr
.sin_port
;
745 if (addr
.sin_addr
.s_addr
== 0 || addr
.sin_addr
.s_addr
== loopback_addr
.s_addr
)
746 so
->so_faddr
= slirp
->vhost_addr
;
748 so
->so_faddr
= addr
.sin_addr
;
755 * Various session state calls
756 * XXX Should be #define's
757 * The socket state stuff needs work, these often get call 2 or 3
758 * times each when only 1 was needed
761 soisfconnecting(struct socket
*so
)
763 so
->so_state
&= ~(SS_NOFDREF
|SS_ISFCONNECTED
|SS_FCANTRCVMORE
|
764 SS_FCANTSENDMORE
|SS_FWDRAIN
);
765 so
->so_state
|= SS_ISFCONNECTING
; /* Clobber other states */
769 soisfconnected(struct socket
*so
)
771 so
->so_state
&= ~(SS_ISFCONNECTING
|SS_FWDRAIN
|SS_NOFDREF
);
772 so
->so_state
|= SS_ISFCONNECTED
; /* Clobber other states */
776 sofcantrcvmore(struct socket
*so
)
778 if ((so
->so_state
& SS_NOFDREF
) == 0) {
781 so
->so_state
&= ~(SS_ISFCONNECTING
);
782 if (so
->so_state
& SS_FCANTSENDMORE
) {
783 so
->so_state
&= SS_PERSISTENT_MASK
;
784 so
->so_state
|= SS_NOFDREF
; /* Don't select it */
786 so
->so_state
|= SS_FCANTRCVMORE
;
791 sofcantsendmore(struct socket
*so
)
793 if ((so
->so_state
& SS_NOFDREF
) == 0) {
794 shutdown(so
->s
,1); /* send FIN to fhost */
796 so
->so_state
&= ~(SS_ISFCONNECTING
);
797 if (so
->so_state
& SS_FCANTRCVMORE
) {
798 so
->so_state
&= SS_PERSISTENT_MASK
;
799 so
->so_state
|= SS_NOFDREF
; /* as above */
801 so
->so_state
|= SS_FCANTSENDMORE
;
806 * Set write drain mode
807 * Set CANTSENDMORE once all data has been write()n
810 sofwdrain(struct socket
*so
)
812 if (so
->so_rcv
.sb_cc
)
813 so
->so_state
|= SS_FWDRAIN
;
819 * Translate addr in host addr when it is a virtual address
821 void sotranslate_out(struct socket
*so
, struct sockaddr_storage
*addr
)
823 Slirp
*slirp
= so
->slirp
;
824 struct sockaddr_in
*sin
= (struct sockaddr_in
*)addr
;
825 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)addr
;
827 switch (addr
->ss_family
) {
829 if ((so
->so_faddr
.s_addr
& slirp
->vnetwork_mask
.s_addr
) ==
830 slirp
->vnetwork_addr
.s_addr
) {
832 if (so
->so_faddr
.s_addr
== slirp
->vnameserver_addr
.s_addr
) {
833 if (get_dns_addr(&sin
->sin_addr
) < 0) {
834 sin
->sin_addr
= loopback_addr
;
837 sin
->sin_addr
= loopback_addr
;
841 DEBUG_MISC(" addr.sin_port=%d, addr.sin_addr.s_addr=%.16s",
842 ntohs(sin
->sin_port
), inet_ntoa(sin
->sin_addr
));
846 if (in6_equal_net(&so
->so_faddr6
, &slirp
->vprefix_addr6
,
847 slirp
->vprefix_len
)) {
848 if (in6_equal(&so
->so_faddr6
, &slirp
->vnameserver_addr6
)) {
850 if (get_dns6_addr(&sin6
->sin6_addr
, &scope_id
) >= 0) {
851 sin6
->sin6_scope_id
= scope_id
;
853 sin6
->sin6_addr
= in6addr_loopback
;
856 sin6
->sin6_addr
= in6addr_loopback
;
866 void sotranslate_in(struct socket
*so
, struct sockaddr_storage
*addr
)
868 Slirp
*slirp
= so
->slirp
;
869 struct sockaddr_in
*sin
= (struct sockaddr_in
*)addr
;
870 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)addr
;
872 switch (addr
->ss_family
) {
874 if ((so
->so_faddr
.s_addr
& slirp
->vnetwork_mask
.s_addr
) ==
875 slirp
->vnetwork_addr
.s_addr
) {
876 uint32_t inv_mask
= ~slirp
->vnetwork_mask
.s_addr
;
878 if ((so
->so_faddr
.s_addr
& inv_mask
) == inv_mask
) {
879 sin
->sin_addr
= slirp
->vhost_addr
;
880 } else if (sin
->sin_addr
.s_addr
== loopback_addr
.s_addr
||
881 so
->so_faddr
.s_addr
!= slirp
->vhost_addr
.s_addr
) {
882 sin
->sin_addr
= so
->so_faddr
;
888 if (in6_equal_net(&so
->so_faddr6
, &slirp
->vprefix_addr6
,
889 slirp
->vprefix_len
)) {
890 if (in6_equal(&sin6
->sin6_addr
, &in6addr_loopback
)
891 || !in6_equal(&so
->so_faddr6
, &slirp
->vhost_addr6
)) {
892 sin6
->sin6_addr
= so
->so_faddr6
;
903 * Translate connections from localhost to the real hostname
905 void sotranslate_accept(struct socket
*so
)
907 Slirp
*slirp
= so
->slirp
;
909 switch (so
->so_ffamily
) {
911 if (so
->so_faddr
.s_addr
== INADDR_ANY
||
912 (so
->so_faddr
.s_addr
& loopback_mask
) ==
913 (loopback_addr
.s_addr
& loopback_mask
)) {
914 so
->so_faddr
= slirp
->vhost_addr
;
919 if (in6_equal(&so
->so_faddr6
, &in6addr_any
) ||
920 in6_equal(&so
->so_faddr6
, &in6addr_loopback
)) {
921 so
->so_faddr6
= slirp
->vhost_addr6
;
930 void sodrop(struct socket
*s
, int num
)
932 if (sbdrop(&s
->so_snd
, num
)) {
933 s
->slirp
->cb
->notify();