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
parent
ebd353aff6
commit
5f7facdc63
17
udp.c
17
udp.c
|
@ -113,6 +113,20 @@ static int interface_index(int fd, char *name)
|
||||||
return ifreq.ifr_ifindex;
|
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,
|
static int mcast_join(int fd, int index, const struct sockaddr *grp,
|
||||||
socklen_t grplen)
|
socklen_t grplen)
|
||||||
{
|
{
|
||||||
|
@ -169,6 +183,9 @@ static int open_socket(char *name, struct in_addr *mc_addr, short port)
|
||||||
perror("mcast_join");
|
perror("mcast_join");
|
||||||
goto no_option;
|
goto no_option;
|
||||||
}
|
}
|
||||||
|
if (mcast_bind(fd, index)) {
|
||||||
|
goto no_option;
|
||||||
|
}
|
||||||
return fd;
|
return fd;
|
||||||
no_option:
|
no_option:
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|
Loading…
Reference in New Issue