int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp)

{

    struct addrinfo ai,*res,*e;

    const char *addr;

    char port[33];

    char uaddr[INET6_ADDRSTRLEN+1];

    char uport[33];

    int slisten, rc, to, port_min, port_max, p;



    memset(&ai,0, sizeof(ai));

    ai.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;

    ai.ai_family = PF_UNSPEC;

    ai.ai_socktype = SOCK_STREAM;



    if ((qemu_opt_get(opts, "host") == NULL) ||

        (qemu_opt_get(opts, "port") == NULL)) {

        error_setg(errp, "host and/or port not specified");

        return -1;

    }

    pstrcpy(port, sizeof(port), qemu_opt_get(opts, "port"));

    addr = qemu_opt_get(opts, "host");



    to = qemu_opt_get_number(opts, "to", 0);

    if (qemu_opt_get_bool(opts, "ipv4", 0))

        ai.ai_family = PF_INET;

    if (qemu_opt_get_bool(opts, "ipv6", 0))

        ai.ai_family = PF_INET6;



    /* lookup */

    if (port_offset) {

        unsigned long long baseport;

        if (parse_uint_full(port, &baseport, 10) < 0) {

            error_setg(errp, "can't convert to a number: %s", port);

            return -1;

        }

        if (baseport > 65535 ||

            baseport + port_offset > 65535) {

            error_setg(errp, "port %s out of range", port);

            return -1;

        }

        snprintf(port, sizeof(port), "%d", (int)baseport + port_offset);

    }

    rc = getaddrinfo(strlen(addr) ? addr : NULL, port, &ai, &res);

    if (rc != 0) {

        error_setg(errp, "address resolution failed for %s:%s: %s", addr, port,

                   gai_strerror(rc));

        return -1;

    }



    /* create socket + bind */

    for (e = res; e != NULL; e = e->ai_next) {

        getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,

		        uaddr,INET6_ADDRSTRLEN,uport,32,

		        NI_NUMERICHOST | NI_NUMERICSERV);

        slisten = qemu_socket(e->ai_family, e->ai_socktype, e->ai_protocol);

        if (slisten < 0) {

            if (!e->ai_next) {

                error_setg_errno(errp, errno, "Failed to create socket");

            }

            continue;

        }



        socket_set_fast_reuse(slisten);

#ifdef IPV6_V6ONLY

        if (e->ai_family == PF_INET6) {

            /* listen on both ipv4 and ipv6 */

            const int off = 0;

            qemu_setsockopt(slisten, IPPROTO_IPV6, IPV6_V6ONLY, &off,

                            sizeof(off));

        }

#endif



        port_min = inet_getport(e);

        port_max = to ? to + port_offset : port_min;

        for (p = port_min; p <= port_max; p++) {

            inet_setport(e, p);

            if (bind(slisten, e->ai_addr, e->ai_addrlen) == 0) {

                goto listen;

            }

            if (p == port_max) {

                if (!e->ai_next) {

                    error_setg_errno(errp, errno, "Failed to bind socket");

                }

            }

        }

        closesocket(slisten);

    }

    freeaddrinfo(res);

    return -1;



listen:

    if (listen(slisten,1) != 0) {

        error_setg_errno(errp, errno, "Failed to listen on socket");

        closesocket(slisten);

        freeaddrinfo(res);

        return -1;

    }

    qemu_opt_set(opts, "host", uaddr, &error_abort);

    qemu_opt_set_number(opts, "port", inet_getport(e) - port_offset,

                        &error_abort);

    qemu_opt_set_bool(opts, "ipv6", e->ai_family == PF_INET6,

                      &error_abort);

    qemu_opt_set_bool(opts, "ipv4", e->ai_family != PF_INET6,

                      &error_abort);

    freeaddrinfo(res);

    return slisten;

}

int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp)

