]> git.puffer.fish Git - matthieu/frr.git/commitdiff
*: create a helper function to set the IP_MULTICAST_LOOP sockoption
authorRenato Westphal <renato@opensourcerouting.org>
Sat, 12 Nov 2016 21:05:08 +0000 (19:05 -0200)
committerRenato Westphal <renato@opensourcerouting.org>
Fri, 25 Nov 2016 13:46:06 +0000 (11:46 -0200)
Signed-off-by: Renato Westphal <renato@opensourcerouting.org>
ldpd/socket.c
lib/sockopt.c
lib/sockopt.h
ospfd/ospf_network.c
pimd/pim_sock.c
pimd/pim_ssmpingd.c
zebra/irdp_packet.c

index cf352d7204f64aa8f2294052337eaf40a359e296..1bb08374019a93873b13a1d9a6a32a34682a5294 100644 (file)
@@ -421,15 +421,7 @@ sock_set_ipv4_mcast(struct iface *iface)
 int
 sock_set_ipv4_mcast_loop(int fd)
 {
-       uint8_t loop = 0;
-
-       if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
-           (char *)&loop, sizeof(loop)) < 0) {
-               log_warn("%s: error setting IP_MULTICAST_LOOP", __func__);
-               return (-1);
-       }
-
-       return (0);
+       return (setsockopt_ipv4_multicast_loop(fd, 0));
 }
 
 int
index c480cee0d723d08af874fe8c48e1184d56ff5dd8..be3ac0e4bfa20bc2c56935a70e37fc9e3ba9cd60 100644 (file)
@@ -384,7 +384,20 @@ setsockopt_ipv4_multicast_if(int sock, struct in_addr if_addr,
   #error "Unsupported multicast API"
 #endif
 }
-  
+
+int
+setsockopt_ipv4_multicast_loop (int sock, u_char val)
+{
+  int ret;
+
+  ret = setsockopt (sock, IPPROTO_IP, IP_MULTICAST_LOOP, (void *) &val,
+                   sizeof (val));
+  if (ret < 0)
+    zlog_warn ("can't setsockopt IP_MULTICAST_LOOP");
+
+  return ret;
+}
+
 static int
 setsockopt_ipv4_ifindex (int sock, ifindex_t val)
 {
index d67b510b66b36d4e227601d6322dae53d7c9083c..02f0189345f85067c47f5abe0355a6d0fed40e45 100644 (file)
@@ -89,6 +89,8 @@ extern int setsockopt_ipv4_multicast(int sock, int optname,
                                      struct in_addr if_addr,
                                      unsigned int mcast_addr,
                                     ifindex_t ifindex);
+extern int setsockopt_ipv4_multicast_loop (int sock, u_char val);
+
 extern int setsockopt_ipv4_tos(int sock, int tos);
 
 /* Ask for, and get, ifindex, by whatever method is supported. */
index 6caa38d68b163da0f3b01d37eaa93f99a3c754da..088123ea24967230ad8be6708df4956749c61bdd 100644 (file)
@@ -132,18 +132,16 @@ ospf_if_ipmulticast (struct ospf *top, struct prefix *p, ifindex_t ifindex)
 {
   u_char val;
   int ret, len;
-  
-  val = 0;
-  len = sizeof (val);
-  
+
   /* Prevent receiving self-origined multicast packets. */
-  ret = setsockopt (top->fd, IPPROTO_IP, IP_MULTICAST_LOOP, (void *)&val, len);
+  ret = setsockopt_ipv4_multicast_loop (top->fd, 0);
   if (ret < 0)
     zlog_warn ("can't setsockopt IP_MULTICAST_LOOP(0) for fd %d: %s",
               top->fd, safe_strerror(errno));
   
   /* Explicitly set multicast ttl to 1 -- endo. */
   val = 1;
+  len = sizeof (val);
   ret = setsockopt (top->fd, IPPROTO_IP, IP_MULTICAST_TTL, (void *)&val, len);
   if (ret < 0)
     zlog_warn ("can't setsockopt IP_MULTICAST_TTL(1) for fd %d: %s",
index 90b11dcfd668aec9a153bd1ff9f152c26a5cb3c5..231efd0f223ab67eed0ba5823c97939c5880e532 100644 (file)
@@ -35,6 +35,7 @@
 #include "privs.h"
 #include "if.h"
 #include "vrf.h"
+#include "sockopt.h"
 
 #include "pimd.h"
 #include "pim_mroute.h"
@@ -173,8 +174,7 @@ int pim_socket_mcast(int protocol, struct in_addr ifaddr, int ifindex, u_char lo
     }
   }
 
-  if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
-                (void *) &loop, sizeof(loop))) {
+  if (setsockopt_ipv4_multicast_loop (fd, loop)) {
     zlog_warn("Could not %s Multicast Loopback Option on socket fd=%d: errno=%d: %s",
              loop ? "enable" : "disable",
              fd, errno, safe_strerror(errno));
index daa3c65e36fb17a26aa5e927777f9414740e9953..fba563a6f86ee839c5b4ed13c7f69689dcad96e7 100644 (file)
@@ -25,6 +25,7 @@
 #include "if.h"
 #include "log.h"
 #include "memory.h"
+#include "sockopt.h"
 
 #include "pim_ssmpingd.h"
 #include "pim_time.h"
@@ -150,17 +151,12 @@ static int ssmpingd_socket(struct in_addr addr, int port, int mttl)
     return -1;
   }
 
-  {
-    u_char loop = 0;
-    if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
-                  (void *) &loop, sizeof(loop))) {
-      zlog_warn("%s: could not %s Multicast Loopback Option on socket fd=%d: errno=%d: %s",
-               __PRETTY_FUNCTION__,
-               loop ? "enable" : "disable",
-               fd, errno, safe_strerror(errno));
-      close(fd);
-      return PIM_SOCK_ERR_LOOP;
-    }
+  if (setsockopt_ipv4_multicast_loop (fd, 0)) {
+    zlog_warn("%s: could not disable Multicast Loopback Option on socket fd=%d: errno=%d: %s",
+             __PRETTY_FUNCTION__,
+             fd, errno, safe_strerror(errno));
+    close(fd);
+    return PIM_SOCK_ERR_LOOP;
   }
 
   if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF,
index 290a6180e7283013ab7911505b9eb742e18a76cc..c9c32ce311d553d8c3ba058a0a27b9004df2c89d 100644 (file)
@@ -323,12 +323,8 @@ send_packet(struct interface *ifp,
       zlog_warn("sendto %s", safe_strerror (errno));
   }
 
-  if(dst !=  INADDR_BROADCAST) {
-      on = 0; 
-      if( setsockopt(irdp_sock,IPPROTO_IP, IP_MULTICAST_LOOP, 
-                    (char *)&on,sizeof(on)) < 0)
-       zlog_warn("sendto %s", safe_strerror (errno));
-  }
+  if(dst !=  INADDR_BROADCAST)
+    setsockopt_ipv4_multicast_loop (irdp_sock, 0);
 
   memset(&sockdst,0,sizeof(sockdst));
   sockdst.sin_family=AF_INET;