network plugin: Be more strict when checking the return value of "setsockopt".
[collectd.git] / src / network.c
index 6d0381b..96053a1 100644 (file)
@@ -1621,7 +1621,7 @@ static int network_set_interface (const sockent_t *se, const struct addrinfo *ai
 #endif
 
                        if (setsockopt (se->data.client.fd, IPPROTO_IP, IP_MULTICAST_IF,
-                                               &mreq, sizeof (mreq)) == -1)
+                                               &mreq, sizeof (mreq)) != 0)
                        {
                                char errbuf[1024];
                                ERROR ("setsockopt: %s",
@@ -1640,7 +1640,7 @@ static int network_set_interface (const sockent_t *se, const struct addrinfo *ai
                {
                        if (setsockopt (se->data.client.fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
                                                &se->interface,
-                                               sizeof (se->interface)) == -1)
+                                               sizeof (se->interface)) != 0)
                        {
                                char errbuf[1024];
                                ERROR ("setsockopt: %s",
@@ -1653,7 +1653,8 @@ static int network_set_interface (const sockent_t *se, const struct addrinfo *ai
                }
        }
 
-#if KERNEL_LINUX
+       /* else: Not a multicast interface. */
+#if defined(HAVE_IF_INDEXTONAME) && HAVE_IF_INDEXTONAME && defined(SO_BINDTODEVICE)
        if (se->interface != 0)
        {
                char interface_name[IFNAMSIZ];
@@ -1673,6 +1674,17 @@ static int network_set_interface (const sockent_t *se, const struct addrinfo *ai
                        return (-1);
                }
        }
+/* #endif HAVE_IF_INDEXTONAME && SO_BINDTODEVICE */
+
+#else
+       WARNING ("network plugin: Cannot set the interface on a unicast "
+                       "socket because "
+# if !defined(SO_BINDTODEVICE)
+                       "the the \"SO_BINDTODEVICE\" socket option "
+# else
+                       "the \"if_indextoname\" function "
+# endif
+                       "is not available on your system.");
 #endif
 
        return (0);
@@ -1798,7 +1810,7 @@ static int network_bind_socket (int fd, const struct addrinfo *ai, const int int
                }
        }
 
-#if KERNEL_LINUX
+#if defined(HAVE_IF_INDEXTONAME) && HAVE_IF_INDEXTONAME && defined(SO_BINDTODEVICE)
        /* if a specific interface was set, bind the socket to it. But to avoid
         * possible problems with multicast routing, only do that for non-multicast
         * addresses */
@@ -1821,7 +1833,7 @@ static int network_bind_socket (int fd, const struct addrinfo *ai, const int int
                        return (-1);
                }
        }
-#endif
+#endif /* HAVE_IF_INDEXTONAME && SO_BINDTODEVICE */
 
        return (0);
 } /* int network_bind_socket */