static int inet_listen_saddr(InetSocketAddress *saddr,

                             int port_offset,

                             bool update_addr,

                             Error **errp)

{

    struct addrinfo ai,*res,*e;

    char port[33];

    char uaddr[INET6_ADDRSTRLEN+1];

    char uport[33];

    int rc, port_min, port_max, p;

    int slisten = 0;

    int saved_errno = 0;

    bool socket_created = false;

    Error *err = NULL;



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

    ai.ai_flags = AI_PASSIVE;

    if (saddr->has_numeric && saddr->numeric) {

        ai.ai_flags |= AI_NUMERICHOST | AI_NUMERICSERV;

    }

    ai.ai_family = inet_ai_family_from_address(saddr, &err);

    ai.ai_socktype = SOCK_STREAM;



    if (err) {

        error_propagate(errp, err);

        return -1;

    }



    if (saddr->host == NULL) {

        error_setg(errp, "host not specified");

        return -1;

    }

    if (saddr->port != NULL) {

        pstrcpy(port, sizeof(port), saddr->port);

    } else {

        port[0] = '\0';

    }



    /* lookup */

    if (port_offset) {

        unsigned long long baseport;

        if (strlen(port) == 0) {

            error_setg(errp, "port not specified");

            return -1;

        }

        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(saddr->host) ? saddr->host : NULL,

                     strlen(port) ? port : NULL, &ai, &res);

    if (rc != 0) {

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

                   saddr->host, port, gai_strerror(rc));

        return -1;

    }



    /* create socket + bind/listen */

    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 = create_fast_reuse_socket(e);

        if (slisten < 0) {

            continue;

        }



        socket_created = true;

        port_min = inet_getport(e);

        port_max = saddr->has_to ? saddr->to + port_offset : port_min;

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

            inet_setport(e, p);

            rc = try_bind(slisten, saddr, e);

            if (rc) {

                if (errno == EADDRINUSE) {

                    continue;

                } else {

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

                    goto listen_failed;

                }

            }

            if (!listen(slisten, 1)) {

                goto listen_ok;

            }

            if (errno != EADDRINUSE) {

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

                goto listen_failed;

            }

            /* Someone else managed to bind to the same port and beat us

             * to listen on it! Socket semantics does not allow us to

             * recover from this situation, so we need to recreate the

             * socket to allow bind attempts for subsequent ports:

             */

            closesocket(slisten);

            slisten = create_fast_reuse_socket(e);

            if (slisten < 0) {

                error_setg_errno(errp, errno,

                                 "Failed to recreate failed listening socket");

                goto listen_failed;

            }

        }

    }

    error_setg_errno(errp, errno,

                     socket_created ?

                     "Failed to find an available port" :

                     "Failed to create a socket");

listen_failed:

    saved_errno = errno;

    if (slisten >= 0) {

        closesocket(slisten);

    }

    freeaddrinfo(res);

    errno = saved_errno;

    return -1;



listen_ok:

    if (update_addr) {

        g_free(saddr->host);

        saddr->host = g_strdup(uaddr);

        g_free(saddr->port);

        saddr->port = g_strdup_printf("%d",

                                      inet_getport(e) - port_offset);

        saddr->has_ipv6 = saddr->ipv6 = e->ai_family == PF_INET6;

        saddr->has_ipv4 = saddr->ipv4 = e->ai_family != PF_INET6;

    }

    freeaddrinfo(res);

    return slisten;

}

static int inet_listen_saddr(InetSocketAddress *saddr,

                             int port_offset,

                             bool update_addr,

                             Error **errp)

