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