blob: 0f091645ed901e9c4d41c4405a483652e5a7f1b0 [file] [log] [blame]
Mark McLoughlin42281ac2009-11-25 18:48:56 +00001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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
22 * THE SOFTWARE.
23 */
24#include "net/socket.h"
25
26#include "config-host.h"
27
28#include "net.h"
29#include "qemu-char.h"
30#include "qemu-common.h"
Markus Armbruster2f792012010-02-18 16:24:31 +010031#include "qemu-error.h"
Mark McLoughlin42281ac2009-11-25 18:48:56 +000032#include "qemu-option.h"
33#include "qemu_socket.h"
Mark McLoughlin42281ac2009-11-25 18:48:56 +000034
35typedef struct NetSocketState {
Mark McLoughlin564f63e2009-11-25 18:49:08 +000036 VLANClientState nc;
Mark McLoughlin42281ac2009-11-25 18:48:56 +000037 int fd;
38 int state; /* 0 = getting length, 1 = getting data */
39 unsigned int index;
40 unsigned int packet_len;
41 uint8_t buf[4096];
42 struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
43} NetSocketState;
44
45typedef struct NetSocketListenState {
46 VLANState *vlan;
47 char *model;
48 char *name;
49 int fd;
50} NetSocketListenState;
51
52/* XXX: we consider we can send the whole packet without blocking */
Mark McLoughlin564f63e2009-11-25 18:49:08 +000053static ssize_t net_socket_receive(VLANClientState *nc, const uint8_t *buf, size_t size)
Mark McLoughlin42281ac2009-11-25 18:48:56 +000054{
Mark McLoughlin564f63e2009-11-25 18:49:08 +000055 NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
Mark McLoughlin42281ac2009-11-25 18:48:56 +000056 uint32_t len;
57 len = htonl(size);
58
59 send_all(s->fd, (const uint8_t *)&len, sizeof(len));
60 return send_all(s->fd, buf, size);
61}
62
Mark McLoughlin564f63e2009-11-25 18:49:08 +000063static ssize_t net_socket_receive_dgram(VLANClientState *nc, const uint8_t *buf, size_t size)
Mark McLoughlin42281ac2009-11-25 18:48:56 +000064{
Mark McLoughlin564f63e2009-11-25 18:49:08 +000065 NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
Mark McLoughlin42281ac2009-11-25 18:48:56 +000066
67 return sendto(s->fd, (const void *)buf, size, 0,
68 (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst));
69}
70
71static void net_socket_send(void *opaque)
72{
73 NetSocketState *s = opaque;
74 int size, err;
75 unsigned l;
76 uint8_t buf1[4096];
77 const uint8_t *buf;
78
Blue Swirl00aa0042011-07-23 20:04:29 +000079 size = qemu_recv(s->fd, buf1, sizeof(buf1), 0);
Mark McLoughlin42281ac2009-11-25 18:48:56 +000080 if (size < 0) {
81 err = socket_error();
82 if (err != EWOULDBLOCK)
83 goto eoc;
84 } else if (size == 0) {
85 /* end of connection */
86 eoc:
87 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
88 closesocket(s->fd);
89 return;
90 }
91 buf = buf1;
92 while (size > 0) {
93 /* reassemble a packet from the network */
94 switch(s->state) {
95 case 0:
96 l = 4 - s->index;
97 if (l > size)
98 l = size;
99 memcpy(s->buf + s->index, buf, l);
100 buf += l;
101 size -= l;
102 s->index += l;
103 if (s->index == 4) {
104 /* got length */
105 s->packet_len = ntohl(*(uint32_t *)s->buf);
106 s->index = 0;
107 s->state = 1;
108 }
109 break;
110 case 1:
111 l = s->packet_len - s->index;
112 if (l > size)
113 l = size;
114 if (s->index + l <= sizeof(s->buf)) {
115 memcpy(s->buf + s->index, buf, l);
116 } else {
117 fprintf(stderr, "serious error: oversized packet received,"
118 "connection terminated.\n");
119 s->state = 0;
120 goto eoc;
121 }
122
123 s->index += l;
124 buf += l;
125 size -= l;
126 if (s->index >= s->packet_len) {
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000127 qemu_send_packet(&s->nc, s->buf, s->packet_len);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000128 s->index = 0;
129 s->state = 0;
130 }
131 break;
132 }
133 }
134}
135
136static void net_socket_send_dgram(void *opaque)
137{
138 NetSocketState *s = opaque;
139 int size;
140
Blue Swirl00aa0042011-07-23 20:04:29 +0000141 size = qemu_recv(s->fd, s->buf, sizeof(s->buf), 0);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000142 if (size < 0)
143 return;
144 if (size == 0) {
145 /* end of connection */
146 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
147 return;
148 }
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000149 qemu_send_packet(&s->nc, s->buf, size);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000150}
151
Mike Ryan3a75e742010-12-01 11:16:47 -0800152static int net_socket_mcast_create(struct sockaddr_in *mcastaddr, struct in_addr *localaddr)
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000153{
154 struct ip_mreq imr;
155 int fd;
156 int val, ret;
Brad23ddf2b2011-08-07 11:06:43 +0000157#ifdef __OpenBSD__
158 unsigned char loop;
159#else
160 int loop;
161#endif
162
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000163 if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
164 fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
165 inet_ntoa(mcastaddr->sin_addr),
166 (int)ntohl(mcastaddr->sin_addr.s_addr));
167 return -1;
168
169 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100170 fd = qemu_socket(PF_INET, SOCK_DGRAM, 0);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000171 if (fd < 0) {
172 perror("socket(PF_INET, SOCK_DGRAM)");
173 return -1;
174 }
175
176 val = 1;
177 ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
178 (const char *)&val, sizeof(val));
179 if (ret < 0) {
180 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
181 goto fail;
182 }
183
184 ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
185 if (ret < 0) {
186 perror("bind");
187 goto fail;
188 }
189
190 /* Add host to multicast group */
191 imr.imr_multiaddr = mcastaddr->sin_addr;
Mike Ryan3a75e742010-12-01 11:16:47 -0800192 if (localaddr) {
193 imr.imr_interface = *localaddr;
194 } else {
195 imr.imr_interface.s_addr = htonl(INADDR_ANY);
196 }
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000197
198 ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
199 (const char *)&imr, sizeof(struct ip_mreq));
200 if (ret < 0) {
201 perror("setsockopt(IP_ADD_MEMBERSHIP)");
202 goto fail;
203 }
204
205 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
Brad23ddf2b2011-08-07 11:06:43 +0000206 loop = 1;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000207 ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
Brad23ddf2b2011-08-07 11:06:43 +0000208 (const char *)&loop, sizeof(loop));
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000209 if (ret < 0) {
210 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
211 goto fail;
212 }
213
Mike Ryan3a75e742010-12-01 11:16:47 -0800214 /* If a bind address is given, only send packets from that address */
215 if (localaddr != NULL) {
Blue Swirl4d22c6c2010-12-17 21:03:00 +0000216 ret = setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF,
217 (const char *)localaddr, sizeof(*localaddr));
Mike Ryan3a75e742010-12-01 11:16:47 -0800218 if (ret < 0) {
219 perror("setsockopt(IP_MULTICAST_IF)");
220 goto fail;
221 }
222 }
223
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000224 socket_set_nonblock(fd);
225 return fd;
226fail:
227 if (fd >= 0)
228 closesocket(fd);
229 return -1;
230}
231
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000232static void net_socket_cleanup(VLANClientState *nc)
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000233{
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000234 NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000235 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
236 close(s->fd);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000237}
238
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000239static NetClientInfo net_dgram_socket_info = {
240 .type = NET_CLIENT_TYPE_SOCKET,
241 .size = sizeof(NetSocketState),
242 .receive = net_socket_receive_dgram,
243 .cleanup = net_socket_cleanup,
244};
245
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000246static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan,
247 const char *model,
248 const char *name,
249 int fd, int is_connected)
250{
251 struct sockaddr_in saddr;
252 int newfd;
253 socklen_t saddr_len;
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000254 VLANClientState *nc;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000255 NetSocketState *s;
256
257 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
258 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
259 * by ONLY ONE process: we must "clone" this dgram socket --jjo
260 */
261
262 if (is_connected) {
263 if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) {
264 /* must be bound */
265 if (saddr.sin_addr.s_addr==0) {
266 fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
267 fd);
268 return NULL;
269 }
270 /* clone dgram socket */
Mike Ryan3a75e742010-12-01 11:16:47 -0800271 newfd = net_socket_mcast_create(&saddr, NULL);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000272 if (newfd < 0) {
273 /* error already reported by net_socket_mcast_create() */
274 close(fd);
275 return NULL;
276 }
277 /* clone newfd to fd, close newfd */
278 dup2(newfd, fd);
279 close(newfd);
280
281 } else {
282 fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
283 fd, strerror(errno));
284 return NULL;
285 }
286 }
287
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000288 nc = qemu_new_net_client(&net_dgram_socket_info, vlan, NULL, model, name);
289
290 snprintf(nc->info_str, sizeof(nc->info_str),
291 "socket: fd=%d (%s mcast=%s:%d)",
292 fd, is_connected ? "cloned" : "",
293 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
294
295 s = DO_UPCAST(NetSocketState, nc, nc);
296
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000297 s->fd = fd;
298
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000299 qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
300
301 /* mcast: save bound address as dst */
302 if (is_connected) s->dgram_dst=saddr;
303
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000304 return s;
305}
306
307static void net_socket_connect(void *opaque)
308{
309 NetSocketState *s = opaque;
310 qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
311}
312
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000313static NetClientInfo net_socket_info = {
314 .type = NET_CLIENT_TYPE_SOCKET,
315 .size = sizeof(NetSocketState),
316 .receive = net_socket_receive,
317 .cleanup = net_socket_cleanup,
318};
319
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000320static NetSocketState *net_socket_fd_init_stream(VLANState *vlan,
321 const char *model,
322 const char *name,
323 int fd, int is_connected)
324{
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000325 VLANClientState *nc;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000326 NetSocketState *s;
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000327
328 nc = qemu_new_net_client(&net_socket_info, vlan, NULL, model, name);
329
330 snprintf(nc->info_str, sizeof(nc->info_str), "socket: fd=%d", fd);
331
332 s = DO_UPCAST(NetSocketState, nc, nc);
333
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000334 s->fd = fd;
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000335
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000336 if (is_connected) {
337 net_socket_connect(s);
338 } else {
339 qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
340 }
341 return s;
342}
343
344static NetSocketState *net_socket_fd_init(VLANState *vlan,
345 const char *model, const char *name,
346 int fd, int is_connected)
347{
348 int so_type = -1, optlen=sizeof(so_type);
349
350 if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type,
351 (socklen_t *)&optlen)< 0) {
352 fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd);
353 return NULL;
354 }
355 switch(so_type) {
356 case SOCK_DGRAM:
357 return net_socket_fd_init_dgram(vlan, model, name, fd, is_connected);
358 case SOCK_STREAM:
359 return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
360 default:
361 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
362 fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
363 return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
364 }
365 return NULL;
366}
367
368static void net_socket_accept(void *opaque)
369{
370 NetSocketListenState *s = opaque;
371 NetSocketState *s1;
372 struct sockaddr_in saddr;
373 socklen_t len;
374 int fd;
375
376 for(;;) {
377 len = sizeof(saddr);
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100378 fd = qemu_accept(s->fd, (struct sockaddr *)&saddr, &len);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000379 if (fd < 0 && errno != EINTR) {
380 return;
381 } else if (fd >= 0) {
382 break;
383 }
384 }
385 s1 = net_socket_fd_init(s->vlan, s->model, s->name, fd, 1);
386 if (!s1) {
387 closesocket(fd);
388 } else {
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000389 snprintf(s1->nc.info_str, sizeof(s1->nc.info_str),
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000390 "socket: connection from %s:%d",
391 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
392 }
393}
394
395static int net_socket_listen_init(VLANState *vlan,
396 const char *model,
397 const char *name,
398 const char *host_str)
399{
400 NetSocketListenState *s;
401 int fd, val, ret;
402 struct sockaddr_in saddr;
403
404 if (parse_host_port(&saddr, host_str) < 0)
405 return -1;
406
Anthony Liguori7267c092011-08-20 22:09:37 -0500407 s = g_malloc0(sizeof(NetSocketListenState));
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000408
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100409 fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000410 if (fd < 0) {
411 perror("socket");
Zhi Hui Lic7ee8f62011-11-24 16:23:00 +0800412 g_free(s);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000413 return -1;
414 }
415 socket_set_nonblock(fd);
416
417 /* allow fast reuse */
418 val = 1;
419 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
420
421 ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
422 if (ret < 0) {
423 perror("bind");
Zhi Hui Lic7ee8f62011-11-24 16:23:00 +0800424 g_free(s);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000425 return -1;
426 }
427 ret = listen(fd, 0);
428 if (ret < 0) {
429 perror("listen");
Zhi Hui Lic7ee8f62011-11-24 16:23:00 +0800430 g_free(s);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000431 return -1;
432 }
433 s->vlan = vlan;
Anthony Liguori7267c092011-08-20 22:09:37 -0500434 s->model = g_strdup(model);
435 s->name = name ? g_strdup(name) : NULL;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000436 s->fd = fd;
437 qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
438 return 0;
439}
440
441static int net_socket_connect_init(VLANState *vlan,
442 const char *model,
443 const char *name,
444 const char *host_str)
445{
446 NetSocketState *s;
447 int fd, connected, ret, err;
448 struct sockaddr_in saddr;
449
450 if (parse_host_port(&saddr, host_str) < 0)
451 return -1;
452
Kevin Wolf40ff6d72009-12-02 12:24:42 +0100453 fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000454 if (fd < 0) {
455 perror("socket");
456 return -1;
457 }
458 socket_set_nonblock(fd);
459
460 connected = 0;
461 for(;;) {
462 ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
463 if (ret < 0) {
464 err = socket_error();
465 if (err == EINTR || err == EWOULDBLOCK) {
466 } else if (err == EINPROGRESS) {
467 break;
468#ifdef _WIN32
Pavel Dovgalukc7eb1f02011-02-21 14:46:44 +0300469 } else if (err == WSAEALREADY || err == WSAEINVAL) {
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000470 break;
471#endif
472 } else {
473 perror("connect");
474 closesocket(fd);
475 return -1;
476 }
477 } else {
478 connected = 1;
479 break;
480 }
481 }
482 s = net_socket_fd_init(vlan, model, name, fd, connected);
483 if (!s)
484 return -1;
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000485 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000486 "socket: connect to %s:%d",
487 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
488 return 0;
489}
490
491static int net_socket_mcast_init(VLANState *vlan,
492 const char *model,
493 const char *name,
Mike Ryan3a75e742010-12-01 11:16:47 -0800494 const char *host_str,
495 const char *localaddr_str)
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000496{
497 NetSocketState *s;
498 int fd;
499 struct sockaddr_in saddr;
Mike Ryan3a75e742010-12-01 11:16:47 -0800500 struct in_addr localaddr, *param_localaddr;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000501
502 if (parse_host_port(&saddr, host_str) < 0)
503 return -1;
504
Mike Ryan3a75e742010-12-01 11:16:47 -0800505 if (localaddr_str != NULL) {
506 if (inet_aton(localaddr_str, &localaddr) == 0)
507 return -1;
508 param_localaddr = &localaddr;
509 } else {
510 param_localaddr = NULL;
511 }
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000512
Mike Ryan3a75e742010-12-01 11:16:47 -0800513 fd = net_socket_mcast_create(&saddr, param_localaddr);
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000514 if (fd < 0)
515 return -1;
516
517 s = net_socket_fd_init(vlan, model, name, fd, 0);
518 if (!s)
519 return -1;
520
521 s->dgram_dst = saddr;
522
Mark McLoughlin564f63e2009-11-25 18:49:08 +0000523 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000524 "socket: mcast=%s:%d",
525 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
526 return 0;
527
528}
529
530int net_init_socket(QemuOpts *opts,
531 Monitor *mon,
532 const char *name,
533 VLANState *vlan)
534{
535 if (qemu_opt_get(opts, "fd")) {
536 int fd;
537
538 if (qemu_opt_get(opts, "listen") ||
539 qemu_opt_get(opts, "connect") ||
Mike Ryan3a75e742010-12-01 11:16:47 -0800540 qemu_opt_get(opts, "mcast") ||
541 qemu_opt_get(opts, "localaddr")) {
Markus Armbruster6daf1942011-06-22 14:03:54 +0200542 error_report("listen=, connect=, mcast= and localaddr= is invalid with fd=");
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000543 return -1;
544 }
545
546 fd = net_handle_fd_param(mon, qemu_opt_get(opts, "fd"));
547 if (fd == -1) {
548 return -1;
549 }
550
551 if (!net_socket_fd_init(vlan, "socket", name, fd, 1)) {
552 close(fd);
553 return -1;
554 }
555 } else if (qemu_opt_get(opts, "listen")) {
556 const char *listen;
557
558 if (qemu_opt_get(opts, "fd") ||
559 qemu_opt_get(opts, "connect") ||
Mike Ryan3a75e742010-12-01 11:16:47 -0800560 qemu_opt_get(opts, "mcast") ||
561 qemu_opt_get(opts, "localaddr")) {
Markus Armbruster6daf1942011-06-22 14:03:54 +0200562 error_report("fd=, connect=, mcast= and localaddr= is invalid with listen=");
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000563 return -1;
564 }
565
566 listen = qemu_opt_get(opts, "listen");
567
568 if (net_socket_listen_init(vlan, "socket", name, listen) == -1) {
569 return -1;
570 }
571 } else if (qemu_opt_get(opts, "connect")) {
572 const char *connect;
573
574 if (qemu_opt_get(opts, "fd") ||
575 qemu_opt_get(opts, "listen") ||
Mike Ryan3a75e742010-12-01 11:16:47 -0800576 qemu_opt_get(opts, "mcast") ||
577 qemu_opt_get(opts, "localaddr")) {
Markus Armbruster6daf1942011-06-22 14:03:54 +0200578 error_report("fd=, listen=, mcast= and localaddr= is invalid with connect=");
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000579 return -1;
580 }
581
582 connect = qemu_opt_get(opts, "connect");
583
584 if (net_socket_connect_init(vlan, "socket", name, connect) == -1) {
585 return -1;
586 }
587 } else if (qemu_opt_get(opts, "mcast")) {
Mike Ryan3a75e742010-12-01 11:16:47 -0800588 const char *mcast, *localaddr;
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000589
590 if (qemu_opt_get(opts, "fd") ||
591 qemu_opt_get(opts, "connect") ||
592 qemu_opt_get(opts, "listen")) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100593 error_report("fd=, connect= and listen= is invalid with mcast=");
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000594 return -1;
595 }
596
597 mcast = qemu_opt_get(opts, "mcast");
Mike Ryan3a75e742010-12-01 11:16:47 -0800598 localaddr = qemu_opt_get(opts, "localaddr");
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000599
Mike Ryan3a75e742010-12-01 11:16:47 -0800600 if (net_socket_mcast_init(vlan, "socket", name, mcast, localaddr) == -1) {
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000601 return -1;
602 }
603 } else {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100604 error_report("-socket requires fd=, listen=, connect= or mcast=");
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000605 return -1;
606 }
607
Mark McLoughlin42281ac2009-11-25 18:48:56 +0000608 return 0;
609}