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?