Fixed leak and hint
[collectd.git] / src / network.c
index e3d5278..687717c 100644 (file)
@@ -1691,14 +1691,14 @@ static int network_bind_socket_to_addr(sockent_t *se,
   if (se->data.client.bind_addr == NULL)
     return 0;
 
-  DEBUG("fd %i: bind socket to address", se->data.client.fd);
+  DEBUG("network_plugin: fd %i: bind socket to address", se->data.client.fd);
   char pbuffer[64];
 
   if (ai->ai_family == AF_INET) {
     struct sockaddr_in *addr =
         (struct sockaddr_in *)(se->data.client.bind_addr);
     inet_ntop(AF_INET, &(addr->sin_addr), pbuffer, 64);
-    DEBUG("binding client socket to ipv4 address: %s", pbuffer);
+    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",
@@ -1709,7 +1709,7 @@ static int network_bind_socket_to_addr(sockent_t *se,
     struct sockaddr_in6 *addr =
         (struct sockaddr_in6 *)(se->data.client.bind_addr);
     inet_ntop(AF_INET, &(addr->sin6_addr), pbuffer, 64);
-    DEBUG("binding client socket to ipv6 address: %s", pbuffer);
+    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",
@@ -2729,7 +2729,7 @@ network_config_set_bind_address(const oconfig_item_t *ci,
                                 struct sockaddr_storage **bind_address) {
   if ((*bind_address) != NULL) {
     ERROR("network_plugin: only a single bind address is allowed");
-    return 1;
+    return -1;
   }
 
   char addr_text[256];
@@ -2738,16 +2738,18 @@ network_config_set_bind_address(const oconfig_item_t *ci,
     return -1;
 
   int ret;
-  struct addrinfo hint, *res = NULL;
-
-  memset(&hint, '\0', sizeof hint);
-  hint.ai_family = PF_UNSPEC;
-  hint.ai_flags = AI_NUMERICHOST;
+  struct addrinfo *res = NULL;
+  struct addrinfo hint = {.ai_family = PF_UNSPEC,
+                          .ai_flags = AI_NUMERICHOST,
+                          .ai_protocol = IPPROTO_IP,
+                          .ai_socktype = SOCK_DGRAM};
 
   ret = getaddrinfo(addr_text, NULL, &hint, &res);
   if (ret) {
-    ERROR("Bind address option has invalid address set: %s", gai_strerror(ret));
-    return 1;
+    ERROR("network plugin: Bind address option has invalid address set: %s",
+          gai_strerror(ret));
+    freeaddrinfo(res);
+    return -1;
   }
 
   *bind_address = malloc(sizeof(**bind_address));
@@ -2759,9 +2761,14 @@ network_config_set_bind_address(const oconfig_item_t *ci,
     struct sockaddr_in6 *addr = (struct sockaddr_in6 *)(*bind_address);
     inet_pton(AF_INET6, addr_text, &(addr->sin6_addr));
   } else {
-    ERROR("%s is an unknown address format %d\n", addr_text, res->ai_family);
+    ERROR("network plugin: %s is an unknown address format %d\n", addr_text,
+          res->ai_family);
+    sfree(*bind_address);
+    freeaddrinfo(res);
+    return -1;
   }
 
+  freeaddrinfo(res);
   return 0;
 } /* int network_config_set_bind_address */