2 * Copyright (c) 1995 Danny Gasparovski.
4 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
8 #include "qemu-common.h"
12 #include <sys/filio.h>
15 static void sofcantrcvmore(struct socket
*so
);
16 static void sofcantsendmore(struct socket
*so
);
18 struct socket
*solookup(struct socket
**last
, struct socket
*head
,
19 struct sockaddr_storage
*lhost
, struct sockaddr_storage
*fhost
)
21 struct socket
*so
= *last
;
24 if (so
!= head
&& sockaddr_equal(&(so
->lhost
.ss
), lhost
)
25 && (!fhost
|| sockaddr_equal(&so
->fhost
.ss
, fhost
))) {
29 for (so
= head
->so_next
; so
!= head
; so
= so
->so_next
) {
30 if (sockaddr_equal(&(so
->lhost
.ss
), lhost
)
31 && (!fhost
|| sockaddr_equal(&so
->fhost
.ss
, fhost
))) {
37 return (struct socket
*)NULL
;
41 * Create a new socket, initialise the fields
42 * It is the responsibility of the caller to
43 * insque() it into the correct linked-list
46 socreate(Slirp
*slirp
)
50 so
= (struct socket
*)malloc(sizeof(struct socket
));
52 memset(so
, 0, sizeof(struct socket
));
53 so
->so_state
= SS_NOFDREF
;
62 * remque and free a socket, clobber cache
65 sofree(struct socket
*so
)
67 Slirp
*slirp
= so
->slirp
;
69 if (so
->so_emu
==EMU_RSH
&& so
->extra
) {
73 if (so
== slirp
->tcp_last_so
) {
74 slirp
->tcp_last_so
= &slirp
->tcb
;
75 } else if (so
== slirp
->udp_last_so
) {
76 slirp
->udp_last_so
= &slirp
->udb
;
77 } else if (so
== slirp
->icmp_last_so
) {
78 slirp
->icmp_last_so
= &slirp
->icmp
;
82 if(so
->so_next
&& so
->so_prev
)
83 remque(so
); /* crashes if so is not in a queue */
88 size_t sopreprbuf(struct socket
*so
, struct iovec
*iov
, int *np
)
91 struct sbuf
*sb
= &so
->so_snd
;
92 int len
= sb
->sb_datalen
- sb
->sb_cc
;
93 int mss
= so
->so_tcpcb
->t_maxseg
;
95 DEBUG_CALL("sopreprbuf");
96 DEBUG_ARG("so = %p", so
);
101 iov
[0].iov_base
= sb
->sb_wptr
;
102 iov
[1].iov_base
= NULL
;
104 if (sb
->sb_wptr
< sb
->sb_rptr
) {
105 iov
[0].iov_len
= sb
->sb_rptr
- sb
->sb_wptr
;
106 /* Should never succeed, but... */
107 if (iov
[0].iov_len
> len
)
108 iov
[0].iov_len
= len
;
109 if (iov
[0].iov_len
> mss
)
110 iov
[0].iov_len
-= iov
[0].iov_len
%mss
;
113 iov
[0].iov_len
= (sb
->sb_data
+ sb
->sb_datalen
) - sb
->sb_wptr
;
114 /* Should never succeed, but... */
115 if (iov
[0].iov_len
> len
) iov
[0].iov_len
= len
;
116 len
-= iov
[0].iov_len
;
118 iov
[1].iov_base
= sb
->sb_data
;
119 iov
[1].iov_len
= sb
->sb_rptr
- sb
->sb_data
;
120 if(iov
[1].iov_len
> len
)
121 iov
[1].iov_len
= len
;
122 total
= iov
[0].iov_len
+ iov
[1].iov_len
;
125 if (iov
[1].iov_len
> lss
) {
126 iov
[1].iov_len
-= lss
;
129 lss
-= iov
[1].iov_len
;
130 iov
[0].iov_len
-= lss
;
136 if (iov
[0].iov_len
> mss
)
137 iov
[0].iov_len
-= iov
[0].iov_len
%mss
;
144 return iov
[0].iov_len
+ (n
- 1) * iov
[1].iov_len
;
148 * Read from so's socket into sb_snd, updating all relevant sbuf fields
149 * NOTE: This will only be called if it is select()ed for reading, so
150 * a read() of 0 (or less) means it's disconnected
153 soread(struct socket
*so
)
156 struct sbuf
*sb
= &so
->so_snd
;
159 DEBUG_CALL("soread");
160 DEBUG_ARG("so = %p", so
);
163 * No need to check if there's enough room to read.
164 * soread wouldn't have been called if there weren't
166 sopreprbuf(so
, iov
, &n
);
169 nn
= readv(so
->s
, (struct iovec
*)iov
, n
);
170 DEBUG_MISC((dfd
, " ... read nn = %d bytes\n", nn
));
172 nn
= qemu_recv(so
->s
, iov
[0].iov_base
, iov
[0].iov_len
,0);
175 if (nn
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
178 DEBUG_MISC((dfd
, " --- soread() disconnected, nn = %d, errno = %d-%s\n", nn
, errno
,strerror(errno
)));
180 tcp_sockclosed(sototcpcb(so
));
187 * If there was no error, try and read the second time round
188 * We read again if n = 2 (ie, there's another part of the buffer)
189 * and we read as much as we could in the first read
190 * We don't test for <= 0 this time, because there legitimately
191 * might not be any more data (since the socket is non-blocking),
192 * a close will be detected on next iteration.
193 * A return of -1 wont (shouldn't) happen, since it didn't happen above
195 if (n
== 2 && nn
== iov
[0].iov_len
) {
197 ret
= qemu_recv(so
->s
, iov
[1].iov_base
, iov
[1].iov_len
,0);
202 DEBUG_MISC((dfd
, " ... read nn = %d bytes\n", nn
));
208 if (sb
->sb_wptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
209 sb
->sb_wptr
-= sb
->sb_datalen
;
213 int soreadbuf(struct socket
*so
, const char *buf
, int size
)
215 int n
, nn
, copy
= size
;
216 struct sbuf
*sb
= &so
->so_snd
;
219 DEBUG_CALL("soreadbuf");
220 DEBUG_ARG("so = %p", so
);
223 * No need to check if there's enough room to read.
224 * soread wouldn't have been called if there weren't
226 if (sopreprbuf(so
, iov
, &n
) < size
)
229 nn
= MIN(iov
[0].iov_len
, copy
);
230 memcpy(iov
[0].iov_base
, buf
, nn
);
238 memcpy(iov
[1].iov_base
, buf
, copy
);
244 if (sb
->sb_wptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
245 sb
->sb_wptr
-= sb
->sb_datalen
;
250 tcp_sockclosed(sototcpcb(so
));
251 fprintf(stderr
, "soreadbuf buffer to small");
258 * When the socket is created, we set it SO_OOBINLINE,
259 * so when OOB data arrives, we soread() it and everything
260 * in the send buffer is sent as urgent data
263 sorecvoob(struct socket
*so
)
265 struct tcpcb
*tp
= sototcpcb(so
);
267 DEBUG_CALL("sorecvoob");
268 DEBUG_ARG("so = %p", so
);
271 * We take a guess at how much urgent data has arrived.
272 * In most situations, when urgent data arrives, the next
273 * read() should get all the urgent data. This guess will
274 * be wrong however if more data arrives just after the
275 * urgent data, or the read() doesn't return all the
279 tp
->snd_up
= tp
->snd_una
+ so
->so_snd
.sb_cc
;
287 * There's a lot duplicated code here, but...
290 sosendoob(struct socket
*so
)
292 struct sbuf
*sb
= &so
->so_rcv
;
293 char buff
[2048]; /* XXX Shouldn't be sending more oob data than this */
297 DEBUG_CALL("sosendoob");
298 DEBUG_ARG("so = %p", so
);
299 DEBUG_ARG("sb->sb_cc = %d", sb
->sb_cc
);
301 if (so
->so_urgc
> 2048)
302 so
->so_urgc
= 2048; /* XXXX */
304 if (sb
->sb_rptr
< sb
->sb_wptr
) {
305 /* We can send it directly */
306 n
= slirp_send(so
, sb
->sb_rptr
, so
->so_urgc
, (MSG_OOB
)); /* |MSG_DONTWAIT)); */
309 DEBUG_MISC((dfd
, " --- sent %d bytes urgent data, %d urgent bytes left\n", n
, so
->so_urgc
));
312 * Since there's no sendv or sendtov like writev,
313 * we must copy all data to a linear buffer then
316 len
= (sb
->sb_data
+ sb
->sb_datalen
) - sb
->sb_rptr
;
317 if (len
> so
->so_urgc
) len
= so
->so_urgc
;
318 memcpy(buff
, sb
->sb_rptr
, len
);
321 n
= sb
->sb_wptr
- sb
->sb_data
;
322 if (n
> so
->so_urgc
) n
= so
->so_urgc
;
323 memcpy((buff
+ len
), sb
->sb_data
, n
);
327 n
= slirp_send(so
, buff
, len
, (MSG_OOB
)); /* |MSG_DONTWAIT)); */
330 DEBUG_ERROR((dfd
, "Didn't send all data urgently XXXXX\n"));
332 DEBUG_MISC((dfd
, " ---2 sent %d bytes urgent data, %d urgent bytes left\n", n
, so
->so_urgc
));
337 if (sb
->sb_rptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
338 sb
->sb_rptr
-= sb
->sb_datalen
;
344 * Write data from so_rcv to so's socket,
345 * updating all sbuf field as necessary
348 sowrite(struct socket
*so
)
351 struct sbuf
*sb
= &so
->so_rcv
;
355 DEBUG_CALL("sowrite");
356 DEBUG_ARG("so = %p", so
);
365 * No need to check if there's something to write,
366 * sowrite wouldn't have been called otherwise
369 iov
[0].iov_base
= sb
->sb_rptr
;
370 iov
[1].iov_base
= NULL
;
372 if (sb
->sb_rptr
< sb
->sb_wptr
) {
373 iov
[0].iov_len
= sb
->sb_wptr
- sb
->sb_rptr
;
374 /* Should never succeed, but... */
375 if (iov
[0].iov_len
> len
) iov
[0].iov_len
= len
;
378 iov
[0].iov_len
= (sb
->sb_data
+ sb
->sb_datalen
) - sb
->sb_rptr
;
379 if (iov
[0].iov_len
> len
) iov
[0].iov_len
= len
;
380 len
-= iov
[0].iov_len
;
382 iov
[1].iov_base
= sb
->sb_data
;
383 iov
[1].iov_len
= sb
->sb_wptr
- sb
->sb_data
;
384 if (iov
[1].iov_len
> len
) iov
[1].iov_len
= len
;
389 /* Check if there's urgent data to send, and if so, send it */
392 nn
= writev(so
->s
, (const struct iovec
*)iov
, n
);
394 DEBUG_MISC((dfd
, " ... wrote nn = %d bytes\n", nn
));
396 nn
= slirp_send(so
, iov
[0].iov_base
, iov
[0].iov_len
,0);
398 /* This should never happen, but people tell me it does *shrug* */
399 if (nn
< 0 && (errno
== EAGAIN
|| errno
== EINTR
))
403 DEBUG_MISC((dfd
, " --- sowrite disconnected, so->so_state = %x, errno = %d\n",
404 so
->so_state
, errno
));
406 tcp_sockclosed(sototcpcb(so
));
411 if (n
== 2 && nn
== iov
[0].iov_len
) {
413 ret
= slirp_send(so
, iov
[1].iov_base
, iov
[1].iov_len
,0);
417 DEBUG_MISC((dfd
, " ... wrote nn = %d bytes\n", nn
));
423 if (sb
->sb_rptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
424 sb
->sb_rptr
-= sb
->sb_datalen
;
427 * If in DRAIN mode, and there's no more data, set
430 if ((so
->so_state
& SS_FWDRAIN
) && sb
->sb_cc
== 0)
437 * recvfrom() a UDP socket
440 sorecvfrom(struct socket
*so
)
442 struct sockaddr_storage addr
;
443 struct sockaddr_storage saddr
, daddr
;
444 socklen_t addrlen
= sizeof(struct sockaddr_storage
);
446 DEBUG_CALL("sorecvfrom");
447 DEBUG_ARG("so = %p", so
);
449 if (so
->so_type
== IPPROTO_ICMP
) { /* This is a "ping" reply */
453 len
= recvfrom(so
->s
, buff
, 256, 0,
454 (struct sockaddr
*)&addr
, &addrlen
);
455 /* XXX Check if reply is "correct"? */
457 if(len
== -1 || len
== 0) {
458 u_char code
=ICMP_UNREACH_PORT
;
460 if(errno
== EHOSTUNREACH
) code
=ICMP_UNREACH_HOST
;
461 else if(errno
== ENETUNREACH
) code
=ICMP_UNREACH_NET
;
463 DEBUG_MISC((dfd
," udp icmp rx errno = %d-%s\n",
464 errno
,strerror(errno
)));
465 icmp_error(so
->so_m
, ICMP_UNREACH
,code
, 0,strerror(errno
));
467 icmp_reflect(so
->so_m
);
468 so
->so_m
= NULL
; /* Don't m_free() it again! */
470 /* No need for this socket anymore, udp_detach it */
472 } else { /* A "normal" UDP packet */
481 m
= m_get(so
->slirp
);
485 m
->m_data
+= IF_MAXLINKHDR
;
488 * XXX Shouldn't FIONREAD packets destined for port 53,
489 * but I don't know the max packet size for DNS lookups
492 /* if (so->so_fport != htons(53)) { */
493 ioctlsocket(so
->s
, FIONREAD
, &n
);
496 n
= (m
->m_data
- m
->m_dat
) + m
->m_len
+ n
+ 1;
502 m
->m_len
= recvfrom(so
->s
, m
->m_data
, len
, 0,
503 (struct sockaddr
*)&addr
, &addrlen
);
504 DEBUG_MISC((dfd
, " did recvfrom %d, errno = %d-%s\n",
505 m
->m_len
, errno
,strerror(errno
)));
507 u_char code
=ICMP_UNREACH_PORT
;
509 if(errno
== EHOSTUNREACH
) code
=ICMP_UNREACH_HOST
;
510 else if(errno
== ENETUNREACH
) code
=ICMP_UNREACH_NET
;
512 DEBUG_MISC((dfd
," rx error, tx icmp ICMP_UNREACH:%i\n", code
));
513 icmp_error(so
->so_m
, ICMP_UNREACH
,code
, 0,strerror(errno
));
517 * Hack: domain name lookup will be used the most for UDP,
518 * and since they'll only be used once there's no need
519 * for the 4 minute (or whatever) timeout... So we time them
520 * out much quicker (10 seconds for now...)
523 if (so
->so_fport
== htons(53))
524 so
->so_expire
= curtime
+ SO_EXPIREFAST
;
526 so
->so_expire
= curtime
+ SO_EXPIRE
;
530 * If this packet was destined for CTL_ADDR,
531 * make it look like that's where it came from
534 sotranslate_in(so
, &saddr
);
535 daddr
= so
->lhost
.ss
;
537 switch (so
->so_ffamily
) {
539 udp_output(so
, m
, (struct sockaddr_in
*) &saddr
,
540 (struct sockaddr_in
*) &daddr
,
547 } /* if ping packet */
554 sosendto(struct socket
*so
, struct mbuf
*m
)
557 struct sockaddr_storage addr
;
559 DEBUG_CALL("sosendto");
560 DEBUG_ARG("so = %p", so
);
561 DEBUG_ARG("m = %p", m
);
564 DEBUG_CALL(" sendto()ing)");
565 sotranslate_out(so
, &addr
);
567 /* Don't care what port we get */
568 ret
= sendto(so
->s
, m
->m_data
, m
->m_len
, 0,
569 (struct sockaddr
*)&addr
, sizeof(addr
));
574 * Kill the socket if there's no reply in 4 minutes,
575 * but only if it's an expirable socket
578 so
->so_expire
= curtime
+ SO_EXPIRE
;
579 so
->so_state
&= SS_PERSISTENT_MASK
;
580 so
->so_state
|= SS_ISFCONNECTED
; /* So that it gets select()ed */
585 * Listen for incoming TCP connections
588 tcp_listen(Slirp
*slirp
, uint32_t haddr
, u_int hport
, uint32_t laddr
,
589 u_int lport
, int flags
)
591 struct sockaddr_in addr
;
594 socklen_t addrlen
= sizeof(addr
);
595 memset(&addr
, 0, addrlen
);
597 DEBUG_CALL("tcp_listen");
598 DEBUG_ARG("haddr = %x", haddr
);
599 DEBUG_ARG("hport = %d", hport
);
600 DEBUG_ARG("laddr = %x", laddr
);
601 DEBUG_ARG("lport = %d", lport
);
602 DEBUG_ARG("flags = %x", flags
);
604 so
= socreate(slirp
);
609 /* Don't tcp_attach... we don't need so_snd nor so_rcv */
610 if ((so
->so_tcpcb
= tcp_newtcpcb(so
)) == NULL
) {
614 insque(so
, &slirp
->tcb
);
617 * SS_FACCEPTONCE sockets must time out.
619 if (flags
& SS_FACCEPTONCE
)
620 so
->so_tcpcb
->t_timer
[TCPT_KEEP
] = TCPTV_KEEP_INIT
*2;
622 so
->so_state
&= SS_PERSISTENT_MASK
;
623 so
->so_state
|= (SS_FACCEPTCONN
| flags
);
624 so
->so_lfamily
= AF_INET
;
625 so
->so_lport
= lport
; /* Kept in network format */
626 so
->so_laddr
.s_addr
= laddr
; /* Ditto */
628 addr
.sin_family
= AF_INET
;
629 addr
.sin_addr
.s_addr
= haddr
;
630 addr
.sin_port
= hport
;
632 if (((s
= qemu_socket(AF_INET
,SOCK_STREAM
,0)) < 0) ||
633 (socket_set_fast_reuse(s
) < 0) ||
634 (bind(s
,(struct sockaddr
*)&addr
, sizeof(addr
)) < 0) ||
636 int tmperrno
= errno
; /* Don't clobber the real reason we failed */
640 /* Restore the real errno */
642 WSASetLastError(tmperrno
);
648 qemu_setsockopt(s
, SOL_SOCKET
, SO_OOBINLINE
, &opt
, sizeof(int));
650 getsockname(s
,(struct sockaddr
*)&addr
,&addrlen
);
651 so
->so_ffamily
= AF_INET
;
652 so
->so_fport
= addr
.sin_port
;
653 if (addr
.sin_addr
.s_addr
== 0 || addr
.sin_addr
.s_addr
== loopback_addr
.s_addr
)
654 so
->so_faddr
= slirp
->vhost_addr
;
656 so
->so_faddr
= addr
.sin_addr
;
663 * Various session state calls
664 * XXX Should be #define's
665 * The socket state stuff needs work, these often get call 2 or 3
666 * times each when only 1 was needed
669 soisfconnecting(struct socket
*so
)
671 so
->so_state
&= ~(SS_NOFDREF
|SS_ISFCONNECTED
|SS_FCANTRCVMORE
|
672 SS_FCANTSENDMORE
|SS_FWDRAIN
);
673 so
->so_state
|= SS_ISFCONNECTING
; /* Clobber other states */
677 soisfconnected(struct socket
*so
)
679 so
->so_state
&= ~(SS_ISFCONNECTING
|SS_FWDRAIN
|SS_NOFDREF
);
680 so
->so_state
|= SS_ISFCONNECTED
; /* Clobber other states */
684 sofcantrcvmore(struct socket
*so
)
686 if ((so
->so_state
& SS_NOFDREF
) == 0) {
689 so
->so_state
&= ~(SS_ISFCONNECTING
);
690 if (so
->so_state
& SS_FCANTSENDMORE
) {
691 so
->so_state
&= SS_PERSISTENT_MASK
;
692 so
->so_state
|= SS_NOFDREF
; /* Don't select it */
694 so
->so_state
|= SS_FCANTRCVMORE
;
699 sofcantsendmore(struct socket
*so
)
701 if ((so
->so_state
& SS_NOFDREF
) == 0) {
702 shutdown(so
->s
,1); /* send FIN to fhost */
704 so
->so_state
&= ~(SS_ISFCONNECTING
);
705 if (so
->so_state
& SS_FCANTRCVMORE
) {
706 so
->so_state
&= SS_PERSISTENT_MASK
;
707 so
->so_state
|= SS_NOFDREF
; /* as above */
709 so
->so_state
|= SS_FCANTSENDMORE
;
714 * Set write drain mode
715 * Set CANTSENDMORE once all data has been write()n
718 sofwdrain(struct socket
*so
)
720 if (so
->so_rcv
.sb_cc
)
721 so
->so_state
|= SS_FWDRAIN
;
727 * Translate addr in host addr when it is a virtual address
729 void sotranslate_out(struct socket
*so
, struct sockaddr_storage
*addr
)
731 Slirp
*slirp
= so
->slirp
;
732 struct sockaddr_in
*sin
= (struct sockaddr_in
*)addr
;
734 switch (addr
->ss_family
) {
736 if ((so
->so_faddr
.s_addr
& slirp
->vnetwork_mask
.s_addr
) ==
737 slirp
->vnetwork_addr
.s_addr
) {
739 if (so
->so_faddr
.s_addr
== slirp
->vnameserver_addr
.s_addr
) {
740 if (get_dns_addr(&sin
->sin_addr
) < 0) {
741 sin
->sin_addr
= loopback_addr
;
744 sin
->sin_addr
= loopback_addr
;
748 DEBUG_MISC((dfd
, " addr.sin_port=%d, "
749 "addr.sin_addr.s_addr=%.16s\n",
750 ntohs(sin
->sin_port
), inet_ntoa(sin
->sin_addr
)));
758 void sotranslate_in(struct socket
*so
, struct sockaddr_storage
*addr
)
760 Slirp
*slirp
= so
->slirp
;
761 struct sockaddr_in
*sin
= (struct sockaddr_in
*)addr
;
763 switch (addr
->ss_family
) {
765 if ((so
->so_faddr
.s_addr
& slirp
->vnetwork_mask
.s_addr
) ==
766 slirp
->vnetwork_addr
.s_addr
) {
767 uint32_t inv_mask
= ~slirp
->vnetwork_mask
.s_addr
;
769 if ((so
->so_faddr
.s_addr
& inv_mask
) == inv_mask
) {
770 sin
->sin_addr
= slirp
->vhost_addr
;
771 } else if (sin
->sin_addr
.s_addr
== loopback_addr
.s_addr
||
772 so
->so_faddr
.s_addr
!= slirp
->vhost_addr
.s_addr
) {
773 sin
->sin_addr
= so
->so_faddr
;
784 * Translate connections from localhost to the real hostname
786 void sotranslate_accept(struct socket
*so
)
788 Slirp
*slirp
= so
->slirp
;
790 switch (so
->so_ffamily
) {
792 if (so
->so_faddr
.s_addr
== INADDR_ANY
||
793 (so
->so_faddr
.s_addr
& loopback_mask
) ==
794 (loopback_addr
.s_addr
& loopback_mask
)) {
795 so
->so_faddr
= slirp
->vhost_addr
;