These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / slirp / socket.c
1 /*
2  * Copyright (c) 1995 Danny Gasparovski.
3  *
4  * Please read the file COPYRIGHT for the
5  * terms and conditions of the copyright.
6  */
7
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
15
16 static void sofcantrcvmore(struct socket *so);
17 static void sofcantsendmore(struct socket *so);
18
19 struct socket *solookup(struct socket **last, struct socket *head,
20         struct sockaddr_storage *lhost, struct sockaddr_storage *fhost)
21 {
22     struct socket *so = *last;
23
24     /* Optimisation */
25     if (so != head && sockaddr_equal(&(so->lhost.ss), lhost)
26             && (!fhost || sockaddr_equal(&so->fhost.ss, fhost))) {
27         return so;
28     }
29
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;
35         }
36     }
37
38     return (struct socket *)NULL;
39 }
40
41 /*
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
45  */
46 struct socket *
47 socreate(Slirp *slirp)
48 {
49   struct socket *so;
50
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;
58   }
59   return(so);
60 }
61
62 /*
63  * remque and free a socket, clobber cache
64  */
65 void
66 sofree(struct socket *so)
67 {
68   Slirp *slirp = so->slirp;
69
70   if (so->so_emu==EMU_RSH && so->extra) {
71         sofree(so->extra);
72         so->extra=NULL;
73   }
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;
80   }
81   m_free(so->so_m);
82
83   if(so->so_next && so->so_prev)
84     remque(so);  /* crashes if so is not in a queue */
85
86   free(so);
87 }
88
89 size_t sopreprbuf(struct socket *so, struct iovec *iov, int *np)
90 {
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;
95
96         DEBUG_CALL("sopreprbuf");
97         DEBUG_ARG("so = %p", so);
98
99         if (len <= 0)
100                 return 0;
101
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;
133                                 }
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;
140                 }
141         }
142         if (np)
143                 *np = n;
144
145         return iov[0].iov_len + (n - 1) * iov[1].iov_len;
146 }
147
148 /*
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
152  */
153 int
154 soread(struct socket *so)
155 {
156         int n, nn;
157         struct sbuf *sb = &so->so_snd;
158         struct iovec iov[2];
159
160         DEBUG_CALL("soread");
161         DEBUG_ARG("so = %p", so);
162
163         /*
164          * No need to check if there's enough room to read.
165          * soread wouldn't have been called if there weren't
166          */
167         sopreprbuf(so, iov, &n);
168
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;
181
182                         err = errno;
183                         if (nn == 0) {
184                                 getsockopt(so->s, SOL_SOCKET, SO_ERROR,
185                                            &err, &slen);
186                         }
187
188                         DEBUG_MISC((dfd, " --- soread() disconnected, nn = %d, errno = %d-%s\n", nn, errno,strerror(errno)));
189                         sofcantrcvmore(so);
190
191                         if (err == ECONNRESET || err == ECONNREFUSED
192                             || err == ENOTCONN || err == EPIPE) {
193                                 tcp_drop(sototcpcb(so), err);
194                         } else {
195                                 tcp_sockclosed(sototcpcb(so));
196                         }
197                         return -1;
198                 }
199         }
200
201 #ifndef HAVE_READV
202         /*
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 wont (shouldn't) happen, since it didn't happen above
210          */
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;
216         }
217
218         DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
219 #endif
220
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;
227 }
228
229 int soreadbuf(struct socket *so, const char *buf, int size)
230 {
231     int n, nn, copy = size;
232         struct sbuf *sb = &so->so_snd;
233         struct iovec iov[2];
234
235         DEBUG_CALL("soreadbuf");
236         DEBUG_ARG("so = %p", so);
237
238         /*
239          * No need to check if there's enough room to read.
240          * soread wouldn't have been called if there weren't
241          */
242         if (sopreprbuf(so, iov, &n) < size)
243         goto err;
244
245     nn = MIN(iov[0].iov_len, copy);
246     memcpy(iov[0].iov_base, buf, nn);
247
248     copy -= nn;
249     buf += nn;
250
251     if (copy == 0)
252         goto done;
253
254     memcpy(iov[1].iov_base, buf, copy);
255
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:
264
265     sofcantrcvmore(so);
266     tcp_sockclosed(sototcpcb(so));
267     fprintf(stderr, "soreadbuf buffer to small");
268     return -1;
269 }
270
271 /*
272  * Get urgent data
273  *
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
277  */
278 int
279 sorecvoob(struct socket *so)
280 {
281         struct tcpcb *tp = sototcpcb(so);
282         int ret;
283
284         DEBUG_CALL("sorecvoob");
285         DEBUG_ARG("so = %p", so);
286
287         /*
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.
294          */
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;
301         }
302
303         return ret;
304 }
305
306 /*
307  * Send urgent data
308  * There's a lot duplicated code here, but...
309  */
310 int
311 sosendoob(struct socket *so)
312 {
313         struct sbuf *sb = &so->so_rcv;
314         char buff[2048]; /* XXX Shouldn't be sending more oob data than this */
315
316         int n, len;
317
318         DEBUG_CALL("sosendoob");
319         DEBUG_ARG("so = %p", so);
320         DEBUG_ARG("sb->sb_cc = %d", sb->sb_cc);
321
322         if (so->so_urgc > 2048)
323            so->so_urgc = 2048; /* XXXX */
324
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;
329
330                 DEBUG_MISC((dfd, " --- sent %d bytes urgent data, %d urgent bytes left\n", n, so->so_urgc));
331         } else {
332                 /*
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
336                  */
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;
347                 }
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));
354         }
355
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;
360
361         return n;
362 }
363
364 /*
365  * Write data from so_rcv to so's socket,
366  * updating all sbuf field as necessary
367  */
368 int
369 sowrite(struct socket *so)
370 {
371         int  n,nn;
372         struct sbuf *sb = &so->so_rcv;
373         int len = sb->sb_cc;
374         struct iovec iov[2];
375
376         DEBUG_CALL("sowrite");
377         DEBUG_ARG("so = %p", so);
378
379         if (so->so_urgc) {
380                 sosendoob(so);
381                 if (sb->sb_cc == 0)
382                         return 0;
383         }
384
385         /*
386          * No need to check if there's something to write,
387          * sowrite wouldn't have been called otherwise
388          */
389
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;
409         }
410         /* Check if there's urgent data to send, and if so, send it */
411
412 #ifdef HAVE_READV
413         nn = writev(so->s, (const struct iovec *)iov, n);
414
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;
422
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;
429         }
430
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;
437         }
438         DEBUG_MISC((dfd, "  ... wrote nn = %d bytes\n", nn));
439 #endif
440
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;
446
447         /*
448          * If in DRAIN mode, and there's no more data, set
449          * it CANTSENDMORE
450          */
451         if ((so->so_state & SS_FWDRAIN) && sb->sb_cc == 0)
452                 sofcantsendmore(so);
453
454         return nn;
455 }
456
457 /*
458  * recvfrom() a UDP socket
459  */
460 void
461 sorecvfrom(struct socket *so)
462 {
463         struct sockaddr_storage addr;
464         struct sockaddr_storage saddr, daddr;
465         socklen_t addrlen = sizeof(struct sockaddr_storage);
466
467         DEBUG_CALL("sorecvfrom");
468         DEBUG_ARG("so = %p", so);
469
470         if (so->so_type == IPPROTO_ICMP) {   /* This is a "ping" reply */
471           char buff[256];
472           int len;
473
474           len = recvfrom(so->s, buff, 256, 0,
475                          (struct sockaddr *)&addr, &addrlen);
476           /* XXX Check if reply is "correct"? */
477
478           if(len == -1 || len == 0) {
479             u_char code=ICMP_UNREACH_PORT;
480
481             if(errno == EHOSTUNREACH) code=ICMP_UNREACH_HOST;
482             else if(errno == ENETUNREACH) code=ICMP_UNREACH_NET;
483
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! */
490           }
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
501
502           m = m_get(so->slirp);
503           if (!m) {
504               return;
505           }
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;
517           }
518
519           /*
520            * XXX Shouldn't FIONREAD packets destined for port 53,
521            * but I don't know the max packet size for DNS lookups
522            */
523           len = M_FREEROOM(m);
524           /* if (so->so_fport != htons(53)) { */
525           ioctlsocket(so->s, FIONREAD, &n);
526
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);
531           }
532           /* } */
533
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;
544
545               if (errno == EHOSTUNREACH) {
546                 code = ICMP_UNREACH_HOST;
547               } else if (errno == ENETUNREACH) {
548                 code = ICMP_UNREACH_NET;
549               }
550
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;
556
557               if (errno == EHOSTUNREACH) {
558                 code = ICMP6_UNREACH_ADDRESS;
559               } else if (errno == ENETUNREACH) {
560                 code = ICMP6_UNREACH_NO_ROUTE;
561               }
562
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;
569             }
570             m_free(m);
571           } else {
572           /*
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...)
577            */
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;
583             }
584
585             /*
586              * If this packet was destined for CTL_ADDR,
587              * make it look like that's where it came from
588              */
589             saddr = addr;
590             sotranslate_in(so, &saddr);
591             daddr = so->lhost.ss;
592
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;
606             }
607           } /* rx error */
608         } /* if ping packet */
609 }
610
611 /*
612  * sendto() a socket
613  */
614 int
615 sosendto(struct socket *so, struct mbuf *m)
616 {
617         int ret;
618         struct sockaddr_storage addr;
619
620         DEBUG_CALL("sosendto");
621         DEBUG_ARG("so = %p", so);
622         DEBUG_ARG("m = %p", m);
623
624         addr = so->fhost.ss;
625         DEBUG_CALL(" sendto()ing)");
626         sotranslate_out(so, &addr);
627
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;
633
634         /*
635          * Kill the socket if there's no reply in 4 minutes,
636          * but only if it's an expirable socket
637          */
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;
643 }
644
645 /*
646  * Listen for incoming TCP connections
647  */
648 struct socket *
649 tcp_listen(Slirp *slirp, uint32_t haddr, u_int hport, uint32_t laddr,
650            u_int lport, int flags)
651 {
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);
657
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);
664
665         so = socreate(slirp);
666         if (!so) {
667           return NULL;
668         }
669
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;
674         }
675         insque(so, &slirp->tcb);
676
677         /*
678          * SS_FACCEPTONCE sockets must time out.
679          */
680         if (flags & SS_FACCEPTONCE)
681            so->so_tcpcb->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT*2;
682
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 */
688
689         addr.sin_family = AF_INET;
690         addr.sin_addr.s_addr = haddr;
691         addr.sin_port = hport;
692
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 */
698
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;
708         }
709         qemu_setsockopt(s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
710
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;
718
719         so->s = s;
720         return so;
721 }
722
723 /*
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
728  */
729 void
730 soisfconnecting(struct socket *so)
731 {
732         so->so_state &= ~(SS_NOFDREF|SS_ISFCONNECTED|SS_FCANTRCVMORE|
733                           SS_FCANTSENDMORE|SS_FWDRAIN);
734         so->so_state |= SS_ISFCONNECTING; /* Clobber other states */
735 }
736
737 void
738 soisfconnected(struct socket *so)
739 {
740         so->so_state &= ~(SS_ISFCONNECTING|SS_FWDRAIN|SS_NOFDREF);
741         so->so_state |= SS_ISFCONNECTED; /* Clobber other states */
742 }
743
744 static void
745 sofcantrcvmore(struct socket *so)
746 {
747         if ((so->so_state & SS_NOFDREF) == 0) {
748                 shutdown(so->s,0);
749         }
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;
756         }
757 }
758
759 static void
760 sofcantsendmore(struct socket *so)
761 {
762         if ((so->so_state & SS_NOFDREF) == 0) {
763             shutdown(so->s,1);           /* send FIN to fhost */
764         }
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;
771         }
772 }
773
774 /*
775  * Set write drain mode
776  * Set CANTSENDMORE once all data has been write()n
777  */
778 void
779 sofwdrain(struct socket *so)
780 {
781         if (so->so_rcv.sb_cc)
782                 so->so_state |= SS_FWDRAIN;
783         else
784                 sofcantsendmore(so);
785 }
786
787 /*
788  * Translate addr in host addr when it is a virtual address
789  */
790 void sotranslate_out(struct socket *so, struct sockaddr_storage *addr)
791 {
792     Slirp *slirp = so->slirp;
793     struct sockaddr_in *sin = (struct sockaddr_in *)addr;
794     struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
795
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;
804                 }
805             } else {
806                 sin->sin_addr = loopback_addr;
807             }
808         }
809
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;
814
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                 /*if (get_dns_addr(&addr) < 0) {*/ /* TODO */
820                     sin6->sin6_addr = in6addr_loopback;
821                 /*}*/
822             } else {
823                 sin6->sin6_addr = in6addr_loopback;
824             }
825         }
826         break;
827
828     default:
829         break;
830     }
831 }
832
833 void sotranslate_in(struct socket *so, struct sockaddr_storage *addr)
834 {
835     Slirp *slirp = so->slirp;
836     struct sockaddr_in *sin = (struct sockaddr_in *)addr;
837     struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
838
839     switch (addr->ss_family) {
840     case AF_INET:
841         if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) ==
842             slirp->vnetwork_addr.s_addr) {
843             uint32_t inv_mask = ~slirp->vnetwork_mask.s_addr;
844
845             if ((so->so_faddr.s_addr & inv_mask) == inv_mask) {
846                 sin->sin_addr = slirp->vhost_addr;
847             } else if (sin->sin_addr.s_addr == loopback_addr.s_addr ||
848                        so->so_faddr.s_addr != slirp->vhost_addr.s_addr) {
849                 sin->sin_addr = so->so_faddr;
850             }
851         }
852         break;
853
854     case AF_INET6:
855         if (in6_equal_net(&so->so_faddr6, &slirp->vprefix_addr6,
856                     slirp->vprefix_len)) {
857             if (in6_equal(&sin6->sin6_addr, &in6addr_loopback)
858                     || !in6_equal(&so->so_faddr6, &slirp->vhost_addr6)) {
859                 sin6->sin6_addr = so->so_faddr6;
860             }
861         }
862         break;
863
864     default:
865         break;
866     }
867 }
868
869 /*
870  * Translate connections from localhost to the real hostname
871  */
872 void sotranslate_accept(struct socket *so)
873 {
874     Slirp *slirp = so->slirp;
875
876     switch (so->so_ffamily) {
877     case AF_INET:
878         if (so->so_faddr.s_addr == INADDR_ANY ||
879             (so->so_faddr.s_addr & loopback_mask) ==
880             (loopback_addr.s_addr & loopback_mask)) {
881            so->so_faddr = slirp->vhost_addr;
882         }
883         break;
884
885    case AF_INET6:
886         if (in6_equal(&so->so_faddr6, &in6addr_any) ||
887                 in6_equal(&so->so_faddr6, &in6addr_loopback)) {
888            so->so_faddr6 = slirp->vhost_addr6;
889         }
890         break;
891
892     default:
893         break;
894     }
895 }