int inet_listen_opts(QemuOpts *opts, int port_offset)

{

    struct addrinfo ai,*res,*e;

    const char *addr;

    char port[33];

    char uaddr[INET6_ADDRSTRLEN+1];

    char uport[33];

    int slisten,rc,to,try_next;



    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, "port") == NULL) {

        fprintf(stderr, "%s: host and/or port not specified\n", __FUNCTION__);

        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)

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

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

    if (rc != 0) {

        fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,

                gai_strerror(rc));

        return -1;

    }

    if (sockets_debug)

        inet_print_addrinfo(__FUNCTION__, res);



    /* 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 = socket(e->ai_family, e->ai_socktype, e->ai_protocol);

        if (slisten < 0) {

            fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,

                    inet_strfamily(e->ai_family), strerror(errno));

            continue;

        }



        setsockopt(slisten,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));

#ifdef IPV6_V6ONLY

        if (e->ai_family == PF_INET6) {

            /* listen on both ipv4 and ipv6 */

            setsockopt(slisten,IPPROTO_IPV6,IPV6_V6ONLY,(void*)&off,

                sizeof(off));

        }

#endif



        for (;;) {

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

                if (sockets_debug)

                    fprintf(stderr,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__,

                        inet_strfamily(e->ai_family), uaddr, inet_getport(e));

                goto listen;

            }

            try_next = to && (inet_getport(e) <= to + port_offset);

            if (!try_next || sockets_debug)

                fprintf(stderr,"%s: bind(%s,%s,%d): %s\n", __FUNCTION__,

                        inet_strfamily(e->ai_family), uaddr, inet_getport(e),

                        strerror(errno));

            if (try_next) {

                inet_setport(e, inet_getport(e) + 1);

                continue;

            }

            break;

        }

        closesocket(slisten);

    }

    fprintf(stderr, "%s: FAILED\n", __FUNCTION__);

    freeaddrinfo(res);

    return -1;



listen:

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

        perror("listen");

        closesocket(slisten);

        freeaddrinfo(res);

        return -1;

    }

    snprintf(uport, sizeof(uport), "%d", inet_getport(e) - port_offset);

    qemu_opt_set(opts, "host", uaddr);

    qemu_opt_set(opts, "port", uport);

    qemu_opt_set(opts, "ipv6", (e->ai_family == PF_INET6) ? "on" : "off");

    qemu_opt_set(opts, "ipv4", (e->ai_family != PF_INET6) ? "on" : "off");

    freeaddrinfo(res);

    return slisten;

}

int inet_listen_opts(QemuOpts *opts, int port_offset)

{

    struct addrinfo ai,*res,*e;

    const char *addr;

    char port[33];

    char uaddr[INET6_ADDRSTRLEN+1];

    char uport[33];

    int slisten,rc,to,try_next;



    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, "port") == NULL) {

        fprintf(stderr, "%s: host and/or port not specified\n", __FUNCTION__);

        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)

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

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

    if (rc != 0) {

        fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,

                gai_strerror(rc));

        return -1;

    }

    if (sockets_debug)

        inet_print_addrinfo(__FUNCTION__, res);



    

    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 = socket(e->ai_family, e->ai_socktype, e->ai_protocol);

        if (slisten < 0) {

            fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,

                    inet_strfamily(e->ai_family), strerror(errno));

            continue;

        }



        setsockopt(slisten,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));

#ifdef IPV6_V6ONLY

        if (e->ai_family == PF_INET6) {

            

            setsockopt(slisten,IPPROTO_IPV6,IPV6_V6ONLY,(void*)&off,

                sizeof(off));

        }

#endif



        for (;;) {

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

                if (sockets_debug)

                    fprintf(stderr,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__,

                        inet_strfamily(e->ai_family), uaddr, inet_getport(e));

                goto listen;

            }

            try_next = to && (inet_getport(e) <= to + port_offset);

            if (!try_next || sockets_debug)

                fprintf(stderr,"%s: bind(%s,%s,%d): %s\n", __FUNCTION__,

                        inet_strfamily(e->ai_family), uaddr, inet_getport(e),

                        strerror(errno));

            if (try_next) {

                inet_setport(e, inet_getport(e) + 1);

                continue;

            }

            break;

        }

        closesocket(slisten);

    }

    fprintf(stderr, "%s: FAILED\n", __FUNCTION__);

    freeaddrinfo(res);

    return -1;



