#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"
} data;
struct sockent *next;
+ pthread_mutex_t lock;
} sockent_t;
/* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
LOG_ERR, &complain_forwarding,
"network plugin: A notification has been received via the network "
"and forwarding is enabled. Forwarding of notifications is currently "
- "not supported, because there is not loop-deteciton available. "
+ "not supported, because there is not loop-detection available. "
"Please contact the collectd mailing list if you need this "
"feature.");
}
#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);
return 0;
} /* }}} int parse_part_sign_sha256 */
-/* #endif HAVE_GCRYPT_H */
+ /* #endif HAVE_GCRYPT_H */
#else /* if !HAVE_GCRYPT_H */
static int parse_part_sign_sha256(sockent_t *se, /* {{{ */
return 0;
} /* }}} int parse_part_encr_aes256 */
-/* #endif HAVE_GCRYPT_H */
+ /* #endif HAVE_GCRYPT_H */
#else /* if !HAVE_GCRYPT_H */
static int parse_part_encr_aes256(sockent_t *se, /* {{{ */
sfree(se->node);
sfree(se->service);
+ pthread_mutex_destroy(&se->lock);
if (se->type == SOCKENT_TYPE_CLIENT)
free_sockent_client(&se->data.client);
ERROR("network plugin: setsockopt (bind-if): %s", STRERRNO);
return -1;
}
-/* #endif HAVE_IF_INDEXTONAME && SO_BINDTODEVICE */
+ /* #endif HAVE_IF_INDEXTONAME && SO_BINDTODEVICE */
#else
WARNING("network plugin: Cannot set the interface on a unicast "
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",
- 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);
- DEBUG("binding client socket to ipv6 address: %s", pbuffer);
+ 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;
}
}
#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;
}
se->service = NULL;
se->interface = 0;
se->next = NULL;
+ pthread_mutex_init(&se->lock, NULL);
if (type == SOCKENT_TYPE_SERVER) {
se->data.server.fd = NULL;
client->fd = -1;
}
+ DEBUG("network plugin: free (se = %p, addr = %p);", (void *)se,
+ (void *)client->addr);
sfree(client->addr);
client->addrlen = 0;
client->fd = -1;
continue;
}
+ DEBUG("network plugin: alloc (se = %p, addr = %p);", (void *)se,
+ (void *)client->addr);
assert(sizeof(*client->addr) >= ai_ptr->ai_addrlen);
memcpy(client->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
buffer_len);
for (sockent_t *se = sending_sockets; se != NULL; se = se->next) {
+ pthread_mutex_lock(&se->lock);
#if HAVE_GCRYPT_H
if (se->data.client.security_level == SECURITY_LEVEL_ENCRYPT)
network_send_buffer_encrypted(se, buffer, buffer_len);
else /* if (se->data.client.security_level == SECURITY_LEVEL_NONE) */
#endif /* HAVE_GCRYPT_H */
network_send_buffer_plain(se, buffer, buffer_len);
+ pthread_mutex_unlock(&se->lock);
} /* for (sending_sockets) */
} /* }}} void network_send_buffer */
#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);
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];
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 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("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));
+ return -1;
}
*bind_address = malloc(sizeof(**bind_address));
+ if (*bind_address == NULL) {
+ ERROR("network plugin: network_config_set_bind_address: malloc failed.");
+ freeaddrinfo(res);
+ return -1;
+ }
(*bind_address)->ss_family = res->ai_family;
if (res->ai_family == AF_INET) {
struct sockaddr_in *addr = (struct sockaddr_in *)(*bind_address);
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 */