socket.c 21.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * QEMU System Emulator
 *
 * Copyright (c) 2003-2008 Fabrice Bellard
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
#include "net/socket.h"

#include "config-host.h"

#include "net.h"
29
#include "monitor.h"
30 31
#include "qemu-char.h"
#include "qemu-common.h"
32
#include "qemu-error.h"
33 34
#include "qemu-option.h"
#include "qemu_socket.h"
35
#include "iov.h"
36 37

typedef struct NetSocketState {
38
    NetClientState nc;
39
    int listen_fd;
40 41 42 43
    int fd;
    int state; /* 0 = getting length, 1 = getting data */
    unsigned int index;
    unsigned int packet_len;
44
    unsigned int send_index;      /* number of bytes sent (only SOCK_STREAM) */
45 46
    uint8_t buf[4096];
    struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
47 48 49
    IOHandler *send_fn;           /* differs between SOCK_STREAM/SOCK_DGRAM */
    bool read_poll;               /* waiting to receive data? */
    bool write_poll;              /* waiting to transmit data? */
50 51
} NetSocketState;

52
static void net_socket_accept(void *opaque);
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
static void net_socket_writable(void *opaque);

/* Only read packets from socket when peer can receive them */
static int net_socket_can_send(void *opaque)
{
    NetSocketState *s = opaque;

    return qemu_can_send_packet(&s->nc);
}

static void net_socket_update_fd_handler(NetSocketState *s)
{
    qemu_set_fd_handler2(s->fd,
                         s->read_poll  ? net_socket_can_send : NULL,
                         s->read_poll  ? s->send_fn : NULL,
                         s->write_poll ? net_socket_writable : NULL,
                         s);
}

static void net_socket_read_poll(NetSocketState *s, bool enable)
{
    s->read_poll = enable;
    net_socket_update_fd_handler(s);
}

static void net_socket_write_poll(NetSocketState *s, bool enable)
{
    s->write_poll = enable;
    net_socket_update_fd_handler(s);
}

static void net_socket_writable(void *opaque)
{
    NetSocketState *s = opaque;

    net_socket_write_poll(s, false);

    qemu_flush_queued_packets(&s->nc);
}
92

93
static ssize_t net_socket_receive(NetClientState *nc, const uint8_t *buf, size_t size)
94
{
95
    NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
96 97 98 99 100 101 102 103 104 105 106 107
    uint32_t len = htonl(size);
    struct iovec iov[] = {
        {
            .iov_base = &len,
            .iov_len  = sizeof(len),
        }, {
            .iov_base = (void *)buf,
            .iov_len  = size,
        },
    };
    size_t remaining;
    ssize_t ret;
108

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    remaining = iov_size(iov, 2) - s->send_index;
    ret = iov_send(s->fd, iov, 2, s->send_index, remaining);

    if (ret == -1 && errno == EAGAIN) {
        ret = 0; /* handled further down */
    }
    if (ret == -1) {
        s->send_index = 0;
        return -errno;
    }
    if (ret < (ssize_t)remaining) {
        s->send_index += ret;
        net_socket_write_poll(s, true);
        return 0;
    }
    s->send_index = 0;
    return size;
126 127
}

128
static ssize_t net_socket_receive_dgram(NetClientState *nc, const uint8_t *buf, size_t size)
129
{
130
    NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
131 132 133 134 135 136 137 138 139 140 141 142 143
    ssize_t ret;

    do {
        ret = sendto(s->fd, buf, size, 0,
                     (struct sockaddr *)&s->dgram_dst,
                     sizeof(s->dgram_dst));
    } while (ret == -1 && errno == EINTR);

    if (ret == -1 && errno == EAGAIN) {
        net_socket_write_poll(s, true);
        return 0;
    }
    return ret;
144 145 146 147 148 149 150 151 152 153
}

