Bind transmitted packets to the port's network interface.

Even though the MCAST_JOIN_GROUP socket option includes the interface
index, this applies to the received packets only. To bind the outgoing
packets to a particular interface, the IP_MULTICAST_IF option is needed.

Signed-off-by: Richard Cochran <richardcochran@gmail.com>
master
Richard Cochran 2011-11-20 18:31:33 +01:00
parent ebd353aff6
commit 5f7facdc63
1 changed files with 17 additions and 0 deletions

17
udp.c
View File

@ -113,6 +113,20 @@ static int interface_index(int fd, char *name)
return ifreq.ifr_ifindex;
}
static int mcast_bind(int fd, int index)
{
int err;
struct ip_mreqn req;
memset(&req, 0, sizeof(req));
req.imr_ifindex = index;
err = setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &req, sizeof(req));
if (err) {
perror("setsockopt IP_MULTICAST_IF");
return -1;
}
return 0;
}
static int mcast_join(int fd, int index, const struct sockaddr *grp,
socklen_t grplen)
{
@ -169,6 +183,9 @@ static int open_socket(char *name, struct in_addr *mc_addr, short port)
perror("mcast_join");
goto no_option;
}
if (mcast_bind(fd, index)) {
goto no_option;
}
return fd;
no_option:
close(fd);