dump-guest-memory: add "detach" support
[qemu.git] / slirp / socket.c
blob2b5453e0205d886e837ec12ee5ac996e57b8039a
1 /*
2 * Copyright (c) 1995 Danny Gasparovski.
4 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
6 */
8 #include "qemu/osdep.h"
9 #include "qemu-common.h"
10 #include <slirp.h>
11 #include "ip_icmp.h"
12 #ifdef __sun__
13 #include <sys/filio.h>
14 #endif
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;
24 /* Optimisation */
25 if (so != head && sockaddr_equal(&(so->lhost.ss), lhost)
26 && (!fhost || sockaddr_equal(&so->fhost.ss, fhost))) {
27 return so;
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))) {
33 *last = so;
34 return so;
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
46 struct socket *
47 socreate(Slirp *slirp)
49 struct socket *so;
51 so = (struct socket *)malloc(sizeof(struct socket));
52 if(so) {
53 memset(so, 0, sizeof(struct socket));
54 so->so_state = SS_NOFDREF;
55 so->s = -1;
56 so->slirp = slirp;
57 so->pollfds_idx = -1;
59 return(so);
63 * remque and free a socket, clobber cache
65 void
66 sofree(struct socket *so)
68 Slirp *slirp = so->slirp;
70 if (so->so_emu==EMU_RSH && so->extra) {
71 sofree(so->extra);
72 so->extra=NULL;
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;
81 m_free(so->so_m);
83 if(so->so_next && so->so_prev)
84 remque(so); /* crashes if so is not in a queue */
86 free(so);
89 size_t sopreprbuf(struct socket *so, struct iovec *iov, int *np)
91 int n, lss, total;
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);
99 if (len <= 0)
100 return 0;
102 iov[0].iov_base = sb->sb_wptr;
103 iov[1].iov_base = NULL;
104 iov[1].iov_len = 0;
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;
112 n = 1;
113 } else {
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;
118 if (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;
124 if (total > mss) {
125 lss = total%mss;
126 if (iov[1].iov_len > lss) {
127 iov[1].iov_len -= lss;
128 n = 2;
129 } else {
130 lss -= iov[1].iov_len;
131 iov[0].iov_len -= lss;
132 n = 1;
134 } else
135 n = 2;
136 } else {
137 if (iov[0].iov_len > mss)
138 iov[0].iov_len -= iov[0].iov_len%mss;
139 n = 1;
142 if (np)
143 *np = n;
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)
156 int n, nn;
157 struct sbuf *sb = &so->so_snd;
158 struct iovec iov[2];
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);
169 #ifdef HAVE_READV
170 nn = readv(so->s, (struct iovec *)iov, n);
171 DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
172 #else
173 nn = qemu_recv(so->s, iov[0].iov_base, iov[0].iov_len,0);
174 #endif
175 if (nn <= 0) {
176 if (nn < 0 && (errno == EINTR || errno == EAGAIN))
177 return 0;
178 else {
179 DEBUG_MISC((dfd, " --- soread() disconnected, nn = %d, errno = %d-%s\n", nn, errno,strerror(errno)));
180 sofcantrcvmore(so);
181 tcp_sockclosed(sototcpcb(so));
182 return -1;
186 #ifndef HAVE_READV
188 * If there was no error, try and read the second time round
189 * We read again if n = 2 (ie, there's another part of the buffer)
190 * and we read as much as we could in the first read
191 * We don't test for <= 0 this time, because there legitimately
192 * might not be any more data (since the socket is non-blocking),
193 * a close will be detected on next iteration.
194 * A return of -1 wont (shouldn't) happen, since it didn't happen above
196 if (n == 2 && nn == iov[0].iov_len) {
197 int ret;
198 ret = qemu_recv(so->s, iov[1].iov_base, iov[1].iov_len,0);
199 if (ret > 0)
200 nn += ret;
203 DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
204 #endif
206 /* Update fields */
207 sb->sb_cc += nn;
208 sb->sb_wptr += nn;
209 if (sb->sb_wptr >= (sb->sb_data + sb->sb_datalen))
210 sb->sb_wptr -= sb->sb_datalen;
211 return nn;
214 int soreadbuf(struct socket *so, const char *buf, int size)
216 int n, nn, copy = size;
217 struct sbuf *sb = &so->so_snd;
218 struct iovec iov[2];
220 DEBUG_CALL("soreadbuf");
221 DEBUG_ARG("so = %p", so);
224 * No need to check if there's enough room to read.
225 * soread wouldn't have been called if there weren't
227 if (sopreprbuf(so, iov, &n) < size)
228 goto err;
230 nn = MIN(iov[0].iov_len, copy);
231 memcpy(iov[0].iov_base, buf, nn);
233 copy -= nn;
234 buf += nn;
236 if (copy == 0)
237 goto done;
239 memcpy(iov[1].iov_base, buf, copy);
241 done:
242 /* Update fields */
243 sb->sb_cc += size;
244 sb->sb_wptr += size;
245 if (sb->sb_wptr >= (sb->sb_data + sb->sb_datalen))
246 sb->sb_wptr -= sb->sb_datalen;
247 return size;
248 err:
250 sofcantrcvmore(so);
251 tcp_sockclosed(sototcpcb(so));
252 fprintf(stderr, "soreadbuf buffer to small");
253 return -1;
257 * Get urgent data
259 * When the socket is created, we set it SO_OOBINLINE,
260 * so when OOB data arrives, we soread() it and everything
261 * in the send buffer is sent as urgent data
263 void
264 sorecvoob(struct socket *so)
266 struct tcpcb *tp = sototcpcb(so);
268 DEBUG_CALL("sorecvoob");
269 DEBUG_ARG("so = %p", so);
272 * We take a guess at how much urgent data has arrived.
273 * In most situations, when urgent data arrives, the next
274 * read() should get all the urgent data. This guess will
275 * be wrong however if more data arrives just after the
276 * urgent data, or the read() doesn't return all the
277 * urgent data.
279 soread(so);
280 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
281 tp->t_force = 1;
282 tcp_output(tp);
283 tp->t_force = 0;
287 * Send urgent data
288 * There's a lot duplicated code here, but...
291 sosendoob(struct socket *so)
293 struct sbuf *sb = &so->so_rcv;
294 char buff[2048]; /* XXX Shouldn't be sending more oob data than this */
296 int n, len;
298 DEBUG_CALL("sosendoob");
299 DEBUG_ARG("so = %p", so);
300 DEBUG_ARG("sb->sb_cc = %d", sb->sb_cc);
302 if (so->so_urgc > 2048)
303 so->so_urgc = 2048; /* XXXX */
305 if (sb->sb_rptr < sb->sb_wptr) {
306 /* We can send it directly */
307 n = slirp_send(so, sb->sb_rptr, so->so_urgc, (MSG_OOB)); /* |MSG_DONTWAIT)); */
308 so->so_urgc -= n;
310 DEBUG_MISC((dfd, " --- sent %d bytes urgent data, %d urgent bytes left\n", n, so->so_urgc));
311 } else {
313 * Since there's no sendv or sendtov like writev,
314 * we must copy all data to a linear buffer then
315 * send it all
317 len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
318 if (len > so->so_urgc) len = so->so_urgc;
319 memcpy(buff, sb->sb_rptr, len);
320 so->so_urgc -= len;
321 if (so->so_urgc) {
322 n = sb->sb_wptr - sb->sb_data;
323 if (n > so->so_urgc) n = so->so_urgc;
324 memcpy((buff + len), sb->sb_data, n);
325 so->so_urgc -= n;
326 len += n;
328 n = slirp_send(so, buff, len, (MSG_OOB)); /* |MSG_DONTWAIT)); */
329 #ifdef DEBUG
330 if (n != len)
331 DEBUG_ERROR((dfd, "Didn't send all data urgently XXXXX\n"));
332 #endif
333 DEBUG_MISC((dfd, " ---2 sent %d bytes urgent data, %d urgent bytes left\n", n, so->so_urgc));
336 sb->sb_cc -= n;
337 sb->sb_rptr += n;
338 if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
339 sb->sb_rptr -= sb->sb_datalen;
341 return n;
345 * Write data from so_rcv to so's socket,
346 * updating all sbuf field as necessary
349 sowrite(struct socket *so)
351 int n,nn;
352 struct sbuf *sb = &so->so_rcv;
353 int len = sb->sb_cc;
354 struct iovec iov[2];
356 DEBUG_CALL("sowrite");
357 DEBUG_ARG("so = %p", so);
359 if (so->so_urgc) {
360 sosendoob(so);
361 if (sb->sb_cc == 0)
362 return 0;
366 * No need to check if there's something to write,
367 * sowrite wouldn't have been called otherwise
370 iov[0].iov_base = sb->sb_rptr;
371 iov[1].iov_base = NULL;
372 iov[1].iov_len = 0;
373 if (sb->sb_rptr < sb->sb_wptr) {
374 iov[0].iov_len = sb->sb_wptr - sb->sb_rptr;
375 /* Should never succeed, but... */
376 if (iov[0].iov_len > len) iov[0].iov_len = len;
377 n = 1;
378 } else {
379 iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
380 if (iov[0].iov_len > len) iov[0].iov_len = len;
381 len -= iov[0].iov_len;
382 if (len) {
383 iov[1].iov_base = sb->sb_data;
384 iov[1].iov_len = sb->sb_wptr - sb->sb_data;
385 if (iov[1].iov_len > len) iov[1].iov_len = len;
386 n = 2;
387 } else
388 n = 1;
390 /* Check if there's urgent data to send, and if so, send it */
392 #ifdef HAVE_READV
393 nn = writev(so->s, (const struct iovec *)iov, n);
395 DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", nn));
396 #else
397 nn = slirp_send(so, iov[0].iov_base, iov[0].iov_len,0);
398 #endif
399 /* This should never happen, but people tell me it does *shrug* */
400 if (nn < 0 && (errno == EAGAIN || errno == EINTR))
401 return 0;
403 if (nn <= 0) {
404 DEBUG_MISC((dfd, " --- sowrite disconnected, so->so_state = %x, errno = %d\n",
405 so->so_state, errno));
406 sofcantsendmore(so);
407 tcp_sockclosed(sototcpcb(so));
408 return -1;
411 #ifndef HAVE_READV
412 if (n == 2 && nn == iov[0].iov_len) {
413 int ret;
414 ret = slirp_send(so, iov[1].iov_base, iov[1].iov_len,0);
415 if (ret > 0)
416 nn += ret;
418 DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", nn));
419 #endif
421 /* Update sbuf */
422 sb->sb_cc -= nn;
423 sb->sb_rptr += nn;
424 if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
425 sb->sb_rptr -= sb->sb_datalen;
428 * If in DRAIN mode, and there's no more data, set
429 * it CANTSENDMORE
431 if ((so->so_state & SS_FWDRAIN) && sb->sb_cc == 0)
432 sofcantsendmore(so);
434 return nn;
438 * recvfrom() a UDP socket
440 void
441 sorecvfrom(struct socket *so)
443 struct sockaddr_storage addr;
444 struct sockaddr_storage saddr, daddr;
445 socklen_t addrlen = sizeof(struct sockaddr_storage);
447 DEBUG_CALL("sorecvfrom");
448 DEBUG_ARG("so = %p", so);
450 if (so->so_type == IPPROTO_ICMP) { /* This is a "ping" reply */
451 char buff[256];
452 int len;
454 len = recvfrom(so->s, buff, 256, 0,
455 (struct sockaddr *)&addr, &addrlen);
456 /* XXX Check if reply is "correct"? */
458 if(len == -1 || len == 0) {
459 u_char code=ICMP_UNREACH_PORT;
461 if(errno == EHOSTUNREACH) code=ICMP_UNREACH_HOST;
462 else if(errno == ENETUNREACH) code=ICMP_UNREACH_NET;
464 DEBUG_MISC((dfd," udp icmp rx errno = %d-%s\n",
465 errno,strerror(errno)));
466 icmp_error(so->so_m, ICMP_UNREACH,code, 0,strerror(errno));
467 } else {
468 icmp_reflect(so->so_m);
469 so->so_m = NULL; /* Don't m_free() it again! */
471 /* No need for this socket anymore, udp_detach it */
472 udp_detach(so);
473 } else { /* A "normal" UDP packet */
474 struct mbuf *m;
475 int len;
476 #ifdef _WIN32
477 unsigned long n;
478 #else
479 int n;
480 #endif
482 m = m_get(so->slirp);
483 if (!m) {
484 return;
486 m->m_data += IF_MAXLINKHDR;
489 * XXX Shouldn't FIONREAD packets destined for port 53,
490 * but I don't know the max packet size for DNS lookups
492 len = M_FREEROOM(m);
493 /* if (so->so_fport != htons(53)) { */
494 ioctlsocket(so->s, FIONREAD, &n);
496 if (n > len) {
497 n = (m->m_data - m->m_dat) + m->m_len + n + 1;
498 m_inc(m, n);
499 len = M_FREEROOM(m);
501 /* } */
503 m->m_len = recvfrom(so->s, m->m_data, len, 0,
504 (struct sockaddr *)&addr, &addrlen);
505 DEBUG_MISC((dfd, " did recvfrom %d, errno = %d-%s\n",
506 m->m_len, errno,strerror(errno)));
507 if(m->m_len<0) {
508 u_char code=ICMP_UNREACH_PORT;
510 if(errno == EHOSTUNREACH) code=ICMP_UNREACH_HOST;
511 else if(errno == ENETUNREACH) code=ICMP_UNREACH_NET;
513 DEBUG_MISC((dfd," rx error, tx icmp ICMP_UNREACH:%i\n", code));
514 icmp_error(so->so_m, ICMP_UNREACH,code, 0,strerror(errno));
515 m_free(m);
516 } else {
518 * Hack: domain name lookup will be used the most for UDP,
519 * and since they'll only be used once there's no need
520 * for the 4 minute (or whatever) timeout... So we time them
521 * out much quicker (10 seconds for now...)
523 if (so->so_expire) {
524 if (so->so_fport == htons(53))
525 so->so_expire = curtime + SO_EXPIREFAST;
526 else
527 so->so_expire = curtime + SO_EXPIRE;
531 * If this packet was destined for CTL_ADDR,
532 * make it look like that's where it came from
534 saddr = addr;
535 sotranslate_in(so, &saddr);
536 daddr = so->lhost.ss;
538 switch (so->so_ffamily) {
539 case AF_INET:
540 udp_output(so, m, (struct sockaddr_in *) &saddr,
541 (struct sockaddr_in *) &daddr,
542 so->so_iptos);
543 break;
544 default:
545 break;
547 } /* rx error */
548 } /* if ping packet */
552 * sendto() a socket
555 sosendto(struct socket *so, struct mbuf *m)
557 int ret;
558 struct sockaddr_storage addr;
560 DEBUG_CALL("sosendto");
561 DEBUG_ARG("so = %p", so);
562 DEBUG_ARG("m = %p", m);
564 addr = so->fhost.ss;
565 DEBUG_CALL(" sendto()ing)");
566 sotranslate_out(so, &addr);
568 /* Don't care what port we get */
569 ret = sendto(so->s, m->m_data, m->m_len, 0,
570 (struct sockaddr *)&addr, sizeof(addr));
571 if (ret < 0)
572 return -1;
575 * Kill the socket if there's no reply in 4 minutes,
576 * but only if it's an expirable socket
578 if (so->so_expire)
579 so->so_expire = curtime + SO_EXPIRE;
580 so->so_state &= SS_PERSISTENT_MASK;
581 so->so_state |= SS_ISFCONNECTED; /* So that it gets select()ed */
582 return 0;
586 * Listen for incoming TCP connections
588 struct socket *
589 tcp_listen(Slirp *slirp, uint32_t haddr, u_int hport, uint32_t laddr,
590 u_int lport, int flags)
592 struct sockaddr_in addr;
593 struct socket *so;
594 int s, opt = 1;
595 socklen_t addrlen = sizeof(addr);
596 memset(&addr, 0, addrlen);
598 DEBUG_CALL("tcp_listen");
599 DEBUG_ARG("haddr = %x", haddr);
600 DEBUG_ARG("hport = %d", hport);
601 DEBUG_ARG("laddr = %x", laddr);
602 DEBUG_ARG("lport = %d", lport);
603 DEBUG_ARG("flags = %x", flags);
605 so = socreate(slirp);
606 if (!so) {
607 return NULL;
610 /* Don't tcp_attach... we don't need so_snd nor so_rcv */
611 if ((so->so_tcpcb = tcp_newtcpcb(so)) == NULL) {
612 free(so);
613 return NULL;
615 insque(so, &slirp->tcb);
618 * SS_FACCEPTONCE sockets must time out.
620 if (flags & SS_FACCEPTONCE)
621 so->so_tcpcb->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT*2;
623 so->so_state &= SS_PERSISTENT_MASK;
624 so->so_state |= (SS_FACCEPTCONN | flags);
625 so->so_lfamily = AF_INET;
626 so->so_lport = lport; /* Kept in network format */
627 so->so_laddr.s_addr = laddr; /* Ditto */
629 addr.sin_family = AF_INET;
630 addr.sin_addr.s_addr = haddr;
631 addr.sin_port = hport;
633 if (((s = qemu_socket(AF_INET,SOCK_STREAM,0)) < 0) ||
634 (socket_set_fast_reuse(s) < 0) ||
635 (bind(s,(struct sockaddr *)&addr, sizeof(addr)) < 0) ||
636 (listen(s,1) < 0)) {
637 int tmperrno = errno; /* Don't clobber the real reason we failed */
639 close(s);
640 sofree(so);
641 /* Restore the real errno */
642 #ifdef _WIN32
643 WSASetLastError(tmperrno);
644 #else
645 errno = tmperrno;
646 #endif
647 return NULL;
649 qemu_setsockopt(s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
651 getsockname(s,(struct sockaddr *)&addr,&addrlen);
652 so->so_ffamily = AF_INET;
653 so->so_fport = addr.sin_port;
654 if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr)
655 so->so_faddr = slirp->vhost_addr;
656 else
657 so->so_faddr = addr.sin_addr;
659 so->s = s;
660 return so;
664 * Various session state calls
665 * XXX Should be #define's
666 * The socket state stuff needs work, these often get call 2 or 3
667 * times each when only 1 was needed
669 void
670 soisfconnecting(struct socket *so)
672 so->so_state &= ~(SS_NOFDREF|SS_ISFCONNECTED|SS_FCANTRCVMORE|
673 SS_FCANTSENDMORE|SS_FWDRAIN);
674 so->so_state |= SS_ISFCONNECTING; /* Clobber other states */
677 void
678 soisfconnected(struct socket *so)
680 so->so_state &= ~(SS_ISFCONNECTING|SS_FWDRAIN|SS_NOFDREF);
681 so->so_state |= SS_ISFCONNECTED; /* Clobber other states */
684 static void
685 sofcantrcvmore(struct socket *so)
687 if ((so->so_state & SS_NOFDREF) == 0) {
688 shutdown(so->s,0);
690 so->so_state &= ~(SS_ISFCONNECTING);
691 if (so->so_state & SS_FCANTSENDMORE) {
692 so->so_state &= SS_PERSISTENT_MASK;
693 so->so_state |= SS_NOFDREF; /* Don't select it */
694 } else {
695 so->so_state |= SS_FCANTRCVMORE;
699 static void
700 sofcantsendmore(struct socket *so)
702 if ((so->so_state & SS_NOFDREF) == 0) {
703 shutdown(so->s,1); /* send FIN to fhost */
705 so->so_state &= ~(SS_ISFCONNECTING);
706 if (so->so_state & SS_FCANTRCVMORE) {
707 so->so_state &= SS_PERSISTENT_MASK;
708 so->so_state |= SS_NOFDREF; /* as above */
709 } else {
710 so->so_state |= SS_FCANTSENDMORE;
715 * Set write drain mode
716 * Set CANTSENDMORE once all data has been write()n
718 void
719 sofwdrain(struct socket *so)
721 if (so->so_rcv.sb_cc)
722 so->so_state |= SS_FWDRAIN;
723 else
724 sofcantsendmore(so);
728 * Translate addr in host addr when it is a virtual address
730 void sotranslate_out(struct socket *so, struct sockaddr_storage *addr)
732 Slirp *slirp = so->slirp;
733 struct sockaddr_in *sin = (struct sockaddr_in *)addr;
735 switch (addr->ss_family) {
736 case AF_INET:
737 if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) ==
738 slirp->vnetwork_addr.s_addr) {
739 /* It's an alias */
740 if (so->so_faddr.s_addr == slirp->vnameserver_addr.s_addr) {
741 if (get_dns_addr(&sin->sin_addr) < 0) {
742 sin->sin_addr = loopback_addr;
744 } else {
745 sin->sin_addr = loopback_addr;
749 DEBUG_MISC((dfd, " addr.sin_port=%d, "
750 "addr.sin_addr.s_addr=%.16s\n",
751 ntohs(sin->sin_port), inet_ntoa(sin->sin_addr)));
752 break;
754 default:
755 break;
759 void sotranslate_in(struct socket *so, struct sockaddr_storage *addr)
761 Slirp *slirp = so->slirp;
762 struct sockaddr_in *sin = (struct sockaddr_in *)addr;
764 switch (addr->ss_family) {
765 case AF_INET:
766 if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) ==
767 slirp->vnetwork_addr.s_addr) {
768 uint32_t inv_mask = ~slirp->vnetwork_mask.s_addr;
770 if ((so->so_faddr.s_addr & inv_mask) == inv_mask) {
771 sin->sin_addr = slirp->vhost_addr;
772 } else if (sin->sin_addr.s_addr == loopback_addr.s_addr ||
773 so->so_faddr.s_addr != slirp->vhost_addr.s_addr) {
774 sin->sin_addr = so->so_faddr;
777 break;
779 default:
780 break;
785 * Translate connections from localhost to the real hostname
787 void sotranslate_accept(struct socket *so)
789 Slirp *slirp = so->slirp;
791 switch (so->so_ffamily) {
792 case AF_INET:
793 if (so->so_faddr.s_addr == INADDR_ANY ||
794 (so->so_faddr.s_addr & loopback_mask) ==
795 (loopback_addr.s_addr & loopback_mask)) {
796 so->so_faddr = slirp->vhost_addr;
798 break;
800 default:
801 break;