static void net_socket_send(void *opaque)
{
    NetSocketState *s = opaque;
    int size, err;
    unsigned l;
    uint8_t buf1[4096];
    const uint8_t *buf;

B
Blue Swirl 已提交
154
    size = qemu_recv(s->fd, buf1, sizeof(buf1), 0);
155 156 157 158 159 160 161
    if (size < 0) {
        err = socket_error();
        if (err != EWOULDBLOCK)
            goto eoc;
    } else if (size == 0) {
        /* end of connection */
    eoc:
162 163
        net_socket_read_poll(s, false);
        net_socket_write_poll(s, false);
164 165 166
        if (s->listen_fd != -1) {
            qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
        }
167
        closesocket(s->fd);
168 169 170 171 172 173 174 175 176

        s->fd = -1;
        s->state = 0;
        s->index = 0;
        s->packet_len = 0;
        s->nc.link_down = true;
        memset(s->buf, 0, sizeof(s->buf));
        memset(s->nc.info_str, 0, sizeof(s->nc.info_str));

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
        return;
    }
    buf = buf1;
    while (size > 0) {
        /* reassemble a packet from the network */
        switch(s->state) {
        case 0:
            l = 4 - s->index;
            if (l > size)
                l = size;
            memcpy(s->buf + s->index, buf, l);
            buf += l;
            size -= l;
            s->index += l;
            if (s->index == 4) {
                /* got length */
                s->packet_len = ntohl(*(uint32_t *)s->buf);
                s->index = 0;
                s->state = 1;
            }
            break;
        case 1:
            l = s->packet_len - s->index;
            if (l > size)
                l = size;
            if (s->index + l <= sizeof(s->buf)) {
                memcpy(s->buf + s->index, buf, l);
            } else {
                fprintf(stderr, "serious error: oversized packet received,"
                    "connection terminated.\n");
                s->state = 0;
                goto eoc;
            }

            s->index += l;
            buf += l;
            size -= l;
            if (s->index >= s->packet_len) {
215
                qemu_send_packet(&s->nc, s->buf, s->packet_len);
216 217 218 219 220 221 222 223 224 225 226 227 228
                s->index = 0;
                s->state = 0;
            }
            break;
        }
    }
}

static void net_socket_send_dgram(void *opaque)
{
    NetSocketState *s = opaque;
    int size;

B
Blue Swirl 已提交
229
    size = qemu_recv(s->fd, s->buf, sizeof(s->buf), 0);
230 231 232 233
    if (size < 0)
        return;
    if (size == 0) {
        /* end of connection */
234 235
        net_socket_read_poll(s, false);
        net_socket_write_poll(s, false);
236 237
        return;
    }
238
    qemu_send_packet(&s->nc, s->buf, size);
239 240
}

241
static int net_socket_mcast_create(struct sockaddr_in *mcastaddr, struct in_addr *localaddr)
242 243 244 245
{
    struct ip_mreq imr;
    int fd;
    int val, ret;
246 247 248 249 250 251
#ifdef __OpenBSD__
    unsigned char loop;
#else
    int loop;
#endif

252
    if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
S
Stefan Hajnoczi 已提交
253 254 255
        fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) "
                "does not contain a multicast address\n",
                inet_ntoa(mcastaddr->sin_addr),
256
                (int)ntohl(mcastaddr->sin_addr.s_addr));
S
Stefan Hajnoczi 已提交
257
        return -1;
258 259

    }
K
Kevin Wolf 已提交
260
    fd = qemu_socket(PF_INET, SOCK_DGRAM, 0);
261 262 263 264 265 266 267 268 269
    if (fd < 0) {
        perror("socket(PF_INET, SOCK_DGRAM)");
        return -1;
    }

    val = 1;
    ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
                   (const char *)&val, sizeof(val));
    if (ret < 0) {
S
Stefan Hajnoczi 已提交
270 271
        perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
        goto fail;
272 273 274 275 276 277 278 279 280 281
    }

    ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
    if (ret < 0) {
        perror("bind");
        goto fail;
    }

    /* Add host to multicast group */
    imr.imr_multiaddr = mcastaddr->sin_addr;
282 283 284 285 286
    if (localaddr) {
        imr.imr_interface = *localaddr;
    } else {
        imr.imr_interface.s_addr = htonl(INADDR_ANY);
    }
287 288 289 290

    ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
                     (const char *)&imr, sizeof(struct ip_mreq));
    if (ret < 0) {
S
Stefan Hajnoczi 已提交
291 292
        perror("setsockopt(IP_ADD_MEMBERSHIP)");
        goto fail;
293 294 295
    }

    /* Force mcast msgs to loopback (eg. several QEMUs in same host */
296
    loop = 1;
297
    ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
298
                   (const char *)&loop, sizeof(loop));
299
    if (ret < 0) {
S
Stefan Hajnoczi 已提交
300 301
        perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
        goto fail;
302 303
    }

304 305
    /* If a bind address is given, only send packets from that address */
    if (localaddr != NULL) {
B
Blue Swirl 已提交
306 307
        ret = setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF,
                         (const char *)localaddr, sizeof(*localaddr));
308 309 310 311 312 313
        if (ret < 0) {
            perror("setsockopt(IP_MULTICAST_IF)");
            goto fail;
        }
    }

314 315 316 317 318 319 320 321
    socket_set_nonblock(fd);
    return fd;
