block: List anonymous device BBs in query-block
[qemu/kevin.git] / slirp / socket.c
blobecec0295a9f47cacc86a7a0cbc9f55744eeec16f
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;
69 struct mbuf *ifm;
71 for (ifm = (struct mbuf *) slirp->if_fastq.qh_link;
72 (struct quehead *) ifm != &slirp->if_fastq;
73 ifm = ifm->ifq_next) {
74 if (ifm->ifq_so == so) {
75 ifm->ifq_so = NULL;
79 for (ifm = (struct mbuf *) slirp->if_batchq.qh_link;
80 (struct quehead *) ifm != &slirp->if_batchq;
81 ifm = ifm->ifq_next) {
82 if (ifm->ifq_so == so) {
83 ifm->ifq_so = NULL;
87 if (so->so_emu==EMU_RSH && so->extra) {
88 sofree(so->extra);
89 so->extra=NULL;
91 if (so == slirp->tcp_last_so) {
92 slirp->tcp_last_so = &slirp->tcb;
93 } else if (so == slirp->udp_last_so) {
94 slirp->udp_last_so = &slirp->udb;
95 } else if (so == slirp->icmp_last_so) {
96 slirp->icmp_last_so = &slirp->icmp;
98 m_free(so->so_m);
100 if(so->so_next && so->so_prev)
101 remque(so); /* crashes if so is not in a queue */
103 if (so->so_tcpcb) {
104 free(so->so_tcpcb);
106 free(so);
109 size_t sopreprbuf(struct socket *so, struct iovec *iov, int *np)
111 int n, lss, total;
112 struct sbuf *sb = &so->so_snd;
113 int len = sb->sb_datalen - sb->sb_cc;
114 int mss = so->so_tcpcb->t_maxseg;
116 DEBUG_CALL("sopreprbuf");
117 DEBUG_ARG("so = %p", so);
119 if (len <= 0)
120 return 0;
122 iov[0].iov_base = sb->sb_wptr;
123 iov[1].iov_base = NULL;
124 iov[1].iov_len = 0;
125 if (sb->sb_wptr < sb->sb_rptr) {
126 iov[0].iov_len = sb->sb_rptr - sb->sb_wptr;
127 /* Should never succeed, but... */
128 if (iov[0].iov_len > len)
129 iov[0].iov_len = len;
130 if (iov[0].iov_len > mss)
131 iov[0].iov_len -= iov[0].iov_len%mss;
132 n = 1;
133 } else {
134 iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_wptr;
135 /* Should never succeed, but... */
136 if (iov[0].iov_len > len) iov[0].iov_len = len;
137 len -= iov[0].iov_len;
138 if (len) {
139 iov[1].iov_base = sb->sb_data;
140 iov[1].iov_len = sb->sb_rptr - sb->sb_data;
141 if(iov[1].iov_len > len)
142 iov[1].iov_len = len;
143 total = iov[0].iov_len + iov[1].iov_len;
144 if (total > mss) {
145 lss = total%mss;
146 if (iov[1].iov_len > lss) {
147 iov[1].iov_len -= lss;
148 n = 2;
149 } else {
150 lss -= iov[1].iov_len;
151 iov[0].iov_len -= lss;
152 n = 1;
154 } else
155 n = 2;
156 } else {
157 if (iov[0].iov_len > mss)
158 iov[0].iov_len -= iov[0].iov_len%mss;
159 n = 1;
162 if (np)
163 *np = n;
165 return iov[0].iov_len + (n - 1) * iov[1].iov_len;
169 * Read from so's socket into sb_snd, updating all relevant sbuf fields
170 * NOTE: This will only be called if it is select()ed for reading, so
171 * a read() of 0 (or less) means it's disconnected
174 soread(struct socket *so)
176 int n, nn;
177 struct sbuf *sb = &so->so_snd;
178 struct iovec iov[2];
180 DEBUG_CALL("soread");
181 DEBUG_ARG("so = %p", so);
184 * No need to check if there's enough room to read.
185 * soread wouldn't have been called if there weren't
187 sopreprbuf(so, iov, &n);
189 #ifdef HAVE_READV
190 nn = readv(so->s, (struct iovec *)iov, n);
191 DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
192 #else
193 nn = qemu_recv(so->s, iov[0].iov_base, iov[0].iov_len,0);
194 #endif
195 if (nn <= 0) {
196 if (nn < 0 && (errno == EINTR || errno == EAGAIN))
197 return 0;
198 else {
199 int err;
200 socklen_t slen = sizeof err;
202 err = errno;
203 if (nn == 0) {
204 getsockopt(so->s, SOL_SOCKET, SO_ERROR,
205 &err, &slen);
208 DEBUG_MISC((dfd, " --- soread() disconnected, nn = %d, errno = %d-%s\n", nn, errno,strerror(errno)));
209 sofcantrcvmore(so);
211 if (err == ECONNRESET || err == ECONNREFUSED
212 || err == ENOTCONN || err == EPIPE) {
213 tcp_drop(sototcpcb(so), err);
214 } else {
215 tcp_sockclosed(sototcpcb(so));
217 return -1;
221 #ifndef HAVE_READV
223 * If there was no error, try and read the second time round
224 * We read again if n = 2 (ie, there's another part of the buffer)
225 * and we read as much as we could in the first read
226 * We don't test for <= 0 this time, because there legitimately
227 * might not be any more data (since the socket is non-blocking),
228 * a close will be detected on next iteration.
229 * A return of -1 won't (shouldn't) happen, since it didn't happen above
231 if (n == 2 && nn == iov[0].iov_len) {
232 int ret;
233 ret = qemu_recv(so->s, iov[1].iov_base, iov[1].iov_len,0);
234 if (ret > 0)
235 nn += ret;
238 DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
239 #endif
241 /* Update fields */
242 sb->sb_cc += nn;
243 sb->sb_wptr += nn;
244 if (sb->sb_wptr >= (sb->sb_data + sb->sb_datalen))
245 sb->sb_wptr -= sb->sb_datalen;
246 return nn;
249 int soreadbuf(struct socket *so, const char *buf, int size)
251 int n, nn, copy = size;
252 struct sbuf *sb = &so->so_snd;
253 struct iovec iov[2];
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)
263 goto err;
265 nn = MIN(iov[0].iov_len, copy);
266 memcpy(iov[0].iov_base, buf, nn);
268 copy -= nn;
269 buf += nn;
271 if (copy == 0)
272 goto done;
274 memcpy(iov[1].iov_base, buf, copy);
276 done:
277 /* Update fields */
278 sb->sb_cc += size;
279 sb->sb_wptr += size;
280 if (sb->sb_wptr >= (sb->sb_data + sb->sb_datalen))
281 sb->sb_wptr -= sb->sb_datalen;
282 return size;
283 err:
285 sofcantrcvmore(so);
286 tcp_sockclosed(sototcpcb(so));
287 fprintf(stderr, "soreadbuf buffer to small");
288 return -1;
292 * Get urgent data
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);
302 int ret;
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
313 * urgent data.
315 ret = soread(so);
316 if (ret > 0) {
317 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
318 tp->t_force = 1;
319 tcp_output(tp);
320 tp->t_force = 0;
323 return ret;
327 * Send urgent data
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 */
336 int n, len;
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)); */
348 } else {
350 * Since there's no sendv or sendtov like writev,
351 * we must copy all data to a linear buffer then
352 * send it all
354 uint32_t urgc = so->so_urgc;
355 len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
356 if (len > urgc) {
357 len = urgc;
359 memcpy(buff, sb->sb_rptr, len);
360 urgc -= len;
361 if (urgc) {
362 n = sb->sb_wptr - sb->sb_data;
363 if (n > urgc) {
364 n = urgc;
366 memcpy((buff + len), sb->sb_data, n);
367 len += n;
369 n = slirp_send(so, buff, len, (MSG_OOB)); /* |MSG_DONTWAIT)); */
372 #ifdef DEBUG
373 if (n != len) {
374 DEBUG_ERROR((dfd, "Didn't send all data urgently XXXXX\n"));
376 #endif
377 if (n < 0) {
378 return n;
380 so->so_urgc -= n;
381 DEBUG_MISC((dfd, " ---2 sent %d bytes urgent data, %d urgent bytes left\n", n, so->so_urgc));
383 sb->sb_cc -= n;
384 sb->sb_rptr += n;
385 if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
386 sb->sb_rptr -= sb->sb_datalen;
388 return n;
392 * Write data from so_rcv to so's socket,
393 * updating all sbuf field as necessary
396 sowrite(struct socket *so)
398 int n,nn;
399 struct sbuf *sb = &so->so_rcv;
400 int len = sb->sb_cc;
401 struct iovec iov[2];
403 DEBUG_CALL("sowrite");
404 DEBUG_ARG("so = %p", so);
406 if (so->so_urgc) {
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;
416 if (sb->sb_cc == 0)
417 return 0;
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;
427 iov[1].iov_len = 0;
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;
432 n = 1;
433 } else {
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;
437 if (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;
441 n = 2;
442 } else
443 n = 1;
445 /* Check if there's urgent data to send, and if so, send it */
447 #ifdef HAVE_READV
448 nn = writev(so->s, (const struct iovec *)iov, n);
450 DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", nn));
451 #else
452 nn = slirp_send(so, iov[0].iov_base, iov[0].iov_len,0);
453 #endif
454 /* This should never happen, but people tell me it does *shrug* */
455 if (nn < 0 && (errno == EAGAIN || errno == EINTR))
456 return 0;
458 if (nn <= 0) {
459 goto err_disconnected;
462 #ifndef HAVE_READV
463 if (n == 2 && nn == iov[0].iov_len) {
464 int ret;
465 ret = slirp_send(so, iov[1].iov_base, iov[1].iov_len,0);
466 if (ret > 0)
467 nn += ret;
469 DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", nn));
470 #endif
472 /* Update sbuf */
473 sb->sb_cc -= nn;
474 sb->sb_rptr += nn;
475 if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
476 sb->sb_rptr -= sb->sb_datalen;
479 * If in DRAIN mode, and there's no more data, set
480 * it CANTSENDMORE
482 if ((so->so_state & SS_FWDRAIN) && sb->sb_cc == 0)
483 sofcantsendmore(so);
485 return nn;
487 err_disconnected:
488 DEBUG_MISC((dfd, " --- sowrite disconnected, so->so_state = %x, errno = %d\n",
489 so->so_state, errno));
490 sofcantsendmore(so);
491 tcp_sockclosed(sototcpcb(so));
492 return -1;
496 * recvfrom() a UDP socket
498 void
499 sorecvfrom(struct socket *so)
501 struct sockaddr_storage addr;
502 struct sockaddr_storage saddr, daddr;
503 socklen_t addrlen = sizeof(struct sockaddr_storage);
505 DEBUG_CALL("sorecvfrom");
506 DEBUG_ARG("so = %p", so);
508 if (so->so_type == IPPROTO_ICMP) { /* This is a "ping" reply */
509 char buff[256];
510 int len;
512 len = recvfrom(so->s, buff, 256, 0,
513 (struct sockaddr *)&addr, &addrlen);
514 /* XXX Check if reply is "correct"? */
516 if(len == -1 || len == 0) {
517 u_char code=ICMP_UNREACH_PORT;
519 if(errno == EHOSTUNREACH) code=ICMP_UNREACH_HOST;
520 else if(errno == ENETUNREACH) code=ICMP_UNREACH_NET;
522 DEBUG_MISC((dfd," udp icmp rx errno = %d-%s\n",
523 errno,strerror(errno)));
524 icmp_send_error(so->so_m, ICMP_UNREACH, code, 0, strerror(errno));
525 } else {
526 icmp_reflect(so->so_m);
527 so->so_m = NULL; /* Don't m_free() it again! */
529 /* No need for this socket anymore, udp_detach it */
530 udp_detach(so);
531 } else { /* A "normal" UDP packet */
532 struct mbuf *m;
533 int len;
534 #ifdef _WIN32
535 unsigned long n;
536 #else
537 int n;
538 #endif
540 m = m_get(so->slirp);
541 if (!m) {
542 return;
544 switch (so->so_ffamily) {
545 case AF_INET:
546 m->m_data += IF_MAXLINKHDR + sizeof(struct udpiphdr);
547 break;
548 case AF_INET6:
549 m->m_data += IF_MAXLINKHDR + sizeof(struct ip6)
550 + sizeof(struct udphdr);
551 break;
552 default:
553 g_assert_not_reached();
554 break;
558 * XXX Shouldn't FIONREAD packets destined for port 53,
559 * but I don't know the max packet size for DNS lookups
561 len = M_FREEROOM(m);
562 /* if (so->so_fport != htons(53)) { */
563 ioctlsocket(so->s, FIONREAD, &n);
565 if (n > len) {
566 n = (m->m_data - m->m_dat) + m->m_len + n + 1;
567 m_inc(m, n);
568 len = M_FREEROOM(m);
570 /* } */
572 m->m_len = recvfrom(so->s, m->m_data, len, 0,
573 (struct sockaddr *)&addr, &addrlen);
574 DEBUG_MISC((dfd, " did recvfrom %d, errno = %d-%s\n",
575 m->m_len, errno,strerror(errno)));
576 if(m->m_len<0) {
577 /* Report error as ICMP */
578 switch (so->so_lfamily) {
579 uint8_t code;
580 case AF_INET:
581 code = ICMP_UNREACH_PORT;
583 if (errno == EHOSTUNREACH) {
584 code = ICMP_UNREACH_HOST;
585 } else if (errno == ENETUNREACH) {
586 code = ICMP_UNREACH_NET;
589 DEBUG_MISC((dfd, " rx error, tx icmp ICMP_UNREACH:%i\n", code));
590 icmp_send_error(so->so_m, ICMP_UNREACH, code, 0, strerror(errno));
591 break;
592 case AF_INET6:
593 code = ICMP6_UNREACH_PORT;
595 if (errno == EHOSTUNREACH) {
596 code = ICMP6_UNREACH_ADDRESS;
597 } else if (errno == ENETUNREACH) {
598 code = ICMP6_UNREACH_NO_ROUTE;
601 DEBUG_MISC((dfd, " rx error, tx icmp6 ICMP_UNREACH:%i\n", code));
602 icmp6_send_error(so->so_m, ICMP6_UNREACH, code);
603 break;
604 default:
605 g_assert_not_reached();
606 break;
608 m_free(m);
609 } else {
611 * Hack: domain name lookup will be used the most for UDP,
612 * and since they'll only be used once there's no need
613 * for the 4 minute (or whatever) timeout... So we time them
614 * out much quicker (10 seconds for now...)
616 if (so->so_expire) {
617 if (so->so_fport == htons(53))
618 so->so_expire = curtime + SO_EXPIREFAST;
619 else
620 so->so_expire = curtime + SO_EXPIRE;
624 * If this packet was destined for CTL_ADDR,
625 * make it look like that's where it came from
627 saddr = addr;
628 sotranslate_in(so, &saddr);
629 daddr = so->lhost.ss;
631 switch (so->so_ffamily) {
632 case AF_INET:
633 udp_output(so, m, (struct sockaddr_in *) &saddr,
634 (struct sockaddr_in *) &daddr,
635 so->so_iptos);
636 break;
637 case AF_INET6:
638 udp6_output(so, m, (struct sockaddr_in6 *) &saddr,
639 (struct sockaddr_in6 *) &daddr);
640 break;
641 default:
642 g_assert_not_reached();
643 break;
645 } /* rx error */
646 } /* if ping packet */
650 * sendto() a socket
653 sosendto(struct socket *so, struct mbuf *m)
655 int ret;
656 struct sockaddr_storage addr;
658 DEBUG_CALL("sosendto");
659 DEBUG_ARG("so = %p", so);
660 DEBUG_ARG("m = %p", m);
662 addr = so->fhost.ss;
663 DEBUG_CALL(" sendto()ing)");
664 sotranslate_out(so, &addr);
666 /* Don't care what port we get */
667 ret = sendto(so->s, m->m_data, m->m_len, 0,
668 (struct sockaddr *)&addr, sockaddr_size(&addr));
669 if (ret < 0)
670 return -1;
673 * Kill the socket if there's no reply in 4 minutes,
674 * but only if it's an expirable socket
676 if (so->so_expire)
677 so->so_expire = curtime + SO_EXPIRE;
678 so->so_state &= SS_PERSISTENT_MASK;
679 so->so_state |= SS_ISFCONNECTED; /* So that it gets select()ed */
680 return 0;
684 * Listen for incoming TCP connections
686 struct socket *
687 tcp_listen(Slirp *slirp, uint32_t haddr, u_int hport, uint32_t laddr,
688 u_int lport, int flags)
690 struct sockaddr_in addr;
691 struct socket *so;
692 int s, opt = 1;
693 socklen_t addrlen = sizeof(addr);
694 memset(&addr, 0, addrlen);
696 DEBUG_CALL("tcp_listen");
697 DEBUG_ARG("haddr = %x", haddr);
698 DEBUG_ARG("hport = %d", hport);
699 DEBUG_ARG("laddr = %x", laddr);
700 DEBUG_ARG("lport = %d", lport);
701 DEBUG_ARG("flags = %x", flags);
703 so = socreate(slirp);
704 if (!so) {
705 return NULL;
708 /* Don't tcp_attach... we don't need so_snd nor so_rcv */
709 if ((so->so_tcpcb = tcp_newtcpcb(so)) == NULL) {
710 free(so);
711 return NULL;
713 insque(so, &slirp->tcb);
716 * SS_FACCEPTONCE sockets must time out.
718 if (flags & SS_FACCEPTONCE)
719 so->so_tcpcb->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT*2;
721 so->so_state &= SS_PERSISTENT_MASK;
722 so->so_state |= (SS_FACCEPTCONN | flags);
723 so->so_lfamily = AF_INET;
724 so->so_lport = lport; /* Kept in network format */
725 so->so_laddr.s_addr = laddr; /* Ditto */
727 addr.sin_family = AF_INET;
728 addr.sin_addr.s_addr = haddr;
729 addr.sin_port = hport;
731 if (((s = qemu_socket(AF_INET,SOCK_STREAM,0)) < 0) ||
732 (socket_set_fast_reuse(s) < 0) ||
733 (bind(s,(struct sockaddr *)&addr, sizeof(addr)) < 0) ||
734 (listen(s,1) < 0)) {
735 int tmperrno = errno; /* Don't clobber the real reason we failed */
737 if (s >= 0) {
738 closesocket(s);
740 sofree(so);
741 /* Restore the real errno */
742 #ifdef _WIN32
743 WSASetLastError(tmperrno);
744 #else
745 errno = tmperrno;
746 #endif
747 return NULL;
749 qemu_setsockopt(s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
751 getsockname(s,(struct sockaddr *)&addr,&addrlen);
752 so->so_ffamily = AF_INET;
753 so->so_fport = addr.sin_port;
754 if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr)
755 so->so_faddr = slirp->vhost_addr;
756 else
757 so->so_faddr = addr.sin_addr;
759 so->s = s;
760 return so;
764 * Various session state calls
765 * XXX Should be #define's
766 * The socket state stuff needs work, these often get call 2 or 3
767 * times each when only 1 was needed
769 void
770 soisfconnecting(struct socket *so)
772 so->so_state &= ~(SS_NOFDREF|SS_ISFCONNECTED|SS_FCANTRCVMORE|
773 SS_FCANTSENDMORE|SS_FWDRAIN);
774 so->so_state |= SS_ISFCONNECTING; /* Clobber other states */
777 void
778 soisfconnected(struct socket *so)
780 so->so_state &= ~(SS_ISFCONNECTING|SS_FWDRAIN|SS_NOFDREF);
781 so->so_state |= SS_ISFCONNECTED; /* Clobber other states */
784 static void
785 sofcantrcvmore(struct socket *so)
787 if ((so->so_state & SS_NOFDREF) == 0) {
788 shutdown(so->s,0);
790 so->so_state &= ~(SS_ISFCONNECTING);
791 if (so->so_state & SS_FCANTSENDMORE) {
792 so->so_state &= SS_PERSISTENT_MASK;
793 so->so_state |= SS_NOFDREF; /* Don't select it */
794 } else {
795 so->so_state |= SS_FCANTRCVMORE;
799 static void
800 sofcantsendmore(struct socket *so)
802 if ((so->so_state & SS_NOFDREF) == 0) {
803 shutdown(so->s,1); /* send FIN to fhost */
805 so->so_state &= ~(SS_ISFCONNECTING);
806 if (so->so_state & SS_FCANTRCVMORE) {
807 so->so_state &= SS_PERSISTENT_MASK;
808 so->so_state |= SS_NOFDREF; /* as above */
809 } else {
810 so->so_state |= SS_FCANTSENDMORE;
815 * Set write drain mode
816 * Set CANTSENDMORE once all data has been write()n
818 void
819 sofwdrain(struct socket *so)
821 if (so->so_rcv.sb_cc)
822 so->so_state |= SS_FWDRAIN;
823 else
824 sofcantsendmore(so);
828 * Translate addr in host addr when it is a virtual address
830 void sotranslate_out(struct socket *so, struct sockaddr_storage *addr)
832 Slirp *slirp = so->slirp;
833 struct sockaddr_in *sin = (struct sockaddr_in *)addr;
834 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
836 switch (addr->ss_family) {
837 case AF_INET:
838 if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) ==
839 slirp->vnetwork_addr.s_addr) {
840 /* It's an alias */
841 if (so->so_faddr.s_addr == slirp->vnameserver_addr.s_addr) {
842 if (get_dns_addr(&sin->sin_addr) < 0) {
843 sin->sin_addr = loopback_addr;
845 } else {
846 sin->sin_addr = loopback_addr;
850 DEBUG_MISC((dfd, " addr.sin_port=%d, "
851 "addr.sin_addr.s_addr=%.16s\n",
852 ntohs(sin->sin_port), inet_ntoa(sin->sin_addr)));
853 break;
855 case AF_INET6:
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)) {
859 uint32_t scope_id;
860 if (get_dns6_addr(&sin6->sin6_addr, &scope_id) >= 0) {
861 sin6->sin6_scope_id = scope_id;
862 } else {
863 sin6->sin6_addr = in6addr_loopback;
865 } else {
866 sin6->sin6_addr = in6addr_loopback;
869 break;
871 default:
872 break;
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) {
883 case AF_INET:
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;
895 break;
897 case AF_INET6:
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;
905 break;
907 default:
908 break;
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) {
920 case AF_INET:
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;
926 break;
928 case AF_INET6:
929 if (in6_equal(&so->so_faddr6, &in6addr_any) ||
930 in6_equal(&so->so_faddr6, &in6addr_loopback)) {
931 so->so_faddr6 = slirp->vhost_addr6;
933 break;
935 default:
936 break;