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