static int net_socket_mcast_create(struct sockaddr_in *mcastaddr, struct in_addr *localaddr)

{

    struct ip_mreq imr;

    int fd;

    int val, ret;

    if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {

	fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",

		inet_ntoa(mcastaddr->sin_addr),

                (int)ntohl(mcastaddr->sin_addr.s_addr));

	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)");

	goto fail;

    }



    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;

    if (localaddr) {

        imr.imr_interface = *localaddr;

    } else {

        imr.imr_interface.s_addr = htonl(INADDR_ANY);

    }



    ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,

                     (const char *)&imr, sizeof(struct ip_mreq));

    if (ret < 0) {

	perror("setsockopt(IP_ADD_MEMBERSHIP)");

	goto fail;

    }



    /* Force mcast msgs to loopback (eg. several QEMUs in same host */

    val = 1;

    ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,

                   (const char *)&val, sizeof(val));

    if (ret < 0) {

	perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");

	goto fail;

    }



    /* If a bind address is given, only send packets from that address */

    if (localaddr != NULL) {

        ret = setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF,

                         (const char *)localaddr, sizeof(*localaddr));

        if (ret < 0) {

            perror("setsockopt(IP_MULTICAST_IF)");

            goto fail;

        }

    }



    socket_set_nonblock(fd);

    return fd;

fail:

    if (fd >= 0)

        closesocket(fd);

    return -1;

}

static int net_socket_mcast_create(struct sockaddr_in *mcastaddr, struct in_addr *localaddr)

{

    struct ip_mreq imr;

    int fd;

    int val, ret;

    if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {

	fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",

		inet_ntoa(mcastaddr->sin_addr),

                (int)ntohl(mcastaddr->sin_addr.s_addr));

	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)");

	goto fail;

    }



    ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));

    if (ret < 0) {

        perror("bind");

        goto fail;

    }



    

    imr.imr_multiaddr = mcastaddr->sin_addr;

    if (localaddr) {

        imr.imr_interface = *localaddr;

    } else {

        imr.imr_interface.s_addr = htonl(INADDR_ANY);

    }



    ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,

                     (const char *)&imr, sizeof(struct ip_mreq));

    if (ret < 0) {

	perror("setsockopt(IP_ADD_MEMBERSHIP)");

	goto fail;

    }



    

    val = 1;

    ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,

                   (const char *)&val, sizeof(val));

    if (ret < 0) {

	perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");

	goto fail;

    }



    

    if (localaddr != NULL) {

        ret = setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF,

                         (const char *)localaddr, sizeof(*localaddr));

        if (ret < 0) {

            perror("setsockopt(IP_MULTICAST_IF)");

            goto fail;

        }

    }



    socket_set_nonblock(fd);

    return fd;

fail:

    if (fd >= 0)

        closesocket(fd);

    return -1;

}

static int FUNC_0(struct sockaddr_in *VAR_0, struct in_addr *VAR_1)

