lib: simplify interface of setsockopt_multicast_ipv4().
* sockopt.[ch] (setsockopt_ipv4_multicast): ifindex is now mandatory (all
non-ancient OSes can use it anyway), and if_addr parameter (the address
of the interface) is now gone. (setsockopt_ipv4_multicast_if):
IP_MULTICAST_IF processing moved to this new function
* ospf_network.c (ospf_if_add_allspfrouters, ospf_if_drop_allspfrouters,
ospf_if_add_alldrouters, ospf_if_drop_alldrouters, ospf_if_ipmulticast),
rip_interface.c (ipv4_multicast_join, ipv4_multicast_leave,
rip_interface_new): adapt to the new interface
diff --git a/lib/sockopt.c b/lib/sockopt.c
index 6ab9768..1f9f527 100644
--- a/lib/sockopt.c
+++ b/lib/sockopt.c
@@ -184,7 +184,7 @@
/*
* Process multicast socket options for IPv4 in an OS-dependent manner.
- * Supported options are IP_MULTICAST_IF and IP_{ADD,DROP}_MEMBERSHIP.
+ * Supported options are IP_{ADD,DROP}_MEMBERSHIP.
*
* Many operating systems have a limit on the number of groups that
* can be joined per socket (where each group and local address
@@ -204,22 +204,18 @@
* allow leaves, or implicitly leave all groups joined to down interfaces.
*/
int
-setsockopt_multicast_ipv4(int sock,
+setsockopt_ipv4_multicast(int sock,
int optname,
- struct in_addr if_addr /* required */,
unsigned int mcast_addr,
- unsigned int ifindex /* optional: if non-zero, may be
- used instead of if_addr */)
+ unsigned int ifindex)
{
#ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
- /* This is better because it uses ifindex directly */
struct ip_mreqn mreqn;
int ret;
switch (optname)
{
- case IP_MULTICAST_IF:
case IP_ADD_MEMBERSHIP:
case IP_DROP_MEMBERSHIP:
memset (&mreqn, 0, sizeof(mreqn));
@@ -227,23 +223,19 @@
if (mcast_addr)
mreqn.imr_multiaddr.s_addr = mcast_addr;
- if (ifindex)
- mreqn.imr_ifindex = ifindex;
- else
- mreqn.imr_address = if_addr;
+ mreqn.imr_ifindex = ifindex;
ret = setsockopt(sock, IPPROTO_IP, optname,
(void *)&mreqn, sizeof(mreqn));
if ((ret < 0) && (optname == IP_ADD_MEMBERSHIP) && (errno == EADDRINUSE))
{
/* see above: handle possible problem when interface comes back up */
- char buf[2][INET_ADDRSTRLEN];
- zlog_info("setsockopt_multicast_ipv4 attempting to drop and "
- "re-add (fd %d, ifaddr %s, mcast %s, ifindex %u)",
+ char buf[1][INET_ADDRSTRLEN];
+ zlog_info("setsockopt_ipv4_multicast attempting to drop and "
+ "re-add (fd %d, mcast %s, ifindex %u)",
sock,
- inet_ntop(AF_INET, &if_addr, buf[0], sizeof(buf[0])),
inet_ntop(AF_INET, &mreqn.imr_multiaddr,
- buf[1], sizeof(buf[1])), ifindex);
+ buf[0], sizeof(buf[0])), ifindex);
setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP,
(void *)&mreqn, sizeof(mreqn));
ret = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
@@ -264,26 +256,17 @@
/* #elif defined(BOGON_NIX) && EXAMPLE_VERSION_CODE > -100000 */
/* Add your favourite OS here! */
-#else /* #if OS_TYPE */
+#elif defined(HAVE_BSD_STRUCT_IP_MREQ_HACK) /* #if OS_TYPE */
/* standard BSD API */
struct in_addr m;
struct ip_mreq mreq;
int ret;
-#ifdef HAVE_BSD_STRUCT_IP_MREQ_HACK
- if (ifindex)
- m.s_addr = htonl(ifindex);
- else
-#endif
- m = if_addr;
+ m.s_addr = htonl(ifindex);
switch (optname)
{
- case IP_MULTICAST_IF:
- return setsockopt (sock, IPPROTO_IP, optname, (void *)&m, sizeof(m));
- break;
-
case IP_ADD_MEMBERSHIP:
case IP_DROP_MEMBERSHIP:
memset (&mreq, 0, sizeof(mreq));
@@ -294,13 +277,12 @@
if ((ret < 0) && (optname == IP_ADD_MEMBERSHIP) && (errno == EADDRINUSE))
{
/* see above: handle possible problem when interface comes back up */
- char buf[2][INET_ADDRSTRLEN];
- zlog_info("setsockopt_multicast_ipv4 attempting to drop and "
- "re-add (fd %d, ifaddr %s, mcast %s, ifindex %u)",
+ char buf[1][INET_ADDRSTRLEN];
+ zlog_info("setsockopt_ipv4_multicast attempting to drop and "
+ "re-add (fd %d, mcast %s, ifindex %u)",
sock,
- inet_ntop(AF_INET, &if_addr, buf[0], sizeof(buf[0])),
inet_ntop(AF_INET, &mreq.imr_multiaddr,
- buf[1], sizeof(buf[1])), ifindex);
+ buf[0], sizeof(buf[0])), ifindex);
setsockopt (sock, IPPROTO_IP, IP_DROP_MEMBERSHIP,
(void *)&mreq, sizeof(mreq));
ret = setsockopt (sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
@@ -315,10 +297,41 @@
return -1;
break;
}
+#else
+ #error "Unsupported multicast API"
#endif /* #if OS_TYPE */
}
+/*
+ * Set IP_MULTICAST_IF socket option in an OS-dependent manner.
+ */
+int
+setsockopt_ipv4_multicast_if(int sock,
+ unsigned int ifindex)
+{
+
+#ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
+ struct ip_mreqn mreqn;
+
+ mreqn.imr_ifindex = ifindex;
+ return setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, (void *)&mreqn, sizeof(mreqn));
+
+ /* Example defines for another OS, boilerplate off other code in this
+ function */
+ /* #elif defined(BOGON_NIX) && EXAMPLE_VERSION_CODE > -100000 */
+ /* Add your favourite OS here! */
+#elif defined(HAVE_BSD_STRUCT_IP_MREQ_HACK)
+ struct in_addr m;
+
+ m.s_addr = htonl(ifindex);
+
+ return setsockopt (sock, IPPROTO_IP, IP_MULTICAST_IF, (void *)&m, sizeof(m));
+#else
+ #error "Unsupported multicast API"
+#endif
+}
+
static int
setsockopt_ipv4_ifindex (int sock, int val)
{
diff --git a/lib/sockopt.h b/lib/sockopt.h
index cb05c6f..69309e0 100644
--- a/lib/sockopt.h
+++ b/lib/sockopt.h
@@ -82,13 +82,11 @@
(((af) == AF_INET) : SOPT_SIZE_CMSG_IFINDEX_IPV4() \
? SOPT_SIZE_CMSG_PKTINFO_IPV6())
-extern int setsockopt_multicast_ipv4(int sock, int optname,
- struct in_addr if_addr
- /* required: interface to join on */,
+extern int setsockopt_ipv4_multicast_if(int sock,
+ unsigned int ifindex);
+extern int setsockopt_ipv4_multicast(int sock, int optname,
unsigned int mcast_addr,
- unsigned int ifindex
- /* optional: if non-zero, may be used
- instead of if_addr */);
+ unsigned int ifindex);
extern int setsockopt_ipv4_tos(int sock, int tos);
/* Ask for, and get, ifindex, by whatever method is supported. */
diff --git a/ospfd/ospf_network.c b/ospfd/ospf_network.c
index 1e2d44e..3e326a8 100644
--- a/ospfd/ospf_network.c
+++ b/ospfd/ospf_network.c
@@ -52,8 +52,8 @@
{
int ret;
- ret = setsockopt_multicast_ipv4 (top->fd, IP_ADD_MEMBERSHIP,
- p->u.prefix4, htonl (OSPF_ALLSPFROUTERS),
+ ret = setsockopt_ipv4_multicast (top->fd, IP_ADD_MEMBERSHIP,
+ htonl (OSPF_ALLSPFROUTERS),
ifindex);
if (ret < 0)
zlog_warn ("can't setsockopt IP_ADD_MEMBERSHIP (fd %d, addr %s, "
@@ -73,8 +73,8 @@
{
int ret;
- ret = setsockopt_multicast_ipv4 (top->fd, IP_DROP_MEMBERSHIP,
- p->u.prefix4, htonl (OSPF_ALLSPFROUTERS),
+ ret = setsockopt_ipv4_multicast (top->fd, IP_DROP_MEMBERSHIP,
+ htonl (OSPF_ALLSPFROUTERS),
ifindex);
if (ret < 0)
zlog_warn ("can't setsockopt IP_DROP_MEMBERSHIP (fd %d, addr %s, "
@@ -94,8 +94,8 @@
{
int ret;
- ret = setsockopt_multicast_ipv4 (top->fd, IP_ADD_MEMBERSHIP,
- p->u.prefix4, htonl (OSPF_ALLDROUTERS),
+ ret = setsockopt_ipv4_multicast (top->fd, IP_ADD_MEMBERSHIP,
+ htonl (OSPF_ALLDROUTERS),
ifindex);
if (ret < 0)
zlog_warn ("can't setsockopt IP_ADD_MEMBERSHIP (fd %d, addr %s, "
@@ -115,8 +115,8 @@
{
int ret;
- ret = setsockopt_multicast_ipv4 (top->fd, IP_DROP_MEMBERSHIP,
- p->u.prefix4, htonl (OSPF_ALLDROUTERS),
+ ret = setsockopt_ipv4_multicast (top->fd, IP_DROP_MEMBERSHIP,
+ htonl (OSPF_ALLDROUTERS),
ifindex);
if (ret < 0)
zlog_warn ("can't setsockopt IP_DROP_MEMBERSHIP (fd %d, addr %s, "
@@ -151,8 +151,7 @@
zlog_warn ("can't setsockopt IP_MULTICAST_TTL(1) for fd %d: %s",
top->fd, safe_strerror (errno));
- ret = setsockopt_multicast_ipv4 (top->fd, IP_MULTICAST_IF,
- p->u.prefix4, 0, ifindex);
+ ret = setsockopt_ipv4_multicast_if (top->fd, ifindex);
if (ret < 0)
zlog_warn("can't setsockopt IP_MULTICAST_IF(fd %d, addr %s, "
"ifindex %u): %s",
diff --git a/ripd/rip_interface.c b/ripd/rip_interface.c
index d3b55fc..810b71c 100644
--- a/ripd/rip_interface.c
+++ b/ripd/rip_interface.c
@@ -78,9 +78,8 @@
{
int ret;
- ret = setsockopt_multicast_ipv4 (sock,
+ ret = setsockopt_ipv4_multicast (sock,
IP_ADD_MEMBERSHIP,
- ifa,
group.s_addr,
ifindex);
@@ -100,9 +99,8 @@
{
int ret;
- ret = setsockopt_multicast_ipv4 (sock,
+ ret = setsockopt_ipv4_multicast (sock,
IP_DROP_MEMBERSHIP,
- ifa,
group.s_addr,
ifindex);
@@ -138,18 +136,13 @@
void
rip_interface_multicast_set (int sock, struct connected *connected)
{
- struct in_addr addr;
-
assert (connected != NULL);
- addr = CONNECTED_ID(connected)->u.prefix4;
-
- if (setsockopt_multicast_ipv4 (sock, IP_MULTICAST_IF, addr, 0,
- connected->ifp->ifindex) < 0)
+ if (setsockopt_ipv4_multicast_if (sock, connected->ifp->ifindex) < 0)
{
zlog_warn ("Can't setsockopt IP_MULTICAST_IF on fd %d to "
- "source address %s for interface %s",
- sock, inet_ntoa(addr),
+ "ifindex %d for interface %s",
+ sock, connected->ifp->ifindex,
connected->ifp->name);
}