Merge branch 'collectd-4.7' into collectd-4.8
[collectd.git] / src / network.c
index 156028e..ac69254 100644 (file)
@@ -1,22 +1,25 @@
 /**
  * collectd - src/network.c
  * Copyright (C) 2005-2009  Florian octo Forster
+ * Copyright (C) 2009       Aman Gupta
  *
  * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the
- * Free Software Foundation; only version 2 of the License is applicable.
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; only version 2.1 of the License is
+ * applicable.
  *
  * This program is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License for more details.
+ * Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  *
  * Authors:
  *   Florian octo Forster <octo at verplant.org>
+ *   Aman Gupta <aman at tmm1.net>
  **/
 
 #define _BSD_SOURCE /* For struct ip_mreq */
@@ -52,6 +55,7 @@
 
 #if HAVE_LIBGCRYPT
 # include <gcrypt.h>
+GCRY_THREAD_OPTION_PTHREAD_IMPL;
 #endif
 
 #ifndef IPV6_ADD_MEMBERSHIP
@@ -1958,7 +1962,7 @@ static void *dispatch_thread (void __attribute__((unused)) *arg) /* {{{ */
     /* Lock and wait for more data to come in */
     pthread_mutex_lock (&receive_list_lock);
     while ((listen_loop == 0)
-       && (receive_list_head == NULL))
+        && (receive_list_head == NULL))
       pthread_cond_wait (&receive_list_cond, &receive_list_lock);
 
     /* Remove the head entry and unlock */
@@ -1990,14 +1994,16 @@ static void *dispatch_thread (void __attribute__((unused)) *arg) /* {{{ */
 
     if (se == NULL)
     {
-           ERROR ("network plugin: Got packet from FD %i, but can't "
-                           "find an appropriate socket entry.",
-                           ent->fd);
-           sfree (ent);
-           continue;
+      ERROR ("network plugin: Got packet from FD %i, but can't "
+          "find an appropriate socket entry.",
+          ent->fd);
+      sfree (ent->data);
+      sfree (ent);
+      continue;
     }
 
     parse_packet (se, ent->data, ent->data_len, /* flags = */ 0);
+    sfree (ent->data);
     sfree (ent);
   } /* while (42) */
 
@@ -2075,11 +2081,6 @@ static int network_receive (void) /* {{{ */
                        ent->fd = listen_sockets_pollfd[i].fd;
                        ent->next = NULL;
 
-                       /* Hopefully this be optimized out by the compiler. It
-                        * might help prevent stupid bugs in the future though.
-                        */
-                       assert (sizeof (ent->data) == sizeof (buffer));
-
                        memcpy (ent->data, buffer, buffer_len);
                        ent->data_len = buffer_len;
 
@@ -2136,7 +2137,7 @@ static void *receive_thread (void __attribute__((unused)) *arg)
 
 static void network_init_buffer (void)
 {
-       memset (send_buffer, 0, sizeof (send_buffer));
+       memset (send_buffer, 0, network_config_packet_size);
        send_buffer_ptr = send_buffer;
        send_buffer_fill = 0;
 
@@ -2455,7 +2456,7 @@ static int network_write (const data_set_t *ds, const value_list_t *vl,
        pthread_mutex_lock (&send_buffer_lock);
 
        status = add_to_buffer (send_buffer_ptr,
-                       sizeof (send_buffer) - (send_buffer_fill + BUFF_SIG_SIZE),
+                       network_config_packet_size - (send_buffer_fill + BUFF_SIG_SIZE),
                        &send_buffer_vl,
                        ds, vl);
        if (status >= 0)
@@ -2469,7 +2470,7 @@ static int network_write (const data_set_t *ds, const value_list_t *vl,
                flush_buffer ();
 
                status = add_to_buffer (send_buffer_ptr,
-                               sizeof (send_buffer) - (send_buffer_fill + BUFF_SIG_SIZE),
+                               network_config_packet_size - (send_buffer_fill + BUFF_SIG_SIZE),
                                &send_buffer_vl,
                                ds, vl);
 
@@ -2485,7 +2486,7 @@ static int network_write (const data_set_t *ds, const value_list_t *vl,
                ERROR ("network plugin: Unable to append to the "
                                "buffer for some weird reason");
        }
-       else if ((sizeof (send_buffer) - send_buffer_fill) < 15)
+       else if ((network_config_packet_size - send_buffer_fill) < 15)
        {
                flush_buffer ();
        }
@@ -2915,7 +2916,7 @@ static int network_shutdown (void)
        if (send_buffer_fill > 0)
                flush_buffer ();
 
-       free (send_buffer);
+       sfree (send_buffer);
 
        /* TODO: Close `sending_sockets' */
 
@@ -2937,6 +2938,12 @@ static int network_init (void)
                return (0);
        have_init = true;
 
+#if HAVE_LIBGCRYPT
+       gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
+       gcry_control (GCRYCTL_INIT_SECMEM, 32768, 0);
+       gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
+#endif
+
        plugin_register_shutdown ("network", network_shutdown);
 
        send_buffer = malloc (network_config_packet_size);