{

    struct addrinfo ai,*res,*e;

    char port[33];

    char uaddr[INET6_ADDRSTRLEN+1];

    char uport[33];

    int rc, port_min, port_max, p;

    int slisten = 0;

    int saved_errno = 0;

    bool socket_created = false;

    Error *err = NULL;



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

    ai.ai_flags = AI_PASSIVE;

    if (saddr->has_numeric && saddr->numeric) {

        ai.ai_flags |= AI_NUMERICHOST | AI_NUMERICSERV;

    }

    ai.ai_family = inet_ai_family_from_address(saddr, &err);

    ai.ai_socktype = SOCK_STREAM;



    if (err) {

        error_propagate(errp, err);

        return -1;

    }



    if (saddr->host == NULL) {

        error_setg(errp, "host not specified");

        return -1;

    }

    if (saddr->port != NULL) {

        pstrcpy(port, sizeof(port), saddr->port);

    } else {

        port[0] = '\0';

    }



    

    if (port_offset) {

        unsigned long long baseport;

        if (strlen(port) == 0) {

            error_setg(errp, "port not specified");

            return -1;

        }

        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(saddr->host) ? saddr->host : NULL,

                     strlen(port) ? port : NULL, &ai, &res);

    if (rc != 0) {

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

                   saddr->host, 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 = create_fast_reuse_socket(e);

        if (slisten < 0) {

            continue;

        }



        socket_created = true;

        port_min = inet_getport(e);

        port_max = saddr->has_to ? saddr->to + port_offset : port_min;

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

            inet_setport(e, p);

            rc = try_bind(slisten, saddr, e);

            if (rc) {

                if (errno == EADDRINUSE) {

                    continue;

                } else {

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

                    goto listen_failed;

                }

            }

            if (!listen(slisten, 1)) {

                goto listen_ok;

            }

            if (errno != EADDRINUSE) {

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

                goto listen_failed;

            }

            









            closesocket(slisten);

            slisten = create_fast_reuse_socket(e);

            if (slisten < 0) {

                error_setg_errno(errp, errno,

                                 "Failed to recreate failed listening socket");

                goto listen_failed;

            }

        }

    }

    error_setg_errno(errp, errno,

                     socket_created ?

                     "Failed to find an available port" :

                     "Failed to create a socket");

listen_failed:

    saved_errno = errno;

    if (slisten >= 0) {

        closesocket(slisten);

    }

    freeaddrinfo(res);

    errno = saved_errno;

    return -1;



