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>
55 GCRY_THREAD_OPTION_PTHREAD_IMPL;
58 #ifndef IPV6_ADD_MEMBERSHIP
59 # ifdef IPV6_JOIN_GROUP
60 # define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
62 # error "Neither IP_ADD_MEMBERSHIP nor IPV6_JOIN_GROUP is defined"
64 #endif /* !IP_ADD_MEMBERSHIP */
67 * Maximum size required for encryption / signing:
69 * 42 bytes for the encryption header
70 * + 64 bytes for the username
74 #define BUFF_SIG_SIZE 106
79 #define SECURITY_LEVEL_NONE 0
81 # define SECURITY_LEVEL_SIGN 1
82 # define SECURITY_LEVEL_ENCRYPT 2
87 struct sockaddr_storage *addr;
93 gcry_cipher_hd_t cypher;
94 unsigned char password_hash[32];
106 gcry_cipher_hd_t cypher;
110 typedef struct sockent
112 #define SOCKENT_TYPE_CLIENT 1
113 #define SOCKENT_TYPE_SERVER 2
121 struct sockent_client client;
122 struct sockent_server server;
125 struct sockent *next;
128 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
129 * 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
130 * +-------+-----------------------+-------------------------------+
131 * ! Ver. ! ! Length !
132 * +-------+-----------------------+-------------------------------+
139 typedef struct part_header_s part_header_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 * : (Length - 4) Bytes :
147 * +---------------------------------------------------------------+
154 typedef struct part_string_s part_string_t;
156 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
157 * 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
158 * +-------------------------------+-------------------------------+
160 * +-------------------------------+-------------------------------+
161 * : (Length - 4 == 2 || 4 || 8) Bytes :
162 * +---------------------------------------------------------------+
169 typedef struct part_number_s part_number_t;
171 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
172 * 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
173 * +-------------------------------+-------------------------------+
175 * +-------------------------------+---------------+---------------+
176 * ! Num of values ! Type0 ! Type1 !
177 * +-------------------------------+---------------+---------------+
180 * +---------------------------------------------------------------+
183 * +---------------------------------------------------------------+
188 uint16_t *num_values;
189 uint8_t *values_types;
192 typedef struct part_values_s part_values_t;
194 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
195 * 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
196 * +-------------------------------+-------------------------------+
198 * +-------------------------------+-------------------------------+
199 * ! Hash (Bits 0 - 31) !
201 * ! Hash (Bits 224 - 255) !
202 * +---------------------------------------------------------------+
205 #define PART_SIGNATURE_SHA256_SIZE 36
206 struct part_signature_sha256_s
209 unsigned char hash[32];
212 typedef struct part_signature_sha256_s part_signature_sha256_t;
214 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
215 * 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
216 * +-------------------------------+-------------------------------+
218 * +-------------------------------+-------------------------------+
219 * ! Original length ! Padding (0 - 15 bytes) !
220 * +-------------------------------+-------------------------------+
221 * ! Hash (Bits 0 - 31) !
223 * ! Hash (Bits 128 - 159) !
224 * +---------------------------------------------------------------+
227 #define PART_ENCRYPTION_AES256_SIZE 42
228 struct part_encryption_aes256_s
231 uint16_t username_length;
233 unsigned char iv[16];
235 unsigned char hash[20];
239 typedef struct part_encryption_aes256_s part_encryption_aes256_t;
241 struct receive_list_entry_s
246 struct receive_list_entry_s *next;
248 typedef struct receive_list_entry_s receive_list_entry_t;
253 static int network_config_ttl = 0;
254 static size_t network_config_packet_size = 1024;
255 static int network_config_forward = 0;
257 static sockent_t *sending_sockets = NULL;
259 static receive_list_entry_t *receive_list_head = NULL;
260 static receive_list_entry_t *receive_list_tail = NULL;
261 static pthread_mutex_t receive_list_lock = PTHREAD_MUTEX_INITIALIZER;
262 static pthread_cond_t receive_list_cond = PTHREAD_COND_INITIALIZER;
264 static sockent_t *listen_sockets = NULL;
265 static struct pollfd *listen_sockets_pollfd = NULL;
266 static size_t listen_sockets_num = 0;
268 /* The receive and dispatch threads will run as long as `listen_loop' is set to
270 static int listen_loop = 0;
271 static int receive_thread_running = 0;
272 static pthread_t receive_thread_id;
273 static int dispatch_thread_running = 0;
274 static pthread_t dispatch_thread_id;
276 /* Buffer in which to-be-sent network packets are constructed. */
277 static char *send_buffer;
278 static char *send_buffer_ptr;
279 static int send_buffer_fill;
280 static value_list_t send_buffer_vl = VALUE_LIST_STATIC;
281 static pthread_mutex_t send_buffer_lock = PTHREAD_MUTEX_INITIALIZER;
286 static _Bool check_receive_okay (const value_list_t *vl) /* {{{ */
288 uint64_t time_sent = 0;
291 status = uc_meta_data_get_unsigned_int (vl,
292 "network:time_sent", &time_sent);
294 /* This is a value we already sent. Don't allow it to be received again in
295 * order to avoid looping. */
296 if ((status == 0) && (time_sent >= ((uint64_t) vl->time)))
300 } /* }}} _Bool check_receive_okay */
302 static _Bool check_send_okay (const value_list_t *vl) /* {{{ */
304 _Bool received = false;
307 if (network_config_forward != 0)
310 if (vl->meta == NULL)
313 status = meta_data_get_boolean (vl->meta, "network:received", &received);
314 if (status == -ENOENT)
316 else if (status != 0)
318 ERROR ("network plugin: check_send_okay: meta_data_get_boolean failed "
319 "with status %i.", status);
323 /* By default, only *send* value lists that were not *received* by the
326 } /* }}} _Bool check_send_okay */
328 static int network_dispatch_values (value_list_t *vl) /* {{{ */
333 || (strlen (vl->host) <= 0)
334 || (strlen (vl->plugin) <= 0)
335 || (strlen (vl->type) <= 0))
338 if (!check_receive_okay (vl))
341 char name[6*DATA_MAX_NAME_LEN];
342 FORMAT_VL (name, sizeof (name), vl);
343 name[sizeof (name) - 1] = 0;
344 DEBUG ("network plugin: network_dispatch_values: "
345 "NOT dispatching %s.", name);
350 assert (vl->meta == NULL);
352 vl->meta = meta_data_create ();
353 if (vl->meta == NULL)
355 ERROR ("network plugin: meta_data_create failed.");
359 status = meta_data_add_boolean (vl->meta, "network:received", true);
362 ERROR ("network plugin: meta_data_add_boolean failed.");
363 meta_data_destroy (vl->meta);
368 plugin_dispatch_values (vl);
370 meta_data_destroy (vl->meta);
374 } /* }}} int network_dispatch_values */
377 static gcry_cipher_hd_t network_get_aes256_cypher (sockent_t *se, /* {{{ */
378 const void *iv, size_t iv_size, const char *username)
381 gcry_cipher_hd_t *cyper_ptr;
382 unsigned char password_hash[32];
384 if (se->type == SOCKENT_TYPE_CLIENT)
386 cyper_ptr = &se->data.client.cypher;
387 memcpy (password_hash, se->data.client.password_hash,
388 sizeof (password_hash));
394 cyper_ptr = &se->data.server.cypher;
396 if (username == NULL)
399 secret = fbh_get (se->data.server.userdb, username);
403 gcry_md_hash_buffer (GCRY_MD_SHA256,
405 secret, strlen (secret));
410 if (*cyper_ptr == NULL)
412 err = gcry_cipher_open (cyper_ptr,
413 GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_OFB, /* flags = */ 0);
416 ERROR ("network plugin: gcry_cipher_open returned: %s",
417 gcry_strerror (err));
424 gcry_cipher_reset (*cyper_ptr);
426 assert (*cyper_ptr != NULL);
428 err = gcry_cipher_setkey (*cyper_ptr,
429 password_hash, sizeof (password_hash));
432 ERROR ("network plugin: gcry_cipher_setkey returned: %s",
433 gcry_strerror (err));
434 gcry_cipher_close (*cyper_ptr);
439 err = gcry_cipher_setiv (*cyper_ptr, iv, iv_size);
442 ERROR ("network plugin: gcry_cipher_setkey returned: %s",
443 gcry_strerror (err));
444 gcry_cipher_close (*cyper_ptr);
450 } /* }}} int network_get_aes256_cypher */
451 #endif /* HAVE_LIBGCRYPT */
453 static int write_part_values (char **ret_buffer, int *ret_buffer_len,
454 const data_set_t *ds, const value_list_t *vl)
460 part_header_t pkg_ph;
461 uint16_t pkg_num_values;
462 uint8_t *pkg_values_types;
468 num_values = vl->values_len;
469 packet_len = sizeof (part_header_t) + sizeof (uint16_t)
470 + (num_values * sizeof (uint8_t))
471 + (num_values * sizeof (value_t));
473 if (*ret_buffer_len < packet_len)
476 pkg_values_types = (uint8_t *) malloc (num_values * sizeof (uint8_t));
477 if (pkg_values_types == NULL)
479 ERROR ("network plugin: write_part_values: malloc failed.");
483 pkg_values = (value_t *) malloc (num_values * sizeof (value_t));
484 if (pkg_values == NULL)
486 free (pkg_values_types);
487 ERROR ("network plugin: write_part_values: malloc failed.");
491 pkg_ph.type = htons (TYPE_VALUES);
492 pkg_ph.length = htons (packet_len);
494 pkg_num_values = htons ((uint16_t) vl->values_len);
496 for (i = 0; i < num_values; i++)
498 pkg_values_types[i] = (uint8_t) ds->ds[i].type;
499 switch (ds->ds[i].type)
501 case DS_TYPE_COUNTER:
502 pkg_values[i].counter = htonll (vl->values[i].counter);
506 pkg_values[i].gauge = htond (vl->values[i].gauge);
510 pkg_values[i].derive = htonll (vl->values[i].derive);
513 case DS_TYPE_ABSOLUTE:
514 pkg_values[i].absolute = htonll (vl->values[i].absolute);
518 free (pkg_values_types);
520 ERROR ("network plugin: write_part_values: "
521 "Unknown data source type: %i",
524 } /* switch (ds->ds[i].type) */
525 } /* for (num_values) */
528 * Use `memcpy' to write everything to the buffer, because the pointer
529 * may be unaligned and some architectures, such as SPARC, can't handle
532 packet_ptr = *ret_buffer;
534 memcpy (packet_ptr + offset, &pkg_ph, sizeof (pkg_ph));
535 offset += sizeof (pkg_ph);
536 memcpy (packet_ptr + offset, &pkg_num_values, sizeof (pkg_num_values));
537 offset += sizeof (pkg_num_values);
538 memcpy (packet_ptr + offset, pkg_values_types, num_values * sizeof (uint8_t));
539 offset += num_values * sizeof (uint8_t);
540 memcpy (packet_ptr + offset, pkg_values, num_values * sizeof (value_t));
541 offset += num_values * sizeof (value_t);
543 assert (offset == packet_len);
545 *ret_buffer = packet_ptr + packet_len;
546 *ret_buffer_len -= packet_len;
548 free (pkg_values_types);
552 } /* int write_part_values */
554 static int write_part_number (char **ret_buffer, int *ret_buffer_len,
555 int type, uint64_t value)
560 part_header_t pkg_head;
565 packet_len = sizeof (pkg_head) + sizeof (pkg_value);
567 if (*ret_buffer_len < packet_len)
570 pkg_head.type = htons (type);
571 pkg_head.length = htons (packet_len);
572 pkg_value = htonll (value);
574 packet_ptr = *ret_buffer;
576 memcpy (packet_ptr + offset, &pkg_head, sizeof (pkg_head));
577 offset += sizeof (pkg_head);
578 memcpy (packet_ptr + offset, &pkg_value, sizeof (pkg_value));
579 offset += sizeof (pkg_value);
581 assert (offset == packet_len);
583 *ret_buffer = packet_ptr + packet_len;
584 *ret_buffer_len -= packet_len;
587 } /* int write_part_number */
589 static int write_part_string (char **ret_buffer, int *ret_buffer_len,
590 int type, const char *str, int str_len)
600 buffer_len = 2 * sizeof (uint16_t) + str_len + 1;
601 if (*ret_buffer_len < buffer_len)
604 pkg_type = htons (type);
605 pkg_length = htons (buffer_len);
607 buffer = *ret_buffer;
609 memcpy (buffer + offset, (void *) &pkg_type, sizeof (pkg_type));
610 offset += sizeof (pkg_type);
611 memcpy (buffer + offset, (void *) &pkg_length, sizeof (pkg_length));
612 offset += sizeof (pkg_length);
613 memcpy (buffer + offset, str, str_len);
615 memset (buffer + offset, '\0', 1);
618 assert (offset == buffer_len);
620 *ret_buffer = buffer + buffer_len;
621 *ret_buffer_len -= buffer_len;
624 } /* int write_part_string */
626 static int parse_part_values (void **ret_buffer, size_t *ret_buffer_len,
627 value_t **ret_values, int *ret_num_values)
629 char *buffer = *ret_buffer;
630 size_t buffer_len = *ret_buffer_len;
645 NOTICE ("network plugin: packet is too short: "
646 "buffer_len = %zu", buffer_len);
650 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
651 buffer += sizeof (tmp16);
652 pkg_type = ntohs (tmp16);
654 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
655 buffer += sizeof (tmp16);
656 pkg_length = ntohs (tmp16);
658 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
659 buffer += sizeof (tmp16);
660 pkg_numval = ntohs (tmp16);
662 assert (pkg_type == TYPE_VALUES);
664 exp_size = 3 * sizeof (uint16_t)
665 + pkg_numval * (sizeof (uint8_t) + sizeof (value_t));
666 if ((buffer_len < 0) || (buffer_len < exp_size))
668 WARNING ("network plugin: parse_part_values: "
670 "Chunk of size %zu expected, "
671 "but buffer has only %zu bytes left.",
672 exp_size, buffer_len);
676 if (pkg_length != exp_size)
678 WARNING ("network plugin: parse_part_values: "
679 "Length and number of values "
680 "in the packet don't match.");
684 pkg_types = (uint8_t *) malloc (pkg_numval * sizeof (uint8_t));
685 pkg_values = (value_t *) malloc (pkg_numval * sizeof (value_t));
686 if ((pkg_types == NULL) || (pkg_values == NULL))
690 ERROR ("network plugin: parse_part_values: malloc failed.");
694 memcpy ((void *) pkg_types, (void *) buffer, pkg_numval * sizeof (uint8_t));
695 buffer += pkg_numval * sizeof (uint8_t);
696 memcpy ((void *) pkg_values, (void *) buffer, pkg_numval * sizeof (value_t));
697 buffer += pkg_numval * sizeof (value_t);
699 for (i = 0; i < pkg_numval; i++)
701 switch (pkg_types[i])
703 case DS_TYPE_COUNTER:
704 pkg_values[i].counter = (counter_t) ntohll (pkg_values[i].counter);
708 pkg_values[i].gauge = (gauge_t) ntohd (pkg_values[i].gauge);
712 pkg_values[i].derive = (derive_t) ntohll (pkg_values[i].derive);
715 case DS_TYPE_ABSOLUTE:
716 pkg_values[i].absolute = (absolute_t) ntohll (pkg_values[i].absolute);
722 NOTICE ("network plugin: parse_part_values: "
723 "Don't know how to handle data source type %"PRIu8,
726 } /* switch (pkg_types[i]) */
729 *ret_buffer = buffer;
730 *ret_buffer_len = buffer_len - pkg_length;
731 *ret_num_values = pkg_numval;
732 *ret_values = pkg_values;
737 } /* int parse_part_values */
739 static int parse_part_number (void **ret_buffer, size_t *ret_buffer_len,
742 char *buffer = *ret_buffer;
743 size_t buffer_len = *ret_buffer_len;
747 size_t exp_size = 2 * sizeof (uint16_t) + sizeof (uint64_t);
752 if ((buffer_len < 0) || ((size_t) buffer_len < exp_size))
754 WARNING ("network plugin: parse_part_number: "
756 "Chunk of size %zu expected, "
757 "but buffer has only %zu bytes left.",
758 exp_size, buffer_len);
762 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
763 buffer += sizeof (tmp16);
764 pkg_type = ntohs (tmp16);
766 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
767 buffer += sizeof (tmp16);
768 pkg_length = ntohs (tmp16);
770 memcpy ((void *) &tmp64, buffer, sizeof (tmp64));
771 buffer += sizeof (tmp64);
772 *value = ntohll (tmp64);
774 *ret_buffer = buffer;
775 *ret_buffer_len = buffer_len - pkg_length;
778 } /* int parse_part_number */
780 static int parse_part_string (void **ret_buffer, size_t *ret_buffer_len,
781 char *output, int output_len)
783 char *buffer = *ret_buffer;
784 size_t buffer_len = *ret_buffer_len;
787 size_t header_size = 2 * sizeof (uint16_t);
792 if ((buffer_len < 0) || (buffer_len < header_size))
794 WARNING ("network plugin: parse_part_string: "
796 "Chunk of at least size %zu expected, "
797 "but buffer has only %zu bytes left.",
798 header_size, buffer_len);
802 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
803 buffer += sizeof (tmp16);
804 pkg_type = ntohs (tmp16);
806 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
807 buffer += sizeof (tmp16);
808 pkg_length = ntohs (tmp16);
810 /* Check that packet fits in the input buffer */
811 if (pkg_length > buffer_len)
813 WARNING ("network plugin: parse_part_string: "
815 "Chunk of size %"PRIu16" received, "
816 "but buffer has only %zu bytes left.",
817 pkg_length, buffer_len);
821 /* Check that pkg_length is in the valid range */
822 if (pkg_length <= header_size)
824 WARNING ("network plugin: parse_part_string: "
826 "Header claims this packet is only %hu "
827 "bytes long.", pkg_length);
831 /* Check that the package data fits into the output buffer.
832 * The previous if-statement ensures that:
833 * `pkg_length > header_size' */
835 || ((size_t) output_len < ((size_t) pkg_length - header_size)))
837 WARNING ("network plugin: parse_part_string: "
838 "Output buffer too small.");
842 /* All sanity checks successfull, let's copy the data over */
843 output_len = pkg_length - header_size;
844 memcpy ((void *) output, (void *) buffer, output_len);
845 buffer += output_len;
847 /* For some very weird reason '\0' doesn't do the trick on SPARC in
849 if (output[output_len - 1] != 0)
851 WARNING ("network plugin: parse_part_string: "
852 "Received string does not end "
853 "with a NULL-byte.");
857 *ret_buffer = buffer;
858 *ret_buffer_len = buffer_len - pkg_length;
861 } /* int parse_part_string */
863 /* Forward declaration: parse_part_sign_sha256 and parse_part_encr_aes256 call
864 * parse_packet and vice versa. */
865 #define PP_SIGNED 0x01
866 #define PP_ENCRYPTED 0x02
867 static int parse_packet (sockent_t *se,
868 void *buffer, size_t buffer_size, int flags);
870 #define BUFFER_READ(p,s) do { \
871 memcpy ((p), buffer + buffer_offset, (s)); \
872 buffer_offset += (s); \
876 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
877 void **ret_buffer, size_t *ret_buffer_len, int flags)
881 size_t buffer_offset;
886 part_signature_sha256_t pss;
887 uint16_t pss_head_length;
888 char hash[sizeof (pss.hash)];
892 unsigned char *hash_ptr;
894 buffer = *ret_buffer;
895 buffer_len = *ret_buffer_len;
898 if (se->data.server.userdb == NULL)
900 NOTICE ("network plugin: Received signed network packet but can't verify "
901 "it because no user DB has been configured. Will accept it.");
905 /* Check if the buffer has enough data for this structure. */
906 if (buffer_len <= PART_SIGNATURE_SHA256_SIZE)
909 /* Read type and length header */
910 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
911 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
912 pss_head_length = ntohs (pss.head.length);
914 /* Check if the `pss_head_length' is within bounds. */
915 if ((pss_head_length <= PART_SIGNATURE_SHA256_SIZE)
916 || (pss_head_length > buffer_len))
918 ERROR ("network plugin: HMAC-SHA-256 with invalid length received.");
923 BUFFER_READ (pss.hash, sizeof (pss.hash));
925 /* Calculate username length (without null byte) and allocate memory */
926 username_len = pss_head_length - PART_SIGNATURE_SHA256_SIZE;
927 pss.username = malloc (username_len + 1);
928 if (pss.username == NULL)
931 /* Read the username */
932 BUFFER_READ (pss.username, username_len);
933 pss.username[username_len] = 0;
935 assert (buffer_offset == pss_head_length);
937 /* Query the password */
938 secret = fbh_get (se->data.server.userdb, pss.username);
941 ERROR ("network plugin: Unknown user: %s", pss.username);
942 sfree (pss.username);
946 /* Create a hash device and check the HMAC */
948 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
951 ERROR ("network plugin: Creating HMAC-SHA-256 object failed: %s",
952 gcry_strerror (err));
954 sfree (pss.username);
958 err = gcry_md_setkey (hd, secret, strlen (secret));
961 ERROR ("network plugin: gcry_md_setkey failed: %s", gcry_strerror (err));
967 buffer + PART_SIGNATURE_SHA256_SIZE,
968 buffer_len - PART_SIGNATURE_SHA256_SIZE);
969 hash_ptr = gcry_md_read (hd, GCRY_MD_SHA256);
970 if (hash_ptr == NULL)
972 ERROR ("network plugin: gcry_md_read failed.");
975 sfree (pss.username);
978 memcpy (hash, hash_ptr, sizeof (hash));
985 sfree (pss.username);
987 if (memcmp (pss.hash, hash, sizeof (pss.hash)) != 0)
989 WARNING ("network plugin: Verifying HMAC-SHA-256 signature failed: "
994 parse_packet (se, buffer + buffer_offset, buffer_len - buffer_offset,
998 *ret_buffer = buffer + buffer_len;
1002 } /* }}} int parse_part_sign_sha256 */
1003 /* #endif HAVE_LIBGCRYPT */
1005 #else /* if !HAVE_LIBGCRYPT */
1006 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
1007 void **ret_buffer, size_t *ret_buffer_size, int flags)
1009 static int warning_has_been_printed = 0;
1013 size_t buffer_offset;
1016 part_signature_sha256_t pss;
1018 buffer = *ret_buffer;
1019 buffer_size = *ret_buffer_size;
1022 if (buffer_size <= PART_SIGNATURE_SHA256_SIZE)
1025 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
1026 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
1027 part_len = ntohs (pss.head.length);
1029 if ((part_len <= PART_SIGNATURE_SHA256_SIZE)
1030 || (part_len > buffer_size))
1033 if (warning_has_been_printed == 0)
1035 WARNING ("network plugin: Received signed packet, but the network "
1036 "plugin was not linked with libgcrypt, so I cannot "
1037 "verify the signature. The packet will be accepted.");
1038 warning_has_been_printed = 1;
1041 parse_packet (se, buffer + part_len, buffer_size - part_len, flags);
1043 *ret_buffer = buffer + buffer_size;
1044 *ret_buffer_size = 0;
1047 } /* }}} int parse_part_sign_sha256 */
1048 #endif /* !HAVE_LIBGCRYPT */
1051 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1052 void **ret_buffer, size_t *ret_buffer_len,
1055 char *buffer = *ret_buffer;
1056 size_t buffer_len = *ret_buffer_len;
1059 size_t buffer_offset;
1060 uint16_t username_len;
1061 part_encryption_aes256_t pea;
1062 unsigned char hash[sizeof (pea.hash)];
1064 gcry_cipher_hd_t cypher;
1067 /* Make sure at least the header if available. */
1068 if (buffer_len <= PART_ENCRYPTION_AES256_SIZE)
1070 NOTICE ("network plugin: parse_part_encr_aes256: "
1071 "Discarding short packet.");
1077 /* Copy the unencrypted information into `pea'. */
1078 BUFFER_READ (&pea.head.type, sizeof (pea.head.type));
1079 BUFFER_READ (&pea.head.length, sizeof (pea.head.length));
1081 /* Check the `part size'. */
1082 part_size = ntohs (pea.head.length);
1083 if ((part_size <= PART_ENCRYPTION_AES256_SIZE)
1084 || (part_size > buffer_len))
1086 NOTICE ("network plugin: parse_part_encr_aes256: "
1087 "Discarding part with invalid size.");
1091 /* Read the username */
1092 BUFFER_READ (&username_len, sizeof (username_len));
1093 username_len = ntohs (username_len);
1095 if ((username_len <= 0)
1096 || (username_len > (part_size - (PART_ENCRYPTION_AES256_SIZE + 1))))
1098 NOTICE ("network plugin: parse_part_encr_aes256: "
1099 "Discarding part with invalid username length.");
1103 assert (username_len > 0);
1104 pea.username = malloc (username_len + 1);
1105 if (pea.username == NULL)
1107 BUFFER_READ (pea.username, username_len);
1108 pea.username[username_len] = 0;
1110 /* Last but not least, the initialization vector */
1111 BUFFER_READ (pea.iv, sizeof (pea.iv));
1113 /* Make sure we are at the right position */
1114 assert (buffer_offset == (username_len +
1115 PART_ENCRYPTION_AES256_SIZE - sizeof (pea.hash)));
1117 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
1122 payload_len = part_size - (PART_ENCRYPTION_AES256_SIZE + username_len);
1123 assert (payload_len > 0);
1125 /* Decrypt the packet in-place */
1126 err = gcry_cipher_decrypt (cypher,
1127 buffer + buffer_offset,
1128 part_size - buffer_offset,
1129 /* in = */ NULL, /* in len = */ 0);
1132 ERROR ("network plugin: gcry_cipher_decrypt returned: %s",
1133 gcry_strerror (err));
1138 BUFFER_READ (pea.hash, sizeof (pea.hash));
1140 /* Make sure we're at the right position - again */
1141 assert (buffer_offset == (username_len + PART_ENCRYPTION_AES256_SIZE));
1142 assert (buffer_offset == (part_size - payload_len));
1144 /* Check hash sum */
1145 memset (hash, 0, sizeof (hash));
1146 gcry_md_hash_buffer (GCRY_MD_SHA1, hash,
1147 buffer + buffer_offset, payload_len);
1148 if (memcmp (hash, pea.hash, sizeof (hash)) != 0)
1150 ERROR ("network plugin: Decryption failed: Checksum mismatch.");
1154 parse_packet (se, buffer + buffer_offset, payload_len,
1155 flags | PP_ENCRYPTED);
1157 /* Update return values */
1158 *ret_buffer = buffer + part_size;
1159 *ret_buffer_len = buffer_len - part_size;
1162 } /* }}} int parse_part_encr_aes256 */
1163 /* #endif HAVE_LIBGCRYPT */
1165 #else /* if !HAVE_LIBGCRYPT */
1166 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1167 void **ret_buffer, size_t *ret_buffer_size, int flags)
1169 static int warning_has_been_printed = 0;
1173 size_t buffer_offset;
1178 buffer = *ret_buffer;
1179 buffer_size = *ret_buffer_size;
1182 /* parse_packet assures this minimum size. */
1183 assert (buffer_size >= (sizeof (ph.type) + sizeof (ph.length)));
1185 BUFFER_READ (&ph.type, sizeof (ph.type));
1186 BUFFER_READ (&ph.length, sizeof (ph.length));
1187 ph_length = ntohs (ph.length);
1189 if ((ph_length <= PART_ENCRYPTION_AES256_SIZE)
1190 || (ph_length > buffer_size))
1192 ERROR ("network plugin: AES-256 encrypted part "
1193 "with invalid length received.");
1197 if (warning_has_been_printed == 0)
1199 WARNING ("network plugin: Received encrypted packet, but the network "
1200 "plugin was not linked with libgcrypt, so I cannot "
1201 "decrypt it. The part will be discarded.");
1202 warning_has_been_printed = 1;
1205 *ret_buffer += ph_length;
1206 *ret_buffer_size -= ph_length;
1209 } /* }}} int parse_part_encr_aes256 */
1210 #endif /* !HAVE_LIBGCRYPT */
1214 static int parse_packet (sockent_t *se, /* {{{ */
1215 void *buffer, size_t buffer_size, int flags)
1219 value_list_t vl = VALUE_LIST_INIT;
1223 int packet_was_signed = (flags & PP_SIGNED);
1224 int packet_was_encrypted = (flags & PP_ENCRYPTED);
1225 int printed_ignore_warning = 0;
1226 #endif /* HAVE_LIBGCRYPT */
1229 memset (&vl, '\0', sizeof (vl));
1230 memset (&n, '\0', sizeof (n));
1233 while ((status == 0) && (0 < buffer_size)
1234 && ((unsigned int) buffer_size > sizeof (part_header_t)))
1236 uint16_t pkg_length;
1239 memcpy ((void *) &pkg_type,
1242 memcpy ((void *) &pkg_length,
1243 (void *) (buffer + sizeof (pkg_type)),
1244 sizeof (pkg_length));
1246 pkg_length = ntohs (pkg_length);
1247 pkg_type = ntohs (pkg_type);
1249 if (pkg_length > buffer_size)
1251 /* Ensure that this loop terminates eventually */
1252 if (pkg_length < (2 * sizeof (uint16_t)))
1255 if (pkg_type == TYPE_ENCR_AES256)
1257 status = parse_part_encr_aes256 (se,
1258 &buffer, &buffer_size, flags);
1261 ERROR ("network plugin: Decrypting AES256 "
1263 "with status %i.", status);
1268 else if ((se->data.server.security_level == SECURITY_LEVEL_ENCRYPT)
1269 && (packet_was_encrypted == 0))
1271 if (printed_ignore_warning == 0)
1273 INFO ("network plugin: Unencrypted packet or "
1274 "part has been ignored.");
1275 printed_ignore_warning = 1;
1277 buffer = ((char *) buffer) + pkg_length;
1280 #endif /* HAVE_LIBGCRYPT */
1281 else if (pkg_type == TYPE_SIGN_SHA256)
1283 status = parse_part_sign_sha256 (se,
1284 &buffer, &buffer_size, flags);
1287 ERROR ("network plugin: Verifying HMAC-SHA-256 "
1289 "with status %i.", status);
1294 else if ((se->data.server.security_level == SECURITY_LEVEL_SIGN)
1295 && (packet_was_encrypted == 0)
1296 && (packet_was_signed == 0))
1298 if (printed_ignore_warning == 0)
1300 INFO ("network plugin: Unsigned packet or "
1301 "part has been ignored.");
1302 printed_ignore_warning = 1;
1304 buffer = ((char *) buffer) + pkg_length;
1307 #endif /* HAVE_LIBGCRYPT */
1308 else if (pkg_type == TYPE_VALUES)
1310 status = parse_part_values (&buffer, &buffer_size,
1311 &vl.values, &vl.values_len);
1315 network_dispatch_values (&vl);
1319 else if (pkg_type == TYPE_TIME)
1322 status = parse_part_number (&buffer, &buffer_size,
1326 vl.time = (time_t) tmp;
1327 n.time = (time_t) tmp;
1330 else if (pkg_type == TYPE_INTERVAL)
1333 status = parse_part_number (&buffer, &buffer_size,
1336 vl.interval = (int) tmp;
1338 else if (pkg_type == TYPE_HOST)
1340 status = parse_part_string (&buffer, &buffer_size,
1341 vl.host, sizeof (vl.host));
1343 sstrncpy (n.host, vl.host, sizeof (n.host));
1345 else if (pkg_type == TYPE_PLUGIN)
1347 status = parse_part_string (&buffer, &buffer_size,
1348 vl.plugin, sizeof (vl.plugin));
1350 sstrncpy (n.plugin, vl.plugin,
1353 else if (pkg_type == TYPE_PLUGIN_INSTANCE)
1355 status = parse_part_string (&buffer, &buffer_size,
1357 sizeof (vl.plugin_instance));
1359 sstrncpy (n.plugin_instance,
1361 sizeof (n.plugin_instance));
1363 else if (pkg_type == TYPE_TYPE)
1365 status = parse_part_string (&buffer, &buffer_size,
1366 vl.type, sizeof (vl.type));
1368 sstrncpy (n.type, vl.type, sizeof (n.type));
1370 else if (pkg_type == TYPE_TYPE_INSTANCE)
1372 status = parse_part_string (&buffer, &buffer_size,
1374 sizeof (vl.type_instance));
1376 sstrncpy (n.type_instance, vl.type_instance,
1377 sizeof (n.type_instance));
1379 else if (pkg_type == TYPE_MESSAGE)
1381 status = parse_part_string (&buffer, &buffer_size,
1382 n.message, sizeof (n.message));
1388 else if ((n.severity != NOTIF_FAILURE)
1389 && (n.severity != NOTIF_WARNING)
1390 && (n.severity != NOTIF_OKAY))
1392 INFO ("network plugin: "
1393 "Ignoring notification with "
1394 "unknown severity %i.",
1397 else if (n.time <= 0)
1399 INFO ("network plugin: "
1400 "Ignoring notification with "
1403 else if (strlen (n.message) <= 0)
1405 INFO ("network plugin: "
1406 "Ignoring notification with "
1407 "an empty message.");
1411 plugin_dispatch_notification (&n);
1414 else if (pkg_type == TYPE_SEVERITY)
1417 status = parse_part_number (&buffer, &buffer_size,
1420 n.severity = (int) tmp;
1424 DEBUG ("network plugin: parse_packet: Unknown part"
1425 " type: 0x%04hx", pkg_type);
1426 buffer = ((char *) buffer) + pkg_length;
1428 } /* while (buffer_size > sizeof (part_header_t)) */
1430 if (status == 0 && buffer_size > 0)
1431 WARNING ("network plugin: parse_packet: Received truncated "
1432 "packet, try increasing `MaxPacketSize'");
1435 } /* }}} int parse_packet */
1437 static void free_sockent_client (struct sockent_client *sec) /* {{{ */
1446 sfree (sec->username);
1447 sfree (sec->password);
1448 if (sec->cypher != NULL)
1449 gcry_cipher_close (sec->cypher);
1451 } /* }}} void free_sockent_client */
1453 static void free_sockent_server (struct sockent_server *ses) /* {{{ */
1457 for (i = 0; i < ses->fd_num; i++)
1459 if (ses->fd[i] >= 0)
1468 sfree (ses->auth_file);
1469 fbh_destroy (ses->userdb);
1470 if (ses->cypher != NULL)
1471 gcry_cipher_close (ses->cypher);
1473 } /* }}} void free_sockent_server */
1475 static void sockent_destroy (sockent_t *se) /* {{{ */
1479 DEBUG ("network plugin: sockent_destroy (se = %p);", (void *) se);
1486 sfree (se->service);
1488 if (se->type == SOCKENT_TYPE_CLIENT)
1489 free_sockent_client (&se->data.client);
1491 free_sockent_server (&se->data.server);
1496 } /* }}} void sockent_destroy */
1499 * int network_set_ttl
1501 * Set the `IP_MULTICAST_TTL', `IP_TTL', `IPV6_MULTICAST_HOPS' or
1502 * `IPV6_UNICAST_HOPS', depending on which option is applicable.
1504 * The `struct addrinfo' is used to destinguish between unicast and multicast
1507 static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
1509 DEBUG ("network plugin: network_set_ttl: network_config_ttl = %i;",
1510 network_config_ttl);
1512 assert (se->type == SOCKENT_TYPE_CLIENT);
1514 if ((network_config_ttl < 1) || (network_config_ttl > 255))
1517 if (ai->ai_family == AF_INET)
1519 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1522 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1523 optname = IP_MULTICAST_TTL;
1527 if (setsockopt (se->data.client.fd, IPPROTO_IP, optname,
1528 &network_config_ttl,
1529 sizeof (network_config_ttl)) == -1)
1532 ERROR ("setsockopt: %s",
1533 sstrerror (errno, errbuf, sizeof (errbuf)));
1537 else if (ai->ai_family == AF_INET6)
1539 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1540 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1543 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1544 optname = IPV6_MULTICAST_HOPS;
1546 optname = IPV6_UNICAST_HOPS;
1548 if (setsockopt (se->data.client.fd, IPPROTO_IPV6, optname,
1549 &network_config_ttl,
1550 sizeof (network_config_ttl)) == -1)
1553 ERROR ("setsockopt: %s",
1554 sstrerror (errno, errbuf,
1561 } /* int network_set_ttl */
1563 static int network_bind_socket (int fd, const struct addrinfo *ai)
1568 /* allow multiple sockets to use the same PORT number */
1569 if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR,
1570 &yes, sizeof(yes)) == -1) {
1572 ERROR ("setsockopt: %s",
1573 sstrerror (errno, errbuf, sizeof (errbuf)));
1577 DEBUG ("fd = %i; calling `bind'", fd);
1579 if (bind (fd, ai->ai_addr, ai->ai_addrlen) == -1)
1583 sstrerror (errno, errbuf, sizeof (errbuf)));
1587 if (ai->ai_family == AF_INET)
1589 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1590 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1592 struct ip_mreq mreq;
1594 DEBUG ("fd = %i; IPv4 multicast address found", fd);
1596 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1597 mreq.imr_interface.s_addr = htonl (INADDR_ANY);
1599 if (setsockopt (fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1600 &loop, sizeof (loop)) == -1)
1603 ERROR ("setsockopt: %s",
1604 sstrerror (errno, errbuf,
1609 if (setsockopt (fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1610 &mreq, sizeof (mreq)) == -1)
1613 ERROR ("setsockopt: %s",
1614 sstrerror (errno, errbuf,
1620 else if (ai->ai_family == AF_INET6)
1622 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1623 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1624 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1626 struct ipv6_mreq mreq;
1628 DEBUG ("fd = %i; IPv6 multicast address found", fd);
1630 memcpy (&mreq.ipv6mr_multiaddr,
1632 sizeof (addr->sin6_addr));
1634 /* http://developer.apple.com/documentation/Darwin/Reference/ManPages/man4/ip6.4.html
1635 * ipv6mr_interface may be set to zeroes to
1636 * choose the default multicast interface or to
1637 * the index of a particular multicast-capable
1638 * interface if the host is multihomed.
1639 * Membership is associ-associated with a
1640 * single interface; programs running on
1641 * multihomed hosts may need to join the same
1642 * group on more than one interface.*/
1643 mreq.ipv6mr_interface = 0;
1645 if (setsockopt (fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
1646 &loop, sizeof (loop)) == -1)
1649 ERROR ("setsockopt: %s",
1650 sstrerror (errno, errbuf,
1655 if (setsockopt (fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
1656 &mreq, sizeof (mreq)) == -1)
1659 ERROR ("setsockopt: %s",
1660 sstrerror (errno, errbuf,
1668 } /* int network_bind_socket */
1670 /* Initialize a sockent structure. `type' must be either `SOCKENT_TYPE_CLIENT'
1671 * or `SOCKENT_TYPE_SERVER' */
1672 static int sockent_init (sockent_t *se, int type) /* {{{ */
1677 memset (se, 0, sizeof (*se));
1679 se->type = SOCKENT_TYPE_CLIENT;
1684 if (type == SOCKENT_TYPE_SERVER)
1686 se->type = SOCKENT_TYPE_SERVER;
1687 se->data.server.fd = NULL;
1689 se->data.server.security_level = SECURITY_LEVEL_NONE;
1690 se->data.server.auth_file = NULL;
1691 se->data.server.userdb = NULL;
1692 se->data.server.cypher = NULL;
1697 se->data.client.fd = -1;
1698 se->data.client.addr = NULL;
1700 se->data.client.security_level = SECURITY_LEVEL_NONE;
1701 se->data.client.username = NULL;
1702 se->data.client.password = NULL;
1703 se->data.client.cypher = NULL;
1708 } /* }}} int sockent_init */
1710 /* Open the file descriptors for a initialized sockent structure. */
1711 static int sockent_open (sockent_t *se) /* {{{ */
1713 struct addrinfo ai_hints;
1714 struct addrinfo *ai_list, *ai_ptr;
1718 const char *service;
1723 /* Set up the security structures. */
1724 #if HAVE_LIBGCRYPT /* {{{ */
1725 if (se->type == SOCKENT_TYPE_CLIENT)
1727 if (se->data.client.security_level > SECURITY_LEVEL_NONE)
1729 if ((se->data.client.username == NULL)
1730 || (se->data.client.password == NULL))
1732 ERROR ("network plugin: Client socket with "
1733 "security requested, but no "
1734 "credentials are configured.");
1737 gcry_md_hash_buffer (GCRY_MD_SHA256,
1738 se->data.client.password_hash,
1739 se->data.client.password,
1740 strlen (se->data.client.password));
1743 else /* (se->type == SOCKENT_TYPE_SERVER) */
1745 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
1747 if (se->data.server.auth_file == NULL)
1749 ERROR ("network plugin: Server socket with "
1750 "security requested, but no "
1751 "password file is configured.");
1755 if (se->data.server.auth_file != NULL)
1757 se->data.server.userdb = fbh_create (se->data.server.auth_file);
1758 if (se->data.server.userdb == NULL)
1760 ERROR ("network plugin: Reading password file "
1762 se->data.server.auth_file);
1763 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
1768 #endif /* }}} HAVE_LIBGCRYPT */
1771 service = se->service;
1773 if (service == NULL)
1774 service = NET_DEFAULT_PORT;
1776 DEBUG ("network plugin: sockent_open: node = %s; service = %s;",
1779 memset (&ai_hints, 0, sizeof (ai_hints));
1780 ai_hints.ai_flags = 0;
1782 ai_hints.ai_flags |= AI_PASSIVE;
1784 #ifdef AI_ADDRCONFIG
1785 ai_hints.ai_flags |= AI_ADDRCONFIG;
1787 ai_hints.ai_family = AF_UNSPEC;
1788 ai_hints.ai_socktype = SOCK_DGRAM;
1789 ai_hints.ai_protocol = IPPROTO_UDP;
1791 ai_return = getaddrinfo (node, service, &ai_hints, &ai_list);
1794 ERROR ("network plugin: getaddrinfo (%s, %s) failed: %s",
1795 (se->node == NULL) ? "(null)" : se->node,
1796 (se->service == NULL) ? "(null)" : se->service,
1797 gai_strerror (ai_return));
1801 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1805 if (se->type == SOCKENT_TYPE_SERVER) /* {{{ */
1809 tmp = realloc (se->data.server.fd,
1810 sizeof (*tmp) * (se->data.server.fd_num + 1));
1813 ERROR ("network plugin: realloc failed.");
1816 se->data.server.fd = tmp;
1817 tmp = se->data.server.fd + se->data.server.fd_num;
1819 *tmp = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
1820 ai_ptr->ai_protocol);
1824 ERROR ("network plugin: socket(2) failed: %s",
1825 sstrerror (errno, errbuf,
1830 status = network_bind_socket (*tmp, ai_ptr);
1838 se->data.server.fd_num++;
1840 } /* }}} if (se->type == SOCKENT_TYPE_SERVER) */
1841 else /* if (se->type == SOCKENT_TYPE_CLIENT) {{{ */
1843 se->data.client.fd = socket (ai_ptr->ai_family,
1844 ai_ptr->ai_socktype,
1845 ai_ptr->ai_protocol);
1846 if (se->data.client.fd < 0)
1849 ERROR ("network plugin: socket(2) failed: %s",
1850 sstrerror (errno, errbuf,
1855 se->data.client.addr = malloc (sizeof (*se->data.client.addr));
1856 if (se->data.client.addr == NULL)
1858 ERROR ("network plugin: malloc failed.");
1859 close (se->data.client.fd);
1860 se->data.client.fd = -1;
1864 memset (se->data.client.addr, 0, sizeof (*se->data.client.addr));
1865 assert (sizeof (*se->data.client.addr) >= ai_ptr->ai_addrlen);
1866 memcpy (se->data.client.addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1867 se->data.client.addrlen = ai_ptr->ai_addrlen;
1869 network_set_ttl (se, ai_ptr);
1871 /* We don't open more than one write-socket per
1872 * node/service pair.. */
1874 } /* }}} if (se->type == SOCKENT_TYPE_CLIENT) */
1875 } /* for (ai_list) */
1877 freeaddrinfo (ai_list);
1879 /* Check if all went well. */
1880 if (se->type == SOCKENT_TYPE_SERVER)
1882 if (se->data.server.fd_num <= 0)
1885 else /* if (se->type == SOCKENT_TYPE_CLIENT) */
1887 if (se->data.client.fd < 0)
1892 } /* }}} int sockent_open */
1894 /* Add a sockent to the global list of sockets */
1895 static int sockent_add (sockent_t *se) /* {{{ */
1897 sockent_t *last_ptr;
1902 if (se->type == SOCKENT_TYPE_SERVER)
1907 tmp = realloc (listen_sockets_pollfd,
1908 sizeof (*tmp) * (listen_sockets_num
1909 + se->data.server.fd_num));
1912 ERROR ("network plugin: realloc failed.");
1915 listen_sockets_pollfd = tmp;
1916 tmp = listen_sockets_pollfd + listen_sockets_num;
1918 for (i = 0; i < se->data.server.fd_num; i++)
1920 memset (tmp + i, 0, sizeof (*tmp));
1921 tmp[i].fd = se->data.server.fd[i];
1922 tmp[i].events = POLLIN | POLLPRI;
1926 listen_sockets_num += se->data.server.fd_num;
1928 if (listen_sockets == NULL)
1930 listen_sockets = se;
1933 last_ptr = listen_sockets;
1935 else /* if (se->type == SOCKENT_TYPE_CLIENT) */
1937 if (sending_sockets == NULL)
1939 sending_sockets = se;
1942 last_ptr = sending_sockets;
1945 while (last_ptr->next != NULL)
1946 last_ptr = last_ptr->next;
1947 last_ptr->next = se;
1950 } /* }}} int sockent_add */
1952 static void *dispatch_thread (void __attribute__((unused)) *arg) /* {{{ */
1956 receive_list_entry_t *ent;
1959 /* Lock and wait for more data to come in */
1960 pthread_mutex_lock (&receive_list_lock);
1961 while ((listen_loop == 0)
1962 && (receive_list_head == NULL))
1963 pthread_cond_wait (&receive_list_cond, &receive_list_lock);
1965 /* Remove the head entry and unlock */
1966 ent = receive_list_head;
1968 receive_list_head = ent->next;
1969 pthread_mutex_unlock (&receive_list_lock);
1971 /* Check whether we are supposed to exit. We do NOT check `listen_loop'
1972 * because we dispatch all missing packets before shutting down. */
1976 /* Look for the correct `sockent_t' */
1977 se = listen_sockets;
1982 for (i = 0; i < se->data.server.fd_num; i++)
1983 if (se->data.server.fd[i] == ent->fd)
1986 if (i < se->data.server.fd_num)
1994 ERROR ("network plugin: Got packet from FD %i, but can't "
1995 "find an appropriate socket entry.",
2002 parse_packet (se, ent->data, ent->data_len, /* flags = */ 0);
2008 } /* }}} void *dispatch_thread */
2010 static int network_receive (void) /* {{{ */
2012 char buffer[network_config_packet_size];
2018 receive_list_entry_t *private_list_head;
2019 receive_list_entry_t *private_list_tail;
2021 assert (listen_sockets_num > 0);
2023 private_list_head = NULL;
2024 private_list_tail = NULL;
2026 while (listen_loop == 0)
2028 status = poll (listen_sockets_pollfd, listen_sockets_num, -1);
2035 ERROR ("poll failed: %s",
2036 sstrerror (errno, errbuf, sizeof (errbuf)));
2040 for (i = 0; (i < listen_sockets_num) && (status > 0); i++)
2042 receive_list_entry_t *ent;
2044 if ((listen_sockets_pollfd[i].revents
2045 & (POLLIN | POLLPRI)) == 0)
2049 buffer_len = recv (listen_sockets_pollfd[i].fd,
2050 buffer, sizeof (buffer),
2055 ERROR ("recv failed: %s",
2056 sstrerror (errno, errbuf,
2061 /* TODO: Possible performance enhancement: Do not free
2062 * these entries in the dispatch thread but put them in
2063 * another list, so we don't have to allocate more and
2064 * more of these structures. */
2065 ent = malloc (sizeof (receive_list_entry_t));
2068 ERROR ("network plugin: malloc failed.");
2071 memset (ent, 0, sizeof (receive_list_entry_t));
2072 ent->data = malloc (network_config_packet_size);
2073 if (ent->data == NULL)
2075 ERROR ("network plugin: malloc failed.");
2078 ent->fd = listen_sockets_pollfd[i].fd;
2081 memcpy (ent->data, buffer, buffer_len);
2082 ent->data_len = buffer_len;
2084 if (private_list_head == NULL)
2085 private_list_head = ent;
2087 private_list_tail->next = ent;
2088 private_list_tail = ent;
2090 /* Do not block here. Blocking here has led to
2091 * insufficient performance in the past. */
2092 if (pthread_mutex_trylock (&receive_list_lock) == 0)
2094 if (receive_list_head == NULL)
2095 receive_list_head = private_list_head;
2097 receive_list_tail->next = private_list_head;
2098 receive_list_tail = private_list_tail;
2100 private_list_head = NULL;
2101 private_list_tail = NULL;
2103 pthread_cond_signal (&receive_list_cond);
2104 pthread_mutex_unlock (&receive_list_lock);
2106 } /* for (listen_sockets_pollfd) */
2107 } /* while (listen_loop == 0) */
2109 /* Make sure everything is dispatched before exiting. */
2110 if (private_list_head != NULL)
2112 pthread_mutex_lock (&receive_list_lock);
2114 if (receive_list_head == NULL)
2115 receive_list_head = private_list_head;
2117 receive_list_tail->next = private_list_head;
2118 receive_list_tail = private_list_tail;
2120 private_list_head = NULL;
2121 private_list_tail = NULL;
2123 pthread_cond_signal (&receive_list_cond);
2124 pthread_mutex_unlock (&receive_list_lock);
2128 } /* }}} int network_receive */
2130 static void *receive_thread (void __attribute__((unused)) *arg)
2132 return (network_receive () ? (void *) 1 : (void *) 0);
2133 } /* void *receive_thread */
2135 static void network_init_buffer (void)
2137 memset (send_buffer, 0, network_config_packet_size);
2138 send_buffer_ptr = send_buffer;
2139 send_buffer_fill = 0;
2141 memset (&send_buffer_vl, 0, sizeof (send_buffer_vl));
2142 } /* int network_init_buffer */
2144 static void networt_send_buffer_plain (const sockent_t *se, /* {{{ */
2145 const char *buffer, size_t buffer_size)
2151 status = sendto (se->data.client.fd, buffer, buffer_size,
2153 (struct sockaddr *) se->data.client.addr,
2154 se->data.client.addrlen);
2160 ERROR ("network plugin: sendto failed: %s",
2161 sstrerror (errno, errbuf,
2168 } /* }}} void networt_send_buffer_plain */
2171 #define BUFFER_ADD(p,s) do { \
2172 memcpy (buffer + buffer_offset, (p), (s)); \
2173 buffer_offset += (s); \
2176 static void networt_send_buffer_signed (const sockent_t *se, /* {{{ */
2177 const char *in_buffer, size_t in_buffer_size)
2179 part_signature_sha256_t ps;
2180 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2181 size_t buffer_offset;
2182 size_t username_len;
2186 unsigned char *hash;
2189 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
2192 ERROR ("network plugin: Creating HMAC object failed: %s",
2193 gcry_strerror (err));
2197 err = gcry_md_setkey (hd, se->data.client.password,
2198 strlen (se->data.client.password));
2201 ERROR ("network plugin: gcry_md_setkey failed: %s",
2202 gcry_strerror (err));
2207 username_len = strlen (se->data.client.username);
2208 if (username_len > (BUFF_SIG_SIZE - PART_SIGNATURE_SHA256_SIZE))
2210 ERROR ("network plugin: Username too long: %s",
2211 se->data.client.username);
2215 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE,
2216 se->data.client.username, username_len);
2217 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE + username_len,
2218 in_buffer, in_buffer_size);
2220 /* Initialize the `ps' structure. */
2221 memset (&ps, 0, sizeof (ps));
2222 ps.head.type = htons (TYPE_SIGN_SHA256);
2223 ps.head.length = htons (PART_SIGNATURE_SHA256_SIZE + username_len);
2225 /* Calculate the hash value. */
2226 gcry_md_write (hd, buffer + PART_SIGNATURE_SHA256_SIZE,
2227 username_len + in_buffer_size);
2228 hash = gcry_md_read (hd, GCRY_MD_SHA256);
2231 ERROR ("network plugin: gcry_md_read failed.");
2235 memcpy (ps.hash, hash, sizeof (ps.hash));
2237 /* Add the header */
2240 BUFFER_ADD (&ps.head.type, sizeof (ps.head.type));
2241 BUFFER_ADD (&ps.head.length, sizeof (ps.head.length));
2242 BUFFER_ADD (ps.hash, sizeof (ps.hash));
2244 assert (buffer_offset == PART_SIGNATURE_SHA256_SIZE);
2249 buffer_offset = PART_SIGNATURE_SHA256_SIZE + username_len + in_buffer_size;
2250 networt_send_buffer_plain (se, buffer, buffer_offset);
2251 } /* }}} void networt_send_buffer_signed */
2253 static void networt_send_buffer_encrypted (sockent_t *se, /* {{{ */
2254 const char *in_buffer, size_t in_buffer_size)
2256 part_encryption_aes256_t pea;
2257 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2259 size_t buffer_offset;
2261 size_t username_len;
2263 gcry_cipher_hd_t cypher;
2265 /* Initialize the header fields */
2266 memset (&pea, 0, sizeof (pea));
2267 pea.head.type = htons (TYPE_ENCR_AES256);
2269 pea.username = se->data.client.username;
2271 username_len = strlen (pea.username);
2272 if ((PART_ENCRYPTION_AES256_SIZE + username_len) > BUFF_SIG_SIZE)
2274 ERROR ("network plugin: Username too long: %s", pea.username);
2278 buffer_size = PART_ENCRYPTION_AES256_SIZE + username_len + in_buffer_size;
2279 header_size = PART_ENCRYPTION_AES256_SIZE + username_len
2280 - sizeof (pea.hash);
2282 assert (buffer_size <= sizeof (buffer));
2283 DEBUG ("network plugin: networt_send_buffer_encrypted: "
2284 "buffer_size = %zu;", buffer_size);
2286 pea.head.length = htons ((uint16_t) (PART_ENCRYPTION_AES256_SIZE
2287 + username_len + in_buffer_size));
2288 pea.username_length = htons ((uint16_t) username_len);
2290 /* Chose a random initialization vector. */
2291 gcry_randomize ((void *) &pea.iv, sizeof (pea.iv), GCRY_STRONG_RANDOM);
2293 /* Create hash of the payload */
2294 gcry_md_hash_buffer (GCRY_MD_SHA1, pea.hash, in_buffer, in_buffer_size);
2296 /* Initialize the buffer */
2298 memset (buffer, 0, sizeof (buffer));
2301 BUFFER_ADD (&pea.head.type, sizeof (pea.head.type));
2302 BUFFER_ADD (&pea.head.length, sizeof (pea.head.length));
2303 BUFFER_ADD (&pea.username_length, sizeof (pea.username_length));
2304 BUFFER_ADD (pea.username, username_len);
2305 BUFFER_ADD (pea.iv, sizeof (pea.iv));
2306 assert (buffer_offset == header_size);
2307 BUFFER_ADD (pea.hash, sizeof (pea.hash));
2308 BUFFER_ADD (in_buffer, in_buffer_size);
2310 assert (buffer_offset == buffer_size);
2312 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
2313 se->data.client.password);
2317 /* Encrypt the buffer in-place */
2318 err = gcry_cipher_encrypt (cypher,
2319 buffer + header_size,
2320 buffer_size - header_size,
2321 /* in = */ NULL, /* in len = */ 0);
2324 ERROR ("network plugin: gcry_cipher_encrypt returned: %s",
2325 gcry_strerror (err));
2329 /* Send it out without further modifications */
2330 networt_send_buffer_plain (se, buffer, buffer_size);
2331 } /* }}} void networt_send_buffer_encrypted */
2333 #endif /* HAVE_LIBGCRYPT */
2335 static void network_send_buffer (char *buffer, size_t buffer_len) /* {{{ */
2339 DEBUG ("network plugin: network_send_buffer: buffer_len = %zu", buffer_len);
2341 for (se = sending_sockets; se != NULL; se = se->next)
2344 if (se->data.client.security_level == SECURITY_LEVEL_ENCRYPT)
2345 networt_send_buffer_encrypted (se, buffer, buffer_len);
2346 else if (se->data.client.security_level == SECURITY_LEVEL_SIGN)
2347 networt_send_buffer_signed (se, buffer, buffer_len);
2348 else /* if (se->data.client.security_level == SECURITY_LEVEL_NONE) */
2349 #endif /* HAVE_LIBGCRYPT */
2350 networt_send_buffer_plain (se, buffer, buffer_len);
2351 } /* for (sending_sockets) */
2352 } /* }}} void network_send_buffer */
2354 static int add_to_buffer (char *buffer, int buffer_size, /* {{{ */
2355 value_list_t *vl_def,
2356 const data_set_t *ds, const value_list_t *vl)
2358 char *buffer_orig = buffer;
2360 if (strcmp (vl_def->host, vl->host) != 0)
2362 if (write_part_string (&buffer, &buffer_size, TYPE_HOST,
2363 vl->host, strlen (vl->host)) != 0)
2365 sstrncpy (vl_def->host, vl->host, sizeof (vl_def->host));
2368 if (vl_def->time != vl->time)
2370 if (write_part_number (&buffer, &buffer_size, TYPE_TIME,
2371 (uint64_t) vl->time))
2373 vl_def->time = vl->time;
2376 if (vl_def->interval != vl->interval)
2378 if (write_part_number (&buffer, &buffer_size, TYPE_INTERVAL,
2379 (uint64_t) vl->interval))
2381 vl_def->interval = vl->interval;
2384 if (strcmp (vl_def->plugin, vl->plugin) != 0)
2386 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN,
2387 vl->plugin, strlen (vl->plugin)) != 0)
2389 sstrncpy (vl_def->plugin, vl->plugin, sizeof (vl_def->plugin));
2392 if (strcmp (vl_def->plugin_instance, vl->plugin_instance) != 0)
2394 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
2395 vl->plugin_instance,
2396 strlen (vl->plugin_instance)) != 0)
2398 sstrncpy (vl_def->plugin_instance, vl->plugin_instance, sizeof (vl_def->plugin_instance));
2401 if (strcmp (vl_def->type, vl->type) != 0)
2403 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
2404 vl->type, strlen (vl->type)) != 0)
2406 sstrncpy (vl_def->type, ds->type, sizeof (vl_def->type));
2409 if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
2411 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE_INSTANCE,
2413 strlen (vl->type_instance)) != 0)
2415 sstrncpy (vl_def->type_instance, vl->type_instance, sizeof (vl_def->type_instance));
2418 if (write_part_values (&buffer, &buffer_size, ds, vl) != 0)
2421 return (buffer - buffer_orig);
2422 } /* }}} int add_to_buffer */
2424 static void flush_buffer (void)
2426 DEBUG ("network plugin: flush_buffer: send_buffer_fill = %i",
2429 network_send_buffer (send_buffer, (size_t) send_buffer_fill);
2430 network_init_buffer ();
2433 static int network_write (const data_set_t *ds, const value_list_t *vl,
2434 user_data_t __attribute__((unused)) *user_data)
2438 if (!check_send_okay (vl))
2441 char name[6*DATA_MAX_NAME_LEN];
2442 FORMAT_VL (name, sizeof (name), vl);
2443 name[sizeof (name) - 1] = 0;
2444 DEBUG ("network plugin: network_write: "
2445 "NOT sending %s.", name);
2450 uc_meta_data_add_unsigned_int (vl,
2451 "network:time_sent", (uint64_t) vl->time);
2453 pthread_mutex_lock (&send_buffer_lock);
2455 status = add_to_buffer (send_buffer_ptr,
2456 network_config_packet_size - (send_buffer_fill + BUFF_SIG_SIZE),
2461 /* status == bytes added to the buffer */
2462 send_buffer_fill += status;
2463 send_buffer_ptr += status;
2469 status = add_to_buffer (send_buffer_ptr,
2470 network_config_packet_size - (send_buffer_fill + BUFF_SIG_SIZE),
2476 send_buffer_fill += status;
2477 send_buffer_ptr += status;
2483 ERROR ("network plugin: Unable to append to the "
2484 "buffer for some weird reason");
2486 else if ((network_config_packet_size - send_buffer_fill) < 15)
2491 pthread_mutex_unlock (&send_buffer_lock);
2493 return ((status < 0) ? -1 : 0);
2494 } /* int network_write */
2496 static int network_config_set_boolean (const oconfig_item_t *ci, /* {{{ */
2499 if ((ci->values_num != 1)
2500 || ((ci->values[0].type != OCONFIG_TYPE_BOOLEAN)
2501 && (ci->values[0].type != OCONFIG_TYPE_STRING)))
2503 ERROR ("network plugin: The `%s' config option needs "
2504 "exactly one boolean argument.", ci->key);
2508 if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
2510 if (ci->values[0].value.boolean)
2517 char *str = ci->values[0].value.string;
2519 if ((strcasecmp ("true", str) == 0)
2520 || (strcasecmp ("yes", str) == 0)
2521 || (strcasecmp ("on", str) == 0))
2523 else if ((strcasecmp ("false", str) == 0)
2524 || (strcasecmp ("no", str) == 0)
2525 || (strcasecmp ("off", str) == 0))
2529 ERROR ("network plugin: Cannot parse string value `%s' of the `%s' "
2530 "option as boolean value.",
2537 } /* }}} int network_config_set_boolean */
2539 static int network_config_set_ttl (const oconfig_item_t *ci) /* {{{ */
2542 if ((ci->values_num != 1)
2543 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
2545 WARNING ("network plugin: The `TimeToLive' config option needs exactly "
2546 "one numeric argument.");
2550 tmp = (int) ci->values[0].value.number;
2551 if ((tmp > 0) && (tmp <= 255))
2552 network_config_ttl = tmp;
2555 } /* }}} int network_config_set_ttl */
2557 static int network_config_set_buffer_size (const oconfig_item_t *ci) /* {{{ */
2560 if ((ci->values_num != 1)
2561 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
2563 WARNING ("network plugin: The `MaxPacketSize' config option needs exactly "
2564 "one numeric argument.");
2568 tmp = (int) ci->values[0].value.number;
2569 if ((tmp >= 1024) && (tmp <= 65535))
2570 network_config_packet_size = tmp;
2573 } /* }}} int network_config_set_buffer_size */
2576 static int network_config_set_string (const oconfig_item_t *ci, /* {{{ */
2580 if ((ci->values_num != 1)
2581 || (ci->values[0].type != OCONFIG_TYPE_STRING))
2583 WARNING ("network plugin: The `%s' config option needs exactly "
2584 "one string argument.", ci->key);
2588 tmp = strdup (ci->values[0].value.string);
2592 sfree (*ret_string);
2596 } /* }}} int network_config_set_string */
2597 #endif /* HAVE_LIBGCRYPT */
2600 static int network_config_set_security_level (oconfig_item_t *ci, /* {{{ */
2604 if ((ci->values_num != 1)
2605 || (ci->values[0].type != OCONFIG_TYPE_STRING))
2607 WARNING ("network plugin: The `SecurityLevel' config option needs exactly "
2608 "one string argument.");
2612 str = ci->values[0].value.string;
2613 if (strcasecmp ("Encrypt", str) == 0)
2614 *retval = SECURITY_LEVEL_ENCRYPT;
2615 else if (strcasecmp ("Sign", str) == 0)
2616 *retval = SECURITY_LEVEL_SIGN;
2617 else if (strcasecmp ("None", str) == 0)
2618 *retval = SECURITY_LEVEL_NONE;
2621 WARNING ("network plugin: Unknown security level: %s.", str);
2626 } /* }}} int network_config_set_security_level */
2627 #endif /* HAVE_LIBGCRYPT */
2629 static int network_config_add_listen (const oconfig_item_t *ci) /* {{{ */
2635 if ((ci->values_num < 1) || (ci->values_num > 2)
2636 || (ci->values[0].type != OCONFIG_TYPE_STRING)
2637 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
2639 ERROR ("network plugin: The `%s' config option needs "
2640 "one or two string arguments.", ci->key);
2644 se = malloc (sizeof (*se));
2647 ERROR ("network plugin: malloc failed.");
2650 sockent_init (se, SOCKENT_TYPE_SERVER);
2652 se->node = strdup (ci->values[0].value.string);
2653 if (ci->values_num >= 2)
2654 se->service = strdup (ci->values[1].value.string);
2656 for (i = 0; i < ci->children_num; i++)
2658 oconfig_item_t *child = ci->children + i;
2661 if (strcasecmp ("AuthFile", child->key) == 0)
2662 network_config_set_string (child, &se->data.server.auth_file);
2663 else if (strcasecmp ("SecurityLevel", child->key) == 0)
2664 network_config_set_security_level (child,
2665 &se->data.server.security_level);
2667 #endif /* HAVE_LIBGCRYPT */
2669 WARNING ("network plugin: Option `%s' is not allowed here.",
2675 if ((se->data.server.security_level > SECURITY_LEVEL_NONE)
2676 && (se->data.server.auth_file == NULL))
2678 ERROR ("network plugin: A security level higher than `none' was "
2679 "requested, but no AuthFile option was given. Cowardly refusing to "
2680 "open this socket!");
2681 sockent_destroy (se);
2684 #endif /* HAVE_LIBGCRYPT */
2686 status = sockent_open (se);
2689 ERROR ("network plugin: network_config_add_listen: sockent_open failed.");
2690 sockent_destroy (se);
2694 status = sockent_add (se);
2697 ERROR ("network plugin: network_config_add_listen: sockent_add failed.");
2698 sockent_destroy (se);
2703 } /* }}} int network_config_add_listen */
2705 static int network_config_add_server (const oconfig_item_t *ci) /* {{{ */
2711 if ((ci->values_num < 1) || (ci->values_num > 2)
2712 || (ci->values[0].type != OCONFIG_TYPE_STRING)
2713 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
2715 ERROR ("network plugin: The `%s' config option needs "
2716 "one or two string arguments.", ci->key);
2720 se = malloc (sizeof (*se));
2723 ERROR ("network plugin: malloc failed.");
2726 sockent_init (se, SOCKENT_TYPE_CLIENT);
2728 se->node = strdup (ci->values[0].value.string);
2729 if (ci->values_num >= 2)
2730 se->service = strdup (ci->values[1].value.string);
2732 for (i = 0; i < ci->children_num; i++)
2734 oconfig_item_t *child = ci->children + i;
2737 if (strcasecmp ("Username", child->key) == 0)
2738 network_config_set_string (child, &se->data.client.username);
2739 else if (strcasecmp ("Password", child->key) == 0)
2740 network_config_set_string (child, &se->data.client.password);
2741 else if (strcasecmp ("SecurityLevel", child->key) == 0)
2742 network_config_set_security_level (child,
2743 &se->data.client.security_level);
2745 #endif /* HAVE_LIBGCRYPT */
2747 WARNING ("network plugin: Option `%s' is not allowed here.",
2753 if ((se->data.client.security_level > SECURITY_LEVEL_NONE)
2754 && ((se->data.client.username == NULL)
2755 || (se->data.client.password == NULL)))
2757 ERROR ("network plugin: A security level higher than `none' was "
2758 "requested, but no Username or Password option was given. "
2759 "Cowardly refusing to open this socket!");
2760 sockent_destroy (se);
2763 #endif /* HAVE_LIBGCRYPT */
2765 status = sockent_open (se);
2768 ERROR ("network plugin: network_config_add_server: sockent_open failed.");
2769 sockent_destroy (se);
2773 status = sockent_add (se);
2776 ERROR ("network plugin: network_config_add_server: sockent_add failed.");
2777 sockent_destroy (se);
2782 } /* }}} int network_config_add_server */
2784 static int network_config (oconfig_item_t *ci) /* {{{ */
2788 for (i = 0; i < ci->children_num; i++)
2790 oconfig_item_t *child = ci->children + i;
2792 if (strcasecmp ("Listen", child->key) == 0)
2793 network_config_add_listen (child);
2794 else if (strcasecmp ("Server", child->key) == 0)
2795 network_config_add_server (child);
2796 else if (strcasecmp ("TimeToLive", child->key) == 0)
2797 network_config_set_ttl (child);
2798 else if (strcasecmp ("MaxPacketSize", child->key) == 0)
2799 network_config_set_buffer_size (child);
2800 else if (strcasecmp ("Forward", child->key) == 0)
2801 network_config_set_boolean (child, &network_config_forward);
2802 else if (strcasecmp ("CacheFlush", child->key) == 0)
2803 /* no op for backwards compatibility only */;
2806 WARNING ("network plugin: Option `%s' is not allowed here.",
2812 } /* }}} int network_config */
2814 static int network_notification (const notification_t *n,
2815 user_data_t __attribute__((unused)) *user_data)
2817 char buffer[network_config_packet_size];
2818 char *buffer_ptr = buffer;
2819 int buffer_free = sizeof (buffer);
2822 memset (buffer, '\0', sizeof (buffer));
2825 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_TIME,
2826 (uint64_t) n->time);
2830 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_SEVERITY,
2831 (uint64_t) n->severity);
2835 if (strlen (n->host) > 0)
2837 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_HOST,
2838 n->host, strlen (n->host));
2843 if (strlen (n->plugin) > 0)
2845 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_PLUGIN,
2846 n->plugin, strlen (n->plugin));
2851 if (strlen (n->plugin_instance) > 0)
2853 status = write_part_string (&buffer_ptr, &buffer_free,
2854 TYPE_PLUGIN_INSTANCE,
2855 n->plugin_instance, strlen (n->plugin_instance));
2860 if (strlen (n->type) > 0)
2862 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE,
2863 n->type, strlen (n->type));
2868 if (strlen (n->type_instance) > 0)
2870 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE_INSTANCE,
2871 n->type_instance, strlen (n->type_instance));
2876 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_MESSAGE,
2877 n->message, strlen (n->message));
2881 network_send_buffer (buffer, sizeof (buffer) - buffer_free);
2884 } /* int network_notification */
2886 static int network_shutdown (void)
2890 /* Kill the listening thread */
2891 if (receive_thread_running != 0)
2893 INFO ("network plugin: Stopping receive thread.");
2894 pthread_kill (receive_thread_id, SIGTERM);
2895 pthread_join (receive_thread_id, NULL /* no return value */);
2896 memset (&receive_thread_id, 0, sizeof (receive_thread_id));
2897 receive_thread_running = 0;
2900 /* Shutdown the dispatching thread */
2901 if (dispatch_thread_running != 0)
2903 INFO ("network plugin: Stopping dispatch thread.");
2904 pthread_mutex_lock (&receive_list_lock);
2905 pthread_cond_broadcast (&receive_list_cond);
2906 pthread_mutex_unlock (&receive_list_lock);
2907 pthread_join (dispatch_thread_id, /* ret = */ NULL);
2908 dispatch_thread_running = 0;
2911 sockent_destroy (listen_sockets);
2913 if (send_buffer_fill > 0)
2916 sfree (send_buffer);
2918 /* TODO: Close `sending_sockets' */
2920 plugin_unregister_config ("network");
2921 plugin_unregister_init ("network");
2922 plugin_unregister_write ("network");
2923 plugin_unregister_shutdown ("network");
2926 } /* int network_shutdown */
2928 static int network_init (void)
2930 static _Bool have_init = false;
2932 /* Check if we were already initialized. If so, just return - there's
2933 * nothing more to do (for now, that is). */
2939 gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
2940 gcry_control (GCRYCTL_INIT_SECMEM, 32768, 0);
2941 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
2944 plugin_register_shutdown ("network", network_shutdown);
2946 send_buffer = malloc (network_config_packet_size);
2947 if (send_buffer == NULL)
2949 ERROR ("network plugin: malloc failed.");
2952 network_init_buffer ();
2954 /* setup socket(s) and so on */
2955 if (sending_sockets != NULL)
2957 plugin_register_write ("network", network_write,
2958 /* user_data = */ NULL);
2959 plugin_register_notification ("network", network_notification,
2960 /* user_data = */ NULL);
2963 /* If no threads need to be started, return here. */
2964 if ((listen_sockets_num == 0)
2965 || ((dispatch_thread_running != 0)
2966 && (receive_thread_running != 0)))
2969 if (dispatch_thread_running == 0)
2972 status = pthread_create (&dispatch_thread_id,
2973 NULL /* no attributes */,
2975 NULL /* no argument */);
2979 ERROR ("network: pthread_create failed: %s",
2980 sstrerror (errno, errbuf,
2985 dispatch_thread_running = 1;
2989 if (receive_thread_running == 0)
2992 status = pthread_create (&receive_thread_id,
2993 NULL /* no attributes */,
2995 NULL /* no argument */);
2999 ERROR ("network: pthread_create failed: %s",
3000 sstrerror (errno, errbuf,
3005 receive_thread_running = 1;
3010 } /* int network_init */
3013 * The flush option of the network plugin cannot flush individual identifiers.
3014 * All the values are added to a buffer and sent when the buffer is full, the
3015 * requested value may or may not be in there, it's not worth finding out. We
3016 * just send the buffer if `flush' is called - if the requested value was in
3017 * there, good. If not, well, then there is nothing to flush.. -octo
3019 static int network_flush (int timeout,
3020 const char __attribute__((unused)) *identifier,
3021 user_data_t __attribute__((unused)) *user_data)
3023 pthread_mutex_lock (&send_buffer_lock);
3025 if (send_buffer_fill > 0)
3028 pthread_mutex_unlock (&send_buffer_lock);
3031 } /* int network_flush */
3033 void module_register (void)
3035 plugin_register_complex_config ("network", network_config);
3036 plugin_register_init ("network", network_init);
3037 plugin_register_flush ("network", network_flush,
3038 /* user_data = */ NULL);
3039 } /* void module_register */
3041 /* vim: set fdm=marker : */