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-option.h"
32 #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 *vc, const uint8_t *buf, size_t size)
55 NetSocketState *s = vc->opaque;
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 *vc, const uint8_t *buf, size_t size)
65 NetSocketState *s = vc->opaque;
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 = recv(s->fd, (void *)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->vc, s->buf, s->packet_len);
136 static void net_socket_send_dgram(void *opaque)
138 NetSocketState *s = opaque;
141 size = recv(s->fd, (void *)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->vc, s->buf, size);
152 static int net_socket_mcast_create(struct sockaddr_in *mcastaddr)
157 if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
158 fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
159 inet_ntoa(mcastaddr->sin_addr),
160 (int)ntohl(mcastaddr->sin_addr.s_addr));
164 fd = socket(PF_INET, SOCK_DGRAM, 0);
166 perror("socket(PF_INET, SOCK_DGRAM)");
171 ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
172 (const char *)&val, sizeof(val));
174 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
178 ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
184 /* Add host to multicast group */
185 imr.imr_multiaddr = mcastaddr->sin_addr;
186 imr.imr_interface.s_addr = htonl(INADDR_ANY);
188 ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
189 (const char *)&imr, sizeof(struct ip_mreq));
191 perror("setsockopt(IP_ADD_MEMBERSHIP)");
195 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
197 ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
198 (const char *)&val, sizeof(val));
200 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
204 socket_set_nonblock(fd);
212 static void net_socket_cleanup(VLANClientState *vc)
214 NetSocketState *s = vc->opaque;
215 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
220 static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan,
223 int fd, int is_connected)
225 struct sockaddr_in saddr;
230 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
231 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
232 * by ONLY ONE process: we must "clone" this dgram socket --jjo
236 if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) {
238 if (saddr.sin_addr.s_addr==0) {
239 fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
243 /* clone dgram socket */
244 newfd = net_socket_mcast_create(&saddr);
246 /* error already reported by net_socket_mcast_create() */
250 /* clone newfd to fd, close newfd */
255 fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
256 fd, strerror(errno));
261 s = qemu_mallocz(sizeof(NetSocketState));
264 s->vc = qemu_new_vlan_client(NET_CLIENT_TYPE_SOCKET,
265 vlan, NULL, model, name, NULL,
266 net_socket_receive_dgram, NULL, NULL,
267 net_socket_cleanup, s);
268 qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
270 /* mcast: save bound address as dst */
271 if (is_connected) s->dgram_dst=saddr;
273 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
274 "socket: fd=%d (%s mcast=%s:%d)",
275 fd, is_connected? "cloned" : "",
276 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
280 static void net_socket_connect(void *opaque)
282 NetSocketState *s = opaque;
283 qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
286 static NetSocketState *net_socket_fd_init_stream(VLANState *vlan,
289 int fd, int is_connected)
292 s = qemu_mallocz(sizeof(NetSocketState));
294 s->vc = qemu_new_vlan_client(NET_CLIENT_TYPE_SOCKET,
295 vlan, NULL, model, name, NULL,
296 net_socket_receive, NULL, NULL,
297 net_socket_cleanup, s);
298 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
299 "socket: fd=%d", fd);
301 net_socket_connect(s);
303 qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
308 static NetSocketState *net_socket_fd_init(VLANState *vlan,
309 const char *model, const char *name,
310 int fd, int is_connected)
312 int so_type = -1, optlen=sizeof(so_type);
314 if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type,
315 (socklen_t *)&optlen)< 0) {
316 fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd);
321 return net_socket_fd_init_dgram(vlan, model, name, fd, is_connected);
323 return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
325 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
326 fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
327 return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
332 static void net_socket_accept(void *opaque)
334 NetSocketListenState *s = opaque;
336 struct sockaddr_in saddr;
342 fd = accept(s->fd, (struct sockaddr *)&saddr, &len);
343 if (fd < 0 && errno != EINTR) {
345 } else if (fd >= 0) {
349 s1 = net_socket_fd_init(s->vlan, s->model, s->name, fd, 1);
353 snprintf(s1->vc->info_str, sizeof(s1->vc->info_str),
354 "socket: connection from %s:%d",
355 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
359 static int net_socket_listen_init(VLANState *vlan,
362 const char *host_str)
364 NetSocketListenState *s;
366 struct sockaddr_in saddr;
368 if (parse_host_port(&saddr, host_str) < 0)
371 s = qemu_mallocz(sizeof(NetSocketListenState));
373 fd = socket(PF_INET, SOCK_STREAM, 0);
378 socket_set_nonblock(fd);
380 /* allow fast reuse */
382 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
384 ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
395 s->model = qemu_strdup(model);
396 s->name = name ? qemu_strdup(name) : NULL;
398 qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
402 static int net_socket_connect_init(VLANState *vlan,
405 const char *host_str)
408 int fd, connected, ret, err;
409 struct sockaddr_in saddr;
411 if (parse_host_port(&saddr, host_str) < 0)
414 fd = socket(PF_INET, SOCK_STREAM, 0);
419 socket_set_nonblock(fd);
423 ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
425 err = socket_error();
426 if (err == EINTR || err == EWOULDBLOCK) {
427 } else if (err == EINPROGRESS) {
430 } else if (err == WSAEALREADY) {
443 s = net_socket_fd_init(vlan, model, name, fd, connected);
446 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
447 "socket: connect to %s:%d",
448 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
452 static int net_socket_mcast_init(VLANState *vlan,
455 const char *host_str)
459 struct sockaddr_in saddr;
461 if (parse_host_port(&saddr, host_str) < 0)
465 fd = net_socket_mcast_create(&saddr);
469 s = net_socket_fd_init(vlan, model, name, fd, 0);
473 s->dgram_dst = saddr;
475 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
476 "socket: mcast=%s:%d",
477 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
482 int net_init_socket(QemuOpts *opts,
487 if (qemu_opt_get(opts, "fd")) {
490 if (qemu_opt_get(opts, "listen") ||
491 qemu_opt_get(opts, "connect") ||
492 qemu_opt_get(opts, "mcast")) {
493 qemu_error("listen=, connect= and mcast= is invalid with fd=\n");
497 fd = net_handle_fd_param(mon, qemu_opt_get(opts, "fd"));
502 if (!net_socket_fd_init(vlan, "socket", name, fd, 1)) {
506 } else if (qemu_opt_get(opts, "listen")) {
509 if (qemu_opt_get(opts, "fd") ||
510 qemu_opt_get(opts, "connect") ||
511 qemu_opt_get(opts, "mcast")) {
512 qemu_error("fd=, connect= and mcast= is invalid with listen=\n");
516 listen = qemu_opt_get(opts, "listen");
518 if (net_socket_listen_init(vlan, "socket", name, listen) == -1) {
521 } else if (qemu_opt_get(opts, "connect")) {
524 if (qemu_opt_get(opts, "fd") ||
525 qemu_opt_get(opts, "listen") ||
526 qemu_opt_get(opts, "mcast")) {
527 qemu_error("fd=, listen= and mcast= is invalid with connect=\n");
531 connect = qemu_opt_get(opts, "connect");
533 if (net_socket_connect_init(vlan, "socket", name, connect) == -1) {
536 } else if (qemu_opt_get(opts, "mcast")) {
539 if (qemu_opt_get(opts, "fd") ||
540 qemu_opt_get(opts, "connect") ||
541 qemu_opt_get(opts, "listen")) {
542 qemu_error("fd=, connect= and listen= is invalid with mcast=\n");
546 mcast = qemu_opt_get(opts, "mcast");
548 if (net_socket_mcast_init(vlan, "socket", name, mcast) == -1) {
552 qemu_error("-socket requires fd=, listen=, connect= or mcast=\n");
557 vlan->nb_host_devs++;