{

    struct addrinfo ai,*res,*e;

    const char *addr;

    char port[33];

    char uaddr[INET6_ADDRSTRLEN+1];

    char uport[33];

    int slisten, rc, to, port_min, port_max, p;



    memset(&ai,0, sizeof(ai));

    ai.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;

    ai.ai_family = PF_UNSPEC;

    ai.ai_socktype = SOCK_STREAM;



    if ((qemu_opt_get(opts, "host") == NULL) ||

        (qemu_opt_get(opts, "port") == NULL)) {

        error_setg(errp, "host and/or port not specified");

        return -1;

    }

    pstrcpy(port, sizeof(port), qemu_opt_get(opts, "port"));

    addr = qemu_opt_get(opts, "host");



    to = qemu_opt_get_number(opts, "to", 0);

    if (qemu_opt_get_bool(opts, "ipv4", 0))

        ai.ai_family = PF_INET;

    if (qemu_opt_get_bool(opts, "ipv6", 0))

        ai.ai_family = PF_INET6;



    

    if (port_offset) {

        unsigned long long baseport;

        if (parse_uint_full(port, &baseport, 10) < 0) {

            error_setg(errp, "can't convert to a number: %s", port);

            return -1;

        }

        if (baseport > 65535 ||

            baseport + port_offset > 65535) {

            error_setg(errp, "port %s out of range", port);

            return -1;

        }

        snprintf(port, sizeof(port), "%d", (int)baseport + port_offset);

    }

    rc = getaddrinfo(strlen(addr) ? addr : NULL, port, &ai, &res);

    if (rc != 0) {

        error_setg(errp, "address resolution failed for %s:%s: %s", addr, port,

                   gai_strerror(rc));

        return -1;

    }



    

    for (e = res; e != NULL; e = e->ai_next) {

        getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,

		        uaddr,INET6_ADDRSTRLEN,uport,32,

		        NI_NUMERICHOST | NI_NUMERICSERV);

        slisten = qemu_socket(e->ai_family, e->ai_socktype, e->ai_protocol);

        if (slisten < 0) {

            if (!e->ai_next) {

                error_setg_errno(errp, errno, "Failed to create socket");

            }

            continue;

        }



        socket_set_fast_reuse(slisten);

#ifdef IPV6_V6ONLY

        if (e->ai_family == PF_INET6) {

            

            const int off = 0;

            qemu_setsockopt(slisten, IPPROTO_IPV6, IPV6_V6ONLY, &off,

                            sizeof(off));

        }

#endif



        port_min = inet_getport(e);

        port_max = to ? to + port_offset : port_min;

        for (p = port_min; p <= port_max; p++) {

            inet_setport(e, p);

            if (bind(slisten, e->ai_addr, e->ai_addrlen) == 0) {

                goto listen;

            }

            if (p == port_max) {

                if (!e->ai_next) {

                    error_setg_errno(errp, errno, "Failed to bind socket");

                }

            }

        }

        closesocket(slisten);

    }

    freeaddrinfo(res);

    return -1;



listen:

    if (listen(slisten,1) != 0) {

        error_setg_errno(errp, errno, "Failed to listen on socket");

        closesocket(slisten);

        freeaddrinfo(res);

        return -1;

    }

    qemu_opt_set(opts, "host", uaddr, &error_abort);

    qemu_opt_set_number(opts, "port", inet_getport(e) - port_offset,

                        &error_abort);

    qemu_opt_set_bool(opts, "ipv6", e->ai_family == PF_INET6,

                      &error_abort);

    qemu_opt_set_bool(opts, "ipv4", e->ai_family != PF_INET6,

                      &error_abort);

    freeaddrinfo(res);

    return slisten;

}

    ai.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
int FUNC_0(QemuOpts *VAR_0, int VAR_1, Error **VAR_2)

