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