]> Git Repo - qemu.git/blob - slirp/socket.c
tests/migration: Enable the migration test on s390x, too
[qemu.git] / 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  * Remove references to so from the given message queue.
64  */
65 static void
66 soqfree(struct socket *so, struct quehead *qh)
67 {
68     struct mbuf *ifq;
69
70     for (ifq = (struct mbuf *) qh->qh_link;
71              (struct quehead *) ifq != qh;
72              ifq = ifq->ifq_next) {
73         if (ifq->ifq_so == so) {
74             struct mbuf *ifm;
75             ifq->ifq_so = NULL;
76             for (ifm = ifq->ifs_next; ifm != ifq; ifm = ifm->ifs_next) {
77                 ifm->ifq_so = NULL;
78             }
79         }
80     }
81 }
82
83 /*
84  * remque and free a socket, clobber cache
85  */
86 void
87 sofree(struct socket *so)
88 {
89   Slirp *slirp = so->slirp;
90
91   soqfree(so, &slirp->if_fastq);
92   soqfree(so, &slirp->if_batchq);
93
94   if (so->so_emu==EMU_RSH && so->extra) {
95         sofree(so->extra);
96         so->extra=NULL;
97   }
98   if (so == slirp->tcp_last_so) {
99       slirp->tcp_last_so = &slirp->tcb;
100   } else if (so == slirp->udp_last_so) {
101       slirp->udp_last_so = &slirp->udb;
102   } else if (so == slirp->icmp_last_so) {
103       slirp->icmp_last_so = &slirp->icmp;
104   }
105   m_free(so->so_m);
106
107   if(so->so_next && so->so_prev)
108     remque(so);  /* crashes if so is not in a queue */
109
110   if (so->so_tcpcb) {
111       free(so->so_tcpcb);
112   }
113   free(so);
114 }
115
116 size_t sopreprbuf(struct socket *so, struct iovec *iov, int *np)
117 {
118         int n, lss, total;
119         struct sbuf *sb = &so->so_snd;
120         int len = sb->sb_datalen - sb->sb_cc;
121         int mss = so->so_tcpcb->t_maxseg;
122
123         DEBUG_CALL("sopreprbuf");
124         DEBUG_ARG("so = %p", so);
125
126         if (len <= 0)
127                 return 0;
128
129         iov[0].iov_base = sb->sb_wptr;
130         iov[1].iov_base = NULL;
131         iov[1].iov_len = 0;
132         if (sb->sb_wptr < sb->sb_rptr) {
133                 iov[0].iov_len = sb->sb_rptr - sb->sb_wptr;
134                 /* Should never succeed, but... */
135                 if (iov[0].iov_len > len)
136                    iov[0].iov_len = len;
137                 if (iov[0].iov_len > mss)
138                    iov[0].iov_len -= iov[0].iov_len%mss;
139                 n = 1;
140         } else {
141                 iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_wptr;
142                 /* Should never succeed, but... */
143                 if (iov[0].iov_len > len) iov[0].iov_len = len;
144                 len -= iov[0].iov_len;
145                 if (len) {
146                         iov[1].iov_base = sb->sb_data;
147                         iov[1].iov_len = sb->sb_rptr - sb->sb_data;
148                         if(iov[1].iov_len > len)
149                            iov[1].iov_len = len;
150                         total = iov[0].iov_len + iov[1].iov_len;
151                         if (total > mss) {
152                                 lss = total%mss;
153                                 if (iov[1].iov_len > lss) {
154                                         iov[1].iov_len -= lss;
155                                         n = 2;
156                                 } else {
157                                         lss -= iov[1].iov_len;
158                                         iov[0].iov_len -= lss;
159                                         n = 1;
160                                 }
161                         } else
162                                 n = 2;
163                 } else {
164                         if (iov[0].iov_len > mss)
165                            iov[0].iov_len -= iov[0].iov_len%mss;
166                         n = 1;
167                 }
168         }
169         if (np)
170                 *np = n;
171
172         return iov[0].iov_len + (n - 1) * iov[1].iov_len;
173 }
174
175 /*
176  * Read from so's socket into sb_snd, updating all relevant sbuf fields
177  * NOTE: This will only be called if it is select()ed for reading, so
178  * a read() of 0 (or less) means it's disconnected
179  */
180 int
181 soread(struct socket *so)
182 {
183         int n, nn;
184         struct sbuf *sb = &so->so_snd;
185         struct iovec iov[2];
186
187         DEBUG_CALL("soread");
188         DEBUG_ARG("so = %p", so);
189
190         /*
191          * No need to check if there's enough room to read.
192          * soread wouldn't have been called if there weren't
193          */
194         sopreprbuf(so, iov, &n);
195
196 #ifdef HAVE_READV
197         nn = readv(so->s, (struct iovec *)iov, n);
198         DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
199 #else
200         nn = qemu_recv(so->s, iov[0].iov_base, iov[0].iov_len,0);
201 #endif
202         if (nn <= 0) {
203                 if (nn < 0 && (errno == EINTR || errno == EAGAIN))
204                         return 0;
205                 else {
206                         int err;
207                         socklen_t elen = sizeof err;
208                         struct sockaddr_storage addr;
209                         struct sockaddr *paddr = (struct sockaddr *) &addr;
210                         socklen_t alen = sizeof addr;
211
212                         err = errno;
213                         if (nn == 0) {
214                                 if (getpeername(so->s, paddr, &alen) < 0) {
215                                         err = errno;
216                                 } else {
217                                         getsockopt(so->s, SOL_SOCKET, SO_ERROR,
218                                                 &err, &elen);
219                                 }
220                         }
221
222                         DEBUG_MISC((dfd, " --- soread() disconnected, nn = %d, errno = %d-%s\n", nn, errno,strerror(errno)));
223                         sofcantrcvmore(so);
224
225                         if (err == ECONNRESET || err == ECONNREFUSED
226                             || err == ENOTCONN || err == EPIPE) {
227                                 tcp_drop(sototcpcb(so), err);
228                         } else {
229                                 tcp_sockclosed(sototcpcb(so));
230                         }
231                         return -1;
232                 }
233         }
234
235 #ifndef HAVE_READV
236         /*
237          * If there was no error, try and read the second time round
238          * We read again if n = 2 (ie, there's another part of the buffer)
239          * and we read as much as we could in the first read
240          * We don't test for <= 0 this time, because there legitimately
241          * might not be any more data (since the socket is non-blocking),
242          * a close will be detected on next iteration.
243          * A return of -1 won't (shouldn't) happen, since it didn't happen above
244          */
245         if (n == 2 && nn == iov[0].iov_len) {
246             int ret;
247             ret = qemu_recv(so->s, iov[1].iov_base, iov[1].iov_len,0);
248             if (ret > 0)
249                 nn += ret;
250         }
251
252         DEBUG_MISC((dfd, " ... read nn = %d bytes\n", nn));
253 #endif
254
255         /* Update fields */
256         sb->sb_cc += nn;
257         sb->sb_wptr += nn;
258         if (sb->sb_wptr >= (sb->sb_data + sb->sb_datalen))
259                 sb->sb_wptr -= sb->sb_datalen;
260         return nn;
261 }
262
263 int soreadbuf(struct socket *so, const char *buf, int size)
264 {
265     int n, nn, copy = size;
266         struct sbuf *sb = &so->so_snd;
267         struct iovec iov[2];
268
269         DEBUG_CALL("soreadbuf");
270         DEBUG_ARG("so = %p", so);
271
272         /*
273          * No need to check if there's enough room to read.
274          * soread wouldn't have been called if there weren't
275          */
276         if (sopreprbuf(so, iov, &n) < size)
277         goto err;
278
279     nn = MIN(iov[0].iov_len, copy);
280     memcpy(iov[0].iov_base, buf, nn);
281
282     copy -= nn;
283     buf += nn;
284
285     if (copy == 0)
286         goto done;
287
288     memcpy(iov[1].iov_base, buf, copy);
289
290 done:
291     /* Update fields */
292         sb->sb_cc += size;
293         sb->sb_wptr += size;
294         if (sb->sb_wptr >= (sb->sb_data + sb->sb_datalen))
295                 sb->sb_wptr -= sb->sb_datalen;
296     return size;
297 err:
298
299     sofcantrcvmore(so);
300     tcp_sockclosed(sototcpcb(so));
301     fprintf(stderr, "soreadbuf buffer to small");
302     return -1;
303 }
304
305 /*
306  * Get urgent data
307  *
308  * When the socket is created, we set it SO_OOBINLINE,
309  * so when OOB data arrives, we soread() it and everything
310  * in the send buffer is sent as urgent data
311  */
312 int
313 sorecvoob(struct socket *so)
314 {
315         struct tcpcb *tp = sototcpcb(so);
316         int ret;
317
318         DEBUG_CALL("sorecvoob");
319         DEBUG_ARG("so = %p", so);
320
321         /*
322          * We take a guess at how much urgent data has arrived.
323          * In most situations, when urgent data arrives, the next
324          * read() should get all the urgent data.  This guess will
325          * be wrong however if more data arrives just after the
326          * urgent data, or the read() doesn't return all the
327          * urgent data.
328          */
329         ret = soread(so);
330         if (ret > 0) {
331             tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
332             tp->t_force = 1;
333             tcp_output(tp);
334             tp->t_force = 0;
335         }
336
337         return ret;
338 }
339
340 /*
341  * Send urgent data
342  * There's a lot duplicated code here, but...
343  */
344 int
345 sosendoob(struct socket *so)
346 {
347         struct sbuf *sb = &so->so_rcv;
348         char buff[2048]; /* XXX Shouldn't be sending more oob data than this */
349
350         int n;
351
352         DEBUG_CALL("sosendoob");
353         DEBUG_ARG("so = %p", so);
354         DEBUG_ARG("sb->sb_cc = %d", sb->sb_cc);
355
356         if (so->so_urgc > 2048)
357            so->so_urgc = 2048; /* XXXX */
358
359         if (sb->sb_rptr < sb->sb_wptr) {
360                 /* We can send it directly */
361                 n = slirp_send(so, sb->sb_rptr, so->so_urgc, (MSG_OOB)); /* |MSG_DONTWAIT)); */
362         } else {
363                 /*
364                  * Since there's no sendv or sendtov like writev,
365                  * we must copy all data to a linear buffer then
366                  * send it all
367                  */
368                 uint32_t urgc = so->so_urgc;
369                 int len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
370                 if (len > urgc) {
371                         len = urgc;
372                 }
373                 memcpy(buff, sb->sb_rptr, len);
374                 urgc -= len;
375                 if (urgc) {
376                         n = sb->sb_wptr - sb->sb_data;
377                         if (n > urgc) {
378                                 n = urgc;
379                         }
380                         memcpy((buff + len), sb->sb_data, n);
381                         len += n;
382                 }
383                 n = slirp_send(so, buff, len, (MSG_OOB)); /* |MSG_DONTWAIT)); */
384 #ifdef DEBUG
385                 if (n != len) {
386                         DEBUG_ERROR((dfd, "Didn't send all data urgently XXXXX\n"));
387                 }
388 #endif
389         }
390
391         if (n < 0) {
392                 return n;
393         }
394         so->so_urgc -= n;
395         DEBUG_MISC((dfd, " ---2 sent %d bytes urgent data, %d urgent bytes left\n", n, so->so_urgc));
396
397         sb->sb_cc -= n;
398         sb->sb_rptr += n;
399         if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
400                 sb->sb_rptr -= sb->sb_datalen;
401
402         return n;
403 }
404
405 /*
406  * Write data from so_rcv to so's socket,
407  * updating all sbuf field as necessary
408  */
409 int
410 sowrite(struct socket *so)
411 {
412         int  n,nn;
413         struct sbuf *sb = &so->so_rcv;
414         int len = sb->sb_cc;
415         struct iovec iov[2];
416
417         DEBUG_CALL("sowrite");
418         DEBUG_ARG("so = %p", so);
419
420         if (so->so_urgc) {
421                 uint32_t expected = so->so_urgc;
422                 if (sosendoob(so) < expected) {
423                         /* Treat a short write as a fatal error too,
424                          * rather than continuing on and sending the urgent
425                          * data as if it were non-urgent and leaving the
426                          * so_urgc count wrong.
427                          */
428                         goto err_disconnected;
429                 }
430                 if (sb->sb_cc == 0)
431                         return 0;
432         }
433
434         /*
435          * No need to check if there's something to write,
436          * sowrite wouldn't have been called otherwise
437          */
438
439         iov[0].iov_base = sb->sb_rptr;
440         iov[1].iov_base = NULL;
441         iov[1].iov_len = 0;
442         if (sb->sb_rptr < sb->sb_wptr) {
443                 iov[0].iov_len = sb->sb_wptr - sb->sb_rptr;
444                 /* Should never succeed, but... */
445                 if (iov[0].iov_len > len) iov[0].iov_len = len;
446                 n = 1;
447         } else {
448                 iov[0].iov_len = (sb->sb_data + sb->sb_datalen) - sb->sb_rptr;
449                 if (iov[0].iov_len > len) iov[0].iov_len = len;
450                 len -= iov[0].iov_len;
451                 if (len) {
452                         iov[1].iov_base = sb->sb_data;
453                         iov[1].iov_len = sb->sb_wptr - sb->sb_data;
454                         if (iov[1].iov_len > len) iov[1].iov_len = len;
455                         n = 2;
456                 } else
457                         n = 1;
458         }
459         /* Check if there's urgent data to send, and if so, send it */
460
461 #ifdef HAVE_READV
462         nn = writev(so->s, (const struct iovec *)iov, n);
463
464         DEBUG_MISC((dfd, "  ... wrote nn = %d bytes\n", nn));
465 #else
466         nn = slirp_send(so, iov[0].iov_base, iov[0].iov_len,0);
467 #endif
468         /* This should never happen, but people tell me it does *shrug* */
469         if (nn < 0 && (errno == EAGAIN || errno == EINTR))
470                 return 0;
471
472         if (nn <= 0) {
473                 goto err_disconnected;
474         }
475
476 #ifndef HAVE_READV
477         if (n == 2 && nn == iov[0].iov_len) {
478             int ret;
479             ret = slirp_send(so, iov[1].iov_base, iov[1].iov_len,0);
480             if (ret > 0)
481                 nn += ret;
482         }
483         DEBUG_MISC((dfd, "  ... wrote nn = %d bytes\n", nn));
484 #endif
485
486         /* Update sbuf */
487         sb->sb_cc -= nn;
488         sb->sb_rptr += nn;
489         if (sb->sb_rptr >= (sb->sb_data + sb->sb_datalen))
490                 sb->sb_rptr -= sb->sb_datalen;
491
492         /*
493          * If in DRAIN mode, and there's no more data, set
494          * it CANTSENDMORE
495          */
496         if ((so->so_state & SS_FWDRAIN) && sb->sb_cc == 0)
497                 sofcantsendmore(so);
498
499         return nn;
500
501 err_disconnected:
502         DEBUG_MISC((dfd, " --- sowrite disconnected, so->so_state = %x, errno = %d\n",
503                     so->so_state, errno));
504         sofcantsendmore(so);
505         tcp_sockclosed(sototcpcb(so));
506         return -1;
507 }
508
509 /*
510  * recvfrom() a UDP socket
511  */
512 void
513 sorecvfrom(struct socket *so)
514 {
515         struct sockaddr_storage addr;
516         struct sockaddr_storage saddr, daddr;
517         socklen_t addrlen = sizeof(struct sockaddr_storage);
518
519         DEBUG_CALL("sorecvfrom");
520         DEBUG_ARG("so = %p", so);
521
522         if (so->so_type == IPPROTO_ICMP) {   /* This is a "ping" reply */
523           char buff[256];
524           int len;
525
526           len = recvfrom(so->s, buff, 256, 0,
527                          (struct sockaddr *)&addr, &addrlen);
528           /* XXX Check if reply is "correct"? */
529
530           if(len == -1 || len == 0) {
531             u_char code=ICMP_UNREACH_PORT;
532
533             if(errno == EHOSTUNREACH) code=ICMP_UNREACH_HOST;
534             else if(errno == ENETUNREACH) code=ICMP_UNREACH_NET;
535
536             DEBUG_MISC((dfd," udp icmp rx errno = %d-%s\n",
537                         errno,strerror(errno)));
538             icmp_send_error(so->so_m, ICMP_UNREACH, code, 0, strerror(errno));
539           } else {
540             icmp_reflect(so->so_m);
541             so->so_m = NULL; /* Don't m_free() it again! */
542           }
543           /* No need for this socket anymore, udp_detach it */
544           udp_detach(so);
545         } else {                                /* A "normal" UDP packet */
546           struct mbuf *m;
547           int len;
548 #ifdef _WIN32
549           unsigned long n;
550 #else
551           int n;
552 #endif
553
554           m = m_get(so->slirp);
555           if (!m) {
556               return;
557           }
558           switch (so->so_ffamily) {
559           case AF_INET:
560               m->m_data += IF_MAXLINKHDR + sizeof(struct udpiphdr);
561               break;
562           case AF_INET6:
563               m->m_data += IF_MAXLINKHDR + sizeof(struct ip6)
564                                          + sizeof(struct udphdr);
565               break;
566           default:
567               g_assert_not_reached();
568               break;
569           }
570
571           /*
572            * XXX Shouldn't FIONREAD packets destined for port 53,
573            * but I don't know the max packet size for DNS lookups
574            */
575           len = M_FREEROOM(m);
576           /* if (so->so_fport != htons(53)) { */
577           ioctlsocket(so->s, FIONREAD, &n);
578
579           if (n > len) {
580             n = (m->m_data - m->m_dat) + m->m_len + n + 1;
581             m_inc(m, n);
582             len = M_FREEROOM(m);
583           }
584           /* } */
585
586           m->m_len = recvfrom(so->s, m->m_data, len, 0,
587                               (struct sockaddr *)&addr, &addrlen);
588           DEBUG_MISC((dfd, " did recvfrom %d, errno = %d-%s\n",
589                       m->m_len, errno,strerror(errno)));
590           if(m->m_len<0) {
591             /* Report error as ICMP */
592             switch (so->so_lfamily) {
593             uint8_t code;
594             case AF_INET:
595               code = ICMP_UNREACH_PORT;
596
597               if (errno == EHOSTUNREACH) {
598                 code = ICMP_UNREACH_HOST;
599               } else if (errno == ENETUNREACH) {
600                 code = ICMP_UNREACH_NET;
601               }
602
603               DEBUG_MISC((dfd, " rx error, tx icmp ICMP_UNREACH:%i\n", code));
604               icmp_send_error(so->so_m, ICMP_UNREACH, code, 0, strerror(errno));
605               break;
606             case AF_INET6:
607               code = ICMP6_UNREACH_PORT;
608
609               if (errno == EHOSTUNREACH) {
610                 code = ICMP6_UNREACH_ADDRESS;
611               } else if (errno == ENETUNREACH) {
612                 code = ICMP6_UNREACH_NO_ROUTE;
613               }
614
615               DEBUG_MISC((dfd, " rx error, tx icmp6 ICMP_UNREACH:%i\n", code));
616               icmp6_send_error(so->so_m, ICMP6_UNREACH, code);
617               break;
618             default:
619               g_assert_not_reached();
620               break;
621             }
622             m_free(m);
623           } else {
624           /*
625            * Hack: domain name lookup will be used the most for UDP,
626            * and since they'll only be used once there's no need
627            * for the 4 minute (or whatever) timeout... So we time them
628            * out much quicker (10 seconds  for now...)
629            */
630             if (so->so_expire) {
631               if (so->so_fport == htons(53))
632                 so->so_expire = curtime + SO_EXPIREFAST;
633               else
634                 so->so_expire = curtime + SO_EXPIRE;
635             }
636
637             /*
638              * If this packet was destined for CTL_ADDR,
639              * make it look like that's where it came from
640              */
641             saddr = addr;
642             sotranslate_in(so, &saddr);
643             daddr = so->lhost.ss;
644
645             switch (so->so_ffamily) {
646             case AF_INET:
647                 udp_output(so, m, (struct sockaddr_in *) &saddr,
648                            (struct sockaddr_in *) &daddr,
649                            so->so_iptos);
650                 break;
651             case AF_INET6:
652                 udp6_output(so, m, (struct sockaddr_in6 *) &saddr,
653                             (struct sockaddr_in6 *) &daddr);
654                 break;
655             default:
656                 g_assert_not_reached();
657                 break;
658             }
659           } /* rx error */
660         } /* if ping packet */
661 }
662
663 /*
664  * sendto() a socket
665  */
666 int
667 sosendto(struct socket *so, struct mbuf *m)
668 {
669         int ret;
670         struct sockaddr_storage addr;
671
672         DEBUG_CALL("sosendto");
673         DEBUG_ARG("so = %p", so);
674         DEBUG_ARG("m = %p", m);
675
676         addr = so->fhost.ss;
677         DEBUG_CALL(" sendto()ing)");
678         sotranslate_out(so, &addr);
679
680         /* Don't care what port we get */
681         ret = sendto(so->s, m->m_data, m->m_len, 0,
682                      (struct sockaddr *)&addr, sockaddr_size(&addr));
683         if (ret < 0)
684                 return -1;
685
686         /*
687          * Kill the socket if there's no reply in 4 minutes,
688          * but only if it's an expirable socket
689          */
690         if (so->so_expire)
691                 so->so_expire = curtime + SO_EXPIRE;
692         so->so_state &= SS_PERSISTENT_MASK;
693         so->so_state |= SS_ISFCONNECTED; /* So that it gets select()ed */
694         return 0;
695 }
696
697 /*
698  * Listen for incoming TCP connections
699  */
700 struct socket *
701 tcp_listen(Slirp *slirp, uint32_t haddr, u_int hport, uint32_t laddr,
702            u_int lport, int flags)
703 {
704         struct sockaddr_in addr;
705         struct socket *so;
706         int s, opt = 1;
707         socklen_t addrlen = sizeof(addr);
708         memset(&addr, 0, addrlen);
709
710         DEBUG_CALL("tcp_listen");
711         DEBUG_ARG("haddr = %s", inet_ntoa((struct in_addr){.s_addr = haddr}));
712         DEBUG_ARG("hport = %d", ntohs(hport));
713         DEBUG_ARG("laddr = %s", inet_ntoa((struct in_addr){.s_addr = laddr}));
714         DEBUG_ARG("lport = %d", ntohs(lport));
715         DEBUG_ARG("flags = %x", flags);
716
717         so = socreate(slirp);
718         if (!so) {
719           return NULL;
720         }
721
722         /* Don't tcp_attach... we don't need so_snd nor so_rcv */
723         if ((so->so_tcpcb = tcp_newtcpcb(so)) == NULL) {
724                 free(so);
725                 return NULL;
726         }
727         insque(so, &slirp->tcb);
728
729         /*
730          * SS_FACCEPTONCE sockets must time out.
731          */
732         if (flags & SS_FACCEPTONCE)
733            so->so_tcpcb->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT*2;
734
735         so->so_state &= SS_PERSISTENT_MASK;
736         so->so_state |= (SS_FACCEPTCONN | flags);
737         so->so_lfamily = AF_INET;
738         so->so_lport = lport; /* Kept in network format */
739         so->so_laddr.s_addr = laddr; /* Ditto */
740
741         addr.sin_family = AF_INET;
742         addr.sin_addr.s_addr = haddr;
743         addr.sin_port = hport;
744
745         if (((s = qemu_socket(AF_INET,SOCK_STREAM,0)) < 0) ||
746             (socket_set_fast_reuse(s) < 0) ||
747             (bind(s,(struct sockaddr *)&addr, sizeof(addr)) < 0) ||
748             (listen(s,1) < 0)) {
749                 int tmperrno = errno; /* Don't clobber the real reason we failed */
750
751                 if (s >= 0) {
752                     closesocket(s);
753                 }
754                 sofree(so);
755                 /* Restore the real errno */
756 #ifdef _WIN32
757                 WSASetLastError(tmperrno);
758 #else
759                 errno = tmperrno;
760 #endif
761                 return NULL;
762         }
763         qemu_setsockopt(s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
764         opt = 1;
765         qemu_setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(int));
766
767         getsockname(s,(struct sockaddr *)&addr,&addrlen);
768         so->so_ffamily = AF_INET;
769         so->so_fport = addr.sin_port;
770         if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr)
771            so->so_faddr = slirp->vhost_addr;
772         else
773            so->so_faddr = addr.sin_addr;
774
775         so->s = s;
776         return so;
777 }
778
779 /*
780  * Various session state calls
781  * XXX Should be #define's
782  * The socket state stuff needs work, these often get call 2 or 3
783  * times each when only 1 was needed
784  */
785 void
786 soisfconnecting(struct socket *so)
787 {
788         so->so_state &= ~(SS_NOFDREF|SS_ISFCONNECTED|SS_FCANTRCVMORE|
789                           SS_FCANTSENDMORE|SS_FWDRAIN);
790         so->so_state |= SS_ISFCONNECTING; /* Clobber other states */
791 }
792
793 void
794 soisfconnected(struct socket *so)
795 {
796         so->so_state &= ~(SS_ISFCONNECTING|SS_FWDRAIN|SS_NOFDREF);
797         so->so_state |= SS_ISFCONNECTED; /* Clobber other states */
798 }
799
800 static void
801 sofcantrcvmore(struct socket *so)
802 {
803         if ((so->so_state & SS_NOFDREF) == 0) {
804                 shutdown(so->s,0);
805         }
806         so->so_state &= ~(SS_ISFCONNECTING);
807         if (so->so_state & SS_FCANTSENDMORE) {
808            so->so_state &= SS_PERSISTENT_MASK;
809            so->so_state |= SS_NOFDREF; /* Don't select it */
810         } else {
811            so->so_state |= SS_FCANTRCVMORE;
812         }
813 }
814
815 static void
816 sofcantsendmore(struct socket *so)
817 {
818         if ((so->so_state & SS_NOFDREF) == 0) {
819             shutdown(so->s,1);           /* send FIN to fhost */
820         }
821         so->so_state &= ~(SS_ISFCONNECTING);
822         if (so->so_state & SS_FCANTRCVMORE) {
823            so->so_state &= SS_PERSISTENT_MASK;
824            so->so_state |= SS_NOFDREF; /* as above */
825         } else {
826            so->so_state |= SS_FCANTSENDMORE;
827         }
828 }
829
830 /*
831  * Set write drain mode
832  * Set CANTSENDMORE once all data has been write()n
833  */
834 void
835 sofwdrain(struct socket *so)
836 {
837         if (so->so_rcv.sb_cc)
838                 so->so_state |= SS_FWDRAIN;
839         else
840                 sofcantsendmore(so);
841 }
842
843 /*
844  * Translate addr in host addr when it is a virtual address
845  */
846 void sotranslate_out(struct socket *so, struct sockaddr_storage *addr)
847 {
848     Slirp *slirp = so->slirp;
849     struct sockaddr_in *sin = (struct sockaddr_in *)addr;
850     struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
851
852     switch (addr->ss_family) {
853     case AF_INET:
854         if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) ==
855                 slirp->vnetwork_addr.s_addr) {
856             /* It's an alias */
857             if (so->so_faddr.s_addr == slirp->vnameserver_addr.s_addr) {
858                 if (get_dns_addr(&sin->sin_addr) < 0) {
859                     sin->sin_addr = loopback_addr;
860                 }
861             } else {
862                 sin->sin_addr = loopback_addr;
863             }
864         }
865
866         DEBUG_MISC((dfd, " addr.sin_port=%d, "
867             "addr.sin_addr.s_addr=%.16s\n",
868             ntohs(sin->sin_port), inet_ntoa(sin->sin_addr)));
869         break;
870
871     case AF_INET6:
872         if (in6_equal_net(&so->so_faddr6, &slirp->vprefix_addr6,
873                     slirp->vprefix_len)) {
874             if (in6_equal(&so->so_faddr6, &slirp->vnameserver_addr6)) {
875                 uint32_t scope_id;
876                 if (get_dns6_addr(&sin6->sin6_addr, &scope_id) >= 0) {
877                     sin6->sin6_scope_id = scope_id;
878                 } else {
879                     sin6->sin6_addr = in6addr_loopback;
880                 }
881             } else {
882                 sin6->sin6_addr = in6addr_loopback;
883             }
884         }
885         break;
886
887     default:
888         break;
889     }
890 }
891
892 void sotranslate_in(struct socket *so, struct sockaddr_storage *addr)
893 {
894     Slirp *slirp = so->slirp;
895     struct sockaddr_in *sin = (struct sockaddr_in *)addr;
896     struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
897
898     switch (addr->ss_family) {
899     case AF_INET:
900         if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) ==
901             slirp->vnetwork_addr.s_addr) {
902             uint32_t inv_mask = ~slirp->vnetwork_mask.s_addr;
903
904             if ((so->so_faddr.s_addr & inv_mask) == inv_mask) {
905                 sin->sin_addr = slirp->vhost_addr;
906             } else if (sin->sin_addr.s_addr == loopback_addr.s_addr ||
907                        so->so_faddr.s_addr != slirp->vhost_addr.s_addr) {
908                 sin->sin_addr = so->so_faddr;
909             }
910         }
911         break;
912
913     case AF_INET6:
914         if (in6_equal_net(&so->so_faddr6, &slirp->vprefix_addr6,
915                     slirp->vprefix_len)) {
916             if (in6_equal(&sin6->sin6_addr, &in6addr_loopback)
917                     || !in6_equal(&so->so_faddr6, &slirp->vhost_addr6)) {
918                 sin6->sin6_addr = so->so_faddr6;
919             }
920         }
921         break;
922
923     default:
924         break;
925     }
926 }
927
928 /*
929  * Translate connections from localhost to the real hostname
930  */
931 void sotranslate_accept(struct socket *so)
932 {
933     Slirp *slirp = so->slirp;
934
935     switch (so->so_ffamily) {
936     case AF_INET:
937         if (so->so_faddr.s_addr == INADDR_ANY ||
938             (so->so_faddr.s_addr & loopback_mask) ==
939             (loopback_addr.s_addr & loopback_mask)) {
940            so->so_faddr = slirp->vhost_addr;
941         }
942         break;
943
944    case AF_INET6:
945         if (in6_equal(&so->so_faddr6, &in6addr_any) ||
946                 in6_equal(&so->so_faddr6, &in6addr_loopback)) {
947            so->so_faddr6 = slirp->vhost_addr6;
948         }
949         break;
950
951     default:
952         break;
953     }
954 }
This page took 0.077191 seconds and 4 git commands to generate.