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"
36 typedef struct NetSocketState
{
40 int state
; /* 0 = getting length, 1 = getting data */
42 unsigned int packet_len
;
44 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
45 IOHandler
*send_fn
; /* differs between SOCK_STREAM/SOCK_DGRAM */
46 bool read_poll
; /* waiting to receive data? */
47 bool write_poll
; /* waiting to transmit data? */
50 static void net_socket_accept(void *opaque
);
51 static void net_socket_writable(void *opaque
);
53 /* Only read packets from socket when peer can receive them */
54 static int net_socket_can_send(void *opaque
)
56 NetSocketState
*s
= opaque
;
58 return qemu_can_send_packet(&s
->nc
);
61 static void net_socket_update_fd_handler(NetSocketState
*s
)
63 qemu_set_fd_handler2(s
->fd
,
64 s
->read_poll
? net_socket_can_send
: NULL
,
65 s
->read_poll
? s
->send_fn
: NULL
,
66 s
->write_poll
? net_socket_writable
: NULL
,
70 static void net_socket_read_poll(NetSocketState
*s
, bool enable
)
72 s
->read_poll
= enable
;
73 net_socket_update_fd_handler(s
);
76 static void net_socket_write_poll(NetSocketState
*s
, bool enable
)
78 s
->write_poll
= enable
;
79 net_socket_update_fd_handler(s
);
82 static void net_socket_writable(void *opaque
)
84 NetSocketState
*s
= opaque
;
86 net_socket_write_poll(s
, false);
88 qemu_flush_queued_packets(&s
->nc
);
91 /* XXX: we consider we can send the whole packet without blocking */
92 static ssize_t
net_socket_receive(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
94 NetSocketState
*s
= DO_UPCAST(NetSocketState
, nc
, nc
);
98 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
99 return send_all(s
->fd
, buf
, size
);
102 static ssize_t
net_socket_receive_dgram(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
104 NetSocketState
*s
= DO_UPCAST(NetSocketState
, nc
, nc
);
108 ret
= sendto(s
->fd
, buf
, size
, 0,
109 (struct sockaddr
*)&s
->dgram_dst
,
110 sizeof(s
->dgram_dst
));
111 } while (ret
== -1 && errno
== EINTR
);
113 if (ret
== -1 && errno
== EAGAIN
) {
114 net_socket_write_poll(s
, true);
120 static void net_socket_send(void *opaque
)
122 NetSocketState
*s
= opaque
;
128 size
= qemu_recv(s
->fd
, buf1
, sizeof(buf1
), 0);
130 err
= socket_error();
131 if (err
!= EWOULDBLOCK
)
133 } else if (size
== 0) {
134 /* end of connection */
136 net_socket_read_poll(s
, false);
137 net_socket_write_poll(s
, false);
138 if (s
->listen_fd
!= -1) {
139 qemu_set_fd_handler(s
->listen_fd
, net_socket_accept
, NULL
, s
);
147 s
->nc
.link_down
= true;
148 memset(s
->buf
, 0, sizeof(s
->buf
));
149 memset(s
->nc
.info_str
, 0, sizeof(s
->nc
.info_str
));
155 /* reassemble a packet from the network */
161 memcpy(s
->buf
+ s
->index
, buf
, l
);
167 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
173 l
= s
->packet_len
- s
->index
;
176 if (s
->index
+ l
<= sizeof(s
->buf
)) {
177 memcpy(s
->buf
+ s
->index
, buf
, l
);
179 fprintf(stderr
, "serious error: oversized packet received,"
180 "connection terminated.\n");
188 if (s
->index
>= s
->packet_len
) {
189 qemu_send_packet(&s
->nc
, s
->buf
, s
->packet_len
);
198 static void net_socket_send_dgram(void *opaque
)
200 NetSocketState
*s
= opaque
;
203 size
= qemu_recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
207 /* end of connection */
208 net_socket_read_poll(s
, false);
209 net_socket_write_poll(s
, false);
212 qemu_send_packet(&s
->nc
, s
->buf
, size
);
215 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
, struct in_addr
*localaddr
)
226 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
227 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) "
228 "does not contain a multicast address\n",
229 inet_ntoa(mcastaddr
->sin_addr
),
230 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
234 fd
= qemu_socket(PF_INET
, SOCK_DGRAM
, 0);
236 perror("socket(PF_INET, SOCK_DGRAM)");
241 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
242 (const char *)&val
, sizeof(val
));
244 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
248 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
254 /* Add host to multicast group */
255 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
257 imr
.imr_interface
= *localaddr
;
259 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
262 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
263 (const char *)&imr
, sizeof(struct ip_mreq
));
265 perror("setsockopt(IP_ADD_MEMBERSHIP)");
269 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
271 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
272 (const char *)&loop
, sizeof(loop
));
274 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
278 /* If a bind address is given, only send packets from that address */
279 if (localaddr
!= NULL
) {
280 ret
= setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_IF
,
281 (const char *)localaddr
, sizeof(*localaddr
));
283 perror("setsockopt(IP_MULTICAST_IF)");
288 socket_set_nonblock(fd
);
296 static void net_socket_cleanup(NetClientState
*nc
)
298 NetSocketState
*s
= DO_UPCAST(NetSocketState
, nc
, nc
);
300 net_socket_read_poll(s
, false);
301 net_socket_write_poll(s
, false);
305 if (s
->listen_fd
!= -1) {
306 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
307 closesocket(s
->listen_fd
);
312 static NetClientInfo net_dgram_socket_info
= {
313 .type
= NET_CLIENT_OPTIONS_KIND_SOCKET
,
314 .size
= sizeof(NetSocketState
),
315 .receive
= net_socket_receive_dgram
,
316 .cleanup
= net_socket_cleanup
,
319 static NetSocketState
*net_socket_fd_init_dgram(NetClientState
*peer
,
322 int fd
, int is_connected
)
324 struct sockaddr_in saddr
;
330 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
331 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
332 * by ONLY ONE process: we must "clone" this dgram socket --jjo
336 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
338 if (saddr
.sin_addr
.s_addr
== 0) {
339 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, "
340 "cannot setup multicast dst addr\n", fd
);
343 /* clone dgram socket */
344 newfd
= net_socket_mcast_create(&saddr
, NULL
);
346 /* error already reported by net_socket_mcast_create() */
349 /* clone newfd to fd, close newfd */
355 "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
356 fd
, strerror(errno
));
361 nc
= qemu_new_net_client(&net_dgram_socket_info
, peer
, model
, name
);
363 snprintf(nc
->info_str
, sizeof(nc
->info_str
),
364 "socket: fd=%d (%s mcast=%s:%d)",
365 fd
, is_connected
? "cloned" : "",
366 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
368 s
= DO_UPCAST(NetSocketState
, nc
, nc
);
372 s
->send_fn
= net_socket_send_dgram
;
373 net_socket_read_poll(s
, true);
375 /* mcast: save bound address as dst */
377 s
->dgram_dst
= saddr
;
387 static void net_socket_connect(void *opaque
)
389 NetSocketState
*s
= opaque
;
390 s
->send_fn
= net_socket_send
;
391 net_socket_read_poll(s
, true);
394 static NetClientInfo net_socket_info
= {
395 .type
= NET_CLIENT_OPTIONS_KIND_SOCKET
,
396 .size
= sizeof(NetSocketState
),
397 .receive
= net_socket_receive
,
398 .cleanup
= net_socket_cleanup
,
401 static NetSocketState
*net_socket_fd_init_stream(NetClientState
*peer
,
404 int fd
, int is_connected
)
409 nc
= qemu_new_net_client(&net_socket_info
, peer
, model
, name
);
411 snprintf(nc
->info_str
, sizeof(nc
->info_str
), "socket: fd=%d", fd
);
413 s
= DO_UPCAST(NetSocketState
, nc
, nc
);
419 net_socket_connect(s
);
421 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
426 static NetSocketState
*net_socket_fd_init(NetClientState
*peer
,
427 const char *model
, const char *name
,
428 int fd
, int is_connected
)
430 int so_type
= -1, optlen
=sizeof(so_type
);
432 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
,
433 (socklen_t
*)&optlen
)< 0) {
434 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n",
441 return net_socket_fd_init_dgram(peer
, model
, name
, fd
, is_connected
);
443 return net_socket_fd_init_stream(peer
, model
, name
, fd
, is_connected
);
445 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
446 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
447 return net_socket_fd_init_stream(peer
, model
, name
, fd
, is_connected
);
452 static void net_socket_accept(void *opaque
)
454 NetSocketState
*s
= opaque
;
455 struct sockaddr_in saddr
;
461 fd
= qemu_accept(s
->listen_fd
, (struct sockaddr
*)&saddr
, &len
);
462 if (fd
< 0 && errno
!= EINTR
) {
464 } else if (fd
>= 0) {
465 qemu_set_fd_handler(s
->listen_fd
, NULL
, NULL
, NULL
);
471 s
->nc
.link_down
= false;
472 net_socket_connect(s
);
473 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
474 "socket: connection from %s:%d",
475 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
478 static int net_socket_listen_init(NetClientState
*peer
,
481 const char *host_str
)
485 struct sockaddr_in saddr
;
488 if (parse_host_port(&saddr
, host_str
) < 0)
491 fd
= qemu_socket(PF_INET
, SOCK_STREAM
, 0);
496 socket_set_nonblock(fd
);
498 /* allow fast reuse */
500 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
502 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
515 nc
= qemu_new_net_client(&net_socket_info
, peer
, model
, name
);
516 s
= DO_UPCAST(NetSocketState
, nc
, nc
);
519 s
->nc
.link_down
= true;
521 qemu_set_fd_handler(s
->listen_fd
, net_socket_accept
, NULL
, s
);
525 static int net_socket_connect_init(NetClientState
*peer
,
528 const char *host_str
)
531 int fd
, connected
, ret
, err
;
532 struct sockaddr_in saddr
;
534 if (parse_host_port(&saddr
, host_str
) < 0)
537 fd
= qemu_socket(PF_INET
, SOCK_STREAM
, 0);
542 socket_set_nonblock(fd
);
546 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
548 err
= socket_error();
549 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
550 } else if (err
== EINPROGRESS
) {
553 } else if (err
== WSAEALREADY
|| err
== WSAEINVAL
) {
566 s
= net_socket_fd_init(peer
, model
, name
, fd
, connected
);
569 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
570 "socket: connect to %s:%d",
571 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
575 static int net_socket_mcast_init(NetClientState
*peer
,
578 const char *host_str
,
579 const char *localaddr_str
)
583 struct sockaddr_in saddr
;
584 struct in_addr localaddr
, *param_localaddr
;
586 if (parse_host_port(&saddr
, host_str
) < 0)
589 if (localaddr_str
!= NULL
) {
590 if (inet_aton(localaddr_str
, &localaddr
) == 0)
592 param_localaddr
= &localaddr
;
594 param_localaddr
= NULL
;
597 fd
= net_socket_mcast_create(&saddr
, param_localaddr
);
601 s
= net_socket_fd_init(peer
, model
, name
, fd
, 0);
605 s
->dgram_dst
= saddr
;
607 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
608 "socket: mcast=%s:%d",
609 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
614 static int net_socket_udp_init(NetClientState
*peer
,
622 struct sockaddr_in laddr
, raddr
;
624 if (parse_host_port(&laddr
, lhost
) < 0) {
628 if (parse_host_port(&raddr
, rhost
) < 0) {
632 fd
= qemu_socket(PF_INET
, SOCK_DGRAM
, 0);
634 perror("socket(PF_INET, SOCK_DGRAM)");
638 ret
= setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
639 (const char *)&val
, sizeof(val
));
641 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
645 ret
= bind(fd
, (struct sockaddr
*)&laddr
, sizeof(laddr
));
652 s
= net_socket_fd_init(peer
, model
, name
, fd
, 0);
657 s
->dgram_dst
= raddr
;
659 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
661 inet_ntoa(raddr
.sin_addr
), ntohs(raddr
.sin_port
));
665 int net_init_socket(const NetClientOptions
*opts
, const char *name
,
666 NetClientState
*peer
)
668 const NetdevSocketOptions
*sock
;
670 assert(opts
->kind
== NET_CLIENT_OPTIONS_KIND_SOCKET
);
673 if (sock
->has_fd
+ sock
->has_listen
+ sock
->has_connect
+ sock
->has_mcast
+
674 sock
->has_udp
!= 1) {
675 error_report("exactly one of fd=, listen=, connect=, mcast= or udp="
680 if (sock
->has_localaddr
&& !sock
->has_mcast
&& !sock
->has_udp
) {
681 error_report("localaddr= is only valid with mcast= or udp=");
688 fd
= monitor_handle_fd_param(cur_mon
, sock
->fd
);
689 if (fd
== -1 || !net_socket_fd_init(peer
, "socket", name
, fd
, 1)) {
695 if (sock
->has_listen
) {
696 if (net_socket_listen_init(peer
, "socket", name
, sock
->listen
) == -1) {
702 if (sock
->has_connect
) {
703 if (net_socket_connect_init(peer
, "socket", name
, sock
->connect
) ==
710 if (sock
->has_mcast
) {
711 /* if sock->localaddr is missing, it has been initialized to "all bits
713 if (net_socket_mcast_init(peer
, "socket", name
, sock
->mcast
,
714 sock
->localaddr
) == -1) {
720 assert(sock
->has_udp
);
721 if (!sock
->has_localaddr
) {
722 error_report("localaddr= is mandatory with udp=");
725 if (net_socket_udp_init(peer
, "udp", name
, sock
->udp
, sock
->localaddr
) ==