listen_ok:

    if (update_addr) {

        g_free(saddr->host);

        saddr->host = g_strdup(uaddr);

        g_free(saddr->port);

        saddr->port = g_strdup_printf("%d",

                                      inet_getport(e) - port_offset);

        saddr->has_ipv6 = saddr->ipv6 = e->ai_family == PF_INET6;

        saddr->has_ipv4 = saddr->ipv4 = e->ai_family != PF_INET6;

    }

    freeaddrinfo(res);

    return slisten;

}

    int slisten = 0;         slisten = create_fast_reuse_socket(e);         if (slisten < 0) {             continue;         socket_created = true;             rc = try_bind(slisten, saddr, e);             if (rc) {                 if (errno == EADDRINUSE) {                     error_setg_errno(errp, errno, "Failed to bind socket");             if (!listen(slisten, 1)) {                 goto listen_ok;             if (errno != EADDRINUSE) {                 error_setg_errno(errp, errno, "Failed to listen on socket");                 goto listen_failed;             slisten = create_fast_reuse_socket(e);             if (slisten < 0) {                 error_setg_errno(errp, errno,                                  "Failed to recreate failed listening socket");                 goto listen_failed;
static int FUNC_0(InetSocketAddress *VAR_0,

                             int VAR_1,

                             bool VAR_2,

                             Error **VAR_3)

{

    struct addrinfo VAR_4,*VAR_5,*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;

    int VAR_14 = 0;

    int VAR_15 = 0;

    bool socket_created = false;

    Error *err = NULL;



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

    VAR_4.ai_flags = AI_PASSIVE;

    if (VAR_0->has_numeric && VAR_0->numeric) {

        VAR_4.ai_flags |= AI_NUMERICHOST | AI_NUMERICSERV;

    }

    VAR_4.ai_family = inet_ai_family_from_address(VAR_0, &err);

    VAR_4.ai_socktype = SOCK_STREAM;



    if (err) {

        error_propagate(VAR_3, err);

        return -1;

    }



    if (VAR_0->host == NULL) {

        error_setg(VAR_3, "host not specified");

        return -1;

    }

    if (VAR_0->VAR_7 != NULL) {

        pstrcpy(VAR_7, sizeof(VAR_7), VAR_0->VAR_7);

    } else {

        VAR_7[0] = '\0';

    }



    

    if (VAR_1) {

        unsigned long long VAR_16;

        if (strlen(VAR_7) == 0) {

            error_setg(VAR_3, "VAR_7 not specified");

            return -1;

        }

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

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

            return -1;

        }

        if (VAR_16 > 65535 ||

            VAR_16 + VAR_1 > 65535) {

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

            return -1;

        }

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

    }

    VAR_10 = getaddrinfo(strlen(VAR_0->host) ? VAR_0->host : NULL,

                     strlen(VAR_7) ? VAR_7 : NULL, &VAR_4, &VAR_5);

    if (VAR_10 != 0) {

        error_setg(VAR_3, "address resolution failed for %s:%s: %s",

                   VAR_0->host, VAR_7, gai_strerror(VAR_10));

        return -1;

    }



    

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

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

		        VAR_8,INET6_ADDRSTRLEN,VAR_9,32,

		        NI_NUMERICHOST | NI_NUMERICSERV);



        VAR_14 = create_fast_reuse_socket(VAR_6);

        if (VAR_14 < 0) {

            continue;

        }



        socket_created = true;

        VAR_11 = inet_getport(VAR_6);

        VAR_12 = VAR_0->has_to ? VAR_0->to + VAR_1 : VAR_11;

        for (VAR_13 = VAR_11; VAR_13 <= VAR_12; VAR_13++) {

            inet_setport(VAR_6, VAR_13);

            VAR_10 = try_bind(VAR_14, VAR_0, VAR_6);

            if (VAR_10) {

                if (errno == EADDRINUSE) {

                    continue;

                } else {

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

                    goto listen_failed;

                }

            }

            if (!listen(VAR_14, 1)) {

                goto listen_ok;

            }

            if (errno != EADDRINUSE) {

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

                goto listen_failed;

            }

            









            closesocket(VAR_14);

            VAR_14 = create_fast_reuse_socket(VAR_6);

            if (VAR_14 < 0) {

                error_setg_errno(VAR_3, errno,

                                 "Failed to recreate failed listening socket");

                goto listen_failed;

            }

        }

    }

    error_setg_errno(VAR_3, errno,

                     socket_created ?

                     "Failed to find an available VAR_7" :

                     "Failed to create a socket");

listen_failed:

    VAR_15 = errno;

    if (VAR_14 >= 0) {

        closesocket(VAR_14);

    }

    freeaddrinfo(VAR_5);

    errno = VAR_15;

    return -1;



listen_ok:

    if (VAR_2) {

        g_free(VAR_0->host);

        VAR_0->host = g_strdup(VAR_8);

        g_free(VAR_0->VAR_7);

        VAR_0->VAR_7 = g_strdup_printf("%d",

                                      inet_getport(VAR_6) - VAR_1);

        VAR_0->has_ipv6 = VAR_0->ipv6 = VAR_6->ai_family == PF_INET6;

        VAR_0->has_ipv4 = VAR_0->ipv4 = VAR_6->ai_family != PF_INET6;

    }

    freeaddrinfo(VAR_5);

    return VAR_14;

}

static int FUNC_0(InetSocketAddress *VAR_0,
int VAR_1,
bool VAR_2,
Error **VAR_3)
{ struct addrinfo VAR_4,*VAR_5,*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; int VAR_14 = 0; int VAR_15 = 0; bool socket_created = false; Error *err = NULL; memset(&VAR_4,0, sizeof(VAR_4)); VAR_4.ai_flags = AI_PASSIVE; if (VAR_0->has_numeric && VAR_0->numeric) { VAR_4.ai_flags |= AI_NUMERICHOST | AI_NUMERICSERV; } VAR_4.ai_family = inet_ai_family_from_address(VAR_0, &err); VAR_4.ai_socktype = SOCK_STREAM; if (err) { error_propagate(VAR_3, err); return -1; } if (VAR_0->host == NULL) { error_setg(VAR_3, "host not specified"); return -1; } if (VAR_0->VAR_7 != NULL) { pstrcpy(VAR_7, sizeof(VAR_7), VAR_0->VAR_7); } else { VAR_7[0] = '\0'; } if (VAR_1) { unsigned long long VAR_16; if (strlen(VAR_7) == 0) { error_setg(VAR_3, "VAR_7 not specified"); return -1; } if (parse_uint_full(VAR_7, &VAR_16, 10) < 0) { error_setg(VAR_3, "can't convert to a number: %s", VAR_7); return -1; } if (VAR_16 > 65535 ||
VAR_16 + VAR_1 > 65535) { error_setg(VAR_3, "VAR_7 %s out of range", VAR_7); return -1; } snprintf(VAR_7, sizeof(VAR_7), "%d", (int)VAR_16 + VAR_1); } VAR_10 = getaddrinfo(strlen(VAR_0->host) ? VAR_0->host : NULL,
strlen(VAR_7) ? VAR_7 : NULL, &VAR_4, &VAR_5); if (VAR_10 != 0) { error_setg(VAR_3, "address resolution failed for %s:%s: %s",
VAR_0->host, VAR_7, gai_strerror(VAR_10)); return -1; } for (VAR_6 = VAR_5; VAR_6 != NULL; VAR_6 = VAR_6->ai_next) { getnameinfo((struct sockaddr*)VAR_6->ai_addr,VAR_6->ai_addrlen,
VAR_8,INET6_ADDRSTRLEN,VAR_9,32,
NI_NUMERICHOST | NI_NUMERICSERV); VAR_14 = create_fast_reuse_socket(VAR_6); if (VAR_14 < 0) { continue; } socket_created = true; VAR_11 = inet_getport(VAR_6); VAR_12 = VAR_0->has_to ? VAR_0->to + VAR_1 : VAR_11; for (VAR_13 = VAR_11; VAR_13 <= VAR_12; VAR_13++) { inet_setport(VAR_6, VAR_13); VAR_10 = try_bind(VAR_14, VAR_0, VAR_6); if (VAR_10) { if (errno == EADDRINUSE) { continue; } else { error_setg_errno(VAR_3, errno, "Failed to bind socket"); goto listen_failed; } } if (!listen(VAR_14, 1)) { goto listen_ok; } if (errno != EADDRINUSE) { error_setg_errno(VAR_3, errno, "Failed to listen on socket"); goto listen_failed; } closesocket(VAR_14); VAR_14 = create_fast_reuse_socket(VAR_6); if (VAR_14 < 0) { error_setg_errno(VAR_3, errno,
"Failed to recreate failed listening socket"); goto listen_failed; } } } error_setg_errno(VAR_3, errno,
socket_created ?
"Failed to find an available VAR_7" :
"Failed to create a socket"); listen_failed:
VAR_15 = errno; if (VAR_14 >= 0) { closesocket(VAR_14); } freeaddrinfo(VAR_5); errno = VAR_15; return -1; listen_ok:
if (VAR_2) { g_free(VAR_0->host); VAR_0->host = g_strdup(VAR_8); g_free(VAR_0->VAR_7); VAR_0->VAR_7 = g_strdup_printf("%d",
inet_getport(VAR_6) - VAR_1); VAR_0->has_ipv6 = VAR_0->ipv6 = VAR_6->ai_family == PF_INET6; VAR_0->has_ipv4 = VAR_0->ipv4 = VAR_6->ai_family != PF_INET6; } freeaddrinfo(VAR_5); return VAR_14; }