X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fnetwork.c;h=6ed9a4e5a1758c67c3fcfa4603522f0fe13fbb78;hb=ab11d1b4db61f750f51be6e41dbe8320d3c65404;hp=086f04eccd28700327e472e60394d0e61db0d50c;hpb=6894d846ac42cbe88073db074d75279f5a6e31b8;p=collectd.git diff --git a/src/network.c b/src/network.c index 086f04ec..6ed9a4e5 100644 --- a/src/network.c +++ b/src/network.c @@ -30,15 +30,11 @@ #include "common.h" #include "configfile.h" #include "utils_fbhash.h" -#include "utils_avltree.h" #include "utils_cache.h" #include "utils_complain.h" #include "network.h" -#if HAVE_PTHREAD_H -# include -#endif #if HAVE_NETDB_H # include #endif @@ -56,7 +52,6 @@ #endif #if HAVE_LIBGCRYPT -# include # if defined __APPLE__ /* default xcode compiler throws warnings even when deprecated functionality * is not used. -Werror breaks the build because of erroneous warnings. @@ -414,10 +409,10 @@ static int network_dispatch_values (value_list_t *vl, /* {{{ */ { int status; - if ((vl->time <= 0) - || (strlen (vl->host) <= 0) - || (strlen (vl->plugin) <= 0) - || (strlen (vl->type) <= 0)) + if ((vl->time == 0) + || (strlen (vl->host) == 0) + || (strlen (vl->plugin) == 0) + || (strlen (vl->type) == 0)) return (-EINVAL); if (!check_receive_okay (vl)) @@ -498,6 +493,8 @@ static int network_dispatch_notification (notification_t *n) /* {{{ */ #if HAVE_LIBGCRYPT static void 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 */ @@ -512,10 +509,23 @@ 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)); + abort (); + } # 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)); + abort (); + } + gcry_control (GCRYCTL_INITIALIZATION_FINISHED); } /* }}} void network_init_gcrypt */ @@ -618,14 +628,14 @@ static int write_part_values (char **ret_buffer, int *ret_buffer_len, if (*ret_buffer_len < packet_len) return (-1); - pkg_values_types = (uint8_t *) malloc (num_values * sizeof (uint8_t)); + pkg_values_types = malloc (num_values * sizeof (*pkg_values_types)); if (pkg_values_types == NULL) { ERROR ("network plugin: write_part_values: malloc failed."); return (-1); } - pkg_values = (value_t *) malloc (num_values * sizeof (value_t)); + pkg_values = malloc (num_values * sizeof (*pkg_values)); if (pkg_values == NULL) { free (pkg_values_types); @@ -934,7 +944,7 @@ static int parse_part_string (void **ret_buffer, size_t *ret_buffer_len, uint16_t pkg_length; size_t payload_size; - if (output_len <= 0) + if (output_len == 0) return (EINVAL); if (buffer_len < header_size) @@ -1250,7 +1260,7 @@ static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */ BUFFER_READ (&username_len, sizeof (username_len)); username_len = ntohs (username_len); - if ((username_len <= 0) + if ((username_len == 0) || (username_len > (part_size - (PART_ENCRYPTION_AES256_SIZE + 1)))) { NOTICE ("network plugin: parse_part_encr_aes256: " @@ -1369,7 +1379,7 @@ static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */ warning_has_been_printed = 1; } - *ret_buffer += ph_length; + *ret_buffer = (void *) (((char *) *ret_buffer) + ph_length); *ret_buffer_size -= ph_length; return (0); @@ -1408,7 +1418,7 @@ static int parse_packet (sockent_t *se, /* {{{ */ (void *) buffer, sizeof (pkg_type)); memcpy ((void *) &pkg_length, - (void *) (buffer + sizeof (pkg_type)), + (void *) (((char *) buffer) + sizeof (pkg_type)), sizeof (pkg_length)); pkg_length = ntohs (pkg_length); @@ -1443,6 +1453,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 */ @@ -1470,6 +1481,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 */ @@ -1581,13 +1593,13 @@ static int parse_packet (sockent_t *se, /* {{{ */ "unknown severity %i.", n.severity); } - else if (n.time <= 0) + else if (n.time == 0) { INFO ("network plugin: " "Ignoring notification with " "time == 0."); } - else if (strlen (n.message) <= 0) + else if (strlen (n.message) == 0) { INFO ("network plugin: " "Ignoring notification with " @@ -1611,6 +1623,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)) */ @@ -2012,10 +2025,9 @@ static sockent_t *sockent_create (int type) /* {{{ */ if ((type != SOCKENT_TYPE_CLIENT) && (type != SOCKENT_TYPE_SERVER)) return (NULL); - se = malloc (sizeof (*se)); + se = calloc (1, sizeof (*se)); if (se == NULL) return (NULL); - memset (se, 0, sizeof (*se)); se->type = type; se->node = NULL; @@ -2196,16 +2208,15 @@ static int sockent_client_connect (sockent_t *se) /* {{{ */ continue; } - client->addr = malloc (sizeof (*client->addr)); + client->addr = calloc (1, sizeof (*client->addr)); if (client->addr == NULL) { - ERROR ("network plugin: malloc failed."); + ERROR ("network plugin: calloc failed."); close (client->fd); client->fd = -1; continue; } - memset (client->addr, 0, sizeof (*client->addr)); assert (sizeof (*client->addr) >= ai_ptr->ai_addrlen); memcpy (client->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen); client->addrlen = ai_ptr->ai_addrlen; @@ -2313,7 +2324,7 @@ static int sockent_server_listen (sockent_t *se) /* {{{ */ freeaddrinfo (ai_list); - if (se->data.server.fd_num <= 0) + if (se->data.server.fd_num == 0) return (-1); return (0); } /* }}} int sockent_server_listen */ @@ -2495,14 +2506,14 @@ static int network_receive (void) /* {{{ */ * these entries in the dispatch thread but put them in * another list, so we don't have to allocate more and * more of these structures. */ - ent = malloc (sizeof (receive_list_entry_t)); + ent = calloc (1, sizeof (*ent)); if (ent == NULL) { - ERROR ("network plugin: malloc failed."); + ERROR ("network plugin: calloc failed."); status = ENOMEM; break; } - memset (ent, 0, sizeof (receive_list_entry_t)); + ent->data = malloc (network_config_packet_size); if (ent->data == NULL) {