pinba plugin: pinba_socket_open: Rewrote the function ...
[collectd.git] / src / network.c
index 6d0381b..74325a2 100644 (file)
@@ -1144,7 +1144,10 @@ static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
   cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
       pea.username);
   if (cypher == NULL)
+  {
+    sfree (pea.username);
     return (-1);
+  }
 
   payload_len = part_size - (PART_ENCRYPTION_AES256_SIZE + username_len);
   assert (payload_len > 0);
@@ -1156,6 +1159,7 @@ static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
       /* in = */ NULL, /* in len = */ 0);
   if (err != 0)
   {
+    sfree (pea.username);
     ERROR ("network plugin: gcry_cipher_decrypt returned: %s",
         gcry_strerror (err));
     return (-1);
@@ -1174,6 +1178,7 @@ static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
       buffer + buffer_offset, payload_len);
   if (memcmp (hash, pea.hash, sizeof (hash)) != 0)
   {
+    sfree (pea.username);
     ERROR ("network plugin: Decryption failed: Checksum mismatch.");
     return (-1);
   }
@@ -1185,6 +1190,8 @@ static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
   *ret_buffer =     buffer     + part_size;
   *ret_buffer_len = buffer_len - part_size;
 
+  sfree (pea.username);
+
   return (0);
 } /* }}} int parse_part_encr_aes256 */
 /* #endif HAVE_LIBGCRYPT */
@@ -1553,7 +1560,7 @@ static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
 
                if (setsockopt (se->data.client.fd, IPPROTO_IP, optname,
                                        &network_config_ttl,
-                                       sizeof (network_config_ttl)) == -1)
+                                       sizeof (network_config_ttl)) != 0)
                {
                        char errbuf[1024];
                        ERROR ("setsockopt: %s",
@@ -1574,7 +1581,7 @@ static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
 
                if (setsockopt (se->data.client.fd, IPPROTO_IPV6, optname,
                                        &network_config_ttl,
-                                       sizeof (network_config_ttl)) == -1)
+                                       sizeof (network_config_ttl)) != 0)
                {
                        char errbuf[1024];
                        ERROR ("setsockopt: %s",
@@ -1621,7 +1628,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 +1647,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 +1660,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 +1681,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 +1817,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 +1840,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 */