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>
22 #define _BSD_SOURCE /* For struct ip_mreq */
27 #include "configfile.h"
28 #include "utils_fbhash.h"
29 #include "utils_avltree.h"
37 # include <sys/socket.h>
43 # include <netinet/in.h>
46 # include <arpa/inet.h>
56 /* 1500 - 40 - 8 = Ethernet packet - IPv6 header - UDP header */
57 /* #define BUFF_SIZE 1452 */
59 #ifndef IPV6_ADD_MEMBERSHIP
60 # ifdef IPV6_JOIN_GROUP
61 # define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
63 # error "Neither IP_ADD_MEMBERSHIP nor IPV6_JOIN_GROUP is defined"
65 #endif /* !IP_ADD_MEMBERSHIP */
67 /* Buffer size to allocate. */
68 #define BUFF_SIZE 1024
71 * Maximum size required for encryption / signing:
73 * 42 bytes for the encryption header
74 * + 64 bytes for the username
78 #define BUFF_SIG_SIZE 106
83 #define SECURITY_LEVEL_NONE 0
85 # define SECURITY_LEVEL_SIGN 1
86 # define SECURITY_LEVEL_ENCRYPT 2
91 struct sockaddr_storage *addr;
97 gcry_cipher_hd_t cypher;
98 unsigned char password_hash[32];
102 struct sockent_server
110 gcry_cipher_hd_t cypher;
114 typedef struct sockent
116 #define SOCKENT_TYPE_CLIENT 1
117 #define SOCKENT_TYPE_SERVER 2
125 struct sockent_client client;
126 struct sockent_server server;
129 struct sockent *next;
132 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
133 * 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
134 * +-------+-----------------------+-------------------------------+
135 * ! Ver. ! ! Length !
136 * +-------+-----------------------+-------------------------------+
143 typedef struct part_header_s part_header_t;
145 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
146 * 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
147 * +-------------------------------+-------------------------------+
149 * +-------------------------------+-------------------------------+
150 * : (Length - 4) Bytes :
151 * +---------------------------------------------------------------+
158 typedef struct part_string_s part_string_t;
160 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
161 * 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
162 * +-------------------------------+-------------------------------+
164 * +-------------------------------+-------------------------------+
165 * : (Length - 4 == 2 || 4 || 8) Bytes :
166 * +---------------------------------------------------------------+
173 typedef struct part_number_s part_number_t;
175 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
176 * 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
177 * +-------------------------------+-------------------------------+
179 * +-------------------------------+---------------+---------------+
180 * ! Num of values ! Type0 ! Type1 !
181 * +-------------------------------+---------------+---------------+
184 * +---------------------------------------------------------------+
187 * +---------------------------------------------------------------+
192 uint16_t *num_values;
193 uint8_t *values_types;
196 typedef struct part_values_s part_values_t;
198 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
199 * 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
200 * +-------------------------------+-------------------------------+
202 * +-------------------------------+-------------------------------+
203 * ! Hash (Bits 0 - 31) !
205 * ! Hash (Bits 224 - 255) !
206 * +---------------------------------------------------------------+
209 #define PART_SIGNATURE_SHA256_SIZE 36
210 struct part_signature_sha256_s
213 unsigned char hash[32];
216 typedef struct part_signature_sha256_s part_signature_sha256_t;
218 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
219 * 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
220 * +-------------------------------+-------------------------------+
222 * +-------------------------------+-------------------------------+
223 * ! Original length ! Padding (0 - 15 bytes) !
224 * +-------------------------------+-------------------------------+
225 * ! Hash (Bits 0 - 31) !
227 * ! Hash (Bits 128 - 159) !
228 * +---------------------------------------------------------------+
231 #define PART_ENCRYPTION_AES256_SIZE 42
232 struct part_encryption_aes256_s
235 uint16_t username_length;
237 unsigned char iv[16];
239 unsigned char hash[20];
243 typedef struct part_encryption_aes256_s part_encryption_aes256_t;
245 struct receive_list_entry_s
247 char data[BUFF_SIZE];
250 struct receive_list_entry_s *next;
252 typedef struct receive_list_entry_s receive_list_entry_t;
257 static int network_config_ttl = 0;
258 static int network_config_forward = 0;
260 static sockent_t *sending_sockets = NULL;
262 static receive_list_entry_t *receive_list_head = NULL;
263 static receive_list_entry_t *receive_list_tail = NULL;
264 static pthread_mutex_t receive_list_lock = PTHREAD_MUTEX_INITIALIZER;
265 static pthread_cond_t receive_list_cond = PTHREAD_COND_INITIALIZER;
267 static sockent_t *listen_sockets = NULL;
268 static struct pollfd *listen_sockets_pollfd = NULL;
269 static size_t listen_sockets_num = 0;
271 /* The receive and dispatch threads will run as long as `listen_loop' is set to
273 static int listen_loop = 0;
274 static int receive_thread_running = 0;
275 static pthread_t receive_thread_id;
276 static int dispatch_thread_running = 0;
277 static pthread_t dispatch_thread_id;
279 /* Buffer in which to-be-sent network packets are constructed. */
280 static char send_buffer[BUFF_SIZE];
281 static char *send_buffer_ptr;
282 static int send_buffer_fill;
283 static value_list_t send_buffer_vl = VALUE_LIST_STATIC;
284 static pthread_mutex_t send_buffer_lock = PTHREAD_MUTEX_INITIALIZER;
286 /* In this cache we store all the values we received, so we can send out only
287 * those values which were *not* received via the network plugin, too. This is
288 * used for the `Forward false' option. */
289 static c_avl_tree_t *cache_tree = NULL;
290 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
291 static time_t cache_flush_last = 0;
292 static int cache_flush_interval = 1800;
297 static int cache_flush (void)
307 c_avl_iterator_t *iter;
309 time_t curtime = time (NULL);
311 iter = c_avl_get_iterator (cache_tree);
312 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
314 if ((curtime - *value) <= cache_flush_interval)
316 tmp = (char **) realloc (keys,
317 (keys_num + 1) * sizeof (char *));
321 c_avl_iterator_destroy (iter);
322 ERROR ("network plugin: cache_flush: realloc"
327 keys[keys_num] = key;
329 } /* while (c_avl_iterator_next) */
330 c_avl_iterator_destroy (iter);
332 for (i = 0; i < keys_num; i++)
334 if (c_avl_remove (cache_tree, keys[i], (void *) &key,
335 (void *) &value) != 0)
337 WARNING ("network plugin: cache_flush: c_avl_remove"
338 " (%s) failed.", keys[i]);
348 DEBUG ("network plugin: cache_flush: Removed %i %s",
349 keys_num, (keys_num == 1) ? "entry" : "entries");
350 cache_flush_last = curtime;
352 } /* int cache_flush */
354 static int cache_check (const value_list_t *vl)
357 time_t *value = NULL;
360 if (cache_tree == NULL)
363 if (format_name (key, sizeof (key), vl->host, vl->plugin,
364 vl->plugin_instance, vl->type, vl->type_instance))
367 pthread_mutex_lock (&cache_lock);
369 if (c_avl_get (cache_tree, key, (void *) &value) == 0)
371 if (*value < vl->time)
378 DEBUG ("network plugin: cache_check: *value = %i >= vl->time = %i",
379 (int) *value, (int) vl->time);
385 char *key_copy = strdup (key);
386 value = malloc (sizeof (time_t));
387 if ((key_copy != NULL) && (value != NULL))
390 c_avl_insert (cache_tree, key_copy, value);
400 if ((time (NULL) - cache_flush_last) > cache_flush_interval)
403 pthread_mutex_unlock (&cache_lock);
406 } /* int cache_check */
409 static gcry_cipher_hd_t network_get_aes256_cypher (sockent_t *se, /* {{{ */
410 const void *iv, size_t iv_size, const char *username)
413 gcry_cipher_hd_t *cyper_ptr;
414 unsigned char password_hash[32];
416 if (se->type == SOCKENT_TYPE_CLIENT)
418 cyper_ptr = &se->data.client.cypher;
419 memcpy (password_hash, se->data.client.password_hash,
420 sizeof (password_hash));
426 cyper_ptr = &se->data.server.cypher;
428 if (username == NULL)
431 secret = fbh_get (se->data.server.userdb, username);
435 gcry_md_hash_buffer (GCRY_MD_SHA256,
437 secret, strlen (secret));
442 if (*cyper_ptr == NULL)
444 err = gcry_cipher_open (cyper_ptr,
445 GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_OFB, /* flags = */ 0);
448 ERROR ("network plugin: gcry_cipher_open returned: %s",
449 gcry_strerror (err));
456 gcry_cipher_reset (*cyper_ptr);
458 assert (*cyper_ptr != NULL);
460 err = gcry_cipher_setkey (*cyper_ptr,
461 password_hash, sizeof (password_hash));
464 ERROR ("network plugin: gcry_cipher_setkey returned: %s",
465 gcry_strerror (err));
466 gcry_cipher_close (*cyper_ptr);
471 err = gcry_cipher_setiv (*cyper_ptr, iv, iv_size);
474 ERROR ("network plugin: gcry_cipher_setkey returned: %s",
475 gcry_strerror (err));
476 gcry_cipher_close (*cyper_ptr);
482 } /* }}} int network_get_aes256_cypher */
483 #endif /* HAVE_LIBGCRYPT */
485 static int write_part_values (char **ret_buffer, int *ret_buffer_len,
486 const data_set_t *ds, const value_list_t *vl)
492 part_header_t pkg_ph;
493 uint16_t pkg_num_values;
494 uint8_t *pkg_values_types;
500 num_values = vl->values_len;
501 packet_len = sizeof (part_header_t) + sizeof (uint16_t)
502 + (num_values * sizeof (uint8_t))
503 + (num_values * sizeof (value_t));
505 if (*ret_buffer_len < packet_len)
508 pkg_values_types = (uint8_t *) malloc (num_values * sizeof (uint8_t));
509 if (pkg_values_types == NULL)
511 ERROR ("network plugin: write_part_values: malloc failed.");
515 pkg_values = (value_t *) malloc (num_values * sizeof (value_t));
516 if (pkg_values == NULL)
518 free (pkg_values_types);
519 ERROR ("network plugin: write_part_values: malloc failed.");
523 pkg_ph.type = htons (TYPE_VALUES);
524 pkg_ph.length = htons (packet_len);
526 pkg_num_values = htons ((uint16_t) vl->values_len);
528 for (i = 0; i < num_values; i++)
530 if (ds->ds[i].type == DS_TYPE_COUNTER)
532 pkg_values_types[i] = DS_TYPE_COUNTER;
533 pkg_values[i].counter = htonll (vl->values[i].counter);
537 pkg_values_types[i] = DS_TYPE_GAUGE;
538 pkg_values[i].gauge = htond (vl->values[i].gauge);
543 * Use `memcpy' to write everything to the buffer, because the pointer
544 * may be unaligned and some architectures, such as SPARC, can't handle
547 packet_ptr = *ret_buffer;
549 memcpy (packet_ptr + offset, &pkg_ph, sizeof (pkg_ph));
550 offset += sizeof (pkg_ph);
551 memcpy (packet_ptr + offset, &pkg_num_values, sizeof (pkg_num_values));
552 offset += sizeof (pkg_num_values);
553 memcpy (packet_ptr + offset, pkg_values_types, num_values * sizeof (uint8_t));
554 offset += num_values * sizeof (uint8_t);
555 memcpy (packet_ptr + offset, pkg_values, num_values * sizeof (value_t));
556 offset += num_values * sizeof (value_t);
558 assert (offset == packet_len);
560 *ret_buffer = packet_ptr + packet_len;
561 *ret_buffer_len -= packet_len;
563 free (pkg_values_types);
567 } /* int write_part_values */
569 static int write_part_number (char **ret_buffer, int *ret_buffer_len,
570 int type, uint64_t value)
575 part_header_t pkg_head;
580 packet_len = sizeof (pkg_head) + sizeof (pkg_value);
582 if (*ret_buffer_len < packet_len)
585 pkg_head.type = htons (type);
586 pkg_head.length = htons (packet_len);
587 pkg_value = htonll (value);
589 packet_ptr = *ret_buffer;
591 memcpy (packet_ptr + offset, &pkg_head, sizeof (pkg_head));
592 offset += sizeof (pkg_head);
593 memcpy (packet_ptr + offset, &pkg_value, sizeof (pkg_value));
594 offset += sizeof (pkg_value);
596 assert (offset == packet_len);
598 *ret_buffer = packet_ptr + packet_len;
599 *ret_buffer_len -= packet_len;
602 } /* int write_part_number */
604 static int write_part_string (char **ret_buffer, int *ret_buffer_len,
605 int type, const char *str, int str_len)
615 buffer_len = 2 * sizeof (uint16_t) + str_len + 1;
616 if (*ret_buffer_len < buffer_len)
619 pkg_type = htons (type);
620 pkg_length = htons (buffer_len);
622 buffer = *ret_buffer;
624 memcpy (buffer + offset, (void *) &pkg_type, sizeof (pkg_type));
625 offset += sizeof (pkg_type);
626 memcpy (buffer + offset, (void *) &pkg_length, sizeof (pkg_length));
627 offset += sizeof (pkg_length);
628 memcpy (buffer + offset, str, str_len);
630 memset (buffer + offset, '\0', 1);
633 assert (offset == buffer_len);
635 *ret_buffer = buffer + buffer_len;
636 *ret_buffer_len -= buffer_len;
639 } /* int write_part_string */
641 static int parse_part_values (void **ret_buffer, size_t *ret_buffer_len,
642 value_t **ret_values, int *ret_num_values)
644 char *buffer = *ret_buffer;
645 size_t buffer_len = *ret_buffer_len;
660 NOTICE ("network plugin: packet is too short: "
661 "buffer_len = %zu", buffer_len);
665 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
666 buffer += sizeof (tmp16);
667 pkg_type = ntohs (tmp16);
669 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
670 buffer += sizeof (tmp16);
671 pkg_length = ntohs (tmp16);
673 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
674 buffer += sizeof (tmp16);
675 pkg_numval = ntohs (tmp16);
677 assert (pkg_type == TYPE_VALUES);
679 exp_size = 3 * sizeof (uint16_t)
680 + pkg_numval * (sizeof (uint8_t) + sizeof (value_t));
681 if ((buffer_len < 0) || (buffer_len < exp_size))
683 WARNING ("network plugin: parse_part_values: "
685 "Chunk of size %zu expected, "
686 "but buffer has only %zu bytes left.",
687 exp_size, buffer_len);
691 if (pkg_length != exp_size)
693 WARNING ("network plugin: parse_part_values: "
694 "Length and number of values "
695 "in the packet don't match.");
699 pkg_types = (uint8_t *) malloc (pkg_numval * sizeof (uint8_t));
700 pkg_values = (value_t *) malloc (pkg_numval * sizeof (value_t));
701 if ((pkg_types == NULL) || (pkg_values == NULL))
705 ERROR ("network plugin: parse_part_values: malloc failed.");
709 memcpy ((void *) pkg_types, (void *) buffer, pkg_numval * sizeof (uint8_t));
710 buffer += pkg_numval * sizeof (uint8_t);
711 memcpy ((void *) pkg_values, (void *) buffer, pkg_numval * sizeof (value_t));
712 buffer += pkg_numval * sizeof (value_t);
714 for (i = 0; i < pkg_numval; i++)
716 if (pkg_types[i] == DS_TYPE_COUNTER)
717 pkg_values[i].counter = ntohll (pkg_values[i].counter);
718 else if (pkg_types[i] == DS_TYPE_GAUGE)
719 pkg_values[i].gauge = ntohd (pkg_values[i].gauge);
722 *ret_buffer = buffer;
723 *ret_buffer_len = buffer_len - pkg_length;
724 *ret_num_values = pkg_numval;
725 *ret_values = pkg_values;
730 } /* int parse_part_values */
732 static int parse_part_number (void **ret_buffer, size_t *ret_buffer_len,
735 char *buffer = *ret_buffer;
736 size_t buffer_len = *ret_buffer_len;
740 size_t exp_size = 2 * sizeof (uint16_t) + sizeof (uint64_t);
745 if ((buffer_len < 0) || ((size_t) buffer_len < exp_size))
747 WARNING ("network plugin: parse_part_number: "
749 "Chunk of size %zu expected, "
750 "but buffer has only %zu bytes left.",
751 exp_size, buffer_len);
755 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
756 buffer += sizeof (tmp16);
757 pkg_type = ntohs (tmp16);
759 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
760 buffer += sizeof (tmp16);
761 pkg_length = ntohs (tmp16);
763 memcpy ((void *) &tmp64, buffer, sizeof (tmp64));
764 buffer += sizeof (tmp64);
765 *value = ntohll (tmp64);
767 *ret_buffer = buffer;
768 *ret_buffer_len = buffer_len - pkg_length;
771 } /* int parse_part_number */
773 static int parse_part_string (void **ret_buffer, size_t *ret_buffer_len,
774 char *output, int output_len)
776 char *buffer = *ret_buffer;
777 size_t buffer_len = *ret_buffer_len;
780 size_t header_size = 2 * sizeof (uint16_t);
785 if ((buffer_len < 0) || (buffer_len < header_size))
787 WARNING ("network plugin: parse_part_string: "
789 "Chunk of at least size %zu expected, "
790 "but buffer has only %zu bytes left.",
791 header_size, buffer_len);
795 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
796 buffer += sizeof (tmp16);
797 pkg_type = ntohs (tmp16);
799 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
800 buffer += sizeof (tmp16);
801 pkg_length = ntohs (tmp16);
803 /* Check that packet fits in the input buffer */
804 if (pkg_length > buffer_len)
806 WARNING ("network plugin: parse_part_string: "
808 "Chunk of size %"PRIu16" received, "
809 "but buffer has only %zu bytes left.",
810 pkg_length, buffer_len);
814 /* Check that pkg_length is in the valid range */
815 if (pkg_length <= header_size)
817 WARNING ("network plugin: parse_part_string: "
819 "Header claims this packet is only %hu "
820 "bytes long.", pkg_length);
824 /* Check that the package data fits into the output buffer.
825 * The previous if-statement ensures that:
826 * `pkg_length > header_size' */
828 || ((size_t) output_len < ((size_t) pkg_length - header_size)))
830 WARNING ("network plugin: parse_part_string: "
831 "Output buffer too small.");
835 /* All sanity checks successfull, let's copy the data over */
836 output_len = pkg_length - header_size;
837 memcpy ((void *) output, (void *) buffer, output_len);
838 buffer += output_len;
840 /* For some very weird reason '\0' doesn't do the trick on SPARC in
842 if (output[output_len - 1] != 0)
844 WARNING ("network plugin: parse_part_string: "
845 "Received string does not end "
846 "with a NULL-byte.");
850 *ret_buffer = buffer;
851 *ret_buffer_len = buffer_len - pkg_length;
854 } /* int parse_part_string */
856 /* Forward declaration: parse_part_sign_sha256 and parse_part_encr_aes256 call
857 * parse_packet and vice versa. */
858 #define PP_SIGNED 0x01
859 #define PP_ENCRYPTED 0x02
860 static int parse_packet (sockent_t *se,
861 void *buffer, size_t buffer_size, int flags);
863 #define BUFFER_READ(p,s) do { \
864 memcpy ((p), buffer + buffer_offset, (s)); \
865 buffer_offset += (s); \
869 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
870 void **ret_buffer, size_t *ret_buffer_len, int flags)
874 size_t buffer_offset;
879 part_signature_sha256_t pss;
880 uint16_t pss_head_length;
881 char hash[sizeof (pss.hash)];
885 unsigned char *hash_ptr;
887 buffer = *ret_buffer;
888 buffer_len = *ret_buffer_len;
891 if (se->data.server.userdb == NULL)
893 NOTICE ("network plugin: Received signed network packet but can't verify "
894 "it because no user DB has been configured. Will accept it.");
898 /* Check if the buffer has enough data for this structure. */
899 if (buffer_len <= PART_SIGNATURE_SHA256_SIZE)
902 /* Read type and length header */
903 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
904 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
905 pss_head_length = ntohs (pss.head.length);
907 /* Check if the `pss_head_length' is within bounds. */
908 if ((pss_head_length <= PART_SIGNATURE_SHA256_SIZE)
909 || (pss_head_length > buffer_len))
911 ERROR ("network plugin: HMAC-SHA-256 with invalid length received.");
916 BUFFER_READ (pss.hash, sizeof (pss.hash));
918 /* Calculate username length (without null byte) and allocate memory */
919 username_len = pss_head_length - PART_SIGNATURE_SHA256_SIZE;
920 pss.username = malloc (username_len + 1);
921 if (pss.username == NULL)
924 /* Read the username */
925 BUFFER_READ (pss.username, username_len);
926 pss.username[username_len] = 0;
928 assert (buffer_offset == pss_head_length);
930 /* Query the password */
931 secret = fbh_get (se->data.server.userdb, pss.username);
934 ERROR ("network plugin: Unknown user: %s", pss.username);
935 sfree (pss.username);
939 /* Create a hash device and check the HMAC */
941 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
944 ERROR ("network plugin: Creating HMAC-SHA-256 object failed: %s",
945 gcry_strerror (err));
947 sfree (pss.username);
951 err = gcry_md_setkey (hd, secret, strlen (secret));
954 ERROR ("network plugin: gcry_md_setkey failed: %s", gcry_strerror (err));
960 buffer + PART_SIGNATURE_SHA256_SIZE,
961 buffer_len - PART_SIGNATURE_SHA256_SIZE);
962 hash_ptr = gcry_md_read (hd, GCRY_MD_SHA256);
963 if (hash_ptr == NULL)
965 ERROR ("network plugin: gcry_md_read failed.");
968 sfree (pss.username);
971 memcpy (hash, hash_ptr, sizeof (hash));
978 sfree (pss.username);
980 if (memcmp (pss.hash, hash, sizeof (pss.hash)) != 0)
982 WARNING ("network plugin: Verifying HMAC-SHA-256 signature failed: "
987 parse_packet (se, buffer + buffer_offset, buffer_len - buffer_offset,
991 *ret_buffer = buffer + buffer_len;
995 } /* }}} int parse_part_sign_sha256 */
996 /* #endif HAVE_LIBGCRYPT */
998 #else /* if !HAVE_LIBGCRYPT */
999 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
1000 void **ret_buffer, size_t *ret_buffer_size, int flags)
1002 static int warning_has_been_printed = 0;
1006 size_t buffer_offset;
1009 part_signature_sha256_t pss;
1011 buffer = *ret_buffer;
1012 buffer_size = *ret_buffer_size;
1015 if (buffer_size <= PART_SIGNATURE_SHA256_SIZE)
1018 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
1019 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
1020 part_len = ntohs (pss.head.length);
1022 if ((part_len <= PART_SIGNATURE_SHA256_SIZE)
1023 || (part_len > buffer_size))
1026 if (warning_has_been_printed == 0)
1028 WARNING ("network plugin: Received signed packet, but the network "
1029 "plugin was not linked with libgcrypt, so I cannot "
1030 "verify the signature. The packet will be accepted.");
1031 warning_has_been_printed = 1;
1034 parse_packet (se, buffer + part_len, buffer_size - part_len, flags);
1036 *ret_buffer = buffer + buffer_size;
1037 *ret_buffer_size = 0;
1040 } /* }}} int parse_part_sign_sha256 */
1041 #endif /* !HAVE_LIBGCRYPT */
1044 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1045 void **ret_buffer, size_t *ret_buffer_len,
1048 char *buffer = *ret_buffer;
1049 size_t buffer_len = *ret_buffer_len;
1052 size_t buffer_offset;
1053 uint16_t username_len;
1054 part_encryption_aes256_t pea;
1055 unsigned char hash[sizeof (pea.hash)];
1057 gcry_cipher_hd_t cypher;
1060 /* Make sure at least the header if available. */
1061 if (buffer_len <= PART_ENCRYPTION_AES256_SIZE)
1063 NOTICE ("network plugin: parse_part_encr_aes256: "
1064 "Discarding short packet.");
1070 /* Copy the unencrypted information into `pea'. */
1071 BUFFER_READ (&pea.head.type, sizeof (pea.head.type));
1072 BUFFER_READ (&pea.head.length, sizeof (pea.head.length));
1074 /* Check the `part size'. */
1075 part_size = ntohs (pea.head.length);
1076 if ((part_size <= PART_ENCRYPTION_AES256_SIZE)
1077 || (part_size > buffer_len))
1079 NOTICE ("network plugin: parse_part_encr_aes256: "
1080 "Discarding part with invalid size.");
1084 /* Read the username */
1085 BUFFER_READ (&username_len, sizeof (username_len));
1086 username_len = ntohs (username_len);
1088 if ((username_len <= 0)
1089 || (username_len > (part_size - (PART_ENCRYPTION_AES256_SIZE + 1))))
1091 NOTICE ("network plugin: parse_part_encr_aes256: "
1092 "Discarding part with invalid username length.");
1096 assert (username_len > 0);
1097 pea.username = malloc (username_len + 1);
1098 if (pea.username == NULL)
1100 BUFFER_READ (pea.username, username_len);
1101 pea.username[username_len] = 0;
1103 /* Last but not least, the initialization vector */
1104 BUFFER_READ (pea.iv, sizeof (pea.iv));
1106 /* Make sure we are at the right position */
1107 assert (buffer_offset == (username_len +
1108 PART_ENCRYPTION_AES256_SIZE - sizeof (pea.hash)));
1110 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
1115 payload_len = part_size - (PART_ENCRYPTION_AES256_SIZE + username_len);
1116 assert (payload_len > 0);
1118 /* Decrypt the packet in-place */
1119 err = gcry_cipher_decrypt (cypher,
1120 buffer + buffer_offset,
1121 part_size - buffer_offset,
1122 /* in = */ NULL, /* in len = */ 0);
1125 ERROR ("network plugin: gcry_cipher_decrypt returned: %s",
1126 gcry_strerror (err));
1131 BUFFER_READ (pea.hash, sizeof (pea.hash));
1133 /* Make sure we're at the right position - again */
1134 assert (buffer_offset == (username_len + PART_ENCRYPTION_AES256_SIZE));
1135 assert (buffer_offset == (part_size - payload_len));
1137 /* Check hash sum */
1138 memset (hash, 0, sizeof (hash));
1139 gcry_md_hash_buffer (GCRY_MD_SHA1, hash,
1140 buffer + buffer_offset, payload_len);
1141 if (memcmp (hash, pea.hash, sizeof (hash)) != 0)
1143 ERROR ("network plugin: Decryption failed: Checksum mismatch.");
1147 parse_packet (se, buffer + buffer_offset, payload_len,
1148 flags | PP_ENCRYPTED);
1150 /* Update return values */
1151 *ret_buffer = buffer + part_size;
1152 *ret_buffer_len = buffer_len - part_size;
1155 } /* }}} int parse_part_encr_aes256 */
1156 /* #endif HAVE_LIBGCRYPT */
1158 #else /* if !HAVE_LIBGCRYPT */
1159 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1160 void **ret_buffer, size_t *ret_buffer_size, int flags)
1162 static int warning_has_been_printed = 0;
1166 size_t buffer_offset;
1171 buffer = *ret_buffer;
1172 buffer_size = *ret_buffer_size;
1175 /* parse_packet assures this minimum size. */
1176 assert (buffer_size >= (sizeof (ph.type) + sizeof (ph.length)));
1178 BUFFER_READ (&ph.type, sizeof (ph.type));
1179 BUFFER_READ (&ph.length, sizeof (ph.length));
1180 ph_length = ntohs (ph.length);
1182 if ((ph_length <= PART_ENCRYPTION_AES256_SIZE)
1183 || (ph_length > buffer_size))
1185 ERROR ("network plugin: AES-256 encrypted part "
1186 "with invalid length received.");
1190 if (warning_has_been_printed == 0)
1192 WARNING ("network plugin: Received encrypted packet, but the network "
1193 "plugin was not linked with libgcrypt, so I cannot "
1194 "decrypt it. The part will be discarded.");
1195 warning_has_been_printed = 1;
1198 *ret_buffer += ph_length;
1199 *ret_buffer_size -= ph_length;
1202 } /* }}} int parse_part_encr_aes256 */
1203 #endif /* !HAVE_LIBGCRYPT */
1207 static int parse_packet (sockent_t *se, /* {{{ */
1208 void *buffer, size_t buffer_size, int flags)
1212 value_list_t vl = VALUE_LIST_INIT;
1216 int packet_was_signed = (flags & PP_SIGNED);
1217 int packet_was_encrypted = (flags & PP_ENCRYPTED);
1218 int printed_ignore_warning = 0;
1219 #endif /* HAVE_LIBGCRYPT */
1222 memset (&vl, '\0', sizeof (vl));
1223 memset (&n, '\0', sizeof (n));
1226 while ((status == 0) && (0 < buffer_size)
1227 && ((unsigned int) buffer_size > sizeof (part_header_t)))
1229 uint16_t pkg_length;
1232 memcpy ((void *) &pkg_type,
1235 memcpy ((void *) &pkg_length,
1236 (void *) (buffer + sizeof (pkg_type)),
1237 sizeof (pkg_length));
1239 pkg_length = ntohs (pkg_length);
1240 pkg_type = ntohs (pkg_type);
1242 if (pkg_length > buffer_size)
1244 /* Ensure that this loop terminates eventually */
1245 if (pkg_length < (2 * sizeof (uint16_t)))
1248 if (pkg_type == TYPE_ENCR_AES256)
1250 status = parse_part_encr_aes256 (se,
1251 &buffer, &buffer_size, flags);
1254 ERROR ("network plugin: Decrypting AES256 "
1256 "with status %i.", status);
1261 else if ((se->data.server.security_level == SECURITY_LEVEL_ENCRYPT)
1262 && (packet_was_encrypted == 0))
1264 if (printed_ignore_warning == 0)
1266 INFO ("network plugin: Unencrypted packet or "
1267 "part has been ignored.");
1268 printed_ignore_warning = 1;
1270 buffer = ((char *) buffer) + pkg_length;
1273 #endif /* HAVE_LIBGCRYPT */
1274 else if (pkg_type == TYPE_SIGN_SHA256)
1276 status = parse_part_sign_sha256 (se,
1277 &buffer, &buffer_size, flags);
1280 ERROR ("network plugin: Verifying HMAC-SHA-256 "
1282 "with status %i.", status);
1287 else if ((se->data.server.security_level == SECURITY_LEVEL_SIGN)
1288 && (packet_was_encrypted == 0)
1289 && (packet_was_signed == 0))
1291 if (printed_ignore_warning == 0)
1293 INFO ("network plugin: Unsigned packet or "
1294 "part has been ignored.");
1295 printed_ignore_warning = 1;
1297 buffer = ((char *) buffer) + pkg_length;
1300 #endif /* HAVE_LIBGCRYPT */
1301 else if (pkg_type == TYPE_VALUES)
1303 status = parse_part_values (&buffer, &buffer_size,
1304 &vl.values, &vl.values_len);
1310 && (strlen (vl.host) > 0)
1311 && (strlen (vl.plugin) > 0)
1312 && (strlen (vl.type) > 0)
1313 && (cache_check (&vl) == 0))
1315 plugin_dispatch_values (&vl);
1319 DEBUG ("network plugin: parse_packet:"
1320 " NOT dispatching values");
1325 else if (pkg_type == TYPE_TIME)
1328 status = parse_part_number (&buffer, &buffer_size,
1332 vl.time = (time_t) tmp;
1333 n.time = (time_t) tmp;
1336 else if (pkg_type == TYPE_INTERVAL)
1339 status = parse_part_number (&buffer, &buffer_size,
1342 vl.interval = (int) tmp;
1344 else if (pkg_type == TYPE_HOST)
1346 status = parse_part_string (&buffer, &buffer_size,
1347 vl.host, sizeof (vl.host));
1349 sstrncpy (n.host, vl.host, sizeof (n.host));
1351 else if (pkg_type == TYPE_PLUGIN)
1353 status = parse_part_string (&buffer, &buffer_size,
1354 vl.plugin, sizeof (vl.plugin));
1356 sstrncpy (n.plugin, vl.plugin,
1359 else if (pkg_type == TYPE_PLUGIN_INSTANCE)
1361 status = parse_part_string (&buffer, &buffer_size,
1363 sizeof (vl.plugin_instance));
1365 sstrncpy (n.plugin_instance,
1367 sizeof (n.plugin_instance));
1369 else if (pkg_type == TYPE_TYPE)
1371 status = parse_part_string (&buffer, &buffer_size,
1372 vl.type, sizeof (vl.type));
1374 sstrncpy (n.type, vl.type, sizeof (n.type));
1376 else if (pkg_type == TYPE_TYPE_INSTANCE)
1378 status = parse_part_string (&buffer, &buffer_size,
1380 sizeof (vl.type_instance));
1382 sstrncpy (n.type_instance, vl.type_instance,
1383 sizeof (n.type_instance));
1385 else if (pkg_type == TYPE_MESSAGE)
1387 status = parse_part_string (&buffer, &buffer_size,
1388 n.message, sizeof (n.message));
1394 else if ((n.severity != NOTIF_FAILURE)
1395 && (n.severity != NOTIF_WARNING)
1396 && (n.severity != NOTIF_OKAY))
1398 INFO ("network plugin: "
1399 "Ignoring notification with "
1400 "unknown severity %i.",
1403 else if (n.time <= 0)
1405 INFO ("network plugin: "
1406 "Ignoring notification with "
1409 else if (strlen (n.message) <= 0)
1411 INFO ("network plugin: "
1412 "Ignoring notification with "
1413 "an empty message.");
1417 plugin_dispatch_notification (&n);
1420 else if (pkg_type == TYPE_SEVERITY)
1423 status = parse_part_number (&buffer, &buffer_size,
1426 n.severity = (int) tmp;
1430 DEBUG ("network plugin: parse_packet: Unknown part"
1431 " type: 0x%04hx", pkg_type);
1432 buffer = ((char *) buffer) + pkg_length;
1434 } /* while (buffer_size > sizeof (part_header_t)) */
1437 } /* }}} int parse_packet */
1439 static void free_sockent_client (struct sockent_client *sec) /* {{{ */
1448 sfree (sec->username);
1449 sfree (sec->password);
1450 if (sec->cypher != NULL)
1451 gcry_cipher_close (sec->cypher);
1453 } /* }}} void free_sockent_client */
1455 static void free_sockent_server (struct sockent_server *ses) /* {{{ */
1459 for (i = 0; i < ses->fd_num; i++)
1461 if (ses->fd[i] >= 0)
1470 sfree (ses->auth_file);
1471 fbh_destroy (ses->userdb);
1472 if (ses->cypher != NULL)
1473 gcry_cipher_close (ses->cypher);
1475 } /* }}} void free_sockent_server */
1477 static void sockent_destroy (sockent_t *se) /* {{{ */
1481 DEBUG ("network plugin: sockent_destroy (se = %p);", (void *) se);
1488 sfree (se->service);
1490 if (se->type == SOCKENT_TYPE_CLIENT)
1491 free_sockent_client (&se->data.client);
1493 free_sockent_server (&se->data.server);
1498 } /* }}} void sockent_destroy */
1501 * int network_set_ttl
1503 * Set the `IP_MULTICAST_TTL', `IP_TTL', `IPV6_MULTICAST_HOPS' or
1504 * `IPV6_UNICAST_HOPS', depending on which option is applicable.
1506 * The `struct addrinfo' is used to destinguish between unicast and multicast
1509 static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
1511 DEBUG ("network plugin: network_set_ttl: network_config_ttl = %i;",
1512 network_config_ttl);
1514 assert (se->type == SOCKENT_TYPE_CLIENT);
1516 if ((network_config_ttl < 1) || (network_config_ttl > 255))
1519 if (ai->ai_family == AF_INET)
1521 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1524 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1525 optname = IP_MULTICAST_TTL;
1529 if (setsockopt (se->data.client.fd, IPPROTO_IP, optname,
1530 &network_config_ttl,
1531 sizeof (network_config_ttl)) == -1)
1534 ERROR ("setsockopt: %s",
1535 sstrerror (errno, errbuf, sizeof (errbuf)));
1539 else if (ai->ai_family == AF_INET6)
1541 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1542 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1545 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1546 optname = IPV6_MULTICAST_HOPS;
1548 optname = IPV6_UNICAST_HOPS;
1550 if (setsockopt (se->data.client.fd, IPPROTO_IPV6, optname,
1551 &network_config_ttl,
1552 sizeof (network_config_ttl)) == -1)
1555 ERROR ("setsockopt: %s",
1556 sstrerror (errno, errbuf,
1563 } /* int network_set_ttl */
1565 static int network_bind_socket (int fd, const struct addrinfo *ai)
1570 /* allow multiple sockets to use the same PORT number */
1571 if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR,
1572 &yes, sizeof(yes)) == -1) {
1574 ERROR ("setsockopt: %s",
1575 sstrerror (errno, errbuf, sizeof (errbuf)));
1579 DEBUG ("fd = %i; calling `bind'", fd);
1581 if (bind (fd, ai->ai_addr, ai->ai_addrlen) == -1)
1585 sstrerror (errno, errbuf, sizeof (errbuf)));
1589 if (ai->ai_family == AF_INET)
1591 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1592 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1594 struct ip_mreq mreq;
1596 DEBUG ("fd = %i; IPv4 multicast address found", fd);
1598 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1599 mreq.imr_interface.s_addr = htonl (INADDR_ANY);
1601 if (setsockopt (fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1602 &loop, sizeof (loop)) == -1)
1605 ERROR ("setsockopt: %s",
1606 sstrerror (errno, errbuf,
1611 if (setsockopt (fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1612 &mreq, sizeof (mreq)) == -1)
1615 ERROR ("setsockopt: %s",
1616 sstrerror (errno, errbuf,
1622 else if (ai->ai_family == AF_INET6)
1624 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1625 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1626 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1628 struct ipv6_mreq mreq;
1630 DEBUG ("fd = %i; IPv6 multicast address found", fd);
1632 memcpy (&mreq.ipv6mr_multiaddr,
1634 sizeof (addr->sin6_addr));
1636 /* http://developer.apple.com/documentation/Darwin/Reference/ManPages/man4/ip6.4.html
1637 * ipv6mr_interface may be set to zeroes to
1638 * choose the default multicast interface or to
1639 * the index of a particular multicast-capable
1640 * interface if the host is multihomed.
1641 * Membership is associ-associated with a
1642 * single interface; programs running on
1643 * multihomed hosts may need to join the same
1644 * group on more than one interface.*/
1645 mreq.ipv6mr_interface = 0;
1647 if (setsockopt (fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
1648 &loop, sizeof (loop)) == -1)
1651 ERROR ("setsockopt: %s",
1652 sstrerror (errno, errbuf,
1657 if (setsockopt (fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
1658 &mreq, sizeof (mreq)) == -1)
1661 ERROR ("setsockopt: %s",
1662 sstrerror (errno, errbuf,
1670 } /* int network_bind_socket */
1672 /* Initialize a sockent structure. `type' must be either `SOCKENT_TYPE_CLIENT'
1673 * or `SOCKENT_TYPE_SERVER' */
1674 static int sockent_init (sockent_t *se, int type) /* {{{ */
1679 memset (se, 0, sizeof (*se));
1681 se->type = SOCKENT_TYPE_CLIENT;
1686 if (type == SOCKENT_TYPE_SERVER)
1688 se->type = SOCKENT_TYPE_SERVER;
1689 se->data.server.fd = NULL;
1691 se->data.server.security_level = SECURITY_LEVEL_NONE;
1692 se->data.server.auth_file = NULL;
1693 se->data.server.userdb = NULL;
1694 se->data.server.cypher = NULL;
1699 se->data.client.fd = -1;
1700 se->data.client.addr = NULL;
1702 se->data.client.security_level = SECURITY_LEVEL_NONE;
1703 se->data.client.username = NULL;
1704 se->data.client.password = NULL;
1705 se->data.client.cypher = NULL;
1710 } /* }}} int sockent_init */
1712 /* Open the file descriptors for a initialized sockent structure. */
1713 static int sockent_open (sockent_t *se) /* {{{ */
1715 struct addrinfo ai_hints;
1716 struct addrinfo *ai_list, *ai_ptr;
1720 const char *service;
1725 /* Set up the security structures. */
1726 #if HAVE_LIBGCRYPT /* {{{ */
1727 if (se->type == SOCKENT_TYPE_CLIENT)
1729 if (se->data.client.security_level > SECURITY_LEVEL_NONE)
1731 if ((se->data.client.username == NULL)
1732 || (se->data.client.password == NULL))
1734 ERROR ("network plugin: Client socket with "
1735 "security requested, but no "
1736 "credentials are configured.");
1739 gcry_md_hash_buffer (GCRY_MD_SHA256,
1740 se->data.client.password_hash,
1741 se->data.client.password,
1742 strlen (se->data.client.password));
1745 else /* (se->type == SOCKENT_TYPE_SERVER) */
1747 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
1749 if (se->data.server.auth_file == NULL)
1751 ERROR ("network plugin: Server socket with "
1752 "security requested, but no "
1753 "password file is configured.");
1757 if (se->data.server.auth_file != NULL)
1759 se->data.server.userdb = fbh_create (se->data.server.auth_file);
1760 if (se->data.server.userdb == NULL)
1762 ERROR ("network plugin: Reading password file "
1764 se->data.server.auth_file);
1765 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
1770 #endif /* }}} HAVE_LIBGCRYPT */
1773 service = se->service;
1775 if (service == NULL)
1776 service = NET_DEFAULT_PORT;
1778 DEBUG ("network plugin: sockent_open: node = %s; service = %s;",
1781 memset (&ai_hints, 0, sizeof (ai_hints));
1782 ai_hints.ai_flags = 0;
1784 ai_hints.ai_flags |= AI_PASSIVE;
1786 #ifdef AI_ADDRCONFIG
1787 ai_hints.ai_flags |= AI_ADDRCONFIG;
1789 ai_hints.ai_family = AF_UNSPEC;
1790 ai_hints.ai_socktype = SOCK_DGRAM;
1791 ai_hints.ai_protocol = IPPROTO_UDP;
1793 ai_return = getaddrinfo (node, service, &ai_hints, &ai_list);
1796 ERROR ("network plugin: getaddrinfo (%s, %s) failed: %s",
1797 (se->node == NULL) ? "(null)" : se->node,
1798 (se->service == NULL) ? "(null)" : se->service,
1799 gai_strerror (ai_return));
1803 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1807 if (se->type == SOCKENT_TYPE_SERVER) /* {{{ */
1811 tmp = realloc (se->data.server.fd,
1812 sizeof (*tmp) * (se->data.server.fd_num + 1));
1815 ERROR ("network plugin: realloc failed.");
1818 se->data.server.fd = tmp;
1819 tmp = se->data.server.fd + se->data.server.fd_num;
1821 *tmp = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
1822 ai_ptr->ai_protocol);
1826 ERROR ("network plugin: socket(2) failed: %s",
1827 sstrerror (errno, errbuf,
1832 status = network_bind_socket (*tmp, ai_ptr);
1840 se->data.server.fd_num++;
1842 } /* }}} if (se->type == SOCKENT_TYPE_SERVER) */
1843 else /* if (se->type == SOCKENT_TYPE_CLIENT) {{{ */
1845 se->data.client.fd = socket (ai_ptr->ai_family,
1846 ai_ptr->ai_socktype,
1847 ai_ptr->ai_protocol);
1848 if (se->data.client.fd < 0)
1851 ERROR ("network plugin: socket(2) failed: %s",
1852 sstrerror (errno, errbuf,
1857 se->data.client.addr = malloc (sizeof (*se->data.client.addr));
1858 if (se->data.client.addr == NULL)
1860 ERROR ("network plugin: malloc failed.");
1861 close (se->data.client.fd);
1862 se->data.client.fd = -1;
1866 memset (se->data.client.addr, 0, sizeof (*se->data.client.addr));
1867 assert (sizeof (*se->data.client.addr) >= ai_ptr->ai_addrlen);
1868 memcpy (se->data.client.addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1869 se->data.client.addrlen = ai_ptr->ai_addrlen;
1871 network_set_ttl (se, ai_ptr);
1873 /* We don't open more than one write-socket per
1874 * node/service pair.. */
1876 } /* }}} if (se->type == SOCKENT_TYPE_CLIENT) */
1877 } /* for (ai_list) */
1879 freeaddrinfo (ai_list);
1881 /* Check if all went well. */
1882 if (se->type == SOCKENT_TYPE_SERVER)
1884 if (se->data.server.fd_num <= 0)
1887 else /* if (se->type == SOCKENT_TYPE_CLIENT) */
1889 if (se->data.client.fd < 0)
1894 } /* }}} int sockent_open */
1896 /* Add a sockent to the global list of sockets */
1897 static int sockent_add (sockent_t *se) /* {{{ */
1899 sockent_t *last_ptr;
1904 if (se->type == SOCKENT_TYPE_SERVER)
1909 tmp = realloc (listen_sockets_pollfd,
1910 sizeof (*tmp) * (listen_sockets_num
1911 + se->data.server.fd_num));
1914 ERROR ("network plugin: realloc failed.");
1917 listen_sockets_pollfd = tmp;
1918 tmp = listen_sockets_pollfd + listen_sockets_num;
1920 for (i = 0; i < se->data.server.fd_num; i++)
1922 memset (tmp + i, 0, sizeof (*tmp));
1923 tmp[i].fd = se->data.server.fd[i];
1924 tmp[i].events = POLLIN | POLLPRI;
1928 listen_sockets_num += se->data.server.fd_num;
1930 if (listen_sockets == NULL)
1932 listen_sockets = se;
1935 last_ptr = listen_sockets;
1937 else /* if (se->type == SOCKENT_TYPE_CLIENT) */
1939 if (sending_sockets == NULL)
1941 sending_sockets = se;
1944 last_ptr = sending_sockets;
1947 while (last_ptr->next != NULL)
1948 last_ptr = last_ptr->next;
1949 last_ptr->next = se;
1952 } /* }}} int sockent_add */
1954 static void *dispatch_thread (void __attribute__((unused)) *arg) /* {{{ */
1958 receive_list_entry_t *ent;
1961 /* Lock and wait for more data to come in */
1962 pthread_mutex_lock (&receive_list_lock);
1963 while ((listen_loop == 0)
1964 && (receive_list_head == NULL))
1965 pthread_cond_wait (&receive_list_cond, &receive_list_lock);
1967 /* Remove the head entry and unlock */
1968 ent = receive_list_head;
1970 receive_list_head = ent->next;
1971 pthread_mutex_unlock (&receive_list_lock);
1973 /* Check whether we are supposed to exit. We do NOT check `listen_loop'
1974 * because we dispatch all missing packets before shutting down. */
1978 /* Look for the correct `sockent_t' */
1979 se = listen_sockets;
1984 for (i = 0; i < se->data.server.fd_num; i++)
1985 if (se->data.server.fd[i] == ent->fd)
1988 if (i < se->data.server.fd_num)
1996 ERROR ("network plugin: Got packet from FD %i, but can't "
1997 "find an appropriate socket entry.",
2003 parse_packet (se, ent->data, ent->data_len, /* flags = */ 0);
2008 } /* }}} void *dispatch_thread */
2010 static int network_receive (void) /* {{{ */
2012 char buffer[BUFF_SIZE];
2018 receive_list_entry_t *private_list_head;
2019 receive_list_entry_t *private_list_tail;
2021 assert (listen_sockets_num > 0);
2023 private_list_head = NULL;
2024 private_list_tail = NULL;
2026 while (listen_loop == 0)
2028 status = poll (listen_sockets_pollfd, listen_sockets_num, -1);
2035 ERROR ("poll failed: %s",
2036 sstrerror (errno, errbuf, sizeof (errbuf)));
2040 for (i = 0; (i < listen_sockets_num) && (status > 0); i++)
2042 receive_list_entry_t *ent;
2044 if ((listen_sockets_pollfd[i].revents
2045 & (POLLIN | POLLPRI)) == 0)
2049 buffer_len = recv (listen_sockets_pollfd[i].fd,
2050 buffer, sizeof (buffer),
2055 ERROR ("recv failed: %s",
2056 sstrerror (errno, errbuf,
2061 /* TODO: Possible performance enhancement: Do not free
2062 * these entries in the dispatch thread but put them in
2063 * another list, so we don't have to allocate more and
2064 * more of these structures. */
2065 ent = malloc (sizeof (receive_list_entry_t));
2068 ERROR ("network plugin: malloc failed.");
2071 memset (ent, 0, sizeof (receive_list_entry_t));
2072 ent->fd = listen_sockets_pollfd[i].fd;
2075 /* Hopefully this be optimized out by the compiler. It
2076 * might help prevent stupid bugs in the future though.
2078 assert (sizeof (ent->data) == sizeof (buffer));
2080 memcpy (ent->data, buffer, buffer_len);
2081 ent->data_len = buffer_len;
2083 if (private_list_head == NULL)
2084 private_list_head = ent;
2086 private_list_tail->next = ent;
2087 private_list_tail = ent;
2089 /* Do not block here. Blocking here has led to
2090 * insufficient performance in the past. */
2091 if (pthread_mutex_trylock (&receive_list_lock) == 0)
2093 if (receive_list_head == NULL)
2094 receive_list_head = private_list_head;
2096 receive_list_tail->next = private_list_head;
2097 receive_list_tail = private_list_tail;
2099 private_list_head = NULL;
2100 private_list_tail = NULL;
2102 pthread_cond_signal (&receive_list_cond);
2103 pthread_mutex_unlock (&receive_list_lock);
2105 } /* for (listen_sockets_pollfd) */
2106 } /* while (listen_loop == 0) */
2108 /* Make sure everything is dispatched before exiting. */
2109 if (private_list_head != NULL)
2111 pthread_mutex_lock (&receive_list_lock);
2113 if (receive_list_head == NULL)
2114 receive_list_head = private_list_head;
2116 receive_list_tail->next = private_list_head;
2117 receive_list_tail = private_list_tail;
2119 private_list_head = NULL;
2120 private_list_tail = NULL;
2122 pthread_cond_signal (&receive_list_cond);
2123 pthread_mutex_unlock (&receive_list_lock);
2127 } /* }}} int network_receive */
2129 static void *receive_thread (void __attribute__((unused)) *arg)
2131 return (network_receive () ? (void *) 1 : (void *) 0);
2132 } /* void *receive_thread */
2134 static void network_init_buffer (void)
2136 memset (send_buffer, 0, sizeof (send_buffer));
2137 send_buffer_ptr = send_buffer;
2138 send_buffer_fill = 0;
2140 memset (&send_buffer_vl, 0, sizeof (send_buffer_vl));
2141 } /* int network_init_buffer */
2143 static void networt_send_buffer_plain (const sockent_t *se, /* {{{ */
2144 const char *buffer, size_t buffer_size)
2150 status = sendto (se->data.client.fd, buffer, buffer_size,
2152 (struct sockaddr *) se->data.client.addr,
2153 se->data.client.addrlen);
2159 ERROR ("network plugin: sendto failed: %s",
2160 sstrerror (errno, errbuf,
2167 } /* }}} void networt_send_buffer_plain */
2170 #define BUFFER_ADD(p,s) do { \
2171 memcpy (buffer + buffer_offset, (p), (s)); \
2172 buffer_offset += (s); \
2175 static void networt_send_buffer_signed (const sockent_t *se, /* {{{ */
2176 const char *in_buffer, size_t in_buffer_size)
2178 part_signature_sha256_t ps;
2179 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2180 size_t buffer_offset;
2181 size_t username_len;
2185 unsigned char *hash;
2188 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
2191 ERROR ("network plugin: Creating HMAC object failed: %s",
2192 gcry_strerror (err));
2196 err = gcry_md_setkey (hd, se->data.client.password,
2197 strlen (se->data.client.password));
2200 ERROR ("network plugin: gcry_md_setkey failed: %s",
2201 gcry_strerror (err));
2206 username_len = strlen (se->data.client.username);
2207 if (username_len > (BUFF_SIG_SIZE - PART_SIGNATURE_SHA256_SIZE))
2209 ERROR ("network plugin: Username too long: %s",
2210 se->data.client.username);
2214 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE,
2215 se->data.client.username, username_len);
2216 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE + username_len,
2217 in_buffer, in_buffer_size);
2219 /* Initialize the `ps' structure. */
2220 memset (&ps, 0, sizeof (ps));
2221 ps.head.type = htons (TYPE_SIGN_SHA256);
2222 ps.head.length = htons (PART_SIGNATURE_SHA256_SIZE + username_len);
2224 /* Calculate the hash value. */
2225 gcry_md_write (hd, buffer + PART_SIGNATURE_SHA256_SIZE,
2226 username_len + in_buffer_size);
2227 hash = gcry_md_read (hd, GCRY_MD_SHA256);
2230 ERROR ("network plugin: gcry_md_read failed.");
2234 memcpy (ps.hash, hash, sizeof (ps.hash));
2236 /* Add the header */
2239 BUFFER_ADD (&ps.head.type, sizeof (ps.head.type));
2240 BUFFER_ADD (&ps.head.length, sizeof (ps.head.length));
2241 BUFFER_ADD (ps.hash, sizeof (ps.hash));
2243 assert (buffer_offset == PART_SIGNATURE_SHA256_SIZE);
2248 buffer_offset = PART_SIGNATURE_SHA256_SIZE + username_len + in_buffer_size;
2249 networt_send_buffer_plain (se, buffer, buffer_offset);
2250 } /* }}} void networt_send_buffer_signed */
2252 static void networt_send_buffer_encrypted (sockent_t *se, /* {{{ */
2253 const char *in_buffer, size_t in_buffer_size)
2255 part_encryption_aes256_t pea;
2256 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2258 size_t buffer_offset;
2260 size_t username_len;
2262 gcry_cipher_hd_t cypher;
2264 /* Initialize the header fields */
2265 memset (&pea, 0, sizeof (pea));
2266 pea.head.type = htons (TYPE_ENCR_AES256);
2268 pea.username = se->data.client.username;
2270 username_len = strlen (pea.username);
2271 if ((PART_ENCRYPTION_AES256_SIZE + username_len) > BUFF_SIG_SIZE)
2273 ERROR ("network plugin: Username too long: %s", pea.username);
2277 buffer_size = PART_ENCRYPTION_AES256_SIZE + username_len + in_buffer_size;
2278 header_size = PART_ENCRYPTION_AES256_SIZE + username_len
2279 - sizeof (pea.hash);
2281 assert (buffer_size <= sizeof (buffer));
2282 DEBUG ("network plugin: networt_send_buffer_encrypted: "
2283 "buffer_size = %zu;", buffer_size);
2285 pea.head.length = htons ((uint16_t) (PART_ENCRYPTION_AES256_SIZE
2286 + username_len + in_buffer_size));
2287 pea.username_length = htons ((uint16_t) username_len);
2289 /* Chose a random initialization vector. */
2290 gcry_randomize ((void *) &pea.iv, sizeof (pea.iv), GCRY_STRONG_RANDOM);
2292 /* Create hash of the payload */
2293 gcry_md_hash_buffer (GCRY_MD_SHA1, pea.hash, in_buffer, in_buffer_size);
2295 /* Initialize the buffer */
2297 memset (buffer, 0, sizeof (buffer));
2300 BUFFER_ADD (&pea.head.type, sizeof (pea.head.type));
2301 BUFFER_ADD (&pea.head.length, sizeof (pea.head.length));
2302 BUFFER_ADD (&pea.username_length, sizeof (pea.username_length));
2303 BUFFER_ADD (pea.username, username_len);
2304 BUFFER_ADD (pea.iv, sizeof (pea.iv));
2305 assert (buffer_offset == header_size);
2306 BUFFER_ADD (pea.hash, sizeof (pea.hash));
2307 BUFFER_ADD (in_buffer, in_buffer_size);
2309 assert (buffer_offset == buffer_size);
2311 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
2312 se->data.client.password);
2316 /* Encrypt the buffer in-place */
2317 err = gcry_cipher_encrypt (cypher,
2318 buffer + header_size,
2319 buffer_size - header_size,
2320 /* in = */ NULL, /* in len = */ 0);
2323 ERROR ("network plugin: gcry_cipher_encrypt returned: %s",
2324 gcry_strerror (err));
2328 /* Send it out without further modifications */
2329 networt_send_buffer_plain (se, buffer, buffer_size);
2330 } /* }}} void networt_send_buffer_encrypted */
2332 #endif /* HAVE_LIBGCRYPT */
2334 static void network_send_buffer (char *buffer, size_t buffer_len) /* {{{ */
2338 DEBUG ("network plugin: network_send_buffer: buffer_len = %zu", buffer_len);
2340 for (se = sending_sockets; se != NULL; se = se->next)
2343 if (se->data.client.security_level == SECURITY_LEVEL_ENCRYPT)
2344 networt_send_buffer_encrypted (se, buffer, buffer_len);
2345 else if (se->data.client.security_level == SECURITY_LEVEL_SIGN)
2346 networt_send_buffer_signed (se, buffer, buffer_len);
2347 else /* if (se->data.client.security_level == SECURITY_LEVEL_NONE) */
2348 #endif /* HAVE_LIBGCRYPT */
2349 networt_send_buffer_plain (se, buffer, buffer_len);
2350 } /* for (sending_sockets) */
2351 } /* }}} void network_send_buffer */
2353 static int add_to_buffer (char *buffer, int buffer_size, /* {{{ */
2354 value_list_t *vl_def,
2355 const data_set_t *ds, const value_list_t *vl)
2357 char *buffer_orig = buffer;
2359 if (strcmp (vl_def->host, vl->host) != 0)
2361 if (write_part_string (&buffer, &buffer_size, TYPE_HOST,
2362 vl->host, strlen (vl->host)) != 0)
2364 sstrncpy (vl_def->host, vl->host, sizeof (vl_def->host));
2367 if (vl_def->time != vl->time)
2369 if (write_part_number (&buffer, &buffer_size, TYPE_TIME,
2370 (uint64_t) vl->time))
2372 vl_def->time = vl->time;
2375 if (vl_def->interval != vl->interval)
2377 if (write_part_number (&buffer, &buffer_size, TYPE_INTERVAL,
2378 (uint64_t) vl->interval))
2380 vl_def->interval = vl->interval;
2383 if (strcmp (vl_def->plugin, vl->plugin) != 0)
2385 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN,
2386 vl->plugin, strlen (vl->plugin)) != 0)
2388 sstrncpy (vl_def->plugin, vl->plugin, sizeof (vl_def->plugin));
2391 if (strcmp (vl_def->plugin_instance, vl->plugin_instance) != 0)
2393 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
2394 vl->plugin_instance,
2395 strlen (vl->plugin_instance)) != 0)
2397 sstrncpy (vl_def->plugin_instance, vl->plugin_instance, sizeof (vl_def->plugin_instance));
2400 if (strcmp (vl_def->type, vl->type) != 0)
2402 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
2403 vl->type, strlen (vl->type)) != 0)
2405 sstrncpy (vl_def->type, ds->type, sizeof (vl_def->type));
2408 if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
2410 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE_INSTANCE,
2412 strlen (vl->type_instance)) != 0)
2414 sstrncpy (vl_def->type_instance, vl->type_instance, sizeof (vl_def->type_instance));
2417 if (write_part_values (&buffer, &buffer_size, ds, vl) != 0)
2420 return (buffer - buffer_orig);
2421 } /* }}} int add_to_buffer */
2423 static void flush_buffer (void)
2425 DEBUG ("network plugin: flush_buffer: send_buffer_fill = %i",
2428 network_send_buffer (send_buffer, (size_t) send_buffer_fill);
2429 network_init_buffer ();
2432 static int network_write (const data_set_t *ds, const value_list_t *vl,
2433 user_data_t __attribute__((unused)) *user_data)
2437 /* If the value is already in the cache, we have received it via the
2438 * network. We write it again if forwarding is activated. It's then in
2439 * the cache and should we receive it again we will ignore it. */
2440 status = cache_check (vl);
2441 if ((network_config_forward == 0)
2445 pthread_mutex_lock (&send_buffer_lock);
2447 status = add_to_buffer (send_buffer_ptr,
2448 sizeof (send_buffer) - (send_buffer_fill + BUFF_SIG_SIZE),
2453 /* status == bytes added to the buffer */
2454 send_buffer_fill += status;
2455 send_buffer_ptr += status;
2461 status = add_to_buffer (send_buffer_ptr,
2462 sizeof (send_buffer) - (send_buffer_fill + BUFF_SIG_SIZE),
2468 send_buffer_fill += status;
2469 send_buffer_ptr += status;
2475 ERROR ("network plugin: Unable to append to the "
2476 "buffer for some weird reason");
2478 else if ((sizeof (send_buffer) - send_buffer_fill) < 15)
2483 pthread_mutex_unlock (&send_buffer_lock);
2485 return ((status < 0) ? -1 : 0);
2486 } /* int network_write */
2488 static int network_config_set_boolean (const oconfig_item_t *ci, /* {{{ */
2491 if ((ci->values_num != 1)
2492 || ((ci->values[0].type != OCONFIG_TYPE_BOOLEAN)
2493 && (ci->values[0].type != OCONFIG_TYPE_STRING)))
2495 ERROR ("network plugin: The `%s' config option needs "
2496 "exactly one boolean argument.", ci->key);
2500 if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
2502 if (ci->values[0].value.boolean)
2509 char *str = ci->values[0].value.string;
2511 if ((strcasecmp ("true", str) == 0)
2512 || (strcasecmp ("yes", str) == 0)
2513 || (strcasecmp ("on", str) == 0))
2515 else if ((strcasecmp ("false", str) == 0)
2516 || (strcasecmp ("no", str) == 0)
2517 || (strcasecmp ("off", str) == 0))
2521 ERROR ("network plugin: Cannot parse string value `%s' of the `%s' "
2522 "option as boolean value.",
2529 } /* }}} int network_config_set_boolean */
2531 static int network_config_set_ttl (const oconfig_item_t *ci) /* {{{ */
2534 if ((ci->values_num != 1)
2535 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
2537 WARNING ("network plugin: The `TimeToLive' config option needs exactly "
2538 "one numeric argument.");
2542 tmp = (int) ci->values[0].value.number;
2543 if ((tmp > 0) && (tmp <= 255))
2544 network_config_ttl = tmp;
2547 } /* }}} int network_config_set_ttl */
2550 static int network_config_set_string (const oconfig_item_t *ci, /* {{{ */
2554 if ((ci->values_num != 1)
2555 || (ci->values[0].type != OCONFIG_TYPE_STRING))
2557 WARNING ("network plugin: The `%s' config option needs exactly "
2558 "one string argument.", ci->key);
2562 tmp = strdup (ci->values[0].value.string);
2566 sfree (*ret_string);
2570 } /* }}} int network_config_set_string */
2571 #endif /* HAVE_LIBGCRYPT */
2574 static int network_config_set_security_level (oconfig_item_t *ci, /* {{{ */
2578 if ((ci->values_num != 1)
2579 || (ci->values[0].type != OCONFIG_TYPE_STRING))
2581 WARNING ("network plugin: The `SecurityLevel' config option needs exactly "
2582 "one string argument.");
2586 str = ci->values[0].value.string;
2587 if (strcasecmp ("Encrypt", str) == 0)
2588 *retval = SECURITY_LEVEL_ENCRYPT;
2589 else if (strcasecmp ("Sign", str) == 0)
2590 *retval = SECURITY_LEVEL_SIGN;
2591 else if (strcasecmp ("None", str) == 0)
2592 *retval = SECURITY_LEVEL_NONE;
2595 WARNING ("network plugin: Unknown security level: %s.", str);
2600 } /* }}} int network_config_set_security_level */
2601 #endif /* HAVE_LIBGCRYPT */
2603 static int network_config_add_listen (const oconfig_item_t *ci) /* {{{ */
2609 if ((ci->values_num < 1) || (ci->values_num > 2)
2610 || (ci->values[0].type != OCONFIG_TYPE_STRING)
2611 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
2613 ERROR ("network plugin: The `%s' config option needs "
2614 "one or two string arguments.", ci->key);
2618 se = malloc (sizeof (*se));
2621 ERROR ("network plugin: malloc failed.");
2624 sockent_init (se, SOCKENT_TYPE_SERVER);
2626 se->node = strdup (ci->values[0].value.string);
2627 if (ci->values_num >= 2)
2628 se->service = strdup (ci->values[1].value.string);
2630 for (i = 0; i < ci->children_num; i++)
2632 oconfig_item_t *child = ci->children + i;
2635 if (strcasecmp ("AuthFile", child->key) == 0)
2636 network_config_set_string (child, &se->data.server.auth_file);
2637 else if (strcasecmp ("SecurityLevel", child->key) == 0)
2638 network_config_set_security_level (child,
2639 &se->data.server.security_level);
2641 #endif /* HAVE_LIBGCRYPT */
2643 WARNING ("network plugin: Option `%s' is not allowed here.",
2649 if ((se->data.server.security_level > SECURITY_LEVEL_NONE)
2650 && (se->data.server.auth_file == NULL))
2652 ERROR ("network plugin: A security level higher than `none' was "
2653 "requested, but no AuthFile option was given. Cowardly refusing to "
2654 "open this socket!");
2655 sockent_destroy (se);
2658 #endif /* HAVE_LIBGCRYPT */
2660 status = sockent_open (se);
2663 ERROR ("network plugin: network_config_add_listen: sockent_open failed.");
2664 sockent_destroy (se);
2668 status = sockent_add (se);
2671 ERROR ("network plugin: network_config_add_listen: sockent_add failed.");
2672 sockent_destroy (se);
2677 } /* }}} int network_config_add_listen */
2679 static int network_config_add_server (const oconfig_item_t *ci) /* {{{ */
2685 if ((ci->values_num < 1) || (ci->values_num > 2)
2686 || (ci->values[0].type != OCONFIG_TYPE_STRING)
2687 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
2689 ERROR ("network plugin: The `%s' config option needs "
2690 "one or two string arguments.", ci->key);
2694 se = malloc (sizeof (*se));
2697 ERROR ("network plugin: malloc failed.");
2700 sockent_init (se, SOCKENT_TYPE_CLIENT);
2702 se->node = strdup (ci->values[0].value.string);
2703 if (ci->values_num >= 2)
2704 se->service = strdup (ci->values[1].value.string);
2706 for (i = 0; i < ci->children_num; i++)
2708 oconfig_item_t *child = ci->children + i;
2711 if (strcasecmp ("Username", child->key) == 0)
2712 network_config_set_string (child, &se->data.client.username);
2713 else if (strcasecmp ("Password", child->key) == 0)
2714 network_config_set_string (child, &se->data.client.password);
2715 else if (strcasecmp ("SecurityLevel", child->key) == 0)
2716 network_config_set_security_level (child,
2717 &se->data.client.security_level);
2719 #endif /* HAVE_LIBGCRYPT */
2721 WARNING ("network plugin: Option `%s' is not allowed here.",
2727 if ((se->data.client.security_level > SECURITY_LEVEL_NONE)
2728 && ((se->data.client.username == NULL)
2729 || (se->data.client.password == NULL)))
2731 ERROR ("network plugin: A security level higher than `none' was "
2732 "requested, but no Username or Password option was given. "
2733 "Cowardly refusing to open this socket!");
2734 sockent_destroy (se);
2737 #endif /* HAVE_LIBGCRYPT */
2739 status = sockent_open (se);
2742 ERROR ("network plugin: network_config_add_server: sockent_open failed.");
2743 sockent_destroy (se);
2747 status = sockent_add (se);
2750 ERROR ("network plugin: network_config_add_server: sockent_add failed.");
2751 sockent_destroy (se);
2756 } /* }}} int network_config_add_server */
2758 static int network_config_set_cache_flush (const oconfig_item_t *ci) /* {{{ */
2761 if ((ci->values_num != 1)
2762 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
2764 WARNING ("network plugin: The `CacheFlush' config option needs exactly "
2765 "one numeric argument.");
2769 tmp = (int) ci->values[0].value.number;
2771 network_config_ttl = tmp;
2774 } /* }}} int network_config_set_cache_flush */
2776 static int network_config (oconfig_item_t *ci) /* {{{ */
2780 for (i = 0; i < ci->children_num; i++)
2782 oconfig_item_t *child = ci->children + i;
2784 if (strcasecmp ("Listen", child->key) == 0)
2785 network_config_add_listen (child);
2786 else if (strcasecmp ("Server", child->key) == 0)
2787 network_config_add_server (child);
2788 else if (strcasecmp ("TimeToLive", child->key) == 0)
2789 network_config_set_ttl (child);
2790 else if (strcasecmp ("Forward", child->key) == 0)
2791 network_config_set_boolean (child, &network_config_forward);
2792 else if (strcasecmp ("CacheFlush", child->key) == 0)
2793 network_config_set_cache_flush (child);
2796 WARNING ("network plugin: Option `%s' is not allowed here.",
2802 } /* }}} int network_config */
2804 static int network_notification (const notification_t *n,
2805 user_data_t __attribute__((unused)) *user_data)
2807 char buffer[BUFF_SIZE];
2808 char *buffer_ptr = buffer;
2809 int buffer_free = sizeof (buffer);
2812 memset (buffer, '\0', sizeof (buffer));
2815 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_TIME,
2816 (uint64_t) n->time);
2820 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_SEVERITY,
2821 (uint64_t) n->severity);
2825 if (strlen (n->host) > 0)
2827 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_HOST,
2828 n->host, strlen (n->host));
2833 if (strlen (n->plugin) > 0)
2835 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_PLUGIN,
2836 n->plugin, strlen (n->plugin));
2841 if (strlen (n->plugin_instance) > 0)
2843 status = write_part_string (&buffer_ptr, &buffer_free,
2844 TYPE_PLUGIN_INSTANCE,
2845 n->plugin_instance, strlen (n->plugin_instance));
2850 if (strlen (n->type) > 0)
2852 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE,
2853 n->type, strlen (n->type));
2858 if (strlen (n->type_instance) > 0)
2860 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE_INSTANCE,
2861 n->type_instance, strlen (n->type_instance));
2866 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_MESSAGE,
2867 n->message, strlen (n->message));
2871 network_send_buffer (buffer, sizeof (buffer) - buffer_free);
2874 } /* int network_notification */
2876 static int network_shutdown (void)
2880 /* Kill the listening thread */
2881 if (receive_thread_running != 0)
2883 INFO ("network plugin: Stopping receive thread.");
2884 pthread_kill (receive_thread_id, SIGTERM);
2885 pthread_join (receive_thread_id, NULL /* no return value */);
2886 memset (&receive_thread_id, 0, sizeof (receive_thread_id));
2887 receive_thread_running = 0;
2890 /* Shutdown the dispatching thread */
2891 if (dispatch_thread_running != 0)
2893 INFO ("network plugin: Stopping dispatch thread.");
2894 pthread_mutex_lock (&receive_list_lock);
2895 pthread_cond_broadcast (&receive_list_cond);
2896 pthread_mutex_unlock (&receive_list_lock);
2897 pthread_join (dispatch_thread_id, /* ret = */ NULL);
2898 dispatch_thread_running = 0;
2901 sockent_destroy (listen_sockets);
2903 if (send_buffer_fill > 0)
2906 if (cache_tree != NULL)
2911 while (c_avl_pick (cache_tree, &key, &value) == 0)
2916 c_avl_destroy (cache_tree);
2920 /* TODO: Close `sending_sockets' */
2922 plugin_unregister_config ("network");
2923 plugin_unregister_init ("network");
2924 plugin_unregister_write ("network");
2925 plugin_unregister_shutdown ("network");
2927 /* Let the init function do it's move again ;) */
2928 cache_flush_last = 0;
2931 } /* int network_shutdown */
2933 static int network_init (void)
2935 /* Check if we were already initialized. If so, just return - there's
2936 * nothing more to do (for now, that is). */
2937 if (cache_flush_last != 0)
2940 plugin_register_shutdown ("network", network_shutdown);
2942 network_init_buffer ();
2944 cache_tree = c_avl_create ((int (*) (const void *, const void *)) strcmp);
2945 cache_flush_last = time (NULL);
2947 /* setup socket(s) and so on */
2948 if (sending_sockets != NULL)
2950 plugin_register_write ("network", network_write,
2951 /* user_data = */ NULL);
2952 plugin_register_notification ("network", network_notification,
2953 /* user_data = */ NULL);
2956 /* If no threads need to be started, return here. */
2957 if ((listen_sockets_num == 0)
2958 || ((dispatch_thread_running != 0)
2959 && (receive_thread_running != 0)))
2962 if (dispatch_thread_running == 0)
2965 status = pthread_create (&dispatch_thread_id,
2966 NULL /* no attributes */,
2968 NULL /* no argument */);
2972 ERROR ("network: pthread_create failed: %s",
2973 sstrerror (errno, errbuf,
2978 dispatch_thread_running = 1;
2982 if (receive_thread_running == 0)
2985 status = pthread_create (&receive_thread_id,
2986 NULL /* no attributes */,
2988 NULL /* no argument */);
2992 ERROR ("network: pthread_create failed: %s",
2993 sstrerror (errno, errbuf,
2998 receive_thread_running = 1;
3003 } /* int network_init */
3006 * The flush option of the network plugin cannot flush individual identifiers.
3007 * All the values are added to a buffer and sent when the buffer is full, the
3008 * requested value may or may not be in there, it's not worth finding out. We
3009 * just send the buffer if `flush' is called - if the requested value was in
3010 * there, good. If not, well, then there is nothing to flush.. -octo
3012 static int network_flush (int timeout,
3013 const char __attribute__((unused)) *identifier,
3014 user_data_t __attribute__((unused)) *user_data)
3016 pthread_mutex_lock (&send_buffer_lock);
3018 if (((time (NULL) - cache_flush_last) >= timeout)
3019 && (send_buffer_fill > 0))
3024 pthread_mutex_unlock (&send_buffer_lock);
3027 } /* int network_flush */
3029 void module_register (void)
3031 plugin_register_complex_config ("network", network_config);
3032 plugin_register_init ("network", network_init);
3033 plugin_register_flush ("network", network_flush,
3034 /* user_data = */ NULL);
3035 } /* void module_register */
3037 /* vim: set fdm=marker : */