2 * Copyright (c) 1995 Danny Gasparovski.
4 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
8 #include "qemu/osdep.h"
9 #include "qemu-common.h"
13 #include <sys/filio.h>
16 static void sofcantrcvmore(struct socket
*so
);
17 static void sofcantsendmore(struct socket
*so
);
19 struct socket
*solookup(struct socket
**last
, struct socket
*head
,
20 struct sockaddr_storage
*lhost
, struct sockaddr_storage
*fhost
)
22 struct socket
*so
= *last
;
25 if (so
!= head
&& sockaddr_equal(&(so
->lhost
.ss
), lhost
)
26 && (!fhost
|| sockaddr_equal(&so
->fhost
.ss
, fhost
))) {
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
))) {
38 return (struct socket
*)NULL
;
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
47 socreate(Slirp
*slirp
)
51 so
= (struct socket
*)malloc(sizeof(struct socket
));
53 memset(so
, 0, sizeof(struct socket
));
54 so
->so_state
= SS_NOFDREF
;
63 * remque and free a socket, clobber cache
66 sofree(struct socket
*so
)
68 Slirp
*slirp
= so
->slirp
;
70 if (so
->so_emu
==EMU_RSH
&& so
->extra
) {
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
;
83 if(so
->so_next
&& so
->so_prev
)
84 remque(so
); /* crashes if so is not in a queue */
89 size_t sopreprbuf(struct socket
*so
, struct iovec
*iov
, int *np
)
92 struct sbuf
*sb
= &so
->so_snd
;
93 int len
= sb
->sb_datalen
- sb
->sb_cc
;
94 int mss
= so
->so_tcpcb
->t_maxseg
;
96 DEBUG_CALL("sopreprbuf");
97 DEBUG_ARG("so = %p", so
);
102 iov
[0].iov_base
= sb
->sb_wptr
;
103 iov
[1].iov_base
= NULL
;
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
;
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
;
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
;
126 if (iov
[1].iov_len
> lss
) {
127 iov
[1].iov_len
-= lss
;
130 lss
-= iov
[1].iov_len
;
131 iov
[0].iov_len
-= lss
;
137 if (iov
[0].iov_len
> mss
)
138 iov
[0].iov_len
-= iov
[0].iov_len
%mss
;
145 return iov
[0].iov_len
+ (n
- 1) * iov
[1].iov_len
;
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
154 soread(struct socket
*so
)
157 struct sbuf
*sb
= &so
->so_snd
;
160 DEBUG_CALL("soread");
161 DEBUG_ARG("so = %p", so
);
164 * No need to check if there's enough room to read.
165 * soread wouldn't have been called if there weren't
167 sopreprbuf(so
, iov
, &n
);
170 nn
= readv(so
->s
, (struct iovec
*)iov
, n
);
171 DEBUG_MISC((dfd
, " ... read nn = %d bytes\n", nn
));
173 nn
= qemu_recv(so
->s
, iov
[0].iov_base
, iov
[0].iov_len
,0);
176 if (nn
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
179 DEBUG_MISC((dfd
, " --- soread() disconnected, nn = %d, errno = %d-%s\n", nn
, errno
,strerror(errno
)));
181 tcp_sockclosed(sototcpcb(so
));
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
196 if (n
== 2 && nn
== iov
[0].iov_len
) {
198 ret
= qemu_recv(so
->s
, iov
[1].iov_base
, iov
[1].iov_len
,0);
203 DEBUG_MISC((dfd
, " ... read nn = %d bytes\n", nn
));
209 if (sb
->sb_wptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
210 sb
->sb_wptr
-= sb
->sb_datalen
;
214 int soreadbuf(struct socket
*so
, const char *buf
, int size
)
216 int n
, nn
, copy
= size
;
217 struct sbuf
*sb
= &so
->so_snd
;
220 DEBUG_CALL("soreadbuf");
221 DEBUG_ARG("so = %p", so
);
224 * No need to check if there's enough room to read.
225 * soread wouldn't have been called if there weren't
227 if (sopreprbuf(so
, iov
, &n
) < size
)
230 nn
= MIN(iov
[0].iov_len
, copy
);
231 memcpy(iov
[0].iov_base
, buf
, nn
);
239 memcpy(iov
[1].iov_base
, buf
, copy
);
245 if (sb
->sb_wptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
246 sb
->sb_wptr
-= sb
->sb_datalen
;
251 tcp_sockclosed(sototcpcb(so
));
252 fprintf(stderr
, "soreadbuf buffer to small");
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
264 sorecvoob(struct socket
*so
)
266 struct tcpcb
*tp
= sototcpcb(so
);
268 DEBUG_CALL("sorecvoob");
269 DEBUG_ARG("so = %p", so
);
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
280 tp
->snd_up
= tp
->snd_una
+ so
->so_snd
.sb_cc
;
288 * There's a lot duplicated code here, but...
291 sosendoob(struct socket
*so
)
293 struct sbuf
*sb
= &so
->so_rcv
;
294 char buff
[2048]; /* XXX Shouldn't be sending more oob data than this */
298 DEBUG_CALL("sosendoob");
299 DEBUG_ARG("so = %p", so
);
300 DEBUG_ARG("sb->sb_cc = %d", sb
->sb_cc
);
302 if (so
->so_urgc
> 2048)
303 so
->so_urgc
= 2048; /* XXXX */
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)); */
310 DEBUG_MISC((dfd
, " --- sent %d bytes urgent data, %d urgent bytes left\n", n
, so
->so_urgc
));
313 * Since there's no sendv or sendtov like writev,
314 * we must copy all data to a linear buffer then
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
);
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
);
328 n
= slirp_send(so
, buff
, len
, (MSG_OOB
)); /* |MSG_DONTWAIT)); */
331 DEBUG_ERROR((dfd
, "Didn't send all data urgently XXXXX\n"));
333 DEBUG_MISC((dfd
, " ---2 sent %d bytes urgent data, %d urgent bytes left\n", n
, so
->so_urgc
));
338 if (sb
->sb_rptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
339 sb
->sb_rptr
-= sb
->sb_datalen
;
345 * Write data from so_rcv to so's socket,
346 * updating all sbuf field as necessary
349 sowrite(struct socket
*so
)
352 struct sbuf
*sb
= &so
->so_rcv
;
356 DEBUG_CALL("sowrite");
357 DEBUG_ARG("so = %p", so
);
366 * No need to check if there's something to write,
367 * sowrite wouldn't have been called otherwise
370 iov
[0].iov_base
= sb
->sb_rptr
;
371 iov
[1].iov_base
= NULL
;
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
;
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
;
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
;
390 /* Check if there's urgent data to send, and if so, send it */
393 nn
= writev(so
->s
, (const struct iovec
*)iov
, n
);
395 DEBUG_MISC((dfd
, " ... wrote nn = %d bytes\n", nn
));
397 nn
= slirp_send(so
, iov
[0].iov_base
, iov
[0].iov_len
,0);
399 /* This should never happen, but people tell me it does *shrug* */
400 if (nn
< 0 && (errno
== EAGAIN
|| errno
== EINTR
))
404 DEBUG_MISC((dfd
, " --- sowrite disconnected, so->so_state = %x, errno = %d\n",
405 so
->so_state
, errno
));
407 tcp_sockclosed(sototcpcb(so
));
412 if (n
== 2 && nn
== iov
[0].iov_len
) {
414 ret
= slirp_send(so
, iov
[1].iov_base
, iov
[1].iov_len
,0);
418 DEBUG_MISC((dfd
, " ... wrote nn = %d bytes\n", nn
));
424 if (sb
->sb_rptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
425 sb
->sb_rptr
-= sb
->sb_datalen
;
428 * If in DRAIN mode, and there's no more data, set
431 if ((so
->so_state
& SS_FWDRAIN
) && sb
->sb_cc
== 0)
438 * recvfrom() a UDP socket
441 sorecvfrom(struct socket
*so
)
443 struct sockaddr_storage addr
;
444 struct sockaddr_storage saddr
, daddr
;
445 socklen_t addrlen
= sizeof(struct sockaddr_storage
);
447 DEBUG_CALL("sorecvfrom");
448 DEBUG_ARG("so = %p", so
);
450 if (so
->so_type
== IPPROTO_ICMP
) { /* This is a "ping" reply */
454 len
= recvfrom(so
->s
, buff
, 256, 0,
455 (struct sockaddr
*)&addr
, &addrlen
);
456 /* XXX Check if reply is "correct"? */
458 if(len
== -1 || len
== 0) {
459 u_char code
=ICMP_UNREACH_PORT
;
461 if(errno
== EHOSTUNREACH
) code
=ICMP_UNREACH_HOST
;
462 else if(errno
== ENETUNREACH
) code
=ICMP_UNREACH_NET
;
464 DEBUG_MISC((dfd
," udp icmp rx errno = %d-%s\n",
465 errno
,strerror(errno
)));
466 icmp_error(so
->so_m
, ICMP_UNREACH
,code
, 0,strerror(errno
));
468 icmp_reflect(so
->so_m
);
469 so
->so_m
= NULL
; /* Don't m_free() it again! */
471 /* No need for this socket anymore, udp_detach it */
473 } else { /* A "normal" UDP packet */
482 m
= m_get(so
->slirp
);
486 m
->m_data
+= IF_MAXLINKHDR
;
489 * XXX Shouldn't FIONREAD packets destined for port 53,
490 * but I don't know the max packet size for DNS lookups
493 /* if (so->so_fport != htons(53)) { */
494 ioctlsocket(so
->s
, FIONREAD
, &n
);
497 n
= (m
->m_data
- m
->m_dat
) + m
->m_len
+ n
+ 1;
503 m
->m_len
= recvfrom(so
->s
, m
->m_data
, len
, 0,
504 (struct sockaddr
*)&addr
, &addrlen
);
505 DEBUG_MISC((dfd
, " did recvfrom %d, errno = %d-%s\n",
506 m
->m_len
, errno
,strerror(errno
)));
508 u_char code
=ICMP_UNREACH_PORT
;
510 if(errno
== EHOSTUNREACH
) code
=ICMP_UNREACH_HOST
;
511 else if(errno
== ENETUNREACH
) code
=ICMP_UNREACH_NET
;
513 DEBUG_MISC((dfd
," rx error, tx icmp ICMP_UNREACH:%i\n", code
));
514 icmp_error(so
->so_m
, ICMP_UNREACH
,code
, 0,strerror(errno
));
518 * Hack: domain name lookup will be used the most for UDP,
519 * and since they'll only be used once there's no need
520 * for the 4 minute (or whatever) timeout... So we time them
521 * out much quicker (10 seconds for now...)
524 if (so
->so_fport
== htons(53))
525 so
->so_expire
= curtime
+ SO_EXPIREFAST
;
527 so
->so_expire
= curtime
+ SO_EXPIRE
;
531 * If this packet was destined for CTL_ADDR,
532 * make it look like that's where it came from
535 sotranslate_in(so
, &saddr
);
536 daddr
= so
->lhost
.ss
;
538 switch (so
->so_ffamily
) {
540 udp_output(so
, m
, (struct sockaddr_in
*) &saddr
,
541 (struct sockaddr_in
*) &daddr
,
548 } /* if ping packet */
555 sosendto(struct socket
*so
, struct mbuf
*m
)
558 struct sockaddr_storage addr
;
560 DEBUG_CALL("sosendto");
561 DEBUG_ARG("so = %p", so
);
562 DEBUG_ARG("m = %p", m
);
565 DEBUG_CALL(" sendto()ing)");
566 sotranslate_out(so
, &addr
);
568 /* Don't care what port we get */
569 ret
= sendto(so
->s
, m
->m_data
, m
->m_len
, 0,
570 (struct sockaddr
*)&addr
, sizeof(addr
));
575 * Kill the socket if there's no reply in 4 minutes,
576 * but only if it's an expirable socket
579 so
->so_expire
= curtime
+ SO_EXPIRE
;
580 so
->so_state
&= SS_PERSISTENT_MASK
;
581 so
->so_state
|= SS_ISFCONNECTED
; /* So that it gets select()ed */
586 * Listen for incoming TCP connections
589 tcp_listen(Slirp
*slirp
, uint32_t haddr
, u_int hport
, uint32_t laddr
,
590 u_int lport
, int flags
)
592 struct sockaddr_in addr
;
595 socklen_t addrlen
= sizeof(addr
);
596 memset(&addr
, 0, addrlen
);
598 DEBUG_CALL("tcp_listen");
599 DEBUG_ARG("haddr = %x", haddr
);
600 DEBUG_ARG("hport = %d", hport
);
601 DEBUG_ARG("laddr = %x", laddr
);
602 DEBUG_ARG("lport = %d", lport
);
603 DEBUG_ARG("flags = %x", flags
);
605 so
= socreate(slirp
);
610 /* Don't tcp_attach... we don't need so_snd nor so_rcv */
611 if ((so
->so_tcpcb
= tcp_newtcpcb(so
)) == NULL
) {
615 insque(so
, &slirp
->tcb
);
618 * SS_FACCEPTONCE sockets must time out.
620 if (flags
& SS_FACCEPTONCE
)
621 so
->so_tcpcb
->t_timer
[TCPT_KEEP
] = TCPTV_KEEP_INIT
*2;
623 so
->so_state
&= SS_PERSISTENT_MASK
;
624 so
->so_state
|= (SS_FACCEPTCONN
| flags
);
625 so
->so_lfamily
= AF_INET
;
626 so
->so_lport
= lport
; /* Kept in network format */
627 so
->so_laddr
.s_addr
= laddr
; /* Ditto */
629 addr
.sin_family
= AF_INET
;
630 addr
.sin_addr
.s_addr
= haddr
;
631 addr
.sin_port
= hport
;
633 if (((s
= qemu_socket(AF_INET
,SOCK_STREAM
,0)) < 0) ||
634 (socket_set_fast_reuse(s
) < 0) ||
635 (bind(s
,(struct sockaddr
*)&addr
, sizeof(addr
)) < 0) ||
637 int tmperrno
= errno
; /* Don't clobber the real reason we failed */
641 /* Restore the real errno */
643 WSASetLastError(tmperrno
);
649 qemu_setsockopt(s
, SOL_SOCKET
, SO_OOBINLINE
, &opt
, sizeof(int));
651 getsockname(s
,(struct sockaddr
*)&addr
,&addrlen
);
652 so
->so_ffamily
= AF_INET
;
653 so
->so_fport
= addr
.sin_port
;
654 if (addr
.sin_addr
.s_addr
== 0 || addr
.sin_addr
.s_addr
== loopback_addr
.s_addr
)
655 so
->so_faddr
= slirp
->vhost_addr
;
657 so
->so_faddr
= addr
.sin_addr
;
664 * Various session state calls
665 * XXX Should be #define's
666 * The socket state stuff needs work, these often get call 2 or 3
667 * times each when only 1 was needed
670 soisfconnecting(struct socket
*so
)
672 so
->so_state
&= ~(SS_NOFDREF
|SS_ISFCONNECTED
|SS_FCANTRCVMORE
|
673 SS_FCANTSENDMORE
|SS_FWDRAIN
);
674 so
->so_state
|= SS_ISFCONNECTING
; /* Clobber other states */
678 soisfconnected(struct socket
*so
)
680 so
->so_state
&= ~(SS_ISFCONNECTING
|SS_FWDRAIN
|SS_NOFDREF
);
681 so
->so_state
|= SS_ISFCONNECTED
; /* Clobber other states */
685 sofcantrcvmore(struct socket
*so
)
687 if ((so
->so_state
& SS_NOFDREF
) == 0) {
690 so
->so_state
&= ~(SS_ISFCONNECTING
);
691 if (so
->so_state
& SS_FCANTSENDMORE
) {
692 so
->so_state
&= SS_PERSISTENT_MASK
;
693 so
->so_state
|= SS_NOFDREF
; /* Don't select it */
695 so
->so_state
|= SS_FCANTRCVMORE
;
700 sofcantsendmore(struct socket
*so
)
702 if ((so
->so_state
& SS_NOFDREF
) == 0) {
703 shutdown(so
->s
,1); /* send FIN to fhost */
705 so
->so_state
&= ~(SS_ISFCONNECTING
);
706 if (so
->so_state
& SS_FCANTRCVMORE
) {
707 so
->so_state
&= SS_PERSISTENT_MASK
;
708 so
->so_state
|= SS_NOFDREF
; /* as above */
710 so
->so_state
|= SS_FCANTSENDMORE
;
715 * Set write drain mode
716 * Set CANTSENDMORE once all data has been write()n
719 sofwdrain(struct socket
*so
)
721 if (so
->so_rcv
.sb_cc
)
722 so
->so_state
|= SS_FWDRAIN
;
728 * Translate addr in host addr when it is a virtual address
730 void sotranslate_out(struct socket
*so
, struct sockaddr_storage
*addr
)
732 Slirp
*slirp
= so
->slirp
;
733 struct sockaddr_in
*sin
= (struct sockaddr_in
*)addr
;
735 switch (addr
->ss_family
) {
737 if ((so
->so_faddr
.s_addr
& slirp
->vnetwork_mask
.s_addr
) ==
738 slirp
->vnetwork_addr
.s_addr
) {
740 if (so
->so_faddr
.s_addr
== slirp
->vnameserver_addr
.s_addr
) {
741 if (get_dns_addr(&sin
->sin_addr
) < 0) {
742 sin
->sin_addr
= loopback_addr
;
745 sin
->sin_addr
= loopback_addr
;
749 DEBUG_MISC((dfd
, " addr.sin_port=%d, "
750 "addr.sin_addr.s_addr=%.16s\n",
751 ntohs(sin
->sin_port
), inet_ntoa(sin
->sin_addr
)));
759 void sotranslate_in(struct socket
*so
, struct sockaddr_storage
*addr
)
761 Slirp
*slirp
= so
->slirp
;
762 struct sockaddr_in
*sin
= (struct sockaddr_in
*)addr
;
764 switch (addr
->ss_family
) {
766 if ((so
->so_faddr
.s_addr
& slirp
->vnetwork_mask
.s_addr
) ==
767 slirp
->vnetwork_addr
.s_addr
) {
768 uint32_t inv_mask
= ~slirp
->vnetwork_mask
.s_addr
;
770 if ((so
->so_faddr
.s_addr
& inv_mask
) == inv_mask
) {
771 sin
->sin_addr
= slirp
->vhost_addr
;
772 } else if (sin
->sin_addr
.s_addr
== loopback_addr
.s_addr
||
773 so
->so_faddr
.s_addr
!= slirp
->vhost_addr
.s_addr
) {
774 sin
->sin_addr
= so
->so_faddr
;
785 * Translate connections from localhost to the real hostname
787 void sotranslate_accept(struct socket
*so
)
789 Slirp
*slirp
= so
->slirp
;
791 switch (so
->so_ffamily
) {
793 if (so
->so_faddr
.s_addr
== INADDR_ANY
||
794 (so
->so_faddr
.s_addr
& loopback_mask
) ==
795 (loopback_addr
.s_addr
& loopback_mask
)) {
796 so
->so_faddr
= slirp
->vhost_addr
;