2 * collectd - src/network.c
3 * Copyright (C) 2005-2009 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
25 #include "configfile.h"
26 #include "utils_avltree.h"
34 # include <sys/socket.h>
40 # include <netinet/in.h>
43 # include <arpa/inet.h>
53 /* 1500 - 40 - 8 = Ethernet packet - IPv6 header - UDP header */
54 /* #define BUFF_SIZE 1452 */
56 #ifndef IPV6_ADD_MEMBERSHIP
57 # ifdef IPV6_JOIN_GROUP
58 # define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
60 # error "Neither IP_ADD_MEMBERSHIP nor IPV6_JOIN_GROUP is defined"
62 #endif /* !IP_ADD_MEMBERSHIP */
64 /* Buffer size to allocate. */
65 #define BUFF_SIZE 1024
68 * Maximum size required for encryption / signing:
70 * Hash/orig length: 32
72 * --------------------
75 #define BUFF_SIG_SIZE 52
80 typedef struct sockent
83 struct sockaddr_storage *addr;
86 #define SECURITY_LEVEL_NONE 0
88 # define SECURITY_LEVEL_SIGN 1
89 # define SECURITY_LEVEL_ENCRYPT 2
92 gcry_cipher_hd_t cypher;
93 #endif /* HAVE_GCRYPT_H */
98 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
99 * 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
100 * +-------+-----------------------+-------------------------------+
101 * ! Ver. ! ! Length !
102 * +-------+-----------------------+-------------------------------+
109 typedef struct part_header_s part_header_t;
111 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
112 * 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
113 * +-------------------------------+-------------------------------+
115 * +-------------------------------+-------------------------------+
116 * : (Length - 4) Bytes :
117 * +---------------------------------------------------------------+
124 typedef struct part_string_s part_string_t;
126 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
127 * 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
128 * +-------------------------------+-------------------------------+
130 * +-------------------------------+-------------------------------+
131 * : (Length - 4 == 2 || 4 || 8) Bytes :
132 * +---------------------------------------------------------------+
139 typedef struct part_number_s part_number_t;
141 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
142 * 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
143 * +-------------------------------+-------------------------------+
145 * +-------------------------------+---------------+---------------+
146 * ! Num of values ! Type0 ! Type1 !
147 * +-------------------------------+---------------+---------------+
150 * +---------------------------------------------------------------+
153 * +---------------------------------------------------------------+
158 uint16_t *num_values;
159 uint8_t *values_types;
162 typedef struct part_values_s part_values_t;
164 struct part_signature_sha256_s
169 typedef struct part_signature_sha256_s part_signature_sha256_t;
171 struct part_encryption_aes256_s
174 uint16_t orig_length;
178 typedef struct part_encryption_aes256_s part_encryption_aes256_t;
180 struct receive_list_entry_s
182 char data[BUFF_SIZE];
185 struct receive_list_entry_s *next;
187 typedef struct receive_list_entry_s receive_list_entry_t;
192 static int network_config_ttl = 0;
193 static int network_config_forward = 0;
195 static sockent_t *sending_sockets = NULL;
197 static receive_list_entry_t *receive_list_head = NULL;
198 static receive_list_entry_t *receive_list_tail = NULL;
199 static pthread_mutex_t receive_list_lock = PTHREAD_MUTEX_INITIALIZER;
200 static pthread_cond_t receive_list_cond = PTHREAD_COND_INITIALIZER;
202 static sockent_t *listen_sockets = NULL;
203 static struct pollfd *listen_sockets_pollfd = NULL;
204 static int listen_sockets_num = 0;
206 /* The receive and dispatch threads will run as long as `listen_loop' is set to
208 static int listen_loop = 0;
209 static int receive_thread_running = 0;
210 static pthread_t receive_thread_id;
211 static int dispatch_thread_running = 0;
212 static pthread_t dispatch_thread_id;
214 /* Buffer in which to-be-sent network packets are constructed. */
215 static char send_buffer[BUFF_SIZE];
216 static char *send_buffer_ptr;
217 static int send_buffer_fill;
218 static value_list_t send_buffer_vl = VALUE_LIST_STATIC;
219 static pthread_mutex_t send_buffer_lock = PTHREAD_MUTEX_INITIALIZER;
221 /* In this cache we store all the values we received, so we can send out only
222 * those values which were *not* received via the network plugin, too. This is
223 * used for the `Forward false' option. */
224 static c_avl_tree_t *cache_tree = NULL;
225 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
226 static time_t cache_flush_last = 0;
227 static int cache_flush_interval = 1800;
232 static int cache_flush (void)
242 c_avl_iterator_t *iter;
244 time_t curtime = time (NULL);
246 iter = c_avl_get_iterator (cache_tree);
247 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
249 if ((curtime - *value) <= cache_flush_interval)
251 tmp = (char **) realloc (keys,
252 (keys_num + 1) * sizeof (char *));
256 c_avl_iterator_destroy (iter);
257 ERROR ("network plugin: cache_flush: realloc"
262 keys[keys_num] = key;
264 } /* while (c_avl_iterator_next) */
265 c_avl_iterator_destroy (iter);
267 for (i = 0; i < keys_num; i++)
269 if (c_avl_remove (cache_tree, keys[i], (void *) &key,
270 (void *) &value) != 0)
272 WARNING ("network plugin: cache_flush: c_avl_remove"
273 " (%s) failed.", keys[i]);
283 DEBUG ("network plugin: cache_flush: Removed %i %s",
284 keys_num, (keys_num == 1) ? "entry" : "entries");
285 cache_flush_last = curtime;
287 } /* int cache_flush */
289 static int cache_check (const value_list_t *vl)
292 time_t *value = NULL;
295 if (cache_tree == NULL)
298 if (format_name (key, sizeof (key), vl->host, vl->plugin,
299 vl->plugin_instance, vl->type, vl->type_instance))
302 pthread_mutex_lock (&cache_lock);
304 if (c_avl_get (cache_tree, key, (void *) &value) == 0)
306 if (*value < vl->time)
313 DEBUG ("network plugin: cache_check: *value = %i >= vl->time = %i",
314 (int) *value, (int) vl->time);
320 char *key_copy = strdup (key);
321 value = malloc (sizeof (time_t));
322 if ((key_copy != NULL) && (value != NULL))
325 c_avl_insert (cache_tree, key_copy, value);
335 if ((time (NULL) - cache_flush_last) > cache_flush_interval)
338 pthread_mutex_unlock (&cache_lock);
341 } /* int cache_check */
343 static int write_part_values (char **ret_buffer, int *ret_buffer_len,
344 const data_set_t *ds, const value_list_t *vl)
350 part_header_t pkg_ph;
351 uint16_t pkg_num_values;
352 uint8_t *pkg_values_types;
358 num_values = vl->values_len;
359 packet_len = sizeof (part_header_t) + sizeof (uint16_t)
360 + (num_values * sizeof (uint8_t))
361 + (num_values * sizeof (value_t));
363 if (*ret_buffer_len < packet_len)
366 pkg_values_types = (uint8_t *) malloc (num_values * sizeof (uint8_t));
367 if (pkg_values_types == NULL)
369 ERROR ("network plugin: write_part_values: malloc failed.");
373 pkg_values = (value_t *) malloc (num_values * sizeof (value_t));
374 if (pkg_values == NULL)
376 free (pkg_values_types);
377 ERROR ("network plugin: write_part_values: malloc failed.");
381 pkg_ph.type = htons (TYPE_VALUES);
382 pkg_ph.length = htons (packet_len);
384 pkg_num_values = htons ((uint16_t) vl->values_len);
386 for (i = 0; i < num_values; i++)
388 if (ds->ds[i].type == DS_TYPE_COUNTER)
390 pkg_values_types[i] = DS_TYPE_COUNTER;
391 pkg_values[i].counter = htonll (vl->values[i].counter);
395 pkg_values_types[i] = DS_TYPE_GAUGE;
396 pkg_values[i].gauge = htond (vl->values[i].gauge);
401 * Use `memcpy' to write everything to the buffer, because the pointer
402 * may be unaligned and some architectures, such as SPARC, can't handle
405 packet_ptr = *ret_buffer;
407 memcpy (packet_ptr + offset, &pkg_ph, sizeof (pkg_ph));
408 offset += sizeof (pkg_ph);
409 memcpy (packet_ptr + offset, &pkg_num_values, sizeof (pkg_num_values));
410 offset += sizeof (pkg_num_values);
411 memcpy (packet_ptr + offset, pkg_values_types, num_values * sizeof (uint8_t));
412 offset += num_values * sizeof (uint8_t);
413 memcpy (packet_ptr + offset, pkg_values, num_values * sizeof (value_t));
414 offset += num_values * sizeof (value_t);
416 assert (offset == packet_len);
418 *ret_buffer = packet_ptr + packet_len;
419 *ret_buffer_len -= packet_len;
421 free (pkg_values_types);
425 } /* int write_part_values */
427 static int write_part_number (char **ret_buffer, int *ret_buffer_len,
428 int type, uint64_t value)
433 part_header_t pkg_head;
438 packet_len = sizeof (pkg_head) + sizeof (pkg_value);
440 if (*ret_buffer_len < packet_len)
443 pkg_head.type = htons (type);
444 pkg_head.length = htons (packet_len);
445 pkg_value = htonll (value);
447 packet_ptr = *ret_buffer;
449 memcpy (packet_ptr + offset, &pkg_head, sizeof (pkg_head));
450 offset += sizeof (pkg_head);
451 memcpy (packet_ptr + offset, &pkg_value, sizeof (pkg_value));
452 offset += sizeof (pkg_value);
454 assert (offset == packet_len);
456 *ret_buffer = packet_ptr + packet_len;
457 *ret_buffer_len -= packet_len;
460 } /* int write_part_number */
462 static int write_part_string (char **ret_buffer, int *ret_buffer_len,
463 int type, const char *str, int str_len)
473 buffer_len = 2 * sizeof (uint16_t) + str_len + 1;
474 if (*ret_buffer_len < buffer_len)
477 pkg_type = htons (type);
478 pkg_length = htons (buffer_len);
480 buffer = *ret_buffer;
482 memcpy (buffer + offset, (void *) &pkg_type, sizeof (pkg_type));
483 offset += sizeof (pkg_type);
484 memcpy (buffer + offset, (void *) &pkg_length, sizeof (pkg_length));
485 offset += sizeof (pkg_length);
486 memcpy (buffer + offset, str, str_len);
488 memset (buffer + offset, '\0', 1);
491 assert (offset == buffer_len);
493 *ret_buffer = buffer + buffer_len;
494 *ret_buffer_len -= buffer_len;
497 } /* int write_part_string */
499 static int parse_part_values (void **ret_buffer, int *ret_buffer_len,
500 value_t **ret_values, int *ret_num_values)
502 char *buffer = *ret_buffer;
503 int buffer_len = *ret_buffer_len;
516 if (buffer_len < (15))
518 DEBUG ("network plugin: packet is too short: buffer_len = %i",
523 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
524 buffer += sizeof (tmp16);
525 pkg_type = ntohs (tmp16);
527 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
528 buffer += sizeof (tmp16);
529 pkg_length = ntohs (tmp16);
531 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
532 buffer += sizeof (tmp16);
533 pkg_numval = ntohs (tmp16);
535 assert (pkg_type == TYPE_VALUES);
537 exp_size = 3 * sizeof (uint16_t)
538 + pkg_numval * (sizeof (uint8_t) + sizeof (value_t));
539 if ((buffer_len < 0) || ((size_t) buffer_len < exp_size))
541 WARNING ("network plugin: parse_part_values: "
543 "Chunk of size %u expected, "
544 "but buffer has only %i bytes left.",
545 (unsigned int) exp_size, buffer_len);
549 if (pkg_length != exp_size)
551 WARNING ("network plugin: parse_part_values: "
552 "Length and number of values "
553 "in the packet don't match.");
557 pkg_types = (uint8_t *) malloc (pkg_numval * sizeof (uint8_t));
558 pkg_values = (value_t *) malloc (pkg_numval * sizeof (value_t));
559 if ((pkg_types == NULL) || (pkg_values == NULL))
563 ERROR ("network plugin: parse_part_values: malloc failed.");
567 memcpy ((void *) pkg_types, (void *) buffer, pkg_numval * sizeof (uint8_t));
568 buffer += pkg_numval * sizeof (uint8_t);
569 memcpy ((void *) pkg_values, (void *) buffer, pkg_numval * sizeof (value_t));
570 buffer += pkg_numval * sizeof (value_t);
572 for (i = 0; i < pkg_numval; i++)
574 if (pkg_types[i] == DS_TYPE_COUNTER)
575 pkg_values[i].counter = ntohll (pkg_values[i].counter);
576 else if (pkg_types[i] == DS_TYPE_GAUGE)
577 pkg_values[i].gauge = ntohd (pkg_values[i].gauge);
580 *ret_buffer = buffer;
581 *ret_buffer_len = buffer_len - pkg_length;
582 *ret_num_values = pkg_numval;
583 *ret_values = pkg_values;
588 } /* int parse_part_values */
590 static int parse_part_number (void **ret_buffer, int *ret_buffer_len,
593 char *buffer = *ret_buffer;
594 int buffer_len = *ret_buffer_len;
598 size_t exp_size = 2 * sizeof (uint16_t) + sizeof (uint64_t);
603 if ((buffer_len < 0) || ((size_t) buffer_len < exp_size))
605 WARNING ("network plugin: parse_part_number: "
607 "Chunk of size %u expected, "
608 "but buffer has only %i bytes left.",
609 (unsigned int) exp_size, buffer_len);
613 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
614 buffer += sizeof (tmp16);
615 pkg_type = ntohs (tmp16);
617 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
618 buffer += sizeof (tmp16);
619 pkg_length = ntohs (tmp16);
621 memcpy ((void *) &tmp64, buffer, sizeof (tmp64));
622 buffer += sizeof (tmp64);
623 *value = ntohll (tmp64);
625 *ret_buffer = buffer;
626 *ret_buffer_len = buffer_len - pkg_length;
629 } /* int parse_part_number */
631 static int parse_part_string (void **ret_buffer, int *ret_buffer_len,
632 char *output, int output_len)
634 char *buffer = *ret_buffer;
635 int buffer_len = *ret_buffer_len;
638 size_t header_size = 2 * sizeof (uint16_t);
643 if ((buffer_len < 0) || ((size_t) buffer_len < header_size))
645 WARNING ("network plugin: parse_part_string: "
647 "Chunk of at least size %u expected, "
648 "but buffer has only %i bytes left.",
649 (unsigned int) header_size, buffer_len);
653 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
654 buffer += sizeof (tmp16);
655 pkg_type = ntohs (tmp16);
657 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
658 buffer += sizeof (tmp16);
659 pkg_length = ntohs (tmp16);
661 /* Check that packet fits in the input buffer */
662 if (pkg_length > buffer_len)
664 WARNING ("network plugin: parse_part_string: "
666 "Chunk of size %hu received, "
667 "but buffer has only %i bytes left.",
668 pkg_length, buffer_len);
672 /* Check that pkg_length is in the valid range */
673 if (pkg_length <= header_size)
675 WARNING ("network plugin: parse_part_string: "
677 "Header claims this packet is only %hu "
678 "bytes long.", pkg_length);
682 /* Check that the package data fits into the output buffer.
683 * The previous if-statement ensures that:
684 * `pkg_length > header_size' */
686 || ((size_t) output_len < ((size_t) pkg_length - header_size)))
688 WARNING ("network plugin: parse_part_string: "
689 "Output buffer too small.");
693 /* All sanity checks successfull, let's copy the data over */
694 output_len = pkg_length - header_size;
695 memcpy ((void *) output, (void *) buffer, output_len);
696 buffer += output_len;
698 /* For some very weird reason '\0' doesn't do the trick on SPARC in
700 if (output[output_len - 1] != 0)
702 WARNING ("network plugin: parse_part_string: "
703 "Received string does not end "
704 "with a NULL-byte.");
708 *ret_buffer = buffer;
709 *ret_buffer_len = buffer_len - pkg_length;
712 } /* int parse_part_string */
715 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
716 void **ret_buffer, int *ret_buffer_len)
718 char *buffer = *ret_buffer;
719 size_t buffer_len = (size_t) *ret_buffer_len;
721 part_signature_sha256_t ps_received;
722 part_signature_sha256_t ps_expected;
724 if (se->shared_secret == NULL)
726 NOTICE ("network plugin: Received signed network packet but can't verify "
727 "it because no shared secret has been configured. Will accept it.");
731 if (buffer_len < sizeof (ps_received))
734 memcpy (&ps_received, buffer, sizeof (ps_received));
736 memset (&ps_expected, 0, sizeof (ps_expected));
737 ps_expected.head.type = htons (TYPE_SIGN_SHA256);
738 ps_expected.head.length = htons (sizeof (ps_expected));
739 sstrncpy (ps_expected.hash, se->shared_secret, sizeof (ps_expected.hash));
740 memcpy (buffer, &ps_expected, sizeof (ps_expected));
742 gcry_md_hash_buffer (GCRY_MD_SHA256, ps_expected.hash, buffer, buffer_len);
744 *ret_buffer += sizeof (ps_received);
746 if (memcmp (ps_received.hash, ps_expected.hash,
747 sizeof (ps_received.hash)) == 0)
749 else /* hashes do not match. */
751 } /* }}} int parse_part_sign_sha256 */
752 /* #endif HAVE_GCRYPT_H */
754 #else /* if !HAVE_GCRYPT_H */
755 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
756 void **ret_buffer, int *ret_buffer_len)
758 INFO ("network plugin: Received signed packet, but the network "
759 "plugin was not linked with libgcrypt, so I cannot "
760 "verify the signature. The packet will be accepted.");
762 } /* }}} int parse_part_sign_sha256 */
763 #endif /* !HAVE_GCRYPT_H */
766 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
767 void **ret_buffer, int *ret_buffer_len)
769 char *buffer = *ret_buffer;
770 int buffer_len = *ret_buffer_len;
772 part_encryption_aes256_t pea;
776 if (se->cypher == NULL)
778 NOTICE ("network plugin: Unable to decrypt packet, because no cypher "
779 "instance is present.");
783 /* Decrypt the packet in-place */
784 err = gcry_cipher_decrypt (se->cypher,
785 buffer + sizeof (pea.head), buffer_len - sizeof (pea.head),
786 /* in = */ NULL, /* in len = */ 0);
787 gcry_cipher_reset (se->cypher);
790 ERROR ("network plugin: gcry_cipher_decrypt returned: %s",
791 gcry_strerror (err));
795 /* Copy the header information to `pea' */
796 memcpy (&pea, buffer, sizeof (pea));
797 buffer += sizeof (pea);
798 buffer_len -= sizeof (pea);
800 /* Check sanity of the original length */
801 orig_buffer_len = ntohs (pea.orig_length);
802 if (orig_buffer_len > buffer_len)
804 ERROR ("network plugin: Decryption failed: Invalid original length.");
809 memset (hash, 0, sizeof (hash));
810 gcry_md_hash_buffer (GCRY_MD_SHA224, hash, buffer, orig_buffer_len);
812 if (memcmp (hash, pea.hash, sizeof (hash)) != 0)
814 ERROR ("network plugin: Decryption failed: Checksum mismatch.");
818 /* Update return values */
819 *ret_buffer = buffer;
820 *ret_buffer_len = orig_buffer_len;
823 } /* }}} int parse_part_encr_aes256 */
824 /* #endif HAVE_GCRYPT_H */
826 #else /* if !HAVE_GCRYPT_H */
827 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
828 void **ret_buffer, int *ret_buffer_len)
830 INFO ("network plugin: Received encrypted packet, but the network "
831 "plugin was not linked with libgcrypt, so I cannot "
832 "decrypt it. The packet will be discarded.");
834 } /* }}} int parse_part_encr_aes256 */
835 #endif /* !HAVE_GCRYPT_H */
837 static int parse_packet (receive_list_entry_t *rle) /* {{{ */
845 value_list_t vl = VALUE_LIST_INIT;
848 int packet_was_encrypted = 0;
849 int packet_was_signed = 0;
851 int printed_ignore_warning = 0;
852 #endif /* HAVE_GCRYPT_H */
855 buffer_len = rle->data_len;
857 /* Look for the correct `sockent_t' */
859 while ((se != NULL) && (se->fd != rle->fd))
864 ERROR ("network plugin: Got packet from FD %i, but can't "
865 "find an appropriate socket entry.",
870 memset (&vl, '\0', sizeof (vl));
871 memset (&n, '\0', sizeof (n));
874 while ((status == 0) && (0 < buffer_len)
875 && ((unsigned int) buffer_len > sizeof (part_header_t)))
880 memcpy ((void *) &pkg_type,
883 memcpy ((void *) &pkg_length,
884 (void *) (buffer + sizeof (pkg_type)),
885 sizeof (pkg_length));
887 pkg_length = ntohs (pkg_length);
888 pkg_type = ntohs (pkg_type);
890 if (pkg_length > buffer_len)
892 /* Ensure that this loop terminates eventually */
893 if (pkg_length < (2 * sizeof (uint16_t)))
896 if (pkg_type == TYPE_ENCR_AES256)
898 status = parse_part_encr_aes256 (se, &buffer, &buffer_len);
901 ERROR ("network plugin: Decrypting AES256 "
903 "with status %i.", status);
908 packet_was_encrypted = 1;
912 else if ((se->security_level == SECURITY_LEVEL_ENCRYPT)
913 && (packet_was_encrypted == 0))
915 if (printed_ignore_warning == 0)
917 INFO ("network plugin: Unencrypted packet or "
918 "part has been ignored.");
919 printed_ignore_warning = 1;
921 buffer = ((char *) buffer) + pkg_length;
924 #endif /* HAVE_GCRYPT_H */
925 else if (pkg_type == TYPE_SIGN_SHA256)
927 status = parse_part_sign_sha256 (se, &buffer, &buffer_len);
930 ERROR ("network plugin: Verifying SHA-256 "
932 "with status %i.", status);
937 ERROR ("network plugin: Ignoring packet with "
938 "invalid SHA-256 signature.");
943 packet_was_signed = 1;
947 else if ((se->security_level == SECURITY_LEVEL_SIGN)
948 && (packet_was_encrypted == 0)
949 && (packet_was_signed == 0))
951 if (printed_ignore_warning == 0)
953 INFO ("network plugin: Unsigned packet or "
954 "part has been ignored.");
955 printed_ignore_warning = 1;
957 buffer = ((char *) buffer) + pkg_length;
960 #endif /* HAVE_GCRYPT_H */
961 else if (pkg_type == TYPE_VALUES)
963 status = parse_part_values (&buffer, &buffer_len,
964 &vl.values, &vl.values_len);
970 && (strlen (vl.host) > 0)
971 && (strlen (vl.plugin) > 0)
972 && (strlen (vl.type) > 0)
973 && (cache_check (&vl) == 0))
975 plugin_dispatch_values (&vl);
979 DEBUG ("network plugin: parse_packet:"
980 " NOT dispatching values");
985 else if (pkg_type == TYPE_TIME)
988 status = parse_part_number (&buffer, &buffer_len,
992 vl.time = (time_t) tmp;
993 n.time = (time_t) tmp;
996 else if (pkg_type == TYPE_INTERVAL)
999 status = parse_part_number (&buffer, &buffer_len,
1002 vl.interval = (int) tmp;
1004 else if (pkg_type == TYPE_HOST)
1006 status = parse_part_string (&buffer, &buffer_len,
1007 vl.host, sizeof (vl.host));
1009 sstrncpy (n.host, vl.host, sizeof (n.host));
1011 else if (pkg_type == TYPE_PLUGIN)
1013 status = parse_part_string (&buffer, &buffer_len,
1014 vl.plugin, sizeof (vl.plugin));
1016 sstrncpy (n.plugin, vl.plugin,
1019 else if (pkg_type == TYPE_PLUGIN_INSTANCE)
1021 status = parse_part_string (&buffer, &buffer_len,
1023 sizeof (vl.plugin_instance));
1025 sstrncpy (n.plugin_instance,
1027 sizeof (n.plugin_instance));
1029 else if (pkg_type == TYPE_TYPE)
1031 status = parse_part_string (&buffer, &buffer_len,
1032 vl.type, sizeof (vl.type));
1034 sstrncpy (n.type, vl.type, sizeof (n.type));
1036 else if (pkg_type == TYPE_TYPE_INSTANCE)
1038 status = parse_part_string (&buffer, &buffer_len,
1040 sizeof (vl.type_instance));
1042 sstrncpy (n.type_instance, vl.type_instance,
1043 sizeof (n.type_instance));
1045 else if (pkg_type == TYPE_MESSAGE)
1047 status = parse_part_string (&buffer, &buffer_len,
1048 n.message, sizeof (n.message));
1054 else if ((n.severity != NOTIF_FAILURE)
1055 && (n.severity != NOTIF_WARNING)
1056 && (n.severity != NOTIF_OKAY))
1058 INFO ("network plugin: "
1059 "Ignoring notification with "
1060 "unknown severity %i.",
1063 else if (n.time <= 0)
1065 INFO ("network plugin: "
1066 "Ignoring notification with "
1069 else if (strlen (n.message) <= 0)
1071 INFO ("network plugin: "
1072 "Ignoring notification with "
1073 "an empty message.");
1077 plugin_dispatch_notification (&n);
1080 else if (pkg_type == TYPE_SEVERITY)
1083 status = parse_part_number (&buffer, &buffer_len,
1086 n.severity = (int) tmp;
1090 DEBUG ("network plugin: parse_packet: Unknown part"
1091 " type: 0x%04hx", pkg_type);
1092 buffer = ((char *) buffer) + pkg_length;
1094 } /* while (buffer_len > sizeof (part_header_t)) */
1097 } /* }}} int parse_packet */
1099 static void free_sockent (sockent_t *se) /* {{{ */
1107 if (se->cypher != NULL)
1109 gcry_cipher_close (se->cypher);
1112 free (se->shared_secret);
1113 #endif /* HAVE_GCRYPT_H */
1120 } /* }}} void free_sockent */
1123 * int network_set_ttl
1125 * Set the `IP_MULTICAST_TTL', `IP_TTL', `IPV6_MULTICAST_HOPS' or
1126 * `IPV6_UNICAST_HOPS', depending on which option is applicable.
1128 * The `struct addrinfo' is used to destinguish between unicast and multicast
1131 static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
1133 if ((network_config_ttl < 1) || (network_config_ttl > 255))
1136 DEBUG ("ttl = %i", network_config_ttl);
1138 if (ai->ai_family == AF_INET)
1140 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1143 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1144 optname = IP_MULTICAST_TTL;
1148 if (setsockopt (se->fd, IPPROTO_IP, optname,
1149 &network_config_ttl,
1150 sizeof (network_config_ttl)) == -1)
1153 ERROR ("setsockopt: %s",
1154 sstrerror (errno, errbuf, sizeof (errbuf)));
1158 else if (ai->ai_family == AF_INET6)
1160 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1161 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1164 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1165 optname = IPV6_MULTICAST_HOPS;
1167 optname = IPV6_UNICAST_HOPS;
1169 if (setsockopt (se->fd, IPPROTO_IPV6, optname,
1170 &network_config_ttl,
1171 sizeof (network_config_ttl)) == -1)
1174 ERROR ("setsockopt: %s",
1175 sstrerror (errno, errbuf,
1182 } /* int network_set_ttl */
1185 static int network_set_encryption (sockent_t *se, /* {{{ */
1186 const char *shared_secret)
1191 se->shared_secret = sstrdup (shared_secret);
1194 * We use CBC *without* an initialization vector: The cipher is reset after
1195 * each packet and we would have to re-set the IV each time. The first
1196 * encrypted block will contain the SHA-224 checksum anyway, so this should
1197 * be quite unpredictable. Also, there's a 2 byte field in the header that's
1198 * being filled with random numbers. So we only use CBC so the blocks
1199 * *within* one packet are chained.
1201 err = gcry_cipher_open (&se->cypher,
1202 GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CBC, /* flags = */ 0);
1205 ERROR ("network plugin: gcry_cipher_open returned: %s",
1206 gcry_strerror (err));
1210 assert (se->shared_secret != NULL);
1211 gcry_md_hash_buffer (GCRY_MD_SHA256, hash,
1212 se->shared_secret, strlen (se->shared_secret));
1214 err = gcry_cipher_setkey (se->cypher, hash, sizeof (hash));
1217 DEBUG ("network plugin: gcry_cipher_setkey returned: %s",
1218 gcry_strerror (err));
1219 gcry_cipher_close (se->cypher);
1225 } /* }}} int network_set_encryption */
1226 #endif /* HAVE_GCRYPT_H */
1228 static int network_bind_socket (const sockent_t *se, const struct addrinfo *ai)
1233 /* allow multiple sockets to use the same PORT number */
1234 if (setsockopt(se->fd, SOL_SOCKET, SO_REUSEADDR,
1235 &yes, sizeof(yes)) == -1) {
1237 ERROR ("setsockopt: %s",
1238 sstrerror (errno, errbuf, sizeof (errbuf)));
1242 DEBUG ("fd = %i; calling `bind'", se->fd);
1244 if (bind (se->fd, ai->ai_addr, ai->ai_addrlen) == -1)
1248 sstrerror (errno, errbuf, sizeof (errbuf)));
1252 if (ai->ai_family == AF_INET)
1254 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1255 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1257 struct ip_mreq mreq;
1259 DEBUG ("fd = %i; IPv4 multicast address found", se->fd);
1261 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1262 mreq.imr_interface.s_addr = htonl (INADDR_ANY);
1264 if (setsockopt (se->fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1265 &loop, sizeof (loop)) == -1)
1268 ERROR ("setsockopt: %s",
1269 sstrerror (errno, errbuf,
1274 if (setsockopt (se->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1275 &mreq, sizeof (mreq)) == -1)
1278 ERROR ("setsockopt: %s",
1279 sstrerror (errno, errbuf,
1285 else if (ai->ai_family == AF_INET6)
1287 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1288 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1289 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1291 struct ipv6_mreq mreq;
1293 DEBUG ("fd = %i; IPv6 multicast address found", se->fd);
1295 memcpy (&mreq.ipv6mr_multiaddr,
1297 sizeof (addr->sin6_addr));
1299 /* http://developer.apple.com/documentation/Darwin/Reference/ManPages/man4/ip6.4.html
1300 * ipv6mr_interface may be set to zeroes to
1301 * choose the default multicast interface or to
1302 * the index of a particular multicast-capable
1303 * interface if the host is multihomed.
1304 * Membership is associ-associated with a
1305 * single interface; programs running on
1306 * multihomed hosts may need to join the same
1307 * group on more than one interface.*/
1308 mreq.ipv6mr_interface = 0;
1310 if (setsockopt (se->fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
1311 &loop, sizeof (loop)) == -1)
1314 ERROR ("setsockopt: %s",
1315 sstrerror (errno, errbuf,
1320 if (setsockopt (se->fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
1321 &mreq, sizeof (mreq)) == -1)
1324 ERROR ("setsockopt: %s",
1325 sstrerror (errno, errbuf,
1333 } /* int network_bind_socket */
1335 #define CREATE_SOCKET_FLAGS_LISTEN 0x0001
1336 static sockent_t *network_create_socket (const char *node, /* {{{ */
1337 const char *service,
1338 const char *shared_secret,
1342 struct addrinfo ai_hints;
1343 struct addrinfo *ai_list, *ai_ptr;
1346 sockent_t *se_head = NULL;
1347 sockent_t *se_tail = NULL;
1349 DEBUG ("node = %s, service = %s", node, service);
1351 memset (&ai_hints, '\0', sizeof (ai_hints));
1352 ai_hints.ai_flags = 0;
1354 ai_hints.ai_flags |= AI_PASSIVE;
1356 #ifdef AI_ADDRCONFIG
1357 ai_hints.ai_flags |= AI_ADDRCONFIG;
1359 ai_hints.ai_family = AF_UNSPEC;
1360 ai_hints.ai_socktype = SOCK_DGRAM;
1361 ai_hints.ai_protocol = IPPROTO_UDP;
1363 ai_return = getaddrinfo (node, service, &ai_hints, &ai_list);
1367 ERROR ("getaddrinfo (%s, %s): %s",
1368 (node == NULL) ? "(null)" : node,
1369 (service == NULL) ? "(null)" : service,
1370 (ai_return == EAI_SYSTEM)
1371 ? sstrerror (errno, errbuf, sizeof (errbuf))
1372 : gai_strerror (ai_return));
1376 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1381 if ((se = (sockent_t *) malloc (sizeof (sockent_t))) == NULL)
1384 ERROR ("malloc: %s",
1385 sstrerror (errno, errbuf,
1390 if ((se->addr = (struct sockaddr_storage *) malloc (sizeof (struct sockaddr_storage))) == NULL)
1393 ERROR ("malloc: %s",
1394 sstrerror (errno, errbuf,
1400 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
1401 memset (se->addr, '\0', sizeof (struct sockaddr_storage));
1402 memcpy (se->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1403 se->addrlen = ai_ptr->ai_addrlen;
1405 se->fd = socket (ai_ptr->ai_family,
1406 ai_ptr->ai_socktype,
1407 ai_ptr->ai_protocol);
1413 ERROR ("socket: %s",
1414 sstrerror (errno, errbuf,
1421 if ((flags & CREATE_SOCKET_FLAGS_LISTEN) != 0)
1423 status = network_bind_socket (se, ai_ptr);
1432 else /* sending socket */
1434 network_set_ttl (se, ai_ptr);
1438 se->security_level = security_level;
1439 se->shared_secret = NULL;
1441 if (shared_secret != NULL)
1443 status = network_set_encryption (se, shared_secret);
1444 if ((status != 0) && (security_level <= SECURITY_LEVEL_SIGN))
1446 WARNING ("network plugin: Starting cryptograp"
1447 "hic subsystem failed. Since "
1448 "security level `Sign' or "
1449 "`None' is configured I will "
1452 else if (status != 0)
1454 ERROR ("network plugin: Starting cryptograp"
1455 "hic subsystem failed. "
1456 "Because the security level "
1457 "is set to `Encrypt' I will "
1464 } /* if (shared_secret != NULL) */
1466 /* Make compiler happy */
1468 shared_secret = NULL;
1469 #endif /* HAVE_GCRYPT_H */
1471 if (se_tail == NULL)
1482 /* We don't open more than one write-socket per node/service pair.. */
1483 if ((flags & CREATE_SOCKET_FLAGS_LISTEN) == 0)
1487 freeaddrinfo (ai_list);
1490 } /* }}} sockent_t *network_create_socket */
1492 static sockent_t *network_create_default_socket (int flags) /* {{{ */
1494 sockent_t *se_ptr = NULL;
1495 sockent_t *se_head = NULL;
1496 sockent_t *se_tail = NULL;
1498 se_ptr = network_create_socket (NET_DEFAULT_V6_ADDR, NET_DEFAULT_PORT,
1499 /* shared secret = */ NULL, SECURITY_LEVEL_NONE,
1502 /* Don't send to the same machine in IPv6 and IPv4 if both are available. */
1503 if (((flags & CREATE_SOCKET_FLAGS_LISTEN) == 0) && (se_ptr != NULL))
1510 while (se_tail->next != NULL)
1511 se_tail = se_tail->next;
1514 se_ptr = network_create_socket (NET_DEFAULT_V4_ADDR, NET_DEFAULT_PORT,
1515 /* shared secret = */ NULL, SECURITY_LEVEL_NONE,
1518 if (se_tail == NULL)
1521 se_tail->next = se_ptr;
1523 } /* }}} sockent_t *network_create_default_socket */
1525 static int network_add_listen_socket (const char *node, /* {{{ */
1526 const char *service, const char *shared_secret, int security_level)
1534 flags = CREATE_SOCKET_FLAGS_LISTEN;
1536 if (service == NULL)
1537 service = NET_DEFAULT_PORT;
1540 se = network_create_default_socket (flags);
1542 se = network_create_socket (node, service,
1543 shared_secret, security_level, flags);
1548 for (se_ptr = se; se_ptr != NULL; se_ptr = se_ptr->next)
1551 listen_sockets_pollfd = realloc (listen_sockets_pollfd,
1552 (listen_sockets_num + se_num)
1553 * sizeof (struct pollfd));
1555 for (se_ptr = se; se_ptr != NULL; se_ptr = se_ptr->next)
1557 listen_sockets_pollfd[listen_sockets_num].fd = se_ptr->fd;
1558 listen_sockets_pollfd[listen_sockets_num].events = POLLIN | POLLPRI;
1559 listen_sockets_pollfd[listen_sockets_num].revents = 0;
1560 listen_sockets_num++;
1563 se_ptr = listen_sockets;
1564 while ((se_ptr != NULL) && (se_ptr->next != NULL))
1565 se_ptr = se_ptr->next;
1568 listen_sockets = se;
1573 } /* }}} int network_add_listen_socket */
1575 static int network_add_sending_socket (const char *node, /* {{{ */
1576 const char *service, const char *shared_secret, int security_level)
1581 if (service == NULL)
1582 service = NET_DEFAULT_PORT;
1585 se = network_create_default_socket (/* flags = */ 0);
1587 se = network_create_socket (node, service,
1588 shared_secret, security_level,
1594 if (sending_sockets == NULL)
1596 sending_sockets = se;
1600 for (se_ptr = sending_sockets; se_ptr->next != NULL; se_ptr = se_ptr->next)
1605 } /* }}} int network_add_sending_socket */
1607 static void *dispatch_thread (void __attribute__((unused)) *arg)
1611 receive_list_entry_t *ent;
1613 /* Lock and wait for more data to come in */
1614 pthread_mutex_lock (&receive_list_lock);
1615 while ((listen_loop == 0)
1616 && (receive_list_head == NULL))
1617 pthread_cond_wait (&receive_list_cond, &receive_list_lock);
1619 /* Remove the head entry and unlock */
1620 ent = receive_list_head;
1622 receive_list_head = ent->next;
1623 pthread_mutex_unlock (&receive_list_lock);
1625 /* Check whether we are supposed to exit. We do NOT check `listen_loop'
1626 * because we dispatch all missing packets before shutting down. */
1636 } /* void *dispatch_thread */
1638 static int network_receive (void)
1640 char buffer[BUFF_SIZE];
1646 receive_list_entry_t *private_list_head;
1647 receive_list_entry_t *private_list_tail;
1649 if (listen_sockets_num == 0)
1650 network_add_listen_socket (/* node = */ NULL,
1651 /* service = */ NULL,
1652 /* shared secret = */ NULL,
1653 /* encryption = */ 0);
1655 if (listen_sockets_num == 0)
1657 ERROR ("network: Failed to open a listening socket.");
1661 private_list_head = NULL;
1662 private_list_tail = NULL;
1664 while (listen_loop == 0)
1666 status = poll (listen_sockets_pollfd, listen_sockets_num, -1);
1673 ERROR ("poll failed: %s",
1674 sstrerror (errno, errbuf, sizeof (errbuf)));
1678 for (i = 0; (i < listen_sockets_num) && (status > 0); i++)
1680 receive_list_entry_t *ent;
1682 if ((listen_sockets_pollfd[i].revents
1683 & (POLLIN | POLLPRI)) == 0)
1687 buffer_len = recv (listen_sockets_pollfd[i].fd,
1688 buffer, sizeof (buffer),
1693 ERROR ("recv failed: %s",
1694 sstrerror (errno, errbuf,
1699 /* TODO: Possible performance enhancement: Do not free
1700 * these entries in the dispatch thread but put them in
1701 * another list, so we don't have to allocate more and
1702 * more of these structures. */
1703 ent = malloc (sizeof (receive_list_entry_t));
1706 ERROR ("network plugin: malloc failed.");
1709 memset (ent, 0, sizeof (receive_list_entry_t));
1710 ent->fd = listen_sockets_pollfd[i].fd;
1713 /* Hopefully this be optimized out by the compiler. It
1714 * might help prevent stupid bugs in the future though.
1716 assert (sizeof (ent->data) == sizeof (buffer));
1718 memcpy (ent->data, buffer, buffer_len);
1719 ent->data_len = buffer_len;
1721 if (private_list_head == NULL)
1722 private_list_head = ent;
1724 private_list_tail->next = ent;
1725 private_list_tail = ent;
1727 /* Do not block here. Blocking here has led to
1728 * insufficient performance in the past. */
1729 if (pthread_mutex_trylock (&receive_list_lock) == 0)
1731 if (receive_list_head == NULL)
1732 receive_list_head = private_list_head;
1734 receive_list_tail->next = private_list_head;
1735 receive_list_tail = private_list_tail;
1737 private_list_head = NULL;
1738 private_list_tail = NULL;
1740 pthread_cond_signal (&receive_list_cond);
1741 pthread_mutex_unlock (&receive_list_lock);
1743 } /* for (listen_sockets_pollfd) */
1744 } /* while (listen_loop == 0) */
1746 /* Make sure everything is dispatched before exiting. */
1747 if (private_list_head != NULL)
1749 pthread_mutex_lock (&receive_list_lock);
1751 if (receive_list_head == NULL)
1752 receive_list_head = private_list_head;
1754 receive_list_tail->next = private_list_head;
1755 receive_list_tail = private_list_tail;
1757 private_list_head = NULL;
1758 private_list_tail = NULL;
1760 pthread_cond_signal (&receive_list_cond);
1761 pthread_mutex_unlock (&receive_list_lock);
1765 } /* int network_receive */
1767 static void *receive_thread (void __attribute__((unused)) *arg)
1769 return (network_receive () ? (void *) 1 : (void *) 0);
1770 } /* void *receive_thread */
1772 static void network_init_buffer (void)
1774 memset (send_buffer, 0, sizeof (send_buffer));
1775 send_buffer_ptr = send_buffer;
1776 send_buffer_fill = 0;
1778 memset (&send_buffer_vl, 0, sizeof (send_buffer_vl));
1779 } /* int network_init_buffer */
1781 static void networt_send_buffer_plain (const sockent_t *se, /* {{{ */
1782 const char *buffer, size_t buffer_size)
1788 status = sendto (se->fd, buffer, buffer_size, 0 /* no flags */,
1789 (struct sockaddr *) se->addr, se->addrlen);
1795 ERROR ("network plugin: sendto failed: %s",
1796 sstrerror (errno, errbuf,
1803 } /* }}} void networt_send_buffer_plain */
1806 static void networt_send_buffer_signed (const sockent_t *se, /* {{{ */
1807 const char *in_buffer, size_t in_buffer_size)
1809 part_signature_sha256_t ps;
1810 char buffer[sizeof (ps) + in_buffer_size];
1811 char hash[sizeof (ps.hash)];
1813 /* Initialize the `ps' structure. */
1814 memset (&ps, 0, sizeof (ps));
1815 ps.head.type = htons (TYPE_SIGN_SHA256);
1816 ps.head.length = htons ((uint16_t) sizeof (ps));
1817 sstrncpy (ps.hash, se->shared_secret, sizeof (ps.hash));
1819 /* Prepend the signature. */
1820 memcpy (buffer, &ps, sizeof (ps));
1821 memcpy (buffer + sizeof (ps), in_buffer, in_buffer_size);
1823 /* Calculate the hash value. */
1824 gcry_md_hash_buffer (GCRY_MD_SHA256, hash, buffer, sizeof (buffer));
1826 /* Copy the hash value into the buffer. */
1827 memcpy (ps.hash, hash, sizeof (ps.hash));
1828 memcpy (buffer, &ps, sizeof (ps));
1830 networt_send_buffer_plain (se, buffer, sizeof (buffer));
1831 } /* }}} void networt_send_buffer_signed */
1833 static void networt_send_buffer_encrypted (const sockent_t *se, /* {{{ */
1834 const char *in_buffer, size_t in_buffer_size)
1836 part_encryption_aes256_t pea;
1837 char buffer[sizeof (pea) + in_buffer_size + 16];
1841 /* Round to the next multiple of 16, because AES has a block size of 128 bit.
1842 * the first four bytes of `pea' are not encrypted and must be subtracted. */
1843 buffer_size = (sizeof (pea) + in_buffer_size + 15 - sizeof (pea.head)) / 16;
1844 buffer_size = buffer_size * 16;
1845 buffer_size += sizeof (pea.head);
1847 DEBUG ("network plugin: networt_send_buffer_encrypted: "
1848 "buffer_size = %zu;", buffer_size);
1850 /* Initialize the header fields */
1851 memset (&pea, 0, sizeof (pea));
1852 pea.head.type = htons (TYPE_ENCR_AES256);
1853 pea.head.length = htons ((uint16_t) buffer_size);
1854 pea.orig_length = htons ((uint16_t) in_buffer_size);
1856 /* Fill the extra field with random values. Some entropy in the encrypted
1857 * data is usually not a bad thing, I hope. */
1858 gcry_randomize (&pea.random, sizeof (pea.random), GCRY_STRONG_RANDOM);
1860 /* Create hash of the payload */
1861 gcry_md_hash_buffer (GCRY_MD_SHA224, pea.hash, in_buffer, in_buffer_size);
1863 /* Initialize the buffer */
1864 memset (buffer, 0, sizeof (buffer));
1865 memcpy (buffer, &pea, sizeof (pea));
1866 memcpy (buffer + sizeof (pea), in_buffer, in_buffer_size);
1868 /* Encrypt the buffer in-place */
1869 err = gcry_cipher_encrypt (se->cypher,
1870 buffer + sizeof (pea.head), buffer_size - sizeof (pea.head),
1871 /* in = */ NULL, /* in len = */ 0);
1872 gcry_cipher_reset (se->cypher);
1875 ERROR ("network plugin: gcry_cipher_encrypt returned: %s",
1876 gcry_strerror (err));
1880 /* Send it out without further modifications */
1881 networt_send_buffer_plain (se, buffer, buffer_size);
1882 } /* }}} void networt_send_buffer_encrypted */
1883 #endif /* HAVE_GCRYPT_H */
1885 static void network_send_buffer (char *buffer, size_t buffer_len) /* {{{ */
1889 DEBUG ("network plugin: network_send_buffer: buffer_len = %zu", buffer_len);
1891 for (se = sending_sockets; se != NULL; se = se->next)
1894 if (se->security_level == SECURITY_LEVEL_ENCRYPT)
1895 networt_send_buffer_encrypted (se, buffer, buffer_len);
1896 else if (se->security_level == SECURITY_LEVEL_SIGN)
1897 networt_send_buffer_signed (se, buffer, buffer_len);
1898 else /* if (se->security_level == SECURITY_LEVEL_NONE) */
1899 #endif /* HAVE_GCRYPT_H */
1900 networt_send_buffer_plain (se, buffer, buffer_len);
1901 } /* for (sending_sockets) */
1902 } /* }}} void network_send_buffer */
1904 static int add_to_buffer (char *buffer, int buffer_size, /* {{{ */
1905 value_list_t *vl_def,
1906 const data_set_t *ds, const value_list_t *vl)
1908 char *buffer_orig = buffer;
1910 if (strcmp (vl_def->host, vl->host) != 0)
1912 if (write_part_string (&buffer, &buffer_size, TYPE_HOST,
1913 vl->host, strlen (vl->host)) != 0)
1915 sstrncpy (vl_def->host, vl->host, sizeof (vl_def->host));
1918 if (vl_def->time != vl->time)
1920 if (write_part_number (&buffer, &buffer_size, TYPE_TIME,
1921 (uint64_t) vl->time))
1923 vl_def->time = vl->time;
1926 if (vl_def->interval != vl->interval)
1928 if (write_part_number (&buffer, &buffer_size, TYPE_INTERVAL,
1929 (uint64_t) vl->interval))
1931 vl_def->interval = vl->interval;
1934 if (strcmp (vl_def->plugin, vl->plugin) != 0)
1936 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN,
1937 vl->plugin, strlen (vl->plugin)) != 0)
1939 sstrncpy (vl_def->plugin, vl->plugin, sizeof (vl_def->plugin));
1942 if (strcmp (vl_def->plugin_instance, vl->plugin_instance) != 0)
1944 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
1945 vl->plugin_instance,
1946 strlen (vl->plugin_instance)) != 0)
1948 sstrncpy (vl_def->plugin_instance, vl->plugin_instance, sizeof (vl_def->plugin_instance));
1951 if (strcmp (vl_def->type, vl->type) != 0)
1953 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
1954 vl->type, strlen (vl->type)) != 0)
1956 sstrncpy (vl_def->type, ds->type, sizeof (vl_def->type));
1959 if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
1961 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE_INSTANCE,
1963 strlen (vl->type_instance)) != 0)
1965 sstrncpy (vl_def->type_instance, vl->type_instance, sizeof (vl_def->type_instance));
1968 if (write_part_values (&buffer, &buffer_size, ds, vl) != 0)
1971 return (buffer - buffer_orig);
1972 } /* }}} int add_to_buffer */
1974 static void flush_buffer (void)
1976 DEBUG ("network plugin: flush_buffer: send_buffer_fill = %i",
1979 network_send_buffer (send_buffer, (size_t) send_buffer_fill);
1980 network_init_buffer ();
1983 static int network_write (const data_set_t *ds, const value_list_t *vl,
1984 user_data_t __attribute__((unused)) *user_data)
1988 /* If the value is already in the cache, we have received it via the
1989 * network. We write it again if forwarding is activated. It's then in
1990 * the cache and should we receive it again we will ignore it. */
1991 status = cache_check (vl);
1992 if ((network_config_forward == 0)
1996 pthread_mutex_lock (&send_buffer_lock);
1998 status = add_to_buffer (send_buffer_ptr,
1999 sizeof (send_buffer) - (send_buffer_fill + BUFF_SIG_SIZE),
2004 /* status == bytes added to the buffer */
2005 send_buffer_fill += status;
2006 send_buffer_ptr += status;
2012 status = add_to_buffer (send_buffer_ptr,
2013 sizeof (send_buffer) - (send_buffer_fill + BUFF_SIG_SIZE),
2019 send_buffer_fill += status;
2020 send_buffer_ptr += status;
2026 ERROR ("network plugin: Unable to append to the "
2027 "buffer for some weird reason");
2029 else if ((sizeof (send_buffer) - send_buffer_fill) < 15)
2034 pthread_mutex_unlock (&send_buffer_lock);
2036 return ((status < 0) ? -1 : 0);
2037 } /* int network_write */
2039 static int network_config_set_boolean (const oconfig_item_t *ci, /* {{{ */
2042 if ((ci->values_num != 1)
2043 || ((ci->values[0].type != OCONFIG_TYPE_BOOLEAN)
2044 && (ci->values[0].type != OCONFIG_TYPE_STRING)))
2046 ERROR ("network plugin: The `%s' config option needs "
2047 "exactly one boolean argument.", ci->key);
2051 if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
2053 if (ci->values[0].value.boolean)
2060 char *str = ci->values[0].value.string;
2062 if ((strcasecmp ("true", str) == 0)
2063 || (strcasecmp ("yes", str) == 0)
2064 || (strcasecmp ("on", str) == 0))
2066 else if ((strcasecmp ("false", str) == 0)
2067 || (strcasecmp ("no", str) == 0)
2068 || (strcasecmp ("off", str) == 0))
2072 ERROR ("network plugin: Cannot parse string value `%s' of the `%s' "
2073 "option as boolean value.",
2080 } /* }}} int network_config_set_boolean */
2082 static int network_config_set_ttl (const oconfig_item_t *ci) /* {{{ */
2085 if ((ci->values_num != 1)
2086 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
2088 WARNING ("network plugin: The `TimeToLive' config option needs exactly "
2089 "one numeric argument.");
2093 tmp = (int) ci->values[0].value.number;
2094 if ((tmp > 0) && (tmp <= 255))
2095 network_config_ttl = tmp;
2098 } /* }}} int network_config_set_ttl */
2101 static int network_config_set_security_level (oconfig_item_t *ci, /* {{{ */
2105 if ((ci->values_num != 1)
2106 || (ci->values[0].type != OCONFIG_TYPE_STRING))
2108 WARNING ("network plugin: The `SecurityLevel' config option needs exactly "
2109 "one string argument.");
2113 str = ci->values[0].value.string;
2114 if (strcasecmp ("Encrypt", str) == 0)
2115 *retval = SECURITY_LEVEL_ENCRYPT;
2116 else if (strcasecmp ("Sign", str) == 0)
2117 *retval = SECURITY_LEVEL_SIGN;
2118 else if (strcasecmp ("None", str) == 0)
2119 *retval = SECURITY_LEVEL_NONE;
2122 WARNING ("network plugin: Unknown security level: %s.", str);
2127 } /* }}} int network_config_set_security_level */
2128 #endif /* HAVE_GCRYPT_H */
2130 static int network_config_listen_server (const oconfig_item_t *ci) /* {{{ */
2134 char *shared_secret = NULL;
2135 int security_level = SECURITY_LEVEL_NONE;
2138 if ((ci->values_num < 1) || (ci->values_num > 2)
2139 || (ci->values[0].type != OCONFIG_TYPE_STRING)
2140 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
2142 ERROR ("network plugin: The `%s' config option needs "
2143 "one or two string arguments.", ci->key);
2147 node = ci->values[0].value.string;
2148 if (ci->values_num >= 2)
2149 service = ci->values[1].value.string;
2153 for (i = 0; i < ci->children_num; i++)
2155 oconfig_item_t *child = ci->children + i;
2158 if (strcasecmp ("Secret", child->key) == 0)
2160 if ((child->values_num == 1)
2161 && (child->values[0].type == OCONFIG_TYPE_STRING))
2162 shared_secret = child->values[0].value.string;
2164 ERROR ("network plugin: The `Secret' option needs exactly one string "
2167 else if (strcasecmp ("SecurityLevel", child->key) == 0)
2168 network_config_set_security_level (child, &security_level);
2170 #endif /* HAVE_GCRYPT_H */
2172 WARNING ("network plugin: Option `%s' is not allowed here.",
2177 if ((security_level > SECURITY_LEVEL_NONE) && (shared_secret == NULL))
2179 ERROR ("network plugin: A security level higher than `none' was "
2180 "requested, but no shared key was given. Cowardly refusing to open "
2185 if (strcasecmp ("Listen", ci->key) == 0)
2186 network_add_listen_socket (node, service, shared_secret, security_level);
2188 network_add_sending_socket (node, service, shared_secret, security_level);
2191 } /* }}} int network_config_listen_server */
2193 static int network_config_set_cache_flush (const oconfig_item_t *ci) /* {{{ */
2196 if ((ci->values_num != 1)
2197 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
2199 WARNING ("network plugin: The `CacheFlush' config option needs exactly "
2200 "one numeric argument.");
2204 tmp = (int) ci->values[0].value.number;
2206 network_config_ttl = tmp;
2209 } /* }}} int network_config_set_cache_flush */
2211 static int network_config (oconfig_item_t *ci) /* {{{ */
2215 for (i = 0; i < ci->children_num; i++)
2217 oconfig_item_t *child = ci->children + i;
2219 if ((strcasecmp ("Listen", child->key) == 0)
2220 || (strcasecmp ("Server", child->key) == 0))
2221 network_config_listen_server (child);
2222 else if (strcasecmp ("TimeToLive", child->key) == 0)
2223 network_config_set_ttl (child);
2224 else if (strcasecmp ("Forward", child->key) == 0)
2225 network_config_set_boolean (child, &network_config_forward);
2226 else if (strcasecmp ("CacheFlush", child->key) == 0)
2227 network_config_set_cache_flush (child);
2230 WARNING ("network plugin: Option `%s' is not allowed here.",
2236 } /* }}} int network_config */
2238 static int network_notification (const notification_t *n,
2239 user_data_t __attribute__((unused)) *user_data)
2241 char buffer[BUFF_SIZE];
2242 char *buffer_ptr = buffer;
2243 int buffer_free = sizeof (buffer);
2246 memset (buffer, '\0', sizeof (buffer));
2249 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_TIME,
2250 (uint64_t) n->time);
2254 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_SEVERITY,
2255 (uint64_t) n->severity);
2259 if (strlen (n->host) > 0)
2261 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_HOST,
2262 n->host, strlen (n->host));
2267 if (strlen (n->plugin) > 0)
2269 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_PLUGIN,
2270 n->plugin, strlen (n->plugin));
2275 if (strlen (n->plugin_instance) > 0)
2277 status = write_part_string (&buffer_ptr, &buffer_free,
2278 TYPE_PLUGIN_INSTANCE,
2279 n->plugin_instance, strlen (n->plugin_instance));
2284 if (strlen (n->type) > 0)
2286 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE,
2287 n->type, strlen (n->type));
2292 if (strlen (n->type_instance) > 0)
2294 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE_INSTANCE,
2295 n->type_instance, strlen (n->type_instance));
2300 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_MESSAGE,
2301 n->message, strlen (n->message));
2305 network_send_buffer (buffer, sizeof (buffer) - buffer_free);
2308 } /* int network_notification */
2310 static int network_shutdown (void)
2314 /* Kill the listening thread */
2315 if (receive_thread_running != 0)
2317 INFO ("network plugin: Stopping receive thread.");
2318 pthread_kill (receive_thread_id, SIGTERM);
2319 pthread_join (receive_thread_id, NULL /* no return value */);
2320 memset (&receive_thread_id, 0, sizeof (receive_thread_id));
2321 receive_thread_running = 0;
2324 /* Shutdown the dispatching thread */
2325 if (dispatch_thread_running != 0)
2327 INFO ("network plugin: Stopping dispatch thread.");
2328 pthread_mutex_lock (&receive_list_lock);
2329 pthread_cond_broadcast (&receive_list_cond);
2330 pthread_mutex_unlock (&receive_list_lock);
2331 pthread_join (dispatch_thread_id, /* ret = */ NULL);
2332 dispatch_thread_running = 0;
2335 free_sockent (listen_sockets);
2337 if (send_buffer_fill > 0)
2340 if (cache_tree != NULL)
2345 while (c_avl_pick (cache_tree, &key, &value) == 0)
2350 c_avl_destroy (cache_tree);
2354 /* TODO: Close `sending_sockets' */
2356 plugin_unregister_config ("network");
2357 plugin_unregister_init ("network");
2358 plugin_unregister_write ("network");
2359 plugin_unregister_shutdown ("network");
2361 /* Let the init function do it's move again ;) */
2362 cache_flush_last = 0;
2365 } /* int network_shutdown */
2367 static int network_init (void)
2369 /* Check if we were already initialized. If so, just return - there's
2370 * nothing more to do (for now, that is). */
2371 if (cache_flush_last != 0)
2374 plugin_register_shutdown ("network", network_shutdown);
2376 network_init_buffer ();
2378 cache_tree = c_avl_create ((int (*) (const void *, const void *)) strcmp);
2379 cache_flush_last = time (NULL);
2381 /* setup socket(s) and so on */
2382 if (sending_sockets != NULL)
2384 plugin_register_write ("network", network_write,
2385 /* user_data = */ NULL);
2386 plugin_register_notification ("network", network_notification,
2387 /* user_data = */ NULL);
2390 /* If no threads need to be started, return here. */
2391 if ((listen_sockets_num == 0)
2392 || ((dispatch_thread_running != 0)
2393 && (receive_thread_running != 0)))
2396 if (dispatch_thread_running == 0)
2399 status = pthread_create (&dispatch_thread_id,
2400 NULL /* no attributes */,
2402 NULL /* no argument */);
2406 ERROR ("network: pthread_create failed: %s",
2407 sstrerror (errno, errbuf,
2412 dispatch_thread_running = 1;
2416 if (receive_thread_running == 0)
2419 status = pthread_create (&receive_thread_id,
2420 NULL /* no attributes */,
2422 NULL /* no argument */);
2426 ERROR ("network: pthread_create failed: %s",
2427 sstrerror (errno, errbuf,
2432 receive_thread_running = 1;
2437 } /* int network_init */
2440 * The flush option of the network plugin cannot flush individual identifiers.
2441 * All the values are added to a buffer and sent when the buffer is full, the
2442 * requested value may or may not be in there, it's not worth finding out. We
2443 * just send the buffer if `flush' is called - if the requested value was in
2444 * there, good. If not, well, then there is nothing to flush.. -octo
2446 static int network_flush (int timeout,
2447 const char __attribute__((unused)) *identifier,
2448 user_data_t __attribute__((unused)) *user_data)
2450 pthread_mutex_lock (&send_buffer_lock);
2452 if (((time (NULL) - cache_flush_last) >= timeout)
2453 && (send_buffer_fill > 0))
2458 pthread_mutex_unlock (&send_buffer_lock);
2461 } /* int network_flush */
2463 void module_register (void)
2465 plugin_register_complex_config ("network", network_config);
2466 plugin_register_init ("network", network_init);
2467 plugin_register_flush ("network", network_flush,
2468 /* user_data = */ NULL);
2469 } /* void module_register */
2471 /* vim: set fdm=marker : */