X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fnetwork.c;h=c6845eb9696198cc068e88686fb439e82bfc9df6;hb=7bfda8d327240ac73297e4449663814dd0594be5;hp=5d485ba3a23894a844d771336313dc9f4b693015;hpb=e9ef1a2f33fb6fb5a379f877fb8c5bb0d3810fed;p=collectd.git diff --git a/src/network.c b/src/network.c index 5d485ba3..c6845eb9 100644 --- a/src/network.c +++ b/src/network.c @@ -22,6 +22,7 @@ * Aman Gupta **/ +#define _DEFAULT_SOURCE #define _BSD_SOURCE /* For struct ip_mreq */ #include "collectd.h" @@ -119,6 +120,8 @@ struct sockent_client gcry_cipher_hd_t cypher; unsigned char password_hash[32]; #endif + cdtime_t next_resolve_reconnect; + cdtime_t resolve_interval; }; struct sockent_server @@ -495,13 +498,15 @@ static int network_dispatch_notification (notification_t *n) /* {{{ */ } /* }}} int network_dispatch_notification */ #if HAVE_LIBGCRYPT -static void network_init_gcrypt (void) /* {{{ */ +static int network_init_gcrypt (void) /* {{{ */ { + gcry_error_t err; + /* http://lists.gnupg.org/pipermail/gcrypt-devel/2003-August/000458.html * Because you can't know in a library whether another library has * already initialized the library */ if (gcry_control (GCRYCTL_ANY_INITIALIZATION_P)) - return; + return (0); /* http://www.gnupg.org/documentation/manuals/gcrypt/Multi_002dThreading.html * To ensure thread-safety, it's important to set GCRYCTL_SET_THREAD_CBS @@ -511,12 +516,26 @@ static void network_init_gcrypt (void) /* {{{ */ * * tl;dr: keep all these gry_* statements in this exact order please. */ # if GCRYPT_VERSION_NUMBER < 0x010600 - gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread); + err = gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread); + if (err) + { + ERROR ("network plugin: gcry_control (GCRYCTL_SET_THREAD_CBS) failed: %s", gcry_strerror (err)); + return (-1); + } # endif + gcry_check_version (NULL); - gcry_control (GCRYCTL_INIT_SECMEM, 32768); + + err = gcry_control (GCRYCTL_INIT_SECMEM, 32768); + if (err) + { + ERROR ("network plugin: gcry_control (GCRYCTL_INIT_SECMEM) failed: %s", gcry_strerror (err)); + return (-1); + } + gcry_control (GCRYCTL_INITIALIZATION_FINISHED); -} /* }}} void network_init_gcrypt */ + return (0); +} /* }}} int network_init_gcrypt */ static gcry_cipher_hd_t network_get_aes256_cypher (sockent_t *se, /* {{{ */ const void *iv, size_t iv_size, const char *username) @@ -921,15 +940,19 @@ static int parse_part_number (void **ret_buffer, size_t *ret_buffer_len, } /* int parse_part_number */ static int parse_part_string (void **ret_buffer, size_t *ret_buffer_len, - char *output, int output_len) + char *output, size_t const output_len) { char *buffer = *ret_buffer; size_t buffer_len = *ret_buffer_len; uint16_t tmp16; - size_t header_size = 2 * sizeof (uint16_t); + size_t const header_size = 2 * sizeof (uint16_t); uint16_t pkg_length; + size_t payload_size; + + if (output_len <= 0) + return (EINVAL); if (buffer_len < header_size) { @@ -948,6 +971,7 @@ static int parse_part_string (void **ret_buffer, size_t *ret_buffer_len, memcpy ((void *) &tmp16, buffer, sizeof (tmp16)); buffer += sizeof (tmp16); pkg_length = ntohs (tmp16); + payload_size = ((size_t) pkg_length) - header_size; /* Check that packet fits in the input buffer */ if (pkg_length > buffer_len) @@ -973,22 +997,24 @@ static int parse_part_string (void **ret_buffer, size_t *ret_buffer_len, /* Check that the package data fits into the output buffer. * The previous if-statement ensures that: * `pkg_length > header_size' */ - if ((output_len < 0) - || ((size_t) output_len < ((size_t) pkg_length - header_size))) + if (output_len < payload_size) { WARNING ("network plugin: parse_part_string: " - "Output buffer too small."); + "Buffer too small: " + "Output buffer holds %zu bytes, " + "which is too small to hold the received " + "%zu byte string.", + output_len, payload_size); return (-1); } /* All sanity checks successfull, let's copy the data over */ - output_len = pkg_length - header_size; - memcpy ((void *) output, (void *) buffer, output_len); - buffer += output_len; + memcpy ((void *) output, (void *) buffer, payload_size); + buffer += payload_size; /* For some very weird reason '\0' doesn't do the trick on SPARC in * this statement. */ - if (output[output_len - 1] != 0) + if (output[payload_size - 1] != 0) { WARNING ("network plugin: parse_part_string: " "Received string does not end " @@ -1434,6 +1460,7 @@ static int parse_packet (sockent_t *se, /* {{{ */ printed_ignore_warning = 1; } buffer = ((char *) buffer) + pkg_length; + buffer_size -= (size_t) pkg_length; continue; } #endif /* HAVE_LIBGCRYPT */ @@ -1461,6 +1488,7 @@ static int parse_packet (sockent_t *se, /* {{{ */ printed_ignore_warning = 1; } buffer = ((char *) buffer) + pkg_length; + buffer_size -= (size_t) pkg_length; continue; } #endif /* HAVE_LIBGCRYPT */ @@ -1602,6 +1630,7 @@ static int parse_packet (sockent_t *se, /* {{{ */ DEBUG ("network plugin: parse_packet: Unknown part" " type: 0x%04hx", pkg_type); buffer = ((char *) buffer) + pkg_length; + buffer_size -= (size_t) pkg_length; } } /* while (buffer_size > sizeof (part_header_t)) */ @@ -2017,6 +2046,7 @@ static sockent_t *sockent_create (int type) /* {{{ */ if (type == SOCKENT_TYPE_SERVER) { se->data.server.fd = NULL; + se->data.server.fd_num = 0; #if HAVE_LIBGCRYPT se->data.server.security_level = SECURITY_LEVEL_NONE; se->data.server.auth_file = NULL; @@ -2028,6 +2058,8 @@ static sockent_t *sockent_create (int type) /* {{{ */ { se->data.client.fd = -1; se->data.client.addr = NULL; + se->data.client.resolve_interval = 0; + se->data.client.next_resolve_reconnect = 0; #if HAVE_LIBGCRYPT se->data.client.security_level = SECURITY_LEVEL_NONE; se->data.client.username = NULL; @@ -2046,7 +2078,12 @@ static int sockent_init_crypto (sockent_t *se) /* {{{ */ { if (se->data.client.security_level > SECURITY_LEVEL_NONE) { - network_init_gcrypt (); + if (network_init_gcrypt () < 0) + { + ERROR ("network plugin: Cannot configure client socket with " + "security: Failed to initialize crypto library."); + return (-1); + } if ((se->data.client.username == NULL) || (se->data.client.password == NULL)) @@ -2066,7 +2103,12 @@ static int sockent_init_crypto (sockent_t *se) /* {{{ */ { if (se->data.server.security_level > SECURITY_LEVEL_NONE) { - network_init_gcrypt (); + if (network_init_gcrypt () < 0) + { + ERROR ("network plugin: Cannot configure server socket with " + "security: Failed to initialize crypto library."); + return (-1); + } if (se->data.server.auth_file == NULL) { @@ -2094,6 +2136,26 @@ static int sockent_init_crypto (sockent_t *se) /* {{{ */ return (0); } /* }}} int sockent_init_crypto */ +static int sockent_client_disconnect (sockent_t *se) /* {{{ */ +{ + struct sockent_client *client; + + if ((se == NULL) || (se->type != SOCKENT_TYPE_CLIENT)) + return (EINVAL); + + client = &se->data.client; + if (client->fd >= 0) /* connected */ + { + close (client->fd); + client->fd = -1; + } + + sfree (client->addr); + client->addrlen = 0; + + return (0); +} /* }}} int sockent_client_disconnect */ + static int sockent_client_connect (sockent_t *se) /* {{{ */ { static c_complain_t complaint = C_COMPLAIN_INIT_STATIC; @@ -2102,12 +2164,22 @@ static int sockent_client_connect (sockent_t *se) /* {{{ */ struct addrinfo ai_hints; struct addrinfo *ai_list = NULL, *ai_ptr; int status; + _Bool reconnect = 0; + cdtime_t now; if ((se == NULL) || (se->type != SOCKENT_TYPE_CLIENT)) return (EINVAL); client = &se->data.client; - if (client->fd >= 0) /* already connected */ + + now = cdtime (); + if (client->resolve_interval != 0 && client->next_resolve_reconnect < now) { + DEBUG("network plugin: Reconnecting socket, resolve_interval = %lf, next_resolve_reconnect = %lf", + CDTIME_T_TO_DOUBLE(client->resolve_interval), CDTIME_T_TO_DOUBLE(client->next_resolve_reconnect)); + reconnect = 1; + } + + if (client->fd >= 0 && !reconnect) /* already connected and not stale*/ return (0); memset (&ai_hints, 0, sizeof (ai_hints)); @@ -2139,6 +2211,9 @@ static int sockent_client_connect (sockent_t *se) /* {{{ */ for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) { + if (client->fd >= 0) /* when we reconnect */ + sockent_client_disconnect(se); + client->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol); @@ -2176,28 +2251,11 @@ static int sockent_client_connect (sockent_t *se) /* {{{ */ freeaddrinfo (ai_list); if (client->fd < 0) return (-1); - return (0); -} /* }}} int sockent_client_connect */ - -static int sockent_client_disconnect (sockent_t *se) /* {{{ */ -{ - struct sockent_client *client; - - if ((se == NULL) || (se->type != SOCKENT_TYPE_CLIENT)) - return (EINVAL); - - client = &se->data.client; - if (client->fd >= 0) /* connected */ - { - close (client->fd); - client->fd = -1; - } - - sfree (client->addr); - client->addrlen = 0; + if (client->resolve_interval > 0) + client->next_resolve_reconnect = now + client->resolve_interval; return (0); -} /* }}} int sockent_client_disconnect */ +} /* }}} int sockent_client_connect */ /* Open the file descriptors for a initialized sockent structure. */ static int sockent_server_listen (sockent_t *se) /* {{{ */ @@ -2212,6 +2270,9 @@ static int sockent_server_listen (sockent_t *se) /* {{{ */ if (se == NULL) return (-1); + assert (se->data.server.fd == NULL); + assert (se->data.server.fd_num == 0); + node = se->node; service = se->service; @@ -2411,13 +2472,13 @@ static int network_receive (void) /* {{{ */ int buffer_len; int i; - int status; + int status = 0; receive_list_entry_t *private_list_head; receive_list_entry_t *private_list_tail; uint64_t private_list_length; - assert (listen_sockets_num > 0); + assert (listen_sockets_num > 0); private_list_head = NULL; private_list_tail = NULL; @@ -2426,15 +2487,14 @@ static int network_receive (void) /* {{{ */ while (listen_loop == 0) { status = poll (listen_sockets_pollfd, listen_sockets_num, -1); - if (status <= 0) { char errbuf[1024]; if (errno == EINTR) continue; - ERROR ("poll failed: %s", + ERROR ("network plugin: poll(2) failed: %s", sstrerror (errno, errbuf, sizeof (errbuf))); - return (-1); + break; } for (i = 0; (i < listen_sockets_num) && (status > 0); i++) @@ -2452,10 +2512,10 @@ static int network_receive (void) /* {{{ */ if (buffer_len < 0) { char errbuf[1024]; - ERROR ("recv failed: %s", - sstrerror (errno, errbuf, - sizeof (errbuf))); - return (-1); + status = (errno != 0) ? errno : -1; + ERROR ("network plugin: recv(2) failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); + break; } stats_octets_rx += ((uint64_t) buffer_len); @@ -2469,7 +2529,8 @@ static int network_receive (void) /* {{{ */ if (ent == NULL) { ERROR ("network plugin: malloc failed."); - return (-1); + status = ENOMEM; + break; } memset (ent, 0, sizeof (receive_list_entry_t)); ent->data = malloc (network_config_packet_size); @@ -2477,7 +2538,8 @@ static int network_receive (void) /* {{{ */ { sfree (ent); ERROR ("network plugin: malloc failed."); - return (-1); + status = ENOMEM; + break; } ent->fd = listen_sockets_pollfd[i].fd; ent->next = NULL; @@ -2513,7 +2575,12 @@ static int network_receive (void) /* {{{ */ private_list_tail = NULL; private_list_length = 0; } + + status = 0; } /* for (listen_sockets_pollfd) */ + + if (status != 0) + break; } /* while (listen_loop == 0) */ /* Make sure everything is dispatched before exiting. */ @@ -2528,15 +2595,11 @@ static int network_receive (void) /* {{{ */ receive_list_tail = private_list_tail; receive_list_length += private_list_length; - private_list_head = NULL; - private_list_tail = NULL; - private_list_length = 0; - pthread_cond_signal (&receive_list_cond); pthread_mutex_unlock (&receive_list_lock); } - return (0); + return (status); } /* }}} int network_receive */ static void *receive_thread (void __attribute__((unused)) *arg) @@ -2857,6 +2920,11 @@ static int network_write (const data_set_t *ds, const value_list_t *vl, { int status; + /* listen_loop is set to non-zero in the shutdown callback, which is + * guaranteed to be called *after* all the write threads have been shut + * down. */ + assert (listen_loop == 0); + if (!check_send_okay (vl)) { #if COLLECT_DEBUG @@ -2979,7 +3047,7 @@ static int network_config_set_ttl (const oconfig_item_t *ci) /* {{{ */ network_config_ttl = tmp; else { WARNING ("network plugin: The `TimeToLive' must be between 1 and 255."); - return (-1); + return (-1); } return (0); @@ -3206,6 +3274,8 @@ static int network_config_add_server (const oconfig_item_t *ci) /* {{{ */ if (strcasecmp ("Interface", child->key) == 0) network_config_set_interface (child, &se->interface); + else if (strcasecmp ("ResolveInterval", child->key) == 0) + cf_util_get_cdtime(child, &se->data.client.resolve_interval); else { WARNING ("network plugin: Option `%s' is not allowed here.", @@ -3494,7 +3564,11 @@ static int network_init (void) have_init = 1; #if HAVE_LIBGCRYPT - network_init_gcrypt (); + if (network_init_gcrypt () < 0) + { + ERROR ("network plugin: Failed to initialize crypto library."); + return (-1); + } #endif if (network_config_stats != 0)