2 * collectd - src/network.c
3 * Copyright (C) 2005-2013 Florian octo Forster
4 * Copyright (C) 2009 Aman Gupta
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; only version 2.1 of the License is
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 * Florian octo Forster <octo at collectd.org>
22 * Aman Gupta <aman at tmm1.net>
25 #define _DEFAULT_SOURCE
26 #define _BSD_SOURCE /* For struct ip_mreq */
31 #include "configfile.h"
32 #include "utils_fbhash.h"
33 #include "utils_avltree.h"
34 #include "utils_cache.h"
35 #include "utils_complain.h"
43 # include <sys/socket.h>
49 # include <netinet/in.h>
52 # include <arpa/inet.h>
63 # if defined __APPLE__
64 /* default xcode compiler throws warnings even when deprecated functionality
65 * is not used. -Werror breaks the build because of erroneous warnings.
66 * http://stackoverflow.com/questions/10556299/compiler-warnings-with-libgcrypt-v1-5-0/12830209#12830209
68 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
70 /* FreeBSD's copy of libgcrypt extends the existing GCRYPT_NO_DEPRECATED
71 * to properly hide all deprecated functionality.
72 * http://svnweb.freebsd.org/ports/head/security/libgcrypt/files/patch-src__gcrypt.h.in
74 # define GCRYPT_NO_DEPRECATED
76 # if defined __APPLE__
77 /* Re enable deprecation warnings */
78 # pragma GCC diagnostic warning "-Wdeprecated-declarations"
80 # if GCRYPT_VERSION_NUMBER < 0x010600
81 GCRY_THREAD_OPTION_PTHREAD_IMPL;
85 #ifndef IPV6_ADD_MEMBERSHIP
86 # ifdef IPV6_JOIN_GROUP
87 # define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
89 # error "Neither IP_ADD_MEMBERSHIP nor IPV6_JOIN_GROUP is defined"
91 #endif /* !IP_ADD_MEMBERSHIP */
94 * Maximum size required for encryption / signing:
96 * 42 bytes for the encryption header
97 * + 64 bytes for the username
101 #define BUFF_SIG_SIZE 106
106 #define SECURITY_LEVEL_NONE 0
108 # define SECURITY_LEVEL_SIGN 1
109 # define SECURITY_LEVEL_ENCRYPT 2
111 struct sockent_client
114 struct sockaddr_storage *addr;
120 gcry_cipher_hd_t cypher;
121 unsigned char password_hash[32];
123 cdtime_t next_resolve_reconnect;
124 cdtime_t resolve_interval;
127 struct sockent_server
135 gcry_cipher_hd_t cypher;
139 typedef struct sockent
141 #define SOCKENT_TYPE_CLIENT 1
142 #define SOCKENT_TYPE_SERVER 2
151 struct sockent_client client;
152 struct sockent_server server;
155 struct sockent *next;
158 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
159 * 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
160 * +-------+-----------------------+-------------------------------+
161 * ! Ver. ! ! Length !
162 * +-------+-----------------------+-------------------------------+
169 typedef struct part_header_s part_header_t;
171 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
172 * 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
173 * +-------------------------------+-------------------------------+
175 * +-------------------------------+-------------------------------+
176 * : (Length - 4) Bytes :
177 * +---------------------------------------------------------------+
184 typedef struct part_string_s part_string_t;
186 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
187 * 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
188 * +-------------------------------+-------------------------------+
190 * +-------------------------------+-------------------------------+
191 * : (Length - 4 == 2 || 4 || 8) Bytes :
192 * +---------------------------------------------------------------+
199 typedef struct part_number_s part_number_t;
201 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
202 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
203 * +-------------------------------+-------------------------------+
205 * +-------------------------------+---------------+---------------+
206 * ! Num of values ! Type0 ! Type1 !
207 * +-------------------------------+---------------+---------------+
210 * +---------------------------------------------------------------+
213 * +---------------------------------------------------------------+
218 uint16_t *num_values;
219 uint8_t *values_types;
222 typedef struct part_values_s part_values_t;
224 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
225 * 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
226 * +-------------------------------+-------------------------------+
228 * +-------------------------------+-------------------------------+
229 * ! Hash (Bits 0 - 31) !
231 * ! Hash (Bits 224 - 255) !
232 * +---------------------------------------------------------------+
235 #define PART_SIGNATURE_SHA256_SIZE 36
236 struct part_signature_sha256_s
239 unsigned char hash[32];
242 typedef struct part_signature_sha256_s part_signature_sha256_t;
244 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
245 * 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
246 * +-------------------------------+-------------------------------+
248 * +-------------------------------+-------------------------------+
249 * ! Original length ! Padding (0 - 15 bytes) !
250 * +-------------------------------+-------------------------------+
251 * ! Hash (Bits 0 - 31) !
253 * ! Hash (Bits 128 - 159) !
254 * +---------------------------------------------------------------+
257 #define PART_ENCRYPTION_AES256_SIZE 42
258 struct part_encryption_aes256_s
261 uint16_t username_length;
263 unsigned char iv[16];
265 unsigned char hash[20];
269 typedef struct part_encryption_aes256_s part_encryption_aes256_t;
271 struct receive_list_entry_s
276 struct receive_list_entry_s *next;
278 typedef struct receive_list_entry_s receive_list_entry_t;
283 static int network_config_ttl = 0;
284 /* Ethernet - (IPv6 + UDP) = 1500 - (40 + 8) = 1452 */
285 static size_t network_config_packet_size = 1452;
286 static int network_config_forward = 0;
287 static int network_config_stats = 0;
289 static sockent_t *sending_sockets = NULL;
291 static receive_list_entry_t *receive_list_head = NULL;
292 static receive_list_entry_t *receive_list_tail = NULL;
293 static pthread_mutex_t receive_list_lock = PTHREAD_MUTEX_INITIALIZER;
294 static pthread_cond_t receive_list_cond = PTHREAD_COND_INITIALIZER;
295 static uint64_t receive_list_length = 0;
297 static sockent_t *listen_sockets = NULL;
298 static struct pollfd *listen_sockets_pollfd = NULL;
299 static size_t listen_sockets_num = 0;
301 /* The receive and dispatch threads will run as long as `listen_loop' is set to
303 static int listen_loop = 0;
304 static int receive_thread_running = 0;
305 static pthread_t receive_thread_id;
306 static int dispatch_thread_running = 0;
307 static pthread_t dispatch_thread_id;
309 /* Buffer in which to-be-sent network packets are constructed. */
310 static char *send_buffer;
311 static char *send_buffer_ptr;
312 static int send_buffer_fill;
313 static value_list_t send_buffer_vl = VALUE_LIST_STATIC;
314 static pthread_mutex_t send_buffer_lock = PTHREAD_MUTEX_INITIALIZER;
316 /* XXX: These counters are incremented from one place only. The spot in which
317 * the values are incremented is either only reachable by one thread (the
318 * dispatch thread, for example) or locked by some lock (send_buffer_lock for
319 * example). Only if neither is true, the stats_lock is acquired. The counters
320 * are always read without holding a lock in the hope that writing 8 bytes to
321 * memory is an atomic operation. */
322 static derive_t stats_octets_rx = 0;
323 static derive_t stats_octets_tx = 0;
324 static derive_t stats_packets_rx = 0;
325 static derive_t stats_packets_tx = 0;
326 static derive_t stats_values_dispatched = 0;
327 static derive_t stats_values_not_dispatched = 0;
328 static derive_t stats_values_sent = 0;
329 static derive_t stats_values_not_sent = 0;
330 static pthread_mutex_t stats_lock = PTHREAD_MUTEX_INITIALIZER;
335 static _Bool check_receive_okay (const value_list_t *vl) /* {{{ */
337 uint64_t time_sent = 0;
340 status = uc_meta_data_get_unsigned_int (vl,
341 "network:time_sent", &time_sent);
343 /* This is a value we already sent. Don't allow it to be received again in
344 * order to avoid looping. */
345 if ((status == 0) && (time_sent >= ((uint64_t) vl->time)))
349 } /* }}} _Bool check_receive_okay */
351 static _Bool check_send_okay (const value_list_t *vl) /* {{{ */
356 if (network_config_forward != 0)
359 if (vl->meta == NULL)
362 status = meta_data_get_boolean (vl->meta, "network:received", &received);
363 if (status == -ENOENT)
365 else if (status != 0)
367 ERROR ("network plugin: check_send_okay: meta_data_get_boolean failed "
368 "with status %i.", status);
372 /* By default, only *send* value lists that were not *received* by the
375 } /* }}} _Bool check_send_okay */
377 static _Bool check_notify_received (const notification_t *n) /* {{{ */
379 notification_meta_t *ptr;
381 for (ptr = n->meta; ptr != NULL; ptr = ptr->next)
382 if ((strcmp ("network:received", ptr->name) == 0)
383 && (ptr->type == NM_TYPE_BOOLEAN))
384 return ((_Bool) ptr->nm_value.nm_boolean);
387 } /* }}} _Bool check_notify_received */
389 static _Bool check_send_notify_okay (const notification_t *n) /* {{{ */
391 static c_complain_t complain_forwarding = C_COMPLAIN_INIT_STATIC;
397 received = check_notify_received (n);
399 if (network_config_forward && received)
401 c_complain_once (LOG_ERR, &complain_forwarding,
402 "network plugin: A notification has been received via the network "
403 "and forwarding is enabled. Forwarding of notifications is currently "
404 "not supported, because there is not loop-deteciton available. "
405 "Please contact the collectd mailing list if you need this "
409 /* By default, only *send* value lists that were not *received* by the
412 } /* }}} _Bool check_send_notify_okay */
414 static int network_dispatch_values (value_list_t *vl, /* {{{ */
415 const char *username)
420 || (strlen (vl->host) <= 0)
421 || (strlen (vl->plugin) <= 0)
422 || (strlen (vl->type) <= 0))
425 if (!check_receive_okay (vl))
428 char name[6*DATA_MAX_NAME_LEN];
429 FORMAT_VL (name, sizeof (name), vl);
430 name[sizeof (name) - 1] = 0;
431 DEBUG ("network plugin: network_dispatch_values: "
432 "NOT dispatching %s.", name);
434 stats_values_not_dispatched++;
438 assert (vl->meta == NULL);
440 vl->meta = meta_data_create ();
441 if (vl->meta == NULL)
443 ERROR ("network plugin: meta_data_create failed.");
447 status = meta_data_add_boolean (vl->meta, "network:received", 1);
450 ERROR ("network plugin: meta_data_add_boolean failed.");
451 meta_data_destroy (vl->meta);
456 if (username != NULL)
458 status = meta_data_add_string (vl->meta, "network:username", username);
461 ERROR ("network plugin: meta_data_add_string failed.");
462 meta_data_destroy (vl->meta);
468 plugin_dispatch_values (vl);
469 stats_values_dispatched++;
471 meta_data_destroy (vl->meta);
475 } /* }}} int network_dispatch_values */
477 static int network_dispatch_notification (notification_t *n) /* {{{ */
481 assert (n->meta == NULL);
483 status = plugin_notification_meta_add_boolean (n, "network:received", 1);
486 ERROR ("network plugin: plugin_notification_meta_add_boolean failed.");
487 plugin_notification_meta_free (n->meta);
492 status = plugin_dispatch_notification (n);
494 plugin_notification_meta_free (n->meta);
498 } /* }}} int network_dispatch_notification */
501 static void network_init_gcrypt (void) /* {{{ */
503 /* http://lists.gnupg.org/pipermail/gcrypt-devel/2003-August/000458.html
504 * Because you can't know in a library whether another library has
505 * already initialized the library */
506 if (gcry_control (GCRYCTL_ANY_INITIALIZATION_P))
509 /* http://www.gnupg.org/documentation/manuals/gcrypt/Multi_002dThreading.html
510 * To ensure thread-safety, it's important to set GCRYCTL_SET_THREAD_CBS
511 * *before* initalizing Libgcrypt with gcry_check_version(), which itself must
512 * be called before any other gcry_* function. GCRYCTL_ANY_INITIALIZATION_P
513 * above doesn't count, as it doesn't implicitly initalize Libgcrypt.
515 * tl;dr: keep all these gry_* statements in this exact order please. */
516 # if GCRYPT_VERSION_NUMBER < 0x010600
517 gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
519 gcry_check_version (NULL);
520 gcry_control (GCRYCTL_INIT_SECMEM, 32768);
521 gcry_control (GCRYCTL_INITIALIZATION_FINISHED);
522 } /* }}} void network_init_gcrypt */
524 static gcry_cipher_hd_t network_get_aes256_cypher (sockent_t *se, /* {{{ */
525 const void *iv, size_t iv_size, const char *username)
528 gcry_cipher_hd_t *cyper_ptr;
529 unsigned char password_hash[32];
531 if (se->type == SOCKENT_TYPE_CLIENT)
533 cyper_ptr = &se->data.client.cypher;
534 memcpy (password_hash, se->data.client.password_hash,
535 sizeof (password_hash));
541 cyper_ptr = &se->data.server.cypher;
543 if (username == NULL)
546 secret = fbh_get (se->data.server.userdb, username);
550 gcry_md_hash_buffer (GCRY_MD_SHA256,
552 secret, strlen (secret));
557 if (*cyper_ptr == NULL)
559 err = gcry_cipher_open (cyper_ptr,
560 GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_OFB, /* flags = */ 0);
563 ERROR ("network plugin: gcry_cipher_open returned: %s",
564 gcry_strerror (err));
571 gcry_cipher_reset (*cyper_ptr);
573 assert (*cyper_ptr != NULL);
575 err = gcry_cipher_setkey (*cyper_ptr,
576 password_hash, sizeof (password_hash));
579 ERROR ("network plugin: gcry_cipher_setkey returned: %s",
580 gcry_strerror (err));
581 gcry_cipher_close (*cyper_ptr);
586 err = gcry_cipher_setiv (*cyper_ptr, iv, iv_size);
589 ERROR ("network plugin: gcry_cipher_setkey returned: %s",
590 gcry_strerror (err));
591 gcry_cipher_close (*cyper_ptr);
597 } /* }}} int network_get_aes256_cypher */
598 #endif /* HAVE_LIBGCRYPT */
600 static int write_part_values (char **ret_buffer, int *ret_buffer_len,
601 const data_set_t *ds, const value_list_t *vl)
607 part_header_t pkg_ph;
608 uint16_t pkg_num_values;
609 uint8_t *pkg_values_types;
615 num_values = vl->values_len;
616 packet_len = sizeof (part_header_t) + sizeof (uint16_t)
617 + (num_values * sizeof (uint8_t))
618 + (num_values * sizeof (value_t));
620 if (*ret_buffer_len < packet_len)
623 pkg_values_types = (uint8_t *) malloc (num_values * sizeof (uint8_t));
624 if (pkg_values_types == NULL)
626 ERROR ("network plugin: write_part_values: malloc failed.");
630 pkg_values = (value_t *) malloc (num_values * sizeof (value_t));
631 if (pkg_values == NULL)
633 free (pkg_values_types);
634 ERROR ("network plugin: write_part_values: malloc failed.");
638 pkg_ph.type = htons (TYPE_VALUES);
639 pkg_ph.length = htons (packet_len);
641 pkg_num_values = htons ((uint16_t) vl->values_len);
643 for (i = 0; i < num_values; i++)
645 pkg_values_types[i] = (uint8_t) ds->ds[i].type;
646 switch (ds->ds[i].type)
648 case DS_TYPE_COUNTER:
649 pkg_values[i].counter = htonll (vl->values[i].counter);
653 pkg_values[i].gauge = htond (vl->values[i].gauge);
657 pkg_values[i].derive = htonll (vl->values[i].derive);
660 case DS_TYPE_ABSOLUTE:
661 pkg_values[i].absolute = htonll (vl->values[i].absolute);
665 free (pkg_values_types);
667 ERROR ("network plugin: write_part_values: "
668 "Unknown data source type: %i",
671 } /* switch (ds->ds[i].type) */
672 } /* for (num_values) */
675 * Use `memcpy' to write everything to the buffer, because the pointer
676 * may be unaligned and some architectures, such as SPARC, can't handle
679 packet_ptr = *ret_buffer;
681 memcpy (packet_ptr + offset, &pkg_ph, sizeof (pkg_ph));
682 offset += sizeof (pkg_ph);
683 memcpy (packet_ptr + offset, &pkg_num_values, sizeof (pkg_num_values));
684 offset += sizeof (pkg_num_values);
685 memcpy (packet_ptr + offset, pkg_values_types, num_values * sizeof (uint8_t));
686 offset += num_values * sizeof (uint8_t);
687 memcpy (packet_ptr + offset, pkg_values, num_values * sizeof (value_t));
688 offset += num_values * sizeof (value_t);
690 assert (offset == packet_len);
692 *ret_buffer = packet_ptr + packet_len;
693 *ret_buffer_len -= packet_len;
695 free (pkg_values_types);
699 } /* int write_part_values */
701 static int write_part_number (char **ret_buffer, int *ret_buffer_len,
702 int type, uint64_t value)
707 part_header_t pkg_head;
712 packet_len = sizeof (pkg_head) + sizeof (pkg_value);
714 if (*ret_buffer_len < packet_len)
717 pkg_head.type = htons (type);
718 pkg_head.length = htons (packet_len);
719 pkg_value = htonll (value);
721 packet_ptr = *ret_buffer;
723 memcpy (packet_ptr + offset, &pkg_head, sizeof (pkg_head));
724 offset += sizeof (pkg_head);
725 memcpy (packet_ptr + offset, &pkg_value, sizeof (pkg_value));
726 offset += sizeof (pkg_value);
728 assert (offset == packet_len);
730 *ret_buffer = packet_ptr + packet_len;
731 *ret_buffer_len -= packet_len;
734 } /* int write_part_number */
736 static int write_part_string (char **ret_buffer, int *ret_buffer_len,
737 int type, const char *str, int str_len)
747 buffer_len = 2 * sizeof (uint16_t) + str_len + 1;
748 if (*ret_buffer_len < buffer_len)
751 pkg_type = htons (type);
752 pkg_length = htons (buffer_len);
754 buffer = *ret_buffer;
756 memcpy (buffer + offset, (void *) &pkg_type, sizeof (pkg_type));
757 offset += sizeof (pkg_type);
758 memcpy (buffer + offset, (void *) &pkg_length, sizeof (pkg_length));
759 offset += sizeof (pkg_length);
760 memcpy (buffer + offset, str, str_len);
762 memset (buffer + offset, '\0', 1);
765 assert (offset == buffer_len);
767 *ret_buffer = buffer + buffer_len;
768 *ret_buffer_len -= buffer_len;
771 } /* int write_part_string */
773 static int parse_part_values (void **ret_buffer, size_t *ret_buffer_len,
774 value_t **ret_values, int *ret_num_values)
776 char *buffer = *ret_buffer;
777 size_t buffer_len = *ret_buffer_len;
792 NOTICE ("network plugin: packet is too short: "
793 "buffer_len = %zu", buffer_len);
797 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
798 buffer += sizeof (tmp16);
799 pkg_type = ntohs (tmp16);
801 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
802 buffer += sizeof (tmp16);
803 pkg_length = ntohs (tmp16);
805 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
806 buffer += sizeof (tmp16);
807 pkg_numval = ntohs (tmp16);
809 assert (pkg_type == TYPE_VALUES);
811 exp_size = 3 * sizeof (uint16_t)
812 + pkg_numval * (sizeof (uint8_t) + sizeof (value_t));
813 if (buffer_len < exp_size)
815 WARNING ("network plugin: parse_part_values: "
817 "Chunk of size %zu expected, "
818 "but buffer has only %zu bytes left.",
819 exp_size, buffer_len);
823 if (pkg_length != exp_size)
825 WARNING ("network plugin: parse_part_values: "
826 "Length and number of values "
827 "in the packet don't match.");
831 pkg_types = (uint8_t *) malloc (pkg_numval * sizeof (uint8_t));
832 pkg_values = (value_t *) malloc (pkg_numval * sizeof (value_t));
833 if ((pkg_types == NULL) || (pkg_values == NULL))
837 ERROR ("network plugin: parse_part_values: malloc failed.");
841 memcpy ((void *) pkg_types, (void *) buffer, pkg_numval * sizeof (uint8_t));
842 buffer += pkg_numval * sizeof (uint8_t);
843 memcpy ((void *) pkg_values, (void *) buffer, pkg_numval * sizeof (value_t));
844 buffer += pkg_numval * sizeof (value_t);
846 for (i = 0; i < pkg_numval; i++)
848 switch (pkg_types[i])
850 case DS_TYPE_COUNTER:
851 pkg_values[i].counter = (counter_t) ntohll (pkg_values[i].counter);
855 pkg_values[i].gauge = (gauge_t) ntohd (pkg_values[i].gauge);
859 pkg_values[i].derive = (derive_t) ntohll (pkg_values[i].derive);
862 case DS_TYPE_ABSOLUTE:
863 pkg_values[i].absolute = (absolute_t) ntohll (pkg_values[i].absolute);
867 NOTICE ("network plugin: parse_part_values: "
868 "Don't know how to handle data source type %"PRIu8,
873 } /* switch (pkg_types[i]) */
876 *ret_buffer = buffer;
877 *ret_buffer_len = buffer_len - pkg_length;
878 *ret_num_values = pkg_numval;
879 *ret_values = pkg_values;
884 } /* int parse_part_values */
886 static int parse_part_number (void **ret_buffer, size_t *ret_buffer_len,
889 char *buffer = *ret_buffer;
890 size_t buffer_len = *ret_buffer_len;
894 size_t exp_size = 2 * sizeof (uint16_t) + sizeof (uint64_t);
898 if (buffer_len < exp_size)
900 WARNING ("network plugin: parse_part_number: "
902 "Chunk of size %zu expected, "
903 "but buffer has only %zu bytes left.",
904 exp_size, buffer_len);
908 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
909 buffer += sizeof (tmp16);
910 /* pkg_type = ntohs (tmp16); */
912 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
913 buffer += sizeof (tmp16);
914 pkg_length = ntohs (tmp16);
916 memcpy ((void *) &tmp64, buffer, sizeof (tmp64));
917 buffer += sizeof (tmp64);
918 *value = ntohll (tmp64);
920 *ret_buffer = buffer;
921 *ret_buffer_len = buffer_len - pkg_length;
924 } /* int parse_part_number */
926 static int parse_part_string (void **ret_buffer, size_t *ret_buffer_len,
927 char *output, size_t const output_len)
929 char *buffer = *ret_buffer;
930 size_t buffer_len = *ret_buffer_len;
933 size_t const header_size = 2 * sizeof (uint16_t);
941 if (buffer_len < header_size)
943 WARNING ("network plugin: parse_part_string: "
945 "Chunk of at least size %zu expected, "
946 "but buffer has only %zu bytes left.",
947 header_size, buffer_len);
951 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
952 buffer += sizeof (tmp16);
953 /* pkg_type = ntohs (tmp16); */
955 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
956 buffer += sizeof (tmp16);
957 pkg_length = ntohs (tmp16);
958 payload_size = ((size_t) pkg_length) - header_size;
960 /* Check that packet fits in the input buffer */
961 if (pkg_length > buffer_len)
963 WARNING ("network plugin: parse_part_string: "
965 "Chunk of size %"PRIu16" received, "
966 "but buffer has only %zu bytes left.",
967 pkg_length, buffer_len);
971 /* Check that pkg_length is in the valid range */
972 if (pkg_length <= header_size)
974 WARNING ("network plugin: parse_part_string: "
976 "Header claims this packet is only %hu "
977 "bytes long.", pkg_length);
981 /* Check that the package data fits into the output buffer.
982 * The previous if-statement ensures that:
983 * `pkg_length > header_size' */
984 if (output_len < payload_size)
986 WARNING ("network plugin: parse_part_string: "
988 "Output buffer holds %zu bytes, "
989 "which is too small to hold the received "
991 output_len, payload_size);
995 /* All sanity checks successfull, let's copy the data over */
996 memcpy ((void *) output, (void *) buffer, payload_size);
997 buffer += payload_size;
999 /* For some very weird reason '\0' doesn't do the trick on SPARC in
1000 * this statement. */
1001 if (output[payload_size - 1] != 0)
1003 WARNING ("network plugin: parse_part_string: "
1004 "Received string does not end "
1005 "with a NULL-byte.");
1009 *ret_buffer = buffer;
1010 *ret_buffer_len = buffer_len - pkg_length;
1013 } /* int parse_part_string */
1015 /* Forward declaration: parse_part_sign_sha256 and parse_part_encr_aes256 call
1016 * parse_packet and vice versa. */
1017 #define PP_SIGNED 0x01
1018 #define PP_ENCRYPTED 0x02
1019 static int parse_packet (sockent_t *se,
1020 void *buffer, size_t buffer_size, int flags,
1021 const char *username);
1023 #define BUFFER_READ(p,s) do { \
1024 memcpy ((p), buffer + buffer_offset, (s)); \
1025 buffer_offset += (s); \
1029 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
1030 void **ret_buffer, size_t *ret_buffer_len, int flags)
1032 static c_complain_t complain_no_users = C_COMPLAIN_INIT_STATIC;
1036 size_t buffer_offset;
1038 size_t username_len;
1041 part_signature_sha256_t pss;
1042 uint16_t pss_head_length;
1043 char hash[sizeof (pss.hash)];
1047 unsigned char *hash_ptr;
1049 buffer = *ret_buffer;
1050 buffer_len = *ret_buffer_len;
1053 if (se->data.server.userdb == NULL)
1055 c_complain (LOG_NOTICE, &complain_no_users,
1056 "network plugin: Received signed network packet but can't verify it "
1057 "because no user DB has been configured. Will accept it.");
1061 /* Check if the buffer has enough data for this structure. */
1062 if (buffer_len <= PART_SIGNATURE_SHA256_SIZE)
1065 /* Read type and length header */
1066 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
1067 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
1068 pss_head_length = ntohs (pss.head.length);
1070 /* Check if the `pss_head_length' is within bounds. */
1071 if ((pss_head_length <= PART_SIGNATURE_SHA256_SIZE)
1072 || (pss_head_length > buffer_len))
1074 ERROR ("network plugin: HMAC-SHA-256 with invalid length received.");
1078 /* Copy the hash. */
1079 BUFFER_READ (pss.hash, sizeof (pss.hash));
1081 /* Calculate username length (without null byte) and allocate memory */
1082 username_len = pss_head_length - PART_SIGNATURE_SHA256_SIZE;
1083 pss.username = malloc (username_len + 1);
1084 if (pss.username == NULL)
1087 /* Read the username */
1088 BUFFER_READ (pss.username, username_len);
1089 pss.username[username_len] = 0;
1091 assert (buffer_offset == pss_head_length);
1093 /* Query the password */
1094 secret = fbh_get (se->data.server.userdb, pss.username);
1097 ERROR ("network plugin: Unknown user: %s", pss.username);
1098 sfree (pss.username);
1102 /* Create a hash device and check the HMAC */
1104 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
1107 ERROR ("network plugin: Creating HMAC-SHA-256 object failed: %s",
1108 gcry_strerror (err));
1110 sfree (pss.username);
1114 err = gcry_md_setkey (hd, secret, strlen (secret));
1117 ERROR ("network plugin: gcry_md_setkey failed: %s", gcry_strerror (err));
1120 sfree (pss.username);
1125 buffer + PART_SIGNATURE_SHA256_SIZE,
1126 buffer_len - PART_SIGNATURE_SHA256_SIZE);
1127 hash_ptr = gcry_md_read (hd, GCRY_MD_SHA256);
1128 if (hash_ptr == NULL)
1130 ERROR ("network plugin: gcry_md_read failed.");
1133 sfree (pss.username);
1136 memcpy (hash, hash_ptr, sizeof (hash));
1142 if (memcmp (pss.hash, hash, sizeof (pss.hash)) != 0)
1144 WARNING ("network plugin: Verifying HMAC-SHA-256 signature failed: "
1149 parse_packet (se, buffer + buffer_offset, buffer_len - buffer_offset,
1150 flags | PP_SIGNED, pss.username);
1154 sfree (pss.username);
1156 *ret_buffer = buffer + buffer_len;
1157 *ret_buffer_len = 0;
1160 } /* }}} int parse_part_sign_sha256 */
1161 /* #endif HAVE_LIBGCRYPT */
1163 #else /* if !HAVE_LIBGCRYPT */
1164 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
1165 void **ret_buffer, size_t *ret_buffer_size, int flags)
1167 static int warning_has_been_printed = 0;
1171 size_t buffer_offset;
1174 part_signature_sha256_t pss;
1176 buffer = *ret_buffer;
1177 buffer_size = *ret_buffer_size;
1180 if (buffer_size <= PART_SIGNATURE_SHA256_SIZE)
1183 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
1184 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
1185 part_len = ntohs (pss.head.length);
1187 if ((part_len <= PART_SIGNATURE_SHA256_SIZE)
1188 || (part_len > buffer_size))
1191 if (warning_has_been_printed == 0)
1193 WARNING ("network plugin: Received signed packet, but the network "
1194 "plugin was not linked with libgcrypt, so I cannot "
1195 "verify the signature. The packet will be accepted.");
1196 warning_has_been_printed = 1;
1199 parse_packet (se, buffer + part_len, buffer_size - part_len, flags,
1200 /* username = */ NULL);
1202 *ret_buffer = buffer + buffer_size;
1203 *ret_buffer_size = 0;
1206 } /* }}} int parse_part_sign_sha256 */
1207 #endif /* !HAVE_LIBGCRYPT */
1210 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1211 void **ret_buffer, size_t *ret_buffer_len,
1214 char *buffer = *ret_buffer;
1215 size_t buffer_len = *ret_buffer_len;
1218 size_t buffer_offset;
1219 uint16_t username_len;
1220 part_encryption_aes256_t pea;
1221 unsigned char hash[sizeof (pea.hash)];
1223 gcry_cipher_hd_t cypher;
1226 /* Make sure at least the header if available. */
1227 if (buffer_len <= PART_ENCRYPTION_AES256_SIZE)
1229 NOTICE ("network plugin: parse_part_encr_aes256: "
1230 "Discarding short packet.");
1236 /* Copy the unencrypted information into `pea'. */
1237 BUFFER_READ (&pea.head.type, sizeof (pea.head.type));
1238 BUFFER_READ (&pea.head.length, sizeof (pea.head.length));
1240 /* Check the `part size'. */
1241 part_size = ntohs (pea.head.length);
1242 if ((part_size <= PART_ENCRYPTION_AES256_SIZE)
1243 || (part_size > buffer_len))
1245 NOTICE ("network plugin: parse_part_encr_aes256: "
1246 "Discarding part with invalid size.");
1250 /* Read the username */
1251 BUFFER_READ (&username_len, sizeof (username_len));
1252 username_len = ntohs (username_len);
1254 if ((username_len <= 0)
1255 || (username_len > (part_size - (PART_ENCRYPTION_AES256_SIZE + 1))))
1257 NOTICE ("network plugin: parse_part_encr_aes256: "
1258 "Discarding part with invalid username length.");
1262 assert (username_len > 0);
1263 pea.username = malloc (username_len + 1);
1264 if (pea.username == NULL)
1266 BUFFER_READ (pea.username, username_len);
1267 pea.username[username_len] = 0;
1269 /* Last but not least, the initialization vector */
1270 BUFFER_READ (pea.iv, sizeof (pea.iv));
1272 /* Make sure we are at the right position */
1273 assert (buffer_offset == (username_len +
1274 PART_ENCRYPTION_AES256_SIZE - sizeof (pea.hash)));
1276 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
1280 sfree (pea.username);
1284 payload_len = part_size - (PART_ENCRYPTION_AES256_SIZE + username_len);
1285 assert (payload_len > 0);
1287 /* Decrypt the packet in-place */
1288 err = gcry_cipher_decrypt (cypher,
1289 buffer + buffer_offset,
1290 part_size - buffer_offset,
1291 /* in = */ NULL, /* in len = */ 0);
1294 sfree (pea.username);
1295 ERROR ("network plugin: gcry_cipher_decrypt returned: %s",
1296 gcry_strerror (err));
1301 BUFFER_READ (pea.hash, sizeof (pea.hash));
1303 /* Make sure we're at the right position - again */
1304 assert (buffer_offset == (username_len + PART_ENCRYPTION_AES256_SIZE));
1305 assert (buffer_offset == (part_size - payload_len));
1307 /* Check hash sum */
1308 memset (hash, 0, sizeof (hash));
1309 gcry_md_hash_buffer (GCRY_MD_SHA1, hash,
1310 buffer + buffer_offset, payload_len);
1311 if (memcmp (hash, pea.hash, sizeof (hash)) != 0)
1313 sfree (pea.username);
1314 ERROR ("network plugin: Decryption failed: Checksum mismatch.");
1318 parse_packet (se, buffer + buffer_offset, payload_len,
1319 flags | PP_ENCRYPTED, pea.username);
1321 /* XXX: Free pea.username?!? */
1323 /* Update return values */
1324 *ret_buffer = buffer + part_size;
1325 *ret_buffer_len = buffer_len - part_size;
1327 sfree (pea.username);
1330 } /* }}} int parse_part_encr_aes256 */
1331 /* #endif HAVE_LIBGCRYPT */
1333 #else /* if !HAVE_LIBGCRYPT */
1334 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1335 void **ret_buffer, size_t *ret_buffer_size, int flags)
1337 static int warning_has_been_printed = 0;
1341 size_t buffer_offset;
1346 buffer = *ret_buffer;
1347 buffer_size = *ret_buffer_size;
1350 /* parse_packet assures this minimum size. */
1351 assert (buffer_size >= (sizeof (ph.type) + sizeof (ph.length)));
1353 BUFFER_READ (&ph.type, sizeof (ph.type));
1354 BUFFER_READ (&ph.length, sizeof (ph.length));
1355 ph_length = ntohs (ph.length);
1357 if ((ph_length <= PART_ENCRYPTION_AES256_SIZE)
1358 || (ph_length > buffer_size))
1360 ERROR ("network plugin: AES-256 encrypted part "
1361 "with invalid length received.");
1365 if (warning_has_been_printed == 0)
1367 WARNING ("network plugin: Received encrypted packet, but the network "
1368 "plugin was not linked with libgcrypt, so I cannot "
1369 "decrypt it. The part will be discarded.");
1370 warning_has_been_printed = 1;
1373 *ret_buffer += ph_length;
1374 *ret_buffer_size -= ph_length;
1377 } /* }}} int parse_part_encr_aes256 */
1378 #endif /* !HAVE_LIBGCRYPT */
1382 static int parse_packet (sockent_t *se, /* {{{ */
1383 void *buffer, size_t buffer_size, int flags,
1384 const char *username)
1388 value_list_t vl = VALUE_LIST_INIT;
1392 int packet_was_signed = (flags & PP_SIGNED);
1393 int packet_was_encrypted = (flags & PP_ENCRYPTED);
1394 int printed_ignore_warning = 0;
1395 #endif /* HAVE_LIBGCRYPT */
1398 memset (&vl, '\0', sizeof (vl));
1399 memset (&n, '\0', sizeof (n));
1402 while ((status == 0) && (0 < buffer_size)
1403 && ((unsigned int) buffer_size > sizeof (part_header_t)))
1405 uint16_t pkg_length;
1408 memcpy ((void *) &pkg_type,
1411 memcpy ((void *) &pkg_length,
1412 (void *) (buffer + sizeof (pkg_type)),
1413 sizeof (pkg_length));
1415 pkg_length = ntohs (pkg_length);
1416 pkg_type = ntohs (pkg_type);
1418 if (pkg_length > buffer_size)
1420 /* Ensure that this loop terminates eventually */
1421 if (pkg_length < (2 * sizeof (uint16_t)))
1424 if (pkg_type == TYPE_ENCR_AES256)
1426 status = parse_part_encr_aes256 (se,
1427 &buffer, &buffer_size, flags);
1430 ERROR ("network plugin: Decrypting AES256 "
1432 "with status %i.", status);
1437 else if ((se->data.server.security_level == SECURITY_LEVEL_ENCRYPT)
1438 && (packet_was_encrypted == 0))
1440 if (printed_ignore_warning == 0)
1442 INFO ("network plugin: Unencrypted packet or "
1443 "part has been ignored.");
1444 printed_ignore_warning = 1;
1446 buffer = ((char *) buffer) + pkg_length;
1449 #endif /* HAVE_LIBGCRYPT */
1450 else if (pkg_type == TYPE_SIGN_SHA256)
1452 status = parse_part_sign_sha256 (se,
1453 &buffer, &buffer_size, flags);
1456 ERROR ("network plugin: Verifying HMAC-SHA-256 "
1458 "with status %i.", status);
1463 else if ((se->data.server.security_level == SECURITY_LEVEL_SIGN)
1464 && (packet_was_encrypted == 0)
1465 && (packet_was_signed == 0))
1467 if (printed_ignore_warning == 0)
1469 INFO ("network plugin: Unsigned packet or "
1470 "part has been ignored.");
1471 printed_ignore_warning = 1;
1473 buffer = ((char *) buffer) + pkg_length;
1476 #endif /* HAVE_LIBGCRYPT */
1477 else if (pkg_type == TYPE_VALUES)
1479 status = parse_part_values (&buffer, &buffer_size,
1480 &vl.values, &vl.values_len);
1484 network_dispatch_values (&vl, username);
1488 else if (pkg_type == TYPE_TIME)
1491 status = parse_part_number (&buffer, &buffer_size,
1495 vl.time = TIME_T_TO_CDTIME_T (tmp);
1496 n.time = TIME_T_TO_CDTIME_T (tmp);
1499 else if (pkg_type == TYPE_TIME_HR)
1502 status = parse_part_number (&buffer, &buffer_size,
1506 vl.time = (cdtime_t) tmp;
1507 n.time = (cdtime_t) tmp;
1510 else if (pkg_type == TYPE_INTERVAL)
1513 status = parse_part_number (&buffer, &buffer_size,
1516 vl.interval = TIME_T_TO_CDTIME_T (tmp);
1518 else if (pkg_type == TYPE_INTERVAL_HR)
1521 status = parse_part_number (&buffer, &buffer_size,
1524 vl.interval = (cdtime_t) tmp;
1526 else if (pkg_type == TYPE_HOST)
1528 status = parse_part_string (&buffer, &buffer_size,
1529 vl.host, sizeof (vl.host));
1531 sstrncpy (n.host, vl.host, sizeof (n.host));
1533 else if (pkg_type == TYPE_PLUGIN)
1535 status = parse_part_string (&buffer, &buffer_size,
1536 vl.plugin, sizeof (vl.plugin));
1538 sstrncpy (n.plugin, vl.plugin,
1541 else if (pkg_type == TYPE_PLUGIN_INSTANCE)
1543 status = parse_part_string (&buffer, &buffer_size,
1545 sizeof (vl.plugin_instance));
1547 sstrncpy (n.plugin_instance,
1549 sizeof (n.plugin_instance));
1551 else if (pkg_type == TYPE_TYPE)
1553 status = parse_part_string (&buffer, &buffer_size,
1554 vl.type, sizeof (vl.type));
1556 sstrncpy (n.type, vl.type, sizeof (n.type));
1558 else if (pkg_type == TYPE_TYPE_INSTANCE)
1560 status = parse_part_string (&buffer, &buffer_size,
1562 sizeof (vl.type_instance));
1564 sstrncpy (n.type_instance, vl.type_instance,
1565 sizeof (n.type_instance));
1567 else if (pkg_type == TYPE_MESSAGE)
1569 status = parse_part_string (&buffer, &buffer_size,
1570 n.message, sizeof (n.message));
1576 else if ((n.severity != NOTIF_FAILURE)
1577 && (n.severity != NOTIF_WARNING)
1578 && (n.severity != NOTIF_OKAY))
1580 INFO ("network plugin: "
1581 "Ignoring notification with "
1582 "unknown severity %i.",
1585 else if (n.time <= 0)
1587 INFO ("network plugin: "
1588 "Ignoring notification with "
1591 else if (strlen (n.message) <= 0)
1593 INFO ("network plugin: "
1594 "Ignoring notification with "
1595 "an empty message.");
1599 network_dispatch_notification (&n);
1602 else if (pkg_type == TYPE_SEVERITY)
1605 status = parse_part_number (&buffer, &buffer_size,
1608 n.severity = (int) tmp;
1612 DEBUG ("network plugin: parse_packet: Unknown part"
1613 " type: 0x%04hx", pkg_type);
1614 buffer = ((char *) buffer) + pkg_length;
1616 } /* while (buffer_size > sizeof (part_header_t)) */
1618 if (status == 0 && buffer_size > 0)
1619 WARNING ("network plugin: parse_packet: Received truncated "
1620 "packet, try increasing `MaxPacketSize'");
1623 } /* }}} int parse_packet */
1625 static void free_sockent_client (struct sockent_client *sec) /* {{{ */
1634 sfree (sec->username);
1635 sfree (sec->password);
1636 if (sec->cypher != NULL)
1637 gcry_cipher_close (sec->cypher);
1639 } /* }}} void free_sockent_client */
1641 static void free_sockent_server (struct sockent_server *ses) /* {{{ */
1645 for (i = 0; i < ses->fd_num; i++)
1647 if (ses->fd[i] >= 0)
1656 sfree (ses->auth_file);
1657 fbh_destroy (ses->userdb);
1658 if (ses->cypher != NULL)
1659 gcry_cipher_close (ses->cypher);
1661 } /* }}} void free_sockent_server */
1663 static void sockent_destroy (sockent_t *se) /* {{{ */
1667 DEBUG ("network plugin: sockent_destroy (se = %p);", (void *) se);
1674 sfree (se->service);
1676 if (se->type == SOCKENT_TYPE_CLIENT)
1677 free_sockent_client (&se->data.client);
1679 free_sockent_server (&se->data.server);
1684 } /* }}} void sockent_destroy */
1687 * int network_set_ttl
1689 * Set the `IP_MULTICAST_TTL', `IP_TTL', `IPV6_MULTICAST_HOPS' or
1690 * `IPV6_UNICAST_HOPS', depending on which option is applicable.
1692 * The `struct addrinfo' is used to destinguish between unicast and multicast
1695 static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
1697 DEBUG ("network plugin: network_set_ttl: network_config_ttl = %i;",
1698 network_config_ttl);
1700 assert (se->type == SOCKENT_TYPE_CLIENT);
1702 if ((network_config_ttl < 1) || (network_config_ttl > 255))
1705 if (ai->ai_family == AF_INET)
1707 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1710 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1711 optname = IP_MULTICAST_TTL;
1715 if (setsockopt (se->data.client.fd, IPPROTO_IP, optname,
1716 &network_config_ttl,
1717 sizeof (network_config_ttl)) != 0)
1720 ERROR ("network plugin: setsockopt (ipv4-ttl): %s",
1721 sstrerror (errno, errbuf, sizeof (errbuf)));
1725 else if (ai->ai_family == AF_INET6)
1727 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1728 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1731 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1732 optname = IPV6_MULTICAST_HOPS;
1734 optname = IPV6_UNICAST_HOPS;
1736 if (setsockopt (se->data.client.fd, IPPROTO_IPV6, optname,
1737 &network_config_ttl,
1738 sizeof (network_config_ttl)) != 0)
1741 ERROR ("network plugin: setsockopt(ipv6-ttl): %s",
1742 sstrerror (errno, errbuf,
1749 } /* int network_set_ttl */
1751 static int network_set_interface (const sockent_t *se, const struct addrinfo *ai) /* {{{ */
1753 DEBUG ("network plugin: network_set_interface: interface index = %i;",
1756 assert (se->type == SOCKENT_TYPE_CLIENT);
1758 if (ai->ai_family == AF_INET)
1760 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1762 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1764 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1765 /* If possible, use the "ip_mreqn" structure which has
1766 * an "interface index" member. Using the interface
1767 * index is preferred here, because of its similarity
1768 * to the way IPv6 handles this. Unfortunately, it
1769 * appears not to be portable. */
1770 struct ip_mreqn mreq;
1772 memset (&mreq, 0, sizeof (mreq));
1773 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1774 mreq.imr_address.s_addr = ntohl (INADDR_ANY);
1775 mreq.imr_ifindex = se->interface;
1777 struct ip_mreq mreq;
1779 memset (&mreq, 0, sizeof (mreq));
1780 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1781 mreq.imr_interface.s_addr = ntohl (INADDR_ANY);
1784 if (setsockopt (se->data.client.fd, IPPROTO_IP, IP_MULTICAST_IF,
1785 &mreq, sizeof (mreq)) != 0)
1788 ERROR ("network plugin: setsockopt (ipv4-multicast-if): %s",
1789 sstrerror (errno, errbuf, sizeof (errbuf)));
1796 else if (ai->ai_family == AF_INET6)
1798 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1800 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1802 if (setsockopt (se->data.client.fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
1804 sizeof (se->interface)) != 0)
1807 ERROR ("network plugin: setsockopt (ipv6-multicast-if): %s",
1808 sstrerror (errno, errbuf,
1817 /* else: Not a multicast interface. */
1818 if (se->interface != 0)
1820 #if defined(HAVE_IF_INDEXTONAME) && HAVE_IF_INDEXTONAME && defined(SO_BINDTODEVICE)
1821 char interface_name[IFNAMSIZ];
1823 if (if_indextoname (se->interface, interface_name) == NULL)
1826 DEBUG ("network plugin: Binding socket to interface %s", interface_name);
1828 if (setsockopt (se->data.client.fd, SOL_SOCKET, SO_BINDTODEVICE,
1830 sizeof(interface_name)) == -1 )
1833 ERROR ("network plugin: setsockopt (bind-if): %s",
1834 sstrerror (errno, errbuf, sizeof (errbuf)));
1837 /* #endif HAVE_IF_INDEXTONAME && SO_BINDTODEVICE */
1840 WARNING ("network plugin: Cannot set the interface on a unicast "
1842 # if !defined(SO_BINDTODEVICE)
1843 "the \"SO_BINDTODEVICE\" socket option "
1845 "the \"if_indextoname\" function "
1847 "is not available on your system.");
1853 } /* }}} network_set_interface */
1855 static int network_bind_socket (int fd, const struct addrinfo *ai, const int interface_idx)
1864 /* allow multiple sockets to use the same PORT number */
1865 if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR,
1866 &yes, sizeof(yes)) == -1) {
1868 ERROR ("network plugin: setsockopt (reuseaddr): %s",
1869 sstrerror (errno, errbuf, sizeof (errbuf)));
1873 DEBUG ("fd = %i; calling `bind'", fd);
1875 if (bind (fd, ai->ai_addr, ai->ai_addrlen) == -1)
1879 sstrerror (errno, errbuf, sizeof (errbuf)));
1883 if (ai->ai_family == AF_INET)
1885 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1886 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1888 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1889 struct ip_mreqn mreq;
1891 struct ip_mreq mreq;
1894 DEBUG ("fd = %i; IPv4 multicast address found", fd);
1896 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1897 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1898 /* Set the interface using the interface index if
1899 * possible (available). Unfortunately, the struct
1900 * ip_mreqn is not portable. */
1901 mreq.imr_address.s_addr = ntohl (INADDR_ANY);
1902 mreq.imr_ifindex = interface_idx;
1904 mreq.imr_interface.s_addr = ntohl (INADDR_ANY);
1907 if (setsockopt (fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1908 &loop, sizeof (loop)) == -1)
1911 ERROR ("network plugin: setsockopt (multicast-loop): %s",
1912 sstrerror (errno, errbuf,
1917 if (setsockopt (fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1918 &mreq, sizeof (mreq)) == -1)
1921 ERROR ("network plugin: setsockopt (add-membership): %s",
1922 sstrerror (errno, errbuf,
1930 else if (ai->ai_family == AF_INET6)
1932 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1933 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1934 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1936 struct ipv6_mreq mreq;
1938 DEBUG ("fd = %i; IPv6 multicast address found", fd);
1940 memcpy (&mreq.ipv6mr_multiaddr,
1942 sizeof (addr->sin6_addr));
1944 /* http://developer.apple.com/documentation/Darwin/Reference/ManPages/man4/ip6.4.html
1945 * ipv6mr_interface may be set to zeroes to
1946 * choose the default multicast interface or to
1947 * the index of a particular multicast-capable
1948 * interface if the host is multihomed.
1949 * Membership is associ-associated with a
1950 * single interface; programs running on
1951 * multihomed hosts may need to join the same
1952 * group on more than one interface.*/
1953 mreq.ipv6mr_interface = interface_idx;
1955 if (setsockopt (fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
1956 &loop, sizeof (loop)) == -1)
1959 ERROR ("network plugin: setsockopt (ipv6-multicast-loop): %s",
1960 sstrerror (errno, errbuf,
1965 if (setsockopt (fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
1966 &mreq, sizeof (mreq)) == -1)
1969 ERROR ("network plugin: setsockopt (ipv6-add-membership): %s",
1970 sstrerror (errno, errbuf,
1979 #if defined(HAVE_IF_INDEXTONAME) && HAVE_IF_INDEXTONAME && defined(SO_BINDTODEVICE)
1980 /* if a specific interface was set, bind the socket to it. But to avoid
1981 * possible problems with multicast routing, only do that for non-multicast
1983 if (interface_idx != 0)
1985 char interface_name[IFNAMSIZ];
1987 if (if_indextoname (interface_idx, interface_name) == NULL)
1990 DEBUG ("fd = %i; Binding socket to interface %s", fd, interface_name);
1992 if (setsockopt (fd, SOL_SOCKET, SO_BINDTODEVICE,
1994 sizeof(interface_name)) == -1 )
1997 ERROR ("network plugin: setsockopt (bind-if): %s",
1998 sstrerror (errno, errbuf, sizeof (errbuf)));
2002 #endif /* HAVE_IF_INDEXTONAME && SO_BINDTODEVICE */
2005 } /* int network_bind_socket */
2007 /* Initialize a sockent structure. `type' must be either `SOCKENT_TYPE_CLIENT'
2008 * or `SOCKENT_TYPE_SERVER' */
2009 static sockent_t *sockent_create (int type) /* {{{ */
2013 if ((type != SOCKENT_TYPE_CLIENT) && (type != SOCKENT_TYPE_SERVER))
2016 se = malloc (sizeof (*se));
2019 memset (se, 0, sizeof (*se));
2027 if (type == SOCKENT_TYPE_SERVER)
2029 se->data.server.fd = NULL;
2030 se->data.server.fd_num = 0;
2032 se->data.server.security_level = SECURITY_LEVEL_NONE;
2033 se->data.server.auth_file = NULL;
2034 se->data.server.userdb = NULL;
2035 se->data.server.cypher = NULL;
2040 se->data.client.fd = -1;
2041 se->data.client.addr = NULL;
2042 se->data.client.resolve_interval = 0;
2043 se->data.client.next_resolve_reconnect = 0;
2045 se->data.client.security_level = SECURITY_LEVEL_NONE;
2046 se->data.client.username = NULL;
2047 se->data.client.password = NULL;
2048 se->data.client.cypher = NULL;
2053 } /* }}} sockent_t *sockent_create */
2055 static int sockent_init_crypto (sockent_t *se) /* {{{ */
2057 #if HAVE_LIBGCRYPT /* {{{ */
2058 if (se->type == SOCKENT_TYPE_CLIENT)
2060 if (se->data.client.security_level > SECURITY_LEVEL_NONE)
2062 network_init_gcrypt ();
2064 if ((se->data.client.username == NULL)
2065 || (se->data.client.password == NULL))
2067 ERROR ("network plugin: Client socket with "
2068 "security requested, but no "
2069 "credentials are configured.");
2072 gcry_md_hash_buffer (GCRY_MD_SHA256,
2073 se->data.client.password_hash,
2074 se->data.client.password,
2075 strlen (se->data.client.password));
2078 else /* (se->type == SOCKENT_TYPE_SERVER) */
2080 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
2082 network_init_gcrypt ();
2084 if (se->data.server.auth_file == NULL)
2086 ERROR ("network plugin: Server socket with "
2087 "security requested, but no "
2088 "password file is configured.");
2092 if (se->data.server.auth_file != NULL)
2094 se->data.server.userdb = fbh_create (se->data.server.auth_file);
2095 if (se->data.server.userdb == NULL)
2097 ERROR ("network plugin: Reading password file "
2099 se->data.server.auth_file);
2100 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
2105 #endif /* }}} HAVE_LIBGCRYPT */
2108 } /* }}} int sockent_init_crypto */
2110 static int sockent_client_disconnect (sockent_t *se) /* {{{ */
2112 struct sockent_client *client;
2114 if ((se == NULL) || (se->type != SOCKENT_TYPE_CLIENT))
2117 client = &se->data.client;
2118 if (client->fd >= 0) /* connected */
2124 sfree (client->addr);
2125 client->addrlen = 0;
2128 } /* }}} int sockent_client_disconnect */
2130 static int sockent_client_connect (sockent_t *se) /* {{{ */
2132 static c_complain_t complaint = C_COMPLAIN_INIT_STATIC;
2134 struct sockent_client *client;
2135 struct addrinfo ai_hints;
2136 struct addrinfo *ai_list = NULL, *ai_ptr;
2138 _Bool reconnect = 0;
2141 if ((se == NULL) || (se->type != SOCKENT_TYPE_CLIENT))
2144 client = &se->data.client;
2147 if (client->resolve_interval != 0 && client->next_resolve_reconnect < now) {
2148 DEBUG("network plugin: Reconnecting socket, resolve_interval = %lf, next_resolve_reconnect = %lf",
2149 CDTIME_T_TO_DOUBLE(client->resolve_interval), CDTIME_T_TO_DOUBLE(client->next_resolve_reconnect));
2153 if (client->fd >= 0 && !reconnect) /* already connected and not stale*/
2156 memset (&ai_hints, 0, sizeof (ai_hints));
2157 #ifdef AI_ADDRCONFIG
2158 ai_hints.ai_flags |= AI_ADDRCONFIG;
2160 ai_hints.ai_family = AF_UNSPEC;
2161 ai_hints.ai_socktype = SOCK_DGRAM;
2162 ai_hints.ai_protocol = IPPROTO_UDP;
2164 status = getaddrinfo (se->node,
2165 (se->service != NULL) ? se->service : NET_DEFAULT_PORT,
2166 &ai_hints, &ai_list);
2169 c_complain (LOG_ERR, &complaint,
2170 "network plugin: getaddrinfo (%s, %s) failed: %s",
2171 (se->node == NULL) ? "(null)" : se->node,
2172 (se->service == NULL) ? "(null)" : se->service,
2173 gai_strerror (status));
2178 c_release (LOG_NOTICE, &complaint,
2179 "network plugin: Successfully resolved \"%s\".",
2183 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
2185 if (client->fd >= 0) /* when we reconnect */
2186 sockent_client_disconnect(se);
2188 client->fd = socket (ai_ptr->ai_family,
2189 ai_ptr->ai_socktype,
2190 ai_ptr->ai_protocol);
2194 ERROR ("network plugin: socket(2) failed: %s",
2195 sstrerror (errno, errbuf,
2200 client->addr = malloc (sizeof (*client->addr));
2201 if (client->addr == NULL)
2203 ERROR ("network plugin: malloc failed.");
2209 memset (client->addr, 0, sizeof (*client->addr));
2210 assert (sizeof (*client->addr) >= ai_ptr->ai_addrlen);
2211 memcpy (client->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
2212 client->addrlen = ai_ptr->ai_addrlen;
2214 network_set_ttl (se, ai_ptr);
2215 network_set_interface (se, ai_ptr);
2217 /* We don't open more than one write-socket per
2218 * node/service pair.. */
2222 freeaddrinfo (ai_list);
2226 if (client->resolve_interval > 0)
2227 client->next_resolve_reconnect = now + client->resolve_interval;
2229 } /* }}} int sockent_client_connect */
2231 /* Open the file descriptors for a initialized sockent structure. */
2232 static int sockent_server_listen (sockent_t *se) /* {{{ */
2234 struct addrinfo ai_hints;
2235 struct addrinfo *ai_list, *ai_ptr;
2239 const char *service;
2244 assert (se->data.server.fd == NULL);
2245 assert (se->data.server.fd_num == 0);
2248 service = se->service;
2250 if (service == NULL)
2251 service = NET_DEFAULT_PORT;
2253 DEBUG ("network plugin: sockent_server_listen: node = %s; service = %s;",
2256 memset (&ai_hints, 0, sizeof (ai_hints));
2257 ai_hints.ai_flags = 0;
2259 ai_hints.ai_flags |= AI_PASSIVE;
2261 #ifdef AI_ADDRCONFIG
2262 ai_hints.ai_flags |= AI_ADDRCONFIG;
2264 ai_hints.ai_family = AF_UNSPEC;
2265 ai_hints.ai_socktype = SOCK_DGRAM;
2266 ai_hints.ai_protocol = IPPROTO_UDP;
2268 status = getaddrinfo (node, service, &ai_hints, &ai_list);
2271 ERROR ("network plugin: getaddrinfo (%s, %s) failed: %s",
2272 (se->node == NULL) ? "(null)" : se->node,
2273 (se->service == NULL) ? "(null)" : se->service,
2274 gai_strerror (status));
2278 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
2282 tmp = realloc (se->data.server.fd,
2283 sizeof (*tmp) * (se->data.server.fd_num + 1));
2286 ERROR ("network plugin: realloc failed.");
2289 se->data.server.fd = tmp;
2290 tmp = se->data.server.fd + se->data.server.fd_num;
2292 *tmp = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
2293 ai_ptr->ai_protocol);
2297 ERROR ("network plugin: socket(2) failed: %s",
2298 sstrerror (errno, errbuf,
2303 status = network_bind_socket (*tmp, ai_ptr, se->interface);
2311 se->data.server.fd_num++;
2313 } /* for (ai_list) */
2315 freeaddrinfo (ai_list);
2317 if (se->data.server.fd_num <= 0)
2320 } /* }}} int sockent_server_listen */
2322 /* Add a sockent to the global list of sockets */
2323 static int sockent_add (sockent_t *se) /* {{{ */
2325 sockent_t *last_ptr;
2330 if (se->type == SOCKENT_TYPE_SERVER)
2335 tmp = realloc (listen_sockets_pollfd,
2336 sizeof (*tmp) * (listen_sockets_num
2337 + se->data.server.fd_num));
2340 ERROR ("network plugin: realloc failed.");
2343 listen_sockets_pollfd = tmp;
2344 tmp = listen_sockets_pollfd + listen_sockets_num;
2346 for (i = 0; i < se->data.server.fd_num; i++)
2348 memset (tmp + i, 0, sizeof (*tmp));
2349 tmp[i].fd = se->data.server.fd[i];
2350 tmp[i].events = POLLIN | POLLPRI;
2354 listen_sockets_num += se->data.server.fd_num;
2356 if (listen_sockets == NULL)
2358 listen_sockets = se;
2361 last_ptr = listen_sockets;
2363 else /* if (se->type == SOCKENT_TYPE_CLIENT) */
2365 if (sending_sockets == NULL)
2367 sending_sockets = se;
2370 last_ptr = sending_sockets;
2373 while (last_ptr->next != NULL)
2374 last_ptr = last_ptr->next;
2375 last_ptr->next = se;
2378 } /* }}} int sockent_add */
2380 static void *dispatch_thread (void __attribute__((unused)) *arg) /* {{{ */
2384 receive_list_entry_t *ent;
2387 /* Lock and wait for more data to come in */
2388 pthread_mutex_lock (&receive_list_lock);
2389 while ((listen_loop == 0)
2390 && (receive_list_head == NULL))
2391 pthread_cond_wait (&receive_list_cond, &receive_list_lock);
2393 /* Remove the head entry and unlock */
2394 ent = receive_list_head;
2396 receive_list_head = ent->next;
2397 receive_list_length--;
2398 pthread_mutex_unlock (&receive_list_lock);
2400 /* Check whether we are supposed to exit. We do NOT check `listen_loop'
2401 * because we dispatch all missing packets before shutting down. */
2405 /* Look for the correct `sockent_t' */
2406 se = listen_sockets;
2411 for (i = 0; i < se->data.server.fd_num; i++)
2412 if (se->data.server.fd[i] == ent->fd)
2415 if (i < se->data.server.fd_num)
2423 ERROR ("network plugin: Got packet from FD %i, but can't "
2424 "find an appropriate socket entry.",
2431 parse_packet (se, ent->data, ent->data_len, /* flags = */ 0,
2432 /* username = */ NULL);
2438 } /* }}} void *dispatch_thread */
2440 static int network_receive (void) /* {{{ */
2442 char buffer[network_config_packet_size];
2448 receive_list_entry_t *private_list_head;
2449 receive_list_entry_t *private_list_tail;
2450 uint64_t private_list_length;
2452 assert (listen_sockets_num > 0);
2454 private_list_head = NULL;
2455 private_list_tail = NULL;
2456 private_list_length = 0;
2458 while (listen_loop == 0)
2460 status = poll (listen_sockets_pollfd, listen_sockets_num, -1);
2466 ERROR ("network plugin: poll(2) failed: %s",
2467 sstrerror (errno, errbuf, sizeof (errbuf)));
2471 for (i = 0; (i < listen_sockets_num) && (status > 0); i++)
2473 receive_list_entry_t *ent;
2475 if ((listen_sockets_pollfd[i].revents
2476 & (POLLIN | POLLPRI)) == 0)
2480 buffer_len = recv (listen_sockets_pollfd[i].fd,
2481 buffer, sizeof (buffer),
2486 status = (errno != 0) ? errno : -1;
2487 ERROR ("network plugin: recv(2) failed: %s",
2488 sstrerror (errno, errbuf, sizeof (errbuf)));
2492 stats_octets_rx += ((uint64_t) buffer_len);
2495 /* TODO: Possible performance enhancement: Do not free
2496 * these entries in the dispatch thread but put them in
2497 * another list, so we don't have to allocate more and
2498 * more of these structures. */
2499 ent = malloc (sizeof (receive_list_entry_t));
2502 ERROR ("network plugin: malloc failed.");
2506 memset (ent, 0, sizeof (receive_list_entry_t));
2507 ent->data = malloc (network_config_packet_size);
2508 if (ent->data == NULL)
2511 ERROR ("network plugin: malloc failed.");
2515 ent->fd = listen_sockets_pollfd[i].fd;
2518 memcpy (ent->data, buffer, buffer_len);
2519 ent->data_len = buffer_len;
2521 if (private_list_head == NULL)
2522 private_list_head = ent;
2524 private_list_tail->next = ent;
2525 private_list_tail = ent;
2526 private_list_length++;
2528 /* Do not block here. Blocking here has led to
2529 * insufficient performance in the past. */
2530 if (pthread_mutex_trylock (&receive_list_lock) == 0)
2532 assert (((receive_list_head == NULL) && (receive_list_length == 0))
2533 || ((receive_list_head != NULL) && (receive_list_length != 0)));
2535 if (receive_list_head == NULL)
2536 receive_list_head = private_list_head;
2538 receive_list_tail->next = private_list_head;
2539 receive_list_tail = private_list_tail;
2540 receive_list_length += private_list_length;
2542 pthread_cond_signal (&receive_list_cond);
2543 pthread_mutex_unlock (&receive_list_lock);
2545 private_list_head = NULL;
2546 private_list_tail = NULL;
2547 private_list_length = 0;
2551 } /* for (listen_sockets_pollfd) */
2555 } /* while (listen_loop == 0) */
2557 /* Make sure everything is dispatched before exiting. */
2558 if (private_list_head != NULL)
2560 pthread_mutex_lock (&receive_list_lock);
2562 if (receive_list_head == NULL)
2563 receive_list_head = private_list_head;
2565 receive_list_tail->next = private_list_head;
2566 receive_list_tail = private_list_tail;
2567 receive_list_length += private_list_length;
2569 private_list_head = NULL;
2570 private_list_tail = NULL;
2571 private_list_length = 0;
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;
2591 memset (&send_buffer_vl, 0, sizeof (send_buffer_vl));
2592 } /* int network_init_buffer */
2594 static void networt_send_buffer_plain (sockent_t *se, /* {{{ */
2595 const char *buffer, size_t buffer_size)
2601 status = sockent_client_connect (se);
2605 status = sendto (se->data.client.fd, buffer, buffer_size,
2607 (struct sockaddr *) se->data.client.addr,
2608 se->data.client.addrlen);
2613 if ((errno == EINTR) || (errno == EAGAIN))
2616 ERROR ("network plugin: sendto failed: %s. Closing sending socket.",
2617 sstrerror (errno, errbuf, sizeof (errbuf)));
2618 sockent_client_disconnect (se);
2624 } /* }}} void networt_send_buffer_plain */
2627 #define BUFFER_ADD(p,s) do { \
2628 memcpy (buffer + buffer_offset, (p), (s)); \
2629 buffer_offset += (s); \
2632 static void networt_send_buffer_signed (sockent_t *se, /* {{{ */
2633 const char *in_buffer, size_t in_buffer_size)
2635 part_signature_sha256_t ps;
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 memset (&ps, 0, sizeof (ps));
2678 ps.head.type = htons (TYPE_SIGN_SHA256);
2679 ps.head.length = htons (PART_SIGNATURE_SHA256_SIZE + username_len);
2681 /* Calculate the hash value. */
2682 gcry_md_write (hd, buffer + PART_SIGNATURE_SHA256_SIZE,
2683 username_len + in_buffer_size);
2684 hash = gcry_md_read (hd, GCRY_MD_SHA256);
2687 ERROR ("network plugin: gcry_md_read failed.");
2691 memcpy (ps.hash, hash, sizeof (ps.hash));
2693 /* Add the header */
2696 BUFFER_ADD (&ps.head.type, sizeof (ps.head.type));
2697 BUFFER_ADD (&ps.head.length, sizeof (ps.head.length));
2698 BUFFER_ADD (ps.hash, sizeof (ps.hash));
2700 assert (buffer_offset == PART_SIGNATURE_SHA256_SIZE);
2705 buffer_offset = PART_SIGNATURE_SHA256_SIZE + username_len + in_buffer_size;
2706 networt_send_buffer_plain (se, buffer, buffer_offset);
2707 } /* }}} void networt_send_buffer_signed */
2709 static void networt_send_buffer_encrypted (sockent_t *se, /* {{{ */
2710 const char *in_buffer, size_t in_buffer_size)
2712 part_encryption_aes256_t pea;
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 memset (&pea, 0, sizeof (pea));
2723 pea.head.type = htons (TYPE_ENCR_AES256);
2725 pea.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: networt_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 networt_send_buffer_plain (se, buffer, buffer_size);
2787 } /* }}} void networt_send_buffer_encrypted */
2789 #endif /* HAVE_LIBGCRYPT */
2791 static void network_send_buffer (char *buffer, size_t buffer_len) /* {{{ */
2795 DEBUG ("network plugin: network_send_buffer: buffer_len = %zu", buffer_len);
2797 for (se = sending_sockets; se != NULL; se = se->next)
2800 if (se->data.client.security_level == SECURITY_LEVEL_ENCRYPT)
2801 networt_send_buffer_encrypted (se, buffer, buffer_len);
2802 else if (se->data.client.security_level == SECURITY_LEVEL_SIGN)
2803 networt_send_buffer_signed (se, buffer, buffer_len);
2804 else /* if (se->data.client.security_level == SECURITY_LEVEL_NONE) */
2805 #endif /* HAVE_LIBGCRYPT */
2806 networt_send_buffer_plain (se, buffer, buffer_len);
2807 } /* for (sending_sockets) */
2808 } /* }}} void network_send_buffer */
2810 static int add_to_buffer (char *buffer, int buffer_size, /* {{{ */
2811 value_list_t *vl_def,
2812 const data_set_t *ds, const value_list_t *vl)
2814 char *buffer_orig = buffer;
2816 if (strcmp (vl_def->host, vl->host) != 0)
2818 if (write_part_string (&buffer, &buffer_size, TYPE_HOST,
2819 vl->host, strlen (vl->host)) != 0)
2821 sstrncpy (vl_def->host, vl->host, sizeof (vl_def->host));
2824 if (vl_def->time != vl->time)
2826 if (write_part_number (&buffer, &buffer_size, TYPE_TIME_HR,
2827 (uint64_t) vl->time))
2829 vl_def->time = vl->time;
2832 if (vl_def->interval != vl->interval)
2834 if (write_part_number (&buffer, &buffer_size, TYPE_INTERVAL_HR,
2835 (uint64_t) vl->interval))
2837 vl_def->interval = vl->interval;
2840 if (strcmp (vl_def->plugin, vl->plugin) != 0)
2842 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN,
2843 vl->plugin, strlen (vl->plugin)) != 0)
2845 sstrncpy (vl_def->plugin, vl->plugin, sizeof (vl_def->plugin));
2848 if (strcmp (vl_def->plugin_instance, vl->plugin_instance) != 0)
2850 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
2851 vl->plugin_instance,
2852 strlen (vl->plugin_instance)) != 0)
2854 sstrncpy (vl_def->plugin_instance, vl->plugin_instance, sizeof (vl_def->plugin_instance));
2857 if (strcmp (vl_def->type, vl->type) != 0)
2859 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
2860 vl->type, strlen (vl->type)) != 0)
2862 sstrncpy (vl_def->type, ds->type, sizeof (vl_def->type));
2865 if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
2867 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE_INSTANCE,
2869 strlen (vl->type_instance)) != 0)
2871 sstrncpy (vl_def->type_instance, vl->type_instance, sizeof (vl_def->type_instance));
2874 if (write_part_values (&buffer, &buffer_size, ds, vl) != 0)
2877 return (buffer - buffer_orig);
2878 } /* }}} int add_to_buffer */
2880 static void flush_buffer (void)
2882 DEBUG ("network plugin: flush_buffer: send_buffer_fill = %i",
2885 network_send_buffer (send_buffer, (size_t) send_buffer_fill);
2887 stats_octets_tx += ((uint64_t) send_buffer_fill);
2890 network_init_buffer ();
2893 static int network_write (const data_set_t *ds, const value_list_t *vl,
2894 user_data_t __attribute__((unused)) *user_data)
2898 if (!check_send_okay (vl))
2901 char name[6*DATA_MAX_NAME_LEN];
2902 FORMAT_VL (name, sizeof (name), vl);
2903 name[sizeof (name) - 1] = 0;
2904 DEBUG ("network plugin: network_write: "
2905 "NOT sending %s.", name);
2907 /* Counter is not protected by another lock and may be reached by
2908 * multiple threads */
2909 pthread_mutex_lock (&stats_lock);
2910 stats_values_not_sent++;
2911 pthread_mutex_unlock (&stats_lock);
2915 uc_meta_data_add_unsigned_int (vl,
2916 "network:time_sent", (uint64_t) vl->time);
2918 pthread_mutex_lock (&send_buffer_lock);
2920 status = add_to_buffer (send_buffer_ptr,
2921 network_config_packet_size - (send_buffer_fill + BUFF_SIG_SIZE),
2926 /* status == bytes added to the buffer */
2927 send_buffer_fill += status;
2928 send_buffer_ptr += status;
2930 stats_values_sent++;
2936 status = add_to_buffer (send_buffer_ptr,
2937 network_config_packet_size - (send_buffer_fill + BUFF_SIG_SIZE),
2943 send_buffer_fill += status;
2944 send_buffer_ptr += status;
2946 stats_values_sent++;
2952 ERROR ("network plugin: Unable to append to the "
2953 "buffer for some weird reason");
2955 else if ((network_config_packet_size - send_buffer_fill) < 15)
2960 pthread_mutex_unlock (&send_buffer_lock);
2962 return ((status < 0) ? -1 : 0);
2963 } /* int network_write */
2965 static int network_config_set_boolean (const oconfig_item_t *ci, /* {{{ */
2968 if ((ci->values_num != 1)
2969 || ((ci->values[0].type != OCONFIG_TYPE_BOOLEAN)
2970 && (ci->values[0].type != OCONFIG_TYPE_STRING)))
2972 ERROR ("network plugin: The `%s' config option needs "
2973 "exactly one boolean argument.", ci->key);
2977 if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
2979 if (ci->values[0].value.boolean)
2986 char *str = ci->values[0].value.string;
2990 else if (IS_FALSE (str))
2994 ERROR ("network plugin: Cannot parse string value `%s' of the `%s' "
2995 "option as boolean value.",
3002 } /* }}} int network_config_set_boolean */
3004 static int network_config_set_ttl (const oconfig_item_t *ci) /* {{{ */
3007 if ((ci->values_num != 1)
3008 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
3010 WARNING ("network plugin: The `TimeToLive' config option needs exactly "
3011 "one numeric argument.");
3015 tmp = (int) ci->values[0].value.number;
3016 if ((tmp > 0) && (tmp <= 255))
3017 network_config_ttl = tmp;
3019 WARNING ("network plugin: The `TimeToLive' must be between 1 and 255.");
3024 } /* }}} int network_config_set_ttl */
3026 static int network_config_set_interface (const oconfig_item_t *ci, /* {{{ */
3029 if ((ci->values_num != 1)
3030 || (ci->values[0].type != OCONFIG_TYPE_STRING))
3032 WARNING ("network plugin: The `Interface' config option needs exactly "
3033 "one string argument.");
3037 if (interface == NULL)
3040 *interface = if_nametoindex (ci->values[0].value.string);
3043 } /* }}} int network_config_set_interface */
3045 static int network_config_set_buffer_size (const oconfig_item_t *ci) /* {{{ */
3048 if ((ci->values_num != 1)
3049 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
3051 WARNING ("network plugin: The `MaxPacketSize' config option needs exactly "
3052 "one numeric argument.");
3056 tmp = (int) ci->values[0].value.number;
3057 if ((tmp >= 1024) && (tmp <= 65535))
3058 network_config_packet_size = tmp;
3061 } /* }}} int network_config_set_buffer_size */
3064 static int network_config_set_string (const oconfig_item_t *ci, /* {{{ */
3068 if ((ci->values_num != 1)
3069 || (ci->values[0].type != OCONFIG_TYPE_STRING))
3071 WARNING ("network plugin: The `%s' config option needs exactly "
3072 "one string argument.", ci->key);
3076 tmp = strdup (ci->values[0].value.string);
3080 sfree (*ret_string);
3084 } /* }}} int network_config_set_string */
3085 #endif /* HAVE_LIBGCRYPT */
3088 static int network_config_set_security_level (oconfig_item_t *ci, /* {{{ */
3092 if ((ci->values_num != 1)
3093 || (ci->values[0].type != OCONFIG_TYPE_STRING))
3095 WARNING ("network plugin: The `SecurityLevel' config option needs exactly "
3096 "one string argument.");
3100 str = ci->values[0].value.string;
3101 if (strcasecmp ("Encrypt", str) == 0)
3102 *retval = SECURITY_LEVEL_ENCRYPT;
3103 else if (strcasecmp ("Sign", str) == 0)
3104 *retval = SECURITY_LEVEL_SIGN;
3105 else if (strcasecmp ("None", str) == 0)
3106 *retval = SECURITY_LEVEL_NONE;
3109 WARNING ("network plugin: Unknown security level: %s.", str);
3114 } /* }}} int network_config_set_security_level */
3115 #endif /* HAVE_LIBGCRYPT */
3117 static int network_config_add_listen (const oconfig_item_t *ci) /* {{{ */
3123 if ((ci->values_num < 1) || (ci->values_num > 2)
3124 || (ci->values[0].type != OCONFIG_TYPE_STRING)
3125 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
3127 ERROR ("network plugin: The `%s' config option needs "
3128 "one or two string arguments.", ci->key);
3132 se = sockent_create (SOCKENT_TYPE_SERVER);
3135 ERROR ("network plugin: sockent_create failed.");
3139 se->node = strdup (ci->values[0].value.string);
3140 if (ci->values_num >= 2)
3141 se->service = strdup (ci->values[1].value.string);
3143 for (i = 0; i < ci->children_num; i++)
3145 oconfig_item_t *child = ci->children + i;
3148 if (strcasecmp ("AuthFile", child->key) == 0)
3149 network_config_set_string (child, &se->data.server.auth_file);
3150 else if (strcasecmp ("SecurityLevel", child->key) == 0)
3151 network_config_set_security_level (child,
3152 &se->data.server.security_level);
3154 #endif /* HAVE_LIBGCRYPT */
3155 if (strcasecmp ("Interface", child->key) == 0)
3156 network_config_set_interface (child,
3160 WARNING ("network plugin: Option `%s' is not allowed here.",
3166 if ((se->data.server.security_level > SECURITY_LEVEL_NONE)
3167 && (se->data.server.auth_file == NULL))
3169 ERROR ("network plugin: A security level higher than `none' was "
3170 "requested, but no AuthFile option was given. Cowardly refusing to "
3171 "open this socket!");
3172 sockent_destroy (se);
3175 #endif /* HAVE_LIBGCRYPT */
3177 status = sockent_init_crypto (se);
3180 ERROR ("network plugin: network_config_add_listen: sockent_init_crypto() failed.");
3181 sockent_destroy (se);
3185 status = sockent_server_listen (se);
3188 ERROR ("network plugin: network_config_add_server: sockent_server_listen failed.");
3189 sockent_destroy (se);
3193 status = sockent_add (se);
3196 ERROR ("network plugin: network_config_add_listen: sockent_add failed.");
3197 sockent_destroy (se);
3202 } /* }}} int network_config_add_listen */
3204 static int network_config_add_server (const oconfig_item_t *ci) /* {{{ */
3210 if ((ci->values_num < 1) || (ci->values_num > 2)
3211 || (ci->values[0].type != OCONFIG_TYPE_STRING)
3212 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
3214 ERROR ("network plugin: The `%s' config option needs "
3215 "one or two string arguments.", ci->key);
3219 se = sockent_create (SOCKENT_TYPE_CLIENT);
3222 ERROR ("network plugin: sockent_create failed.");
3226 se->node = strdup (ci->values[0].value.string);
3227 if (ci->values_num >= 2)
3228 se->service = strdup (ci->values[1].value.string);
3230 for (i = 0; i < ci->children_num; i++)
3232 oconfig_item_t *child = ci->children + i;
3235 if (strcasecmp ("Username", child->key) == 0)
3236 network_config_set_string (child, &se->data.client.username);
3237 else if (strcasecmp ("Password", child->key) == 0)
3238 network_config_set_string (child, &se->data.client.password);
3239 else if (strcasecmp ("SecurityLevel", child->key) == 0)
3240 network_config_set_security_level (child,
3241 &se->data.client.security_level);
3243 #endif /* HAVE_LIBGCRYPT */
3244 if (strcasecmp ("Interface", child->key) == 0)
3245 network_config_set_interface (child,
3247 else if (strcasecmp ("ResolveInterval", child->key) == 0)
3248 cf_util_get_cdtime(child, &se->data.client.resolve_interval);
3251 WARNING ("network plugin: Option `%s' is not allowed here.",
3257 if ((se->data.client.security_level > SECURITY_LEVEL_NONE)
3258 && ((se->data.client.username == NULL)
3259 || (se->data.client.password == NULL)))
3261 ERROR ("network plugin: A security level higher than `none' was "
3262 "requested, but no Username or Password option was given. "
3263 "Cowardly refusing to open this socket!");
3264 sockent_destroy (se);
3267 #endif /* HAVE_LIBGCRYPT */
3269 status = sockent_init_crypto (se);
3272 ERROR ("network plugin: network_config_add_server: sockent_init_crypto() failed.");
3273 sockent_destroy (se);
3277 /* No call to sockent_client_connect() here -- it is called from
3278 * networt_send_buffer_plain(). */
3280 status = sockent_add (se);
3283 ERROR ("network plugin: network_config_add_server: sockent_add failed.");
3284 sockent_destroy (se);
3289 } /* }}} int network_config_add_server */
3291 static int network_config (oconfig_item_t *ci) /* {{{ */
3295 /* The options need to be applied first */
3296 for (i = 0; i < ci->children_num; i++)
3298 oconfig_item_t *child = ci->children + i;
3299 if (strcasecmp ("TimeToLive", child->key) == 0)
3300 network_config_set_ttl (child);
3303 for (i = 0; i < ci->children_num; i++)
3305 oconfig_item_t *child = ci->children + i;
3307 if (strcasecmp ("Listen", child->key) == 0)
3308 network_config_add_listen (child);
3309 else if (strcasecmp ("Server", child->key) == 0)
3310 network_config_add_server (child);
3311 else if (strcasecmp ("TimeToLive", child->key) == 0) {
3312 /* Handled earlier */
3314 else if (strcasecmp ("MaxPacketSize", child->key) == 0)
3315 network_config_set_buffer_size (child);
3316 else if (strcasecmp ("Forward", child->key) == 0)
3317 network_config_set_boolean (child, &network_config_forward);
3318 else if (strcasecmp ("ReportStats", child->key) == 0)
3319 network_config_set_boolean (child, &network_config_stats);
3322 WARNING ("network plugin: Option `%s' is not allowed here.",
3328 } /* }}} int network_config */
3330 static int network_notification (const notification_t *n,
3331 user_data_t __attribute__((unused)) *user_data)
3333 char buffer[network_config_packet_size];
3334 char *buffer_ptr = buffer;
3335 int buffer_free = sizeof (buffer);
3338 if (!check_send_notify_okay (n))
3341 memset (buffer, 0, sizeof (buffer));
3343 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_TIME_HR,
3344 (uint64_t) n->time);
3348 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_SEVERITY,
3349 (uint64_t) n->severity);
3353 if (strlen (n->host) > 0)
3355 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_HOST,
3356 n->host, strlen (n->host));
3361 if (strlen (n->plugin) > 0)
3363 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_PLUGIN,
3364 n->plugin, strlen (n->plugin));
3369 if (strlen (n->plugin_instance) > 0)
3371 status = write_part_string (&buffer_ptr, &buffer_free,
3372 TYPE_PLUGIN_INSTANCE,
3373 n->plugin_instance, strlen (n->plugin_instance));
3378 if (strlen (n->type) > 0)
3380 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE,
3381 n->type, strlen (n->type));
3386 if (strlen (n->type_instance) > 0)
3388 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE_INSTANCE,
3389 n->type_instance, strlen (n->type_instance));
3394 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_MESSAGE,
3395 n->message, strlen (n->message));
3399 network_send_buffer (buffer, sizeof (buffer) - buffer_free);
3402 } /* int network_notification */
3404 static int network_shutdown (void)
3410 /* Kill the listening thread */
3411 if (receive_thread_running != 0)
3413 INFO ("network plugin: Stopping receive thread.");
3414 pthread_kill (receive_thread_id, SIGTERM);
3415 pthread_join (receive_thread_id, NULL /* no return value */);
3416 memset (&receive_thread_id, 0, sizeof (receive_thread_id));
3417 receive_thread_running = 0;
3420 /* Shutdown the dispatching thread */
3421 if (dispatch_thread_running != 0)
3423 INFO ("network plugin: Stopping dispatch thread.");
3424 pthread_mutex_lock (&receive_list_lock);
3425 pthread_cond_broadcast (&receive_list_cond);
3426 pthread_mutex_unlock (&receive_list_lock);
3427 pthread_join (dispatch_thread_id, /* ret = */ NULL);
3428 dispatch_thread_running = 0;
3431 sockent_destroy (listen_sockets);
3433 if (send_buffer_fill > 0)
3436 sfree (send_buffer);
3438 for (se = sending_sockets; se != NULL; se = se->next)
3439 sockent_client_disconnect (se);
3440 sockent_destroy (sending_sockets);
3442 plugin_unregister_config ("network");
3443 plugin_unregister_init ("network");
3444 plugin_unregister_write ("network");
3445 plugin_unregister_shutdown ("network");
3448 } /* int network_shutdown */
3450 static int network_stats_read (void) /* {{{ */
3452 derive_t copy_octets_rx;
3453 derive_t copy_octets_tx;
3454 derive_t copy_packets_rx;
3455 derive_t copy_packets_tx;
3456 derive_t copy_values_dispatched;
3457 derive_t copy_values_not_dispatched;
3458 derive_t copy_values_sent;
3459 derive_t copy_values_not_sent;
3460 derive_t copy_receive_list_length;
3461 value_list_t vl = VALUE_LIST_INIT;
3464 copy_octets_rx = stats_octets_rx;
3465 copy_octets_tx = stats_octets_tx;
3466 copy_packets_rx = stats_packets_rx;
3467 copy_packets_tx = stats_packets_tx;
3468 copy_values_dispatched = stats_values_dispatched;
3469 copy_values_not_dispatched = stats_values_not_dispatched;
3470 copy_values_sent = stats_values_sent;
3471 copy_values_not_sent = stats_values_not_sent;
3472 copy_receive_list_length = receive_list_length;
3474 /* Initialize `vl' */
3478 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
3479 sstrncpy (vl.plugin, "network", sizeof (vl.plugin));
3481 /* Octets received / sent */
3482 vl.values[0].derive = (derive_t) copy_octets_rx;
3483 vl.values[1].derive = (derive_t) copy_octets_tx;
3484 sstrncpy (vl.type, "if_octets", sizeof (vl.type));
3485 plugin_dispatch_values (&vl);
3487 /* Packets received / send */
3488 vl.values[0].derive = (derive_t) copy_packets_rx;
3489 vl.values[1].derive = (derive_t) copy_packets_tx;
3490 sstrncpy (vl.type, "if_packets", sizeof (vl.type));
3491 plugin_dispatch_values (&vl);
3493 /* Values (not) dispatched and (not) send */
3494 sstrncpy (vl.type, "total_values", sizeof (vl.type));
3497 vl.values[0].derive = (derive_t) copy_values_dispatched;
3498 sstrncpy (vl.type_instance, "dispatch-accepted",
3499 sizeof (vl.type_instance));
3500 plugin_dispatch_values (&vl);
3502 vl.values[0].derive = (derive_t) copy_values_not_dispatched;
3503 sstrncpy (vl.type_instance, "dispatch-rejected",
3504 sizeof (vl.type_instance));
3505 plugin_dispatch_values (&vl);
3507 vl.values[0].derive = (derive_t) copy_values_sent;
3508 sstrncpy (vl.type_instance, "send-accepted",
3509 sizeof (vl.type_instance));
3510 plugin_dispatch_values (&vl);
3512 vl.values[0].derive = (derive_t) copy_values_not_sent;
3513 sstrncpy (vl.type_instance, "send-rejected",
3514 sizeof (vl.type_instance));
3515 plugin_dispatch_values (&vl);
3517 /* Receive queue length */
3518 vl.values[0].gauge = (gauge_t) copy_receive_list_length;
3519 sstrncpy (vl.type, "queue_length", sizeof (vl.type));
3520 vl.type_instance[0] = 0;
3521 plugin_dispatch_values (&vl);
3524 } /* }}} int network_stats_read */
3526 static int network_init (void)
3528 static _Bool have_init = 0;
3530 /* Check if we were already initialized. If so, just return - there's
3531 * nothing more to do (for now, that is). */
3537 network_init_gcrypt ();
3540 if (network_config_stats != 0)
3541 plugin_register_read ("network", network_stats_read);
3543 plugin_register_shutdown ("network", network_shutdown);
3545 send_buffer = malloc (network_config_packet_size);
3546 if (send_buffer == NULL)
3548 ERROR ("network plugin: malloc failed.");
3551 network_init_buffer ();
3553 /* setup socket(s) and so on */
3554 if (sending_sockets != NULL)
3556 plugin_register_write ("network", network_write,
3557 /* user_data = */ NULL);
3558 plugin_register_notification ("network", network_notification,
3559 /* user_data = */ NULL);
3562 /* If no threads need to be started, return here. */
3563 if ((listen_sockets_num == 0)
3564 || ((dispatch_thread_running != 0)
3565 && (receive_thread_running != 0)))
3568 if (dispatch_thread_running == 0)
3571 status = plugin_thread_create (&dispatch_thread_id,
3572 NULL /* no attributes */,
3574 NULL /* no argument */);
3578 ERROR ("network: pthread_create failed: %s",
3579 sstrerror (errno, errbuf,
3584 dispatch_thread_running = 1;
3588 if (receive_thread_running == 0)
3591 status = plugin_thread_create (&receive_thread_id,
3592 NULL /* no attributes */,
3594 NULL /* no argument */);
3598 ERROR ("network: pthread_create failed: %s",
3599 sstrerror (errno, errbuf,
3604 receive_thread_running = 1;
3609 } /* int network_init */
3612 * The flush option of the network plugin cannot flush individual identifiers.
3613 * All the values are added to a buffer and sent when the buffer is full, the
3614 * requested value may or may not be in there, it's not worth finding out. We
3615 * just send the buffer if `flush' is called - if the requested value was in
3616 * there, good. If not, well, then there is nothing to flush.. -octo
3618 static int network_flush (__attribute__((unused)) cdtime_t timeout,
3619 __attribute__((unused)) const char *identifier,
3620 __attribute__((unused)) user_data_t *user_data)
3622 pthread_mutex_lock (&send_buffer_lock);
3624 if (send_buffer_fill > 0)
3627 pthread_mutex_unlock (&send_buffer_lock);
3630 } /* int network_flush */
3632 void module_register (void)
3634 plugin_register_complex_config ("network", network_config);
3635 plugin_register_init ("network", network_init);
3636 plugin_register_flush ("network", network_flush,
3637 /* user_data = */ NULL);
3638 } /* void module_register */
3640 /* vim: set fdm=marker : */