]>
git.proxmox.com Git - 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
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
;
59 * remque and free a socket, clobber cache
62 sofree(struct socket
*so
)
64 Slirp
*slirp
= so
->slirp
;
66 if (so
->so_emu
==EMU_RSH
&& so
->extra
) {
70 if (so
== slirp
->tcp_last_so
) {
71 slirp
->tcp_last_so
= &slirp
->tcb
;
72 } else if (so
== slirp
->udp_last_so
) {
73 slirp
->udp_last_so
= &slirp
->udb
;
74 } else if (so
== slirp
->icmp_last_so
) {
75 slirp
->icmp_last_so
= &slirp
->icmp
;
79 if(so
->so_next
&& so
->so_prev
)
80 remque(so
); /* crashes if so is not in a queue */
85 size_t sopreprbuf(struct socket
*so
, struct iovec
*iov
, int *np
)
88 struct sbuf
*sb
= &so
->so_snd
;
89 int len
= sb
->sb_datalen
- sb
->sb_cc
;
90 int mss
= so
->so_tcpcb
->t_maxseg
;
92 DEBUG_CALL("sopreprbuf");
93 DEBUG_ARG("so = %lx", (long )so
);
98 iov
[0].iov_base
= sb
->sb_wptr
;
99 iov
[1].iov_base
= NULL
;
101 if (sb
->sb_wptr
< sb
->sb_rptr
) {
102 iov
[0].iov_len
= sb
->sb_rptr
- sb
->sb_wptr
;
103 /* Should never succeed, but... */
104 if (iov
[0].iov_len
> len
)
105 iov
[0].iov_len
= len
;
106 if (iov
[0].iov_len
> mss
)
107 iov
[0].iov_len
-= iov
[0].iov_len
%mss
;
110 iov
[0].iov_len
= (sb
->sb_data
+ sb
->sb_datalen
) - sb
->sb_wptr
;
111 /* Should never succeed, but... */
112 if (iov
[0].iov_len
> len
) iov
[0].iov_len
= len
;
113 len
-= iov
[0].iov_len
;
115 iov
[1].iov_base
= sb
->sb_data
;
116 iov
[1].iov_len
= sb
->sb_rptr
- sb
->sb_data
;
117 if(iov
[1].iov_len
> len
)
118 iov
[1].iov_len
= len
;
119 total
= iov
[0].iov_len
+ iov
[1].iov_len
;
122 if (iov
[1].iov_len
> lss
) {
123 iov
[1].iov_len
-= lss
;
126 lss
-= iov
[1].iov_len
;
127 iov
[0].iov_len
-= lss
;
133 if (iov
[0].iov_len
> mss
)
134 iov
[0].iov_len
-= iov
[0].iov_len
%mss
;
141 return iov
[0].iov_len
+ (n
- 1) * iov
[1].iov_len
;
145 * Read from so's socket into sb_snd, updating all relevant sbuf fields
146 * NOTE: This will only be called if it is select()ed for reading, so
147 * a read() of 0 (or less) means it's disconnected
150 soread(struct socket
*so
)
153 struct sbuf
*sb
= &so
->so_snd
;
156 DEBUG_CALL("soread");
157 DEBUG_ARG("so = %lx", (long )so
);
160 * No need to check if there's enough room to read.
161 * soread wouldn't have been called if there weren't
163 sopreprbuf(so
, iov
, &n
);
166 nn
= readv(so
->s
, (struct iovec
*)iov
, n
);
167 DEBUG_MISC((dfd
, " ... read nn = %d bytes\n", nn
));
169 nn
= qemu_recv(so
->s
, iov
[0].iov_base
, iov
[0].iov_len
,0);
172 if (nn
< 0 && (errno
== EINTR
|| errno
== EAGAIN
))
175 DEBUG_MISC((dfd
, " --- soread() disconnected, nn = %d, errno = %d-%s\n", nn
, errno
,strerror(errno
)));
177 tcp_sockclosed(sototcpcb(so
));
184 * If there was no error, try and read the second time round
185 * We read again if n = 2 (ie, there's another part of the buffer)
186 * and we read as much as we could in the first read
187 * We don't test for <= 0 this time, because there legitimately
188 * might not be any more data (since the socket is non-blocking),
189 * a close will be detected on next iteration.
190 * A return of -1 wont (shouldn't) happen, since it didn't happen above
192 if (n
== 2 && nn
== iov
[0].iov_len
) {
194 ret
= qemu_recv(so
->s
, iov
[1].iov_base
, iov
[1].iov_len
,0);
199 DEBUG_MISC((dfd
, " ... read nn = %d bytes\n", nn
));
205 if (sb
->sb_wptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
206 sb
->sb_wptr
-= sb
->sb_datalen
;
210 int soreadbuf(struct socket
*so
, const char *buf
, int size
)
212 int n
, nn
, copy
= size
;
213 struct sbuf
*sb
= &so
->so_snd
;
216 DEBUG_CALL("soreadbuf");
217 DEBUG_ARG("so = %lx", (long )so
);
220 * No need to check if there's enough room to read.
221 * soread wouldn't have been called if there weren't
223 if (sopreprbuf(so
, iov
, &n
) < size
)
226 nn
= MIN(iov
[0].iov_len
, copy
);
227 memcpy(iov
[0].iov_base
, buf
, nn
);
235 memcpy(iov
[1].iov_base
, buf
, copy
);
241 if (sb
->sb_wptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
242 sb
->sb_wptr
-= sb
->sb_datalen
;
247 tcp_sockclosed(sototcpcb(so
));
248 fprintf(stderr
, "soreadbuf buffer to small");
255 * When the socket is created, we set it SO_OOBINLINE,
256 * so when OOB data arrives, we soread() it and everything
257 * in the send buffer is sent as urgent data
260 sorecvoob(struct socket
*so
)
262 struct tcpcb
*tp
= sototcpcb(so
);
264 DEBUG_CALL("sorecvoob");
265 DEBUG_ARG("so = %lx", (long)so
);
268 * We take a guess at how much urgent data has arrived.
269 * In most situations, when urgent data arrives, the next
270 * read() should get all the urgent data. This guess will
271 * be wrong however if more data arrives just after the
272 * urgent data, or the read() doesn't return all the
276 tp
->snd_up
= tp
->snd_una
+ so
->so_snd
.sb_cc
;
284 * There's a lot duplicated code here, but...
287 sosendoob(struct socket
*so
)
289 struct sbuf
*sb
= &so
->so_rcv
;
290 char buff
[2048]; /* XXX Shouldn't be sending more oob data than this */
294 DEBUG_CALL("sosendoob");
295 DEBUG_ARG("so = %lx", (long)so
);
296 DEBUG_ARG("sb->sb_cc = %d", sb
->sb_cc
);
298 if (so
->so_urgc
> 2048)
299 so
->so_urgc
= 2048; /* XXXX */
301 if (sb
->sb_rptr
< sb
->sb_wptr
) {
302 /* We can send it directly */
303 n
= slirp_send(so
, sb
->sb_rptr
, so
->so_urgc
, (MSG_OOB
)); /* |MSG_DONTWAIT)); */
306 DEBUG_MISC((dfd
, " --- sent %d bytes urgent data, %d urgent bytes left\n", n
, so
->so_urgc
));
309 * Since there's no sendv or sendtov like writev,
310 * we must copy all data to a linear buffer then
313 len
= (sb
->sb_data
+ sb
->sb_datalen
) - sb
->sb_rptr
;
314 if (len
> so
->so_urgc
) len
= so
->so_urgc
;
315 memcpy(buff
, sb
->sb_rptr
, len
);
318 n
= sb
->sb_wptr
- sb
->sb_data
;
319 if (n
> so
->so_urgc
) n
= so
->so_urgc
;
320 memcpy((buff
+ len
), sb
->sb_data
, n
);
324 n
= slirp_send(so
, buff
, len
, (MSG_OOB
)); /* |MSG_DONTWAIT)); */
327 DEBUG_ERROR((dfd
, "Didn't send all data urgently XXXXX\n"));
329 DEBUG_MISC((dfd
, " ---2 sent %d bytes urgent data, %d urgent bytes left\n", n
, so
->so_urgc
));
334 if (sb
->sb_rptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
335 sb
->sb_rptr
-= sb
->sb_datalen
;
341 * Write data from so_rcv to so's socket,
342 * updating all sbuf field as necessary
345 sowrite(struct socket
*so
)
348 struct sbuf
*sb
= &so
->so_rcv
;
352 DEBUG_CALL("sowrite");
353 DEBUG_ARG("so = %lx", (long)so
);
362 * No need to check if there's something to write,
363 * sowrite wouldn't have been called otherwise
366 iov
[0].iov_base
= sb
->sb_rptr
;
367 iov
[1].iov_base
= NULL
;
369 if (sb
->sb_rptr
< sb
->sb_wptr
) {
370 iov
[0].iov_len
= sb
->sb_wptr
- sb
->sb_rptr
;
371 /* Should never succeed, but... */
372 if (iov
[0].iov_len
> len
) iov
[0].iov_len
= len
;
375 iov
[0].iov_len
= (sb
->sb_data
+ sb
->sb_datalen
) - sb
->sb_rptr
;
376 if (iov
[0].iov_len
> len
) iov
[0].iov_len
= len
;
377 len
-= iov
[0].iov_len
;
379 iov
[1].iov_base
= sb
->sb_data
;
380 iov
[1].iov_len
= sb
->sb_wptr
- sb
->sb_data
;
381 if (iov
[1].iov_len
> len
) iov
[1].iov_len
= len
;
386 /* Check if there's urgent data to send, and if so, send it */
389 nn
= writev(so
->s
, (const struct iovec
*)iov
, n
);
391 DEBUG_MISC((dfd
, " ... wrote nn = %d bytes\n", nn
));
393 nn
= slirp_send(so
, iov
[0].iov_base
, iov
[0].iov_len
,0);
395 /* This should never happen, but people tell me it does *shrug* */
396 if (nn
< 0 && (errno
== EAGAIN
|| errno
== EINTR
))
400 DEBUG_MISC((dfd
, " --- sowrite disconnected, so->so_state = %x, errno = %d\n",
401 so
->so_state
, errno
));
403 tcp_sockclosed(sototcpcb(so
));
408 if (n
== 2 && nn
== iov
[0].iov_len
) {
410 ret
= slirp_send(so
, iov
[1].iov_base
, iov
[1].iov_len
,0);
414 DEBUG_MISC((dfd
, " ... wrote nn = %d bytes\n", nn
));
420 if (sb
->sb_rptr
>= (sb
->sb_data
+ sb
->sb_datalen
))
421 sb
->sb_rptr
-= sb
->sb_datalen
;
424 * If in DRAIN mode, and there's no more data, set
427 if ((so
->so_state
& SS_FWDRAIN
) && sb
->sb_cc
== 0)
434 * recvfrom() a UDP socket
437 sorecvfrom(struct socket
*so
)
439 struct sockaddr_in addr
;
440 socklen_t addrlen
= sizeof(struct sockaddr_in
);
442 DEBUG_CALL("sorecvfrom");
443 DEBUG_ARG("so = %lx", (long)so
);
445 if (so
->so_type
== IPPROTO_ICMP
) { /* This is a "ping" reply */
449 len
= recvfrom(so
->s
, buff
, 256, 0,
450 (struct sockaddr
*)&addr
, &addrlen
);
451 /* XXX Check if reply is "correct"? */
453 if(len
== -1 || len
== 0) {
454 u_char code
=ICMP_UNREACH_PORT
;
456 if(errno
== EHOSTUNREACH
) code
=ICMP_UNREACH_HOST
;
457 else if(errno
== ENETUNREACH
) code
=ICMP_UNREACH_NET
;
459 DEBUG_MISC((dfd
," udp icmp rx errno = %d-%s\n",
460 errno
,strerror(errno
)));
461 icmp_error(so
->so_m
, ICMP_UNREACH
,code
, 0,strerror(errno
));
463 icmp_reflect(so
->so_m
);
464 so
->so_m
= NULL
; /* Don't m_free() it again! */
466 /* No need for this socket anymore, udp_detach it */
468 } else { /* A "normal" UDP packet */
477 m
= m_get(so
->slirp
);
481 m
->m_data
+= IF_MAXLINKHDR
;
484 * XXX Shouldn't FIONREAD packets destined for port 53,
485 * but I don't know the max packet size for DNS lookups
488 /* if (so->so_fport != htons(53)) { */
489 ioctlsocket(so
->s
, FIONREAD
, &n
);
492 n
= (m
->m_data
- m
->m_dat
) + m
->m_len
+ n
+ 1;
498 m
->m_len
= recvfrom(so
->s
, m
->m_data
, len
, 0,
499 (struct sockaddr
*)&addr
, &addrlen
);
500 DEBUG_MISC((dfd
, " did recvfrom %d, errno = %d-%s\n",
501 m
->m_len
, errno
,strerror(errno
)));
503 u_char code
=ICMP_UNREACH_PORT
;
505 if(errno
== EHOSTUNREACH
) code
=ICMP_UNREACH_HOST
;
506 else if(errno
== ENETUNREACH
) code
=ICMP_UNREACH_NET
;
508 DEBUG_MISC((dfd
," rx error, tx icmp ICMP_UNREACH:%i\n", code
));
509 icmp_error(so
->so_m
, ICMP_UNREACH
,code
, 0,strerror(errno
));
513 * Hack: domain name lookup will be used the most for UDP,
514 * and since they'll only be used once there's no need
515 * for the 4 minute (or whatever) timeout... So we time them
516 * out much quicker (10 seconds for now...)
519 if (so
->so_fport
== htons(53))
520 so
->so_expire
= curtime
+ SO_EXPIREFAST
;
522 so
->so_expire
= curtime
+ SO_EXPIRE
;
526 * If this packet was destined for CTL_ADDR,
527 * make it look like that's where it came from, done by udp_output
529 udp_output(so
, m
, &addr
);
531 } /* if ping packet */
538 sosendto(struct socket
*so
, struct mbuf
*m
)
540 Slirp
*slirp
= so
->slirp
;
542 struct sockaddr_in addr
;
544 DEBUG_CALL("sosendto");
545 DEBUG_ARG("so = %lx", (long)so
);
546 DEBUG_ARG("m = %lx", (long)m
);
548 addr
.sin_family
= AF_INET
;
549 if ((so
->so_faddr
.s_addr
& slirp
->vnetwork_mask
.s_addr
) ==
550 slirp
->vnetwork_addr
.s_addr
) {
552 if (so
->so_faddr
.s_addr
== slirp
->vnameserver_addr
.s_addr
) {
553 if (get_dns_addr(&addr
.sin_addr
) < 0)
554 addr
.sin_addr
= loopback_addr
;
556 addr
.sin_addr
= loopback_addr
;
559 addr
.sin_addr
= so
->so_faddr
;
560 addr
.sin_port
= so
->so_fport
;
562 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
)));
564 /* Don't care what port we get */
565 ret
= sendto(so
->s
, m
->m_data
, m
->m_len
, 0,
566 (struct sockaddr
*)&addr
, sizeof (struct sockaddr
));
571 * Kill the socket if there's no reply in 4 minutes,
572 * but only if it's an expirable socket
575 so
->so_expire
= curtime
+ SO_EXPIRE
;
576 so
->so_state
&= SS_PERSISTENT_MASK
;
577 so
->so_state
|= SS_ISFCONNECTED
; /* So that it gets select()ed */
582 * Listen for incoming TCP connections
585 tcp_listen(Slirp
*slirp
, uint32_t haddr
, u_int hport
, uint32_t laddr
,
586 u_int lport
, int flags
)
588 struct sockaddr_in addr
;
591 socklen_t addrlen
= sizeof(addr
);
592 memset(&addr
, 0, addrlen
);
594 DEBUG_CALL("tcp_listen");
595 DEBUG_ARG("haddr = %x", haddr
);
596 DEBUG_ARG("hport = %d", hport
);
597 DEBUG_ARG("laddr = %x", laddr
);
598 DEBUG_ARG("lport = %d", lport
);
599 DEBUG_ARG("flags = %x", flags
);
601 so
= socreate(slirp
);
606 /* Don't tcp_attach... we don't need so_snd nor so_rcv */
607 if ((so
->so_tcpcb
= tcp_newtcpcb(so
)) == NULL
) {
611 insque(so
, &slirp
->tcb
);
614 * SS_FACCEPTONCE sockets must time out.
616 if (flags
& SS_FACCEPTONCE
)
617 so
->so_tcpcb
->t_timer
[TCPT_KEEP
] = TCPTV_KEEP_INIT
*2;
619 so
->so_state
&= SS_PERSISTENT_MASK
;
620 so
->so_state
|= (SS_FACCEPTCONN
| flags
);
621 so
->so_lport
= lport
; /* Kept in network format */
622 so
->so_laddr
.s_addr
= laddr
; /* Ditto */
624 addr
.sin_family
= AF_INET
;
625 addr
.sin_addr
.s_addr
= haddr
;
626 addr
.sin_port
= hport
;
628 if (((s
= qemu_socket(AF_INET
,SOCK_STREAM
,0)) < 0) ||
629 (setsockopt(s
,SOL_SOCKET
,SO_REUSEADDR
,(char *)&opt
,sizeof(int)) < 0) ||
630 (bind(s
,(struct sockaddr
*)&addr
, sizeof(addr
)) < 0) ||
632 int tmperrno
= errno
; /* Don't clobber the real reason we failed */
636 /* Restore the real errno */
638 WSASetLastError(tmperrno
);
644 setsockopt(s
,SOL_SOCKET
,SO_OOBINLINE
,(char *)&opt
,sizeof(int));
646 getsockname(s
,(struct sockaddr
*)&addr
,&addrlen
);
647 so
->so_fport
= addr
.sin_port
;
648 if (addr
.sin_addr
.s_addr
== 0 || addr
.sin_addr
.s_addr
== loopback_addr
.s_addr
)
649 so
->so_faddr
= slirp
->vhost_addr
;
651 so
->so_faddr
= addr
.sin_addr
;
658 * Various session state calls
659 * XXX Should be #define's
660 * The socket state stuff needs work, these often get call 2 or 3
661 * times each when only 1 was needed
664 soisfconnecting(struct socket
*so
)
666 so
->so_state
&= ~(SS_NOFDREF
|SS_ISFCONNECTED
|SS_FCANTRCVMORE
|
667 SS_FCANTSENDMORE
|SS_FWDRAIN
);
668 so
->so_state
|= SS_ISFCONNECTING
; /* Clobber other states */
672 soisfconnected(struct socket
*so
)
674 so
->so_state
&= ~(SS_ISFCONNECTING
|SS_FWDRAIN
|SS_NOFDREF
);
675 so
->so_state
|= SS_ISFCONNECTED
; /* Clobber other states */
679 sofcantrcvmore(struct socket
*so
)
681 if ((so
->so_state
& SS_NOFDREF
) == 0) {
684 so
->so_state
&= ~(SS_ISFCONNECTING
);
685 if (so
->so_state
& SS_FCANTSENDMORE
) {
686 so
->so_state
&= SS_PERSISTENT_MASK
;
687 so
->so_state
|= SS_NOFDREF
; /* Don't select it */
689 so
->so_state
|= SS_FCANTRCVMORE
;
694 sofcantsendmore(struct socket
*so
)
696 if ((so
->so_state
& SS_NOFDREF
) == 0) {
697 shutdown(so
->s
,1); /* send FIN to fhost */
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
;