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