]> git.proxmox.com Git - mirror_qemu.git/blame - net/socket.c
net/socket: Don't treat odd socket type as SOCK_STREAM
[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;
3cde5ea2 177 net_socket_rs_init(&s->rs, net_socket_rs_finalize, false);
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,
0f8c289a
JF
323 int fd, int is_connected,
324 const char *mcast)
42281ac9
MM
325{
326 struct sockaddr_in saddr;
327 int newfd;
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
0f8c289a
JF
336 if (is_connected && mcast != NULL) {
337 if (parse_host_port(&saddr, mcast) < 0) {
338 fprintf(stderr,
339 "qemu: error: init_dgram: fd=%d failed parse_host_port()\n",
340 fd);
341 goto err;
342 }
842480d4
SH
343 /* must be bound */
344 if (saddr.sin_addr.s_addr == 0) {
345 fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, "
346 "cannot setup multicast dst addr\n", fd);
e5d1fca0 347 goto err;
842480d4
SH
348 }
349 /* clone dgram socket */
350 newfd = net_socket_mcast_create(&saddr, NULL);
351 if (newfd < 0) {
352 /* error already reported by net_socket_mcast_create() */
e5d1fca0 353 goto err;
842480d4
SH
354 }
355 /* clone newfd to fd, close newfd */
356 dup2(newfd, fd);
357 close(newfd);
358
42281ac9
MM
359 }
360
ab5f3f84 361 nc = qemu_new_net_client(&net_dgram_socket_info, peer, model, name);
564f63e3 362
564f63e3
MM
363 s = DO_UPCAST(NetSocketState, nc, nc);
364
42281ac9 365 s->fd = fd;
011de2b5 366 s->listen_fd = -1;
863f678f 367 s->send_fn = net_socket_send_dgram;
3cde5ea2 368 net_socket_rs_init(&s->rs, net_socket_rs_finalize, false);
863f678f 369 net_socket_read_poll(s, true);
42281ac9
MM
370
371 /* mcast: save bound address as dst */
e34cde35
ZYW
372 if (is_connected) {
373 s->dgram_dst = saddr;
8db804ac
GA
374 snprintf(nc->info_str, sizeof(nc->info_str),
375 "socket: fd=%d (cloned mcast=%s:%d)",
376 fd, inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
377 } else {
378 snprintf(nc->info_str, sizeof(nc->info_str),
379 "socket: fd=%d", fd);
e34cde35 380 }
42281ac9 381
42281ac9 382 return s;
e5d1fca0
SH
383
384err:
385 closesocket(fd);
386 return NULL;
42281ac9
MM
387}
388
389static void net_socket_connect(void *opaque)
390{
391 NetSocketState *s = opaque;
863f678f
SH
392 s->send_fn = net_socket_send;
393 net_socket_read_poll(s, true);
42281ac9
MM
394}
395
564f63e3 396static NetClientInfo net_socket_info = {
f394b2e2 397 .type = NET_CLIENT_DRIVER_SOCKET,
564f63e3
MM
398 .size = sizeof(NetSocketState),
399 .receive = net_socket_receive,
400 .cleanup = net_socket_cleanup,
401};
402
4e68f7a0 403static NetSocketState *net_socket_fd_init_stream(NetClientState *peer,
42281ac9
MM
404 const char *model,
405 const char *name,
406 int fd, int is_connected)
407{
4e68f7a0 408 NetClientState *nc;
42281ac9 409 NetSocketState *s;
564f63e3 410
ab5f3f84 411 nc = qemu_new_net_client(&net_socket_info, peer, model, name);
564f63e3
MM
412
413 snprintf(nc->info_str, sizeof(nc->info_str), "socket: fd=%d", fd);
414
415 s = DO_UPCAST(NetSocketState, nc, nc);
416
42281ac9 417 s->fd = fd;
011de2b5 418 s->listen_fd = -1;
3cde5ea2 419 net_socket_rs_init(&s->rs, net_socket_rs_finalize, false);
564f63e3 420
20048d0a
SH
421 /* Disable Nagle algorithm on TCP sockets to reduce latency */
422 socket_set_nodelay(fd);
423
42281ac9
MM
424 if (is_connected) {
425 net_socket_connect(s);
426 } else {
427 qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
428 }
429 return s;
430}
431
4e68f7a0 432static NetSocketState *net_socket_fd_init(NetClientState *peer,
42281ac9 433 const char *model, const char *name,
0f8c289a 434 int fd, int is_connected, const char *mc)
42281ac9
MM
435{
436 int so_type = -1, optlen=sizeof(so_type);
437
438 if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type,
439 (socklen_t *)&optlen)< 0) {
842480d4
SH
440 fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n",
441 fd);
e5d1fca0 442 closesocket(fd);
842480d4 443 return NULL;
42281ac9
MM
444 }
445 switch(so_type) {
446 case SOCK_DGRAM:
0f8c289a 447 return net_socket_fd_init_dgram(peer, model, name, fd, is_connected, mc);
42281ac9 448 case SOCK_STREAM:
d33d93b2 449 return net_socket_fd_init_stream(peer, model, name, fd, is_connected);
42281ac9 450 default:
e1b24b64
MZ
451 error_report("socket type=%d for fd=%d must be either"
452 " SOCK_DGRAM or SOCK_STREAM", so_type, fd);
453 closesocket(fd);
42281ac9
MM
454 }
455 return NULL;
456}
457
458static void net_socket_accept(void *opaque)
459{
011de2b5 460 NetSocketState *s = opaque;
42281ac9
MM
461 struct sockaddr_in saddr;
462 socklen_t len;
463 int fd;
464
465 for(;;) {
466 len = sizeof(saddr);
011de2b5 467 fd = qemu_accept(s->listen_fd, (struct sockaddr *)&saddr, &len);
42281ac9
MM
468 if (fd < 0 && errno != EINTR) {
469 return;
470 } else if (fd >= 0) {
011de2b5 471 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
42281ac9
MM
472 break;
473 }
474 }
011de2b5
ZYW
475
476 s->fd = fd;
477 s->nc.link_down = false;
478 net_socket_connect(s);
479 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
480 "socket: connection from %s:%d",
481 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
42281ac9
MM
482}
483
4e68f7a0 484static int net_socket_listen_init(NetClientState *peer,
42281ac9
MM
485 const char *model,
486 const char *name,
487 const char *host_str)
488{
011de2b5
ZYW
489 NetClientState *nc;
490 NetSocketState *s;
6701e551
DB
491 struct sockaddr_in saddr;
492 int fd, ret;
42281ac9 493
6701e551
DB
494 if (parse_host_port(&saddr, host_str) < 0)
495 return -1;
496
497 fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
498 if (fd < 0) {
499 perror("socket");
42281ac9
MM
500 return -1;
501 }
6701e551
DB
502 qemu_set_nonblock(fd);
503
504 socket_set_fast_reuse(fd);
42281ac9 505
6701e551 506 ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
61601835 507 if (ret < 0) {
6701e551
DB
508 perror("bind");
509 closesocket(fd);
510 return -1;
511 }
512 ret = listen(fd, 0);
513 if (ret < 0) {
514 perror("listen");
515 closesocket(fd);
42281ac9
MM
516 return -1;
517 }
011de2b5
ZYW
518
519 nc = qemu_new_net_client(&net_socket_info, peer, model, name);
520 s = DO_UPCAST(NetSocketState, nc, nc);
521 s->fd = -1;
6701e551 522 s->listen_fd = fd;
011de2b5 523 s->nc.link_down = true;
3cde5ea2 524 net_socket_rs_init(&s->rs, net_socket_rs_finalize, false);
011de2b5
ZYW
525
526 qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
42281ac9
MM
527 return 0;
528}
529
4e68f7a0 530static int net_socket_connect_init(NetClientState *peer,
42281ac9
MM
531 const char *model,
532 const char *name,
533 const char *host_str)
534{
6701e551
DB
535 NetSocketState *s;
536 int fd, connected, ret;
537 struct sockaddr_in saddr;
538
539 if (parse_host_port(&saddr, host_str) < 0)
540 return -1;
42281ac9 541
6701e551 542 fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
42281ac9 543 if (fd < 0) {
6701e551
DB
544 perror("socket");
545 return -1;
42281ac9 546 }
6701e551 547 qemu_set_nonblock(fd);
61601835 548
6701e551
DB
549 connected = 0;
550 for(;;) {
551 ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
552 if (ret < 0) {
553 if (errno == EINTR || errno == EWOULDBLOCK) {
554 /* continue */
555 } else if (errno == EINPROGRESS ||
556 errno == EALREADY ||
557 errno == EINVAL) {
558 break;
559 } else {
560 perror("connect");
561 closesocket(fd);
562 return -1;
563 }
564 } else {
565 connected = 1;
566 break;
567 }
568 }
0f8c289a 569 s = net_socket_fd_init(peer, model, name, fd, connected, NULL);
6701e551
DB
570 if (!s)
571 return -1;
572 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
573 "socket: connect to %s:%d",
574 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
42281ac9
MM
575 return 0;
576}
577
4e68f7a0 578static int net_socket_mcast_init(NetClientState *peer,
42281ac9
MM
579 const char *model,
580 const char *name,
3a75e74c
MR
581 const char *host_str,
582 const char *localaddr_str)
42281ac9
MM
583{
584 NetSocketState *s;
585 int fd;
586 struct sockaddr_in saddr;
3a75e74c 587 struct in_addr localaddr, *param_localaddr;
42281ac9
MM
588
589 if (parse_host_port(&saddr, host_str) < 0)
590 return -1;
591
3a75e74c
MR
592 if (localaddr_str != NULL) {
593 if (inet_aton(localaddr_str, &localaddr) == 0)
594 return -1;
595 param_localaddr = &localaddr;
596 } else {
597 param_localaddr = NULL;
598 }
42281ac9 599
3a75e74c 600 fd = net_socket_mcast_create(&saddr, param_localaddr);
42281ac9 601 if (fd < 0)
842480d4 602 return -1;
42281ac9 603
0f8c289a 604 s = net_socket_fd_init(peer, model, name, fd, 0, NULL);
42281ac9
MM
605 if (!s)
606 return -1;
607
608 s->dgram_dst = saddr;
609
564f63e3 610 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
42281ac9
MM
611 "socket: mcast=%s:%d",
612 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
613 return 0;
614
615}
616
4e68f7a0 617static int net_socket_udp_init(NetClientState *peer,
0e0e7fac
BM
618 const char *model,
619 const char *name,
620 const char *rhost,
621 const char *lhost)
622{
623 NetSocketState *s;
bcbe92fb 624 int fd, ret;
0e0e7fac
BM
625 struct sockaddr_in laddr, raddr;
626
627 if (parse_host_port(&laddr, lhost) < 0) {
628 return -1;
629 }
630
631 if (parse_host_port(&raddr, rhost) < 0) {
632 return -1;
633 }
634
635 fd = qemu_socket(PF_INET, SOCK_DGRAM, 0);
636 if (fd < 0) {
637 perror("socket(PF_INET, SOCK_DGRAM)");
638 return -1;
639 }
bcbe92fb
SO
640
641 ret = socket_set_fast_reuse(fd);
0e0e7fac 642 if (ret < 0) {
0e0e7fac
BM
643 closesocket(fd);
644 return -1;
645 }
646 ret = bind(fd, (struct sockaddr *)&laddr, sizeof(laddr));
647 if (ret < 0) {
648 perror("bind");
649 closesocket(fd);
650 return -1;
651 }
fc13fa00 652 qemu_set_nonblock(fd);
0e0e7fac 653
0f8c289a 654 s = net_socket_fd_init(peer, model, name, fd, 0, NULL);
0e0e7fac
BM
655 if (!s) {
656 return -1;
657 }
658
659 s->dgram_dst = raddr;
660
661 snprintf(s->nc.info_str, sizeof(s->nc.info_str),
662 "socket: udp=%s:%d",
663 inet_ntoa(raddr.sin_addr), ntohs(raddr.sin_port));
664 return 0;
665}
666
cebea510 667int net_init_socket(const Netdev *netdev, const char *name,
a30ecde6 668 NetClientState *peer, Error **errp)
42281ac9 669{
a30ecde6 670 /* FIXME error_setg(errp, ...) on failure */
1677f4c6 671 Error *err = NULL;
bef8e8fe 672 const NetdevSocketOptions *sock;
42281ac9 673
f394b2e2
EB
674 assert(netdev->type == NET_CLIENT_DRIVER_SOCKET);
675 sock = &netdev->u.socket;
42281ac9 676
0f8c289a
JF
677 if (sock->has_listen + sock->has_connect + sock->has_mcast +
678 sock->has_udp > 1) {
679 error_report("exactly one of listen=, connect=, mcast= or udp="
bef8e8fe
LE
680 " is required");
681 return -1;
682 }
42281ac9 683
bef8e8fe
LE
684 if (sock->has_localaddr && !sock->has_mcast && !sock->has_udp) {
685 error_report("localaddr= is only valid with mcast= or udp=");
686 return -1;
687 }
42281ac9 688
bef8e8fe
LE
689 if (sock->has_fd) {
690 int fd;
42281ac9 691
1677f4c6 692 fd = monitor_fd_param(cur_mon, sock->fd, &err);
fc13fa00 693 if (fd == -1) {
1677f4c6 694 error_report_err(err);
fc13fa00
SH
695 return -1;
696 }
697 qemu_set_nonblock(fd);
0f8c289a 698 if (!net_socket_fd_init(peer, "socket", name, fd, 1, sock->mcast)) {
42281ac9
MM
699 return -1;
700 }
bef8e8fe
LE
701 return 0;
702 }
42281ac9 703
bef8e8fe 704 if (sock->has_listen) {
d33d93b2 705 if (net_socket_listen_init(peer, "socket", name, sock->listen) == -1) {
42281ac9
MM
706 return -1;
707 }
bef8e8fe
LE
708 return 0;
709 }
42281ac9 710
bef8e8fe 711 if (sock->has_connect) {
d33d93b2 712 if (net_socket_connect_init(peer, "socket", name, sock->connect) ==
bef8e8fe 713 -1) {
42281ac9
MM
714 return -1;
715 }
bef8e8fe
LE
716 return 0;
717 }
42281ac9 718
bef8e8fe
LE
719 if (sock->has_mcast) {
720 /* if sock->localaddr is missing, it has been initialized to "all bits
721 * zero" */
d33d93b2 722 if (net_socket_mcast_init(peer, "socket", name, sock->mcast,
bef8e8fe 723 sock->localaddr) == -1) {
0e0e7fac
BM
724 return -1;
725 }
bef8e8fe
LE
726 return 0;
727 }
0e0e7fac 728
bef8e8fe
LE
729 assert(sock->has_udp);
730 if (!sock->has_localaddr) {
731 error_report("localaddr= is mandatory with udp=");
732 return -1;
733 }
f0e3ac70 734 if (net_socket_udp_init(peer, "socket", name, sock->udp, sock->localaddr) ==
bef8e8fe 735 -1) {
42281ac9
MM
736 return -1;
737 }
42281ac9
MM
738 return 0;
739}