2 * collectd - src/network.c
3 * Copyright (C) 2005-2009 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
22 #define _BSD_SOURCE /* For struct ip_mreq */
27 #include "configfile.h"
28 #include "utils_fbhash.h"
29 #include "utils_avltree.h"
30 #include "utils_cache.h"
38 # include <sys/socket.h>
44 # include <netinet/in.h>
47 # include <arpa/inet.h>
57 #ifndef IPV6_ADD_MEMBERSHIP
58 # ifdef IPV6_JOIN_GROUP
59 # define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
61 # error "Neither IP_ADD_MEMBERSHIP nor IPV6_JOIN_GROUP is defined"
63 #endif /* !IP_ADD_MEMBERSHIP */
66 * Maximum size required for encryption / signing:
68 * 42 bytes for the encryption header
69 * + 64 bytes for the username
73 #define BUFF_SIG_SIZE 106
78 #define SECURITY_LEVEL_NONE 0
80 # define SECURITY_LEVEL_SIGN 1
81 # define SECURITY_LEVEL_ENCRYPT 2
86 struct sockaddr_storage *addr;
92 gcry_cipher_hd_t cypher;
93 unsigned char password_hash[32];
105 gcry_cipher_hd_t cypher;
109 typedef struct sockent
111 #define SOCKENT_TYPE_CLIENT 1
112 #define SOCKENT_TYPE_SERVER 2
120 struct sockent_client client;
121 struct sockent_server server;
124 struct sockent *next;
127 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
128 * 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
129 * +-------+-----------------------+-------------------------------+
130 * ! Ver. ! ! Length !
131 * +-------+-----------------------+-------------------------------+
138 typedef struct part_header_s part_header_t;
140 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
141 * 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
142 * +-------------------------------+-------------------------------+
144 * +-------------------------------+-------------------------------+
145 * : (Length - 4) Bytes :
146 * +---------------------------------------------------------------+
153 typedef struct part_string_s part_string_t;
155 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
156 * 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
157 * +-------------------------------+-------------------------------+
159 * +-------------------------------+-------------------------------+
160 * : (Length - 4 == 2 || 4 || 8) Bytes :
161 * +---------------------------------------------------------------+
168 typedef struct part_number_s part_number_t;
170 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
171 * 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
172 * +-------------------------------+-------------------------------+
174 * +-------------------------------+---------------+---------------+
175 * ! Num of values ! Type0 ! Type1 !
176 * +-------------------------------+---------------+---------------+
179 * +---------------------------------------------------------------+
182 * +---------------------------------------------------------------+
187 uint16_t *num_values;
188 uint8_t *values_types;
191 typedef struct part_values_s part_values_t;
193 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
194 * 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
195 * +-------------------------------+-------------------------------+
197 * +-------------------------------+-------------------------------+
198 * ! Hash (Bits 0 - 31) !
200 * ! Hash (Bits 224 - 255) !
201 * +---------------------------------------------------------------+
204 #define PART_SIGNATURE_SHA256_SIZE 36
205 struct part_signature_sha256_s
208 unsigned char hash[32];
211 typedef struct part_signature_sha256_s part_signature_sha256_t;
213 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
214 * 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
215 * +-------------------------------+-------------------------------+
217 * +-------------------------------+-------------------------------+
218 * ! Original length ! Padding (0 - 15 bytes) !
219 * +-------------------------------+-------------------------------+
220 * ! Hash (Bits 0 - 31) !
222 * ! Hash (Bits 128 - 159) !
223 * +---------------------------------------------------------------+
226 #define PART_ENCRYPTION_AES256_SIZE 42
227 struct part_encryption_aes256_s
230 uint16_t username_length;
232 unsigned char iv[16];
234 unsigned char hash[20];
238 typedef struct part_encryption_aes256_s part_encryption_aes256_t;
240 struct receive_list_entry_s
245 struct receive_list_entry_s *next;
247 typedef struct receive_list_entry_s receive_list_entry_t;
252 static int network_config_ttl = 0;
253 static size_t network_config_packet_size = 1024;
254 static int network_config_forward = 0;
256 static sockent_t *sending_sockets = NULL;
258 static receive_list_entry_t *receive_list_head = NULL;
259 static receive_list_entry_t *receive_list_tail = NULL;
260 static pthread_mutex_t receive_list_lock = PTHREAD_MUTEX_INITIALIZER;
261 static pthread_cond_t receive_list_cond = PTHREAD_COND_INITIALIZER;
263 static sockent_t *listen_sockets = NULL;
264 static struct pollfd *listen_sockets_pollfd = NULL;
265 static size_t listen_sockets_num = 0;
267 /* The receive and dispatch threads will run as long as `listen_loop' is set to
269 static int listen_loop = 0;
270 static int receive_thread_running = 0;
271 static pthread_t receive_thread_id;
272 static int dispatch_thread_running = 0;
273 static pthread_t dispatch_thread_id;
275 /* Buffer in which to-be-sent network packets are constructed. */
276 static char *send_buffer;
277 static char *send_buffer_ptr;
278 static int send_buffer_fill;
279 static value_list_t send_buffer_vl = VALUE_LIST_STATIC;
280 static pthread_mutex_t send_buffer_lock = PTHREAD_MUTEX_INITIALIZER;
285 static _Bool check_receive_okay (const value_list_t *vl) /* {{{ */
287 uint64_t time_sent = 0;
290 status = uc_meta_data_get_unsigned_int (vl,
291 "network:time_sent", &time_sent);
293 /* This is a value we already sent. Don't allow it to be received again in
294 * order to avoid looping. */
295 if ((status == 0) && (time_sent >= ((uint64_t) vl->time)))
299 } /* }}} _Bool check_receive_okay */
301 static _Bool check_send_okay (const value_list_t *vl) /* {{{ */
303 _Bool received = false;
306 if (network_config_forward != 0)
309 if (vl->meta == NULL)
312 status = meta_data_get_boolean (vl->meta, "network:received", &received);
313 if (status == -ENOENT)
315 else if (status != 0)
317 ERROR ("network plugin: check_send_okay: meta_data_get_boolean failed "
318 "with status %i.", status);
322 /* By default, only *send* value lists that were not *received* by the
325 } /* }}} _Bool check_send_okay */
327 static int network_dispatch_values (value_list_t *vl) /* {{{ */
332 || (strlen (vl->host) <= 0)
333 || (strlen (vl->plugin) <= 0)
334 || (strlen (vl->type) <= 0))
337 if (!check_receive_okay (vl))
340 char name[6*DATA_MAX_NAME_LEN];
341 FORMAT_VL (name, sizeof (name), vl);
342 name[sizeof (name) - 1] = 0;
343 DEBUG ("network plugin: network_dispatch_values: "
344 "NOT dispatching %s.", name);
349 assert (vl->meta == NULL);
351 vl->meta = meta_data_create ();
352 if (vl->meta == NULL)
354 ERROR ("network plugin: meta_data_create failed.");
358 status = meta_data_add_boolean (vl->meta, "network:received", true);
361 ERROR ("network plugin: meta_data_add_boolean failed.");
362 meta_data_destroy (vl->meta);
367 plugin_dispatch_values (vl);
369 meta_data_destroy (vl->meta);
373 } /* }}} int network_dispatch_values */
376 static gcry_cipher_hd_t network_get_aes256_cypher (sockent_t *se, /* {{{ */
377 const void *iv, size_t iv_size, const char *username)
380 gcry_cipher_hd_t *cyper_ptr;
381 unsigned char password_hash[32];
383 if (se->type == SOCKENT_TYPE_CLIENT)
385 cyper_ptr = &se->data.client.cypher;
386 memcpy (password_hash, se->data.client.password_hash,
387 sizeof (password_hash));
393 cyper_ptr = &se->data.server.cypher;
395 if (username == NULL)
398 secret = fbh_get (se->data.server.userdb, username);
402 gcry_md_hash_buffer (GCRY_MD_SHA256,
404 secret, strlen (secret));
409 if (*cyper_ptr == NULL)
411 err = gcry_cipher_open (cyper_ptr,
412 GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_OFB, /* flags = */ 0);
415 ERROR ("network plugin: gcry_cipher_open returned: %s",
416 gcry_strerror (err));
423 gcry_cipher_reset (*cyper_ptr);
425 assert (*cyper_ptr != NULL);
427 err = gcry_cipher_setkey (*cyper_ptr,
428 password_hash, sizeof (password_hash));
431 ERROR ("network plugin: gcry_cipher_setkey returned: %s",
432 gcry_strerror (err));
433 gcry_cipher_close (*cyper_ptr);
438 err = gcry_cipher_setiv (*cyper_ptr, iv, iv_size);
441 ERROR ("network plugin: gcry_cipher_setkey returned: %s",
442 gcry_strerror (err));
443 gcry_cipher_close (*cyper_ptr);
449 } /* }}} int network_get_aes256_cypher */
450 #endif /* HAVE_LIBGCRYPT */
452 static int write_part_values (char **ret_buffer, int *ret_buffer_len,
453 const data_set_t *ds, const value_list_t *vl)
459 part_header_t pkg_ph;
460 uint16_t pkg_num_values;
461 uint8_t *pkg_values_types;
467 num_values = vl->values_len;
468 packet_len = sizeof (part_header_t) + sizeof (uint16_t)
469 + (num_values * sizeof (uint8_t))
470 + (num_values * sizeof (value_t));
472 if (*ret_buffer_len < packet_len)
475 pkg_values_types = (uint8_t *) malloc (num_values * sizeof (uint8_t));
476 if (pkg_values_types == NULL)
478 ERROR ("network plugin: write_part_values: malloc failed.");
482 pkg_values = (value_t *) malloc (num_values * sizeof (value_t));
483 if (pkg_values == NULL)
485 free (pkg_values_types);
486 ERROR ("network plugin: write_part_values: malloc failed.");
490 pkg_ph.type = htons (TYPE_VALUES);
491 pkg_ph.length = htons (packet_len);
493 pkg_num_values = htons ((uint16_t) vl->values_len);
495 for (i = 0; i < num_values; i++)
497 pkg_values_types[i] = (uint8_t) ds->ds[i].type;
498 switch (ds->ds[i].type)
500 case DS_TYPE_COUNTER:
501 pkg_values[i].counter = htonll (vl->values[i].counter);
505 pkg_values[i].gauge = htond (vl->values[i].gauge);
509 pkg_values[i].derive = htonll (vl->values[i].derive);
512 case DS_TYPE_ABSOLUTE:
513 pkg_values[i].absolute = htonll (vl->values[i].absolute);
517 free (pkg_values_types);
519 ERROR ("network plugin: write_part_values: "
520 "Unknown data source type: %i",
523 } /* switch (ds->ds[i].type) */
524 } /* for (num_values) */
527 * Use `memcpy' to write everything to the buffer, because the pointer
528 * may be unaligned and some architectures, such as SPARC, can't handle
531 packet_ptr = *ret_buffer;
533 memcpy (packet_ptr + offset, &pkg_ph, sizeof (pkg_ph));
534 offset += sizeof (pkg_ph);
535 memcpy (packet_ptr + offset, &pkg_num_values, sizeof (pkg_num_values));
536 offset += sizeof (pkg_num_values);
537 memcpy (packet_ptr + offset, pkg_values_types, num_values * sizeof (uint8_t));
538 offset += num_values * sizeof (uint8_t);
539 memcpy (packet_ptr + offset, pkg_values, num_values * sizeof (value_t));
540 offset += num_values * sizeof (value_t);
542 assert (offset == packet_len);
544 *ret_buffer = packet_ptr + packet_len;
545 *ret_buffer_len -= packet_len;
547 free (pkg_values_types);
551 } /* int write_part_values */
553 static int write_part_number (char **ret_buffer, int *ret_buffer_len,
554 int type, uint64_t value)
559 part_header_t pkg_head;
564 packet_len = sizeof (pkg_head) + sizeof (pkg_value);
566 if (*ret_buffer_len < packet_len)
569 pkg_head.type = htons (type);
570 pkg_head.length = htons (packet_len);
571 pkg_value = htonll (value);
573 packet_ptr = *ret_buffer;
575 memcpy (packet_ptr + offset, &pkg_head, sizeof (pkg_head));
576 offset += sizeof (pkg_head);
577 memcpy (packet_ptr + offset, &pkg_value, sizeof (pkg_value));
578 offset += sizeof (pkg_value);
580 assert (offset == packet_len);
582 *ret_buffer = packet_ptr + packet_len;
583 *ret_buffer_len -= packet_len;
586 } /* int write_part_number */
588 static int write_part_string (char **ret_buffer, int *ret_buffer_len,
589 int type, const char *str, int str_len)
599 buffer_len = 2 * sizeof (uint16_t) + str_len + 1;
600 if (*ret_buffer_len < buffer_len)
603 pkg_type = htons (type);
604 pkg_length = htons (buffer_len);
606 buffer = *ret_buffer;
608 memcpy (buffer + offset, (void *) &pkg_type, sizeof (pkg_type));
609 offset += sizeof (pkg_type);
610 memcpy (buffer + offset, (void *) &pkg_length, sizeof (pkg_length));
611 offset += sizeof (pkg_length);
612 memcpy (buffer + offset, str, str_len);
614 memset (buffer + offset, '\0', 1);
617 assert (offset == buffer_len);
619 *ret_buffer = buffer + buffer_len;
620 *ret_buffer_len -= buffer_len;
623 } /* int write_part_string */
625 static int parse_part_values (void **ret_buffer, size_t *ret_buffer_len,
626 value_t **ret_values, int *ret_num_values)
628 char *buffer = *ret_buffer;
629 size_t buffer_len = *ret_buffer_len;
644 NOTICE ("network plugin: packet is too short: "
645 "buffer_len = %zu", buffer_len);
649 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
650 buffer += sizeof (tmp16);
651 pkg_type = ntohs (tmp16);
653 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
654 buffer += sizeof (tmp16);
655 pkg_length = ntohs (tmp16);
657 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
658 buffer += sizeof (tmp16);
659 pkg_numval = ntohs (tmp16);
661 assert (pkg_type == TYPE_VALUES);
663 exp_size = 3 * sizeof (uint16_t)
664 + pkg_numval * (sizeof (uint8_t) + sizeof (value_t));
665 if ((buffer_len < 0) || (buffer_len < exp_size))
667 WARNING ("network plugin: parse_part_values: "
669 "Chunk of size %zu expected, "
670 "but buffer has only %zu bytes left.",
671 exp_size, buffer_len);
675 if (pkg_length != exp_size)
677 WARNING ("network plugin: parse_part_values: "
678 "Length and number of values "
679 "in the packet don't match.");
683 pkg_types = (uint8_t *) malloc (pkg_numval * sizeof (uint8_t));
684 pkg_values = (value_t *) malloc (pkg_numval * sizeof (value_t));
685 if ((pkg_types == NULL) || (pkg_values == NULL))
689 ERROR ("network plugin: parse_part_values: malloc failed.");
693 memcpy ((void *) pkg_types, (void *) buffer, pkg_numval * sizeof (uint8_t));
694 buffer += pkg_numval * sizeof (uint8_t);
695 memcpy ((void *) pkg_values, (void *) buffer, pkg_numval * sizeof (value_t));
696 buffer += pkg_numval * sizeof (value_t);
698 for (i = 0; i < pkg_numval; i++)
700 switch (pkg_types[i])
702 case DS_TYPE_COUNTER:
703 pkg_values[i].counter = (counter_t) ntohll (pkg_values[i].counter);
707 pkg_values[i].gauge = (gauge_t) ntohd (pkg_values[i].gauge);
711 pkg_values[i].derive = (derive_t) ntohll (pkg_values[i].derive);
714 case DS_TYPE_ABSOLUTE:
715 pkg_values[i].absolute = (absolute_t) ntohll (pkg_values[i].absolute);
721 NOTICE ("network plugin: parse_part_values: "
722 "Don't know how to handle data source type %"PRIu8,
725 } /* switch (pkg_types[i]) */
728 *ret_buffer = buffer;
729 *ret_buffer_len = buffer_len - pkg_length;
730 *ret_num_values = pkg_numval;
731 *ret_values = pkg_values;
736 } /* int parse_part_values */
738 static int parse_part_number (void **ret_buffer, size_t *ret_buffer_len,
741 char *buffer = *ret_buffer;
742 size_t buffer_len = *ret_buffer_len;
746 size_t exp_size = 2 * sizeof (uint16_t) + sizeof (uint64_t);
751 if ((buffer_len < 0) || ((size_t) buffer_len < exp_size))
753 WARNING ("network plugin: parse_part_number: "
755 "Chunk of size %zu expected, "
756 "but buffer has only %zu bytes left.",
757 exp_size, buffer_len);
761 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
762 buffer += sizeof (tmp16);
763 pkg_type = ntohs (tmp16);
765 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
766 buffer += sizeof (tmp16);
767 pkg_length = ntohs (tmp16);
769 memcpy ((void *) &tmp64, buffer, sizeof (tmp64));
770 buffer += sizeof (tmp64);
771 *value = ntohll (tmp64);
773 *ret_buffer = buffer;
774 *ret_buffer_len = buffer_len - pkg_length;
777 } /* int parse_part_number */
779 static int parse_part_string (void **ret_buffer, size_t *ret_buffer_len,
780 char *output, int output_len)
782 char *buffer = *ret_buffer;
783 size_t buffer_len = *ret_buffer_len;
786 size_t header_size = 2 * sizeof (uint16_t);
791 if ((buffer_len < 0) || (buffer_len < header_size))
793 WARNING ("network plugin: parse_part_string: "
795 "Chunk of at least size %zu expected, "
796 "but buffer has only %zu bytes left.",
797 header_size, buffer_len);
801 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
802 buffer += sizeof (tmp16);
803 pkg_type = ntohs (tmp16);
805 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
806 buffer += sizeof (tmp16);
807 pkg_length = ntohs (tmp16);
809 /* Check that packet fits in the input buffer */
810 if (pkg_length > buffer_len)
812 WARNING ("network plugin: parse_part_string: "
814 "Chunk of size %"PRIu16" received, "
815 "but buffer has only %zu bytes left.",
816 pkg_length, buffer_len);
820 /* Check that pkg_length is in the valid range */
821 if (pkg_length <= header_size)
823 WARNING ("network plugin: parse_part_string: "
825 "Header claims this packet is only %hu "
826 "bytes long.", pkg_length);
830 /* Check that the package data fits into the output buffer.
831 * The previous if-statement ensures that:
832 * `pkg_length > header_size' */
834 || ((size_t) output_len < ((size_t) pkg_length - header_size)))
836 WARNING ("network plugin: parse_part_string: "
837 "Output buffer too small.");
841 /* All sanity checks successfull, let's copy the data over */
842 output_len = pkg_length - header_size;
843 memcpy ((void *) output, (void *) buffer, output_len);
844 buffer += output_len;
846 /* For some very weird reason '\0' doesn't do the trick on SPARC in
848 if (output[output_len - 1] != 0)
850 WARNING ("network plugin: parse_part_string: "
851 "Received string does not end "
852 "with a NULL-byte.");
856 *ret_buffer = buffer;
857 *ret_buffer_len = buffer_len - pkg_length;
860 } /* int parse_part_string */
862 /* Forward declaration: parse_part_sign_sha256 and parse_part_encr_aes256 call
863 * parse_packet and vice versa. */
864 #define PP_SIGNED 0x01
865 #define PP_ENCRYPTED 0x02
866 static int parse_packet (sockent_t *se,
867 void *buffer, size_t buffer_size, int flags);
869 #define BUFFER_READ(p,s) do { \
870 memcpy ((p), buffer + buffer_offset, (s)); \
871 buffer_offset += (s); \
875 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
876 void **ret_buffer, size_t *ret_buffer_len, int flags)
880 size_t buffer_offset;
885 part_signature_sha256_t pss;
886 uint16_t pss_head_length;
887 char hash[sizeof (pss.hash)];
891 unsigned char *hash_ptr;
893 buffer = *ret_buffer;
894 buffer_len = *ret_buffer_len;
897 if (se->data.server.userdb == NULL)
899 NOTICE ("network plugin: Received signed network packet but can't verify "
900 "it because no user DB has been configured. Will accept it.");
904 /* Check if the buffer has enough data for this structure. */
905 if (buffer_len <= PART_SIGNATURE_SHA256_SIZE)
908 /* Read type and length header */
909 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
910 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
911 pss_head_length = ntohs (pss.head.length);
913 /* Check if the `pss_head_length' is within bounds. */
914 if ((pss_head_length <= PART_SIGNATURE_SHA256_SIZE)
915 || (pss_head_length > buffer_len))
917 ERROR ("network plugin: HMAC-SHA-256 with invalid length received.");
922 BUFFER_READ (pss.hash, sizeof (pss.hash));
924 /* Calculate username length (without null byte) and allocate memory */
925 username_len = pss_head_length - PART_SIGNATURE_SHA256_SIZE;
926 pss.username = malloc (username_len + 1);
927 if (pss.username == NULL)
930 /* Read the username */
931 BUFFER_READ (pss.username, username_len);
932 pss.username[username_len] = 0;
934 assert (buffer_offset == pss_head_length);
936 /* Query the password */
937 secret = fbh_get (se->data.server.userdb, pss.username);
940 ERROR ("network plugin: Unknown user: %s", pss.username);
941 sfree (pss.username);
945 /* Create a hash device and check the HMAC */
947 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
950 ERROR ("network plugin: Creating HMAC-SHA-256 object failed: %s",
951 gcry_strerror (err));
953 sfree (pss.username);
957 err = gcry_md_setkey (hd, secret, strlen (secret));
960 ERROR ("network plugin: gcry_md_setkey failed: %s", gcry_strerror (err));
966 buffer + PART_SIGNATURE_SHA256_SIZE,
967 buffer_len - PART_SIGNATURE_SHA256_SIZE);
968 hash_ptr = gcry_md_read (hd, GCRY_MD_SHA256);
969 if (hash_ptr == NULL)
971 ERROR ("network plugin: gcry_md_read failed.");
974 sfree (pss.username);
977 memcpy (hash, hash_ptr, sizeof (hash));
984 sfree (pss.username);
986 if (memcmp (pss.hash, hash, sizeof (pss.hash)) != 0)
988 WARNING ("network plugin: Verifying HMAC-SHA-256 signature failed: "
993 parse_packet (se, buffer + buffer_offset, buffer_len - buffer_offset,
997 *ret_buffer = buffer + buffer_len;
1001 } /* }}} int parse_part_sign_sha256 */
1002 /* #endif HAVE_LIBGCRYPT */
1004 #else /* if !HAVE_LIBGCRYPT */
1005 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
1006 void **ret_buffer, size_t *ret_buffer_size, int flags)
1008 static int warning_has_been_printed = 0;
1012 size_t buffer_offset;
1015 part_signature_sha256_t pss;
1017 buffer = *ret_buffer;
1018 buffer_size = *ret_buffer_size;
1021 if (buffer_size <= PART_SIGNATURE_SHA256_SIZE)
1024 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
1025 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
1026 part_len = ntohs (pss.head.length);
1028 if ((part_len <= PART_SIGNATURE_SHA256_SIZE)
1029 || (part_len > buffer_size))
1032 if (warning_has_been_printed == 0)
1034 WARNING ("network plugin: Received signed packet, but the network "
1035 "plugin was not linked with libgcrypt, so I cannot "
1036 "verify the signature. The packet will be accepted.");
1037 warning_has_been_printed = 1;
1040 parse_packet (se, buffer + part_len, buffer_size - part_len, flags);
1042 *ret_buffer = buffer + buffer_size;
1043 *ret_buffer_size = 0;
1046 } /* }}} int parse_part_sign_sha256 */
1047 #endif /* !HAVE_LIBGCRYPT */
1050 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1051 void **ret_buffer, size_t *ret_buffer_len,
1054 char *buffer = *ret_buffer;
1055 size_t buffer_len = *ret_buffer_len;
1058 size_t buffer_offset;
1059 uint16_t username_len;
1060 part_encryption_aes256_t pea;
1061 unsigned char hash[sizeof (pea.hash)];
1063 gcry_cipher_hd_t cypher;
1066 /* Make sure at least the header if available. */
1067 if (buffer_len <= PART_ENCRYPTION_AES256_SIZE)
1069 NOTICE ("network plugin: parse_part_encr_aes256: "
1070 "Discarding short packet.");
1076 /* Copy the unencrypted information into `pea'. */
1077 BUFFER_READ (&pea.head.type, sizeof (pea.head.type));
1078 BUFFER_READ (&pea.head.length, sizeof (pea.head.length));
1080 /* Check the `part size'. */
1081 part_size = ntohs (pea.head.length);
1082 if ((part_size <= PART_ENCRYPTION_AES256_SIZE)
1083 || (part_size > buffer_len))
1085 NOTICE ("network plugin: parse_part_encr_aes256: "
1086 "Discarding part with invalid size.");
1090 /* Read the username */
1091 BUFFER_READ (&username_len, sizeof (username_len));
1092 username_len = ntohs (username_len);
1094 if ((username_len <= 0)
1095 || (username_len > (part_size - (PART_ENCRYPTION_AES256_SIZE + 1))))
1097 NOTICE ("network plugin: parse_part_encr_aes256: "
1098 "Discarding part with invalid username length.");
1102 assert (username_len > 0);
1103 pea.username = malloc (username_len + 1);
1104 if (pea.username == NULL)
1106 BUFFER_READ (pea.username, username_len);
1107 pea.username[username_len] = 0;
1109 /* Last but not least, the initialization vector */
1110 BUFFER_READ (pea.iv, sizeof (pea.iv));
1112 /* Make sure we are at the right position */
1113 assert (buffer_offset == (username_len +
1114 PART_ENCRYPTION_AES256_SIZE - sizeof (pea.hash)));
1116 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
1121 payload_len = part_size - (PART_ENCRYPTION_AES256_SIZE + username_len);
1122 assert (payload_len > 0);
1124 /* Decrypt the packet in-place */
1125 err = gcry_cipher_decrypt (cypher,
1126 buffer + buffer_offset,
1127 part_size - buffer_offset,
1128 /* in = */ NULL, /* in len = */ 0);
1131 ERROR ("network plugin: gcry_cipher_decrypt returned: %s",
1132 gcry_strerror (err));
1137 BUFFER_READ (pea.hash, sizeof (pea.hash));
1139 /* Make sure we're at the right position - again */
1140 assert (buffer_offset == (username_len + PART_ENCRYPTION_AES256_SIZE));
1141 assert (buffer_offset == (part_size - payload_len));
1143 /* Check hash sum */
1144 memset (hash, 0, sizeof (hash));
1145 gcry_md_hash_buffer (GCRY_MD_SHA1, hash,
1146 buffer + buffer_offset, payload_len);
1147 if (memcmp (hash, pea.hash, sizeof (hash)) != 0)
1149 ERROR ("network plugin: Decryption failed: Checksum mismatch.");
1153 parse_packet (se, buffer + buffer_offset, payload_len,
1154 flags | PP_ENCRYPTED);
1156 /* Update return values */
1157 *ret_buffer = buffer + part_size;
1158 *ret_buffer_len = buffer_len - part_size;
1161 } /* }}} int parse_part_encr_aes256 */
1162 /* #endif HAVE_LIBGCRYPT */
1164 #else /* if !HAVE_LIBGCRYPT */
1165 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1166 void **ret_buffer, size_t *ret_buffer_size, int flags)
1168 static int warning_has_been_printed = 0;
1172 size_t buffer_offset;
1177 buffer = *ret_buffer;
1178 buffer_size = *ret_buffer_size;
1181 /* parse_packet assures this minimum size. */
1182 assert (buffer_size >= (sizeof (ph.type) + sizeof (ph.length)));
1184 BUFFER_READ (&ph.type, sizeof (ph.type));
1185 BUFFER_READ (&ph.length, sizeof (ph.length));
1186 ph_length = ntohs (ph.length);
1188 if ((ph_length <= PART_ENCRYPTION_AES256_SIZE)
1189 || (ph_length > buffer_size))
1191 ERROR ("network plugin: AES-256 encrypted part "
1192 "with invalid length received.");
1196 if (warning_has_been_printed == 0)
1198 WARNING ("network plugin: Received encrypted packet, but the network "
1199 "plugin was not linked with libgcrypt, so I cannot "
1200 "decrypt it. The part will be discarded.");
1201 warning_has_been_printed = 1;
1204 *ret_buffer += ph_length;
1205 *ret_buffer_size -= ph_length;
1208 } /* }}} int parse_part_encr_aes256 */
1209 #endif /* !HAVE_LIBGCRYPT */
1213 static int parse_packet (sockent_t *se, /* {{{ */
1214 void *buffer, size_t buffer_size, int flags)
1218 value_list_t vl = VALUE_LIST_INIT;
1222 int packet_was_signed = (flags & PP_SIGNED);
1223 int packet_was_encrypted = (flags & PP_ENCRYPTED);
1224 int printed_ignore_warning = 0;
1225 #endif /* HAVE_LIBGCRYPT */
1228 memset (&vl, '\0', sizeof (vl));
1229 memset (&n, '\0', sizeof (n));
1232 while ((status == 0) && (0 < buffer_size)
1233 && ((unsigned int) buffer_size > sizeof (part_header_t)))
1235 uint16_t pkg_length;
1238 memcpy ((void *) &pkg_type,
1241 memcpy ((void *) &pkg_length,
1242 (void *) (buffer + sizeof (pkg_type)),
1243 sizeof (pkg_length));
1245 pkg_length = ntohs (pkg_length);
1246 pkg_type = ntohs (pkg_type);
1248 if (pkg_length > buffer_size)
1250 /* Ensure that this loop terminates eventually */
1251 if (pkg_length < (2 * sizeof (uint16_t)))
1254 if (pkg_type == TYPE_ENCR_AES256)
1256 status = parse_part_encr_aes256 (se,
1257 &buffer, &buffer_size, flags);
1260 ERROR ("network plugin: Decrypting AES256 "
1262 "with status %i.", status);
1267 else if ((se->data.server.security_level == SECURITY_LEVEL_ENCRYPT)
1268 && (packet_was_encrypted == 0))
1270 if (printed_ignore_warning == 0)
1272 INFO ("network plugin: Unencrypted packet or "
1273 "part has been ignored.");
1274 printed_ignore_warning = 1;
1276 buffer = ((char *) buffer) + pkg_length;
1279 #endif /* HAVE_LIBGCRYPT */
1280 else if (pkg_type == TYPE_SIGN_SHA256)
1282 status = parse_part_sign_sha256 (se,
1283 &buffer, &buffer_size, flags);
1286 ERROR ("network plugin: Verifying HMAC-SHA-256 "
1288 "with status %i.", status);
1293 else if ((se->data.server.security_level == SECURITY_LEVEL_SIGN)
1294 && (packet_was_encrypted == 0)
1295 && (packet_was_signed == 0))
1297 if (printed_ignore_warning == 0)
1299 INFO ("network plugin: Unsigned packet or "
1300 "part has been ignored.");
1301 printed_ignore_warning = 1;
1303 buffer = ((char *) buffer) + pkg_length;
1306 #endif /* HAVE_LIBGCRYPT */
1307 else if (pkg_type == TYPE_VALUES)
1309 status = parse_part_values (&buffer, &buffer_size,
1310 &vl.values, &vl.values_len);
1314 network_dispatch_values (&vl);
1318 else if (pkg_type == TYPE_TIME)
1321 status = parse_part_number (&buffer, &buffer_size,
1325 vl.time = (time_t) tmp;
1326 n.time = (time_t) tmp;
1329 else if (pkg_type == TYPE_INTERVAL)
1332 status = parse_part_number (&buffer, &buffer_size,
1335 vl.interval = (int) tmp;
1337 else if (pkg_type == TYPE_HOST)
1339 status = parse_part_string (&buffer, &buffer_size,
1340 vl.host, sizeof (vl.host));
1342 sstrncpy (n.host, vl.host, sizeof (n.host));
1344 else if (pkg_type == TYPE_PLUGIN)
1346 status = parse_part_string (&buffer, &buffer_size,
1347 vl.plugin, sizeof (vl.plugin));
1349 sstrncpy (n.plugin, vl.plugin,
1352 else if (pkg_type == TYPE_PLUGIN_INSTANCE)
1354 status = parse_part_string (&buffer, &buffer_size,
1356 sizeof (vl.plugin_instance));
1358 sstrncpy (n.plugin_instance,
1360 sizeof (n.plugin_instance));
1362 else if (pkg_type == TYPE_TYPE)
1364 status = parse_part_string (&buffer, &buffer_size,
1365 vl.type, sizeof (vl.type));
1367 sstrncpy (n.type, vl.type, sizeof (n.type));
1369 else if (pkg_type == TYPE_TYPE_INSTANCE)
1371 status = parse_part_string (&buffer, &buffer_size,
1373 sizeof (vl.type_instance));
1375 sstrncpy (n.type_instance, vl.type_instance,
1376 sizeof (n.type_instance));
1378 else if (pkg_type == TYPE_MESSAGE)
1380 status = parse_part_string (&buffer, &buffer_size,
1381 n.message, sizeof (n.message));
1387 else if ((n.severity != NOTIF_FAILURE)
1388 && (n.severity != NOTIF_WARNING)
1389 && (n.severity != NOTIF_OKAY))
1391 INFO ("network plugin: "
1392 "Ignoring notification with "
1393 "unknown severity %i.",
1396 else if (n.time <= 0)
1398 INFO ("network plugin: "
1399 "Ignoring notification with "
1402 else if (strlen (n.message) <= 0)
1404 INFO ("network plugin: "
1405 "Ignoring notification with "
1406 "an empty message.");
1410 plugin_dispatch_notification (&n);
1413 else if (pkg_type == TYPE_SEVERITY)
1416 status = parse_part_number (&buffer, &buffer_size,
1419 n.severity = (int) tmp;
1423 DEBUG ("network plugin: parse_packet: Unknown part"
1424 " type: 0x%04hx", pkg_type);
1425 buffer = ((char *) buffer) + pkg_length;
1427 } /* while (buffer_size > sizeof (part_header_t)) */
1429 if (status == 0 && buffer_size > 0)
1430 WARNING ("network plugin: parse_packet: Received truncated "
1431 "packet, try increasing `MaxPacketSize'");
1434 } /* }}} int parse_packet */
1436 static void free_sockent_client (struct sockent_client *sec) /* {{{ */
1445 sfree (sec->username);
1446 sfree (sec->password);
1447 if (sec->cypher != NULL)
1448 gcry_cipher_close (sec->cypher);
1450 } /* }}} void free_sockent_client */
1452 static void free_sockent_server (struct sockent_server *ses) /* {{{ */
1456 for (i = 0; i < ses->fd_num; i++)
1458 if (ses->fd[i] >= 0)
1467 sfree (ses->auth_file);
1468 fbh_destroy (ses->userdb);
1469 if (ses->cypher != NULL)
1470 gcry_cipher_close (ses->cypher);
1472 } /* }}} void free_sockent_server */
1474 static void sockent_destroy (sockent_t *se) /* {{{ */
1478 DEBUG ("network plugin: sockent_destroy (se = %p);", (void *) se);
1485 sfree (se->service);
1487 if (se->type == SOCKENT_TYPE_CLIENT)
1488 free_sockent_client (&se->data.client);
1490 free_sockent_server (&se->data.server);
1495 } /* }}} void sockent_destroy */
1498 * int network_set_ttl
1500 * Set the `IP_MULTICAST_TTL', `IP_TTL', `IPV6_MULTICAST_HOPS' or
1501 * `IPV6_UNICAST_HOPS', depending on which option is applicable.
1503 * The `struct addrinfo' is used to destinguish between unicast and multicast
1506 static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
1508 DEBUG ("network plugin: network_set_ttl: network_config_ttl = %i;",
1509 network_config_ttl);
1511 assert (se->type == SOCKENT_TYPE_CLIENT);
1513 if ((network_config_ttl < 1) || (network_config_ttl > 255))
1516 if (ai->ai_family == AF_INET)
1518 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1521 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1522 optname = IP_MULTICAST_TTL;
1526 if (setsockopt (se->data.client.fd, IPPROTO_IP, optname,
1527 &network_config_ttl,
1528 sizeof (network_config_ttl)) == -1)
1531 ERROR ("setsockopt: %s",
1532 sstrerror (errno, errbuf, sizeof (errbuf)));
1536 else if (ai->ai_family == AF_INET6)
1538 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1539 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1542 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1543 optname = IPV6_MULTICAST_HOPS;
1545 optname = IPV6_UNICAST_HOPS;
1547 if (setsockopt (se->data.client.fd, IPPROTO_IPV6, optname,
1548 &network_config_ttl,
1549 sizeof (network_config_ttl)) == -1)
1552 ERROR ("setsockopt: %s",
1553 sstrerror (errno, errbuf,
1560 } /* int network_set_ttl */
1562 static int network_bind_socket (int fd, const struct addrinfo *ai)
1567 /* allow multiple sockets to use the same PORT number */
1568 if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR,
1569 &yes, sizeof(yes)) == -1) {
1571 ERROR ("setsockopt: %s",
1572 sstrerror (errno, errbuf, sizeof (errbuf)));
1576 DEBUG ("fd = %i; calling `bind'", fd);
1578 if (bind (fd, ai->ai_addr, ai->ai_addrlen) == -1)
1582 sstrerror (errno, errbuf, sizeof (errbuf)));
1586 if (ai->ai_family == AF_INET)
1588 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1589 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1591 struct ip_mreq mreq;
1593 DEBUG ("fd = %i; IPv4 multicast address found", fd);
1595 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1596 mreq.imr_interface.s_addr = htonl (INADDR_ANY);
1598 if (setsockopt (fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1599 &loop, sizeof (loop)) == -1)
1602 ERROR ("setsockopt: %s",
1603 sstrerror (errno, errbuf,
1608 if (setsockopt (fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1609 &mreq, sizeof (mreq)) == -1)
1612 ERROR ("setsockopt: %s",
1613 sstrerror (errno, errbuf,
1619 else if (ai->ai_family == AF_INET6)
1621 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1622 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1623 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1625 struct ipv6_mreq mreq;
1627 DEBUG ("fd = %i; IPv6 multicast address found", fd);
1629 memcpy (&mreq.ipv6mr_multiaddr,
1631 sizeof (addr->sin6_addr));
1633 /* http://developer.apple.com/documentation/Darwin/Reference/ManPages/man4/ip6.4.html
1634 * ipv6mr_interface may be set to zeroes to
1635 * choose the default multicast interface or to
1636 * the index of a particular multicast-capable
1637 * interface if the host is multihomed.
1638 * Membership is associ-associated with a
1639 * single interface; programs running on
1640 * multihomed hosts may need to join the same
1641 * group on more than one interface.*/
1642 mreq.ipv6mr_interface = 0;
1644 if (setsockopt (fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
1645 &loop, sizeof (loop)) == -1)
1648 ERROR ("setsockopt: %s",
1649 sstrerror (errno, errbuf,
1654 if (setsockopt (fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
1655 &mreq, sizeof (mreq)) == -1)
1658 ERROR ("setsockopt: %s",
1659 sstrerror (errno, errbuf,
1667 } /* int network_bind_socket */
1669 /* Initialize a sockent structure. `type' must be either `SOCKENT_TYPE_CLIENT'
1670 * or `SOCKENT_TYPE_SERVER' */
1671 static int sockent_init (sockent_t *se, int type) /* {{{ */
1676 memset (se, 0, sizeof (*se));
1678 se->type = SOCKENT_TYPE_CLIENT;
1683 if (type == SOCKENT_TYPE_SERVER)
1685 se->type = SOCKENT_TYPE_SERVER;
1686 se->data.server.fd = NULL;
1688 se->data.server.security_level = SECURITY_LEVEL_NONE;
1689 se->data.server.auth_file = NULL;
1690 se->data.server.userdb = NULL;
1691 se->data.server.cypher = NULL;
1696 se->data.client.fd = -1;
1697 se->data.client.addr = NULL;
1699 se->data.client.security_level = SECURITY_LEVEL_NONE;
1700 se->data.client.username = NULL;
1701 se->data.client.password = NULL;
1702 se->data.client.cypher = NULL;
1707 } /* }}} int sockent_init */
1709 /* Open the file descriptors for a initialized sockent structure. */
1710 static int sockent_open (sockent_t *se) /* {{{ */
1712 struct addrinfo ai_hints;
1713 struct addrinfo *ai_list, *ai_ptr;
1717 const char *service;
1722 /* Set up the security structures. */
1723 #if HAVE_LIBGCRYPT /* {{{ */
1724 if (se->type == SOCKENT_TYPE_CLIENT)
1726 if (se->data.client.security_level > SECURITY_LEVEL_NONE)
1728 if ((se->data.client.username == NULL)
1729 || (se->data.client.password == NULL))
1731 ERROR ("network plugin: Client socket with "
1732 "security requested, but no "
1733 "credentials are configured.");
1736 gcry_md_hash_buffer (GCRY_MD_SHA256,
1737 se->data.client.password_hash,
1738 se->data.client.password,
1739 strlen (se->data.client.password));
1742 else /* (se->type == SOCKENT_TYPE_SERVER) */
1744 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
1746 if (se->data.server.auth_file == NULL)
1748 ERROR ("network plugin: Server socket with "
1749 "security requested, but no "
1750 "password file is configured.");
1754 if (se->data.server.auth_file != NULL)
1756 se->data.server.userdb = fbh_create (se->data.server.auth_file);
1757 if (se->data.server.userdb == NULL)
1759 ERROR ("network plugin: Reading password file "
1761 se->data.server.auth_file);
1762 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
1767 #endif /* }}} HAVE_LIBGCRYPT */
1770 service = se->service;
1772 if (service == NULL)
1773 service = NET_DEFAULT_PORT;
1775 DEBUG ("network plugin: sockent_open: node = %s; service = %s;",
1778 memset (&ai_hints, 0, sizeof (ai_hints));
1779 ai_hints.ai_flags = 0;
1781 ai_hints.ai_flags |= AI_PASSIVE;
1783 #ifdef AI_ADDRCONFIG
1784 ai_hints.ai_flags |= AI_ADDRCONFIG;
1786 ai_hints.ai_family = AF_UNSPEC;
1787 ai_hints.ai_socktype = SOCK_DGRAM;
1788 ai_hints.ai_protocol = IPPROTO_UDP;
1790 ai_return = getaddrinfo (node, service, &ai_hints, &ai_list);
1793 ERROR ("network plugin: getaddrinfo (%s, %s) failed: %s",
1794 (se->node == NULL) ? "(null)" : se->node,
1795 (se->service == NULL) ? "(null)" : se->service,
1796 gai_strerror (ai_return));
1800 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1804 if (se->type == SOCKENT_TYPE_SERVER) /* {{{ */
1808 tmp = realloc (se->data.server.fd,
1809 sizeof (*tmp) * (se->data.server.fd_num + 1));
1812 ERROR ("network plugin: realloc failed.");
1815 se->data.server.fd = tmp;
1816 tmp = se->data.server.fd + se->data.server.fd_num;
1818 *tmp = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
1819 ai_ptr->ai_protocol);
1823 ERROR ("network plugin: socket(2) failed: %s",
1824 sstrerror (errno, errbuf,
1829 status = network_bind_socket (*tmp, ai_ptr);
1837 se->data.server.fd_num++;
1839 } /* }}} if (se->type == SOCKENT_TYPE_SERVER) */
1840 else /* if (se->type == SOCKENT_TYPE_CLIENT) {{{ */
1842 se->data.client.fd = socket (ai_ptr->ai_family,
1843 ai_ptr->ai_socktype,
1844 ai_ptr->ai_protocol);
1845 if (se->data.client.fd < 0)
1848 ERROR ("network plugin: socket(2) failed: %s",
1849 sstrerror (errno, errbuf,
1854 se->data.client.addr = malloc (sizeof (*se->data.client.addr));
1855 if (se->data.client.addr == NULL)
1857 ERROR ("network plugin: malloc failed.");
1858 close (se->data.client.fd);
1859 se->data.client.fd = -1;
1863 memset (se->data.client.addr, 0, sizeof (*se->data.client.addr));
1864 assert (sizeof (*se->data.client.addr) >= ai_ptr->ai_addrlen);
1865 memcpy (se->data.client.addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1866 se->data.client.addrlen = ai_ptr->ai_addrlen;
1868 network_set_ttl (se, ai_ptr);
1870 /* We don't open more than one write-socket per
1871 * node/service pair.. */
1873 } /* }}} if (se->type == SOCKENT_TYPE_CLIENT) */
1874 } /* for (ai_list) */
1876 freeaddrinfo (ai_list);
1878 /* Check if all went well. */
1879 if (se->type == SOCKENT_TYPE_SERVER)
1881 if (se->data.server.fd_num <= 0)
1884 else /* if (se->type == SOCKENT_TYPE_CLIENT) */
1886 if (se->data.client.fd < 0)
1891 } /* }}} int sockent_open */
1893 /* Add a sockent to the global list of sockets */
1894 static int sockent_add (sockent_t *se) /* {{{ */
1896 sockent_t *last_ptr;
1901 if (se->type == SOCKENT_TYPE_SERVER)
1906 tmp = realloc (listen_sockets_pollfd,
1907 sizeof (*tmp) * (listen_sockets_num
1908 + se->data.server.fd_num));
1911 ERROR ("network plugin: realloc failed.");
1914 listen_sockets_pollfd = tmp;
1915 tmp = listen_sockets_pollfd + listen_sockets_num;
1917 for (i = 0; i < se->data.server.fd_num; i++)
1919 memset (tmp + i, 0, sizeof (*tmp));
1920 tmp[i].fd = se->data.server.fd[i];
1921 tmp[i].events = POLLIN | POLLPRI;
1925 listen_sockets_num += se->data.server.fd_num;
1927 if (listen_sockets == NULL)
1929 listen_sockets = se;
1932 last_ptr = listen_sockets;
1934 else /* if (se->type == SOCKENT_TYPE_CLIENT) */
1936 if (sending_sockets == NULL)
1938 sending_sockets = se;
1941 last_ptr = sending_sockets;
1944 while (last_ptr->next != NULL)
1945 last_ptr = last_ptr->next;
1946 last_ptr->next = se;
1949 } /* }}} int sockent_add */
1951 static void *dispatch_thread (void __attribute__((unused)) *arg) /* {{{ */
1955 receive_list_entry_t *ent;
1958 /* Lock and wait for more data to come in */
1959 pthread_mutex_lock (&receive_list_lock);
1960 while ((listen_loop == 0)
1961 && (receive_list_head == NULL))
1962 pthread_cond_wait (&receive_list_cond, &receive_list_lock);
1964 /* Remove the head entry and unlock */
1965 ent = receive_list_head;
1967 receive_list_head = ent->next;
1968 pthread_mutex_unlock (&receive_list_lock);
1970 /* Check whether we are supposed to exit. We do NOT check `listen_loop'
1971 * because we dispatch all missing packets before shutting down. */
1975 /* Look for the correct `sockent_t' */
1976 se = listen_sockets;
1981 for (i = 0; i < se->data.server.fd_num; i++)
1982 if (se->data.server.fd[i] == ent->fd)
1985 if (i < se->data.server.fd_num)
1993 ERROR ("network plugin: Got packet from FD %i, but can't "
1994 "find an appropriate socket entry.",
2001 parse_packet (se, ent->data, ent->data_len, /* flags = */ 0);
2007 } /* }}} void *dispatch_thread */
2009 static int network_receive (void) /* {{{ */
2011 char buffer[network_config_packet_size];
2017 receive_list_entry_t *private_list_head;
2018 receive_list_entry_t *private_list_tail;
2020 assert (listen_sockets_num > 0);
2022 private_list_head = NULL;
2023 private_list_tail = NULL;
2025 while (listen_loop == 0)
2027 status = poll (listen_sockets_pollfd, listen_sockets_num, -1);
2034 ERROR ("poll failed: %s",
2035 sstrerror (errno, errbuf, sizeof (errbuf)));
2039 for (i = 0; (i < listen_sockets_num) && (status > 0); i++)
2041 receive_list_entry_t *ent;
2043 if ((listen_sockets_pollfd[i].revents
2044 & (POLLIN | POLLPRI)) == 0)
2048 buffer_len = recv (listen_sockets_pollfd[i].fd,
2049 buffer, sizeof (buffer),
2054 ERROR ("recv failed: %s",
2055 sstrerror (errno, errbuf,
2060 /* TODO: Possible performance enhancement: Do not free
2061 * these entries in the dispatch thread but put them in
2062 * another list, so we don't have to allocate more and
2063 * more of these structures. */
2064 ent = malloc (sizeof (receive_list_entry_t));
2067 ERROR ("network plugin: malloc failed.");
2070 memset (ent, 0, sizeof (receive_list_entry_t));
2071 ent->data = malloc (network_config_packet_size);
2072 if (ent->data == NULL)
2074 ERROR ("network plugin: malloc failed.");
2077 ent->fd = listen_sockets_pollfd[i].fd;
2080 memcpy (ent->data, buffer, buffer_len);
2081 ent->data_len = buffer_len;
2083 if (private_list_head == NULL)
2084 private_list_head = ent;
2086 private_list_tail->next = ent;
2087 private_list_tail = ent;
2089 /* Do not block here. Blocking here has led to
2090 * insufficient performance in the past. */
2091 if (pthread_mutex_trylock (&receive_list_lock) == 0)
2093 if (receive_list_head == NULL)
2094 receive_list_head = private_list_head;
2096 receive_list_tail->next = private_list_head;
2097 receive_list_tail = private_list_tail;
2099 private_list_head = NULL;
2100 private_list_tail = NULL;
2102 pthread_cond_signal (&receive_list_cond);
2103 pthread_mutex_unlock (&receive_list_lock);
2105 } /* for (listen_sockets_pollfd) */
2106 } /* while (listen_loop == 0) */
2108 /* Make sure everything is dispatched before exiting. */
2109 if (private_list_head != NULL)
2111 pthread_mutex_lock (&receive_list_lock);
2113 if (receive_list_head == NULL)
2114 receive_list_head = private_list_head;
2116 receive_list_tail->next = private_list_head;
2117 receive_list_tail = private_list_tail;
2119 private_list_head = NULL;
2120 private_list_tail = NULL;
2122 pthread_cond_signal (&receive_list_cond);
2123 pthread_mutex_unlock (&receive_list_lock);
2127 } /* }}} int network_receive */
2129 static void *receive_thread (void __attribute__((unused)) *arg)
2131 return (network_receive () ? (void *) 1 : (void *) 0);
2132 } /* void *receive_thread */
2134 static void network_init_buffer (void)
2136 memset (send_buffer, 0, sizeof (send_buffer));
2137 send_buffer_ptr = send_buffer;
2138 send_buffer_fill = 0;
2140 memset (&send_buffer_vl, 0, sizeof (send_buffer_vl));
2141 } /* int network_init_buffer */
2143 static void networt_send_buffer_plain (const sockent_t *se, /* {{{ */
2144 const char *buffer, size_t buffer_size)
2150 status = sendto (se->data.client.fd, buffer, buffer_size,
2152 (struct sockaddr *) se->data.client.addr,
2153 se->data.client.addrlen);
2159 ERROR ("network plugin: sendto failed: %s",
2160 sstrerror (errno, errbuf,
2167 } /* }}} void networt_send_buffer_plain */
2170 #define BUFFER_ADD(p,s) do { \
2171 memcpy (buffer + buffer_offset, (p), (s)); \
2172 buffer_offset += (s); \
2175 static void networt_send_buffer_signed (const sockent_t *se, /* {{{ */
2176 const char *in_buffer, size_t in_buffer_size)
2178 part_signature_sha256_t ps;
2179 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2180 size_t buffer_offset;
2181 size_t username_len;
2185 unsigned char *hash;
2188 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
2191 ERROR ("network plugin: Creating HMAC object failed: %s",
2192 gcry_strerror (err));
2196 err = gcry_md_setkey (hd, se->data.client.password,
2197 strlen (se->data.client.password));
2200 ERROR ("network plugin: gcry_md_setkey failed: %s",
2201 gcry_strerror (err));
2206 username_len = strlen (se->data.client.username);
2207 if (username_len > (BUFF_SIG_SIZE - PART_SIGNATURE_SHA256_SIZE))
2209 ERROR ("network plugin: Username too long: %s",
2210 se->data.client.username);
2214 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE,
2215 se->data.client.username, username_len);
2216 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE + username_len,
2217 in_buffer, in_buffer_size);
2219 /* Initialize the `ps' structure. */
2220 memset (&ps, 0, sizeof (ps));
2221 ps.head.type = htons (TYPE_SIGN_SHA256);
2222 ps.head.length = htons (PART_SIGNATURE_SHA256_SIZE + username_len);
2224 /* Calculate the hash value. */
2225 gcry_md_write (hd, buffer + PART_SIGNATURE_SHA256_SIZE,
2226 username_len + in_buffer_size);
2227 hash = gcry_md_read (hd, GCRY_MD_SHA256);
2230 ERROR ("network plugin: gcry_md_read failed.");
2234 memcpy (ps.hash, hash, sizeof (ps.hash));
2236 /* Add the header */
2239 BUFFER_ADD (&ps.head.type, sizeof (ps.head.type));
2240 BUFFER_ADD (&ps.head.length, sizeof (ps.head.length));
2241 BUFFER_ADD (ps.hash, sizeof (ps.hash));
2243 assert (buffer_offset == PART_SIGNATURE_SHA256_SIZE);
2248 buffer_offset = PART_SIGNATURE_SHA256_SIZE + username_len + in_buffer_size;
2249 networt_send_buffer_plain (se, buffer, buffer_offset);
2250 } /* }}} void networt_send_buffer_signed */
2252 static void networt_send_buffer_encrypted (sockent_t *se, /* {{{ */
2253 const char *in_buffer, size_t in_buffer_size)
2255 part_encryption_aes256_t pea;
2256 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2258 size_t buffer_offset;
2260 size_t username_len;
2262 gcry_cipher_hd_t cypher;
2264 /* Initialize the header fields */
2265 memset (&pea, 0, sizeof (pea));
2266 pea.head.type = htons (TYPE_ENCR_AES256);
2268 pea.username = se->data.client.username;
2270 username_len = strlen (pea.username);
2271 if ((PART_ENCRYPTION_AES256_SIZE + username_len) > BUFF_SIG_SIZE)
2273 ERROR ("network plugin: Username too long: %s", pea.username);
2277 buffer_size = PART_ENCRYPTION_AES256_SIZE + username_len + in_buffer_size;
2278 header_size = PART_ENCRYPTION_AES256_SIZE + username_len
2279 - sizeof (pea.hash);
2281 assert (buffer_size <= sizeof (buffer));
2282 DEBUG ("network plugin: networt_send_buffer_encrypted: "
2283 "buffer_size = %zu;", buffer_size);
2285 pea.head.length = htons ((uint16_t) (PART_ENCRYPTION_AES256_SIZE
2286 + username_len + in_buffer_size));
2287 pea.username_length = htons ((uint16_t) username_len);
2289 /* Chose a random initialization vector. */
2290 gcry_randomize ((void *) &pea.iv, sizeof (pea.iv), GCRY_STRONG_RANDOM);
2292 /* Create hash of the payload */
2293 gcry_md_hash_buffer (GCRY_MD_SHA1, pea.hash, in_buffer, in_buffer_size);
2295 /* Initialize the buffer */
2297 memset (buffer, 0, sizeof (buffer));
2300 BUFFER_ADD (&pea.head.type, sizeof (pea.head.type));
2301 BUFFER_ADD (&pea.head.length, sizeof (pea.head.length));
2302 BUFFER_ADD (&pea.username_length, sizeof (pea.username_length));
2303 BUFFER_ADD (pea.username, username_len);
2304 BUFFER_ADD (pea.iv, sizeof (pea.iv));
2305 assert (buffer_offset == header_size);
2306 BUFFER_ADD (pea.hash, sizeof (pea.hash));
2307 BUFFER_ADD (in_buffer, in_buffer_size);
2309 assert (buffer_offset == buffer_size);
2311 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
2312 se->data.client.password);
2316 /* Encrypt the buffer in-place */
2317 err = gcry_cipher_encrypt (cypher,
2318 buffer + header_size,
2319 buffer_size - header_size,
2320 /* in = */ NULL, /* in len = */ 0);
2323 ERROR ("network plugin: gcry_cipher_encrypt returned: %s",
2324 gcry_strerror (err));
2328 /* Send it out without further modifications */
2329 networt_send_buffer_plain (se, buffer, buffer_size);
2330 } /* }}} void networt_send_buffer_encrypted */
2332 #endif /* HAVE_LIBGCRYPT */
2334 static void network_send_buffer (char *buffer, size_t buffer_len) /* {{{ */
2338 DEBUG ("network plugin: network_send_buffer: buffer_len = %zu", buffer_len);
2340 for (se = sending_sockets; se != NULL; se = se->next)
2343 if (se->data.client.security_level == SECURITY_LEVEL_ENCRYPT)
2344 networt_send_buffer_encrypted (se, buffer, buffer_len);
2345 else if (se->data.client.security_level == SECURITY_LEVEL_SIGN)
2346 networt_send_buffer_signed (se, buffer, buffer_len);
2347 else /* if (se->data.client.security_level == SECURITY_LEVEL_NONE) */
2348 #endif /* HAVE_LIBGCRYPT */
2349 networt_send_buffer_plain (se, buffer, buffer_len);
2350 } /* for (sending_sockets) */
2351 } /* }}} void network_send_buffer */
2353 static int add_to_buffer (char *buffer, int buffer_size, /* {{{ */
2354 value_list_t *vl_def,
2355 const data_set_t *ds, const value_list_t *vl)
2357 char *buffer_orig = buffer;
2359 if (strcmp (vl_def->host, vl->host) != 0)
2361 if (write_part_string (&buffer, &buffer_size, TYPE_HOST,
2362 vl->host, strlen (vl->host)) != 0)
2364 sstrncpy (vl_def->host, vl->host, sizeof (vl_def->host));
2367 if (vl_def->time != vl->time)
2369 if (write_part_number (&buffer, &buffer_size, TYPE_TIME,
2370 (uint64_t) vl->time))
2372 vl_def->time = vl->time;
2375 if (vl_def->interval != vl->interval)
2377 if (write_part_number (&buffer, &buffer_size, TYPE_INTERVAL,
2378 (uint64_t) vl->interval))
2380 vl_def->interval = vl->interval;
2383 if (strcmp (vl_def->plugin, vl->plugin) != 0)
2385 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN,
2386 vl->plugin, strlen (vl->plugin)) != 0)
2388 sstrncpy (vl_def->plugin, vl->plugin, sizeof (vl_def->plugin));
2391 if (strcmp (vl_def->plugin_instance, vl->plugin_instance) != 0)
2393 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
2394 vl->plugin_instance,
2395 strlen (vl->plugin_instance)) != 0)
2397 sstrncpy (vl_def->plugin_instance, vl->plugin_instance, sizeof (vl_def->plugin_instance));
2400 if (strcmp (vl_def->type, vl->type) != 0)
2402 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
2403 vl->type, strlen (vl->type)) != 0)
2405 sstrncpy (vl_def->type, ds->type, sizeof (vl_def->type));
2408 if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
2410 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE_INSTANCE,
2412 strlen (vl->type_instance)) != 0)
2414 sstrncpy (vl_def->type_instance, vl->type_instance, sizeof (vl_def->type_instance));
2417 if (write_part_values (&buffer, &buffer_size, ds, vl) != 0)
2420 return (buffer - buffer_orig);
2421 } /* }}} int add_to_buffer */
2423 static void flush_buffer (void)
2425 DEBUG ("network plugin: flush_buffer: send_buffer_fill = %i",
2428 network_send_buffer (send_buffer, (size_t) send_buffer_fill);
2429 network_init_buffer ();
2432 static int network_write (const data_set_t *ds, const value_list_t *vl,
2433 user_data_t __attribute__((unused)) *user_data)
2437 if (!check_send_okay (vl))
2440 char name[6*DATA_MAX_NAME_LEN];
2441 FORMAT_VL (name, sizeof (name), vl);
2442 name[sizeof (name) - 1] = 0;
2443 DEBUG ("network plugin: network_write: "
2444 "NOT sending %s.", name);
2449 uc_meta_data_add_unsigned_int (vl,
2450 "network:time_sent", (uint64_t) vl->time);
2452 pthread_mutex_lock (&send_buffer_lock);
2454 status = add_to_buffer (send_buffer_ptr,
2455 sizeof (send_buffer) - (send_buffer_fill + BUFF_SIG_SIZE),
2460 /* status == bytes added to the buffer */
2461 send_buffer_fill += status;
2462 send_buffer_ptr += status;
2468 status = add_to_buffer (send_buffer_ptr,
2469 sizeof (send_buffer) - (send_buffer_fill + BUFF_SIG_SIZE),
2475 send_buffer_fill += status;
2476 send_buffer_ptr += status;
2482 ERROR ("network plugin: Unable to append to the "
2483 "buffer for some weird reason");
2485 else if ((sizeof (send_buffer) - send_buffer_fill) < 15)
2490 pthread_mutex_unlock (&send_buffer_lock);
2492 return ((status < 0) ? -1 : 0);
2493 } /* int network_write */
2495 static int network_config_set_boolean (const oconfig_item_t *ci, /* {{{ */
2498 if ((ci->values_num != 1)
2499 || ((ci->values[0].type != OCONFIG_TYPE_BOOLEAN)
2500 && (ci->values[0].type != OCONFIG_TYPE_STRING)))
2502 ERROR ("network plugin: The `%s' config option needs "
2503 "exactly one boolean argument.", ci->key);
2507 if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
2509 if (ci->values[0].value.boolean)
2516 char *str = ci->values[0].value.string;
2518 if ((strcasecmp ("true", str) == 0)
2519 || (strcasecmp ("yes", str) == 0)
2520 || (strcasecmp ("on", str) == 0))
2522 else if ((strcasecmp ("false", str) == 0)
2523 || (strcasecmp ("no", str) == 0)
2524 || (strcasecmp ("off", str) == 0))
2528 ERROR ("network plugin: Cannot parse string value `%s' of the `%s' "
2529 "option as boolean value.",
2536 } /* }}} int network_config_set_boolean */
2538 static int network_config_set_ttl (const oconfig_item_t *ci) /* {{{ */
2541 if ((ci->values_num != 1)
2542 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
2544 WARNING ("network plugin: The `TimeToLive' config option needs exactly "
2545 "one numeric argument.");
2549 tmp = (int) ci->values[0].value.number;
2550 if ((tmp > 0) && (tmp <= 255))
2551 network_config_ttl = tmp;
2554 } /* }}} int network_config_set_ttl */
2556 static int network_config_set_buffer_size (const oconfig_item_t *ci) /* {{{ */
2559 if ((ci->values_num != 1)
2560 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
2562 WARNING ("network plugin: The `MaxPacketSize' config option needs exactly "
2563 "one numeric argument.");
2567 tmp = (int) ci->values[0].value.number;
2568 if ((tmp >= 1024) && (tmp <= 65535))
2569 network_config_packet_size = tmp;
2572 } /* }}} int network_config_set_buffer_size */
2575 static int network_config_set_string (const oconfig_item_t *ci, /* {{{ */
2579 if ((ci->values_num != 1)
2580 || (ci->values[0].type != OCONFIG_TYPE_STRING))
2582 WARNING ("network plugin: The `%s' config option needs exactly "
2583 "one string argument.", ci->key);
2587 tmp = strdup (ci->values[0].value.string);
2591 sfree (*ret_string);
2595 } /* }}} int network_config_set_string */
2596 #endif /* HAVE_LIBGCRYPT */
2599 static int network_config_set_security_level (oconfig_item_t *ci, /* {{{ */
2603 if ((ci->values_num != 1)
2604 || (ci->values[0].type != OCONFIG_TYPE_STRING))
2606 WARNING ("network plugin: The `SecurityLevel' config option needs exactly "
2607 "one string argument.");
2611 str = ci->values[0].value.string;
2612 if (strcasecmp ("Encrypt", str) == 0)
2613 *retval = SECURITY_LEVEL_ENCRYPT;
2614 else if (strcasecmp ("Sign", str) == 0)
2615 *retval = SECURITY_LEVEL_SIGN;
2616 else if (strcasecmp ("None", str) == 0)
2617 *retval = SECURITY_LEVEL_NONE;
2620 WARNING ("network plugin: Unknown security level: %s.", str);
2625 } /* }}} int network_config_set_security_level */
2626 #endif /* HAVE_LIBGCRYPT */
2628 static int network_config_add_listen (const oconfig_item_t *ci) /* {{{ */
2634 if ((ci->values_num < 1) || (ci->values_num > 2)
2635 || (ci->values[0].type != OCONFIG_TYPE_STRING)
2636 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
2638 ERROR ("network plugin: The `%s' config option needs "
2639 "one or two string arguments.", ci->key);
2643 se = malloc (sizeof (*se));
2646 ERROR ("network plugin: malloc failed.");
2649 sockent_init (se, SOCKENT_TYPE_SERVER);
2651 se->node = strdup (ci->values[0].value.string);
2652 if (ci->values_num >= 2)
2653 se->service = strdup (ci->values[1].value.string);
2655 for (i = 0; i < ci->children_num; i++)
2657 oconfig_item_t *child = ci->children + i;
2660 if (strcasecmp ("AuthFile", child->key) == 0)
2661 network_config_set_string (child, &se->data.server.auth_file);
2662 else if (strcasecmp ("SecurityLevel", child->key) == 0)
2663 network_config_set_security_level (child,
2664 &se->data.server.security_level);
2666 #endif /* HAVE_LIBGCRYPT */
2668 WARNING ("network plugin: Option `%s' is not allowed here.",
2674 if ((se->data.server.security_level > SECURITY_LEVEL_NONE)
2675 && (se->data.server.auth_file == NULL))
2677 ERROR ("network plugin: A security level higher than `none' was "
2678 "requested, but no AuthFile option was given. Cowardly refusing to "
2679 "open this socket!");
2680 sockent_destroy (se);
2683 #endif /* HAVE_LIBGCRYPT */
2685 status = sockent_open (se);
2688 ERROR ("network plugin: network_config_add_listen: sockent_open failed.");
2689 sockent_destroy (se);
2693 status = sockent_add (se);
2696 ERROR ("network plugin: network_config_add_listen: sockent_add failed.");
2697 sockent_destroy (se);
2702 } /* }}} int network_config_add_listen */
2704 static int network_config_add_server (const oconfig_item_t *ci) /* {{{ */
2710 if ((ci->values_num < 1) || (ci->values_num > 2)
2711 || (ci->values[0].type != OCONFIG_TYPE_STRING)
2712 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
2714 ERROR ("network plugin: The `%s' config option needs "
2715 "one or two string arguments.", ci->key);
2719 se = malloc (sizeof (*se));
2722 ERROR ("network plugin: malloc failed.");
2725 sockent_init (se, SOCKENT_TYPE_CLIENT);
2727 se->node = strdup (ci->values[0].value.string);
2728 if (ci->values_num >= 2)
2729 se->service = strdup (ci->values[1].value.string);
2731 for (i = 0; i < ci->children_num; i++)
2733 oconfig_item_t *child = ci->children + i;
2736 if (strcasecmp ("Username", child->key) == 0)
2737 network_config_set_string (child, &se->data.client.username);
2738 else if (strcasecmp ("Password", child->key) == 0)
2739 network_config_set_string (child, &se->data.client.password);
2740 else if (strcasecmp ("SecurityLevel", child->key) == 0)
2741 network_config_set_security_level (child,
2742 &se->data.client.security_level);
2744 #endif /* HAVE_LIBGCRYPT */
2746 WARNING ("network plugin: Option `%s' is not allowed here.",
2752 if ((se->data.client.security_level > SECURITY_LEVEL_NONE)
2753 && ((se->data.client.username == NULL)
2754 || (se->data.client.password == NULL)))
2756 ERROR ("network plugin: A security level higher than `none' was "
2757 "requested, but no Username or Password option was given. "
2758 "Cowardly refusing to open this socket!");
2759 sockent_destroy (se);
2762 #endif /* HAVE_LIBGCRYPT */
2764 status = sockent_open (se);
2767 ERROR ("network plugin: network_config_add_server: sockent_open failed.");
2768 sockent_destroy (se);
2772 status = sockent_add (se);
2775 ERROR ("network plugin: network_config_add_server: sockent_add failed.");
2776 sockent_destroy (se);
2781 } /* }}} int network_config_add_server */
2783 static int network_config (oconfig_item_t *ci) /* {{{ */
2787 for (i = 0; i < ci->children_num; i++)
2789 oconfig_item_t *child = ci->children + i;
2791 if (strcasecmp ("Listen", child->key) == 0)
2792 network_config_add_listen (child);
2793 else if (strcasecmp ("Server", child->key) == 0)
2794 network_config_add_server (child);
2795 else if (strcasecmp ("TimeToLive", child->key) == 0)
2796 network_config_set_ttl (child);
2797 else if (strcasecmp ("MaxPacketSize", child->key) == 0)
2798 network_config_set_buffer_size (child);
2799 else if (strcasecmp ("Forward", child->key) == 0)
2800 network_config_set_boolean (child, &network_config_forward);
2801 else if (strcasecmp ("CacheFlush", child->key) == 0)
2802 /* no op for backwards compatibility only */;
2805 WARNING ("network plugin: Option `%s' is not allowed here.",
2811 } /* }}} int network_config */
2813 static int network_notification (const notification_t *n,
2814 user_data_t __attribute__((unused)) *user_data)
2816 char buffer[network_config_packet_size];
2817 char *buffer_ptr = buffer;
2818 int buffer_free = sizeof (buffer);
2821 memset (buffer, '\0', sizeof (buffer));
2824 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_TIME,
2825 (uint64_t) n->time);
2829 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_SEVERITY,
2830 (uint64_t) n->severity);
2834 if (strlen (n->host) > 0)
2836 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_HOST,
2837 n->host, strlen (n->host));
2842 if (strlen (n->plugin) > 0)
2844 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_PLUGIN,
2845 n->plugin, strlen (n->plugin));
2850 if (strlen (n->plugin_instance) > 0)
2852 status = write_part_string (&buffer_ptr, &buffer_free,
2853 TYPE_PLUGIN_INSTANCE,
2854 n->plugin_instance, strlen (n->plugin_instance));
2859 if (strlen (n->type) > 0)
2861 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE,
2862 n->type, strlen (n->type));
2867 if (strlen (n->type_instance) > 0)
2869 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE_INSTANCE,
2870 n->type_instance, strlen (n->type_instance));
2875 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_MESSAGE,
2876 n->message, strlen (n->message));
2880 network_send_buffer (buffer, sizeof (buffer) - buffer_free);
2883 } /* int network_notification */
2885 static int network_shutdown (void)
2889 /* Kill the listening thread */
2890 if (receive_thread_running != 0)
2892 INFO ("network plugin: Stopping receive thread.");
2893 pthread_kill (receive_thread_id, SIGTERM);
2894 pthread_join (receive_thread_id, NULL /* no return value */);
2895 memset (&receive_thread_id, 0, sizeof (receive_thread_id));
2896 receive_thread_running = 0;
2899 /* Shutdown the dispatching thread */
2900 if (dispatch_thread_running != 0)
2902 INFO ("network plugin: Stopping dispatch thread.");
2903 pthread_mutex_lock (&receive_list_lock);
2904 pthread_cond_broadcast (&receive_list_cond);
2905 pthread_mutex_unlock (&receive_list_lock);
2906 pthread_join (dispatch_thread_id, /* ret = */ NULL);
2907 dispatch_thread_running = 0;
2910 sockent_destroy (listen_sockets);
2912 if (send_buffer_fill > 0)
2915 sfree (send_buffer);
2917 /* TODO: Close `sending_sockets' */
2919 plugin_unregister_config ("network");
2920 plugin_unregister_init ("network");
2921 plugin_unregister_write ("network");
2922 plugin_unregister_shutdown ("network");
2925 } /* int network_shutdown */
2927 static int network_init (void)
2929 static _Bool have_init = false;
2931 /* Check if we were already initialized. If so, just return - there's
2932 * nothing more to do (for now, that is). */
2937 plugin_register_shutdown ("network", network_shutdown);
2939 send_buffer = malloc (network_config_packet_size);
2940 if (send_buffer == NULL)
2942 ERROR ("network plugin: malloc failed.");
2945 network_init_buffer ();
2947 /* setup socket(s) and so on */
2948 if (sending_sockets != NULL)
2950 plugin_register_write ("network", network_write,
2951 /* user_data = */ NULL);
2952 plugin_register_notification ("network", network_notification,
2953 /* user_data = */ NULL);
2956 /* If no threads need to be started, return here. */
2957 if ((listen_sockets_num == 0)
2958 || ((dispatch_thread_running != 0)
2959 && (receive_thread_running != 0)))
2962 if (dispatch_thread_running == 0)
2965 status = pthread_create (&dispatch_thread_id,
2966 NULL /* no attributes */,
2968 NULL /* no argument */);
2972 ERROR ("network: pthread_create failed: %s",
2973 sstrerror (errno, errbuf,
2978 dispatch_thread_running = 1;
2982 if (receive_thread_running == 0)
2985 status = pthread_create (&receive_thread_id,
2986 NULL /* no attributes */,
2988 NULL /* no argument */);
2992 ERROR ("network: pthread_create failed: %s",
2993 sstrerror (errno, errbuf,
2998 receive_thread_running = 1;
3003 } /* int network_init */
3006 * The flush option of the network plugin cannot flush individual identifiers.
3007 * All the values are added to a buffer and sent when the buffer is full, the
3008 * requested value may or may not be in there, it's not worth finding out. We
3009 * just send the buffer if `flush' is called - if the requested value was in
3010 * there, good. If not, well, then there is nothing to flush.. -octo
3012 static int network_flush (int timeout,
3013 const char __attribute__((unused)) *identifier,
3014 user_data_t __attribute__((unused)) *user_data)
3016 pthread_mutex_lock (&send_buffer_lock);
3018 if (send_buffer_fill > 0)
3021 pthread_mutex_unlock (&send_buffer_lock);
3024 } /* int network_flush */
3026 void module_register (void)
3028 plugin_register_complex_config ("network", network_config);
3029 plugin_register_init ("network", network_init);
3030 plugin_register_flush ("network", network_flush,
3031 /* user_data = */ NULL);
3032 } /* void module_register */
3034 /* vim: set fdm=marker : */