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
);
27 solookup(struct socket
*head
, struct in_addr laddr
, u_int lport
,
28 struct in_addr faddr
, u_int fport
)
32 for (so
= head
->so_next
; so
!= head
; so
= so
->so_next
) {
33 if (so
->so_lport
== lport
&&
34 so
->so_laddr
.s_addr
== laddr
.s_addr
&&
35 so
->so_faddr
.s_addr
== faddr
.s_addr
&&
36 so
->so_fport
== fport
)
41 return (struct socket
*)NULL
;
47 * Create a new socket, initialise the fields
48 * It is the responsibility of the caller to
49 * insque() it into the correct linked-list
56 so
= (struct socket
*)malloc(sizeof(struct socket
));
58 memset(so
, 0, sizeof(struct socket
));
59 so
->so_state
= SS_NOFDREF
;
66 * remque and free a socket, clobber cache
69 sofree(struct socket
*so
)
71 if (so
->so_emu
==EMU_RSH
&& so
->extra
) {
75 if (so
== tcp_last_so
)
77 else if (so
== udp_last_so
)
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 = %lx", (long )so
);
98 len
= sb
->sb_datalen
- sb
->sb_cc
;
103 iov
[0].iov_base
= sb
->sb_wptr
;
104 iov
[1].iov_base
= NULL
;
106 if (sb
->sb_wptr
< sb
->sb_rptr
) {
107 iov
[0].iov_len
= sb
->sb_rptr
- sb
->sb_wptr
;
108 /* Should never succeed, but... */
109 if (iov
[0].iov_len
> len
)
110 iov
[0].iov_len
= len
;
111 if (iov
[0].iov_len
> mss
)
112 iov
[0].iov_len
-= iov
[0].iov_len
%mss
;
115 iov
[0].iov_len
= (sb
->sb_data
+ sb
->sb_datalen
) - sb
->sb_wptr
;
116 /* Should never succeed, but... */
117 if (iov
[0].iov_len
> len
) iov
[0].iov_len
= len
;
118 len
-= iov
[0].iov_len
;
120 iov
[1].iov_base
= sb
->sb_data
;
121 iov
[1].iov_len
= sb
->sb_rptr
- sb
->sb_data
;
122 if(iov
[1].iov_len
> len
)
123 iov
[1].iov_len
= len
;
124 total
= iov
[0].iov_len
+ iov
[1].iov_len
;
127 if (iov
[1].iov_len
> lss
) {
128 iov
[1].iov_len
-= lss
;
131 lss
-= iov
[1].iov_len
;
132 iov
[0].iov_len
-= lss
;
138 if (iov
[0].iov_len
> mss
)
139 iov
[0].iov_len
-= iov
[0].iov_len
%mss
;
146 return iov
[0].iov_len
+ (n
- 1) * iov
[1].iov_len
;
150 * Read from so's socket into sb_snd, updating all relevant sbuf fields
151 * NOTE: This will only be called if it is select()ed for reading, so
152 * a read() of 0 (or less) means it's disconnected
155 soread(struct socket
*so
)
158 struct sbuf
*sb
= &so
->so_snd
;
161 DEBUG_CALL("soread");
162 DEBUG_ARG("so = %lx", (long )so
);
165 * No need to check if there's enough room to read.
166 * soread wouldn't have been called if there weren't
168 sopreprbuf(so
, iov
, &n
);
171 nn
= readv(so
->s
, (struct iovec
*)iov
, n
);
172 DEBUG_MISC((dfd
, " ... read nn = %d bytes\n", nn
));
174 nn
= recv(so
->s
, iov
[0].iov_base
, iov
[0].iov_len
,0);
177 if (nn
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
180 DEBUG_MISC((dfd
, " --- soread() disconnected, nn = %d, errno = %d-%s\n", nn
, errno
,strerror(errno
)));
182 tcp_sockclosed(sototcpcb(so
));
189 * If there was no error, try and read the second time round
190 * We read again if n = 2 (ie, there's another part of the buffer)
191 * and we read as much as we could in the first read
192 * We don't test for <= 0 this time, because there legitimately
193 * might not be any more data (since the socket is non-blocking),
194 * a close will be detected on next iteration.
195 * A return of -1 wont (shouldn't) happen, since it didn't happen above
197 if (n
== 2 && nn
== iov
[0].iov_len
) {
199 ret
= recv(so
->s
, iov
[1].iov_base
, iov
[1].iov_len
,0);
204 DEBUG_MISC((dfd
, " ... read nn = %d bytes\n", nn
));
210 if (sb
->sb_wptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
211 sb
->sb_wptr
-= sb
->sb_datalen
;
215 int soreadbuf(struct socket
*so
, const char *buf
, int size
)
217 int n
, nn
, copy
= size
;
218 struct sbuf
*sb
= &so
->so_snd
;
221 DEBUG_CALL("soreadbuf");
222 DEBUG_ARG("so = %lx", (long )so
);
225 * No need to check if there's enough room to read.
226 * soread wouldn't have been called if there weren't
228 if (sopreprbuf(so
, iov
, &n
) < size
)
231 nn
= MIN(iov
[0].iov_len
, copy
);
232 memcpy(iov
[0].iov_base
, buf
, nn
);
240 memcpy(iov
[1].iov_base
, buf
, copy
);
246 if (sb
->sb_wptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
247 sb
->sb_wptr
-= sb
->sb_datalen
;
252 tcp_sockclosed(sototcpcb(so
));
253 fprintf(stderr
, "soreadbuf buffer to small");
260 * When the socket is created, we set it SO_OOBINLINE,
261 * so when OOB data arrives, we soread() it and everything
262 * in the send buffer is sent as urgent data
265 sorecvoob(struct socket
*so
)
267 struct tcpcb
*tp
= sototcpcb(so
);
269 DEBUG_CALL("sorecvoob");
270 DEBUG_ARG("so = %lx", (long)so
);
273 * We take a guess at how much urgent data has arrived.
274 * In most situations, when urgent data arrives, the next
275 * read() should get all the urgent data. This guess will
276 * be wrong however if more data arrives just after the
277 * urgent data, or the read() doesn't return all the
281 tp
->snd_up
= tp
->snd_una
+ so
->so_snd
.sb_cc
;
289 * There's a lot duplicated code here, but...
292 sosendoob(struct socket
*so
)
294 struct sbuf
*sb
= &so
->so_rcv
;
295 char buff
[2048]; /* XXX Shouldn't be sending more oob data than this */
299 DEBUG_CALL("sosendoob");
300 DEBUG_ARG("so = %lx", (long)so
);
301 DEBUG_ARG("sb->sb_cc = %d", sb
->sb_cc
);
303 if (so
->so_urgc
> 2048)
304 so
->so_urgc
= 2048; /* XXXX */
306 if (sb
->sb_rptr
< sb
->sb_wptr
) {
307 /* We can send it directly */
308 n
= slirp_send(so
, sb
->sb_rptr
, so
->so_urgc
, (MSG_OOB
)); /* |MSG_DONTWAIT)); */
311 DEBUG_MISC((dfd
, " --- sent %d bytes urgent data, %d urgent bytes left\n", n
, so
->so_urgc
));
314 * Since there's no sendv or sendtov like writev,
315 * we must copy all data to a linear buffer then
318 len
= (sb
->sb_data
+ sb
->sb_datalen
) - sb
->sb_rptr
;
319 if (len
> so
->so_urgc
) len
= so
->so_urgc
;
320 memcpy(buff
, sb
->sb_rptr
, len
);
323 n
= sb
->sb_wptr
- sb
->sb_data
;
324 if (n
> so
->so_urgc
) n
= so
->so_urgc
;
325 memcpy((buff
+ len
), sb
->sb_data
, n
);
329 n
= slirp_send(so
, buff
, len
, (MSG_OOB
)); /* |MSG_DONTWAIT)); */
332 DEBUG_ERROR((dfd
, "Didn't send all data urgently XXXXX\n"));
334 DEBUG_MISC((dfd
, " ---2 sent %d bytes urgent data, %d urgent bytes left\n", n
, so
->so_urgc
));
339 if (sb
->sb_rptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
340 sb
->sb_rptr
-= sb
->sb_datalen
;
346 * Write data from so_rcv to so's socket,
347 * updating all sbuf field as necessary
350 sowrite(struct socket
*so
)
353 struct sbuf
*sb
= &so
->so_rcv
;
357 DEBUG_CALL("sowrite");
358 DEBUG_ARG("so = %lx", (long)so
);
367 * No need to check if there's something to write,
368 * sowrite wouldn't have been called otherwise
373 iov
[0].iov_base
= sb
->sb_rptr
;
374 iov
[1].iov_base
= NULL
;
376 if (sb
->sb_rptr
< sb
->sb_wptr
) {
377 iov
[0].iov_len
= sb
->sb_wptr
- sb
->sb_rptr
;
378 /* Should never succeed, but... */
379 if (iov
[0].iov_len
> len
) iov
[0].iov_len
= len
;
382 iov
[0].iov_len
= (sb
->sb_data
+ sb
->sb_datalen
) - sb
->sb_rptr
;
383 if (iov
[0].iov_len
> len
) iov
[0].iov_len
= len
;
384 len
-= iov
[0].iov_len
;
386 iov
[1].iov_base
= sb
->sb_data
;
387 iov
[1].iov_len
= sb
->sb_wptr
- sb
->sb_data
;
388 if (iov
[1].iov_len
> len
) iov
[1].iov_len
= len
;
393 /* Check if there's urgent data to send, and if so, send it */
396 nn
= writev(so
->s
, (const struct iovec
*)iov
, n
);
398 DEBUG_MISC((dfd
, " ... wrote nn = %d bytes\n", nn
));
400 nn
= slirp_send(so
, iov
[0].iov_base
, iov
[0].iov_len
,0);
402 /* This should never happen, but people tell me it does *shrug* */
403 if (nn
< 0 && (errno
== EAGAIN
|| errno
== EINTR
))
407 DEBUG_MISC((dfd
, " --- sowrite disconnected, so->so_state = %x, errno = %d\n",
408 so
->so_state
, errno
));
410 tcp_sockclosed(sototcpcb(so
));
415 if (n
== 2 && nn
== iov
[0].iov_len
) {
417 ret
= slirp_send(so
, iov
[1].iov_base
, iov
[1].iov_len
,0);
421 DEBUG_MISC((dfd
, " ... wrote nn = %d bytes\n", nn
));
427 if (sb
->sb_rptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
428 sb
->sb_rptr
-= sb
->sb_datalen
;
431 * If in DRAIN mode, and there's no more data, set
434 if ((so
->so_state
& SS_FWDRAIN
) && sb
->sb_cc
== 0)
441 * recvfrom() a UDP socket
444 sorecvfrom(struct socket
*so
)
446 struct sockaddr_in addr
;
447 socklen_t addrlen
= sizeof(struct sockaddr_in
);
449 DEBUG_CALL("sorecvfrom");
450 DEBUG_ARG("so = %lx", (long)so
);
452 if (so
->so_type
== IPPROTO_ICMP
) { /* This is a "ping" reply */
456 len
= recvfrom(so
->s
, buff
, 256, 0,
457 (struct sockaddr
*)&addr
, &addrlen
);
458 /* XXX Check if reply is "correct"? */
460 if(len
== -1 || len
== 0) {
461 u_char code
=ICMP_UNREACH_PORT
;
463 if(errno
== EHOSTUNREACH
) code
=ICMP_UNREACH_HOST
;
464 else if(errno
== ENETUNREACH
) code
=ICMP_UNREACH_NET
;
466 DEBUG_MISC((dfd
," udp icmp rx errno = %d-%s\n",
467 errno
,strerror(errno
)));
468 icmp_error(so
->so_m
, ICMP_UNREACH
,code
, 0,strerror(errno
));
470 icmp_reflect(so
->so_m
);
471 so
->so_m
= NULL
; /* Don't m_free() it again! */
473 /* No need for this socket anymore, udp_detach it */
475 } else { /* A "normal" UDP packet */
484 if (!(m
= m_get())) return;
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
;
529 /* if (m->m_len == len) {
530 * m_inc(m, MINCSIZE);
536 * If this packet was destined for CTL_ADDR,
537 * make it look like that's where it came from, done by udp_output
539 udp_output(so
, m
, &addr
);
541 } /* if ping packet */
548 sosendto(struct socket
*so
, struct mbuf
*m
)
551 struct sockaddr_in addr
;
553 DEBUG_CALL("sosendto");
554 DEBUG_ARG("so = %lx", (long)so
);
555 DEBUG_ARG("m = %lx", (long)m
);
557 addr
.sin_family
= AF_INET
;
558 if ((so
->so_faddr
.s_addr
& htonl(0xffffff00)) == special_addr
.s_addr
) {
560 switch(ntohl(so
->so_faddr
.s_addr
) & 0xff) {
562 addr
.sin_addr
= dns_addr
;
566 addr
.sin_addr
= loopback_addr
;
570 addr
.sin_addr
= so
->so_faddr
;
571 addr
.sin_port
= so
->so_fport
;
573 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
)));
575 /* Don't care what port we get */
576 ret
= sendto(so
->s
, m
->m_data
, m
->m_len
, 0,
577 (struct sockaddr
*)&addr
, sizeof (struct sockaddr
));
582 * Kill the socket if there's no reply in 4 minutes,
583 * but only if it's an expirable socket
586 so
->so_expire
= curtime
+ SO_EXPIRE
;
587 so
->so_state
= SS_ISFCONNECTED
; /* So that it gets select()ed */
592 * XXX This should really be tcp_listen
595 solisten(u_int port
, u_int32_t laddr
, u_int lport
, int flags
)
597 struct sockaddr_in addr
;
600 socklen_t addrlen
= sizeof(addr
);
602 DEBUG_CALL("solisten");
603 DEBUG_ARG("port = %d", port
);
604 DEBUG_ARG("laddr = %x", laddr
);
605 DEBUG_ARG("lport = %d", lport
);
606 DEBUG_ARG("flags = %x", flags
);
608 if ((so
= socreate()) == NULL
) {
609 /* free(so); Not sofree() ??? free(NULL) == NOP */
613 /* Don't tcp_attach... we don't need so_snd nor so_rcv */
614 if ((so
->so_tcpcb
= tcp_newtcpcb(so
)) == NULL
) {
621 * SS_FACCEPTONCE sockets must time out.
623 if (flags
& SS_FACCEPTONCE
)
624 so
->so_tcpcb
->t_timer
[TCPT_KEEP
] = TCPTV_KEEP_INIT
*2;
626 so
->so_state
= (SS_FACCEPTCONN
|flags
);
627 so
->so_lport
= lport
; /* Kept in network format */
628 so
->so_laddr
.s_addr
= laddr
; /* Ditto */
630 addr
.sin_family
= AF_INET
;
631 addr
.sin_addr
.s_addr
= INADDR_ANY
;
632 addr
.sin_port
= port
;
634 if (((s
= socket(AF_INET
,SOCK_STREAM
,0)) < 0) ||
635 (setsockopt(s
,SOL_SOCKET
,SO_REUSEADDR
,(char *)&opt
,sizeof(int)) < 0) ||
636 (bind(s
,(struct sockaddr
*)&addr
, sizeof(addr
)) < 0) ||
638 int tmperrno
= errno
; /* Don't clobber the real reason we failed */
642 /* Restore the real errno */
644 WSASetLastError(tmperrno
);
650 setsockopt(s
,SOL_SOCKET
,SO_OOBINLINE
,(char *)&opt
,sizeof(int));
652 getsockname(s
,(struct sockaddr
*)&addr
,&addrlen
);
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
= alias_addr
;
657 so
->so_faddr
= addr
.sin_addr
;
665 * Data is available in so_rcv
666 * Just write() the data to the socket
674 /* FD_CLR(so->s,&writefds); */
678 * Data has been freed in so_snd
679 * We have room for a read() if we want to
680 * For now, don't read, it'll be done in the main loop
691 * Various session state calls
692 * XXX Should be #define's
693 * The socket state stuff needs work, these often get call 2 or 3
694 * times each when only 1 was needed
697 soisfconnecting(struct socket
*so
)
699 so
->so_state
&= ~(SS_NOFDREF
|SS_ISFCONNECTED
|SS_FCANTRCVMORE
|
700 SS_FCANTSENDMORE
|SS_FWDRAIN
);
701 so
->so_state
|= SS_ISFCONNECTING
; /* Clobber other states */
705 soisfconnected(struct socket
*so
)
707 so
->so_state
&= ~(SS_ISFCONNECTING
|SS_FWDRAIN
|SS_NOFDREF
);
708 so
->so_state
|= SS_ISFCONNECTED
; /* Clobber other states */
712 sofcantrcvmore(struct socket
*so
)
714 if ((so
->so_state
& SS_NOFDREF
) == 0) {
716 if(global_writefds
) {
717 FD_CLR(so
->s
,global_writefds
);
720 so
->so_state
&= ~(SS_ISFCONNECTING
);
721 if (so
->so_state
& SS_FCANTSENDMORE
)
722 so
->so_state
= SS_NOFDREF
; /* Don't select it */ /* XXX close() here as well? */
724 so
->so_state
|= SS_FCANTRCVMORE
;
728 sofcantsendmore(struct socket
*so
)
730 if ((so
->so_state
& SS_NOFDREF
) == 0) {
731 shutdown(so
->s
,1); /* send FIN to fhost */
732 if (global_readfds
) {
733 FD_CLR(so
->s
,global_readfds
);
736 FD_CLR(so
->s
,global_xfds
);
739 so
->so_state
&= ~(SS_ISFCONNECTING
);
740 if (so
->so_state
& SS_FCANTRCVMORE
)
741 so
->so_state
= SS_NOFDREF
; /* as above */
743 so
->so_state
|= SS_FCANTSENDMORE
;
747 soisfdisconnected(struct socket
*so
)
749 /* so->so_state &= ~(SS_ISFCONNECTING|SS_ISFCONNECTED); */
751 /* so->so_state = SS_ISFDISCONNECTED; */
753 * XXX Do nothing ... ?
758 * Set write drain mode
759 * Set CANTSENDMORE once all data has been write()n
762 sofwdrain(struct socket
*so
)
764 if (so
->so_rcv
.sb_cc
)
765 so
->so_state
|= SS_FWDRAIN
;