Merge branch 'collectd-5.4' into collectd-5.5
[collectd.git] / src / network.c
index 701571d..d52da68 100644 (file)
@@ -22,6 +22,7 @@
  *   Aman Gupta <aman at tmm1.net>
  **/
 
+#define _DEFAULT_SOURCE
 #define _BSD_SOURCE /* For struct ip_mreq */
 
 #include "collectd.h"
@@ -76,7 +77,9 @@
 /* Re enable deprecation warnings */
 #  pragma GCC diagnostic warning "-Wdeprecated-declarations"
 # endif
+# if GCRYPT_VERSION_NUMBER < 0x010600
 GCRY_THREAD_OPTION_PTHREAD_IMPL;
+# endif
 #endif
 
 #ifndef IPV6_ADD_MEMBERSHIP
@@ -117,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
@@ -508,7 +513,9 @@ static void network_init_gcrypt (void) /* {{{ */
   * above doesn't count, as it doesn't implicitly initalize Libgcrypt.
   *
   * 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);
+# endif
   gcry_check_version (NULL);
   gcry_control (GCRYCTL_INIT_SECMEM, 32768);
   gcry_control (GCRYCTL_INITIALIZATION_FINISHED);
@@ -2003,7 +2010,7 @@ static sockent_t *sockent_create (int type) /* {{{ */
 {
        sockent_t *se;
 
-       if ((type != SOCKENT_TYPE_CLIENT) || (type != SOCKENT_TYPE_SERVER))
+       if ((type != SOCKENT_TYPE_CLIENT) && (type != SOCKENT_TYPE_SERVER))
                return (NULL);
 
        se = malloc (sizeof (*se));
@@ -2020,6 +2027,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;
@@ -2031,6 +2039,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;
@@ -2097,6 +2107,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;
@@ -2105,12 +2135,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));
@@ -2142,6 +2182,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);
@@ -2179,28 +2222,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) /* {{{ */
@@ -2215,6 +2241,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;
 
@@ -2414,13 +2443,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;
@@ -2429,15 +2458,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++)
@@ -2455,10 +2483,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);
@@ -2472,7 +2500,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);
@@ -2480,7 +2509,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;
@@ -2516,7 +2546,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. */
@@ -2539,7 +2574,7 @@ static int network_receive (void) /* {{{ */
                pthread_mutex_unlock (&receive_list_lock);
        }
 
-       return (0);
+       return (status);
 } /* }}} int network_receive */
 
 static void *receive_thread (void __attribute__((unused)) *arg)
@@ -2982,7 +3017,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);
@@ -3209,6 +3244,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.",