]>
git.proxmox.com Git - mirror_qemu.git/blob - slirp/socket.c
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
48 so
= (struct socket
*)malloc(sizeof(struct socket
));
50 memset(so
, 0, sizeof(struct socket
));
51 so
->so_state
= SS_NOFDREF
;
58 * remque and free a socket, clobber cache
61 sofree(struct socket
*so
)
63 if (so
->so_emu
==EMU_RSH
&& so
->extra
) {
67 if (so
== tcp_last_so
)
69 else if (so
== udp_last_so
)
74 if(so
->so_next
&& so
->so_prev
)
75 remque(so
); /* crashes if so is not in a queue */
80 size_t sopreprbuf(struct socket
*so
, struct iovec
*iov
, int *np
)
83 struct sbuf
*sb
= &so
->so_snd
;
84 int len
= sb
->sb_datalen
- sb
->sb_cc
;
85 int mss
= so
->so_tcpcb
->t_maxseg
;
87 DEBUG_CALL("sopreprbuf");
88 DEBUG_ARG("so = %lx", (long )so
);
90 len
= sb
->sb_datalen
- sb
->sb_cc
;
95 iov
[0].iov_base
= sb
->sb_wptr
;
96 iov
[1].iov_base
= NULL
;
98 if (sb
->sb_wptr
< sb
->sb_rptr
) {
99 iov
[0].iov_len
= sb
->sb_rptr
- sb
->sb_wptr
;
100 /* Should never succeed, but... */
101 if (iov
[0].iov_len
> len
)
102 iov
[0].iov_len
= len
;
103 if (iov
[0].iov_len
> mss
)
104 iov
[0].iov_len
-= iov
[0].iov_len
%mss
;
107 iov
[0].iov_len
= (sb
->sb_data
+ sb
->sb_datalen
) - sb
->sb_wptr
;
108 /* Should never succeed, but... */
109 if (iov
[0].iov_len
> len
) iov
[0].iov_len
= len
;
110 len
-= iov
[0].iov_len
;
112 iov
[1].iov_base
= sb
->sb_data
;
113 iov
[1].iov_len
= sb
->sb_rptr
- sb
->sb_data
;
114 if(iov
[1].iov_len
> len
)
115 iov
[1].iov_len
= len
;
116 total
= iov
[0].iov_len
+ iov
[1].iov_len
;
119 if (iov
[1].iov_len
> lss
) {
120 iov
[1].iov_len
-= lss
;
123 lss
-= iov
[1].iov_len
;
124 iov
[0].iov_len
-= lss
;
130 if (iov
[0].iov_len
> mss
)
131 iov
[0].iov_len
-= iov
[0].iov_len
%mss
;
138 return iov
[0].iov_len
+ (n
- 1) * iov
[1].iov_len
;
142 * Read from so's socket into sb_snd, updating all relevant sbuf fields
143 * NOTE: This will only be called if it is select()ed for reading, so
144 * a read() of 0 (or less) means it's disconnected
147 soread(struct socket
*so
)
150 struct sbuf
*sb
= &so
->so_snd
;
153 DEBUG_CALL("soread");
154 DEBUG_ARG("so = %lx", (long )so
);
157 * No need to check if there's enough room to read.
158 * soread wouldn't have been called if there weren't
160 sopreprbuf(so
, iov
, &n
);
163 nn
= readv(so
->s
, (struct iovec
*)iov
, n
);
164 DEBUG_MISC((dfd
, " ... read nn = %d bytes\n", nn
));
166 nn
= recv(so
->s
, iov
[0].iov_base
, iov
[0].iov_len
,0);
169 if (nn
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
172 DEBUG_MISC((dfd
, " --- soread() disconnected, nn = %d, errno = %d-%s\n", nn
, errno
,strerror(errno
)));
174 tcp_sockclosed(sototcpcb(so
));
181 * If there was no error, try and read the second time round
182 * We read again if n = 2 (ie, there's another part of the buffer)
183 * and we read as much as we could in the first read
184 * We don't test for <= 0 this time, because there legitimately
185 * might not be any more data (since the socket is non-blocking),
186 * a close will be detected on next iteration.
187 * A return of -1 wont (shouldn't) happen, since it didn't happen above
189 if (n
== 2 && nn
== iov
[0].iov_len
) {
191 ret
= recv(so
->s
, iov
[1].iov_base
, iov
[1].iov_len
,0);
196 DEBUG_MISC((dfd
, " ... read nn = %d bytes\n", nn
));
202 if (sb
->sb_wptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
203 sb
->sb_wptr
-= sb
->sb_datalen
;
207 int soreadbuf(struct socket
*so
, const char *buf
, int size
)
209 int n
, nn
, copy
= size
;
210 struct sbuf
*sb
= &so
->so_snd
;
213 DEBUG_CALL("soreadbuf");
214 DEBUG_ARG("so = %lx", (long )so
);
217 * No need to check if there's enough room to read.
218 * soread wouldn't have been called if there weren't
220 if (sopreprbuf(so
, iov
, &n
) < size
)
223 nn
= MIN(iov
[0].iov_len
, copy
);
224 memcpy(iov
[0].iov_base
, buf
, nn
);
232 memcpy(iov
[1].iov_base
, buf
, copy
);
238 if (sb
->sb_wptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
239 sb
->sb_wptr
-= sb
->sb_datalen
;
244 tcp_sockclosed(sototcpcb(so
));
245 fprintf(stderr
, "soreadbuf buffer to small");
252 * When the socket is created, we set it SO_OOBINLINE,
253 * so when OOB data arrives, we soread() it and everything
254 * in the send buffer is sent as urgent data
257 sorecvoob(struct socket
*so
)
259 struct tcpcb
*tp
= sototcpcb(so
);
261 DEBUG_CALL("sorecvoob");
262 DEBUG_ARG("so = %lx", (long)so
);
265 * We take a guess at how much urgent data has arrived.
266 * In most situations, when urgent data arrives, the next
267 * read() should get all the urgent data. This guess will
268 * be wrong however if more data arrives just after the
269 * urgent data, or the read() doesn't return all the
273 tp
->snd_up
= tp
->snd_una
+ so
->so_snd
.sb_cc
;
281 * There's a lot duplicated code here, but...
284 sosendoob(struct socket
*so
)
286 struct sbuf
*sb
= &so
->so_rcv
;
287 char buff
[2048]; /* XXX Shouldn't be sending more oob data than this */
291 DEBUG_CALL("sosendoob");
292 DEBUG_ARG("so = %lx", (long)so
);
293 DEBUG_ARG("sb->sb_cc = %d", sb
->sb_cc
);
295 if (so
->so_urgc
> 2048)
296 so
->so_urgc
= 2048; /* XXXX */
298 if (sb
->sb_rptr
< sb
->sb_wptr
) {
299 /* We can send it directly */
300 n
= slirp_send(so
, sb
->sb_rptr
, so
->so_urgc
, (MSG_OOB
)); /* |MSG_DONTWAIT)); */
303 DEBUG_MISC((dfd
, " --- sent %d bytes urgent data, %d urgent bytes left\n", n
, so
->so_urgc
));
306 * Since there's no sendv or sendtov like writev,
307 * we must copy all data to a linear buffer then
310 len
= (sb
->sb_data
+ sb
->sb_datalen
) - sb
->sb_rptr
;
311 if (len
> so
->so_urgc
) len
= so
->so_urgc
;
312 memcpy(buff
, sb
->sb_rptr
, len
);
315 n
= sb
->sb_wptr
- sb
->sb_data
;
316 if (n
> so
->so_urgc
) n
= so
->so_urgc
;
317 memcpy((buff
+ len
), sb
->sb_data
, n
);
321 n
= slirp_send(so
, buff
, len
, (MSG_OOB
)); /* |MSG_DONTWAIT)); */
324 DEBUG_ERROR((dfd
, "Didn't send all data urgently XXXXX\n"));
326 DEBUG_MISC((dfd
, " ---2 sent %d bytes urgent data, %d urgent bytes left\n", n
, so
->so_urgc
));
331 if (sb
->sb_rptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
332 sb
->sb_rptr
-= sb
->sb_datalen
;
338 * Write data from so_rcv to so's socket,
339 * updating all sbuf field as necessary
342 sowrite(struct socket
*so
)
345 struct sbuf
*sb
= &so
->so_rcv
;
349 DEBUG_CALL("sowrite");
350 DEBUG_ARG("so = %lx", (long)so
);
359 * No need to check if there's something to write,
360 * sowrite wouldn't have been called otherwise
365 iov
[0].iov_base
= sb
->sb_rptr
;
366 iov
[1].iov_base
= NULL
;
368 if (sb
->sb_rptr
< sb
->sb_wptr
) {
369 iov
[0].iov_len
= sb
->sb_wptr
- sb
->sb_rptr
;
370 /* Should never succeed, but... */
371 if (iov
[0].iov_len
> len
) iov
[0].iov_len
= len
;
374 iov
[0].iov_len
= (sb
->sb_data
+ sb
->sb_datalen
) - sb
->sb_rptr
;
375 if (iov
[0].iov_len
> len
) iov
[0].iov_len
= len
;
376 len
-= iov
[0].iov_len
;
378 iov
[1].iov_base
= sb
->sb_data
;
379 iov
[1].iov_len
= sb
->sb_wptr
- sb
->sb_data
;
380 if (iov
[1].iov_len
> len
) iov
[1].iov_len
= len
;
385 /* Check if there's urgent data to send, and if so, send it */
388 nn
= writev(so
->s
, (const struct iovec
*)iov
, n
);
390 DEBUG_MISC((dfd
, " ... wrote nn = %d bytes\n", nn
));
392 nn
= slirp_send(so
, iov
[0].iov_base
, iov
[0].iov_len
,0);
394 /* This should never happen, but people tell me it does *shrug* */
395 if (nn
< 0 && (errno
== EAGAIN
|| errno
== EINTR
))
399 DEBUG_MISC((dfd
, " --- sowrite disconnected, so->so_state = %x, errno = %d\n",
400 so
->so_state
, errno
));
402 tcp_sockclosed(sototcpcb(so
));
407 if (n
== 2 && nn
== iov
[0].iov_len
) {
409 ret
= slirp_send(so
, iov
[1].iov_base
, iov
[1].iov_len
,0);
413 DEBUG_MISC((dfd
, " ... wrote nn = %d bytes\n", nn
));
419 if (sb
->sb_rptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
420 sb
->sb_rptr
-= sb
->sb_datalen
;
423 * If in DRAIN mode, and there's no more data, set
426 if ((so
->so_state
& SS_FWDRAIN
) && sb
->sb_cc
== 0)
433 * recvfrom() a UDP socket
436 sorecvfrom(struct socket
*so
)
438 struct sockaddr_in addr
;
439 socklen_t addrlen
= sizeof(struct sockaddr_in
);
441 DEBUG_CALL("sorecvfrom");
442 DEBUG_ARG("so = %lx", (long)so
);
444 if (so
->so_type
== IPPROTO_ICMP
) { /* This is a "ping" reply */
448 len
= recvfrom(so
->s
, buff
, 256, 0,
449 (struct sockaddr
*)&addr
, &addrlen
);
450 /* XXX Check if reply is "correct"? */
452 if(len
== -1 || len
== 0) {
453 u_char code
=ICMP_UNREACH_PORT
;
455 if(errno
== EHOSTUNREACH
) code
=ICMP_UNREACH_HOST
;
456 else if(errno
== ENETUNREACH
) code
=ICMP_UNREACH_NET
;
458 DEBUG_MISC((dfd
," udp icmp rx errno = %d-%s\n",
459 errno
,strerror(errno
)));
460 icmp_error(so
->so_m
, ICMP_UNREACH
,code
, 0,strerror(errno
));
462 icmp_reflect(so
->so_m
);
463 so
->so_m
= NULL
; /* Don't m_free() it again! */
465 /* No need for this socket anymore, udp_detach it */
467 } else { /* A "normal" UDP packet */
476 if (!(m
= m_get())) return;
477 m
->m_data
+= IF_MAXLINKHDR
;
480 * XXX Shouldn't FIONREAD packets destined for port 53,
481 * but I don't know the max packet size for DNS lookups
484 /* if (so->so_fport != htons(53)) { */
485 ioctlsocket(so
->s
, FIONREAD
, &n
);
488 n
= (m
->m_data
- m
->m_dat
) + m
->m_len
+ n
+ 1;
494 m
->m_len
= recvfrom(so
->s
, m
->m_data
, len
, 0,
495 (struct sockaddr
*)&addr
, &addrlen
);
496 DEBUG_MISC((dfd
, " did recvfrom %d, errno = %d-%s\n",
497 m
->m_len
, errno
,strerror(errno
)));
499 u_char code
=ICMP_UNREACH_PORT
;
501 if(errno
== EHOSTUNREACH
) code
=ICMP_UNREACH_HOST
;
502 else if(errno
== ENETUNREACH
) code
=ICMP_UNREACH_NET
;
504 DEBUG_MISC((dfd
," rx error, tx icmp ICMP_UNREACH:%i\n", code
));
505 icmp_error(so
->so_m
, ICMP_UNREACH
,code
, 0,strerror(errno
));
509 * Hack: domain name lookup will be used the most for UDP,
510 * and since they'll only be used once there's no need
511 * for the 4 minute (or whatever) timeout... So we time them
512 * out much quicker (10 seconds for now...)
515 if (so
->so_fport
== htons(53))
516 so
->so_expire
= curtime
+ SO_EXPIREFAST
;
518 so
->so_expire
= curtime
+ SO_EXPIRE
;
522 * If this packet was destined for CTL_ADDR,
523 * make it look like that's where it came from, done by udp_output
525 udp_output(so
, m
, &addr
);
527 } /* if ping packet */
534 sosendto(struct socket
*so
, struct mbuf
*m
)
537 struct sockaddr_in addr
;
539 DEBUG_CALL("sosendto");
540 DEBUG_ARG("so = %lx", (long)so
);
541 DEBUG_ARG("m = %lx", (long)m
);
543 addr
.sin_family
= AF_INET
;
544 if ((so
->so_faddr
.s_addr
& vnetwork_mask
.s_addr
) ==
545 vnetwork_addr
.s_addr
) {
547 if (so
->so_faddr
.s_addr
== vnameserver_addr
.s_addr
) {
548 addr
.sin_addr
= dns_addr
;
550 addr
.sin_addr
= loopback_addr
;
553 addr
.sin_addr
= so
->so_faddr
;
554 addr
.sin_port
= so
->so_fport
;
556 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
)));
558 /* Don't care what port we get */
559 ret
= sendto(so
->s
, m
->m_data
, m
->m_len
, 0,
560 (struct sockaddr
*)&addr
, sizeof (struct sockaddr
));
565 * Kill the socket if there's no reply in 4 minutes,
566 * but only if it's an expirable socket
569 so
->so_expire
= curtime
+ SO_EXPIRE
;
570 so
->so_state
&= SS_PERSISTENT_MASK
;
571 so
->so_state
|= SS_ISFCONNECTED
; /* So that it gets select()ed */
576 * Listen for incoming TCP connections
579 tcp_listen(u_int32_t haddr
, u_int hport
, u_int32_t laddr
, u_int lport
, int flags
)
581 struct sockaddr_in addr
;
584 socklen_t addrlen
= sizeof(addr
);
586 DEBUG_CALL("tcp_listen");
587 DEBUG_ARG("haddr = %x", haddr
);
588 DEBUG_ARG("hport = %d", hport
);
589 DEBUG_ARG("laddr = %x", laddr
);
590 DEBUG_ARG("lport = %d", lport
);
591 DEBUG_ARG("flags = %x", flags
);
593 if ((so
= socreate()) == NULL
) {
597 /* Don't tcp_attach... we don't need so_snd nor so_rcv */
598 if ((so
->so_tcpcb
= tcp_newtcpcb(so
)) == NULL
) {
605 * SS_FACCEPTONCE sockets must time out.
607 if (flags
& SS_FACCEPTONCE
)
608 so
->so_tcpcb
->t_timer
[TCPT_KEEP
] = TCPTV_KEEP_INIT
*2;
610 so
->so_state
&= SS_PERSISTENT_MASK
;
611 so
->so_state
|= (SS_FACCEPTCONN
| flags
);
612 so
->so_lport
= lport
; /* Kept in network format */
613 so
->so_laddr
.s_addr
= laddr
; /* Ditto */
615 addr
.sin_family
= AF_INET
;
616 addr
.sin_addr
.s_addr
= haddr
;
617 addr
.sin_port
= hport
;
619 if (((s
= socket(AF_INET
,SOCK_STREAM
,0)) < 0) ||
620 (setsockopt(s
,SOL_SOCKET
,SO_REUSEADDR
,(char *)&opt
,sizeof(int)) < 0) ||
621 (bind(s
,(struct sockaddr
*)&addr
, sizeof(addr
)) < 0) ||
623 int tmperrno
= errno
; /* Don't clobber the real reason we failed */
627 /* Restore the real errno */
629 WSASetLastError(tmperrno
);
635 setsockopt(s
,SOL_SOCKET
,SO_OOBINLINE
,(char *)&opt
,sizeof(int));
637 getsockname(s
,(struct sockaddr
*)&addr
,&addrlen
);
638 so
->so_fport
= addr
.sin_port
;
639 if (addr
.sin_addr
.s_addr
== 0 || addr
.sin_addr
.s_addr
== loopback_addr
.s_addr
)
640 so
->so_faddr
= vhost_addr
;
642 so
->so_faddr
= addr
.sin_addr
;
649 * Various session state calls
650 * XXX Should be #define's
651 * The socket state stuff needs work, these often get call 2 or 3
652 * times each when only 1 was needed
655 soisfconnecting(struct socket
*so
)
657 so
->so_state
&= ~(SS_NOFDREF
|SS_ISFCONNECTED
|SS_FCANTRCVMORE
|
658 SS_FCANTSENDMORE
|SS_FWDRAIN
);
659 so
->so_state
|= SS_ISFCONNECTING
; /* Clobber other states */
663 soisfconnected(struct socket
*so
)
665 so
->so_state
&= ~(SS_ISFCONNECTING
|SS_FWDRAIN
|SS_NOFDREF
);
666 so
->so_state
|= SS_ISFCONNECTED
; /* Clobber other states */
670 sofcantrcvmore(struct socket
*so
)
672 if ((so
->so_state
& SS_NOFDREF
) == 0) {
674 if(global_writefds
) {
675 FD_CLR(so
->s
,global_writefds
);
678 so
->so_state
&= ~(SS_ISFCONNECTING
);
679 if (so
->so_state
& SS_FCANTSENDMORE
) {
680 so
->so_state
&= SS_PERSISTENT_MASK
;
681 so
->so_state
|= SS_NOFDREF
; /* Don't select it */
683 so
->so_state
|= SS_FCANTRCVMORE
;
688 sofcantsendmore(struct socket
*so
)
690 if ((so
->so_state
& SS_NOFDREF
) == 0) {
691 shutdown(so
->s
,1); /* send FIN to fhost */
692 if (global_readfds
) {
693 FD_CLR(so
->s
,global_readfds
);
696 FD_CLR(so
->s
,global_xfds
);
699 so
->so_state
&= ~(SS_ISFCONNECTING
);
700 if (so
->so_state
& SS_FCANTRCVMORE
) {
701 so
->so_state
&= SS_PERSISTENT_MASK
;
702 so
->so_state
|= SS_NOFDREF
; /* as above */
704 so
->so_state
|= SS_FCANTSENDMORE
;
709 * Set write drain mode
710 * Set CANTSENDMORE once all data has been write()n
713 sofwdrain(struct socket
*so
)
715 if (so
->so_rcv
.sb_cc
)
716 so
->so_state
|= SS_FWDRAIN
;