listen:

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

        perror("listen");

        closesocket(slisten);

        freeaddrinfo(res);

        return -1;

    }

    snprintf(uport, sizeof(uport), "%d", inet_getport(e) - port_offset);

    qemu_opt_set(opts, "host", uaddr);

    qemu_opt_set(opts, "port", uport);

    qemu_opt_set(opts, "ipv6", (e->ai_family == PF_INET6) ? "on" : "off");

    qemu_opt_set(opts, "ipv4", (e->ai_family != PF_INET6) ? "on" : "off");

    freeaddrinfo(res);

    return slisten;

}

        slisten = socket(e->ai_family, e->ai_socktype, e->ai_protocol);
int FUNC_0(QemuOpts *VAR_0, int VAR_1)

{

    struct addrinfo VAR_2,*VAR_3,*VAR_4;

    const char *VAR_5;

    char VAR_6[33];

    char VAR_7[INET6_ADDRSTRLEN+1];

    char VAR_8[33];

    int VAR_9,VAR_10,VAR_11,VAR_12;



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

    VAR_2.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;

    VAR_2.ai_family = PF_UNSPEC;

    VAR_2.ai_socktype = SOCK_STREAM;



    if (qemu_opt_get(VAR_0, "VAR_6") == NULL) {

        fprintf(stderr, "%s: host and/or VAR_6 not specified\n", __FUNCTION__);

        return -1;

    }

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

    VAR_5 = qemu_opt_get(VAR_0, "host");



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

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

        VAR_2.ai_family = PF_INET;

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

        VAR_2.ai_family = PF_INET6;



    

    if (VAR_1)

        snprintf(VAR_6, sizeof(VAR_6), "%d", atoi(VAR_6) + VAR_1);

    VAR_10 = getaddrinfo(strlen(VAR_5) ? VAR_5 : NULL, VAR_6, &VAR_2, &VAR_3);

    if (VAR_10 != 0) {

        fprintf(stderr,"getaddrinfo(%s,%s): %s\n", VAR_5, VAR_6,

                gai_strerror(VAR_10));

        return -1;

    }

    if (sockets_debug)

        inet_print_addrinfo(__FUNCTION__, VAR_3);



    

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

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

		        VAR_7,INET6_ADDRSTRLEN,VAR_8,32,

		        NI_NUMERICHOST | NI_NUMERICSERV);

        VAR_9 = socket(VAR_4->ai_family, VAR_4->ai_socktype, VAR_4->ai_protocol);

        if (VAR_9 < 0) {

            fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,

                    inet_strfamily(VAR_4->ai_family), strerror(errno));

            continue;

        }



        setsockopt(VAR_9,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));

#ifdef IPV6_V6ONLY

        if (VAR_4->ai_family == PF_INET6) {

            

            setsockopt(VAR_9,IPPROTO_IPV6,IPV6_V6ONLY,(void*)&off,

                sizeof(off));

        }

#endif



        for (;;) {

            if (bind(VAR_9, VAR_4->ai_addr, VAR_4->ai_addrlen) == 0) {

                if (sockets_debug)

                    fprintf(stderr,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__,

                        inet_strfamily(VAR_4->ai_family), VAR_7, inet_getport(VAR_4));

                goto listen;

            }

            VAR_12 = VAR_11 && (inet_getport(VAR_4) <= VAR_11 + VAR_1);

            if (!VAR_12 || sockets_debug)

                fprintf(stderr,"%s: bind(%s,%s,%d): %s\n", __FUNCTION__,

                        inet_strfamily(VAR_4->ai_family), VAR_7, inet_getport(VAR_4),

                        strerror(errno));

            if (VAR_12) {

                inet_setport(VAR_4, inet_getport(VAR_4) + 1);

                continue;

            }

            break;

        }

        closesocket(VAR_9);

    }

    fprintf(stderr, "%s: FAILED\n", __FUNCTION__);

    freeaddrinfo(VAR_3);

    return -1;



listen:

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

        perror("listen");

        closesocket(VAR_9);

        freeaddrinfo(VAR_3);

        return -1;

    }

    snprintf(VAR_8, sizeof(VAR_8), "%d", inet_getport(VAR_4) - VAR_1);

    qemu_opt_set(VAR_0, "host", VAR_7);

    qemu_opt_set(VAR_0, "VAR_6", VAR_8);

    qemu_opt_set(VAR_0, "ipv6", (VAR_4->ai_family == PF_INET6) ? "on" : "off");

    qemu_opt_set(VAR_0, "ipv4", (VAR_4->ai_family != PF_INET6) ? "on" : "off");

    freeaddrinfo(VAR_3);

    return VAR_9;

}