{

    struct addrinfo VAR_3,*VAR_4,*VAR_5;

    const char *VAR_6;

    char VAR_7[33];

    char VAR_8[INET6_ADDRSTRLEN+1];

    char VAR_9[33];

    int VAR_10, VAR_11, VAR_12, VAR_13, VAR_14, VAR_15;



    memset(&VAR_3,0, sizeof(VAR_3));

    VAR_3.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;

    VAR_3.ai_family = PF_UNSPEC;

    VAR_3.ai_socktype = SOCK_STREAM;



    if ((qemu_opt_get(VAR_0, "host") == NULL) ||

        (qemu_opt_get(VAR_0, "VAR_7") == NULL)) {

        error_setg(VAR_2, "host and/or VAR_7 not specified");

        return -1;

    }

    pstrcpy(VAR_7, sizeof(VAR_7), qemu_opt_get(VAR_0, "VAR_7"));

    VAR_6 = qemu_opt_get(VAR_0, "host");



    VAR_12 = qemu_opt_get_number(VAR_0, "VAR_12", 0);

    if (qemu_opt_get_bool(VAR_0, "ipv4", 0))

        VAR_3.ai_family = PF_INET;

    if (qemu_opt_get_bool(VAR_0, "ipv6", 0))

        VAR_3.ai_family = PF_INET6;



    

    if (VAR_1) {

        unsigned long long VAR_16;

        if (parse_uint_full(VAR_7, &VAR_16, 10) < 0) {

            error_setg(VAR_2, "can't convert VAR_12 a number: %s", VAR_7);

            return -1;

        }

        if (VAR_16 > 65535 ||

            VAR_16 + VAR_1 > 65535) {

            error_setg(VAR_2, "VAR_7 %s out of range", VAR_7);

            return -1;

        }

        snprintf(VAR_7, sizeof(VAR_7), "%d", (int)VAR_16 + VAR_1);

    }

    VAR_11 = getaddrinfo(strlen(VAR_6) ? VAR_6 : NULL, VAR_7, &VAR_3, &VAR_4);

    if (VAR_11 != 0) {

        error_setg(VAR_2, "address resolution failed for %s:%s: %s", VAR_6, VAR_7,

                   gai_strerror(VAR_11));

        return -1;

    }



    

    for (VAR_5 = VAR_4; VAR_5 != NULL; VAR_5 = VAR_5->ai_next) {

        getnameinfo((struct sockaddr*)VAR_5->ai_addr,VAR_5->ai_addrlen,

		        VAR_8,INET6_ADDRSTRLEN,VAR_9,32,

		        NI_NUMERICHOST | NI_NUMERICSERV);

        VAR_10 = qemu_socket(VAR_5->ai_family, VAR_5->ai_socktype, VAR_5->ai_protocol);

        if (VAR_10 < 0) {

            if (!VAR_5->ai_next) {

                error_setg_errno(VAR_2, errno, "Failed VAR_12 create socket");

            }

            continue;

        }



        socket_set_fast_reuse(VAR_10);

#ifdef IPV6_V6ONLY

        if (VAR_5->ai_family == PF_INET6) {

            

            const int off = 0;

            qemu_setsockopt(VAR_10, IPPROTO_IPV6, IPV6_V6ONLY, &off,

                            sizeof(off));

        }

#endif



        VAR_13 = inet_getport(VAR_5);

        VAR_14 = VAR_12 ? VAR_12 + VAR_1 : VAR_13;

        for (VAR_15 = VAR_13; VAR_15 <= VAR_14; VAR_15++) {

            inet_setport(VAR_5, VAR_15);

            if (bind(VAR_10, VAR_5->ai_addr, VAR_5->ai_addrlen) == 0) {

                goto listen;

            }

            if (VAR_15 == VAR_14) {

                if (!VAR_5->ai_next) {

                    error_setg_errno(VAR_2, errno, "Failed VAR_12 bind socket");

                }

            }

        }

        closesocket(VAR_10);

    }

    freeaddrinfo(VAR_4);

    return -1;



listen:

    if (listen(VAR_10,1) != 0) {

        error_setg_errno(VAR_2, errno, "Failed VAR_12 listen on socket");

        closesocket(VAR_10);

        freeaddrinfo(VAR_4);

        return -1;

    }

    qemu_opt_set(VAR_0, "host", VAR_8, &error_abort);

    qemu_opt_set_number(VAR_0, "VAR_7", inet_getport(VAR_5) - VAR_1,

                        &error_abort);

    qemu_opt_set_bool(VAR_0, "ipv6", VAR_5->ai_family == PF_INET6,

                      &error_abort);

    qemu_opt_set_bool(VAR_0, "ipv4", VAR_5->ai_family != PF_INET6,

                      &error_abort);

    freeaddrinfo(VAR_4);

    return VAR_10;

}