fail:
    if (fd >= 0)
        closesocket(fd);
    return -1;
}

322
static void net_socket_cleanup(NetClientState *nc)
323
{
324
    NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
325
    if (s->fd != -1) {
326 327
        net_socket_read_poll(s, false);
        net_socket_write_poll(s, false);
328 329 330 331 332 333 334 335
        close(s->fd);
        s->fd = -1;
    }
    if (s->listen_fd != -1) {
        qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
        closesocket(s->listen_fd);
        s->listen_fd = -1;
    }
336 337
}

338
static NetClientInfo net_dgram_socket_info = {
339
    .type = NET_CLIENT_OPTIONS_KIND_SOCKET,
340 341 342 343 344
    .size = sizeof(NetSocketState),
    .receive = net_socket_receive_dgram,
    .cleanup = net_socket_cleanup,
};

345
static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,
346 347 348 349 350 351 352
                                                const char *model,
                                                const char *name,
                                                int fd, int is_connected)
{
    struct sockaddr_in saddr;
    int newfd;
    socklen_t saddr_len;
353
    NetClientState *nc;
354 355 356 357 358 359 360 361
    NetSocketState *s;

    /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
     * Because this may be "shared" socket from a "master" process, datagrams would be recv()
     * by ONLY ONE process: we must "clone" this dgram socket --jjo
     */

    if (is_connected) {
S
Stefan Hajnoczi 已提交
362 363 364 365 366
        if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) {
            /* must be bound */
            if (saddr.sin_addr.s_addr == 0) {
                fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, "
                        "cannot setup multicast dst addr\n", fd);
367
                goto err;
S
Stefan Hajnoczi 已提交
368 369 370 371 372
            }
            /* clone dgram socket */
            newfd = net_socket_mcast_create(&saddr, NULL);
            if (newfd < 0) {
                /* error already reported by net_socket_mcast_create() */
373
                goto err;
S
Stefan Hajnoczi 已提交
374 375 376 377 378 379 380 381 382
            }
            /* clone newfd to fd, close newfd */
            dup2(newfd, fd);
            close(newfd);

        } else {
            fprintf(stderr,
                    "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
                    fd, strerror(errno));
383
            goto err;
S
Stefan Hajnoczi 已提交
384
        }
385 386
    }

387
    nc = qemu_new_net_client(&net_dgram_socket_info, peer, model, name);
388 389

    snprintf(nc->info_str, sizeof(nc->info_str),
S
Stefan Hajnoczi 已提交
390 391 392
            "socket: fd=%d (%s mcast=%s:%d)",
            fd, is_connected ? "cloned" : "",
            inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
393 394 395

    s = DO_UPCAST(NetSocketState, nc, nc);

396
    s->fd = fd;
397
    s->listen_fd = -1;
398 399
    s->send_fn = net_socket_send_dgram;
    net_socket_read_poll(s, true);
400 401

    /* mcast: save bound address as dst */
Z
Zhi Yong Wu 已提交
402 403 404
    if (is_connected) {
        s->dgram_dst = saddr;
    }
405 406

    return s;
407 408 409 410

err:
    closesocket(fd);
    return NULL;
411 412 413 414 415
}

static void net_socket_connect(void *opaque)
{
    NetSocketState *s = opaque;
416 417
    s->send_fn = net_socket_send;
    net_socket_read_poll(s, true);
418 419
}

420
static NetClientInfo net_socket_info = {
421
    .type = NET_CLIENT_OPTIONS_KIND_SOCKET,
422 423 424 425 426
    .size = sizeof(NetSocketState),
    .receive = net_socket_receive,
    .cleanup = net_socket_cleanup,
};

427
static NetSocketState *net_socket_fd_init_stream(NetClientState *peer,
428 429 430 431
                                                 const char *model,
                                                 const char *name,
                                                 int fd, int is_connected)
{
432
    NetClientState *nc;
433
    NetSocketState *s;
434

435
    nc = qemu_new_net_client(&net_socket_info, peer, model, name);
436 437 438 439 440

    snprintf(nc->info_str, sizeof(nc->info_str), "socket: fd=%d", fd);

    s = DO_UPCAST(NetSocketState, nc, nc);

441
    s->fd = fd;
442
    s->listen_fd = -1;
443

444 445 446 447 448 449 450 451
    if (is_connected) {
        net_socket_connect(s);
    } else {
        qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
    }
    return s;
}

