简体   繁体   中英

IP_ADD_MEMBERSHIP on a socket, will the socket listen to unicast also?

Considering the code below,

I'm trying to bind a UDP socket for multicast.
I've bound it to a specific port, and set IP_ADD_MEMBERSHIP for the address to listen to.

My question: will the socket receive unicast UDP packets bound for that port? If so, how can I prevent it? I wish to only receive multicast.

int fd;
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
    perror("socket");
    exit(1);
}

u_int yes = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0)
{
    perror("Reusing ADDR failed");
    exit(1);
}

struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = (source_iface.empty()
                          ? htonl(INADDR_ANY)
                          : inet_addr(source_iface.c_str()));

if (bind(fd,(struct sockaddr *)&addr, sizeof(addr)) < 0)
{
    perror("bind");
    exit(1);
}

struct ip_mreq mreq;
mreq.imr_multiaddr.s_addr = inet_addr(group.c_str());
mreq.imr_interface.s_addr = (source_iface.empty()
                               ? htonl(INADDR_ANY)
                               : inet_addr(source_iface.c_str()));

if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0)
{
    perror("setsockopt");
    exit(1);
}

I believe you'll also need to bind on the particular multicast address on which you want to listen, and not just in the setsockopt call - the latter also being necessary to make sure that the network card and IGMP also do the right thing.

See also What does it mean to bind a multicast (UDP) socket?

Okay, I got my instance to work so I know what the problem was. The bind() has to occur against the multicast IP address:

struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = (group.empty()
                          ? htonl(INADDR_ANY)   // <-- this will work here but not below
                          : inet_addr(group.c_str()));

if (bind(fd,(struct sockaddr *)&addr, sizeof(addr)) < 0)
{
    perror("bind");
    exit(1);
}

The difference here is group instead of source_iface . Until I changed that, it would not receive broadcasted packets at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM