2 * collectd - src/network.c
3 * Copyright (C) 2005-2009 Florian octo Forster
4 * Copyright (C) 2009 Aman Gupta
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; only version 2.1 of the License is
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 * Florian octo Forster <octo at verplant.org>
22 * Aman Gupta <aman at tmm1.net>
25 #define _BSD_SOURCE /* For struct ip_mreq */
30 #include "configfile.h"
31 #include "utils_fbhash.h"
32 #include "utils_avltree.h"
33 #include "utils_cache.h"
41 # include <sys/socket.h>
47 # include <netinet/in.h>
50 # include <arpa/inet.h>
61 GCRY_THREAD_OPTION_PTHREAD_IMPL;
64 #ifndef IPV6_ADD_MEMBERSHIP
65 # ifdef IPV6_JOIN_GROUP
66 # define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
68 # error "Neither IP_ADD_MEMBERSHIP nor IPV6_JOIN_GROUP is defined"
70 #endif /* !IP_ADD_MEMBERSHIP */
73 * Maximum size required for encryption / signing:
75 * 42 bytes for the encryption header
76 * + 64 bytes for the username
80 #define BUFF_SIG_SIZE 106
85 #define SECURITY_LEVEL_NONE 0
87 # define SECURITY_LEVEL_SIGN 1
88 # define SECURITY_LEVEL_ENCRYPT 2
93 struct sockaddr_storage *addr;
99 gcry_cipher_hd_t cypher;
100 unsigned char password_hash[32];
104 struct sockent_server
112 gcry_cipher_hd_t cypher;
116 typedef struct sockent
118 #define SOCKENT_TYPE_CLIENT 1
119 #define SOCKENT_TYPE_SERVER 2
128 struct sockent_client client;
129 struct sockent_server server;
132 struct sockent *next;
135 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
136 * 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
137 * +-------+-----------------------+-------------------------------+
138 * ! Ver. ! ! Length !
139 * +-------+-----------------------+-------------------------------+
146 typedef struct part_header_s part_header_t;
148 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
149 * 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
150 * +-------------------------------+-------------------------------+
152 * +-------------------------------+-------------------------------+
153 * : (Length - 4) Bytes :
154 * +---------------------------------------------------------------+
161 typedef struct part_string_s part_string_t;
163 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
164 * 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
165 * +-------------------------------+-------------------------------+
167 * +-------------------------------+-------------------------------+
168 * : (Length - 4 == 2 || 4 || 8) Bytes :
169 * +---------------------------------------------------------------+
176 typedef struct part_number_s part_number_t;
178 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
179 * 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
180 * +-------------------------------+-------------------------------+
182 * +-------------------------------+---------------+---------------+
183 * ! Num of values ! Type0 ! Type1 !
184 * +-------------------------------+---------------+---------------+
187 * +---------------------------------------------------------------+
190 * +---------------------------------------------------------------+
195 uint16_t *num_values;
196 uint8_t *values_types;
199 typedef struct part_values_s part_values_t;
201 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
202 * 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
203 * +-------------------------------+-------------------------------+
205 * +-------------------------------+-------------------------------+
206 * ! Hash (Bits 0 - 31) !
208 * ! Hash (Bits 224 - 255) !
209 * +---------------------------------------------------------------+
212 #define PART_SIGNATURE_SHA256_SIZE 36
213 struct part_signature_sha256_s
216 unsigned char hash[32];
219 typedef struct part_signature_sha256_s part_signature_sha256_t;
221 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
222 * 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
223 * +-------------------------------+-------------------------------+
225 * +-------------------------------+-------------------------------+
226 * ! Original length ! Padding (0 - 15 bytes) !
227 * +-------------------------------+-------------------------------+
228 * ! Hash (Bits 0 - 31) !
230 * ! Hash (Bits 128 - 159) !
231 * +---------------------------------------------------------------+
234 #define PART_ENCRYPTION_AES256_SIZE 42
235 struct part_encryption_aes256_s
238 uint16_t username_length;
240 unsigned char iv[16];
242 unsigned char hash[20];
246 typedef struct part_encryption_aes256_s part_encryption_aes256_t;
248 struct receive_list_entry_s
253 struct receive_list_entry_s *next;
255 typedef struct receive_list_entry_s receive_list_entry_t;
260 static int network_config_ttl = 0;
261 static size_t network_config_packet_size = 1024;
262 static int network_config_forward = 0;
263 static int network_config_stats = 0;
265 static sockent_t *sending_sockets = NULL;
267 static receive_list_entry_t *receive_list_head = NULL;
268 static receive_list_entry_t *receive_list_tail = NULL;
269 static pthread_mutex_t receive_list_lock = PTHREAD_MUTEX_INITIALIZER;
270 static pthread_cond_t receive_list_cond = PTHREAD_COND_INITIALIZER;
271 static uint64_t receive_list_length = 0;
273 static sockent_t *listen_sockets = NULL;
274 static struct pollfd *listen_sockets_pollfd = NULL;
275 static size_t listen_sockets_num = 0;
277 /* The receive and dispatch threads will run as long as `listen_loop' is set to
279 static int listen_loop = 0;
280 static int receive_thread_running = 0;
281 static pthread_t receive_thread_id;
282 static int dispatch_thread_running = 0;
283 static pthread_t dispatch_thread_id;
285 /* Buffer in which to-be-sent network packets are constructed. */
286 static char *send_buffer;
287 static char *send_buffer_ptr;
288 static int send_buffer_fill;
289 static value_list_t send_buffer_vl = VALUE_LIST_STATIC;
290 static pthread_mutex_t send_buffer_lock = PTHREAD_MUTEX_INITIALIZER;
292 /* XXX: These counters are incremented from one place only. The spot in which
293 * the values are incremented is either only reachable by one thread (the
294 * dispatch thread, for example) or locked by some lock (send_buffer_lock for
295 * example). Only if neither is true, the stats_lock is acquired. The counters
296 * are always read without holding a lock in the hope that writing 8 bytes to
297 * memory is an atomic operation. */
298 static uint64_t stats_octets_rx = 0;
299 static uint64_t stats_octets_tx = 0;
300 static uint64_t stats_packets_rx = 0;
301 static uint64_t stats_packets_tx = 0;
302 static uint64_t stats_values_dispatched = 0;
303 static uint64_t stats_values_not_dispatched = 0;
304 static uint64_t stats_values_sent = 0;
305 static uint64_t stats_values_not_sent = 0;
306 static pthread_mutex_t stats_lock = PTHREAD_MUTEX_INITIALIZER;
311 static _Bool check_receive_okay (const value_list_t *vl) /* {{{ */
313 uint64_t time_sent = 0;
316 status = uc_meta_data_get_unsigned_int (vl,
317 "network:time_sent", &time_sent);
319 /* This is a value we already sent. Don't allow it to be received again in
320 * order to avoid looping. */
321 if ((status == 0) && (time_sent >= ((uint64_t) vl->time)))
325 } /* }}} _Bool check_receive_okay */
327 static _Bool check_send_okay (const value_list_t *vl) /* {{{ */
329 _Bool received = false;
332 if (network_config_forward != 0)
335 if (vl->meta == NULL)
338 status = meta_data_get_boolean (vl->meta, "network:received", &received);
339 if (status == -ENOENT)
341 else if (status != 0)
343 ERROR ("network plugin: check_send_okay: meta_data_get_boolean failed "
344 "with status %i.", status);
348 /* By default, only *send* value lists that were not *received* by the
351 } /* }}} _Bool check_send_okay */
353 static int network_dispatch_values (value_list_t *vl) /* {{{ */
358 || (strlen (vl->host) <= 0)
359 || (strlen (vl->plugin) <= 0)
360 || (strlen (vl->type) <= 0))
363 if (!check_receive_okay (vl))
366 char name[6*DATA_MAX_NAME_LEN];
367 FORMAT_VL (name, sizeof (name), vl);
368 name[sizeof (name) - 1] = 0;
369 DEBUG ("network plugin: network_dispatch_values: "
370 "NOT dispatching %s.", name);
372 stats_values_not_dispatched++;
376 assert (vl->meta == NULL);
378 vl->meta = meta_data_create ();
379 if (vl->meta == NULL)
381 ERROR ("network plugin: meta_data_create failed.");
385 status = meta_data_add_boolean (vl->meta, "network:received", true);
388 ERROR ("network plugin: meta_data_add_boolean failed.");
389 meta_data_destroy (vl->meta);
394 plugin_dispatch_values (vl);
395 stats_values_dispatched++;
397 meta_data_destroy (vl->meta);
401 } /* }}} int network_dispatch_values */
404 static gcry_cipher_hd_t network_get_aes256_cypher (sockent_t *se, /* {{{ */
405 const void *iv, size_t iv_size, const char *username)
408 gcry_cipher_hd_t *cyper_ptr;
409 unsigned char password_hash[32];
411 if (se->type == SOCKENT_TYPE_CLIENT)
413 cyper_ptr = &se->data.client.cypher;
414 memcpy (password_hash, se->data.client.password_hash,
415 sizeof (password_hash));
421 cyper_ptr = &se->data.server.cypher;
423 if (username == NULL)
426 secret = fbh_get (se->data.server.userdb, username);
430 gcry_md_hash_buffer (GCRY_MD_SHA256,
432 secret, strlen (secret));
437 if (*cyper_ptr == NULL)
439 err = gcry_cipher_open (cyper_ptr,
440 GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_OFB, /* flags = */ 0);
443 ERROR ("network plugin: gcry_cipher_open returned: %s",
444 gcry_strerror (err));
451 gcry_cipher_reset (*cyper_ptr);
453 assert (*cyper_ptr != NULL);
455 err = gcry_cipher_setkey (*cyper_ptr,
456 password_hash, sizeof (password_hash));
459 ERROR ("network plugin: gcry_cipher_setkey returned: %s",
460 gcry_strerror (err));
461 gcry_cipher_close (*cyper_ptr);
466 err = gcry_cipher_setiv (*cyper_ptr, iv, iv_size);
469 ERROR ("network plugin: gcry_cipher_setkey returned: %s",
470 gcry_strerror (err));
471 gcry_cipher_close (*cyper_ptr);
477 } /* }}} int network_get_aes256_cypher */
478 #endif /* HAVE_LIBGCRYPT */
480 static int write_part_values (char **ret_buffer, int *ret_buffer_len,
481 const data_set_t *ds, const value_list_t *vl)
487 part_header_t pkg_ph;
488 uint16_t pkg_num_values;
489 uint8_t *pkg_values_types;
495 num_values = vl->values_len;
496 packet_len = sizeof (part_header_t) + sizeof (uint16_t)
497 + (num_values * sizeof (uint8_t))
498 + (num_values * sizeof (value_t));
500 if (*ret_buffer_len < packet_len)
503 pkg_values_types = (uint8_t *) malloc (num_values * sizeof (uint8_t));
504 if (pkg_values_types == NULL)
506 ERROR ("network plugin: write_part_values: malloc failed.");
510 pkg_values = (value_t *) malloc (num_values * sizeof (value_t));
511 if (pkg_values == NULL)
513 free (pkg_values_types);
514 ERROR ("network plugin: write_part_values: malloc failed.");
518 pkg_ph.type = htons (TYPE_VALUES);
519 pkg_ph.length = htons (packet_len);
521 pkg_num_values = htons ((uint16_t) vl->values_len);
523 for (i = 0; i < num_values; i++)
525 pkg_values_types[i] = (uint8_t) ds->ds[i].type;
526 switch (ds->ds[i].type)
528 case DS_TYPE_COUNTER:
529 pkg_values[i].counter = htonll (vl->values[i].counter);
533 pkg_values[i].gauge = htond (vl->values[i].gauge);
537 pkg_values[i].derive = htonll (vl->values[i].derive);
540 case DS_TYPE_ABSOLUTE:
541 pkg_values[i].absolute = htonll (vl->values[i].absolute);
545 free (pkg_values_types);
547 ERROR ("network plugin: write_part_values: "
548 "Unknown data source type: %i",
551 } /* switch (ds->ds[i].type) */
552 } /* for (num_values) */
555 * Use `memcpy' to write everything to the buffer, because the pointer
556 * may be unaligned and some architectures, such as SPARC, can't handle
559 packet_ptr = *ret_buffer;
561 memcpy (packet_ptr + offset, &pkg_ph, sizeof (pkg_ph));
562 offset += sizeof (pkg_ph);
563 memcpy (packet_ptr + offset, &pkg_num_values, sizeof (pkg_num_values));
564 offset += sizeof (pkg_num_values);
565 memcpy (packet_ptr + offset, pkg_values_types, num_values * sizeof (uint8_t));
566 offset += num_values * sizeof (uint8_t);
567 memcpy (packet_ptr + offset, pkg_values, num_values * sizeof (value_t));
568 offset += num_values * sizeof (value_t);
570 assert (offset == packet_len);
572 *ret_buffer = packet_ptr + packet_len;
573 *ret_buffer_len -= packet_len;
575 free (pkg_values_types);
579 } /* int write_part_values */
581 static int write_part_number (char **ret_buffer, int *ret_buffer_len,
582 int type, uint64_t value)
587 part_header_t pkg_head;
592 packet_len = sizeof (pkg_head) + sizeof (pkg_value);
594 if (*ret_buffer_len < packet_len)
597 pkg_head.type = htons (type);
598 pkg_head.length = htons (packet_len);
599 pkg_value = htonll (value);
601 packet_ptr = *ret_buffer;
603 memcpy (packet_ptr + offset, &pkg_head, sizeof (pkg_head));
604 offset += sizeof (pkg_head);
605 memcpy (packet_ptr + offset, &pkg_value, sizeof (pkg_value));
606 offset += sizeof (pkg_value);
608 assert (offset == packet_len);
610 *ret_buffer = packet_ptr + packet_len;
611 *ret_buffer_len -= packet_len;
614 } /* int write_part_number */
616 static int write_part_string (char **ret_buffer, int *ret_buffer_len,
617 int type, const char *str, int str_len)
627 buffer_len = 2 * sizeof (uint16_t) + str_len + 1;
628 if (*ret_buffer_len < buffer_len)
631 pkg_type = htons (type);
632 pkg_length = htons (buffer_len);
634 buffer = *ret_buffer;
636 memcpy (buffer + offset, (void *) &pkg_type, sizeof (pkg_type));
637 offset += sizeof (pkg_type);
638 memcpy (buffer + offset, (void *) &pkg_length, sizeof (pkg_length));
639 offset += sizeof (pkg_length);
640 memcpy (buffer + offset, str, str_len);
642 memset (buffer + offset, '\0', 1);
645 assert (offset == buffer_len);
647 *ret_buffer = buffer + buffer_len;
648 *ret_buffer_len -= buffer_len;
651 } /* int write_part_string */
653 static int parse_part_values (void **ret_buffer, size_t *ret_buffer_len,
654 value_t **ret_values, int *ret_num_values)
656 char *buffer = *ret_buffer;
657 size_t buffer_len = *ret_buffer_len;
672 NOTICE ("network plugin: packet is too short: "
673 "buffer_len = %zu", buffer_len);
677 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
678 buffer += sizeof (tmp16);
679 pkg_type = ntohs (tmp16);
681 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
682 buffer += sizeof (tmp16);
683 pkg_length = ntohs (tmp16);
685 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
686 buffer += sizeof (tmp16);
687 pkg_numval = ntohs (tmp16);
689 assert (pkg_type == TYPE_VALUES);
691 exp_size = 3 * sizeof (uint16_t)
692 + pkg_numval * (sizeof (uint8_t) + sizeof (value_t));
693 if ((buffer_len < 0) || (buffer_len < exp_size))
695 WARNING ("network plugin: parse_part_values: "
697 "Chunk of size %zu expected, "
698 "but buffer has only %zu bytes left.",
699 exp_size, buffer_len);
703 if (pkg_length != exp_size)
705 WARNING ("network plugin: parse_part_values: "
706 "Length and number of values "
707 "in the packet don't match.");
711 pkg_types = (uint8_t *) malloc (pkg_numval * sizeof (uint8_t));
712 pkg_values = (value_t *) malloc (pkg_numval * sizeof (value_t));
713 if ((pkg_types == NULL) || (pkg_values == NULL))
717 ERROR ("network plugin: parse_part_values: malloc failed.");
721 memcpy ((void *) pkg_types, (void *) buffer, pkg_numval * sizeof (uint8_t));
722 buffer += pkg_numval * sizeof (uint8_t);
723 memcpy ((void *) pkg_values, (void *) buffer, pkg_numval * sizeof (value_t));
724 buffer += pkg_numval * sizeof (value_t);
726 for (i = 0; i < pkg_numval; i++)
728 switch (pkg_types[i])
730 case DS_TYPE_COUNTER:
731 pkg_values[i].counter = (counter_t) ntohll (pkg_values[i].counter);
735 pkg_values[i].gauge = (gauge_t) ntohd (pkg_values[i].gauge);
739 pkg_values[i].derive = (derive_t) ntohll (pkg_values[i].derive);
742 case DS_TYPE_ABSOLUTE:
743 pkg_values[i].absolute = (absolute_t) ntohll (pkg_values[i].absolute);
749 NOTICE ("network plugin: parse_part_values: "
750 "Don't know how to handle data source type %"PRIu8,
753 } /* switch (pkg_types[i]) */
756 *ret_buffer = buffer;
757 *ret_buffer_len = buffer_len - pkg_length;
758 *ret_num_values = pkg_numval;
759 *ret_values = pkg_values;
764 } /* int parse_part_values */
766 static int parse_part_number (void **ret_buffer, size_t *ret_buffer_len,
769 char *buffer = *ret_buffer;
770 size_t buffer_len = *ret_buffer_len;
774 size_t exp_size = 2 * sizeof (uint16_t) + sizeof (uint64_t);
779 if ((buffer_len < 0) || ((size_t) buffer_len < exp_size))
781 WARNING ("network plugin: parse_part_number: "
783 "Chunk of size %zu expected, "
784 "but buffer has only %zu bytes left.",
785 exp_size, buffer_len);
789 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
790 buffer += sizeof (tmp16);
791 pkg_type = ntohs (tmp16);
793 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
794 buffer += sizeof (tmp16);
795 pkg_length = ntohs (tmp16);
797 memcpy ((void *) &tmp64, buffer, sizeof (tmp64));
798 buffer += sizeof (tmp64);
799 *value = ntohll (tmp64);
801 *ret_buffer = buffer;
802 *ret_buffer_len = buffer_len - pkg_length;
805 } /* int parse_part_number */
807 static int parse_part_string (void **ret_buffer, size_t *ret_buffer_len,
808 char *output, int output_len)
810 char *buffer = *ret_buffer;
811 size_t buffer_len = *ret_buffer_len;
814 size_t header_size = 2 * sizeof (uint16_t);
819 if ((buffer_len < 0) || (buffer_len < header_size))
821 WARNING ("network plugin: parse_part_string: "
823 "Chunk of at least size %zu expected, "
824 "but buffer has only %zu bytes left.",
825 header_size, buffer_len);
829 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
830 buffer += sizeof (tmp16);
831 pkg_type = ntohs (tmp16);
833 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
834 buffer += sizeof (tmp16);
835 pkg_length = ntohs (tmp16);
837 /* Check that packet fits in the input buffer */
838 if (pkg_length > buffer_len)
840 WARNING ("network plugin: parse_part_string: "
842 "Chunk of size %"PRIu16" received, "
843 "but buffer has only %zu bytes left.",
844 pkg_length, buffer_len);
848 /* Check that pkg_length is in the valid range */
849 if (pkg_length <= header_size)
851 WARNING ("network plugin: parse_part_string: "
853 "Header claims this packet is only %hu "
854 "bytes long.", pkg_length);
858 /* Check that the package data fits into the output buffer.
859 * The previous if-statement ensures that:
860 * `pkg_length > header_size' */
862 || ((size_t) output_len < ((size_t) pkg_length - header_size)))
864 WARNING ("network plugin: parse_part_string: "
865 "Output buffer too small.");
869 /* All sanity checks successfull, let's copy the data over */
870 output_len = pkg_length - header_size;
871 memcpy ((void *) output, (void *) buffer, output_len);
872 buffer += output_len;
874 /* For some very weird reason '\0' doesn't do the trick on SPARC in
876 if (output[output_len - 1] != 0)
878 WARNING ("network plugin: parse_part_string: "
879 "Received string does not end "
880 "with a NULL-byte.");
884 *ret_buffer = buffer;
885 *ret_buffer_len = buffer_len - pkg_length;
888 } /* int parse_part_string */
890 /* Forward declaration: parse_part_sign_sha256 and parse_part_encr_aes256 call
891 * parse_packet and vice versa. */
892 #define PP_SIGNED 0x01
893 #define PP_ENCRYPTED 0x02
894 static int parse_packet (sockent_t *se,
895 void *buffer, size_t buffer_size, int flags);
897 #define BUFFER_READ(p,s) do { \
898 memcpy ((p), buffer + buffer_offset, (s)); \
899 buffer_offset += (s); \
903 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
904 void **ret_buffer, size_t *ret_buffer_len, int flags)
908 size_t buffer_offset;
913 part_signature_sha256_t pss;
914 uint16_t pss_head_length;
915 char hash[sizeof (pss.hash)];
919 unsigned char *hash_ptr;
921 buffer = *ret_buffer;
922 buffer_len = *ret_buffer_len;
925 if (se->data.server.userdb == NULL)
927 NOTICE ("network plugin: Received signed network packet but can't verify "
928 "it because no user DB has been configured. Will accept it.");
932 /* Check if the buffer has enough data for this structure. */
933 if (buffer_len <= PART_SIGNATURE_SHA256_SIZE)
936 /* Read type and length header */
937 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
938 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
939 pss_head_length = ntohs (pss.head.length);
941 /* Check if the `pss_head_length' is within bounds. */
942 if ((pss_head_length <= PART_SIGNATURE_SHA256_SIZE)
943 || (pss_head_length > buffer_len))
945 ERROR ("network plugin: HMAC-SHA-256 with invalid length received.");
950 BUFFER_READ (pss.hash, sizeof (pss.hash));
952 /* Calculate username length (without null byte) and allocate memory */
953 username_len = pss_head_length - PART_SIGNATURE_SHA256_SIZE;
954 pss.username = malloc (username_len + 1);
955 if (pss.username == NULL)
958 /* Read the username */
959 BUFFER_READ (pss.username, username_len);
960 pss.username[username_len] = 0;
962 assert (buffer_offset == pss_head_length);
964 /* Query the password */
965 secret = fbh_get (se->data.server.userdb, pss.username);
968 ERROR ("network plugin: Unknown user: %s", pss.username);
969 sfree (pss.username);
973 /* Create a hash device and check the HMAC */
975 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
978 ERROR ("network plugin: Creating HMAC-SHA-256 object failed: %s",
979 gcry_strerror (err));
981 sfree (pss.username);
985 err = gcry_md_setkey (hd, secret, strlen (secret));
988 ERROR ("network plugin: gcry_md_setkey failed: %s", gcry_strerror (err));
994 buffer + PART_SIGNATURE_SHA256_SIZE,
995 buffer_len - PART_SIGNATURE_SHA256_SIZE);
996 hash_ptr = gcry_md_read (hd, GCRY_MD_SHA256);
997 if (hash_ptr == NULL)
999 ERROR ("network plugin: gcry_md_read failed.");
1002 sfree (pss.username);
1005 memcpy (hash, hash_ptr, sizeof (hash));
1012 sfree (pss.username);
1014 if (memcmp (pss.hash, hash, sizeof (pss.hash)) != 0)
1016 WARNING ("network plugin: Verifying HMAC-SHA-256 signature failed: "
1021 parse_packet (se, buffer + buffer_offset, buffer_len - buffer_offset,
1025 *ret_buffer = buffer + buffer_len;
1026 *ret_buffer_len = 0;
1029 } /* }}} int parse_part_sign_sha256 */
1030 /* #endif HAVE_LIBGCRYPT */
1032 #else /* if !HAVE_LIBGCRYPT */
1033 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
1034 void **ret_buffer, size_t *ret_buffer_size, int flags)
1036 static int warning_has_been_printed = 0;
1040 size_t buffer_offset;
1043 part_signature_sha256_t pss;
1045 buffer = *ret_buffer;
1046 buffer_size = *ret_buffer_size;
1049 if (buffer_size <= PART_SIGNATURE_SHA256_SIZE)
1052 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
1053 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
1054 part_len = ntohs (pss.head.length);
1056 if ((part_len <= PART_SIGNATURE_SHA256_SIZE)
1057 || (part_len > buffer_size))
1060 if (warning_has_been_printed == 0)
1062 WARNING ("network plugin: Received signed packet, but the network "
1063 "plugin was not linked with libgcrypt, so I cannot "
1064 "verify the signature. The packet will be accepted.");
1065 warning_has_been_printed = 1;
1068 parse_packet (se, buffer + part_len, buffer_size - part_len, flags);
1070 *ret_buffer = buffer + buffer_size;
1071 *ret_buffer_size = 0;
1074 } /* }}} int parse_part_sign_sha256 */
1075 #endif /* !HAVE_LIBGCRYPT */
1078 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1079 void **ret_buffer, size_t *ret_buffer_len,
1082 char *buffer = *ret_buffer;
1083 size_t buffer_len = *ret_buffer_len;
1086 size_t buffer_offset;
1087 uint16_t username_len;
1088 part_encryption_aes256_t pea;
1089 unsigned char hash[sizeof (pea.hash)];
1091 gcry_cipher_hd_t cypher;
1094 /* Make sure at least the header if available. */
1095 if (buffer_len <= PART_ENCRYPTION_AES256_SIZE)
1097 NOTICE ("network plugin: parse_part_encr_aes256: "
1098 "Discarding short packet.");
1104 /* Copy the unencrypted information into `pea'. */
1105 BUFFER_READ (&pea.head.type, sizeof (pea.head.type));
1106 BUFFER_READ (&pea.head.length, sizeof (pea.head.length));
1108 /* Check the `part size'. */
1109 part_size = ntohs (pea.head.length);
1110 if ((part_size <= PART_ENCRYPTION_AES256_SIZE)
1111 || (part_size > buffer_len))
1113 NOTICE ("network plugin: parse_part_encr_aes256: "
1114 "Discarding part with invalid size.");
1118 /* Read the username */
1119 BUFFER_READ (&username_len, sizeof (username_len));
1120 username_len = ntohs (username_len);
1122 if ((username_len <= 0)
1123 || (username_len > (part_size - (PART_ENCRYPTION_AES256_SIZE + 1))))
1125 NOTICE ("network plugin: parse_part_encr_aes256: "
1126 "Discarding part with invalid username length.");
1130 assert (username_len > 0);
1131 pea.username = malloc (username_len + 1);
1132 if (pea.username == NULL)
1134 BUFFER_READ (pea.username, username_len);
1135 pea.username[username_len] = 0;
1137 /* Last but not least, the initialization vector */
1138 BUFFER_READ (pea.iv, sizeof (pea.iv));
1140 /* Make sure we are at the right position */
1141 assert (buffer_offset == (username_len +
1142 PART_ENCRYPTION_AES256_SIZE - sizeof (pea.hash)));
1144 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
1148 sfree (pea.username);
1152 payload_len = part_size - (PART_ENCRYPTION_AES256_SIZE + username_len);
1153 assert (payload_len > 0);
1155 /* Decrypt the packet in-place */
1156 err = gcry_cipher_decrypt (cypher,
1157 buffer + buffer_offset,
1158 part_size - buffer_offset,
1159 /* in = */ NULL, /* in len = */ 0);
1162 sfree (pea.username);
1163 ERROR ("network plugin: gcry_cipher_decrypt returned: %s",
1164 gcry_strerror (err));
1169 BUFFER_READ (pea.hash, sizeof (pea.hash));
1171 /* Make sure we're at the right position - again */
1172 assert (buffer_offset == (username_len + PART_ENCRYPTION_AES256_SIZE));
1173 assert (buffer_offset == (part_size - payload_len));
1175 /* Check hash sum */
1176 memset (hash, 0, sizeof (hash));
1177 gcry_md_hash_buffer (GCRY_MD_SHA1, hash,
1178 buffer + buffer_offset, payload_len);
1179 if (memcmp (hash, pea.hash, sizeof (hash)) != 0)
1181 sfree (pea.username);
1182 ERROR ("network plugin: Decryption failed: Checksum mismatch.");
1186 parse_packet (se, buffer + buffer_offset, payload_len,
1187 flags | PP_ENCRYPTED);
1189 /* Update return values */
1190 *ret_buffer = buffer + part_size;
1191 *ret_buffer_len = buffer_len - part_size;
1193 sfree (pea.username);
1196 } /* }}} int parse_part_encr_aes256 */
1197 /* #endif HAVE_LIBGCRYPT */
1199 #else /* if !HAVE_LIBGCRYPT */
1200 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1201 void **ret_buffer, size_t *ret_buffer_size, int flags)
1203 static int warning_has_been_printed = 0;
1207 size_t buffer_offset;
1212 buffer = *ret_buffer;
1213 buffer_size = *ret_buffer_size;
1216 /* parse_packet assures this minimum size. */
1217 assert (buffer_size >= (sizeof (ph.type) + sizeof (ph.length)));
1219 BUFFER_READ (&ph.type, sizeof (ph.type));
1220 BUFFER_READ (&ph.length, sizeof (ph.length));
1221 ph_length = ntohs (ph.length);
1223 if ((ph_length <= PART_ENCRYPTION_AES256_SIZE)
1224 || (ph_length > buffer_size))
1226 ERROR ("network plugin: AES-256 encrypted part "
1227 "with invalid length received.");
1231 if (warning_has_been_printed == 0)
1233 WARNING ("network plugin: Received encrypted packet, but the network "
1234 "plugin was not linked with libgcrypt, so I cannot "
1235 "decrypt it. The part will be discarded.");
1236 warning_has_been_printed = 1;
1239 *ret_buffer += ph_length;
1240 *ret_buffer_size -= ph_length;
1243 } /* }}} int parse_part_encr_aes256 */
1244 #endif /* !HAVE_LIBGCRYPT */
1248 static int parse_packet (sockent_t *se, /* {{{ */
1249 void *buffer, size_t buffer_size, int flags)
1253 value_list_t vl = VALUE_LIST_INIT;
1257 int packet_was_signed = (flags & PP_SIGNED);
1258 int packet_was_encrypted = (flags & PP_ENCRYPTED);
1259 int printed_ignore_warning = 0;
1260 #endif /* HAVE_LIBGCRYPT */
1263 memset (&vl, '\0', sizeof (vl));
1264 memset (&n, '\0', sizeof (n));
1267 while ((status == 0) && (0 < buffer_size)
1268 && ((unsigned int) buffer_size > sizeof (part_header_t)))
1270 uint16_t pkg_length;
1273 memcpy ((void *) &pkg_type,
1276 memcpy ((void *) &pkg_length,
1277 (void *) (buffer + sizeof (pkg_type)),
1278 sizeof (pkg_length));
1280 pkg_length = ntohs (pkg_length);
1281 pkg_type = ntohs (pkg_type);
1283 if (pkg_length > buffer_size)
1285 /* Ensure that this loop terminates eventually */
1286 if (pkg_length < (2 * sizeof (uint16_t)))
1289 if (pkg_type == TYPE_ENCR_AES256)
1291 status = parse_part_encr_aes256 (se,
1292 &buffer, &buffer_size, flags);
1295 ERROR ("network plugin: Decrypting AES256 "
1297 "with status %i.", status);
1302 else if ((se->data.server.security_level == SECURITY_LEVEL_ENCRYPT)
1303 && (packet_was_encrypted == 0))
1305 if (printed_ignore_warning == 0)
1307 INFO ("network plugin: Unencrypted packet or "
1308 "part has been ignored.");
1309 printed_ignore_warning = 1;
1311 buffer = ((char *) buffer) + pkg_length;
1314 #endif /* HAVE_LIBGCRYPT */
1315 else if (pkg_type == TYPE_SIGN_SHA256)
1317 status = parse_part_sign_sha256 (se,
1318 &buffer, &buffer_size, flags);
1321 ERROR ("network plugin: Verifying HMAC-SHA-256 "
1323 "with status %i.", status);
1328 else if ((se->data.server.security_level == SECURITY_LEVEL_SIGN)
1329 && (packet_was_encrypted == 0)
1330 && (packet_was_signed == 0))
1332 if (printed_ignore_warning == 0)
1334 INFO ("network plugin: Unsigned packet or "
1335 "part has been ignored.");
1336 printed_ignore_warning = 1;
1338 buffer = ((char *) buffer) + pkg_length;
1341 #endif /* HAVE_LIBGCRYPT */
1342 else if (pkg_type == TYPE_VALUES)
1344 status = parse_part_values (&buffer, &buffer_size,
1345 &vl.values, &vl.values_len);
1349 network_dispatch_values (&vl);
1353 else if (pkg_type == TYPE_TIME)
1356 status = parse_part_number (&buffer, &buffer_size,
1360 vl.time = (time_t) tmp;
1361 n.time = (time_t) tmp;
1364 else if (pkg_type == TYPE_INTERVAL)
1367 status = parse_part_number (&buffer, &buffer_size,
1370 vl.interval = (int) tmp;
1372 else if (pkg_type == TYPE_HOST)
1374 status = parse_part_string (&buffer, &buffer_size,
1375 vl.host, sizeof (vl.host));
1377 sstrncpy (n.host, vl.host, sizeof (n.host));
1379 else if (pkg_type == TYPE_PLUGIN)
1381 status = parse_part_string (&buffer, &buffer_size,
1382 vl.plugin, sizeof (vl.plugin));
1384 sstrncpy (n.plugin, vl.plugin,
1387 else if (pkg_type == TYPE_PLUGIN_INSTANCE)
1389 status = parse_part_string (&buffer, &buffer_size,
1391 sizeof (vl.plugin_instance));
1393 sstrncpy (n.plugin_instance,
1395 sizeof (n.plugin_instance));
1397 else if (pkg_type == TYPE_TYPE)
1399 status = parse_part_string (&buffer, &buffer_size,
1400 vl.type, sizeof (vl.type));
1402 sstrncpy (n.type, vl.type, sizeof (n.type));
1404 else if (pkg_type == TYPE_TYPE_INSTANCE)
1406 status = parse_part_string (&buffer, &buffer_size,
1408 sizeof (vl.type_instance));
1410 sstrncpy (n.type_instance, vl.type_instance,
1411 sizeof (n.type_instance));
1413 else if (pkg_type == TYPE_MESSAGE)
1415 status = parse_part_string (&buffer, &buffer_size,
1416 n.message, sizeof (n.message));
1422 else if ((n.severity != NOTIF_FAILURE)
1423 && (n.severity != NOTIF_WARNING)
1424 && (n.severity != NOTIF_OKAY))
1426 INFO ("network plugin: "
1427 "Ignoring notification with "
1428 "unknown severity %i.",
1431 else if (n.time <= 0)
1433 INFO ("network plugin: "
1434 "Ignoring notification with "
1437 else if (strlen (n.message) <= 0)
1439 INFO ("network plugin: "
1440 "Ignoring notification with "
1441 "an empty message.");
1445 plugin_dispatch_notification (&n);
1448 else if (pkg_type == TYPE_SEVERITY)
1451 status = parse_part_number (&buffer, &buffer_size,
1454 n.severity = (int) tmp;
1458 DEBUG ("network plugin: parse_packet: Unknown part"
1459 " type: 0x%04hx", pkg_type);
1460 buffer = ((char *) buffer) + pkg_length;
1462 } /* while (buffer_size > sizeof (part_header_t)) */
1464 if (status == 0 && buffer_size > 0)
1465 WARNING ("network plugin: parse_packet: Received truncated "
1466 "packet, try increasing `MaxPacketSize'");
1469 } /* }}} int parse_packet */
1471 static void free_sockent_client (struct sockent_client *sec) /* {{{ */
1480 sfree (sec->username);
1481 sfree (sec->password);
1482 if (sec->cypher != NULL)
1483 gcry_cipher_close (sec->cypher);
1485 } /* }}} void free_sockent_client */
1487 static void free_sockent_server (struct sockent_server *ses) /* {{{ */
1491 for (i = 0; i < ses->fd_num; i++)
1493 if (ses->fd[i] >= 0)
1502 sfree (ses->auth_file);
1503 fbh_destroy (ses->userdb);
1504 if (ses->cypher != NULL)
1505 gcry_cipher_close (ses->cypher);
1507 } /* }}} void free_sockent_server */
1509 static void sockent_destroy (sockent_t *se) /* {{{ */
1513 DEBUG ("network plugin: sockent_destroy (se = %p);", (void *) se);
1520 sfree (se->service);
1522 if (se->type == SOCKENT_TYPE_CLIENT)
1523 free_sockent_client (&se->data.client);
1525 free_sockent_server (&se->data.server);
1530 } /* }}} void sockent_destroy */
1533 * int network_set_ttl
1535 * Set the `IP_MULTICAST_TTL', `IP_TTL', `IPV6_MULTICAST_HOPS' or
1536 * `IPV6_UNICAST_HOPS', depending on which option is applicable.
1538 * The `struct addrinfo' is used to destinguish between unicast and multicast
1541 static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
1543 DEBUG ("network plugin: network_set_ttl: network_config_ttl = %i;",
1544 network_config_ttl);
1546 assert (se->type == SOCKENT_TYPE_CLIENT);
1548 if ((network_config_ttl < 1) || (network_config_ttl > 255))
1551 if (ai->ai_family == AF_INET)
1553 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1556 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1557 optname = IP_MULTICAST_TTL;
1561 if (setsockopt (se->data.client.fd, IPPROTO_IP, optname,
1562 &network_config_ttl,
1563 sizeof (network_config_ttl)) != 0)
1566 ERROR ("setsockopt: %s",
1567 sstrerror (errno, errbuf, sizeof (errbuf)));
1571 else if (ai->ai_family == AF_INET6)
1573 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1574 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1577 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1578 optname = IPV6_MULTICAST_HOPS;
1580 optname = IPV6_UNICAST_HOPS;
1582 if (setsockopt (se->data.client.fd, IPPROTO_IPV6, optname,
1583 &network_config_ttl,
1584 sizeof (network_config_ttl)) != 0)
1587 ERROR ("setsockopt: %s",
1588 sstrerror (errno, errbuf,
1595 } /* int network_set_ttl */
1597 static int network_set_interface (const sockent_t *se, const struct addrinfo *ai) /* {{{ */
1599 DEBUG ("network plugin: network_set_interface: interface index = %i;",
1602 assert (se->type == SOCKENT_TYPE_CLIENT);
1604 if (ai->ai_family == AF_INET)
1606 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1608 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1610 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1611 /* If possible, use the "ip_mreqn" structure which has
1612 * an "interface index" member. Using the interface
1613 * index is preferred here, because of its similarity
1614 * to the way IPv6 handles this. Unfortunately, it
1615 * appears not to be portable. */
1616 struct ip_mreqn mreq;
1618 memset (&mreq, 0, sizeof (mreq));
1619 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1620 mreq.imr_address.s_addr = ntohl (INADDR_ANY);
1621 mreq.imr_ifindex = se->interface;
1623 struct ip_mreq mreq;
1625 memset (&mreq, 0, sizeof (mreq));
1626 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1627 mreq.imr_interface.s_addr = ntohl (INADDR_ANY);
1630 if (setsockopt (se->data.client.fd, IPPROTO_IP, IP_MULTICAST_IF,
1631 &mreq, sizeof (mreq)) != 0)
1634 ERROR ("setsockopt: %s",
1635 sstrerror (errno, errbuf, sizeof (errbuf)));
1642 else if (ai->ai_family == AF_INET6)
1644 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1646 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1648 if (setsockopt (se->data.client.fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
1650 sizeof (se->interface)) != 0)
1653 ERROR ("setsockopt: %s",
1654 sstrerror (errno, errbuf,
1663 /* else: Not a multicast interface. */
1664 #if defined(HAVE_IF_INDEXTONAME) && HAVE_IF_INDEXTONAME && defined(SO_BINDTODEVICE)
1665 if (se->interface != 0)
1667 char interface_name[IFNAMSIZ];
1669 if (if_indextoname (se->interface, interface_name) == NULL)
1672 DEBUG ("network plugin: Binding socket to interface %s", interface_name);
1674 if (setsockopt (se->data.client.fd, SOL_SOCKET, SO_BINDTODEVICE,
1676 sizeof(interface_name)) == -1 )
1679 ERROR ("setsockopt: %s",
1680 sstrerror (errno, errbuf, sizeof (errbuf)));
1684 /* #endif HAVE_IF_INDEXTONAME && SO_BINDTODEVICE */
1687 WARNING ("network plugin: Cannot set the interface on a unicast "
1689 # if !defined(SO_BINDTODEVICE)
1690 "the the \"SO_BINDTODEVICE\" socket option "
1692 "the \"if_indextoname\" function "
1694 "is not available on your system.");
1698 } /* }}} network_set_interface */
1700 static int network_bind_socket (int fd, const struct addrinfo *ai, const int interface_idx)
1705 /* allow multiple sockets to use the same PORT number */
1706 if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR,
1707 &yes, sizeof(yes)) == -1) {
1709 ERROR ("setsockopt: %s",
1710 sstrerror (errno, errbuf, sizeof (errbuf)));
1714 DEBUG ("fd = %i; calling `bind'", fd);
1716 if (bind (fd, ai->ai_addr, ai->ai_addrlen) == -1)
1720 sstrerror (errno, errbuf, sizeof (errbuf)));
1724 if (ai->ai_family == AF_INET)
1726 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1727 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1729 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1730 struct ip_mreqn mreq;
1732 struct ip_mreq mreq;
1735 DEBUG ("fd = %i; IPv4 multicast address found", fd);
1737 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1738 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1739 /* Set the interface using the interface index if
1740 * possible (available). Unfortunately, the struct
1741 * ip_mreqn is not portable. */
1742 mreq.imr_address.s_addr = ntohl (INADDR_ANY);
1743 mreq.imr_ifindex = interface_idx;
1745 mreq.imr_interface.s_addr = ntohl (INADDR_ANY);
1748 if (setsockopt (fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1749 &loop, sizeof (loop)) == -1)
1752 ERROR ("setsockopt: %s",
1753 sstrerror (errno, errbuf,
1758 if (setsockopt (fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1759 &mreq, sizeof (mreq)) == -1)
1762 ERROR ("setsockopt: %s",
1763 sstrerror (errno, errbuf,
1771 else if (ai->ai_family == AF_INET6)
1773 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1774 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1775 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1777 struct ipv6_mreq mreq;
1779 DEBUG ("fd = %i; IPv6 multicast address found", fd);
1781 memcpy (&mreq.ipv6mr_multiaddr,
1783 sizeof (addr->sin6_addr));
1785 /* http://developer.apple.com/documentation/Darwin/Reference/ManPages/man4/ip6.4.html
1786 * ipv6mr_interface may be set to zeroes to
1787 * choose the default multicast interface or to
1788 * the index of a particular multicast-capable
1789 * interface if the host is multihomed.
1790 * Membership is associ-associated with a
1791 * single interface; programs running on
1792 * multihomed hosts may need to join the same
1793 * group on more than one interface.*/
1794 mreq.ipv6mr_interface = interface_idx;
1796 if (setsockopt (fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
1797 &loop, sizeof (loop)) == -1)
1800 ERROR ("setsockopt: %s",
1801 sstrerror (errno, errbuf,
1806 if (setsockopt (fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
1807 &mreq, sizeof (mreq)) == -1)
1810 ERROR ("setsockopt: %s",
1811 sstrerror (errno, errbuf,
1820 #if defined(HAVE_IF_INDEXTONAME) && HAVE_IF_INDEXTONAME && defined(SO_BINDTODEVICE)
1821 /* if a specific interface was set, bind the socket to it. But to avoid
1822 * possible problems with multicast routing, only do that for non-multicast
1824 if (interface_idx != 0)
1826 char interface_name[IFNAMSIZ];
1828 if (if_indextoname (interface_idx, interface_name) == NULL)
1831 DEBUG ("fd = %i; Binding socket to interface %s", fd, interface_name);
1833 if (setsockopt (fd, SOL_SOCKET, SO_BINDTODEVICE,
1835 sizeof(interface_name)) == -1 )
1838 ERROR ("setsockopt: %s",
1839 sstrerror (errno, errbuf, sizeof (errbuf)));
1843 #endif /* HAVE_IF_INDEXTONAME && SO_BINDTODEVICE */
1846 } /* int network_bind_socket */
1848 /* Initialize a sockent structure. `type' must be either `SOCKENT_TYPE_CLIENT'
1849 * or `SOCKENT_TYPE_SERVER' */
1850 static int sockent_init (sockent_t *se, int type) /* {{{ */
1855 memset (se, 0, sizeof (*se));
1857 se->type = SOCKENT_TYPE_CLIENT;
1863 if (type == SOCKENT_TYPE_SERVER)
1865 se->type = SOCKENT_TYPE_SERVER;
1866 se->data.server.fd = NULL;
1868 se->data.server.security_level = SECURITY_LEVEL_NONE;
1869 se->data.server.auth_file = NULL;
1870 se->data.server.userdb = NULL;
1871 se->data.server.cypher = NULL;
1876 se->data.client.fd = -1;
1877 se->data.client.addr = NULL;
1879 se->data.client.security_level = SECURITY_LEVEL_NONE;
1880 se->data.client.username = NULL;
1881 se->data.client.password = NULL;
1882 se->data.client.cypher = NULL;
1887 } /* }}} int sockent_init */
1889 /* Open the file descriptors for a initialized sockent structure. */
1890 static int sockent_open (sockent_t *se) /* {{{ */
1892 struct addrinfo ai_hints;
1893 struct addrinfo *ai_list, *ai_ptr;
1897 const char *service;
1902 /* Set up the security structures. */
1903 #if HAVE_LIBGCRYPT /* {{{ */
1904 if (se->type == SOCKENT_TYPE_CLIENT)
1906 if (se->data.client.security_level > SECURITY_LEVEL_NONE)
1908 if ((se->data.client.username == NULL)
1909 || (se->data.client.password == NULL))
1911 ERROR ("network plugin: Client socket with "
1912 "security requested, but no "
1913 "credentials are configured.");
1916 gcry_md_hash_buffer (GCRY_MD_SHA256,
1917 se->data.client.password_hash,
1918 se->data.client.password,
1919 strlen (se->data.client.password));
1922 else /* (se->type == SOCKENT_TYPE_SERVER) */
1924 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
1926 if (se->data.server.auth_file == NULL)
1928 ERROR ("network plugin: Server socket with "
1929 "security requested, but no "
1930 "password file is configured.");
1934 if (se->data.server.auth_file != NULL)
1936 se->data.server.userdb = fbh_create (se->data.server.auth_file);
1937 if (se->data.server.userdb == NULL)
1939 ERROR ("network plugin: Reading password file "
1941 se->data.server.auth_file);
1942 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
1947 #endif /* }}} HAVE_LIBGCRYPT */
1950 service = se->service;
1952 if (service == NULL)
1953 service = NET_DEFAULT_PORT;
1955 DEBUG ("network plugin: sockent_open: node = %s; service = %s;",
1958 memset (&ai_hints, 0, sizeof (ai_hints));
1959 ai_hints.ai_flags = 0;
1961 ai_hints.ai_flags |= AI_PASSIVE;
1963 #ifdef AI_ADDRCONFIG
1964 ai_hints.ai_flags |= AI_ADDRCONFIG;
1966 ai_hints.ai_family = AF_UNSPEC;
1967 ai_hints.ai_socktype = SOCK_DGRAM;
1968 ai_hints.ai_protocol = IPPROTO_UDP;
1970 ai_return = getaddrinfo (node, service, &ai_hints, &ai_list);
1973 ERROR ("network plugin: getaddrinfo (%s, %s) failed: %s",
1974 (se->node == NULL) ? "(null)" : se->node,
1975 (se->service == NULL) ? "(null)" : se->service,
1976 gai_strerror (ai_return));
1980 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1984 if (se->type == SOCKENT_TYPE_SERVER) /* {{{ */
1988 tmp = realloc (se->data.server.fd,
1989 sizeof (*tmp) * (se->data.server.fd_num + 1));
1992 ERROR ("network plugin: realloc failed.");
1995 se->data.server.fd = tmp;
1996 tmp = se->data.server.fd + se->data.server.fd_num;
1998 *tmp = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
1999 ai_ptr->ai_protocol);
2003 ERROR ("network plugin: socket(2) failed: %s",
2004 sstrerror (errno, errbuf,
2009 status = network_bind_socket (*tmp, ai_ptr, se->interface);
2017 se->data.server.fd_num++;
2019 } /* }}} if (se->type == SOCKENT_TYPE_SERVER) */
2020 else /* if (se->type == SOCKENT_TYPE_CLIENT) {{{ */
2022 se->data.client.fd = socket (ai_ptr->ai_family,
2023 ai_ptr->ai_socktype,
2024 ai_ptr->ai_protocol);
2025 if (se->data.client.fd < 0)
2028 ERROR ("network plugin: socket(2) failed: %s",
2029 sstrerror (errno, errbuf,
2034 se->data.client.addr = malloc (sizeof (*se->data.client.addr));
2035 if (se->data.client.addr == NULL)
2037 ERROR ("network plugin: malloc failed.");
2038 close (se->data.client.fd);
2039 se->data.client.fd = -1;
2043 memset (se->data.client.addr, 0, sizeof (*se->data.client.addr));
2044 assert (sizeof (*se->data.client.addr) >= ai_ptr->ai_addrlen);
2045 memcpy (se->data.client.addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
2046 se->data.client.addrlen = ai_ptr->ai_addrlen;
2048 network_set_ttl (se, ai_ptr);
2049 network_set_interface (se, ai_ptr);
2051 /* We don't open more than one write-socket per
2052 * node/service pair.. */
2054 } /* }}} if (se->type == SOCKENT_TYPE_CLIENT) */
2055 } /* for (ai_list) */
2057 freeaddrinfo (ai_list);
2059 /* Check if all went well. */
2060 if (se->type == SOCKENT_TYPE_SERVER)
2062 if (se->data.server.fd_num <= 0)
2065 else /* if (se->type == SOCKENT_TYPE_CLIENT) */
2067 if (se->data.client.fd < 0)
2072 } /* }}} int sockent_open */
2074 /* Add a sockent to the global list of sockets */
2075 static int sockent_add (sockent_t *se) /* {{{ */
2077 sockent_t *last_ptr;
2082 if (se->type == SOCKENT_TYPE_SERVER)
2087 tmp = realloc (listen_sockets_pollfd,
2088 sizeof (*tmp) * (listen_sockets_num
2089 + se->data.server.fd_num));
2092 ERROR ("network plugin: realloc failed.");
2095 listen_sockets_pollfd = tmp;
2096 tmp = listen_sockets_pollfd + listen_sockets_num;
2098 for (i = 0; i < se->data.server.fd_num; i++)
2100 memset (tmp + i, 0, sizeof (*tmp));
2101 tmp[i].fd = se->data.server.fd[i];
2102 tmp[i].events = POLLIN | POLLPRI;
2106 listen_sockets_num += se->data.server.fd_num;
2108 if (listen_sockets == NULL)
2110 listen_sockets = se;
2113 last_ptr = listen_sockets;
2115 else /* if (se->type == SOCKENT_TYPE_CLIENT) */
2117 if (sending_sockets == NULL)
2119 sending_sockets = se;
2122 last_ptr = sending_sockets;
2125 while (last_ptr->next != NULL)
2126 last_ptr = last_ptr->next;
2127 last_ptr->next = se;
2130 } /* }}} int sockent_add */
2132 static void *dispatch_thread (void __attribute__((unused)) *arg) /* {{{ */
2136 receive_list_entry_t *ent;
2139 /* Lock and wait for more data to come in */
2140 pthread_mutex_lock (&receive_list_lock);
2141 while ((listen_loop == 0)
2142 && (receive_list_head == NULL))
2143 pthread_cond_wait (&receive_list_cond, &receive_list_lock);
2145 /* Remove the head entry and unlock */
2146 ent = receive_list_head;
2148 receive_list_head = ent->next;
2149 receive_list_length--;
2150 pthread_mutex_unlock (&receive_list_lock);
2152 /* Check whether we are supposed to exit. We do NOT check `listen_loop'
2153 * because we dispatch all missing packets before shutting down. */
2157 /* Look for the correct `sockent_t' */
2158 se = listen_sockets;
2163 for (i = 0; i < se->data.server.fd_num; i++)
2164 if (se->data.server.fd[i] == ent->fd)
2167 if (i < se->data.server.fd_num)
2175 ERROR ("network plugin: Got packet from FD %i, but can't "
2176 "find an appropriate socket entry.",
2183 parse_packet (se, ent->data, ent->data_len, /* flags = */ 0);
2189 } /* }}} void *dispatch_thread */
2191 static int network_receive (void) /* {{{ */
2193 char buffer[network_config_packet_size];
2199 receive_list_entry_t *private_list_head;
2200 receive_list_entry_t *private_list_tail;
2201 uint64_t private_list_length;
2203 assert (listen_sockets_num > 0);
2205 private_list_head = NULL;
2206 private_list_tail = NULL;
2207 private_list_length = 0;
2209 while (listen_loop == 0)
2211 status = poll (listen_sockets_pollfd, listen_sockets_num, -1);
2218 ERROR ("poll failed: %s",
2219 sstrerror (errno, errbuf, sizeof (errbuf)));
2223 for (i = 0; (i < listen_sockets_num) && (status > 0); i++)
2225 receive_list_entry_t *ent;
2227 if ((listen_sockets_pollfd[i].revents
2228 & (POLLIN | POLLPRI)) == 0)
2232 buffer_len = recv (listen_sockets_pollfd[i].fd,
2233 buffer, sizeof (buffer),
2238 ERROR ("recv failed: %s",
2239 sstrerror (errno, errbuf,
2244 stats_octets_rx += ((uint64_t) buffer_len);
2247 /* TODO: Possible performance enhancement: Do not free
2248 * these entries in the dispatch thread but put them in
2249 * another list, so we don't have to allocate more and
2250 * more of these structures. */
2251 ent = malloc (sizeof (receive_list_entry_t));
2254 ERROR ("network plugin: malloc failed.");
2257 memset (ent, 0, sizeof (receive_list_entry_t));
2258 ent->data = malloc (network_config_packet_size);
2259 if (ent->data == NULL)
2262 ERROR ("network plugin: malloc failed.");
2265 ent->fd = listen_sockets_pollfd[i].fd;
2268 memcpy (ent->data, buffer, buffer_len);
2269 ent->data_len = buffer_len;
2271 if (private_list_head == NULL)
2272 private_list_head = ent;
2274 private_list_tail->next = ent;
2275 private_list_tail = ent;
2276 private_list_length++;
2278 /* Do not block here. Blocking here has led to
2279 * insufficient performance in the past. */
2280 if (pthread_mutex_trylock (&receive_list_lock) == 0)
2282 assert (((receive_list_head == NULL) && (receive_list_length == 0))
2283 || ((receive_list_head != NULL) && (receive_list_length != 0)));
2285 if (receive_list_head == NULL)
2286 receive_list_head = private_list_head;
2288 receive_list_tail->next = private_list_head;
2289 receive_list_tail = private_list_tail;
2290 receive_list_length += private_list_length;
2292 pthread_cond_signal (&receive_list_cond);
2293 pthread_mutex_unlock (&receive_list_lock);
2295 private_list_head = NULL;
2296 private_list_tail = NULL;
2297 private_list_length = 0;
2299 } /* for (listen_sockets_pollfd) */
2300 } /* while (listen_loop == 0) */
2302 /* Make sure everything is dispatched before exiting. */
2303 if (private_list_head != NULL)
2305 pthread_mutex_lock (&receive_list_lock);
2307 if (receive_list_head == NULL)
2308 receive_list_head = private_list_head;
2310 receive_list_tail->next = private_list_head;
2311 receive_list_tail = private_list_tail;
2312 receive_list_length += private_list_length;
2314 private_list_head = NULL;
2315 private_list_tail = NULL;
2316 private_list_length = 0;
2318 pthread_cond_signal (&receive_list_cond);
2319 pthread_mutex_unlock (&receive_list_lock);
2323 } /* }}} int network_receive */
2325 static void *receive_thread (void __attribute__((unused)) *arg)
2327 return (network_receive () ? (void *) 1 : (void *) 0);
2328 } /* void *receive_thread */
2330 static void network_init_buffer (void)
2332 memset (send_buffer, 0, network_config_packet_size);
2333 send_buffer_ptr = send_buffer;
2334 send_buffer_fill = 0;
2336 memset (&send_buffer_vl, 0, sizeof (send_buffer_vl));
2337 } /* int network_init_buffer */
2339 static void networt_send_buffer_plain (const sockent_t *se, /* {{{ */
2340 const char *buffer, size_t buffer_size)
2346 status = sendto (se->data.client.fd, buffer, buffer_size,
2348 (struct sockaddr *) se->data.client.addr,
2349 se->data.client.addrlen);
2355 ERROR ("network plugin: sendto failed: %s",
2356 sstrerror (errno, errbuf,
2363 } /* }}} void networt_send_buffer_plain */
2366 #define BUFFER_ADD(p,s) do { \
2367 memcpy (buffer + buffer_offset, (p), (s)); \
2368 buffer_offset += (s); \
2371 static void networt_send_buffer_signed (const sockent_t *se, /* {{{ */
2372 const char *in_buffer, size_t in_buffer_size)
2374 part_signature_sha256_t ps;
2375 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2376 size_t buffer_offset;
2377 size_t username_len;
2381 unsigned char *hash;
2384 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
2387 ERROR ("network plugin: Creating HMAC object failed: %s",
2388 gcry_strerror (err));
2392 err = gcry_md_setkey (hd, se->data.client.password,
2393 strlen (se->data.client.password));
2396 ERROR ("network plugin: gcry_md_setkey failed: %s",
2397 gcry_strerror (err));
2402 username_len = strlen (se->data.client.username);
2403 if (username_len > (BUFF_SIG_SIZE - PART_SIGNATURE_SHA256_SIZE))
2405 ERROR ("network plugin: Username too long: %s",
2406 se->data.client.username);
2410 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE,
2411 se->data.client.username, username_len);
2412 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE + username_len,
2413 in_buffer, in_buffer_size);
2415 /* Initialize the `ps' structure. */
2416 memset (&ps, 0, sizeof (ps));
2417 ps.head.type = htons (TYPE_SIGN_SHA256);
2418 ps.head.length = htons (PART_SIGNATURE_SHA256_SIZE + username_len);
2420 /* Calculate the hash value. */
2421 gcry_md_write (hd, buffer + PART_SIGNATURE_SHA256_SIZE,
2422 username_len + in_buffer_size);
2423 hash = gcry_md_read (hd, GCRY_MD_SHA256);
2426 ERROR ("network plugin: gcry_md_read failed.");
2430 memcpy (ps.hash, hash, sizeof (ps.hash));
2432 /* Add the header */
2435 BUFFER_ADD (&ps.head.type, sizeof (ps.head.type));
2436 BUFFER_ADD (&ps.head.length, sizeof (ps.head.length));
2437 BUFFER_ADD (ps.hash, sizeof (ps.hash));
2439 assert (buffer_offset == PART_SIGNATURE_SHA256_SIZE);
2444 buffer_offset = PART_SIGNATURE_SHA256_SIZE + username_len + in_buffer_size;
2445 networt_send_buffer_plain (se, buffer, buffer_offset);
2446 } /* }}} void networt_send_buffer_signed */
2448 static void networt_send_buffer_encrypted (sockent_t *se, /* {{{ */
2449 const char *in_buffer, size_t in_buffer_size)
2451 part_encryption_aes256_t pea;
2452 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2454 size_t buffer_offset;
2456 size_t username_len;
2458 gcry_cipher_hd_t cypher;
2460 /* Initialize the header fields */
2461 memset (&pea, 0, sizeof (pea));
2462 pea.head.type = htons (TYPE_ENCR_AES256);
2464 pea.username = se->data.client.username;
2466 username_len = strlen (pea.username);
2467 if ((PART_ENCRYPTION_AES256_SIZE + username_len) > BUFF_SIG_SIZE)
2469 ERROR ("network plugin: Username too long: %s", pea.username);
2473 buffer_size = PART_ENCRYPTION_AES256_SIZE + username_len + in_buffer_size;
2474 header_size = PART_ENCRYPTION_AES256_SIZE + username_len
2475 - sizeof (pea.hash);
2477 assert (buffer_size <= sizeof (buffer));
2478 DEBUG ("network plugin: networt_send_buffer_encrypted: "
2479 "buffer_size = %zu;", buffer_size);
2481 pea.head.length = htons ((uint16_t) (PART_ENCRYPTION_AES256_SIZE
2482 + username_len + in_buffer_size));
2483 pea.username_length = htons ((uint16_t) username_len);
2485 /* Chose a random initialization vector. */
2486 gcry_randomize ((void *) &pea.iv, sizeof (pea.iv), GCRY_STRONG_RANDOM);
2488 /* Create hash of the payload */
2489 gcry_md_hash_buffer (GCRY_MD_SHA1, pea.hash, in_buffer, in_buffer_size);
2491 /* Initialize the buffer */
2493 memset (buffer, 0, sizeof (buffer));
2496 BUFFER_ADD (&pea.head.type, sizeof (pea.head.type));
2497 BUFFER_ADD (&pea.head.length, sizeof (pea.head.length));
2498 BUFFER_ADD (&pea.username_length, sizeof (pea.username_length));
2499 BUFFER_ADD (pea.username, username_len);
2500 BUFFER_ADD (pea.iv, sizeof (pea.iv));
2501 assert (buffer_offset == header_size);
2502 BUFFER_ADD (pea.hash, sizeof (pea.hash));
2503 BUFFER_ADD (in_buffer, in_buffer_size);
2505 assert (buffer_offset == buffer_size);
2507 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
2508 se->data.client.password);
2512 /* Encrypt the buffer in-place */
2513 err = gcry_cipher_encrypt (cypher,
2514 buffer + header_size,
2515 buffer_size - header_size,
2516 /* in = */ NULL, /* in len = */ 0);
2519 ERROR ("network plugin: gcry_cipher_encrypt returned: %s",
2520 gcry_strerror (err));
2524 /* Send it out without further modifications */
2525 networt_send_buffer_plain (se, buffer, buffer_size);
2526 } /* }}} void networt_send_buffer_encrypted */
2528 #endif /* HAVE_LIBGCRYPT */
2530 static void network_send_buffer (char *buffer, size_t buffer_len) /* {{{ */
2534 DEBUG ("network plugin: network_send_buffer: buffer_len = %zu", buffer_len);
2536 for (se = sending_sockets; se != NULL; se = se->next)
2539 if (se->data.client.security_level == SECURITY_LEVEL_ENCRYPT)
2540 networt_send_buffer_encrypted (se, buffer, buffer_len);
2541 else if (se->data.client.security_level == SECURITY_LEVEL_SIGN)
2542 networt_send_buffer_signed (se, buffer, buffer_len);
2543 else /* if (se->data.client.security_level == SECURITY_LEVEL_NONE) */
2544 #endif /* HAVE_LIBGCRYPT */
2545 networt_send_buffer_plain (se, buffer, buffer_len);
2546 } /* for (sending_sockets) */
2547 } /* }}} void network_send_buffer */
2549 static int add_to_buffer (char *buffer, int buffer_size, /* {{{ */
2550 value_list_t *vl_def,
2551 const data_set_t *ds, const value_list_t *vl)
2553 char *buffer_orig = buffer;
2555 if (strcmp (vl_def->host, vl->host) != 0)
2557 if (write_part_string (&buffer, &buffer_size, TYPE_HOST,
2558 vl->host, strlen (vl->host)) != 0)
2560 sstrncpy (vl_def->host, vl->host, sizeof (vl_def->host));
2563 if (vl_def->time != vl->time)
2565 if (write_part_number (&buffer, &buffer_size, TYPE_TIME,
2566 (uint64_t) vl->time))
2568 vl_def->time = vl->time;
2571 if (vl_def->interval != vl->interval)
2573 if (write_part_number (&buffer, &buffer_size, TYPE_INTERVAL,
2574 (uint64_t) vl->interval))
2576 vl_def->interval = vl->interval;
2579 if (strcmp (vl_def->plugin, vl->plugin) != 0)
2581 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN,
2582 vl->plugin, strlen (vl->plugin)) != 0)
2584 sstrncpy (vl_def->plugin, vl->plugin, sizeof (vl_def->plugin));
2587 if (strcmp (vl_def->plugin_instance, vl->plugin_instance) != 0)
2589 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
2590 vl->plugin_instance,
2591 strlen (vl->plugin_instance)) != 0)
2593 sstrncpy (vl_def->plugin_instance, vl->plugin_instance, sizeof (vl_def->plugin_instance));
2596 if (strcmp (vl_def->type, vl->type) != 0)
2598 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
2599 vl->type, strlen (vl->type)) != 0)
2601 sstrncpy (vl_def->type, ds->type, sizeof (vl_def->type));
2604 if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
2606 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE_INSTANCE,
2608 strlen (vl->type_instance)) != 0)
2610 sstrncpy (vl_def->type_instance, vl->type_instance, sizeof (vl_def->type_instance));
2613 if (write_part_values (&buffer, &buffer_size, ds, vl) != 0)
2616 return (buffer - buffer_orig);
2617 } /* }}} int add_to_buffer */
2619 static void flush_buffer (void)
2621 DEBUG ("network plugin: flush_buffer: send_buffer_fill = %i",
2624 network_send_buffer (send_buffer, (size_t) send_buffer_fill);
2626 stats_octets_tx += ((uint64_t) send_buffer_fill);
2629 network_init_buffer ();
2632 static int network_write (const data_set_t *ds, const value_list_t *vl,
2633 user_data_t __attribute__((unused)) *user_data)
2637 if (!check_send_okay (vl))
2640 char name[6*DATA_MAX_NAME_LEN];
2641 FORMAT_VL (name, sizeof (name), vl);
2642 name[sizeof (name) - 1] = 0;
2643 DEBUG ("network plugin: network_write: "
2644 "NOT sending %s.", name);
2646 /* Counter is not protected by another lock and may be reached by
2647 * multiple threads */
2648 pthread_mutex_lock (&stats_lock);
2649 stats_values_not_sent++;
2650 pthread_mutex_unlock (&stats_lock);
2654 uc_meta_data_add_unsigned_int (vl,
2655 "network:time_sent", (uint64_t) vl->time);
2657 pthread_mutex_lock (&send_buffer_lock);
2659 status = add_to_buffer (send_buffer_ptr,
2660 network_config_packet_size - (send_buffer_fill + BUFF_SIG_SIZE),
2665 /* status == bytes added to the buffer */
2666 send_buffer_fill += status;
2667 send_buffer_ptr += status;
2669 stats_values_sent++;
2675 status = add_to_buffer (send_buffer_ptr,
2676 network_config_packet_size - (send_buffer_fill + BUFF_SIG_SIZE),
2682 send_buffer_fill += status;
2683 send_buffer_ptr += status;
2685 stats_values_sent++;
2691 ERROR ("network plugin: Unable to append to the "
2692 "buffer for some weird reason");
2694 else if ((network_config_packet_size - send_buffer_fill) < 15)
2699 pthread_mutex_unlock (&send_buffer_lock);
2701 return ((status < 0) ? -1 : 0);
2702 } /* int network_write */
2704 static int network_config_set_boolean (const oconfig_item_t *ci, /* {{{ */
2707 if ((ci->values_num != 1)
2708 || ((ci->values[0].type != OCONFIG_TYPE_BOOLEAN)
2709 && (ci->values[0].type != OCONFIG_TYPE_STRING)))
2711 ERROR ("network plugin: The `%s' config option needs "
2712 "exactly one boolean argument.", ci->key);
2716 if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
2718 if (ci->values[0].value.boolean)
2725 char *str = ci->values[0].value.string;
2729 else if (IS_FALSE (str))
2733 ERROR ("network plugin: Cannot parse string value `%s' of the `%s' "
2734 "option as boolean value.",
2741 } /* }}} int network_config_set_boolean */
2743 static int network_config_set_ttl (const oconfig_item_t *ci) /* {{{ */
2746 if ((ci->values_num != 1)
2747 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
2749 WARNING ("network plugin: The `TimeToLive' config option needs exactly "
2750 "one numeric argument.");
2754 tmp = (int) ci->values[0].value.number;
2755 if ((tmp > 0) && (tmp <= 255))
2756 network_config_ttl = tmp;
2759 } /* }}} int network_config_set_ttl */
2761 static int network_config_set_interface (const oconfig_item_t *ci, /* {{{ */
2764 if ((ci->values_num != 1)
2765 || (ci->values[0].type != OCONFIG_TYPE_STRING))
2767 WARNING ("network plugin: The `Interface' config option needs exactly "
2768 "one string argument.");
2772 if (interface == NULL)
2775 *interface = if_nametoindex (ci->values[0].value.string);
2778 } /* }}} int network_config_set_interface */
2780 static int network_config_set_buffer_size (const oconfig_item_t *ci) /* {{{ */
2783 if ((ci->values_num != 1)
2784 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
2786 WARNING ("network plugin: The `MaxPacketSize' config option needs exactly "
2787 "one numeric argument.");
2791 tmp = (int) ci->values[0].value.number;
2792 if ((tmp >= 1024) && (tmp <= 65535))
2793 network_config_packet_size = tmp;
2796 } /* }}} int network_config_set_buffer_size */
2799 static int network_config_set_string (const oconfig_item_t *ci, /* {{{ */
2803 if ((ci->values_num != 1)
2804 || (ci->values[0].type != OCONFIG_TYPE_STRING))
2806 WARNING ("network plugin: The `%s' config option needs exactly "
2807 "one string argument.", ci->key);
2811 tmp = strdup (ci->values[0].value.string);
2815 sfree (*ret_string);
2819 } /* }}} int network_config_set_string */
2820 #endif /* HAVE_LIBGCRYPT */
2823 static int network_config_set_security_level (oconfig_item_t *ci, /* {{{ */
2827 if ((ci->values_num != 1)
2828 || (ci->values[0].type != OCONFIG_TYPE_STRING))
2830 WARNING ("network plugin: The `SecurityLevel' config option needs exactly "
2831 "one string argument.");
2835 str = ci->values[0].value.string;
2836 if (strcasecmp ("Encrypt", str) == 0)
2837 *retval = SECURITY_LEVEL_ENCRYPT;
2838 else if (strcasecmp ("Sign", str) == 0)
2839 *retval = SECURITY_LEVEL_SIGN;
2840 else if (strcasecmp ("None", str) == 0)
2841 *retval = SECURITY_LEVEL_NONE;
2844 WARNING ("network plugin: Unknown security level: %s.", str);
2849 } /* }}} int network_config_set_security_level */
2850 #endif /* HAVE_LIBGCRYPT */
2852 static int network_config_add_listen (const oconfig_item_t *ci) /* {{{ */
2858 if ((ci->values_num < 1) || (ci->values_num > 2)
2859 || (ci->values[0].type != OCONFIG_TYPE_STRING)
2860 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
2862 ERROR ("network plugin: The `%s' config option needs "
2863 "one or two string arguments.", ci->key);
2867 se = malloc (sizeof (*se));
2870 ERROR ("network plugin: malloc failed.");
2873 sockent_init (se, SOCKENT_TYPE_SERVER);
2875 se->node = strdup (ci->values[0].value.string);
2876 if (ci->values_num >= 2)
2877 se->service = strdup (ci->values[1].value.string);
2879 for (i = 0; i < ci->children_num; i++)
2881 oconfig_item_t *child = ci->children + i;
2884 if (strcasecmp ("AuthFile", child->key) == 0)
2885 network_config_set_string (child, &se->data.server.auth_file);
2886 else if (strcasecmp ("SecurityLevel", child->key) == 0)
2887 network_config_set_security_level (child,
2888 &se->data.server.security_level);
2890 #endif /* HAVE_LIBGCRYPT */
2891 if (strcasecmp ("Interface", child->key) == 0)
2892 network_config_set_interface (child,
2896 WARNING ("network plugin: Option `%s' is not allowed here.",
2902 if ((se->data.server.security_level > SECURITY_LEVEL_NONE)
2903 && (se->data.server.auth_file == NULL))
2905 ERROR ("network plugin: A security level higher than `none' was "
2906 "requested, but no AuthFile option was given. Cowardly refusing to "
2907 "open this socket!");
2908 sockent_destroy (se);
2911 #endif /* HAVE_LIBGCRYPT */
2913 status = sockent_open (se);
2916 ERROR ("network plugin: network_config_add_listen: sockent_open failed.");
2917 sockent_destroy (se);
2921 status = sockent_add (se);
2924 ERROR ("network plugin: network_config_add_listen: sockent_add failed.");
2925 sockent_destroy (se);
2930 } /* }}} int network_config_add_listen */
2932 static int network_config_add_server (const oconfig_item_t *ci) /* {{{ */
2938 if ((ci->values_num < 1) || (ci->values_num > 2)
2939 || (ci->values[0].type != OCONFIG_TYPE_STRING)
2940 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
2942 ERROR ("network plugin: The `%s' config option needs "
2943 "one or two string arguments.", ci->key);
2947 se = malloc (sizeof (*se));
2950 ERROR ("network plugin: malloc failed.");
2953 sockent_init (se, SOCKENT_TYPE_CLIENT);
2955 se->node = strdup (ci->values[0].value.string);
2956 if (ci->values_num >= 2)
2957 se->service = strdup (ci->values[1].value.string);
2959 for (i = 0; i < ci->children_num; i++)
2961 oconfig_item_t *child = ci->children + i;
2964 if (strcasecmp ("Username", child->key) == 0)
2965 network_config_set_string (child, &se->data.client.username);
2966 else if (strcasecmp ("Password", child->key) == 0)
2967 network_config_set_string (child, &se->data.client.password);
2968 else if (strcasecmp ("SecurityLevel", child->key) == 0)
2969 network_config_set_security_level (child,
2970 &se->data.client.security_level);
2972 #endif /* HAVE_LIBGCRYPT */
2973 if (strcasecmp ("Interface", child->key) == 0)
2974 network_config_set_interface (child,
2978 WARNING ("network plugin: Option `%s' is not allowed here.",
2984 if ((se->data.client.security_level > SECURITY_LEVEL_NONE)
2985 && ((se->data.client.username == NULL)
2986 || (se->data.client.password == NULL)))
2988 ERROR ("network plugin: A security level higher than `none' was "
2989 "requested, but no Username or Password option was given. "
2990 "Cowardly refusing to open this socket!");
2991 sockent_destroy (se);
2994 #endif /* HAVE_LIBGCRYPT */
2996 status = sockent_open (se);
2999 ERROR ("network plugin: network_config_add_server: sockent_open failed.");
3000 sockent_destroy (se);
3004 status = sockent_add (se);
3007 ERROR ("network plugin: network_config_add_server: sockent_add failed.");
3008 sockent_destroy (se);
3013 } /* }}} int network_config_add_server */
3015 static int network_config (oconfig_item_t *ci) /* {{{ */
3019 for (i = 0; i < ci->children_num; i++)
3021 oconfig_item_t *child = ci->children + i;
3023 if (strcasecmp ("Listen", child->key) == 0)
3024 network_config_add_listen (child);
3025 else if (strcasecmp ("Server", child->key) == 0)
3026 network_config_add_server (child);
3027 else if (strcasecmp ("TimeToLive", child->key) == 0)
3028 network_config_set_ttl (child);
3029 else if (strcasecmp ("MaxPacketSize", child->key) == 0)
3030 network_config_set_buffer_size (child);
3031 else if (strcasecmp ("Forward", child->key) == 0)
3032 network_config_set_boolean (child, &network_config_forward);
3033 else if (strcasecmp ("ReportStats", child->key) == 0)
3034 network_config_set_boolean (child, &network_config_stats);
3035 else if (strcasecmp ("CacheFlush", child->key) == 0)
3036 /* no op for backwards compatibility only */;
3039 WARNING ("network plugin: Option `%s' is not allowed here.",
3045 } /* }}} int network_config */
3047 static int network_notification (const notification_t *n,
3048 user_data_t __attribute__((unused)) *user_data)
3050 char buffer[network_config_packet_size];
3051 char *buffer_ptr = buffer;
3052 int buffer_free = sizeof (buffer);
3055 memset (buffer, '\0', sizeof (buffer));
3058 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_TIME,
3059 (uint64_t) n->time);
3063 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_SEVERITY,
3064 (uint64_t) n->severity);
3068 if (strlen (n->host) > 0)
3070 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_HOST,
3071 n->host, strlen (n->host));
3076 if (strlen (n->plugin) > 0)
3078 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_PLUGIN,
3079 n->plugin, strlen (n->plugin));
3084 if (strlen (n->plugin_instance) > 0)
3086 status = write_part_string (&buffer_ptr, &buffer_free,
3087 TYPE_PLUGIN_INSTANCE,
3088 n->plugin_instance, strlen (n->plugin_instance));
3093 if (strlen (n->type) > 0)
3095 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE,
3096 n->type, strlen (n->type));
3101 if (strlen (n->type_instance) > 0)
3103 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE_INSTANCE,
3104 n->type_instance, strlen (n->type_instance));
3109 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_MESSAGE,
3110 n->message, strlen (n->message));
3114 network_send_buffer (buffer, sizeof (buffer) - buffer_free);
3117 } /* int network_notification */
3119 static int network_shutdown (void)
3123 /* Kill the listening thread */
3124 if (receive_thread_running != 0)
3126 INFO ("network plugin: Stopping receive thread.");
3127 pthread_kill (receive_thread_id, SIGTERM);
3128 pthread_join (receive_thread_id, NULL /* no return value */);
3129 memset (&receive_thread_id, 0, sizeof (receive_thread_id));
3130 receive_thread_running = 0;
3133 /* Shutdown the dispatching thread */
3134 if (dispatch_thread_running != 0)
3136 INFO ("network plugin: Stopping dispatch thread.");
3137 pthread_mutex_lock (&receive_list_lock);
3138 pthread_cond_broadcast (&receive_list_cond);
3139 pthread_mutex_unlock (&receive_list_lock);
3140 pthread_join (dispatch_thread_id, /* ret = */ NULL);
3141 dispatch_thread_running = 0;
3144 sockent_destroy (listen_sockets);
3146 if (send_buffer_fill > 0)
3149 sfree (send_buffer);
3151 /* TODO: Close `sending_sockets' */
3153 plugin_unregister_config ("network");
3154 plugin_unregister_init ("network");
3155 plugin_unregister_write ("network");
3156 plugin_unregister_shutdown ("network");
3159 } /* int network_shutdown */
3161 static int network_stats_read (void) /* {{{ */
3163 uint64_t copy_octets_rx;
3164 uint64_t copy_octets_tx;
3165 uint64_t copy_packets_rx;
3166 uint64_t copy_packets_tx;
3167 uint64_t copy_values_dispatched;
3168 uint64_t copy_values_not_dispatched;
3169 uint64_t copy_values_sent;
3170 uint64_t copy_values_not_sent;
3171 uint64_t copy_receive_list_length;
3172 value_list_t vl = VALUE_LIST_INIT;
3175 copy_octets_rx = stats_octets_rx;
3176 copy_octets_tx = stats_octets_tx;
3177 copy_packets_rx = stats_packets_rx;
3178 copy_packets_tx = stats_packets_tx;
3179 copy_values_dispatched = stats_values_dispatched;
3180 copy_values_not_dispatched = stats_values_not_dispatched;
3181 copy_values_sent = stats_values_sent;
3182 copy_values_not_sent = stats_values_not_sent;
3183 copy_receive_list_length = receive_list_length;
3185 /* Initialize `vl' */
3189 vl.interval = interval_g;
3190 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
3191 sstrncpy (vl.plugin, "network", sizeof (vl.plugin));
3193 /* Octets received / sent */
3194 vl.values[0].counter = (counter_t) copy_octets_rx;
3195 vl.values[1].counter = (counter_t) copy_octets_tx;
3196 sstrncpy (vl.type, "if_octets", sizeof (vl.type));
3197 plugin_dispatch_values (&vl);
3199 /* Packets received / send */
3200 vl.values[0].counter = (counter_t) copy_packets_rx;
3201 vl.values[1].counter = (counter_t) copy_packets_tx;
3202 sstrncpy (vl.type, "if_packets", sizeof (vl.type));
3203 plugin_dispatch_values (&vl);
3205 /* Values (not) dispatched and (not) send */
3206 sstrncpy (vl.type, "total_values", sizeof (vl.type));
3209 vl.values[0].derive = (derive_t) copy_values_dispatched;
3210 sstrncpy (vl.type_instance, "dispatch-accepted",
3211 sizeof (vl.type_instance));
3212 plugin_dispatch_values (&vl);
3214 vl.values[0].derive = (derive_t) copy_values_not_dispatched;
3215 sstrncpy (vl.type_instance, "dispatch-rejected",
3216 sizeof (vl.type_instance));
3217 plugin_dispatch_values (&vl);
3219 vl.values[0].derive = (derive_t) copy_values_sent;
3220 sstrncpy (vl.type_instance, "send-accepted",
3221 sizeof (vl.type_instance));
3222 plugin_dispatch_values (&vl);
3224 vl.values[0].derive = (derive_t) copy_values_not_sent;
3225 sstrncpy (vl.type_instance, "send-rejected",
3226 sizeof (vl.type_instance));
3227 plugin_dispatch_values (&vl);
3229 /* Receive queue length */
3230 vl.values[0].gauge = (gauge_t) copy_receive_list_length;
3231 sstrncpy (vl.type, "queue_length", sizeof (vl.type));
3232 vl.type_instance[0] = 0;
3233 plugin_dispatch_values (&vl);
3236 } /* }}} int network_stats_read */
3238 static int network_init (void)
3240 static _Bool have_init = false;
3242 /* Check if we were already initialized. If so, just return - there's
3243 * nothing more to do (for now, that is). */
3249 gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
3250 gcry_control (GCRYCTL_INIT_SECMEM, 32768, 0);
3251 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
3254 if (network_config_stats != 0)
3255 plugin_register_read ("network", network_stats_read);
3257 plugin_register_shutdown ("network", network_shutdown);
3259 send_buffer = malloc (network_config_packet_size);
3260 if (send_buffer == NULL)
3262 ERROR ("network plugin: malloc failed.");
3265 network_init_buffer ();
3267 /* setup socket(s) and so on */
3268 if (sending_sockets != NULL)
3270 plugin_register_write ("network", network_write,
3271 /* user_data = */ NULL);
3272 plugin_register_notification ("network", network_notification,
3273 /* user_data = */ NULL);
3276 /* If no threads need to be started, return here. */
3277 if ((listen_sockets_num == 0)
3278 || ((dispatch_thread_running != 0)
3279 && (receive_thread_running != 0)))
3282 if (dispatch_thread_running == 0)
3285 status = pthread_create (&dispatch_thread_id,
3286 NULL /* no attributes */,
3288 NULL /* no argument */);
3292 ERROR ("network: pthread_create failed: %s",
3293 sstrerror (errno, errbuf,
3298 dispatch_thread_running = 1;
3302 if (receive_thread_running == 0)
3305 status = pthread_create (&receive_thread_id,
3306 NULL /* no attributes */,
3308 NULL /* no argument */);
3312 ERROR ("network: pthread_create failed: %s",
3313 sstrerror (errno, errbuf,
3318 receive_thread_running = 1;
3323 } /* int network_init */
3326 * The flush option of the network plugin cannot flush individual identifiers.
3327 * All the values are added to a buffer and sent when the buffer is full, the
3328 * requested value may or may not be in there, it's not worth finding out. We
3329 * just send the buffer if `flush' is called - if the requested value was in
3330 * there, good. If not, well, then there is nothing to flush.. -octo
3332 static int network_flush (int timeout,
3333 const char __attribute__((unused)) *identifier,
3334 user_data_t __attribute__((unused)) *user_data)
3336 pthread_mutex_lock (&send_buffer_lock);
3338 if (send_buffer_fill > 0)
3341 pthread_mutex_unlock (&send_buffer_lock);
3344 } /* int network_flush */
3346 void module_register (void)
3348 plugin_register_complex_config ("network", network_config);
3349 plugin_register_init ("network", network_init);
3350 plugin_register_flush ("network", network_flush,
3351 /* user_data = */ NULL);
3352 } /* void module_register */
3354 /* vim: set fdm=marker : */