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 _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"
34 #include "utils_complain.h"
42 # include <sys/socket.h>
48 # include <netinet/in.h>
51 # include <arpa/inet.h>
62 # if defined __APPLE__
63 /* default xcode compiler throws warnings even when deprecated functionality
64 * is not used. -Werror breaks the build because of erroneous warnings.
65 * http://stackoverflow.com/questions/10556299/compiler-warnings-with-libgcrypt-v1-5-0/12830209#12830209
67 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
69 /* FreeBSD's copy of libgcrypt extends the existing GCRYPT_NO_DEPRECATED
70 * to properly hide all deprecated functionality.
71 * http://svnweb.freebsd.org/ports/head/security/libgcrypt/files/patch-src__gcrypt.h.in
73 # define GCRYPT_NO_DEPRECATED
75 # if defined __APPLE__
76 /* Re enable deprecation warnings */
77 # pragma GCC diagnostic warning "-Wdeprecated-declarations"
79 # if GCRYPT_VERSION_NUMBER < 0x010600
80 GCRY_THREAD_OPTION_PTHREAD_IMPL;
84 #ifndef IPV6_ADD_MEMBERSHIP
85 # ifdef IPV6_JOIN_GROUP
86 # define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
88 # error "Neither IP_ADD_MEMBERSHIP nor IPV6_JOIN_GROUP is defined"
90 #endif /* !IP_ADD_MEMBERSHIP */
93 * Maximum size required for encryption / signing:
95 * 42 bytes for the encryption header
96 * + 64 bytes for the username
100 #define BUFF_SIG_SIZE 106
105 #define SECURITY_LEVEL_NONE 0
107 # define SECURITY_LEVEL_SIGN 1
108 # define SECURITY_LEVEL_ENCRYPT 2
110 struct sockent_client
113 struct sockaddr_storage *addr;
119 gcry_cipher_hd_t cypher;
120 unsigned char password_hash[32];
124 struct sockent_server
132 gcry_cipher_hd_t cypher;
136 typedef struct sockent
138 #define SOCKENT_TYPE_CLIENT 1
139 #define SOCKENT_TYPE_SERVER 2
148 struct sockent_client client;
149 struct sockent_server server;
152 struct sockent *next;
155 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
156 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
157 * +-------+-----------------------+-------------------------------+
158 * ! Ver. ! ! Length !
159 * +-------+-----------------------+-------------------------------+
166 typedef struct part_header_s part_header_t;
168 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
169 * 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
170 * +-------------------------------+-------------------------------+
172 * +-------------------------------+-------------------------------+
173 * : (Length - 4) Bytes :
174 * +---------------------------------------------------------------+
181 typedef struct part_string_s part_string_t;
183 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
184 * 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
185 * +-------------------------------+-------------------------------+
187 * +-------------------------------+-------------------------------+
188 * : (Length - 4 == 2 || 4 || 8) Bytes :
189 * +---------------------------------------------------------------+
196 typedef struct part_number_s part_number_t;
198 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
199 * 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
200 * +-------------------------------+-------------------------------+
202 * +-------------------------------+---------------+---------------+
203 * ! Num of values ! Type0 ! Type1 !
204 * +-------------------------------+---------------+---------------+
207 * +---------------------------------------------------------------+
210 * +---------------------------------------------------------------+
215 uint16_t *num_values;
216 uint8_t *values_types;
219 typedef struct part_values_s part_values_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 * ! Hash (Bits 0 - 31) !
228 * ! Hash (Bits 224 - 255) !
229 * +---------------------------------------------------------------+
232 #define PART_SIGNATURE_SHA256_SIZE 36
233 struct part_signature_sha256_s
236 unsigned char hash[32];
239 typedef struct part_signature_sha256_s part_signature_sha256_t;
241 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
242 * 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
243 * +-------------------------------+-------------------------------+
245 * +-------------------------------+-------------------------------+
246 * ! Original length ! Padding (0 - 15 bytes) !
247 * +-------------------------------+-------------------------------+
248 * ! Hash (Bits 0 - 31) !
250 * ! Hash (Bits 128 - 159) !
251 * +---------------------------------------------------------------+
254 #define PART_ENCRYPTION_AES256_SIZE 42
255 struct part_encryption_aes256_s
258 uint16_t username_length;
260 unsigned char iv[16];
262 unsigned char hash[20];
266 typedef struct part_encryption_aes256_s part_encryption_aes256_t;
268 struct receive_list_entry_s
273 struct receive_list_entry_s *next;
275 typedef struct receive_list_entry_s receive_list_entry_t;
280 static int network_config_ttl = 0;
281 /* Ethernet - (IPv6 + UDP) = 1500 - (40 + 8) = 1452 */
282 static size_t network_config_packet_size = 1452;
283 static int network_config_forward = 0;
284 static int network_config_stats = 0;
286 static sockent_t *sending_sockets = NULL;
288 static receive_list_entry_t *receive_list_head = NULL;
289 static receive_list_entry_t *receive_list_tail = NULL;
290 static pthread_mutex_t receive_list_lock = PTHREAD_MUTEX_INITIALIZER;
291 static pthread_cond_t receive_list_cond = PTHREAD_COND_INITIALIZER;
292 static uint64_t receive_list_length = 0;
294 static sockent_t *listen_sockets = NULL;
295 static struct pollfd *listen_sockets_pollfd = NULL;
296 static size_t listen_sockets_num = 0;
298 /* The receive and dispatch threads will run as long as `listen_loop' is set to
300 static int listen_loop = 0;
301 static int receive_thread_running = 0;
302 static pthread_t receive_thread_id;
303 static int dispatch_thread_running = 0;
304 static pthread_t dispatch_thread_id;
306 /* Buffer in which to-be-sent network packets are constructed. */
307 static char *send_buffer;
308 static char *send_buffer_ptr;
309 static int send_buffer_fill;
310 static value_list_t send_buffer_vl = VALUE_LIST_STATIC;
311 static pthread_mutex_t send_buffer_lock = PTHREAD_MUTEX_INITIALIZER;
313 /* XXX: These counters are incremented from one place only. The spot in which
314 * the values are incremented is either only reachable by one thread (the
315 * dispatch thread, for example) or locked by some lock (send_buffer_lock for
316 * example). Only if neither is true, the stats_lock is acquired. The counters
317 * are always read without holding a lock in the hope that writing 8 bytes to
318 * memory is an atomic operation. */
319 static derive_t stats_octets_rx = 0;
320 static derive_t stats_octets_tx = 0;
321 static derive_t stats_packets_rx = 0;
322 static derive_t stats_packets_tx = 0;
323 static derive_t stats_values_dispatched = 0;
324 static derive_t stats_values_not_dispatched = 0;
325 static derive_t stats_values_sent = 0;
326 static derive_t stats_values_not_sent = 0;
327 static pthread_mutex_t stats_lock = PTHREAD_MUTEX_INITIALIZER;
332 static _Bool check_receive_okay (const value_list_t *vl) /* {{{ */
334 uint64_t time_sent = 0;
337 status = uc_meta_data_get_unsigned_int (vl,
338 "network:time_sent", &time_sent);
340 /* This is a value we already sent. Don't allow it to be received again in
341 * order to avoid looping. */
342 if ((status == 0) && (time_sent >= ((uint64_t) vl->time)))
346 } /* }}} _Bool check_receive_okay */
348 static _Bool check_send_okay (const value_list_t *vl) /* {{{ */
353 if (network_config_forward != 0)
356 if (vl->meta == NULL)
359 status = meta_data_get_boolean (vl->meta, "network:received", &received);
360 if (status == -ENOENT)
362 else if (status != 0)
364 ERROR ("network plugin: check_send_okay: meta_data_get_boolean failed "
365 "with status %i.", status);
369 /* By default, only *send* value lists that were not *received* by the
372 } /* }}} _Bool check_send_okay */
374 static _Bool check_notify_received (const notification_t *n) /* {{{ */
376 notification_meta_t *ptr;
378 for (ptr = n->meta; ptr != NULL; ptr = ptr->next)
379 if ((strcmp ("network:received", ptr->name) == 0)
380 && (ptr->type == NM_TYPE_BOOLEAN))
381 return ((_Bool) ptr->nm_value.nm_boolean);
384 } /* }}} _Bool check_notify_received */
386 static _Bool check_send_notify_okay (const notification_t *n) /* {{{ */
388 static c_complain_t complain_forwarding = C_COMPLAIN_INIT_STATIC;
394 received = check_notify_received (n);
396 if (network_config_forward && received)
398 c_complain_once (LOG_ERR, &complain_forwarding,
399 "network plugin: A notification has been received via the network "
400 "and forwarding is enabled. Forwarding of notifications is currently "
401 "not supported, because there is not loop-deteciton available. "
402 "Please contact the collectd mailing list if you need this "
406 /* By default, only *send* value lists that were not *received* by the
409 } /* }}} _Bool check_send_notify_okay */
411 static int network_dispatch_values (value_list_t *vl, /* {{{ */
412 const char *username)
417 || (strlen (vl->host) <= 0)
418 || (strlen (vl->plugin) <= 0)
419 || (strlen (vl->type) <= 0))
422 if (!check_receive_okay (vl))
425 char name[6*DATA_MAX_NAME_LEN];
426 FORMAT_VL (name, sizeof (name), vl);
427 name[sizeof (name) - 1] = 0;
428 DEBUG ("network plugin: network_dispatch_values: "
429 "NOT dispatching %s.", name);
431 stats_values_not_dispatched++;
435 assert (vl->meta == NULL);
437 vl->meta = meta_data_create ();
438 if (vl->meta == NULL)
440 ERROR ("network plugin: meta_data_create failed.");
444 status = meta_data_add_boolean (vl->meta, "network:received", 1);
447 ERROR ("network plugin: meta_data_add_boolean failed.");
448 meta_data_destroy (vl->meta);
453 if (username != NULL)
455 status = meta_data_add_string (vl->meta, "network:username", username);
458 ERROR ("network plugin: meta_data_add_string failed.");
459 meta_data_destroy (vl->meta);
465 plugin_dispatch_values (vl);
466 stats_values_dispatched++;
468 meta_data_destroy (vl->meta);
472 } /* }}} int network_dispatch_values */
474 static int network_dispatch_notification (notification_t *n) /* {{{ */
478 assert (n->meta == NULL);
480 status = plugin_notification_meta_add_boolean (n, "network:received", 1);
483 ERROR ("network plugin: plugin_notification_meta_add_boolean failed.");
484 plugin_notification_meta_free (n->meta);
489 status = plugin_dispatch_notification (n);
491 plugin_notification_meta_free (n->meta);
495 } /* }}} int network_dispatch_notification */
498 static void network_init_gcrypt (void) /* {{{ */
500 /* http://lists.gnupg.org/pipermail/gcrypt-devel/2003-August/000458.html
501 * Because you can't know in a library whether another library has
502 * already initialized the library */
503 if (gcry_control (GCRYCTL_ANY_INITIALIZATION_P))
506 /* http://www.gnupg.org/documentation/manuals/gcrypt/Multi_002dThreading.html
507 * To ensure thread-safety, it's important to set GCRYCTL_SET_THREAD_CBS
508 * *before* initalizing Libgcrypt with gcry_check_version(), which itself must
509 * be called before any other gcry_* function. GCRYCTL_ANY_INITIALIZATION_P
510 * above doesn't count, as it doesn't implicitly initalize Libgcrypt.
512 * tl;dr: keep all these gry_* statements in this exact order please. */
513 # if GCRYPT_VERSION_NUMBER < 0x010600
514 gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
516 gcry_check_version (NULL);
517 gcry_control (GCRYCTL_INIT_SECMEM, 32768);
518 gcry_control (GCRYCTL_INITIALIZATION_FINISHED);
519 } /* }}} void network_init_gcrypt */
521 static gcry_cipher_hd_t network_get_aes256_cypher (sockent_t *se, /* {{{ */
522 const void *iv, size_t iv_size, const char *username)
525 gcry_cipher_hd_t *cyper_ptr;
526 unsigned char password_hash[32];
528 if (se->type == SOCKENT_TYPE_CLIENT)
530 cyper_ptr = &se->data.client.cypher;
531 memcpy (password_hash, se->data.client.password_hash,
532 sizeof (password_hash));
538 cyper_ptr = &se->data.server.cypher;
540 if (username == NULL)
543 secret = fbh_get (se->data.server.userdb, username);
547 gcry_md_hash_buffer (GCRY_MD_SHA256,
549 secret, strlen (secret));
554 if (*cyper_ptr == NULL)
556 err = gcry_cipher_open (cyper_ptr,
557 GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_OFB, /* flags = */ 0);
560 ERROR ("network plugin: gcry_cipher_open returned: %s",
561 gcry_strerror (err));
568 gcry_cipher_reset (*cyper_ptr);
570 assert (*cyper_ptr != NULL);
572 err = gcry_cipher_setkey (*cyper_ptr,
573 password_hash, sizeof (password_hash));
576 ERROR ("network plugin: gcry_cipher_setkey returned: %s",
577 gcry_strerror (err));
578 gcry_cipher_close (*cyper_ptr);
583 err = gcry_cipher_setiv (*cyper_ptr, iv, iv_size);
586 ERROR ("network plugin: gcry_cipher_setkey returned: %s",
587 gcry_strerror (err));
588 gcry_cipher_close (*cyper_ptr);
594 } /* }}} int network_get_aes256_cypher */
595 #endif /* HAVE_LIBGCRYPT */
597 static int write_part_values (char **ret_buffer, int *ret_buffer_len,
598 const data_set_t *ds, const value_list_t *vl)
604 part_header_t pkg_ph;
605 uint16_t pkg_num_values;
606 uint8_t *pkg_values_types;
612 num_values = vl->values_len;
613 packet_len = sizeof (part_header_t) + sizeof (uint16_t)
614 + (num_values * sizeof (uint8_t))
615 + (num_values * sizeof (value_t));
617 if (*ret_buffer_len < packet_len)
620 pkg_values_types = (uint8_t *) malloc (num_values * sizeof (uint8_t));
621 if (pkg_values_types == NULL)
623 ERROR ("network plugin: write_part_values: malloc failed.");
627 pkg_values = (value_t *) malloc (num_values * sizeof (value_t));
628 if (pkg_values == NULL)
630 free (pkg_values_types);
631 ERROR ("network plugin: write_part_values: malloc failed.");
635 pkg_ph.type = htons (TYPE_VALUES);
636 pkg_ph.length = htons (packet_len);
638 pkg_num_values = htons ((uint16_t) vl->values_len);
640 for (i = 0; i < num_values; i++)
642 pkg_values_types[i] = (uint8_t) ds->ds[i].type;
643 switch (ds->ds[i].type)
645 case DS_TYPE_COUNTER:
646 pkg_values[i].counter = htonll (vl->values[i].counter);
650 pkg_values[i].gauge = htond (vl->values[i].gauge);
654 pkg_values[i].derive = htonll (vl->values[i].derive);
657 case DS_TYPE_ABSOLUTE:
658 pkg_values[i].absolute = htonll (vl->values[i].absolute);
662 free (pkg_values_types);
664 ERROR ("network plugin: write_part_values: "
665 "Unknown data source type: %i",
668 } /* switch (ds->ds[i].type) */
669 } /* for (num_values) */
672 * Use `memcpy' to write everything to the buffer, because the pointer
673 * may be unaligned and some architectures, such as SPARC, can't handle
676 packet_ptr = *ret_buffer;
678 memcpy (packet_ptr + offset, &pkg_ph, sizeof (pkg_ph));
679 offset += sizeof (pkg_ph);
680 memcpy (packet_ptr + offset, &pkg_num_values, sizeof (pkg_num_values));
681 offset += sizeof (pkg_num_values);
682 memcpy (packet_ptr + offset, pkg_values_types, num_values * sizeof (uint8_t));
683 offset += num_values * sizeof (uint8_t);
684 memcpy (packet_ptr + offset, pkg_values, num_values * sizeof (value_t));
685 offset += num_values * sizeof (value_t);
687 assert (offset == packet_len);
689 *ret_buffer = packet_ptr + packet_len;
690 *ret_buffer_len -= packet_len;
692 free (pkg_values_types);
696 } /* int write_part_values */
698 static int write_part_number (char **ret_buffer, int *ret_buffer_len,
699 int type, uint64_t value)
704 part_header_t pkg_head;
709 packet_len = sizeof (pkg_head) + sizeof (pkg_value);
711 if (*ret_buffer_len < packet_len)
714 pkg_head.type = htons (type);
715 pkg_head.length = htons (packet_len);
716 pkg_value = htonll (value);
718 packet_ptr = *ret_buffer;
720 memcpy (packet_ptr + offset, &pkg_head, sizeof (pkg_head));
721 offset += sizeof (pkg_head);
722 memcpy (packet_ptr + offset, &pkg_value, sizeof (pkg_value));
723 offset += sizeof (pkg_value);
725 assert (offset == packet_len);
727 *ret_buffer = packet_ptr + packet_len;
728 *ret_buffer_len -= packet_len;
731 } /* int write_part_number */
733 static int write_part_string (char **ret_buffer, int *ret_buffer_len,
734 int type, const char *str, int str_len)
744 buffer_len = 2 * sizeof (uint16_t) + str_len + 1;
745 if (*ret_buffer_len < buffer_len)
748 pkg_type = htons (type);
749 pkg_length = htons (buffer_len);
751 buffer = *ret_buffer;
753 memcpy (buffer + offset, (void *) &pkg_type, sizeof (pkg_type));
754 offset += sizeof (pkg_type);
755 memcpy (buffer + offset, (void *) &pkg_length, sizeof (pkg_length));
756 offset += sizeof (pkg_length);
757 memcpy (buffer + offset, str, str_len);
759 memset (buffer + offset, '\0', 1);
762 assert (offset == buffer_len);
764 *ret_buffer = buffer + buffer_len;
765 *ret_buffer_len -= buffer_len;
768 } /* int write_part_string */
770 static int parse_part_values (void **ret_buffer, size_t *ret_buffer_len,
771 value_t **ret_values, int *ret_num_values)
773 char *buffer = *ret_buffer;
774 size_t buffer_len = *ret_buffer_len;
789 NOTICE ("network plugin: packet is too short: "
790 "buffer_len = %zu", buffer_len);
794 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
795 buffer += sizeof (tmp16);
796 pkg_type = ntohs (tmp16);
798 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
799 buffer += sizeof (tmp16);
800 pkg_length = ntohs (tmp16);
802 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
803 buffer += sizeof (tmp16);
804 pkg_numval = ntohs (tmp16);
806 assert (pkg_type == TYPE_VALUES);
808 exp_size = 3 * sizeof (uint16_t)
809 + pkg_numval * (sizeof (uint8_t) + sizeof (value_t));
810 if (buffer_len < exp_size)
812 WARNING ("network plugin: parse_part_values: "
814 "Chunk of size %zu expected, "
815 "but buffer has only %zu bytes left.",
816 exp_size, buffer_len);
820 if (pkg_length != exp_size)
822 WARNING ("network plugin: parse_part_values: "
823 "Length and number of values "
824 "in the packet don't match.");
828 pkg_types = (uint8_t *) malloc (pkg_numval * sizeof (uint8_t));
829 pkg_values = (value_t *) malloc (pkg_numval * sizeof (value_t));
830 if ((pkg_types == NULL) || (pkg_values == NULL))
834 ERROR ("network plugin: parse_part_values: malloc failed.");
838 memcpy ((void *) pkg_types, (void *) buffer, pkg_numval * sizeof (uint8_t));
839 buffer += pkg_numval * sizeof (uint8_t);
840 memcpy ((void *) pkg_values, (void *) buffer, pkg_numval * sizeof (value_t));
841 buffer += pkg_numval * sizeof (value_t);
843 for (i = 0; i < pkg_numval; i++)
845 switch (pkg_types[i])
847 case DS_TYPE_COUNTER:
848 pkg_values[i].counter = (counter_t) ntohll (pkg_values[i].counter);
852 pkg_values[i].gauge = (gauge_t) ntohd (pkg_values[i].gauge);
856 pkg_values[i].derive = (derive_t) ntohll (pkg_values[i].derive);
859 case DS_TYPE_ABSOLUTE:
860 pkg_values[i].absolute = (absolute_t) ntohll (pkg_values[i].absolute);
864 NOTICE ("network plugin: parse_part_values: "
865 "Don't know how to handle data source type %"PRIu8,
870 } /* switch (pkg_types[i]) */
873 *ret_buffer = buffer;
874 *ret_buffer_len = buffer_len - pkg_length;
875 *ret_num_values = pkg_numval;
876 *ret_values = pkg_values;
881 } /* int parse_part_values */
883 static int parse_part_number (void **ret_buffer, size_t *ret_buffer_len,
886 char *buffer = *ret_buffer;
887 size_t buffer_len = *ret_buffer_len;
891 size_t exp_size = 2 * sizeof (uint16_t) + sizeof (uint64_t);
895 if (buffer_len < exp_size)
897 WARNING ("network plugin: parse_part_number: "
899 "Chunk of size %zu expected, "
900 "but buffer has only %zu bytes left.",
901 exp_size, buffer_len);
905 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
906 buffer += sizeof (tmp16);
907 /* pkg_type = ntohs (tmp16); */
909 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
910 buffer += sizeof (tmp16);
911 pkg_length = ntohs (tmp16);
913 memcpy ((void *) &tmp64, buffer, sizeof (tmp64));
914 buffer += sizeof (tmp64);
915 *value = ntohll (tmp64);
917 *ret_buffer = buffer;
918 *ret_buffer_len = buffer_len - pkg_length;
921 } /* int parse_part_number */
923 static int parse_part_string (void **ret_buffer, size_t *ret_buffer_len,
924 char *output, int output_len)
926 char *buffer = *ret_buffer;
927 size_t buffer_len = *ret_buffer_len;
930 size_t header_size = 2 * sizeof (uint16_t);
934 if (buffer_len < header_size)
936 WARNING ("network plugin: parse_part_string: "
938 "Chunk of at least size %zu expected, "
939 "but buffer has only %zu bytes left.",
940 header_size, buffer_len);
944 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
945 buffer += sizeof (tmp16);
946 /* pkg_type = ntohs (tmp16); */
948 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
949 buffer += sizeof (tmp16);
950 pkg_length = ntohs (tmp16);
952 /* Check that packet fits in the input buffer */
953 if (pkg_length > buffer_len)
955 WARNING ("network plugin: parse_part_string: "
957 "Chunk of size %"PRIu16" received, "
958 "but buffer has only %zu bytes left.",
959 pkg_length, buffer_len);
963 /* Check that pkg_length is in the valid range */
964 if (pkg_length <= header_size)
966 WARNING ("network plugin: parse_part_string: "
968 "Header claims this packet is only %hu "
969 "bytes long.", pkg_length);
973 /* Check that the package data fits into the output buffer.
974 * The previous if-statement ensures that:
975 * `pkg_length > header_size' */
977 || ((size_t) output_len < ((size_t) pkg_length - header_size)))
979 WARNING ("network plugin: parse_part_string: "
980 "Output buffer too small.");
984 /* All sanity checks successfull, let's copy the data over */
985 output_len = pkg_length - header_size;
986 memcpy ((void *) output, (void *) buffer, output_len);
987 buffer += output_len;
989 /* For some very weird reason '\0' doesn't do the trick on SPARC in
991 if (output[output_len - 1] != 0)
993 WARNING ("network plugin: parse_part_string: "
994 "Received string does not end "
995 "with a NULL-byte.");
999 *ret_buffer = buffer;
1000 *ret_buffer_len = buffer_len - pkg_length;
1003 } /* int parse_part_string */
1005 /* Forward declaration: parse_part_sign_sha256 and parse_part_encr_aes256 call
1006 * parse_packet and vice versa. */
1007 #define PP_SIGNED 0x01
1008 #define PP_ENCRYPTED 0x02
1009 static int parse_packet (sockent_t *se,
1010 void *buffer, size_t buffer_size, int flags,
1011 const char *username);
1013 #define BUFFER_READ(p,s) do { \
1014 memcpy ((p), buffer + buffer_offset, (s)); \
1015 buffer_offset += (s); \
1019 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
1020 void **ret_buffer, size_t *ret_buffer_len, int flags)
1022 static c_complain_t complain_no_users = C_COMPLAIN_INIT_STATIC;
1026 size_t buffer_offset;
1028 size_t username_len;
1031 part_signature_sha256_t pss;
1032 uint16_t pss_head_length;
1033 char hash[sizeof (pss.hash)];
1037 unsigned char *hash_ptr;
1039 buffer = *ret_buffer;
1040 buffer_len = *ret_buffer_len;
1043 if (se->data.server.userdb == NULL)
1045 c_complain (LOG_NOTICE, &complain_no_users,
1046 "network plugin: Received signed network packet but can't verify it "
1047 "because no user DB has been configured. Will accept it.");
1051 /* Check if the buffer has enough data for this structure. */
1052 if (buffer_len <= PART_SIGNATURE_SHA256_SIZE)
1055 /* Read type and length header */
1056 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
1057 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
1058 pss_head_length = ntohs (pss.head.length);
1060 /* Check if the `pss_head_length' is within bounds. */
1061 if ((pss_head_length <= PART_SIGNATURE_SHA256_SIZE)
1062 || (pss_head_length > buffer_len))
1064 ERROR ("network plugin: HMAC-SHA-256 with invalid length received.");
1068 /* Copy the hash. */
1069 BUFFER_READ (pss.hash, sizeof (pss.hash));
1071 /* Calculate username length (without null byte) and allocate memory */
1072 username_len = pss_head_length - PART_SIGNATURE_SHA256_SIZE;
1073 pss.username = malloc (username_len + 1);
1074 if (pss.username == NULL)
1077 /* Read the username */
1078 BUFFER_READ (pss.username, username_len);
1079 pss.username[username_len] = 0;
1081 assert (buffer_offset == pss_head_length);
1083 /* Query the password */
1084 secret = fbh_get (se->data.server.userdb, pss.username);
1087 ERROR ("network plugin: Unknown user: %s", pss.username);
1088 sfree (pss.username);
1092 /* Create a hash device and check the HMAC */
1094 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
1097 ERROR ("network plugin: Creating HMAC-SHA-256 object failed: %s",
1098 gcry_strerror (err));
1100 sfree (pss.username);
1104 err = gcry_md_setkey (hd, secret, strlen (secret));
1107 ERROR ("network plugin: gcry_md_setkey failed: %s", gcry_strerror (err));
1110 sfree (pss.username);
1115 buffer + PART_SIGNATURE_SHA256_SIZE,
1116 buffer_len - PART_SIGNATURE_SHA256_SIZE);
1117 hash_ptr = gcry_md_read (hd, GCRY_MD_SHA256);
1118 if (hash_ptr == NULL)
1120 ERROR ("network plugin: gcry_md_read failed.");
1123 sfree (pss.username);
1126 memcpy (hash, hash_ptr, sizeof (hash));
1132 if (memcmp (pss.hash, hash, sizeof (pss.hash)) != 0)
1134 WARNING ("network plugin: Verifying HMAC-SHA-256 signature failed: "
1139 parse_packet (se, buffer + buffer_offset, buffer_len - buffer_offset,
1140 flags | PP_SIGNED, pss.username);
1144 sfree (pss.username);
1146 *ret_buffer = buffer + buffer_len;
1147 *ret_buffer_len = 0;
1150 } /* }}} int parse_part_sign_sha256 */
1151 /* #endif HAVE_LIBGCRYPT */
1153 #else /* if !HAVE_LIBGCRYPT */
1154 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
1155 void **ret_buffer, size_t *ret_buffer_size, int flags)
1157 static int warning_has_been_printed = 0;
1161 size_t buffer_offset;
1164 part_signature_sha256_t pss;
1166 buffer = *ret_buffer;
1167 buffer_size = *ret_buffer_size;
1170 if (buffer_size <= PART_SIGNATURE_SHA256_SIZE)
1173 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
1174 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
1175 part_len = ntohs (pss.head.length);
1177 if ((part_len <= PART_SIGNATURE_SHA256_SIZE)
1178 || (part_len > buffer_size))
1181 if (warning_has_been_printed == 0)
1183 WARNING ("network plugin: Received signed packet, but the network "
1184 "plugin was not linked with libgcrypt, so I cannot "
1185 "verify the signature. The packet will be accepted.");
1186 warning_has_been_printed = 1;
1189 parse_packet (se, buffer + part_len, buffer_size - part_len, flags,
1190 /* username = */ NULL);
1192 *ret_buffer = buffer + buffer_size;
1193 *ret_buffer_size = 0;
1196 } /* }}} int parse_part_sign_sha256 */
1197 #endif /* !HAVE_LIBGCRYPT */
1200 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1201 void **ret_buffer, size_t *ret_buffer_len,
1204 char *buffer = *ret_buffer;
1205 size_t buffer_len = *ret_buffer_len;
1208 size_t buffer_offset;
1209 uint16_t username_len;
1210 part_encryption_aes256_t pea;
1211 unsigned char hash[sizeof (pea.hash)];
1213 gcry_cipher_hd_t cypher;
1216 /* Make sure at least the header if available. */
1217 if (buffer_len <= PART_ENCRYPTION_AES256_SIZE)
1219 NOTICE ("network plugin: parse_part_encr_aes256: "
1220 "Discarding short packet.");
1226 /* Copy the unencrypted information into `pea'. */
1227 BUFFER_READ (&pea.head.type, sizeof (pea.head.type));
1228 BUFFER_READ (&pea.head.length, sizeof (pea.head.length));
1230 /* Check the `part size'. */
1231 part_size = ntohs (pea.head.length);
1232 if ((part_size <= PART_ENCRYPTION_AES256_SIZE)
1233 || (part_size > buffer_len))
1235 NOTICE ("network plugin: parse_part_encr_aes256: "
1236 "Discarding part with invalid size.");
1240 /* Read the username */
1241 BUFFER_READ (&username_len, sizeof (username_len));
1242 username_len = ntohs (username_len);
1244 if ((username_len <= 0)
1245 || (username_len > (part_size - (PART_ENCRYPTION_AES256_SIZE + 1))))
1247 NOTICE ("network plugin: parse_part_encr_aes256: "
1248 "Discarding part with invalid username length.");
1252 assert (username_len > 0);
1253 pea.username = malloc (username_len + 1);
1254 if (pea.username == NULL)
1256 BUFFER_READ (pea.username, username_len);
1257 pea.username[username_len] = 0;
1259 /* Last but not least, the initialization vector */
1260 BUFFER_READ (pea.iv, sizeof (pea.iv));
1262 /* Make sure we are at the right position */
1263 assert (buffer_offset == (username_len +
1264 PART_ENCRYPTION_AES256_SIZE - sizeof (pea.hash)));
1266 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
1270 sfree (pea.username);
1274 payload_len = part_size - (PART_ENCRYPTION_AES256_SIZE + username_len);
1275 assert (payload_len > 0);
1277 /* Decrypt the packet in-place */
1278 err = gcry_cipher_decrypt (cypher,
1279 buffer + buffer_offset,
1280 part_size - buffer_offset,
1281 /* in = */ NULL, /* in len = */ 0);
1284 sfree (pea.username);
1285 ERROR ("network plugin: gcry_cipher_decrypt returned: %s",
1286 gcry_strerror (err));
1291 BUFFER_READ (pea.hash, sizeof (pea.hash));
1293 /* Make sure we're at the right position - again */
1294 assert (buffer_offset == (username_len + PART_ENCRYPTION_AES256_SIZE));
1295 assert (buffer_offset == (part_size - payload_len));
1297 /* Check hash sum */
1298 memset (hash, 0, sizeof (hash));
1299 gcry_md_hash_buffer (GCRY_MD_SHA1, hash,
1300 buffer + buffer_offset, payload_len);
1301 if (memcmp (hash, pea.hash, sizeof (hash)) != 0)
1303 sfree (pea.username);
1304 ERROR ("network plugin: Decryption failed: Checksum mismatch.");
1308 parse_packet (se, buffer + buffer_offset, payload_len,
1309 flags | PP_ENCRYPTED, pea.username);
1311 /* XXX: Free pea.username?!? */
1313 /* Update return values */
1314 *ret_buffer = buffer + part_size;
1315 *ret_buffer_len = buffer_len - part_size;
1317 sfree (pea.username);
1320 } /* }}} int parse_part_encr_aes256 */
1321 /* #endif HAVE_LIBGCRYPT */
1323 #else /* if !HAVE_LIBGCRYPT */
1324 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1325 void **ret_buffer, size_t *ret_buffer_size, int flags)
1327 static int warning_has_been_printed = 0;
1331 size_t buffer_offset;
1336 buffer = *ret_buffer;
1337 buffer_size = *ret_buffer_size;
1340 /* parse_packet assures this minimum size. */
1341 assert (buffer_size >= (sizeof (ph.type) + sizeof (ph.length)));
1343 BUFFER_READ (&ph.type, sizeof (ph.type));
1344 BUFFER_READ (&ph.length, sizeof (ph.length));
1345 ph_length = ntohs (ph.length);
1347 if ((ph_length <= PART_ENCRYPTION_AES256_SIZE)
1348 || (ph_length > buffer_size))
1350 ERROR ("network plugin: AES-256 encrypted part "
1351 "with invalid length received.");
1355 if (warning_has_been_printed == 0)
1357 WARNING ("network plugin: Received encrypted packet, but the network "
1358 "plugin was not linked with libgcrypt, so I cannot "
1359 "decrypt it. The part will be discarded.");
1360 warning_has_been_printed = 1;
1363 *ret_buffer += ph_length;
1364 *ret_buffer_size -= ph_length;
1367 } /* }}} int parse_part_encr_aes256 */
1368 #endif /* !HAVE_LIBGCRYPT */
1372 static int parse_packet (sockent_t *se, /* {{{ */
1373 void *buffer, size_t buffer_size, int flags,
1374 const char *username)
1378 value_list_t vl = VALUE_LIST_INIT;
1382 int packet_was_signed = (flags & PP_SIGNED);
1383 int packet_was_encrypted = (flags & PP_ENCRYPTED);
1384 int printed_ignore_warning = 0;
1385 #endif /* HAVE_LIBGCRYPT */
1388 memset (&vl, '\0', sizeof (vl));
1389 memset (&n, '\0', sizeof (n));
1392 while ((status == 0) && (0 < buffer_size)
1393 && ((unsigned int) buffer_size > sizeof (part_header_t)))
1395 uint16_t pkg_length;
1398 memcpy ((void *) &pkg_type,
1401 memcpy ((void *) &pkg_length,
1402 (void *) (buffer + sizeof (pkg_type)),
1403 sizeof (pkg_length));
1405 pkg_length = ntohs (pkg_length);
1406 pkg_type = ntohs (pkg_type);
1408 if (pkg_length > buffer_size)
1410 /* Ensure that this loop terminates eventually */
1411 if (pkg_length < (2 * sizeof (uint16_t)))
1414 if (pkg_type == TYPE_ENCR_AES256)
1416 status = parse_part_encr_aes256 (se,
1417 &buffer, &buffer_size, flags);
1420 ERROR ("network plugin: Decrypting AES256 "
1422 "with status %i.", status);
1427 else if ((se->data.server.security_level == SECURITY_LEVEL_ENCRYPT)
1428 && (packet_was_encrypted == 0))
1430 if (printed_ignore_warning == 0)
1432 INFO ("network plugin: Unencrypted packet or "
1433 "part has been ignored.");
1434 printed_ignore_warning = 1;
1436 buffer = ((char *) buffer) + pkg_length;
1439 #endif /* HAVE_LIBGCRYPT */
1440 else if (pkg_type == TYPE_SIGN_SHA256)
1442 status = parse_part_sign_sha256 (se,
1443 &buffer, &buffer_size, flags);
1446 ERROR ("network plugin: Verifying HMAC-SHA-256 "
1448 "with status %i.", status);
1453 else if ((se->data.server.security_level == SECURITY_LEVEL_SIGN)
1454 && (packet_was_encrypted == 0)
1455 && (packet_was_signed == 0))
1457 if (printed_ignore_warning == 0)
1459 INFO ("network plugin: Unsigned packet or "
1460 "part has been ignored.");
1461 printed_ignore_warning = 1;
1463 buffer = ((char *) buffer) + pkg_length;
1466 #endif /* HAVE_LIBGCRYPT */
1467 else if (pkg_type == TYPE_VALUES)
1469 status = parse_part_values (&buffer, &buffer_size,
1470 &vl.values, &vl.values_len);
1474 network_dispatch_values (&vl, username);
1478 else if (pkg_type == TYPE_TIME)
1481 status = parse_part_number (&buffer, &buffer_size,
1485 vl.time = TIME_T_TO_CDTIME_T (tmp);
1486 n.time = TIME_T_TO_CDTIME_T (tmp);
1489 else if (pkg_type == TYPE_TIME_HR)
1492 status = parse_part_number (&buffer, &buffer_size,
1496 vl.time = (cdtime_t) tmp;
1497 n.time = (cdtime_t) tmp;
1500 else if (pkg_type == TYPE_INTERVAL)
1503 status = parse_part_number (&buffer, &buffer_size,
1506 vl.interval = TIME_T_TO_CDTIME_T (tmp);
1508 else if (pkg_type == TYPE_INTERVAL_HR)
1511 status = parse_part_number (&buffer, &buffer_size,
1514 vl.interval = (cdtime_t) tmp;
1516 else if (pkg_type == TYPE_HOST)
1518 status = parse_part_string (&buffer, &buffer_size,
1519 vl.host, sizeof (vl.host));
1521 sstrncpy (n.host, vl.host, sizeof (n.host));
1523 else if (pkg_type == TYPE_PLUGIN)
1525 status = parse_part_string (&buffer, &buffer_size,
1526 vl.plugin, sizeof (vl.plugin));
1528 sstrncpy (n.plugin, vl.plugin,
1531 else if (pkg_type == TYPE_PLUGIN_INSTANCE)
1533 status = parse_part_string (&buffer, &buffer_size,
1535 sizeof (vl.plugin_instance));
1537 sstrncpy (n.plugin_instance,
1539 sizeof (n.plugin_instance));
1541 else if (pkg_type == TYPE_TYPE)
1543 status = parse_part_string (&buffer, &buffer_size,
1544 vl.type, sizeof (vl.type));
1546 sstrncpy (n.type, vl.type, sizeof (n.type));
1548 else if (pkg_type == TYPE_TYPE_INSTANCE)
1550 status = parse_part_string (&buffer, &buffer_size,
1552 sizeof (vl.type_instance));
1554 sstrncpy (n.type_instance, vl.type_instance,
1555 sizeof (n.type_instance));
1557 else if (pkg_type == TYPE_MESSAGE)
1559 status = parse_part_string (&buffer, &buffer_size,
1560 n.message, sizeof (n.message));
1566 else if ((n.severity != NOTIF_FAILURE)
1567 && (n.severity != NOTIF_WARNING)
1568 && (n.severity != NOTIF_OKAY))
1570 INFO ("network plugin: "
1571 "Ignoring notification with "
1572 "unknown severity %i.",
1575 else if (n.time <= 0)
1577 INFO ("network plugin: "
1578 "Ignoring notification with "
1581 else if (strlen (n.message) <= 0)
1583 INFO ("network plugin: "
1584 "Ignoring notification with "
1585 "an empty message.");
1589 network_dispatch_notification (&n);
1592 else if (pkg_type == TYPE_SEVERITY)
1595 status = parse_part_number (&buffer, &buffer_size,
1598 n.severity = (int) tmp;
1602 DEBUG ("network plugin: parse_packet: Unknown part"
1603 " type: 0x%04hx", pkg_type);
1604 buffer = ((char *) buffer) + pkg_length;
1606 } /* while (buffer_size > sizeof (part_header_t)) */
1608 if (status == 0 && buffer_size > 0)
1609 WARNING ("network plugin: parse_packet: Received truncated "
1610 "packet, try increasing `MaxPacketSize'");
1613 } /* }}} int parse_packet */
1615 static void free_sockent_client (struct sockent_client *sec) /* {{{ */
1624 sfree (sec->username);
1625 sfree (sec->password);
1626 if (sec->cypher != NULL)
1627 gcry_cipher_close (sec->cypher);
1629 } /* }}} void free_sockent_client */
1631 static void free_sockent_server (struct sockent_server *ses) /* {{{ */
1635 for (i = 0; i < ses->fd_num; i++)
1637 if (ses->fd[i] >= 0)
1646 sfree (ses->auth_file);
1647 fbh_destroy (ses->userdb);
1648 if (ses->cypher != NULL)
1649 gcry_cipher_close (ses->cypher);
1651 } /* }}} void free_sockent_server */
1653 static void sockent_destroy (sockent_t *se) /* {{{ */
1657 DEBUG ("network plugin: sockent_destroy (se = %p);", (void *) se);
1664 sfree (se->service);
1666 if (se->type == SOCKENT_TYPE_CLIENT)
1667 free_sockent_client (&se->data.client);
1669 free_sockent_server (&se->data.server);
1674 } /* }}} void sockent_destroy */
1677 * int network_set_ttl
1679 * Set the `IP_MULTICAST_TTL', `IP_TTL', `IPV6_MULTICAST_HOPS' or
1680 * `IPV6_UNICAST_HOPS', depending on which option is applicable.
1682 * The `struct addrinfo' is used to destinguish between unicast and multicast
1685 static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
1687 DEBUG ("network plugin: network_set_ttl: network_config_ttl = %i;",
1688 network_config_ttl);
1690 assert (se->type == SOCKENT_TYPE_CLIENT);
1692 if ((network_config_ttl < 1) || (network_config_ttl > 255))
1695 if (ai->ai_family == AF_INET)
1697 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1700 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1701 optname = IP_MULTICAST_TTL;
1705 if (setsockopt (se->data.client.fd, IPPROTO_IP, optname,
1706 &network_config_ttl,
1707 sizeof (network_config_ttl)) != 0)
1710 ERROR ("network plugin: setsockopt (ipv4-ttl): %s",
1711 sstrerror (errno, errbuf, sizeof (errbuf)));
1715 else if (ai->ai_family == AF_INET6)
1717 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1718 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1721 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1722 optname = IPV6_MULTICAST_HOPS;
1724 optname = IPV6_UNICAST_HOPS;
1726 if (setsockopt (se->data.client.fd, IPPROTO_IPV6, optname,
1727 &network_config_ttl,
1728 sizeof (network_config_ttl)) != 0)
1731 ERROR ("network plugin: setsockopt(ipv6-ttl): %s",
1732 sstrerror (errno, errbuf,
1739 } /* int network_set_ttl */
1741 static int network_set_interface (const sockent_t *se, const struct addrinfo *ai) /* {{{ */
1743 DEBUG ("network plugin: network_set_interface: interface index = %i;",
1746 assert (se->type == SOCKENT_TYPE_CLIENT);
1748 if (ai->ai_family == AF_INET)
1750 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1752 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1754 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1755 /* If possible, use the "ip_mreqn" structure which has
1756 * an "interface index" member. Using the interface
1757 * index is preferred here, because of its similarity
1758 * to the way IPv6 handles this. Unfortunately, it
1759 * appears not to be portable. */
1760 struct ip_mreqn mreq;
1762 memset (&mreq, 0, sizeof (mreq));
1763 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1764 mreq.imr_address.s_addr = ntohl (INADDR_ANY);
1765 mreq.imr_ifindex = se->interface;
1767 struct ip_mreq mreq;
1769 memset (&mreq, 0, sizeof (mreq));
1770 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1771 mreq.imr_interface.s_addr = ntohl (INADDR_ANY);
1774 if (setsockopt (se->data.client.fd, IPPROTO_IP, IP_MULTICAST_IF,
1775 &mreq, sizeof (mreq)) != 0)
1778 ERROR ("network plugin: setsockopt (ipv4-multicast-if): %s",
1779 sstrerror (errno, errbuf, sizeof (errbuf)));
1786 else if (ai->ai_family == AF_INET6)
1788 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1790 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1792 if (setsockopt (se->data.client.fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
1794 sizeof (se->interface)) != 0)
1797 ERROR ("network plugin: setsockopt (ipv6-multicast-if): %s",
1798 sstrerror (errno, errbuf,
1807 /* else: Not a multicast interface. */
1808 if (se->interface != 0)
1810 #if defined(HAVE_IF_INDEXTONAME) && HAVE_IF_INDEXTONAME && defined(SO_BINDTODEVICE)
1811 char interface_name[IFNAMSIZ];
1813 if (if_indextoname (se->interface, interface_name) == NULL)
1816 DEBUG ("network plugin: Binding socket to interface %s", interface_name);
1818 if (setsockopt (se->data.client.fd, SOL_SOCKET, SO_BINDTODEVICE,
1820 sizeof(interface_name)) == -1 )
1823 ERROR ("network plugin: setsockopt (bind-if): %s",
1824 sstrerror (errno, errbuf, sizeof (errbuf)));
1827 /* #endif HAVE_IF_INDEXTONAME && SO_BINDTODEVICE */
1830 WARNING ("network plugin: Cannot set the interface on a unicast "
1832 # if !defined(SO_BINDTODEVICE)
1833 "the \"SO_BINDTODEVICE\" socket option "
1835 "the \"if_indextoname\" function "
1837 "is not available on your system.");
1843 } /* }}} network_set_interface */
1845 static int network_bind_socket (int fd, const struct addrinfo *ai, const int interface_idx)
1854 /* allow multiple sockets to use the same PORT number */
1855 if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR,
1856 &yes, sizeof(yes)) == -1) {
1858 ERROR ("network plugin: setsockopt (reuseaddr): %s",
1859 sstrerror (errno, errbuf, sizeof (errbuf)));
1863 DEBUG ("fd = %i; calling `bind'", fd);
1865 if (bind (fd, ai->ai_addr, ai->ai_addrlen) == -1)
1869 sstrerror (errno, errbuf, sizeof (errbuf)));
1873 if (ai->ai_family == AF_INET)
1875 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1876 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1878 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1879 struct ip_mreqn mreq;
1881 struct ip_mreq mreq;
1884 DEBUG ("fd = %i; IPv4 multicast address found", fd);
1886 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1887 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1888 /* Set the interface using the interface index if
1889 * possible (available). Unfortunately, the struct
1890 * ip_mreqn is not portable. */
1891 mreq.imr_address.s_addr = ntohl (INADDR_ANY);
1892 mreq.imr_ifindex = interface_idx;
1894 mreq.imr_interface.s_addr = ntohl (INADDR_ANY);
1897 if (setsockopt (fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1898 &loop, sizeof (loop)) == -1)
1901 ERROR ("network plugin: setsockopt (multicast-loop): %s",
1902 sstrerror (errno, errbuf,
1907 if (setsockopt (fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1908 &mreq, sizeof (mreq)) == -1)
1911 ERROR ("network plugin: setsockopt (add-membership): %s",
1912 sstrerror (errno, errbuf,
1920 else if (ai->ai_family == AF_INET6)
1922 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1923 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1924 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1926 struct ipv6_mreq mreq;
1928 DEBUG ("fd = %i; IPv6 multicast address found", fd);
1930 memcpy (&mreq.ipv6mr_multiaddr,
1932 sizeof (addr->sin6_addr));
1934 /* http://developer.apple.com/documentation/Darwin/Reference/ManPages/man4/ip6.4.html
1935 * ipv6mr_interface may be set to zeroes to
1936 * choose the default multicast interface or to
1937 * the index of a particular multicast-capable
1938 * interface if the host is multihomed.
1939 * Membership is associ-associated with a
1940 * single interface; programs running on
1941 * multihomed hosts may need to join the same
1942 * group on more than one interface.*/
1943 mreq.ipv6mr_interface = interface_idx;
1945 if (setsockopt (fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
1946 &loop, sizeof (loop)) == -1)
1949 ERROR ("network plugin: setsockopt (ipv6-multicast-loop): %s",
1950 sstrerror (errno, errbuf,
1955 if (setsockopt (fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
1956 &mreq, sizeof (mreq)) == -1)
1959 ERROR ("network plugin: setsockopt (ipv6-add-membership): %s",
1960 sstrerror (errno, errbuf,
1969 #if defined(HAVE_IF_INDEXTONAME) && HAVE_IF_INDEXTONAME && defined(SO_BINDTODEVICE)
1970 /* if a specific interface was set, bind the socket to it. But to avoid
1971 * possible problems with multicast routing, only do that for non-multicast
1973 if (interface_idx != 0)
1975 char interface_name[IFNAMSIZ];
1977 if (if_indextoname (interface_idx, interface_name) == NULL)
1980 DEBUG ("fd = %i; Binding socket to interface %s", fd, interface_name);
1982 if (setsockopt (fd, SOL_SOCKET, SO_BINDTODEVICE,
1984 sizeof(interface_name)) == -1 )
1987 ERROR ("network plugin: setsockopt (bind-if): %s",
1988 sstrerror (errno, errbuf, sizeof (errbuf)));
1992 #endif /* HAVE_IF_INDEXTONAME && SO_BINDTODEVICE */
1995 } /* int network_bind_socket */
1997 /* Initialize a sockent structure. `type' must be either `SOCKENT_TYPE_CLIENT'
1998 * or `SOCKENT_TYPE_SERVER' */
1999 static sockent_t *sockent_create (int type) /* {{{ */
2003 if ((type != SOCKENT_TYPE_CLIENT) && (type != SOCKENT_TYPE_SERVER))
2006 se = malloc (sizeof (*se));
2009 memset (se, 0, sizeof (*se));
2017 if (type == SOCKENT_TYPE_SERVER)
2019 se->data.server.fd = NULL;
2020 se->data.server.fd_num = 0;
2022 se->data.server.security_level = SECURITY_LEVEL_NONE;
2023 se->data.server.auth_file = NULL;
2024 se->data.server.userdb = NULL;
2025 se->data.server.cypher = NULL;
2030 se->data.client.fd = -1;
2031 se->data.client.addr = NULL;
2033 se->data.client.security_level = SECURITY_LEVEL_NONE;
2034 se->data.client.username = NULL;
2035 se->data.client.password = NULL;
2036 se->data.client.cypher = NULL;
2041 } /* }}} sockent_t *sockent_create */
2043 static int sockent_init_crypto (sockent_t *se) /* {{{ */
2045 #if HAVE_LIBGCRYPT /* {{{ */
2046 if (se->type == SOCKENT_TYPE_CLIENT)
2048 if (se->data.client.security_level > SECURITY_LEVEL_NONE)
2050 network_init_gcrypt ();
2052 if ((se->data.client.username == NULL)
2053 || (se->data.client.password == NULL))
2055 ERROR ("network plugin: Client socket with "
2056 "security requested, but no "
2057 "credentials are configured.");
2060 gcry_md_hash_buffer (GCRY_MD_SHA256,
2061 se->data.client.password_hash,
2062 se->data.client.password,
2063 strlen (se->data.client.password));
2066 else /* (se->type == SOCKENT_TYPE_SERVER) */
2068 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
2070 network_init_gcrypt ();
2072 if (se->data.server.auth_file == NULL)
2074 ERROR ("network plugin: Server socket with "
2075 "security requested, but no "
2076 "password file is configured.");
2080 if (se->data.server.auth_file != NULL)
2082 se->data.server.userdb = fbh_create (se->data.server.auth_file);
2083 if (se->data.server.userdb == NULL)
2085 ERROR ("network plugin: Reading password file "
2087 se->data.server.auth_file);
2088 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
2093 #endif /* }}} HAVE_LIBGCRYPT */
2096 } /* }}} int sockent_init_crypto */
2098 static int sockent_client_connect (sockent_t *se) /* {{{ */
2100 static c_complain_t complaint = C_COMPLAIN_INIT_STATIC;
2102 struct sockent_client *client;
2103 struct addrinfo ai_hints;
2104 struct addrinfo *ai_list = NULL, *ai_ptr;
2107 if ((se == NULL) || (se->type != SOCKENT_TYPE_CLIENT))
2110 client = &se->data.client;
2111 if (client->fd >= 0) /* already connected */
2114 memset (&ai_hints, 0, sizeof (ai_hints));
2115 #ifdef AI_ADDRCONFIG
2116 ai_hints.ai_flags |= AI_ADDRCONFIG;
2118 ai_hints.ai_family = AF_UNSPEC;
2119 ai_hints.ai_socktype = SOCK_DGRAM;
2120 ai_hints.ai_protocol = IPPROTO_UDP;
2122 status = getaddrinfo (se->node,
2123 (se->service != NULL) ? se->service : NET_DEFAULT_PORT,
2124 &ai_hints, &ai_list);
2127 c_complain (LOG_ERR, &complaint,
2128 "network plugin: getaddrinfo (%s, %s) failed: %s",
2129 (se->node == NULL) ? "(null)" : se->node,
2130 (se->service == NULL) ? "(null)" : se->service,
2131 gai_strerror (status));
2136 c_release (LOG_NOTICE, &complaint,
2137 "network plugin: Successfully resolved \"%s\".",
2141 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
2143 client->fd = socket (ai_ptr->ai_family,
2144 ai_ptr->ai_socktype,
2145 ai_ptr->ai_protocol);
2149 ERROR ("network plugin: socket(2) failed: %s",
2150 sstrerror (errno, errbuf,
2155 client->addr = malloc (sizeof (*client->addr));
2156 if (client->addr == NULL)
2158 ERROR ("network plugin: malloc failed.");
2164 memset (client->addr, 0, sizeof (*client->addr));
2165 assert (sizeof (*client->addr) >= ai_ptr->ai_addrlen);
2166 memcpy (client->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
2167 client->addrlen = ai_ptr->ai_addrlen;
2169 network_set_ttl (se, ai_ptr);
2170 network_set_interface (se, ai_ptr);
2172 /* We don't open more than one write-socket per
2173 * node/service pair.. */
2177 freeaddrinfo (ai_list);
2181 } /* }}} int sockent_client_connect */
2183 static int sockent_client_disconnect (sockent_t *se) /* {{{ */
2185 struct sockent_client *client;
2187 if ((se == NULL) || (se->type != SOCKENT_TYPE_CLIENT))
2190 client = &se->data.client;
2191 if (client->fd >= 0) /* connected */
2197 sfree (client->addr);
2198 client->addrlen = 0;
2201 } /* }}} int sockent_client_disconnect */
2203 /* Open the file descriptors for a initialized sockent structure. */
2204 static int sockent_server_listen (sockent_t *se) /* {{{ */
2206 struct addrinfo ai_hints;
2207 struct addrinfo *ai_list, *ai_ptr;
2211 const char *service;
2216 assert (se->data.server.fd == NULL);
2217 assert (se->data.server.fd_num == 0);
2220 service = se->service;
2222 if (service == NULL)
2223 service = NET_DEFAULT_PORT;
2225 DEBUG ("network plugin: sockent_server_listen: node = %s; service = %s;",
2228 memset (&ai_hints, 0, sizeof (ai_hints));
2229 ai_hints.ai_flags = 0;
2231 ai_hints.ai_flags |= AI_PASSIVE;
2233 #ifdef AI_ADDRCONFIG
2234 ai_hints.ai_flags |= AI_ADDRCONFIG;
2236 ai_hints.ai_family = AF_UNSPEC;
2237 ai_hints.ai_socktype = SOCK_DGRAM;
2238 ai_hints.ai_protocol = IPPROTO_UDP;
2240 status = getaddrinfo (node, service, &ai_hints, &ai_list);
2243 ERROR ("network plugin: getaddrinfo (%s, %s) failed: %s",
2244 (se->node == NULL) ? "(null)" : se->node,
2245 (se->service == NULL) ? "(null)" : se->service,
2246 gai_strerror (status));
2250 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
2254 tmp = realloc (se->data.server.fd,
2255 sizeof (*tmp) * (se->data.server.fd_num + 1));
2258 ERROR ("network plugin: realloc failed.");
2261 se->data.server.fd = tmp;
2262 tmp = se->data.server.fd + se->data.server.fd_num;
2264 *tmp = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
2265 ai_ptr->ai_protocol);
2269 ERROR ("network plugin: socket(2) failed: %s",
2270 sstrerror (errno, errbuf,
2275 status = network_bind_socket (*tmp, ai_ptr, se->interface);
2283 se->data.server.fd_num++;
2285 } /* for (ai_list) */
2287 freeaddrinfo (ai_list);
2289 if (se->data.server.fd_num <= 0)
2292 } /* }}} int sockent_server_listen */
2294 /* Add a sockent to the global list of sockets */
2295 static int sockent_add (sockent_t *se) /* {{{ */
2297 sockent_t *last_ptr;
2302 if (se->type == SOCKENT_TYPE_SERVER)
2307 tmp = realloc (listen_sockets_pollfd,
2308 sizeof (*tmp) * (listen_sockets_num
2309 + se->data.server.fd_num));
2312 ERROR ("network plugin: realloc failed.");
2315 listen_sockets_pollfd = tmp;
2316 tmp = listen_sockets_pollfd + listen_sockets_num;
2318 for (i = 0; i < se->data.server.fd_num; i++)
2320 memset (tmp + i, 0, sizeof (*tmp));
2321 tmp[i].fd = se->data.server.fd[i];
2322 tmp[i].events = POLLIN | POLLPRI;
2326 listen_sockets_num += se->data.server.fd_num;
2328 if (listen_sockets == NULL)
2330 listen_sockets = se;
2333 last_ptr = listen_sockets;
2335 else /* if (se->type == SOCKENT_TYPE_CLIENT) */
2337 if (sending_sockets == NULL)
2339 sending_sockets = se;
2342 last_ptr = sending_sockets;
2345 while (last_ptr->next != NULL)
2346 last_ptr = last_ptr->next;
2347 last_ptr->next = se;
2350 } /* }}} int sockent_add */
2352 static void *dispatch_thread (void __attribute__((unused)) *arg) /* {{{ */
2356 receive_list_entry_t *ent;
2359 /* Lock and wait for more data to come in */
2360 pthread_mutex_lock (&receive_list_lock);
2361 while ((listen_loop == 0)
2362 && (receive_list_head == NULL))
2363 pthread_cond_wait (&receive_list_cond, &receive_list_lock);
2365 /* Remove the head entry and unlock */
2366 ent = receive_list_head;
2368 receive_list_head = ent->next;
2369 receive_list_length--;
2370 pthread_mutex_unlock (&receive_list_lock);
2372 /* Check whether we are supposed to exit. We do NOT check `listen_loop'
2373 * because we dispatch all missing packets before shutting down. */
2377 /* Look for the correct `sockent_t' */
2378 se = listen_sockets;
2383 for (i = 0; i < se->data.server.fd_num; i++)
2384 if (se->data.server.fd[i] == ent->fd)
2387 if (i < se->data.server.fd_num)
2395 ERROR ("network plugin: Got packet from FD %i, but can't "
2396 "find an appropriate socket entry.",
2403 parse_packet (se, ent->data, ent->data_len, /* flags = */ 0,
2404 /* username = */ NULL);
2410 } /* }}} void *dispatch_thread */
2412 static int network_receive (void) /* {{{ */
2414 char buffer[network_config_packet_size];
2420 receive_list_entry_t *private_list_head;
2421 receive_list_entry_t *private_list_tail;
2422 uint64_t private_list_length;
2424 assert (listen_sockets_num > 0);
2426 private_list_head = NULL;
2427 private_list_tail = NULL;
2428 private_list_length = 0;
2430 while (listen_loop == 0)
2432 status = poll (listen_sockets_pollfd, listen_sockets_num, -1);
2438 ERROR ("network plugin: poll(2) failed: %s",
2439 sstrerror (errno, errbuf, sizeof (errbuf)));
2443 for (i = 0; (i < listen_sockets_num) && (status > 0); i++)
2445 receive_list_entry_t *ent;
2447 if ((listen_sockets_pollfd[i].revents
2448 & (POLLIN | POLLPRI)) == 0)
2452 buffer_len = recv (listen_sockets_pollfd[i].fd,
2453 buffer, sizeof (buffer),
2458 status = (errno != 0) ? errno : -1;
2459 ERROR ("network plugin: recv(2) failed: %s",
2460 sstrerror (errno, errbuf, sizeof (errbuf)));
2464 stats_octets_rx += ((uint64_t) buffer_len);
2467 /* TODO: Possible performance enhancement: Do not free
2468 * these entries in the dispatch thread but put them in
2469 * another list, so we don't have to allocate more and
2470 * more of these structures. */
2471 ent = malloc (sizeof (receive_list_entry_t));
2474 ERROR ("network plugin: malloc failed.");
2478 memset (ent, 0, sizeof (receive_list_entry_t));
2479 ent->data = malloc (network_config_packet_size);
2480 if (ent->data == NULL)
2483 ERROR ("network plugin: malloc failed.");
2487 ent->fd = listen_sockets_pollfd[i].fd;
2490 memcpy (ent->data, buffer, buffer_len);
2491 ent->data_len = buffer_len;
2493 if (private_list_head == NULL)
2494 private_list_head = ent;
2496 private_list_tail->next = ent;
2497 private_list_tail = ent;
2498 private_list_length++;
2500 /* Do not block here. Blocking here has led to
2501 * insufficient performance in the past. */
2502 if (pthread_mutex_trylock (&receive_list_lock) == 0)
2504 assert (((receive_list_head == NULL) && (receive_list_length == 0))
2505 || ((receive_list_head != NULL) && (receive_list_length != 0)));
2507 if (receive_list_head == NULL)
2508 receive_list_head = private_list_head;
2510 receive_list_tail->next = private_list_head;
2511 receive_list_tail = private_list_tail;
2512 receive_list_length += private_list_length;
2514 pthread_cond_signal (&receive_list_cond);
2515 pthread_mutex_unlock (&receive_list_lock);
2517 private_list_head = NULL;
2518 private_list_tail = NULL;
2519 private_list_length = 0;
2523 } /* for (listen_sockets_pollfd) */
2527 } /* while (listen_loop == 0) */
2529 /* Make sure everything is dispatched before exiting. */
2530 if (private_list_head != NULL)
2532 pthread_mutex_lock (&receive_list_lock);
2534 if (receive_list_head == NULL)
2535 receive_list_head = private_list_head;
2537 receive_list_tail->next = private_list_head;
2538 receive_list_tail = private_list_tail;
2539 receive_list_length += private_list_length;
2541 pthread_cond_signal (&receive_list_cond);
2542 pthread_mutex_unlock (&receive_list_lock);
2546 } /* }}} int network_receive */
2548 static void *receive_thread (void __attribute__((unused)) *arg)
2550 return (network_receive () ? (void *) 1 : (void *) 0);
2551 } /* void *receive_thread */
2553 static void network_init_buffer (void)
2555 memset (send_buffer, 0, network_config_packet_size);
2556 send_buffer_ptr = send_buffer;
2557 send_buffer_fill = 0;
2559 memset (&send_buffer_vl, 0, sizeof (send_buffer_vl));
2560 } /* int network_init_buffer */
2562 static void networt_send_buffer_plain (sockent_t *se, /* {{{ */
2563 const char *buffer, size_t buffer_size)
2569 status = sockent_client_connect (se);
2573 status = sendto (se->data.client.fd, buffer, buffer_size,
2575 (struct sockaddr *) se->data.client.addr,
2576 se->data.client.addrlen);
2581 if ((errno == EINTR) || (errno == EAGAIN))
2584 ERROR ("network plugin: sendto failed: %s. Closing sending socket.",
2585 sstrerror (errno, errbuf, sizeof (errbuf)));
2586 sockent_client_disconnect (se);
2592 } /* }}} void networt_send_buffer_plain */
2595 #define BUFFER_ADD(p,s) do { \
2596 memcpy (buffer + buffer_offset, (p), (s)); \
2597 buffer_offset += (s); \
2600 static void networt_send_buffer_signed (sockent_t *se, /* {{{ */
2601 const char *in_buffer, size_t in_buffer_size)
2603 part_signature_sha256_t ps;
2604 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2605 size_t buffer_offset;
2606 size_t username_len;
2610 unsigned char *hash;
2613 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
2616 ERROR ("network plugin: Creating HMAC object failed: %s",
2617 gcry_strerror (err));
2621 err = gcry_md_setkey (hd, se->data.client.password,
2622 strlen (se->data.client.password));
2625 ERROR ("network plugin: gcry_md_setkey failed: %s",
2626 gcry_strerror (err));
2631 username_len = strlen (se->data.client.username);
2632 if (username_len > (BUFF_SIG_SIZE - PART_SIGNATURE_SHA256_SIZE))
2634 ERROR ("network plugin: Username too long: %s",
2635 se->data.client.username);
2639 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE,
2640 se->data.client.username, username_len);
2641 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE + username_len,
2642 in_buffer, in_buffer_size);
2644 /* Initialize the `ps' structure. */
2645 memset (&ps, 0, sizeof (ps));
2646 ps.head.type = htons (TYPE_SIGN_SHA256);
2647 ps.head.length = htons (PART_SIGNATURE_SHA256_SIZE + username_len);
2649 /* Calculate the hash value. */
2650 gcry_md_write (hd, buffer + PART_SIGNATURE_SHA256_SIZE,
2651 username_len + in_buffer_size);
2652 hash = gcry_md_read (hd, GCRY_MD_SHA256);
2655 ERROR ("network plugin: gcry_md_read failed.");
2659 memcpy (ps.hash, hash, sizeof (ps.hash));
2661 /* Add the header */
2664 BUFFER_ADD (&ps.head.type, sizeof (ps.head.type));
2665 BUFFER_ADD (&ps.head.length, sizeof (ps.head.length));
2666 BUFFER_ADD (ps.hash, sizeof (ps.hash));
2668 assert (buffer_offset == PART_SIGNATURE_SHA256_SIZE);
2673 buffer_offset = PART_SIGNATURE_SHA256_SIZE + username_len + in_buffer_size;
2674 networt_send_buffer_plain (se, buffer, buffer_offset);
2675 } /* }}} void networt_send_buffer_signed */
2677 static void networt_send_buffer_encrypted (sockent_t *se, /* {{{ */
2678 const char *in_buffer, size_t in_buffer_size)
2680 part_encryption_aes256_t pea;
2681 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2683 size_t buffer_offset;
2685 size_t username_len;
2687 gcry_cipher_hd_t cypher;
2689 /* Initialize the header fields */
2690 memset (&pea, 0, sizeof (pea));
2691 pea.head.type = htons (TYPE_ENCR_AES256);
2693 pea.username = se->data.client.username;
2695 username_len = strlen (pea.username);
2696 if ((PART_ENCRYPTION_AES256_SIZE + username_len) > BUFF_SIG_SIZE)
2698 ERROR ("network plugin: Username too long: %s", pea.username);
2702 buffer_size = PART_ENCRYPTION_AES256_SIZE + username_len + in_buffer_size;
2703 header_size = PART_ENCRYPTION_AES256_SIZE + username_len
2704 - sizeof (pea.hash);
2706 assert (buffer_size <= sizeof (buffer));
2707 DEBUG ("network plugin: networt_send_buffer_encrypted: "
2708 "buffer_size = %zu;", buffer_size);
2710 pea.head.length = htons ((uint16_t) (PART_ENCRYPTION_AES256_SIZE
2711 + username_len + in_buffer_size));
2712 pea.username_length = htons ((uint16_t) username_len);
2714 /* Chose a random initialization vector. */
2715 gcry_randomize ((void *) &pea.iv, sizeof (pea.iv), GCRY_STRONG_RANDOM);
2717 /* Create hash of the payload */
2718 gcry_md_hash_buffer (GCRY_MD_SHA1, pea.hash, in_buffer, in_buffer_size);
2720 /* Initialize the buffer */
2722 memset (buffer, 0, sizeof (buffer));
2725 BUFFER_ADD (&pea.head.type, sizeof (pea.head.type));
2726 BUFFER_ADD (&pea.head.length, sizeof (pea.head.length));
2727 BUFFER_ADD (&pea.username_length, sizeof (pea.username_length));
2728 BUFFER_ADD (pea.username, username_len);
2729 BUFFER_ADD (pea.iv, sizeof (pea.iv));
2730 assert (buffer_offset == header_size);
2731 BUFFER_ADD (pea.hash, sizeof (pea.hash));
2732 BUFFER_ADD (in_buffer, in_buffer_size);
2734 assert (buffer_offset == buffer_size);
2736 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
2737 se->data.client.password);
2741 /* Encrypt the buffer in-place */
2742 err = gcry_cipher_encrypt (cypher,
2743 buffer + header_size,
2744 buffer_size - header_size,
2745 /* in = */ NULL, /* in len = */ 0);
2748 ERROR ("network plugin: gcry_cipher_encrypt returned: %s",
2749 gcry_strerror (err));
2753 /* Send it out without further modifications */
2754 networt_send_buffer_plain (se, buffer, buffer_size);
2755 } /* }}} void networt_send_buffer_encrypted */
2757 #endif /* HAVE_LIBGCRYPT */
2759 static void network_send_buffer (char *buffer, size_t buffer_len) /* {{{ */
2763 DEBUG ("network plugin: network_send_buffer: buffer_len = %zu", buffer_len);
2765 for (se = sending_sockets; se != NULL; se = se->next)
2768 if (se->data.client.security_level == SECURITY_LEVEL_ENCRYPT)
2769 networt_send_buffer_encrypted (se, buffer, buffer_len);
2770 else if (se->data.client.security_level == SECURITY_LEVEL_SIGN)
2771 networt_send_buffer_signed (se, buffer, buffer_len);
2772 else /* if (se->data.client.security_level == SECURITY_LEVEL_NONE) */
2773 #endif /* HAVE_LIBGCRYPT */
2774 networt_send_buffer_plain (se, buffer, buffer_len);
2775 } /* for (sending_sockets) */
2776 } /* }}} void network_send_buffer */
2778 static int add_to_buffer (char *buffer, int buffer_size, /* {{{ */
2779 value_list_t *vl_def,
2780 const data_set_t *ds, const value_list_t *vl)
2782 char *buffer_orig = buffer;
2784 if (strcmp (vl_def->host, vl->host) != 0)
2786 if (write_part_string (&buffer, &buffer_size, TYPE_HOST,
2787 vl->host, strlen (vl->host)) != 0)
2789 sstrncpy (vl_def->host, vl->host, sizeof (vl_def->host));
2792 if (vl_def->time != vl->time)
2794 if (write_part_number (&buffer, &buffer_size, TYPE_TIME_HR,
2795 (uint64_t) vl->time))
2797 vl_def->time = vl->time;
2800 if (vl_def->interval != vl->interval)
2802 if (write_part_number (&buffer, &buffer_size, TYPE_INTERVAL_HR,
2803 (uint64_t) vl->interval))
2805 vl_def->interval = vl->interval;
2808 if (strcmp (vl_def->plugin, vl->plugin) != 0)
2810 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN,
2811 vl->plugin, strlen (vl->plugin)) != 0)
2813 sstrncpy (vl_def->plugin, vl->plugin, sizeof (vl_def->plugin));
2816 if (strcmp (vl_def->plugin_instance, vl->plugin_instance) != 0)
2818 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
2819 vl->plugin_instance,
2820 strlen (vl->plugin_instance)) != 0)
2822 sstrncpy (vl_def->plugin_instance, vl->plugin_instance, sizeof (vl_def->plugin_instance));
2825 if (strcmp (vl_def->type, vl->type) != 0)
2827 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
2828 vl->type, strlen (vl->type)) != 0)
2830 sstrncpy (vl_def->type, ds->type, sizeof (vl_def->type));
2833 if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
2835 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE_INSTANCE,
2837 strlen (vl->type_instance)) != 0)
2839 sstrncpy (vl_def->type_instance, vl->type_instance, sizeof (vl_def->type_instance));
2842 if (write_part_values (&buffer, &buffer_size, ds, vl) != 0)
2845 return (buffer - buffer_orig);
2846 } /* }}} int add_to_buffer */
2848 static void flush_buffer (void)
2850 DEBUG ("network plugin: flush_buffer: send_buffer_fill = %i",
2853 network_send_buffer (send_buffer, (size_t) send_buffer_fill);
2855 stats_octets_tx += ((uint64_t) send_buffer_fill);
2858 network_init_buffer ();
2861 static int network_write (const data_set_t *ds, const value_list_t *vl,
2862 user_data_t __attribute__((unused)) *user_data)
2866 if (!check_send_okay (vl))
2869 char name[6*DATA_MAX_NAME_LEN];
2870 FORMAT_VL (name, sizeof (name), vl);
2871 name[sizeof (name) - 1] = 0;
2872 DEBUG ("network plugin: network_write: "
2873 "NOT sending %s.", name);
2875 /* Counter is not protected by another lock and may be reached by
2876 * multiple threads */
2877 pthread_mutex_lock (&stats_lock);
2878 stats_values_not_sent++;
2879 pthread_mutex_unlock (&stats_lock);
2883 uc_meta_data_add_unsigned_int (vl,
2884 "network:time_sent", (uint64_t) vl->time);
2886 pthread_mutex_lock (&send_buffer_lock);
2888 status = add_to_buffer (send_buffer_ptr,
2889 network_config_packet_size - (send_buffer_fill + BUFF_SIG_SIZE),
2894 /* status == bytes added to the buffer */
2895 send_buffer_fill += status;
2896 send_buffer_ptr += status;
2898 stats_values_sent++;
2904 status = add_to_buffer (send_buffer_ptr,
2905 network_config_packet_size - (send_buffer_fill + BUFF_SIG_SIZE),
2911 send_buffer_fill += status;
2912 send_buffer_ptr += status;
2914 stats_values_sent++;
2920 ERROR ("network plugin: Unable to append to the "
2921 "buffer for some weird reason");
2923 else if ((network_config_packet_size - send_buffer_fill) < 15)
2928 pthread_mutex_unlock (&send_buffer_lock);
2930 return ((status < 0) ? -1 : 0);
2931 } /* int network_write */
2933 static int network_config_set_boolean (const oconfig_item_t *ci, /* {{{ */
2936 if ((ci->values_num != 1)
2937 || ((ci->values[0].type != OCONFIG_TYPE_BOOLEAN)
2938 && (ci->values[0].type != OCONFIG_TYPE_STRING)))
2940 ERROR ("network plugin: The `%s' config option needs "
2941 "exactly one boolean argument.", ci->key);
2945 if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
2947 if (ci->values[0].value.boolean)
2954 char *str = ci->values[0].value.string;
2958 else if (IS_FALSE (str))
2962 ERROR ("network plugin: Cannot parse string value `%s' of the `%s' "
2963 "option as boolean value.",
2970 } /* }}} int network_config_set_boolean */
2972 static int network_config_set_ttl (const oconfig_item_t *ci) /* {{{ */
2975 if ((ci->values_num != 1)
2976 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
2978 WARNING ("network plugin: The `TimeToLive' config option needs exactly "
2979 "one numeric argument.");
2983 tmp = (int) ci->values[0].value.number;
2984 if ((tmp > 0) && (tmp <= 255))
2985 network_config_ttl = tmp;
2987 WARNING ("network plugin: The `TimeToLive' must be between 1 and 255.");
2992 } /* }}} int network_config_set_ttl */
2994 static int network_config_set_interface (const oconfig_item_t *ci, /* {{{ */
2997 if ((ci->values_num != 1)
2998 || (ci->values[0].type != OCONFIG_TYPE_STRING))
3000 WARNING ("network plugin: The `Interface' config option needs exactly "
3001 "one string argument.");
3005 if (interface == NULL)
3008 *interface = if_nametoindex (ci->values[0].value.string);
3011 } /* }}} int network_config_set_interface */
3013 static int network_config_set_buffer_size (const oconfig_item_t *ci) /* {{{ */
3016 if ((ci->values_num != 1)
3017 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
3019 WARNING ("network plugin: The `MaxPacketSize' config option needs exactly "
3020 "one numeric argument.");
3024 tmp = (int) ci->values[0].value.number;
3025 if ((tmp >= 1024) && (tmp <= 65535))
3026 network_config_packet_size = tmp;
3029 } /* }}} int network_config_set_buffer_size */
3032 static int network_config_set_string (const oconfig_item_t *ci, /* {{{ */
3036 if ((ci->values_num != 1)
3037 || (ci->values[0].type != OCONFIG_TYPE_STRING))
3039 WARNING ("network plugin: The `%s' config option needs exactly "
3040 "one string argument.", ci->key);
3044 tmp = strdup (ci->values[0].value.string);
3048 sfree (*ret_string);
3052 } /* }}} int network_config_set_string */
3053 #endif /* HAVE_LIBGCRYPT */
3056 static int network_config_set_security_level (oconfig_item_t *ci, /* {{{ */
3060 if ((ci->values_num != 1)
3061 || (ci->values[0].type != OCONFIG_TYPE_STRING))
3063 WARNING ("network plugin: The `SecurityLevel' config option needs exactly "
3064 "one string argument.");
3068 str = ci->values[0].value.string;
3069 if (strcasecmp ("Encrypt", str) == 0)
3070 *retval = SECURITY_LEVEL_ENCRYPT;
3071 else if (strcasecmp ("Sign", str) == 0)
3072 *retval = SECURITY_LEVEL_SIGN;
3073 else if (strcasecmp ("None", str) == 0)
3074 *retval = SECURITY_LEVEL_NONE;
3077 WARNING ("network plugin: Unknown security level: %s.", str);
3082 } /* }}} int network_config_set_security_level */
3083 #endif /* HAVE_LIBGCRYPT */
3085 static int network_config_add_listen (const oconfig_item_t *ci) /* {{{ */
3091 if ((ci->values_num < 1) || (ci->values_num > 2)
3092 || (ci->values[0].type != OCONFIG_TYPE_STRING)
3093 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
3095 ERROR ("network plugin: The `%s' config option needs "
3096 "one or two string arguments.", ci->key);
3100 se = sockent_create (SOCKENT_TYPE_SERVER);
3103 ERROR ("network plugin: sockent_create failed.");
3107 se->node = strdup (ci->values[0].value.string);
3108 if (ci->values_num >= 2)
3109 se->service = strdup (ci->values[1].value.string);
3111 for (i = 0; i < ci->children_num; i++)
3113 oconfig_item_t *child = ci->children + i;
3116 if (strcasecmp ("AuthFile", child->key) == 0)
3117 network_config_set_string (child, &se->data.server.auth_file);
3118 else if (strcasecmp ("SecurityLevel", child->key) == 0)
3119 network_config_set_security_level (child,
3120 &se->data.server.security_level);
3122 #endif /* HAVE_LIBGCRYPT */
3123 if (strcasecmp ("Interface", child->key) == 0)
3124 network_config_set_interface (child,
3128 WARNING ("network plugin: Option `%s' is not allowed here.",
3134 if ((se->data.server.security_level > SECURITY_LEVEL_NONE)
3135 && (se->data.server.auth_file == NULL))
3137 ERROR ("network plugin: A security level higher than `none' was "
3138 "requested, but no AuthFile option was given. Cowardly refusing to "
3139 "open this socket!");
3140 sockent_destroy (se);
3143 #endif /* HAVE_LIBGCRYPT */
3145 status = sockent_init_crypto (se);
3148 ERROR ("network plugin: network_config_add_listen: sockent_init_crypto() failed.");
3149 sockent_destroy (se);
3153 status = sockent_server_listen (se);
3156 ERROR ("network plugin: network_config_add_server: sockent_server_listen failed.");
3157 sockent_destroy (se);
3161 status = sockent_add (se);
3164 ERROR ("network plugin: network_config_add_listen: sockent_add failed.");
3165 sockent_destroy (se);
3170 } /* }}} int network_config_add_listen */
3172 static int network_config_add_server (const oconfig_item_t *ci) /* {{{ */
3178 if ((ci->values_num < 1) || (ci->values_num > 2)
3179 || (ci->values[0].type != OCONFIG_TYPE_STRING)
3180 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
3182 ERROR ("network plugin: The `%s' config option needs "
3183 "one or two string arguments.", ci->key);
3187 se = sockent_create (SOCKENT_TYPE_CLIENT);
3190 ERROR ("network plugin: sockent_create failed.");
3194 se->node = strdup (ci->values[0].value.string);
3195 if (ci->values_num >= 2)
3196 se->service = strdup (ci->values[1].value.string);
3198 for (i = 0; i < ci->children_num; i++)
3200 oconfig_item_t *child = ci->children + i;
3203 if (strcasecmp ("Username", child->key) == 0)
3204 network_config_set_string (child, &se->data.client.username);
3205 else if (strcasecmp ("Password", child->key) == 0)
3206 network_config_set_string (child, &se->data.client.password);
3207 else if (strcasecmp ("SecurityLevel", child->key) == 0)
3208 network_config_set_security_level (child,
3209 &se->data.client.security_level);
3211 #endif /* HAVE_LIBGCRYPT */
3212 if (strcasecmp ("Interface", child->key) == 0)
3213 network_config_set_interface (child,
3217 WARNING ("network plugin: Option `%s' is not allowed here.",
3223 if ((se->data.client.security_level > SECURITY_LEVEL_NONE)
3224 && ((se->data.client.username == NULL)
3225 || (se->data.client.password == NULL)))
3227 ERROR ("network plugin: A security level higher than `none' was "
3228 "requested, but no Username or Password option was given. "
3229 "Cowardly refusing to open this socket!");
3230 sockent_destroy (se);
3233 #endif /* HAVE_LIBGCRYPT */
3235 status = sockent_init_crypto (se);
3238 ERROR ("network plugin: network_config_add_server: sockent_init_crypto() failed.");
3239 sockent_destroy (se);
3243 /* No call to sockent_client_connect() here -- it is called from
3244 * networt_send_buffer_plain(). */
3246 status = sockent_add (se);
3249 ERROR ("network plugin: network_config_add_server: sockent_add failed.");
3250 sockent_destroy (se);
3255 } /* }}} int network_config_add_server */
3257 static int network_config (oconfig_item_t *ci) /* {{{ */
3261 /* The options need to be applied first */
3262 for (i = 0; i < ci->children_num; i++)
3264 oconfig_item_t *child = ci->children + i;
3265 if (strcasecmp ("TimeToLive", child->key) == 0)
3266 network_config_set_ttl (child);
3269 for (i = 0; i < ci->children_num; i++)
3271 oconfig_item_t *child = ci->children + i;
3273 if (strcasecmp ("Listen", child->key) == 0)
3274 network_config_add_listen (child);
3275 else if (strcasecmp ("Server", child->key) == 0)
3276 network_config_add_server (child);
3277 else if (strcasecmp ("TimeToLive", child->key) == 0) {
3278 /* Handled earlier */
3280 else if (strcasecmp ("MaxPacketSize", child->key) == 0)
3281 network_config_set_buffer_size (child);
3282 else if (strcasecmp ("Forward", child->key) == 0)
3283 network_config_set_boolean (child, &network_config_forward);
3284 else if (strcasecmp ("ReportStats", child->key) == 0)
3285 network_config_set_boolean (child, &network_config_stats);
3288 WARNING ("network plugin: Option `%s' is not allowed here.",
3294 } /* }}} int network_config */
3296 static int network_notification (const notification_t *n,
3297 user_data_t __attribute__((unused)) *user_data)
3299 char buffer[network_config_packet_size];
3300 char *buffer_ptr = buffer;
3301 int buffer_free = sizeof (buffer);
3304 if (!check_send_notify_okay (n))
3307 memset (buffer, 0, sizeof (buffer));
3309 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_TIME_HR,
3310 (uint64_t) n->time);
3314 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_SEVERITY,
3315 (uint64_t) n->severity);
3319 if (strlen (n->host) > 0)
3321 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_HOST,
3322 n->host, strlen (n->host));
3327 if (strlen (n->plugin) > 0)
3329 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_PLUGIN,
3330 n->plugin, strlen (n->plugin));
3335 if (strlen (n->plugin_instance) > 0)
3337 status = write_part_string (&buffer_ptr, &buffer_free,
3338 TYPE_PLUGIN_INSTANCE,
3339 n->plugin_instance, strlen (n->plugin_instance));
3344 if (strlen (n->type) > 0)
3346 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE,
3347 n->type, strlen (n->type));
3352 if (strlen (n->type_instance) > 0)
3354 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE_INSTANCE,
3355 n->type_instance, strlen (n->type_instance));
3360 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_MESSAGE,
3361 n->message, strlen (n->message));
3365 network_send_buffer (buffer, sizeof (buffer) - buffer_free);
3368 } /* int network_notification */
3370 static int network_shutdown (void)
3376 /* Kill the listening thread */
3377 if (receive_thread_running != 0)
3379 INFO ("network plugin: Stopping receive thread.");
3380 pthread_kill (receive_thread_id, SIGTERM);
3381 pthread_join (receive_thread_id, NULL /* no return value */);
3382 memset (&receive_thread_id, 0, sizeof (receive_thread_id));
3383 receive_thread_running = 0;
3386 /* Shutdown the dispatching thread */
3387 if (dispatch_thread_running != 0)
3389 INFO ("network plugin: Stopping dispatch thread.");
3390 pthread_mutex_lock (&receive_list_lock);
3391 pthread_cond_broadcast (&receive_list_cond);
3392 pthread_mutex_unlock (&receive_list_lock);
3393 pthread_join (dispatch_thread_id, /* ret = */ NULL);
3394 dispatch_thread_running = 0;
3397 sockent_destroy (listen_sockets);
3399 if (send_buffer_fill > 0)
3402 sfree (send_buffer);
3404 for (se = sending_sockets; se != NULL; se = se->next)
3405 sockent_client_disconnect (se);
3406 sockent_destroy (sending_sockets);
3408 plugin_unregister_config ("network");
3409 plugin_unregister_init ("network");
3410 plugin_unregister_write ("network");
3411 plugin_unregister_shutdown ("network");
3414 } /* int network_shutdown */
3416 static int network_stats_read (void) /* {{{ */
3418 derive_t copy_octets_rx;
3419 derive_t copy_octets_tx;
3420 derive_t copy_packets_rx;
3421 derive_t copy_packets_tx;
3422 derive_t copy_values_dispatched;
3423 derive_t copy_values_not_dispatched;
3424 derive_t copy_values_sent;
3425 derive_t copy_values_not_sent;
3426 derive_t copy_receive_list_length;
3427 value_list_t vl = VALUE_LIST_INIT;
3430 copy_octets_rx = stats_octets_rx;
3431 copy_octets_tx = stats_octets_tx;
3432 copy_packets_rx = stats_packets_rx;
3433 copy_packets_tx = stats_packets_tx;
3434 copy_values_dispatched = stats_values_dispatched;
3435 copy_values_not_dispatched = stats_values_not_dispatched;
3436 copy_values_sent = stats_values_sent;
3437 copy_values_not_sent = stats_values_not_sent;
3438 copy_receive_list_length = receive_list_length;
3440 /* Initialize `vl' */
3444 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
3445 sstrncpy (vl.plugin, "network", sizeof (vl.plugin));
3447 /* Octets received / sent */
3448 vl.values[0].derive = (derive_t) copy_octets_rx;
3449 vl.values[1].derive = (derive_t) copy_octets_tx;
3450 sstrncpy (vl.type, "if_octets", sizeof (vl.type));
3451 plugin_dispatch_values (&vl);
3453 /* Packets received / send */
3454 vl.values[0].derive = (derive_t) copy_packets_rx;
3455 vl.values[1].derive = (derive_t) copy_packets_tx;
3456 sstrncpy (vl.type, "if_packets", sizeof (vl.type));
3457 plugin_dispatch_values (&vl);
3459 /* Values (not) dispatched and (not) send */
3460 sstrncpy (vl.type, "total_values", sizeof (vl.type));
3463 vl.values[0].derive = (derive_t) copy_values_dispatched;
3464 sstrncpy (vl.type_instance, "dispatch-accepted",
3465 sizeof (vl.type_instance));
3466 plugin_dispatch_values (&vl);
3468 vl.values[0].derive = (derive_t) copy_values_not_dispatched;
3469 sstrncpy (vl.type_instance, "dispatch-rejected",
3470 sizeof (vl.type_instance));
3471 plugin_dispatch_values (&vl);
3473 vl.values[0].derive = (derive_t) copy_values_sent;
3474 sstrncpy (vl.type_instance, "send-accepted",
3475 sizeof (vl.type_instance));
3476 plugin_dispatch_values (&vl);
3478 vl.values[0].derive = (derive_t) copy_values_not_sent;
3479 sstrncpy (vl.type_instance, "send-rejected",
3480 sizeof (vl.type_instance));
3481 plugin_dispatch_values (&vl);
3483 /* Receive queue length */
3484 vl.values[0].gauge = (gauge_t) copy_receive_list_length;
3485 sstrncpy (vl.type, "queue_length", sizeof (vl.type));
3486 vl.type_instance[0] = 0;
3487 plugin_dispatch_values (&vl);
3490 } /* }}} int network_stats_read */
3492 static int network_init (void)
3494 static _Bool have_init = 0;
3496 /* Check if we were already initialized. If so, just return - there's
3497 * nothing more to do (for now, that is). */
3503 network_init_gcrypt ();
3506 if (network_config_stats != 0)
3507 plugin_register_read ("network", network_stats_read);
3509 plugin_register_shutdown ("network", network_shutdown);
3511 send_buffer = malloc (network_config_packet_size);
3512 if (send_buffer == NULL)
3514 ERROR ("network plugin: malloc failed.");
3517 network_init_buffer ();
3519 /* setup socket(s) and so on */
3520 if (sending_sockets != NULL)
3522 plugin_register_write ("network", network_write,
3523 /* user_data = */ NULL);
3524 plugin_register_notification ("network", network_notification,
3525 /* user_data = */ NULL);
3528 /* If no threads need to be started, return here. */
3529 if ((listen_sockets_num == 0)
3530 || ((dispatch_thread_running != 0)
3531 && (receive_thread_running != 0)))
3534 if (dispatch_thread_running == 0)
3537 status = plugin_thread_create (&dispatch_thread_id,
3538 NULL /* no attributes */,
3540 NULL /* no argument */);
3544 ERROR ("network: pthread_create failed: %s",
3545 sstrerror (errno, errbuf,
3550 dispatch_thread_running = 1;
3554 if (receive_thread_running == 0)
3557 status = plugin_thread_create (&receive_thread_id,
3558 NULL /* no attributes */,
3560 NULL /* no argument */);
3564 ERROR ("network: pthread_create failed: %s",
3565 sstrerror (errno, errbuf,
3570 receive_thread_running = 1;
3575 } /* int network_init */
3578 * The flush option of the network plugin cannot flush individual identifiers.
3579 * All the values are added to a buffer and sent when the buffer is full, the
3580 * requested value may or may not be in there, it's not worth finding out. We
3581 * just send the buffer if `flush' is called - if the requested value was in
3582 * there, good. If not, well, then there is nothing to flush.. -octo
3584 static int network_flush (__attribute__((unused)) cdtime_t timeout,
3585 __attribute__((unused)) const char *identifier,
3586 __attribute__((unused)) user_data_t *user_data)
3588 pthread_mutex_lock (&send_buffer_lock);
3590 if (send_buffer_fill > 0)
3593 pthread_mutex_unlock (&send_buffer_lock);
3596 } /* int network_flush */
3598 void module_register (void)
3600 plugin_register_complex_config ("network", network_config);
3601 plugin_register_init ("network", network_init);
3602 plugin_register_flush ("network", network_flush,
3603 /* user_data = */ NULL);
3604 } /* void module_register */
3606 /* vim: set fdm=marker : */