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) */
47 static void net_socket_accept(void *opaque);
49 /* XXX: we consider we can send the whole packet without blocking */
50 static ssize_t net_socket_receive(NetClientState *nc, const uint8_t *buf, size_t size)
52 NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
56 send_all(s->fd, (const uint8_t *)&len, sizeof(len));
57 return send_all(s->fd, buf, size);
60 static ssize_t net_socket_receive_dgram(NetClientState *nc, const uint8_t *buf, size_t size)
62 NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
64 return sendto(s->fd, (const void *)buf, size, 0,
65 (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst));
68 static void net_socket_send(void *opaque)
70 NetSocketState *s = opaque;
76 size = qemu_recv(s->fd, buf1, sizeof(buf1), 0);
79 if (err != EWOULDBLOCK)
81 } else if (size == 0) {
82 /* end of connection */
84 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
85 if (s->listen_fd != -1) {
86 qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
94 s->nc.link_down = true;
95 memset(s->buf, 0, sizeof(s->buf));
96 memset(s->nc.info_str, 0, sizeof(s->nc.info_str));
102 /* reassemble a packet from the network */
108 memcpy(s->buf + s->index, buf, l);
114 s->packet_len = ntohl(*(uint32_t *)s->buf);
120 l = s->packet_len - s->index;
123 if (s->index + l <= sizeof(s->buf)) {
124 memcpy(s->buf + s->index, buf, l);
126 fprintf(stderr, "serious error: oversized packet received,"
127 "connection terminated.\n");
135 if (s->index >= s->packet_len) {
136 qemu_send_packet(&s->nc, s->buf, s->packet_len);
145 static void net_socket_send_dgram(void *opaque)
147 NetSocketState *s = opaque;
150 size = qemu_recv(s->fd, s->buf, sizeof(s->buf), 0);
154 /* end of connection */
155 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
158 qemu_send_packet(&s->nc, s->buf, size);
161 static int net_socket_mcast_create(struct sockaddr_in *mcastaddr, struct in_addr *localaddr)
172 if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
173 fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) "
174 "does not contain a multicast address\n",
175 inet_ntoa(mcastaddr->sin_addr),
176 (int)ntohl(mcastaddr->sin_addr.s_addr));
180 fd = qemu_socket(PF_INET, SOCK_DGRAM, 0);
182 perror("socket(PF_INET, SOCK_DGRAM)");
187 ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
188 (const char *)&val, sizeof(val));
190 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
194 ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
200 /* Add host to multicast group */
201 imr.imr_multiaddr = mcastaddr->sin_addr;
203 imr.imr_interface = *localaddr;
205 imr.imr_interface.s_addr = htonl(INADDR_ANY);
208 ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
209 (const char *)&imr, sizeof(struct ip_mreq));
211 perror("setsockopt(IP_ADD_MEMBERSHIP)");
215 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
217 ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
218 (const char *)&loop, sizeof(loop));
220 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
224 /* If a bind address is given, only send packets from that address */
225 if (localaddr != NULL) {
226 ret = setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF,
227 (const char *)localaddr, sizeof(*localaddr));
229 perror("setsockopt(IP_MULTICAST_IF)");
234 socket_set_nonblock(fd);
242 static void net_socket_cleanup(NetClientState *nc)
244 NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
246 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
250 if (s->listen_fd != -1) {
251 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
252 closesocket(s->listen_fd);
257 static NetClientInfo net_dgram_socket_info = {
258 .type = NET_CLIENT_OPTIONS_KIND_SOCKET,
259 .size = sizeof(NetSocketState),
260 .receive = net_socket_receive_dgram,
261 .cleanup = net_socket_cleanup,
264 static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,
267 int fd, int is_connected)
269 struct sockaddr_in saddr;
275 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
276 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
277 * by ONLY ONE process: we must "clone" this dgram socket --jjo
281 if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) {
283 if (saddr.sin_addr.s_addr == 0) {
284 fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, "
285 "cannot setup multicast dst addr\n", fd);
288 /* clone dgram socket */
289 newfd = net_socket_mcast_create(&saddr, NULL);
291 /* error already reported by net_socket_mcast_create() */
294 /* clone newfd to fd, close newfd */
300 "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
301 fd, strerror(errno));
306 nc = qemu_new_net_client(&net_dgram_socket_info, peer, model, name);
308 snprintf(nc->info_str, sizeof(nc->info_str),
309 "socket: fd=%d (%s mcast=%s:%d)",
310 fd, is_connected ? "cloned" : "",
311 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
313 s = DO_UPCAST(NetSocketState, nc, nc);
318 qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
320 /* mcast: save bound address as dst */
322 s->dgram_dst = saddr;
332 static void net_socket_connect(void *opaque)
334 NetSocketState *s = opaque;
335 qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
338 static NetClientInfo net_socket_info = {
339 .type = NET_CLIENT_OPTIONS_KIND_SOCKET,
340 .size = sizeof(NetSocketState),
341 .receive = net_socket_receive,
342 .cleanup = net_socket_cleanup,
345 static NetSocketState *net_socket_fd_init_stream(NetClientState *peer,
348 int fd, int is_connected)
353 nc = qemu_new_net_client(&net_socket_info, peer, model, name);
355 snprintf(nc->info_str, sizeof(nc->info_str), "socket: fd=%d", fd);
357 s = DO_UPCAST(NetSocketState, nc, nc);
363 net_socket_connect(s);
365 qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
370 static NetSocketState *net_socket_fd_init(NetClientState *peer,
371 const char *model, const char *name,
372 int fd, int is_connected)
374 int so_type = -1, optlen=sizeof(so_type);
376 if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type,
377 (socklen_t *)&optlen)< 0) {
378 fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n",
385 return net_socket_fd_init_dgram(peer, model, name, fd, is_connected);
387 return net_socket_fd_init_stream(peer, model, name, fd, is_connected);
389 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
390 fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
391 return net_socket_fd_init_stream(peer, model, name, fd, is_connected);
396 static void net_socket_accept(void *opaque)
398 NetSocketState *s = opaque;
399 struct sockaddr_in saddr;
405 fd = qemu_accept(s->listen_fd, (struct sockaddr *)&saddr, &len);
406 if (fd < 0 && errno != EINTR) {
408 } else if (fd >= 0) {
409 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
415 s->nc.link_down = false;
416 net_socket_connect(s);
417 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
418 "socket: connection from %s:%d",
419 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
422 static int net_socket_listen_init(NetClientState *peer,
425 const char *host_str)
429 struct sockaddr_in saddr;
432 if (parse_host_port(&saddr, host_str) < 0)
435 fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
440 socket_set_nonblock(fd);
442 /* allow fast reuse */
444 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
446 ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
459 nc = qemu_new_net_client(&net_socket_info, peer, model, name);
460 s = DO_UPCAST(NetSocketState, nc, nc);
463 s->nc.link_down = true;
465 qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
469 static int net_socket_connect_init(NetClientState *peer,
472 const char *host_str)
475 int fd, connected, ret, err;
476 struct sockaddr_in saddr;
478 if (parse_host_port(&saddr, host_str) < 0)
481 fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
486 socket_set_nonblock(fd);
490 ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
492 err = socket_error();
493 if (err == EINTR || err == EWOULDBLOCK) {
494 } else if (err == EINPROGRESS) {
497 } else if (err == WSAEALREADY || err == WSAEINVAL) {
510 s = net_socket_fd_init(peer, model, name, fd, connected);
513 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
514 "socket: connect to %s:%d",
515 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
519 static int net_socket_mcast_init(NetClientState *peer,
522 const char *host_str,
523 const char *localaddr_str)
527 struct sockaddr_in saddr;
528 struct in_addr localaddr, *param_localaddr;
530 if (parse_host_port(&saddr, host_str) < 0)
533 if (localaddr_str != NULL) {
534 if (inet_aton(localaddr_str, &localaddr) == 0)
536 param_localaddr = &localaddr;
538 param_localaddr = NULL;
541 fd = net_socket_mcast_create(&saddr, param_localaddr);
545 s = net_socket_fd_init(peer, model, name, fd, 0);
549 s->dgram_dst = saddr;
551 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
552 "socket: mcast=%s:%d",
553 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
558 static int net_socket_udp_init(NetClientState *peer,
566 struct sockaddr_in laddr, raddr;
568 if (parse_host_port(&laddr, lhost) < 0) {
572 if (parse_host_port(&raddr, rhost) < 0) {
576 fd = qemu_socket(PF_INET, SOCK_DGRAM, 0);
578 perror("socket(PF_INET, SOCK_DGRAM)");
582 ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
583 (const char *)&val, sizeof(val));
585 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
589 ret = bind(fd, (struct sockaddr *)&laddr, sizeof(laddr));
596 s = net_socket_fd_init(peer, model, name, fd, 0);
601 s->dgram_dst = raddr;
603 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
605 inet_ntoa(raddr.sin_addr), ntohs(raddr.sin_port));
609 int net_init_socket(const NetClientOptions *opts, const char *name,
610 NetClientState *peer)
612 const NetdevSocketOptions *sock;
614 assert(opts->kind == NET_CLIENT_OPTIONS_KIND_SOCKET);
617 if (sock->has_fd + sock->has_listen + sock->has_connect + sock->has_mcast +
618 sock->has_udp != 1) {
619 error_report("exactly one of fd=, listen=, connect=, mcast= or udp="
624 if (sock->has_localaddr && !sock->has_mcast && !sock->has_udp) {
625 error_report("localaddr= is only valid with mcast= or udp=");
632 fd = net_handle_fd_param(cur_mon, sock->fd);
633 if (fd == -1 || !net_socket_fd_init(peer, "socket", name, fd, 1)) {
639 if (sock->has_listen) {
640 if (net_socket_listen_init(peer, "socket", name, sock->listen) == -1) {
646 if (sock->has_connect) {
647 if (net_socket_connect_init(peer, "socket", name, sock->connect) ==
654 if (sock->has_mcast) {
655 /* if sock->localaddr is missing, it has been initialized to "all bits
657 if (net_socket_mcast_init(peer, "socket", name, sock->mcast,
658 sock->localaddr) == -1) {
664 assert(sock->has_udp);
665 if (!sock->has_localaddr) {
666 error_report("localaddr= is mandatory with udp=");
669 if (net_socket_udp_init(peer, "udp", name, sock->udp, sock->localaddr) ==