2 * collectd - src/network.c
3 * Copyright (C) 2005-2013 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 collectd.org>
22 * Aman Gupta <aman at tmm1.net>
25 #define _DEFAULT_SOURCE
26 #define _BSD_SOURCE /* For struct ip_mreq */
31 #include "configfile.h"
32 #include "utils_fbhash.h"
33 #include "utils_avltree.h"
34 #include "utils_cache.h"
35 #include "utils_complain.h"
43 # include <sys/socket.h>
49 # include <netinet/in.h>
52 # include <arpa/inet.h>
63 # if defined __APPLE__
64 /* default xcode compiler throws warnings even when deprecated functionality
65 * is not used. -Werror breaks the build because of erroneous warnings.
66 * http://stackoverflow.com/questions/10556299/compiler-warnings-with-libgcrypt-v1-5-0/12830209#12830209
68 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
70 /* FreeBSD's copy of libgcrypt extends the existing GCRYPT_NO_DEPRECATED
71 * to properly hide all deprecated functionality.
72 * http://svnweb.freebsd.org/ports/head/security/libgcrypt/files/patch-src__gcrypt.h.in
74 # define GCRYPT_NO_DEPRECATED
76 # if defined __APPLE__
77 /* Re enable deprecation warnings */
78 # pragma GCC diagnostic warning "-Wdeprecated-declarations"
80 # if GCRYPT_VERSION_NUMBER < 0x010600
81 GCRY_THREAD_OPTION_PTHREAD_IMPL;
85 #ifndef IPV6_ADD_MEMBERSHIP
86 # ifdef IPV6_JOIN_GROUP
87 # define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
89 # error "Neither IP_ADD_MEMBERSHIP nor IPV6_JOIN_GROUP is defined"
91 #endif /* !IP_ADD_MEMBERSHIP */
94 * Maximum size required for encryption / signing:
96 * 42 bytes for the encryption header
97 * + 64 bytes for the username
101 #define BUFF_SIG_SIZE 106
106 #define SECURITY_LEVEL_NONE 0
108 # define SECURITY_LEVEL_SIGN 1
109 # define SECURITY_LEVEL_ENCRYPT 2
111 struct sockent_client
114 struct sockaddr_storage *addr;
120 gcry_cipher_hd_t cypher;
121 unsigned char password_hash[32];
125 struct sockent_server
133 gcry_cipher_hd_t cypher;
137 typedef struct sockent
139 #define SOCKENT_TYPE_CLIENT 1
140 #define SOCKENT_TYPE_SERVER 2
149 struct sockent_client client;
150 struct sockent_server server;
153 struct sockent *next;
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 * +-------+-----------------------+-------------------------------+
159 * ! Ver. ! ! Length !
160 * +-------+-----------------------+-------------------------------+
167 typedef struct part_header_s part_header_t;
169 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
170 * 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
171 * +-------------------------------+-------------------------------+
173 * +-------------------------------+-------------------------------+
174 * : (Length - 4) Bytes :
175 * +---------------------------------------------------------------+
182 typedef struct part_string_s part_string_t;
184 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
185 * 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
186 * +-------------------------------+-------------------------------+
188 * +-------------------------------+-------------------------------+
189 * : (Length - 4 == 2 || 4 || 8) Bytes :
190 * +---------------------------------------------------------------+
197 typedef struct part_number_s part_number_t;
199 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
200 * 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
201 * +-------------------------------+-------------------------------+
203 * +-------------------------------+---------------+---------------+
204 * ! Num of values ! Type0 ! Type1 !
205 * +-------------------------------+---------------+---------------+
208 * +---------------------------------------------------------------+
211 * +---------------------------------------------------------------+
216 uint16_t *num_values;
217 uint8_t *values_types;
220 typedef struct part_values_s part_values_t;
222 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
223 * 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
224 * +-------------------------------+-------------------------------+
226 * +-------------------------------+-------------------------------+
227 * ! Hash (Bits 0 - 31) !
229 * ! Hash (Bits 224 - 255) !
230 * +---------------------------------------------------------------+
233 #define PART_SIGNATURE_SHA256_SIZE 36
234 struct part_signature_sha256_s
237 unsigned char hash[32];
240 typedef struct part_signature_sha256_s part_signature_sha256_t;
242 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
243 * 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
244 * +-------------------------------+-------------------------------+
246 * +-------------------------------+-------------------------------+
247 * ! Original length ! Padding (0 - 15 bytes) !
248 * +-------------------------------+-------------------------------+
249 * ! Hash (Bits 0 - 31) !
251 * ! Hash (Bits 128 - 159) !
252 * +---------------------------------------------------------------+
255 #define PART_ENCRYPTION_AES256_SIZE 42
256 struct part_encryption_aes256_s
259 uint16_t username_length;
261 unsigned char iv[16];
263 unsigned char hash[20];
267 typedef struct part_encryption_aes256_s part_encryption_aes256_t;
269 struct receive_list_entry_s
274 struct receive_list_entry_s *next;
276 typedef struct receive_list_entry_s receive_list_entry_t;
281 static int network_config_ttl = 0;
282 /* Ethernet - (IPv6 + UDP) = 1500 - (40 + 8) = 1452 */
283 static size_t network_config_packet_size = 1452;
284 static int network_config_forward = 0;
285 static int network_config_stats = 0;
287 static sockent_t *sending_sockets = NULL;
289 static receive_list_entry_t *receive_list_head = NULL;
290 static receive_list_entry_t *receive_list_tail = NULL;
291 static pthread_mutex_t receive_list_lock = PTHREAD_MUTEX_INITIALIZER;
292 static pthread_cond_t receive_list_cond = PTHREAD_COND_INITIALIZER;
293 static uint64_t receive_list_length = 0;
295 static sockent_t *listen_sockets = NULL;
296 static struct pollfd *listen_sockets_pollfd = NULL;
297 static size_t listen_sockets_num = 0;
299 /* The receive and dispatch threads will run as long as `listen_loop' is set to
301 static int listen_loop = 0;
302 static int receive_thread_running = 0;
303 static pthread_t receive_thread_id;
304 static int dispatch_thread_running = 0;
305 static pthread_t dispatch_thread_id;
307 /* Buffer in which to-be-sent network packets are constructed. */
308 static char *send_buffer;
309 static char *send_buffer_ptr;
310 static int send_buffer_fill;
311 static value_list_t send_buffer_vl = VALUE_LIST_STATIC;
312 static pthread_mutex_t send_buffer_lock = PTHREAD_MUTEX_INITIALIZER;
314 /* XXX: These counters are incremented from one place only. The spot in which
315 * the values are incremented is either only reachable by one thread (the
316 * dispatch thread, for example) or locked by some lock (send_buffer_lock for
317 * example). Only if neither is true, the stats_lock is acquired. The counters
318 * are always read without holding a lock in the hope that writing 8 bytes to
319 * memory is an atomic operation. */
320 static derive_t stats_octets_rx = 0;
321 static derive_t stats_octets_tx = 0;
322 static derive_t stats_packets_rx = 0;
323 static derive_t stats_packets_tx = 0;
324 static derive_t stats_values_dispatched = 0;
325 static derive_t stats_values_not_dispatched = 0;
326 static derive_t stats_values_sent = 0;
327 static derive_t stats_values_not_sent = 0;
328 static pthread_mutex_t stats_lock = PTHREAD_MUTEX_INITIALIZER;
333 static _Bool check_receive_okay (const value_list_t *vl) /* {{{ */
335 uint64_t time_sent = 0;
338 status = uc_meta_data_get_unsigned_int (vl,
339 "network:time_sent", &time_sent);
341 /* This is a value we already sent. Don't allow it to be received again in
342 * order to avoid looping. */
343 if ((status == 0) && (time_sent >= ((uint64_t) vl->time)))
347 } /* }}} _Bool check_receive_okay */
349 static _Bool check_send_okay (const value_list_t *vl) /* {{{ */
354 if (network_config_forward != 0)
357 if (vl->meta == NULL)
360 status = meta_data_get_boolean (vl->meta, "network:received", &received);
361 if (status == -ENOENT)
363 else if (status != 0)
365 ERROR ("network plugin: check_send_okay: meta_data_get_boolean failed "
366 "with status %i.", status);
370 /* By default, only *send* value lists that were not *received* by the
373 } /* }}} _Bool check_send_okay */
375 static _Bool check_notify_received (const notification_t *n) /* {{{ */
377 notification_meta_t *ptr;
379 for (ptr = n->meta; ptr != NULL; ptr = ptr->next)
380 if ((strcmp ("network:received", ptr->name) == 0)
381 && (ptr->type == NM_TYPE_BOOLEAN))
382 return ((_Bool) ptr->nm_value.nm_boolean);
385 } /* }}} _Bool check_notify_received */
387 static _Bool check_send_notify_okay (const notification_t *n) /* {{{ */
389 static c_complain_t complain_forwarding = C_COMPLAIN_INIT_STATIC;
395 received = check_notify_received (n);
397 if (network_config_forward && received)
399 c_complain_once (LOG_ERR, &complain_forwarding,
400 "network plugin: A notification has been received via the network "
401 "and forwarding is enabled. Forwarding of notifications is currently "
402 "not supported, because there is not loop-deteciton available. "
403 "Please contact the collectd mailing list if you need this "
407 /* By default, only *send* value lists that were not *received* by the
410 } /* }}} _Bool check_send_notify_okay */
412 static int network_dispatch_values (value_list_t *vl, /* {{{ */
413 const char *username)
418 || (strlen (vl->host) <= 0)
419 || (strlen (vl->plugin) <= 0)
420 || (strlen (vl->type) <= 0))
423 if (!check_receive_okay (vl))
426 char name[6*DATA_MAX_NAME_LEN];
427 FORMAT_VL (name, sizeof (name), vl);
428 name[sizeof (name) - 1] = 0;
429 DEBUG ("network plugin: network_dispatch_values: "
430 "NOT dispatching %s.", name);
432 stats_values_not_dispatched++;
436 assert (vl->meta == NULL);
438 vl->meta = meta_data_create ();
439 if (vl->meta == NULL)
441 ERROR ("network plugin: meta_data_create failed.");
445 status = meta_data_add_boolean (vl->meta, "network:received", 1);
448 ERROR ("network plugin: meta_data_add_boolean failed.");
449 meta_data_destroy (vl->meta);
454 if (username != NULL)
456 status = meta_data_add_string (vl->meta, "network:username", username);
459 ERROR ("network plugin: meta_data_add_string failed.");
460 meta_data_destroy (vl->meta);
466 plugin_dispatch_values (vl);
467 stats_values_dispatched++;
469 meta_data_destroy (vl->meta);
473 } /* }}} int network_dispatch_values */
475 static int network_dispatch_notification (notification_t *n) /* {{{ */
479 assert (n->meta == NULL);
481 status = plugin_notification_meta_add_boolean (n, "network:received", 1);
484 ERROR ("network plugin: plugin_notification_meta_add_boolean failed.");
485 plugin_notification_meta_free (n->meta);
490 status = plugin_dispatch_notification (n);
492 plugin_notification_meta_free (n->meta);
496 } /* }}} int network_dispatch_notification */
499 static void network_init_gcrypt (void) /* {{{ */
501 /* http://lists.gnupg.org/pipermail/gcrypt-devel/2003-August/000458.html
502 * Because you can't know in a library whether another library has
503 * already initialized the library */
504 if (gcry_control (GCRYCTL_ANY_INITIALIZATION_P))
507 /* http://www.gnupg.org/documentation/manuals/gcrypt/Multi_002dThreading.html
508 * To ensure thread-safety, it's important to set GCRYCTL_SET_THREAD_CBS
509 * *before* initalizing Libgcrypt with gcry_check_version(), which itself must
510 * be called before any other gcry_* function. GCRYCTL_ANY_INITIALIZATION_P
511 * above doesn't count, as it doesn't implicitly initalize Libgcrypt.
513 * tl;dr: keep all these gry_* statements in this exact order please. */
514 # if GCRYPT_VERSION_NUMBER < 0x010600
515 gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
517 gcry_check_version (NULL);
518 gcry_control (GCRYCTL_INIT_SECMEM, 32768);
519 gcry_control (GCRYCTL_INITIALIZATION_FINISHED);
520 } /* }}} void network_init_gcrypt */
522 static gcry_cipher_hd_t network_get_aes256_cypher (sockent_t *se, /* {{{ */
523 const void *iv, size_t iv_size, const char *username)
526 gcry_cipher_hd_t *cyper_ptr;
527 unsigned char password_hash[32];
529 if (se->type == SOCKENT_TYPE_CLIENT)
531 cyper_ptr = &se->data.client.cypher;
532 memcpy (password_hash, se->data.client.password_hash,
533 sizeof (password_hash));
539 cyper_ptr = &se->data.server.cypher;
541 if (username == NULL)
544 secret = fbh_get (se->data.server.userdb, username);
548 gcry_md_hash_buffer (GCRY_MD_SHA256,
550 secret, strlen (secret));
555 if (*cyper_ptr == NULL)
557 err = gcry_cipher_open (cyper_ptr,
558 GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_OFB, /* flags = */ 0);
561 ERROR ("network plugin: gcry_cipher_open returned: %s",
562 gcry_strerror (err));
569 gcry_cipher_reset (*cyper_ptr);
571 assert (*cyper_ptr != NULL);
573 err = gcry_cipher_setkey (*cyper_ptr,
574 password_hash, sizeof (password_hash));
577 ERROR ("network plugin: gcry_cipher_setkey returned: %s",
578 gcry_strerror (err));
579 gcry_cipher_close (*cyper_ptr);
584 err = gcry_cipher_setiv (*cyper_ptr, iv, iv_size);
587 ERROR ("network plugin: gcry_cipher_setkey returned: %s",
588 gcry_strerror (err));
589 gcry_cipher_close (*cyper_ptr);
595 } /* }}} int network_get_aes256_cypher */
596 #endif /* HAVE_LIBGCRYPT */
598 static int write_part_values (char **ret_buffer, int *ret_buffer_len,
599 const data_set_t *ds, const value_list_t *vl)
605 part_header_t pkg_ph;
606 uint16_t pkg_num_values;
607 uint8_t *pkg_values_types;
613 num_values = vl->values_len;
614 packet_len = sizeof (part_header_t) + sizeof (uint16_t)
615 + (num_values * sizeof (uint8_t))
616 + (num_values * sizeof (value_t));
618 if (*ret_buffer_len < packet_len)
621 pkg_values_types = (uint8_t *) malloc (num_values * sizeof (uint8_t));
622 if (pkg_values_types == NULL)
624 ERROR ("network plugin: write_part_values: malloc failed.");
628 pkg_values = (value_t *) malloc (num_values * sizeof (value_t));
629 if (pkg_values == NULL)
631 free (pkg_values_types);
632 ERROR ("network plugin: write_part_values: malloc failed.");
636 pkg_ph.type = htons (TYPE_VALUES);
637 pkg_ph.length = htons (packet_len);
639 pkg_num_values = htons ((uint16_t) vl->values_len);
641 for (i = 0; i < num_values; i++)
643 pkg_values_types[i] = (uint8_t) ds->ds[i].type;
644 switch (ds->ds[i].type)
646 case DS_TYPE_COUNTER:
647 pkg_values[i].counter = htonll (vl->values[i].counter);
651 pkg_values[i].gauge = htond (vl->values[i].gauge);
655 pkg_values[i].derive = htonll (vl->values[i].derive);
658 case DS_TYPE_ABSOLUTE:
659 pkg_values[i].absolute = htonll (vl->values[i].absolute);
663 free (pkg_values_types);
665 ERROR ("network plugin: write_part_values: "
666 "Unknown data source type: %i",
669 } /* switch (ds->ds[i].type) */
670 } /* for (num_values) */
673 * Use `memcpy' to write everything to the buffer, because the pointer
674 * may be unaligned and some architectures, such as SPARC, can't handle
677 packet_ptr = *ret_buffer;
679 memcpy (packet_ptr + offset, &pkg_ph, sizeof (pkg_ph));
680 offset += sizeof (pkg_ph);
681 memcpy (packet_ptr + offset, &pkg_num_values, sizeof (pkg_num_values));
682 offset += sizeof (pkg_num_values);
683 memcpy (packet_ptr + offset, pkg_values_types, num_values * sizeof (uint8_t));
684 offset += num_values * sizeof (uint8_t);
685 memcpy (packet_ptr + offset, pkg_values, num_values * sizeof (value_t));
686 offset += num_values * sizeof (value_t);
688 assert (offset == packet_len);
690 *ret_buffer = packet_ptr + packet_len;
691 *ret_buffer_len -= packet_len;
693 free (pkg_values_types);
697 } /* int write_part_values */
699 static int write_part_number (char **ret_buffer, int *ret_buffer_len,
700 int type, uint64_t value)
705 part_header_t pkg_head;
710 packet_len = sizeof (pkg_head) + sizeof (pkg_value);
712 if (*ret_buffer_len < packet_len)
715 pkg_head.type = htons (type);
716 pkg_head.length = htons (packet_len);
717 pkg_value = htonll (value);
719 packet_ptr = *ret_buffer;
721 memcpy (packet_ptr + offset, &pkg_head, sizeof (pkg_head));
722 offset += sizeof (pkg_head);
723 memcpy (packet_ptr + offset, &pkg_value, sizeof (pkg_value));
724 offset += sizeof (pkg_value);
726 assert (offset == packet_len);
728 *ret_buffer = packet_ptr + packet_len;
729 *ret_buffer_len -= packet_len;
732 } /* int write_part_number */
734 static int write_part_string (char **ret_buffer, int *ret_buffer_len,
735 int type, const char *str, int str_len)
745 buffer_len = 2 * sizeof (uint16_t) + str_len + 1;
746 if (*ret_buffer_len < buffer_len)
749 pkg_type = htons (type);
750 pkg_length = htons (buffer_len);
752 buffer = *ret_buffer;
754 memcpy (buffer + offset, (void *) &pkg_type, sizeof (pkg_type));
755 offset += sizeof (pkg_type);
756 memcpy (buffer + offset, (void *) &pkg_length, sizeof (pkg_length));
757 offset += sizeof (pkg_length);
758 memcpy (buffer + offset, str, str_len);
760 memset (buffer + offset, '\0', 1);
763 assert (offset == buffer_len);
765 *ret_buffer = buffer + buffer_len;
766 *ret_buffer_len -= buffer_len;
769 } /* int write_part_string */
771 static int parse_part_values (void **ret_buffer, size_t *ret_buffer_len,
772 value_t **ret_values, int *ret_num_values)
774 char *buffer = *ret_buffer;
775 size_t buffer_len = *ret_buffer_len;
790 NOTICE ("network plugin: packet is too short: "
791 "buffer_len = %zu", buffer_len);
795 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
796 buffer += sizeof (tmp16);
797 pkg_type = ntohs (tmp16);
799 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
800 buffer += sizeof (tmp16);
801 pkg_length = ntohs (tmp16);
803 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
804 buffer += sizeof (tmp16);
805 pkg_numval = ntohs (tmp16);
807 assert (pkg_type == TYPE_VALUES);
809 exp_size = 3 * sizeof (uint16_t)
810 + pkg_numval * (sizeof (uint8_t) + sizeof (value_t));
811 if (buffer_len < exp_size)
813 WARNING ("network plugin: parse_part_values: "
815 "Chunk of size %zu expected, "
816 "but buffer has only %zu bytes left.",
817 exp_size, buffer_len);
821 if (pkg_length != exp_size)
823 WARNING ("network plugin: parse_part_values: "
824 "Length and number of values "
825 "in the packet don't match.");
829 pkg_types = (uint8_t *) malloc (pkg_numval * sizeof (uint8_t));
830 pkg_values = (value_t *) malloc (pkg_numval * sizeof (value_t));
831 if ((pkg_types == NULL) || (pkg_values == NULL))
835 ERROR ("network plugin: parse_part_values: malloc failed.");
839 memcpy ((void *) pkg_types, (void *) buffer, pkg_numval * sizeof (uint8_t));
840 buffer += pkg_numval * sizeof (uint8_t);
841 memcpy ((void *) pkg_values, (void *) buffer, pkg_numval * sizeof (value_t));
842 buffer += pkg_numval * sizeof (value_t);
844 for (i = 0; i < pkg_numval; i++)
846 switch (pkg_types[i])
848 case DS_TYPE_COUNTER:
849 pkg_values[i].counter = (counter_t) ntohll (pkg_values[i].counter);
853 pkg_values[i].gauge = (gauge_t) ntohd (pkg_values[i].gauge);
857 pkg_values[i].derive = (derive_t) ntohll (pkg_values[i].derive);
860 case DS_TYPE_ABSOLUTE:
861 pkg_values[i].absolute = (absolute_t) ntohll (pkg_values[i].absolute);
865 NOTICE ("network plugin: parse_part_values: "
866 "Don't know how to handle data source type %"PRIu8,
871 } /* switch (pkg_types[i]) */
874 *ret_buffer = buffer;
875 *ret_buffer_len = buffer_len - pkg_length;
876 *ret_num_values = pkg_numval;
877 *ret_values = pkg_values;
882 } /* int parse_part_values */
884 static int parse_part_number (void **ret_buffer, size_t *ret_buffer_len,
887 char *buffer = *ret_buffer;
888 size_t buffer_len = *ret_buffer_len;
892 size_t exp_size = 2 * sizeof (uint16_t) + sizeof (uint64_t);
896 if (buffer_len < exp_size)
898 WARNING ("network plugin: parse_part_number: "
900 "Chunk of size %zu expected, "
901 "but buffer has only %zu bytes left.",
902 exp_size, buffer_len);
906 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
907 buffer += sizeof (tmp16);
908 /* pkg_type = ntohs (tmp16); */
910 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
911 buffer += sizeof (tmp16);
912 pkg_length = ntohs (tmp16);
914 memcpy ((void *) &tmp64, buffer, sizeof (tmp64));
915 buffer += sizeof (tmp64);
916 *value = ntohll (tmp64);
918 *ret_buffer = buffer;
919 *ret_buffer_len = buffer_len - pkg_length;
922 } /* int parse_part_number */
924 static int parse_part_string (void **ret_buffer, size_t *ret_buffer_len,
925 char *output, int output_len)
927 char *buffer = *ret_buffer;
928 size_t buffer_len = *ret_buffer_len;
931 size_t header_size = 2 * sizeof (uint16_t);
935 if (buffer_len < header_size)
937 WARNING ("network plugin: parse_part_string: "
939 "Chunk of at least size %zu expected, "
940 "but buffer has only %zu bytes left.",
941 header_size, buffer_len);
945 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
946 buffer += sizeof (tmp16);
947 /* pkg_type = ntohs (tmp16); */
949 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
950 buffer += sizeof (tmp16);
951 pkg_length = ntohs (tmp16);
953 /* Check that packet fits in the input buffer */
954 if (pkg_length > buffer_len)
956 WARNING ("network plugin: parse_part_string: "
958 "Chunk of size %"PRIu16" received, "
959 "but buffer has only %zu bytes left.",
960 pkg_length, buffer_len);
964 /* Check that pkg_length is in the valid range */
965 if (pkg_length <= header_size)
967 WARNING ("network plugin: parse_part_string: "
969 "Header claims this packet is only %hu "
970 "bytes long.", pkg_length);
974 /* Check that the package data fits into the output buffer.
975 * The previous if-statement ensures that:
976 * `pkg_length > header_size' */
978 || ((size_t) output_len < ((size_t) pkg_length - header_size)))
980 WARNING ("network plugin: parse_part_string: "
981 "Output buffer too small.");
985 /* All sanity checks successfull, let's copy the data over */
986 output_len = pkg_length - header_size;
987 memcpy ((void *) output, (void *) buffer, output_len);
988 buffer += output_len;
990 /* For some very weird reason '\0' doesn't do the trick on SPARC in
992 if (output[output_len - 1] != 0)
994 WARNING ("network plugin: parse_part_string: "
995 "Received string does not end "
996 "with a NULL-byte.");
1000 *ret_buffer = buffer;
1001 *ret_buffer_len = buffer_len - pkg_length;
1004 } /* int parse_part_string */
1006 /* Forward declaration: parse_part_sign_sha256 and parse_part_encr_aes256 call
1007 * parse_packet and vice versa. */
1008 #define PP_SIGNED 0x01
1009 #define PP_ENCRYPTED 0x02
1010 static int parse_packet (sockent_t *se,
1011 void *buffer, size_t buffer_size, int flags,
1012 const char *username);
1014 #define BUFFER_READ(p,s) do { \
1015 memcpy ((p), buffer + buffer_offset, (s)); \
1016 buffer_offset += (s); \
1020 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
1021 void **ret_buffer, size_t *ret_buffer_len, int flags)
1023 static c_complain_t complain_no_users = C_COMPLAIN_INIT_STATIC;
1027 size_t buffer_offset;
1029 size_t username_len;
1032 part_signature_sha256_t pss;
1033 uint16_t pss_head_length;
1034 char hash[sizeof (pss.hash)];
1038 unsigned char *hash_ptr;
1040 buffer = *ret_buffer;
1041 buffer_len = *ret_buffer_len;
1044 if (se->data.server.userdb == NULL)
1046 c_complain (LOG_NOTICE, &complain_no_users,
1047 "network plugin: Received signed network packet but can't verify it "
1048 "because no user DB has been configured. Will accept it.");
1052 /* Check if the buffer has enough data for this structure. */
1053 if (buffer_len <= PART_SIGNATURE_SHA256_SIZE)
1056 /* Read type and length header */
1057 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
1058 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
1059 pss_head_length = ntohs (pss.head.length);
1061 /* Check if the `pss_head_length' is within bounds. */
1062 if ((pss_head_length <= PART_SIGNATURE_SHA256_SIZE)
1063 || (pss_head_length > buffer_len))
1065 ERROR ("network plugin: HMAC-SHA-256 with invalid length received.");
1069 /* Copy the hash. */
1070 BUFFER_READ (pss.hash, sizeof (pss.hash));
1072 /* Calculate username length (without null byte) and allocate memory */
1073 username_len = pss_head_length - PART_SIGNATURE_SHA256_SIZE;
1074 pss.username = malloc (username_len + 1);
1075 if (pss.username == NULL)
1078 /* Read the username */
1079 BUFFER_READ (pss.username, username_len);
1080 pss.username[username_len] = 0;
1082 assert (buffer_offset == pss_head_length);
1084 /* Query the password */
1085 secret = fbh_get (se->data.server.userdb, pss.username);
1088 ERROR ("network plugin: Unknown user: %s", pss.username);
1089 sfree (pss.username);
1093 /* Create a hash device and check the HMAC */
1095 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
1098 ERROR ("network plugin: Creating HMAC-SHA-256 object failed: %s",
1099 gcry_strerror (err));
1101 sfree (pss.username);
1105 err = gcry_md_setkey (hd, secret, strlen (secret));
1108 ERROR ("network plugin: gcry_md_setkey failed: %s", gcry_strerror (err));
1111 sfree (pss.username);
1116 buffer + PART_SIGNATURE_SHA256_SIZE,
1117 buffer_len - PART_SIGNATURE_SHA256_SIZE);
1118 hash_ptr = gcry_md_read (hd, GCRY_MD_SHA256);
1119 if (hash_ptr == NULL)
1121 ERROR ("network plugin: gcry_md_read failed.");
1124 sfree (pss.username);
1127 memcpy (hash, hash_ptr, sizeof (hash));
1133 if (memcmp (pss.hash, hash, sizeof (pss.hash)) != 0)
1135 WARNING ("network plugin: Verifying HMAC-SHA-256 signature failed: "
1140 parse_packet (se, buffer + buffer_offset, buffer_len - buffer_offset,
1141 flags | PP_SIGNED, pss.username);
1145 sfree (pss.username);
1147 *ret_buffer = buffer + buffer_len;
1148 *ret_buffer_len = 0;
1151 } /* }}} int parse_part_sign_sha256 */
1152 /* #endif HAVE_LIBGCRYPT */
1154 #else /* if !HAVE_LIBGCRYPT */
1155 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
1156 void **ret_buffer, size_t *ret_buffer_size, int flags)
1158 static int warning_has_been_printed = 0;
1162 size_t buffer_offset;
1165 part_signature_sha256_t pss;
1167 buffer = *ret_buffer;
1168 buffer_size = *ret_buffer_size;
1171 if (buffer_size <= PART_SIGNATURE_SHA256_SIZE)
1174 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
1175 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
1176 part_len = ntohs (pss.head.length);
1178 if ((part_len <= PART_SIGNATURE_SHA256_SIZE)
1179 || (part_len > buffer_size))
1182 if (warning_has_been_printed == 0)
1184 WARNING ("network plugin: Received signed packet, but the network "
1185 "plugin was not linked with libgcrypt, so I cannot "
1186 "verify the signature. The packet will be accepted.");
1187 warning_has_been_printed = 1;
1190 parse_packet (se, buffer + part_len, buffer_size - part_len, flags,
1191 /* username = */ NULL);
1193 *ret_buffer = buffer + buffer_size;
1194 *ret_buffer_size = 0;
1197 } /* }}} int parse_part_sign_sha256 */
1198 #endif /* !HAVE_LIBGCRYPT */
1201 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1202 void **ret_buffer, size_t *ret_buffer_len,
1205 char *buffer = *ret_buffer;
1206 size_t buffer_len = *ret_buffer_len;
1209 size_t buffer_offset;
1210 uint16_t username_len;
1211 part_encryption_aes256_t pea;
1212 unsigned char hash[sizeof (pea.hash)];
1214 gcry_cipher_hd_t cypher;
1217 /* Make sure at least the header if available. */
1218 if (buffer_len <= PART_ENCRYPTION_AES256_SIZE)
1220 NOTICE ("network plugin: parse_part_encr_aes256: "
1221 "Discarding short packet.");
1227 /* Copy the unencrypted information into `pea'. */
1228 BUFFER_READ (&pea.head.type, sizeof (pea.head.type));
1229 BUFFER_READ (&pea.head.length, sizeof (pea.head.length));
1231 /* Check the `part size'. */
1232 part_size = ntohs (pea.head.length);
1233 if ((part_size <= PART_ENCRYPTION_AES256_SIZE)
1234 || (part_size > buffer_len))
1236 NOTICE ("network plugin: parse_part_encr_aes256: "
1237 "Discarding part with invalid size.");
1241 /* Read the username */
1242 BUFFER_READ (&username_len, sizeof (username_len));
1243 username_len = ntohs (username_len);
1245 if ((username_len <= 0)
1246 || (username_len > (part_size - (PART_ENCRYPTION_AES256_SIZE + 1))))
1248 NOTICE ("network plugin: parse_part_encr_aes256: "
1249 "Discarding part with invalid username length.");
1253 assert (username_len > 0);
1254 pea.username = malloc (username_len + 1);
1255 if (pea.username == NULL)
1257 BUFFER_READ (pea.username, username_len);
1258 pea.username[username_len] = 0;
1260 /* Last but not least, the initialization vector */
1261 BUFFER_READ (pea.iv, sizeof (pea.iv));
1263 /* Make sure we are at the right position */
1264 assert (buffer_offset == (username_len +
1265 PART_ENCRYPTION_AES256_SIZE - sizeof (pea.hash)));
1267 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
1271 sfree (pea.username);
1275 payload_len = part_size - (PART_ENCRYPTION_AES256_SIZE + username_len);
1276 assert (payload_len > 0);
1278 /* Decrypt the packet in-place */
1279 err = gcry_cipher_decrypt (cypher,
1280 buffer + buffer_offset,
1281 part_size - buffer_offset,
1282 /* in = */ NULL, /* in len = */ 0);
1285 sfree (pea.username);
1286 ERROR ("network plugin: gcry_cipher_decrypt returned: %s",
1287 gcry_strerror (err));
1292 BUFFER_READ (pea.hash, sizeof (pea.hash));
1294 /* Make sure we're at the right position - again */
1295 assert (buffer_offset == (username_len + PART_ENCRYPTION_AES256_SIZE));
1296 assert (buffer_offset == (part_size - payload_len));
1298 /* Check hash sum */
1299 memset (hash, 0, sizeof (hash));
1300 gcry_md_hash_buffer (GCRY_MD_SHA1, hash,
1301 buffer + buffer_offset, payload_len);
1302 if (memcmp (hash, pea.hash, sizeof (hash)) != 0)
1304 sfree (pea.username);
1305 ERROR ("network plugin: Decryption failed: Checksum mismatch.");
1309 parse_packet (se, buffer + buffer_offset, payload_len,
1310 flags | PP_ENCRYPTED, pea.username);
1312 /* XXX: Free pea.username?!? */
1314 /* Update return values */
1315 *ret_buffer = buffer + part_size;
1316 *ret_buffer_len = buffer_len - part_size;
1318 sfree (pea.username);
1321 } /* }}} int parse_part_encr_aes256 */
1322 /* #endif HAVE_LIBGCRYPT */
1324 #else /* if !HAVE_LIBGCRYPT */
1325 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1326 void **ret_buffer, size_t *ret_buffer_size, int flags)
1328 static int warning_has_been_printed = 0;
1332 size_t buffer_offset;
1337 buffer = *ret_buffer;
1338 buffer_size = *ret_buffer_size;
1341 /* parse_packet assures this minimum size. */
1342 assert (buffer_size >= (sizeof (ph.type) + sizeof (ph.length)));
1344 BUFFER_READ (&ph.type, sizeof (ph.type));
1345 BUFFER_READ (&ph.length, sizeof (ph.length));
1346 ph_length = ntohs (ph.length);
1348 if ((ph_length <= PART_ENCRYPTION_AES256_SIZE)
1349 || (ph_length > buffer_size))
1351 ERROR ("network plugin: AES-256 encrypted part "
1352 "with invalid length received.");
1356 if (warning_has_been_printed == 0)
1358 WARNING ("network plugin: Received encrypted packet, but the network "
1359 "plugin was not linked with libgcrypt, so I cannot "
1360 "decrypt it. The part will be discarded.");
1361 warning_has_been_printed = 1;
1364 *ret_buffer += ph_length;
1365 *ret_buffer_size -= ph_length;
1368 } /* }}} int parse_part_encr_aes256 */
1369 #endif /* !HAVE_LIBGCRYPT */
1373 static int parse_packet (sockent_t *se, /* {{{ */
1374 void *buffer, size_t buffer_size, int flags,
1375 const char *username)
1379 value_list_t vl = VALUE_LIST_INIT;
1383 int packet_was_signed = (flags & PP_SIGNED);
1384 int packet_was_encrypted = (flags & PP_ENCRYPTED);
1385 int printed_ignore_warning = 0;
1386 #endif /* HAVE_LIBGCRYPT */
1389 memset (&vl, '\0', sizeof (vl));
1390 memset (&n, '\0', sizeof (n));
1393 while ((status == 0) && (0 < buffer_size)
1394 && ((unsigned int) buffer_size > sizeof (part_header_t)))
1396 uint16_t pkg_length;
1399 memcpy ((void *) &pkg_type,
1402 memcpy ((void *) &pkg_length,
1403 (void *) (buffer + sizeof (pkg_type)),
1404 sizeof (pkg_length));
1406 pkg_length = ntohs (pkg_length);
1407 pkg_type = ntohs (pkg_type);
1409 if (pkg_length > buffer_size)
1411 /* Ensure that this loop terminates eventually */
1412 if (pkg_length < (2 * sizeof (uint16_t)))
1415 if (pkg_type == TYPE_ENCR_AES256)
1417 status = parse_part_encr_aes256 (se,
1418 &buffer, &buffer_size, flags);
1421 ERROR ("network plugin: Decrypting AES256 "
1423 "with status %i.", status);
1428 else if ((se->data.server.security_level == SECURITY_LEVEL_ENCRYPT)
1429 && (packet_was_encrypted == 0))
1431 if (printed_ignore_warning == 0)
1433 INFO ("network plugin: Unencrypted packet or "
1434 "part has been ignored.");
1435 printed_ignore_warning = 1;
1437 buffer = ((char *) buffer) + pkg_length;
1440 #endif /* HAVE_LIBGCRYPT */
1441 else if (pkg_type == TYPE_SIGN_SHA256)
1443 status = parse_part_sign_sha256 (se,
1444 &buffer, &buffer_size, flags);
1447 ERROR ("network plugin: Verifying HMAC-SHA-256 "
1449 "with status %i.", status);
1454 else if ((se->data.server.security_level == SECURITY_LEVEL_SIGN)
1455 && (packet_was_encrypted == 0)
1456 && (packet_was_signed == 0))
1458 if (printed_ignore_warning == 0)
1460 INFO ("network plugin: Unsigned packet or "
1461 "part has been ignored.");
1462 printed_ignore_warning = 1;
1464 buffer = ((char *) buffer) + pkg_length;
1467 #endif /* HAVE_LIBGCRYPT */
1468 else if (pkg_type == TYPE_VALUES)
1470 status = parse_part_values (&buffer, &buffer_size,
1471 &vl.values, &vl.values_len);
1475 network_dispatch_values (&vl, username);
1479 else if (pkg_type == TYPE_TIME)
1482 status = parse_part_number (&buffer, &buffer_size,
1486 vl.time = TIME_T_TO_CDTIME_T (tmp);
1487 n.time = TIME_T_TO_CDTIME_T (tmp);
1490 else if (pkg_type == TYPE_TIME_HR)
1493 status = parse_part_number (&buffer, &buffer_size,
1497 vl.time = (cdtime_t) tmp;
1498 n.time = (cdtime_t) tmp;
1501 else if (pkg_type == TYPE_INTERVAL)
1504 status = parse_part_number (&buffer, &buffer_size,
1507 vl.interval = TIME_T_TO_CDTIME_T (tmp);
1509 else if (pkg_type == TYPE_INTERVAL_HR)
1512 status = parse_part_number (&buffer, &buffer_size,
1515 vl.interval = (cdtime_t) tmp;
1517 else if (pkg_type == TYPE_HOST)
1519 status = parse_part_string (&buffer, &buffer_size,
1520 vl.host, sizeof (vl.host));
1522 sstrncpy (n.host, vl.host, sizeof (n.host));
1524 else if (pkg_type == TYPE_PLUGIN)
1526 status = parse_part_string (&buffer, &buffer_size,
1527 vl.plugin, sizeof (vl.plugin));
1529 sstrncpy (n.plugin, vl.plugin,
1532 else if (pkg_type == TYPE_PLUGIN_INSTANCE)
1534 status = parse_part_string (&buffer, &buffer_size,
1536 sizeof (vl.plugin_instance));
1538 sstrncpy (n.plugin_instance,
1540 sizeof (n.plugin_instance));
1542 else if (pkg_type == TYPE_TYPE)
1544 status = parse_part_string (&buffer, &buffer_size,
1545 vl.type, sizeof (vl.type));
1547 sstrncpy (n.type, vl.type, sizeof (n.type));
1549 else if (pkg_type == TYPE_TYPE_INSTANCE)
1551 status = parse_part_string (&buffer, &buffer_size,
1553 sizeof (vl.type_instance));
1555 sstrncpy (n.type_instance, vl.type_instance,
1556 sizeof (n.type_instance));
1558 else if (pkg_type == TYPE_MESSAGE)
1560 status = parse_part_string (&buffer, &buffer_size,
1561 n.message, sizeof (n.message));
1567 else if ((n.severity != NOTIF_FAILURE)
1568 && (n.severity != NOTIF_WARNING)
1569 && (n.severity != NOTIF_OKAY))
1571 INFO ("network plugin: "
1572 "Ignoring notification with "
1573 "unknown severity %i.",
1576 else if (n.time <= 0)
1578 INFO ("network plugin: "
1579 "Ignoring notification with "
1582 else if (strlen (n.message) <= 0)
1584 INFO ("network plugin: "
1585 "Ignoring notification with "
1586 "an empty message.");
1590 network_dispatch_notification (&n);
1593 else if (pkg_type == TYPE_SEVERITY)
1596 status = parse_part_number (&buffer, &buffer_size,
1599 n.severity = (int) tmp;
1603 DEBUG ("network plugin: parse_packet: Unknown part"
1604 " type: 0x%04hx", pkg_type);
1605 buffer = ((char *) buffer) + pkg_length;
1607 } /* while (buffer_size > sizeof (part_header_t)) */
1609 if (status == 0 && buffer_size > 0)
1610 WARNING ("network plugin: parse_packet: Received truncated "
1611 "packet, try increasing `MaxPacketSize'");
1614 } /* }}} int parse_packet */
1616 static void free_sockent_client (struct sockent_client *sec) /* {{{ */
1625 sfree (sec->username);
1626 sfree (sec->password);
1627 if (sec->cypher != NULL)
1628 gcry_cipher_close (sec->cypher);
1630 } /* }}} void free_sockent_client */
1632 static void free_sockent_server (struct sockent_server *ses) /* {{{ */
1636 for (i = 0; i < ses->fd_num; i++)
1638 if (ses->fd[i] >= 0)
1647 sfree (ses->auth_file);
1648 fbh_destroy (ses->userdb);
1649 if (ses->cypher != NULL)
1650 gcry_cipher_close (ses->cypher);
1652 } /* }}} void free_sockent_server */
1654 static void sockent_destroy (sockent_t *se) /* {{{ */
1658 DEBUG ("network plugin: sockent_destroy (se = %p);", (void *) se);
1665 sfree (se->service);
1667 if (se->type == SOCKENT_TYPE_CLIENT)
1668 free_sockent_client (&se->data.client);
1670 free_sockent_server (&se->data.server);
1675 } /* }}} void sockent_destroy */
1678 * int network_set_ttl
1680 * Set the `IP_MULTICAST_TTL', `IP_TTL', `IPV6_MULTICAST_HOPS' or
1681 * `IPV6_UNICAST_HOPS', depending on which option is applicable.
1683 * The `struct addrinfo' is used to destinguish between unicast and multicast
1686 static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
1688 DEBUG ("network plugin: network_set_ttl: network_config_ttl = %i;",
1689 network_config_ttl);
1691 assert (se->type == SOCKENT_TYPE_CLIENT);
1693 if ((network_config_ttl < 1) || (network_config_ttl > 255))
1696 if (ai->ai_family == AF_INET)
1698 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1701 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1702 optname = IP_MULTICAST_TTL;
1706 if (setsockopt (se->data.client.fd, IPPROTO_IP, optname,
1707 &network_config_ttl,
1708 sizeof (network_config_ttl)) != 0)
1711 ERROR ("network plugin: setsockopt (ipv4-ttl): %s",
1712 sstrerror (errno, errbuf, sizeof (errbuf)));
1716 else if (ai->ai_family == AF_INET6)
1718 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1719 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1722 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1723 optname = IPV6_MULTICAST_HOPS;
1725 optname = IPV6_UNICAST_HOPS;
1727 if (setsockopt (se->data.client.fd, IPPROTO_IPV6, optname,
1728 &network_config_ttl,
1729 sizeof (network_config_ttl)) != 0)
1732 ERROR ("network plugin: setsockopt(ipv6-ttl): %s",
1733 sstrerror (errno, errbuf,
1740 } /* int network_set_ttl */
1742 static int network_set_interface (const sockent_t *se, const struct addrinfo *ai) /* {{{ */
1744 DEBUG ("network plugin: network_set_interface: interface index = %i;",
1747 assert (se->type == SOCKENT_TYPE_CLIENT);
1749 if (ai->ai_family == AF_INET)
1751 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1753 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1755 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1756 /* If possible, use the "ip_mreqn" structure which has
1757 * an "interface index" member. Using the interface
1758 * index is preferred here, because of its similarity
1759 * to the way IPv6 handles this. Unfortunately, it
1760 * appears not to be portable. */
1761 struct ip_mreqn mreq;
1763 memset (&mreq, 0, sizeof (mreq));
1764 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1765 mreq.imr_address.s_addr = ntohl (INADDR_ANY);
1766 mreq.imr_ifindex = se->interface;
1768 struct ip_mreq mreq;
1770 memset (&mreq, 0, sizeof (mreq));
1771 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1772 mreq.imr_interface.s_addr = ntohl (INADDR_ANY);
1775 if (setsockopt (se->data.client.fd, IPPROTO_IP, IP_MULTICAST_IF,
1776 &mreq, sizeof (mreq)) != 0)
1779 ERROR ("network plugin: setsockopt (ipv4-multicast-if): %s",
1780 sstrerror (errno, errbuf, sizeof (errbuf)));
1787 else if (ai->ai_family == AF_INET6)
1789 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1791 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1793 if (setsockopt (se->data.client.fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
1795 sizeof (se->interface)) != 0)
1798 ERROR ("network plugin: setsockopt (ipv6-multicast-if): %s",
1799 sstrerror (errno, errbuf,
1808 /* else: Not a multicast interface. */
1809 if (se->interface != 0)
1811 #if defined(HAVE_IF_INDEXTONAME) && HAVE_IF_INDEXTONAME && defined(SO_BINDTODEVICE)
1812 char interface_name[IFNAMSIZ];
1814 if (if_indextoname (se->interface, interface_name) == NULL)
1817 DEBUG ("network plugin: Binding socket to interface %s", interface_name);
1819 if (setsockopt (se->data.client.fd, SOL_SOCKET, SO_BINDTODEVICE,
1821 sizeof(interface_name)) == -1 )
1824 ERROR ("network plugin: setsockopt (bind-if): %s",
1825 sstrerror (errno, errbuf, sizeof (errbuf)));
1828 /* #endif HAVE_IF_INDEXTONAME && SO_BINDTODEVICE */
1831 WARNING ("network plugin: Cannot set the interface on a unicast "
1833 # if !defined(SO_BINDTODEVICE)
1834 "the \"SO_BINDTODEVICE\" socket option "
1836 "the \"if_indextoname\" function "
1838 "is not available on your system.");
1844 } /* }}} network_set_interface */
1846 static int network_bind_socket (int fd, const struct addrinfo *ai, const int interface_idx)
1855 /* allow multiple sockets to use the same PORT number */
1856 if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR,
1857 &yes, sizeof(yes)) == -1) {
1859 ERROR ("network plugin: setsockopt (reuseaddr): %s",
1860 sstrerror (errno, errbuf, sizeof (errbuf)));
1864 DEBUG ("fd = %i; calling `bind'", fd);
1866 if (bind (fd, ai->ai_addr, ai->ai_addrlen) == -1)
1870 sstrerror (errno, errbuf, sizeof (errbuf)));
1874 if (ai->ai_family == AF_INET)
1876 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1877 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1879 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1880 struct ip_mreqn mreq;
1882 struct ip_mreq mreq;
1885 DEBUG ("fd = %i; IPv4 multicast address found", fd);
1887 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1888 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1889 /* Set the interface using the interface index if
1890 * possible (available). Unfortunately, the struct
1891 * ip_mreqn is not portable. */
1892 mreq.imr_address.s_addr = ntohl (INADDR_ANY);
1893 mreq.imr_ifindex = interface_idx;
1895 mreq.imr_interface.s_addr = ntohl (INADDR_ANY);
1898 if (setsockopt (fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1899 &loop, sizeof (loop)) == -1)
1902 ERROR ("network plugin: setsockopt (multicast-loop): %s",
1903 sstrerror (errno, errbuf,
1908 if (setsockopt (fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1909 &mreq, sizeof (mreq)) == -1)
1912 ERROR ("network plugin: setsockopt (add-membership): %s",
1913 sstrerror (errno, errbuf,
1921 else if (ai->ai_family == AF_INET6)
1923 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1924 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1925 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1927 struct ipv6_mreq mreq;
1929 DEBUG ("fd = %i; IPv6 multicast address found", fd);
1931 memcpy (&mreq.ipv6mr_multiaddr,
1933 sizeof (addr->sin6_addr));
1935 /* http://developer.apple.com/documentation/Darwin/Reference/ManPages/man4/ip6.4.html
1936 * ipv6mr_interface may be set to zeroes to
1937 * choose the default multicast interface or to
1938 * the index of a particular multicast-capable
1939 * interface if the host is multihomed.
1940 * Membership is associ-associated with a
1941 * single interface; programs running on
1942 * multihomed hosts may need to join the same
1943 * group on more than one interface.*/
1944 mreq.ipv6mr_interface = interface_idx;
1946 if (setsockopt (fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
1947 &loop, sizeof (loop)) == -1)
1950 ERROR ("network plugin: setsockopt (ipv6-multicast-loop): %s",
1951 sstrerror (errno, errbuf,
1956 if (setsockopt (fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
1957 &mreq, sizeof (mreq)) == -1)
1960 ERROR ("network plugin: setsockopt (ipv6-add-membership): %s",
1961 sstrerror (errno, errbuf,
1970 #if defined(HAVE_IF_INDEXTONAME) && HAVE_IF_INDEXTONAME && defined(SO_BINDTODEVICE)
1971 /* if a specific interface was set, bind the socket to it. But to avoid
1972 * possible problems with multicast routing, only do that for non-multicast
1974 if (interface_idx != 0)
1976 char interface_name[IFNAMSIZ];
1978 if (if_indextoname (interface_idx, interface_name) == NULL)
1981 DEBUG ("fd = %i; Binding socket to interface %s", fd, interface_name);
1983 if (setsockopt (fd, SOL_SOCKET, SO_BINDTODEVICE,
1985 sizeof(interface_name)) == -1 )
1988 ERROR ("network plugin: setsockopt (bind-if): %s",
1989 sstrerror (errno, errbuf, sizeof (errbuf)));
1993 #endif /* HAVE_IF_INDEXTONAME && SO_BINDTODEVICE */
1996 } /* int network_bind_socket */
1998 /* Initialize a sockent structure. `type' must be either `SOCKENT_TYPE_CLIENT'
1999 * or `SOCKENT_TYPE_SERVER' */
2000 static sockent_t *sockent_create (int type) /* {{{ */
2004 if ((type != SOCKENT_TYPE_CLIENT) && (type != SOCKENT_TYPE_SERVER))
2007 se = malloc (sizeof (*se));
2010 memset (se, 0, sizeof (*se));
2018 if (type == SOCKENT_TYPE_SERVER)
2020 se->data.server.fd = NULL;
2021 se->data.server.fd_num = 0;
2023 se->data.server.security_level = SECURITY_LEVEL_NONE;
2024 se->data.server.auth_file = NULL;
2025 se->data.server.userdb = NULL;
2026 se->data.server.cypher = NULL;
2031 se->data.client.fd = -1;
2032 se->data.client.addr = NULL;
2034 se->data.client.security_level = SECURITY_LEVEL_NONE;
2035 se->data.client.username = NULL;
2036 se->data.client.password = NULL;
2037 se->data.client.cypher = NULL;
2042 } /* }}} sockent_t *sockent_create */
2044 static int sockent_init_crypto (sockent_t *se) /* {{{ */
2046 #if HAVE_LIBGCRYPT /* {{{ */
2047 if (se->type == SOCKENT_TYPE_CLIENT)
2049 if (se->data.client.security_level > SECURITY_LEVEL_NONE)
2051 network_init_gcrypt ();
2053 if ((se->data.client.username == NULL)
2054 || (se->data.client.password == NULL))
2056 ERROR ("network plugin: Client socket with "
2057 "security requested, but no "
2058 "credentials are configured.");
2061 gcry_md_hash_buffer (GCRY_MD_SHA256,
2062 se->data.client.password_hash,
2063 se->data.client.password,
2064 strlen (se->data.client.password));
2067 else /* (se->type == SOCKENT_TYPE_SERVER) */
2069 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
2071 network_init_gcrypt ();
2073 if (se->data.server.auth_file == NULL)
2075 ERROR ("network plugin: Server socket with "
2076 "security requested, but no "
2077 "password file is configured.");
2081 if (se->data.server.auth_file != NULL)
2083 se->data.server.userdb = fbh_create (se->data.server.auth_file);
2084 if (se->data.server.userdb == NULL)
2086 ERROR ("network plugin: Reading password file "
2088 se->data.server.auth_file);
2089 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
2094 #endif /* }}} HAVE_LIBGCRYPT */
2097 } /* }}} int sockent_init_crypto */
2099 static int sockent_client_connect (sockent_t *se) /* {{{ */
2101 static c_complain_t complaint = C_COMPLAIN_INIT_STATIC;
2103 struct sockent_client *client;
2104 struct addrinfo ai_hints;
2105 struct addrinfo *ai_list = NULL, *ai_ptr;
2108 if ((se == NULL) || (se->type != SOCKENT_TYPE_CLIENT))
2111 client = &se->data.client;
2112 if (client->fd >= 0) /* already connected */
2115 memset (&ai_hints, 0, sizeof (ai_hints));
2116 #ifdef AI_ADDRCONFIG
2117 ai_hints.ai_flags |= AI_ADDRCONFIG;
2119 ai_hints.ai_family = AF_UNSPEC;
2120 ai_hints.ai_socktype = SOCK_DGRAM;
2121 ai_hints.ai_protocol = IPPROTO_UDP;
2123 status = getaddrinfo (se->node,
2124 (se->service != NULL) ? se->service : NET_DEFAULT_PORT,
2125 &ai_hints, &ai_list);
2128 c_complain (LOG_ERR, &complaint,
2129 "network plugin: getaddrinfo (%s, %s) failed: %s",
2130 (se->node == NULL) ? "(null)" : se->node,
2131 (se->service == NULL) ? "(null)" : se->service,
2132 gai_strerror (status));
2137 c_release (LOG_NOTICE, &complaint,
2138 "network plugin: Successfully resolved \"%s\".",
2142 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
2144 client->fd = socket (ai_ptr->ai_family,
2145 ai_ptr->ai_socktype,
2146 ai_ptr->ai_protocol);
2150 ERROR ("network plugin: socket(2) failed: %s",
2151 sstrerror (errno, errbuf,
2156 client->addr = malloc (sizeof (*client->addr));
2157 if (client->addr == NULL)
2159 ERROR ("network plugin: malloc failed.");
2165 memset (client->addr, 0, sizeof (*client->addr));
2166 assert (sizeof (*client->addr) >= ai_ptr->ai_addrlen);
2167 memcpy (client->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
2168 client->addrlen = ai_ptr->ai_addrlen;
2170 network_set_ttl (se, ai_ptr);
2171 network_set_interface (se, ai_ptr);
2173 /* We don't open more than one write-socket per
2174 * node/service pair.. */
2178 freeaddrinfo (ai_list);
2182 } /* }}} int sockent_client_connect */
2184 static int sockent_client_disconnect (sockent_t *se) /* {{{ */
2186 struct sockent_client *client;
2188 if ((se == NULL) || (se->type != SOCKENT_TYPE_CLIENT))
2191 client = &se->data.client;
2192 if (client->fd >= 0) /* connected */
2198 sfree (client->addr);
2199 client->addrlen = 0;
2202 } /* }}} int sockent_client_disconnect */
2204 /* Open the file descriptors for a initialized sockent structure. */
2205 static int sockent_server_listen (sockent_t *se) /* {{{ */
2207 struct addrinfo ai_hints;
2208 struct addrinfo *ai_list, *ai_ptr;
2212 const char *service;
2217 assert (se->data.server.fd == NULL);
2218 assert (se->data.server.fd_num == 0);
2221 service = se->service;
2223 if (service == NULL)
2224 service = NET_DEFAULT_PORT;
2226 DEBUG ("network plugin: sockent_server_listen: node = %s; service = %s;",
2229 memset (&ai_hints, 0, sizeof (ai_hints));
2230 ai_hints.ai_flags = 0;
2232 ai_hints.ai_flags |= AI_PASSIVE;
2234 #ifdef AI_ADDRCONFIG
2235 ai_hints.ai_flags |= AI_ADDRCONFIG;
2237 ai_hints.ai_family = AF_UNSPEC;
2238 ai_hints.ai_socktype = SOCK_DGRAM;
2239 ai_hints.ai_protocol = IPPROTO_UDP;
2241 status = getaddrinfo (node, service, &ai_hints, &ai_list);
2244 ERROR ("network plugin: getaddrinfo (%s, %s) failed: %s",
2245 (se->node == NULL) ? "(null)" : se->node,
2246 (se->service == NULL) ? "(null)" : se->service,
2247 gai_strerror (status));
2251 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
2255 tmp = realloc (se->data.server.fd,
2256 sizeof (*tmp) * (se->data.server.fd_num + 1));
2259 ERROR ("network plugin: realloc failed.");
2262 se->data.server.fd = tmp;
2263 tmp = se->data.server.fd + se->data.server.fd_num;
2265 *tmp = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
2266 ai_ptr->ai_protocol);
2270 ERROR ("network plugin: socket(2) failed: %s",
2271 sstrerror (errno, errbuf,
2276 status = network_bind_socket (*tmp, ai_ptr, se->interface);
2284 se->data.server.fd_num++;
2286 } /* for (ai_list) */
2288 freeaddrinfo (ai_list);
2290 if (se->data.server.fd_num <= 0)
2293 } /* }}} int sockent_server_listen */
2295 /* Add a sockent to the global list of sockets */
2296 static int sockent_add (sockent_t *se) /* {{{ */
2298 sockent_t *last_ptr;
2303 if (se->type == SOCKENT_TYPE_SERVER)
2308 tmp = realloc (listen_sockets_pollfd,
2309 sizeof (*tmp) * (listen_sockets_num
2310 + se->data.server.fd_num));
2313 ERROR ("network plugin: realloc failed.");
2316 listen_sockets_pollfd = tmp;
2317 tmp = listen_sockets_pollfd + listen_sockets_num;
2319 for (i = 0; i < se->data.server.fd_num; i++)
2321 memset (tmp + i, 0, sizeof (*tmp));
2322 tmp[i].fd = se->data.server.fd[i];
2323 tmp[i].events = POLLIN | POLLPRI;
2327 listen_sockets_num += se->data.server.fd_num;
2329 if (listen_sockets == NULL)
2331 listen_sockets = se;
2334 last_ptr = listen_sockets;
2336 else /* if (se->type == SOCKENT_TYPE_CLIENT) */
2338 if (sending_sockets == NULL)
2340 sending_sockets = se;
2343 last_ptr = sending_sockets;
2346 while (last_ptr->next != NULL)
2347 last_ptr = last_ptr->next;
2348 last_ptr->next = se;
2351 } /* }}} int sockent_add */
2353 static void *dispatch_thread (void __attribute__((unused)) *arg) /* {{{ */
2357 receive_list_entry_t *ent;
2360 /* Lock and wait for more data to come in */
2361 pthread_mutex_lock (&receive_list_lock);
2362 while ((listen_loop == 0)
2363 && (receive_list_head == NULL))
2364 pthread_cond_wait (&receive_list_cond, &receive_list_lock);
2366 /* Remove the head entry and unlock */
2367 ent = receive_list_head;
2369 receive_list_head = ent->next;
2370 receive_list_length--;
2371 pthread_mutex_unlock (&receive_list_lock);
2373 /* Check whether we are supposed to exit. We do NOT check `listen_loop'
2374 * because we dispatch all missing packets before shutting down. */
2378 /* Look for the correct `sockent_t' */
2379 se = listen_sockets;
2384 for (i = 0; i < se->data.server.fd_num; i++)
2385 if (se->data.server.fd[i] == ent->fd)
2388 if (i < se->data.server.fd_num)
2396 ERROR ("network plugin: Got packet from FD %i, but can't "
2397 "find an appropriate socket entry.",
2404 parse_packet (se, ent->data, ent->data_len, /* flags = */ 0,
2405 /* username = */ NULL);
2411 } /* }}} void *dispatch_thread */
2413 static int network_receive (void) /* {{{ */
2415 char buffer[network_config_packet_size];
2421 receive_list_entry_t *private_list_head;
2422 receive_list_entry_t *private_list_tail;
2423 uint64_t private_list_length;
2425 assert (listen_sockets_num > 0);
2427 private_list_head = NULL;
2428 private_list_tail = NULL;
2429 private_list_length = 0;
2431 while (listen_loop == 0)
2433 status = poll (listen_sockets_pollfd, listen_sockets_num, -1);
2439 ERROR ("network plugin: poll(2) failed: %s",
2440 sstrerror (errno, errbuf, sizeof (errbuf)));
2444 for (i = 0; (i < listen_sockets_num) && (status > 0); i++)
2446 receive_list_entry_t *ent;
2448 if ((listen_sockets_pollfd[i].revents
2449 & (POLLIN | POLLPRI)) == 0)
2453 buffer_len = recv (listen_sockets_pollfd[i].fd,
2454 buffer, sizeof (buffer),
2459 status = (errno != 0) ? errno : -1;
2460 ERROR ("network plugin: recv(2) failed: %s",
2461 sstrerror (errno, errbuf, sizeof (errbuf)));
2465 stats_octets_rx += ((uint64_t) buffer_len);
2468 /* TODO: Possible performance enhancement: Do not free
2469 * these entries in the dispatch thread but put them in
2470 * another list, so we don't have to allocate more and
2471 * more of these structures. */
2472 ent = malloc (sizeof (receive_list_entry_t));
2475 ERROR ("network plugin: malloc failed.");
2479 memset (ent, 0, sizeof (receive_list_entry_t));
2480 ent->data = malloc (network_config_packet_size);
2481 if (ent->data == NULL)
2484 ERROR ("network plugin: malloc failed.");
2488 ent->fd = listen_sockets_pollfd[i].fd;
2491 memcpy (ent->data, buffer, buffer_len);
2492 ent->data_len = buffer_len;
2494 if (private_list_head == NULL)
2495 private_list_head = ent;
2497 private_list_tail->next = ent;
2498 private_list_tail = ent;
2499 private_list_length++;
2501 /* Do not block here. Blocking here has led to
2502 * insufficient performance in the past. */
2503 if (pthread_mutex_trylock (&receive_list_lock) == 0)
2505 assert (((receive_list_head == NULL) && (receive_list_length == 0))
2506 || ((receive_list_head != NULL) && (receive_list_length != 0)));
2508 if (receive_list_head == NULL)
2509 receive_list_head = private_list_head;
2511 receive_list_tail->next = private_list_head;
2512 receive_list_tail = private_list_tail;
2513 receive_list_length += private_list_length;
2515 pthread_cond_signal (&receive_list_cond);
2516 pthread_mutex_unlock (&receive_list_lock);
2518 private_list_head = NULL;
2519 private_list_tail = NULL;
2520 private_list_length = 0;
2524 } /* for (listen_sockets_pollfd) */
2528 } /* while (listen_loop == 0) */
2530 /* Make sure everything is dispatched before exiting. */
2531 if (private_list_head != NULL)
2533 pthread_mutex_lock (&receive_list_lock);
2535 if (receive_list_head == NULL)
2536 receive_list_head = private_list_head;
2538 receive_list_tail->next = private_list_head;
2539 receive_list_tail = private_list_tail;
2540 receive_list_length += private_list_length;
2542 pthread_cond_signal (&receive_list_cond);
2543 pthread_mutex_unlock (&receive_list_lock);
2547 } /* }}} int network_receive */
2549 static void *receive_thread (void __attribute__((unused)) *arg)
2551 return (network_receive () ? (void *) 1 : (void *) 0);
2552 } /* void *receive_thread */
2554 static void network_init_buffer (void)
2556 memset (send_buffer, 0, network_config_packet_size);
2557 send_buffer_ptr = send_buffer;
2558 send_buffer_fill = 0;
2560 memset (&send_buffer_vl, 0, sizeof (send_buffer_vl));
2561 } /* int network_init_buffer */
2563 static void networt_send_buffer_plain (sockent_t *se, /* {{{ */
2564 const char *buffer, size_t buffer_size)
2570 status = sockent_client_connect (se);
2574 status = sendto (se->data.client.fd, buffer, buffer_size,
2576 (struct sockaddr *) se->data.client.addr,
2577 se->data.client.addrlen);
2582 if ((errno == EINTR) || (errno == EAGAIN))
2585 ERROR ("network plugin: sendto failed: %s. Closing sending socket.",
2586 sstrerror (errno, errbuf, sizeof (errbuf)));
2587 sockent_client_disconnect (se);
2593 } /* }}} void networt_send_buffer_plain */
2596 #define BUFFER_ADD(p,s) do { \
2597 memcpy (buffer + buffer_offset, (p), (s)); \
2598 buffer_offset += (s); \
2601 static void networt_send_buffer_signed (sockent_t *se, /* {{{ */
2602 const char *in_buffer, size_t in_buffer_size)
2604 part_signature_sha256_t ps;
2605 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2606 size_t buffer_offset;
2607 size_t username_len;
2611 unsigned char *hash;
2614 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
2617 ERROR ("network plugin: Creating HMAC object failed: %s",
2618 gcry_strerror (err));
2622 err = gcry_md_setkey (hd, se->data.client.password,
2623 strlen (se->data.client.password));
2626 ERROR ("network plugin: gcry_md_setkey failed: %s",
2627 gcry_strerror (err));
2632 username_len = strlen (se->data.client.username);
2633 if (username_len > (BUFF_SIG_SIZE - PART_SIGNATURE_SHA256_SIZE))
2635 ERROR ("network plugin: Username too long: %s",
2636 se->data.client.username);
2640 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE,
2641 se->data.client.username, username_len);
2642 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE + username_len,
2643 in_buffer, in_buffer_size);
2645 /* Initialize the `ps' structure. */
2646 memset (&ps, 0, sizeof (ps));
2647 ps.head.type = htons (TYPE_SIGN_SHA256);
2648 ps.head.length = htons (PART_SIGNATURE_SHA256_SIZE + username_len);
2650 /* Calculate the hash value. */
2651 gcry_md_write (hd, buffer + PART_SIGNATURE_SHA256_SIZE,
2652 username_len + in_buffer_size);
2653 hash = gcry_md_read (hd, GCRY_MD_SHA256);
2656 ERROR ("network plugin: gcry_md_read failed.");
2660 memcpy (ps.hash, hash, sizeof (ps.hash));
2662 /* Add the header */
2665 BUFFER_ADD (&ps.head.type, sizeof (ps.head.type));
2666 BUFFER_ADD (&ps.head.length, sizeof (ps.head.length));
2667 BUFFER_ADD (ps.hash, sizeof (ps.hash));
2669 assert (buffer_offset == PART_SIGNATURE_SHA256_SIZE);
2674 buffer_offset = PART_SIGNATURE_SHA256_SIZE + username_len + in_buffer_size;
2675 networt_send_buffer_plain (se, buffer, buffer_offset);
2676 } /* }}} void networt_send_buffer_signed */
2678 static void networt_send_buffer_encrypted (sockent_t *se, /* {{{ */
2679 const char *in_buffer, size_t in_buffer_size)
2681 part_encryption_aes256_t pea;
2682 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2684 size_t buffer_offset;
2686 size_t username_len;
2688 gcry_cipher_hd_t cypher;
2690 /* Initialize the header fields */
2691 memset (&pea, 0, sizeof (pea));
2692 pea.head.type = htons (TYPE_ENCR_AES256);
2694 pea.username = se->data.client.username;
2696 username_len = strlen (pea.username);
2697 if ((PART_ENCRYPTION_AES256_SIZE + username_len) > BUFF_SIG_SIZE)
2699 ERROR ("network plugin: Username too long: %s", pea.username);
2703 buffer_size = PART_ENCRYPTION_AES256_SIZE + username_len + in_buffer_size;
2704 header_size = PART_ENCRYPTION_AES256_SIZE + username_len
2705 - sizeof (pea.hash);
2707 assert (buffer_size <= sizeof (buffer));
2708 DEBUG ("network plugin: networt_send_buffer_encrypted: "
2709 "buffer_size = %zu;", buffer_size);
2711 pea.head.length = htons ((uint16_t) (PART_ENCRYPTION_AES256_SIZE
2712 + username_len + in_buffer_size));
2713 pea.username_length = htons ((uint16_t) username_len);
2715 /* Chose a random initialization vector. */
2716 gcry_randomize ((void *) &pea.iv, sizeof (pea.iv), GCRY_STRONG_RANDOM);
2718 /* Create hash of the payload */
2719 gcry_md_hash_buffer (GCRY_MD_SHA1, pea.hash, in_buffer, in_buffer_size);
2721 /* Initialize the buffer */
2723 memset (buffer, 0, sizeof (buffer));
2726 BUFFER_ADD (&pea.head.type, sizeof (pea.head.type));
2727 BUFFER_ADD (&pea.head.length, sizeof (pea.head.length));
2728 BUFFER_ADD (&pea.username_length, sizeof (pea.username_length));
2729 BUFFER_ADD (pea.username, username_len);
2730 BUFFER_ADD (pea.iv, sizeof (pea.iv));
2731 assert (buffer_offset == header_size);
2732 BUFFER_ADD (pea.hash, sizeof (pea.hash));
2733 BUFFER_ADD (in_buffer, in_buffer_size);
2735 assert (buffer_offset == buffer_size);
2737 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
2738 se->data.client.password);
2742 /* Encrypt the buffer in-place */
2743 err = gcry_cipher_encrypt (cypher,
2744 buffer + header_size,
2745 buffer_size - header_size,
2746 /* in = */ NULL, /* in len = */ 0);
2749 ERROR ("network plugin: gcry_cipher_encrypt returned: %s",
2750 gcry_strerror (err));
2754 /* Send it out without further modifications */
2755 networt_send_buffer_plain (se, buffer, buffer_size);
2756 } /* }}} void networt_send_buffer_encrypted */
2758 #endif /* HAVE_LIBGCRYPT */
2760 static void network_send_buffer (char *buffer, size_t buffer_len) /* {{{ */
2764 DEBUG ("network plugin: network_send_buffer: buffer_len = %zu", buffer_len);
2766 for (se = sending_sockets; se != NULL; se = se->next)
2769 if (se->data.client.security_level == SECURITY_LEVEL_ENCRYPT)
2770 networt_send_buffer_encrypted (se, buffer, buffer_len);
2771 else if (se->data.client.security_level == SECURITY_LEVEL_SIGN)
2772 networt_send_buffer_signed (se, buffer, buffer_len);
2773 else /* if (se->data.client.security_level == SECURITY_LEVEL_NONE) */
2774 #endif /* HAVE_LIBGCRYPT */
2775 networt_send_buffer_plain (se, buffer, buffer_len);
2776 } /* for (sending_sockets) */
2777 } /* }}} void network_send_buffer */
2779 static int add_to_buffer (char *buffer, int buffer_size, /* {{{ */
2780 value_list_t *vl_def,
2781 const data_set_t *ds, const value_list_t *vl)
2783 char *buffer_orig = buffer;
2785 if (strcmp (vl_def->host, vl->host) != 0)
2787 if (write_part_string (&buffer, &buffer_size, TYPE_HOST,
2788 vl->host, strlen (vl->host)) != 0)
2790 sstrncpy (vl_def->host, vl->host, sizeof (vl_def->host));
2793 if (vl_def->time != vl->time)
2795 if (write_part_number (&buffer, &buffer_size, TYPE_TIME_HR,
2796 (uint64_t) vl->time))
2798 vl_def->time = vl->time;
2801 if (vl_def->interval != vl->interval)
2803 if (write_part_number (&buffer, &buffer_size, TYPE_INTERVAL_HR,
2804 (uint64_t) vl->interval))
2806 vl_def->interval = vl->interval;
2809 if (strcmp (vl_def->plugin, vl->plugin) != 0)
2811 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN,
2812 vl->plugin, strlen (vl->plugin)) != 0)
2814 sstrncpy (vl_def->plugin, vl->plugin, sizeof (vl_def->plugin));
2817 if (strcmp (vl_def->plugin_instance, vl->plugin_instance) != 0)
2819 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
2820 vl->plugin_instance,
2821 strlen (vl->plugin_instance)) != 0)
2823 sstrncpy (vl_def->plugin_instance, vl->plugin_instance, sizeof (vl_def->plugin_instance));
2826 if (strcmp (vl_def->type, vl->type) != 0)
2828 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
2829 vl->type, strlen (vl->type)) != 0)
2831 sstrncpy (vl_def->type, ds->type, sizeof (vl_def->type));
2834 if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
2836 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE_INSTANCE,
2838 strlen (vl->type_instance)) != 0)
2840 sstrncpy (vl_def->type_instance, vl->type_instance, sizeof (vl_def->type_instance));
2843 if (write_part_values (&buffer, &buffer_size, ds, vl) != 0)
2846 return (buffer - buffer_orig);
2847 } /* }}} int add_to_buffer */
2849 static void flush_buffer (void)
2851 DEBUG ("network plugin: flush_buffer: send_buffer_fill = %i",
2854 network_send_buffer (send_buffer, (size_t) send_buffer_fill);
2856 stats_octets_tx += ((uint64_t) send_buffer_fill);
2859 network_init_buffer ();
2862 static int network_write (const data_set_t *ds, const value_list_t *vl,
2863 user_data_t __attribute__((unused)) *user_data)
2867 if (!check_send_okay (vl))
2870 char name[6*DATA_MAX_NAME_LEN];
2871 FORMAT_VL (name, sizeof (name), vl);
2872 name[sizeof (name) - 1] = 0;
2873 DEBUG ("network plugin: network_write: "
2874 "NOT sending %s.", name);
2876 /* Counter is not protected by another lock and may be reached by
2877 * multiple threads */
2878 pthread_mutex_lock (&stats_lock);
2879 stats_values_not_sent++;
2880 pthread_mutex_unlock (&stats_lock);
2884 uc_meta_data_add_unsigned_int (vl,
2885 "network:time_sent", (uint64_t) vl->time);
2887 pthread_mutex_lock (&send_buffer_lock);
2889 status = add_to_buffer (send_buffer_ptr,
2890 network_config_packet_size - (send_buffer_fill + BUFF_SIG_SIZE),
2895 /* status == bytes added to the buffer */
2896 send_buffer_fill += status;
2897 send_buffer_ptr += status;
2899 stats_values_sent++;
2905 status = add_to_buffer (send_buffer_ptr,
2906 network_config_packet_size - (send_buffer_fill + BUFF_SIG_SIZE),
2912 send_buffer_fill += status;
2913 send_buffer_ptr += status;
2915 stats_values_sent++;
2921 ERROR ("network plugin: Unable to append to the "
2922 "buffer for some weird reason");
2924 else if ((network_config_packet_size - send_buffer_fill) < 15)
2929 pthread_mutex_unlock (&send_buffer_lock);
2931 return ((status < 0) ? -1 : 0);
2932 } /* int network_write */
2934 static int network_config_set_boolean (const oconfig_item_t *ci, /* {{{ */
2937 if ((ci->values_num != 1)
2938 || ((ci->values[0].type != OCONFIG_TYPE_BOOLEAN)
2939 && (ci->values[0].type != OCONFIG_TYPE_STRING)))
2941 ERROR ("network plugin: The `%s' config option needs "
2942 "exactly one boolean argument.", ci->key);
2946 if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
2948 if (ci->values[0].value.boolean)
2955 char *str = ci->values[0].value.string;
2959 else if (IS_FALSE (str))
2963 ERROR ("network plugin: Cannot parse string value `%s' of the `%s' "
2964 "option as boolean value.",
2971 } /* }}} int network_config_set_boolean */
2973 static int network_config_set_ttl (const oconfig_item_t *ci) /* {{{ */
2976 if ((ci->values_num != 1)
2977 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
2979 WARNING ("network plugin: The `TimeToLive' config option needs exactly "
2980 "one numeric argument.");
2984 tmp = (int) ci->values[0].value.number;
2985 if ((tmp > 0) && (tmp <= 255))
2986 network_config_ttl = tmp;
2988 WARNING ("network plugin: The `TimeToLive' must be between 1 and 255.");
2993 } /* }}} int network_config_set_ttl */
2995 static int network_config_set_interface (const oconfig_item_t *ci, /* {{{ */
2998 if ((ci->values_num != 1)
2999 || (ci->values[0].type != OCONFIG_TYPE_STRING))
3001 WARNING ("network plugin: The `Interface' config option needs exactly "
3002 "one string argument.");
3006 if (interface == NULL)
3009 *interface = if_nametoindex (ci->values[0].value.string);
3012 } /* }}} int network_config_set_interface */
3014 static int network_config_set_buffer_size (const oconfig_item_t *ci) /* {{{ */
3017 if ((ci->values_num != 1)
3018 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
3020 WARNING ("network plugin: The `MaxPacketSize' config option needs exactly "
3021 "one numeric argument.");
3025 tmp = (int) ci->values[0].value.number;
3026 if ((tmp >= 1024) && (tmp <= 65535))
3027 network_config_packet_size = tmp;
3030 } /* }}} int network_config_set_buffer_size */
3033 static int network_config_set_string (const oconfig_item_t *ci, /* {{{ */
3037 if ((ci->values_num != 1)
3038 || (ci->values[0].type != OCONFIG_TYPE_STRING))
3040 WARNING ("network plugin: The `%s' config option needs exactly "
3041 "one string argument.", ci->key);
3045 tmp = strdup (ci->values[0].value.string);
3049 sfree (*ret_string);
3053 } /* }}} int network_config_set_string */
3054 #endif /* HAVE_LIBGCRYPT */
3057 static int network_config_set_security_level (oconfig_item_t *ci, /* {{{ */
3061 if ((ci->values_num != 1)
3062 || (ci->values[0].type != OCONFIG_TYPE_STRING))
3064 WARNING ("network plugin: The `SecurityLevel' config option needs exactly "
3065 "one string argument.");
3069 str = ci->values[0].value.string;
3070 if (strcasecmp ("Encrypt", str) == 0)
3071 *retval = SECURITY_LEVEL_ENCRYPT;
3072 else if (strcasecmp ("Sign", str) == 0)
3073 *retval = SECURITY_LEVEL_SIGN;
3074 else if (strcasecmp ("None", str) == 0)
3075 *retval = SECURITY_LEVEL_NONE;
3078 WARNING ("network plugin: Unknown security level: %s.", str);
3083 } /* }}} int network_config_set_security_level */
3084 #endif /* HAVE_LIBGCRYPT */
3086 static int network_config_add_listen (const oconfig_item_t *ci) /* {{{ */
3092 if ((ci->values_num < 1) || (ci->values_num > 2)
3093 || (ci->values[0].type != OCONFIG_TYPE_STRING)
3094 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
3096 ERROR ("network plugin: The `%s' config option needs "
3097 "one or two string arguments.", ci->key);
3101 se = sockent_create (SOCKENT_TYPE_SERVER);
3104 ERROR ("network plugin: sockent_create failed.");
3108 se->node = strdup (ci->values[0].value.string);
3109 if (ci->values_num >= 2)
3110 se->service = strdup (ci->values[1].value.string);
3112 for (i = 0; i < ci->children_num; i++)
3114 oconfig_item_t *child = ci->children + i;
3117 if (strcasecmp ("AuthFile", child->key) == 0)
3118 network_config_set_string (child, &se->data.server.auth_file);
3119 else if (strcasecmp ("SecurityLevel", child->key) == 0)
3120 network_config_set_security_level (child,
3121 &se->data.server.security_level);
3123 #endif /* HAVE_LIBGCRYPT */
3124 if (strcasecmp ("Interface", child->key) == 0)
3125 network_config_set_interface (child,
3129 WARNING ("network plugin: Option `%s' is not allowed here.",
3135 if ((se->data.server.security_level > SECURITY_LEVEL_NONE)
3136 && (se->data.server.auth_file == NULL))
3138 ERROR ("network plugin: A security level higher than `none' was "
3139 "requested, but no AuthFile option was given. Cowardly refusing to "
3140 "open this socket!");
3141 sockent_destroy (se);
3144 #endif /* HAVE_LIBGCRYPT */
3146 status = sockent_init_crypto (se);
3149 ERROR ("network plugin: network_config_add_listen: sockent_init_crypto() failed.");
3150 sockent_destroy (se);
3154 status = sockent_server_listen (se);
3157 ERROR ("network plugin: network_config_add_server: sockent_server_listen failed.");
3158 sockent_destroy (se);
3162 status = sockent_add (se);
3165 ERROR ("network plugin: network_config_add_listen: sockent_add failed.");
3166 sockent_destroy (se);
3171 } /* }}} int network_config_add_listen */
3173 static int network_config_add_server (const oconfig_item_t *ci) /* {{{ */
3179 if ((ci->values_num < 1) || (ci->values_num > 2)
3180 || (ci->values[0].type != OCONFIG_TYPE_STRING)
3181 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
3183 ERROR ("network plugin: The `%s' config option needs "
3184 "one or two string arguments.", ci->key);
3188 se = sockent_create (SOCKENT_TYPE_CLIENT);
3191 ERROR ("network plugin: sockent_create failed.");
3195 se->node = strdup (ci->values[0].value.string);
3196 if (ci->values_num >= 2)
3197 se->service = strdup (ci->values[1].value.string);
3199 for (i = 0; i < ci->children_num; i++)
3201 oconfig_item_t *child = ci->children + i;
3204 if (strcasecmp ("Username", child->key) == 0)
3205 network_config_set_string (child, &se->data.client.username);
3206 else if (strcasecmp ("Password", child->key) == 0)
3207 network_config_set_string (child, &se->data.client.password);
3208 else if (strcasecmp ("SecurityLevel", child->key) == 0)
3209 network_config_set_security_level (child,
3210 &se->data.client.security_level);
3212 #endif /* HAVE_LIBGCRYPT */
3213 if (strcasecmp ("Interface", child->key) == 0)
3214 network_config_set_interface (child,
3218 WARNING ("network plugin: Option `%s' is not allowed here.",
3224 if ((se->data.client.security_level > SECURITY_LEVEL_NONE)
3225 && ((se->data.client.username == NULL)
3226 || (se->data.client.password == NULL)))
3228 ERROR ("network plugin: A security level higher than `none' was "
3229 "requested, but no Username or Password option was given. "
3230 "Cowardly refusing to open this socket!");
3231 sockent_destroy (se);
3234 #endif /* HAVE_LIBGCRYPT */
3236 status = sockent_init_crypto (se);
3239 ERROR ("network plugin: network_config_add_server: sockent_init_crypto() failed.");
3240 sockent_destroy (se);
3244 /* No call to sockent_client_connect() here -- it is called from
3245 * networt_send_buffer_plain(). */
3247 status = sockent_add (se);
3250 ERROR ("network plugin: network_config_add_server: sockent_add failed.");
3251 sockent_destroy (se);
3256 } /* }}} int network_config_add_server */
3258 static int network_config (oconfig_item_t *ci) /* {{{ */
3262 /* The options need to be applied first */
3263 for (i = 0; i < ci->children_num; i++)
3265 oconfig_item_t *child = ci->children + i;
3266 if (strcasecmp ("TimeToLive", child->key) == 0)
3267 network_config_set_ttl (child);
3270 for (i = 0; i < ci->children_num; i++)
3272 oconfig_item_t *child = ci->children + i;
3274 if (strcasecmp ("Listen", child->key) == 0)
3275 network_config_add_listen (child);
3276 else if (strcasecmp ("Server", child->key) == 0)
3277 network_config_add_server (child);
3278 else if (strcasecmp ("TimeToLive", child->key) == 0) {
3279 /* Handled earlier */
3281 else if (strcasecmp ("MaxPacketSize", child->key) == 0)
3282 network_config_set_buffer_size (child);
3283 else if (strcasecmp ("Forward", child->key) == 0)
3284 network_config_set_boolean (child, &network_config_forward);
3285 else if (strcasecmp ("ReportStats", child->key) == 0)
3286 network_config_set_boolean (child, &network_config_stats);
3289 WARNING ("network plugin: Option `%s' is not allowed here.",
3295 } /* }}} int network_config */
3297 static int network_notification (const notification_t *n,
3298 user_data_t __attribute__((unused)) *user_data)
3300 char buffer[network_config_packet_size];
3301 char *buffer_ptr = buffer;
3302 int buffer_free = sizeof (buffer);
3305 if (!check_send_notify_okay (n))
3308 memset (buffer, 0, sizeof (buffer));
3310 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_TIME_HR,
3311 (uint64_t) n->time);
3315 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_SEVERITY,
3316 (uint64_t) n->severity);
3320 if (strlen (n->host) > 0)
3322 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_HOST,
3323 n->host, strlen (n->host));
3328 if (strlen (n->plugin) > 0)
3330 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_PLUGIN,
3331 n->plugin, strlen (n->plugin));
3336 if (strlen (n->plugin_instance) > 0)
3338 status = write_part_string (&buffer_ptr, &buffer_free,
3339 TYPE_PLUGIN_INSTANCE,
3340 n->plugin_instance, strlen (n->plugin_instance));
3345 if (strlen (n->type) > 0)
3347 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE,
3348 n->type, strlen (n->type));
3353 if (strlen (n->type_instance) > 0)
3355 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE_INSTANCE,
3356 n->type_instance, strlen (n->type_instance));
3361 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_MESSAGE,
3362 n->message, strlen (n->message));
3366 network_send_buffer (buffer, sizeof (buffer) - buffer_free);
3369 } /* int network_notification */
3371 static int network_shutdown (void)
3377 /* Kill the listening thread */
3378 if (receive_thread_running != 0)
3380 INFO ("network plugin: Stopping receive thread.");
3381 pthread_kill (receive_thread_id, SIGTERM);
3382 pthread_join (receive_thread_id, NULL /* no return value */);
3383 memset (&receive_thread_id, 0, sizeof (receive_thread_id));
3384 receive_thread_running = 0;
3387 /* Shutdown the dispatching thread */
3388 if (dispatch_thread_running != 0)
3390 INFO ("network plugin: Stopping dispatch thread.");
3391 pthread_mutex_lock (&receive_list_lock);
3392 pthread_cond_broadcast (&receive_list_cond);
3393 pthread_mutex_unlock (&receive_list_lock);
3394 pthread_join (dispatch_thread_id, /* ret = */ NULL);
3395 dispatch_thread_running = 0;
3398 sockent_destroy (listen_sockets);
3400 if (send_buffer_fill > 0)
3403 sfree (send_buffer);
3405 for (se = sending_sockets; se != NULL; se = se->next)
3406 sockent_client_disconnect (se);
3407 sockent_destroy (sending_sockets);
3409 plugin_unregister_config ("network");
3410 plugin_unregister_init ("network");
3411 plugin_unregister_write ("network");
3412 plugin_unregister_shutdown ("network");
3415 } /* int network_shutdown */
3417 static int network_stats_read (void) /* {{{ */
3419 derive_t copy_octets_rx;
3420 derive_t copy_octets_tx;
3421 derive_t copy_packets_rx;
3422 derive_t copy_packets_tx;
3423 derive_t copy_values_dispatched;
3424 derive_t copy_values_not_dispatched;
3425 derive_t copy_values_sent;
3426 derive_t copy_values_not_sent;
3427 derive_t copy_receive_list_length;
3428 value_list_t vl = VALUE_LIST_INIT;
3431 copy_octets_rx = stats_octets_rx;
3432 copy_octets_tx = stats_octets_tx;
3433 copy_packets_rx = stats_packets_rx;
3434 copy_packets_tx = stats_packets_tx;
3435 copy_values_dispatched = stats_values_dispatched;
3436 copy_values_not_dispatched = stats_values_not_dispatched;
3437 copy_values_sent = stats_values_sent;
3438 copy_values_not_sent = stats_values_not_sent;
3439 copy_receive_list_length = receive_list_length;
3441 /* Initialize `vl' */
3445 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
3446 sstrncpy (vl.plugin, "network", sizeof (vl.plugin));
3448 /* Octets received / sent */
3449 vl.values[0].derive = (derive_t) copy_octets_rx;
3450 vl.values[1].derive = (derive_t) copy_octets_tx;
3451 sstrncpy (vl.type, "if_octets", sizeof (vl.type));
3452 plugin_dispatch_values (&vl);
3454 /* Packets received / send */
3455 vl.values[0].derive = (derive_t) copy_packets_rx;
3456 vl.values[1].derive = (derive_t) copy_packets_tx;
3457 sstrncpy (vl.type, "if_packets", sizeof (vl.type));
3458 plugin_dispatch_values (&vl);
3460 /* Values (not) dispatched and (not) send */
3461 sstrncpy (vl.type, "total_values", sizeof (vl.type));
3464 vl.values[0].derive = (derive_t) copy_values_dispatched;
3465 sstrncpy (vl.type_instance, "dispatch-accepted",
3466 sizeof (vl.type_instance));
3467 plugin_dispatch_values (&vl);
3469 vl.values[0].derive = (derive_t) copy_values_not_dispatched;
3470 sstrncpy (vl.type_instance, "dispatch-rejected",
3471 sizeof (vl.type_instance));
3472 plugin_dispatch_values (&vl);
3474 vl.values[0].derive = (derive_t) copy_values_sent;
3475 sstrncpy (vl.type_instance, "send-accepted",
3476 sizeof (vl.type_instance));
3477 plugin_dispatch_values (&vl);
3479 vl.values[0].derive = (derive_t) copy_values_not_sent;
3480 sstrncpy (vl.type_instance, "send-rejected",
3481 sizeof (vl.type_instance));
3482 plugin_dispatch_values (&vl);
3484 /* Receive queue length */
3485 vl.values[0].gauge = (gauge_t) copy_receive_list_length;
3486 sstrncpy (vl.type, "queue_length", sizeof (vl.type));
3487 vl.type_instance[0] = 0;
3488 plugin_dispatch_values (&vl);
3491 } /* }}} int network_stats_read */
3493 static int network_init (void)
3495 static _Bool have_init = 0;
3497 /* Check if we were already initialized. If so, just return - there's
3498 * nothing more to do (for now, that is). */
3504 network_init_gcrypt ();
3507 if (network_config_stats != 0)
3508 plugin_register_read ("network", network_stats_read);
3510 plugin_register_shutdown ("network", network_shutdown);
3512 send_buffer = malloc (network_config_packet_size);
3513 if (send_buffer == NULL)
3515 ERROR ("network plugin: malloc failed.");
3518 network_init_buffer ();
3520 /* setup socket(s) and so on */
3521 if (sending_sockets != NULL)
3523 plugin_register_write ("network", network_write,
3524 /* user_data = */ NULL);
3525 plugin_register_notification ("network", network_notification,
3526 /* user_data = */ NULL);
3529 /* If no threads need to be started, return here. */
3530 if ((listen_sockets_num == 0)
3531 || ((dispatch_thread_running != 0)
3532 && (receive_thread_running != 0)))
3535 if (dispatch_thread_running == 0)
3538 status = plugin_thread_create (&dispatch_thread_id,
3539 NULL /* no attributes */,
3541 NULL /* no argument */);
3545 ERROR ("network: pthread_create failed: %s",
3546 sstrerror (errno, errbuf,
3551 dispatch_thread_running = 1;
3555 if (receive_thread_running == 0)
3558 status = plugin_thread_create (&receive_thread_id,
3559 NULL /* no attributes */,
3561 NULL /* no argument */);
3565 ERROR ("network: pthread_create failed: %s",
3566 sstrerror (errno, errbuf,
3571 receive_thread_running = 1;
3576 } /* int network_init */
3579 * The flush option of the network plugin cannot flush individual identifiers.
3580 * All the values are added to a buffer and sent when the buffer is full, the
3581 * requested value may or may not be in there, it's not worth finding out. We
3582 * just send the buffer if `flush' is called - if the requested value was in
3583 * there, good. If not, well, then there is nothing to flush.. -octo
3585 static int network_flush (__attribute__((unused)) cdtime_t timeout,
3586 __attribute__((unused)) const char *identifier,
3587 __attribute__((unused)) user_data_t *user_data)
3589 pthread_mutex_lock (&send_buffer_lock);
3591 if (send_buffer_fill > 0)
3594 pthread_mutex_unlock (&send_buffer_lock);
3597 } /* int network_flush */
3599 void module_register (void)
3601 plugin_register_complex_config ("network", network_config);
3602 plugin_register_init ("network", network_init);
3603 plugin_register_flush ("network", network_flush,
3604 /* user_data = */ NULL);
3605 } /* void module_register */
3607 /* vim: set fdm=marker : */