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"
29 #include "qemu-char.h"
30 #include "qemu-common.h"
31 #include "qemu-error.h"
32 #include "qemu-option.h"
33 #include "qemu_socket.h"
35 typedef struct NetSocketState
{
38 int state
; /* 0 = getting length, 1 = getting data */
40 unsigned int packet_len
;
42 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
45 typedef struct NetSocketListenState
{
50 } NetSocketListenState
;
52 /* XXX: we consider we can send the whole packet without blocking */
53 static ssize_t
net_socket_receive(VLANClientState
*nc
, const uint8_t *buf
, size_t size
)
55 NetSocketState
*s
= DO_UPCAST(NetSocketState
, nc
, nc
);
59 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
60 return send_all(s
->fd
, buf
, size
);
63 static ssize_t
net_socket_receive_dgram(VLANClientState
*nc
, const uint8_t *buf
, size_t size
)
65 NetSocketState
*s
= DO_UPCAST(NetSocketState
, nc
, nc
);
67 return sendto(s
->fd
, (const void *)buf
, size
, 0,
68 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
71 static void net_socket_send(void *opaque
)
73 NetSocketState
*s
= opaque
;
79 size
= qemu_recv(s
->fd
, buf1
, sizeof(buf1
), 0);
82 if (err
!= EWOULDBLOCK
)
84 } else if (size
== 0) {
85 /* end of connection */
87 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
93 /* reassemble a packet from the network */
99 memcpy(s
->buf
+ s
->index
, buf
, l
);
105 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
111 l
= s
->packet_len
- s
->index
;
114 if (s
->index
+ l
<= sizeof(s
->buf
)) {
115 memcpy(s
->buf
+ s
->index
, buf
, l
);
117 fprintf(stderr
, "serious error: oversized packet received,"
118 "connection terminated.\n");
126 if (s
->index
>= s
->packet_len
) {
127 qemu_send_packet(&s
->nc
, s
->buf
, s
->packet_len
);
136 static void net_socket_send_dgram(void *opaque
)
138 NetSocketState
*s
= opaque
;
141 size
= qemu_recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
145 /* end of connection */
146 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
149 qemu_send_packet(&s
->nc
, s
->buf
, size
);
152 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
, struct in_addr
*localaddr
)
163 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
164 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) "
165 "does not contain a multicast address\n",
166 inet_ntoa(mcastaddr
->sin_addr
),
167 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
171 fd
= qemu_socket(PF_INET
, SOCK_DGRAM
, 0);
173 perror("socket(PF_INET, SOCK_DGRAM)");
178 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
179 (const char *)&val
, sizeof(val
));
181 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
185 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
191 /* Add host to multicast group */
192 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
194 imr
.imr_interface
= *localaddr
;
196 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
199 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
200 (const char *)&imr
, sizeof(struct ip_mreq
));
202 perror("setsockopt(IP_ADD_MEMBERSHIP)");
206 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
208 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
209 (const char *)&loop
, sizeof(loop
));
211 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
215 /* If a bind address is given, only send packets from that address */
216 if (localaddr
!= NULL
) {
217 ret
= setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_IF
,
218 (const char *)localaddr
, sizeof(*localaddr
));
220 perror("setsockopt(IP_MULTICAST_IF)");
225 socket_set_nonblock(fd
);
233 static void net_socket_cleanup(VLANClientState
*nc
)
235 NetSocketState
*s
= DO_UPCAST(NetSocketState
, nc
, nc
);
236 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
240 static NetClientInfo net_dgram_socket_info
= {
241 .type
= NET_CLIENT_TYPE_SOCKET
,
242 .size
= sizeof(NetSocketState
),
243 .receive
= net_socket_receive_dgram
,
244 .cleanup
= net_socket_cleanup
,
247 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
,
250 int fd
, int is_connected
)
252 struct sockaddr_in saddr
;
258 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
259 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
260 * by ONLY ONE process: we must "clone" this dgram socket --jjo
264 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
266 if (saddr
.sin_addr
.s_addr
== 0) {
267 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, "
268 "cannot setup multicast dst addr\n", fd
);
271 /* clone dgram socket */
272 newfd
= net_socket_mcast_create(&saddr
, NULL
);
274 /* error already reported by net_socket_mcast_create() */
277 /* clone newfd to fd, close newfd */
283 "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
284 fd
, strerror(errno
));
289 nc
= qemu_new_net_client(&net_dgram_socket_info
, vlan
, NULL
, model
, name
);
291 snprintf(nc
->info_str
, sizeof(nc
->info_str
),
292 "socket: fd=%d (%s mcast=%s:%d)",
293 fd
, is_connected
? "cloned" : "",
294 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
296 s
= DO_UPCAST(NetSocketState
, nc
, nc
);
300 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
302 /* mcast: save bound address as dst */
303 if (is_connected
) s
->dgram_dst
=saddr
;
312 static void net_socket_connect(void *opaque
)
314 NetSocketState
*s
= opaque
;
315 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
318 static NetClientInfo net_socket_info
= {
319 .type
= NET_CLIENT_TYPE_SOCKET
,
320 .size
= sizeof(NetSocketState
),
321 .receive
= net_socket_receive
,
322 .cleanup
= net_socket_cleanup
,
325 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
,
328 int fd
, int is_connected
)
333 nc
= qemu_new_net_client(&net_socket_info
, vlan
, NULL
, model
, name
);
335 snprintf(nc
->info_str
, sizeof(nc
->info_str
), "socket: fd=%d", fd
);
337 s
= DO_UPCAST(NetSocketState
, nc
, nc
);
342 net_socket_connect(s
);
344 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
349 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
,
350 const char *model
, const char *name
,
351 int fd
, int is_connected
)
353 int so_type
= -1, optlen
=sizeof(so_type
);
355 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
,
356 (socklen_t
*)&optlen
)< 0) {
357 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n",
364 return net_socket_fd_init_dgram(vlan
, model
, name
, fd
, is_connected
);
366 return net_socket_fd_init_stream(vlan
, model
, name
, fd
, is_connected
);
368 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
369 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
370 return net_socket_fd_init_stream(vlan
, model
, name
, fd
, is_connected
);
375 static void net_socket_accept(void *opaque
)
377 NetSocketListenState
*s
= opaque
;
379 struct sockaddr_in saddr
;
385 fd
= qemu_accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
386 if (fd
< 0 && errno
!= EINTR
) {
388 } else if (fd
>= 0) {
392 s1
= net_socket_fd_init(s
->vlan
, s
->model
, s
->name
, fd
, 1);
394 snprintf(s1
->nc
.info_str
, sizeof(s1
->nc
.info_str
),
395 "socket: connection from %s:%d",
396 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
400 static int net_socket_listen_init(VLANState
*vlan
,
403 const char *host_str
)
405 NetSocketListenState
*s
;
407 struct sockaddr_in saddr
;
409 if (parse_host_port(&saddr
, host_str
) < 0)
412 s
= g_malloc0(sizeof(NetSocketListenState
));
414 fd
= qemu_socket(PF_INET
, SOCK_STREAM
, 0);
420 socket_set_nonblock(fd
);
422 /* allow fast reuse */
424 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
426 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
441 s
->model
= g_strdup(model
);
442 s
->name
= name
? g_strdup(name
) : NULL
;
444 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
448 static int net_socket_connect_init(VLANState
*vlan
,
451 const char *host_str
)
454 int fd
, connected
, ret
, err
;
455 struct sockaddr_in saddr
;
457 if (parse_host_port(&saddr
, host_str
) < 0)
460 fd
= qemu_socket(PF_INET
, SOCK_STREAM
, 0);
465 socket_set_nonblock(fd
);
469 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
471 err
= socket_error();
472 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
473 } else if (err
== EINPROGRESS
) {
476 } else if (err
== WSAEALREADY
|| err
== WSAEINVAL
) {
489 s
= net_socket_fd_init(vlan
, model
, name
, fd
, connected
);
492 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
493 "socket: connect to %s:%d",
494 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
498 static int net_socket_mcast_init(VLANState
*vlan
,
501 const char *host_str
,
502 const char *localaddr_str
)
506 struct sockaddr_in saddr
;
507 struct in_addr localaddr
, *param_localaddr
;
509 if (parse_host_port(&saddr
, host_str
) < 0)
512 if (localaddr_str
!= NULL
) {
513 if (inet_aton(localaddr_str
, &localaddr
) == 0)
515 param_localaddr
= &localaddr
;
517 param_localaddr
= NULL
;
520 fd
= net_socket_mcast_create(&saddr
, param_localaddr
);
524 s
= net_socket_fd_init(vlan
, model
, name
, fd
, 0);
528 s
->dgram_dst
= saddr
;
530 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
531 "socket: mcast=%s:%d",
532 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
537 static int net_socket_udp_init(VLANState
*vlan
,
545 struct sockaddr_in laddr
, raddr
;
547 if (parse_host_port(&laddr
, lhost
) < 0) {
551 if (parse_host_port(&raddr
, rhost
) < 0) {
555 fd
= qemu_socket(PF_INET
, SOCK_DGRAM
, 0);
557 perror("socket(PF_INET, SOCK_DGRAM)");
561 ret
= setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
562 (const char *)&val
, sizeof(val
));
564 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
568 ret
= bind(fd
, (struct sockaddr
*)&laddr
, sizeof(laddr
));
575 s
= net_socket_fd_init(vlan
, model
, name
, fd
, 0);
580 s
->dgram_dst
= raddr
;
582 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
584 inet_ntoa(raddr
.sin_addr
), ntohs(raddr
.sin_port
));
588 int net_init_socket(QemuOpts
*opts
,
593 if (qemu_opt_get(opts
, "fd")) {
596 if (qemu_opt_get(opts
, "listen") ||
597 qemu_opt_get(opts
, "connect") ||
598 qemu_opt_get(opts
, "mcast") ||
599 qemu_opt_get(opts
, "localaddr")) {
600 error_report("listen=, connect=, mcast= and localaddr= is invalid with fd=");
604 fd
= net_handle_fd_param(mon
, qemu_opt_get(opts
, "fd"));
609 if (!net_socket_fd_init(vlan
, "socket", name
, fd
, 1)) {
612 } else if (qemu_opt_get(opts
, "listen")) {
615 if (qemu_opt_get(opts
, "fd") ||
616 qemu_opt_get(opts
, "connect") ||
617 qemu_opt_get(opts
, "mcast") ||
618 qemu_opt_get(opts
, "localaddr")) {
619 error_report("fd=, connect=, mcast= and localaddr= is invalid with listen=");
623 listen
= qemu_opt_get(opts
, "listen");
625 if (net_socket_listen_init(vlan
, "socket", name
, listen
) == -1) {
628 } else if (qemu_opt_get(opts
, "connect")) {
631 if (qemu_opt_get(opts
, "fd") ||
632 qemu_opt_get(opts
, "listen") ||
633 qemu_opt_get(opts
, "mcast") ||
634 qemu_opt_get(opts
, "localaddr")) {
635 error_report("fd=, listen=, mcast= and localaddr= is invalid with connect=");
639 connect
= qemu_opt_get(opts
, "connect");
641 if (net_socket_connect_init(vlan
, "socket", name
, connect
) == -1) {
644 } else if (qemu_opt_get(opts
, "mcast")) {
645 const char *mcast
, *localaddr
;
647 if (qemu_opt_get(opts
, "fd") ||
648 qemu_opt_get(opts
, "connect") ||
649 qemu_opt_get(opts
, "listen")) {
650 error_report("fd=, connect= and listen= is invalid with mcast=");
654 mcast
= qemu_opt_get(opts
, "mcast");
655 localaddr
= qemu_opt_get(opts
, "localaddr");
657 if (net_socket_mcast_init(vlan
, "socket", name
, mcast
, localaddr
) == -1) {
660 } else if (qemu_opt_get(opts
, "udp")) {
661 const char *udp
, *localaddr
;
663 if (qemu_opt_get(opts
, "fd") ||
664 qemu_opt_get(opts
, "connect") ||
665 qemu_opt_get(opts
, "listen") ||
666 qemu_opt_get(opts
, "mcast")) {
667 error_report("fd=, connect=, listen="
668 " and mcast= is invalid with udp=");
672 udp
= qemu_opt_get(opts
, "udp");
673 localaddr
= qemu_opt_get(opts
, "localaddr");
674 if (localaddr
== NULL
) {
675 error_report("localaddr= is mandatory with udp=");
679 if (net_socket_udp_init(vlan
, "udp", name
, udp
, localaddr
) == -1) {
683 error_report("-socket requires fd=, listen=,"
684 " connect=, mcast= or udp=");