int FUNC_0(QemuOpts *VAR_0, int VAR_1)
{ struct addrinfo VAR_2,*VAR_3,*VAR_4; const char *VAR_5; char VAR_6[33]; char VAR_7[INET6_ADDRSTRLEN+1]; char VAR_8[33]; int VAR_9,VAR_10,VAR_11,VAR_12; memset(&VAR_2,0, sizeof(VAR_2)); VAR_2.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; VAR_2.ai_family = PF_UNSPEC; VAR_2.ai_socktype = SOCK_STREAM; if (qemu_opt_get(VAR_0, "VAR_6") == NULL) { fprintf(stderr, "%s: host and/or VAR_6 not specified\n", __FUNCTION__); return -1; } pstrcpy(VAR_6, sizeof(VAR_6), qemu_opt_get(VAR_0, "VAR_6")); VAR_5 = qemu_opt_get(VAR_0, "host"); VAR_11 = qemu_opt_get_number(VAR_0, "VAR_11", 0); if (qemu_opt_get_bool(VAR_0, "ipv4", 0))
VAR_2.ai_family = PF_INET; if (qemu_opt_get_bool(VAR_0, "ipv6", 0))
VAR_2.ai_family = PF_INET6; if (VAR_1)
snprintf(VAR_6, sizeof(VAR_6), "%d", atoi(VAR_6) + VAR_1); VAR_10 = getaddrinfo(strlen(VAR_5) ? VAR_5 : NULL, VAR_6, &VAR_2, &VAR_3); if (VAR_10 != 0) { fprintf(stderr,"getaddrinfo(%s,%s): %s\n", VAR_5, VAR_6,
gai_strerror(VAR_10)); return -1; } if (sockets_debug)
inet_print_addrinfo(__FUNCTION__, VAR_3); for (VAR_4 = VAR_3; VAR_4 != NULL; VAR_4 = VAR_4->ai_next) { getnameinfo((struct sockaddr*)VAR_4->ai_addr,VAR_4->ai_addrlen,
VAR_7,INET6_ADDRSTRLEN,VAR_8,32,
NI_NUMERICHOST | NI_NUMERICSERV); VAR_9 = socket(VAR_4->ai_family, VAR_4->ai_socktype, VAR_4->ai_protocol); if (VAR_9 < 0) { fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
inet_strfamily(VAR_4->ai_family), strerror(errno)); continue; } setsockopt(VAR_9,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on)); #ifdef IPV6_V6ONLY
if (VAR_4->ai_family == PF_INET6) { setsockopt(VAR_9,IPPROTO_IPV6,IPV6_V6ONLY,(void*)&off,
sizeof(off)); } #endif
for (;;) { if (bind(VAR_9, VAR_4->ai_addr, VAR_4->ai_addrlen) == 0) { if (sockets_debug)
fprintf(stderr,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__,
inet_strfamily(VAR_4->ai_family), VAR_7, inet_getport(VAR_4)); goto listen; } VAR_12 = VAR_11 && (inet_getport(VAR_4) <= VAR_11 + VAR_1); if (!VAR_12 || sockets_debug)
fprintf(stderr,"%s: bind(%s,%s,%d): %s\n", __FUNCTION__,
inet_strfamily(VAR_4->ai_family), VAR_7, inet_getport(VAR_4),
strerror(errno)); if (VAR_12) { inet_setport(VAR_4, inet_getport(VAR_4) + 1); continue; } break; } closesocket(VAR_9); } fprintf(stderr, "%s: FAILED\n", __FUNCTION__); freeaddrinfo(VAR_3); return -1; listen:
if (listen(VAR_9,1) != 0) { perror("listen"); closesocket(VAR_9); freeaddrinfo(VAR_3); return -1; } snprintf(VAR_8, sizeof(VAR_8), "%d", inet_getport(VAR_4) - VAR_1); qemu_opt_set(VAR_0, "host", VAR_7); qemu_opt_set(VAR_0, "VAR_6", VAR_8); qemu_opt_set(VAR_0, "ipv6", (VAR_4->ai_family == PF_INET6) ? "on" : "off"); qemu_opt_set(VAR_0, "ipv4", (VAR_4->ai_family != PF_INET6) ? "on" : "off"); freeaddrinfo(VAR_3); return VAR_9; }