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