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 */
32 #include "configfile.h"
33 #include "utils_fbhash.h"
34 #include "utils_cache.h"
35 #include "utils_complain.h"
43 # include <netinet/in.h>
46 # include <arpa/inet.h>
56 # if defined __APPLE__
57 /* default xcode compiler throws warnings even when deprecated functionality
58 * is not used. -Werror breaks the build because of erroneous warnings.
59 * http://stackoverflow.com/questions/10556299/compiler-warnings-with-libgcrypt-v1-5-0/12830209#12830209
61 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
63 /* FreeBSD's copy of libgcrypt extends the existing GCRYPT_NO_DEPRECATED
64 * to properly hide all deprecated functionality.
65 * http://svnweb.freebsd.org/ports/head/security/libgcrypt/files/patch-src__gcrypt.h.in
67 # define GCRYPT_NO_DEPRECATED
69 # if defined __APPLE__
70 /* Re enable deprecation warnings */
71 # pragma GCC diagnostic warning "-Wdeprecated-declarations"
73 # if GCRYPT_VERSION_NUMBER < 0x010600
74 GCRY_THREAD_OPTION_PTHREAD_IMPL;
78 #ifndef IPV6_ADD_MEMBERSHIP
79 # ifdef IPV6_JOIN_GROUP
80 # define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
82 # error "Neither IP_ADD_MEMBERSHIP nor IPV6_JOIN_GROUP is defined"
84 #endif /* !IP_ADD_MEMBERSHIP */
87 * Maximum size required for encryption / signing:
89 * 42 bytes for the encryption header
90 * + 64 bytes for the username
94 #define BUFF_SIG_SIZE 106
99 #define SECURITY_LEVEL_NONE 0
101 # define SECURITY_LEVEL_SIGN 1
102 # define SECURITY_LEVEL_ENCRYPT 2
104 struct sockent_client
107 struct sockaddr_storage *addr;
113 gcry_cipher_hd_t cypher;
114 unsigned char password_hash[32];
116 cdtime_t next_resolve_reconnect;
117 cdtime_t resolve_interval;
120 struct sockent_server
128 gcry_cipher_hd_t cypher;
132 typedef struct sockent
134 #define SOCKENT_TYPE_CLIENT 1
135 #define SOCKENT_TYPE_SERVER 2
144 struct sockent_client client;
145 struct sockent_server server;
148 struct sockent *next;
151 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
152 * 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
153 * +-------+-----------------------+-------------------------------+
154 * ! Ver. ! ! Length !
155 * +-------+-----------------------+-------------------------------+
162 typedef struct part_header_s part_header_t;
164 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
165 * 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
166 * +-------------------------------+-------------------------------+
168 * +-------------------------------+-------------------------------+
169 * : (Length - 4) Bytes :
170 * +---------------------------------------------------------------+
177 typedef struct part_string_s part_string_t;
179 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
180 * 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
181 * +-------------------------------+-------------------------------+
183 * +-------------------------------+-------------------------------+
184 * : (Length - 4 == 2 || 4 || 8) Bytes :
185 * +---------------------------------------------------------------+
192 typedef struct part_number_s part_number_t;
194 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
195 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
196 * +-------------------------------+-------------------------------+
198 * +-------------------------------+---------------+---------------+
199 * ! Num of values ! Type0 ! Type1 !
200 * +-------------------------------+---------------+---------------+
203 * +---------------------------------------------------------------+
206 * +---------------------------------------------------------------+
211 uint16_t *num_values;
212 uint8_t *values_types;
215 typedef struct part_values_s part_values_t;
217 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
218 * 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
219 * +-------------------------------+-------------------------------+
221 * +-------------------------------+-------------------------------+
222 * ! Hash (Bits 0 - 31) !
224 * ! Hash (Bits 224 - 255) !
225 * +---------------------------------------------------------------+
228 #define PART_SIGNATURE_SHA256_SIZE 36
229 struct part_signature_sha256_s
232 unsigned char hash[32];
235 typedef struct part_signature_sha256_s part_signature_sha256_t;
237 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
238 * 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
239 * +-------------------------------+-------------------------------+
241 * +-------------------------------+-------------------------------+
242 * ! Original length ! Padding (0 - 15 bytes) !
243 * +-------------------------------+-------------------------------+
244 * ! Hash (Bits 0 - 31) !
246 * ! Hash (Bits 128 - 159) !
247 * +---------------------------------------------------------------+
250 #define PART_ENCRYPTION_AES256_SIZE 42
251 struct part_encryption_aes256_s
254 uint16_t username_length;
256 unsigned char iv[16];
258 unsigned char hash[20];
262 typedef struct part_encryption_aes256_s part_encryption_aes256_t;
264 struct receive_list_entry_s
269 struct receive_list_entry_s *next;
271 typedef struct receive_list_entry_s receive_list_entry_t;
276 static int network_config_ttl = 0;
277 /* Ethernet - (IPv6 + UDP) = 1500 - (40 + 8) = 1452 */
278 static size_t network_config_packet_size = 1452;
279 static _Bool network_config_forward = 0;
280 static _Bool network_config_stats = 0;
282 static sockent_t *sending_sockets = NULL;
284 static receive_list_entry_t *receive_list_head = NULL;
285 static receive_list_entry_t *receive_list_tail = NULL;
286 static pthread_mutex_t receive_list_lock = PTHREAD_MUTEX_INITIALIZER;
287 static pthread_cond_t receive_list_cond = PTHREAD_COND_INITIALIZER;
288 static uint64_t receive_list_length = 0;
290 static sockent_t *listen_sockets = NULL;
291 static struct pollfd *listen_sockets_pollfd = NULL;
292 static size_t listen_sockets_num = 0;
294 /* The receive and dispatch threads will run as long as `listen_loop' is set to
296 static int listen_loop = 0;
297 static int receive_thread_running = 0;
298 static pthread_t receive_thread_id;
299 static int dispatch_thread_running = 0;
300 static pthread_t dispatch_thread_id;
302 /* Buffer in which to-be-sent network packets are constructed. */
303 static char *send_buffer;
304 static char *send_buffer_ptr;
305 static int send_buffer_fill;
306 static cdtime_t send_buffer_last_update;
307 static value_list_t send_buffer_vl = VALUE_LIST_STATIC;
308 static pthread_mutex_t send_buffer_lock = PTHREAD_MUTEX_INITIALIZER;
310 /* XXX: These counters are incremented from one place only. The spot in which
311 * the values are incremented is either only reachable by one thread (the
312 * dispatch thread, for example) or locked by some lock (send_buffer_lock for
313 * example). Only if neither is true, the stats_lock is acquired. The counters
314 * are always read without holding a lock in the hope that writing 8 bytes to
315 * memory is an atomic operation. */
316 static derive_t stats_octets_rx = 0;
317 static derive_t stats_octets_tx = 0;
318 static derive_t stats_packets_rx = 0;
319 static derive_t stats_packets_tx = 0;
320 static derive_t stats_values_dispatched = 0;
321 static derive_t stats_values_not_dispatched = 0;
322 static derive_t stats_values_sent = 0;
323 static derive_t stats_values_not_sent = 0;
324 static pthread_mutex_t stats_lock = PTHREAD_MUTEX_INITIALIZER;
329 static _Bool check_receive_okay (const value_list_t *vl) /* {{{ */
331 uint64_t time_sent = 0;
334 status = uc_meta_data_get_unsigned_int (vl,
335 "network:time_sent", &time_sent);
337 /* This is a value we already sent. Don't allow it to be received again in
338 * order to avoid looping. */
339 if ((status == 0) && (time_sent >= ((uint64_t) vl->time)))
343 } /* }}} _Bool check_receive_okay */
345 static _Bool check_send_okay (const value_list_t *vl) /* {{{ */
350 if (network_config_forward)
353 if (vl->meta == NULL)
356 status = meta_data_get_boolean (vl->meta, "network:received", &received);
357 if (status == -ENOENT)
359 else if (status != 0)
361 ERROR ("network plugin: check_send_okay: meta_data_get_boolean failed "
362 "with status %i.", status);
366 /* By default, only *send* value lists that were not *received* by the
369 } /* }}} _Bool check_send_okay */
371 static _Bool check_notify_received (const notification_t *n) /* {{{ */
373 for (notification_meta_t *ptr = n->meta; ptr != NULL; ptr = ptr->next)
374 if ((strcmp ("network:received", ptr->name) == 0)
375 && (ptr->type == NM_TYPE_BOOLEAN))
376 return ((_Bool) ptr->nm_value.nm_boolean);
379 } /* }}} _Bool check_notify_received */
381 static _Bool check_send_notify_okay (const notification_t *n) /* {{{ */
383 static c_complain_t complain_forwarding = C_COMPLAIN_INIT_STATIC;
389 received = check_notify_received (n);
391 if (network_config_forward && received)
393 c_complain_once (LOG_ERR, &complain_forwarding,
394 "network plugin: A notification has been received via the network "
395 "and forwarding is enabled. Forwarding of notifications is currently "
396 "not supported, because there is not loop-deteciton available. "
397 "Please contact the collectd mailing list if you need this "
401 /* By default, only *send* value lists that were not *received* by the
404 } /* }}} _Bool check_send_notify_okay */
406 static int network_dispatch_values (value_list_t *vl, /* {{{ */
407 const char *username)
412 || (strlen (vl->host) == 0)
413 || (strlen (vl->plugin) == 0)
414 || (strlen (vl->type) == 0))
417 if (!check_receive_okay (vl))
420 char name[6*DATA_MAX_NAME_LEN];
421 FORMAT_VL (name, sizeof (name), vl);
422 name[sizeof (name) - 1] = 0;
423 DEBUG ("network plugin: network_dispatch_values: "
424 "NOT dispatching %s.", name);
426 stats_values_not_dispatched++;
430 assert (vl->meta == NULL);
432 vl->meta = meta_data_create ();
433 if (vl->meta == NULL)
435 ERROR ("network plugin: meta_data_create failed.");
439 status = meta_data_add_boolean (vl->meta, "network:received", 1);
442 ERROR ("network plugin: meta_data_add_boolean failed.");
443 meta_data_destroy (vl->meta);
448 if (username != NULL)
450 status = meta_data_add_string (vl->meta, "network:username", username);
453 ERROR ("network plugin: meta_data_add_string failed.");
454 meta_data_destroy (vl->meta);
460 plugin_dispatch_values (vl);
461 stats_values_dispatched++;
463 meta_data_destroy (vl->meta);
467 } /* }}} int network_dispatch_values */
469 static int network_dispatch_notification (notification_t *n) /* {{{ */
473 assert (n->meta == NULL);
475 status = plugin_notification_meta_add_boolean (n, "network:received", 1);
478 ERROR ("network plugin: plugin_notification_meta_add_boolean failed.");
479 plugin_notification_meta_free (n->meta);
484 status = plugin_dispatch_notification (n);
486 plugin_notification_meta_free (n->meta);
490 } /* }}} int network_dispatch_notification */
493 static int network_init_gcrypt (void) /* {{{ */
497 /* http://lists.gnupg.org/pipermail/gcrypt-devel/2003-August/000458.html
498 * Because you can't know in a library whether another library has
499 * already initialized the library */
500 if (gcry_control (GCRYCTL_ANY_INITIALIZATION_P))
503 /* http://www.gnupg.org/documentation/manuals/gcrypt/Multi_002dThreading.html
504 * To ensure thread-safety, it's important to set GCRYCTL_SET_THREAD_CBS
505 * *before* initalizing Libgcrypt with gcry_check_version(), which itself must
506 * be called before any other gcry_* function. GCRYCTL_ANY_INITIALIZATION_P
507 * above doesn't count, as it doesn't implicitly initalize Libgcrypt.
509 * tl;dr: keep all these gry_* statements in this exact order please. */
510 # if GCRYPT_VERSION_NUMBER < 0x010600
511 err = gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
514 ERROR ("network plugin: gcry_control (GCRYCTL_SET_THREAD_CBS) failed: %s", gcry_strerror (err));
519 gcry_check_version (NULL);
521 err = gcry_control (GCRYCTL_INIT_SECMEM, 32768);
524 ERROR ("network plugin: gcry_control (GCRYCTL_INIT_SECMEM) failed: %s", gcry_strerror (err));
528 gcry_control (GCRYCTL_INITIALIZATION_FINISHED);
530 } /* }}} int 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, size_t *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;
622 num_values = vl->values_len;
623 packet_len = sizeof (part_header_t) + sizeof (uint16_t)
624 + (num_values * sizeof (uint8_t))
625 + (num_values * sizeof (value_t));
627 if (*ret_buffer_len < packet_len)
630 pkg_values_types = malloc (num_values * sizeof (*pkg_values_types));
631 if (pkg_values_types == NULL)
633 ERROR ("network plugin: write_part_values: malloc failed.");
637 pkg_values = malloc (num_values * sizeof (*pkg_values));
638 if (pkg_values == NULL)
640 free (pkg_values_types);
641 ERROR ("network plugin: write_part_values: malloc failed.");
645 pkg_ph.type = htons (TYPE_VALUES);
646 pkg_ph.length = htons (packet_len);
648 pkg_num_values = htons ((uint16_t) vl->values_len);
650 for (int i = 0; i < num_values; i++)
652 pkg_values_types[i] = (uint8_t) ds->ds[i].type;
653 switch (ds->ds[i].type)
655 case DS_TYPE_COUNTER:
656 pkg_values[i].counter = htonll (vl->values[i].counter);
660 pkg_values[i].gauge = htond (vl->values[i].gauge);
664 pkg_values[i].derive = htonll (vl->values[i].derive);
667 case DS_TYPE_ABSOLUTE:
668 pkg_values[i].absolute = htonll (vl->values[i].absolute);
672 free (pkg_values_types);
674 ERROR ("network plugin: write_part_values: "
675 "Unknown data source type: %i",
678 } /* switch (ds->ds[i].type) */
679 } /* for (num_values) */
682 * Use `memcpy' to write everything to the buffer, because the pointer
683 * may be unaligned and some architectures, such as SPARC, can't handle
686 packet_ptr = *ret_buffer;
688 memcpy (packet_ptr + offset, &pkg_ph, sizeof (pkg_ph));
689 offset += sizeof (pkg_ph);
690 memcpy (packet_ptr + offset, &pkg_num_values, sizeof (pkg_num_values));
691 offset += sizeof (pkg_num_values);
692 memcpy (packet_ptr + offset, pkg_values_types, num_values * sizeof (uint8_t));
693 offset += num_values * sizeof (uint8_t);
694 memcpy (packet_ptr + offset, pkg_values, num_values * sizeof (value_t));
695 offset += num_values * sizeof (value_t);
697 assert (offset == packet_len);
699 *ret_buffer = packet_ptr + packet_len;
700 *ret_buffer_len -= packet_len;
702 free (pkg_values_types);
706 } /* int write_part_values */
708 static int write_part_number (char **ret_buffer, size_t *ret_buffer_len,
709 int type, uint64_t value)
714 part_header_t pkg_head;
719 packet_len = sizeof (pkg_head) + sizeof (pkg_value);
721 if (*ret_buffer_len < packet_len)
724 pkg_head.type = htons (type);
725 pkg_head.length = htons (packet_len);
726 pkg_value = htonll (value);
728 packet_ptr = *ret_buffer;
730 memcpy (packet_ptr + offset, &pkg_head, sizeof (pkg_head));
731 offset += sizeof (pkg_head);
732 memcpy (packet_ptr + offset, &pkg_value, sizeof (pkg_value));
733 offset += sizeof (pkg_value);
735 assert (offset == packet_len);
737 *ret_buffer = packet_ptr + packet_len;
738 *ret_buffer_len -= packet_len;
741 } /* int write_part_number */
743 static int write_part_string (char **ret_buffer, size_t *ret_buffer_len,
744 int type, const char *str, size_t str_len)
754 buffer_len = 2 * sizeof (uint16_t) + str_len + 1;
755 if (*ret_buffer_len < buffer_len)
758 pkg_type = htons (type);
759 pkg_length = htons (buffer_len);
761 buffer = *ret_buffer;
763 memcpy (buffer + offset, (void *) &pkg_type, sizeof (pkg_type));
764 offset += sizeof (pkg_type);
765 memcpy (buffer + offset, (void *) &pkg_length, sizeof (pkg_length));
766 offset += sizeof (pkg_length);
767 memcpy (buffer + offset, str, str_len);
769 memset (buffer + offset, '\0', 1);
772 assert (offset == buffer_len);
774 *ret_buffer = buffer + buffer_len;
775 *ret_buffer_len -= buffer_len;
778 } /* int write_part_string */
780 static int parse_part_values (void **ret_buffer, size_t *ret_buffer_len,
781 value_t **ret_values, size_t *ret_num_values)
783 char *buffer = *ret_buffer;
784 size_t buffer_len = *ret_buffer_len;
798 NOTICE ("network plugin: packet is too short: "
799 "buffer_len = %zu", buffer_len);
803 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
804 buffer += sizeof (tmp16);
805 pkg_type = ntohs (tmp16);
807 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
808 buffer += sizeof (tmp16);
809 pkg_length = ntohs (tmp16);
811 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
812 buffer += sizeof (tmp16);
813 pkg_numval = (size_t) ntohs (tmp16);
815 assert (pkg_type == TYPE_VALUES);
817 exp_size = 3 * sizeof (uint16_t)
818 + pkg_numval * (sizeof (uint8_t) + sizeof (value_t));
819 if (buffer_len < exp_size)
821 WARNING ("network plugin: parse_part_values: "
823 "Chunk of size %zu expected, "
824 "but buffer has only %zu bytes left.",
825 exp_size, buffer_len);
828 assert (pkg_numval <= ((buffer_len - 6) / 9));
830 if (pkg_length != exp_size)
832 WARNING ("network plugin: parse_part_values: "
833 "Length and number of values "
834 "in the packet don't match.");
838 pkg_types = calloc (pkg_numval, sizeof (*pkg_types));
839 pkg_values = calloc (pkg_numval, sizeof (*pkg_values));
840 if ((pkg_types == NULL) || (pkg_values == NULL))
844 ERROR ("network plugin: parse_part_values: calloc failed.");
848 memcpy (pkg_types, buffer, pkg_numval * sizeof (*pkg_types));
849 buffer += pkg_numval * sizeof (*pkg_types);
850 memcpy (pkg_values, buffer, pkg_numval * sizeof (*pkg_values));
851 buffer += pkg_numval * sizeof (*pkg_values);
853 for (size_t i = 0; i < pkg_numval; i++)
855 switch (pkg_types[i])
857 case DS_TYPE_COUNTER:
858 pkg_values[i].counter = (counter_t) ntohll (pkg_values[i].counter);
862 pkg_values[i].gauge = (gauge_t) ntohd (pkg_values[i].gauge);
866 pkg_values[i].derive = (derive_t) ntohll (pkg_values[i].derive);
869 case DS_TYPE_ABSOLUTE:
870 pkg_values[i].absolute = (absolute_t) ntohll (pkg_values[i].absolute);
874 NOTICE ("network plugin: parse_part_values: "
875 "Don't know how to handle data source type %"PRIu8,
880 } /* switch (pkg_types[i]) */
883 *ret_buffer = buffer;
884 *ret_buffer_len = buffer_len - pkg_length;
885 *ret_num_values = pkg_numval;
886 *ret_values = pkg_values;
891 } /* int parse_part_values */
893 static int parse_part_number (void **ret_buffer, size_t *ret_buffer_len,
896 char *buffer = *ret_buffer;
897 size_t buffer_len = *ret_buffer_len;
901 size_t exp_size = 2 * sizeof (uint16_t) + sizeof (uint64_t);
905 if (buffer_len < exp_size)
907 WARNING ("network plugin: parse_part_number: "
909 "Chunk of size %zu expected, "
910 "but buffer has only %zu bytes left.",
911 exp_size, buffer_len);
915 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
916 buffer += sizeof (tmp16);
917 /* pkg_type = ntohs (tmp16); */
919 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
920 buffer += sizeof (tmp16);
921 pkg_length = ntohs (tmp16);
923 memcpy ((void *) &tmp64, buffer, sizeof (tmp64));
924 buffer += sizeof (tmp64);
925 *value = ntohll (tmp64);
927 *ret_buffer = buffer;
928 *ret_buffer_len = buffer_len - pkg_length;
931 } /* int parse_part_number */
933 static int parse_part_string (void **ret_buffer, size_t *ret_buffer_len,
934 char *output, size_t const output_len)
936 char *buffer = *ret_buffer;
937 size_t buffer_len = *ret_buffer_len;
940 size_t const header_size = 2 * sizeof (uint16_t);
948 if (buffer_len < header_size)
950 WARNING ("network plugin: parse_part_string: "
952 "Chunk of at least size %zu expected, "
953 "but buffer has only %zu bytes left.",
954 header_size, buffer_len);
958 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
959 buffer += sizeof (tmp16);
960 /* pkg_type = ntohs (tmp16); */
962 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
963 buffer += sizeof (tmp16);
964 pkg_length = ntohs (tmp16);
965 payload_size = ((size_t) pkg_length) - header_size;
967 /* Check that packet fits in the input buffer */
968 if (pkg_length > buffer_len)
970 WARNING ("network plugin: parse_part_string: "
972 "Chunk of size %"PRIu16" received, "
973 "but buffer has only %zu bytes left.",
974 pkg_length, buffer_len);
978 /* Check that pkg_length is in the valid range */
979 if (pkg_length <= header_size)
981 WARNING ("network plugin: parse_part_string: "
983 "Header claims this packet is only %hu "
984 "bytes long.", pkg_length);
988 /* Check that the package data fits into the output buffer.
989 * The previous if-statement ensures that:
990 * `pkg_length > header_size' */
991 if (output_len < payload_size)
993 WARNING ("network plugin: parse_part_string: "
995 "Output buffer holds %zu bytes, "
996 "which is too small to hold the received "
998 output_len, payload_size);
1002 /* All sanity checks successfull, let's copy the data over */
1003 memcpy ((void *) output, (void *) buffer, payload_size);
1004 buffer += payload_size;
1006 /* For some very weird reason '\0' doesn't do the trick on SPARC in
1007 * this statement. */
1008 if (output[payload_size - 1] != 0)
1010 WARNING ("network plugin: parse_part_string: "
1011 "Received string does not end "
1012 "with a NULL-byte.");
1016 *ret_buffer = buffer;
1017 *ret_buffer_len = buffer_len - pkg_length;
1020 } /* int parse_part_string */
1022 /* Forward declaration: parse_part_sign_sha256 and parse_part_encr_aes256 call
1023 * parse_packet and vice versa. */
1024 #define PP_SIGNED 0x01
1025 #define PP_ENCRYPTED 0x02
1026 static int parse_packet (sockent_t *se,
1027 void *buffer, size_t buffer_size, int flags,
1028 const char *username);
1030 #define BUFFER_READ(p,s) do { \
1031 memcpy ((p), buffer + buffer_offset, (s)); \
1032 buffer_offset += (s); \
1036 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
1037 void **ret_buffer, size_t *ret_buffer_len, int flags)
1039 static c_complain_t complain_no_users = C_COMPLAIN_INIT_STATIC;
1043 size_t buffer_offset;
1045 size_t username_len;
1048 part_signature_sha256_t pss;
1049 uint16_t pss_head_length;
1050 char hash[sizeof (pss.hash)];
1054 unsigned char *hash_ptr;
1056 buffer = *ret_buffer;
1057 buffer_len = *ret_buffer_len;
1060 if (se->data.server.userdb == NULL)
1062 c_complain (LOG_NOTICE, &complain_no_users,
1063 "network plugin: Received signed network packet but can't verify it "
1064 "because no user DB has been configured. Will accept it.");
1068 /* Check if the buffer has enough data for this structure. */
1069 if (buffer_len <= PART_SIGNATURE_SHA256_SIZE)
1072 /* Read type and length header */
1073 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
1074 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
1075 pss_head_length = ntohs (pss.head.length);
1077 /* Check if the `pss_head_length' is within bounds. */
1078 if ((pss_head_length <= PART_SIGNATURE_SHA256_SIZE)
1079 || (pss_head_length > buffer_len))
1081 ERROR ("network plugin: HMAC-SHA-256 with invalid length received.");
1085 /* Copy the hash. */
1086 BUFFER_READ (pss.hash, sizeof (pss.hash));
1088 /* Calculate username length (without null byte) and allocate memory */
1089 username_len = pss_head_length - PART_SIGNATURE_SHA256_SIZE;
1090 pss.username = malloc (username_len + 1);
1091 if (pss.username == NULL)
1094 /* Read the username */
1095 BUFFER_READ (pss.username, username_len);
1096 pss.username[username_len] = 0;
1098 assert (buffer_offset == pss_head_length);
1100 /* Query the password */
1101 secret = fbh_get (se->data.server.userdb, pss.username);
1104 ERROR ("network plugin: Unknown user: %s", pss.username);
1105 sfree (pss.username);
1109 /* Create a hash device and check the HMAC */
1111 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
1114 ERROR ("network plugin: Creating HMAC-SHA-256 object failed: %s",
1115 gcry_strerror (err));
1117 sfree (pss.username);
1121 err = gcry_md_setkey (hd, secret, strlen (secret));
1124 ERROR ("network plugin: gcry_md_setkey failed: %s", gcry_strerror (err));
1127 sfree (pss.username);
1132 buffer + PART_SIGNATURE_SHA256_SIZE,
1133 buffer_len - PART_SIGNATURE_SHA256_SIZE);
1134 hash_ptr = gcry_md_read (hd, GCRY_MD_SHA256);
1135 if (hash_ptr == NULL)
1137 ERROR ("network plugin: gcry_md_read failed.");
1140 sfree (pss.username);
1143 memcpy (hash, hash_ptr, sizeof (hash));
1149 if (memcmp (pss.hash, hash, sizeof (pss.hash)) != 0)
1151 WARNING ("network plugin: Verifying HMAC-SHA-256 signature failed: "
1156 parse_packet (se, buffer + buffer_offset, buffer_len - buffer_offset,
1157 flags | PP_SIGNED, pss.username);
1161 sfree (pss.username);
1163 *ret_buffer = buffer + buffer_len;
1164 *ret_buffer_len = 0;
1167 } /* }}} int parse_part_sign_sha256 */
1168 /* #endif HAVE_LIBGCRYPT */
1170 #else /* if !HAVE_LIBGCRYPT */
1171 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
1172 void **ret_buffer, size_t *ret_buffer_size, int flags)
1174 static int warning_has_been_printed = 0;
1178 size_t buffer_offset;
1181 part_signature_sha256_t pss;
1183 buffer = *ret_buffer;
1184 buffer_size = *ret_buffer_size;
1187 if (buffer_size <= PART_SIGNATURE_SHA256_SIZE)
1190 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
1191 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
1192 part_len = ntohs (pss.head.length);
1194 if ((part_len <= PART_SIGNATURE_SHA256_SIZE)
1195 || (part_len > buffer_size))
1198 if (warning_has_been_printed == 0)
1200 WARNING ("network plugin: Received signed packet, but the network "
1201 "plugin was not linked with libgcrypt, so I cannot "
1202 "verify the signature. The packet will be accepted.");
1203 warning_has_been_printed = 1;
1206 parse_packet (se, buffer + part_len, buffer_size - part_len, flags,
1207 /* username = */ NULL);
1209 *ret_buffer = buffer + buffer_size;
1210 *ret_buffer_size = 0;
1213 } /* }}} int parse_part_sign_sha256 */
1214 #endif /* !HAVE_LIBGCRYPT */
1217 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1218 void **ret_buffer, size_t *ret_buffer_len,
1221 char *buffer = *ret_buffer;
1222 size_t buffer_len = *ret_buffer_len;
1225 size_t buffer_offset;
1226 uint16_t username_len;
1227 part_encryption_aes256_t pea;
1228 unsigned char hash[sizeof (pea.hash)] = { 0 };
1230 gcry_cipher_hd_t cypher;
1233 /* Make sure at least the header if available. */
1234 if (buffer_len <= PART_ENCRYPTION_AES256_SIZE)
1236 ERROR ("network plugin: Decryption failed: "
1237 "Discarding short packet.");
1243 /* Copy the unencrypted information into `pea'. */
1244 BUFFER_READ (&pea.head.type, sizeof (pea.head.type));
1245 BUFFER_READ (&pea.head.length, sizeof (pea.head.length));
1247 /* Check the `part size'. */
1248 part_size = ntohs (pea.head.length);
1249 if ((part_size <= PART_ENCRYPTION_AES256_SIZE)
1250 || (part_size > buffer_len))
1252 ERROR ("network plugin: Decryption failed: "
1253 "Discarding part with invalid size.");
1257 /* Read the username */
1258 BUFFER_READ (&username_len, sizeof (username_len));
1259 username_len = ntohs (username_len);
1261 if ((username_len == 0)
1262 || (username_len > (part_size - (PART_ENCRYPTION_AES256_SIZE + 1))))
1264 ERROR ("network plugin: Decryption failed: "
1265 "Discarding part with invalid username length.");
1269 assert (username_len > 0);
1270 pea.username = malloc (username_len + 1);
1271 if (pea.username == NULL)
1273 ERROR ("network plugin: Decryption failed: "
1274 "malloc() failed.");
1277 BUFFER_READ (pea.username, username_len);
1278 pea.username[username_len] = 0;
1280 /* Last but not least, the initialization vector */
1281 BUFFER_READ (pea.iv, sizeof (pea.iv));
1283 /* Make sure we are at the right position */
1284 assert (buffer_offset == (username_len +
1285 PART_ENCRYPTION_AES256_SIZE - sizeof (pea.hash)));
1287 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
1291 ERROR ("network plugin: Decryption failed: "
1292 "Failed to get cypher. Username: %s", pea.username);
1293 sfree (pea.username);
1297 payload_len = part_size - (PART_ENCRYPTION_AES256_SIZE + username_len);
1298 assert (payload_len > 0);
1300 /* Decrypt the packet in-place */
1301 err = gcry_cipher_decrypt (cypher,
1302 buffer + buffer_offset,
1303 part_size - buffer_offset,
1304 /* in = */ NULL, /* in len = */ 0);
1307 sfree (pea.username);
1308 ERROR ("network plugin: gcry_cipher_decrypt returned: %s. Username: %s",
1309 gcry_strerror (err), pea.username);
1314 BUFFER_READ (pea.hash, sizeof (pea.hash));
1316 /* Make sure we're at the right position - again */
1317 assert (buffer_offset == (username_len + PART_ENCRYPTION_AES256_SIZE));
1318 assert (buffer_offset == (part_size - payload_len));
1320 /* Check hash sum */
1321 gcry_md_hash_buffer (GCRY_MD_SHA1, hash,
1322 buffer + buffer_offset, payload_len);
1323 if (memcmp (hash, pea.hash, sizeof (hash)) != 0)
1325 ERROR ("network plugin: Decryption failed: "
1326 "Checksum mismatch. Username: %s", pea.username);
1327 sfree (pea.username);
1331 parse_packet (se, buffer + buffer_offset, payload_len,
1332 flags | PP_ENCRYPTED, pea.username);
1334 /* XXX: Free pea.username?!? */
1336 /* Update return values */
1337 *ret_buffer = buffer + part_size;
1338 *ret_buffer_len = buffer_len - part_size;
1340 sfree (pea.username);
1343 } /* }}} int parse_part_encr_aes256 */
1344 /* #endif HAVE_LIBGCRYPT */
1346 #else /* if !HAVE_LIBGCRYPT */
1347 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1348 void **ret_buffer, size_t *ret_buffer_size, int flags)
1350 static int warning_has_been_printed = 0;
1354 size_t buffer_offset;
1359 buffer = *ret_buffer;
1360 buffer_size = *ret_buffer_size;
1363 /* parse_packet assures this minimum size. */
1364 assert (buffer_size >= (sizeof (ph.type) + sizeof (ph.length)));
1366 BUFFER_READ (&ph.type, sizeof (ph.type));
1367 BUFFER_READ (&ph.length, sizeof (ph.length));
1368 ph_length = ntohs (ph.length);
1370 if ((ph_length <= PART_ENCRYPTION_AES256_SIZE)
1371 || (ph_length > buffer_size))
1373 ERROR ("network plugin: AES-256 encrypted part "
1374 "with invalid length received.");
1378 if (warning_has_been_printed == 0)
1380 WARNING ("network plugin: Received encrypted packet, but the network "
1381 "plugin was not linked with libgcrypt, so I cannot "
1382 "decrypt it. The part will be discarded.");
1383 warning_has_been_printed = 1;
1386 *ret_buffer = (void *) (((char *) *ret_buffer) + ph_length);
1387 *ret_buffer_size -= ph_length;
1390 } /* }}} int parse_part_encr_aes256 */
1391 #endif /* !HAVE_LIBGCRYPT */
1395 static int parse_packet (sockent_t *se, /* {{{ */
1396 void *buffer, size_t buffer_size, int flags,
1397 const char *username)
1401 value_list_t vl = VALUE_LIST_INIT;
1402 notification_t n = { 0 };
1405 int packet_was_signed = (flags & PP_SIGNED);
1406 int packet_was_encrypted = (flags & PP_ENCRYPTED);
1407 int printed_ignore_warning = 0;
1408 #endif /* HAVE_LIBGCRYPT */
1411 memset (&vl, '\0', sizeof (vl));
1414 while ((status == 0) && (0 < buffer_size)
1415 && ((unsigned int) buffer_size > sizeof (part_header_t)))
1417 uint16_t pkg_length;
1420 memcpy ((void *) &pkg_type,
1423 memcpy ((void *) &pkg_length,
1424 (void *) (((char *) buffer) + sizeof (pkg_type)),
1425 sizeof (pkg_length));
1427 pkg_length = ntohs (pkg_length);
1428 pkg_type = ntohs (pkg_type);
1430 if (pkg_length > buffer_size)
1432 /* Ensure that this loop terminates eventually */
1433 if (pkg_length < (2 * sizeof (uint16_t)))
1436 if (pkg_type == TYPE_ENCR_AES256)
1438 status = parse_part_encr_aes256 (se,
1439 &buffer, &buffer_size, flags);
1444 else if ((se->data.server.security_level == SECURITY_LEVEL_ENCRYPT)
1445 && (packet_was_encrypted == 0))
1447 if (printed_ignore_warning == 0)
1449 INFO ("network plugin: Unencrypted packet or "
1450 "part has been ignored.");
1451 printed_ignore_warning = 1;
1453 buffer = ((char *) buffer) + pkg_length;
1454 buffer_size -= (size_t) pkg_length;
1457 #endif /* HAVE_LIBGCRYPT */
1458 else if (pkg_type == TYPE_SIGN_SHA256)
1460 status = parse_part_sign_sha256 (se,
1461 &buffer, &buffer_size, flags);
1464 ERROR ("network plugin: Verifying HMAC-SHA-256 "
1466 "with status %i.", status);
1471 else if ((se->data.server.security_level == SECURITY_LEVEL_SIGN)
1472 && (packet_was_encrypted == 0)
1473 && (packet_was_signed == 0))
1475 if (printed_ignore_warning == 0)
1477 INFO ("network plugin: Unsigned packet or "
1478 "part has been ignored.");
1479 printed_ignore_warning = 1;
1481 buffer = ((char *) buffer) + pkg_length;
1482 buffer_size -= (size_t) pkg_length;
1485 #endif /* HAVE_LIBGCRYPT */
1486 else if (pkg_type == TYPE_VALUES)
1488 status = parse_part_values (&buffer, &buffer_size,
1489 &vl.values, &vl.values_len);
1493 network_dispatch_values (&vl, username);
1497 else if (pkg_type == TYPE_TIME)
1500 status = parse_part_number (&buffer, &buffer_size,
1504 vl.time = TIME_T_TO_CDTIME_T (tmp);
1505 n.time = TIME_T_TO_CDTIME_T (tmp);
1508 else if (pkg_type == TYPE_TIME_HR)
1511 status = parse_part_number (&buffer, &buffer_size,
1515 vl.time = (cdtime_t) tmp;
1516 n.time = (cdtime_t) tmp;
1519 else if (pkg_type == TYPE_INTERVAL)
1522 status = parse_part_number (&buffer, &buffer_size,
1525 vl.interval = TIME_T_TO_CDTIME_T (tmp);
1527 else if (pkg_type == TYPE_INTERVAL_HR)
1530 status = parse_part_number (&buffer, &buffer_size,
1533 vl.interval = (cdtime_t) tmp;
1535 else if (pkg_type == TYPE_HOST)
1537 status = parse_part_string (&buffer, &buffer_size,
1538 vl.host, sizeof (vl.host));
1540 sstrncpy (n.host, vl.host, sizeof (n.host));
1542 else if (pkg_type == TYPE_PLUGIN)
1544 status = parse_part_string (&buffer, &buffer_size,
1545 vl.plugin, sizeof (vl.plugin));
1547 sstrncpy (n.plugin, vl.plugin,
1550 else if (pkg_type == TYPE_PLUGIN_INSTANCE)
1552 status = parse_part_string (&buffer, &buffer_size,
1554 sizeof (vl.plugin_instance));
1556 sstrncpy (n.plugin_instance,
1558 sizeof (n.plugin_instance));
1560 else if (pkg_type == TYPE_TYPE)
1562 status = parse_part_string (&buffer, &buffer_size,
1563 vl.type, sizeof (vl.type));
1565 sstrncpy (n.type, vl.type, sizeof (n.type));
1567 else if (pkg_type == TYPE_TYPE_INSTANCE)
1569 status = parse_part_string (&buffer, &buffer_size,
1571 sizeof (vl.type_instance));
1573 sstrncpy (n.type_instance, vl.type_instance,
1574 sizeof (n.type_instance));
1576 else if (pkg_type == TYPE_MESSAGE)
1578 status = parse_part_string (&buffer, &buffer_size,
1579 n.message, sizeof (n.message));
1585 else if ((n.severity != NOTIF_FAILURE)
1586 && (n.severity != NOTIF_WARNING)
1587 && (n.severity != NOTIF_OKAY))
1589 INFO ("network plugin: "
1590 "Ignoring notification with "
1591 "unknown severity %i.",
1594 else if (n.time == 0)
1596 INFO ("network plugin: "
1597 "Ignoring notification with "
1600 else if (strlen (n.message) == 0)
1602 INFO ("network plugin: "
1603 "Ignoring notification with "
1604 "an empty message.");
1608 network_dispatch_notification (&n);
1611 else if (pkg_type == TYPE_SEVERITY)
1614 status = parse_part_number (&buffer, &buffer_size,
1617 n.severity = (int) tmp;
1621 DEBUG ("network plugin: parse_packet: Unknown part"
1622 " type: 0x%04hx", pkg_type);
1623 buffer = ((char *) buffer) + pkg_length;
1624 buffer_size -= (size_t) pkg_length;
1626 } /* while (buffer_size > sizeof (part_header_t)) */
1628 if (status == 0 && buffer_size > 0)
1629 WARNING ("network plugin: parse_packet: Received truncated "
1630 "packet, try increasing `MaxPacketSize'");
1633 } /* }}} int parse_packet */
1635 static void free_sockent_client (struct sockent_client *sec) /* {{{ */
1644 sfree (sec->username);
1645 sfree (sec->password);
1646 if (sec->cypher != NULL)
1647 gcry_cipher_close (sec->cypher);
1649 } /* }}} void free_sockent_client */
1651 static void free_sockent_server (struct sockent_server *ses) /* {{{ */
1653 for (size_t i = 0; i < ses->fd_num; i++)
1655 if (ses->fd[i] >= 0)
1664 sfree (ses->auth_file);
1665 fbh_destroy (ses->userdb);
1666 if (ses->cypher != NULL)
1667 gcry_cipher_close (ses->cypher);
1669 } /* }}} void free_sockent_server */
1671 static void sockent_destroy (sockent_t *se) /* {{{ */
1675 DEBUG ("network plugin: sockent_destroy (se = %p);", (void *) se);
1682 sfree (se->service);
1684 if (se->type == SOCKENT_TYPE_CLIENT)
1685 free_sockent_client (&se->data.client);
1687 free_sockent_server (&se->data.server);
1692 } /* }}} void sockent_destroy */
1695 * int network_set_ttl
1697 * Set the `IP_MULTICAST_TTL', `IP_TTL', `IPV6_MULTICAST_HOPS' or
1698 * `IPV6_UNICAST_HOPS', depending on which option is applicable.
1700 * The `struct addrinfo' is used to destinguish between unicast and multicast
1703 static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
1705 DEBUG ("network plugin: network_set_ttl: network_config_ttl = %i;",
1706 network_config_ttl);
1708 assert (se->type == SOCKENT_TYPE_CLIENT);
1710 if ((network_config_ttl < 1) || (network_config_ttl > 255))
1713 if (ai->ai_family == AF_INET)
1715 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1718 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1719 optname = IP_MULTICAST_TTL;
1723 if (setsockopt (se->data.client.fd, IPPROTO_IP, optname,
1724 &network_config_ttl,
1725 sizeof (network_config_ttl)) != 0)
1728 ERROR ("network plugin: setsockopt (ipv4-ttl): %s",
1729 sstrerror (errno, errbuf, sizeof (errbuf)));
1733 else if (ai->ai_family == AF_INET6)
1735 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1736 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1739 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1740 optname = IPV6_MULTICAST_HOPS;
1742 optname = IPV6_UNICAST_HOPS;
1744 if (setsockopt (se->data.client.fd, IPPROTO_IPV6, optname,
1745 &network_config_ttl,
1746 sizeof (network_config_ttl)) != 0)
1749 ERROR ("network plugin: setsockopt(ipv6-ttl): %s",
1750 sstrerror (errno, errbuf,
1757 } /* int network_set_ttl */
1759 static int network_set_interface (const sockent_t *se, const struct addrinfo *ai) /* {{{ */
1761 DEBUG ("network plugin: network_set_interface: interface index = %i;",
1764 assert (se->type == SOCKENT_TYPE_CLIENT);
1766 if (ai->ai_family == AF_INET)
1768 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1770 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1772 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1773 /* If possible, use the "ip_mreqn" structure which has
1774 * an "interface index" member. Using the interface
1775 * index is preferred here, because of its similarity
1776 * to the way IPv6 handles this. Unfortunately, it
1777 * appears not to be portable. */
1778 struct ip_mreqn mreq = {
1779 .imr_multiaddr.s_addr = addr->sin_addr.s_addr,
1780 .imr_address.s_addr = ntohl (INADDR_ANY),
1781 .imr_ifindex = se->interface
1784 struct ip_mreq mreq = {
1785 .imr_multiaddr.s_addr = addr->sin_addr.s_addr,
1786 .imr_interface.s_addr = ntohl (INADDR_ANY)
1790 if (setsockopt (se->data.client.fd, IPPROTO_IP, IP_MULTICAST_IF,
1791 &mreq, sizeof (mreq)) != 0)
1794 ERROR ("network plugin: setsockopt (ipv4-multicast-if): %s",
1795 sstrerror (errno, errbuf, sizeof (errbuf)));
1802 else if (ai->ai_family == AF_INET6)
1804 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1806 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1808 if (setsockopt (se->data.client.fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
1810 sizeof (se->interface)) != 0)
1813 ERROR ("network plugin: setsockopt (ipv6-multicast-if): %s",
1814 sstrerror (errno, errbuf,
1823 /* else: Not a multicast interface. */
1824 if (se->interface != 0)
1826 #if defined(HAVE_IF_INDEXTONAME) && HAVE_IF_INDEXTONAME && defined(SO_BINDTODEVICE)
1827 char interface_name[IFNAMSIZ];
1829 if (if_indextoname (se->interface, interface_name) == NULL)
1832 DEBUG ("network plugin: Binding socket to interface %s", interface_name);
1834 if (setsockopt (se->data.client.fd, SOL_SOCKET, SO_BINDTODEVICE,
1836 sizeof(interface_name)) == -1 )
1839 ERROR ("network plugin: setsockopt (bind-if): %s",
1840 sstrerror (errno, errbuf, sizeof (errbuf)));
1843 /* #endif HAVE_IF_INDEXTONAME && SO_BINDTODEVICE */
1846 WARNING ("network plugin: Cannot set the interface on a unicast "
1848 # if !defined(SO_BINDTODEVICE)
1849 "the \"SO_BINDTODEVICE\" socket option "
1851 "the \"if_indextoname\" function "
1853 "is not available on your system.");
1859 } /* }}} network_set_interface */
1861 static int network_bind_socket (int fd, const struct addrinfo *ai, const int interface_idx)
1870 /* allow multiple sockets to use the same PORT number */
1871 if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR,
1872 &yes, sizeof(yes)) == -1) {
1874 ERROR ("network plugin: setsockopt (reuseaddr): %s",
1875 sstrerror (errno, errbuf, sizeof (errbuf)));
1879 DEBUG ("fd = %i; calling `bind'", fd);
1881 if (bind (fd, ai->ai_addr, ai->ai_addrlen) == -1)
1885 sstrerror (errno, errbuf, sizeof (errbuf)));
1889 if (ai->ai_family == AF_INET)
1891 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1892 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1894 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1895 struct ip_mreqn mreq;
1897 struct ip_mreq mreq;
1900 DEBUG ("fd = %i; IPv4 multicast address found", fd);
1902 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1903 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1904 /* Set the interface using the interface index if
1905 * possible (available). Unfortunately, the struct
1906 * ip_mreqn is not portable. */
1907 mreq.imr_address.s_addr = ntohl (INADDR_ANY);
1908 mreq.imr_ifindex = interface_idx;
1910 mreq.imr_interface.s_addr = ntohl (INADDR_ANY);
1913 if (setsockopt (fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1914 &loop, sizeof (loop)) == -1)
1917 ERROR ("network plugin: setsockopt (multicast-loop): %s",
1918 sstrerror (errno, errbuf,
1923 if (setsockopt (fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1924 &mreq, sizeof (mreq)) == -1)
1927 ERROR ("network plugin: setsockopt (add-membership): %s",
1928 sstrerror (errno, errbuf,
1936 else if (ai->ai_family == AF_INET6)
1938 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1939 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1940 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1942 struct ipv6_mreq mreq;
1944 DEBUG ("fd = %i; IPv6 multicast address found", fd);
1946 memcpy (&mreq.ipv6mr_multiaddr,
1948 sizeof (addr->sin6_addr));
1950 /* http://developer.apple.com/documentation/Darwin/Reference/ManPages/man4/ip6.4.html
1951 * ipv6mr_interface may be set to zeroes to
1952 * choose the default multicast interface or to
1953 * the index of a particular multicast-capable
1954 * interface if the host is multihomed.
1955 * Membership is associ-associated with a
1956 * single interface; programs running on
1957 * multihomed hosts may need to join the same
1958 * group on more than one interface.*/
1959 mreq.ipv6mr_interface = interface_idx;
1961 if (setsockopt (fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
1962 &loop, sizeof (loop)) == -1)
1965 ERROR ("network plugin: setsockopt (ipv6-multicast-loop): %s",
1966 sstrerror (errno, errbuf,
1971 if (setsockopt (fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
1972 &mreq, sizeof (mreq)) == -1)
1975 ERROR ("network plugin: setsockopt (ipv6-add-membership): %s",
1976 sstrerror (errno, errbuf,
1985 #if defined(HAVE_IF_INDEXTONAME) && HAVE_IF_INDEXTONAME && defined(SO_BINDTODEVICE)
1986 /* if a specific interface was set, bind the socket to it. But to avoid
1987 * possible problems with multicast routing, only do that for non-multicast
1989 if (interface_idx != 0)
1991 char interface_name[IFNAMSIZ];
1993 if (if_indextoname (interface_idx, interface_name) == NULL)
1996 DEBUG ("fd = %i; Binding socket to interface %s", fd, interface_name);
1998 if (setsockopt (fd, SOL_SOCKET, SO_BINDTODEVICE,
2000 sizeof(interface_name)) == -1 )
2003 ERROR ("network plugin: setsockopt (bind-if): %s",
2004 sstrerror (errno, errbuf, sizeof (errbuf)));
2008 #endif /* HAVE_IF_INDEXTONAME && SO_BINDTODEVICE */
2011 } /* int network_bind_socket */
2013 /* Initialize a sockent structure. `type' must be either `SOCKENT_TYPE_CLIENT'
2014 * or `SOCKENT_TYPE_SERVER' */
2015 static sockent_t *sockent_create (int type) /* {{{ */
2019 if ((type != SOCKENT_TYPE_CLIENT) && (type != SOCKENT_TYPE_SERVER))
2022 se = calloc (1, sizeof (*se));
2032 if (type == SOCKENT_TYPE_SERVER)
2034 se->data.server.fd = NULL;
2035 se->data.server.fd_num = 0;
2037 se->data.server.security_level = SECURITY_LEVEL_NONE;
2038 se->data.server.auth_file = NULL;
2039 se->data.server.userdb = NULL;
2040 se->data.server.cypher = NULL;
2045 se->data.client.fd = -1;
2046 se->data.client.addr = NULL;
2047 se->data.client.resolve_interval = 0;
2048 se->data.client.next_resolve_reconnect = 0;
2050 se->data.client.security_level = SECURITY_LEVEL_NONE;
2051 se->data.client.username = NULL;
2052 se->data.client.password = NULL;
2053 se->data.client.cypher = NULL;
2058 } /* }}} sockent_t *sockent_create */
2060 static int sockent_init_crypto (sockent_t *se) /* {{{ */
2062 #if HAVE_LIBGCRYPT /* {{{ */
2063 if (se->type == SOCKENT_TYPE_CLIENT)
2065 if (se->data.client.security_level > SECURITY_LEVEL_NONE)
2067 if (network_init_gcrypt () < 0)
2069 ERROR ("network plugin: Cannot configure client socket with "
2070 "security: Failed to initialize crypto library.");
2074 if ((se->data.client.username == NULL)
2075 || (se->data.client.password == NULL))
2077 ERROR ("network plugin: Client socket with "
2078 "security requested, but no "
2079 "credentials are configured.");
2082 gcry_md_hash_buffer (GCRY_MD_SHA256,
2083 se->data.client.password_hash,
2084 se->data.client.password,
2085 strlen (se->data.client.password));
2088 else /* (se->type == SOCKENT_TYPE_SERVER) */
2090 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
2092 if (network_init_gcrypt () < 0)
2094 ERROR ("network plugin: Cannot configure server socket with "
2095 "security: Failed to initialize crypto library.");
2099 if (se->data.server.auth_file == NULL)
2101 ERROR ("network plugin: Server socket with "
2102 "security requested, but no "
2103 "password file is configured.");
2107 if (se->data.server.auth_file != NULL)
2109 se->data.server.userdb = fbh_create (se->data.server.auth_file);
2110 if (se->data.server.userdb == NULL)
2112 ERROR ("network plugin: Reading password file "
2114 se->data.server.auth_file);
2115 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
2120 #endif /* }}} HAVE_LIBGCRYPT */
2123 } /* }}} int sockent_init_crypto */
2125 static int sockent_client_disconnect (sockent_t *se) /* {{{ */
2127 struct sockent_client *client;
2129 if ((se == NULL) || (se->type != SOCKENT_TYPE_CLIENT))
2132 client = &se->data.client;
2133 if (client->fd >= 0) /* connected */
2139 sfree (client->addr);
2140 client->addrlen = 0;
2143 } /* }}} int sockent_client_disconnect */
2145 static int sockent_client_connect (sockent_t *se) /* {{{ */
2147 static c_complain_t complaint = C_COMPLAIN_INIT_STATIC;
2149 struct sockent_client *client;
2150 struct addrinfo *ai_list;
2152 _Bool reconnect = 0;
2155 if ((se == NULL) || (se->type != SOCKENT_TYPE_CLIENT))
2158 client = &se->data.client;
2161 if (client->resolve_interval != 0 && client->next_resolve_reconnect < now) {
2162 DEBUG("network plugin: Reconnecting socket, resolve_interval = %lf, next_resolve_reconnect = %lf",
2163 CDTIME_T_TO_DOUBLE(client->resolve_interval), CDTIME_T_TO_DOUBLE(client->next_resolve_reconnect));
2167 if (client->fd >= 0 && !reconnect) /* already connected and not stale*/
2170 struct addrinfo ai_hints = {
2171 .ai_family = AF_UNSPEC,
2172 .ai_flags = AI_ADDRCONFIG,
2173 .ai_protocol = IPPROTO_UDP,
2174 .ai_socktype = SOCK_DGRAM
2177 status = getaddrinfo (se->node,
2178 (se->service != NULL) ? se->service : NET_DEFAULT_PORT,
2179 &ai_hints, &ai_list);
2182 c_complain (LOG_ERR, &complaint,
2183 "network plugin: getaddrinfo (%s, %s) failed: %s",
2184 (se->node == NULL) ? "(null)" : se->node,
2185 (se->service == NULL) ? "(null)" : se->service,
2186 gai_strerror (status));
2191 c_release (LOG_NOTICE, &complaint,
2192 "network plugin: Successfully resolved \"%s\".",
2196 for (struct addrinfo *ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
2198 if (client->fd >= 0) /* when we reconnect */
2199 sockent_client_disconnect(se);
2201 client->fd = socket (ai_ptr->ai_family,
2202 ai_ptr->ai_socktype,
2203 ai_ptr->ai_protocol);
2207 ERROR ("network plugin: socket(2) failed: %s",
2208 sstrerror (errno, errbuf,
2213 client->addr = calloc (1, sizeof (*client->addr));
2214 if (client->addr == NULL)
2216 ERROR ("network plugin: calloc failed.");
2222 assert (sizeof (*client->addr) >= ai_ptr->ai_addrlen);
2223 memcpy (client->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
2224 client->addrlen = ai_ptr->ai_addrlen;
2226 network_set_ttl (se, ai_ptr);
2227 network_set_interface (se, ai_ptr);
2229 /* We don't open more than one write-socket per
2230 * node/service pair.. */
2234 freeaddrinfo (ai_list);
2238 if (client->resolve_interval > 0)
2239 client->next_resolve_reconnect = now + client->resolve_interval;
2241 } /* }}} int sockent_client_connect */
2243 /* Open the file descriptors for a initialized sockent structure. */
2244 static int sockent_server_listen (sockent_t *se) /* {{{ */
2246 struct addrinfo *ai_list;
2250 const char *service;
2255 assert (se->data.server.fd == NULL);
2256 assert (se->data.server.fd_num == 0);
2259 service = se->service;
2261 if (service == NULL)
2262 service = NET_DEFAULT_PORT;
2264 DEBUG ("network plugin: sockent_server_listen: node = %s; service = %s;",
2267 struct addrinfo ai_hints = {
2268 .ai_family = AF_UNSPEC,
2269 .ai_flags = AI_ADDRCONFIG | AI_PASSIVE,
2270 .ai_protocol = IPPROTO_UDP,
2271 .ai_socktype = SOCK_DGRAM
2274 status = getaddrinfo (node, service, &ai_hints, &ai_list);
2277 ERROR ("network plugin: getaddrinfo (%s, %s) failed: %s",
2278 (se->node == NULL) ? "(null)" : se->node,
2279 (se->service == NULL) ? "(null)" : se->service,
2280 gai_strerror (status));
2284 for (struct addrinfo *ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
2288 tmp = realloc (se->data.server.fd,
2289 sizeof (*tmp) * (se->data.server.fd_num + 1));
2292 ERROR ("network plugin: realloc failed.");
2295 se->data.server.fd = tmp;
2296 tmp = se->data.server.fd + se->data.server.fd_num;
2298 *tmp = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
2299 ai_ptr->ai_protocol);
2303 ERROR ("network plugin: socket(2) failed: %s",
2304 sstrerror (errno, errbuf,
2309 status = network_bind_socket (*tmp, ai_ptr, se->interface);
2317 se->data.server.fd_num++;
2319 } /* for (ai_list) */
2321 freeaddrinfo (ai_list);
2323 if (se->data.server.fd_num == 0)
2326 } /* }}} int sockent_server_listen */
2328 /* Add a sockent to the global list of sockets */
2329 static int sockent_add (sockent_t *se) /* {{{ */
2331 sockent_t *last_ptr;
2336 if (se->type == SOCKENT_TYPE_SERVER)
2340 tmp = realloc (listen_sockets_pollfd,
2341 sizeof (*tmp) * (listen_sockets_num
2342 + se->data.server.fd_num));
2345 ERROR ("network plugin: realloc failed.");
2348 listen_sockets_pollfd = tmp;
2349 tmp = listen_sockets_pollfd + listen_sockets_num;
2351 for (size_t i = 0; i < se->data.server.fd_num; i++)
2353 memset (tmp + i, 0, sizeof (*tmp));
2354 tmp[i].fd = se->data.server.fd[i];
2355 tmp[i].events = POLLIN | POLLPRI;
2359 listen_sockets_num += se->data.server.fd_num;
2361 if (listen_sockets == NULL)
2363 listen_sockets = se;
2366 last_ptr = listen_sockets;
2368 else /* if (se->type == SOCKENT_TYPE_CLIENT) */
2370 if (sending_sockets == NULL)
2372 sending_sockets = se;
2375 last_ptr = sending_sockets;
2378 while (last_ptr->next != NULL)
2379 last_ptr = last_ptr->next;
2380 last_ptr->next = se;
2383 } /* }}} int sockent_add */
2385 static void *dispatch_thread (void __attribute__((unused)) *arg) /* {{{ */
2389 receive_list_entry_t *ent;
2392 /* Lock and wait for more data to come in */
2393 pthread_mutex_lock (&receive_list_lock);
2394 while ((listen_loop == 0)
2395 && (receive_list_head == NULL))
2396 pthread_cond_wait (&receive_list_cond, &receive_list_lock);
2398 /* Remove the head entry and unlock */
2399 ent = receive_list_head;
2401 receive_list_head = ent->next;
2402 receive_list_length--;
2403 pthread_mutex_unlock (&receive_list_lock);
2405 /* Check whether we are supposed to exit. We do NOT check `listen_loop'
2406 * because we dispatch all missing packets before shutting down. */
2410 /* Look for the correct `sockent_t' */
2411 se = listen_sockets;
2416 for (i = 0; i < se->data.server.fd_num; i++)
2417 if (se->data.server.fd[i] == ent->fd)
2420 if (i < se->data.server.fd_num)
2428 ERROR ("network plugin: Got packet from FD %i, but can't "
2429 "find an appropriate socket entry.",
2436 parse_packet (se, ent->data, ent->data_len, /* flags = */ 0,
2437 /* username = */ NULL);
2443 } /* }}} void *dispatch_thread */
2445 static int network_receive (void) /* {{{ */
2447 char buffer[network_config_packet_size];
2452 receive_list_entry_t *private_list_head;
2453 receive_list_entry_t *private_list_tail;
2454 uint64_t private_list_length;
2456 assert (listen_sockets_num > 0);
2458 private_list_head = NULL;
2459 private_list_tail = NULL;
2460 private_list_length = 0;
2462 while (listen_loop == 0)
2464 status = poll (listen_sockets_pollfd, listen_sockets_num, -1);
2470 ERROR ("network plugin: poll(2) failed: %s",
2471 sstrerror (errno, errbuf, sizeof (errbuf)));
2475 for (size_t i = 0; (i < listen_sockets_num) && (status > 0); i++)
2477 receive_list_entry_t *ent;
2479 if ((listen_sockets_pollfd[i].revents
2480 & (POLLIN | POLLPRI)) == 0)
2484 buffer_len = recv (listen_sockets_pollfd[i].fd,
2485 buffer, sizeof (buffer),
2490 status = (errno != 0) ? errno : -1;
2491 ERROR ("network plugin: recv(2) failed: %s",
2492 sstrerror (errno, errbuf, sizeof (errbuf)));
2496 stats_octets_rx += ((uint64_t) buffer_len);
2499 /* TODO: Possible performance enhancement: Do not free
2500 * these entries in the dispatch thread but put them in
2501 * another list, so we don't have to allocate more and
2502 * more of these structures. */
2503 ent = calloc (1, sizeof (*ent));
2506 ERROR ("network plugin: calloc failed.");
2511 ent->data = malloc (network_config_packet_size);
2512 if (ent->data == NULL)
2515 ERROR ("network plugin: malloc failed.");
2519 ent->fd = listen_sockets_pollfd[i].fd;
2522 memcpy (ent->data, buffer, buffer_len);
2523 ent->data_len = buffer_len;
2525 if (private_list_head == NULL)
2526 private_list_head = ent;
2528 private_list_tail->next = ent;
2529 private_list_tail = ent;
2530 private_list_length++;
2532 /* Do not block here. Blocking here has led to
2533 * insufficient performance in the past. */
2534 if (pthread_mutex_trylock (&receive_list_lock) == 0)
2536 assert (((receive_list_head == NULL) && (receive_list_length == 0))
2537 || ((receive_list_head != NULL) && (receive_list_length != 0)));
2539 if (receive_list_head == NULL)
2540 receive_list_head = private_list_head;
2542 receive_list_tail->next = private_list_head;
2543 receive_list_tail = private_list_tail;
2544 receive_list_length += private_list_length;
2546 pthread_cond_signal (&receive_list_cond);
2547 pthread_mutex_unlock (&receive_list_lock);
2549 private_list_head = NULL;
2550 private_list_tail = NULL;
2551 private_list_length = 0;
2555 } /* for (listen_sockets_pollfd) */
2559 } /* while (listen_loop == 0) */
2561 /* Make sure everything is dispatched before exiting. */
2562 if (private_list_head != NULL)
2564 pthread_mutex_lock (&receive_list_lock);
2566 if (receive_list_head == NULL)
2567 receive_list_head = private_list_head;
2569 receive_list_tail->next = private_list_head;
2570 receive_list_tail = private_list_tail;
2571 receive_list_length += private_list_length;
2573 pthread_cond_signal (&receive_list_cond);
2574 pthread_mutex_unlock (&receive_list_lock);
2578 } /* }}} int network_receive */
2580 static void *receive_thread (void __attribute__((unused)) *arg)
2582 return (network_receive () ? (void *) 1 : (void *) 0);
2583 } /* void *receive_thread */
2585 static void network_init_buffer (void)
2587 memset (send_buffer, 0, network_config_packet_size);
2588 send_buffer_ptr = send_buffer;
2589 send_buffer_fill = 0;
2590 send_buffer_last_update = 0;
2592 memset (&send_buffer_vl, 0, sizeof (send_buffer_vl));
2593 } /* int network_init_buffer */
2595 static void network_send_buffer_plain (sockent_t *se, /* {{{ */
2596 const char *buffer, size_t buffer_size)
2602 status = sockent_client_connect (se);
2606 status = sendto (se->data.client.fd, buffer, buffer_size,
2608 (struct sockaddr *) se->data.client.addr,
2609 se->data.client.addrlen);
2614 if ((errno == EINTR) || (errno == EAGAIN))
2617 ERROR ("network plugin: sendto failed: %s. Closing sending socket.",
2618 sstrerror (errno, errbuf, sizeof (errbuf)));
2619 sockent_client_disconnect (se);
2625 } /* }}} void network_send_buffer_plain */
2628 #define BUFFER_ADD(p,s) do { \
2629 memcpy (buffer + buffer_offset, (p), (s)); \
2630 buffer_offset += (s); \
2633 static void network_send_buffer_signed (sockent_t *se, /* {{{ */
2634 const char *in_buffer, size_t in_buffer_size)
2636 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2637 size_t buffer_offset;
2638 size_t username_len;
2642 unsigned char *hash;
2645 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
2648 ERROR ("network plugin: Creating HMAC object failed: %s",
2649 gcry_strerror (err));
2653 err = gcry_md_setkey (hd, se->data.client.password,
2654 strlen (se->data.client.password));
2657 ERROR ("network plugin: gcry_md_setkey failed: %s",
2658 gcry_strerror (err));
2663 username_len = strlen (se->data.client.username);
2664 if (username_len > (BUFF_SIG_SIZE - PART_SIGNATURE_SHA256_SIZE))
2666 ERROR ("network plugin: Username too long: %s",
2667 se->data.client.username);
2671 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE,
2672 se->data.client.username, username_len);
2673 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE + username_len,
2674 in_buffer, in_buffer_size);
2676 /* Initialize the `ps' structure. */
2677 part_signature_sha256_t ps = {
2678 .head.type = htons (TYPE_SIGN_SHA256),
2679 .head.length = htons (PART_SIGNATURE_SHA256_SIZE + username_len)
2682 /* Calculate the hash value. */
2683 gcry_md_write (hd, buffer + PART_SIGNATURE_SHA256_SIZE,
2684 username_len + in_buffer_size);
2685 hash = gcry_md_read (hd, GCRY_MD_SHA256);
2688 ERROR ("network plugin: gcry_md_read failed.");
2692 memcpy (ps.hash, hash, sizeof (ps.hash));
2694 /* Add the header */
2697 BUFFER_ADD (&ps.head.type, sizeof (ps.head.type));
2698 BUFFER_ADD (&ps.head.length, sizeof (ps.head.length));
2699 BUFFER_ADD (ps.hash, sizeof (ps.hash));
2701 assert (buffer_offset == PART_SIGNATURE_SHA256_SIZE);
2706 buffer_offset = PART_SIGNATURE_SHA256_SIZE + username_len + in_buffer_size;
2707 network_send_buffer_plain (se, buffer, buffer_offset);
2708 } /* }}} void network_send_buffer_signed */
2710 static void network_send_buffer_encrypted (sockent_t *se, /* {{{ */
2711 const char *in_buffer, size_t in_buffer_size)
2713 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2715 size_t buffer_offset;
2717 size_t username_len;
2719 gcry_cipher_hd_t cypher;
2721 /* Initialize the header fields */
2722 part_encryption_aes256_t pea = {
2723 .head.type = htons (TYPE_ENCR_AES256),
2724 .username = se->data.client.username
2727 username_len = strlen (pea.username);
2728 if ((PART_ENCRYPTION_AES256_SIZE + username_len) > BUFF_SIG_SIZE)
2730 ERROR ("network plugin: Username too long: %s", pea.username);
2734 buffer_size = PART_ENCRYPTION_AES256_SIZE + username_len + in_buffer_size;
2735 header_size = PART_ENCRYPTION_AES256_SIZE + username_len
2736 - sizeof (pea.hash);
2738 assert (buffer_size <= sizeof (buffer));
2739 DEBUG ("network plugin: network_send_buffer_encrypted: "
2740 "buffer_size = %zu;", buffer_size);
2742 pea.head.length = htons ((uint16_t) (PART_ENCRYPTION_AES256_SIZE
2743 + username_len + in_buffer_size));
2744 pea.username_length = htons ((uint16_t) username_len);
2746 /* Chose a random initialization vector. */
2747 gcry_randomize ((void *) &pea.iv, sizeof (pea.iv), GCRY_STRONG_RANDOM);
2749 /* Create hash of the payload */
2750 gcry_md_hash_buffer (GCRY_MD_SHA1, pea.hash, in_buffer, in_buffer_size);
2752 /* Initialize the buffer */
2754 memset (buffer, 0, sizeof (buffer));
2757 BUFFER_ADD (&pea.head.type, sizeof (pea.head.type));
2758 BUFFER_ADD (&pea.head.length, sizeof (pea.head.length));
2759 BUFFER_ADD (&pea.username_length, sizeof (pea.username_length));
2760 BUFFER_ADD (pea.username, username_len);
2761 BUFFER_ADD (pea.iv, sizeof (pea.iv));
2762 assert (buffer_offset == header_size);
2763 BUFFER_ADD (pea.hash, sizeof (pea.hash));
2764 BUFFER_ADD (in_buffer, in_buffer_size);
2766 assert (buffer_offset == buffer_size);
2768 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
2769 se->data.client.password);
2773 /* Encrypt the buffer in-place */
2774 err = gcry_cipher_encrypt (cypher,
2775 buffer + header_size,
2776 buffer_size - header_size,
2777 /* in = */ NULL, /* in len = */ 0);
2780 ERROR ("network plugin: gcry_cipher_encrypt returned: %s",
2781 gcry_strerror (err));
2785 /* Send it out without further modifications */
2786 network_send_buffer_plain (se, buffer, buffer_size);
2787 } /* }}} void network_send_buffer_encrypted */
2789 #endif /* HAVE_LIBGCRYPT */
2791 static void network_send_buffer (char *buffer, size_t buffer_len) /* {{{ */
2793 DEBUG ("network plugin: network_send_buffer: buffer_len = %zu", buffer_len);
2795 for (sockent_t *se = sending_sockets; se != NULL; se = se->next)
2798 if (se->data.client.security_level == SECURITY_LEVEL_ENCRYPT)
2799 network_send_buffer_encrypted (se, buffer, buffer_len);
2800 else if (se->data.client.security_level == SECURITY_LEVEL_SIGN)
2801 network_send_buffer_signed (se, buffer, buffer_len);
2802 else /* if (se->data.client.security_level == SECURITY_LEVEL_NONE) */
2803 #endif /* HAVE_LIBGCRYPT */
2804 network_send_buffer_plain (se, buffer, buffer_len);
2805 } /* for (sending_sockets) */
2806 } /* }}} void network_send_buffer */
2808 static int add_to_buffer (char *buffer, size_t buffer_size, /* {{{ */
2809 value_list_t *vl_def,
2810 const data_set_t *ds, const value_list_t *vl)
2812 char *buffer_orig = buffer;
2814 if (strcmp (vl_def->host, vl->host) != 0)
2816 if (write_part_string (&buffer, &buffer_size, TYPE_HOST,
2817 vl->host, strlen (vl->host)) != 0)
2819 sstrncpy (vl_def->host, vl->host, sizeof (vl_def->host));
2822 if (vl_def->time != vl->time)
2824 if (write_part_number (&buffer, &buffer_size, TYPE_TIME_HR,
2825 (uint64_t) vl->time))
2827 vl_def->time = vl->time;
2830 if (vl_def->interval != vl->interval)
2832 if (write_part_number (&buffer, &buffer_size, TYPE_INTERVAL_HR,
2833 (uint64_t) vl->interval))
2835 vl_def->interval = vl->interval;
2838 if (strcmp (vl_def->plugin, vl->plugin) != 0)
2840 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN,
2841 vl->plugin, strlen (vl->plugin)) != 0)
2843 sstrncpy (vl_def->plugin, vl->plugin, sizeof (vl_def->plugin));
2846 if (strcmp (vl_def->plugin_instance, vl->plugin_instance) != 0)
2848 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
2849 vl->plugin_instance,
2850 strlen (vl->plugin_instance)) != 0)
2852 sstrncpy (vl_def->plugin_instance, vl->plugin_instance, sizeof (vl_def->plugin_instance));
2855 if (strcmp (vl_def->type, vl->type) != 0)
2857 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
2858 vl->type, strlen (vl->type)) != 0)
2860 sstrncpy (vl_def->type, ds->type, sizeof (vl_def->type));
2863 if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
2865 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE_INSTANCE,
2867 strlen (vl->type_instance)) != 0)
2869 sstrncpy (vl_def->type_instance, vl->type_instance, sizeof (vl_def->type_instance));
2872 if (write_part_values (&buffer, &buffer_size, ds, vl) != 0)
2875 return (buffer - buffer_orig);
2876 } /* }}} int add_to_buffer */
2878 static void flush_buffer (void)
2880 DEBUG ("network plugin: flush_buffer: send_buffer_fill = %i",
2883 network_send_buffer (send_buffer, (size_t) send_buffer_fill);
2885 stats_octets_tx += ((uint64_t) send_buffer_fill);
2888 network_init_buffer ();
2891 static int network_write (const data_set_t *ds, const value_list_t *vl,
2892 user_data_t __attribute__((unused)) *user_data)
2896 /* listen_loop is set to non-zero in the shutdown callback, which is
2897 * guaranteed to be called *after* all the write threads have been shut
2899 assert (listen_loop == 0);
2901 if (!check_send_okay (vl))
2904 char name[6*DATA_MAX_NAME_LEN];
2905 FORMAT_VL (name, sizeof (name), vl);
2906 name[sizeof (name) - 1] = 0;
2907 DEBUG ("network plugin: network_write: "
2908 "NOT sending %s.", name);
2910 /* Counter is not protected by another lock and may be reached by
2911 * multiple threads */
2912 pthread_mutex_lock (&stats_lock);
2913 stats_values_not_sent++;
2914 pthread_mutex_unlock (&stats_lock);
2918 uc_meta_data_add_unsigned_int (vl,
2919 "network:time_sent", (uint64_t) vl->time);
2921 pthread_mutex_lock (&send_buffer_lock);
2923 status = add_to_buffer (send_buffer_ptr,
2924 network_config_packet_size - (send_buffer_fill + BUFF_SIG_SIZE),
2929 /* status == bytes added to the buffer */
2930 send_buffer_fill += status;
2931 send_buffer_ptr += status;
2932 send_buffer_last_update = cdtime();
2934 stats_values_sent++;
2940 status = add_to_buffer (send_buffer_ptr,
2941 network_config_packet_size - (send_buffer_fill + BUFF_SIG_SIZE),
2947 send_buffer_fill += status;
2948 send_buffer_ptr += status;
2950 stats_values_sent++;
2956 ERROR ("network plugin: Unable to append to the "
2957 "buffer for some weird reason");
2959 else if ((network_config_packet_size - send_buffer_fill) < 15)
2964 pthread_mutex_unlock (&send_buffer_lock);
2966 return ((status < 0) ? -1 : 0);
2967 } /* int network_write */
2969 static int network_config_set_ttl (const oconfig_item_t *ci) /* {{{ */
2973 if (cf_util_get_int (ci, &tmp) != 0)
2975 else if ((tmp > 0) && (tmp <= 255))
2976 network_config_ttl = tmp;
2978 WARNING ("network plugin: The `TimeToLive' must be between 1 and 255.");
2983 } /* }}} int network_config_set_ttl */
2985 static int network_config_set_interface (const oconfig_item_t *ci, /* {{{ */
2990 if (cf_util_get_string_buffer (ci, if_name, sizeof (if_name)) != 0)
2993 *interface = if_nametoindex (if_name);
2995 } /* }}} int network_config_set_interface */
2997 static int network_config_set_buffer_size (const oconfig_item_t *ci) /* {{{ */
3001 if (cf_util_get_int (ci, &tmp) != 0)
3003 else if ((tmp >= 1024) && (tmp <= 65535))
3004 network_config_packet_size = tmp;
3006 WARNING ("network plugin: The `MaxPacketSize' must be between 1024 and 65535.");
3011 } /* }}} int network_config_set_buffer_size */
3014 static int network_config_set_security_level (oconfig_item_t *ci, /* {{{ */
3018 if ((ci->values_num != 1)
3019 || (ci->values[0].type != OCONFIG_TYPE_STRING))
3021 WARNING ("network plugin: The `SecurityLevel' config option needs exactly "
3022 "one string argument.");
3026 str = ci->values[0].value.string;
3027 if (strcasecmp ("Encrypt", str) == 0)
3028 *retval = SECURITY_LEVEL_ENCRYPT;
3029 else if (strcasecmp ("Sign", str) == 0)
3030 *retval = SECURITY_LEVEL_SIGN;
3031 else if (strcasecmp ("None", str) == 0)
3032 *retval = SECURITY_LEVEL_NONE;
3035 WARNING ("network plugin: Unknown security level: %s.", str);
3040 } /* }}} int network_config_set_security_level */
3041 #endif /* HAVE_LIBGCRYPT */
3043 static int network_config_add_listen (const oconfig_item_t *ci) /* {{{ */
3048 if ((ci->values_num < 1) || (ci->values_num > 2)
3049 || (ci->values[0].type != OCONFIG_TYPE_STRING)
3050 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
3052 ERROR ("network plugin: The `%s' config option needs "
3053 "one or two string arguments.", ci->key);
3057 se = sockent_create (SOCKENT_TYPE_SERVER);
3060 ERROR ("network plugin: sockent_create failed.");
3064 se->node = strdup (ci->values[0].value.string);
3065 if (ci->values_num >= 2)
3066 se->service = strdup (ci->values[1].value.string);
3068 for (int i = 0; i < ci->children_num; i++)
3070 oconfig_item_t *child = ci->children + i;
3073 if (strcasecmp ("AuthFile", child->key) == 0)
3074 cf_util_get_string (child, &se->data.server.auth_file);
3075 else if (strcasecmp ("SecurityLevel", child->key) == 0)
3076 network_config_set_security_level (child,
3077 &se->data.server.security_level);
3079 #endif /* HAVE_LIBGCRYPT */
3080 if (strcasecmp ("Interface", child->key) == 0)
3081 network_config_set_interface (child, &se->interface);
3084 WARNING ("network plugin: Option `%s' is not allowed here.",
3090 if ((se->data.server.security_level > SECURITY_LEVEL_NONE)
3091 && (se->data.server.auth_file == NULL))
3093 ERROR ("network plugin: A security level higher than `none' was "
3094 "requested, but no AuthFile option was given. Cowardly refusing to "
3095 "open this socket!");
3096 sockent_destroy (se);
3099 #endif /* HAVE_LIBGCRYPT */
3101 status = sockent_init_crypto (se);
3104 ERROR ("network plugin: network_config_add_listen: sockent_init_crypto() failed.");
3105 sockent_destroy (se);
3109 status = sockent_server_listen (se);
3112 ERROR ("network plugin: network_config_add_listen: sockent_server_listen failed.");
3113 sockent_destroy (se);
3117 status = sockent_add (se);
3120 ERROR ("network plugin: network_config_add_listen: sockent_add failed.");
3121 sockent_destroy (se);
3126 } /* }}} int network_config_add_listen */
3128 static int network_config_add_server (const oconfig_item_t *ci) /* {{{ */
3133 if ((ci->values_num < 1) || (ci->values_num > 2)
3134 || (ci->values[0].type != OCONFIG_TYPE_STRING)
3135 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
3137 ERROR ("network plugin: The `%s' config option needs "
3138 "one or two string arguments.", ci->key);
3142 se = sockent_create (SOCKENT_TYPE_CLIENT);
3145 ERROR ("network plugin: sockent_create failed.");
3149 se->node = strdup (ci->values[0].value.string);
3150 if (ci->values_num >= 2)
3151 se->service = strdup (ci->values[1].value.string);
3153 for (int i = 0; i < ci->children_num; i++)
3155 oconfig_item_t *child = ci->children + i;
3158 if (strcasecmp ("Username", child->key) == 0)
3159 cf_util_get_string (child, &se->data.client.username);
3160 else if (strcasecmp ("Password", child->key) == 0)
3161 cf_util_get_string (child, &se->data.client.password);
3162 else if (strcasecmp ("SecurityLevel", child->key) == 0)
3163 network_config_set_security_level (child,
3164 &se->data.client.security_level);
3166 #endif /* HAVE_LIBGCRYPT */
3167 if (strcasecmp ("Interface", child->key) == 0)
3168 network_config_set_interface (child, &se->interface);
3169 else if (strcasecmp ("ResolveInterval", child->key) == 0)
3170 cf_util_get_cdtime(child, &se->data.client.resolve_interval);
3173 WARNING ("network plugin: Option `%s' is not allowed here.",
3179 if ((se->data.client.security_level > SECURITY_LEVEL_NONE)
3180 && ((se->data.client.username == NULL)
3181 || (se->data.client.password == NULL)))
3183 ERROR ("network plugin: A security level higher than `none' was "
3184 "requested, but no Username or Password option was given. "
3185 "Cowardly refusing to open this socket!");
3186 sockent_destroy (se);
3189 #endif /* HAVE_LIBGCRYPT */
3191 status = sockent_init_crypto (se);
3194 ERROR ("network plugin: network_config_add_server: sockent_init_crypto() failed.");
3195 sockent_destroy (se);
3199 /* No call to sockent_client_connect() here -- it is called from
3200 * network_send_buffer_plain(). */
3202 status = sockent_add (se);
3205 ERROR ("network plugin: network_config_add_server: sockent_add failed.");
3206 sockent_destroy (se);
3211 } /* }}} int network_config_add_server */
3213 static int network_config (oconfig_item_t *ci) /* {{{ */
3215 /* The options need to be applied first */
3216 for (int i = 0; i < ci->children_num; i++)
3218 oconfig_item_t *child = ci->children + i;
3219 if (strcasecmp ("TimeToLive", child->key) == 0)
3220 network_config_set_ttl (child);
3223 for (int i = 0; i < ci->children_num; i++)
3225 oconfig_item_t *child = ci->children + i;
3227 if (strcasecmp ("Listen", child->key) == 0)
3228 network_config_add_listen (child);
3229 else if (strcasecmp ("Server", child->key) == 0)
3230 network_config_add_server (child);
3231 else if (strcasecmp ("TimeToLive", child->key) == 0) {
3232 /* Handled earlier */
3234 else if (strcasecmp ("MaxPacketSize", child->key) == 0)
3235 network_config_set_buffer_size (child);
3236 else if (strcasecmp ("Forward", child->key) == 0)
3237 cf_util_get_boolean (child, &network_config_forward);
3238 else if (strcasecmp ("ReportStats", child->key) == 0)
3239 cf_util_get_boolean (child, &network_config_stats);
3242 WARNING ("network plugin: Option `%s' is not allowed here.",
3248 } /* }}} int network_config */
3250 static int network_notification (const notification_t *n,
3251 user_data_t __attribute__((unused)) *user_data)
3253 char buffer[network_config_packet_size];
3254 char *buffer_ptr = buffer;
3255 size_t buffer_free = sizeof (buffer);
3258 if (!check_send_notify_okay (n))
3261 memset (buffer, 0, sizeof (buffer));
3263 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_TIME_HR,
3264 (uint64_t) n->time);
3268 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_SEVERITY,
3269 (uint64_t) n->severity);
3273 if (strlen (n->host) > 0)
3275 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_HOST,
3276 n->host, strlen (n->host));
3281 if (strlen (n->plugin) > 0)
3283 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_PLUGIN,
3284 n->plugin, strlen (n->plugin));
3289 if (strlen (n->plugin_instance) > 0)
3291 status = write_part_string (&buffer_ptr, &buffer_free,
3292 TYPE_PLUGIN_INSTANCE,
3293 n->plugin_instance, strlen (n->plugin_instance));
3298 if (strlen (n->type) > 0)
3300 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE,
3301 n->type, strlen (n->type));
3306 if (strlen (n->type_instance) > 0)
3308 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE_INSTANCE,
3309 n->type_instance, strlen (n->type_instance));
3314 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_MESSAGE,
3315 n->message, strlen (n->message));
3319 network_send_buffer (buffer, sizeof (buffer) - buffer_free);
3322 } /* int network_notification */
3324 static int network_shutdown (void)
3328 /* Kill the listening thread */
3329 if (receive_thread_running != 0)
3331 INFO ("network plugin: Stopping receive thread.");
3332 pthread_kill (receive_thread_id, SIGTERM);
3333 pthread_join (receive_thread_id, NULL /* no return value */);
3334 memset (&receive_thread_id, 0, sizeof (receive_thread_id));
3335 receive_thread_running = 0;
3338 /* Shutdown the dispatching thread */
3339 if (dispatch_thread_running != 0)
3341 INFO ("network plugin: Stopping dispatch thread.");
3342 pthread_mutex_lock (&receive_list_lock);
3343 pthread_cond_broadcast (&receive_list_cond);
3344 pthread_mutex_unlock (&receive_list_lock);
3345 pthread_join (dispatch_thread_id, /* ret = */ NULL);
3346 dispatch_thread_running = 0;
3349 sockent_destroy (listen_sockets);
3351 if (send_buffer_fill > 0)
3354 sfree (send_buffer);
3356 for (sockent_t *se = sending_sockets; se != NULL; se = se->next)
3357 sockent_client_disconnect (se);
3358 sockent_destroy (sending_sockets);
3360 plugin_unregister_config ("network");
3361 plugin_unregister_init ("network");
3362 plugin_unregister_write ("network");
3363 plugin_unregister_shutdown ("network");
3366 } /* int network_shutdown */
3368 static int network_stats_read (void) /* {{{ */
3370 derive_t copy_octets_rx;
3371 derive_t copy_octets_tx;
3372 derive_t copy_packets_rx;
3373 derive_t copy_packets_tx;
3374 derive_t copy_values_dispatched;
3375 derive_t copy_values_not_dispatched;
3376 derive_t copy_values_sent;
3377 derive_t copy_values_not_sent;
3378 derive_t copy_receive_list_length;
3379 value_list_t vl = VALUE_LIST_INIT;
3382 copy_octets_rx = stats_octets_rx;
3383 copy_octets_tx = stats_octets_tx;
3384 copy_packets_rx = stats_packets_rx;
3385 copy_packets_tx = stats_packets_tx;
3386 copy_values_dispatched = stats_values_dispatched;
3387 copy_values_not_dispatched = stats_values_not_dispatched;
3388 copy_values_sent = stats_values_sent;
3389 copy_values_not_sent = stats_values_not_sent;
3390 copy_receive_list_length = receive_list_length;
3392 /* Initialize `vl' */
3396 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
3397 sstrncpy (vl.plugin, "network", sizeof (vl.plugin));
3399 /* Octets received / sent */
3400 vl.values[0].derive = (derive_t) copy_octets_rx;
3401 vl.values[1].derive = (derive_t) copy_octets_tx;
3402 sstrncpy (vl.type, "if_octets", sizeof (vl.type));
3403 plugin_dispatch_values (&vl);
3405 /* Packets received / send */
3406 vl.values[0].derive = (derive_t) copy_packets_rx;
3407 vl.values[1].derive = (derive_t) copy_packets_tx;
3408 sstrncpy (vl.type, "if_packets", sizeof (vl.type));
3409 plugin_dispatch_values (&vl);
3411 /* Values (not) dispatched and (not) send */
3412 sstrncpy (vl.type, "total_values", sizeof (vl.type));
3415 vl.values[0].derive = (derive_t) copy_values_dispatched;
3416 sstrncpy (vl.type_instance, "dispatch-accepted",
3417 sizeof (vl.type_instance));
3418 plugin_dispatch_values (&vl);
3420 vl.values[0].derive = (derive_t) copy_values_not_dispatched;
3421 sstrncpy (vl.type_instance, "dispatch-rejected",
3422 sizeof (vl.type_instance));
3423 plugin_dispatch_values (&vl);
3425 vl.values[0].derive = (derive_t) copy_values_sent;
3426 sstrncpy (vl.type_instance, "send-accepted",
3427 sizeof (vl.type_instance));
3428 plugin_dispatch_values (&vl);
3430 vl.values[0].derive = (derive_t) copy_values_not_sent;
3431 sstrncpy (vl.type_instance, "send-rejected",
3432 sizeof (vl.type_instance));
3433 plugin_dispatch_values (&vl);
3435 /* Receive queue length */
3436 vl.values[0].gauge = (gauge_t) copy_receive_list_length;
3437 sstrncpy (vl.type, "queue_length", sizeof (vl.type));
3438 vl.type_instance[0] = 0;
3439 plugin_dispatch_values (&vl);
3442 } /* }}} int network_stats_read */
3444 static int network_init (void)
3446 static _Bool have_init = 0;
3448 /* Check if we were already initialized. If so, just return - there's
3449 * nothing more to do (for now, that is). */
3455 if (network_init_gcrypt () < 0)
3457 ERROR ("network plugin: Failed to initialize crypto library.");
3462 if (network_config_stats)
3463 plugin_register_read ("network", network_stats_read);
3465 plugin_register_shutdown ("network", network_shutdown);
3467 send_buffer = malloc (network_config_packet_size);
3468 if (send_buffer == NULL)
3470 ERROR ("network plugin: malloc failed.");
3473 network_init_buffer ();
3475 /* setup socket(s) and so on */
3476 if (sending_sockets != NULL)
3478 plugin_register_write ("network", network_write,
3479 /* user_data = */ NULL);
3480 plugin_register_notification ("network", network_notification,
3481 /* user_data = */ NULL);
3484 /* If no threads need to be started, return here. */
3485 if ((listen_sockets_num == 0)
3486 || ((dispatch_thread_running != 0)
3487 && (receive_thread_running != 0)))
3490 if (dispatch_thread_running == 0)
3493 status = plugin_thread_create (&dispatch_thread_id,
3494 NULL /* no attributes */,
3496 NULL /* no argument */);
3500 ERROR ("network: pthread_create failed: %s",
3501 sstrerror (errno, errbuf,
3506 dispatch_thread_running = 1;
3510 if (receive_thread_running == 0)
3513 status = plugin_thread_create (&receive_thread_id,
3514 NULL /* no attributes */,
3516 NULL /* no argument */);
3520 ERROR ("network: pthread_create failed: %s",
3521 sstrerror (errno, errbuf,
3526 receive_thread_running = 1;
3531 } /* int network_init */
3534 * The flush option of the network plugin cannot flush individual identifiers.
3535 * All the values are added to a buffer and sent when the buffer is full, the
3536 * requested value may or may not be in there, it's not worth finding out. We
3537 * just send the buffer if `flush' is called - if the requested value was in
3538 * there, good. If not, well, then there is nothing to flush.. -octo
3540 static int network_flush (cdtime_t timeout,
3541 __attribute__((unused)) const char *identifier,
3542 __attribute__((unused)) user_data_t *user_data)
3544 pthread_mutex_lock (&send_buffer_lock);
3546 if (send_buffer_fill > 0)
3550 cdtime_t now = cdtime ();
3551 if ((send_buffer_last_update + timeout) > now)
3553 pthread_mutex_unlock (&send_buffer_lock);
3559 pthread_mutex_unlock (&send_buffer_lock);
3562 } /* int network_flush */
3564 void module_register (void)
3566 plugin_register_complex_config ("network", network_config);
3567 plugin_register_init ("network", network_init);
3568 plugin_register_flush ("network", network_flush,
3569 /* user_data = */ NULL);
3570 } /* void module_register */
3572 /* vim: set fdm=marker : */