]> git.proxmox.com Git - mirror_qemu.git/blame - net/socket.c
exec: revert MemoryRegionCache
[mirror_qemu.git] / net / socket.c
CommitLineData
42281ac9
MM
1/*
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 */
2744d920 24#include "qemu/osdep.h"
42281ac9 25
1422e32d 26#include "net/net.h"
a245fc18 27#include "clients.h"
83c9089e 28#include "monitor/monitor.h"
da34e65c 29#include "qapi/error.h"
42281ac9 30#include "qemu-common.h"
1de7afc9
PB
31#include "qemu/error-report.h"
32#include "qemu/option.h"
33#include "qemu/sockets.h"
34#include "qemu/iov.h"
6a1751b7 35#include "qemu/main-loop.h"
42281ac9
MM
36
37typedef struct NetSocketState {
4e68f7a0 38 NetClientState nc;
011de2b5 39 int listen_fd;
42281ac9 40 int fd;
16a3df40 41 SocketReadState rs;
45a7f54a 42 unsigned int send_index; /* number of bytes sent (only SOCK_STREAM) */
42281ac9 43 struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
863f678f
SH
44 IOHandler *send_fn; /* differs between SOCK_STREAM/SOCK_DGRAM */
45 bool read_poll; /* waiting to receive data? */
46 bool write_poll; /* waiting to transmit data? */
42281ac9
MM
47} NetSocketState;
48
011de2b5 49static void net_socket_accept(void *opaque);
863f678f
SH
50static void net_socket_writable(void *opaque);
51
863f678f
SH
52static void net_socket_update_fd_handler(NetSocketState *s)
53{
82e1cc4b
FZ
54 qemu_set_fd_handler(s->fd,
55 s->read_poll ? s->send_fn : NULL,
56 s->write_poll ? net_socket_writable : NULL,
57 s);
863f678f
SH
58}
59
60static void net_socket_read_poll(NetSocketState *s, bool enable)
61{
62 s->read_poll = enable;
63 net_socket_update_fd_handler(s);
64}
65
66static void net_socket_write_poll(NetSocketState *s, bool enable)
67{
68 s->write_poll = enable;
69 net_socket_update_fd_handler(s);
70}
71
72static void net_socket_writable(void *opaque)
73{
74 NetSocketState *s = opaque;
75
76 net_socket_write_poll(s, false);
77
78 qemu_flush_queued_packets(&s->nc);
79}
42281ac9 80
4e68f7a0 81static ssize_t net_socket_receive(NetClientState *nc, const uint8_t *buf, size_t size)
42281ac9 82{
564f63e3 83 NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
45a7f54a
SH
84 uint32_t len = htonl(size);
85 struct iovec iov[] = {
86 {
87 .iov_base = &len,
88 .iov_len = sizeof(len),
89 }, {
90 .iov_base = (void *)buf,
91 .iov_len = size,
92 },
93 };
94 size_t remaining;
95 ssize_t ret;
42281ac9 96
45a7f54a
SH
97 remaining = iov_size(iov, 2) - s->send_index;
98 ret = iov_send(s->fd, iov, 2, s->send_index, remaining);
99
100 if (ret == -1 && errno == EAGAIN) {
101 ret = 0; /* handled further down */
102 }
103 if (ret == -1) {
104 s->send_index = 0;
105 return -errno;
106 }
107 if (ret < (ssize_t)remaining) {
108 s->send_index += ret;
109 net_socket_write_poll(s, true);
110 return 0;
111 }
112 s->send_index = 0;
113 return size;
42281ac9
MM
114}
115
4e68f7a0 116static ssize_t net_socket_receive_dgram(NetClientState *nc, const uint8_t *buf, size_t size)
42281ac9 117{
564f63e3 118 NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
213fd508
SH
119 ssize_t ret;
120
121 do {
73062dfe
SW
122 ret = qemu_sendto(s->fd, buf, size, 0,
123 (struct sockaddr *)&s->dgram_dst,
124 sizeof(s->dgram_dst));
213fd508
SH
125 } while (ret == -1 && errno == EINTR);
126
127 if (ret == -1 && errno == EAGAIN) {
128 net_socket_write_poll(s, true);
129 return 0;
130 }
131 return ret;
42281ac9
MM
132}
133
6e99c631
FZ
134static void net_socket_send_completed(NetClientState *nc, ssize_t len)
135{
136 NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
137
138 if (!s->read_poll) {
139 net_socket_read_poll(s, true);
140 }
141}
142
16a3df40
ZC
143static void net_socket_rs_finalize(SocketReadState *rs)
144{
145 NetSocketState *s = container_of(rs, NetSocketState, rs);
146
147 if (qemu_send_packet_async(&s->nc, rs->buf,
148 rs->packet_len,
149 net_socket_send_completed) == 0) {
150 net_socket_read_poll(s, false);
151 }
152}
153
42281ac9
MM
154static void net_socket_send(void *opaque)
155{
156 NetSocketState *s = opaque;
b16a44e1 157 int size;
16a3df40 158 int ret;
d32fcad3 159 uint8_t buf1[NET_BUFSIZE];
42281ac9
MM
160 const uint8_t *buf;
161
00aa0040 162 size = qemu_recv(s->fd, buf1, sizeof(buf1), 0);
42281ac9 163 if (size < 0) {
b16a44e1 164 if (errno != EWOULDBLOCK)
42281ac9
MM
165 goto eoc;
166 } else if (size == 0) {
167 /* end of connection */
168 eoc:
863f678f
SH
169 net_socket_read_poll(s, false);
170 net_socket_write_poll(s, false);
011de2b5
ZYW
171 if (s->listen_fd != -1) {
172 qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
173 }
42281ac9 174 closesocket(s->fd);
011de2b5
ZYW
175
176 s->fd = -1;
16a3df40 177 net_socket_rs_init(&s->rs, net_socket_rs_finalize);
011de2b5 178 s->nc.link_down = true;
011de2b5
ZYW
179 memset(s->nc.info_str, 0, sizeof(s->nc.info_str));
180
42281ac9
MM
181 return;
182 }
183 buf = buf1;
42281ac9 184
16a3df40
ZC
185 ret = net_fill_rstate(&s->rs, buf, size);
186
187 if (ret == -1) {
188 goto eoc;
42281ac9
MM
189 }
190}
191
192static void net_socket_send_dgram(void *opaque)
193{
194 NetSocketState *s = opaque;
195 int size;
196
16a3df40 197 size = qemu_recv(s->fd, s->rs.buf, sizeof(s->rs.buf), 0);
42281ac9
MM
198 if (size < 0)
199 return;
200 if (size == 0) {
201 /* end of connection */
863f678f
SH
202 net_socket_read_poll(s, false);
203 net_socket_write_poll(s, false);
42281ac9
MM
204 return;
205 }
16a3df40 206 if (qemu_send_packet_async(&s->nc, s->rs.buf, size,
6e99c631
FZ
207 net_socket_send_completed) == 0) {
208 net_socket_read_poll(s, false);
209 }
42281ac9
MM
210}
211
3a75e74c 212static int net_socket_mcast_create(struct sockaddr_in *mcastaddr, struct in_addr *localaddr)
42281ac9
MM
213{
214 struct ip_mreq imr;
215 int fd;
216 int val, ret;
23ddf2bb
BS
217#ifdef __OpenBSD__
218 unsigned char loop;
219#else
220 int loop;
221#endif
222
42281ac9 223 if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
842480d4
SH
224 fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) "
225 "does not contain a multicast address\n",
226 inet_ntoa(mcastaddr->sin_addr),
42281ac9 227 (int)ntohl(mcastaddr->sin_addr.s_addr));
842480d4 228 return -1;
42281ac9
MM
229
230 }
40ff6d7e 231 fd = qemu_socket(PF_INET, SOCK_DGRAM, 0);
42281ac9
MM
232 if (fd < 0) {
233 perror("socket(PF_INET, SOCK_DGRAM)");
234 return -1;
235 }
236
bcbe92fb
SO
237 /* Allow multiple sockets to bind the same multicast ip and port by setting
238 * SO_REUSEADDR. This is the only situation where SO_REUSEADDR should be set
239 * on windows. Use socket_set_fast_reuse otherwise as it sets SO_REUSEADDR
240 * only on posix systems.
241 */
42281ac9 242 val = 1;
9957fc7f 243 ret = qemu_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
42281ac9 244 if (ret < 0) {
842480d4
SH
245 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
246 goto fail;
42281ac9
MM
247 }
248
249 ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
250 if (ret < 0) {
251 perror("bind");
252 goto fail;
253 }
254
255 /* Add host to multicast group */
256 imr.imr_multiaddr = mcastaddr->sin_addr;
3a75e74c
MR
257 if (localaddr) {
258 imr.imr_interface = *localaddr;
259 } else {
260 imr.imr_interface.s_addr = htonl(INADDR_ANY);
261 }
42281ac9 262
9957fc7f
SW
263 ret = qemu_setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
264 &imr, sizeof(struct ip_mreq));
42281ac9 265 if (ret < 0) {
842480d4
SH
266 perror("setsockopt(IP_ADD_MEMBERSHIP)");
267 goto fail;
42281ac9
MM
268 }
269
270 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
23ddf2bb 271 loop = 1;
9957fc7f
SW
272 ret = qemu_setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
273 &loop, sizeof(loop));
42281ac9 274 if (ret < 0) {
842480d4
SH
275 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
276 goto fail;
42281ac9
MM
277 }
278
3a75e74c
MR
279 /* If a bind address is given, only send packets from that address */
280 if (localaddr != NULL) {
9957fc7f
SW
281 ret = qemu_setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF,
282 localaddr, sizeof(*localaddr));
3a75e74c
MR
283 if (ret < 0) {
284 perror("setsockopt(IP_MULTICAST_IF)");
285 goto fail;
286 }
287 }
288
f9e8cacc 289 qemu_set_nonblock(fd);
42281ac9
MM
290 return fd;
291fail:
292 if (fd >= 0)
293 closesocket(fd);
294 return -1;
295}
296
4e68f7a0 297static void net_socket_cleanup(NetClientState *nc)
42281ac9 298{
564f63e3 299 NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
011de2b5 300 if (s->fd != -1) {
863f678f
SH
301 net_socket_read_poll(s, false);
302 net_socket_write_poll(s, false);
011de2b5
ZYW
303 close(s->fd);
304 s->fd = -1;
305 }
306 if (s->listen_fd != -1) {
307 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
308 closesocket(s->listen_fd);
309 s->listen_fd = -1;
310 }
42281ac9
MM
311}
312
564f63e3 313static NetClientInfo net_dgram_socket_info = {
f394b2e2 314 .type = NET_CLIENT_DRIVER_SOCKET,
564f63e3
MM
315 .size = sizeof(NetSocketState),
316 .receive = net_socket_receive_dgram,
317 .cleanup = net_socket_cleanup,
318};
319
4e68f7a0 320static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,
42281ac9
MM
321 const char *model,
322 const char *name,
323 int fd, int is_connected)
324{
325 struct sockaddr_in saddr;
326 int newfd;
ed6273e2 327 socklen_t saddr_len = sizeof(saddr);
4e68f7a0 328 NetClientState *nc;
42281ac9
MM
329 NetSocketState *s;
330
331 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
332 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
333 * by ONLY ONE process: we must "clone" this dgram socket --jjo
334 */
335
336 if (is_connected) {
842480d4
SH
337 if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) {
338 /* must be bound */
339 if (saddr.sin_addr.s_addr == 0) {
340 fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, "
341 "cannot setup multicast dst addr\n", fd);
e5d1fca0 342 goto err;
842480d4
SH
343 }
344 /* clone dgram socket */
345 newfd = net_socket_mcast_create(&saddr, NULL);
346 if (newfd < 0) {
347 /* error already reported by net_socket_mcast_create() */
e5d1fca0 348 goto err;
842480d4
SH
349 }
350 /* clone newfd to fd, close newfd */
351 dup2(newfd, fd);
352 close(newfd);
353
354 } else {
355 fprintf(stderr,
356 "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
357 fd, strerror(errno));
e5d1fca0 358 goto err;
842480d4 359 }
42281ac9
MM
360 }
361
ab5f3f84 362 nc = qemu_new_net_client(&net_dgram_socket_info, peer, model, name);
564f63e3 363
564f63e3
MM
364 s = DO_UPCAST(NetSocketState, nc, nc);
365
42281ac9 366 s->fd = fd;
011de2b5 367 s->listen_fd = -1;
863f678f 368 s->send_fn = net_socket_send_dgram;
16a3df40 369 net_socket_rs_init(&s->rs, net_socket_rs_finalize);
863f678f 370 net_socket_read_poll(s, true);
42281ac9
MM
371
372 /* mcast: save bound address as dst */
e34cde35
ZYW
373 if (is_connected) {
374 s->dgram_dst = saddr;
8db804ac
GA
375 snprintf(nc->info_str, sizeof(nc->info_str),
376 "socket: fd=%d (cloned mcast=%s:%d)",
377 fd, inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
378 } else {
379 snprintf(nc->info_str, sizeof(nc->info_str),
380 "socket: fd=%d", fd);
e34cde35 381 }
42281ac9 382
42281ac9 383 return s;
e5d1fca0
SH
384
385err:
386 closesocket(fd);
387 return NULL;
42281ac9
MM
388}
389
390static void net_socket_connect(void *opaque)
391{
392 NetSocketState *s = opaque;
863f678f
SH
393 s->send_fn = net_socket_send;
394 net_socket_read_poll(s, true);
42281ac9
MM
395}
396
564f63e3 397static NetClientInfo net_socket_info = {
f394b2e2 398 .type = NET_CLIENT_DRIVER_SOCKET,
564f63e3
MM
399 .size = sizeof(NetSocketState),
400 .receive = net_socket_receive,
401 .cleanup = net_socket_cleanup,
402};
403
4e68f7a0 404static NetSocketState *net_socket_fd_init_stream(NetClientState *peer,
42281ac9
MM
405 const char *model,
406 const char *name,
407 int fd, int is_connected)
408{
4e68f7a0 409 NetClientState *nc;
42281ac9 410 NetSocketState *s;
564f63e3 411
ab5f3f84 412 nc = qemu_new_net_client(&net_socket_info, peer, model, name);
564f63e3
MM
413
414 snprintf(nc->info_str, sizeof(nc->info_str), "socket: fd=%d", fd);
415
416 s = DO_UPCAST(NetSocketState, nc, nc);
417
42281ac9 418 s->fd = fd;
011de2b5 419 s->listen_fd = -1;
16a3df40 420 net_socket_rs_init(&s->rs, net_socket_rs_finalize);
564f63e3 421
20048d0a
SH
422 /* Disable Nagle algorithm on TCP sockets to reduce latency */
423 socket_set_nodelay(fd);
424
42281ac9
MM
425 if (is_connected) {
426 net_socket_connect(s);
427 } else {
428 qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
429 }
430 return s;
431}
432
4e68f7a0 433static NetSocketState *net_socket_fd_init(NetClientState *peer,
42281ac9
MM
434 const char *model, const char *name,
435 int fd, int is_connected)
436{
437 int so_type = -1, optlen=sizeof(so_type);
438
439 if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type,
440 (socklen_t *)&optlen)< 0) {
842480d4
SH
441 fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n",
442 fd);
e5d1fca0 443 closesocket(fd);
842480d4 444 return NULL;
42281ac9
MM
445 }
446 switch(so_type) {
447 case SOCK_DGRAM:
d33d93b2 448 return net_socket_fd_init_dgram(peer, model, name, fd, is_connected);
42281ac9 449 case SOCK_STREAM:
d33d93b2 450 return net_socket_fd_init_stream(peer, model, name, fd, is_connected);
42281ac9
MM
451 default:
452 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
453 fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
d33d93b2 454 return net_socket_fd_init_stream(peer, model, name, fd, is_connected);
42281ac9
MM
455 }
456 return NULL;
457}
458
459static void net_socket_accept(void *opaque)
460{
011de2b5 461 NetSocketState *s = opaque;
42281ac9
MM
462 struct sockaddr_in saddr;
463 socklen_t len;
464 int fd;
465
466 for(;;) {
467 len = sizeof(saddr);
011de2b5 468 fd = qemu_accept(s->listen_fd, (struct sockaddr *)&saddr, &len);
42281ac9
MM
469 if (fd < 0 && errno != EINTR) {
470 return;
471 } else if (fd >= 0) {
011de2b5 472 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
42281ac9
MM
473 break;
474 }
475 }
011de2b5
ZYW
476
477 s->fd = fd;
478 s->nc.link_down = false;
479 net_socket_connect(s);
480 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
481 "socket: connection from %s:%d",
482 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
42281ac9
MM
483}
484
4e68f7a0 485static int net_socket_listen_init(NetClientState *peer,
42281ac9
MM
486 const char *model,
487 const char *name,
488 const char *host_str)
489{
011de2b5
ZYW
490 NetClientState *nc;
491 NetSocketState *s;
883e4f76
MAL
492 SocketAddress *saddr;
493 int ret;
494 Error *local_error = NULL;
42281ac9 495
883e4f76
MAL
496 saddr = socket_parse(host_str, &local_error);
497 if (saddr == NULL) {
498 error_report_err(local_error);
42281ac9
MM
499 return -1;
500 }
42281ac9 501
883e4f76 502 ret = socket_listen(saddr, &local_error);
61601835 503 if (ret < 0) {
883e4f76
MAL
504 qapi_free_SocketAddress(saddr);
505 error_report_err(local_error);
42281ac9
MM
506 return -1;
507 }
011de2b5
ZYW
508
509 nc = qemu_new_net_client(&net_socket_info, peer, model, name);
510 s = DO_UPCAST(NetSocketState, nc, nc);
511 s->fd = -1;
883e4f76 512 s->listen_fd = ret;
011de2b5 513 s->nc.link_down = true;
e79cd406 514 net_socket_rs_init(&s->rs, net_socket_rs_finalize);
011de2b5
ZYW
515
516 qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
883e4f76 517 qapi_free_SocketAddress(saddr);
42281ac9
MM
518 return 0;
519}
520
883e4f76
MAL
521typedef struct {
522 NetClientState *peer;
523 SocketAddress *saddr;
524 char *model;
525 char *name;
526} socket_connect_data;
527
528static void socket_connect_data_free(socket_connect_data *c)
529{
530 qapi_free_SocketAddress(c->saddr);
531 g_free(c->model);
532 g_free(c->name);
533 g_free(c);
534}
535
536static void net_socket_connected(int fd, Error *err, void *opaque)
537{
538 socket_connect_data *c = opaque;
539 NetSocketState *s;
540 char *addr_str = NULL;
541 Error *local_error = NULL;
542
543 addr_str = socket_address_to_string(c->saddr, &local_error);
544 if (addr_str == NULL) {
545 error_report_err(local_error);
546 closesocket(fd);
547 goto end;
548 }
549
550 s = net_socket_fd_init(c->peer, c->model, c->name, fd, true);
551 if (!s) {
552 closesocket(fd);
553 goto end;
554 }
555
556 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
557 "socket: connect to %s", addr_str);
558
559end:
560 g_free(addr_str);
561 socket_connect_data_free(c);
562}
563
4e68f7a0 564static int net_socket_connect_init(NetClientState *peer,
42281ac9
MM
565 const char *model,
566 const char *name,
567 const char *host_str)
568{
883e4f76
MAL
569 socket_connect_data *c = g_new0(socket_connect_data, 1);
570 int fd = -1;
571 Error *local_error = NULL;
572
573 c->peer = peer;
574 c->model = g_strdup(model);
575 c->name = g_strdup(name);
576 c->saddr = socket_parse(host_str, &local_error);
577 if (c->saddr == NULL) {
578 goto err;
579 }
42281ac9 580
883e4f76 581 fd = socket_connect(c->saddr, &local_error, net_socket_connected, c);
42281ac9 582 if (fd < 0) {
883e4f76 583 goto err;
42281ac9 584 }
61601835 585
42281ac9 586 return 0;
883e4f76
MAL
587
588err:
589 error_report_err(local_error);
590 socket_connect_data_free(c);
591 return -1;
42281ac9
MM
592}
593
4e68f7a0 594static int net_socket_mcast_init(NetClientState *peer,
42281ac9
MM
595 const char *model,
596 const char *name,
3a75e74c
MR
597 const char *host_str,
598 const char *localaddr_str)
42281ac9
MM
599{
600 NetSocketState *s;
601 int fd;
602 struct sockaddr_in saddr;
3a75e74c 603 struct in_addr localaddr, *param_localaddr;
42281ac9
MM
604
605 if (parse_host_port(&saddr, host_str) < 0)
606 return -1;
607
3a75e74c
MR
608 if (localaddr_str != NULL) {
609 if (inet_aton(localaddr_str, &localaddr) == 0)
610 return -1;
611 param_localaddr = &localaddr;
612 } else {
613 param_localaddr = NULL;
614 }
42281ac9 615
3a75e74c 616 fd = net_socket_mcast_create(&saddr, param_localaddr);
42281ac9 617 if (fd < 0)
842480d4 618 return -1;
42281ac9 619
d33d93b2 620 s = net_socket_fd_init(peer, model, name, fd, 0);
42281ac9
MM
621 if (!s)
622 return -1;
623
624 s->dgram_dst = saddr;
625
564f63e3 626 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
42281ac9
MM
627 "socket: mcast=%s:%d",
628 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
629 return 0;
630
631}
632
4e68f7a0 633static int net_socket_udp_init(NetClientState *peer,
0e0e7fac
BM
634 const char *model,
635 const char *name,
636 const char *rhost,
637 const char *lhost)
638{
639 NetSocketState *s;
bcbe92fb 640 int fd, ret;
0e0e7fac
BM
641 struct sockaddr_in laddr, raddr;
642
643 if (parse_host_port(&laddr, lhost) < 0) {
644 return -1;
645 }
646
647 if (parse_host_port(&raddr, rhost) < 0) {
648 return -1;
649 }
650
651 fd = qemu_socket(PF_INET, SOCK_DGRAM, 0);
652 if (fd < 0) {
653 perror("socket(PF_INET, SOCK_DGRAM)");
654 return -1;
655 }
bcbe92fb
SO
656
657 ret = socket_set_fast_reuse(fd);
0e0e7fac 658 if (ret < 0) {
0e0e7fac
BM
659 closesocket(fd);
660 return -1;
661 }
662 ret = bind(fd, (struct sockaddr *)&laddr, sizeof(laddr));
663 if (ret < 0) {
664 perror("bind");
665 closesocket(fd);
666 return -1;
667 }
fc13fa00 668 qemu_set_nonblock(fd);
0e0e7fac 669
d33d93b2 670 s = net_socket_fd_init(peer, model, name, fd, 0);
0e0e7fac
BM
671 if (!s) {
672 return -1;
673 }
674
675 s->dgram_dst = raddr;
676
677 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
678 "socket: udp=%s:%d",
679 inet_ntoa(raddr.sin_addr), ntohs(raddr.sin_port));
680 return 0;
681}
682
cebea510 683int net_init_socket(const Netdev *netdev, const char *name,
a30ecde6 684 NetClientState *peer, Error **errp)
42281ac9 685{
a30ecde6 686 /* FIXME error_setg(errp, ...) on failure */
1677f4c6 687 Error *err = NULL;
bef8e8fe 688 const NetdevSocketOptions *sock;
42281ac9 689
f394b2e2
EB
690 assert(netdev->type == NET_CLIENT_DRIVER_SOCKET);
691 sock = &netdev->u.socket;
42281ac9 692
bef8e8fe
LE
693 if (sock->has_fd + sock->has_listen + sock->has_connect + sock->has_mcast +
694 sock->has_udp != 1) {
695 error_report("exactly one of fd=, listen=, connect=, mcast= or udp="
696 " is required");
697 return -1;
698 }
42281ac9 699
bef8e8fe
LE
700 if (sock->has_localaddr && !sock->has_mcast && !sock->has_udp) {
701 error_report("localaddr= is only valid with mcast= or udp=");
702 return -1;
703 }
42281ac9 704
bef8e8fe
LE
705 if (sock->has_fd) {
706 int fd;
42281ac9 707
1677f4c6 708 fd = monitor_fd_param(cur_mon, sock->fd, &err);
fc13fa00 709 if (fd == -1) {
1677f4c6 710 error_report_err(err);
fc13fa00
SH
711 return -1;
712 }
713 qemu_set_nonblock(fd);
714 if (!net_socket_fd_init(peer, "socket", name, fd, 1)) {
42281ac9
MM
715 return -1;
716 }
bef8e8fe
LE
717 return 0;
718 }
42281ac9 719
bef8e8fe 720 if (sock->has_listen) {
d33d93b2 721 if (net_socket_listen_init(peer, "socket", name, sock->listen) == -1) {
42281ac9
MM
722 return -1;
723 }
bef8e8fe
LE
724 return 0;
725 }
42281ac9 726
bef8e8fe 727 if (sock->has_connect) {
d33d93b2 728 if (net_socket_connect_init(peer, "socket", name, sock->connect) ==
bef8e8fe 729 -1) {
42281ac9
MM
730 return -1;
731 }
bef8e8fe
LE
732 return 0;
733 }
42281ac9 734
bef8e8fe
LE
735 if (sock->has_mcast) {
736 /* if sock->localaddr is missing, it has been initialized to "all bits
737 * zero" */
d33d93b2 738 if (net_socket_mcast_init(peer, "socket", name, sock->mcast,
bef8e8fe 739 sock->localaddr) == -1) {
0e0e7fac
BM
740 return -1;
741 }
bef8e8fe
LE
742 return 0;
743 }
0e0e7fac 744
bef8e8fe
LE
745 assert(sock->has_udp);
746 if (!sock->has_localaddr) {
747 error_report("localaddr= is mandatory with udp=");
748 return -1;
749 }
f0e3ac70 750 if (net_socket_udp_init(peer, "socket", name, sock->udp, sock->localaddr) ==
bef8e8fe 751 -1) {
42281ac9
MM
752 return -1;
753 }
42281ac9
MM
754 return 0;
755}