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
{
39 int state
; /* 0 = getting length, 1 = getting data */
41 unsigned int packet_len
;
43 struct sockaddr_in dgram_dst
; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
46 typedef struct NetSocketListenState
{
51 } NetSocketListenState
;
53 /* XXX: we consider we can send the whole packet without blocking */
54 static ssize_t
net_socket_receive(VLANClientState
*nc
, const uint8_t *buf
, size_t size
)
56 NetSocketState
*s
= DO_UPCAST(NetSocketState
, nc
, nc
);
60 send_all(s
->fd
, (const uint8_t *)&len
, sizeof(len
));
61 return send_all(s
->fd
, buf
, size
);
64 static ssize_t
net_socket_receive_dgram(VLANClientState
*nc
, const uint8_t *buf
, size_t size
)
66 NetSocketState
*s
= DO_UPCAST(NetSocketState
, nc
, nc
);
68 return sendto(s
->fd
, (const void *)buf
, size
, 0,
69 (struct sockaddr
*)&s
->dgram_dst
, sizeof(s
->dgram_dst
));
72 static void net_socket_send(void *opaque
)
74 NetSocketState
*s
= opaque
;
80 size
= qemu_recv(s
->fd
, buf1
, sizeof(buf1
), 0);
83 if (err
!= EWOULDBLOCK
)
85 } else if (size
== 0) {
86 /* end of connection */
88 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
94 /* reassemble a packet from the network */
100 memcpy(s
->buf
+ s
->index
, buf
, l
);
106 s
->packet_len
= ntohl(*(uint32_t *)s
->buf
);
112 l
= s
->packet_len
- s
->index
;
115 if (s
->index
+ l
<= sizeof(s
->buf
)) {
116 memcpy(s
->buf
+ s
->index
, buf
, l
);
118 fprintf(stderr
, "serious error: oversized packet received,"
119 "connection terminated.\n");
127 if (s
->index
>= s
->packet_len
) {
128 qemu_send_packet(&s
->nc
, s
->buf
, s
->packet_len
);
137 static void net_socket_send_dgram(void *opaque
)
139 NetSocketState
*s
= opaque
;
142 size
= qemu_recv(s
->fd
, s
->buf
, sizeof(s
->buf
), 0);
146 /* end of connection */
147 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
150 qemu_send_packet(&s
->nc
, s
->buf
, size
);
153 static int net_socket_mcast_create(struct sockaddr_in
*mcastaddr
, struct in_addr
*localaddr
)
164 if (!IN_MULTICAST(ntohl(mcastaddr
->sin_addr
.s_addr
))) {
165 fprintf(stderr
, "qemu: error: specified mcastaddr \"%s\" (0x%08x) "
166 "does not contain a multicast address\n",
167 inet_ntoa(mcastaddr
->sin_addr
),
168 (int)ntohl(mcastaddr
->sin_addr
.s_addr
));
172 fd
= qemu_socket(PF_INET
, SOCK_DGRAM
, 0);
174 perror("socket(PF_INET, SOCK_DGRAM)");
179 ret
=setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
180 (const char *)&val
, sizeof(val
));
182 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
186 ret
= bind(fd
, (struct sockaddr
*)mcastaddr
, sizeof(*mcastaddr
));
192 /* Add host to multicast group */
193 imr
.imr_multiaddr
= mcastaddr
->sin_addr
;
195 imr
.imr_interface
= *localaddr
;
197 imr
.imr_interface
.s_addr
= htonl(INADDR_ANY
);
200 ret
= setsockopt(fd
, IPPROTO_IP
, IP_ADD_MEMBERSHIP
,
201 (const char *)&imr
, sizeof(struct ip_mreq
));
203 perror("setsockopt(IP_ADD_MEMBERSHIP)");
207 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
209 ret
=setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
,
210 (const char *)&loop
, sizeof(loop
));
212 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
216 /* If a bind address is given, only send packets from that address */
217 if (localaddr
!= NULL
) {
218 ret
= setsockopt(fd
, IPPROTO_IP
, IP_MULTICAST_IF
,
219 (const char *)localaddr
, sizeof(*localaddr
));
221 perror("setsockopt(IP_MULTICAST_IF)");
226 socket_set_nonblock(fd
);
234 static void net_socket_cleanup(VLANClientState
*nc
)
236 NetSocketState
*s
= DO_UPCAST(NetSocketState
, nc
, nc
);
237 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
241 static NetClientInfo net_dgram_socket_info
= {
242 .type
= NET_CLIENT_TYPE_SOCKET
,
243 .size
= sizeof(NetSocketState
),
244 .receive
= net_socket_receive_dgram
,
245 .cleanup
= net_socket_cleanup
,
248 static NetSocketState
*net_socket_fd_init_dgram(VLANState
*vlan
,
251 int fd
, int is_connected
)
253 struct sockaddr_in saddr
;
259 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
260 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
261 * by ONLY ONE process: we must "clone" this dgram socket --jjo
265 if (getsockname(fd
, (struct sockaddr
*) &saddr
, &saddr_len
) == 0) {
267 if (saddr
.sin_addr
.s_addr
== 0) {
268 fprintf(stderr
, "qemu: error: init_dgram: fd=%d unbound, "
269 "cannot setup multicast dst addr\n", fd
);
272 /* clone dgram socket */
273 newfd
= net_socket_mcast_create(&saddr
, NULL
);
275 /* error already reported by net_socket_mcast_create() */
278 /* clone newfd to fd, close newfd */
284 "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
285 fd
, strerror(errno
));
290 nc
= qemu_new_net_client(&net_dgram_socket_info
, vlan
, NULL
, model
, name
);
292 snprintf(nc
->info_str
, sizeof(nc
->info_str
),
293 "socket: fd=%d (%s mcast=%s:%d)",
294 fd
, is_connected
? "cloned" : "",
295 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
297 s
= DO_UPCAST(NetSocketState
, nc
, nc
);
301 qemu_set_fd_handler(s
->fd
, net_socket_send_dgram
, NULL
, s
);
303 /* mcast: save bound address as dst */
304 if (is_connected
) s
->dgram_dst
=saddr
;
313 static void net_socket_connect(void *opaque
)
315 NetSocketState
*s
= opaque
;
316 qemu_set_fd_handler(s
->fd
, net_socket_send
, NULL
, s
);
319 static NetClientInfo net_socket_info
= {
320 .type
= NET_CLIENT_TYPE_SOCKET
,
321 .size
= sizeof(NetSocketState
),
322 .receive
= net_socket_receive
,
323 .cleanup
= net_socket_cleanup
,
326 static NetSocketState
*net_socket_fd_init_stream(VLANState
*vlan
,
329 int fd
, int is_connected
)
334 nc
= qemu_new_net_client(&net_socket_info
, vlan
, NULL
, model
, name
);
336 snprintf(nc
->info_str
, sizeof(nc
->info_str
), "socket: fd=%d", fd
);
338 s
= DO_UPCAST(NetSocketState
, nc
, nc
);
343 net_socket_connect(s
);
345 qemu_set_fd_handler(s
->fd
, NULL
, net_socket_connect
, s
);
350 static NetSocketState
*net_socket_fd_init(VLANState
*vlan
,
351 const char *model
, const char *name
,
352 int fd
, int is_connected
)
354 int so_type
= -1, optlen
=sizeof(so_type
);
356 if(getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, (char *)&so_type
,
357 (socklen_t
*)&optlen
)< 0) {
358 fprintf(stderr
, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n",
365 return net_socket_fd_init_dgram(vlan
, model
, name
, fd
, is_connected
);
367 return net_socket_fd_init_stream(vlan
, model
, name
, fd
, is_connected
);
369 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
370 fprintf(stderr
, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type
, fd
);
371 return net_socket_fd_init_stream(vlan
, model
, name
, fd
, is_connected
);
376 static void net_socket_accept(void *opaque
)
378 NetSocketListenState
*s
= opaque
;
380 struct sockaddr_in saddr
;
386 fd
= qemu_accept(s
->fd
, (struct sockaddr
*)&saddr
, &len
);
387 if (fd
< 0 && errno
!= EINTR
) {
389 } else if (fd
>= 0) {
393 s1
= net_socket_fd_init(s
->vlan
, s
->model
, s
->name
, fd
, 1);
395 snprintf(s1
->nc
.info_str
, sizeof(s1
->nc
.info_str
),
396 "socket: connection from %s:%d",
397 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
401 static int net_socket_listen_init(VLANState
*vlan
,
404 const char *host_str
)
406 NetSocketListenState
*s
;
408 struct sockaddr_in saddr
;
410 if (parse_host_port(&saddr
, host_str
) < 0)
413 s
= g_malloc0(sizeof(NetSocketListenState
));
415 fd
= qemu_socket(PF_INET
, SOCK_STREAM
, 0);
421 socket_set_nonblock(fd
);
423 /* allow fast reuse */
425 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (const char *)&val
, sizeof(val
));
427 ret
= bind(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
442 s
->model
= g_strdup(model
);
443 s
->name
= name
? g_strdup(name
) : NULL
;
445 qemu_set_fd_handler(fd
, net_socket_accept
, NULL
, s
);
449 static int net_socket_connect_init(VLANState
*vlan
,
452 const char *host_str
)
455 int fd
, connected
, ret
, err
;
456 struct sockaddr_in saddr
;
458 if (parse_host_port(&saddr
, host_str
) < 0)
461 fd
= qemu_socket(PF_INET
, SOCK_STREAM
, 0);
466 socket_set_nonblock(fd
);
470 ret
= connect(fd
, (struct sockaddr
*)&saddr
, sizeof(saddr
));
472 err
= socket_error();
473 if (err
== EINTR
|| err
== EWOULDBLOCK
) {
474 } else if (err
== EINPROGRESS
) {
477 } else if (err
== WSAEALREADY
|| err
== WSAEINVAL
) {
490 s
= net_socket_fd_init(vlan
, model
, name
, fd
, connected
);
493 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
494 "socket: connect to %s:%d",
495 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
499 static int net_socket_mcast_init(VLANState
*vlan
,
502 const char *host_str
,
503 const char *localaddr_str
)
507 struct sockaddr_in saddr
;
508 struct in_addr localaddr
, *param_localaddr
;
510 if (parse_host_port(&saddr
, host_str
) < 0)
513 if (localaddr_str
!= NULL
) {
514 if (inet_aton(localaddr_str
, &localaddr
) == 0)
516 param_localaddr
= &localaddr
;
518 param_localaddr
= NULL
;
521 fd
= net_socket_mcast_create(&saddr
, param_localaddr
);
525 s
= net_socket_fd_init(vlan
, model
, name
, fd
, 0);
529 s
->dgram_dst
= saddr
;
531 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
532 "socket: mcast=%s:%d",
533 inet_ntoa(saddr
.sin_addr
), ntohs(saddr
.sin_port
));
538 static int net_socket_udp_init(VLANState
*vlan
,
546 struct sockaddr_in laddr
, raddr
;
548 if (parse_host_port(&laddr
, lhost
) < 0) {
552 if (parse_host_port(&raddr
, rhost
) < 0) {
556 fd
= qemu_socket(PF_INET
, SOCK_DGRAM
, 0);
558 perror("socket(PF_INET, SOCK_DGRAM)");
562 ret
= setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
,
563 (const char *)&val
, sizeof(val
));
565 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
569 ret
= bind(fd
, (struct sockaddr
*)&laddr
, sizeof(laddr
));
576 s
= net_socket_fd_init(vlan
, model
, name
, fd
, 0);
581 s
->dgram_dst
= raddr
;
583 snprintf(s
->nc
.info_str
, sizeof(s
->nc
.info_str
),
585 inet_ntoa(raddr
.sin_addr
), ntohs(raddr
.sin_port
));
589 int net_init_socket(QemuOpts
*opts
, const char *name
, VLANState
*vlan
)
591 if (qemu_opt_get(opts
, "fd")) {
594 if (qemu_opt_get(opts
, "listen") ||
595 qemu_opt_get(opts
, "connect") ||
596 qemu_opt_get(opts
, "mcast") ||
597 qemu_opt_get(opts
, "localaddr")) {
598 error_report("listen=, connect=, mcast= and localaddr= is invalid with fd=");
602 fd
= net_handle_fd_param(cur_mon
, qemu_opt_get(opts
, "fd"));
607 if (!net_socket_fd_init(vlan
, "socket", name
, fd
, 1)) {
610 } else if (qemu_opt_get(opts
, "listen")) {
613 if (qemu_opt_get(opts
, "fd") ||
614 qemu_opt_get(opts
, "connect") ||
615 qemu_opt_get(opts
, "mcast") ||
616 qemu_opt_get(opts
, "localaddr")) {
617 error_report("fd=, connect=, mcast= and localaddr= is invalid with listen=");
621 listen
= qemu_opt_get(opts
, "listen");
623 if (net_socket_listen_init(vlan
, "socket", name
, listen
) == -1) {
626 } else if (qemu_opt_get(opts
, "connect")) {
629 if (qemu_opt_get(opts
, "fd") ||
630 qemu_opt_get(opts
, "listen") ||
631 qemu_opt_get(opts
, "mcast") ||
632 qemu_opt_get(opts
, "localaddr")) {
633 error_report("fd=, listen=, mcast= and localaddr= is invalid with connect=");
637 connect
= qemu_opt_get(opts
, "connect");
639 if (net_socket_connect_init(vlan
, "socket", name
, connect
) == -1) {
642 } else if (qemu_opt_get(opts
, "mcast")) {
643 const char *mcast
, *localaddr
;
645 if (qemu_opt_get(opts
, "fd") ||
646 qemu_opt_get(opts
, "connect") ||
647 qemu_opt_get(opts
, "listen")) {
648 error_report("fd=, connect= and listen= is invalid with mcast=");
652 mcast
= qemu_opt_get(opts
, "mcast");
653 localaddr
= qemu_opt_get(opts
, "localaddr");
655 if (net_socket_mcast_init(vlan
, "socket", name
, mcast
, localaddr
) == -1) {
658 } else if (qemu_opt_get(opts
, "udp")) {
659 const char *udp
, *localaddr
;
661 if (qemu_opt_get(opts
, "fd") ||
662 qemu_opt_get(opts
, "connect") ||
663 qemu_opt_get(opts
, "listen") ||
664 qemu_opt_get(opts
, "mcast")) {
665 error_report("fd=, connect=, listen="
666 " and mcast= is invalid with udp=");
670 udp
= qemu_opt_get(opts
, "udp");
671 localaddr
= qemu_opt_get(opts
, "localaddr");
672 if (localaddr
== NULL
) {
673 error_report("localaddr= is mandatory with udp=");
677 if (net_socket_udp_init(vlan
, "udp", name
, udp
, localaddr
) == -1) {
681 error_report("-socket requires fd=, listen=,"
682 " connect=, mcast= or udp=");