hw/mips: fix PCI bus initialization
[qemu/ar7.git] / slirp / socket.c
blob280050a21f87b51cf2f6c056f473d8b6ce081af9
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 int err;
180 socklen_t slen = sizeof err;
182 err = errno;
183 if (nn == 0) {
184 getsockopt(so->s, SOL_SOCKET, SO_ERROR,
185 &err, &slen);
188 DEBUG_MISC((dfd, " --- soread() disconnected, nn = %d, errno = %d-%s\n", nn, errno,strerror(errno)));
189 sofcantrcvmore(so);
191 if (err == ECONNRESET || err == ECONNREFUSED
192 || err == ENOTCONN || err == EPIPE) {
193 tcp_drop(sototcpcb(so), err);
194 } else {
195 tcp_sockclosed(sototcpcb(so));
197 return -1;
201 #ifndef HAVE_READV
203 * If there was no error, try and read the second time round
204 * We read again if n = 2 (ie, there's another part of the buffer)
205 * and we read as much as we could in the first read
206 * We don't test for <= 0 this time, because there legitimately
207 * might not be any more data (since the socket is non-blocking),
208 * a close will be detected on next iteration.
209 * A return of -1 won't (shouldn't) happen, since it didn't happen above
211 if (n == 2 && nn == iov[0].iov_len) {
212 int ret;
213 ret = qemu_recv(so->s, iov[1].iov_base, iov[1].iov_len,0);
214 if (ret > 0)
215 nn += ret;
218 DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
219 #endif
221 /* Update fields */
222 sb->sb_cc += nn;
223 sb->sb_wptr += nn;
224 if (sb->sb_wptr >= (sb->sb_data + sb->sb_datalen))
225 sb->sb_wptr -= sb->sb_datalen;
226 return nn;
229 int soreadbuf(struct socket *so, const char *buf, int size)
231 int n, nn, copy = size;
232 struct sbuf *sb = &so->so_snd;
233 struct iovec iov[2];
235 DEBUG_CALL("soreadbuf");
236 DEBUG_ARG("so = %p", so);
239 * No need to check if there's enough room to read.
240 * soread wouldn't have been called if there weren't
242 if (sopreprbuf(so, iov, &n) < size)
243 goto err;
245 nn = MIN(iov[0].iov_len, copy);
246 memcpy(iov[0].iov_base, buf, nn);
248 copy -= nn;
249 buf += nn;
251 if (copy == 0)
252 goto done;
254 memcpy(iov[1].iov_base, buf, copy);
256 done:
257 /* Update fields */
258 sb->sb_cc += size;
259 sb->sb_wptr += size;
260 if (sb->sb_wptr >= (sb->sb_data + sb->sb_datalen))
261 sb->sb_wptr -= sb->sb_datalen;
262 return size;
263 err:
265 sofcantrcvmore(so);
266 tcp_sockclosed(sototcpcb(so));
267 fprintf(stderr, "soreadbuf buffer to small");
268 return -1;
272 * Get urgent data
274 * When the socket is created, we set it SO_OOBINLINE,
275 * so when OOB data arrives, we soread() it and everything
276 * in the send buffer is sent as urgent data
279 sorecvoob(struct socket *so)
281 struct tcpcb *tp = sototcpcb(so);
282 int ret;
284 DEBUG_CALL("sorecvoob");
285 DEBUG_ARG("so = %p", so);
288 * We take a guess at how much urgent data has arrived.
289 * In most situations, when urgent data arrives, the next
290 * read() should get all the urgent data. This guess will
291 * be wrong however if more data arrives just after the
292 * urgent data, or the read() doesn't return all the
293 * urgent data.
295 ret = soread(so);
296 if (ret > 0) {
297 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
298 tp->t_force = 1;
299 tcp_output(tp);
300 tp->t_force = 0;
303 return ret;
307 * Send urgent data
308 * There's a lot duplicated code here, but...
311 sosendoob(struct socket *so)
313 struct sbuf *sb = &so->so_rcv;
314 char buff[2048]; /* XXX Shouldn't be sending more oob data than this */
316 int n, len;
318 DEBUG_CALL("sosendoob");
319 DEBUG_ARG("so = %p", so);
320 DEBUG_ARG("sb->sb_cc = %d", sb->sb_cc);
322 if (so->so_urgc > 2048)
323 so->so_urgc = 2048; /* XXXX */
325 if (sb->sb_rptr < sb->sb_wptr) {
326 /* We can send it directly */
327 n = slirp_send(so, sb->sb_rptr, so->so_urgc, (MSG_OOB)); /* |MSG_DONTWAIT)); */
328 so->so_urgc -= n;
330 DEBUG_MISC((dfd, " --- sent %d bytes urgent data, %d urgent bytes left\n", n, so->so_urgc));
331 } else {
333 * Since there's no sendv or sendtov like writev,
334 * we must copy all data to a linear buffer then
335 * send it all
337 len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
338 if (len > so->so_urgc) len = so->so_urgc;
339 memcpy(buff, sb->sb_rptr, len);
340 so->so_urgc -= len;
341 if (so->so_urgc) {
342 n = sb->sb_wptr - sb->sb_data;
343 if (n > so->so_urgc) n = so->so_urgc;
344 memcpy((buff + len), sb->sb_data, n);
345 so->so_urgc -= n;
346 len += n;
348 n = slirp_send(so, buff, len, (MSG_OOB)); /* |MSG_DONTWAIT)); */
349 #ifdef DEBUG
350 if (n != len)
351 DEBUG_ERROR((dfd, "Didn't send all data urgently XXXXX\n"));
352 #endif
353 DEBUG_MISC((dfd, " ---2 sent %d bytes urgent data, %d urgent bytes left\n", n, so->so_urgc));
356 sb->sb_cc -= n;
357 sb->sb_rptr += n;
358 if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
359 sb->sb_rptr -= sb->sb_datalen;
361 return n;
365 * Write data from so_rcv to so's socket,
366 * updating all sbuf field as necessary
369 sowrite(struct socket *so)
371 int n,nn;
372 struct sbuf *sb = &so->so_rcv;
373 int len = sb->sb_cc;
374 struct iovec iov[2];
376 DEBUG_CALL("sowrite");
377 DEBUG_ARG("so = %p", so);
379 if (so->so_urgc) {
380 sosendoob(so);
381 if (sb->sb_cc == 0)
382 return 0;
386 * No need to check if there's something to write,
387 * sowrite wouldn't have been called otherwise
390 iov[0].iov_base = sb->sb_rptr;
391 iov[1].iov_base = NULL;
392 iov[1].iov_len = 0;
393 if (sb->sb_rptr < sb->sb_wptr) {
394 iov[0].iov_len = sb->sb_wptr - sb->sb_rptr;
395 /* Should never succeed, but... */
396 if (iov[0].iov_len > len) iov[0].iov_len = len;
397 n = 1;
398 } else {
399 iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
400 if (iov[0].iov_len > len) iov[0].iov_len = len;
401 len -= iov[0].iov_len;
402 if (len) {
403 iov[1].iov_base = sb->sb_data;
404 iov[1].iov_len = sb->sb_wptr - sb->sb_data;
405 if (iov[1].iov_len > len) iov[1].iov_len = len;
406 n = 2;
407 } else
408 n = 1;
410 /* Check if there's urgent data to send, and if so, send it */
412 #ifdef HAVE_READV
413 nn = writev(so->s, (const struct iovec *)iov, n);
415 DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", nn));
416 #else
417 nn = slirp_send(so, iov[0].iov_base, iov[0].iov_len,0);
418 #endif
419 /* This should never happen, but people tell me it does *shrug* */
420 if (nn < 0 && (errno == EAGAIN || errno == EINTR))
421 return 0;
423 if (nn <= 0) {
424 DEBUG_MISC((dfd, " --- sowrite disconnected, so->so_state = %x, errno = %d\n",
425 so->so_state, errno));
426 sofcantsendmore(so);
427 tcp_sockclosed(sototcpcb(so));
428 return -1;
431 #ifndef HAVE_READV
432 if (n == 2 && nn == iov[0].iov_len) {
433 int ret;
434 ret = slirp_send(so, iov[1].iov_base, iov[1].iov_len,0);
435 if (ret > 0)
436 nn += ret;
438 DEBUG_MISC((dfd, " ... wrote nn = %d bytes\n", nn));
439 #endif
441 /* Update sbuf */
442 sb->sb_cc -= nn;
443 sb->sb_rptr += nn;
444 if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
445 sb->sb_rptr -= sb->sb_datalen;
448 * If in DRAIN mode, and there's no more data, set
449 * it CANTSENDMORE
451 if ((so->so_state & SS_FWDRAIN) && sb->sb_cc == 0)
452 sofcantsendmore(so);
454 return nn;
458 * recvfrom() a UDP socket
460 void
461 sorecvfrom(struct socket *so)
463 struct sockaddr_storage addr;
464 struct sockaddr_storage saddr, daddr;
465 socklen_t addrlen = sizeof(struct sockaddr_storage);
467 DEBUG_CALL("sorecvfrom");
468 DEBUG_ARG("so = %p", so);
470 if (so->so_type == IPPROTO_ICMP) { /* This is a "ping" reply */
471 char buff[256];
472 int len;
474 len = recvfrom(so->s, buff, 256, 0,
475 (struct sockaddr *)&addr, &addrlen);
476 /* XXX Check if reply is "correct"? */
478 if(len == -1 || len == 0) {
479 u_char code=ICMP_UNREACH_PORT;
481 if(errno == EHOSTUNREACH) code=ICMP_UNREACH_HOST;
482 else if(errno == ENETUNREACH) code=ICMP_UNREACH_NET;
484 DEBUG_MISC((dfd," udp icmp rx errno = %d-%s\n",
485 errno,strerror(errno)));
486 icmp_send_error(so->so_m, ICMP_UNREACH, code, 0, strerror(errno));
487 } else {
488 icmp_reflect(so->so_m);
489 so->so_m = NULL; /* Don't m_free() it again! */
491 /* No need for this socket anymore, udp_detach it */
492 udp_detach(so);
493 } else { /* A "normal" UDP packet */
494 struct mbuf *m;
495 int len;
496 #ifdef _WIN32
497 unsigned long n;
498 #else
499 int n;
500 #endif
502 m = m_get(so->slirp);
503 if (!m) {
504 return;
506 switch (so->so_ffamily) {
507 case AF_INET:
508 m->m_data += IF_MAXLINKHDR + sizeof(struct udpiphdr);
509 break;
510 case AF_INET6:
511 m->m_data += IF_MAXLINKHDR + sizeof(struct ip6)
512 + sizeof(struct udphdr);
513 break;
514 default:
515 g_assert_not_reached();
516 break;
520 * XXX Shouldn't FIONREAD packets destined for port 53,
521 * but I don't know the max packet size for DNS lookups
523 len = M_FREEROOM(m);
524 /* if (so->so_fport != htons(53)) { */
525 ioctlsocket(so->s, FIONREAD, &n);
527 if (n > len) {
528 n = (m->m_data - m->m_dat) + m->m_len + n + 1;
529 m_inc(m, n);
530 len = M_FREEROOM(m);
532 /* } */
534 m->m_len = recvfrom(so->s, m->m_data, len, 0,
535 (struct sockaddr *)&addr, &addrlen);
536 DEBUG_MISC((dfd, " did recvfrom %d, errno = %d-%s\n",
537 m->m_len, errno,strerror(errno)));
538 if(m->m_len<0) {
539 /* Report error as ICMP */
540 switch (so->so_lfamily) {
541 uint8_t code;
542 case AF_INET:
543 code = ICMP_UNREACH_PORT;
545 if (errno == EHOSTUNREACH) {
546 code = ICMP_UNREACH_HOST;
547 } else if (errno == ENETUNREACH) {
548 code = ICMP_UNREACH_NET;
551 DEBUG_MISC((dfd, " rx error, tx icmp ICMP_UNREACH:%i\n", code));
552 icmp_send_error(so->so_m, ICMP_UNREACH, code, 0, strerror(errno));
553 break;
554 case AF_INET6:
555 code = ICMP6_UNREACH_PORT;
557 if (errno == EHOSTUNREACH) {
558 code = ICMP6_UNREACH_ADDRESS;
559 } else if (errno == ENETUNREACH) {
560 code = ICMP6_UNREACH_NO_ROUTE;
563 DEBUG_MISC((dfd, " rx error, tx icmp6 ICMP_UNREACH:%i\n", code));
564 icmp6_send_error(so->so_m, ICMP6_UNREACH, code);
565 break;
566 default:
567 g_assert_not_reached();
568 break;
570 m_free(m);
571 } else {
573 * Hack: domain name lookup will be used the most for UDP,
574 * and since they'll only be used once there's no need
575 * for the 4 minute (or whatever) timeout... So we time them
576 * out much quicker (10 seconds for now...)
578 if (so->so_expire) {
579 if (so->so_fport == htons(53))
580 so->so_expire = curtime + SO_EXPIREFAST;
581 else
582 so->so_expire = curtime + SO_EXPIRE;
586 * If this packet was destined for CTL_ADDR,
587 * make it look like that's where it came from
589 saddr = addr;
590 sotranslate_in(so, &saddr);
591 daddr = so->lhost.ss;
593 switch (so->so_ffamily) {
594 case AF_INET:
595 udp_output(so, m, (struct sockaddr_in *) &saddr,
596 (struct sockaddr_in *) &daddr,
597 so->so_iptos);
598 break;
599 case AF_INET6:
600 udp6_output(so, m, (struct sockaddr_in6 *) &saddr,
601 (struct sockaddr_in6 *) &daddr);
602 break;
603 default:
604 g_assert_not_reached();
605 break;
607 } /* rx error */
608 } /* if ping packet */
612 * sendto() a socket
615 sosendto(struct socket *so, struct mbuf *m)
617 int ret;
618 struct sockaddr_storage addr;
620 DEBUG_CALL("sosendto");
621 DEBUG_ARG("so = %p", so);
622 DEBUG_ARG("m = %p", m);
624 addr = so->fhost.ss;
625 DEBUG_CALL(" sendto()ing)");
626 sotranslate_out(so, &addr);
628 /* Don't care what port we get */
629 ret = sendto(so->s, m->m_data, m->m_len, 0,
630 (struct sockaddr *)&addr, sockaddr_size(&addr));
631 if (ret < 0)
632 return -1;
635 * Kill the socket if there's no reply in 4 minutes,
636 * but only if it's an expirable socket
638 if (so->so_expire)
639 so->so_expire = curtime + SO_EXPIRE;
640 so->so_state &= SS_PERSISTENT_MASK;
641 so->so_state |= SS_ISFCONNECTED; /* So that it gets select()ed */
642 return 0;
646 * Listen for incoming TCP connections
648 struct socket *
649 tcp_listen(Slirp *slirp, uint32_t haddr, u_int hport, uint32_t laddr,
650 u_int lport, int flags)
652 struct sockaddr_in addr;
653 struct socket *so;
654 int s, opt = 1;
655 socklen_t addrlen = sizeof(addr);
656 memset(&addr, 0, addrlen);
658 DEBUG_CALL("tcp_listen");
659 DEBUG_ARG("haddr = %x", haddr);
660 DEBUG_ARG("hport = %d", hport);
661 DEBUG_ARG("laddr = %x", laddr);
662 DEBUG_ARG("lport = %d", lport);
663 DEBUG_ARG("flags = %x", flags);
665 so = socreate(slirp);
666 if (!so) {
667 return NULL;
670 /* Don't tcp_attach... we don't need so_snd nor so_rcv */
671 if ((so->so_tcpcb = tcp_newtcpcb(so)) == NULL) {
672 free(so);
673 return NULL;
675 insque(so, &slirp->tcb);
678 * SS_FACCEPTONCE sockets must time out.
680 if (flags & SS_FACCEPTONCE)
681 so->so_tcpcb->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT*2;
683 so->so_state &= SS_PERSISTENT_MASK;
684 so->so_state |= (SS_FACCEPTCONN | flags);
685 so->so_lfamily = AF_INET;
686 so->so_lport = lport; /* Kept in network format */
687 so->so_laddr.s_addr = laddr; /* Ditto */
689 addr.sin_family = AF_INET;
690 addr.sin_addr.s_addr = haddr;
691 addr.sin_port = hport;
693 if (((s = qemu_socket(AF_INET,SOCK_STREAM,0)) < 0) ||
694 (socket_set_fast_reuse(s) < 0) ||
695 (bind(s,(struct sockaddr *)&addr, sizeof(addr)) < 0) ||
696 (listen(s,1) < 0)) {
697 int tmperrno = errno; /* Don't clobber the real reason we failed */
699 close(s);
700 sofree(so);
701 /* Restore the real errno */
702 #ifdef _WIN32
703 WSASetLastError(tmperrno);
704 #else
705 errno = tmperrno;
706 #endif
707 return NULL;
709 qemu_setsockopt(s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
711 getsockname(s,(struct sockaddr *)&addr,&addrlen);
712 so->so_ffamily = AF_INET;
713 so->so_fport = addr.sin_port;
714 if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr)
715 so->so_faddr = slirp->vhost_addr;
716 else
717 so->so_faddr = addr.sin_addr;
719 so->s = s;
720 return so;
724 * Various session state calls
725 * XXX Should be #define's
726 * The socket state stuff needs work, these often get call 2 or 3
727 * times each when only 1 was needed
729 void
730 soisfconnecting(struct socket *so)
732 so->so_state &= ~(SS_NOFDREF|SS_ISFCONNECTED|SS_FCANTRCVMORE|
733 SS_FCANTSENDMORE|SS_FWDRAIN);
734 so->so_state |= SS_ISFCONNECTING; /* Clobber other states */
737 void
738 soisfconnected(struct socket *so)
740 so->so_state &= ~(SS_ISFCONNECTING|SS_FWDRAIN|SS_NOFDREF);
741 so->so_state |= SS_ISFCONNECTED; /* Clobber other states */
744 static void
745 sofcantrcvmore(struct socket *so)
747 if ((so->so_state & SS_NOFDREF) == 0) {
748 shutdown(so->s,0);
750 so->so_state &= ~(SS_ISFCONNECTING);
751 if (so->so_state & SS_FCANTSENDMORE) {
752 so->so_state &= SS_PERSISTENT_MASK;
753 so->so_state |= SS_NOFDREF; /* Don't select it */
754 } else {
755 so->so_state |= SS_FCANTRCVMORE;
759 static void
760 sofcantsendmore(struct socket *so)
762 if ((so->so_state & SS_NOFDREF) == 0) {
763 shutdown(so->s,1); /* send FIN to fhost */
765 so->so_state &= ~(SS_ISFCONNECTING);
766 if (so->so_state & SS_FCANTRCVMORE) {
767 so->so_state &= SS_PERSISTENT_MASK;
768 so->so_state |= SS_NOFDREF; /* as above */
769 } else {
770 so->so_state |= SS_FCANTSENDMORE;
775 * Set write drain mode
776 * Set CANTSENDMORE once all data has been write()n
778 void
779 sofwdrain(struct socket *so)
781 if (so->so_rcv.sb_cc)
782 so->so_state |= SS_FWDRAIN;
783 else
784 sofcantsendmore(so);
788 * Translate addr in host addr when it is a virtual address
790 void sotranslate_out(struct socket *so, struct sockaddr_storage *addr)
792 Slirp *slirp = so->slirp;
793 struct sockaddr_in *sin = (struct sockaddr_in *)addr;
794 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
796 switch (addr->ss_family) {
797 case AF_INET:
798 if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) ==
799 slirp->vnetwork_addr.s_addr) {
800 /* It's an alias */
801 if (so->so_faddr.s_addr == slirp->vnameserver_addr.s_addr) {
802 if (get_dns_addr(&sin->sin_addr) < 0) {
803 sin->sin_addr = loopback_addr;
805 } else {
806 sin->sin_addr = loopback_addr;
810 DEBUG_MISC((dfd, " addr.sin_port=%d, "
811 "addr.sin_addr.s_addr=%.16s\n",
812 ntohs(sin->sin_port), inet_ntoa(sin->sin_addr)));
813 break;
815 case AF_INET6:
816 if (in6_equal_net(&so->so_faddr6, &slirp->vprefix_addr6,
817 slirp->vprefix_len)) {
818 if (in6_equal(&so->so_faddr6, &slirp->vnameserver_addr6)) {
819 uint32_t scope_id;
820 if (get_dns6_addr(&sin6->sin6_addr, &scope_id) >= 0) {
821 sin6->sin6_scope_id = scope_id;
822 } else {
823 sin6->sin6_addr = in6addr_loopback;
825 } else {
826 sin6->sin6_addr = in6addr_loopback;
829 break;
831 default:
832 break;
836 void sotranslate_in(struct socket *so, struct sockaddr_storage *addr)
838 Slirp *slirp = so->slirp;
839 struct sockaddr_in *sin = (struct sockaddr_in *)addr;
840 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
842 switch (addr->ss_family) {
843 case AF_INET:
844 if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) ==
845 slirp->vnetwork_addr.s_addr) {
846 uint32_t inv_mask = ~slirp->vnetwork_mask.s_addr;
848 if ((so->so_faddr.s_addr & inv_mask) == inv_mask) {
849 sin->sin_addr = slirp->vhost_addr;
850 } else if (sin->sin_addr.s_addr == loopback_addr.s_addr ||
851 so->so_faddr.s_addr != slirp->vhost_addr.s_addr) {
852 sin->sin_addr = so->so_faddr;
855 break;
857 case AF_INET6:
858 if (in6_equal_net(&so->so_faddr6, &slirp->vprefix_addr6,
859 slirp->vprefix_len)) {
860 if (in6_equal(&sin6->sin6_addr, &in6addr_loopback)
861 || !in6_equal(&so->so_faddr6, &slirp->vhost_addr6)) {
862 sin6->sin6_addr = so->so_faddr6;
865 break;
867 default:
868 break;
873 * Translate connections from localhost to the real hostname
875 void sotranslate_accept(struct socket *so)
877 Slirp *slirp = so->slirp;
879 switch (so->so_ffamily) {
880 case AF_INET:
881 if (so->so_faddr.s_addr == INADDR_ANY ||
882 (so->so_faddr.s_addr & loopback_mask) ==
883 (loopback_addr.s_addr & loopback_mask)) {
884 so->so_faddr = slirp->vhost_addr;
886 break;
888 case AF_INET6:
889 if (in6_equal(&so->so_faddr6, &in6addr_any) ||
890 in6_equal(&so->so_faddr6, &in6addr_loopback)) {
891 so->so_faddr6 = slirp->vhost_addr6;
893 break;
895 default:
896 break;