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