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>
54 GCRY_THREAD_OPTION_PTHREAD_IMPL;
57 /* 1500 - 40 - 8 = Ethernet packet - IPv6 header - UDP header */
58 /* #define BUFF_SIZE 1452 */
60 #ifndef IPV6_ADD_MEMBERSHIP
61 # ifdef IPV6_JOIN_GROUP
62 # define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
64 # error "Neither IP_ADD_MEMBERSHIP nor IPV6_JOIN_GROUP is defined"
66 #endif /* !IP_ADD_MEMBERSHIP */
68 /* Buffer size to allocate. */
69 #define BUFF_SIZE 1024
72 * Maximum size required for encryption / signing:
74 * 42 bytes for the encryption header
75 * + 64 bytes for the username
79 #define BUFF_SIG_SIZE 106
84 #define SECURITY_LEVEL_NONE 0
86 # define SECURITY_LEVEL_SIGN 1
87 # define SECURITY_LEVEL_ENCRYPT 2
92 struct sockaddr_storage *addr;
98 gcry_cipher_hd_t cypher;
99 unsigned char password_hash[32];
103 struct sockent_server
111 gcry_cipher_hd_t cypher;
115 typedef struct sockent
117 #define SOCKENT_TYPE_CLIENT 1
118 #define SOCKENT_TYPE_SERVER 2
126 struct sockent_client client;
127 struct sockent_server server;
130 struct sockent *next;
133 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
134 * 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
135 * +-------+-----------------------+-------------------------------+
136 * ! Ver. ! ! Length !
137 * +-------+-----------------------+-------------------------------+
144 typedef struct part_header_s part_header_t;
146 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
147 * 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
148 * +-------------------------------+-------------------------------+
150 * +-------------------------------+-------------------------------+
151 * : (Length - 4) Bytes :
152 * +---------------------------------------------------------------+
159 typedef struct part_string_s part_string_t;
161 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
162 * 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
163 * +-------------------------------+-------------------------------+
165 * +-------------------------------+-------------------------------+
166 * : (Length - 4 == 2 || 4 || 8) Bytes :
167 * +---------------------------------------------------------------+
174 typedef struct part_number_s part_number_t;
176 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
177 * 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
178 * +-------------------------------+-------------------------------+
180 * +-------------------------------+---------------+---------------+
181 * ! Num of values ! Type0 ! Type1 !
182 * +-------------------------------+---------------+---------------+
185 * +---------------------------------------------------------------+
188 * +---------------------------------------------------------------+
193 uint16_t *num_values;
194 uint8_t *values_types;
197 typedef struct part_values_s part_values_t;
199 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
200 * 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
201 * +-------------------------------+-------------------------------+
203 * +-------------------------------+-------------------------------+
204 * ! Hash (Bits 0 - 31) !
206 * ! Hash (Bits 224 - 255) !
207 * +---------------------------------------------------------------+
210 #define PART_SIGNATURE_SHA256_SIZE 36
211 struct part_signature_sha256_s
214 unsigned char hash[32];
217 typedef struct part_signature_sha256_s part_signature_sha256_t;
219 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
220 * 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
221 * +-------------------------------+-------------------------------+
223 * +-------------------------------+-------------------------------+
224 * ! Original length ! Padding (0 - 15 bytes) !
225 * +-------------------------------+-------------------------------+
226 * ! Hash (Bits 0 - 31) !
228 * ! Hash (Bits 128 - 159) !
229 * +---------------------------------------------------------------+
232 #define PART_ENCRYPTION_AES256_SIZE 42
233 struct part_encryption_aes256_s
236 uint16_t username_length;
238 unsigned char iv[16];
240 unsigned char hash[20];
244 typedef struct part_encryption_aes256_s part_encryption_aes256_t;
246 struct receive_list_entry_s
248 char data[BUFF_SIZE];
251 struct receive_list_entry_s *next;
253 typedef struct receive_list_entry_s receive_list_entry_t;
258 static int network_config_ttl = 0;
259 static int network_config_forward = 0;
261 static sockent_t *sending_sockets = NULL;
263 static receive_list_entry_t *receive_list_head = NULL;
264 static receive_list_entry_t *receive_list_tail = NULL;
265 static pthread_mutex_t receive_list_lock = PTHREAD_MUTEX_INITIALIZER;
266 static pthread_cond_t receive_list_cond = PTHREAD_COND_INITIALIZER;
268 static sockent_t *listen_sockets = NULL;
269 static struct pollfd *listen_sockets_pollfd = NULL;
270 static size_t listen_sockets_num = 0;
272 /* The receive and dispatch threads will run as long as `listen_loop' is set to
274 static int listen_loop = 0;
275 static int receive_thread_running = 0;
276 static pthread_t receive_thread_id;
277 static int dispatch_thread_running = 0;
278 static pthread_t dispatch_thread_id;
280 /* Buffer in which to-be-sent network packets are constructed. */
281 static char send_buffer[BUFF_SIZE];
282 static char *send_buffer_ptr;
283 static int send_buffer_fill;
284 static value_list_t send_buffer_vl = VALUE_LIST_STATIC;
285 static pthread_mutex_t send_buffer_lock = PTHREAD_MUTEX_INITIALIZER;
287 /* In this cache we store all the values we received, so we can send out only
288 * those values which were *not* received via the network plugin, too. This is
289 * used for the `Forward false' option. */
290 static c_avl_tree_t *cache_tree = NULL;
291 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
292 static time_t cache_flush_last = 0;
293 static int cache_flush_interval = 1800;
298 static int cache_flush (void)
308 c_avl_iterator_t *iter;
310 time_t curtime = time (NULL);
312 iter = c_avl_get_iterator (cache_tree);
313 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
315 if ((curtime - *value) <= cache_flush_interval)
317 tmp = (char **) realloc (keys,
318 (keys_num + 1) * sizeof (char *));
322 c_avl_iterator_destroy (iter);
323 ERROR ("network plugin: cache_flush: realloc"
328 keys[keys_num] = key;
330 } /* while (c_avl_iterator_next) */
331 c_avl_iterator_destroy (iter);
333 for (i = 0; i < keys_num; i++)
335 if (c_avl_remove (cache_tree, keys[i], (void *) &key,
336 (void *) &value) != 0)
338 WARNING ("network plugin: cache_flush: c_avl_remove"
339 " (%s) failed.", keys[i]);
349 DEBUG ("network plugin: cache_flush: Removed %i %s",
350 keys_num, (keys_num == 1) ? "entry" : "entries");
351 cache_flush_last = curtime;
353 } /* int cache_flush */
355 static int cache_check (const value_list_t *vl)
358 time_t *value = NULL;
361 if (cache_tree == NULL)
364 if (format_name (key, sizeof (key), vl->host, vl->plugin,
365 vl->plugin_instance, vl->type, vl->type_instance))
368 pthread_mutex_lock (&cache_lock);
370 if (c_avl_get (cache_tree, key, (void *) &value) == 0)
372 if (*value < vl->time)
379 DEBUG ("network plugin: cache_check: *value = %i >= vl->time = %i",
380 (int) *value, (int) vl->time);
386 char *key_copy = strdup (key);
387 value = malloc (sizeof (time_t));
388 if ((key_copy != NULL) && (value != NULL))
391 c_avl_insert (cache_tree, key_copy, value);
401 if ((time (NULL) - cache_flush_last) > cache_flush_interval)
404 pthread_mutex_unlock (&cache_lock);
407 } /* int cache_check */
410 static gcry_cipher_hd_t network_get_aes256_cypher (sockent_t *se, /* {{{ */
411 const void *iv, size_t iv_size, const char *username)
414 gcry_cipher_hd_t *cyper_ptr;
415 unsigned char password_hash[32];
417 if (se->type == SOCKENT_TYPE_CLIENT)
419 cyper_ptr = &se->data.client.cypher;
420 memcpy (password_hash, se->data.client.password_hash,
421 sizeof (password_hash));
427 cyper_ptr = &se->data.server.cypher;
429 if (username == NULL)
432 secret = fbh_get (se->data.server.userdb, username);
436 gcry_md_hash_buffer (GCRY_MD_SHA256,
438 secret, strlen (secret));
443 if (*cyper_ptr == NULL)
445 err = gcry_cipher_open (cyper_ptr,
446 GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_OFB, /* flags = */ 0);
449 ERROR ("network plugin: gcry_cipher_open returned: %s",
450 gcry_strerror (err));
457 gcry_cipher_reset (*cyper_ptr);
459 assert (*cyper_ptr != NULL);
461 err = gcry_cipher_setkey (*cyper_ptr,
462 password_hash, sizeof (password_hash));
465 ERROR ("network plugin: gcry_cipher_setkey returned: %s",
466 gcry_strerror (err));
467 gcry_cipher_close (*cyper_ptr);
472 err = gcry_cipher_setiv (*cyper_ptr, iv, iv_size);
475 ERROR ("network plugin: gcry_cipher_setkey returned: %s",
476 gcry_strerror (err));
477 gcry_cipher_close (*cyper_ptr);
483 } /* }}} int network_get_aes256_cypher */
484 #endif /* HAVE_LIBGCRYPT */
486 static int write_part_values (char **ret_buffer, int *ret_buffer_len,
487 const data_set_t *ds, const value_list_t *vl)
493 part_header_t pkg_ph;
494 uint16_t pkg_num_values;
495 uint8_t *pkg_values_types;
501 num_values = vl->values_len;
502 packet_len = sizeof (part_header_t) + sizeof (uint16_t)
503 + (num_values * sizeof (uint8_t))
504 + (num_values * sizeof (value_t));
506 if (*ret_buffer_len < packet_len)
509 pkg_values_types = (uint8_t *) malloc (num_values * sizeof (uint8_t));
510 if (pkg_values_types == NULL)
512 ERROR ("network plugin: write_part_values: malloc failed.");
516 pkg_values = (value_t *) malloc (num_values * sizeof (value_t));
517 if (pkg_values == NULL)
519 free (pkg_values_types);
520 ERROR ("network plugin: write_part_values: malloc failed.");
524 pkg_ph.type = htons (TYPE_VALUES);
525 pkg_ph.length = htons (packet_len);
527 pkg_num_values = htons ((uint16_t) vl->values_len);
529 for (i = 0; i < num_values; i++)
531 if (ds->ds[i].type == DS_TYPE_COUNTER)
533 pkg_values_types[i] = DS_TYPE_COUNTER;
534 pkg_values[i].counter = htonll (vl->values[i].counter);
538 pkg_values_types[i] = DS_TYPE_GAUGE;
539 pkg_values[i].gauge = htond (vl->values[i].gauge);
544 * Use `memcpy' to write everything to the buffer, because the pointer
545 * may be unaligned and some architectures, such as SPARC, can't handle
548 packet_ptr = *ret_buffer;
550 memcpy (packet_ptr + offset, &pkg_ph, sizeof (pkg_ph));
551 offset += sizeof (pkg_ph);
552 memcpy (packet_ptr + offset, &pkg_num_values, sizeof (pkg_num_values));
553 offset += sizeof (pkg_num_values);
554 memcpy (packet_ptr + offset, pkg_values_types, num_values * sizeof (uint8_t));
555 offset += num_values * sizeof (uint8_t);
556 memcpy (packet_ptr + offset, pkg_values, num_values * sizeof (value_t));
557 offset += num_values * sizeof (value_t);
559 assert (offset == packet_len);
561 *ret_buffer = packet_ptr + packet_len;
562 *ret_buffer_len -= packet_len;
564 free (pkg_values_types);
568 } /* int write_part_values */
570 static int write_part_number (char **ret_buffer, int *ret_buffer_len,
571 int type, uint64_t value)
576 part_header_t pkg_head;
581 packet_len = sizeof (pkg_head) + sizeof (pkg_value);
583 if (*ret_buffer_len < packet_len)
586 pkg_head.type = htons (type);
587 pkg_head.length = htons (packet_len);
588 pkg_value = htonll (value);
590 packet_ptr = *ret_buffer;
592 memcpy (packet_ptr + offset, &pkg_head, sizeof (pkg_head));
593 offset += sizeof (pkg_head);
594 memcpy (packet_ptr + offset, &pkg_value, sizeof (pkg_value));
595 offset += sizeof (pkg_value);
597 assert (offset == packet_len);
599 *ret_buffer = packet_ptr + packet_len;
600 *ret_buffer_len -= packet_len;
603 } /* int write_part_number */
605 static int write_part_string (char **ret_buffer, int *ret_buffer_len,
606 int type, const char *str, int str_len)
616 buffer_len = 2 * sizeof (uint16_t) + str_len + 1;
617 if (*ret_buffer_len < buffer_len)
620 pkg_type = htons (type);
621 pkg_length = htons (buffer_len);
623 buffer = *ret_buffer;
625 memcpy (buffer + offset, (void *) &pkg_type, sizeof (pkg_type));
626 offset += sizeof (pkg_type);
627 memcpy (buffer + offset, (void *) &pkg_length, sizeof (pkg_length));
628 offset += sizeof (pkg_length);
629 memcpy (buffer + offset, str, str_len);
631 memset (buffer + offset, '\0', 1);
634 assert (offset == buffer_len);
636 *ret_buffer = buffer + buffer_len;
637 *ret_buffer_len -= buffer_len;
640 } /* int write_part_string */
642 static int parse_part_values (void **ret_buffer, size_t *ret_buffer_len,
643 value_t **ret_values, int *ret_num_values)
645 char *buffer = *ret_buffer;
646 size_t buffer_len = *ret_buffer_len;
661 NOTICE ("network plugin: packet is too short: "
662 "buffer_len = %zu", buffer_len);
666 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
667 buffer += sizeof (tmp16);
668 pkg_type = ntohs (tmp16);
670 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
671 buffer += sizeof (tmp16);
672 pkg_length = ntohs (tmp16);
674 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
675 buffer += sizeof (tmp16);
676 pkg_numval = ntohs (tmp16);
678 assert (pkg_type == TYPE_VALUES);
680 exp_size = 3 * sizeof (uint16_t)
681 + pkg_numval * (sizeof (uint8_t) + sizeof (value_t));
682 if ((buffer_len < 0) || (buffer_len < exp_size))
684 WARNING ("network plugin: parse_part_values: "
686 "Chunk of size %zu expected, "
687 "but buffer has only %zu bytes left.",
688 exp_size, buffer_len);
692 if (pkg_length != exp_size)
694 WARNING ("network plugin: parse_part_values: "
695 "Length and number of values "
696 "in the packet don't match.");
700 pkg_types = (uint8_t *) malloc (pkg_numval * sizeof (uint8_t));
701 pkg_values = (value_t *) malloc (pkg_numval * sizeof (value_t));
702 if ((pkg_types == NULL) || (pkg_values == NULL))
706 ERROR ("network plugin: parse_part_values: malloc failed.");
710 memcpy ((void *) pkg_types, (void *) buffer, pkg_numval * sizeof (uint8_t));
711 buffer += pkg_numval * sizeof (uint8_t);
712 memcpy ((void *) pkg_values, (void *) buffer, pkg_numval * sizeof (value_t));
713 buffer += pkg_numval * sizeof (value_t);
715 for (i = 0; i < pkg_numval; i++)
717 if (pkg_types[i] == DS_TYPE_COUNTER)
718 pkg_values[i].counter = ntohll (pkg_values[i].counter);
719 else if (pkg_types[i] == DS_TYPE_GAUGE)
720 pkg_values[i].gauge = ntohd (pkg_values[i].gauge);
723 *ret_buffer = buffer;
724 *ret_buffer_len = buffer_len - pkg_length;
725 *ret_num_values = pkg_numval;
726 *ret_values = pkg_values;
731 } /* int parse_part_values */
733 static int parse_part_number (void **ret_buffer, size_t *ret_buffer_len,
736 char *buffer = *ret_buffer;
737 size_t buffer_len = *ret_buffer_len;
741 size_t exp_size = 2 * sizeof (uint16_t) + sizeof (uint64_t);
746 if ((buffer_len < 0) || ((size_t) buffer_len < exp_size))
748 WARNING ("network plugin: parse_part_number: "
750 "Chunk of size %zu expected, "
751 "but buffer has only %zu bytes left.",
752 exp_size, buffer_len);
756 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
757 buffer += sizeof (tmp16);
758 pkg_type = ntohs (tmp16);
760 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
761 buffer += sizeof (tmp16);
762 pkg_length = ntohs (tmp16);
764 memcpy ((void *) &tmp64, buffer, sizeof (tmp64));
765 buffer += sizeof (tmp64);
766 *value = ntohll (tmp64);
768 *ret_buffer = buffer;
769 *ret_buffer_len = buffer_len - pkg_length;
772 } /* int parse_part_number */
774 static int parse_part_string (void **ret_buffer, size_t *ret_buffer_len,
775 char *output, int output_len)
777 char *buffer = *ret_buffer;
778 size_t buffer_len = *ret_buffer_len;
781 size_t header_size = 2 * sizeof (uint16_t);
786 if ((buffer_len < 0) || (buffer_len < header_size))
788 WARNING ("network plugin: parse_part_string: "
790 "Chunk of at least size %zu expected, "
791 "but buffer has only %zu bytes left.",
792 header_size, buffer_len);
796 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
797 buffer += sizeof (tmp16);
798 pkg_type = ntohs (tmp16);
800 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
801 buffer += sizeof (tmp16);
802 pkg_length = ntohs (tmp16);
804 /* Check that packet fits in the input buffer */
805 if (pkg_length > buffer_len)
807 WARNING ("network plugin: parse_part_string: "
809 "Chunk of size %"PRIu16" received, "
810 "but buffer has only %zu bytes left.",
811 pkg_length, buffer_len);
815 /* Check that pkg_length is in the valid range */
816 if (pkg_length <= header_size)
818 WARNING ("network plugin: parse_part_string: "
820 "Header claims this packet is only %hu "
821 "bytes long.", pkg_length);
825 /* Check that the package data fits into the output buffer.
826 * The previous if-statement ensures that:
827 * `pkg_length > header_size' */
829 || ((size_t) output_len < ((size_t) pkg_length - header_size)))
831 WARNING ("network plugin: parse_part_string: "
832 "Output buffer too small.");
836 /* All sanity checks successfull, let's copy the data over */
837 output_len = pkg_length - header_size;
838 memcpy ((void *) output, (void *) buffer, output_len);
839 buffer += output_len;
841 /* For some very weird reason '\0' doesn't do the trick on SPARC in
843 if (output[output_len - 1] != 0)
845 WARNING ("network plugin: parse_part_string: "
846 "Received string does not end "
847 "with a NULL-byte.");
851 *ret_buffer = buffer;
852 *ret_buffer_len = buffer_len - pkg_length;
855 } /* int parse_part_string */
857 /* Forward declaration: parse_part_sign_sha256 and parse_part_encr_aes256 call
858 * parse_packet and vice versa. */
859 #define PP_SIGNED 0x01
860 #define PP_ENCRYPTED 0x02
861 static int parse_packet (sockent_t *se,
862 void *buffer, size_t buffer_size, int flags);
864 #define BUFFER_READ(p,s) do { \
865 memcpy ((p), buffer + buffer_offset, (s)); \
866 buffer_offset += (s); \
870 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
871 void **ret_buffer, size_t *ret_buffer_len, int flags)
875 size_t buffer_offset;
880 part_signature_sha256_t pss;
881 uint16_t pss_head_length;
882 char hash[sizeof (pss.hash)];
886 unsigned char *hash_ptr;
888 buffer = *ret_buffer;
889 buffer_len = *ret_buffer_len;
892 if (se->data.server.userdb == NULL)
894 NOTICE ("network plugin: Received signed network packet but can't verify "
895 "it because no user DB has been configured. Will accept it.");
899 /* Check if the buffer has enough data for this structure. */
900 if (buffer_len <= PART_SIGNATURE_SHA256_SIZE)
903 /* Read type and length header */
904 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
905 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
906 pss_head_length = ntohs (pss.head.length);
908 /* Check if the `pss_head_length' is within bounds. */
909 if ((pss_head_length <= PART_SIGNATURE_SHA256_SIZE)
910 || (pss_head_length > buffer_len))
912 ERROR ("network plugin: HMAC-SHA-256 with invalid length received.");
917 BUFFER_READ (pss.hash, sizeof (pss.hash));
919 /* Calculate username length (without null byte) and allocate memory */
920 username_len = pss_head_length - PART_SIGNATURE_SHA256_SIZE;
921 pss.username = malloc (username_len + 1);
922 if (pss.username == NULL)
925 /* Read the username */
926 BUFFER_READ (pss.username, username_len);
927 pss.username[username_len] = 0;
929 assert (buffer_offset == pss_head_length);
931 /* Query the password */
932 secret = fbh_get (se->data.server.userdb, pss.username);
935 ERROR ("network plugin: Unknown user: %s", pss.username);
936 sfree (pss.username);
940 /* Create a hash device and check the HMAC */
942 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
945 ERROR ("network plugin: Creating HMAC-SHA-256 object failed: %s",
946 gcry_strerror (err));
948 sfree (pss.username);
952 err = gcry_md_setkey (hd, secret, strlen (secret));
955 ERROR ("network plugin: gcry_md_setkey failed: %s", gcry_strerror (err));
961 buffer + PART_SIGNATURE_SHA256_SIZE,
962 buffer_len - PART_SIGNATURE_SHA256_SIZE);
963 hash_ptr = gcry_md_read (hd, GCRY_MD_SHA256);
964 if (hash_ptr == NULL)
966 ERROR ("network plugin: gcry_md_read failed.");
969 sfree (pss.username);
972 memcpy (hash, hash_ptr, sizeof (hash));
979 sfree (pss.username);
981 if (memcmp (pss.hash, hash, sizeof (pss.hash)) != 0)
983 WARNING ("network plugin: Verifying HMAC-SHA-256 signature failed: "
988 parse_packet (se, buffer + buffer_offset, buffer_len - buffer_offset,
992 *ret_buffer = buffer + buffer_len;
996 } /* }}} int parse_part_sign_sha256 */
997 /* #endif HAVE_LIBGCRYPT */
999 #else /* if !HAVE_LIBGCRYPT */
1000 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
1001 void **ret_buffer, size_t *ret_buffer_size, int flags)
1003 static int warning_has_been_printed = 0;
1007 size_t buffer_offset;
1010 part_signature_sha256_t pss;
1012 buffer = *ret_buffer;
1013 buffer_size = *ret_buffer_size;
1016 if (buffer_size <= PART_SIGNATURE_SHA256_SIZE)
1019 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
1020 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
1021 part_len = ntohs (pss.head.length);
1023 if ((part_len <= PART_SIGNATURE_SHA256_SIZE)
1024 || (part_len > buffer_size))
1027 if (warning_has_been_printed == 0)
1029 WARNING ("network plugin: Received signed packet, but the network "
1030 "plugin was not linked with libgcrypt, so I cannot "
1031 "verify the signature. The packet will be accepted.");
1032 warning_has_been_printed = 1;
1035 parse_packet (se, buffer + part_len, buffer_size - part_len, flags);
1037 *ret_buffer = buffer + buffer_size;
1038 *ret_buffer_size = 0;
1041 } /* }}} int parse_part_sign_sha256 */
1042 #endif /* !HAVE_LIBGCRYPT */
1045 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1046 void **ret_buffer, size_t *ret_buffer_len,
1049 char *buffer = *ret_buffer;
1050 size_t buffer_len = *ret_buffer_len;
1053 size_t buffer_offset;
1054 uint16_t username_len;
1055 part_encryption_aes256_t pea;
1056 unsigned char hash[sizeof (pea.hash)];
1058 gcry_cipher_hd_t cypher;
1061 /* Make sure at least the header if available. */
1062 if (buffer_len <= PART_ENCRYPTION_AES256_SIZE)
1064 NOTICE ("network plugin: parse_part_encr_aes256: "
1065 "Discarding short packet.");
1071 /* Copy the unencrypted information into `pea'. */
1072 BUFFER_READ (&pea.head.type, sizeof (pea.head.type));
1073 BUFFER_READ (&pea.head.length, sizeof (pea.head.length));
1075 /* Check the `part size'. */
1076 part_size = ntohs (pea.head.length);
1077 if ((part_size <= PART_ENCRYPTION_AES256_SIZE)
1078 || (part_size > buffer_len))
1080 NOTICE ("network plugin: parse_part_encr_aes256: "
1081 "Discarding part with invalid size.");
1085 /* Read the username */
1086 BUFFER_READ (&username_len, sizeof (username_len));
1087 username_len = ntohs (username_len);
1089 if ((username_len <= 0)
1090 || (username_len > (part_size - (PART_ENCRYPTION_AES256_SIZE + 1))))
1092 NOTICE ("network plugin: parse_part_encr_aes256: "
1093 "Discarding part with invalid username length.");
1097 assert (username_len > 0);
1098 pea.username = malloc (username_len + 1);
1099 if (pea.username == NULL)
1101 BUFFER_READ (pea.username, username_len);
1102 pea.username[username_len] = 0;
1104 /* Last but not least, the initialization vector */
1105 BUFFER_READ (pea.iv, sizeof (pea.iv));
1107 /* Make sure we are at the right position */
1108 assert (buffer_offset == (username_len +
1109 PART_ENCRYPTION_AES256_SIZE - sizeof (pea.hash)));
1111 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
1116 payload_len = part_size - (PART_ENCRYPTION_AES256_SIZE + username_len);
1117 assert (payload_len > 0);
1119 /* Decrypt the packet in-place */
1120 err = gcry_cipher_decrypt (cypher,
1121 buffer + buffer_offset,
1122 part_size - buffer_offset,
1123 /* in = */ NULL, /* in len = */ 0);
1126 ERROR ("network plugin: gcry_cipher_decrypt returned: %s",
1127 gcry_strerror (err));
1132 BUFFER_READ (pea.hash, sizeof (pea.hash));
1134 /* Make sure we're at the right position - again */
1135 assert (buffer_offset == (username_len + PART_ENCRYPTION_AES256_SIZE));
1136 assert (buffer_offset == (part_size - payload_len));
1138 /* Check hash sum */
1139 memset (hash, 0, sizeof (hash));
1140 gcry_md_hash_buffer (GCRY_MD_SHA1, hash,
1141 buffer + buffer_offset, payload_len);
1142 if (memcmp (hash, pea.hash, sizeof (hash)) != 0)
1144 ERROR ("network plugin: Decryption failed: Checksum mismatch.");
1148 parse_packet (se, buffer + buffer_offset, payload_len,
1149 flags | PP_ENCRYPTED);
1151 /* Update return values */
1152 *ret_buffer = buffer + part_size;
1153 *ret_buffer_len = buffer_len - part_size;
1156 } /* }}} int parse_part_encr_aes256 */
1157 /* #endif HAVE_LIBGCRYPT */
1159 #else /* if !HAVE_LIBGCRYPT */
1160 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1161 void **ret_buffer, size_t *ret_buffer_size, int flags)
1163 static int warning_has_been_printed = 0;
1167 size_t buffer_offset;
1172 buffer = *ret_buffer;
1173 buffer_size = *ret_buffer_size;
1176 /* parse_packet assures this minimum size. */
1177 assert (buffer_size >= (sizeof (ph.type) + sizeof (ph.length)));
1179 BUFFER_READ (&ph.type, sizeof (ph.type));
1180 BUFFER_READ (&ph.length, sizeof (ph.length));
1181 ph_length = ntohs (ph.length);
1183 if ((ph_length <= PART_ENCRYPTION_AES256_SIZE)
1184 || (ph_length > buffer_size))
1186 ERROR ("network plugin: AES-256 encrypted part "
1187 "with invalid length received.");
1191 if (warning_has_been_printed == 0)
1193 WARNING ("network plugin: Received encrypted packet, but the network "
1194 "plugin was not linked with libgcrypt, so I cannot "
1195 "decrypt it. The part will be discarded.");
1196 warning_has_been_printed = 1;
1199 *ret_buffer += ph_length;
1200 *ret_buffer_size -= ph_length;
1203 } /* }}} int parse_part_encr_aes256 */
1204 #endif /* !HAVE_LIBGCRYPT */
1208 static int parse_packet (sockent_t *se, /* {{{ */
1209 void *buffer, size_t buffer_size, int flags)
1213 value_list_t vl = VALUE_LIST_INIT;
1217 int packet_was_signed = (flags & PP_SIGNED);
1218 int packet_was_encrypted = (flags & PP_ENCRYPTED);
1219 int printed_ignore_warning = 0;
1220 #endif /* HAVE_LIBGCRYPT */
1223 memset (&vl, '\0', sizeof (vl));
1224 memset (&n, '\0', sizeof (n));
1227 while ((status == 0) && (0 < buffer_size)
1228 && ((unsigned int) buffer_size > sizeof (part_header_t)))
1230 uint16_t pkg_length;
1233 memcpy ((void *) &pkg_type,
1236 memcpy ((void *) &pkg_length,
1237 (void *) (buffer + sizeof (pkg_type)),
1238 sizeof (pkg_length));
1240 pkg_length = ntohs (pkg_length);
1241 pkg_type = ntohs (pkg_type);
1243 if (pkg_length > buffer_size)
1245 /* Ensure that this loop terminates eventually */
1246 if (pkg_length < (2 * sizeof (uint16_t)))
1249 if (pkg_type == TYPE_ENCR_AES256)
1251 status = parse_part_encr_aes256 (se,
1252 &buffer, &buffer_size, flags);
1255 ERROR ("network plugin: Decrypting AES256 "
1257 "with status %i.", status);
1262 else if ((se->data.server.security_level == SECURITY_LEVEL_ENCRYPT)
1263 && (packet_was_encrypted == 0))
1265 if (printed_ignore_warning == 0)
1267 INFO ("network plugin: Unencrypted packet or "
1268 "part has been ignored.");
1269 printed_ignore_warning = 1;
1271 buffer = ((char *) buffer) + pkg_length;
1274 #endif /* HAVE_LIBGCRYPT */
1275 else if (pkg_type == TYPE_SIGN_SHA256)
1277 status = parse_part_sign_sha256 (se,
1278 &buffer, &buffer_size, flags);
1281 ERROR ("network plugin: Verifying HMAC-SHA-256 "
1283 "with status %i.", status);
1288 else if ((se->data.server.security_level == SECURITY_LEVEL_SIGN)
1289 && (packet_was_encrypted == 0)
1290 && (packet_was_signed == 0))
1292 if (printed_ignore_warning == 0)
1294 INFO ("network plugin: Unsigned packet or "
1295 "part has been ignored.");
1296 printed_ignore_warning = 1;
1298 buffer = ((char *) buffer) + pkg_length;
1301 #endif /* HAVE_LIBGCRYPT */
1302 else if (pkg_type == TYPE_VALUES)
1304 status = parse_part_values (&buffer, &buffer_size,
1305 &vl.values, &vl.values_len);
1311 && (strlen (vl.host) > 0)
1312 && (strlen (vl.plugin) > 0)
1313 && (strlen (vl.type) > 0)
1314 && (cache_check (&vl) == 0))
1316 plugin_dispatch_values (&vl);
1320 DEBUG ("network plugin: parse_packet:"
1321 " NOT dispatching values");
1326 else if (pkg_type == TYPE_TIME)
1329 status = parse_part_number (&buffer, &buffer_size,
1333 vl.time = (time_t) tmp;
1334 n.time = (time_t) tmp;
1337 else if (pkg_type == TYPE_INTERVAL)
1340 status = parse_part_number (&buffer, &buffer_size,
1343 vl.interval = (int) tmp;
1345 else if (pkg_type == TYPE_HOST)
1347 status = parse_part_string (&buffer, &buffer_size,
1348 vl.host, sizeof (vl.host));
1350 sstrncpy (n.host, vl.host, sizeof (n.host));
1352 else if (pkg_type == TYPE_PLUGIN)
1354 status = parse_part_string (&buffer, &buffer_size,
1355 vl.plugin, sizeof (vl.plugin));
1357 sstrncpy (n.plugin, vl.plugin,
1360 else if (pkg_type == TYPE_PLUGIN_INSTANCE)
1362 status = parse_part_string (&buffer, &buffer_size,
1364 sizeof (vl.plugin_instance));
1366 sstrncpy (n.plugin_instance,
1368 sizeof (n.plugin_instance));
1370 else if (pkg_type == TYPE_TYPE)
1372 status = parse_part_string (&buffer, &buffer_size,
1373 vl.type, sizeof (vl.type));
1375 sstrncpy (n.type, vl.type, sizeof (n.type));
1377 else if (pkg_type == TYPE_TYPE_INSTANCE)
1379 status = parse_part_string (&buffer, &buffer_size,
1381 sizeof (vl.type_instance));
1383 sstrncpy (n.type_instance, vl.type_instance,
1384 sizeof (n.type_instance));
1386 else if (pkg_type == TYPE_MESSAGE)
1388 status = parse_part_string (&buffer, &buffer_size,
1389 n.message, sizeof (n.message));
1395 else if ((n.severity != NOTIF_FAILURE)
1396 && (n.severity != NOTIF_WARNING)
1397 && (n.severity != NOTIF_OKAY))
1399 INFO ("network plugin: "
1400 "Ignoring notification with "
1401 "unknown severity %i.",
1404 else if (n.time <= 0)
1406 INFO ("network plugin: "
1407 "Ignoring notification with "
1410 else if (strlen (n.message) <= 0)
1412 INFO ("network plugin: "
1413 "Ignoring notification with "
1414 "an empty message.");
1418 plugin_dispatch_notification (&n);
1421 else if (pkg_type == TYPE_SEVERITY)
1424 status = parse_part_number (&buffer, &buffer_size,
1427 n.severity = (int) tmp;
1431 DEBUG ("network plugin: parse_packet: Unknown part"
1432 " type: 0x%04hx", pkg_type);
1433 buffer = ((char *) buffer) + pkg_length;
1435 } /* while (buffer_size > sizeof (part_header_t)) */
1438 } /* }}} int parse_packet */
1440 static void free_sockent_client (struct sockent_client *sec) /* {{{ */
1449 sfree (sec->username);
1450 sfree (sec->password);
1451 if (sec->cypher != NULL)
1452 gcry_cipher_close (sec->cypher);
1454 } /* }}} void free_sockent_client */
1456 static void free_sockent_server (struct sockent_server *ses) /* {{{ */
1460 for (i = 0; i < ses->fd_num; i++)
1462 if (ses->fd[i] >= 0)
1471 sfree (ses->auth_file);
1472 fbh_destroy (ses->userdb);
1473 if (ses->cypher != NULL)
1474 gcry_cipher_close (ses->cypher);
1476 } /* }}} void free_sockent_server */
1478 static void sockent_destroy (sockent_t *se) /* {{{ */
1482 DEBUG ("network plugin: sockent_destroy (se = %p);", (void *) se);
1489 sfree (se->service);
1491 if (se->type == SOCKENT_TYPE_CLIENT)
1492 free_sockent_client (&se->data.client);
1494 free_sockent_server (&se->data.server);
1499 } /* }}} void sockent_destroy */
1502 * int network_set_ttl
1504 * Set the `IP_MULTICAST_TTL', `IP_TTL', `IPV6_MULTICAST_HOPS' or
1505 * `IPV6_UNICAST_HOPS', depending on which option is applicable.
1507 * The `struct addrinfo' is used to destinguish between unicast and multicast
1510 static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
1512 DEBUG ("network plugin: network_set_ttl: network_config_ttl = %i;",
1513 network_config_ttl);
1515 assert (se->type == SOCKENT_TYPE_CLIENT);
1517 if ((network_config_ttl < 1) || (network_config_ttl > 255))
1520 if (ai->ai_family == AF_INET)
1522 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1525 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1526 optname = IP_MULTICAST_TTL;
1530 if (setsockopt (se->data.client.fd, IPPROTO_IP, optname,
1531 &network_config_ttl,
1532 sizeof (network_config_ttl)) == -1)
1535 ERROR ("setsockopt: %s",
1536 sstrerror (errno, errbuf, sizeof (errbuf)));
1540 else if (ai->ai_family == AF_INET6)
1542 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1543 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1546 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1547 optname = IPV6_MULTICAST_HOPS;
1549 optname = IPV6_UNICAST_HOPS;
1551 if (setsockopt (se->data.client.fd, IPPROTO_IPV6, optname,
1552 &network_config_ttl,
1553 sizeof (network_config_ttl)) == -1)
1556 ERROR ("setsockopt: %s",
1557 sstrerror (errno, errbuf,
1564 } /* int network_set_ttl */
1566 static int network_bind_socket (int fd, const struct addrinfo *ai)
1571 /* allow multiple sockets to use the same PORT number */
1572 if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR,
1573 &yes, sizeof(yes)) == -1) {
1575 ERROR ("setsockopt: %s",
1576 sstrerror (errno, errbuf, sizeof (errbuf)));
1580 DEBUG ("fd = %i; calling `bind'", fd);
1582 if (bind (fd, ai->ai_addr, ai->ai_addrlen) == -1)
1586 sstrerror (errno, errbuf, sizeof (errbuf)));
1590 if (ai->ai_family == AF_INET)
1592 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1593 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1595 struct ip_mreq mreq;
1597 DEBUG ("fd = %i; IPv4 multicast address found", fd);
1599 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1600 mreq.imr_interface.s_addr = htonl (INADDR_ANY);
1602 if (setsockopt (fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1603 &loop, sizeof (loop)) == -1)
1606 ERROR ("setsockopt: %s",
1607 sstrerror (errno, errbuf,
1612 if (setsockopt (fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1613 &mreq, sizeof (mreq)) == -1)
1616 ERROR ("setsockopt: %s",
1617 sstrerror (errno, errbuf,
1623 else if (ai->ai_family == AF_INET6)
1625 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1626 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1627 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1629 struct ipv6_mreq mreq;
1631 DEBUG ("fd = %i; IPv6 multicast address found", fd);
1633 memcpy (&mreq.ipv6mr_multiaddr,
1635 sizeof (addr->sin6_addr));
1637 /* http://developer.apple.com/documentation/Darwin/Reference/ManPages/man4/ip6.4.html
1638 * ipv6mr_interface may be set to zeroes to
1639 * choose the default multicast interface or to
1640 * the index of a particular multicast-capable
1641 * interface if the host is multihomed.
1642 * Membership is associ-associated with a
1643 * single interface; programs running on
1644 * multihomed hosts may need to join the same
1645 * group on more than one interface.*/
1646 mreq.ipv6mr_interface = 0;
1648 if (setsockopt (fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
1649 &loop, sizeof (loop)) == -1)
1652 ERROR ("setsockopt: %s",
1653 sstrerror (errno, errbuf,
1658 if (setsockopt (fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
1659 &mreq, sizeof (mreq)) == -1)
1662 ERROR ("setsockopt: %s",
1663 sstrerror (errno, errbuf,
1671 } /* int network_bind_socket */
1673 /* Initialize a sockent structure. `type' must be either `SOCKENT_TYPE_CLIENT'
1674 * or `SOCKENT_TYPE_SERVER' */
1675 static int sockent_init (sockent_t *se, int type) /* {{{ */
1680 memset (se, 0, sizeof (*se));
1682 se->type = SOCKENT_TYPE_CLIENT;
1687 if (type == SOCKENT_TYPE_SERVER)
1689 se->type = SOCKENT_TYPE_SERVER;
1690 se->data.server.fd = NULL;
1692 se->data.server.security_level = SECURITY_LEVEL_NONE;
1693 se->data.server.auth_file = NULL;
1694 se->data.server.userdb = NULL;
1695 se->data.server.cypher = NULL;
1700 se->data.client.fd = -1;
1701 se->data.client.addr = NULL;
1703 se->data.client.security_level = SECURITY_LEVEL_NONE;
1704 se->data.client.username = NULL;
1705 se->data.client.password = NULL;
1706 se->data.client.cypher = NULL;
1711 } /* }}} int sockent_init */
1713 /* Open the file descriptors for a initialized sockent structure. */
1714 static int sockent_open (sockent_t *se) /* {{{ */
1716 struct addrinfo ai_hints;
1717 struct addrinfo *ai_list, *ai_ptr;
1721 const char *service;
1726 /* Set up the security structures. */
1727 #if HAVE_LIBGCRYPT /* {{{ */
1728 if (se->type == SOCKENT_TYPE_CLIENT)
1730 if (se->data.client.security_level > SECURITY_LEVEL_NONE)
1732 if ((se->data.client.username == NULL)
1733 || (se->data.client.password == NULL))
1735 ERROR ("network plugin: Client socket with "
1736 "security requested, but no "
1737 "credentials are configured.");
1740 gcry_md_hash_buffer (GCRY_MD_SHA256,
1741 se->data.client.password_hash,
1742 se->data.client.password,
1743 strlen (se->data.client.password));
1746 else /* (se->type == SOCKENT_TYPE_SERVER) */
1748 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
1750 if (se->data.server.auth_file == NULL)
1752 ERROR ("network plugin: Server socket with "
1753 "security requested, but no "
1754 "password file is configured.");
1758 if (se->data.server.auth_file != NULL)
1760 se->data.server.userdb = fbh_create (se->data.server.auth_file);
1761 if (se->data.server.userdb == NULL)
1763 ERROR ("network plugin: Reading password file "
1765 se->data.server.auth_file);
1766 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
1771 #endif /* }}} HAVE_LIBGCRYPT */
1774 service = se->service;
1776 if (service == NULL)
1777 service = NET_DEFAULT_PORT;
1779 DEBUG ("network plugin: sockent_open: node = %s; service = %s;",
1782 memset (&ai_hints, 0, sizeof (ai_hints));
1783 ai_hints.ai_flags = 0;
1785 ai_hints.ai_flags |= AI_PASSIVE;
1787 #ifdef AI_ADDRCONFIG
1788 ai_hints.ai_flags |= AI_ADDRCONFIG;
1790 ai_hints.ai_family = AF_UNSPEC;
1791 ai_hints.ai_socktype = SOCK_DGRAM;
1792 ai_hints.ai_protocol = IPPROTO_UDP;
1794 ai_return = getaddrinfo (node, service, &ai_hints, &ai_list);
1797 ERROR ("network plugin: getaddrinfo (%s, %s) failed: %s",
1798 (se->node == NULL) ? "(null)" : se->node,
1799 (se->service == NULL) ? "(null)" : se->service,
1800 gai_strerror (ai_return));
1804 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1808 if (se->type == SOCKENT_TYPE_SERVER) /* {{{ */
1812 tmp = realloc (se->data.server.fd,
1813 sizeof (*tmp) * (se->data.server.fd_num + 1));
1816 ERROR ("network plugin: realloc failed.");
1819 se->data.server.fd = tmp;
1820 tmp = se->data.server.fd + se->data.server.fd_num;
1822 *tmp = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
1823 ai_ptr->ai_protocol);
1827 ERROR ("network plugin: socket(2) failed: %s",
1828 sstrerror (errno, errbuf,
1833 status = network_bind_socket (*tmp, ai_ptr);
1841 se->data.server.fd_num++;
1843 } /* }}} if (se->type == SOCKENT_TYPE_SERVER) */
1844 else /* if (se->type == SOCKENT_TYPE_CLIENT) {{{ */
1846 se->data.client.fd = socket (ai_ptr->ai_family,
1847 ai_ptr->ai_socktype,
1848 ai_ptr->ai_protocol);
1849 if (se->data.client.fd < 0)
1852 ERROR ("network plugin: socket(2) failed: %s",
1853 sstrerror (errno, errbuf,
1858 se->data.client.addr = malloc (sizeof (*se->data.client.addr));
1859 if (se->data.client.addr == NULL)
1861 ERROR ("network plugin: malloc failed.");
1862 close (se->data.client.fd);
1863 se->data.client.fd = -1;
1867 memset (se->data.client.addr, 0, sizeof (*se->data.client.addr));
1868 assert (sizeof (*se->data.client.addr) >= ai_ptr->ai_addrlen);
1869 memcpy (se->data.client.addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1870 se->data.client.addrlen = ai_ptr->ai_addrlen;
1872 network_set_ttl (se, ai_ptr);
1874 /* We don't open more than one write-socket per
1875 * node/service pair.. */
1877 } /* }}} if (se->type == SOCKENT_TYPE_CLIENT) */
1878 } /* for (ai_list) */
1880 freeaddrinfo (ai_list);
1882 /* Check if all went well. */
1883 if (se->type == SOCKENT_TYPE_SERVER)
1885 if (se->data.server.fd_num <= 0)
1888 else /* if (se->type == SOCKENT_TYPE_CLIENT) */
1890 if (se->data.client.fd < 0)
1895 } /* }}} int sockent_open */
1897 /* Add a sockent to the global list of sockets */
1898 static int sockent_add (sockent_t *se) /* {{{ */
1900 sockent_t *last_ptr;
1905 if (se->type == SOCKENT_TYPE_SERVER)
1910 tmp = realloc (listen_sockets_pollfd,
1911 sizeof (*tmp) * (listen_sockets_num
1912 + se->data.server.fd_num));
1915 ERROR ("network plugin: realloc failed.");
1918 listen_sockets_pollfd = tmp;
1919 tmp = listen_sockets_pollfd + listen_sockets_num;
1921 for (i = 0; i < se->data.server.fd_num; i++)
1923 memset (tmp + i, 0, sizeof (*tmp));
1924 tmp[i].fd = se->data.server.fd[i];
1925 tmp[i].events = POLLIN | POLLPRI;
1929 listen_sockets_num += se->data.server.fd_num;
1931 if (listen_sockets == NULL)
1933 listen_sockets = se;
1936 last_ptr = listen_sockets;
1938 else /* if (se->type == SOCKENT_TYPE_CLIENT) */
1940 if (sending_sockets == NULL)
1942 sending_sockets = se;
1945 last_ptr = sending_sockets;
1948 while (last_ptr->next != NULL)
1949 last_ptr = last_ptr->next;
1950 last_ptr->next = se;
1953 } /* }}} int sockent_add */
1955 static void *dispatch_thread (void __attribute__((unused)) *arg) /* {{{ */
1959 receive_list_entry_t *ent;
1962 /* Lock and wait for more data to come in */
1963 pthread_mutex_lock (&receive_list_lock);
1964 while ((listen_loop == 0)
1965 && (receive_list_head == NULL))
1966 pthread_cond_wait (&receive_list_cond, &receive_list_lock);
1968 /* Remove the head entry and unlock */
1969 ent = receive_list_head;
1971 receive_list_head = ent->next;
1972 pthread_mutex_unlock (&receive_list_lock);
1974 /* Check whether we are supposed to exit. We do NOT check `listen_loop'
1975 * because we dispatch all missing packets before shutting down. */
1979 /* Look for the correct `sockent_t' */
1980 se = listen_sockets;
1985 for (i = 0; i < se->data.server.fd_num; i++)
1986 if (se->data.server.fd[i] == ent->fd)
1989 if (i < se->data.server.fd_num)
1997 ERROR ("network plugin: Got packet from FD %i, but can't "
1998 "find an appropriate socket entry.",
2004 parse_packet (se, ent->data, ent->data_len, /* flags = */ 0);
2009 } /* }}} void *dispatch_thread */
2011 static int network_receive (void) /* {{{ */
2013 char buffer[BUFF_SIZE];
2019 receive_list_entry_t *private_list_head;
2020 receive_list_entry_t *private_list_tail;
2022 assert (listen_sockets_num > 0);
2024 private_list_head = NULL;
2025 private_list_tail = NULL;
2027 while (listen_loop == 0)
2029 status = poll (listen_sockets_pollfd, listen_sockets_num, -1);
2036 ERROR ("poll failed: %s",
2037 sstrerror (errno, errbuf, sizeof (errbuf)));
2041 for (i = 0; (i < listen_sockets_num) && (status > 0); i++)
2043 receive_list_entry_t *ent;
2045 if ((listen_sockets_pollfd[i].revents
2046 & (POLLIN | POLLPRI)) == 0)
2050 buffer_len = recv (listen_sockets_pollfd[i].fd,
2051 buffer, sizeof (buffer),
2056 ERROR ("recv failed: %s",
2057 sstrerror (errno, errbuf,
2062 /* TODO: Possible performance enhancement: Do not free
2063 * these entries in the dispatch thread but put them in
2064 * another list, so we don't have to allocate more and
2065 * more of these structures. */
2066 ent = malloc (sizeof (receive_list_entry_t));
2069 ERROR ("network plugin: malloc failed.");
2072 memset (ent, 0, sizeof (receive_list_entry_t));
2073 ent->fd = listen_sockets_pollfd[i].fd;
2076 /* Hopefully this be optimized out by the compiler. It
2077 * might help prevent stupid bugs in the future though.
2079 assert (sizeof (ent->data) == sizeof (buffer));
2081 memcpy (ent->data, buffer, buffer_len);
2082 ent->data_len = buffer_len;
2084 if (private_list_head == NULL)
2085 private_list_head = ent;
2087 private_list_tail->next = ent;
2088 private_list_tail = ent;
2090 /* Do not block here. Blocking here has led to
2091 * insufficient performance in the past. */
2092 if (pthread_mutex_trylock (&receive_list_lock) == 0)
2094 if (receive_list_head == NULL)
2095 receive_list_head = private_list_head;
2097 receive_list_tail->next = private_list_head;
2098 receive_list_tail = private_list_tail;
2100 private_list_head = NULL;
2101 private_list_tail = NULL;
2103 pthread_cond_signal (&receive_list_cond);
2104 pthread_mutex_unlock (&receive_list_lock);
2106 } /* for (listen_sockets_pollfd) */
2107 } /* while (listen_loop == 0) */
2109 /* Make sure everything is dispatched before exiting. */
2110 if (private_list_head != NULL)
2112 pthread_mutex_lock (&receive_list_lock);
2114 if (receive_list_head == NULL)
2115 receive_list_head = private_list_head;
2117 receive_list_tail->next = private_list_head;
2118 receive_list_tail = private_list_tail;
2120 private_list_head = NULL;
2121 private_list_tail = NULL;
2123 pthread_cond_signal (&receive_list_cond);
2124 pthread_mutex_unlock (&receive_list_lock);
2128 } /* }}} int network_receive */
2130 static void *receive_thread (void __attribute__((unused)) *arg)
2132 return (network_receive () ? (void *) 1 : (void *) 0);
2133 } /* void *receive_thread */
2135 static void network_init_buffer (void)
2137 memset (send_buffer, 0, sizeof (send_buffer));
2138 send_buffer_ptr = send_buffer;
2139 send_buffer_fill = 0;
2141 memset (&send_buffer_vl, 0, sizeof (send_buffer_vl));
2142 } /* int network_init_buffer */
2144 static void networt_send_buffer_plain (const sockent_t *se, /* {{{ */
2145 const char *buffer, size_t buffer_size)
2151 status = sendto (se->data.client.fd, buffer, buffer_size,
2153 (struct sockaddr *) se->data.client.addr,
2154 se->data.client.addrlen);
2160 ERROR ("network plugin: sendto failed: %s",
2161 sstrerror (errno, errbuf,
2168 } /* }}} void networt_send_buffer_plain */
2171 #define BUFFER_ADD(p,s) do { \
2172 memcpy (buffer + buffer_offset, (p), (s)); \
2173 buffer_offset += (s); \
2176 static void networt_send_buffer_signed (const sockent_t *se, /* {{{ */
2177 const char *in_buffer, size_t in_buffer_size)
2179 part_signature_sha256_t ps;
2180 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2181 size_t buffer_offset;
2182 size_t username_len;
2186 unsigned char *hash;
2189 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
2192 ERROR ("network plugin: Creating HMAC object failed: %s",
2193 gcry_strerror (err));
2197 err = gcry_md_setkey (hd, se->data.client.password,
2198 strlen (se->data.client.password));
2201 ERROR ("network plugin: gcry_md_setkey failed: %s",
2202 gcry_strerror (err));
2207 username_len = strlen (se->data.client.username);
2208 if (username_len > (BUFF_SIG_SIZE - PART_SIGNATURE_SHA256_SIZE))
2210 ERROR ("network plugin: Username too long: %s",
2211 se->data.client.username);
2215 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE,
2216 se->data.client.username, username_len);
2217 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE + username_len,
2218 in_buffer, in_buffer_size);
2220 /* Initialize the `ps' structure. */
2221 memset (&ps, 0, sizeof (ps));
2222 ps.head.type = htons (TYPE_SIGN_SHA256);
2223 ps.head.length = htons (PART_SIGNATURE_SHA256_SIZE + username_len);
2225 /* Calculate the hash value. */
2226 gcry_md_write (hd, buffer + PART_SIGNATURE_SHA256_SIZE,
2227 username_len + in_buffer_size);
2228 hash = gcry_md_read (hd, GCRY_MD_SHA256);
2231 ERROR ("network plugin: gcry_md_read failed.");
2235 memcpy (ps.hash, hash, sizeof (ps.hash));
2237 /* Add the header */
2240 BUFFER_ADD (&ps.head.type, sizeof (ps.head.type));
2241 BUFFER_ADD (&ps.head.length, sizeof (ps.head.length));
2242 BUFFER_ADD (ps.hash, sizeof (ps.hash));
2244 assert (buffer_offset == PART_SIGNATURE_SHA256_SIZE);
2249 buffer_offset = PART_SIGNATURE_SHA256_SIZE + username_len + in_buffer_size;
2250 networt_send_buffer_plain (se, buffer, buffer_offset);
2251 } /* }}} void networt_send_buffer_signed */
2253 static void networt_send_buffer_encrypted (sockent_t *se, /* {{{ */
2254 const char *in_buffer, size_t in_buffer_size)
2256 part_encryption_aes256_t pea;
2257 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2259 size_t buffer_offset;
2261 size_t username_len;
2263 gcry_cipher_hd_t cypher;
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));
2283 DEBUG ("network plugin: networt_send_buffer_encrypted: "
2284 "buffer_size = %zu;", buffer_size);
2286 pea.head.length = htons ((uint16_t) (PART_ENCRYPTION_AES256_SIZE
2287 + username_len + in_buffer_size));
2288 pea.username_length = htons ((uint16_t) username_len);
2290 /* Chose a random initialization vector. */
2291 gcry_randomize ((void *) &pea.iv, sizeof (pea.iv), GCRY_STRONG_RANDOM);
2293 /* Create hash of the payload */
2294 gcry_md_hash_buffer (GCRY_MD_SHA1, pea.hash, in_buffer, in_buffer_size);
2296 /* Initialize the buffer */
2298 memset (buffer, 0, sizeof (buffer));
2301 BUFFER_ADD (&pea.head.type, sizeof (pea.head.type));
2302 BUFFER_ADD (&pea.head.length, sizeof (pea.head.length));
2303 BUFFER_ADD (&pea.username_length, sizeof (pea.username_length));
2304 BUFFER_ADD (pea.username, username_len);
2305 BUFFER_ADD (pea.iv, sizeof (pea.iv));
2306 assert (buffer_offset == header_size);
2307 BUFFER_ADD (pea.hash, sizeof (pea.hash));
2308 BUFFER_ADD (in_buffer, in_buffer_size);
2310 assert (buffer_offset == buffer_size);
2312 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
2313 se->data.client.password);
2317 /* Encrypt the buffer in-place */
2318 err = gcry_cipher_encrypt (cypher,
2319 buffer + header_size,
2320 buffer_size - header_size,
2321 /* in = */ NULL, /* in len = */ 0);
2324 ERROR ("network plugin: gcry_cipher_encrypt returned: %s",
2325 gcry_strerror (err));
2329 /* Send it out without further modifications */
2330 networt_send_buffer_plain (se, buffer, buffer_size);
2331 } /* }}} void networt_send_buffer_encrypted */
2333 #endif /* HAVE_LIBGCRYPT */
2335 static void network_send_buffer (char *buffer, size_t buffer_len) /* {{{ */
2339 DEBUG ("network plugin: network_send_buffer: buffer_len = %zu", buffer_len);
2341 for (se = sending_sockets; se != NULL; se = se->next)
2344 if (se->data.client.security_level == SECURITY_LEVEL_ENCRYPT)
2345 networt_send_buffer_encrypted (se, buffer, buffer_len);
2346 else if (se->data.client.security_level == SECURITY_LEVEL_SIGN)
2347 networt_send_buffer_signed (se, buffer, buffer_len);
2348 else /* if (se->data.client.security_level == SECURITY_LEVEL_NONE) */
2349 #endif /* HAVE_LIBGCRYPT */
2350 networt_send_buffer_plain (se, buffer, buffer_len);
2351 } /* for (sending_sockets) */
2352 } /* }}} void network_send_buffer */
2354 static int add_to_buffer (char *buffer, int buffer_size, /* {{{ */
2355 value_list_t *vl_def,
2356 const data_set_t *ds, const value_list_t *vl)
2358 char *buffer_orig = buffer;
2360 if (strcmp (vl_def->host, vl->host) != 0)
2362 if (write_part_string (&buffer, &buffer_size, TYPE_HOST,
2363 vl->host, strlen (vl->host)) != 0)
2365 sstrncpy (vl_def->host, vl->host, sizeof (vl_def->host));
2368 if (vl_def->time != vl->time)
2370 if (write_part_number (&buffer, &buffer_size, TYPE_TIME,
2371 (uint64_t) vl->time))
2373 vl_def->time = vl->time;
2376 if (vl_def->interval != vl->interval)
2378 if (write_part_number (&buffer, &buffer_size, TYPE_INTERVAL,
2379 (uint64_t) vl->interval))
2381 vl_def->interval = vl->interval;
2384 if (strcmp (vl_def->plugin, vl->plugin) != 0)
2386 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN,
2387 vl->plugin, strlen (vl->plugin)) != 0)
2389 sstrncpy (vl_def->plugin, vl->plugin, sizeof (vl_def->plugin));
2392 if (strcmp (vl_def->plugin_instance, vl->plugin_instance) != 0)
2394 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
2395 vl->plugin_instance,
2396 strlen (vl->plugin_instance)) != 0)
2398 sstrncpy (vl_def->plugin_instance, vl->plugin_instance, sizeof (vl_def->plugin_instance));
2401 if (strcmp (vl_def->type, vl->type) != 0)
2403 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
2404 vl->type, strlen (vl->type)) != 0)
2406 sstrncpy (vl_def->type, ds->type, sizeof (vl_def->type));
2409 if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
2411 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE_INSTANCE,
2413 strlen (vl->type_instance)) != 0)
2415 sstrncpy (vl_def->type_instance, vl->type_instance, sizeof (vl_def->type_instance));
2418 if (write_part_values (&buffer, &buffer_size, ds, vl) != 0)
2421 return (buffer - buffer_orig);
2422 } /* }}} int add_to_buffer */
2424 static void flush_buffer (void)
2426 DEBUG ("network plugin: flush_buffer: send_buffer_fill = %i",
2429 network_send_buffer (send_buffer, (size_t) send_buffer_fill);
2430 network_init_buffer ();
2433 static int network_write (const data_set_t *ds, const value_list_t *vl,
2434 user_data_t __attribute__((unused)) *user_data)
2438 /* If the value is already in the cache, we have received it via the
2439 * network. We write it again if forwarding is activated. It's then in
2440 * the cache and should we receive it again we will ignore it. */
2441 status = cache_check (vl);
2442 if ((network_config_forward == 0)
2446 pthread_mutex_lock (&send_buffer_lock);
2448 status = add_to_buffer (send_buffer_ptr,
2449 sizeof (send_buffer) - (send_buffer_fill + BUFF_SIG_SIZE),
2454 /* status == bytes added to the buffer */
2455 send_buffer_fill += status;
2456 send_buffer_ptr += status;
2462 status = add_to_buffer (send_buffer_ptr,
2463 sizeof (send_buffer) - (send_buffer_fill + BUFF_SIG_SIZE),
2469 send_buffer_fill += status;
2470 send_buffer_ptr += status;
2476 ERROR ("network plugin: Unable to append to the "
2477 "buffer for some weird reason");
2479 else if ((sizeof (send_buffer) - send_buffer_fill) < 15)
2484 pthread_mutex_unlock (&send_buffer_lock);
2486 return ((status < 0) ? -1 : 0);
2487 } /* int network_write */
2489 static int network_config_set_boolean (const oconfig_item_t *ci, /* {{{ */
2492 if ((ci->values_num != 1)
2493 || ((ci->values[0].type != OCONFIG_TYPE_BOOLEAN)
2494 && (ci->values[0].type != OCONFIG_TYPE_STRING)))
2496 ERROR ("network plugin: The `%s' config option needs "
2497 "exactly one boolean argument.", ci->key);
2501 if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
2503 if (ci->values[0].value.boolean)
2510 char *str = ci->values[0].value.string;
2512 if ((strcasecmp ("true", str) == 0)
2513 || (strcasecmp ("yes", str) == 0)
2514 || (strcasecmp ("on", str) == 0))
2516 else if ((strcasecmp ("false", str) == 0)
2517 || (strcasecmp ("no", str) == 0)
2518 || (strcasecmp ("off", str) == 0))
2522 ERROR ("network plugin: Cannot parse string value `%s' of the `%s' "
2523 "option as boolean value.",
2530 } /* }}} int network_config_set_boolean */
2532 static int network_config_set_ttl (const oconfig_item_t *ci) /* {{{ */
2535 if ((ci->values_num != 1)
2536 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
2538 WARNING ("network plugin: The `TimeToLive' config option needs exactly "
2539 "one numeric argument.");
2543 tmp = (int) ci->values[0].value.number;
2544 if ((tmp > 0) && (tmp <= 255))
2545 network_config_ttl = tmp;
2548 } /* }}} int network_config_set_ttl */
2551 static int network_config_set_string (const oconfig_item_t *ci, /* {{{ */
2555 if ((ci->values_num != 1)
2556 || (ci->values[0].type != OCONFIG_TYPE_STRING))
2558 WARNING ("network plugin: The `%s' config option needs exactly "
2559 "one string argument.", ci->key);
2563 tmp = strdup (ci->values[0].value.string);
2567 sfree (*ret_string);
2571 } /* }}} int network_config_set_string */
2572 #endif /* HAVE_LIBGCRYPT */
2575 static int network_config_set_security_level (oconfig_item_t *ci, /* {{{ */
2579 if ((ci->values_num != 1)
2580 || (ci->values[0].type != OCONFIG_TYPE_STRING))
2582 WARNING ("network plugin: The `SecurityLevel' config option needs exactly "
2583 "one string argument.");
2587 str = ci->values[0].value.string;
2588 if (strcasecmp ("Encrypt", str) == 0)
2589 *retval = SECURITY_LEVEL_ENCRYPT;
2590 else if (strcasecmp ("Sign", str) == 0)
2591 *retval = SECURITY_LEVEL_SIGN;
2592 else if (strcasecmp ("None", str) == 0)
2593 *retval = SECURITY_LEVEL_NONE;
2596 WARNING ("network plugin: Unknown security level: %s.", str);
2601 } /* }}} int network_config_set_security_level */
2602 #endif /* HAVE_LIBGCRYPT */
2604 static int network_config_add_listen (const oconfig_item_t *ci) /* {{{ */
2610 if ((ci->values_num < 1) || (ci->values_num > 2)
2611 || (ci->values[0].type != OCONFIG_TYPE_STRING)
2612 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
2614 ERROR ("network plugin: The `%s' config option needs "
2615 "one or two string arguments.", ci->key);
2619 se = malloc (sizeof (*se));
2622 ERROR ("network plugin: malloc failed.");
2625 sockent_init (se, SOCKENT_TYPE_SERVER);
2627 se->node = strdup (ci->values[0].value.string);
2628 if (ci->values_num >= 2)
2629 se->service = strdup (ci->values[1].value.string);
2631 for (i = 0; i < ci->children_num; i++)
2633 oconfig_item_t *child = ci->children + i;
2636 if (strcasecmp ("AuthFile", child->key) == 0)
2637 network_config_set_string (child, &se->data.server.auth_file);
2638 else if (strcasecmp ("SecurityLevel", child->key) == 0)
2639 network_config_set_security_level (child,
2640 &se->data.server.security_level);
2642 #endif /* HAVE_LIBGCRYPT */
2644 WARNING ("network plugin: Option `%s' is not allowed here.",
2650 if ((se->data.server.security_level > SECURITY_LEVEL_NONE)
2651 && (se->data.server.auth_file == NULL))
2653 ERROR ("network plugin: A security level higher than `none' was "
2654 "requested, but no AuthFile option was given. Cowardly refusing to "
2655 "open this socket!");
2656 sockent_destroy (se);
2659 #endif /* HAVE_LIBGCRYPT */
2661 status = sockent_open (se);
2664 ERROR ("network plugin: network_config_add_listen: sockent_open failed.");
2665 sockent_destroy (se);
2669 status = sockent_add (se);
2672 ERROR ("network plugin: network_config_add_listen: sockent_add failed.");
2673 sockent_destroy (se);
2678 } /* }}} int network_config_add_listen */
2680 static int network_config_add_server (const oconfig_item_t *ci) /* {{{ */
2686 if ((ci->values_num < 1) || (ci->values_num > 2)
2687 || (ci->values[0].type != OCONFIG_TYPE_STRING)
2688 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
2690 ERROR ("network plugin: The `%s' config option needs "
2691 "one or two string arguments.", ci->key);
2695 se = malloc (sizeof (*se));
2698 ERROR ("network plugin: malloc failed.");
2701 sockent_init (se, SOCKENT_TYPE_CLIENT);
2703 se->node = strdup (ci->values[0].value.string);
2704 if (ci->values_num >= 2)
2705 se->service = strdup (ci->values[1].value.string);
2707 for (i = 0; i < ci->children_num; i++)
2709 oconfig_item_t *child = ci->children + i;
2712 if (strcasecmp ("Username", child->key) == 0)
2713 network_config_set_string (child, &se->data.client.username);
2714 else if (strcasecmp ("Password", child->key) == 0)
2715 network_config_set_string (child, &se->data.client.password);
2716 else if (strcasecmp ("SecurityLevel", child->key) == 0)
2717 network_config_set_security_level (child,
2718 &se->data.client.security_level);
2720 #endif /* HAVE_LIBGCRYPT */
2722 WARNING ("network plugin: Option `%s' is not allowed here.",
2728 if ((se->data.client.security_level > SECURITY_LEVEL_NONE)
2729 && ((se->data.client.username == NULL)
2730 || (se->data.client.password == NULL)))
2732 ERROR ("network plugin: A security level higher than `none' was "
2733 "requested, but no Username or Password option was given. "
2734 "Cowardly refusing to open this socket!");
2735 sockent_destroy (se);
2738 #endif /* HAVE_LIBGCRYPT */
2740 status = sockent_open (se);
2743 ERROR ("network plugin: network_config_add_server: sockent_open failed.");
2744 sockent_destroy (se);
2748 status = sockent_add (se);
2751 ERROR ("network plugin: network_config_add_server: sockent_add failed.");
2752 sockent_destroy (se);
2757 } /* }}} int network_config_add_server */
2759 static int network_config_set_cache_flush (const oconfig_item_t *ci) /* {{{ */
2762 if ((ci->values_num != 1)
2763 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
2765 WARNING ("network plugin: The `CacheFlush' config option needs exactly "
2766 "one numeric argument.");
2770 tmp = (int) ci->values[0].value.number;
2772 cache_flush_interval = tmp;
2775 } /* }}} int network_config_set_cache_flush */
2777 static int network_config (oconfig_item_t *ci) /* {{{ */
2781 for (i = 0; i < ci->children_num; i++)
2783 oconfig_item_t *child = ci->children + i;
2785 if (strcasecmp ("Listen", child->key) == 0)
2786 network_config_add_listen (child);
2787 else if (strcasecmp ("Server", child->key) == 0)
2788 network_config_add_server (child);
2789 else if (strcasecmp ("TimeToLive", child->key) == 0)
2790 network_config_set_ttl (child);
2791 else if (strcasecmp ("Forward", child->key) == 0)
2792 network_config_set_boolean (child, &network_config_forward);
2793 else if (strcasecmp ("CacheFlush", child->key) == 0)
2794 network_config_set_cache_flush (child);
2797 WARNING ("network plugin: Option `%s' is not allowed here.",
2803 } /* }}} int network_config */
2805 static int network_notification (const notification_t *n,
2806 user_data_t __attribute__((unused)) *user_data)
2808 char buffer[BUFF_SIZE];
2809 char *buffer_ptr = buffer;
2810 int buffer_free = sizeof (buffer);
2813 memset (buffer, '\0', sizeof (buffer));
2816 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_TIME,
2817 (uint64_t) n->time);
2821 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_SEVERITY,
2822 (uint64_t) n->severity);
2826 if (strlen (n->host) > 0)
2828 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_HOST,
2829 n->host, strlen (n->host));
2834 if (strlen (n->plugin) > 0)
2836 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_PLUGIN,
2837 n->plugin, strlen (n->plugin));
2842 if (strlen (n->plugin_instance) > 0)
2844 status = write_part_string (&buffer_ptr, &buffer_free,
2845 TYPE_PLUGIN_INSTANCE,
2846 n->plugin_instance, strlen (n->plugin_instance));
2851 if (strlen (n->type) > 0)
2853 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE,
2854 n->type, strlen (n->type));
2859 if (strlen (n->type_instance) > 0)
2861 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE_INSTANCE,
2862 n->type_instance, strlen (n->type_instance));
2867 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_MESSAGE,
2868 n->message, strlen (n->message));
2872 network_send_buffer (buffer, sizeof (buffer) - buffer_free);
2875 } /* int network_notification */
2877 static int network_shutdown (void)
2881 /* Kill the listening thread */
2882 if (receive_thread_running != 0)
2884 INFO ("network plugin: Stopping receive thread.");
2885 pthread_kill (receive_thread_id, SIGTERM);
2886 pthread_join (receive_thread_id, NULL /* no return value */);
2887 memset (&receive_thread_id, 0, sizeof (receive_thread_id));
2888 receive_thread_running = 0;
2891 /* Shutdown the dispatching thread */
2892 if (dispatch_thread_running != 0)
2894 INFO ("network plugin: Stopping dispatch thread.");
2895 pthread_mutex_lock (&receive_list_lock);
2896 pthread_cond_broadcast (&receive_list_cond);
2897 pthread_mutex_unlock (&receive_list_lock);
2898 pthread_join (dispatch_thread_id, /* ret = */ NULL);
2899 dispatch_thread_running = 0;
2902 sockent_destroy (listen_sockets);
2904 if (send_buffer_fill > 0)
2907 if (cache_tree != NULL)
2912 while (c_avl_pick (cache_tree, &key, &value) == 0)
2917 c_avl_destroy (cache_tree);
2921 /* TODO: Close `sending_sockets' */
2923 plugin_unregister_config ("network");
2924 plugin_unregister_init ("network");
2925 plugin_unregister_write ("network");
2926 plugin_unregister_shutdown ("network");
2928 /* Let the init function do it's move again ;) */
2929 cache_flush_last = 0;
2932 } /* int network_shutdown */
2934 static int network_init (void)
2936 /* Check if we were already initialized. If so, just return - there's
2937 * nothing more to do (for now, that is). */
2938 if (cache_flush_last != 0)
2942 gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
2943 gcry_control (GCRYCTL_INIT_SECMEM, 32768, 0);
2944 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
2947 plugin_register_shutdown ("network", network_shutdown);
2949 network_init_buffer ();
2951 cache_tree = c_avl_create ((int (*) (const void *, const void *)) strcmp);
2952 cache_flush_last = time (NULL);
2954 /* setup socket(s) and so on */
2955 if (sending_sockets != NULL)
2957 plugin_register_write ("network", network_write,
2958 /* user_data = */ NULL);
2959 plugin_register_notification ("network", network_notification,
2960 /* user_data = */ NULL);
2963 /* If no threads need to be started, return here. */
2964 if ((listen_sockets_num == 0)
2965 || ((dispatch_thread_running != 0)
2966 && (receive_thread_running != 0)))
2969 if (dispatch_thread_running == 0)
2972 status = pthread_create (&dispatch_thread_id,
2973 NULL /* no attributes */,
2975 NULL /* no argument */);
2979 ERROR ("network: pthread_create failed: %s",
2980 sstrerror (errno, errbuf,
2985 dispatch_thread_running = 1;
2989 if (receive_thread_running == 0)
2992 status = pthread_create (&receive_thread_id,
2993 NULL /* no attributes */,
2995 NULL /* no argument */);
2999 ERROR ("network: pthread_create failed: %s",
3000 sstrerror (errno, errbuf,
3005 receive_thread_running = 1;
3010 } /* int network_init */
3013 * The flush option of the network plugin cannot flush individual identifiers.
3014 * All the values are added to a buffer and sent when the buffer is full, the
3015 * requested value may or may not be in there, it's not worth finding out. We
3016 * just send the buffer if `flush' is called - if the requested value was in
3017 * there, good. If not, well, then there is nothing to flush.. -octo
3019 static int network_flush (int timeout,
3020 const char __attribute__((unused)) *identifier,
3021 user_data_t __attribute__((unused)) *user_data)
3023 pthread_mutex_lock (&send_buffer_lock);
3025 if (((time (NULL) - cache_flush_last) >= timeout)
3026 && (send_buffer_fill > 0))
3031 pthread_mutex_unlock (&send_buffer_lock);
3034 } /* int network_flush */
3036 void module_register (void)
3038 plugin_register_complex_config ("network", network_config);
3039 plugin_register_init ("network", network_init);
3040 plugin_register_flush ("network", network_flush,
3041 /* user_data = */ NULL);
3042 } /* void module_register */
3044 /* vim: set fdm=marker : */