452
static NetSocketState *net_socket_fd_init(NetClientState *peer,
453 454 455 456 457 458 459
                                          const char *model, const char *name,
                                          int fd, int is_connected)
{
    int so_type = -1, optlen=sizeof(so_type);

    if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type,
        (socklen_t *)&optlen)< 0) {
S
Stefan Hajnoczi 已提交
460 461
        fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n",
                fd);
462
        closesocket(fd);
S
Stefan Hajnoczi 已提交
463
        return NULL;
464 465 466
    }
    switch(so_type) {
    case SOCK_DGRAM:
467
        return net_socket_fd_init_dgram(peer, model, name, fd, is_connected);
468
    case SOCK_STREAM:
469
        return net_socket_fd_init_stream(peer, model, name, fd, is_connected);
470 471 472
    default:
        /* who knows ... this could be a eg. a pty, do warn and continue as stream */
        fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
473
        return net_socket_fd_init_stream(peer, model, name, fd, is_connected);
474 475 476 477 478 479
    }
    return NULL;
}

static void net_socket_accept(void *opaque)
{
480
    NetSocketState *s = opaque;
481 482 483 484 485 486
    struct sockaddr_in saddr;
    socklen_t len;
    int fd;

    for(;;) {
        len = sizeof(saddr);
487
        fd = qemu_accept(s->listen_fd, (struct sockaddr *)&saddr, &len);
488 489 490
        if (fd < 0 && errno != EINTR) {
            return;
        } else if (fd >= 0) {
491
            qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
492 493 494
            break;
        }
    }
495 496 497 498 499 500 501

    s->fd = fd;
    s->nc.link_down = false;
    net_socket_connect(s);
    snprintf(s->nc.info_str, sizeof(s->nc.info_str),
             "socket: connection from %s:%d",
             inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
502 503
}

504
static int net_socket_listen_init(NetClientState *peer,
505 506 507 508
                                  const char *model,
                                  const char *name,
                                  const char *host_str)
{
509 510
    NetClientState *nc;
    NetSocketState *s;
511
    struct sockaddr_in saddr;
512
    int fd, val, ret;
513 514 515 516

    if (parse_host_port(&saddr, host_str) < 0)
        return -1;

K
Kevin Wolf 已提交
517
    fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
518 519 520 521 522 523 524 525 526 527 528 529 530
    if (fd < 0) {
        perror("socket");
        return -1;
    }
    socket_set_nonblock(fd);

    /* allow fast reuse */
    val = 1;
    setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));

    ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
    if (ret < 0) {
        perror("bind");
531
        closesocket(fd);
532 533 534 535 536
        return -1;
    }
    ret = listen(fd, 0);
    if (ret < 0) {
        perror("listen");
537
        closesocket(fd);
538 539
        return -1;
    }
540 541 542 543 544 545 546 547

    nc = qemu_new_net_client(&net_socket_info, peer, model, name);
    s = DO_UPCAST(NetSocketState, nc, nc);
    s->fd = -1;
    s->listen_fd = fd;
    s->nc.link_down = true;

    qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s);
548 549 550
    return 0;
}

