From 44b9926c84c9cb9a3fcf2d0770d4951a4eab5fe2 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Sat, 7 Mar 2020 14:59:05 +0100 Subject: socket: Add osmo_sock_mcast_iface_set() to bind multicast to device Change-Id: Ib52d22710020b56965aefcef09bde8247ace4a9c Related: OS#2966 --- src/socket.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'src/socket.c') diff --git a/src/socket.c b/src/socket.c index 9b1d30ec..503ceaf4 100644 --- a/src/socket.c +++ b/src/socket.c @@ -41,6 +41,7 @@ #include #include #include +#include #include #include @@ -1194,6 +1195,27 @@ int osmo_sock_mcast_ttl_set(int fd, uint8_t ttl) } } +/*! Set the network device to which we should bind the multicast socket + * \param[in] fd file descriptor of related socket + * \param[in] ifname name of network interface to user for multicast + * \returns 0 on success; negative otherwise */ +int osmo_sock_mcast_iface_set(int fd, const char *ifname) +{ + unsigned int ifindex; + struct ip_mreqn mr; + + /* first, resolve interface name to ifindex */ + ifindex = if_nametoindex(ifname); + if (ifindex == 0) + return -errno; + + /* next, configure kernel to use that ifindex for this sockets multicast traffic */ + memset(&mr, 0, sizeof(mr)); + mr.imr_ifindex = ifindex; + return setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &mr, sizeof(mr)); +} + + /*! Enable/disable receiving all multicast packets, even for non-subscribed groups * \param[in] fd file descriptor of related socket * \param[in] enable Enable or Disable receiving of all packets -- cgit v1.2.3