int FUNC_0(QemuOpts *VAR_0, int VAR_1, Error **VAR_2)
{ struct addrinfo VAR_3,*VAR_4,*VAR_5; const char *VAR_6; char VAR_7[33]; char VAR_8[INET6_ADDRSTRLEN+1]; char VAR_9[33]; int VAR_10, VAR_11, VAR_12, VAR_13, VAR_14, VAR_15; memset(&VAR_3,0, sizeof(VAR_3)); VAR_3.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; VAR_3.ai_family = PF_UNSPEC; VAR_3.ai_socktype = SOCK_STREAM; if ((qemu_opt_get(VAR_0, "host") == NULL) ||
(qemu_opt_get(VAR_0, "VAR_7") == NULL)) { error_setg(VAR_2, "host and/or VAR_7 not specified"); return -1; } pstrcpy(VAR_7, sizeof(VAR_7), qemu_opt_get(VAR_0, "VAR_7")); VAR_6 = qemu_opt_get(VAR_0, "host"); VAR_12 = qemu_opt_get_number(VAR_0, "VAR_12", 0); if (qemu_opt_get_bool(VAR_0, "ipv4", 0))
VAR_3.ai_family = PF_INET; if (qemu_opt_get_bool(VAR_0, "ipv6", 0))
VAR_3.ai_family = PF_INET6; if (VAR_1) { unsigned long long VAR_16; if (parse_uint_full(VAR_7, &VAR_16, 10) < 0) { error_setg(VAR_2, "can't convert VAR_12 a number: %s", VAR_7); return -1; } if (VAR_16 > 65535 ||
VAR_16 + VAR_1 > 65535) { error_setg(VAR_2, "VAR_7 %s out of range", VAR_7); return -1; } snprintf(VAR_7, sizeof(VAR_7), "%d", (int)VAR_16 + VAR_1); } VAR_11 = getaddrinfo(strlen(VAR_6) ? VAR_6 : NULL, VAR_7, &VAR_3, &VAR_4); if (VAR_11 != 0) { error_setg(VAR_2, "address resolution failed for %s:%s: %s", VAR_6, VAR_7,
gai_strerror(VAR_11)); return -1; } for (VAR_5 = VAR_4; VAR_5 != NULL; VAR_5 = VAR_5->ai_next) { getnameinfo((struct sockaddr*)VAR_5->ai_addr,VAR_5->ai_addrlen,
VAR_8,INET6_ADDRSTRLEN,VAR_9,32,
NI_NUMERICHOST | NI_NUMERICSERV); VAR_10 = qemu_socket(VAR_5->ai_family, VAR_5->ai_socktype, VAR_5->ai_protocol); if (VAR_10 < 0) { if (!VAR_5->ai_next) { error_setg_errno(VAR_2, errno, "Failed VAR_12 create socket"); } continue; } socket_set_fast_reuse(VAR_10); #ifdef IPV6_V6ONLY
if (VAR_5->ai_family == PF_INET6) { const int off = 0; qemu_setsockopt(VAR_10, IPPROTO_IPV6, IPV6_V6ONLY, &off,
sizeof(off)); } #endif
VAR_13 = inet_getport(VAR_5); VAR_14 = VAR_12 ? VAR_12 + VAR_1 : VAR_13; for (VAR_15 = VAR_13; VAR_15 <= VAR_14; VAR_15++) { inet_setport(VAR_5, VAR_15); if (bind(VAR_10, VAR_5->ai_addr, VAR_5->ai_addrlen) == 0) { goto listen; } if (VAR_15 == VAR_14) { if (!VAR_5->ai_next) { error_setg_errno(VAR_2, errno, "Failed VAR_12 bind socket"); } } } closesocket(VAR_10); } freeaddrinfo(VAR_4); return -1; listen:
if (listen(VAR_10,1) != 0) { error_setg_errno(VAR_2, errno, "Failed VAR_12 listen on socket"); closesocket(VAR_10); freeaddrinfo(VAR_4); return -1; } qemu_opt_set(VAR_0, "host", VAR_8, &error_abort); qemu_opt_set_number(VAR_0, "VAR_7", inet_getport(VAR_5) - VAR_1,
&error_abort); qemu_opt_set_bool(VAR_0, "ipv6", VAR_5->ai_family == PF_INET6,
&error_abort); qemu_opt_set_bool(VAR_0, "ipv4", VAR_5->ai_family != PF_INET6,
&error_abort); freeaddrinfo(VAR_4); return VAR_10; }