0
votes

I am building a overlay network system to test a protocol (Uni research). I need to join and leave multicast groups in order to receive packets from different sources.

I am unsure about the correct setup of the socket so I can close the socket and leave the multicast group and then rejoin the same multicast group later. When I try to join the same multicast group I get "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;
}

This are the functions I used for joining a group. When leaving I simply do: (that happens inside another function)

    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

I am getting a "Address already in use" error when I try to bind the address inside the build address function. I tried solving that by setting SO_REUSEADDR but it didn't help, also added the DROP_MEMBERSHIP but bind still fails.

Do I need to bind in order to get it to work? What should I call or do to allow for joining and leaving of groups without this issues? I would need to do this during intervals of 30s.

Thanks a lot M

1
Problem solved. Using SO_REUSEADDR when creating the groups for the first time and using DROP_MEMBERSHIP do the trick. I am not sure which one did the actual fix, as I realised I wasn't seeing DROP_MEMBERSHIP in one case, either way I read that it is good practice to set SO_REUSEADDR always when dealing with multicast. Cheers, Munixsnob
@ unixsnob: good that you solved it youself. You can probably put that as your answer.. Offtopic: Please void comments in code- which are really trivial : for example //free the struct has not value.Jayan

1 Answers

1
votes

roblem solved. Using SO_REUSEADDR when creating the groups for the first time and using DROP_MEMBERSHIP do the trick. I am not sure which one did the actual fix, as I realised I wasn't seeing DROP_MEMBERSHIP in one case, either way I read that it is good practice to set SO_REUSEADDR always when dealing with multicast. Cheers, M