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_cache.h"
34 #include "utils_complain.h"
42 # include <netinet/in.h>
45 # include <arpa/inet.h>
55 # if defined __APPLE__
56 /* default xcode compiler throws warnings even when deprecated functionality
57 * is not used. -Werror breaks the build because of erroneous warnings.
58 * http://stackoverflow.com/questions/10556299/compiler-warnings-with-libgcrypt-v1-5-0/12830209#12830209
60 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
62 /* FreeBSD's copy of libgcrypt extends the existing GCRYPT_NO_DEPRECATED
63 * to properly hide all deprecated functionality.
64 * http://svnweb.freebsd.org/ports/head/security/libgcrypt/files/patch-src__gcrypt.h.in
66 # define GCRYPT_NO_DEPRECATED
68 # if defined __APPLE__
69 /* Re enable deprecation warnings */
70 # pragma GCC diagnostic warning "-Wdeprecated-declarations"
72 # if GCRYPT_VERSION_NUMBER < 0x010600
73 GCRY_THREAD_OPTION_PTHREAD_IMPL;
77 #ifndef IPV6_ADD_MEMBERSHIP
78 # ifdef IPV6_JOIN_GROUP
79 # define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
81 # error "Neither IP_ADD_MEMBERSHIP nor IPV6_JOIN_GROUP is defined"
83 #endif /* !IP_ADD_MEMBERSHIP */
86 * Maximum size required for encryption / signing:
88 * 42 bytes for the encryption header
89 * + 64 bytes for the username
93 #define BUFF_SIG_SIZE 106
98 #define SECURITY_LEVEL_NONE 0
100 # define SECURITY_LEVEL_SIGN 1
101 # define SECURITY_LEVEL_ENCRYPT 2
103 struct sockent_client
106 struct sockaddr_storage *addr;
112 gcry_cipher_hd_t cypher;
113 unsigned char password_hash[32];
115 cdtime_t next_resolve_reconnect;
116 cdtime_t resolve_interval;
119 struct sockent_server
127 gcry_cipher_hd_t cypher;
131 typedef struct sockent
133 #define SOCKENT_TYPE_CLIENT 1
134 #define SOCKENT_TYPE_SERVER 2
143 struct sockent_client client;
144 struct sockent_server server;
147 struct sockent *next;
150 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
151 * 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
152 * +-------+-----------------------+-------------------------------+
153 * ! Ver. ! ! Length !
154 * +-------+-----------------------+-------------------------------+
161 typedef struct part_header_s part_header_t;
163 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
164 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
165 * +-------------------------------+-------------------------------+
167 * +-------------------------------+-------------------------------+
168 * : (Length - 4) Bytes :
169 * +---------------------------------------------------------------+
176 typedef struct part_string_s part_string_t;
178 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
179 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
180 * +-------------------------------+-------------------------------+
182 * +-------------------------------+-------------------------------+
183 * : (Length - 4 == 2 || 4 || 8) Bytes :
184 * +---------------------------------------------------------------+
191 typedef struct part_number_s part_number_t;
193 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
194 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
195 * +-------------------------------+-------------------------------+
197 * +-------------------------------+---------------+---------------+
198 * ! Num of values ! Type0 ! Type1 !
199 * +-------------------------------+---------------+---------------+
202 * +---------------------------------------------------------------+
205 * +---------------------------------------------------------------+
210 uint16_t *num_values;
211 uint8_t *values_types;
214 typedef struct part_values_s part_values_t;
216 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
217 * 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
218 * +-------------------------------+-------------------------------+
220 * +-------------------------------+-------------------------------+
221 * ! Hash (Bits 0 - 31) !
223 * ! Hash (Bits 224 - 255) !
224 * +---------------------------------------------------------------+
227 #define PART_SIGNATURE_SHA256_SIZE 36
228 struct part_signature_sha256_s
231 unsigned char hash[32];
234 typedef struct part_signature_sha256_s part_signature_sha256_t;
236 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
237 * 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
238 * +-------------------------------+-------------------------------+
240 * +-------------------------------+-------------------------------+
241 * ! Original length ! Padding (0 - 15 bytes) !
242 * +-------------------------------+-------------------------------+
243 * ! Hash (Bits 0 - 31) !
245 * ! Hash (Bits 128 - 159) !
246 * +---------------------------------------------------------------+
249 #define PART_ENCRYPTION_AES256_SIZE 42
250 struct part_encryption_aes256_s
253 uint16_t username_length;
255 unsigned char iv[16];
257 unsigned char hash[20];
261 typedef struct part_encryption_aes256_s part_encryption_aes256_t;
263 struct receive_list_entry_s
268 struct receive_list_entry_s *next;
270 typedef struct receive_list_entry_s receive_list_entry_t;
275 static int network_config_ttl = 0;
276 /* Ethernet - (IPv6 + UDP) = 1500 - (40 + 8) = 1452 */
277 static size_t network_config_packet_size = 1452;
278 static _Bool network_config_forward = 0;
279 static _Bool network_config_stats = 0;
281 static sockent_t *sending_sockets = NULL;
283 static receive_list_entry_t *receive_list_head = NULL;
284 static receive_list_entry_t *receive_list_tail = NULL;
285 static pthread_mutex_t receive_list_lock = PTHREAD_MUTEX_INITIALIZER;
286 static pthread_cond_t receive_list_cond = PTHREAD_COND_INITIALIZER;
287 static uint64_t receive_list_length = 0;
289 static sockent_t *listen_sockets = NULL;
290 static struct pollfd *listen_sockets_pollfd = NULL;
291 static size_t listen_sockets_num = 0;
293 /* The receive and dispatch threads will run as long as `listen_loop' is set to
295 static int listen_loop = 0;
296 static int receive_thread_running = 0;
297 static pthread_t receive_thread_id;
298 static int dispatch_thread_running = 0;
299 static pthread_t dispatch_thread_id;
301 /* Buffer in which to-be-sent network packets are constructed. */
302 static char *send_buffer;
303 static char *send_buffer_ptr;
304 static int send_buffer_fill;
305 static cdtime_t send_buffer_last_update;
306 static value_list_t send_buffer_vl = VALUE_LIST_STATIC;
307 static pthread_mutex_t send_buffer_lock = PTHREAD_MUTEX_INITIALIZER;
309 /* XXX: These counters are incremented from one place only. The spot in which
310 * the values are incremented is either only reachable by one thread (the
311 * dispatch thread, for example) or locked by some lock (send_buffer_lock for
312 * example). Only if neither is true, the stats_lock is acquired. The counters
313 * are always read without holding a lock in the hope that writing 8 bytes to
314 * memory is an atomic operation. */
315 static derive_t stats_octets_rx = 0;
316 static derive_t stats_octets_tx = 0;
317 static derive_t stats_packets_rx = 0;
318 static derive_t stats_packets_tx = 0;
319 static derive_t stats_values_dispatched = 0;
320 static derive_t stats_values_not_dispatched = 0;
321 static derive_t stats_values_sent = 0;
322 static derive_t stats_values_not_sent = 0;
323 static pthread_mutex_t stats_lock = PTHREAD_MUTEX_INITIALIZER;
328 static _Bool check_receive_okay (const value_list_t *vl) /* {{{ */
330 uint64_t time_sent = 0;
333 status = uc_meta_data_get_unsigned_int (vl,
334 "network:time_sent", &time_sent);
336 /* This is a value we already sent. Don't allow it to be received again in
337 * order to avoid looping. */
338 if ((status == 0) && (time_sent >= ((uint64_t) vl->time)))
342 } /* }}} _Bool check_receive_okay */
344 static _Bool check_send_okay (const value_list_t *vl) /* {{{ */
349 if (network_config_forward)
352 if (vl->meta == NULL)
355 status = meta_data_get_boolean (vl->meta, "network:received", &received);
356 if (status == -ENOENT)
358 else if (status != 0)
360 ERROR ("network plugin: check_send_okay: meta_data_get_boolean failed "
361 "with status %i.", status);
365 /* By default, only *send* value lists that were not *received* by the
368 } /* }}} _Bool check_send_okay */
370 static _Bool check_notify_received (const notification_t *n) /* {{{ */
372 notification_meta_t *ptr;
374 for (ptr = n->meta; ptr != NULL; ptr = ptr->next)
375 if ((strcmp ("network:received", ptr->name) == 0)
376 && (ptr->type == NM_TYPE_BOOLEAN))
377 return ((_Bool) ptr->nm_value.nm_boolean);
380 } /* }}} _Bool check_notify_received */
382 static _Bool check_send_notify_okay (const notification_t *n) /* {{{ */
384 static c_complain_t complain_forwarding = C_COMPLAIN_INIT_STATIC;
390 received = check_notify_received (n);
392 if (network_config_forward && received)
394 c_complain_once (LOG_ERR, &complain_forwarding,
395 "network plugin: A notification has been received via the network "
396 "and forwarding is enabled. Forwarding of notifications is currently "
397 "not supported, because there is not loop-deteciton available. "
398 "Please contact the collectd mailing list if you need this "
402 /* By default, only *send* value lists that were not *received* by the
405 } /* }}} _Bool check_send_notify_okay */
407 static int network_dispatch_values (value_list_t *vl, /* {{{ */
408 const char *username)
413 || (strlen (vl->host) == 0)
414 || (strlen (vl->plugin) == 0)
415 || (strlen (vl->type) == 0))
418 if (!check_receive_okay (vl))
421 char name[6*DATA_MAX_NAME_LEN];
422 FORMAT_VL (name, sizeof (name), vl);
423 name[sizeof (name) - 1] = 0;
424 DEBUG ("network plugin: network_dispatch_values: "
425 "NOT dispatching %s.", name);
427 stats_values_not_dispatched++;
431 assert (vl->meta == NULL);
433 vl->meta = meta_data_create ();
434 if (vl->meta == NULL)
436 ERROR ("network plugin: meta_data_create failed.");
440 status = meta_data_add_boolean (vl->meta, "network:received", 1);
443 ERROR ("network plugin: meta_data_add_boolean failed.");
444 meta_data_destroy (vl->meta);
449 if (username != NULL)
451 status = meta_data_add_string (vl->meta, "network:username", username);
454 ERROR ("network plugin: meta_data_add_string failed.");
455 meta_data_destroy (vl->meta);
461 plugin_dispatch_values (vl);
462 stats_values_dispatched++;
464 meta_data_destroy (vl->meta);
468 } /* }}} int network_dispatch_values */
470 static int network_dispatch_notification (notification_t *n) /* {{{ */
474 assert (n->meta == NULL);
476 status = plugin_notification_meta_add_boolean (n, "network:received", 1);
479 ERROR ("network plugin: plugin_notification_meta_add_boolean failed.");
480 plugin_notification_meta_free (n->meta);
485 status = plugin_dispatch_notification (n);
487 plugin_notification_meta_free (n->meta);
491 } /* }}} int network_dispatch_notification */
494 static void network_init_gcrypt (void) /* {{{ */
498 /* http://lists.gnupg.org/pipermail/gcrypt-devel/2003-August/000458.html
499 * Because you can't know in a library whether another library has
500 * already initialized the library */
501 if (gcry_control (GCRYCTL_ANY_INITIALIZATION_P))
504 /* http://www.gnupg.org/documentation/manuals/gcrypt/Multi_002dThreading.html
505 * To ensure thread-safety, it's important to set GCRYCTL_SET_THREAD_CBS
506 * *before* initalizing Libgcrypt with gcry_check_version(), which itself must
507 * be called before any other gcry_* function. GCRYCTL_ANY_INITIALIZATION_P
508 * above doesn't count, as it doesn't implicitly initalize Libgcrypt.
510 * tl;dr: keep all these gry_* statements in this exact order please. */
511 # if GCRYPT_VERSION_NUMBER < 0x010600
512 err = gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
515 ERROR ("network plugin: gcry_control (GCRYCTL_SET_THREAD_CBS) failed: %s", gcry_strerror (err));
520 gcry_check_version (NULL);
522 err = gcry_control (GCRYCTL_INIT_SECMEM, 32768);
525 ERROR ("network plugin: gcry_control (GCRYCTL_INIT_SECMEM) failed: %s", gcry_strerror (err));
529 gcry_control (GCRYCTL_INITIALIZATION_FINISHED);
530 } /* }}} void network_init_gcrypt */
532 static gcry_cipher_hd_t network_get_aes256_cypher (sockent_t *se, /* {{{ */
533 const void *iv, size_t iv_size, const char *username)
536 gcry_cipher_hd_t *cyper_ptr;
537 unsigned char password_hash[32];
539 if (se->type == SOCKENT_TYPE_CLIENT)
541 cyper_ptr = &se->data.client.cypher;
542 memcpy (password_hash, se->data.client.password_hash,
543 sizeof (password_hash));
549 cyper_ptr = &se->data.server.cypher;
551 if (username == NULL)
554 secret = fbh_get (se->data.server.userdb, username);
558 gcry_md_hash_buffer (GCRY_MD_SHA256,
560 secret, strlen (secret));
565 if (*cyper_ptr == NULL)
567 err = gcry_cipher_open (cyper_ptr,
568 GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_OFB, /* flags = */ 0);
571 ERROR ("network plugin: gcry_cipher_open returned: %s",
572 gcry_strerror (err));
579 gcry_cipher_reset (*cyper_ptr);
581 assert (*cyper_ptr != NULL);
583 err = gcry_cipher_setkey (*cyper_ptr,
584 password_hash, sizeof (password_hash));
587 ERROR ("network plugin: gcry_cipher_setkey returned: %s",
588 gcry_strerror (err));
589 gcry_cipher_close (*cyper_ptr);
594 err = gcry_cipher_setiv (*cyper_ptr, iv, iv_size);
597 ERROR ("network plugin: gcry_cipher_setkey returned: %s",
598 gcry_strerror (err));
599 gcry_cipher_close (*cyper_ptr);
605 } /* }}} int network_get_aes256_cypher */
606 #endif /* HAVE_LIBGCRYPT */
608 static int write_part_values (char **ret_buffer, int *ret_buffer_len,
609 const data_set_t *ds, const value_list_t *vl)
615 part_header_t pkg_ph;
616 uint16_t pkg_num_values;
617 uint8_t *pkg_values_types;
623 num_values = vl->values_len;
624 packet_len = sizeof (part_header_t) + sizeof (uint16_t)
625 + (num_values * sizeof (uint8_t))
626 + (num_values * sizeof (value_t));
628 if (*ret_buffer_len < packet_len)
631 pkg_values_types = malloc (num_values * sizeof (*pkg_values_types));
632 if (pkg_values_types == NULL)
634 ERROR ("network plugin: write_part_values: malloc failed.");
638 pkg_values = malloc (num_values * sizeof (*pkg_values));
639 if (pkg_values == NULL)
641 free (pkg_values_types);
642 ERROR ("network plugin: write_part_values: malloc failed.");
646 pkg_ph.type = htons (TYPE_VALUES);
647 pkg_ph.length = htons (packet_len);
649 pkg_num_values = htons ((uint16_t) vl->values_len);
651 for (i = 0; i < num_values; i++)
653 pkg_values_types[i] = (uint8_t) ds->ds[i].type;
654 switch (ds->ds[i].type)
656 case DS_TYPE_COUNTER:
657 pkg_values[i].counter = htonll (vl->values[i].counter);
661 pkg_values[i].gauge = htond (vl->values[i].gauge);
665 pkg_values[i].derive = htonll (vl->values[i].derive);
668 case DS_TYPE_ABSOLUTE:
669 pkg_values[i].absolute = htonll (vl->values[i].absolute);
673 free (pkg_values_types);
675 ERROR ("network plugin: write_part_values: "
676 "Unknown data source type: %i",
679 } /* switch (ds->ds[i].type) */
680 } /* for (num_values) */
683 * Use `memcpy' to write everything to the buffer, because the pointer
684 * may be unaligned and some architectures, such as SPARC, can't handle
687 packet_ptr = *ret_buffer;
689 memcpy (packet_ptr + offset, &pkg_ph, sizeof (pkg_ph));
690 offset += sizeof (pkg_ph);
691 memcpy (packet_ptr + offset, &pkg_num_values, sizeof (pkg_num_values));
692 offset += sizeof (pkg_num_values);
693 memcpy (packet_ptr + offset, pkg_values_types, num_values * sizeof (uint8_t));
694 offset += num_values * sizeof (uint8_t);
695 memcpy (packet_ptr + offset, pkg_values, num_values * sizeof (value_t));
696 offset += num_values * sizeof (value_t);
698 assert (offset == packet_len);
700 *ret_buffer = packet_ptr + packet_len;
701 *ret_buffer_len -= packet_len;
703 free (pkg_values_types);
707 } /* int write_part_values */
709 static int write_part_number (char **ret_buffer, int *ret_buffer_len,
710 int type, uint64_t value)
715 part_header_t pkg_head;
720 packet_len = sizeof (pkg_head) + sizeof (pkg_value);
722 if (*ret_buffer_len < packet_len)
725 pkg_head.type = htons (type);
726 pkg_head.length = htons (packet_len);
727 pkg_value = htonll (value);
729 packet_ptr = *ret_buffer;
731 memcpy (packet_ptr + offset, &pkg_head, sizeof (pkg_head));
732 offset += sizeof (pkg_head);
733 memcpy (packet_ptr + offset, &pkg_value, sizeof (pkg_value));
734 offset += sizeof (pkg_value);
736 assert (offset == packet_len);
738 *ret_buffer = packet_ptr + packet_len;
739 *ret_buffer_len -= packet_len;
742 } /* int write_part_number */
744 static int write_part_string (char **ret_buffer, int *ret_buffer_len,
745 int type, const char *str, int str_len)
755 buffer_len = 2 * sizeof (uint16_t) + str_len + 1;
756 if (*ret_buffer_len < buffer_len)
759 pkg_type = htons (type);
760 pkg_length = htons (buffer_len);
762 buffer = *ret_buffer;
764 memcpy (buffer + offset, (void *) &pkg_type, sizeof (pkg_type));
765 offset += sizeof (pkg_type);
766 memcpy (buffer + offset, (void *) &pkg_length, sizeof (pkg_length));
767 offset += sizeof (pkg_length);
768 memcpy (buffer + offset, str, str_len);
770 memset (buffer + offset, '\0', 1);
773 assert (offset == buffer_len);
775 *ret_buffer = buffer + buffer_len;
776 *ret_buffer_len -= buffer_len;
779 } /* int write_part_string */
781 static int parse_part_values (void **ret_buffer, size_t *ret_buffer_len,
782 value_t **ret_values, size_t *ret_num_values)
784 char *buffer = *ret_buffer;
785 size_t buffer_len = *ret_buffer_len;
800 NOTICE ("network plugin: packet is too short: "
801 "buffer_len = %zu", buffer_len);
805 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
806 buffer += sizeof (tmp16);
807 pkg_type = ntohs (tmp16);
809 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
810 buffer += sizeof (tmp16);
811 pkg_length = ntohs (tmp16);
813 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
814 buffer += sizeof (tmp16);
815 pkg_numval = (size_t) ntohs (tmp16);
817 assert (pkg_type == TYPE_VALUES);
819 exp_size = 3 * sizeof (uint16_t)
820 + pkg_numval * (sizeof (uint8_t) + sizeof (value_t));
821 if (buffer_len < exp_size)
823 WARNING ("network plugin: parse_part_values: "
825 "Chunk of size %zu expected, "
826 "but buffer has only %zu bytes left.",
827 exp_size, buffer_len);
830 assert (pkg_numval <= ((buffer_len - 6) / 9));
832 if (pkg_length != exp_size)
834 WARNING ("network plugin: parse_part_values: "
835 "Length and number of values "
836 "in the packet don't match.");
840 pkg_types = calloc (pkg_numval, sizeof (*pkg_types));
841 pkg_values = calloc (pkg_numval, sizeof (*pkg_values));
842 if ((pkg_types == NULL) || (pkg_values == NULL))
846 ERROR ("network plugin: parse_part_values: calloc failed.");
850 memcpy (pkg_types, buffer, pkg_numval * sizeof (*pkg_types));
851 buffer += pkg_numval * sizeof (*pkg_types);
852 memcpy (pkg_values, buffer, pkg_numval * sizeof (*pkg_values));
853 buffer += pkg_numval * sizeof (*pkg_values);
855 for (i = 0; i < pkg_numval; i++)
857 switch (pkg_types[i])
859 case DS_TYPE_COUNTER:
860 pkg_values[i].counter = (counter_t) ntohll (pkg_values[i].counter);
864 pkg_values[i].gauge = (gauge_t) ntohd (pkg_values[i].gauge);
868 pkg_values[i].derive = (derive_t) ntohll (pkg_values[i].derive);
871 case DS_TYPE_ABSOLUTE:
872 pkg_values[i].absolute = (absolute_t) ntohll (pkg_values[i].absolute);
876 NOTICE ("network plugin: parse_part_values: "
877 "Don't know how to handle data source type %"PRIu8,
882 } /* switch (pkg_types[i]) */
885 *ret_buffer = buffer;
886 *ret_buffer_len = buffer_len - pkg_length;
887 *ret_num_values = pkg_numval;
888 *ret_values = pkg_values;
893 } /* int parse_part_values */
895 static int parse_part_number (void **ret_buffer, size_t *ret_buffer_len,
898 char *buffer = *ret_buffer;
899 size_t buffer_len = *ret_buffer_len;
903 size_t exp_size = 2 * sizeof (uint16_t) + sizeof (uint64_t);
907 if (buffer_len < exp_size)
909 WARNING ("network plugin: parse_part_number: "
911 "Chunk of size %zu expected, "
912 "but buffer has only %zu bytes left.",
913 exp_size, buffer_len);
917 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
918 buffer += sizeof (tmp16);
919 /* pkg_type = ntohs (tmp16); */
921 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
922 buffer += sizeof (tmp16);
923 pkg_length = ntohs (tmp16);
925 memcpy ((void *) &tmp64, buffer, sizeof (tmp64));
926 buffer += sizeof (tmp64);
927 *value = ntohll (tmp64);
929 *ret_buffer = buffer;
930 *ret_buffer_len = buffer_len - pkg_length;
933 } /* int parse_part_number */
935 static int parse_part_string (void **ret_buffer, size_t *ret_buffer_len,
936 char *output, size_t const output_len)
938 char *buffer = *ret_buffer;
939 size_t buffer_len = *ret_buffer_len;
942 size_t const header_size = 2 * sizeof (uint16_t);
950 if (buffer_len < header_size)
952 WARNING ("network plugin: parse_part_string: "
954 "Chunk of at least size %zu expected, "
955 "but buffer has only %zu bytes left.",
956 header_size, buffer_len);
960 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
961 buffer += sizeof (tmp16);
962 /* pkg_type = ntohs (tmp16); */
964 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
965 buffer += sizeof (tmp16);
966 pkg_length = ntohs (tmp16);
967 payload_size = ((size_t) pkg_length) - header_size;
969 /* Check that packet fits in the input buffer */
970 if (pkg_length > buffer_len)
972 WARNING ("network plugin: parse_part_string: "
974 "Chunk of size %"PRIu16" received, "
975 "but buffer has only %zu bytes left.",
976 pkg_length, buffer_len);
980 /* Check that pkg_length is in the valid range */
981 if (pkg_length <= header_size)
983 WARNING ("network plugin: parse_part_string: "
985 "Header claims this packet is only %hu "
986 "bytes long.", pkg_length);
990 /* Check that the package data fits into the output buffer.
991 * The previous if-statement ensures that:
992 * `pkg_length > header_size' */
993 if (output_len < payload_size)
995 WARNING ("network plugin: parse_part_string: "
997 "Output buffer holds %zu bytes, "
998 "which is too small to hold the received "
1000 output_len, payload_size);
1004 /* All sanity checks successfull, let's copy the data over */
1005 memcpy ((void *) output, (void *) buffer, payload_size);
1006 buffer += payload_size;
1008 /* For some very weird reason '\0' doesn't do the trick on SPARC in
1009 * this statement. */
1010 if (output[payload_size - 1] != 0)
1012 WARNING ("network plugin: parse_part_string: "
1013 "Received string does not end "
1014 "with a NULL-byte.");
1018 *ret_buffer = buffer;
1019 *ret_buffer_len = buffer_len - pkg_length;
1022 } /* int parse_part_string */
1024 /* Forward declaration: parse_part_sign_sha256 and parse_part_encr_aes256 call
1025 * parse_packet and vice versa. */
1026 #define PP_SIGNED 0x01
1027 #define PP_ENCRYPTED 0x02
1028 static int parse_packet (sockent_t *se,
1029 void *buffer, size_t buffer_size, int flags,
1030 const char *username);
1032 #define BUFFER_READ(p,s) do { \
1033 memcpy ((p), buffer + buffer_offset, (s)); \
1034 buffer_offset += (s); \
1038 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
1039 void **ret_buffer, size_t *ret_buffer_len, int flags)
1041 static c_complain_t complain_no_users = C_COMPLAIN_INIT_STATIC;
1045 size_t buffer_offset;
1047 size_t username_len;
1050 part_signature_sha256_t pss;
1051 uint16_t pss_head_length;
1052 char hash[sizeof (pss.hash)];
1056 unsigned char *hash_ptr;
1058 buffer = *ret_buffer;
1059 buffer_len = *ret_buffer_len;
1062 if (se->data.server.userdb == NULL)
1064 c_complain (LOG_NOTICE, &complain_no_users,
1065 "network plugin: Received signed network packet but can't verify it "
1066 "because no user DB has been configured. Will accept it.");
1070 /* Check if the buffer has enough data for this structure. */
1071 if (buffer_len <= PART_SIGNATURE_SHA256_SIZE)
1074 /* Read type and length header */
1075 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
1076 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
1077 pss_head_length = ntohs (pss.head.length);
1079 /* Check if the `pss_head_length' is within bounds. */
1080 if ((pss_head_length <= PART_SIGNATURE_SHA256_SIZE)
1081 || (pss_head_length > buffer_len))
1083 ERROR ("network plugin: HMAC-SHA-256 with invalid length received.");
1087 /* Copy the hash. */
1088 BUFFER_READ (pss.hash, sizeof (pss.hash));
1090 /* Calculate username length (without null byte) and allocate memory */
1091 username_len = pss_head_length - PART_SIGNATURE_SHA256_SIZE;
1092 pss.username = malloc (username_len + 1);
1093 if (pss.username == NULL)
1096 /* Read the username */
1097 BUFFER_READ (pss.username, username_len);
1098 pss.username[username_len] = 0;
1100 assert (buffer_offset == pss_head_length);
1102 /* Query the password */
1103 secret = fbh_get (se->data.server.userdb, pss.username);
1106 ERROR ("network plugin: Unknown user: %s", pss.username);
1107 sfree (pss.username);
1111 /* Create a hash device and check the HMAC */
1113 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
1116 ERROR ("network plugin: Creating HMAC-SHA-256 object failed: %s",
1117 gcry_strerror (err));
1119 sfree (pss.username);
1123 err = gcry_md_setkey (hd, secret, strlen (secret));
1126 ERROR ("network plugin: gcry_md_setkey failed: %s", gcry_strerror (err));
1129 sfree (pss.username);
1134 buffer + PART_SIGNATURE_SHA256_SIZE,
1135 buffer_len - PART_SIGNATURE_SHA256_SIZE);
1136 hash_ptr = gcry_md_read (hd, GCRY_MD_SHA256);
1137 if (hash_ptr == NULL)
1139 ERROR ("network plugin: gcry_md_read failed.");
1142 sfree (pss.username);
1145 memcpy (hash, hash_ptr, sizeof (hash));
1151 if (memcmp (pss.hash, hash, sizeof (pss.hash)) != 0)
1153 WARNING ("network plugin: Verifying HMAC-SHA-256 signature failed: "
1158 parse_packet (se, buffer + buffer_offset, buffer_len - buffer_offset,
1159 flags | PP_SIGNED, pss.username);
1163 sfree (pss.username);
1165 *ret_buffer = buffer + buffer_len;
1166 *ret_buffer_len = 0;
1169 } /* }}} int parse_part_sign_sha256 */
1170 /* #endif HAVE_LIBGCRYPT */
1172 #else /* if !HAVE_LIBGCRYPT */
1173 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
1174 void **ret_buffer, size_t *ret_buffer_size, int flags)
1176 static int warning_has_been_printed = 0;
1180 size_t buffer_offset;
1183 part_signature_sha256_t pss;
1185 buffer = *ret_buffer;
1186 buffer_size = *ret_buffer_size;
1189 if (buffer_size <= PART_SIGNATURE_SHA256_SIZE)
1192 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
1193 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
1194 part_len = ntohs (pss.head.length);
1196 if ((part_len <= PART_SIGNATURE_SHA256_SIZE)
1197 || (part_len > buffer_size))
1200 if (warning_has_been_printed == 0)
1202 WARNING ("network plugin: Received signed packet, but the network "
1203 "plugin was not linked with libgcrypt, so I cannot "
1204 "verify the signature. The packet will be accepted.");
1205 warning_has_been_printed = 1;
1208 parse_packet (se, buffer + part_len, buffer_size - part_len, flags,
1209 /* username = */ NULL);
1211 *ret_buffer = buffer + buffer_size;
1212 *ret_buffer_size = 0;
1215 } /* }}} int parse_part_sign_sha256 */
1216 #endif /* !HAVE_LIBGCRYPT */
1219 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1220 void **ret_buffer, size_t *ret_buffer_len,
1223 char *buffer = *ret_buffer;
1224 size_t buffer_len = *ret_buffer_len;
1227 size_t buffer_offset;
1228 uint16_t username_len;
1229 part_encryption_aes256_t pea;
1230 unsigned char hash[sizeof (pea.hash)];
1232 gcry_cipher_hd_t cypher;
1235 /* Make sure at least the header if available. */
1236 if (buffer_len <= PART_ENCRYPTION_AES256_SIZE)
1238 NOTICE ("network plugin: parse_part_encr_aes256: "
1239 "Discarding short packet.");
1245 /* Copy the unencrypted information into `pea'. */
1246 BUFFER_READ (&pea.head.type, sizeof (pea.head.type));
1247 BUFFER_READ (&pea.head.length, sizeof (pea.head.length));
1249 /* Check the `part size'. */
1250 part_size = ntohs (pea.head.length);
1251 if ((part_size <= PART_ENCRYPTION_AES256_SIZE)
1252 || (part_size > buffer_len))
1254 NOTICE ("network plugin: parse_part_encr_aes256: "
1255 "Discarding part with invalid size.");
1259 /* Read the username */
1260 BUFFER_READ (&username_len, sizeof (username_len));
1261 username_len = ntohs (username_len);
1263 if ((username_len == 0)
1264 || (username_len > (part_size - (PART_ENCRYPTION_AES256_SIZE + 1))))
1266 NOTICE ("network plugin: parse_part_encr_aes256: "
1267 "Discarding part with invalid username length.");
1271 assert (username_len > 0);
1272 pea.username = malloc (username_len + 1);
1273 if (pea.username == NULL)
1275 BUFFER_READ (pea.username, username_len);
1276 pea.username[username_len] = 0;
1278 /* Last but not least, the initialization vector */
1279 BUFFER_READ (pea.iv, sizeof (pea.iv));
1281 /* Make sure we are at the right position */
1282 assert (buffer_offset == (username_len +
1283 PART_ENCRYPTION_AES256_SIZE - sizeof (pea.hash)));
1285 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
1289 sfree (pea.username);
1293 payload_len = part_size - (PART_ENCRYPTION_AES256_SIZE + username_len);
1294 assert (payload_len > 0);
1296 /* Decrypt the packet in-place */
1297 err = gcry_cipher_decrypt (cypher,
1298 buffer + buffer_offset,
1299 part_size - buffer_offset,
1300 /* in = */ NULL, /* in len = */ 0);
1303 sfree (pea.username);
1304 ERROR ("network plugin: gcry_cipher_decrypt returned: %s",
1305 gcry_strerror (err));
1310 BUFFER_READ (pea.hash, sizeof (pea.hash));
1312 /* Make sure we're at the right position - again */
1313 assert (buffer_offset == (username_len + PART_ENCRYPTION_AES256_SIZE));
1314 assert (buffer_offset == (part_size - payload_len));
1316 /* Check hash sum */
1317 memset (hash, 0, sizeof (hash));
1318 gcry_md_hash_buffer (GCRY_MD_SHA1, hash,
1319 buffer + buffer_offset, payload_len);
1320 if (memcmp (hash, pea.hash, sizeof (hash)) != 0)
1322 sfree (pea.username);
1323 ERROR ("network plugin: Decryption failed: Checksum mismatch.");
1327 parse_packet (se, buffer + buffer_offset, payload_len,
1328 flags | PP_ENCRYPTED, pea.username);
1330 /* XXX: Free pea.username?!? */
1332 /* Update return values */
1333 *ret_buffer = buffer + part_size;
1334 *ret_buffer_len = buffer_len - part_size;
1336 sfree (pea.username);
1339 } /* }}} int parse_part_encr_aes256 */
1340 /* #endif HAVE_LIBGCRYPT */
1342 #else /* if !HAVE_LIBGCRYPT */
1343 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1344 void **ret_buffer, size_t *ret_buffer_size, int flags)
1346 static int warning_has_been_printed = 0;
1350 size_t buffer_offset;
1355 buffer = *ret_buffer;
1356 buffer_size = *ret_buffer_size;
1359 /* parse_packet assures this minimum size. */
1360 assert (buffer_size >= (sizeof (ph.type) + sizeof (ph.length)));
1362 BUFFER_READ (&ph.type, sizeof (ph.type));
1363 BUFFER_READ (&ph.length, sizeof (ph.length));
1364 ph_length = ntohs (ph.length);
1366 if ((ph_length <= PART_ENCRYPTION_AES256_SIZE)
1367 || (ph_length > buffer_size))
1369 ERROR ("network plugin: AES-256 encrypted part "
1370 "with invalid length received.");
1374 if (warning_has_been_printed == 0)
1376 WARNING ("network plugin: Received encrypted packet, but the network "
1377 "plugin was not linked with libgcrypt, so I cannot "
1378 "decrypt it. The part will be discarded.");
1379 warning_has_been_printed = 1;
1382 *ret_buffer = (void *) (((char *) *ret_buffer) + ph_length);
1383 *ret_buffer_size -= ph_length;
1386 } /* }}} int parse_part_encr_aes256 */
1387 #endif /* !HAVE_LIBGCRYPT */
1391 static int parse_packet (sockent_t *se, /* {{{ */
1392 void *buffer, size_t buffer_size, int flags,
1393 const char *username)
1397 value_list_t vl = VALUE_LIST_INIT;
1401 int packet_was_signed = (flags & PP_SIGNED);
1402 int packet_was_encrypted = (flags & PP_ENCRYPTED);
1403 int printed_ignore_warning = 0;
1404 #endif /* HAVE_LIBGCRYPT */
1407 memset (&vl, '\0', sizeof (vl));
1408 memset (&n, '\0', sizeof (n));
1411 while ((status == 0) && (0 < buffer_size)
1412 && ((unsigned int) buffer_size > sizeof (part_header_t)))
1414 uint16_t pkg_length;
1417 memcpy ((void *) &pkg_type,
1420 memcpy ((void *) &pkg_length,
1421 (void *) (((char *) buffer) + sizeof (pkg_type)),
1422 sizeof (pkg_length));
1424 pkg_length = ntohs (pkg_length);
1425 pkg_type = ntohs (pkg_type);
1427 if (pkg_length > buffer_size)
1429 /* Ensure that this loop terminates eventually */
1430 if (pkg_length < (2 * sizeof (uint16_t)))
1433 if (pkg_type == TYPE_ENCR_AES256)
1435 status = parse_part_encr_aes256 (se,
1436 &buffer, &buffer_size, flags);
1439 ERROR ("network plugin: Decrypting AES256 "
1441 "with status %i.", status);
1446 else if ((se->data.server.security_level == SECURITY_LEVEL_ENCRYPT)
1447 && (packet_was_encrypted == 0))
1449 if (printed_ignore_warning == 0)
1451 INFO ("network plugin: Unencrypted packet or "
1452 "part has been ignored.");
1453 printed_ignore_warning = 1;
1455 buffer = ((char *) buffer) + pkg_length;
1456 buffer_size -= (size_t) pkg_length;
1459 #endif /* HAVE_LIBGCRYPT */
1460 else if (pkg_type == TYPE_SIGN_SHA256)
1462 status = parse_part_sign_sha256 (se,
1463 &buffer, &buffer_size, flags);
1466 ERROR ("network plugin: Verifying HMAC-SHA-256 "
1468 "with status %i.", status);
1473 else if ((se->data.server.security_level == SECURITY_LEVEL_SIGN)
1474 && (packet_was_encrypted == 0)
1475 && (packet_was_signed == 0))
1477 if (printed_ignore_warning == 0)
1479 INFO ("network plugin: Unsigned packet or "
1480 "part has been ignored.");
1481 printed_ignore_warning = 1;
1483 buffer = ((char *) buffer) + pkg_length;
1484 buffer_size -= (size_t) pkg_length;
1487 #endif /* HAVE_LIBGCRYPT */
1488 else if (pkg_type == TYPE_VALUES)
1490 status = parse_part_values (&buffer, &buffer_size,
1491 &vl.values, &vl.values_len);
1495 network_dispatch_values (&vl, username);
1499 else if (pkg_type == TYPE_TIME)
1502 status = parse_part_number (&buffer, &buffer_size,
1506 vl.time = TIME_T_TO_CDTIME_T (tmp);
1507 n.time = TIME_T_TO_CDTIME_T (tmp);
1510 else if (pkg_type == TYPE_TIME_HR)
1513 status = parse_part_number (&buffer, &buffer_size,
1517 vl.time = (cdtime_t) tmp;
1518 n.time = (cdtime_t) tmp;
1521 else if (pkg_type == TYPE_INTERVAL)
1524 status = parse_part_number (&buffer, &buffer_size,
1527 vl.interval = TIME_T_TO_CDTIME_T (tmp);
1529 else if (pkg_type == TYPE_INTERVAL_HR)
1532 status = parse_part_number (&buffer, &buffer_size,
1535 vl.interval = (cdtime_t) tmp;
1537 else if (pkg_type == TYPE_HOST)
1539 status = parse_part_string (&buffer, &buffer_size,
1540 vl.host, sizeof (vl.host));
1542 sstrncpy (n.host, vl.host, sizeof (n.host));
1544 else if (pkg_type == TYPE_PLUGIN)
1546 status = parse_part_string (&buffer, &buffer_size,
1547 vl.plugin, sizeof (vl.plugin));
1549 sstrncpy (n.plugin, vl.plugin,
1552 else if (pkg_type == TYPE_PLUGIN_INSTANCE)
1554 status = parse_part_string (&buffer, &buffer_size,
1556 sizeof (vl.plugin_instance));
1558 sstrncpy (n.plugin_instance,
1560 sizeof (n.plugin_instance));
1562 else if (pkg_type == TYPE_TYPE)
1564 status = parse_part_string (&buffer, &buffer_size,
1565 vl.type, sizeof (vl.type));
1567 sstrncpy (n.type, vl.type, sizeof (n.type));
1569 else if (pkg_type == TYPE_TYPE_INSTANCE)
1571 status = parse_part_string (&buffer, &buffer_size,
1573 sizeof (vl.type_instance));
1575 sstrncpy (n.type_instance, vl.type_instance,
1576 sizeof (n.type_instance));
1578 else if (pkg_type == TYPE_MESSAGE)
1580 status = parse_part_string (&buffer, &buffer_size,
1581 n.message, sizeof (n.message));
1587 else if ((n.severity != NOTIF_FAILURE)
1588 && (n.severity != NOTIF_WARNING)
1589 && (n.severity != NOTIF_OKAY))
1591 INFO ("network plugin: "
1592 "Ignoring notification with "
1593 "unknown severity %i.",
1596 else if (n.time == 0)
1598 INFO ("network plugin: "
1599 "Ignoring notification with "
1602 else if (strlen (n.message) == 0)
1604 INFO ("network plugin: "
1605 "Ignoring notification with "
1606 "an empty message.");
1610 network_dispatch_notification (&n);
1613 else if (pkg_type == TYPE_SEVERITY)
1616 status = parse_part_number (&buffer, &buffer_size,
1619 n.severity = (int) tmp;
1623 DEBUG ("network plugin: parse_packet: Unknown part"
1624 " type: 0x%04hx", pkg_type);
1625 buffer = ((char *) buffer) + pkg_length;
1626 buffer_size -= (size_t) pkg_length;
1628 } /* while (buffer_size > sizeof (part_header_t)) */
1630 if (status == 0 && buffer_size > 0)
1631 WARNING ("network plugin: parse_packet: Received truncated "
1632 "packet, try increasing `MaxPacketSize'");
1635 } /* }}} int parse_packet */
1637 static void free_sockent_client (struct sockent_client *sec) /* {{{ */
1646 sfree (sec->username);
1647 sfree (sec->password);
1648 if (sec->cypher != NULL)
1649 gcry_cipher_close (sec->cypher);
1651 } /* }}} void free_sockent_client */
1653 static void free_sockent_server (struct sockent_server *ses) /* {{{ */
1657 for (i = 0; i < ses->fd_num; i++)
1659 if (ses->fd[i] >= 0)
1668 sfree (ses->auth_file);
1669 fbh_destroy (ses->userdb);
1670 if (ses->cypher != NULL)
1671 gcry_cipher_close (ses->cypher);
1673 } /* }}} void free_sockent_server */
1675 static void sockent_destroy (sockent_t *se) /* {{{ */
1679 DEBUG ("network plugin: sockent_destroy (se = %p);", (void *) se);
1686 sfree (se->service);
1688 if (se->type == SOCKENT_TYPE_CLIENT)
1689 free_sockent_client (&se->data.client);
1691 free_sockent_server (&se->data.server);
1696 } /* }}} void sockent_destroy */
1699 * int network_set_ttl
1701 * Set the `IP_MULTICAST_TTL', `IP_TTL', `IPV6_MULTICAST_HOPS' or
1702 * `IPV6_UNICAST_HOPS', depending on which option is applicable.
1704 * The `struct addrinfo' is used to destinguish between unicast and multicast
1707 static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
1709 DEBUG ("network plugin: network_set_ttl: network_config_ttl = %i;",
1710 network_config_ttl);
1712 assert (se->type == SOCKENT_TYPE_CLIENT);
1714 if ((network_config_ttl < 1) || (network_config_ttl > 255))
1717 if (ai->ai_family == AF_INET)
1719 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1722 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1723 optname = IP_MULTICAST_TTL;
1727 if (setsockopt (se->data.client.fd, IPPROTO_IP, optname,
1728 &network_config_ttl,
1729 sizeof (network_config_ttl)) != 0)
1732 ERROR ("network plugin: setsockopt (ipv4-ttl): %s",
1733 sstrerror (errno, errbuf, sizeof (errbuf)));
1737 else if (ai->ai_family == AF_INET6)
1739 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1740 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1743 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1744 optname = IPV6_MULTICAST_HOPS;
1746 optname = IPV6_UNICAST_HOPS;
1748 if (setsockopt (se->data.client.fd, IPPROTO_IPV6, optname,
1749 &network_config_ttl,
1750 sizeof (network_config_ttl)) != 0)
1753 ERROR ("network plugin: setsockopt(ipv6-ttl): %s",
1754 sstrerror (errno, errbuf,
1761 } /* int network_set_ttl */
1763 static int network_set_interface (const sockent_t *se, const struct addrinfo *ai) /* {{{ */
1765 DEBUG ("network plugin: network_set_interface: interface index = %i;",
1768 assert (se->type == SOCKENT_TYPE_CLIENT);
1770 if (ai->ai_family == AF_INET)
1772 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1774 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1776 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1777 /* If possible, use the "ip_mreqn" structure which has
1778 * an "interface index" member. Using the interface
1779 * index is preferred here, because of its similarity
1780 * to the way IPv6 handles this. Unfortunately, it
1781 * appears not to be portable. */
1782 struct ip_mreqn mreq;
1784 memset (&mreq, 0, sizeof (mreq));
1785 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1786 mreq.imr_address.s_addr = ntohl (INADDR_ANY);
1787 mreq.imr_ifindex = se->interface;
1789 struct ip_mreq mreq;
1791 memset (&mreq, 0, sizeof (mreq));
1792 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1793 mreq.imr_interface.s_addr = ntohl (INADDR_ANY);
1796 if (setsockopt (se->data.client.fd, IPPROTO_IP, IP_MULTICAST_IF,
1797 &mreq, sizeof (mreq)) != 0)
1800 ERROR ("network plugin: setsockopt (ipv4-multicast-if): %s",
1801 sstrerror (errno, errbuf, sizeof (errbuf)));
1808 else if (ai->ai_family == AF_INET6)
1810 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1812 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1814 if (setsockopt (se->data.client.fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
1816 sizeof (se->interface)) != 0)
1819 ERROR ("network plugin: setsockopt (ipv6-multicast-if): %s",
1820 sstrerror (errno, errbuf,
1829 /* else: Not a multicast interface. */
1830 if (se->interface != 0)
1832 #if defined(HAVE_IF_INDEXTONAME) && HAVE_IF_INDEXTONAME && defined(SO_BINDTODEVICE)
1833 char interface_name[IFNAMSIZ];
1835 if (if_indextoname (se->interface, interface_name) == NULL)
1838 DEBUG ("network plugin: Binding socket to interface %s", interface_name);
1840 if (setsockopt (se->data.client.fd, SOL_SOCKET, SO_BINDTODEVICE,
1842 sizeof(interface_name)) == -1 )
1845 ERROR ("network plugin: setsockopt (bind-if): %s",
1846 sstrerror (errno, errbuf, sizeof (errbuf)));
1849 /* #endif HAVE_IF_INDEXTONAME && SO_BINDTODEVICE */
1852 WARNING ("network plugin: Cannot set the interface on a unicast "
1854 # if !defined(SO_BINDTODEVICE)
1855 "the \"SO_BINDTODEVICE\" socket option "
1857 "the \"if_indextoname\" function "
1859 "is not available on your system.");
1865 } /* }}} network_set_interface */
1867 static int network_bind_socket (int fd, const struct addrinfo *ai, const int interface_idx)
1876 /* allow multiple sockets to use the same PORT number */
1877 if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR,
1878 &yes, sizeof(yes)) == -1) {
1880 ERROR ("network plugin: setsockopt (reuseaddr): %s",
1881 sstrerror (errno, errbuf, sizeof (errbuf)));
1885 DEBUG ("fd = %i; calling `bind'", fd);
1887 if (bind (fd, ai->ai_addr, ai->ai_addrlen) == -1)
1891 sstrerror (errno, errbuf, sizeof (errbuf)));
1895 if (ai->ai_family == AF_INET)
1897 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1898 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1900 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1901 struct ip_mreqn mreq;
1903 struct ip_mreq mreq;
1906 DEBUG ("fd = %i; IPv4 multicast address found", fd);
1908 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1909 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1910 /* Set the interface using the interface index if
1911 * possible (available). Unfortunately, the struct
1912 * ip_mreqn is not portable. */
1913 mreq.imr_address.s_addr = ntohl (INADDR_ANY);
1914 mreq.imr_ifindex = interface_idx;
1916 mreq.imr_interface.s_addr = ntohl (INADDR_ANY);
1919 if (setsockopt (fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1920 &loop, sizeof (loop)) == -1)
1923 ERROR ("network plugin: setsockopt (multicast-loop): %s",
1924 sstrerror (errno, errbuf,
1929 if (setsockopt (fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1930 &mreq, sizeof (mreq)) == -1)
1933 ERROR ("network plugin: setsockopt (add-membership): %s",
1934 sstrerror (errno, errbuf,
1942 else if (ai->ai_family == AF_INET6)
1944 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1945 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1946 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1948 struct ipv6_mreq mreq;
1950 DEBUG ("fd = %i; IPv6 multicast address found", fd);
1952 memcpy (&mreq.ipv6mr_multiaddr,
1954 sizeof (addr->sin6_addr));
1956 /* http://developer.apple.com/documentation/Darwin/Reference/ManPages/man4/ip6.4.html
1957 * ipv6mr_interface may be set to zeroes to
1958 * choose the default multicast interface or to
1959 * the index of a particular multicast-capable
1960 * interface if the host is multihomed.
1961 * Membership is associ-associated with a
1962 * single interface; programs running on
1963 * multihomed hosts may need to join the same
1964 * group on more than one interface.*/
1965 mreq.ipv6mr_interface = interface_idx;
1967 if (setsockopt (fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
1968 &loop, sizeof (loop)) == -1)
1971 ERROR ("network plugin: setsockopt (ipv6-multicast-loop): %s",
1972 sstrerror (errno, errbuf,
1977 if (setsockopt (fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
1978 &mreq, sizeof (mreq)) == -1)
1981 ERROR ("network plugin: setsockopt (ipv6-add-membership): %s",
1982 sstrerror (errno, errbuf,
1991 #if defined(HAVE_IF_INDEXTONAME) && HAVE_IF_INDEXTONAME && defined(SO_BINDTODEVICE)
1992 /* if a specific interface was set, bind the socket to it. But to avoid
1993 * possible problems with multicast routing, only do that for non-multicast
1995 if (interface_idx != 0)
1997 char interface_name[IFNAMSIZ];
1999 if (if_indextoname (interface_idx, interface_name) == NULL)
2002 DEBUG ("fd = %i; Binding socket to interface %s", fd, interface_name);
2004 if (setsockopt (fd, SOL_SOCKET, SO_BINDTODEVICE,
2006 sizeof(interface_name)) == -1 )
2009 ERROR ("network plugin: setsockopt (bind-if): %s",
2010 sstrerror (errno, errbuf, sizeof (errbuf)));
2014 #endif /* HAVE_IF_INDEXTONAME && SO_BINDTODEVICE */
2017 } /* int network_bind_socket */
2019 /* Initialize a sockent structure. `type' must be either `SOCKENT_TYPE_CLIENT'
2020 * or `SOCKENT_TYPE_SERVER' */
2021 static sockent_t *sockent_create (int type) /* {{{ */
2025 if ((type != SOCKENT_TYPE_CLIENT) && (type != SOCKENT_TYPE_SERVER))
2028 se = calloc (1, sizeof (*se));
2038 if (type == SOCKENT_TYPE_SERVER)
2040 se->data.server.fd = NULL;
2041 se->data.server.fd_num = 0;
2043 se->data.server.security_level = SECURITY_LEVEL_NONE;
2044 se->data.server.auth_file = NULL;
2045 se->data.server.userdb = NULL;
2046 se->data.server.cypher = NULL;
2051 se->data.client.fd = -1;
2052 se->data.client.addr = NULL;
2053 se->data.client.resolve_interval = 0;
2054 se->data.client.next_resolve_reconnect = 0;
2056 se->data.client.security_level = SECURITY_LEVEL_NONE;
2057 se->data.client.username = NULL;
2058 se->data.client.password = NULL;
2059 se->data.client.cypher = NULL;
2064 } /* }}} sockent_t *sockent_create */
2066 static int sockent_init_crypto (sockent_t *se) /* {{{ */
2068 #if HAVE_LIBGCRYPT /* {{{ */
2069 if (se->type == SOCKENT_TYPE_CLIENT)
2071 if (se->data.client.security_level > SECURITY_LEVEL_NONE)
2073 network_init_gcrypt ();
2075 if ((se->data.client.username == NULL)
2076 || (se->data.client.password == NULL))
2078 ERROR ("network plugin: Client socket with "
2079 "security requested, but no "
2080 "credentials are configured.");
2083 gcry_md_hash_buffer (GCRY_MD_SHA256,
2084 se->data.client.password_hash,
2085 se->data.client.password,
2086 strlen (se->data.client.password));
2089 else /* (se->type == SOCKENT_TYPE_SERVER) */
2091 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
2093 network_init_gcrypt ();
2095 if (se->data.server.auth_file == NULL)
2097 ERROR ("network plugin: Server socket with "
2098 "security requested, but no "
2099 "password file is configured.");
2103 if (se->data.server.auth_file != NULL)
2105 se->data.server.userdb = fbh_create (se->data.server.auth_file);
2106 if (se->data.server.userdb == NULL)
2108 ERROR ("network plugin: Reading password file "
2110 se->data.server.auth_file);
2111 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
2116 #endif /* }}} HAVE_LIBGCRYPT */
2119 } /* }}} int sockent_init_crypto */
2121 static int sockent_client_disconnect (sockent_t *se) /* {{{ */
2123 struct sockent_client *client;
2125 if ((se == NULL) || (se->type != SOCKENT_TYPE_CLIENT))
2128 client = &se->data.client;
2129 if (client->fd >= 0) /* connected */
2135 sfree (client->addr);
2136 client->addrlen = 0;
2139 } /* }}} int sockent_client_disconnect */
2141 static int sockent_client_connect (sockent_t *se) /* {{{ */
2143 static c_complain_t complaint = C_COMPLAIN_INIT_STATIC;
2145 struct sockent_client *client;
2146 struct addrinfo ai_hints;
2147 struct addrinfo *ai_list = NULL, *ai_ptr;
2149 _Bool reconnect = 0;
2152 if ((se == NULL) || (se->type != SOCKENT_TYPE_CLIENT))
2155 client = &se->data.client;
2158 if (client->resolve_interval != 0 && client->next_resolve_reconnect < now) {
2159 DEBUG("network plugin: Reconnecting socket, resolve_interval = %lf, next_resolve_reconnect = %lf",
2160 CDTIME_T_TO_DOUBLE(client->resolve_interval), CDTIME_T_TO_DOUBLE(client->next_resolve_reconnect));
2164 if (client->fd >= 0 && !reconnect) /* already connected and not stale*/
2167 memset (&ai_hints, 0, sizeof (ai_hints));
2168 #ifdef AI_ADDRCONFIG
2169 ai_hints.ai_flags |= AI_ADDRCONFIG;
2171 ai_hints.ai_family = AF_UNSPEC;
2172 ai_hints.ai_socktype = SOCK_DGRAM;
2173 ai_hints.ai_protocol = IPPROTO_UDP;
2175 status = getaddrinfo (se->node,
2176 (se->service != NULL) ? se->service : NET_DEFAULT_PORT,
2177 &ai_hints, &ai_list);
2180 c_complain (LOG_ERR, &complaint,
2181 "network plugin: getaddrinfo (%s, %s) failed: %s",
2182 (se->node == NULL) ? "(null)" : se->node,
2183 (se->service == NULL) ? "(null)" : se->service,
2184 gai_strerror (status));
2189 c_release (LOG_NOTICE, &complaint,
2190 "network plugin: Successfully resolved \"%s\".",
2194 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
2196 if (client->fd >= 0) /* when we reconnect */
2197 sockent_client_disconnect(se);
2199 client->fd = socket (ai_ptr->ai_family,
2200 ai_ptr->ai_socktype,
2201 ai_ptr->ai_protocol);
2205 ERROR ("network plugin: socket(2) failed: %s",
2206 sstrerror (errno, errbuf,
2211 client->addr = calloc (1, sizeof (*client->addr));
2212 if (client->addr == NULL)
2214 ERROR ("network plugin: calloc failed.");
2220 assert (sizeof (*client->addr) >= ai_ptr->ai_addrlen);
2221 memcpy (client->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
2222 client->addrlen = ai_ptr->ai_addrlen;
2224 network_set_ttl (se, ai_ptr);
2225 network_set_interface (se, ai_ptr);
2227 /* We don't open more than one write-socket per
2228 * node/service pair.. */
2232 freeaddrinfo (ai_list);
2236 if (client->resolve_interval > 0)
2237 client->next_resolve_reconnect = now + client->resolve_interval;
2239 } /* }}} int sockent_client_connect */
2241 /* Open the file descriptors for a initialized sockent structure. */
2242 static int sockent_server_listen (sockent_t *se) /* {{{ */
2244 struct addrinfo ai_hints;
2245 struct addrinfo *ai_list, *ai_ptr;
2249 const char *service;
2254 assert (se->data.server.fd == NULL);
2255 assert (se->data.server.fd_num == 0);
2258 service = se->service;
2260 if (service == NULL)
2261 service = NET_DEFAULT_PORT;
2263 DEBUG ("network plugin: sockent_server_listen: node = %s; service = %s;",
2266 memset (&ai_hints, 0, sizeof (ai_hints));
2267 ai_hints.ai_flags = 0;
2269 ai_hints.ai_flags |= AI_PASSIVE;
2271 #ifdef AI_ADDRCONFIG
2272 ai_hints.ai_flags |= AI_ADDRCONFIG;
2274 ai_hints.ai_family = AF_UNSPEC;
2275 ai_hints.ai_socktype = SOCK_DGRAM;
2276 ai_hints.ai_protocol = IPPROTO_UDP;
2278 status = getaddrinfo (node, service, &ai_hints, &ai_list);
2281 ERROR ("network plugin: getaddrinfo (%s, %s) failed: %s",
2282 (se->node == NULL) ? "(null)" : se->node,
2283 (se->service == NULL) ? "(null)" : se->service,
2284 gai_strerror (status));
2288 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
2292 tmp = realloc (se->data.server.fd,
2293 sizeof (*tmp) * (se->data.server.fd_num + 1));
2296 ERROR ("network plugin: realloc failed.");
2299 se->data.server.fd = tmp;
2300 tmp = se->data.server.fd + se->data.server.fd_num;
2302 *tmp = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
2303 ai_ptr->ai_protocol);
2307 ERROR ("network plugin: socket(2) failed: %s",
2308 sstrerror (errno, errbuf,
2313 status = network_bind_socket (*tmp, ai_ptr, se->interface);
2321 se->data.server.fd_num++;
2323 } /* for (ai_list) */
2325 freeaddrinfo (ai_list);
2327 if (se->data.server.fd_num == 0)
2330 } /* }}} int sockent_server_listen */
2332 /* Add a sockent to the global list of sockets */
2333 static int sockent_add (sockent_t *se) /* {{{ */
2335 sockent_t *last_ptr;
2340 if (se->type == SOCKENT_TYPE_SERVER)
2345 tmp = realloc (listen_sockets_pollfd,
2346 sizeof (*tmp) * (listen_sockets_num
2347 + se->data.server.fd_num));
2350 ERROR ("network plugin: realloc failed.");
2353 listen_sockets_pollfd = tmp;
2354 tmp = listen_sockets_pollfd + listen_sockets_num;
2356 for (i = 0; i < se->data.server.fd_num; i++)
2358 memset (tmp + i, 0, sizeof (*tmp));
2359 tmp[i].fd = se->data.server.fd[i];
2360 tmp[i].events = POLLIN | POLLPRI;
2364 listen_sockets_num += se->data.server.fd_num;
2366 if (listen_sockets == NULL)
2368 listen_sockets = se;
2371 last_ptr = listen_sockets;
2373 else /* if (se->type == SOCKENT_TYPE_CLIENT) */
2375 if (sending_sockets == NULL)
2377 sending_sockets = se;
2380 last_ptr = sending_sockets;
2383 while (last_ptr->next != NULL)
2384 last_ptr = last_ptr->next;
2385 last_ptr->next = se;
2388 } /* }}} int sockent_add */
2390 static void *dispatch_thread (void __attribute__((unused)) *arg) /* {{{ */
2394 receive_list_entry_t *ent;
2397 /* Lock and wait for more data to come in */
2398 pthread_mutex_lock (&receive_list_lock);
2399 while ((listen_loop == 0)
2400 && (receive_list_head == NULL))
2401 pthread_cond_wait (&receive_list_cond, &receive_list_lock);
2403 /* Remove the head entry and unlock */
2404 ent = receive_list_head;
2406 receive_list_head = ent->next;
2407 receive_list_length--;
2408 pthread_mutex_unlock (&receive_list_lock);
2410 /* Check whether we are supposed to exit. We do NOT check `listen_loop'
2411 * because we dispatch all missing packets before shutting down. */
2415 /* Look for the correct `sockent_t' */
2416 se = listen_sockets;
2421 for (i = 0; i < se->data.server.fd_num; i++)
2422 if (se->data.server.fd[i] == ent->fd)
2425 if (i < se->data.server.fd_num)
2433 ERROR ("network plugin: Got packet from FD %i, but can't "
2434 "find an appropriate socket entry.",
2441 parse_packet (se, ent->data, ent->data_len, /* flags = */ 0,
2442 /* username = */ NULL);
2448 } /* }}} void *dispatch_thread */
2450 static int network_receive (void) /* {{{ */
2452 char buffer[network_config_packet_size];
2458 receive_list_entry_t *private_list_head;
2459 receive_list_entry_t *private_list_tail;
2460 uint64_t private_list_length;
2462 assert (listen_sockets_num > 0);
2464 private_list_head = NULL;
2465 private_list_tail = NULL;
2466 private_list_length = 0;
2468 while (listen_loop == 0)
2470 status = poll (listen_sockets_pollfd, listen_sockets_num, -1);
2476 ERROR ("network plugin: poll(2) failed: %s",
2477 sstrerror (errno, errbuf, sizeof (errbuf)));
2481 for (i = 0; (i < listen_sockets_num) && (status > 0); i++)
2483 receive_list_entry_t *ent;
2485 if ((listen_sockets_pollfd[i].revents
2486 & (POLLIN | POLLPRI)) == 0)
2490 buffer_len = recv (listen_sockets_pollfd[i].fd,
2491 buffer, sizeof (buffer),
2496 status = (errno != 0) ? errno : -1;
2497 ERROR ("network plugin: recv(2) failed: %s",
2498 sstrerror (errno, errbuf, sizeof (errbuf)));
2502 stats_octets_rx += ((uint64_t) buffer_len);
2505 /* TODO: Possible performance enhancement: Do not free
2506 * these entries in the dispatch thread but put them in
2507 * another list, so we don't have to allocate more and
2508 * more of these structures. */
2509 ent = calloc (1, sizeof (*ent));
2512 ERROR ("network plugin: calloc failed.");
2517 ent->data = malloc (network_config_packet_size);
2518 if (ent->data == NULL)
2521 ERROR ("network plugin: malloc failed.");
2525 ent->fd = listen_sockets_pollfd[i].fd;
2528 memcpy (ent->data, buffer, buffer_len);
2529 ent->data_len = buffer_len;
2531 if (private_list_head == NULL)
2532 private_list_head = ent;
2534 private_list_tail->next = ent;
2535 private_list_tail = ent;
2536 private_list_length++;
2538 /* Do not block here. Blocking here has led to
2539 * insufficient performance in the past. */
2540 if (pthread_mutex_trylock (&receive_list_lock) == 0)
2542 assert (((receive_list_head == NULL) && (receive_list_length == 0))
2543 || ((receive_list_head != NULL) && (receive_list_length != 0)));
2545 if (receive_list_head == NULL)
2546 receive_list_head = private_list_head;
2548 receive_list_tail->next = private_list_head;
2549 receive_list_tail = private_list_tail;
2550 receive_list_length += private_list_length;
2552 pthread_cond_signal (&receive_list_cond);
2553 pthread_mutex_unlock (&receive_list_lock);
2555 private_list_head = NULL;
2556 private_list_tail = NULL;
2557 private_list_length = 0;
2561 } /* for (listen_sockets_pollfd) */
2565 } /* while (listen_loop == 0) */
2567 /* Make sure everything is dispatched before exiting. */
2568 if (private_list_head != NULL)
2570 pthread_mutex_lock (&receive_list_lock);
2572 if (receive_list_head == NULL)
2573 receive_list_head = private_list_head;
2575 receive_list_tail->next = private_list_head;
2576 receive_list_tail = private_list_tail;
2577 receive_list_length += private_list_length;
2579 pthread_cond_signal (&receive_list_cond);
2580 pthread_mutex_unlock (&receive_list_lock);
2584 } /* }}} int network_receive */
2586 static void *receive_thread (void __attribute__((unused)) *arg)
2588 return (network_receive () ? (void *) 1 : (void *) 0);
2589 } /* void *receive_thread */
2591 static void network_init_buffer (void)
2593 memset (send_buffer, 0, network_config_packet_size);
2594 send_buffer_ptr = send_buffer;
2595 send_buffer_fill = 0;
2596 send_buffer_last_update = 0;
2598 memset (&send_buffer_vl, 0, sizeof (send_buffer_vl));
2599 } /* int network_init_buffer */
2601 static void network_send_buffer_plain (sockent_t *se, /* {{{ */
2602 const char *buffer, size_t buffer_size)
2608 status = sockent_client_connect (se);
2612 status = sendto (se->data.client.fd, buffer, buffer_size,
2614 (struct sockaddr *) se->data.client.addr,
2615 se->data.client.addrlen);
2620 if ((errno == EINTR) || (errno == EAGAIN))
2623 ERROR ("network plugin: sendto failed: %s. Closing sending socket.",
2624 sstrerror (errno, errbuf, sizeof (errbuf)));
2625 sockent_client_disconnect (se);
2631 } /* }}} void network_send_buffer_plain */
2634 #define BUFFER_ADD(p,s) do { \
2635 memcpy (buffer + buffer_offset, (p), (s)); \
2636 buffer_offset += (s); \
2639 static void network_send_buffer_signed (sockent_t *se, /* {{{ */
2640 const char *in_buffer, size_t in_buffer_size)
2642 part_signature_sha256_t ps;
2643 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2644 size_t buffer_offset;
2645 size_t username_len;
2649 unsigned char *hash;
2652 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
2655 ERROR ("network plugin: Creating HMAC object failed: %s",
2656 gcry_strerror (err));
2660 err = gcry_md_setkey (hd, se->data.client.password,
2661 strlen (se->data.client.password));
2664 ERROR ("network plugin: gcry_md_setkey failed: %s",
2665 gcry_strerror (err));
2670 username_len = strlen (se->data.client.username);
2671 if (username_len > (BUFF_SIG_SIZE - PART_SIGNATURE_SHA256_SIZE))
2673 ERROR ("network plugin: Username too long: %s",
2674 se->data.client.username);
2678 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE,
2679 se->data.client.username, username_len);
2680 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE + username_len,
2681 in_buffer, in_buffer_size);
2683 /* Initialize the `ps' structure. */
2684 memset (&ps, 0, sizeof (ps));
2685 ps.head.type = htons (TYPE_SIGN_SHA256);
2686 ps.head.length = htons (PART_SIGNATURE_SHA256_SIZE + username_len);
2688 /* Calculate the hash value. */
2689 gcry_md_write (hd, buffer + PART_SIGNATURE_SHA256_SIZE,
2690 username_len + in_buffer_size);
2691 hash = gcry_md_read (hd, GCRY_MD_SHA256);
2694 ERROR ("network plugin: gcry_md_read failed.");
2698 memcpy (ps.hash, hash, sizeof (ps.hash));
2700 /* Add the header */
2703 BUFFER_ADD (&ps.head.type, sizeof (ps.head.type));
2704 BUFFER_ADD (&ps.head.length, sizeof (ps.head.length));
2705 BUFFER_ADD (ps.hash, sizeof (ps.hash));
2707 assert (buffer_offset == PART_SIGNATURE_SHA256_SIZE);
2712 buffer_offset = PART_SIGNATURE_SHA256_SIZE + username_len + in_buffer_size;
2713 network_send_buffer_plain (se, buffer, buffer_offset);
2714 } /* }}} void network_send_buffer_signed */
2716 static void network_send_buffer_encrypted (sockent_t *se, /* {{{ */
2717 const char *in_buffer, size_t in_buffer_size)
2719 part_encryption_aes256_t pea;
2720 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2722 size_t buffer_offset;
2724 size_t username_len;
2726 gcry_cipher_hd_t cypher;
2728 /* Initialize the header fields */
2729 memset (&pea, 0, sizeof (pea));
2730 pea.head.type = htons (TYPE_ENCR_AES256);
2732 pea.username = se->data.client.username;
2734 username_len = strlen (pea.username);
2735 if ((PART_ENCRYPTION_AES256_SIZE + username_len) > BUFF_SIG_SIZE)
2737 ERROR ("network plugin: Username too long: %s", pea.username);
2741 buffer_size = PART_ENCRYPTION_AES256_SIZE + username_len + in_buffer_size;
2742 header_size = PART_ENCRYPTION_AES256_SIZE + username_len
2743 - sizeof (pea.hash);
2745 assert (buffer_size <= sizeof (buffer));
2746 DEBUG ("network plugin: network_send_buffer_encrypted: "
2747 "buffer_size = %zu;", buffer_size);
2749 pea.head.length = htons ((uint16_t) (PART_ENCRYPTION_AES256_SIZE
2750 + username_len + in_buffer_size));
2751 pea.username_length = htons ((uint16_t) username_len);
2753 /* Chose a random initialization vector. */
2754 gcry_randomize ((void *) &pea.iv, sizeof (pea.iv), GCRY_STRONG_RANDOM);
2756 /* Create hash of the payload */
2757 gcry_md_hash_buffer (GCRY_MD_SHA1, pea.hash, in_buffer, in_buffer_size);
2759 /* Initialize the buffer */
2761 memset (buffer, 0, sizeof (buffer));
2764 BUFFER_ADD (&pea.head.type, sizeof (pea.head.type));
2765 BUFFER_ADD (&pea.head.length, sizeof (pea.head.length));
2766 BUFFER_ADD (&pea.username_length, sizeof (pea.username_length));
2767 BUFFER_ADD (pea.username, username_len);
2768 BUFFER_ADD (pea.iv, sizeof (pea.iv));
2769 assert (buffer_offset == header_size);
2770 BUFFER_ADD (pea.hash, sizeof (pea.hash));
2771 BUFFER_ADD (in_buffer, in_buffer_size);
2773 assert (buffer_offset == buffer_size);
2775 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
2776 se->data.client.password);
2780 /* Encrypt the buffer in-place */
2781 err = gcry_cipher_encrypt (cypher,
2782 buffer + header_size,
2783 buffer_size - header_size,
2784 /* in = */ NULL, /* in len = */ 0);
2787 ERROR ("network plugin: gcry_cipher_encrypt returned: %s",
2788 gcry_strerror (err));
2792 /* Send it out without further modifications */
2793 network_send_buffer_plain (se, buffer, buffer_size);
2794 } /* }}} void network_send_buffer_encrypted */
2796 #endif /* HAVE_LIBGCRYPT */
2798 static void network_send_buffer (char *buffer, size_t buffer_len) /* {{{ */
2802 DEBUG ("network plugin: network_send_buffer: buffer_len = %zu", buffer_len);
2804 for (se = sending_sockets; se != NULL; se = se->next)
2807 if (se->data.client.security_level == SECURITY_LEVEL_ENCRYPT)
2808 network_send_buffer_encrypted (se, buffer, buffer_len);
2809 else if (se->data.client.security_level == SECURITY_LEVEL_SIGN)
2810 network_send_buffer_signed (se, buffer, buffer_len);
2811 else /* if (se->data.client.security_level == SECURITY_LEVEL_NONE) */
2812 #endif /* HAVE_LIBGCRYPT */
2813 network_send_buffer_plain (se, buffer, buffer_len);
2814 } /* for (sending_sockets) */
2815 } /* }}} void network_send_buffer */
2817 static int add_to_buffer (char *buffer, int buffer_size, /* {{{ */
2818 value_list_t *vl_def,
2819 const data_set_t *ds, const value_list_t *vl)
2821 char *buffer_orig = buffer;
2823 if (strcmp (vl_def->host, vl->host) != 0)
2825 if (write_part_string (&buffer, &buffer_size, TYPE_HOST,
2826 vl->host, strlen (vl->host)) != 0)
2828 sstrncpy (vl_def->host, vl->host, sizeof (vl_def->host));
2831 if (vl_def->time != vl->time)
2833 if (write_part_number (&buffer, &buffer_size, TYPE_TIME_HR,
2834 (uint64_t) vl->time))
2836 vl_def->time = vl->time;
2839 if (vl_def->interval != vl->interval)
2841 if (write_part_number (&buffer, &buffer_size, TYPE_INTERVAL_HR,
2842 (uint64_t) vl->interval))
2844 vl_def->interval = vl->interval;
2847 if (strcmp (vl_def->plugin, vl->plugin) != 0)
2849 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN,
2850 vl->plugin, strlen (vl->plugin)) != 0)
2852 sstrncpy (vl_def->plugin, vl->plugin, sizeof (vl_def->plugin));
2855 if (strcmp (vl_def->plugin_instance, vl->plugin_instance) != 0)
2857 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
2858 vl->plugin_instance,
2859 strlen (vl->plugin_instance)) != 0)
2861 sstrncpy (vl_def->plugin_instance, vl->plugin_instance, sizeof (vl_def->plugin_instance));
2864 if (strcmp (vl_def->type, vl->type) != 0)
2866 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
2867 vl->type, strlen (vl->type)) != 0)
2869 sstrncpy (vl_def->type, ds->type, sizeof (vl_def->type));
2872 if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
2874 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE_INSTANCE,
2876 strlen (vl->type_instance)) != 0)
2878 sstrncpy (vl_def->type_instance, vl->type_instance, sizeof (vl_def->type_instance));
2881 if (write_part_values (&buffer, &buffer_size, ds, vl) != 0)
2884 return (buffer - buffer_orig);
2885 } /* }}} int add_to_buffer */
2887 static void flush_buffer (void)
2889 DEBUG ("network plugin: flush_buffer: send_buffer_fill = %i",
2892 network_send_buffer (send_buffer, (size_t) send_buffer_fill);
2894 stats_octets_tx += ((uint64_t) send_buffer_fill);
2897 network_init_buffer ();
2900 static int network_write (const data_set_t *ds, const value_list_t *vl,
2901 user_data_t __attribute__((unused)) *user_data)
2905 if (!check_send_okay (vl))
2908 char name[6*DATA_MAX_NAME_LEN];
2909 FORMAT_VL (name, sizeof (name), vl);
2910 name[sizeof (name) - 1] = 0;
2911 DEBUG ("network plugin: network_write: "
2912 "NOT sending %s.", name);
2914 /* Counter is not protected by another lock and may be reached by
2915 * multiple threads */
2916 pthread_mutex_lock (&stats_lock);
2917 stats_values_not_sent++;
2918 pthread_mutex_unlock (&stats_lock);
2922 uc_meta_data_add_unsigned_int (vl,
2923 "network:time_sent", (uint64_t) vl->time);
2925 pthread_mutex_lock (&send_buffer_lock);
2927 status = add_to_buffer (send_buffer_ptr,
2928 network_config_packet_size - (send_buffer_fill + BUFF_SIG_SIZE),
2933 /* status == bytes added to the buffer */
2934 send_buffer_fill += status;
2935 send_buffer_ptr += status;
2936 send_buffer_last_update = cdtime();
2938 stats_values_sent++;
2944 status = add_to_buffer (send_buffer_ptr,
2945 network_config_packet_size - (send_buffer_fill + BUFF_SIG_SIZE),
2951 send_buffer_fill += status;
2952 send_buffer_ptr += status;
2954 stats_values_sent++;
2960 ERROR ("network plugin: Unable to append to the "
2961 "buffer for some weird reason");
2963 else if ((network_config_packet_size - send_buffer_fill) < 15)
2968 pthread_mutex_unlock (&send_buffer_lock);
2970 return ((status < 0) ? -1 : 0);
2971 } /* int network_write */
2973 static int network_config_set_ttl (const oconfig_item_t *ci) /* {{{ */
2977 if (cf_util_get_int (ci, &tmp) != 0)
2979 else if ((tmp > 0) && (tmp <= 255))
2980 network_config_ttl = tmp;
2982 WARNING ("network plugin: The `TimeToLive' must be between 1 and 255.");
2987 } /* }}} int network_config_set_ttl */
2989 static int network_config_set_interface (const oconfig_item_t *ci, /* {{{ */
2994 if (cf_util_get_string_buffer (ci, if_name, sizeof (if_name)) != 0)
2997 *interface = if_nametoindex (if_name);
2999 } /* }}} int network_config_set_interface */
3001 static int network_config_set_buffer_size (const oconfig_item_t *ci) /* {{{ */
3005 if (cf_util_get_int (ci, &tmp) != 0)
3007 else if ((tmp >= 1024) && (tmp <= 65535))
3008 network_config_packet_size = tmp;
3010 WARNING ("network plugin: The `MaxPacketSize' must be between 1024 and 65535.");
3015 } /* }}} int network_config_set_buffer_size */
3018 static int network_config_set_security_level (oconfig_item_t *ci, /* {{{ */
3022 if ((ci->values_num != 1)
3023 || (ci->values[0].type != OCONFIG_TYPE_STRING))
3025 WARNING ("network plugin: The `SecurityLevel' config option needs exactly "
3026 "one string argument.");
3030 str = ci->values[0].value.string;
3031 if (strcasecmp ("Encrypt", str) == 0)
3032 *retval = SECURITY_LEVEL_ENCRYPT;
3033 else if (strcasecmp ("Sign", str) == 0)
3034 *retval = SECURITY_LEVEL_SIGN;
3035 else if (strcasecmp ("None", str) == 0)
3036 *retval = SECURITY_LEVEL_NONE;
3039 WARNING ("network plugin: Unknown security level: %s.", str);
3044 } /* }}} int network_config_set_security_level */
3045 #endif /* HAVE_LIBGCRYPT */
3047 static int network_config_add_listen (const oconfig_item_t *ci) /* {{{ */
3053 if ((ci->values_num < 1) || (ci->values_num > 2)
3054 || (ci->values[0].type != OCONFIG_TYPE_STRING)
3055 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
3057 ERROR ("network plugin: The `%s' config option needs "
3058 "one or two string arguments.", ci->key);
3062 se = sockent_create (SOCKENT_TYPE_SERVER);
3065 ERROR ("network plugin: sockent_create failed.");
3069 se->node = strdup (ci->values[0].value.string);
3070 if (ci->values_num >= 2)
3071 se->service = strdup (ci->values[1].value.string);
3073 for (i = 0; i < ci->children_num; i++)
3075 oconfig_item_t *child = ci->children + i;
3078 if (strcasecmp ("AuthFile", child->key) == 0)
3079 cf_util_get_string (child, &se->data.server.auth_file);
3080 else if (strcasecmp ("SecurityLevel", child->key) == 0)
3081 network_config_set_security_level (child,
3082 &se->data.server.security_level);
3084 #endif /* HAVE_LIBGCRYPT */
3085 if (strcasecmp ("Interface", child->key) == 0)
3086 network_config_set_interface (child, &se->interface);
3089 WARNING ("network plugin: Option `%s' is not allowed here.",
3095 if ((se->data.server.security_level > SECURITY_LEVEL_NONE)
3096 && (se->data.server.auth_file == NULL))
3098 ERROR ("network plugin: A security level higher than `none' was "
3099 "requested, but no AuthFile option was given. Cowardly refusing to "
3100 "open this socket!");
3101 sockent_destroy (se);
3104 #endif /* HAVE_LIBGCRYPT */
3106 status = sockent_init_crypto (se);
3109 ERROR ("network plugin: network_config_add_listen: sockent_init_crypto() failed.");
3110 sockent_destroy (se);
3114 status = sockent_server_listen (se);
3117 ERROR ("network plugin: network_config_add_listen: sockent_server_listen failed.");
3118 sockent_destroy (se);
3122 status = sockent_add (se);
3125 ERROR ("network plugin: network_config_add_listen: sockent_add failed.");
3126 sockent_destroy (se);
3131 } /* }}} int network_config_add_listen */
3133 static int network_config_add_server (const oconfig_item_t *ci) /* {{{ */
3139 if ((ci->values_num < 1) || (ci->values_num > 2)
3140 || (ci->values[0].type != OCONFIG_TYPE_STRING)
3141 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
3143 ERROR ("network plugin: The `%s' config option needs "
3144 "one or two string arguments.", ci->key);
3148 se = sockent_create (SOCKENT_TYPE_CLIENT);
3151 ERROR ("network plugin: sockent_create failed.");
3155 se->node = strdup (ci->values[0].value.string);
3156 if (ci->values_num >= 2)
3157 se->service = strdup (ci->values[1].value.string);
3159 for (i = 0; i < ci->children_num; i++)
3161 oconfig_item_t *child = ci->children + i;
3164 if (strcasecmp ("Username", child->key) == 0)
3165 cf_util_get_string (child, &se->data.client.username);
3166 else if (strcasecmp ("Password", child->key) == 0)
3167 cf_util_get_string (child, &se->data.client.password);
3168 else if (strcasecmp ("SecurityLevel", child->key) == 0)
3169 network_config_set_security_level (child,
3170 &se->data.client.security_level);
3172 #endif /* HAVE_LIBGCRYPT */
3173 if (strcasecmp ("Interface", child->key) == 0)
3174 network_config_set_interface (child, &se->interface);
3175 else if (strcasecmp ("ResolveInterval", child->key) == 0)
3176 cf_util_get_cdtime(child, &se->data.client.resolve_interval);
3179 WARNING ("network plugin: Option `%s' is not allowed here.",
3185 if ((se->data.client.security_level > SECURITY_LEVEL_NONE)
3186 && ((se->data.client.username == NULL)
3187 || (se->data.client.password == NULL)))
3189 ERROR ("network plugin: A security level higher than `none' was "
3190 "requested, but no Username or Password option was given. "
3191 "Cowardly refusing to open this socket!");
3192 sockent_destroy (se);
3195 #endif /* HAVE_LIBGCRYPT */
3197 status = sockent_init_crypto (se);
3200 ERROR ("network plugin: network_config_add_server: sockent_init_crypto() failed.");
3201 sockent_destroy (se);
3205 /* No call to sockent_client_connect() here -- it is called from
3206 * network_send_buffer_plain(). */
3208 status = sockent_add (se);
3211 ERROR ("network plugin: network_config_add_server: sockent_add failed.");
3212 sockent_destroy (se);
3217 } /* }}} int network_config_add_server */
3219 static int network_config (oconfig_item_t *ci) /* {{{ */
3223 /* The options need to be applied first */
3224 for (i = 0; i < ci->children_num; i++)
3226 oconfig_item_t *child = ci->children + i;
3227 if (strcasecmp ("TimeToLive", child->key) == 0)
3228 network_config_set_ttl (child);
3231 for (i = 0; i < ci->children_num; i++)
3233 oconfig_item_t *child = ci->children + i;
3235 if (strcasecmp ("Listen", child->key) == 0)
3236 network_config_add_listen (child);
3237 else if (strcasecmp ("Server", child->key) == 0)
3238 network_config_add_server (child);
3239 else if (strcasecmp ("TimeToLive", child->key) == 0) {
3240 /* Handled earlier */
3242 else if (strcasecmp ("MaxPacketSize", child->key) == 0)
3243 network_config_set_buffer_size (child);
3244 else if (strcasecmp ("Forward", child->key) == 0)
3245 cf_util_get_boolean (child, &network_config_forward);
3246 else if (strcasecmp ("ReportStats", child->key) == 0)
3247 cf_util_get_boolean (child, &network_config_stats);
3250 WARNING ("network plugin: Option `%s' is not allowed here.",
3256 } /* }}} int network_config */
3258 static int network_notification (const notification_t *n,
3259 user_data_t __attribute__((unused)) *user_data)
3261 char buffer[network_config_packet_size];
3262 char *buffer_ptr = buffer;
3263 int buffer_free = sizeof (buffer);
3266 if (!check_send_notify_okay (n))
3269 memset (buffer, 0, sizeof (buffer));
3271 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_TIME_HR,
3272 (uint64_t) n->time);
3276 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_SEVERITY,
3277 (uint64_t) n->severity);
3281 if (strlen (n->host) > 0)
3283 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_HOST,
3284 n->host, strlen (n->host));
3289 if (strlen (n->plugin) > 0)
3291 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_PLUGIN,
3292 n->plugin, strlen (n->plugin));
3297 if (strlen (n->plugin_instance) > 0)
3299 status = write_part_string (&buffer_ptr, &buffer_free,
3300 TYPE_PLUGIN_INSTANCE,
3301 n->plugin_instance, strlen (n->plugin_instance));
3306 if (strlen (n->type) > 0)
3308 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE,
3309 n->type, strlen (n->type));
3314 if (strlen (n->type_instance) > 0)
3316 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE_INSTANCE,
3317 n->type_instance, strlen (n->type_instance));
3322 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_MESSAGE,
3323 n->message, strlen (n->message));
3327 network_send_buffer (buffer, sizeof (buffer) - buffer_free);
3330 } /* int network_notification */
3332 static int network_shutdown (void)
3338 /* Kill the listening thread */
3339 if (receive_thread_running != 0)
3341 INFO ("network plugin: Stopping receive thread.");
3342 pthread_kill (receive_thread_id, SIGTERM);
3343 pthread_join (receive_thread_id, NULL /* no return value */);
3344 memset (&receive_thread_id, 0, sizeof (receive_thread_id));
3345 receive_thread_running = 0;
3348 /* Shutdown the dispatching thread */
3349 if (dispatch_thread_running != 0)
3351 INFO ("network plugin: Stopping dispatch thread.");
3352 pthread_mutex_lock (&receive_list_lock);
3353 pthread_cond_broadcast (&receive_list_cond);
3354 pthread_mutex_unlock (&receive_list_lock);
3355 pthread_join (dispatch_thread_id, /* ret = */ NULL);
3356 dispatch_thread_running = 0;
3359 sockent_destroy (listen_sockets);
3361 if (send_buffer_fill > 0)
3364 sfree (send_buffer);
3366 for (se = sending_sockets; se != NULL; se = se->next)
3367 sockent_client_disconnect (se);
3368 sockent_destroy (sending_sockets);
3370 plugin_unregister_config ("network");
3371 plugin_unregister_init ("network");
3372 plugin_unregister_write ("network");
3373 plugin_unregister_shutdown ("network");
3376 } /* int network_shutdown */
3378 static int network_stats_read (void) /* {{{ */
3380 derive_t copy_octets_rx;
3381 derive_t copy_octets_tx;
3382 derive_t copy_packets_rx;
3383 derive_t copy_packets_tx;
3384 derive_t copy_values_dispatched;
3385 derive_t copy_values_not_dispatched;
3386 derive_t copy_values_sent;
3387 derive_t copy_values_not_sent;
3388 derive_t copy_receive_list_length;
3389 value_list_t vl = VALUE_LIST_INIT;
3392 copy_octets_rx = stats_octets_rx;
3393 copy_octets_tx = stats_octets_tx;
3394 copy_packets_rx = stats_packets_rx;
3395 copy_packets_tx = stats_packets_tx;
3396 copy_values_dispatched = stats_values_dispatched;
3397 copy_values_not_dispatched = stats_values_not_dispatched;
3398 copy_values_sent = stats_values_sent;
3399 copy_values_not_sent = stats_values_not_sent;
3400 copy_receive_list_length = receive_list_length;
3402 /* Initialize `vl' */
3406 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
3407 sstrncpy (vl.plugin, "network", sizeof (vl.plugin));
3409 /* Octets received / sent */
3410 vl.values[0].derive = (derive_t) copy_octets_rx;
3411 vl.values[1].derive = (derive_t) copy_octets_tx;
3412 sstrncpy (vl.type, "if_octets", sizeof (vl.type));
3413 plugin_dispatch_values (&vl);
3415 /* Packets received / send */
3416 vl.values[0].derive = (derive_t) copy_packets_rx;
3417 vl.values[1].derive = (derive_t) copy_packets_tx;
3418 sstrncpy (vl.type, "if_packets", sizeof (vl.type));
3419 plugin_dispatch_values (&vl);
3421 /* Values (not) dispatched and (not) send */
3422 sstrncpy (vl.type, "total_values", sizeof (vl.type));
3425 vl.values[0].derive = (derive_t) copy_values_dispatched;
3426 sstrncpy (vl.type_instance, "dispatch-accepted",
3427 sizeof (vl.type_instance));
3428 plugin_dispatch_values (&vl);
3430 vl.values[0].derive = (derive_t) copy_values_not_dispatched;
3431 sstrncpy (vl.type_instance, "dispatch-rejected",
3432 sizeof (vl.type_instance));
3433 plugin_dispatch_values (&vl);
3435 vl.values[0].derive = (derive_t) copy_values_sent;
3436 sstrncpy (vl.type_instance, "send-accepted",
3437 sizeof (vl.type_instance));
3438 plugin_dispatch_values (&vl);
3440 vl.values[0].derive = (derive_t) copy_values_not_sent;
3441 sstrncpy (vl.type_instance, "send-rejected",
3442 sizeof (vl.type_instance));
3443 plugin_dispatch_values (&vl);
3445 /* Receive queue length */
3446 vl.values[0].gauge = (gauge_t) copy_receive_list_length;
3447 sstrncpy (vl.type, "queue_length", sizeof (vl.type));
3448 vl.type_instance[0] = 0;
3449 plugin_dispatch_values (&vl);
3452 } /* }}} int network_stats_read */
3454 static int network_init (void)
3456 static _Bool have_init = 0;
3458 /* Check if we were already initialized. If so, just return - there's
3459 * nothing more to do (for now, that is). */
3465 network_init_gcrypt ();
3468 if (network_config_stats)
3469 plugin_register_read ("network", network_stats_read);
3471 plugin_register_shutdown ("network", network_shutdown);
3473 send_buffer = malloc (network_config_packet_size);
3474 if (send_buffer == NULL)
3476 ERROR ("network plugin: malloc failed.");
3479 network_init_buffer ();
3481 /* setup socket(s) and so on */
3482 if (sending_sockets != NULL)
3484 plugin_register_write ("network", network_write,
3485 /* user_data = */ NULL);
3486 plugin_register_notification ("network", network_notification,
3487 /* user_data = */ NULL);
3490 /* If no threads need to be started, return here. */
3491 if ((listen_sockets_num == 0)
3492 || ((dispatch_thread_running != 0)
3493 && (receive_thread_running != 0)))
3496 if (dispatch_thread_running == 0)
3499 status = plugin_thread_create (&dispatch_thread_id,
3500 NULL /* no attributes */,
3502 NULL /* no argument */);
3506 ERROR ("network: pthread_create failed: %s",
3507 sstrerror (errno, errbuf,
3512 dispatch_thread_running = 1;
3516 if (receive_thread_running == 0)
3519 status = plugin_thread_create (&receive_thread_id,
3520 NULL /* no attributes */,
3522 NULL /* no argument */);
3526 ERROR ("network: pthread_create failed: %s",
3527 sstrerror (errno, errbuf,
3532 receive_thread_running = 1;
3537 } /* int network_init */
3540 * The flush option of the network plugin cannot flush individual identifiers.
3541 * All the values are added to a buffer and sent when the buffer is full, the
3542 * requested value may or may not be in there, it's not worth finding out. We
3543 * just send the buffer if `flush' is called - if the requested value was in
3544 * there, good. If not, well, then there is nothing to flush.. -octo
3546 static int network_flush (cdtime_t timeout,
3547 __attribute__((unused)) const char *identifier,
3548 __attribute__((unused)) user_data_t *user_data)
3550 pthread_mutex_lock (&send_buffer_lock);
3552 if (send_buffer_fill > 0)
3556 cdtime_t now = cdtime ();
3557 if ((send_buffer_last_update + timeout) > now)
3559 pthread_mutex_unlock (&send_buffer_lock);
3565 pthread_mutex_unlock (&send_buffer_lock);
3568 } /* int network_flush */
3570 void module_register (void)
3572 plugin_register_complex_config ("network", network_config);
3573 plugin_register_init ("network", network_init);
3574 plugin_register_flush ("network", network_flush,
3575 /* user_data = */ NULL);
3576 } /* void module_register */
3578 /* vim: set fdm=marker : */