4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "net/socket.h"
26 #include "config-host.h"
30 #include "qemu-char.h"
31 #include "qemu-common.h"
32 #include "qemu-error.h"
33 #include "qemu-option.h"
34 #include "qemu_socket.h"
37 typedef struct NetSocketState
{
41 int state
; /* 0 = getting length, 1 = getting data */
43 unsigned int packet_len
;
44 unsigned int send_index
; /* number of bytes sent (only SOCK_STREAM) */
46 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
47 IOHandler
*send_fn
; /* differs between SOCK_STREAM/SOCK_DGRAM */
48 bool read_poll
; /* waiting to receive data? */
49 bool write_poll
; /* waiting to transmit data? */
52 static void net_socket_accept(void *opaque
);
53 static void net_socket_writable(void *opaque
);
55 /* Only read packets from socket when peer can receive them */
56 static int net_socket_can_send(void *opaque
)
58 NetSocketState
*s
= opaque
;
60 return qemu_can_send_packet(&s
->nc
);
63 static void net_socket_update_fd_handler(NetSocketState
*s
)
65 qemu_set_fd_handler2(s
->fd
,
66 s
->read_poll
? net_socket_can_send
: NULL
,
67 s
->read_poll
? s
->send_fn
: NULL
,
68 s
->write_poll
? net_socket_writable
: NULL
,
72 static void net_socket_read_poll(NetSocketState
*s
, bool enable
)
74 s
->read_poll
= enable
;
75 net_socket_update_fd_handler(s
);
78 static void net_socket_write_poll(NetSocketState
*s
, bool enable
)
80 s
->write_poll
= enable
;
81 net_socket_update_fd_handler(s
);
84 static void net_socket_writable(void *opaque
)
86 NetSocketState
*s
= opaque
;
88 net_socket_write_poll(s
, false);
90 qemu_flush_queued_packets(&s
->nc
);
93 static ssize_t
net_socket_receive(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
95 NetSocketState
*s
= DO_UPCAST(NetSocketState
, nc
, nc
);
96 uint32_t len
= htonl(size
);
97 struct iovec iov
[] = {
100 .iov_len
= sizeof(len
),
102 .iov_base
= (void *)buf
,
109 remaining
= iov_size(iov
, 2) - s
->send_index
;
110 ret
= iov_send(s
->fd
, iov
, 2, s
->send_index
, remaining
);
112 if (ret
== -1 && errno
== EAGAIN
) {
113 ret
= 0; /* handled further down */
119 if (ret
< (ssize_t
)remaining
) {
120 s
->send_index
+= ret
;
121 net_socket_write_poll(s
, true);
128 static ssize_t
net_socket_receive_dgram(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
130 NetSocketState
*s
= DO_UPCAST(NetSocketState
, nc
, nc
);
134 ret
= qemu_sendto(s
->fd
, buf
, size
, 0,
135 (struct sockaddr
*)&s
->dgram_dst
,
136 sizeof(s
->dgram_dst
));
137 } while (ret
== -1 && errno
== EINTR
);
139 if (ret
== -1 && errno
== EAGAIN
) {
140 net_socket_write_poll(s
, true);
146 static void net_socket_send(void *opaque
)
148 NetSocketState
*s
= opaque
;
154 size
= qemu_recv(s
->fd
, buf1
, sizeof(buf1
), 0);
156 err
= socket_error();
157 if (err
!= EWOULDBLOCK
)
159 } else if (size
== 0) {
160 /* end of connection */
162 net_socket_read_poll(s
, false);
163 net_socket_write_poll(s
, false);
164 if (s
->listen_fd
!= -1) {
165 qemu_set_fd_handler(s
->listen_fd
, net_socket_accept
, NULL
, s
);
173 s
->nc
.link_down
= true;
174 memset(s
->buf
, 0, sizeof(s
->buf
));
175 memset(s
->nc
.info_str
, 0, sizeof(s
->nc
.info_str
));
181 /* reassemble a packet from the network */
187 memcpy(s
->buf
+ s
->index
, buf
, l
);
193 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
199 l
= s
->packet_len
- s
->index
;
202 if (s
->index
+ l
<= sizeof(s
->buf
)) {
203 memcpy(s
->buf
+ s
->index
, buf
, l
);
205 fprintf(stderr
, "serious error: oversized packet received,"
206 "connection terminated.\n");
214 if (s
->index
>= s
->packet_len
) {
215 qemu_send_packet(&s
->nc
, s
->buf
, s
->packet_len
);
224 static void net_socket_send_dgram(void *opaque
)
226 NetSocketState
*s
= opaque
;
229 size
= qemu_recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
233 /* end of connection */
234 net_socket_read_poll(s
, false);
235 net_socket_write_poll(s
, false);
238 qemu_send_packet(&s
->nc
, s
->buf
, size
);
241 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
, struct in_addr
*localaddr
)
252 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
253 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) "
254 "does not contain a multicast address\n",
255 inet_ntoa(mcastaddr
->sin_addr
),
256 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
260 fd
= qemu_socket(PF_INET
, SOCK_DGRAM
, 0);
262 perror("socket(PF_INET, SOCK_DGRAM)");
267 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
268 (const char *)&val
, sizeof(val
));
270 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
274 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
280 /* Add host to multicast group */
281 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
283 imr
.imr_interface
= *localaddr
;
285 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
288 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
289 (const char *)&imr
, sizeof(struct ip_mreq
));
291 perror("setsockopt(IP_ADD_MEMBERSHIP)");
295 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
297 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
298 (const char *)&loop
, sizeof(loop
));
300 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
304 /* If a bind address is given, only send packets from that address */
305 if (localaddr
!= NULL
) {
306 ret
= setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_IF
,
307 (const char *)localaddr
, sizeof(*localaddr
));
309 perror("setsockopt(IP_MULTICAST_IF)");
314 socket_set_nonblock(fd
);
322 static void net_socket_cleanup(NetClientState
*nc
)
324 NetSocketState
*s
= DO_UPCAST(NetSocketState
, nc
, nc
);
326 net_socket_read_poll(s
, false);
327 net_socket_write_poll(s
, false);
331 if (s
->listen_fd
!= -1) {
332 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
333 closesocket(s
->listen_fd
);
338 static NetClientInfo net_dgram_socket_info
= {
339 .type
= NET_CLIENT_OPTIONS_KIND_SOCKET
,
340 .size
= sizeof(NetSocketState
),
341 .receive
= net_socket_receive_dgram
,
342 .cleanup
= net_socket_cleanup
,
345 static NetSocketState
*net_socket_fd_init_dgram(NetClientState
*peer
,
348 int fd
, int is_connected
)
350 struct sockaddr_in saddr
;
356 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
357 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
358 * by ONLY ONE process: we must "clone" this dgram socket --jjo
362 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
364 if (saddr
.sin_addr
.s_addr
== 0) {
365 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, "
366 "cannot setup multicast dst addr\n", fd
);
369 /* clone dgram socket */
370 newfd
= net_socket_mcast_create(&saddr
, NULL
);
372 /* error already reported by net_socket_mcast_create() */
375 /* clone newfd to fd, close newfd */
381 "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
382 fd
, strerror(errno
));
387 nc
= qemu_new_net_client(&net_dgram_socket_info
, peer
, model
, name
);
389 snprintf(nc
->info_str
, sizeof(nc
->info_str
),
390 "socket: fd=%d (%s mcast=%s:%d)",
391 fd
, is_connected
? "cloned" : "",
392 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
394 s
= DO_UPCAST(NetSocketState
, nc
, nc
);
398 s
->send_fn
= net_socket_send_dgram
;
399 net_socket_read_poll(s
, true);
401 /* mcast: save bound address as dst */
403 s
->dgram_dst
= saddr
;
413 static void net_socket_connect(void *opaque
)
415 NetSocketState
*s
= opaque
;
416 s
->send_fn
= net_socket_send
;
417 net_socket_read_poll(s
, true);
420 static NetClientInfo net_socket_info
= {
421 .type
= NET_CLIENT_OPTIONS_KIND_SOCKET
,
422 .size
= sizeof(NetSocketState
),
423 .receive
= net_socket_receive
,
424 .cleanup
= net_socket_cleanup
,
427 static NetSocketState
*net_socket_fd_init_stream(NetClientState
*peer
,
430 int fd
, int is_connected
)
435 nc
= qemu_new_net_client(&net_socket_info
, peer
, model
, name
);
437 snprintf(nc
->info_str
, sizeof(nc
->info_str
), "socket: fd=%d", fd
);
439 s
= DO_UPCAST(NetSocketState
, nc
, nc
);
445 net_socket_connect(s
);
447 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
452 static NetSocketState
*net_socket_fd_init(NetClientState
*peer
,
453 const char *model
, const char *name
,
454 int fd
, int is_connected
)
456 int so_type
= -1, optlen
=sizeof(so_type
);
458 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
,
459 (socklen_t
*)&optlen
)< 0) {
460 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n",
467 return net_socket_fd_init_dgram(peer
, model
, name
, fd
, is_connected
);
469 return net_socket_fd_init_stream(peer
, model
, name
, fd
, is_connected
);
471 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
472 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
473 return net_socket_fd_init_stream(peer
, model
, name
, fd
, is_connected
);
478 static void net_socket_accept(void *opaque
)
480 NetSocketState
*s
= opaque
;
481 struct sockaddr_in saddr
;
487 fd
= qemu_accept(s
->listen_fd
, (struct sockaddr
*)&saddr
, &len
);
488 if (fd
< 0 && errno
!= EINTR
) {
490 } else if (fd
>= 0) {
491 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
497 s
->nc
.link_down
= false;
498 net_socket_connect(s
);
499 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
500 "socket: connection from %s:%d",
501 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
504 static int net_socket_listen_init(NetClientState
*peer
,
507 const char *host_str
)
511 struct sockaddr_in saddr
;
514 if (parse_host_port(&saddr
, host_str
) < 0)
517 fd
= qemu_socket(PF_INET
, SOCK_STREAM
, 0);
522 socket_set_nonblock(fd
);
524 /* allow fast reuse */
526 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
528 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
541 nc
= qemu_new_net_client(&net_socket_info
, peer
, model
, name
);
542 s
= DO_UPCAST(NetSocketState
, nc
, nc
);
545 s
->nc
.link_down
= true;
547 qemu_set_fd_handler(s
->listen_fd
, net_socket_accept
, NULL
, s
);
551 static int net_socket_connect_init(NetClientState
*peer
,
554 const char *host_str
)
557 int fd
, connected
, ret
, err
;
558 struct sockaddr_in saddr
;
560 if (parse_host_port(&saddr
, host_str
) < 0)
563 fd
= qemu_socket(PF_INET
, SOCK_STREAM
, 0);
568 socket_set_nonblock(fd
);
572 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
574 err
= socket_error();
575 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
576 } else if (err
== EINPROGRESS
) {
579 } else if (err
== WSAEALREADY
|| err
== WSAEINVAL
) {
592 s
= net_socket_fd_init(peer
, model
, name
, fd
, connected
);
595 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
596 "socket: connect to %s:%d",
597 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
601 static int net_socket_mcast_init(NetClientState
*peer
,
604 const char *host_str
,
605 const char *localaddr_str
)
609 struct sockaddr_in saddr
;
610 struct in_addr localaddr
, *param_localaddr
;
612 if (parse_host_port(&saddr
, host_str
) < 0)
615 if (localaddr_str
!= NULL
) {
616 if (inet_aton(localaddr_str
, &localaddr
) == 0)
618 param_localaddr
= &localaddr
;
620 param_localaddr
= NULL
;
623 fd
= net_socket_mcast_create(&saddr
, param_localaddr
);
627 s
= net_socket_fd_init(peer
, model
, name
, fd
, 0);
631 s
->dgram_dst
= saddr
;
633 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
634 "socket: mcast=%s:%d",
635 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
640 static int net_socket_udp_init(NetClientState
*peer
,
648 struct sockaddr_in laddr
, raddr
;
650 if (parse_host_port(&laddr
, lhost
) < 0) {
654 if (parse_host_port(&raddr
, rhost
) < 0) {
658 fd
= qemu_socket(PF_INET
, SOCK_DGRAM
, 0);
660 perror("socket(PF_INET, SOCK_DGRAM)");
664 ret
= setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
665 (const char *)&val
, sizeof(val
));
667 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
671 ret
= bind(fd
, (struct sockaddr
*)&laddr
, sizeof(laddr
));
678 s
= net_socket_fd_init(peer
, model
, name
, fd
, 0);
683 s
->dgram_dst
= raddr
;
685 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
687 inet_ntoa(raddr
.sin_addr
), ntohs(raddr
.sin_port
));
691 int net_init_socket(const NetClientOptions
*opts
, const char *name
,
692 NetClientState
*peer
)
694 const NetdevSocketOptions
*sock
;
696 assert(opts
->kind
== NET_CLIENT_OPTIONS_KIND_SOCKET
);
699 if (sock
->has_fd
+ sock
->has_listen
+ sock
->has_connect
+ sock
->has_mcast
+
700 sock
->has_udp
!= 1) {
701 error_report("exactly one of fd=, listen=, connect=, mcast= or udp="
706 if (sock
->has_localaddr
&& !sock
->has_mcast
&& !sock
->has_udp
) {
707 error_report("localaddr= is only valid with mcast= or udp=");
714 fd
= monitor_handle_fd_param(cur_mon
, sock
->fd
);
715 if (fd
== -1 || !net_socket_fd_init(peer
, "socket", name
, fd
, 1)) {
721 if (sock
->has_listen
) {
722 if (net_socket_listen_init(peer
, "socket", name
, sock
->listen
) == -1) {
728 if (sock
->has_connect
) {
729 if (net_socket_connect_init(peer
, "socket", name
, sock
->connect
) ==
736 if (sock
->has_mcast
) {
737 /* if sock->localaddr is missing, it has been initialized to "all bits
739 if (net_socket_mcast_init(peer
, "socket", name
, sock
->mcast
,
740 sock
->localaddr
) == -1) {
746 assert(sock
->has_udp
);
747 if (!sock
->has_localaddr
) {
748 error_report("localaddr= is mandatory with udp=");
751 if (net_socket_udp_init(peer
, "udp", name
, sock
->udp
, sock
->localaddr
) ==