aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Welte <laforge@osmocom.org>2020-03-07 14:59:05 +0100
committerHarald Welte <laforge@osmocom.org>2020-03-10 19:08:51 +0100
commit44b9926c84c9cb9a3fcf2d0770d4951a4eab5fe2 (patch)
tree5ceb5a159345d46e67380e2bdab081c137cf0239
parent31c29ea0517904cd588654426fb133aeb34af506 (diff)
socket: Add osmo_sock_mcast_iface_set() to bind multicast to device
-rw-r--r--include/osmocom/core/socket.h1
-rw-r--r--src/socket.c22
2 files changed, 23 insertions, 0 deletions
diff --git a/include/osmocom/core/socket.h b/include/osmocom/core/socket.h
index e26ca0d3..129612c6 100644
--- a/include/osmocom/core/socket.h
+++ b/include/osmocom/core/socket.h
@@ -87,6 +87,7 @@ int osmo_sock_get_remote_ip_port(int fd, char *port, size_t len);
int osmo_sock_mcast_loop_set(int fd, bool enable);
int osmo_sock_mcast_ttl_set(int fd, uint8_t ttl);
int osmo_sock_mcast_all_set(int fd, bool enable);
+int osmo_sock_mcast_iface_set(int fd, const char *ifname);
int osmo_sock_mcast_subscribe(int fd, const char *grp_addr);
int osmo_sock_local_ip(char *local_ip, const char *remote_ip);
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 <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
+#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
@@ -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