xref: /openbmc/qemu/net/socket.c (revision 5e437d3c)
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  */
24 #include "qemu/osdep.h"
25 
26 #include "net/net.h"
27 #include "clients.h"
28 #include "monitor/monitor.h"
29 #include "qapi/error.h"
30 #include "qemu-common.h"
31 #include "qemu/error-report.h"
32 #include "qemu/option.h"
33 #include "qemu/sockets.h"
34 #include "qemu/iov.h"
35 #include "qemu/main-loop.h"
36 
37 typedef struct NetSocketState {
38     NetClientState nc;
39     int listen_fd;
40     int fd;
41     SocketReadState rs;
42     unsigned int send_index;      /* number of bytes sent (only SOCK_STREAM) */
43     struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
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? */
47 } NetSocketState;
48 
49 static void net_socket_accept(void *opaque);
50 static void net_socket_writable(void *opaque);
51 
52 static void net_socket_update_fd_handler(NetSocketState *s)
53 {
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);
58 }
59 
60 static void net_socket_read_poll(NetSocketState *s, bool enable)
61 {
62     s->read_poll = enable;
63     net_socket_update_fd_handler(s);
64 }
65 
66 static void net_socket_write_poll(NetSocketState *s, bool enable)
67 {
68     s->write_poll = enable;
69     net_socket_update_fd_handler(s);
70 }
71 
72 static 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 }
80 
81 static ssize_t net_socket_receive(NetClientState *nc, const uint8_t *buf, size_t size)
82 {
83     NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
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;
96 
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;
114 }
115 
116 static ssize_t net_socket_receive_dgram(NetClientState *nc, const uint8_t *buf, size_t size)
117 {
118     NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
119     ssize_t ret;
120 
121     do {
122         if (s->dgram_dst.sin_family != AF_UNIX) {
123             ret = qemu_sendto(s->fd, buf, size, 0,
124                               (struct sockaddr *)&s->dgram_dst,
125                               sizeof(s->dgram_dst));
126         } else {
127             ret = send(s->fd, buf, size, 0);
128         }
129     } while (ret == -1 && errno == EINTR);
130 
131     if (ret == -1 && errno == EAGAIN) {
132         net_socket_write_poll(s, true);
133         return 0;
134     }
135     return ret;
136 }
137 
138 static void net_socket_send_completed(NetClientState *nc, ssize_t len)
139 {
140     NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
141 
142     if (!s->read_poll) {
143         net_socket_read_poll(s, true);
144     }
145 }
146 
147 static void net_socket_rs_finalize(SocketReadState *rs)
148 {
149     NetSocketState *s = container_of(rs, NetSocketState, rs);
150 
151     if (qemu_send_packet_async(&s->nc, rs->buf,
152                                rs->packet_len,
153                                net_socket_send_completed) == 0) {
154         net_socket_read_poll(s, false);
155     }
156 }
157 
158 static void net_socket_send(void *opaque)
159 {
160     NetSocketState *s = opaque;
161     int size;
162     int ret;
163     uint8_t buf1[NET_BUFSIZE];
164     const uint8_t *buf;
165 
166     size = qemu_recv(s->fd, buf1, sizeof(buf1), 0);
167     if (size < 0) {
168         if (errno != EWOULDBLOCK)
169             goto eoc;
170     } else if (size == 0) {
171         /* end of connection */
172     eoc:
173         net_socket_read_poll(s, false);
174         net_socket_write_poll(s, false);
175         if (s->listen_fd != -1) {
176             qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
177         }
178         closesocket(s->fd);
179 
180         s->fd = -1;
181         net_socket_rs_init(&s->rs, net_socket_rs_finalize, false);
182         s->nc.link_down = true;
183 
184         return;
185     }
186     buf = buf1;
187 
188     ret = net_fill_rstate(&s->rs, buf, size);
189 
190     if (ret == -1) {
191         goto eoc;
192     }
193 }
194 
195 static void net_socket_send_dgram(void *opaque)
196 {
197     NetSocketState *s = opaque;
198     int size;
199 
200     size = qemu_recv(s->fd, s->rs.buf, sizeof(s->rs.buf), 0);
201     if (size < 0)
202         return;
203     if (size == 0) {
204         /* end of connection */
205         net_socket_read_poll(s, false);
206         net_socket_write_poll(s, false);
207         return;
208     }
209     if (qemu_send_packet_async(&s->nc, s->rs.buf, size,
210                                net_socket_send_completed) == 0) {
211         net_socket_read_poll(s, false);
212     }
213 }
214 
215 static int net_socket_mcast_create(struct sockaddr_in *mcastaddr,
216                                    struct in_addr *localaddr,
217                                    Error **errp)
218 {
219     struct ip_mreq imr;
220     int fd;
221     int val, ret;
222 #ifdef __OpenBSD__
223     unsigned char loop;
224 #else
225     int loop;
226 #endif
227 
228     if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
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));
233         return -1;
234     }
235 
236     fd = qemu_socket(PF_INET, SOCK_DGRAM, 0);
237     if (fd < 0) {
238         error_setg_errno(errp, errno, "can't create datagram socket");
239         return -1;
240     }
241 
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      */
247     val = 1;
248     ret = qemu_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
249     if (ret < 0) {
250         error_setg_errno(errp, errno,
251                          "can't set socket option SO_REUSEADDR");
252         goto fail;
253     }
254 
255     ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
256     if (ret < 0) {
257         error_setg_errno(errp, errno, "can't bind ip=%s to socket",
258                          inet_ntoa(mcastaddr->sin_addr));
259         goto fail;
260     }
261 
262     /* Add host to multicast group */
263     imr.imr_multiaddr = mcastaddr->sin_addr;
264     if (localaddr) {
265         imr.imr_interface = *localaddr;
266     } else {
267         imr.imr_interface.s_addr = htonl(INADDR_ANY);
268     }
269 
270     ret = qemu_setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
271                           &imr, sizeof(struct ip_mreq));
272     if (ret < 0) {
273         error_setg_errno(errp, errno,
274                          "can't add socket to multicast group %s",
275                          inet_ntoa(imr.imr_multiaddr));
276         goto fail;
277     }
278 
279     /* Force mcast msgs to loopback (eg. several QEMUs in same host */
280     loop = 1;
281     ret = qemu_setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
282                           &loop, sizeof(loop));
283     if (ret < 0) {
284         error_setg_errno(errp, errno,
285                          "can't force multicast message to loopback");
286         goto fail;
287     }
288 
289     /* If a bind address is given, only send packets from that address */
290     if (localaddr != NULL) {
291         ret = qemu_setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF,
292                               localaddr, sizeof(*localaddr));
293         if (ret < 0) {
294             error_setg_errno(errp, errno,
295                              "can't set the default network send interface");
296             goto fail;
297         }
298     }
299 
300     qemu_set_nonblock(fd);
301     return fd;
302 fail:
303     if (fd >= 0)
304         closesocket(fd);
305     return -1;
306 }
307 
308 static void net_socket_cleanup(NetClientState *nc)
309 {
310     NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
311     if (s->fd != -1) {
312         net_socket_read_poll(s, false);
313         net_socket_write_poll(s, false);
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     }
322 }
323 
324 static NetClientInfo net_dgram_socket_info = {
325     .type = NET_CLIENT_DRIVER_SOCKET,
326     .size = sizeof(NetSocketState),
327     .receive = net_socket_receive_dgram,
328     .cleanup = net_socket_cleanup,
329 };
330 
331 static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,
332                                                 const char *model,
333                                                 const char *name,
334                                                 int fd, int is_connected,
335                                                 const char *mcast,
336                                                 Error **errp)
337 {
338     struct sockaddr_in saddr;
339     int newfd;
340     NetClientState *nc;
341     NetSocketState *s;
342     SocketAddress *sa;
343     SocketAddressType sa_type;
344     NetdevSocketOptions *stored;
345 
346     sa = socket_local_address(fd, errp);
347     if (!sa) {
348         return NULL;
349     }
350     sa_type = sa->type;
351     qapi_free_SocketAddress(sa);
352 
353     /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
354      * Because this may be "shared" socket from a "master" process, datagrams would be recv()
355      * by ONLY ONE process: we must "clone" this dgram socket --jjo
356      */
357 
358     if (is_connected && mcast != NULL) {
359             if (parse_host_port(&saddr, mcast, errp) < 0) {
360                 goto err;
361             }
362             /* must be bound */
363             if (saddr.sin_addr.s_addr == 0) {
364                 error_setg(errp, "can't setup multicast destination address");
365                 goto err;
366             }
367             /* clone dgram socket */
368             newfd = net_socket_mcast_create(&saddr, NULL, errp);
369             if (newfd < 0) {
370                 goto err;
371             }
372             /* clone newfd to fd, close newfd */
373             dup2(newfd, fd);
374             close(newfd);
375 
376     }
377 
378     nc = qemu_new_net_client(&net_dgram_socket_info, peer, model, name);
379 
380     s = DO_UPCAST(NetSocketState, nc, nc);
381 
382     s->fd = fd;
383     s->listen_fd = -1;
384     s->send_fn = net_socket_send_dgram;
385     net_socket_rs_init(&s->rs, net_socket_rs_finalize, false);
386     net_socket_read_poll(s, true);
387 
388     /* Store startup parameters */
389     nc->stored_config = g_new0(NetdevInfo, 1);
390     nc->stored_config->type = NET_BACKEND_SOCKET;
391     stored = &nc->stored_config->u.socket;
392 
393     stored->has_fd = true;
394     stored->fd = g_strdup_printf("%d", fd);
395 
396     /* mcast: save bound address as dst */
397     if (is_connected && mcast != NULL) {
398         stored->has_mcast = true;
399         stored->mcast = g_strdup(mcast);
400 
401         s->dgram_dst = saddr;
402     } else {
403         if (sa_type == SOCKET_ADDRESS_TYPE_UNIX) {
404             s->dgram_dst.sin_family = AF_UNIX;
405         }
406     }
407 
408     return s;
409 
410 err:
411     closesocket(fd);
412     return NULL;
413 }
414 
415 static void net_socket_connect(void *opaque)
416 {
417     NetSocketState *s = opaque;
418     s->send_fn = net_socket_send;
419     net_socket_read_poll(s, true);
420 }
421 
422 static NetClientInfo net_socket_info = {
423     .type = NET_CLIENT_DRIVER_SOCKET,
424     .size = sizeof(NetSocketState),
425     .receive = net_socket_receive,
426     .cleanup = net_socket_cleanup,
427 };
428 
429 static NetSocketState *net_socket_fd_init_stream(NetClientState *peer,
430                                                  const char *model,
431                                                  const char *name,
432                                                  int fd, int is_connected)
433 {
434     NetClientState *nc;
435     NetSocketState *s;
436     NetdevSocketOptions *stored;
437 
438     nc = qemu_new_net_client(&net_socket_info, peer, model, name);
439 
440     s = DO_UPCAST(NetSocketState, nc, nc);
441 
442     s->fd = fd;
443     s->listen_fd = -1;
444     net_socket_rs_init(&s->rs, net_socket_rs_finalize, false);
445 
446     /* Disable Nagle algorithm on TCP sockets to reduce latency */
447     socket_set_nodelay(fd);
448 
449     if (is_connected) {
450         net_socket_connect(s);
451     } else {
452         qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
453     }
454 
455     /* Store startup parameters */
456     nc->stored_config = g_new0(NetdevInfo, 1);
457     nc->stored_config->type = NET_BACKEND_SOCKET;
458     stored = &nc->stored_config->u.socket;
459 
460     stored->has_fd = true;
461     stored->fd = g_strdup_printf("%d", fd);
462 
463     return s;
464 }
465 
466 static NetSocketState *net_socket_fd_init(NetClientState *peer,
467                                           const char *model, const char *name,
468                                           int fd, int is_connected,
469                                           const char *mc, Error **errp)
470 {
471     int so_type = -1, optlen=sizeof(so_type);
472 
473     if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type,
474         (socklen_t *)&optlen)< 0) {
475         error_setg(errp, "can't get socket option SO_TYPE");
476         closesocket(fd);
477         return NULL;
478     }
479     switch(so_type) {
480     case SOCK_DGRAM:
481         return net_socket_fd_init_dgram(peer, model, name, fd, is_connected,
482                                         mc, errp);
483     case SOCK_STREAM:
484         return net_socket_fd_init_stream(peer, model, name, fd, is_connected);
485     default:
486         error_setg(errp, "socket type=%d for fd=%d must be either"
487                    " SOCK_DGRAM or SOCK_STREAM", so_type, fd);
488         closesocket(fd);
489     }
490     return NULL;
491 }
492 
493 static void net_socket_accept(void *opaque)
494 {
495     NetSocketState *s = opaque;
496     struct sockaddr_in saddr;
497     socklen_t len;
498     int fd;
499     NetdevSocketOptions *stored;
500 
501     for(;;) {
502         len = sizeof(saddr);
503         fd = qemu_accept(s->listen_fd, (struct sockaddr *)&saddr, &len);
504         if (fd < 0 && errno != EINTR) {
505             return;
506         } else if (fd >= 0) {
507             qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
508             break;
509         }
510     }
511 
512     s->fd = fd;
513     s->nc.link_down = false;
514     net_socket_connect(s);
515 
516     /* Store additional startup parameters (extend net_socket_listen_init) */
517     stored = &s->nc.stored_config->u.socket;
518 
519     stored->has_fd = true;
520     stored->fd = g_strdup_printf("%d", fd);
521 }
522 
523 static int net_socket_listen_init(NetClientState *peer,
524                                   const char *model,
525                                   const char *name,
526                                   const char *host_str,
527                                   Error **errp)
528 {
529     NetClientState *nc;
530     NetSocketState *s;
531     struct sockaddr_in saddr;
532     int fd, ret;
533     NetdevSocketOptions *stored;
534 
535     if (parse_host_port(&saddr, host_str, errp) < 0) {
536         return -1;
537     }
538 
539     fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
540     if (fd < 0) {
541         error_setg_errno(errp, errno, "can't create stream socket");
542         return -1;
543     }
544     qemu_set_nonblock(fd);
545 
546     socket_set_fast_reuse(fd);
547 
548     ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
549     if (ret < 0) {
550         error_setg_errno(errp, errno, "can't bind ip=%s to socket",
551                          inet_ntoa(saddr.sin_addr));
552         closesocket(fd);
553         return -1;
554     }
555     ret = listen(fd, 0);
556     if (ret < 0) {
557         error_setg_errno(errp, errno, "can't listen on socket");
558         closesocket(fd);
559         return -1;
560     }
561 
562     nc = qemu_new_net_client(&net_socket_info, peer, model, name);
563     s = DO_UPCAST(NetSocketState, nc, nc);
564     s->fd = -1;
565     s->listen_fd = fd;
566     s->nc.link_down = true;
567     net_socket_rs_init(&s->rs, net_socket_rs_finalize, false);
568 
569     qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
570 
571     /* Store startup parameters */
572     nc->stored_config = g_new0(NetdevInfo, 1);
573     nc->stored_config->type = NET_BACKEND_SOCKET;
574     stored = &nc->stored_config->u.socket;
575 
576     stored->has_listen = true;
577     stored->listen = g_strdup(host_str);
578 
579     return 0;
580 }
581 
582 static int net_socket_connect_init(NetClientState *peer,
583                                    const char *model,
584                                    const char *name,
585                                    const char *host_str,
586                                    Error **errp)
587 {
588     NetSocketState *s;
589     int fd, connected, ret;
590     struct sockaddr_in saddr;
591     NetdevSocketOptions *stored;
592 
593     if (parse_host_port(&saddr, host_str, errp) < 0) {
594         return -1;
595     }
596 
597     fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
598     if (fd < 0) {
599         error_setg_errno(errp, errno, "can't create stream socket");
600         return -1;
601     }
602     qemu_set_nonblock(fd);
603 
604     connected = 0;
605     for(;;) {
606         ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
607         if (ret < 0) {
608             if (errno == EINTR || errno == EWOULDBLOCK) {
609                 /* continue */
610             } else if (errno == EINPROGRESS ||
611                        errno == EALREADY ||
612                        errno == EINVAL) {
613                 break;
614             } else {
615                 error_setg_errno(errp, errno, "can't connect socket");
616                 closesocket(fd);
617                 return -1;
618             }
619         } else {
620             connected = 1;
621             break;
622         }
623     }
624     s = net_socket_fd_init(peer, model, name, fd, connected, NULL, errp);
625     if (!s) {
626         return -1;
627     }
628 
629     /* Store additional startup parameters (extend net_socket_fd_init) */
630     stored = &s->nc.stored_config->u.socket;
631 
632     stored->has_connect = true;
633     stored->connect = g_strdup(host_str);
634 
635     return 0;
636 }
637 
638 static int net_socket_mcast_init(NetClientState *peer,
639                                  const char *model,
640                                  const char *name,
641                                  const char *host_str,
642                                  const char *localaddr_str,
643                                  Error **errp)
644 {
645     NetSocketState *s;
646     int fd;
647     struct sockaddr_in saddr;
648     struct in_addr localaddr, *param_localaddr;
649     NetdevSocketOptions *stored;
650 
651     if (parse_host_port(&saddr, host_str, errp) < 0) {
652         return -1;
653     }
654 
655     if (localaddr_str != NULL) {
656         if (inet_aton(localaddr_str, &localaddr) == 0) {
657             error_setg(errp, "localaddr '%s' is not a valid IPv4 address",
658                        localaddr_str);
659             return -1;
660         }
661         param_localaddr = &localaddr;
662     } else {
663         param_localaddr = NULL;
664     }
665 
666     fd = net_socket_mcast_create(&saddr, param_localaddr, errp);
667     if (fd < 0) {
668         return -1;
669     }
670 
671     s = net_socket_fd_init(peer, model, name, fd, 0, NULL, errp);
672     if (!s) {
673         return -1;
674     }
675 
676     s->dgram_dst = saddr;
677 
678     /* Store additional startup parameters (extend net_socket_fd_init) */
679     stored = &s->nc.stored_config->u.socket;
680 
681     if (!stored->has_mcast) {
682         stored->has_mcast = true;
683         stored->mcast = g_strdup(host_str);
684     }
685 
686     if (localaddr_str) {
687         stored->has_localaddr = true;
688         stored->localaddr = g_strdup(localaddr_str);
689     }
690 
691     return 0;
692 }
693 
694 static int net_socket_udp_init(NetClientState *peer,
695                                  const char *model,
696                                  const char *name,
697                                  const char *rhost,
698                                  const char *lhost,
699                                  Error **errp)
700 {
701     NetSocketState *s;
702     int fd, ret;
703     struct sockaddr_in laddr, raddr;
704     NetdevSocketOptions *stored;
705 
706     if (parse_host_port(&laddr, lhost, errp) < 0) {
707         return -1;
708     }
709 
710     if (parse_host_port(&raddr, rhost, errp) < 0) {
711         return -1;
712     }
713 
714     fd = qemu_socket(PF_INET, SOCK_DGRAM, 0);
715     if (fd < 0) {
716         error_setg_errno(errp, errno, "can't create datagram socket");
717         return -1;
718     }
719 
720     ret = socket_set_fast_reuse(fd);
721     if (ret < 0) {
722         error_setg_errno(errp, errno,
723                          "can't set socket option SO_REUSEADDR");
724         closesocket(fd);
725         return -1;
726     }
727     ret = bind(fd, (struct sockaddr *)&laddr, sizeof(laddr));
728     if (ret < 0) {
729         error_setg_errno(errp, errno, "can't bind ip=%s to socket",
730                          inet_ntoa(laddr.sin_addr));
731         closesocket(fd);
732         return -1;
733     }
734     qemu_set_nonblock(fd);
735 
736     s = net_socket_fd_init(peer, model, name, fd, 0, NULL, errp);
737     if (!s) {
738         return -1;
739     }
740 
741     s->dgram_dst = raddr;
742 
743     /* Store additional startup parameters (extend net_socket_fd_init) */
744     stored = &s->nc.stored_config->u.socket;
745 
746     stored->has_localaddr = true;
747     stored->localaddr = g_strdup(lhost);
748 
749     stored->has_udp = true;
750     stored->udp = g_strdup(rhost);
751 
752     return 0;
753 }
754 
755 int net_init_socket(const Netdev *netdev, const char *name,
756                     NetClientState *peer, Error **errp)
757 {
758     const NetdevSocketOptions *sock;
759 
760     assert(netdev->type == NET_CLIENT_DRIVER_SOCKET);
761     sock = &netdev->u.socket;
762 
763     if (sock->has_fd + sock->has_listen + sock->has_connect + sock->has_mcast +
764         sock->has_udp != 1) {
765         error_setg(errp, "exactly one of listen=, connect=, mcast= or udp="
766                    " is required");
767         return -1;
768     }
769 
770     if (sock->has_localaddr && !sock->has_mcast && !sock->has_udp) {
771         error_setg(errp, "localaddr= is only valid with mcast= or udp=");
772         return -1;
773     }
774 
775     if (sock->has_fd) {
776         int fd, ret;
777 
778         fd = monitor_fd_param(monitor_cur(), sock->fd, errp);
779         if (fd == -1) {
780             return -1;
781         }
782         ret = qemu_try_set_nonblock(fd);
783         if (ret < 0) {
784             error_setg_errno(errp, -ret, "%s: Can't use file descriptor %d",
785                              name, fd);
786             return -1;
787         }
788         if (!net_socket_fd_init(peer, "socket", name, fd, 1, sock->mcast,
789                                 errp)) {
790             return -1;
791         }
792         return 0;
793     }
794 
795     if (sock->has_listen) {
796         if (net_socket_listen_init(peer, "socket", name, sock->listen, errp)
797             < 0) {
798             return -1;
799         }
800         return 0;
801     }
802 
803     if (sock->has_connect) {
804         if (net_socket_connect_init(peer, "socket", name, sock->connect, errp)
805             < 0) {
806             return -1;
807         }
808         return 0;
809     }
810 
811     if (sock->has_mcast) {
812         /* if sock->localaddr is missing, it has been initialized to "all bits
813          * zero" */
814         if (net_socket_mcast_init(peer, "socket", name, sock->mcast,
815                                   sock->localaddr, errp) < 0) {
816             return -1;
817         }
818         return 0;
819     }
820 
821     assert(sock->has_udp);
822     if (!sock->has_localaddr) {
823         error_setg(errp, "localaddr= is mandatory with udp=");
824         return -1;
825     }
826     if (net_socket_udp_init(peer, "socket", name, sock->udp, sock->localaddr,
827                             errp) < 0) {
828         return -1;
829     }
830     return 0;
831 }
832