551
static int net_socket_connect_init(NetClientState *peer,
552 553 554 555 556 557 558 559 560 561 562
                                   const char *model,
                                   const char *name,
                                   const char *host_str)
{
    NetSocketState *s;
    int fd, connected, ret, err;
    struct sockaddr_in saddr;

    if (parse_host_port(&saddr, host_str) < 0)
        return -1;

K
Kevin Wolf 已提交
563
    fd = qemu_socket(PF_INET, SOCK_STREAM, 0);
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
    if (fd < 0) {
        perror("socket");
        return -1;
    }
    socket_set_nonblock(fd);

    connected = 0;
    for(;;) {
        ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
        if (ret < 0) {
            err = socket_error();
            if (err == EINTR || err == EWOULDBLOCK) {
            } else if (err == EINPROGRESS) {
                break;
#ifdef _WIN32
579
            } else if (err == WSAEALREADY || err == WSAEINVAL) {
580 581 582 583 584 585 586 587 588 589 590 591
                break;
#endif
            } else {
                perror("connect");
                closesocket(fd);
                return -1;
            }
        } else {
            connected = 1;
            break;
        }
    }
592
    s = net_socket_fd_init(peer, model, name, fd, connected);
593 594
    if (!s)
        return -1;
595
    snprintf(s->nc.info_str, sizeof(s->nc.info_str),
596 597 598 599 600
             "socket: connect to %s:%d",
             inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
    return 0;
}

601
static int net_socket_mcast_init(NetClientState *peer,
602 603
                                 const char *model,
                                 const char *name,
604 605
                                 const char *host_str,
                                 const char *localaddr_str)
606 607 608 609
{
    NetSocketState *s;
    int fd;
    struct sockaddr_in saddr;
610
    struct in_addr localaddr, *param_localaddr;
611 612 613 614

    if (parse_host_port(&saddr, host_str) < 0)
        return -1;

615 616 617 618 619 620 621
    if (localaddr_str != NULL) {
        if (inet_aton(localaddr_str, &localaddr) == 0)
            return -1;
        param_localaddr = &localaddr;
    } else {
        param_localaddr = NULL;
    }
622

623
    fd = net_socket_mcast_create(&saddr, param_localaddr);
624
    if (fd < 0)
S
Stefan Hajnoczi 已提交
625
        return -1;
626

627
    s = net_socket_fd_init(peer, model, name, fd, 0);
628 629 630 631 632
    if (!s)
        return -1;

    s->dgram_dst = saddr;

633
    snprintf(s->nc.info_str, sizeof(s->nc.info_str),
634 635 636 637 638 639
             "socket: mcast=%s:%d",
             inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
    return 0;

}

640
static int net_socket_udp_init(NetClientState *peer,
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
                                 const char *model,
                                 const char *name,
                                 const char *rhost,
                                 const char *lhost)
{
    NetSocketState *s;
    int fd, val, ret;
    struct sockaddr_in laddr, raddr;

    if (parse_host_port(&laddr, lhost) < 0) {
        return -1;
    }

    if (parse_host_port(&raddr, rhost) < 0) {
        return -1;
    }

    fd = qemu_socket(PF_INET, SOCK_DGRAM, 0);
    if (fd < 0) {
        perror("socket(PF_INET, SOCK_DGRAM)");
        return -1;
    }
    val = 1;
    ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
                   (const char *)&val, sizeof(val));
    if (ret < 0) {
        perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
        closesocket(fd);
        return -1;
    }
    ret = bind(fd, (struct sockaddr *)&laddr, sizeof(laddr));
    if (ret < 0) {
        perror("bind");
        closesocket(fd);
        return -1;
    }

678
    s = net_socket_fd_init(peer, model, name, fd, 0);
679 680 681 682 683 684 685 686 687 688 689 690
    if (!s) {
        return -1;
    }

    s->dgram_dst = raddr;

    snprintf(s->nc.info_str, sizeof(s->nc.info_str),
             "socket: udp=%s:%d",
             inet_ntoa(raddr.sin_addr), ntohs(raddr.sin_port));
    return 0;
}

691
int net_init_socket(const NetClientOptions *opts, const char *name,
692
                    NetClientState *peer)
693
{
694
    const NetdevSocketOptions *sock;
695

696 697
    assert(opts->kind == NET_CLIENT_OPTIONS_KIND_SOCKET);
    sock = opts->socket;
698

699 700 701 702 703 704
    if (sock->has_fd + sock->has_listen + sock->has_connect + sock->has_mcast +
        sock->has_udp != 1) {
        error_report("exactly one of fd=, listen=, connect=, mcast= or udp="
                     " is required");
        return -1;
    }
705

706 707 708 709
    if (sock->has_localaddr && !sock->has_mcast && !sock->has_udp) {
        error_report("localaddr= is only valid with mcast= or udp=");
        return -1;
    }
710

711 712
    if (sock->has_fd) {
        int fd;
713

714
        fd = monitor_handle_fd_param(cur_mon, sock->fd);
715
        if (fd == -1 || !net_socket_fd_init(peer, "socket", name, fd, 1)) {
716 717
            return -1;
        }
718 719
        return 0;
    }
720

721
    if (sock->has_listen) {
722
        if (net_socket_listen_init(peer, "socket", name, sock->listen) == -1) {
723 724
            return -1;
        }
725 726
        return 0;
    }
727

728
    if (sock->has_connect) {
729
        if (net_socket_connect_init(peer, "socket", name, sock->connect) ==
730
            -1) {
731 732
            return -1;
        }
733 734
        return 0;
    }
735

736 737 738
    if (sock->has_mcast) {
        /* if sock->localaddr is missing, it has been initialized to "all bits
         * zero" */
739
        if (net_socket_mcast_init(peer, "socket", name, sock->mcast,
740
            sock->localaddr) == -1) {
741 742
            return -1;
        }
743 744
        return 0;
    }
745

746 747 748 749 750
    assert(sock->has_udp);
    if (!sock->has_localaddr) {
        error_report("localaddr= is mandatory with udp=");
        return -1;
    }
751
    if (net_socket_udp_init(peer, "udp", name, sock->udp, sock->localaddr) ==
752
        -1) {
753 754 755 756
        return -1;
    }
    return 0;
}