组播组Linux - 加入和离开

时间:2012-04-01 16:54:08

标签: c multicast

我正在构建一个覆盖网络系统来测试协议(Uni研究)。我需要加入和离开多播组才能接收来自不同源的数据包。

我不确定套接字的正确设置,因此我可以关闭套接字并离开组播组,然后再重新加入同一个组播组。当我尝试加入同一个组播组时,我得到“bind error: Address already in use”。

//for setting up individual groups
int setUpForGroup(struct locgro_node* node, const char* port)
{
    char mcastaddr[INET6_ADDRSTRLEN];

    struct in6_addr* full_addr_gro = &(node->group);
    if( NULL == inet_ntop(AF_INET6, full_addr_gro, mcastaddr, INET6_ADDRSTRLEN))
    {   
        printf("error inet_pton, retval: \n");
        return -1;
    }

    if (buildAdd(mcastaddr, port, AF_INET6, SOCK_DGRAM, &(node->addr_st)) <0) 
    {
        fprintf(stderr, "get_addr error:: could not find multicast, address=[%s] port=[%s]\n", mcastaddr, port);
        return -1;
    }

    node->sockfd = socket(AF_INET6, SOCK_DGRAM, 0);

    if (bind(node->sockfd, (struct sockaddr *)&(node->addr_st), sizeof(node->addr_st)) < 0) {
        perror("bind error:: ");
        close(node->sockfd);
        return -1;
    }

    if (joinGroup(node->sockfd, 0 , 8, &(node->addr_st)) <0) {
        close(node->sockfd);
        return -1;
    }

    return 0;
}
//internal function
int joinGroup(int sockfd, int loopBack, int mcastTTL, struct sockaddr_in6 *addr_st)
{
    int r1, r2, r3, retval;
    retval=-1;
    struct ipv6_mreq mreq6;

    memcpy(&mreq6.ipv6mr_multiaddr, &((addr_st)->sin6_addr), sizeof(struct in6_addr));

    mreq6.ipv6mr_interface= 0; // allow any interface

    //set the loopback case
    r1 = setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &loopBack, sizeof(loopBack));
    if (r1<0) perror("joinGroup:: IPV6_MULTICAST_LOOP:: ");

/*
    setsockopt(sock_fd, IPPROTO_IPV6, SO_REUSEADDR, &mreq6, sizeof(mreq6));

    setsockopt(sock_fd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &multicast_req, sizeof(multicast_req));
*/
    //set the time to live for the packets (hops)
    r2 = setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL, sizeof(mcastTTL));
    if (r2<0) perror("joinGroup:: IPV6_MULTICAST_HOPS::  ");

    //add this address to the group
    r3 = setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6));
    if (r3<0) perror("joinGroup:: IPV6_ADD_MEMBERSHIP:: ");

    if ((r1>=0) && (r2>=0) && (r3>=0)) retval=0;

    return retval;
}


//internal function
int buildAdd(const char *hostname, const char *service, int family, int socktype,  struct sockaddr_in6 *addr_st)
{
    struct addrinfo hints, *res, *ressave;
    int n, sockfd, retval;

    retval = -1;

    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_family = family;
    hints.ai_socktype = socktype;

    n = getaddrinfo(hostname, service, &hints, &res);

    if (n <0) 
    {
        fprintf(stderr, "getaddrinfo error: [%s]\n", gai_strerror(n));
        return retval;
    }

    ressave = res;

    sockfd=-1;
    while(res) 
    {
        sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);

        if (!(sockfd < 0)) 
        {
            int opval = 1;
            setsockopt(sockfd, IPPROTO_IPV6, SO_REUSEADDR, &opval, res->ai_addrlen);

            if (bind(sockfd, res->ai_addr, res->ai_addrlen) == 0) 
            {
            //to test that the address really is correct
                close(sockfd);
                memcpy(addr_st, res->ai_addr, sizeof(*addr_st));
                retval=0;
                break;
            }
            perror("build addr : bind error");
            close(sockfd);
            sockfd=-1;
        }
        res=res->ai_next;
    }

    freeaddrinfo(ressave); //free the struct

    return retval;
}

这是我用来加入小组的功能。 离开时我只做:(发生在另一个函数内)

    struct ipv6_mreq mreq6;
    memcpy(&mreq6.ipv6mr_multiaddr, &(temp_lnode->group), sizeof(struct in6_addr));
    mreq6.ipv6mr_interface= 0; // allow any interface
    setsockopt(temp_lnode->sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6));

    close(temp_lnode->sockfd); //close socket

当我尝试绑定构建地址函数中的地址时,我收到“Address in in use”错误。 我尝试通过设置SO_REUSEADDR来解决这个问题,但它没有帮助,还添加了DROP_MEMBERSHIP但绑定仍然失败。

我需要绑定才能让它工作吗?如果没有这个问题,我应该打电话或做什么来允许团体加入和离开?我需要在30秒的间隔期间这样做。

非常感谢 中号

1 个答案:

答案 0 :(得分:1)

roblem解决了。在第一次创建组并使用DROP_MEMBERSHIP时使用SO_REUSEADDR可以解决问题。我不确定哪一个做了实际的修复,因为我意识到在一种情况下我没有看到DROP_MEMBERSHIP,无论哪种方式我都知道在处理多播时总是设置SO_REUSEADDR是一种好习惯。干杯,M