]>
git.proxmox.com Git - mirror_qemu.git/blob - slirp/src/socket.c
1 /* SPDX-License-Identifier: BSD-3-Clause */
3 * Copyright (c) 1995 Danny Gasparovski.
12 static void sofcantrcvmore(struct socket
*so
);
13 static void sofcantsendmore(struct socket
*so
);
15 struct socket
*solookup(struct socket
**last
, struct socket
*head
,
16 struct sockaddr_storage
*lhost
, struct sockaddr_storage
*fhost
)
18 struct socket
*so
= *last
;
21 if (so
!= head
&& sockaddr_equal(&(so
->lhost
.ss
), lhost
)
22 && (!fhost
|| sockaddr_equal(&so
->fhost
.ss
, fhost
))) {
26 for (so
= head
->so_next
; so
!= head
; so
= so
->so_next
) {
27 if (sockaddr_equal(&(so
->lhost
.ss
), lhost
)
28 && (!fhost
|| sockaddr_equal(&so
->fhost
.ss
, fhost
))) {
34 return (struct socket
*)NULL
;
38 * Create a new socket, initialise the fields
39 * It is the responsibility of the caller to
40 * insque() it into the correct linked-list
43 socreate(Slirp
*slirp
)
45 struct socket
*so
= g_new(struct socket
, 1);
47 memset(so
, 0, sizeof(struct socket
));
48 so
->so_state
= SS_NOFDREF
;
57 * Remove references to so from the given message queue.
60 soqfree(struct socket
*so
, struct quehead
*qh
)
64 for (ifq
= (struct mbuf
*) qh
->qh_link
;
65 (struct quehead
*) ifq
!= qh
;
66 ifq
= ifq
->ifq_next
) {
67 if (ifq
->ifq_so
== so
) {
70 for (ifm
= ifq
->ifs_next
; ifm
!= ifq
; ifm
= ifm
->ifs_next
) {
78 * remque and free a socket, clobber cache
81 sofree(struct socket
*so
)
83 Slirp
*slirp
= so
->slirp
;
85 soqfree(so
, &slirp
->if_fastq
);
86 soqfree(so
, &slirp
->if_batchq
);
88 if (so
== slirp
->tcp_last_so
) {
89 slirp
->tcp_last_so
= &slirp
->tcb
;
90 } else if (so
== slirp
->udp_last_so
) {
91 slirp
->udp_last_so
= &slirp
->udb
;
92 } else if (so
== slirp
->icmp_last_so
) {
93 slirp
->icmp_last_so
= &slirp
->icmp
;
97 if(so
->so_next
&& so
->so_prev
)
98 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
)
175 struct sbuf
*sb
= &so
->so_snd
;
178 DEBUG_CALL("soread");
179 DEBUG_ARG("so = %p", so
);
182 * No need to check if there's enough room to read.
183 * soread wouldn't have been called if there weren't
185 buf_len
= sopreprbuf(so
, iov
, &n
);
186 assert(buf_len
!= 0);
188 nn
= 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 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
= 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
263 if (sopreprbuf(so
, iov
, &n
) < size
)
266 nn
= MIN(iov
[0].iov_len
, copy
);
267 memcpy(iov
[0].iov_base
, buf
, nn
);
275 memcpy(iov
[1].iov_base
, buf
, copy
);
281 if (sb
->sb_wptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
282 sb
->sb_wptr
-= sb
->sb_datalen
;
287 tcp_sockclosed(sototcpcb(so
));
288 g_critical("soreadbuf buffer too small");
295 * When the socket is created, we set it SO_OOBINLINE,
296 * so when OOB data arrives, we soread() it and everything
297 * in the send buffer is sent as urgent data
300 sorecvoob(struct socket
*so
)
302 struct tcpcb
*tp
= sototcpcb(so
);
305 DEBUG_CALL("sorecvoob");
306 DEBUG_ARG("so = %p", so
);
309 * We take a guess at how much urgent data has arrived.
310 * In most situations, when urgent data arrives, the next
311 * read() should get all the urgent data. This guess will
312 * be wrong however if more data arrives just after the
313 * urgent data, or the read() doesn't return all the
318 tp
->snd_up
= tp
->snd_una
+ so
->so_snd
.sb_cc
;
329 * There's a lot duplicated code here, but...
332 sosendoob(struct socket
*so
)
334 struct sbuf
*sb
= &so
->so_rcv
;
335 char buff
[2048]; /* XXX Shouldn't be sending more oob data than this */
339 DEBUG_CALL("sosendoob");
340 DEBUG_ARG("so = %p", so
);
341 DEBUG_ARG("sb->sb_cc = %d", sb
->sb_cc
);
343 if (so
->so_urgc
> 2048)
344 so
->so_urgc
= 2048; /* XXXX */
346 if (sb
->sb_rptr
< sb
->sb_wptr
) {
347 /* We can send it directly */
348 n
= slirp_send(so
, sb
->sb_rptr
, so
->so_urgc
, (MSG_OOB
)); /* |MSG_DONTWAIT)); */
351 * Since there's no sendv or sendtov like writev,
352 * we must copy all data to a linear buffer then
355 uint32_t urgc
= so
->so_urgc
;
356 int len
= (sb
->sb_data
+ sb
->sb_datalen
) - sb
->sb_rptr
;
360 memcpy(buff
, sb
->sb_rptr
, len
);
363 n
= sb
->sb_wptr
- sb
->sb_data
;
367 memcpy((buff
+ len
), sb
->sb_data
, n
);
370 n
= slirp_send(so
, buff
, len
, (MSG_OOB
)); /* |MSG_DONTWAIT)); */
373 DEBUG_ERROR("Didn't send all data urgently XXXXX");
382 DEBUG_MISC(" ---2 sent %d bytes urgent data, %d urgent bytes left", n
, so
->so_urgc
);
386 if (sb
->sb_rptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
387 sb
->sb_rptr
-= sb
->sb_datalen
;
393 * Write data from so_rcv to so's socket,
394 * updating all sbuf field as necessary
397 sowrite(struct socket
*so
)
400 struct sbuf
*sb
= &so
->so_rcv
;
404 DEBUG_CALL("sowrite");
405 DEBUG_ARG("so = %p", so
);
408 uint32_t expected
= so
->so_urgc
;
409 if (sosendoob(so
) < expected
) {
410 /* Treat a short write as a fatal error too,
411 * rather than continuing on and sending the urgent
412 * data as if it were non-urgent and leaving the
413 * so_urgc count wrong.
415 goto err_disconnected
;
422 * No need to check if there's something to write,
423 * sowrite wouldn't have been called otherwise
426 iov
[0].iov_base
= sb
->sb_rptr
;
427 iov
[1].iov_base
= NULL
;
429 if (sb
->sb_rptr
< sb
->sb_wptr
) {
430 iov
[0].iov_len
= sb
->sb_wptr
- sb
->sb_rptr
;
431 /* Should never succeed, but... */
432 if (iov
[0].iov_len
> len
) iov
[0].iov_len
= len
;
435 iov
[0].iov_len
= (sb
->sb_data
+ sb
->sb_datalen
) - sb
->sb_rptr
;
436 if (iov
[0].iov_len
> len
) iov
[0].iov_len
= len
;
437 len
-= iov
[0].iov_len
;
439 iov
[1].iov_base
= sb
->sb_data
;
440 iov
[1].iov_len
= sb
->sb_wptr
- sb
->sb_data
;
441 if (iov
[1].iov_len
> len
) iov
[1].iov_len
= len
;
446 /* Check if there's urgent data to send, and if so, send it */
448 nn
= slirp_send(so
, iov
[0].iov_base
, iov
[0].iov_len
,0);
449 /* This should never happen, but people tell me it does *shrug* */
450 if (nn
< 0 && (errno
== EAGAIN
|| errno
== EINTR
))
454 goto err_disconnected
;
457 if (n
== 2 && nn
== iov
[0].iov_len
) {
459 ret
= slirp_send(so
, iov
[1].iov_base
, iov
[1].iov_len
,0);
463 DEBUG_MISC(" ... wrote nn = %d bytes", nn
);
468 if (sb
->sb_rptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
469 sb
->sb_rptr
-= sb
->sb_datalen
;
472 * If in DRAIN mode, and there's no more data, set
475 if ((so
->so_state
& SS_FWDRAIN
) && sb
->sb_cc
== 0)
481 DEBUG_MISC(" --- sowrite disconnected, so->so_state = %x, errno = %d",
482 so
->so_state
, errno
);
484 tcp_sockclosed(sototcpcb(so
));
489 * recvfrom() a UDP socket
492 sorecvfrom(struct socket
*so
)
494 struct sockaddr_storage addr
;
495 struct sockaddr_storage saddr
, daddr
;
496 socklen_t addrlen
= sizeof(struct sockaddr_storage
);
498 DEBUG_CALL("sorecvfrom");
499 DEBUG_ARG("so = %p", so
);
501 if (so
->so_type
== IPPROTO_ICMP
) { /* This is a "ping" reply */
505 len
= recvfrom(so
->s
, buff
, 256, 0,
506 (struct sockaddr
*)&addr
, &addrlen
);
507 /* XXX Check if reply is "correct"? */
509 if(len
== -1 || len
== 0) {
510 uint8_t code
=ICMP_UNREACH_PORT
;
512 if(errno
== EHOSTUNREACH
) code
=ICMP_UNREACH_HOST
;
513 else if(errno
== ENETUNREACH
) code
=ICMP_UNREACH_NET
;
515 DEBUG_MISC(" udp icmp rx errno = %d-%s",
516 errno
,strerror(errno
));
517 icmp_send_error(so
->so_m
, ICMP_UNREACH
, code
, 0, strerror(errno
));
519 icmp_reflect(so
->so_m
);
520 so
->so_m
= NULL
; /* Don't m_free() it again! */
522 /* No need for this socket anymore, udp_detach it */
524 } else { /* A "normal" UDP packet */
533 if (ioctlsocket(so
->s
, FIONREAD
, &n
) != 0) {
534 DEBUG_MISC(" ioctlsocket errno = %d-%s\n",
535 errno
,strerror(errno
));
542 m
= m_get(so
->slirp
);
546 switch (so
->so_ffamily
) {
548 m
->m_data
+= IF_MAXLINKHDR
+ sizeof(struct udpiphdr
);
551 m
->m_data
+= IF_MAXLINKHDR
+ sizeof(struct ip6
)
552 + sizeof(struct udphdr
);
555 g_assert_not_reached();
560 * XXX Shouldn't FIONREAD packets destined for port 53,
561 * but I don't know the max packet size for DNS lookups
564 /* if (so->so_fport != htons(53)) { */
567 n
= (m
->m_data
- m
->m_dat
) + m
->m_len
+ n
+ 1;
573 m
->m_len
= recvfrom(so
->s
, m
->m_data
, len
, 0,
574 (struct sockaddr
*)&addr
, &addrlen
);
575 DEBUG_MISC(" did recvfrom %d, errno = %d-%s",
576 m
->m_len
, errno
,strerror(errno
));
578 /* Report error as ICMP */
579 switch (so
->so_lfamily
) {
582 code
= ICMP_UNREACH_PORT
;
584 if (errno
== EHOSTUNREACH
) {
585 code
= ICMP_UNREACH_HOST
;
586 } else if (errno
== ENETUNREACH
) {
587 code
= ICMP_UNREACH_NET
;
590 DEBUG_MISC(" rx error, tx icmp ICMP_UNREACH:%i", code
);
591 icmp_send_error(so
->so_m
, ICMP_UNREACH
, code
, 0, strerror(errno
));
594 code
= ICMP6_UNREACH_PORT
;
596 if (errno
== EHOSTUNREACH
) {
597 code
= ICMP6_UNREACH_ADDRESS
;
598 } else if (errno
== ENETUNREACH
) {
599 code
= ICMP6_UNREACH_NO_ROUTE
;
602 DEBUG_MISC(" rx error, tx icmp6 ICMP_UNREACH:%i", code
);
603 icmp6_send_error(so
->so_m
, ICMP6_UNREACH
, code
);
606 g_assert_not_reached();
612 * Hack: domain name lookup will be used the most for UDP,
613 * and since they'll only be used once there's no need
614 * for the 4 minute (or whatever) timeout... So we time them
615 * out much quicker (10 seconds for now...)
618 if (so
->so_fport
== htons(53))
619 so
->so_expire
= curtime
+ SO_EXPIREFAST
;
621 so
->so_expire
= curtime
+ SO_EXPIRE
;
625 * If this packet was destined for CTL_ADDR,
626 * make it look like that's where it came from
629 sotranslate_in(so
, &saddr
);
630 daddr
= so
->lhost
.ss
;
632 switch (so
->so_ffamily
) {
634 udp_output(so
, m
, (struct sockaddr_in
*) &saddr
,
635 (struct sockaddr_in
*) &daddr
,
639 udp6_output(so
, m
, (struct sockaddr_in6
*) &saddr
,
640 (struct sockaddr_in6
*) &daddr
);
643 g_assert_not_reached();
647 } /* if ping packet */
654 sosendto(struct socket
*so
, struct mbuf
*m
)
657 struct sockaddr_storage addr
;
659 DEBUG_CALL("sosendto");
660 DEBUG_ARG("so = %p", so
);
661 DEBUG_ARG("m = %p", m
);
664 DEBUG_CALL(" sendto()ing)");
665 sotranslate_out(so
, &addr
);
667 /* Don't care what port we get */
668 ret
= sendto(so
->s
, m
->m_data
, m
->m_len
, 0,
669 (struct sockaddr
*)&addr
, sockaddr_size(&addr
));
674 * Kill the socket if there's no reply in 4 minutes,
675 * but only if it's an expirable socket
678 so
->so_expire
= curtime
+ SO_EXPIRE
;
679 so
->so_state
&= SS_PERSISTENT_MASK
;
680 so
->so_state
|= SS_ISFCONNECTED
; /* So that it gets select()ed */
685 * Listen for incoming TCP connections
688 tcp_listen(Slirp
*slirp
, uint32_t haddr
, unsigned hport
, uint32_t laddr
,
689 unsigned lport
, int flags
)
692 struct sockaddr_in addr
;
695 socklen_t addrlen
= sizeof(addr
);
696 memset(&addr
, 0, addrlen
);
698 DEBUG_CALL("tcp_listen");
699 DEBUG_ARG("haddr = %s", inet_ntoa((struct in_addr
){.s_addr
= haddr
}));
700 DEBUG_ARG("hport = %d", ntohs(hport
));
701 DEBUG_ARG("laddr = %s", inet_ntoa((struct in_addr
){.s_addr
= laddr
}));
702 DEBUG_ARG("lport = %d", ntohs(lport
));
703 DEBUG_ARG("flags = %x", flags
);
705 so
= socreate(slirp
);
707 /* Don't tcp_attach... we don't need so_snd nor so_rcv */
708 if ((so
->so_tcpcb
= tcp_newtcpcb(so
)) == NULL
) {
712 insque(so
, &slirp
->tcb
);
715 * SS_FACCEPTONCE sockets must time out.
717 if (flags
& SS_FACCEPTONCE
)
718 so
->so_tcpcb
->t_timer
[TCPT_KEEP
] = TCPTV_KEEP_INIT
*2;
720 so
->so_state
&= SS_PERSISTENT_MASK
;
721 so
->so_state
|= (SS_FACCEPTCONN
| flags
);
722 so
->so_lfamily
= AF_INET
;
723 so
->so_lport
= lport
; /* Kept in network format */
724 so
->so_laddr
.s_addr
= laddr
; /* Ditto */
726 addr
.sin_family
= AF_INET
;
727 addr
.sin_addr
.s_addr
= haddr
;
728 addr
.sin_port
= hport
;
730 if (((s
= slirp_socket(AF_INET
,SOCK_STREAM
,0)) < 0) ||
731 (slirp_socket_set_fast_reuse(s
) < 0) ||
732 (bind(s
,(struct sockaddr
*)&addr
, sizeof(addr
)) < 0) ||
734 int tmperrno
= errno
; /* Don't clobber the real reason we failed */
740 /* Restore the real errno */
742 WSASetLastError(tmperrno
);
748 setsockopt(s
, SOL_SOCKET
, SO_OOBINLINE
, &opt
, sizeof(int));
750 setsockopt(s
, IPPROTO_TCP
, TCP_NODELAY
, &opt
, sizeof(int));
752 getsockname(s
,(struct sockaddr
*)&addr
,&addrlen
);
753 so
->so_ffamily
= AF_INET
;
754 so
->so_fport
= addr
.sin_port
;
755 if (addr
.sin_addr
.s_addr
== 0 || addr
.sin_addr
.s_addr
== loopback_addr
.s_addr
)
756 so
->so_faddr
= slirp
->vhost_addr
;
758 so
->so_faddr
= addr
.sin_addr
;
765 * Various session state calls
766 * XXX Should be #define's
767 * The socket state stuff needs work, these often get call 2 or 3
768 * times each when only 1 was needed
771 soisfconnecting(struct socket
*so
)
773 so
->so_state
&= ~(SS_NOFDREF
|SS_ISFCONNECTED
|SS_FCANTRCVMORE
|
774 SS_FCANTSENDMORE
|SS_FWDRAIN
);
775 so
->so_state
|= SS_ISFCONNECTING
; /* Clobber other states */
779 soisfconnected(struct socket
*so
)
781 so
->so_state
&= ~(SS_ISFCONNECTING
|SS_FWDRAIN
|SS_NOFDREF
);
782 so
->so_state
|= SS_ISFCONNECTED
; /* Clobber other states */
786 sofcantrcvmore(struct socket
*so
)
788 if ((so
->so_state
& SS_NOFDREF
) == 0) {
791 so
->so_state
&= ~(SS_ISFCONNECTING
);
792 if (so
->so_state
& SS_FCANTSENDMORE
) {
793 so
->so_state
&= SS_PERSISTENT_MASK
;
794 so
->so_state
|= SS_NOFDREF
; /* Don't select it */
796 so
->so_state
|= SS_FCANTRCVMORE
;
801 sofcantsendmore(struct socket
*so
)
803 if ((so
->so_state
& SS_NOFDREF
) == 0) {
804 shutdown(so
->s
,1); /* send FIN to fhost */
806 so
->so_state
&= ~(SS_ISFCONNECTING
);
807 if (so
->so_state
& SS_FCANTRCVMORE
) {
808 so
->so_state
&= SS_PERSISTENT_MASK
;
809 so
->so_state
|= SS_NOFDREF
; /* as above */
811 so
->so_state
|= SS_FCANTSENDMORE
;
816 * Set write drain mode
817 * Set CANTSENDMORE once all data has been write()n
820 sofwdrain(struct socket
*so
)
822 if (so
->so_rcv
.sb_cc
)
823 so
->so_state
|= SS_FWDRAIN
;
829 * Translate addr in host addr when it is a virtual address
831 void sotranslate_out(struct socket
*so
, struct sockaddr_storage
*addr
)
833 Slirp
*slirp
= so
->slirp
;
834 struct sockaddr_in
*sin
= (struct sockaddr_in
*)addr
;
835 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)addr
;
837 switch (addr
->ss_family
) {
839 if ((so
->so_faddr
.s_addr
& slirp
->vnetwork_mask
.s_addr
) ==
840 slirp
->vnetwork_addr
.s_addr
) {
842 if (so
->so_faddr
.s_addr
== slirp
->vnameserver_addr
.s_addr
) {
843 if (get_dns_addr(&sin
->sin_addr
) < 0) {
844 sin
->sin_addr
= loopback_addr
;
847 sin
->sin_addr
= loopback_addr
;
851 DEBUG_MISC(" addr.sin_port=%d, addr.sin_addr.s_addr=%.16s",
852 ntohs(sin
->sin_port
), inet_ntoa(sin
->sin_addr
));
856 if (in6_equal_net(&so
->so_faddr6
, &slirp
->vprefix_addr6
,
857 slirp
->vprefix_len
)) {
858 if (in6_equal(&so
->so_faddr6
, &slirp
->vnameserver_addr6
)) {
860 if (get_dns6_addr(&sin6
->sin6_addr
, &scope_id
) >= 0) {
861 sin6
->sin6_scope_id
= scope_id
;
863 sin6
->sin6_addr
= in6addr_loopback
;
866 sin6
->sin6_addr
= in6addr_loopback
;
876 void sotranslate_in(struct socket
*so
, struct sockaddr_storage
*addr
)
878 Slirp
*slirp
= so
->slirp
;
879 struct sockaddr_in
*sin
= (struct sockaddr_in
*)addr
;
880 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)addr
;
882 switch (addr
->ss_family
) {
884 if ((so
->so_faddr
.s_addr
& slirp
->vnetwork_mask
.s_addr
) ==
885 slirp
->vnetwork_addr
.s_addr
) {
886 uint32_t inv_mask
= ~slirp
->vnetwork_mask
.s_addr
;
888 if ((so
->so_faddr
.s_addr
& inv_mask
) == inv_mask
) {
889 sin
->sin_addr
= slirp
->vhost_addr
;
890 } else if (sin
->sin_addr
.s_addr
== loopback_addr
.s_addr
||
891 so
->so_faddr
.s_addr
!= slirp
->vhost_addr
.s_addr
) {
892 sin
->sin_addr
= so
->so_faddr
;
898 if (in6_equal_net(&so
->so_faddr6
, &slirp
->vprefix_addr6
,
899 slirp
->vprefix_len
)) {
900 if (in6_equal(&sin6
->sin6_addr
, &in6addr_loopback
)
901 || !in6_equal(&so
->so_faddr6
, &slirp
->vhost_addr6
)) {
902 sin6
->sin6_addr
= so
->so_faddr6
;
913 * Translate connections from localhost to the real hostname
915 void sotranslate_accept(struct socket
*so
)
917 Slirp
*slirp
= so
->slirp
;
919 switch (so
->so_ffamily
) {
921 if (so
->so_faddr
.s_addr
== INADDR_ANY
||
922 (so
->so_faddr
.s_addr
& loopback_mask
) ==
923 (loopback_addr
.s_addr
& loopback_mask
)) {
924 so
->so_faddr
= slirp
->vhost_addr
;
929 if (in6_equal(&so
->so_faddr6
, &in6addr_any
) ||
930 in6_equal(&so
->so_faddr6
, &in6addr_loopback
)) {
931 so
->so_faddr6
= slirp
->vhost_addr6
;
940 void sodrop(struct socket
*s
, int num
)
942 if (sbdrop(&s
->so_snd
, num
)) {
943 s
->slirp
->cb
->notify(s
->slirp
->opaque
);