2 * collectd - src/network.c
3 * Copyright (C) 2005-2009 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
25 #include "configfile.h"
26 #include "utils_fbhash.h"
27 #include "utils_avltree.h"
35 # include <sys/socket.h>
41 # include <netinet/in.h>
44 # include <arpa/inet.h>
54 /* 1500 - 40 - 8 = Ethernet packet - IPv6 header - UDP header */
55 /* #define BUFF_SIZE 1452 */
57 #ifndef IPV6_ADD_MEMBERSHIP
58 # ifdef IPV6_JOIN_GROUP
59 # define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
61 # error "Neither IP_ADD_MEMBERSHIP nor IPV6_JOIN_GROUP is defined"
63 #endif /* !IP_ADD_MEMBERSHIP */
65 /* Buffer size to allocate. */
66 #define BUFF_SIZE 1024
69 * Maximum size required for encryption / signing:
71 * 42 bytes for the encryption header
72 * + 64 bytes for the username
76 #define BUFF_SIG_SIZE 106
81 #define SECURITY_LEVEL_NONE 0
83 # define SECURITY_LEVEL_SIGN 1
84 # define SECURITY_LEVEL_ENCRYPT 2
89 struct sockaddr_storage *addr;
95 gcry_cipher_hd_t cypher;
96 unsigned char password_hash[32];
100 struct sockent_server
108 gcry_cipher_hd_t cypher;
112 typedef struct sockent
114 #define SOCKENT_TYPE_CLIENT 1
115 #define SOCKENT_TYPE_SERVER 2
123 struct sockent_client client;
124 struct sockent_server server;
127 struct sockent *next;
130 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
131 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
132 * +-------+-----------------------+-------------------------------+
133 * ! Ver. ! ! Length !
134 * +-------+-----------------------+-------------------------------+
141 typedef struct part_header_s part_header_t;
143 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
144 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
145 * +-------------------------------+-------------------------------+
147 * +-------------------------------+-------------------------------+
148 * : (Length - 4) Bytes :
149 * +---------------------------------------------------------------+
156 typedef struct part_string_s part_string_t;
158 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
159 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
160 * +-------------------------------+-------------------------------+
162 * +-------------------------------+-------------------------------+
163 * : (Length - 4 == 2 || 4 || 8) Bytes :
164 * +---------------------------------------------------------------+
171 typedef struct part_number_s part_number_t;
173 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
174 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
175 * +-------------------------------+-------------------------------+
177 * +-------------------------------+---------------+---------------+
178 * ! Num of values ! Type0 ! Type1 !
179 * +-------------------------------+---------------+---------------+
182 * +---------------------------------------------------------------+
185 * +---------------------------------------------------------------+
190 uint16_t *num_values;
191 uint8_t *values_types;
194 typedef struct part_values_s part_values_t;
196 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
197 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
198 * +-------------------------------+-------------------------------+
200 * +-------------------------------+-------------------------------+
201 * ! Hash (Bits 0 - 31) !
203 * ! Hash (Bits 224 - 255) !
204 * +---------------------------------------------------------------+
207 #define PART_SIGNATURE_SHA256_SIZE 36
208 struct part_signature_sha256_s
211 unsigned char hash[32];
214 typedef struct part_signature_sha256_s part_signature_sha256_t;
216 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
217 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
218 * +-------------------------------+-------------------------------+
220 * +-------------------------------+-------------------------------+
221 * ! Original length ! Padding (0 - 15 bytes) !
222 * +-------------------------------+-------------------------------+
223 * ! Hash (Bits 0 - 31) !
225 * ! Hash (Bits 128 - 159) !
226 * +---------------------------------------------------------------+
229 #define PART_ENCRYPTION_AES256_SIZE 42
230 struct part_encryption_aes256_s
233 uint16_t username_length;
235 unsigned char iv[16];
237 unsigned char hash[20];
241 typedef struct part_encryption_aes256_s part_encryption_aes256_t;
243 struct receive_list_entry_s
245 char data[BUFF_SIZE];
248 struct receive_list_entry_s *next;
250 typedef struct receive_list_entry_s receive_list_entry_t;
255 static int network_config_ttl = 0;
256 static int network_config_forward = 0;
258 static sockent_t *sending_sockets = NULL;
260 static receive_list_entry_t *receive_list_head = NULL;
261 static receive_list_entry_t *receive_list_tail = NULL;
262 static pthread_mutex_t receive_list_lock = PTHREAD_MUTEX_INITIALIZER;
263 static pthread_cond_t receive_list_cond = PTHREAD_COND_INITIALIZER;
265 static sockent_t *listen_sockets = NULL;
266 static struct pollfd *listen_sockets_pollfd = NULL;
267 static size_t listen_sockets_num = 0;
269 /* The receive and dispatch threads will run as long as `listen_loop' is set to
271 static int listen_loop = 0;
272 static int receive_thread_running = 0;
273 static pthread_t receive_thread_id;
274 static int dispatch_thread_running = 0;
275 static pthread_t dispatch_thread_id;
277 /* Buffer in which to-be-sent network packets are constructed. */
278 static char send_buffer[BUFF_SIZE];
279 static char *send_buffer_ptr;
280 static int send_buffer_fill;
281 static value_list_t send_buffer_vl = VALUE_LIST_STATIC;
282 static pthread_mutex_t send_buffer_lock = PTHREAD_MUTEX_INITIALIZER;
284 /* In this cache we store all the values we received, so we can send out only
285 * those values which were *not* received via the network plugin, too. This is
286 * used for the `Forward false' option. */
287 static c_avl_tree_t *cache_tree = NULL;
288 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
289 static time_t cache_flush_last = 0;
290 static int cache_flush_interval = 1800;
295 static int cache_flush (void)
305 c_avl_iterator_t *iter;
307 time_t curtime = time (NULL);
309 iter = c_avl_get_iterator (cache_tree);
310 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
312 if ((curtime - *value) <= cache_flush_interval)
314 tmp = (char **) realloc (keys,
315 (keys_num + 1) * sizeof (char *));
319 c_avl_iterator_destroy (iter);
320 ERROR ("network plugin: cache_flush: realloc"
325 keys[keys_num] = key;
327 } /* while (c_avl_iterator_next) */
328 c_avl_iterator_destroy (iter);
330 for (i = 0; i < keys_num; i++)
332 if (c_avl_remove (cache_tree, keys[i], (void *) &key,
333 (void *) &value) != 0)
335 WARNING ("network plugin: cache_flush: c_avl_remove"
336 " (%s) failed.", keys[i]);
346 DEBUG ("network plugin: cache_flush: Removed %i %s",
347 keys_num, (keys_num == 1) ? "entry" : "entries");
348 cache_flush_last = curtime;
350 } /* int cache_flush */
352 static int cache_check (const value_list_t *vl)
355 time_t *value = NULL;
358 if (cache_tree == NULL)
361 if (format_name (key, sizeof (key), vl->host, vl->plugin,
362 vl->plugin_instance, vl->type, vl->type_instance))
365 pthread_mutex_lock (&cache_lock);
367 if (c_avl_get (cache_tree, key, (void *) &value) == 0)
369 if (*value < vl->time)
376 DEBUG ("network plugin: cache_check: *value = %i >= vl->time = %i",
377 (int) *value, (int) vl->time);
383 char *key_copy = strdup (key);
384 value = malloc (sizeof (time_t));
385 if ((key_copy != NULL) && (value != NULL))
388 c_avl_insert (cache_tree, key_copy, value);
398 if ((time (NULL) - cache_flush_last) > cache_flush_interval)
401 pthread_mutex_unlock (&cache_lock);
404 } /* int cache_check */
407 static gcry_cipher_hd_t network_get_aes256_cypher (sockent_t *se, /* {{{ */
408 const void *iv, size_t iv_size, const char *username)
411 gcry_cipher_hd_t *cyper_ptr;
412 unsigned char password_hash[32];
414 if (se->type == SOCKENT_TYPE_CLIENT)
416 cyper_ptr = &se->data.client.cypher;
417 memcpy (password_hash, se->data.client.password_hash,
418 sizeof (password_hash));
424 cyper_ptr = &se->data.server.cypher;
426 if (username == NULL)
429 secret = fbh_get (se->data.server.userdb, username);
433 gcry_md_hash_buffer (GCRY_MD_SHA256,
435 secret, strlen (secret));
440 if (*cyper_ptr == NULL)
442 err = gcry_cipher_open (cyper_ptr,
443 GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_OFB, /* flags = */ 0);
446 ERROR ("network plugin: gcry_cipher_open returned: %s",
447 gcry_strerror (err));
454 gcry_cipher_reset (*cyper_ptr);
456 assert (*cyper_ptr != NULL);
458 err = gcry_cipher_setkey (*cyper_ptr,
459 password_hash, sizeof (password_hash));
462 ERROR ("network plugin: gcry_cipher_setkey returned: %s",
463 gcry_strerror (err));
464 gcry_cipher_close (*cyper_ptr);
469 err = gcry_cipher_setiv (*cyper_ptr, iv, iv_size);
472 ERROR ("network plugin: gcry_cipher_setkey returned: %s",
473 gcry_strerror (err));
474 gcry_cipher_close (*cyper_ptr);
480 } /* }}} int network_get_aes256_cypher */
481 #endif /* HAVE_LIBGCRYPT */
483 static int write_part_values (char **ret_buffer, int *ret_buffer_len,
484 const data_set_t *ds, const value_list_t *vl)
490 part_header_t pkg_ph;
491 uint16_t pkg_num_values;
492 uint8_t *pkg_values_types;
498 num_values = vl->values_len;
499 packet_len = sizeof (part_header_t) + sizeof (uint16_t)
500 + (num_values * sizeof (uint8_t))
501 + (num_values * sizeof (value_t));
503 if (*ret_buffer_len < packet_len)
506 pkg_values_types = (uint8_t *) malloc (num_values * sizeof (uint8_t));
507 if (pkg_values_types == NULL)
509 ERROR ("network plugin: write_part_values: malloc failed.");
513 pkg_values = (value_t *) malloc (num_values * sizeof (value_t));
514 if (pkg_values == NULL)
516 free (pkg_values_types);
517 ERROR ("network plugin: write_part_values: malloc failed.");
521 pkg_ph.type = htons (TYPE_VALUES);
522 pkg_ph.length = htons (packet_len);
524 pkg_num_values = htons ((uint16_t) vl->values_len);
526 for (i = 0; i < num_values; i++)
528 if (ds->ds[i].type == DS_TYPE_COUNTER)
530 pkg_values_types[i] = DS_TYPE_COUNTER;
531 pkg_values[i].counter = htonll (vl->values[i].counter);
535 pkg_values_types[i] = DS_TYPE_GAUGE;
536 pkg_values[i].gauge = htond (vl->values[i].gauge);
541 * Use `memcpy' to write everything to the buffer, because the pointer
542 * may be unaligned and some architectures, such as SPARC, can't handle
545 packet_ptr = *ret_buffer;
547 memcpy (packet_ptr + offset, &pkg_ph, sizeof (pkg_ph));
548 offset += sizeof (pkg_ph);
549 memcpy (packet_ptr + offset, &pkg_num_values, sizeof (pkg_num_values));
550 offset += sizeof (pkg_num_values);
551 memcpy (packet_ptr + offset, pkg_values_types, num_values * sizeof (uint8_t));
552 offset += num_values * sizeof (uint8_t);
553 memcpy (packet_ptr + offset, pkg_values, num_values * sizeof (value_t));
554 offset += num_values * sizeof (value_t);
556 assert (offset == packet_len);
558 *ret_buffer = packet_ptr + packet_len;
559 *ret_buffer_len -= packet_len;
561 free (pkg_values_types);
565 } /* int write_part_values */
567 static int write_part_number (char **ret_buffer, int *ret_buffer_len,
568 int type, uint64_t value)
573 part_header_t pkg_head;
578 packet_len = sizeof (pkg_head) + sizeof (pkg_value);
580 if (*ret_buffer_len < packet_len)
583 pkg_head.type = htons (type);
584 pkg_head.length = htons (packet_len);
585 pkg_value = htonll (value);
587 packet_ptr = *ret_buffer;
589 memcpy (packet_ptr + offset, &pkg_head, sizeof (pkg_head));
590 offset += sizeof (pkg_head);
591 memcpy (packet_ptr + offset, &pkg_value, sizeof (pkg_value));
592 offset += sizeof (pkg_value);
594 assert (offset == packet_len);
596 *ret_buffer = packet_ptr + packet_len;
597 *ret_buffer_len -= packet_len;
600 } /* int write_part_number */
602 static int write_part_string (char **ret_buffer, int *ret_buffer_len,
603 int type, const char *str, int str_len)
613 buffer_len = 2 * sizeof (uint16_t) + str_len + 1;
614 if (*ret_buffer_len < buffer_len)
617 pkg_type = htons (type);
618 pkg_length = htons (buffer_len);
620 buffer = *ret_buffer;
622 memcpy (buffer + offset, (void *) &pkg_type, sizeof (pkg_type));
623 offset += sizeof (pkg_type);
624 memcpy (buffer + offset, (void *) &pkg_length, sizeof (pkg_length));
625 offset += sizeof (pkg_length);
626 memcpy (buffer + offset, str, str_len);
628 memset (buffer + offset, '\0', 1);
631 assert (offset == buffer_len);
633 *ret_buffer = buffer + buffer_len;
634 *ret_buffer_len -= buffer_len;
637 } /* int write_part_string */
639 static int parse_part_values (void **ret_buffer, size_t *ret_buffer_len,
640 value_t **ret_values, int *ret_num_values)
642 char *buffer = *ret_buffer;
643 size_t buffer_len = *ret_buffer_len;
658 NOTICE ("network plugin: packet is too short: "
659 "buffer_len = %zu", buffer_len);
663 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
664 buffer += sizeof (tmp16);
665 pkg_type = ntohs (tmp16);
667 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
668 buffer += sizeof (tmp16);
669 pkg_length = ntohs (tmp16);
671 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
672 buffer += sizeof (tmp16);
673 pkg_numval = ntohs (tmp16);
675 assert (pkg_type == TYPE_VALUES);
677 exp_size = 3 * sizeof (uint16_t)
678 + pkg_numval * (sizeof (uint8_t) + sizeof (value_t));
679 if ((buffer_len < 0) || (buffer_len < exp_size))
681 WARNING ("network plugin: parse_part_values: "
683 "Chunk of size %zu expected, "
684 "but buffer has only %zu bytes left.",
685 exp_size, buffer_len);
689 if (pkg_length != exp_size)
691 WARNING ("network plugin: parse_part_values: "
692 "Length and number of values "
693 "in the packet don't match.");
697 pkg_types = (uint8_t *) malloc (pkg_numval * sizeof (uint8_t));
698 pkg_values = (value_t *) malloc (pkg_numval * sizeof (value_t));
699 if ((pkg_types == NULL) || (pkg_values == NULL))
703 ERROR ("network plugin: parse_part_values: malloc failed.");
707 memcpy ((void *) pkg_types, (void *) buffer, pkg_numval * sizeof (uint8_t));
708 buffer += pkg_numval * sizeof (uint8_t);
709 memcpy ((void *) pkg_values, (void *) buffer, pkg_numval * sizeof (value_t));
710 buffer += pkg_numval * sizeof (value_t);
712 for (i = 0; i < pkg_numval; i++)
714 if (pkg_types[i] == DS_TYPE_COUNTER)
715 pkg_values[i].counter = ntohll (pkg_values[i].counter);
716 else if (pkg_types[i] == DS_TYPE_GAUGE)
717 pkg_values[i].gauge = ntohd (pkg_values[i].gauge);
720 *ret_buffer = buffer;
721 *ret_buffer_len = buffer_len - pkg_length;
722 *ret_num_values = pkg_numval;
723 *ret_values = pkg_values;
728 } /* int parse_part_values */
730 static int parse_part_number (void **ret_buffer, size_t *ret_buffer_len,
733 char *buffer = *ret_buffer;
734 size_t buffer_len = *ret_buffer_len;
738 size_t exp_size = 2 * sizeof (uint16_t) + sizeof (uint64_t);
743 if ((buffer_len < 0) || ((size_t) buffer_len < exp_size))
745 WARNING ("network plugin: parse_part_number: "
747 "Chunk of size %zu expected, "
748 "but buffer has only %zu bytes left.",
749 exp_size, buffer_len);
753 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
754 buffer += sizeof (tmp16);
755 pkg_type = ntohs (tmp16);
757 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
758 buffer += sizeof (tmp16);
759 pkg_length = ntohs (tmp16);
761 memcpy ((void *) &tmp64, buffer, sizeof (tmp64));
762 buffer += sizeof (tmp64);
763 *value = ntohll (tmp64);
765 *ret_buffer = buffer;
766 *ret_buffer_len = buffer_len - pkg_length;
769 } /* int parse_part_number */
771 static int parse_part_string (void **ret_buffer, size_t *ret_buffer_len,
772 char *output, int output_len)
774 char *buffer = *ret_buffer;
775 size_t buffer_len = *ret_buffer_len;
778 size_t header_size = 2 * sizeof (uint16_t);
783 if ((buffer_len < 0) || (buffer_len < header_size))
785 WARNING ("network plugin: parse_part_string: "
787 "Chunk of at least size %zu expected, "
788 "but buffer has only %zu bytes left.",
789 header_size, buffer_len);
793 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
794 buffer += sizeof (tmp16);
795 pkg_type = ntohs (tmp16);
797 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
798 buffer += sizeof (tmp16);
799 pkg_length = ntohs (tmp16);
801 /* Check that packet fits in the input buffer */
802 if (pkg_length > buffer_len)
804 WARNING ("network plugin: parse_part_string: "
806 "Chunk of size %"PRIu16" received, "
807 "but buffer has only %zu bytes left.",
808 pkg_length, buffer_len);
812 /* Check that pkg_length is in the valid range */
813 if (pkg_length <= header_size)
815 WARNING ("network plugin: parse_part_string: "
817 "Header claims this packet is only %hu "
818 "bytes long.", pkg_length);
822 /* Check that the package data fits into the output buffer.
823 * The previous if-statement ensures that:
824 * `pkg_length > header_size' */
826 || ((size_t) output_len < ((size_t) pkg_length - header_size)))
828 WARNING ("network plugin: parse_part_string: "
829 "Output buffer too small.");
833 /* All sanity checks successfull, let's copy the data over */
834 output_len = pkg_length - header_size;
835 memcpy ((void *) output, (void *) buffer, output_len);
836 buffer += output_len;
838 /* For some very weird reason '\0' doesn't do the trick on SPARC in
840 if (output[output_len - 1] != 0)
842 WARNING ("network plugin: parse_part_string: "
843 "Received string does not end "
844 "with a NULL-byte.");
848 *ret_buffer = buffer;
849 *ret_buffer_len = buffer_len - pkg_length;
852 } /* int parse_part_string */
854 /* Forward declaration: parse_part_sign_sha256 and parse_part_encr_aes256 call
855 * parse_packet and vice versa. */
856 #define PP_SIGNED 0x01
857 #define PP_ENCRYPTED 0x02
858 static int parse_packet (sockent_t *se,
859 void *buffer, size_t buffer_size, int flags);
861 #define BUFFER_READ(p,s) do { \
862 memcpy ((p), buffer + buffer_offset, (s)); \
863 buffer_offset += (s); \
867 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
868 void **ret_buffer, size_t *ret_buffer_len, int flags)
872 size_t buffer_offset;
877 part_signature_sha256_t pss;
878 uint16_t pss_head_length;
879 char hash[sizeof (pss.hash)];
883 unsigned char *hash_ptr;
885 buffer = *ret_buffer;
886 buffer_len = *ret_buffer_len;
889 if (se->data.server.userdb == NULL)
891 NOTICE ("network plugin: Received signed network packet but can't verify "
892 "it because no user DB has been configured. Will accept it.");
896 /* Check if the buffer has enough data for this structure. */
897 if (buffer_len <= PART_SIGNATURE_SHA256_SIZE)
900 /* Read type and length header */
901 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
902 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
903 pss_head_length = ntohs (pss.head.length);
905 /* Check if the `pss_head_length' is within bounds. */
906 if ((pss_head_length <= PART_SIGNATURE_SHA256_SIZE)
907 || (pss_head_length > buffer_len))
909 ERROR ("network plugin: HMAC-SHA-256 with invalid length received.");
914 BUFFER_READ (pss.hash, sizeof (pss.hash));
916 /* Calculate username length (without null byte) and allocate memory */
917 username_len = pss_head_length - PART_SIGNATURE_SHA256_SIZE;
918 pss.username = malloc (username_len + 1);
919 if (pss.username == NULL)
922 /* Read the username */
923 BUFFER_READ (pss.username, username_len);
924 pss.username[username_len] = 0;
926 assert (buffer_offset == pss_head_length);
928 /* Query the password */
929 secret = fbh_get (se->data.server.userdb, pss.username);
932 ERROR ("network plugin: Unknown user: %s", pss.username);
933 sfree (pss.username);
937 /* Create a hash device and check the HMAC */
939 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
942 ERROR ("network plugin: Creating HMAC-SHA-256 object failed: %s",
943 gcry_strerror (err));
945 sfree (pss.username);
949 err = gcry_md_setkey (hd, secret, strlen (secret));
952 ERROR ("network plugin: gcry_md_setkey failed: %s", gcry_strerror (err));
958 buffer + PART_SIGNATURE_SHA256_SIZE,
959 buffer_len - PART_SIGNATURE_SHA256_SIZE);
960 hash_ptr = gcry_md_read (hd, GCRY_MD_SHA256);
961 if (hash_ptr == NULL)
963 ERROR ("network plugin: gcry_md_read failed.");
966 sfree (pss.username);
969 memcpy (hash, hash_ptr, sizeof (hash));
976 sfree (pss.username);
978 if (memcmp (pss.hash, hash, sizeof (pss.hash)) != 0)
980 WARNING ("network plugin: Verifying HMAC-SHA-256 signature failed: "
985 parse_packet (se, buffer + buffer_offset, buffer_len - buffer_offset,
989 *ret_buffer = buffer + buffer_len;
993 } /* }}} int parse_part_sign_sha256 */
994 /* #endif HAVE_LIBGCRYPT */
996 #else /* if !HAVE_LIBGCRYPT */
997 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
998 void **ret_buffer, size_t *ret_buffer_size, int flags)
1000 static int warning_has_been_printed = 0;
1004 size_t buffer_offset;
1007 part_signature_sha256_t pss;
1009 buffer = *ret_buffer;
1010 buffer_size = *ret_buffer_size;
1013 if (buffer_size <= PART_SIGNATURE_SHA256_SIZE)
1016 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
1017 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
1018 part_len = ntohs (pss.head.length);
1020 if ((part_len <= PART_SIGNATURE_SHA256_SIZE)
1021 || (part_len > buffer_size))
1024 if (warning_has_been_printed == 0)
1026 WARNING ("network plugin: Received signed packet, but the network "
1027 "plugin was not linked with libgcrypt, so I cannot "
1028 "verify the signature. The packet will be accepted.");
1029 warning_has_been_printed = 1;
1032 parse_packet (se, buffer + part_len, buffer_size - part_len, flags);
1034 *ret_buffer = buffer + buffer_size;
1035 *ret_buffer_size = 0;
1038 } /* }}} int parse_part_sign_sha256 */
1039 #endif /* !HAVE_LIBGCRYPT */
1042 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1043 void **ret_buffer, size_t *ret_buffer_len,
1046 char *buffer = *ret_buffer;
1047 size_t buffer_len = *ret_buffer_len;
1050 size_t buffer_offset;
1051 uint16_t username_len;
1052 part_encryption_aes256_t pea;
1053 unsigned char hash[sizeof (pea.hash)];
1055 gcry_cipher_hd_t cypher;
1058 /* Make sure at least the header if available. */
1059 if (buffer_len <= PART_ENCRYPTION_AES256_SIZE)
1061 NOTICE ("network plugin: parse_part_encr_aes256: "
1062 "Discarding short packet.");
1068 /* Copy the unencrypted information into `pea'. */
1069 BUFFER_READ (&pea.head.type, sizeof (pea.head.type));
1070 BUFFER_READ (&pea.head.length, sizeof (pea.head.length));
1072 /* Check the `part size'. */
1073 part_size = ntohs (pea.head.length);
1074 if ((part_size <= PART_ENCRYPTION_AES256_SIZE)
1075 || (part_size > buffer_len))
1077 NOTICE ("network plugin: parse_part_encr_aes256: "
1078 "Discarding part with invalid size.");
1082 /* Read the username */
1083 BUFFER_READ (&username_len, sizeof (username_len));
1084 username_len = ntohs (username_len);
1086 if ((username_len <= 0)
1087 || (username_len > (part_size - (PART_ENCRYPTION_AES256_SIZE + 1))))
1089 NOTICE ("network plugin: parse_part_encr_aes256: "
1090 "Discarding part with invalid username length.");
1094 assert (username_len > 0);
1095 pea.username = malloc (username_len + 1);
1096 if (pea.username == NULL)
1098 BUFFER_READ (pea.username, username_len);
1099 pea.username[username_len] = 0;
1101 /* Last but not least, the initialization vector */
1102 BUFFER_READ (pea.iv, sizeof (pea.iv));
1104 /* Make sure we are at the right position */
1105 assert (buffer_offset == (username_len +
1106 PART_ENCRYPTION_AES256_SIZE - sizeof (pea.hash)));
1108 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
1113 payload_len = part_size - (PART_ENCRYPTION_AES256_SIZE + username_len);
1114 assert (payload_len > 0);
1116 /* Decrypt the packet in-place */
1117 err = gcry_cipher_decrypt (cypher,
1118 buffer + buffer_offset,
1119 part_size - buffer_offset,
1120 /* in = */ NULL, /* in len = */ 0);
1123 ERROR ("network plugin: gcry_cipher_decrypt returned: %s",
1124 gcry_strerror (err));
1129 BUFFER_READ (pea.hash, sizeof (pea.hash));
1131 /* Make sure we're at the right position - again */
1132 assert (buffer_offset == (username_len + PART_ENCRYPTION_AES256_SIZE));
1133 assert (buffer_offset == (part_size - payload_len));
1135 /* Check hash sum */
1136 memset (hash, 0, sizeof (hash));
1137 gcry_md_hash_buffer (GCRY_MD_SHA1, hash,
1138 buffer + buffer_offset, payload_len);
1139 if (memcmp (hash, pea.hash, sizeof (hash)) != 0)
1141 ERROR ("network plugin: Decryption failed: Checksum mismatch.");
1145 parse_packet (se, buffer + buffer_offset, payload_len,
1146 flags | PP_ENCRYPTED);
1148 /* Update return values */
1149 *ret_buffer = buffer + part_size;
1150 *ret_buffer_len = buffer_len - part_size;
1153 } /* }}} int parse_part_encr_aes256 */
1154 /* #endif HAVE_LIBGCRYPT */
1156 #else /* if !HAVE_LIBGCRYPT */
1157 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1158 void **ret_buffer, size_t *ret_buffer_size, int flags)
1160 static int warning_has_been_printed = 0;
1164 size_t buffer_offset;
1169 buffer = *ret_buffer;
1170 buffer_size = *ret_buffer_size;
1173 /* parse_packet assures this minimum size. */
1174 assert (buffer_size >= (sizeof (ph.type) + sizeof (ph.length)));
1176 BUFFER_READ (&ph.type, sizeof (ph.type));
1177 BUFFER_READ (&ph.length, sizeof (ph.length));
1178 ph_length = ntohs (ph.length);
1180 if ((ph_length <= PART_ENCRYPTION_AES256_SIZE)
1181 || (ph_length > buffer_size))
1183 ERROR ("network plugin: AES-256 encrypted part "
1184 "with invalid length received.");
1188 if (warning_has_been_printed == 0)
1190 WARNING ("network plugin: Received encrypted packet, but the network "
1191 "plugin was not linked with libgcrypt, so I cannot "
1192 "decrypt it. The part will be discarded.");
1193 warning_has_been_printed = 1;
1196 *ret_buffer += ph_length;
1197 *ret_buffer_size -= ph_length;
1200 } /* }}} int parse_part_encr_aes256 */
1201 #endif /* !HAVE_LIBGCRYPT */
1205 static int parse_packet (sockent_t *se, /* {{{ */
1206 void *buffer, size_t buffer_size, int flags)
1210 value_list_t vl = VALUE_LIST_INIT;
1214 int packet_was_signed = (flags & PP_SIGNED);
1215 int packet_was_encrypted = (flags & PP_ENCRYPTED);
1216 int printed_ignore_warning = 0;
1217 #endif /* HAVE_LIBGCRYPT */
1220 memset (&vl, '\0', sizeof (vl));
1221 memset (&n, '\0', sizeof (n));
1224 while ((status == 0) && (0 < buffer_size)
1225 && ((unsigned int) buffer_size > sizeof (part_header_t)))
1227 uint16_t pkg_length;
1230 memcpy ((void *) &pkg_type,
1233 memcpy ((void *) &pkg_length,
1234 (void *) (buffer + sizeof (pkg_type)),
1235 sizeof (pkg_length));
1237 pkg_length = ntohs (pkg_length);
1238 pkg_type = ntohs (pkg_type);
1240 if (pkg_length > buffer_size)
1242 /* Ensure that this loop terminates eventually */
1243 if (pkg_length < (2 * sizeof (uint16_t)))
1246 if (pkg_type == TYPE_ENCR_AES256)
1248 status = parse_part_encr_aes256 (se,
1249 &buffer, &buffer_size, flags);
1252 ERROR ("network plugin: Decrypting AES256 "
1254 "with status %i.", status);
1259 else if ((se->data.server.security_level == SECURITY_LEVEL_ENCRYPT)
1260 && (packet_was_encrypted == 0))
1262 if (printed_ignore_warning == 0)
1264 INFO ("network plugin: Unencrypted packet or "
1265 "part has been ignored.");
1266 printed_ignore_warning = 1;
1268 buffer = ((char *) buffer) + pkg_length;
1271 #endif /* HAVE_LIBGCRYPT */
1272 else if (pkg_type == TYPE_SIGN_SHA256)
1274 status = parse_part_sign_sha256 (se,
1275 &buffer, &buffer_size, flags);
1278 ERROR ("network plugin: Verifying HMAC-SHA-256 "
1280 "with status %i.", status);
1285 else if ((se->data.server.security_level == SECURITY_LEVEL_SIGN)
1286 && (packet_was_encrypted == 0)
1287 && (packet_was_signed == 0))
1289 if (printed_ignore_warning == 0)
1291 INFO ("network plugin: Unsigned packet or "
1292 "part has been ignored.");
1293 printed_ignore_warning = 1;
1295 buffer = ((char *) buffer) + pkg_length;
1298 #endif /* HAVE_LIBGCRYPT */
1299 else if (pkg_type == TYPE_VALUES)
1301 status = parse_part_values (&buffer, &buffer_size,
1302 &vl.values, &vl.values_len);
1308 && (strlen (vl.host) > 0)
1309 && (strlen (vl.plugin) > 0)
1310 && (strlen (vl.type) > 0)
1311 && (cache_check (&vl) == 0))
1313 plugin_dispatch_values (&vl);
1317 DEBUG ("network plugin: parse_packet:"
1318 " NOT dispatching values");
1323 else if (pkg_type == TYPE_TIME)
1326 status = parse_part_number (&buffer, &buffer_size,
1330 vl.time = (time_t) tmp;
1331 n.time = (time_t) tmp;
1334 else if (pkg_type == TYPE_INTERVAL)
1337 status = parse_part_number (&buffer, &buffer_size,
1340 vl.interval = (int) tmp;
1342 else if (pkg_type == TYPE_HOST)
1344 status = parse_part_string (&buffer, &buffer_size,
1345 vl.host, sizeof (vl.host));
1347 sstrncpy (n.host, vl.host, sizeof (n.host));
1349 else if (pkg_type == TYPE_PLUGIN)
1351 status = parse_part_string (&buffer, &buffer_size,
1352 vl.plugin, sizeof (vl.plugin));
1354 sstrncpy (n.plugin, vl.plugin,
1357 else if (pkg_type == TYPE_PLUGIN_INSTANCE)
1359 status = parse_part_string (&buffer, &buffer_size,
1361 sizeof (vl.plugin_instance));
1363 sstrncpy (n.plugin_instance,
1365 sizeof (n.plugin_instance));
1367 else if (pkg_type == TYPE_TYPE)
1369 status = parse_part_string (&buffer, &buffer_size,
1370 vl.type, sizeof (vl.type));
1372 sstrncpy (n.type, vl.type, sizeof (n.type));
1374 else if (pkg_type == TYPE_TYPE_INSTANCE)
1376 status = parse_part_string (&buffer, &buffer_size,
1378 sizeof (vl.type_instance));
1380 sstrncpy (n.type_instance, vl.type_instance,
1381 sizeof (n.type_instance));
1383 else if (pkg_type == TYPE_MESSAGE)
1385 status = parse_part_string (&buffer, &buffer_size,
1386 n.message, sizeof (n.message));
1392 else if ((n.severity != NOTIF_FAILURE)
1393 && (n.severity != NOTIF_WARNING)
1394 && (n.severity != NOTIF_OKAY))
1396 INFO ("network plugin: "
1397 "Ignoring notification with "
1398 "unknown severity %i.",
1401 else if (n.time <= 0)
1403 INFO ("network plugin: "
1404 "Ignoring notification with "
1407 else if (strlen (n.message) <= 0)
1409 INFO ("network plugin: "
1410 "Ignoring notification with "
1411 "an empty message.");
1415 plugin_dispatch_notification (&n);
1418 else if (pkg_type == TYPE_SEVERITY)
1421 status = parse_part_number (&buffer, &buffer_size,
1424 n.severity = (int) tmp;
1428 DEBUG ("network plugin: parse_packet: Unknown part"
1429 " type: 0x%04hx", pkg_type);
1430 buffer = ((char *) buffer) + pkg_length;
1432 } /* while (buffer_size > sizeof (part_header_t)) */
1435 } /* }}} int parse_packet */
1437 static void free_sockent_client (struct sockent_client *sec) /* {{{ */
1446 sfree (sec->username);
1447 sfree (sec->password);
1448 if (sec->cypher != NULL)
1449 gcry_cipher_close (sec->cypher);
1451 } /* }}} void free_sockent_client */
1453 static void free_sockent_server (struct sockent_server *ses) /* {{{ */
1457 for (i = 0; i < ses->fd_num; i++)
1459 if (ses->fd[i] >= 0)
1468 sfree (ses->auth_file);
1469 fbh_destroy (ses->userdb);
1470 if (ses->cypher != NULL)
1471 gcry_cipher_close (ses->cypher);
1473 } /* }}} void free_sockent_server */
1475 static void sockent_destroy (sockent_t *se) /* {{{ */
1479 DEBUG ("network plugin: sockent_destroy (se = %p);", (void *) se);
1486 sfree (se->service);
1488 if (se->type == SOCKENT_TYPE_CLIENT)
1489 free_sockent_client (&se->data.client);
1491 free_sockent_server (&se->data.server);
1496 } /* }}} void sockent_destroy */
1499 * int network_set_ttl
1501 * Set the `IP_MULTICAST_TTL', `IP_TTL', `IPV6_MULTICAST_HOPS' or
1502 * `IPV6_UNICAST_HOPS', depending on which option is applicable.
1504 * The `struct addrinfo' is used to destinguish between unicast and multicast
1507 static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
1509 DEBUG ("network plugin: network_set_ttl: network_config_ttl = %i;",
1510 network_config_ttl);
1512 assert (se->type == SOCKENT_TYPE_CLIENT);
1514 if ((network_config_ttl < 1) || (network_config_ttl > 255))
1517 if (ai->ai_family == AF_INET)
1519 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1522 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1523 optname = IP_MULTICAST_TTL;
1527 if (setsockopt (se->data.client.fd, IPPROTO_IP, optname,
1528 &network_config_ttl,
1529 sizeof (network_config_ttl)) == -1)
1532 ERROR ("setsockopt: %s",
1533 sstrerror (errno, errbuf, sizeof (errbuf)));
1537 else if (ai->ai_family == AF_INET6)
1539 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1540 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1543 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1544 optname = IPV6_MULTICAST_HOPS;
1546 optname = IPV6_UNICAST_HOPS;
1548 if (setsockopt (se->data.client.fd, IPPROTO_IPV6, optname,
1549 &network_config_ttl,
1550 sizeof (network_config_ttl)) == -1)
1553 ERROR ("setsockopt: %s",
1554 sstrerror (errno, errbuf,
1561 } /* int network_set_ttl */
1563 static int network_bind_socket (int fd, const struct addrinfo *ai)
1568 /* allow multiple sockets to use the same PORT number */
1569 if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR,
1570 &yes, sizeof(yes)) == -1) {
1572 ERROR ("setsockopt: %s",
1573 sstrerror (errno, errbuf, sizeof (errbuf)));
1577 DEBUG ("fd = %i; calling `bind'", fd);
1579 if (bind (fd, ai->ai_addr, ai->ai_addrlen) == -1)
1583 sstrerror (errno, errbuf, sizeof (errbuf)));
1587 if (ai->ai_family == AF_INET)
1589 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1590 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1592 struct ip_mreq mreq;
1594 DEBUG ("fd = %i; IPv4 multicast address found", fd);
1596 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1597 mreq.imr_interface.s_addr = htonl (INADDR_ANY);
1599 if (setsockopt (fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1600 &loop, sizeof (loop)) == -1)
1603 ERROR ("setsockopt: %s",
1604 sstrerror (errno, errbuf,
1609 if (setsockopt (fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1610 &mreq, sizeof (mreq)) == -1)
1613 ERROR ("setsockopt: %s",
1614 sstrerror (errno, errbuf,
1620 else if (ai->ai_family == AF_INET6)
1622 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1623 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1624 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1626 struct ipv6_mreq mreq;
1628 DEBUG ("fd = %i; IPv6 multicast address found", fd);
1630 memcpy (&mreq.ipv6mr_multiaddr,
1632 sizeof (addr->sin6_addr));
1634 /* http://developer.apple.com/documentation/Darwin/Reference/ManPages/man4/ip6.4.html
1635 * ipv6mr_interface may be set to zeroes to
1636 * choose the default multicast interface or to
1637 * the index of a particular multicast-capable
1638 * interface if the host is multihomed.
1639 * Membership is associ-associated with a
1640 * single interface; programs running on
1641 * multihomed hosts may need to join the same
1642 * group on more than one interface.*/
1643 mreq.ipv6mr_interface = 0;
1645 if (setsockopt (fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
1646 &loop, sizeof (loop)) == -1)
1649 ERROR ("setsockopt: %s",
1650 sstrerror (errno, errbuf,
1655 if (setsockopt (fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
1656 &mreq, sizeof (mreq)) == -1)
1659 ERROR ("setsockopt: %s",
1660 sstrerror (errno, errbuf,
1668 } /* int network_bind_socket */
1670 /* Initialize a sockent structure. `type' must be either `SOCKENT_TYPE_CLIENT'
1671 * or `SOCKENT_TYPE_SERVER' */
1672 static int sockent_init (sockent_t *se, int type) /* {{{ */
1677 memset (se, 0, sizeof (*se));
1679 se->type = SOCKENT_TYPE_CLIENT;
1684 if (type == SOCKENT_TYPE_SERVER)
1686 se->type = SOCKENT_TYPE_SERVER;
1687 se->data.server.fd = NULL;
1689 se->data.server.security_level = SECURITY_LEVEL_NONE;
1690 se->data.server.auth_file = NULL;
1691 se->data.server.userdb = NULL;
1692 se->data.server.cypher = NULL;
1697 se->data.client.fd = -1;
1698 se->data.client.addr = NULL;
1700 se->data.client.security_level = SECURITY_LEVEL_NONE;
1701 se->data.client.username = NULL;
1702 se->data.client.password = NULL;
1703 se->data.client.cypher = NULL;
1708 } /* }}} int sockent_init */
1710 /* Open the file descriptors for a initialized sockent structure. */
1711 static int sockent_open (sockent_t *se) /* {{{ */
1713 struct addrinfo ai_hints;
1714 struct addrinfo *ai_list, *ai_ptr;
1718 const char *service;
1723 /* Set up the security structures. */
1724 #if HAVE_LIBGCRYPT /* {{{ */
1725 if (se->type == SOCKENT_TYPE_CLIENT)
1727 if (se->data.client.security_level > SECURITY_LEVEL_NONE)
1729 if ((se->data.client.username == NULL)
1730 || (se->data.client.password == NULL))
1732 ERROR ("network plugin: Client socket with "
1733 "security requested, but no "
1734 "credentials are configured.");
1737 gcry_md_hash_buffer (GCRY_MD_SHA256,
1738 se->data.client.password_hash,
1739 se->data.client.password,
1740 strlen (se->data.client.password));
1743 else /* (se->type == SOCKENT_TYPE_SERVER) */
1745 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
1747 if (se->data.server.auth_file == NULL)
1749 ERROR ("network plugin: Server socket with "
1750 "security requested, but no "
1751 "password file is configured.");
1755 if (se->data.server.auth_file != NULL)
1757 se->data.server.userdb = fbh_create (se->data.server.auth_file);
1758 if (se->data.server.userdb == NULL)
1760 ERROR ("network plugin: Reading password file "
1762 se->data.server.auth_file);
1763 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
1768 #endif /* }}} HAVE_LIBGCRYPT */
1771 service = se->service;
1773 if (service == NULL)
1774 service = NET_DEFAULT_PORT;
1776 DEBUG ("network plugin: sockent_open: node = %s; service = %s;",
1779 memset (&ai_hints, 0, sizeof (ai_hints));
1780 ai_hints.ai_flags = 0;
1782 ai_hints.ai_flags |= AI_PASSIVE;
1784 #ifdef AI_ADDRCONFIG
1785 ai_hints.ai_flags |= AI_ADDRCONFIG;
1787 ai_hints.ai_family = AF_UNSPEC;
1788 ai_hints.ai_socktype = SOCK_DGRAM;
1789 ai_hints.ai_protocol = IPPROTO_UDP;
1791 ai_return = getaddrinfo (node, service, &ai_hints, &ai_list);
1794 ERROR ("network plugin: getaddrinfo (%s, %s) failed: %s",
1795 (se->node == NULL) ? "(null)" : se->node,
1796 (se->service == NULL) ? "(null)" : se->service,
1797 gai_strerror (ai_return));
1801 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1805 if (se->type == SOCKENT_TYPE_SERVER) /* {{{ */
1809 tmp = realloc (se->data.server.fd,
1810 sizeof (*tmp) * (se->data.server.fd_num + 1));
1813 ERROR ("network plugin: realloc failed.");
1816 se->data.server.fd = tmp;
1817 tmp = se->data.server.fd + se->data.server.fd_num;
1819 *tmp = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
1820 ai_ptr->ai_protocol);
1824 ERROR ("network plugin: socket(2) failed: %s",
1825 sstrerror (errno, errbuf,
1830 status = network_bind_socket (*tmp, ai_ptr);
1838 se->data.server.fd_num++;
1840 } /* }}} if (se->type == SOCKENT_TYPE_SERVER) */
1841 else /* if (se->type == SOCKENT_TYPE_CLIENT) {{{ */
1843 se->data.client.fd = socket (ai_ptr->ai_family,
1844 ai_ptr->ai_socktype,
1845 ai_ptr->ai_protocol);
1846 if (se->data.client.fd < 0)
1849 ERROR ("network plugin: socket(2) failed: %s",
1850 sstrerror (errno, errbuf,
1855 se->data.client.addr = malloc (sizeof (*se->data.client.addr));
1856 if (se->data.client.addr == NULL)
1858 ERROR ("network plugin: malloc failed.");
1859 close (se->data.client.fd);
1860 se->data.client.fd = -1;
1864 memset (se->data.client.addr, 0, sizeof (*se->data.client.addr));
1865 assert (sizeof (*se->data.client.addr) >= ai_ptr->ai_addrlen);
1866 memcpy (se->data.client.addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1867 se->data.client.addrlen = ai_ptr->ai_addrlen;
1869 network_set_ttl (se, ai_ptr);
1871 /* We don't open more than one write-socket per
1872 * node/service pair.. */
1874 } /* }}} if (se->type == SOCKENT_TYPE_CLIENT) */
1875 } /* for (ai_list) */
1877 freeaddrinfo (ai_list);
1879 /* Check if all went well. */
1880 if (se->type == SOCKENT_TYPE_SERVER)
1882 if (se->data.server.fd_num <= 0)
1885 else /* if (se->type == SOCKENT_TYPE_CLIENT) */
1887 if (se->data.client.fd < 0)
1892 } /* }}} int sockent_open */
1894 /* Add a sockent to the global list of sockets */
1895 static int sockent_add (sockent_t *se) /* {{{ */
1897 sockent_t *last_ptr;
1902 if (se->type == SOCKENT_TYPE_SERVER)
1907 tmp = realloc (listen_sockets_pollfd,
1908 sizeof (*tmp) * (listen_sockets_num
1909 + se->data.server.fd_num));
1912 ERROR ("network plugin: realloc failed.");
1915 listen_sockets_pollfd = tmp;
1916 tmp = listen_sockets_pollfd + listen_sockets_num;
1918 for (i = 0; i < se->data.server.fd_num; i++)
1920 memset (tmp + i, 0, sizeof (*tmp));
1921 tmp[i].fd = se->data.server.fd[i];
1922 tmp[i].events = POLLIN | POLLPRI;
1926 listen_sockets_num += se->data.server.fd_num;
1928 if (listen_sockets == NULL)
1930 listen_sockets = se;
1933 last_ptr = listen_sockets;
1935 else /* if (se->type == SOCKENT_TYPE_CLIENT) */
1937 if (sending_sockets == NULL)
1939 sending_sockets = se;
1942 last_ptr = sending_sockets;
1945 while (last_ptr->next != NULL)
1946 last_ptr = last_ptr->next;
1947 last_ptr->next = se;
1950 } /* }}} int sockent_add */
1952 static void *dispatch_thread (void __attribute__((unused)) *arg) /* {{{ */
1956 receive_list_entry_t *ent;
1959 /* Lock and wait for more data to come in */
1960 pthread_mutex_lock (&receive_list_lock);
1961 while ((listen_loop == 0)
1962 && (receive_list_head == NULL))
1963 pthread_cond_wait (&receive_list_cond, &receive_list_lock);
1965 /* Remove the head entry and unlock */
1966 ent = receive_list_head;
1968 receive_list_head = ent->next;
1969 pthread_mutex_unlock (&receive_list_lock);
1971 /* Check whether we are supposed to exit. We do NOT check `listen_loop'
1972 * because we dispatch all missing packets before shutting down. */
1976 /* Look for the correct `sockent_t' */
1977 se = listen_sockets;
1982 for (i = 0; i < se->data.server.fd_num; i++)
1983 if (se->data.server.fd[i] == ent->fd)
1986 if (i < se->data.server.fd_num)
1994 ERROR ("network plugin: Got packet from FD %i, but can't "
1995 "find an appropriate socket entry.",
2001 parse_packet (se, ent->data, ent->data_len, /* flags = */ 0);
2006 } /* }}} void *dispatch_thread */
2008 static int network_receive (void) /* {{{ */
2010 char buffer[BUFF_SIZE];
2016 receive_list_entry_t *private_list_head;
2017 receive_list_entry_t *private_list_tail;
2019 assert (listen_sockets_num > 0);
2021 private_list_head = NULL;
2022 private_list_tail = NULL;
2024 while (listen_loop == 0)
2026 status = poll (listen_sockets_pollfd, listen_sockets_num, -1);
2033 ERROR ("poll failed: %s",
2034 sstrerror (errno, errbuf, sizeof (errbuf)));
2038 for (i = 0; (i < listen_sockets_num) && (status > 0); i++)
2040 receive_list_entry_t *ent;
2042 if ((listen_sockets_pollfd[i].revents
2043 & (POLLIN | POLLPRI)) == 0)
2047 buffer_len = recv (listen_sockets_pollfd[i].fd,
2048 buffer, sizeof (buffer),
2053 ERROR ("recv failed: %s",
2054 sstrerror (errno, errbuf,
2059 /* TODO: Possible performance enhancement: Do not free
2060 * these entries in the dispatch thread but put them in
2061 * another list, so we don't have to allocate more and
2062 * more of these structures. */
2063 ent = malloc (sizeof (receive_list_entry_t));
2066 ERROR ("network plugin: malloc failed.");
2069 memset (ent, 0, sizeof (receive_list_entry_t));
2070 ent->fd = listen_sockets_pollfd[i].fd;
2073 /* Hopefully this be optimized out by the compiler. It
2074 * might help prevent stupid bugs in the future though.
2076 assert (sizeof (ent->data) == sizeof (buffer));
2078 memcpy (ent->data, buffer, buffer_len);
2079 ent->data_len = buffer_len;
2081 if (private_list_head == NULL)
2082 private_list_head = ent;
2084 private_list_tail->next = ent;
2085 private_list_tail = ent;
2087 /* Do not block here. Blocking here has led to
2088 * insufficient performance in the past. */
2089 if (pthread_mutex_trylock (&receive_list_lock) == 0)
2091 if (receive_list_head == NULL)
2092 receive_list_head = private_list_head;
2094 receive_list_tail->next = private_list_head;
2095 receive_list_tail = private_list_tail;
2097 private_list_head = NULL;
2098 private_list_tail = NULL;
2100 pthread_cond_signal (&receive_list_cond);
2101 pthread_mutex_unlock (&receive_list_lock);
2103 } /* for (listen_sockets_pollfd) */
2104 } /* while (listen_loop == 0) */
2106 /* Make sure everything is dispatched before exiting. */
2107 if (private_list_head != NULL)
2109 pthread_mutex_lock (&receive_list_lock);
2111 if (receive_list_head == NULL)
2112 receive_list_head = private_list_head;
2114 receive_list_tail->next = private_list_head;
2115 receive_list_tail = private_list_tail;
2117 private_list_head = NULL;
2118 private_list_tail = NULL;
2120 pthread_cond_signal (&receive_list_cond);
2121 pthread_mutex_unlock (&receive_list_lock);
2125 } /* }}} int network_receive */
2127 static void *receive_thread (void __attribute__((unused)) *arg)
2129 return (network_receive () ? (void *) 1 : (void *) 0);
2130 } /* void *receive_thread */
2132 static void network_init_buffer (void)
2134 memset (send_buffer, 0, sizeof (send_buffer));
2135 send_buffer_ptr = send_buffer;
2136 send_buffer_fill = 0;
2138 memset (&send_buffer_vl, 0, sizeof (send_buffer_vl));
2139 } /* int network_init_buffer */
2141 static void networt_send_buffer_plain (const sockent_t *se, /* {{{ */
2142 const char *buffer, size_t buffer_size)
2148 status = sendto (se->data.client.fd, buffer, buffer_size,
2150 (struct sockaddr *) se->data.client.addr,
2151 se->data.client.addrlen);
2157 ERROR ("network plugin: sendto failed: %s",
2158 sstrerror (errno, errbuf,
2165 } /* }}} void networt_send_buffer_plain */
2168 #define BUFFER_ADD(p,s) do { \
2169 memcpy (buffer + buffer_offset, (p), (s)); \
2170 buffer_offset += (s); \
2173 static void networt_send_buffer_signed (const sockent_t *se, /* {{{ */
2174 const char *in_buffer, size_t in_buffer_size)
2176 part_signature_sha256_t ps;
2177 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2178 size_t buffer_offset;
2179 size_t username_len;
2183 unsigned char *hash;
2186 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
2189 ERROR ("network plugin: Creating HMAC object failed: %s",
2190 gcry_strerror (err));
2194 err = gcry_md_setkey (hd, se->data.client.password,
2195 strlen (se->data.client.password));
2198 ERROR ("network plugin: gcry_md_setkey failed: %s",
2199 gcry_strerror (err));
2204 username_len = strlen (se->data.client.username);
2205 if (username_len > (BUFF_SIG_SIZE - PART_SIGNATURE_SHA256_SIZE))
2207 ERROR ("network plugin: Username too long: %s",
2208 se->data.client.username);
2212 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE,
2213 se->data.client.username, username_len);
2214 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE + username_len,
2215 in_buffer, in_buffer_size);
2217 /* Initialize the `ps' structure. */
2218 memset (&ps, 0, sizeof (ps));
2219 ps.head.type = htons (TYPE_SIGN_SHA256);
2220 ps.head.length = htons (PART_SIGNATURE_SHA256_SIZE + username_len);
2222 /* Calculate the hash value. */
2223 gcry_md_write (hd, buffer + PART_SIGNATURE_SHA256_SIZE,
2224 username_len + in_buffer_size);
2225 hash = gcry_md_read (hd, GCRY_MD_SHA256);
2228 ERROR ("network plugin: gcry_md_read failed.");
2232 memcpy (ps.hash, hash, sizeof (ps.hash));
2234 /* Add the header */
2237 BUFFER_ADD (&ps.head.type, sizeof (ps.head.type));
2238 BUFFER_ADD (&ps.head.length, sizeof (ps.head.length));
2239 BUFFER_ADD (ps.hash, sizeof (ps.hash));
2241 assert (buffer_offset == PART_SIGNATURE_SHA256_SIZE);
2246 buffer_offset = PART_SIGNATURE_SHA256_SIZE + username_len + in_buffer_size;
2247 networt_send_buffer_plain (se, buffer, buffer_offset);
2248 } /* }}} void networt_send_buffer_signed */
2250 static void networt_send_buffer_encrypted (sockent_t *se, /* {{{ */
2251 const char *in_buffer, size_t in_buffer_size)
2253 part_encryption_aes256_t pea;
2254 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2256 size_t buffer_offset;
2258 size_t username_len;
2260 gcry_cipher_hd_t cypher;
2262 DEBUG ("network plugin: networt_send_buffer_encrypted: "
2263 "buffer_size = %zu;", buffer_size);
2265 /* Initialize the header fields */
2266 memset (&pea, 0, sizeof (pea));
2267 pea.head.type = htons (TYPE_ENCR_AES256);
2269 pea.username = se->data.client.username;
2271 username_len = strlen (pea.username);
2272 if ((PART_ENCRYPTION_AES256_SIZE + username_len) > BUFF_SIG_SIZE)
2274 ERROR ("network plugin: Username too long: %s", pea.username);
2278 buffer_size = PART_ENCRYPTION_AES256_SIZE + username_len + in_buffer_size;
2279 header_size = PART_ENCRYPTION_AES256_SIZE + username_len
2280 - sizeof (pea.hash);
2282 assert (buffer_size <= sizeof (buffer));
2284 pea.head.length = htons ((uint16_t) (PART_ENCRYPTION_AES256_SIZE
2285 + username_len + in_buffer_size));
2286 pea.username_length = htons ((uint16_t) username_len);
2288 /* Chose a random initialization vector. */
2289 gcry_randomize ((void *) &pea.iv, sizeof (pea.iv), GCRY_STRONG_RANDOM);
2291 /* Create hash of the payload */
2292 gcry_md_hash_buffer (GCRY_MD_SHA1, pea.hash, in_buffer, in_buffer_size);
2294 /* Initialize the buffer */
2296 memset (buffer, 0, sizeof (buffer));
2299 BUFFER_ADD (&pea.head.type, sizeof (pea.head.type));
2300 BUFFER_ADD (&pea.head.length, sizeof (pea.head.length));
2301 BUFFER_ADD (&pea.username_length, sizeof (pea.username_length));
2302 BUFFER_ADD (pea.username, username_len);
2303 BUFFER_ADD (pea.iv, sizeof (pea.iv));
2304 assert (buffer_offset == header_size);
2305 BUFFER_ADD (pea.hash, sizeof (pea.hash));
2306 BUFFER_ADD (in_buffer, in_buffer_size);
2308 assert (buffer_offset == buffer_size);
2310 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
2311 se->data.client.password);
2315 /* Encrypt the buffer in-place */
2316 err = gcry_cipher_encrypt (cypher,
2317 buffer + header_size,
2318 buffer_size - header_size,
2319 /* in = */ NULL, /* in len = */ 0);
2322 ERROR ("network plugin: gcry_cipher_encrypt returned: %s",
2323 gcry_strerror (err));
2327 /* Send it out without further modifications */
2328 networt_send_buffer_plain (se, buffer, buffer_size);
2329 } /* }}} void networt_send_buffer_encrypted */
2331 #endif /* HAVE_LIBGCRYPT */
2333 static void network_send_buffer (char *buffer, size_t buffer_len) /* {{{ */
2337 DEBUG ("network plugin: network_send_buffer: buffer_len = %zu", buffer_len);
2339 for (se = sending_sockets; se != NULL; se = se->next)
2342 if (se->data.client.security_level == SECURITY_LEVEL_ENCRYPT)
2343 networt_send_buffer_encrypted (se, buffer, buffer_len);
2344 else if (se->data.client.security_level == SECURITY_LEVEL_SIGN)
2345 networt_send_buffer_signed (se, buffer, buffer_len);
2346 else /* if (se->data.client.security_level == SECURITY_LEVEL_NONE) */
2347 #endif /* HAVE_LIBGCRYPT */
2348 networt_send_buffer_plain (se, buffer, buffer_len);
2349 } /* for (sending_sockets) */
2350 } /* }}} void network_send_buffer */
2352 static int add_to_buffer (char *buffer, int buffer_size, /* {{{ */
2353 value_list_t *vl_def,
2354 const data_set_t *ds, const value_list_t *vl)
2356 char *buffer_orig = buffer;
2358 if (strcmp (vl_def->host, vl->host) != 0)
2360 if (write_part_string (&buffer, &buffer_size, TYPE_HOST,
2361 vl->host, strlen (vl->host)) != 0)
2363 sstrncpy (vl_def->host, vl->host, sizeof (vl_def->host));
2366 if (vl_def->time != vl->time)
2368 if (write_part_number (&buffer, &buffer_size, TYPE_TIME,
2369 (uint64_t) vl->time))
2371 vl_def->time = vl->time;
2374 if (vl_def->interval != vl->interval)
2376 if (write_part_number (&buffer, &buffer_size, TYPE_INTERVAL,
2377 (uint64_t) vl->interval))
2379 vl_def->interval = vl->interval;
2382 if (strcmp (vl_def->plugin, vl->plugin) != 0)
2384 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN,
2385 vl->plugin, strlen (vl->plugin)) != 0)
2387 sstrncpy (vl_def->plugin, vl->plugin, sizeof (vl_def->plugin));
2390 if (strcmp (vl_def->plugin_instance, vl->plugin_instance) != 0)
2392 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
2393 vl->plugin_instance,
2394 strlen (vl->plugin_instance)) != 0)
2396 sstrncpy (vl_def->plugin_instance, vl->plugin_instance, sizeof (vl_def->plugin_instance));
2399 if (strcmp (vl_def->type, vl->type) != 0)
2401 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
2402 vl->type, strlen (vl->type)) != 0)
2404 sstrncpy (vl_def->type, ds->type, sizeof (vl_def->type));
2407 if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
2409 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE_INSTANCE,
2411 strlen (vl->type_instance)) != 0)
2413 sstrncpy (vl_def->type_instance, vl->type_instance, sizeof (vl_def->type_instance));
2416 if (write_part_values (&buffer, &buffer_size, ds, vl) != 0)
2419 return (buffer - buffer_orig);
2420 } /* }}} int add_to_buffer */
2422 static void flush_buffer (void)
2424 DEBUG ("network plugin: flush_buffer: send_buffer_fill = %i",
2427 network_send_buffer (send_buffer, (size_t) send_buffer_fill);
2428 network_init_buffer ();
2431 static int network_write (const data_set_t *ds, const value_list_t *vl,
2432 user_data_t __attribute__((unused)) *user_data)
2436 /* If the value is already in the cache, we have received it via the
2437 * network. We write it again if forwarding is activated. It's then in
2438 * the cache and should we receive it again we will ignore it. */
2439 status = cache_check (vl);
2440 if ((network_config_forward == 0)
2444 pthread_mutex_lock (&send_buffer_lock);
2446 status = add_to_buffer (send_buffer_ptr,
2447 sizeof (send_buffer) - (send_buffer_fill + BUFF_SIG_SIZE),
2452 /* status == bytes added to the buffer */
2453 send_buffer_fill += status;
2454 send_buffer_ptr += status;
2460 status = add_to_buffer (send_buffer_ptr,
2461 sizeof (send_buffer) - (send_buffer_fill + BUFF_SIG_SIZE),
2467 send_buffer_fill += status;
2468 send_buffer_ptr += status;
2474 ERROR ("network plugin: Unable to append to the "
2475 "buffer for some weird reason");
2477 else if ((sizeof (send_buffer) - send_buffer_fill) < 15)
2482 pthread_mutex_unlock (&send_buffer_lock);
2484 return ((status < 0) ? -1 : 0);
2485 } /* int network_write */
2487 static int network_config_set_boolean (const oconfig_item_t *ci, /* {{{ */
2490 if ((ci->values_num != 1)
2491 || ((ci->values[0].type != OCONFIG_TYPE_BOOLEAN)
2492 && (ci->values[0].type != OCONFIG_TYPE_STRING)))
2494 ERROR ("network plugin: The `%s' config option needs "
2495 "exactly one boolean argument.", ci->key);
2499 if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
2501 if (ci->values[0].value.boolean)
2508 char *str = ci->values[0].value.string;
2510 if ((strcasecmp ("true", str) == 0)
2511 || (strcasecmp ("yes", str) == 0)
2512 || (strcasecmp ("on", str) == 0))
2514 else if ((strcasecmp ("false", str) == 0)
2515 || (strcasecmp ("no", str) == 0)
2516 || (strcasecmp ("off", str) == 0))
2520 ERROR ("network plugin: Cannot parse string value `%s' of the `%s' "
2521 "option as boolean value.",
2528 } /* }}} int network_config_set_boolean */
2530 static int network_config_set_ttl (const oconfig_item_t *ci) /* {{{ */
2533 if ((ci->values_num != 1)
2534 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
2536 WARNING ("network plugin: The `TimeToLive' config option needs exactly "
2537 "one numeric argument.");
2541 tmp = (int) ci->values[0].value.number;
2542 if ((tmp > 0) && (tmp <= 255))
2543 network_config_ttl = tmp;
2546 } /* }}} int network_config_set_ttl */
2549 static int network_config_set_string (const oconfig_item_t *ci, /* {{{ */
2553 if ((ci->values_num != 1)
2554 || (ci->values[0].type != OCONFIG_TYPE_STRING))
2556 WARNING ("network plugin: The `%s' config option needs exactly "
2557 "one string argument.", ci->key);
2561 tmp = strdup (ci->values[0].value.string);
2565 sfree (*ret_string);
2569 } /* }}} int network_config_set_string */
2570 #endif /* HAVE_LIBGCRYPT */
2573 static int network_config_set_security_level (oconfig_item_t *ci, /* {{{ */
2577 if ((ci->values_num != 1)
2578 || (ci->values[0].type != OCONFIG_TYPE_STRING))
2580 WARNING ("network plugin: The `SecurityLevel' config option needs exactly "
2581 "one string argument.");
2585 str = ci->values[0].value.string;
2586 if (strcasecmp ("Encrypt", str) == 0)
2587 *retval = SECURITY_LEVEL_ENCRYPT;
2588 else if (strcasecmp ("Sign", str) == 0)
2589 *retval = SECURITY_LEVEL_SIGN;
2590 else if (strcasecmp ("None", str) == 0)
2591 *retval = SECURITY_LEVEL_NONE;
2594 WARNING ("network plugin: Unknown security level: %s.", str);
2599 } /* }}} int network_config_set_security_level */
2600 #endif /* HAVE_LIBGCRYPT */
2602 static int network_config_add_listen (const oconfig_item_t *ci) /* {{{ */
2608 if ((ci->values_num < 1) || (ci->values_num > 2)
2609 || (ci->values[0].type != OCONFIG_TYPE_STRING)
2610 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
2612 ERROR ("network plugin: The `%s' config option needs "
2613 "one or two string arguments.", ci->key);
2617 se = malloc (sizeof (*se));
2620 ERROR ("network plugin: malloc failed.");
2623 sockent_init (se, SOCKENT_TYPE_SERVER);
2625 se->node = strdup (ci->values[0].value.string);
2626 if (ci->values_num >= 2)
2627 se->service = strdup (ci->values[1].value.string);
2629 for (i = 0; i < ci->children_num; i++)
2631 oconfig_item_t *child = ci->children + i;
2634 if (strcasecmp ("AuthFile", child->key) == 0)
2635 network_config_set_string (child, &se->data.server.auth_file);
2636 else if (strcasecmp ("SecurityLevel", child->key) == 0)
2637 network_config_set_security_level (child,
2638 &se->data.server.security_level);
2640 #endif /* HAVE_LIBGCRYPT */
2642 WARNING ("network plugin: Option `%s' is not allowed here.",
2648 if ((se->data.server.security_level > SECURITY_LEVEL_NONE)
2649 && (se->data.server.auth_file == NULL))
2651 ERROR ("network plugin: A security level higher than `none' was "
2652 "requested, but no AuthFile option was given. Cowardly refusing to "
2653 "open this socket!");
2654 sockent_destroy (se);
2657 #endif /* HAVE_LIBGCRYPT */
2659 status = sockent_open (se);
2662 ERROR ("network plugin: network_config_add_listen: sockent_open failed.");
2663 sockent_destroy (se);
2667 status = sockent_add (se);
2670 ERROR ("network plugin: network_config_add_listen: sockent_add failed.");
2671 sockent_destroy (se);
2676 } /* }}} int network_config_add_listen */
2678 static int network_config_add_server (const oconfig_item_t *ci) /* {{{ */
2684 if ((ci->values_num < 1) || (ci->values_num > 2)
2685 || (ci->values[0].type != OCONFIG_TYPE_STRING)
2686 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
2688 ERROR ("network plugin: The `%s' config option needs "
2689 "one or two string arguments.", ci->key);
2693 se = malloc (sizeof (*se));
2696 ERROR ("network plugin: malloc failed.");
2699 sockent_init (se, SOCKENT_TYPE_CLIENT);
2701 se->node = strdup (ci->values[0].value.string);
2702 if (ci->values_num >= 2)
2703 se->service = strdup (ci->values[1].value.string);
2705 for (i = 0; i < ci->children_num; i++)
2707 oconfig_item_t *child = ci->children + i;
2710 if (strcasecmp ("Username", child->key) == 0)
2711 network_config_set_string (child, &se->data.client.username);
2712 else if (strcasecmp ("Password", child->key) == 0)
2713 network_config_set_string (child, &se->data.client.password);
2714 else if (strcasecmp ("SecurityLevel", child->key) == 0)
2715 network_config_set_security_level (child,
2716 &se->data.client.security_level);
2718 #endif /* HAVE_LIBGCRYPT */
2720 WARNING ("network plugin: Option `%s' is not allowed here.",
2726 if ((se->data.client.security_level > SECURITY_LEVEL_NONE)
2727 && ((se->data.client.username == NULL)
2728 || (se->data.client.password == NULL)))
2730 ERROR ("network plugin: A security level higher than `none' was "
2731 "requested, but no Username or Password option was given. "
2732 "Cowardly refusing to open this socket!");
2733 sockent_destroy (se);
2736 #endif /* HAVE_LIBGCRYPT */
2738 status = sockent_open (se);
2741 ERROR ("network plugin: network_config_add_server: sockent_open failed.");
2742 sockent_destroy (se);
2746 status = sockent_add (se);
2749 ERROR ("network plugin: network_config_add_server: sockent_add failed.");
2750 sockent_destroy (se);
2755 } /* }}} int network_config_add_server */
2757 static int network_config_set_cache_flush (const oconfig_item_t *ci) /* {{{ */
2760 if ((ci->values_num != 1)
2761 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
2763 WARNING ("network plugin: The `CacheFlush' config option needs exactly "
2764 "one numeric argument.");
2768 tmp = (int) ci->values[0].value.number;
2770 network_config_ttl = tmp;
2773 } /* }}} int network_config_set_cache_flush */
2775 static int network_config (oconfig_item_t *ci) /* {{{ */
2779 for (i = 0; i < ci->children_num; i++)
2781 oconfig_item_t *child = ci->children + i;
2783 if (strcasecmp ("Listen", child->key) == 0)
2784 network_config_add_listen (child);
2785 else if (strcasecmp ("Server", child->key) == 0)
2786 network_config_add_server (child);
2787 else if (strcasecmp ("TimeToLive", child->key) == 0)
2788 network_config_set_ttl (child);
2789 else if (strcasecmp ("Forward", child->key) == 0)
2790 network_config_set_boolean (child, &network_config_forward);
2791 else if (strcasecmp ("CacheFlush", child->key) == 0)
2792 network_config_set_cache_flush (child);
2795 WARNING ("network plugin: Option `%s' is not allowed here.",
2801 } /* }}} int network_config */
2803 static int network_notification (const notification_t *n,
2804 user_data_t __attribute__((unused)) *user_data)
2806 char buffer[BUFF_SIZE];
2807 char *buffer_ptr = buffer;
2808 int buffer_free = sizeof (buffer);
2811 memset (buffer, '\0', sizeof (buffer));
2814 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_TIME,
2815 (uint64_t) n->time);
2819 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_SEVERITY,
2820 (uint64_t) n->severity);
2824 if (strlen (n->host) > 0)
2826 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_HOST,
2827 n->host, strlen (n->host));
2832 if (strlen (n->plugin) > 0)
2834 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_PLUGIN,
2835 n->plugin, strlen (n->plugin));
2840 if (strlen (n->plugin_instance) > 0)
2842 status = write_part_string (&buffer_ptr, &buffer_free,
2843 TYPE_PLUGIN_INSTANCE,
2844 n->plugin_instance, strlen (n->plugin_instance));
2849 if (strlen (n->type) > 0)
2851 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE,
2852 n->type, strlen (n->type));
2857 if (strlen (n->type_instance) > 0)
2859 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE_INSTANCE,
2860 n->type_instance, strlen (n->type_instance));
2865 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_MESSAGE,
2866 n->message, strlen (n->message));
2870 network_send_buffer (buffer, sizeof (buffer) - buffer_free);
2873 } /* int network_notification */
2875 static int network_shutdown (void)
2879 /* Kill the listening thread */
2880 if (receive_thread_running != 0)
2882 INFO ("network plugin: Stopping receive thread.");
2883 pthread_kill (receive_thread_id, SIGTERM);
2884 pthread_join (receive_thread_id, NULL /* no return value */);
2885 memset (&receive_thread_id, 0, sizeof (receive_thread_id));
2886 receive_thread_running = 0;
2889 /* Shutdown the dispatching thread */
2890 if (dispatch_thread_running != 0)
2892 INFO ("network plugin: Stopping dispatch thread.");
2893 pthread_mutex_lock (&receive_list_lock);
2894 pthread_cond_broadcast (&receive_list_cond);
2895 pthread_mutex_unlock (&receive_list_lock);
2896 pthread_join (dispatch_thread_id, /* ret = */ NULL);
2897 dispatch_thread_running = 0;
2900 sockent_destroy (listen_sockets);
2902 if (send_buffer_fill > 0)
2905 if (cache_tree != NULL)
2910 while (c_avl_pick (cache_tree, &key, &value) == 0)
2915 c_avl_destroy (cache_tree);
2919 /* TODO: Close `sending_sockets' */
2921 plugin_unregister_config ("network");
2922 plugin_unregister_init ("network");
2923 plugin_unregister_write ("network");
2924 plugin_unregister_shutdown ("network");
2926 /* Let the init function do it's move again ;) */
2927 cache_flush_last = 0;
2930 } /* int network_shutdown */
2932 static int network_init (void)
2934 /* Check if we were already initialized. If so, just return - there's
2935 * nothing more to do (for now, that is). */
2936 if (cache_flush_last != 0)
2939 plugin_register_shutdown ("network", network_shutdown);
2941 network_init_buffer ();
2943 cache_tree = c_avl_create ((int (*) (const void *, const void *)) strcmp);
2944 cache_flush_last = time (NULL);
2946 /* setup socket(s) and so on */
2947 if (sending_sockets != NULL)
2949 plugin_register_write ("network", network_write,
2950 /* user_data = */ NULL);
2951 plugin_register_notification ("network", network_notification,
2952 /* user_data = */ NULL);
2955 /* If no threads need to be started, return here. */
2956 if ((listen_sockets_num == 0)
2957 || ((dispatch_thread_running != 0)
2958 && (receive_thread_running != 0)))
2961 if (dispatch_thread_running == 0)
2964 status = pthread_create (&dispatch_thread_id,
2965 NULL /* no attributes */,
2967 NULL /* no argument */);
2971 ERROR ("network: pthread_create failed: %s",
2972 sstrerror (errno, errbuf,
2977 dispatch_thread_running = 1;
2981 if (receive_thread_running == 0)
2984 status = pthread_create (&receive_thread_id,
2985 NULL /* no attributes */,
2987 NULL /* no argument */);
2991 ERROR ("network: pthread_create failed: %s",
2992 sstrerror (errno, errbuf,
2997 receive_thread_running = 1;
3002 } /* int network_init */
3005 * The flush option of the network plugin cannot flush individual identifiers.
3006 * All the values are added to a buffer and sent when the buffer is full, the
3007 * requested value may or may not be in there, it's not worth finding out. We
3008 * just send the buffer if `flush' is called - if the requested value was in
3009 * there, good. If not, well, then there is nothing to flush.. -octo
3011 static int network_flush (int timeout,
3012 const char __attribute__((unused)) *identifier,
3013 user_data_t __attribute__((unused)) *user_data)
3015 pthread_mutex_lock (&send_buffer_lock);
3017 if (((time (NULL) - cache_flush_last) >= timeout)
3018 && (send_buffer_fill > 0))
3023 pthread_mutex_unlock (&send_buffer_lock);
3026 } /* int network_flush */
3028 void module_register (void)
3030 plugin_register_complex_config ("network", network_config);
3031 plugin_register_init ("network", network_init);
3032 plugin_register_flush ("network", network_flush,
3033 /* user_data = */ NULL);
3034 } /* void module_register */
3036 /* vim: set fdm=marker : */