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 do { 121 if (s->dgram_dst.sin_family != AF_UNIX) { 122 ret = sendto(s->fd, buf, size, 0, 123 (struct sockaddr *)&s->dgram_dst, 124 sizeof(s->dgram_dst)); 125 } else { 126 ret = send(s->fd, buf, size, 0); 127 } 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; 135 } 136 137 static 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 146 static 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 157 static void net_socket_send(void *opaque) 158 { 159 NetSocketState *s = opaque; 160 int size; 161 int ret; 162 uint8_t buf1[NET_BUFSIZE]; 163 const uint8_t *buf; 164 165 size = recv(s->fd, buf1, sizeof(buf1), 0); 166 if (size < 0) { 167 if (errno != EWOULDBLOCK) 168 goto eoc; 169 } else if (size == 0) { 170 /* end of connection */ 171 eoc: 172 net_socket_read_poll(s, false); 173 net_socket_write_poll(s, false); 174 if (s->listen_fd != -1) { 175 qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s); 176 } 177 closesocket(s->fd); 178 179 s->fd = -1; 180 net_socket_rs_init(&s->rs, net_socket_rs_finalize, false); 181 s->nc.link_down = true; 182 qemu_set_info_str(&s->nc, "%s", ""); 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 = 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 = 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 = 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 = 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 = 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_socket_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 345 sa = socket_local_address(fd, errp); 346 if (!sa) { 347 return NULL; 348 } 349 sa_type = sa->type; 350 qapi_free_SocketAddress(sa); 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 357 if (is_connected && mcast != NULL) { 358 if (parse_host_port(&saddr, mcast, errp) < 0) { 359 goto err; 360 } 361 /* must be bound */ 362 if (saddr.sin_addr.s_addr == 0) { 363 error_setg(errp, "can't setup multicast destination address"); 364 goto err; 365 } 366 /* clone dgram socket */ 367 newfd = net_socket_mcast_create(&saddr, NULL, errp); 368 if (newfd < 0) { 369 goto err; 370 } 371 /* clone newfd to fd, close newfd */ 372 dup2(newfd, fd); 373 close(newfd); 374 375 } 376 377 nc = qemu_new_net_client(&net_dgram_socket_info, peer, model, name); 378 379 s = DO_UPCAST(NetSocketState, nc, nc); 380 381 s->fd = fd; 382 s->listen_fd = -1; 383 s->send_fn = net_socket_send_dgram; 384 net_socket_rs_init(&s->rs, net_socket_rs_finalize, false); 385 net_socket_read_poll(s, true); 386 387 /* mcast: save bound address as dst */ 388 if (is_connected && mcast != NULL) { 389 s->dgram_dst = saddr; 390 qemu_set_info_str(nc, "socket: fd=%d (cloned mcast=%s:%d)", fd, 391 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); 392 } else { 393 if (sa_type == SOCKET_ADDRESS_TYPE_UNIX) { 394 s->dgram_dst.sin_family = AF_UNIX; 395 } 396 397 qemu_set_info_str(nc, "socket: fd=%d %s", fd, 398 SocketAddressType_str(sa_type)); 399 } 400 401 return s; 402 403 err: 404 closesocket(fd); 405 return NULL; 406 } 407 408 static void net_socket_connect(void *opaque) 409 { 410 NetSocketState *s = opaque; 411 s->send_fn = net_socket_send; 412 net_socket_read_poll(s, true); 413 } 414 415 static NetClientInfo net_socket_info = { 416 .type = NET_CLIENT_DRIVER_SOCKET, 417 .size = sizeof(NetSocketState), 418 .receive = net_socket_receive, 419 .cleanup = net_socket_cleanup, 420 }; 421 422 static NetSocketState *net_socket_fd_init_stream(NetClientState *peer, 423 const char *model, 424 const char *name, 425 int fd, int is_connected) 426 { 427 NetClientState *nc; 428 NetSocketState *s; 429 430 nc = qemu_new_net_client(&net_socket_info, peer, model, name); 431 432 qemu_set_info_str(nc, "socket: fd=%d", fd); 433 434 s = DO_UPCAST(NetSocketState, nc, nc); 435 436 s->fd = fd; 437 s->listen_fd = -1; 438 net_socket_rs_init(&s->rs, net_socket_rs_finalize, false); 439 440 /* Disable Nagle algorithm on TCP sockets to reduce latency */ 441 socket_set_nodelay(fd); 442 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 451 static NetSocketState *net_socket_fd_init(NetClientState *peer, 452 const char *model, const char *name, 453 int fd, int is_connected, 454 const char *mc, Error **errp) 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) { 460 error_setg(errp, "can't get socket option SO_TYPE"); 461 closesocket(fd); 462 return NULL; 463 } 464 switch(so_type) { 465 case SOCK_DGRAM: 466 return net_socket_fd_init_dgram(peer, model, name, fd, is_connected, 467 mc, errp); 468 case SOCK_STREAM: 469 return net_socket_fd_init_stream(peer, model, name, fd, is_connected); 470 default: 471 error_setg(errp, "socket type=%d for fd=%d must be either" 472 " SOCK_DGRAM or SOCK_STREAM", so_type, fd); 473 closesocket(fd); 474 } 475 return NULL; 476 } 477 478 static void net_socket_accept(void *opaque) 479 { 480 NetSocketState *s = opaque; 481 struct sockaddr_in saddr; 482 socklen_t len; 483 int fd; 484 485 for(;;) { 486 len = sizeof(saddr); 487 fd = qemu_accept(s->listen_fd, (struct sockaddr *)&saddr, &len); 488 if (fd < 0 && errno != EINTR) { 489 return; 490 } else if (fd >= 0) { 491 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL); 492 break; 493 } 494 } 495 496 s->fd = fd; 497 s->nc.link_down = false; 498 net_socket_connect(s); 499 qemu_set_info_str(&s->nc, "socket: connection from %s:%d", 500 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); 501 } 502 503 static int net_socket_listen_init(NetClientState *peer, 504 const char *model, 505 const char *name, 506 const char *host_str, 507 Error **errp) 508 { 509 NetClientState *nc; 510 NetSocketState *s; 511 struct sockaddr_in saddr; 512 int fd, ret; 513 514 if (parse_host_port(&saddr, host_str, errp) < 0) { 515 return -1; 516 } 517 518 fd = qemu_socket(PF_INET, SOCK_STREAM, 0); 519 if (fd < 0) { 520 error_setg_errno(errp, errno, "can't create stream socket"); 521 return -1; 522 } 523 qemu_socket_set_nonblock(fd); 524 525 socket_set_fast_reuse(fd); 526 527 ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)); 528 if (ret < 0) { 529 error_setg_errno(errp, errno, "can't bind ip=%s to socket", 530 inet_ntoa(saddr.sin_addr)); 531 closesocket(fd); 532 return -1; 533 } 534 ret = listen(fd, 0); 535 if (ret < 0) { 536 error_setg_errno(errp, errno, "can't listen on socket"); 537 closesocket(fd); 538 return -1; 539 } 540 541 nc = qemu_new_net_client(&net_socket_info, peer, model, name); 542 s = DO_UPCAST(NetSocketState, nc, nc); 543 s->fd = -1; 544 s->listen_fd = fd; 545 s->nc.link_down = true; 546 net_socket_rs_init(&s->rs, net_socket_rs_finalize, false); 547 548 qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s); 549 return 0; 550 } 551 552 static int net_socket_connect_init(NetClientState *peer, 553 const char *model, 554 const char *name, 555 const char *host_str, 556 Error **errp) 557 { 558 NetSocketState *s; 559 int fd, connected, ret; 560 struct sockaddr_in saddr; 561 562 if (parse_host_port(&saddr, host_str, errp) < 0) { 563 return -1; 564 } 565 566 fd = qemu_socket(PF_INET, SOCK_STREAM, 0); 567 if (fd < 0) { 568 error_setg_errno(errp, errno, "can't create stream socket"); 569 return -1; 570 } 571 qemu_socket_set_nonblock(fd); 572 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 || 580 errno == EALREADY) { 581 break; 582 } else { 583 error_setg_errno(errp, errno, "can't connect socket"); 584 closesocket(fd); 585 return -1; 586 } 587 } else { 588 connected = 1; 589 break; 590 } 591 } 592 s = net_socket_fd_init(peer, model, name, fd, connected, NULL, errp); 593 if (!s) { 594 return -1; 595 } 596 597 qemu_set_info_str(&s->nc, "socket: connect to %s:%d", 598 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); 599 return 0; 600 } 601 602 static int net_socket_mcast_init(NetClientState *peer, 603 const char *model, 604 const char *name, 605 const char *host_str, 606 const char *localaddr_str, 607 Error **errp) 608 { 609 NetSocketState *s; 610 int fd; 611 struct sockaddr_in saddr; 612 struct in_addr localaddr, *param_localaddr; 613 614 if (parse_host_port(&saddr, host_str, errp) < 0) { 615 return -1; 616 } 617 618 if (localaddr_str != NULL) { 619 if (inet_aton(localaddr_str, &localaddr) == 0) { 620 error_setg(errp, "localaddr '%s' is not a valid IPv4 address", 621 localaddr_str); 622 return -1; 623 } 624 param_localaddr = &localaddr; 625 } else { 626 param_localaddr = NULL; 627 } 628 629 fd = net_socket_mcast_create(&saddr, param_localaddr, errp); 630 if (fd < 0) { 631 return -1; 632 } 633 634 s = net_socket_fd_init(peer, model, name, fd, 0, NULL, errp); 635 if (!s) { 636 return -1; 637 } 638 639 s->dgram_dst = saddr; 640 641 qemu_set_info_str(&s->nc, "socket: mcast=%s:%d", 642 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); 643 return 0; 644 645 } 646 647 static int net_socket_udp_init(NetClientState *peer, 648 const char *model, 649 const char *name, 650 const char *rhost, 651 const char *lhost, 652 Error **errp) 653 { 654 NetSocketState *s; 655 int fd, ret; 656 struct sockaddr_in laddr, raddr; 657 658 if (parse_host_port(&laddr, lhost, errp) < 0) { 659 return -1; 660 } 661 662 if (parse_host_port(&raddr, rhost, errp) < 0) { 663 return -1; 664 } 665 666 fd = qemu_socket(PF_INET, SOCK_DGRAM, 0); 667 if (fd < 0) { 668 error_setg_errno(errp, errno, "can't create datagram socket"); 669 return -1; 670 } 671 672 ret = socket_set_fast_reuse(fd); 673 if (ret < 0) { 674 error_setg_errno(errp, errno, 675 "can't set socket option SO_REUSEADDR"); 676 closesocket(fd); 677 return -1; 678 } 679 ret = bind(fd, (struct sockaddr *)&laddr, sizeof(laddr)); 680 if (ret < 0) { 681 error_setg_errno(errp, errno, "can't bind ip=%s to socket", 682 inet_ntoa(laddr.sin_addr)); 683 closesocket(fd); 684 return -1; 685 } 686 qemu_socket_set_nonblock(fd); 687 688 s = net_socket_fd_init(peer, model, name, fd, 0, NULL, errp); 689 if (!s) { 690 return -1; 691 } 692 693 s->dgram_dst = raddr; 694 695 qemu_set_info_str(&s->nc, "socket: udp=%s:%d", inet_ntoa(raddr.sin_addr), 696 ntohs(raddr.sin_port)); 697 return 0; 698 } 699 700 int net_init_socket(const Netdev *netdev, const char *name, 701 NetClientState *peer, Error **errp) 702 { 703 const NetdevSocketOptions *sock; 704 705 assert(netdev->type == NET_CLIENT_DRIVER_SOCKET); 706 sock = &netdev->u.socket; 707 708 if (!!sock->fd + !!sock->listen + !!sock->connect + !!sock->mcast + 709 !!sock->udp != 1) { 710 error_setg(errp, "exactly one of listen=, connect=, mcast= or udp=" 711 " is required"); 712 return -1; 713 } 714 715 if (sock->localaddr && !sock->mcast && !sock->udp) { 716 error_setg(errp, "localaddr= is only valid with mcast= or udp="); 717 return -1; 718 } 719 720 if (sock->fd) { 721 int fd, ret; 722 723 fd = monitor_fd_param(monitor_cur(), sock->fd, errp); 724 if (fd == -1) { 725 return -1; 726 } 727 ret = qemu_socket_try_set_nonblock(fd); 728 if (ret < 0) { 729 error_setg_errno(errp, -ret, "%s: Can't use file descriptor %d", 730 name, fd); 731 return -1; 732 } 733 if (!net_socket_fd_init(peer, "socket", name, fd, 1, sock->mcast, 734 errp)) { 735 return -1; 736 } 737 return 0; 738 } 739 740 if (sock->listen) { 741 if (net_socket_listen_init(peer, "socket", name, sock->listen, errp) 742 < 0) { 743 return -1; 744 } 745 return 0; 746 } 747 748 if (sock->connect) { 749 if (net_socket_connect_init(peer, "socket", name, sock->connect, errp) 750 < 0) { 751 return -1; 752 } 753 return 0; 754 } 755 756 if (sock->mcast) { 757 /* if sock->localaddr is missing, it has been initialized to "all bits 758 * zero" */ 759 if (net_socket_mcast_init(peer, "socket", name, sock->mcast, 760 sock->localaddr, errp) < 0) { 761 return -1; 762 } 763 return 0; 764 } 765 766 assert(sock->udp); 767 if (!sock->localaddr) { 768 error_setg(errp, "localaddr= is mandatory with udp="); 769 return -1; 770 } 771 if (net_socket_udp_init(peer, "socket", name, sock->udp, sock->localaddr, 772 errp) < 0) { 773 return -1; 774 } 775 return 0; 776 } 777