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
);
19 solookup(struct socket
*head
, struct in_addr laddr
, u_int lport
,
20 struct in_addr faddr
, u_int fport
)
24 for (so
= head
->so_next
; so
!= head
; so
= so
->so_next
) {
25 if (so
->so_lport
== lport
&&
26 so
->so_laddr
.s_addr
== laddr
.s_addr
&&
27 so
->so_faddr
.s_addr
== faddr
.s_addr
&&
28 so
->so_fport
== fport
)
33 return (struct socket
*)NULL
;
39 * Create a new socket, initialise the fields
40 * It is the responsibility of the caller to
41 * insque() it into the correct linked-list
44 socreate(Slirp
*slirp
)
48 so
= (struct socket
*)malloc(sizeof(struct socket
));
50 memset(so
, 0, sizeof(struct socket
));
51 so
->so_state
= SS_NOFDREF
;
60 * remque and free a socket, clobber cache
63 sofree(struct socket
*so
)
65 Slirp
*slirp
= so
->slirp
;
67 if (so
->so_emu
==EMU_RSH
&& so
->extra
) {
71 if (so
== slirp
->tcp_last_so
) {
72 slirp
->tcp_last_so
= &slirp
->tcb
;
73 } else if (so
== slirp
->udp_last_so
) {
74 slirp
->udp_last_so
= &slirp
->udb
;
75 } else if (so
== slirp
->icmp_last_so
) {
76 slirp
->icmp_last_so
= &slirp
->icmp
;
80 if(so
->so_next
&& so
->so_prev
)
81 remque(so
); /* crashes if so is not in a queue */
86 size_t sopreprbuf(struct socket
*so
, struct iovec
*iov
, int *np
)
89 struct sbuf
*sb
= &so
->so_snd
;
90 int len
= sb
->sb_datalen
- sb
->sb_cc
;
91 int mss
= so
->so_tcpcb
->t_maxseg
;
93 DEBUG_CALL("sopreprbuf");
94 DEBUG_ARG("so = %lx", (long )so
);
99 iov
[0].iov_base
= sb
->sb_wptr
;
100 iov
[1].iov_base
= NULL
;
102 if (sb
->sb_wptr
< sb
->sb_rptr
) {
103 iov
[0].iov_len
= sb
->sb_rptr
- sb
->sb_wptr
;
104 /* Should never succeed, but... */
105 if (iov
[0].iov_len
> len
)
106 iov
[0].iov_len
= len
;
107 if (iov
[0].iov_len
> mss
)
108 iov
[0].iov_len
-= iov
[0].iov_len
%mss
;
111 iov
[0].iov_len
= (sb
->sb_data
+ sb
->sb_datalen
) - sb
->sb_wptr
;
112 /* Should never succeed, but... */
113 if (iov
[0].iov_len
> len
) iov
[0].iov_len
= len
;
114 len
-= iov
[0].iov_len
;
116 iov
[1].iov_base
= sb
->sb_data
;
117 iov
[1].iov_len
= sb
->sb_rptr
- sb
->sb_data
;
118 if(iov
[1].iov_len
> len
)
119 iov
[1].iov_len
= len
;
120 total
= iov
[0].iov_len
+ iov
[1].iov_len
;
123 if (iov
[1].iov_len
> lss
) {
124 iov
[1].iov_len
-= lss
;
127 lss
-= iov
[1].iov_len
;
128 iov
[0].iov_len
-= lss
;
134 if (iov
[0].iov_len
> mss
)
135 iov
[0].iov_len
-= iov
[0].iov_len
%mss
;
142 return iov
[0].iov_len
+ (n
- 1) * iov
[1].iov_len
;
146 * Read from so's socket into sb_snd, updating all relevant sbuf fields
147 * NOTE: This will only be called if it is select()ed for reading, so
148 * a read() of 0 (or less) means it's disconnected
151 soread(struct socket
*so
)
154 struct sbuf
*sb
= &so
->so_snd
;
157 DEBUG_CALL("soread");
158 DEBUG_ARG("so = %lx", (long )so
);
161 * No need to check if there's enough room to read.
162 * soread wouldn't have been called if there weren't
164 sopreprbuf(so
, iov
, &n
);
167 nn
= readv(so
->s
, (struct iovec
*)iov
, n
);
168 DEBUG_MISC((dfd
, " ... read nn = %d bytes\n", nn
));
170 nn
= qemu_recv(so
->s
, iov
[0].iov_base
, iov
[0].iov_len
,0);
173 if (nn
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
176 DEBUG_MISC((dfd
, " --- soread() disconnected, nn = %d, errno = %d-%s\n", nn
, errno
,strerror(errno
)));
178 tcp_sockclosed(sototcpcb(so
));
185 * If there was no error, try and read the second time round
186 * We read again if n = 2 (ie, there's another part of the buffer)
187 * and we read as much as we could in the first read
188 * We don't test for <= 0 this time, because there legitimately
189 * might not be any more data (since the socket is non-blocking),
190 * a close will be detected on next iteration.
191 * A return of -1 wont (shouldn't) happen, since it didn't happen above
193 if (n
== 2 && nn
== iov
[0].iov_len
) {
195 ret
= qemu_recv(so
->s
, iov
[1].iov_base
, iov
[1].iov_len
,0);
200 DEBUG_MISC((dfd
, " ... read nn = %d bytes\n", nn
));
206 if (sb
->sb_wptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
207 sb
->sb_wptr
-= sb
->sb_datalen
;
211 int soreadbuf(struct socket
*so
, const char *buf
, int size
)
213 int n
, nn
, copy
= size
;
214 struct sbuf
*sb
= &so
->so_snd
;
217 DEBUG_CALL("soreadbuf");
218 DEBUG_ARG("so = %lx", (long )so
);
221 * No need to check if there's enough room to read.
222 * soread wouldn't have been called if there weren't
224 if (sopreprbuf(so
, iov
, &n
) < size
)
227 nn
= MIN(iov
[0].iov_len
, copy
);
228 memcpy(iov
[0].iov_base
, buf
, nn
);
236 memcpy(iov
[1].iov_base
, buf
, copy
);
242 if (sb
->sb_wptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
243 sb
->sb_wptr
-= sb
->sb_datalen
;
248 tcp_sockclosed(sototcpcb(so
));
249 fprintf(stderr
, "soreadbuf buffer to small");
256 * When the socket is created, we set it SO_OOBINLINE,
257 * so when OOB data arrives, we soread() it and everything
258 * in the send buffer is sent as urgent data
261 sorecvoob(struct socket
*so
)
263 struct tcpcb
*tp
= sototcpcb(so
);
265 DEBUG_CALL("sorecvoob");
266 DEBUG_ARG("so = %lx", (long)so
);
269 * We take a guess at how much urgent data has arrived.
270 * In most situations, when urgent data arrives, the next
271 * read() should get all the urgent data. This guess will
272 * be wrong however if more data arrives just after the
273 * urgent data, or the read() doesn't return all the
277 tp
->snd_up
= tp
->snd_una
+ so
->so_snd
.sb_cc
;
285 * There's a lot duplicated code here, but...
288 sosendoob(struct socket
*so
)
290 struct sbuf
*sb
= &so
->so_rcv
;
291 char buff
[2048]; /* XXX Shouldn't be sending more oob data than this */
295 DEBUG_CALL("sosendoob");
296 DEBUG_ARG("so = %lx", (long)so
);
297 DEBUG_ARG("sb->sb_cc = %d", sb
->sb_cc
);
299 if (so
->so_urgc
> 2048)
300 so
->so_urgc
= 2048; /* XXXX */
302 if (sb
->sb_rptr
< sb
->sb_wptr
) {
303 /* We can send it directly */
304 n
= slirp_send(so
, sb
->sb_rptr
, so
->so_urgc
, (MSG_OOB
)); /* |MSG_DONTWAIT)); */
307 DEBUG_MISC((dfd
, " --- sent %d bytes urgent data, %d urgent bytes left\n", n
, so
->so_urgc
));
310 * Since there's no sendv or sendtov like writev,
311 * we must copy all data to a linear buffer then
314 len
= (sb
->sb_data
+ sb
->sb_datalen
) - sb
->sb_rptr
;
315 if (len
> so
->so_urgc
) len
= so
->so_urgc
;
316 memcpy(buff
, sb
->sb_rptr
, len
);
319 n
= sb
->sb_wptr
- sb
->sb_data
;
320 if (n
> so
->so_urgc
) n
= so
->so_urgc
;
321 memcpy((buff
+ len
), sb
->sb_data
, n
);
325 n
= slirp_send(so
, buff
, len
, (MSG_OOB
)); /* |MSG_DONTWAIT)); */
328 DEBUG_ERROR((dfd
, "Didn't send all data urgently XXXXX\n"));
330 DEBUG_MISC((dfd
, " ---2 sent %d bytes urgent data, %d urgent bytes left\n", n
, so
->so_urgc
));
335 if (sb
->sb_rptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
336 sb
->sb_rptr
-= sb
->sb_datalen
;
342 * Write data from so_rcv to so's socket,
343 * updating all sbuf field as necessary
346 sowrite(struct socket
*so
)
349 struct sbuf
*sb
= &so
->so_rcv
;
353 DEBUG_CALL("sowrite");
354 DEBUG_ARG("so = %lx", (long)so
);
363 * No need to check if there's something to write,
364 * sowrite wouldn't have been called otherwise
367 iov
[0].iov_base
= sb
->sb_rptr
;
368 iov
[1].iov_base
= NULL
;
370 if (sb
->sb_rptr
< sb
->sb_wptr
) {
371 iov
[0].iov_len
= sb
->sb_wptr
- sb
->sb_rptr
;
372 /* Should never succeed, but... */
373 if (iov
[0].iov_len
> len
) iov
[0].iov_len
= len
;
376 iov
[0].iov_len
= (sb
->sb_data
+ sb
->sb_datalen
) - sb
->sb_rptr
;
377 if (iov
[0].iov_len
> len
) iov
[0].iov_len
= len
;
378 len
-= iov
[0].iov_len
;
380 iov
[1].iov_base
= sb
->sb_data
;
381 iov
[1].iov_len
= sb
->sb_wptr
- sb
->sb_data
;
382 if (iov
[1].iov_len
> len
) iov
[1].iov_len
= len
;
387 /* Check if there's urgent data to send, and if so, send it */
390 nn
= writev(so
->s
, (const struct iovec
*)iov
, n
);
392 DEBUG_MISC((dfd
, " ... wrote nn = %d bytes\n", nn
));
394 nn
= slirp_send(so
, iov
[0].iov_base
, iov
[0].iov_len
,0);
396 /* This should never happen, but people tell me it does *shrug* */
397 if (nn
< 0 && (errno
== EAGAIN
|| errno
== EINTR
))
401 DEBUG_MISC((dfd
, " --- sowrite disconnected, so->so_state = %x, errno = %d\n",
402 so
->so_state
, errno
));
404 tcp_sockclosed(sototcpcb(so
));
409 if (n
== 2 && nn
== iov
[0].iov_len
) {
411 ret
= slirp_send(so
, iov
[1].iov_base
, iov
[1].iov_len
,0);
415 DEBUG_MISC((dfd
, " ... wrote nn = %d bytes\n", nn
));
421 if (sb
->sb_rptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
422 sb
->sb_rptr
-= sb
->sb_datalen
;
425 * If in DRAIN mode, and there's no more data, set
428 if ((so
->so_state
& SS_FWDRAIN
) && sb
->sb_cc
== 0)
435 * recvfrom() a UDP socket
438 sorecvfrom(struct socket
*so
)
440 struct sockaddr_in addr
;
441 socklen_t addrlen
= sizeof(struct sockaddr_in
);
443 DEBUG_CALL("sorecvfrom");
444 DEBUG_ARG("so = %lx", (long)so
);
446 if (so
->so_type
== IPPROTO_ICMP
) { /* This is a "ping" reply */
450 len
= recvfrom(so
->s
, buff
, 256, 0,
451 (struct sockaddr
*)&addr
, &addrlen
);
452 /* XXX Check if reply is "correct"? */
454 if(len
== -1 || len
== 0) {
455 u_char code
=ICMP_UNREACH_PORT
;
457 if(errno
== EHOSTUNREACH
) code
=ICMP_UNREACH_HOST
;
458 else if(errno
== ENETUNREACH
) code
=ICMP_UNREACH_NET
;
460 DEBUG_MISC((dfd
," udp icmp rx errno = %d-%s\n",
461 errno
,strerror(errno
)));
462 icmp_error(so
->so_m
, ICMP_UNREACH
,code
, 0,strerror(errno
));
464 icmp_reflect(so
->so_m
);
465 so
->so_m
= NULL
; /* Don't m_free() it again! */
467 /* No need for this socket anymore, udp_detach it */
469 } else { /* A "normal" UDP packet */
478 m
= m_get(so
->slirp
);
482 m
->m_data
+= IF_MAXLINKHDR
;
485 * XXX Shouldn't FIONREAD packets destined for port 53,
486 * but I don't know the max packet size for DNS lookups
489 /* if (so->so_fport != htons(53)) { */
490 ioctlsocket(so
->s
, FIONREAD
, &n
);
493 n
= (m
->m_data
- m
->m_dat
) + m
->m_len
+ n
+ 1;
499 m
->m_len
= recvfrom(so
->s
, m
->m_data
, len
, 0,
500 (struct sockaddr
*)&addr
, &addrlen
);
501 DEBUG_MISC((dfd
, " did recvfrom %d, errno = %d-%s\n",
502 m
->m_len
, errno
,strerror(errno
)));
504 u_char code
=ICMP_UNREACH_PORT
;
506 if(errno
== EHOSTUNREACH
) code
=ICMP_UNREACH_HOST
;
507 else if(errno
== ENETUNREACH
) code
=ICMP_UNREACH_NET
;
509 DEBUG_MISC((dfd
," rx error, tx icmp ICMP_UNREACH:%i\n", code
));
510 icmp_error(so
->so_m
, ICMP_UNREACH
,code
, 0,strerror(errno
));
514 * Hack: domain name lookup will be used the most for UDP,
515 * and since they'll only be used once there's no need
516 * for the 4 minute (or whatever) timeout... So we time them
517 * out much quicker (10 seconds for now...)
520 if (so
->so_fport
== htons(53))
521 so
->so_expire
= curtime
+ SO_EXPIREFAST
;
523 so
->so_expire
= curtime
+ SO_EXPIRE
;
527 * If this packet was destined for CTL_ADDR,
528 * make it look like that's where it came from, done by udp_output
530 udp_output(so
, m
, &addr
);
532 } /* if ping packet */
539 sosendto(struct socket
*so
, struct mbuf
*m
)
541 Slirp
*slirp
= so
->slirp
;
543 struct sockaddr_in addr
;
545 DEBUG_CALL("sosendto");
546 DEBUG_ARG("so = %lx", (long)so
);
547 DEBUG_ARG("m = %lx", (long)m
);
549 addr
.sin_family
= AF_INET
;
550 if ((so
->so_faddr
.s_addr
& slirp
->vnetwork_mask
.s_addr
) ==
551 slirp
->vnetwork_addr
.s_addr
) {
553 if (so
->so_faddr
.s_addr
== slirp
->vnameserver_addr
.s_addr
) {
554 if (get_dns_addr(&addr
.sin_addr
) < 0)
555 addr
.sin_addr
= loopback_addr
;
557 addr
.sin_addr
= loopback_addr
;
560 addr
.sin_addr
= so
->so_faddr
;
561 addr
.sin_port
= so
->so_fport
;
563 DEBUG_MISC((dfd
, " sendto()ing, addr.sin_port=%d, addr.sin_addr.s_addr=%.16s\n", ntohs(addr
.sin_port
), inet_ntoa(addr
.sin_addr
)));
565 /* Don't care what port we get */
566 ret
= sendto(so
->s
, m
->m_data
, m
->m_len
, 0,
567 (struct sockaddr
*)&addr
, sizeof (struct sockaddr
));
572 * Kill the socket if there's no reply in 4 minutes,
573 * but only if it's an expirable socket
576 so
->so_expire
= curtime
+ SO_EXPIRE
;
577 so
->so_state
&= SS_PERSISTENT_MASK
;
578 so
->so_state
|= SS_ISFCONNECTED
; /* So that it gets select()ed */
583 * Listen for incoming TCP connections
586 tcp_listen(Slirp
*slirp
, uint32_t haddr
, u_int hport
, uint32_t laddr
,
587 u_int lport
, int flags
)
589 struct sockaddr_in addr
;
592 socklen_t addrlen
= sizeof(addr
);
593 memset(&addr
, 0, addrlen
);
595 DEBUG_CALL("tcp_listen");
596 DEBUG_ARG("haddr = %x", haddr
);
597 DEBUG_ARG("hport = %d", hport
);
598 DEBUG_ARG("laddr = %x", laddr
);
599 DEBUG_ARG("lport = %d", lport
);
600 DEBUG_ARG("flags = %x", flags
);
602 so
= socreate(slirp
);
607 /* Don't tcp_attach... we don't need so_snd nor so_rcv */
608 if ((so
->so_tcpcb
= tcp_newtcpcb(so
)) == NULL
) {
612 insque(so
, &slirp
->tcb
);
615 * SS_FACCEPTONCE sockets must time out.
617 if (flags
& SS_FACCEPTONCE
)
618 so
->so_tcpcb
->t_timer
[TCPT_KEEP
] = TCPTV_KEEP_INIT
*2;
620 so
->so_state
&= SS_PERSISTENT_MASK
;
621 so
->so_state
|= (SS_FACCEPTCONN
| flags
);
622 so
->so_lport
= lport
; /* Kept in network format */
623 so
->so_laddr
.s_addr
= laddr
; /* Ditto */
625 addr
.sin_family
= AF_INET
;
626 addr
.sin_addr
.s_addr
= haddr
;
627 addr
.sin_port
= hport
;
629 if (((s
= qemu_socket(AF_INET
,SOCK_STREAM
,0)) < 0) ||
630 (qemu_setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, &opt
, sizeof(int)) < 0) ||
631 (bind(s
,(struct sockaddr
*)&addr
, sizeof(addr
)) < 0) ||
633 int tmperrno
= errno
; /* Don't clobber the real reason we failed */
637 /* Restore the real errno */
639 WSASetLastError(tmperrno
);
645 qemu_setsockopt(s
, SOL_SOCKET
, SO_OOBINLINE
, &opt
, sizeof(int));
647 getsockname(s
,(struct sockaddr
*)&addr
,&addrlen
);
648 so
->so_fport
= addr
.sin_port
;
649 if (addr
.sin_addr
.s_addr
== 0 || addr
.sin_addr
.s_addr
== loopback_addr
.s_addr
)
650 so
->so_faddr
= slirp
->vhost_addr
;
652 so
->so_faddr
= addr
.sin_addr
;
659 * Various session state calls
660 * XXX Should be #define's
661 * The socket state stuff needs work, these often get call 2 or 3
662 * times each when only 1 was needed
665 soisfconnecting(struct socket
*so
)
667 so
->so_state
&= ~(SS_NOFDREF
|SS_ISFCONNECTED
|SS_FCANTRCVMORE
|
668 SS_FCANTSENDMORE
|SS_FWDRAIN
);
669 so
->so_state
|= SS_ISFCONNECTING
; /* Clobber other states */
673 soisfconnected(struct socket
*so
)
675 so
->so_state
&= ~(SS_ISFCONNECTING
|SS_FWDRAIN
|SS_NOFDREF
);
676 so
->so_state
|= SS_ISFCONNECTED
; /* Clobber other states */
680 sofcantrcvmore(struct socket
*so
)
682 if ((so
->so_state
& SS_NOFDREF
) == 0) {
685 so
->so_state
&= ~(SS_ISFCONNECTING
);
686 if (so
->so_state
& SS_FCANTSENDMORE
) {
687 so
->so_state
&= SS_PERSISTENT_MASK
;
688 so
->so_state
|= SS_NOFDREF
; /* Don't select it */
690 so
->so_state
|= SS_FCANTRCVMORE
;
695 sofcantsendmore(struct socket
*so
)
697 if ((so
->so_state
& SS_NOFDREF
) == 0) {
698 shutdown(so
->s
,1); /* send FIN to fhost */
700 so
->so_state
&= ~(SS_ISFCONNECTING
);
701 if (so
->so_state
& SS_FCANTRCVMORE
) {
702 so
->so_state
&= SS_PERSISTENT_MASK
;
703 so
->so_state
|= SS_NOFDREF
; /* as above */
705 so
->so_state
|= SS_FCANTSENDMORE
;
710 * Set write drain mode
711 * Set CANTSENDMORE once all data has been write()n
714 sofwdrain(struct socket
*so
)
716 if (so
->so_rcv
.sb_cc
)
717 so
->so_state
|= SS_FWDRAIN
;