Merge pull request #2874 from elfiesmelfie/feat_virt_block_info
[collectd.git] / src / network.c
index 687717c..f6f0ac1 100644 (file)
@@ -27,8 +27,8 @@
 
 #include "collectd.h"
 
-#include "common.h"
 #include "plugin.h"
+#include "utils/common/common.h"
 #include "utils_cache.h"
 #include "utils_complain.h"
 #include "utils_fbhash.h"
@@ -403,7 +403,7 @@ static int network_dispatch_values(value_list_t *vl, /* {{{ */
 #if COLLECT_DEBUG
     char name[6 * DATA_MAX_NAME_LEN];
     FORMAT_VL(name, sizeof(name), vl);
-    name[sizeof(name) - 1] = 0;
+    name[sizeof(name) - 1] = '\0';
     DEBUG("network plugin: network_dispatch_values: "
           "NOT dispatching %s.",
           name);
@@ -1701,19 +1701,19 @@ static int network_bind_socket_to_addr(sockent_t *se,
     DEBUG("network_plugin: binding client socket to ipv4 address: %s", pbuffer);
     if (bind(se->data.client.fd, (struct sockaddr *)addr, sizeof(*addr)) ==
         -1) {
-      ERROR("network_plugin: failed to bind client socket (ipv4): %s",
-            STRERRNO);
+      ERROR("network plugin: failed to bind client socket (ipv4) to %s: %s",
+            pbuffer, STRERRNO);
       return -1;
     }
   } else if (ai->ai_family == AF_INET6) {
     struct sockaddr_in6 *addr =
         (struct sockaddr_in6 *)(se->data.client.bind_addr);
-    inet_ntop(AF_INET, &(addr->sin6_addr), pbuffer, 64);
+    inet_ntop(AF_INET6, &(addr->sin6_addr), pbuffer, 64);
     DEBUG("network_plugin: binding client socket to ipv6 address: %s", pbuffer);
     if (bind(se->data.client.fd, (struct sockaddr *)addr, sizeof(*addr)) ==
         -1) {
-      ERROR("network_plugin: failed to bind client socket (ipv6): %s",
-            STRERRNO);
+      ERROR("network plugin: failed to bind client socket (ipv6) to %s: %s",
+            pbuffer, STRERRNO);
       return -1;
     }
   }
@@ -1728,10 +1728,9 @@ static int network_bind_socket(int fd, const struct addrinfo *ai,
 #else
   int loop = 0;
 #endif
-  int yes = 1;
 
   /* allow multiple sockets to use the same PORT number */
-  if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1) {
+  if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int)) == -1) {
     ERROR("network plugin: setsockopt (reuseaddr): %s", STRERRNO);
     return -1;
   }
@@ -2641,7 +2640,7 @@ static int network_write(const data_set_t *ds, const value_list_t *vl,
 #if COLLECT_DEBUG
     char name[6 * DATA_MAX_NAME_LEN];
     FORMAT_VL(name, sizeof(name), vl);
-    name[sizeof(name) - 1] = 0;
+    name[sizeof(name) - 1] = '\0';
     DEBUG("network plugin: network_write: "
           "NOT sending %s.",
           name);
@@ -2739,20 +2738,23 @@ network_config_set_bind_address(const oconfig_item_t *ci,
 
   int ret;
   struct addrinfo *res = NULL;
-  struct addrinfo hint = {.ai_family = PF_UNSPEC,
-                          .ai_flags = AI_NUMERICHOST,
-                          .ai_protocol = IPPROTO_IP,
-                          .ai_socktype = SOCK_DGRAM};
+  struct addrinfo ai_hints = {.ai_family = AF_UNSPEC,
+                              .ai_flags = AI_NUMERICHOST,
+                              .ai_protocol = IPPROTO_UDP,
+                              .ai_socktype = SOCK_DGRAM};
 
-  ret = getaddrinfo(addr_text, NULL, &hint, &res);
+  ret = getaddrinfo(addr_text, NULL, &ai_hints, &res);
   if (ret) {
     ERROR("network plugin: Bind address option has invalid address set: %s",
           gai_strerror(ret));
-    freeaddrinfo(res);
     return -1;
   }
 
   *bind_address = malloc(sizeof(**bind_address));
+  if (*bind_address == NULL) {
+    ERROR("network plugin: network_config_set_bind_address: malloc failed.");
+    return -1;
+  }
   (*bind_address)->ss_family = res->ai_family;
   if (res->ai_family == AF_INET) {
     struct sockaddr_in *addr = (struct sockaddr_in *)(*bind_address);