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 DEBUG ("network plugin: network_set_ttl: network_config_ttl = %i;",
1134 network_config_ttl);
1136 if ((network_config_ttl < 1) || (network_config_ttl > 255))
1139 if (ai->ai_family == AF_INET)
1141 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1144 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1145 optname = IP_MULTICAST_TTL;
1149 if (setsockopt (se->fd, IPPROTO_IP, optname,
1150 &network_config_ttl,
1151 sizeof (network_config_ttl)) == -1)
1154 ERROR ("setsockopt: %s",
1155 sstrerror (errno, errbuf, sizeof (errbuf)));
1159 else if (ai->ai_family == AF_INET6)
1161 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1162 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1165 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1166 optname = IPV6_MULTICAST_HOPS;
1168 optname = IPV6_UNICAST_HOPS;
1170 if (setsockopt (se->fd, IPPROTO_IPV6, optname,
1171 &network_config_ttl,
1172 sizeof (network_config_ttl)) == -1)
1175 ERROR ("setsockopt: %s",
1176 sstrerror (errno, errbuf,
1183 } /* int network_set_ttl */
1186 static int network_set_encryption (sockent_t *se, /* {{{ */
1187 const char *shared_secret)
1192 se->shared_secret = sstrdup (shared_secret);
1195 * We use CBC *without* an initialization vector: The cipher is reset after
1196 * each packet and we would have to re-set the IV each time. The first
1197 * encrypted block will contain the SHA-224 checksum anyway, so this should
1198 * be quite unpredictable. Also, there's a 2 byte field in the header that's
1199 * being filled with random numbers. So we only use CBC so the blocks
1200 * *within* one packet are chained.
1202 err = gcry_cipher_open (&se->cypher,
1203 GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CBC, /* flags = */ 0);
1206 ERROR ("network plugin: gcry_cipher_open returned: %s",
1207 gcry_strerror (err));
1211 assert (se->shared_secret != NULL);
1212 gcry_md_hash_buffer (GCRY_MD_SHA256, hash,
1213 se->shared_secret, strlen (se->shared_secret));
1215 err = gcry_cipher_setkey (se->cypher, hash, sizeof (hash));
1218 DEBUG ("network plugin: gcry_cipher_setkey returned: %s",
1219 gcry_strerror (err));
1220 gcry_cipher_close (se->cypher);
1226 } /* }}} int network_set_encryption */
1227 #endif /* HAVE_GCRYPT_H */
1229 static int network_bind_socket (const sockent_t *se, const struct addrinfo *ai)
1234 /* allow multiple sockets to use the same PORT number */
1235 if (setsockopt(se->fd, SOL_SOCKET, SO_REUSEADDR,
1236 &yes, sizeof(yes)) == -1) {
1238 ERROR ("setsockopt: %s",
1239 sstrerror (errno, errbuf, sizeof (errbuf)));
1243 DEBUG ("fd = %i; calling `bind'", se->fd);
1245 if (bind (se->fd, ai->ai_addr, ai->ai_addrlen) == -1)
1249 sstrerror (errno, errbuf, sizeof (errbuf)));
1253 if (ai->ai_family == AF_INET)
1255 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1256 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1258 struct ip_mreq mreq;
1260 DEBUG ("fd = %i; IPv4 multicast address found", se->fd);
1262 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1263 mreq.imr_interface.s_addr = htonl (INADDR_ANY);
1265 if (setsockopt (se->fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1266 &loop, sizeof (loop)) == -1)
1269 ERROR ("setsockopt: %s",
1270 sstrerror (errno, errbuf,
1275 if (setsockopt (se->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1276 &mreq, sizeof (mreq)) == -1)
1279 ERROR ("setsockopt: %s",
1280 sstrerror (errno, errbuf,
1286 else if (ai->ai_family == AF_INET6)
1288 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1289 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1290 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1292 struct ipv6_mreq mreq;
1294 DEBUG ("fd = %i; IPv6 multicast address found", se->fd);
1296 memcpy (&mreq.ipv6mr_multiaddr,
1298 sizeof (addr->sin6_addr));
1300 /* http://developer.apple.com/documentation/Darwin/Reference/ManPages/man4/ip6.4.html
1301 * ipv6mr_interface may be set to zeroes to
1302 * choose the default multicast interface or to
1303 * the index of a particular multicast-capable
1304 * interface if the host is multihomed.
1305 * Membership is associ-associated with a
1306 * single interface; programs running on
1307 * multihomed hosts may need to join the same
1308 * group on more than one interface.*/
1309 mreq.ipv6mr_interface = 0;
1311 if (setsockopt (se->fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
1312 &loop, sizeof (loop)) == -1)
1315 ERROR ("setsockopt: %s",
1316 sstrerror (errno, errbuf,
1321 if (setsockopt (se->fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
1322 &mreq, sizeof (mreq)) == -1)
1325 ERROR ("setsockopt: %s",
1326 sstrerror (errno, errbuf,
1334 } /* int network_bind_socket */
1336 #define CREATE_SOCKET_FLAGS_LISTEN 0x0001
1337 static sockent_t *network_create_socket (const char *node, /* {{{ */
1338 const char *service,
1339 const char *shared_secret,
1343 struct addrinfo ai_hints;
1344 struct addrinfo *ai_list, *ai_ptr;
1347 sockent_t *se_head = NULL;
1348 sockent_t *se_tail = NULL;
1350 DEBUG ("node = %s, service = %s", node, service);
1352 memset (&ai_hints, '\0', sizeof (ai_hints));
1353 ai_hints.ai_flags = 0;
1355 ai_hints.ai_flags |= AI_PASSIVE;
1357 #ifdef AI_ADDRCONFIG
1358 ai_hints.ai_flags |= AI_ADDRCONFIG;
1360 ai_hints.ai_family = AF_UNSPEC;
1361 ai_hints.ai_socktype = SOCK_DGRAM;
1362 ai_hints.ai_protocol = IPPROTO_UDP;
1364 ai_return = getaddrinfo (node, service, &ai_hints, &ai_list);
1368 ERROR ("getaddrinfo (%s, %s): %s",
1369 (node == NULL) ? "(null)" : node,
1370 (service == NULL) ? "(null)" : service,
1371 (ai_return == EAI_SYSTEM)
1372 ? sstrerror (errno, errbuf, sizeof (errbuf))
1373 : gai_strerror (ai_return));
1377 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1382 if ((se = (sockent_t *) malloc (sizeof (sockent_t))) == NULL)
1385 ERROR ("malloc: %s",
1386 sstrerror (errno, errbuf,
1391 if ((se->addr = (struct sockaddr_storage *) malloc (sizeof (struct sockaddr_storage))) == NULL)
1394 ERROR ("malloc: %s",
1395 sstrerror (errno, errbuf,
1401 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
1402 memset (se->addr, '\0', sizeof (struct sockaddr_storage));
1403 memcpy (se->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1404 se->addrlen = ai_ptr->ai_addrlen;
1406 se->fd = socket (ai_ptr->ai_family,
1407 ai_ptr->ai_socktype,
1408 ai_ptr->ai_protocol);
1414 ERROR ("socket: %s",
1415 sstrerror (errno, errbuf,
1422 if ((flags & CREATE_SOCKET_FLAGS_LISTEN) != 0)
1424 status = network_bind_socket (se, ai_ptr);
1433 else /* sending socket */
1435 network_set_ttl (se, ai_ptr);
1439 se->security_level = security_level;
1440 se->shared_secret = NULL;
1442 if (shared_secret != NULL)
1444 status = network_set_encryption (se, shared_secret);
1445 if ((status != 0) && (security_level <= SECURITY_LEVEL_SIGN))
1447 WARNING ("network plugin: Starting cryptograp"
1448 "hic subsystem failed. Since "
1449 "security level `Sign' or "
1450 "`None' is configured I will "
1453 else if (status != 0)
1455 ERROR ("network plugin: Starting cryptograp"
1456 "hic subsystem failed. "
1457 "Because the security level "
1458 "is set to `Encrypt' I will "
1465 } /* if (shared_secret != NULL) */
1467 /* Make compiler happy */
1469 shared_secret = NULL;
1470 #endif /* HAVE_GCRYPT_H */
1472 if (se_tail == NULL)
1483 /* We don't open more than one write-socket per node/service pair.. */
1484 if ((flags & CREATE_SOCKET_FLAGS_LISTEN) == 0)
1488 freeaddrinfo (ai_list);
1491 } /* }}} sockent_t *network_create_socket */
1493 static sockent_t *network_create_default_socket (int flags) /* {{{ */
1495 sockent_t *se_ptr = NULL;
1496 sockent_t *se_head = NULL;
1497 sockent_t *se_tail = NULL;
1499 se_ptr = network_create_socket (NET_DEFAULT_V6_ADDR, NET_DEFAULT_PORT,
1500 /* shared secret = */ NULL, SECURITY_LEVEL_NONE,
1503 /* Don't send to the same machine in IPv6 and IPv4 if both are available. */
1504 if (((flags & CREATE_SOCKET_FLAGS_LISTEN) == 0) && (se_ptr != NULL))
1511 while (se_tail->next != NULL)
1512 se_tail = se_tail->next;
1515 se_ptr = network_create_socket (NET_DEFAULT_V4_ADDR, NET_DEFAULT_PORT,
1516 /* shared secret = */ NULL, SECURITY_LEVEL_NONE,
1519 if (se_tail == NULL)
1522 se_tail->next = se_ptr;
1524 } /* }}} sockent_t *network_create_default_socket */
1526 static int network_add_listen_socket (const char *node, /* {{{ */
1527 const char *service, const char *shared_secret, int security_level)
1535 flags = CREATE_SOCKET_FLAGS_LISTEN;
1537 if (service == NULL)
1538 service = NET_DEFAULT_PORT;
1541 se = network_create_default_socket (flags);
1543 se = network_create_socket (node, service,
1544 shared_secret, security_level, flags);
1549 for (se_ptr = se; se_ptr != NULL; se_ptr = se_ptr->next)
1552 listen_sockets_pollfd = realloc (listen_sockets_pollfd,
1553 (listen_sockets_num + se_num)
1554 * sizeof (struct pollfd));
1556 for (se_ptr = se; se_ptr != NULL; se_ptr = se_ptr->next)
1558 listen_sockets_pollfd[listen_sockets_num].fd = se_ptr->fd;
1559 listen_sockets_pollfd[listen_sockets_num].events = POLLIN | POLLPRI;
1560 listen_sockets_pollfd[listen_sockets_num].revents = 0;
1561 listen_sockets_num++;
1564 se_ptr = listen_sockets;
1565 while ((se_ptr != NULL) && (se_ptr->next != NULL))
1566 se_ptr = se_ptr->next;
1569 listen_sockets = se;
1574 } /* }}} int network_add_listen_socket */
1576 static int network_add_sending_socket (const char *node, /* {{{ */
1577 const char *service, const char *shared_secret, int security_level)
1582 if (service == NULL)
1583 service = NET_DEFAULT_PORT;
1586 se = network_create_default_socket (/* flags = */ 0);
1588 se = network_create_socket (node, service,
1589 shared_secret, security_level,
1595 if (sending_sockets == NULL)
1597 sending_sockets = se;
1601 for (se_ptr = sending_sockets; se_ptr->next != NULL; se_ptr = se_ptr->next)
1606 } /* }}} int network_add_sending_socket */
1608 static void *dispatch_thread (void __attribute__((unused)) *arg)
1612 receive_list_entry_t *ent;
1614 /* Lock and wait for more data to come in */
1615 pthread_mutex_lock (&receive_list_lock);
1616 while ((listen_loop == 0)
1617 && (receive_list_head == NULL))
1618 pthread_cond_wait (&receive_list_cond, &receive_list_lock);
1620 /* Remove the head entry and unlock */
1621 ent = receive_list_head;
1623 receive_list_head = ent->next;
1624 pthread_mutex_unlock (&receive_list_lock);
1626 /* Check whether we are supposed to exit. We do NOT check `listen_loop'
1627 * because we dispatch all missing packets before shutting down. */
1637 } /* void *dispatch_thread */
1639 static int network_receive (void)
1641 char buffer[BUFF_SIZE];
1647 receive_list_entry_t *private_list_head;
1648 receive_list_entry_t *private_list_tail;
1650 if (listen_sockets_num == 0)
1651 network_add_listen_socket (/* node = */ NULL,
1652 /* service = */ NULL,
1653 /* shared secret = */ NULL,
1654 /* encryption = */ 0);
1656 if (listen_sockets_num == 0)
1658 ERROR ("network: Failed to open a listening socket.");
1662 private_list_head = NULL;
1663 private_list_tail = NULL;
1665 while (listen_loop == 0)
1667 status = poll (listen_sockets_pollfd, listen_sockets_num, -1);
1674 ERROR ("poll failed: %s",
1675 sstrerror (errno, errbuf, sizeof (errbuf)));
1679 for (i = 0; (i < listen_sockets_num) && (status > 0); i++)
1681 receive_list_entry_t *ent;
1683 if ((listen_sockets_pollfd[i].revents
1684 & (POLLIN | POLLPRI)) == 0)
1688 buffer_len = recv (listen_sockets_pollfd[i].fd,
1689 buffer, sizeof (buffer),
1694 ERROR ("recv failed: %s",
1695 sstrerror (errno, errbuf,
1700 /* TODO: Possible performance enhancement: Do not free
1701 * these entries in the dispatch thread but put them in
1702 * another list, so we don't have to allocate more and
1703 * more of these structures. */
1704 ent = malloc (sizeof (receive_list_entry_t));
1707 ERROR ("network plugin: malloc failed.");
1710 memset (ent, 0, sizeof (receive_list_entry_t));
1711 ent->fd = listen_sockets_pollfd[i].fd;
1714 /* Hopefully this be optimized out by the compiler. It
1715 * might help prevent stupid bugs in the future though.
1717 assert (sizeof (ent->data) == sizeof (buffer));
1719 memcpy (ent->data, buffer, buffer_len);
1720 ent->data_len = buffer_len;
1722 if (private_list_head == NULL)
1723 private_list_head = ent;
1725 private_list_tail->next = ent;
1726 private_list_tail = ent;
1728 /* Do not block here. Blocking here has led to
1729 * insufficient performance in the past. */
1730 if (pthread_mutex_trylock (&receive_list_lock) == 0)
1732 if (receive_list_head == NULL)
1733 receive_list_head = private_list_head;
1735 receive_list_tail->next = private_list_head;
1736 receive_list_tail = private_list_tail;
1738 private_list_head = NULL;
1739 private_list_tail = NULL;
1741 pthread_cond_signal (&receive_list_cond);
1742 pthread_mutex_unlock (&receive_list_lock);
1744 } /* for (listen_sockets_pollfd) */
1745 } /* while (listen_loop == 0) */
1747 /* Make sure everything is dispatched before exiting. */
1748 if (private_list_head != NULL)
1750 pthread_mutex_lock (&receive_list_lock);
1752 if (receive_list_head == NULL)
1753 receive_list_head = private_list_head;
1755 receive_list_tail->next = private_list_head;
1756 receive_list_tail = private_list_tail;
1758 private_list_head = NULL;
1759 private_list_tail = NULL;
1761 pthread_cond_signal (&receive_list_cond);
1762 pthread_mutex_unlock (&receive_list_lock);
1766 } /* int network_receive */
1768 static void *receive_thread (void __attribute__((unused)) *arg)
1770 return (network_receive () ? (void *) 1 : (void *) 0);
1771 } /* void *receive_thread */
1773 static void network_init_buffer (void)
1775 memset (send_buffer, 0, sizeof (send_buffer));
1776 send_buffer_ptr = send_buffer;
1777 send_buffer_fill = 0;
1779 memset (&send_buffer_vl, 0, sizeof (send_buffer_vl));
1780 } /* int network_init_buffer */
1782 static void networt_send_buffer_plain (const sockent_t *se, /* {{{ */
1783 const char *buffer, size_t buffer_size)
1789 status = sendto (se->fd, buffer, buffer_size, 0 /* no flags */,
1790 (struct sockaddr *) se->addr, se->addrlen);
1796 ERROR ("network plugin: sendto failed: %s",
1797 sstrerror (errno, errbuf,
1804 } /* }}} void networt_send_buffer_plain */
1807 static void networt_send_buffer_signed (const sockent_t *se, /* {{{ */
1808 const char *in_buffer, size_t in_buffer_size)
1810 part_signature_sha256_t ps;
1811 char buffer[sizeof (ps) + in_buffer_size];
1812 char hash[sizeof (ps.hash)];
1814 /* Initialize the `ps' structure. */
1815 memset (&ps, 0, sizeof (ps));
1816 ps.head.type = htons (TYPE_SIGN_SHA256);
1817 ps.head.length = htons ((uint16_t) sizeof (ps));
1818 sstrncpy (ps.hash, se->shared_secret, sizeof (ps.hash));
1820 /* Prepend the signature. */
1821 memcpy (buffer, &ps, sizeof (ps));
1822 memcpy (buffer + sizeof (ps), in_buffer, in_buffer_size);
1824 /* Calculate the hash value. */
1825 gcry_md_hash_buffer (GCRY_MD_SHA256, hash, buffer, sizeof (buffer));
1827 /* Copy the hash value into the buffer. */
1828 memcpy (ps.hash, hash, sizeof (ps.hash));
1829 memcpy (buffer, &ps, sizeof (ps));
1831 networt_send_buffer_plain (se, buffer, sizeof (buffer));
1832 } /* }}} void networt_send_buffer_signed */
1834 static void networt_send_buffer_encrypted (const sockent_t *se, /* {{{ */
1835 const char *in_buffer, size_t in_buffer_size)
1837 part_encryption_aes256_t pea;
1838 char buffer[sizeof (pea) + in_buffer_size + 16];
1842 /* Round to the next multiple of 16, because AES has a block size of 128 bit.
1843 * the first four bytes of `pea' are not encrypted and must be subtracted. */
1844 buffer_size = (sizeof (pea) + in_buffer_size + 15 - sizeof (pea.head)) / 16;
1845 buffer_size = buffer_size * 16;
1846 buffer_size += sizeof (pea.head);
1848 DEBUG ("network plugin: networt_send_buffer_encrypted: "
1849 "buffer_size = %zu;", buffer_size);
1851 /* Initialize the header fields */
1852 memset (&pea, 0, sizeof (pea));
1853 pea.head.type = htons (TYPE_ENCR_AES256);
1854 pea.head.length = htons ((uint16_t) buffer_size);
1855 pea.orig_length = htons ((uint16_t) in_buffer_size);
1857 /* Fill the extra field with random values. Some entropy in the encrypted
1858 * data is usually not a bad thing, I hope. */
1859 gcry_randomize (&pea.random, sizeof (pea.random), GCRY_STRONG_RANDOM);
1861 /* Create hash of the payload */
1862 gcry_md_hash_buffer (GCRY_MD_SHA224, pea.hash, in_buffer, in_buffer_size);
1864 /* Initialize the buffer */
1865 memset (buffer, 0, sizeof (buffer));
1866 memcpy (buffer, &pea, sizeof (pea));
1867 memcpy (buffer + sizeof (pea), in_buffer, in_buffer_size);
1869 /* Encrypt the buffer in-place */
1870 err = gcry_cipher_encrypt (se->cypher,
1871 buffer + sizeof (pea.head), buffer_size - sizeof (pea.head),
1872 /* in = */ NULL, /* in len = */ 0);
1873 gcry_cipher_reset (se->cypher);
1876 ERROR ("network plugin: gcry_cipher_encrypt returned: %s",
1877 gcry_strerror (err));
1881 /* Send it out without further modifications */
1882 networt_send_buffer_plain (se, buffer, buffer_size);
1883 } /* }}} void networt_send_buffer_encrypted */
1884 #endif /* HAVE_GCRYPT_H */
1886 static void network_send_buffer (char *buffer, size_t buffer_len) /* {{{ */
1890 DEBUG ("network plugin: network_send_buffer: buffer_len = %zu", buffer_len);
1892 for (se = sending_sockets; se != NULL; se = se->next)
1895 if (se->security_level == SECURITY_LEVEL_ENCRYPT)
1896 networt_send_buffer_encrypted (se, buffer, buffer_len);
1897 else if (se->security_level == SECURITY_LEVEL_SIGN)
1898 networt_send_buffer_signed (se, buffer, buffer_len);
1899 else /* if (se->security_level == SECURITY_LEVEL_NONE) */
1900 #endif /* HAVE_GCRYPT_H */
1901 networt_send_buffer_plain (se, buffer, buffer_len);
1902 } /* for (sending_sockets) */
1903 } /* }}} void network_send_buffer */
1905 static int add_to_buffer (char *buffer, int buffer_size, /* {{{ */
1906 value_list_t *vl_def,
1907 const data_set_t *ds, const value_list_t *vl)
1909 char *buffer_orig = buffer;
1911 if (strcmp (vl_def->host, vl->host) != 0)
1913 if (write_part_string (&buffer, &buffer_size, TYPE_HOST,
1914 vl->host, strlen (vl->host)) != 0)
1916 sstrncpy (vl_def->host, vl->host, sizeof (vl_def->host));
1919 if (vl_def->time != vl->time)
1921 if (write_part_number (&buffer, &buffer_size, TYPE_TIME,
1922 (uint64_t) vl->time))
1924 vl_def->time = vl->time;
1927 if (vl_def->interval != vl->interval)
1929 if (write_part_number (&buffer, &buffer_size, TYPE_INTERVAL,
1930 (uint64_t) vl->interval))
1932 vl_def->interval = vl->interval;
1935 if (strcmp (vl_def->plugin, vl->plugin) != 0)
1937 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN,
1938 vl->plugin, strlen (vl->plugin)) != 0)
1940 sstrncpy (vl_def->plugin, vl->plugin, sizeof (vl_def->plugin));
1943 if (strcmp (vl_def->plugin_instance, vl->plugin_instance) != 0)
1945 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
1946 vl->plugin_instance,
1947 strlen (vl->plugin_instance)) != 0)
1949 sstrncpy (vl_def->plugin_instance, vl->plugin_instance, sizeof (vl_def->plugin_instance));
1952 if (strcmp (vl_def->type, vl->type) != 0)
1954 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
1955 vl->type, strlen (vl->type)) != 0)
1957 sstrncpy (vl_def->type, ds->type, sizeof (vl_def->type));
1960 if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
1962 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE_INSTANCE,
1964 strlen (vl->type_instance)) != 0)
1966 sstrncpy (vl_def->type_instance, vl->type_instance, sizeof (vl_def->type_instance));
1969 if (write_part_values (&buffer, &buffer_size, ds, vl) != 0)
1972 return (buffer - buffer_orig);
1973 } /* }}} int add_to_buffer */
1975 static void flush_buffer (void)
1977 DEBUG ("network plugin: flush_buffer: send_buffer_fill = %i",
1980 network_send_buffer (send_buffer, (size_t) send_buffer_fill);
1981 network_init_buffer ();
1984 static int network_write (const data_set_t *ds, const value_list_t *vl,
1985 user_data_t __attribute__((unused)) *user_data)
1989 /* If the value is already in the cache, we have received it via the
1990 * network. We write it again if forwarding is activated. It's then in
1991 * the cache and should we receive it again we will ignore it. */
1992 status = cache_check (vl);
1993 if ((network_config_forward == 0)
1997 pthread_mutex_lock (&send_buffer_lock);
1999 status = add_to_buffer (send_buffer_ptr,
2000 sizeof (send_buffer) - (send_buffer_fill + BUFF_SIG_SIZE),
2005 /* status == bytes added to the buffer */
2006 send_buffer_fill += status;
2007 send_buffer_ptr += status;
2013 status = add_to_buffer (send_buffer_ptr,
2014 sizeof (send_buffer) - (send_buffer_fill + BUFF_SIG_SIZE),
2020 send_buffer_fill += status;
2021 send_buffer_ptr += status;
2027 ERROR ("network plugin: Unable to append to the "
2028 "buffer for some weird reason");
2030 else if ((sizeof (send_buffer) - send_buffer_fill) < 15)
2035 pthread_mutex_unlock (&send_buffer_lock);
2037 return ((status < 0) ? -1 : 0);
2038 } /* int network_write */
2040 static int network_config_set_boolean (const oconfig_item_t *ci, /* {{{ */
2043 if ((ci->values_num != 1)
2044 || ((ci->values[0].type != OCONFIG_TYPE_BOOLEAN)
2045 && (ci->values[0].type != OCONFIG_TYPE_STRING)))
2047 ERROR ("network plugin: The `%s' config option needs "
2048 "exactly one boolean argument.", ci->key);
2052 if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
2054 if (ci->values[0].value.boolean)
2061 char *str = ci->values[0].value.string;
2063 if ((strcasecmp ("true", str) == 0)
2064 || (strcasecmp ("yes", str) == 0)
2065 || (strcasecmp ("on", str) == 0))
2067 else if ((strcasecmp ("false", str) == 0)
2068 || (strcasecmp ("no", str) == 0)
2069 || (strcasecmp ("off", str) == 0))
2073 ERROR ("network plugin: Cannot parse string value `%s' of the `%s' "
2074 "option as boolean value.",
2081 } /* }}} int network_config_set_boolean */
2083 static int network_config_set_ttl (const oconfig_item_t *ci) /* {{{ */
2086 if ((ci->values_num != 1)
2087 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
2089 WARNING ("network plugin: The `TimeToLive' config option needs exactly "
2090 "one numeric argument.");
2094 tmp = (int) ci->values[0].value.number;
2095 if ((tmp > 0) && (tmp <= 255))
2096 network_config_ttl = tmp;
2099 } /* }}} int network_config_set_ttl */
2102 static int network_config_set_security_level (oconfig_item_t *ci, /* {{{ */
2106 if ((ci->values_num != 1)
2107 || (ci->values[0].type != OCONFIG_TYPE_STRING))
2109 WARNING ("network plugin: The `SecurityLevel' config option needs exactly "
2110 "one string argument.");
2114 str = ci->values[0].value.string;
2115 if (strcasecmp ("Encrypt", str) == 0)
2116 *retval = SECURITY_LEVEL_ENCRYPT;
2117 else if (strcasecmp ("Sign", str) == 0)
2118 *retval = SECURITY_LEVEL_SIGN;
2119 else if (strcasecmp ("None", str) == 0)
2120 *retval = SECURITY_LEVEL_NONE;
2123 WARNING ("network plugin: Unknown security level: %s.", str);
2128 } /* }}} int network_config_set_security_level */
2129 #endif /* HAVE_GCRYPT_H */
2131 static int network_config_listen_server (const oconfig_item_t *ci) /* {{{ */
2135 char *shared_secret = NULL;
2136 int security_level = SECURITY_LEVEL_NONE;
2139 if ((ci->values_num < 1) || (ci->values_num > 2)
2140 || (ci->values[0].type != OCONFIG_TYPE_STRING)
2141 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
2143 ERROR ("network plugin: The `%s' config option needs "
2144 "one or two string arguments.", ci->key);
2148 node = ci->values[0].value.string;
2149 if (ci->values_num >= 2)
2150 service = ci->values[1].value.string;
2154 for (i = 0; i < ci->children_num; i++)
2156 oconfig_item_t *child = ci->children + i;
2159 if (strcasecmp ("Secret", child->key) == 0)
2161 if ((child->values_num == 1)
2162 && (child->values[0].type == OCONFIG_TYPE_STRING))
2163 shared_secret = child->values[0].value.string;
2165 ERROR ("network plugin: The `Secret' option needs exactly one string "
2168 else if (strcasecmp ("SecurityLevel", child->key) == 0)
2169 network_config_set_security_level (child, &security_level);
2171 #endif /* HAVE_GCRYPT_H */
2173 WARNING ("network plugin: Option `%s' is not allowed here.",
2178 if ((security_level > SECURITY_LEVEL_NONE) && (shared_secret == NULL))
2180 ERROR ("network plugin: A security level higher than `none' was "
2181 "requested, but no shared key was given. Cowardly refusing to open "
2186 if (strcasecmp ("Listen", ci->key) == 0)
2187 network_add_listen_socket (node, service, shared_secret, security_level);
2189 network_add_sending_socket (node, service, shared_secret, security_level);
2192 } /* }}} int network_config_listen_server */
2194 static int network_config_set_cache_flush (const oconfig_item_t *ci) /* {{{ */
2197 if ((ci->values_num != 1)
2198 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
2200 WARNING ("network plugin: The `CacheFlush' config option needs exactly "
2201 "one numeric argument.");
2205 tmp = (int) ci->values[0].value.number;
2207 network_config_ttl = tmp;
2210 } /* }}} int network_config_set_cache_flush */
2212 static int network_config (oconfig_item_t *ci) /* {{{ */
2216 for (i = 0; i < ci->children_num; i++)
2218 oconfig_item_t *child = ci->children + i;
2220 if ((strcasecmp ("Listen", child->key) == 0)
2221 || (strcasecmp ("Server", child->key) == 0))
2222 network_config_listen_server (child);
2223 else if (strcasecmp ("TimeToLive", child->key) == 0)
2224 network_config_set_ttl (child);
2225 else if (strcasecmp ("Forward", child->key) == 0)
2226 network_config_set_boolean (child, &network_config_forward);
2227 else if (strcasecmp ("CacheFlush", child->key) == 0)
2228 network_config_set_cache_flush (child);
2231 WARNING ("network plugin: Option `%s' is not allowed here.",
2237 } /* }}} int network_config */
2239 static int network_notification (const notification_t *n,
2240 user_data_t __attribute__((unused)) *user_data)
2242 char buffer[BUFF_SIZE];
2243 char *buffer_ptr = buffer;
2244 int buffer_free = sizeof (buffer);
2247 memset (buffer, '\0', sizeof (buffer));
2250 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_TIME,
2251 (uint64_t) n->time);
2255 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_SEVERITY,
2256 (uint64_t) n->severity);
2260 if (strlen (n->host) > 0)
2262 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_HOST,
2263 n->host, strlen (n->host));
2268 if (strlen (n->plugin) > 0)
2270 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_PLUGIN,
2271 n->plugin, strlen (n->plugin));
2276 if (strlen (n->plugin_instance) > 0)
2278 status = write_part_string (&buffer_ptr, &buffer_free,
2279 TYPE_PLUGIN_INSTANCE,
2280 n->plugin_instance, strlen (n->plugin_instance));
2285 if (strlen (n->type) > 0)
2287 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE,
2288 n->type, strlen (n->type));
2293 if (strlen (n->type_instance) > 0)
2295 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE_INSTANCE,
2296 n->type_instance, strlen (n->type_instance));
2301 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_MESSAGE,
2302 n->message, strlen (n->message));
2306 network_send_buffer (buffer, sizeof (buffer) - buffer_free);
2309 } /* int network_notification */
2311 static int network_shutdown (void)
2315 /* Kill the listening thread */
2316 if (receive_thread_running != 0)
2318 INFO ("network plugin: Stopping receive thread.");
2319 pthread_kill (receive_thread_id, SIGTERM);
2320 pthread_join (receive_thread_id, NULL /* no return value */);
2321 memset (&receive_thread_id, 0, sizeof (receive_thread_id));
2322 receive_thread_running = 0;
2325 /* Shutdown the dispatching thread */
2326 if (dispatch_thread_running != 0)
2328 INFO ("network plugin: Stopping dispatch thread.");
2329 pthread_mutex_lock (&receive_list_lock);
2330 pthread_cond_broadcast (&receive_list_cond);
2331 pthread_mutex_unlock (&receive_list_lock);
2332 pthread_join (dispatch_thread_id, /* ret = */ NULL);
2333 dispatch_thread_running = 0;
2336 free_sockent (listen_sockets);
2338 if (send_buffer_fill > 0)
2341 if (cache_tree != NULL)
2346 while (c_avl_pick (cache_tree, &key, &value) == 0)
2351 c_avl_destroy (cache_tree);
2355 /* TODO: Close `sending_sockets' */
2357 plugin_unregister_config ("network");
2358 plugin_unregister_init ("network");
2359 plugin_unregister_write ("network");
2360 plugin_unregister_shutdown ("network");
2362 /* Let the init function do it's move again ;) */
2363 cache_flush_last = 0;
2366 } /* int network_shutdown */
2368 static int network_init (void)
2370 /* Check if we were already initialized. If so, just return - there's
2371 * nothing more to do (for now, that is). */
2372 if (cache_flush_last != 0)
2375 plugin_register_shutdown ("network", network_shutdown);
2377 network_init_buffer ();
2379 cache_tree = c_avl_create ((int (*) (const void *, const void *)) strcmp);
2380 cache_flush_last = time (NULL);
2382 /* setup socket(s) and so on */
2383 if (sending_sockets != NULL)
2385 plugin_register_write ("network", network_write,
2386 /* user_data = */ NULL);
2387 plugin_register_notification ("network", network_notification,
2388 /* user_data = */ NULL);
2391 /* If no threads need to be started, return here. */
2392 if ((listen_sockets_num == 0)
2393 || ((dispatch_thread_running != 0)
2394 && (receive_thread_running != 0)))
2397 if (dispatch_thread_running == 0)
2400 status = pthread_create (&dispatch_thread_id,
2401 NULL /* no attributes */,
2403 NULL /* no argument */);
2407 ERROR ("network: pthread_create failed: %s",
2408 sstrerror (errno, errbuf,
2413 dispatch_thread_running = 1;
2417 if (receive_thread_running == 0)
2420 status = pthread_create (&receive_thread_id,
2421 NULL /* no attributes */,
2423 NULL /* no argument */);
2427 ERROR ("network: pthread_create failed: %s",
2428 sstrerror (errno, errbuf,
2433 receive_thread_running = 1;
2438 } /* int network_init */
2441 * The flush option of the network plugin cannot flush individual identifiers.
2442 * All the values are added to a buffer and sent when the buffer is full, the
2443 * requested value may or may not be in there, it's not worth finding out. We
2444 * just send the buffer if `flush' is called - if the requested value was in
2445 * there, good. If not, well, then there is nothing to flush.. -octo
2447 static int network_flush (int timeout,
2448 const char __attribute__((unused)) *identifier,
2449 user_data_t __attribute__((unused)) *user_data)
2451 pthread_mutex_lock (&send_buffer_lock);
2453 if (((time (NULL) - cache_flush_last) >= timeout)
2454 && (send_buffer_fill > 0))
2459 pthread_mutex_unlock (&send_buffer_lock);
2462 } /* int network_flush */
2464 void module_register (void)
2466 plugin_register_complex_config ("network", network_config);
2467 plugin_register_init ("network", network_init);
2468 plugin_register_flush ("network", network_flush,
2469 /* user_data = */ NULL);
2470 } /* void module_register */
2472 /* vim: set fdm=marker : */