{

    struct ip_mreq VAR_2;

    int VAR_3;

    int VAR_4, VAR_5;

    if (!IN_MULTICAST(ntohl(VAR_0->sin_addr.s_addr))) {

	fprintf(stderr, "qemu: error: specified VAR_0 \"%s\" (0x%08x) does not contain a multicast address\n",

		inet_ntoa(VAR_0->sin_addr),

                (int)ntohl(VAR_0->sin_addr.s_addr));

	return -1;



    }

    VAR_3 = qemu_socket(PF_INET, SOCK_DGRAM, 0);

    if (VAR_3 < 0) {

        perror("socket(PF_INET, SOCK_DGRAM)");

        return -1;

    }



    VAR_4 = 1;

    VAR_5=setsockopt(VAR_3, SOL_SOCKET, SO_REUSEADDR,

                   (const char *)&VAR_4, sizeof(VAR_4));

    if (VAR_5 < 0) {

	perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");

	goto fail;

    }



    VAR_5 = bind(VAR_3, (struct sockaddr *)VAR_0, sizeof(*VAR_0));

    if (VAR_5 < 0) {

        perror("bind");

        goto fail;

    }



    

    VAR_2.imr_multiaddr = VAR_0->sin_addr;

    if (VAR_1) {

        VAR_2.imr_interface = *VAR_1;

    } else {

        VAR_2.imr_interface.s_addr = htonl(INADDR_ANY);

    }



    VAR_5 = setsockopt(VAR_3, IPPROTO_IP, IP_ADD_MEMBERSHIP,

                     (const char *)&VAR_2, sizeof(struct ip_mreq));

    if (VAR_5 < 0) {

	perror("setsockopt(IP_ADD_MEMBERSHIP)");

	goto fail;

    }



    

    VAR_4 = 1;

    VAR_5=setsockopt(VAR_3, IPPROTO_IP, IP_MULTICAST_LOOP,

                   (const char *)&VAR_4, sizeof(VAR_4));

    if (VAR_5 < 0) {

	perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");

	goto fail;

    }



    

    if (VAR_1 != NULL) {

        VAR_5 = setsockopt(VAR_3, IPPROTO_IP, IP_MULTICAST_IF,

                         (const char *)VAR_1, sizeof(*VAR_1));

        if (VAR_5 < 0) {

            perror("setsockopt(IP_MULTICAST_IF)");

            goto fail;

        }

    }



    socket_set_nonblock(VAR_3);

    return VAR_3;

fail:

    if (VAR_3 >= 0)

        closesocket(VAR_3);

    return -1;

}

static int FUNC_0(struct sockaddr_in *VAR_0, struct in_addr *VAR_1)
{ struct ip_mreq VAR_2; int VAR_3; int VAR_4, VAR_5; if (!IN_MULTICAST(ntohl(VAR_0->sin_addr.s_addr))) { fprintf(stderr, "qemu: error: specified VAR_0 \"%s\" (0x%08x) does not contain a multicast address\n",
inet_ntoa(VAR_0->sin_addr),
(int)ntohl(VAR_0->sin_addr.s_addr)); return -1; } VAR_3 = qemu_socket(PF_INET, SOCK_DGRAM, 0); if (VAR_3 < 0) { perror("socket(PF_INET, SOCK_DGRAM)"); return -1; } VAR_4 = 1; VAR_5=setsockopt(VAR_3, SOL_SOCKET, SO_REUSEADDR,
(const char *)&VAR_4, sizeof(VAR_4)); if (VAR_5 < 0) { perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)"); goto fail; } VAR_5 = bind(VAR_3, (struct sockaddr *)VAR_0, sizeof(*VAR_0)); if (VAR_5 < 0) { perror("bind"); goto fail; } VAR_2.imr_multiaddr = VAR_0->sin_addr; if (VAR_1) { VAR_2.imr_interface = *VAR_1; } else { VAR_2.imr_interface.s_addr = htonl(INADDR_ANY); } VAR_5 = setsockopt(VAR_3, IPPROTO_IP, IP_ADD_MEMBERSHIP,
(const char *)&VAR_2, sizeof(struct ip_mreq)); if (VAR_5 < 0) { perror("setsockopt(IP_ADD_MEMBERSHIP)"); goto fail; } VAR_4 = 1; VAR_5=setsockopt(VAR_3, IPPROTO_IP, IP_MULTICAST_LOOP,
(const char *)&VAR_4, sizeof(VAR_4)); if (VAR_5 < 0) { perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)"); goto fail; } if (VAR_1 != NULL) { VAR_5 = setsockopt(VAR_3, IPPROTO_IP, IP_MULTICAST_IF,
(const char *)VAR_1, sizeof(*VAR_1)); if (VAR_5 < 0) { perror("setsockopt(IP_MULTICAST_IF)"); goto fail; } } socket_set_nonblock(VAR_3); return VAR_3; fail:
if (VAR_3 >= 0)
closesocket(VAR_3); return -1; }