2 * collectd - src/network.c
3 * Copyright (C) 2005-2013 Florian octo Forster
4 * Copyright (C) 2009 Aman Gupta
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; only version 2.1 of the License is
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 * Florian octo Forster <octo at collectd.org>
22 * Aman Gupta <aman at tmm1.net>
25 #define _BSD_SOURCE /* For struct ip_mreq */
30 #include "configfile.h"
31 #include "utils_fbhash.h"
32 #include "utils_avltree.h"
33 #include "utils_cache.h"
34 #include "utils_complain.h"
42 # include <sys/socket.h>
48 # include <netinet/in.h>
51 # include <arpa/inet.h>
62 # if defined __APPLE__
63 /* default xcode compiler throws warnings even when deprecated functionality
64 * is not used. -Werror breaks the build because of erroneous warnings.
65 * http://stackoverflow.com/questions/10556299/compiler-warnings-with-libgcrypt-v1-5-0/12830209#12830209
67 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
69 /* FreeBSD's copy of libgcrypt extends the existing GCRYPT_NO_DEPRECATED
70 * to properly hide all deprecated functionality.
71 * http://svnweb.freebsd.org/ports/head/security/libgcrypt/files/patch-src__gcrypt.h.in
73 # define GCRYPT_NO_DEPRECATED
75 # if defined __APPLE__
76 /* Re enable deprecation warnings */
77 # pragma GCC diagnostic warning "-Wdeprecated-declarations"
79 # if GCRYPT_VERSION_NUMBER < 0x010600
80 GCRY_THREAD_OPTION_PTHREAD_IMPL;
84 #ifndef IPV6_ADD_MEMBERSHIP
85 # ifdef IPV6_JOIN_GROUP
86 # define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
88 # error "Neither IP_ADD_MEMBERSHIP nor IPV6_JOIN_GROUP is defined"
90 #endif /* !IP_ADD_MEMBERSHIP */
93 * Maximum size required for encryption / signing:
95 * 42 bytes for the encryption header
96 * + 64 bytes for the username
100 #define BUFF_SIG_SIZE 106
105 #define SECURITY_LEVEL_NONE 0
107 # define SECURITY_LEVEL_SIGN 1
108 # define SECURITY_LEVEL_ENCRYPT 2
110 struct sockent_client
113 struct sockaddr_storage *addr;
119 gcry_cipher_hd_t cypher;
120 unsigned char password_hash[32];
122 cdtime_t next_resolve_reconnect;
123 cdtime_t resolve_interval;
126 struct sockent_server
134 gcry_cipher_hd_t cypher;
138 typedef struct sockent
140 #define SOCKENT_TYPE_CLIENT 1
141 #define SOCKENT_TYPE_SERVER 2
150 struct sockent_client client;
151 struct sockent_server server;
154 struct sockent *next;
157 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
158 * 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
159 * +-------+-----------------------+-------------------------------+
160 * ! Ver. ! ! Length !
161 * +-------+-----------------------+-------------------------------+
168 typedef struct part_header_s part_header_t;
170 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
171 * 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
172 * +-------------------------------+-------------------------------+
174 * +-------------------------------+-------------------------------+
175 * : (Length - 4) Bytes :
176 * +---------------------------------------------------------------+
183 typedef struct part_string_s part_string_t;
185 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
186 * 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
187 * +-------------------------------+-------------------------------+
189 * +-------------------------------+-------------------------------+
190 * : (Length - 4 == 2 || 4 || 8) Bytes :
191 * +---------------------------------------------------------------+
198 typedef struct part_number_s part_number_t;
200 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
201 * 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
202 * +-------------------------------+-------------------------------+
204 * +-------------------------------+---------------+---------------+
205 * ! Num of values ! Type0 ! Type1 !
206 * +-------------------------------+---------------+---------------+
209 * +---------------------------------------------------------------+
212 * +---------------------------------------------------------------+
217 uint16_t *num_values;
218 uint8_t *values_types;
221 typedef struct part_values_s part_values_t;
223 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
224 * 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
225 * +-------------------------------+-------------------------------+
227 * +-------------------------------+-------------------------------+
228 * ! Hash (Bits 0 - 31) !
230 * ! Hash (Bits 224 - 255) !
231 * +---------------------------------------------------------------+
234 #define PART_SIGNATURE_SHA256_SIZE 36
235 struct part_signature_sha256_s
238 unsigned char hash[32];
241 typedef struct part_signature_sha256_s part_signature_sha256_t;
243 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
244 * 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
245 * +-------------------------------+-------------------------------+
247 * +-------------------------------+-------------------------------+
248 * ! Original length ! Padding (0 - 15 bytes) !
249 * +-------------------------------+-------------------------------+
250 * ! Hash (Bits 0 - 31) !
252 * ! Hash (Bits 128 - 159) !
253 * +---------------------------------------------------------------+
256 #define PART_ENCRYPTION_AES256_SIZE 42
257 struct part_encryption_aes256_s
260 uint16_t username_length;
262 unsigned char iv[16];
264 unsigned char hash[20];
268 typedef struct part_encryption_aes256_s part_encryption_aes256_t;
270 struct receive_list_entry_s
275 struct receive_list_entry_s *next;
277 typedef struct receive_list_entry_s receive_list_entry_t;
282 static int network_config_ttl = 0;
283 /* Ethernet - (IPv6 + UDP) = 1500 - (40 + 8) = 1452 */
284 static size_t network_config_packet_size = 1452;
285 static int network_config_forward = 0;
286 static int network_config_stats = 0;
288 static sockent_t *sending_sockets = NULL;
290 static receive_list_entry_t *receive_list_head = NULL;
291 static receive_list_entry_t *receive_list_tail = NULL;
292 static pthread_mutex_t receive_list_lock = PTHREAD_MUTEX_INITIALIZER;
293 static pthread_cond_t receive_list_cond = PTHREAD_COND_INITIALIZER;
294 static uint64_t receive_list_length = 0;
296 static sockent_t *listen_sockets = NULL;
297 static struct pollfd *listen_sockets_pollfd = NULL;
298 static size_t listen_sockets_num = 0;
300 /* The receive and dispatch threads will run as long as `listen_loop' is set to
302 static int listen_loop = 0;
303 static int receive_thread_running = 0;
304 static pthread_t receive_thread_id;
305 static int dispatch_thread_running = 0;
306 static pthread_t dispatch_thread_id;
308 /* Buffer in which to-be-sent network packets are constructed. */
309 static char *send_buffer;
310 static char *send_buffer_ptr;
311 static int send_buffer_fill;
312 static value_list_t send_buffer_vl = VALUE_LIST_STATIC;
313 static pthread_mutex_t send_buffer_lock = PTHREAD_MUTEX_INITIALIZER;
315 /* XXX: These counters are incremented from one place only. The spot in which
316 * the values are incremented is either only reachable by one thread (the
317 * dispatch thread, for example) or locked by some lock (send_buffer_lock for
318 * example). Only if neither is true, the stats_lock is acquired. The counters
319 * are always read without holding a lock in the hope that writing 8 bytes to
320 * memory is an atomic operation. */
321 static derive_t stats_octets_rx = 0;
322 static derive_t stats_octets_tx = 0;
323 static derive_t stats_packets_rx = 0;
324 static derive_t stats_packets_tx = 0;
325 static derive_t stats_values_dispatched = 0;
326 static derive_t stats_values_not_dispatched = 0;
327 static derive_t stats_values_sent = 0;
328 static derive_t stats_values_not_sent = 0;
329 static pthread_mutex_t stats_lock = PTHREAD_MUTEX_INITIALIZER;
334 static _Bool check_receive_okay (const value_list_t *vl) /* {{{ */
336 uint64_t time_sent = 0;
339 status = uc_meta_data_get_unsigned_int (vl,
340 "network:time_sent", &time_sent);
342 /* This is a value we already sent. Don't allow it to be received again in
343 * order to avoid looping. */
344 if ((status == 0) && (time_sent >= ((uint64_t) vl->time)))
348 } /* }}} _Bool check_receive_okay */
350 static _Bool check_send_okay (const value_list_t *vl) /* {{{ */
355 if (network_config_forward != 0)
358 if (vl->meta == NULL)
361 status = meta_data_get_boolean (vl->meta, "network:received", &received);
362 if (status == -ENOENT)
364 else if (status != 0)
366 ERROR ("network plugin: check_send_okay: meta_data_get_boolean failed "
367 "with status %i.", status);
371 /* By default, only *send* value lists that were not *received* by the
374 } /* }}} _Bool check_send_okay */
376 static _Bool check_notify_received (const notification_t *n) /* {{{ */
378 notification_meta_t *ptr;
380 for (ptr = n->meta; ptr != NULL; ptr = ptr->next)
381 if ((strcmp ("network:received", ptr->name) == 0)
382 && (ptr->type == NM_TYPE_BOOLEAN))
383 return ((_Bool) ptr->nm_value.nm_boolean);
386 } /* }}} _Bool check_notify_received */
388 static _Bool check_send_notify_okay (const notification_t *n) /* {{{ */
390 static c_complain_t complain_forwarding = C_COMPLAIN_INIT_STATIC;
396 received = check_notify_received (n);
398 if (network_config_forward && received)
400 c_complain_once (LOG_ERR, &complain_forwarding,
401 "network plugin: A notification has been received via the network "
402 "and forwarding is enabled. Forwarding of notifications is currently "
403 "not supported, because there is not loop-deteciton available. "
404 "Please contact the collectd mailing list if you need this "
408 /* By default, only *send* value lists that were not *received* by the
411 } /* }}} _Bool check_send_notify_okay */
413 static int network_dispatch_values (value_list_t *vl, /* {{{ */
414 const char *username)
419 || (strlen (vl->host) <= 0)
420 || (strlen (vl->plugin) <= 0)
421 || (strlen (vl->type) <= 0))
424 if (!check_receive_okay (vl))
427 char name[6*DATA_MAX_NAME_LEN];
428 FORMAT_VL (name, sizeof (name), vl);
429 name[sizeof (name) - 1] = 0;
430 DEBUG ("network plugin: network_dispatch_values: "
431 "NOT dispatching %s.", name);
433 stats_values_not_dispatched++;
437 assert (vl->meta == NULL);
439 vl->meta = meta_data_create ();
440 if (vl->meta == NULL)
442 ERROR ("network plugin: meta_data_create failed.");
446 status = meta_data_add_boolean (vl->meta, "network:received", 1);
449 ERROR ("network plugin: meta_data_add_boolean failed.");
450 meta_data_destroy (vl->meta);
455 if (username != NULL)
457 status = meta_data_add_string (vl->meta, "network:username", username);
460 ERROR ("network plugin: meta_data_add_string failed.");
461 meta_data_destroy (vl->meta);
467 plugin_dispatch_values (vl);
468 stats_values_dispatched++;
470 meta_data_destroy (vl->meta);
474 } /* }}} int network_dispatch_values */
476 static int network_dispatch_notification (notification_t *n) /* {{{ */
480 assert (n->meta == NULL);
482 status = plugin_notification_meta_add_boolean (n, "network:received", 1);
485 ERROR ("network plugin: plugin_notification_meta_add_boolean failed.");
486 plugin_notification_meta_free (n->meta);
491 status = plugin_dispatch_notification (n);
493 plugin_notification_meta_free (n->meta);
497 } /* }}} int network_dispatch_notification */
500 static void network_init_gcrypt (void) /* {{{ */
502 /* http://lists.gnupg.org/pipermail/gcrypt-devel/2003-August/000458.html
503 * Because you can't know in a library whether another library has
504 * already initialized the library */
505 if (gcry_control (GCRYCTL_ANY_INITIALIZATION_P))
508 /* http://www.gnupg.org/documentation/manuals/gcrypt/Multi_002dThreading.html
509 * To ensure thread-safety, it's important to set GCRYCTL_SET_THREAD_CBS
510 * *before* initalizing Libgcrypt with gcry_check_version(), which itself must
511 * be called before any other gcry_* function. GCRYCTL_ANY_INITIALIZATION_P
512 * above doesn't count, as it doesn't implicitly initalize Libgcrypt.
514 * tl;dr: keep all these gry_* statements in this exact order please. */
515 # if GCRYPT_VERSION_NUMBER < 0x010600
516 gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
518 gcry_check_version (NULL);
519 gcry_control (GCRYCTL_INIT_SECMEM, 32768);
520 gcry_control (GCRYCTL_INITIALIZATION_FINISHED);
521 } /* }}} void network_init_gcrypt */
523 static gcry_cipher_hd_t network_get_aes256_cypher (sockent_t *se, /* {{{ */
524 const void *iv, size_t iv_size, const char *username)
527 gcry_cipher_hd_t *cyper_ptr;
528 unsigned char password_hash[32];
530 if (se->type == SOCKENT_TYPE_CLIENT)
532 cyper_ptr = &se->data.client.cypher;
533 memcpy (password_hash, se->data.client.password_hash,
534 sizeof (password_hash));
540 cyper_ptr = &se->data.server.cypher;
542 if (username == NULL)
545 secret = fbh_get (se->data.server.userdb, username);
549 gcry_md_hash_buffer (GCRY_MD_SHA256,
551 secret, strlen (secret));
556 if (*cyper_ptr == NULL)
558 err = gcry_cipher_open (cyper_ptr,
559 GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_OFB, /* flags = */ 0);
562 ERROR ("network plugin: gcry_cipher_open returned: %s",
563 gcry_strerror (err));
570 gcry_cipher_reset (*cyper_ptr);
572 assert (*cyper_ptr != NULL);
574 err = gcry_cipher_setkey (*cyper_ptr,
575 password_hash, sizeof (password_hash));
578 ERROR ("network plugin: gcry_cipher_setkey returned: %s",
579 gcry_strerror (err));
580 gcry_cipher_close (*cyper_ptr);
585 err = gcry_cipher_setiv (*cyper_ptr, iv, iv_size);
588 ERROR ("network plugin: gcry_cipher_setkey returned: %s",
589 gcry_strerror (err));
590 gcry_cipher_close (*cyper_ptr);
596 } /* }}} int network_get_aes256_cypher */
597 #endif /* HAVE_LIBGCRYPT */
599 static int write_part_values (char **ret_buffer, int *ret_buffer_len,
600 const data_set_t *ds, const value_list_t *vl)
606 part_header_t pkg_ph;
607 uint16_t pkg_num_values;
608 uint8_t *pkg_values_types;
614 num_values = vl->values_len;
615 packet_len = sizeof (part_header_t) + sizeof (uint16_t)
616 + (num_values * sizeof (uint8_t))
617 + (num_values * sizeof (value_t));
619 if (*ret_buffer_len < packet_len)
622 pkg_values_types = (uint8_t *) malloc (num_values * sizeof (uint8_t));
623 if (pkg_values_types == NULL)
625 ERROR ("network plugin: write_part_values: malloc failed.");
629 pkg_values = (value_t *) malloc (num_values * sizeof (value_t));
630 if (pkg_values == NULL)
632 free (pkg_values_types);
633 ERROR ("network plugin: write_part_values: malloc failed.");
637 pkg_ph.type = htons (TYPE_VALUES);
638 pkg_ph.length = htons (packet_len);
640 pkg_num_values = htons ((uint16_t) vl->values_len);
642 for (i = 0; i < num_values; i++)
644 pkg_values_types[i] = (uint8_t) ds->ds[i].type;
645 switch (ds->ds[i].type)
647 case DS_TYPE_COUNTER:
648 pkg_values[i].counter = htonll (vl->values[i].counter);
652 pkg_values[i].gauge = htond (vl->values[i].gauge);
656 pkg_values[i].derive = htonll (vl->values[i].derive);
659 case DS_TYPE_ABSOLUTE:
660 pkg_values[i].absolute = htonll (vl->values[i].absolute);
664 free (pkg_values_types);
666 ERROR ("network plugin: write_part_values: "
667 "Unknown data source type: %i",
670 } /* switch (ds->ds[i].type) */
671 } /* for (num_values) */
674 * Use `memcpy' to write everything to the buffer, because the pointer
675 * may be unaligned and some architectures, such as SPARC, can't handle
678 packet_ptr = *ret_buffer;
680 memcpy (packet_ptr + offset, &pkg_ph, sizeof (pkg_ph));
681 offset += sizeof (pkg_ph);
682 memcpy (packet_ptr + offset, &pkg_num_values, sizeof (pkg_num_values));
683 offset += sizeof (pkg_num_values);
684 memcpy (packet_ptr + offset, pkg_values_types, num_values * sizeof (uint8_t));
685 offset += num_values * sizeof (uint8_t);
686 memcpy (packet_ptr + offset, pkg_values, num_values * sizeof (value_t));
687 offset += num_values * sizeof (value_t);
689 assert (offset == packet_len);
691 *ret_buffer = packet_ptr + packet_len;
692 *ret_buffer_len -= packet_len;
694 free (pkg_values_types);
698 } /* int write_part_values */
700 static int write_part_number (char **ret_buffer, int *ret_buffer_len,
701 int type, uint64_t value)
706 part_header_t pkg_head;
711 packet_len = sizeof (pkg_head) + sizeof (pkg_value);
713 if (*ret_buffer_len < packet_len)
716 pkg_head.type = htons (type);
717 pkg_head.length = htons (packet_len);
718 pkg_value = htonll (value);
720 packet_ptr = *ret_buffer;
722 memcpy (packet_ptr + offset, &pkg_head, sizeof (pkg_head));
723 offset += sizeof (pkg_head);
724 memcpy (packet_ptr + offset, &pkg_value, sizeof (pkg_value));
725 offset += sizeof (pkg_value);
727 assert (offset == packet_len);
729 *ret_buffer = packet_ptr + packet_len;
730 *ret_buffer_len -= packet_len;
733 } /* int write_part_number */
735 static int write_part_string (char **ret_buffer, int *ret_buffer_len,
736 int type, const char *str, int str_len)
746 buffer_len = 2 * sizeof (uint16_t) + str_len + 1;
747 if (*ret_buffer_len < buffer_len)
750 pkg_type = htons (type);
751 pkg_length = htons (buffer_len);
753 buffer = *ret_buffer;
755 memcpy (buffer + offset, (void *) &pkg_type, sizeof (pkg_type));
756 offset += sizeof (pkg_type);
757 memcpy (buffer + offset, (void *) &pkg_length, sizeof (pkg_length));
758 offset += sizeof (pkg_length);
759 memcpy (buffer + offset, str, str_len);
761 memset (buffer + offset, '\0', 1);
764 assert (offset == buffer_len);
766 *ret_buffer = buffer + buffer_len;
767 *ret_buffer_len -= buffer_len;
770 } /* int write_part_string */
772 static int parse_part_values (void **ret_buffer, size_t *ret_buffer_len,
773 value_t **ret_values, int *ret_num_values)
775 char *buffer = *ret_buffer;
776 size_t buffer_len = *ret_buffer_len;
791 NOTICE ("network plugin: packet is too short: "
792 "buffer_len = %zu", buffer_len);
796 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
797 buffer += sizeof (tmp16);
798 pkg_type = ntohs (tmp16);
800 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
801 buffer += sizeof (tmp16);
802 pkg_length = ntohs (tmp16);
804 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
805 buffer += sizeof (tmp16);
806 pkg_numval = ntohs (tmp16);
808 assert (pkg_type == TYPE_VALUES);
810 exp_size = 3 * sizeof (uint16_t)
811 + pkg_numval * (sizeof (uint8_t) + sizeof (value_t));
812 if (buffer_len < exp_size)
814 WARNING ("network plugin: parse_part_values: "
816 "Chunk of size %zu expected, "
817 "but buffer has only %zu bytes left.",
818 exp_size, buffer_len);
822 if (pkg_length != exp_size)
824 WARNING ("network plugin: parse_part_values: "
825 "Length and number of values "
826 "in the packet don't match.");
830 pkg_types = (uint8_t *) malloc (pkg_numval * sizeof (uint8_t));
831 pkg_values = (value_t *) malloc (pkg_numval * sizeof (value_t));
832 if ((pkg_types == NULL) || (pkg_values == NULL))
836 ERROR ("network plugin: parse_part_values: malloc failed.");
840 memcpy ((void *) pkg_types, (void *) buffer, pkg_numval * sizeof (uint8_t));
841 buffer += pkg_numval * sizeof (uint8_t);
842 memcpy ((void *) pkg_values, (void *) buffer, pkg_numval * sizeof (value_t));
843 buffer += pkg_numval * sizeof (value_t);
845 for (i = 0; i < pkg_numval; i++)
847 switch (pkg_types[i])
849 case DS_TYPE_COUNTER:
850 pkg_values[i].counter = (counter_t) ntohll (pkg_values[i].counter);
854 pkg_values[i].gauge = (gauge_t) ntohd (pkg_values[i].gauge);
858 pkg_values[i].derive = (derive_t) ntohll (pkg_values[i].derive);
861 case DS_TYPE_ABSOLUTE:
862 pkg_values[i].absolute = (absolute_t) ntohll (pkg_values[i].absolute);
866 NOTICE ("network plugin: parse_part_values: "
867 "Don't know how to handle data source type %"PRIu8,
872 } /* switch (pkg_types[i]) */
875 *ret_buffer = buffer;
876 *ret_buffer_len = buffer_len - pkg_length;
877 *ret_num_values = pkg_numval;
878 *ret_values = pkg_values;
883 } /* int parse_part_values */
885 static int parse_part_number (void **ret_buffer, size_t *ret_buffer_len,
888 char *buffer = *ret_buffer;
889 size_t buffer_len = *ret_buffer_len;
893 size_t exp_size = 2 * sizeof (uint16_t) + sizeof (uint64_t);
897 if (buffer_len < exp_size)
899 WARNING ("network plugin: parse_part_number: "
901 "Chunk of size %zu expected, "
902 "but buffer has only %zu bytes left.",
903 exp_size, buffer_len);
907 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
908 buffer += sizeof (tmp16);
909 /* pkg_type = ntohs (tmp16); */
911 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
912 buffer += sizeof (tmp16);
913 pkg_length = ntohs (tmp16);
915 memcpy ((void *) &tmp64, buffer, sizeof (tmp64));
916 buffer += sizeof (tmp64);
917 *value = ntohll (tmp64);
919 *ret_buffer = buffer;
920 *ret_buffer_len = buffer_len - pkg_length;
923 } /* int parse_part_number */
925 static int parse_part_string (void **ret_buffer, size_t *ret_buffer_len,
926 char *output, size_t const output_len)
928 char *buffer = *ret_buffer;
929 size_t buffer_len = *ret_buffer_len;
932 size_t const header_size = 2 * sizeof (uint16_t);
940 if (buffer_len < header_size)
942 WARNING ("network plugin: parse_part_string: "
944 "Chunk of at least size %zu expected, "
945 "but buffer has only %zu bytes left.",
946 header_size, buffer_len);
950 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
951 buffer += sizeof (tmp16);
952 /* pkg_type = ntohs (tmp16); */
954 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
955 buffer += sizeof (tmp16);
956 pkg_length = ntohs (tmp16);
957 payload_size = ((size_t) pkg_length) - header_size;
959 /* Check that packet fits in the input buffer */
960 if (pkg_length > buffer_len)
962 WARNING ("network plugin: parse_part_string: "
964 "Chunk of size %"PRIu16" received, "
965 "but buffer has only %zu bytes left.",
966 pkg_length, buffer_len);
970 /* Check that pkg_length is in the valid range */
971 if (pkg_length <= header_size)
973 WARNING ("network plugin: parse_part_string: "
975 "Header claims this packet is only %hu "
976 "bytes long.", pkg_length);
980 /* Check that the package data fits into the output buffer.
981 * The previous if-statement ensures that:
982 * `pkg_length > header_size' */
983 if (output_len < payload_size)
985 WARNING ("network plugin: parse_part_string: "
987 "Output buffer holds %zu bytes, "
988 "which is too small to hold the received "
990 output_len, payload_size);
994 /* All sanity checks successfull, let's copy the data over */
995 memcpy ((void *) output, (void *) buffer, payload_size);
996 buffer += payload_size;
998 /* For some very weird reason '\0' doesn't do the trick on SPARC in
1000 if (output[payload_size - 1] != 0)
1002 WARNING ("network plugin: parse_part_string: "
1003 "Received string does not end "
1004 "with a NULL-byte.");
1008 *ret_buffer = buffer;
1009 *ret_buffer_len = buffer_len - pkg_length;
1012 } /* int parse_part_string */
1014 /* Forward declaration: parse_part_sign_sha256 and parse_part_encr_aes256 call
1015 * parse_packet and vice versa. */
1016 #define PP_SIGNED 0x01
1017 #define PP_ENCRYPTED 0x02
1018 static int parse_packet (sockent_t *se,
1019 void *buffer, size_t buffer_size, int flags,
1020 const char *username);
1022 #define BUFFER_READ(p,s) do { \
1023 memcpy ((p), buffer + buffer_offset, (s)); \
1024 buffer_offset += (s); \
1028 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
1029 void **ret_buffer, size_t *ret_buffer_len, int flags)
1031 static c_complain_t complain_no_users = C_COMPLAIN_INIT_STATIC;
1035 size_t buffer_offset;
1037 size_t username_len;
1040 part_signature_sha256_t pss;
1041 uint16_t pss_head_length;
1042 char hash[sizeof (pss.hash)];
1046 unsigned char *hash_ptr;
1048 buffer = *ret_buffer;
1049 buffer_len = *ret_buffer_len;
1052 if (se->data.server.userdb == NULL)
1054 c_complain (LOG_NOTICE, &complain_no_users,
1055 "network plugin: Received signed network packet but can't verify it "
1056 "because no user DB has been configured. Will accept it.");
1060 /* Check if the buffer has enough data for this structure. */
1061 if (buffer_len <= PART_SIGNATURE_SHA256_SIZE)
1064 /* Read type and length header */
1065 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
1066 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
1067 pss_head_length = ntohs (pss.head.length);
1069 /* Check if the `pss_head_length' is within bounds. */
1070 if ((pss_head_length <= PART_SIGNATURE_SHA256_SIZE)
1071 || (pss_head_length > buffer_len))
1073 ERROR ("network plugin: HMAC-SHA-256 with invalid length received.");
1077 /* Copy the hash. */
1078 BUFFER_READ (pss.hash, sizeof (pss.hash));
1080 /* Calculate username length (without null byte) and allocate memory */
1081 username_len = pss_head_length - PART_SIGNATURE_SHA256_SIZE;
1082 pss.username = malloc (username_len + 1);
1083 if (pss.username == NULL)
1086 /* Read the username */
1087 BUFFER_READ (pss.username, username_len);
1088 pss.username[username_len] = 0;
1090 assert (buffer_offset == pss_head_length);
1092 /* Query the password */
1093 secret = fbh_get (se->data.server.userdb, pss.username);
1096 ERROR ("network plugin: Unknown user: %s", pss.username);
1097 sfree (pss.username);
1101 /* Create a hash device and check the HMAC */
1103 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
1106 ERROR ("network plugin: Creating HMAC-SHA-256 object failed: %s",
1107 gcry_strerror (err));
1109 sfree (pss.username);
1113 err = gcry_md_setkey (hd, secret, strlen (secret));
1116 ERROR ("network plugin: gcry_md_setkey failed: %s", gcry_strerror (err));
1119 sfree (pss.username);
1124 buffer + PART_SIGNATURE_SHA256_SIZE,
1125 buffer_len - PART_SIGNATURE_SHA256_SIZE);
1126 hash_ptr = gcry_md_read (hd, GCRY_MD_SHA256);
1127 if (hash_ptr == NULL)
1129 ERROR ("network plugin: gcry_md_read failed.");
1132 sfree (pss.username);
1135 memcpy (hash, hash_ptr, sizeof (hash));
1141 if (memcmp (pss.hash, hash, sizeof (pss.hash)) != 0)
1143 WARNING ("network plugin: Verifying HMAC-SHA-256 signature failed: "
1148 parse_packet (se, buffer + buffer_offset, buffer_len - buffer_offset,
1149 flags | PP_SIGNED, pss.username);
1153 sfree (pss.username);
1155 *ret_buffer = buffer + buffer_len;
1156 *ret_buffer_len = 0;
1159 } /* }}} int parse_part_sign_sha256 */
1160 /* #endif HAVE_LIBGCRYPT */
1162 #else /* if !HAVE_LIBGCRYPT */
1163 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
1164 void **ret_buffer, size_t *ret_buffer_size, int flags)
1166 static int warning_has_been_printed = 0;
1170 size_t buffer_offset;
1173 part_signature_sha256_t pss;
1175 buffer = *ret_buffer;
1176 buffer_size = *ret_buffer_size;
1179 if (buffer_size <= PART_SIGNATURE_SHA256_SIZE)
1182 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
1183 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
1184 part_len = ntohs (pss.head.length);
1186 if ((part_len <= PART_SIGNATURE_SHA256_SIZE)
1187 || (part_len > buffer_size))
1190 if (warning_has_been_printed == 0)
1192 WARNING ("network plugin: Received signed packet, but the network "
1193 "plugin was not linked with libgcrypt, so I cannot "
1194 "verify the signature. The packet will be accepted.");
1195 warning_has_been_printed = 1;
1198 parse_packet (se, buffer + part_len, buffer_size - part_len, flags,
1199 /* username = */ NULL);
1201 *ret_buffer = buffer + buffer_size;
1202 *ret_buffer_size = 0;
1205 } /* }}} int parse_part_sign_sha256 */
1206 #endif /* !HAVE_LIBGCRYPT */
1209 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1210 void **ret_buffer, size_t *ret_buffer_len,
1213 char *buffer = *ret_buffer;
1214 size_t buffer_len = *ret_buffer_len;
1217 size_t buffer_offset;
1218 uint16_t username_len;
1219 part_encryption_aes256_t pea;
1220 unsigned char hash[sizeof (pea.hash)];
1222 gcry_cipher_hd_t cypher;
1225 /* Make sure at least the header if available. */
1226 if (buffer_len <= PART_ENCRYPTION_AES256_SIZE)
1228 NOTICE ("network plugin: parse_part_encr_aes256: "
1229 "Discarding short packet.");
1235 /* Copy the unencrypted information into `pea'. */
1236 BUFFER_READ (&pea.head.type, sizeof (pea.head.type));
1237 BUFFER_READ (&pea.head.length, sizeof (pea.head.length));
1239 /* Check the `part size'. */
1240 part_size = ntohs (pea.head.length);
1241 if ((part_size <= PART_ENCRYPTION_AES256_SIZE)
1242 || (part_size > buffer_len))
1244 NOTICE ("network plugin: parse_part_encr_aes256: "
1245 "Discarding part with invalid size.");
1249 /* Read the username */
1250 BUFFER_READ (&username_len, sizeof (username_len));
1251 username_len = ntohs (username_len);
1253 if ((username_len <= 0)
1254 || (username_len > (part_size - (PART_ENCRYPTION_AES256_SIZE + 1))))
1256 NOTICE ("network plugin: parse_part_encr_aes256: "
1257 "Discarding part with invalid username length.");
1261 assert (username_len > 0);
1262 pea.username = malloc (username_len + 1);
1263 if (pea.username == NULL)
1265 BUFFER_READ (pea.username, username_len);
1266 pea.username[username_len] = 0;
1268 /* Last but not least, the initialization vector */
1269 BUFFER_READ (pea.iv, sizeof (pea.iv));
1271 /* Make sure we are at the right position */
1272 assert (buffer_offset == (username_len +
1273 PART_ENCRYPTION_AES256_SIZE - sizeof (pea.hash)));
1275 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
1279 sfree (pea.username);
1283 payload_len = part_size - (PART_ENCRYPTION_AES256_SIZE + username_len);
1284 assert (payload_len > 0);
1286 /* Decrypt the packet in-place */
1287 err = gcry_cipher_decrypt (cypher,
1288 buffer + buffer_offset,
1289 part_size - buffer_offset,
1290 /* in = */ NULL, /* in len = */ 0);
1293 sfree (pea.username);
1294 ERROR ("network plugin: gcry_cipher_decrypt returned: %s",
1295 gcry_strerror (err));
1300 BUFFER_READ (pea.hash, sizeof (pea.hash));
1302 /* Make sure we're at the right position - again */
1303 assert (buffer_offset == (username_len + PART_ENCRYPTION_AES256_SIZE));
1304 assert (buffer_offset == (part_size - payload_len));
1306 /* Check hash sum */
1307 memset (hash, 0, sizeof (hash));
1308 gcry_md_hash_buffer (GCRY_MD_SHA1, hash,
1309 buffer + buffer_offset, payload_len);
1310 if (memcmp (hash, pea.hash, sizeof (hash)) != 0)
1312 sfree (pea.username);
1313 ERROR ("network plugin: Decryption failed: Checksum mismatch.");
1317 parse_packet (se, buffer + buffer_offset, payload_len,
1318 flags | PP_ENCRYPTED, pea.username);
1320 /* XXX: Free pea.username?!? */
1322 /* Update return values */
1323 *ret_buffer = buffer + part_size;
1324 *ret_buffer_len = buffer_len - part_size;
1326 sfree (pea.username);
1329 } /* }}} int parse_part_encr_aes256 */
1330 /* #endif HAVE_LIBGCRYPT */
1332 #else /* if !HAVE_LIBGCRYPT */
1333 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1334 void **ret_buffer, size_t *ret_buffer_size, int flags)
1336 static int warning_has_been_printed = 0;
1340 size_t buffer_offset;
1345 buffer = *ret_buffer;
1346 buffer_size = *ret_buffer_size;
1349 /* parse_packet assures this minimum size. */
1350 assert (buffer_size >= (sizeof (ph.type) + sizeof (ph.length)));
1352 BUFFER_READ (&ph.type, sizeof (ph.type));
1353 BUFFER_READ (&ph.length, sizeof (ph.length));
1354 ph_length = ntohs (ph.length);
1356 if ((ph_length <= PART_ENCRYPTION_AES256_SIZE)
1357 || (ph_length > buffer_size))
1359 ERROR ("network plugin: AES-256 encrypted part "
1360 "with invalid length received.");
1364 if (warning_has_been_printed == 0)
1366 WARNING ("network plugin: Received encrypted packet, but the network "
1367 "plugin was not linked with libgcrypt, so I cannot "
1368 "decrypt it. The part will be discarded.");
1369 warning_has_been_printed = 1;
1372 *ret_buffer += ph_length;
1373 *ret_buffer_size -= ph_length;
1376 } /* }}} int parse_part_encr_aes256 */
1377 #endif /* !HAVE_LIBGCRYPT */
1381 static int parse_packet (sockent_t *se, /* {{{ */
1382 void *buffer, size_t buffer_size, int flags,
1383 const char *username)
1387 value_list_t vl = VALUE_LIST_INIT;
1391 int packet_was_signed = (flags & PP_SIGNED);
1392 int packet_was_encrypted = (flags & PP_ENCRYPTED);
1393 int printed_ignore_warning = 0;
1394 #endif /* HAVE_LIBGCRYPT */
1397 memset (&vl, '\0', sizeof (vl));
1398 memset (&n, '\0', sizeof (n));
1401 while ((status == 0) && (0 < buffer_size)
1402 && ((unsigned int) buffer_size > sizeof (part_header_t)))
1404 uint16_t pkg_length;
1407 memcpy ((void *) &pkg_type,
1410 memcpy ((void *) &pkg_length,
1411 (void *) (buffer + sizeof (pkg_type)),
1412 sizeof (pkg_length));
1414 pkg_length = ntohs (pkg_length);
1415 pkg_type = ntohs (pkg_type);
1417 if (pkg_length > buffer_size)
1419 /* Ensure that this loop terminates eventually */
1420 if (pkg_length < (2 * sizeof (uint16_t)))
1423 if (pkg_type == TYPE_ENCR_AES256)
1425 status = parse_part_encr_aes256 (se,
1426 &buffer, &buffer_size, flags);
1429 ERROR ("network plugin: Decrypting AES256 "
1431 "with status %i.", status);
1436 else if ((se->data.server.security_level == SECURITY_LEVEL_ENCRYPT)
1437 && (packet_was_encrypted == 0))
1439 if (printed_ignore_warning == 0)
1441 INFO ("network plugin: Unencrypted packet or "
1442 "part has been ignored.");
1443 printed_ignore_warning = 1;
1445 buffer = ((char *) buffer) + pkg_length;
1448 #endif /* HAVE_LIBGCRYPT */
1449 else if (pkg_type == TYPE_SIGN_SHA256)
1451 status = parse_part_sign_sha256 (se,
1452 &buffer, &buffer_size, flags);
1455 ERROR ("network plugin: Verifying HMAC-SHA-256 "
1457 "with status %i.", status);
1462 else if ((se->data.server.security_level == SECURITY_LEVEL_SIGN)
1463 && (packet_was_encrypted == 0)
1464 && (packet_was_signed == 0))
1466 if (printed_ignore_warning == 0)
1468 INFO ("network plugin: Unsigned packet or "
1469 "part has been ignored.");
1470 printed_ignore_warning = 1;
1472 buffer = ((char *) buffer) + pkg_length;
1475 #endif /* HAVE_LIBGCRYPT */
1476 else if (pkg_type == TYPE_VALUES)
1478 status = parse_part_values (&buffer, &buffer_size,
1479 &vl.values, &vl.values_len);
1483 network_dispatch_values (&vl, username);
1487 else if (pkg_type == TYPE_TIME)
1490 status = parse_part_number (&buffer, &buffer_size,
1494 vl.time = TIME_T_TO_CDTIME_T (tmp);
1495 n.time = TIME_T_TO_CDTIME_T (tmp);
1498 else if (pkg_type == TYPE_TIME_HR)
1501 status = parse_part_number (&buffer, &buffer_size,
1505 vl.time = (cdtime_t) tmp;
1506 n.time = (cdtime_t) tmp;
1509 else if (pkg_type == TYPE_INTERVAL)
1512 status = parse_part_number (&buffer, &buffer_size,
1515 vl.interval = TIME_T_TO_CDTIME_T (tmp);
1517 else if (pkg_type == TYPE_INTERVAL_HR)
1520 status = parse_part_number (&buffer, &buffer_size,
1523 vl.interval = (cdtime_t) tmp;
1525 else if (pkg_type == TYPE_HOST)
1527 status = parse_part_string (&buffer, &buffer_size,
1528 vl.host, sizeof (vl.host));
1530 sstrncpy (n.host, vl.host, sizeof (n.host));
1532 else if (pkg_type == TYPE_PLUGIN)
1534 status = parse_part_string (&buffer, &buffer_size,
1535 vl.plugin, sizeof (vl.plugin));
1537 sstrncpy (n.plugin, vl.plugin,
1540 else if (pkg_type == TYPE_PLUGIN_INSTANCE)
1542 status = parse_part_string (&buffer, &buffer_size,
1544 sizeof (vl.plugin_instance));
1546 sstrncpy (n.plugin_instance,
1548 sizeof (n.plugin_instance));
1550 else if (pkg_type == TYPE_TYPE)
1552 status = parse_part_string (&buffer, &buffer_size,
1553 vl.type, sizeof (vl.type));
1555 sstrncpy (n.type, vl.type, sizeof (n.type));
1557 else if (pkg_type == TYPE_TYPE_INSTANCE)
1559 status = parse_part_string (&buffer, &buffer_size,
1561 sizeof (vl.type_instance));
1563 sstrncpy (n.type_instance, vl.type_instance,
1564 sizeof (n.type_instance));
1566 else if (pkg_type == TYPE_MESSAGE)
1568 status = parse_part_string (&buffer, &buffer_size,
1569 n.message, sizeof (n.message));
1575 else if ((n.severity != NOTIF_FAILURE)
1576 && (n.severity != NOTIF_WARNING)
1577 && (n.severity != NOTIF_OKAY))
1579 INFO ("network plugin: "
1580 "Ignoring notification with "
1581 "unknown severity %i.",
1584 else if (n.time <= 0)
1586 INFO ("network plugin: "
1587 "Ignoring notification with "
1590 else if (strlen (n.message) <= 0)
1592 INFO ("network plugin: "
1593 "Ignoring notification with "
1594 "an empty message.");
1598 network_dispatch_notification (&n);
1601 else if (pkg_type == TYPE_SEVERITY)
1604 status = parse_part_number (&buffer, &buffer_size,
1607 n.severity = (int) tmp;
1611 DEBUG ("network plugin: parse_packet: Unknown part"
1612 " type: 0x%04hx", pkg_type);
1613 buffer = ((char *) buffer) + pkg_length;
1615 } /* while (buffer_size > sizeof (part_header_t)) */
1617 if (status == 0 && buffer_size > 0)
1618 WARNING ("network plugin: parse_packet: Received truncated "
1619 "packet, try increasing `MaxPacketSize'");
1622 } /* }}} int parse_packet */
1624 static void free_sockent_client (struct sockent_client *sec) /* {{{ */
1633 sfree (sec->username);
1634 sfree (sec->password);
1635 if (sec->cypher != NULL)
1636 gcry_cipher_close (sec->cypher);
1638 } /* }}} void free_sockent_client */
1640 static void free_sockent_server (struct sockent_server *ses) /* {{{ */
1644 for (i = 0; i < ses->fd_num; i++)
1646 if (ses->fd[i] >= 0)
1655 sfree (ses->auth_file);
1656 fbh_destroy (ses->userdb);
1657 if (ses->cypher != NULL)
1658 gcry_cipher_close (ses->cypher);
1660 } /* }}} void free_sockent_server */
1662 static void sockent_destroy (sockent_t *se) /* {{{ */
1666 DEBUG ("network plugin: sockent_destroy (se = %p);", (void *) se);
1673 sfree (se->service);
1675 if (se->type == SOCKENT_TYPE_CLIENT)
1676 free_sockent_client (&se->data.client);
1678 free_sockent_server (&se->data.server);
1683 } /* }}} void sockent_destroy */
1686 * int network_set_ttl
1688 * Set the `IP_MULTICAST_TTL', `IP_TTL', `IPV6_MULTICAST_HOPS' or
1689 * `IPV6_UNICAST_HOPS', depending on which option is applicable.
1691 * The `struct addrinfo' is used to destinguish between unicast and multicast
1694 static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
1696 DEBUG ("network plugin: network_set_ttl: network_config_ttl = %i;",
1697 network_config_ttl);
1699 assert (se->type == SOCKENT_TYPE_CLIENT);
1701 if ((network_config_ttl < 1) || (network_config_ttl > 255))
1704 if (ai->ai_family == AF_INET)
1706 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1709 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1710 optname = IP_MULTICAST_TTL;
1714 if (setsockopt (se->data.client.fd, IPPROTO_IP, optname,
1715 &network_config_ttl,
1716 sizeof (network_config_ttl)) != 0)
1719 ERROR ("network plugin: setsockopt (ipv4-ttl): %s",
1720 sstrerror (errno, errbuf, sizeof (errbuf)));
1724 else if (ai->ai_family == AF_INET6)
1726 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1727 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1730 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1731 optname = IPV6_MULTICAST_HOPS;
1733 optname = IPV6_UNICAST_HOPS;
1735 if (setsockopt (se->data.client.fd, IPPROTO_IPV6, optname,
1736 &network_config_ttl,
1737 sizeof (network_config_ttl)) != 0)
1740 ERROR ("network plugin: setsockopt(ipv6-ttl): %s",
1741 sstrerror (errno, errbuf,
1748 } /* int network_set_ttl */
1750 static int network_set_interface (const sockent_t *se, const struct addrinfo *ai) /* {{{ */
1752 DEBUG ("network plugin: network_set_interface: interface index = %i;",
1755 assert (se->type == SOCKENT_TYPE_CLIENT);
1757 if (ai->ai_family == AF_INET)
1759 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1761 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1763 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1764 /* If possible, use the "ip_mreqn" structure which has
1765 * an "interface index" member. Using the interface
1766 * index is preferred here, because of its similarity
1767 * to the way IPv6 handles this. Unfortunately, it
1768 * appears not to be portable. */
1769 struct ip_mreqn mreq;
1771 memset (&mreq, 0, sizeof (mreq));
1772 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1773 mreq.imr_address.s_addr = ntohl (INADDR_ANY);
1774 mreq.imr_ifindex = se->interface;
1776 struct ip_mreq mreq;
1778 memset (&mreq, 0, sizeof (mreq));
1779 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1780 mreq.imr_interface.s_addr = ntohl (INADDR_ANY);
1783 if (setsockopt (se->data.client.fd, IPPROTO_IP, IP_MULTICAST_IF,
1784 &mreq, sizeof (mreq)) != 0)
1787 ERROR ("network plugin: setsockopt (ipv4-multicast-if): %s",
1788 sstrerror (errno, errbuf, sizeof (errbuf)));
1795 else if (ai->ai_family == AF_INET6)
1797 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1799 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1801 if (setsockopt (se->data.client.fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
1803 sizeof (se->interface)) != 0)
1806 ERROR ("network plugin: setsockopt (ipv6-multicast-if): %s",
1807 sstrerror (errno, errbuf,
1816 /* else: Not a multicast interface. */
1817 if (se->interface != 0)
1819 #if defined(HAVE_IF_INDEXTONAME) && HAVE_IF_INDEXTONAME && defined(SO_BINDTODEVICE)
1820 char interface_name[IFNAMSIZ];
1822 if (if_indextoname (se->interface, interface_name) == NULL)
1825 DEBUG ("network plugin: Binding socket to interface %s", interface_name);
1827 if (setsockopt (se->data.client.fd, SOL_SOCKET, SO_BINDTODEVICE,
1829 sizeof(interface_name)) == -1 )
1832 ERROR ("network plugin: setsockopt (bind-if): %s",
1833 sstrerror (errno, errbuf, sizeof (errbuf)));
1836 /* #endif HAVE_IF_INDEXTONAME && SO_BINDTODEVICE */
1839 WARNING ("network plugin: Cannot set the interface on a unicast "
1841 # if !defined(SO_BINDTODEVICE)
1842 "the \"SO_BINDTODEVICE\" socket option "
1844 "the \"if_indextoname\" function "
1846 "is not available on your system.");
1852 } /* }}} network_set_interface */
1854 static int network_bind_socket (int fd, const struct addrinfo *ai, const int interface_idx)
1863 /* allow multiple sockets to use the same PORT number */
1864 if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR,
1865 &yes, sizeof(yes)) == -1) {
1867 ERROR ("network plugin: setsockopt (reuseaddr): %s",
1868 sstrerror (errno, errbuf, sizeof (errbuf)));
1872 DEBUG ("fd = %i; calling `bind'", fd);
1874 if (bind (fd, ai->ai_addr, ai->ai_addrlen) == -1)
1878 sstrerror (errno, errbuf, sizeof (errbuf)));
1882 if (ai->ai_family == AF_INET)
1884 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1885 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1887 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1888 struct ip_mreqn mreq;
1890 struct ip_mreq mreq;
1893 DEBUG ("fd = %i; IPv4 multicast address found", fd);
1895 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1896 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1897 /* Set the interface using the interface index if
1898 * possible (available). Unfortunately, the struct
1899 * ip_mreqn is not portable. */
1900 mreq.imr_address.s_addr = ntohl (INADDR_ANY);
1901 mreq.imr_ifindex = interface_idx;
1903 mreq.imr_interface.s_addr = ntohl (INADDR_ANY);
1906 if (setsockopt (fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1907 &loop, sizeof (loop)) == -1)
1910 ERROR ("network plugin: setsockopt (multicast-loop): %s",
1911 sstrerror (errno, errbuf,
1916 if (setsockopt (fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1917 &mreq, sizeof (mreq)) == -1)
1920 ERROR ("network plugin: setsockopt (add-membership): %s",
1921 sstrerror (errno, errbuf,
1929 else if (ai->ai_family == AF_INET6)
1931 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1932 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1933 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1935 struct ipv6_mreq mreq;
1937 DEBUG ("fd = %i; IPv6 multicast address found", fd);
1939 memcpy (&mreq.ipv6mr_multiaddr,
1941 sizeof (addr->sin6_addr));
1943 /* http://developer.apple.com/documentation/Darwin/Reference/ManPages/man4/ip6.4.html
1944 * ipv6mr_interface may be set to zeroes to
1945 * choose the default multicast interface or to
1946 * the index of a particular multicast-capable
1947 * interface if the host is multihomed.
1948 * Membership is associ-associated with a
1949 * single interface; programs running on
1950 * multihomed hosts may need to join the same
1951 * group on more than one interface.*/
1952 mreq.ipv6mr_interface = interface_idx;
1954 if (setsockopt (fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
1955 &loop, sizeof (loop)) == -1)
1958 ERROR ("network plugin: setsockopt (ipv6-multicast-loop): %s",
1959 sstrerror (errno, errbuf,
1964 if (setsockopt (fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
1965 &mreq, sizeof (mreq)) == -1)
1968 ERROR ("network plugin: setsockopt (ipv6-add-membership): %s",
1969 sstrerror (errno, errbuf,
1978 #if defined(HAVE_IF_INDEXTONAME) && HAVE_IF_INDEXTONAME && defined(SO_BINDTODEVICE)
1979 /* if a specific interface was set, bind the socket to it. But to avoid
1980 * possible problems with multicast routing, only do that for non-multicast
1982 if (interface_idx != 0)
1984 char interface_name[IFNAMSIZ];
1986 if (if_indextoname (interface_idx, interface_name) == NULL)
1989 DEBUG ("fd = %i; Binding socket to interface %s", fd, interface_name);
1991 if (setsockopt (fd, SOL_SOCKET, SO_BINDTODEVICE,
1993 sizeof(interface_name)) == -1 )
1996 ERROR ("network plugin: setsockopt (bind-if): %s",
1997 sstrerror (errno, errbuf, sizeof (errbuf)));
2001 #endif /* HAVE_IF_INDEXTONAME && SO_BINDTODEVICE */
2004 } /* int network_bind_socket */
2006 /* Initialize a sockent structure. `type' must be either `SOCKENT_TYPE_CLIENT'
2007 * or `SOCKENT_TYPE_SERVER' */
2008 static sockent_t *sockent_create (int type) /* {{{ */
2012 if ((type != SOCKENT_TYPE_CLIENT) && (type != SOCKENT_TYPE_SERVER))
2015 se = malloc (sizeof (*se));
2018 memset (se, 0, sizeof (*se));
2026 if (type == SOCKENT_TYPE_SERVER)
2028 se->data.server.fd = NULL;
2030 se->data.server.security_level = SECURITY_LEVEL_NONE;
2031 se->data.server.auth_file = NULL;
2032 se->data.server.userdb = NULL;
2033 se->data.server.cypher = NULL;
2038 se->data.client.fd = -1;
2039 se->data.client.addr = NULL;
2040 se->data.client.resolve_interval = 0;
2041 se->data.client.next_resolve_reconnect = 0;
2043 se->data.client.security_level = SECURITY_LEVEL_NONE;
2044 se->data.client.username = NULL;
2045 se->data.client.password = NULL;
2046 se->data.client.cypher = NULL;
2051 } /* }}} sockent_t *sockent_create */
2053 static int sockent_init_crypto (sockent_t *se) /* {{{ */
2055 #if HAVE_LIBGCRYPT /* {{{ */
2056 if (se->type == SOCKENT_TYPE_CLIENT)
2058 if (se->data.client.security_level > SECURITY_LEVEL_NONE)
2060 network_init_gcrypt ();
2062 if ((se->data.client.username == NULL)
2063 || (se->data.client.password == NULL))
2065 ERROR ("network plugin: Client socket with "
2066 "security requested, but no "
2067 "credentials are configured.");
2070 gcry_md_hash_buffer (GCRY_MD_SHA256,
2071 se->data.client.password_hash,
2072 se->data.client.password,
2073 strlen (se->data.client.password));
2076 else /* (se->type == SOCKENT_TYPE_SERVER) */
2078 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
2080 network_init_gcrypt ();
2082 if (se->data.server.auth_file == NULL)
2084 ERROR ("network plugin: Server socket with "
2085 "security requested, but no "
2086 "password file is configured.");
2090 if (se->data.server.auth_file != NULL)
2092 se->data.server.userdb = fbh_create (se->data.server.auth_file);
2093 if (se->data.server.userdb == NULL)
2095 ERROR ("network plugin: Reading password file "
2097 se->data.server.auth_file);
2098 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
2103 #endif /* }}} HAVE_LIBGCRYPT */
2106 } /* }}} int sockent_init_crypto */
2108 static int sockent_client_disconnect (sockent_t *se) /* {{{ */
2110 struct sockent_client *client;
2112 if ((se == NULL) || (se->type != SOCKENT_TYPE_CLIENT))
2115 client = &se->data.client;
2116 if (client->fd >= 0) /* connected */
2122 sfree (client->addr);
2123 client->addrlen = 0;
2126 } /* }}} int sockent_client_disconnect */
2128 static int sockent_client_connect (sockent_t *se) /* {{{ */
2130 static c_complain_t complaint = C_COMPLAIN_INIT_STATIC;
2132 struct sockent_client *client;
2133 struct addrinfo ai_hints;
2134 struct addrinfo *ai_list = NULL, *ai_ptr;
2136 _Bool reconnect = 0;
2139 if ((se == NULL) || (se->type != SOCKENT_TYPE_CLIENT))
2142 client = &se->data.client;
2145 if (client->resolve_interval != 0 && client->next_resolve_reconnect < now) {
2146 DEBUG("network plugin: Reconnecting socket, resolve_interval = %lf, next_resolve_reconnect = %lf",
2147 CDTIME_T_TO_DOUBLE(client->resolve_interval), CDTIME_T_TO_DOUBLE(client->next_resolve_reconnect));
2151 if (client->fd >= 0 && !reconnect) /* already connected and not stale*/
2154 memset (&ai_hints, 0, sizeof (ai_hints));
2155 #ifdef AI_ADDRCONFIG
2156 ai_hints.ai_flags |= AI_ADDRCONFIG;
2158 ai_hints.ai_family = AF_UNSPEC;
2159 ai_hints.ai_socktype = SOCK_DGRAM;
2160 ai_hints.ai_protocol = IPPROTO_UDP;
2162 status = getaddrinfo (se->node,
2163 (se->service != NULL) ? se->service : NET_DEFAULT_PORT,
2164 &ai_hints, &ai_list);
2167 c_complain (LOG_ERR, &complaint,
2168 "network plugin: getaddrinfo (%s, %s) failed: %s",
2169 (se->node == NULL) ? "(null)" : se->node,
2170 (se->service == NULL) ? "(null)" : se->service,
2171 gai_strerror (status));
2176 c_release (LOG_NOTICE, &complaint,
2177 "network plugin: Successfully resolved \"%s\".",
2181 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
2183 if (client->fd >= 0) /* when we reconnect */
2184 sockent_client_disconnect(se);
2186 client->fd = socket (ai_ptr->ai_family,
2187 ai_ptr->ai_socktype,
2188 ai_ptr->ai_protocol);
2192 ERROR ("network plugin: socket(2) failed: %s",
2193 sstrerror (errno, errbuf,
2198 client->addr = malloc (sizeof (*client->addr));
2199 if (client->addr == NULL)
2201 ERROR ("network plugin: malloc failed.");
2207 memset (client->addr, 0, sizeof (*client->addr));
2208 assert (sizeof (*client->addr) >= ai_ptr->ai_addrlen);
2209 memcpy (client->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
2210 client->addrlen = ai_ptr->ai_addrlen;
2212 network_set_ttl (se, ai_ptr);
2213 network_set_interface (se, ai_ptr);
2215 /* We don't open more than one write-socket per
2216 * node/service pair.. */
2220 freeaddrinfo (ai_list);
2224 if (client->resolve_interval > 0)
2225 client->next_resolve_reconnect = now + client->resolve_interval;
2227 } /* }}} int sockent_client_connect */
2229 /* Open the file descriptors for a initialized sockent structure. */
2230 static int sockent_server_listen (sockent_t *se) /* {{{ */
2232 struct addrinfo ai_hints;
2233 struct addrinfo *ai_list, *ai_ptr;
2237 const char *service;
2243 service = se->service;
2245 if (service == NULL)
2246 service = NET_DEFAULT_PORT;
2248 DEBUG ("network plugin: sockent_server_listen: node = %s; service = %s;",
2251 memset (&ai_hints, 0, sizeof (ai_hints));
2252 ai_hints.ai_flags = 0;
2254 ai_hints.ai_flags |= AI_PASSIVE;
2256 #ifdef AI_ADDRCONFIG
2257 ai_hints.ai_flags |= AI_ADDRCONFIG;
2259 ai_hints.ai_family = AF_UNSPEC;
2260 ai_hints.ai_socktype = SOCK_DGRAM;
2261 ai_hints.ai_protocol = IPPROTO_UDP;
2263 status = getaddrinfo (node, service, &ai_hints, &ai_list);
2266 ERROR ("network plugin: getaddrinfo (%s, %s) failed: %s",
2267 (se->node == NULL) ? "(null)" : se->node,
2268 (se->service == NULL) ? "(null)" : se->service,
2269 gai_strerror (status));
2273 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
2277 tmp = realloc (se->data.server.fd,
2278 sizeof (*tmp) * (se->data.server.fd_num + 1));
2281 ERROR ("network plugin: realloc failed.");
2284 se->data.server.fd = tmp;
2285 tmp = se->data.server.fd + se->data.server.fd_num;
2287 *tmp = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
2288 ai_ptr->ai_protocol);
2292 ERROR ("network plugin: socket(2) failed: %s",
2293 sstrerror (errno, errbuf,
2298 status = network_bind_socket (*tmp, ai_ptr, se->interface);
2306 se->data.server.fd_num++;
2308 } /* for (ai_list) */
2310 freeaddrinfo (ai_list);
2312 if (se->data.server.fd_num <= 0)
2315 } /* }}} int sockent_server_listen */
2317 /* Add a sockent to the global list of sockets */
2318 static int sockent_add (sockent_t *se) /* {{{ */
2320 sockent_t *last_ptr;
2325 if (se->type == SOCKENT_TYPE_SERVER)
2330 tmp = realloc (listen_sockets_pollfd,
2331 sizeof (*tmp) * (listen_sockets_num
2332 + se->data.server.fd_num));
2335 ERROR ("network plugin: realloc failed.");
2338 listen_sockets_pollfd = tmp;
2339 tmp = listen_sockets_pollfd + listen_sockets_num;
2341 for (i = 0; i < se->data.server.fd_num; i++)
2343 memset (tmp + i, 0, sizeof (*tmp));
2344 tmp[i].fd = se->data.server.fd[i];
2345 tmp[i].events = POLLIN | POLLPRI;
2349 listen_sockets_num += se->data.server.fd_num;
2351 if (listen_sockets == NULL)
2353 listen_sockets = se;
2356 last_ptr = listen_sockets;
2358 else /* if (se->type == SOCKENT_TYPE_CLIENT) */
2360 if (sending_sockets == NULL)
2362 sending_sockets = se;
2365 last_ptr = sending_sockets;
2368 while (last_ptr->next != NULL)
2369 last_ptr = last_ptr->next;
2370 last_ptr->next = se;
2373 } /* }}} int sockent_add */
2375 static void *dispatch_thread (void __attribute__((unused)) *arg) /* {{{ */
2379 receive_list_entry_t *ent;
2382 /* Lock and wait for more data to come in */
2383 pthread_mutex_lock (&receive_list_lock);
2384 while ((listen_loop == 0)
2385 && (receive_list_head == NULL))
2386 pthread_cond_wait (&receive_list_cond, &receive_list_lock);
2388 /* Remove the head entry and unlock */
2389 ent = receive_list_head;
2391 receive_list_head = ent->next;
2392 receive_list_length--;
2393 pthread_mutex_unlock (&receive_list_lock);
2395 /* Check whether we are supposed to exit. We do NOT check `listen_loop'
2396 * because we dispatch all missing packets before shutting down. */
2400 /* Look for the correct `sockent_t' */
2401 se = listen_sockets;
2406 for (i = 0; i < se->data.server.fd_num; i++)
2407 if (se->data.server.fd[i] == ent->fd)
2410 if (i < se->data.server.fd_num)
2418 ERROR ("network plugin: Got packet from FD %i, but can't "
2419 "find an appropriate socket entry.",
2426 parse_packet (se, ent->data, ent->data_len, /* flags = */ 0,
2427 /* username = */ NULL);
2433 } /* }}} void *dispatch_thread */
2435 static int network_receive (void) /* {{{ */
2437 char buffer[network_config_packet_size];
2443 receive_list_entry_t *private_list_head;
2444 receive_list_entry_t *private_list_tail;
2445 uint64_t private_list_length;
2447 assert (listen_sockets_num > 0);
2449 private_list_head = NULL;
2450 private_list_tail = NULL;
2451 private_list_length = 0;
2453 while (listen_loop == 0)
2455 status = poll (listen_sockets_pollfd, listen_sockets_num, -1);
2462 ERROR ("poll failed: %s",
2463 sstrerror (errno, errbuf, sizeof (errbuf)));
2467 for (i = 0; (i < listen_sockets_num) && (status > 0); i++)
2469 receive_list_entry_t *ent;
2471 if ((listen_sockets_pollfd[i].revents
2472 & (POLLIN | POLLPRI)) == 0)
2476 buffer_len = recv (listen_sockets_pollfd[i].fd,
2477 buffer, sizeof (buffer),
2482 ERROR ("recv failed: %s",
2483 sstrerror (errno, errbuf,
2488 stats_octets_rx += ((uint64_t) buffer_len);
2491 /* TODO: Possible performance enhancement: Do not free
2492 * these entries in the dispatch thread but put them in
2493 * another list, so we don't have to allocate more and
2494 * more of these structures. */
2495 ent = malloc (sizeof (receive_list_entry_t));
2498 ERROR ("network plugin: malloc failed.");
2501 memset (ent, 0, sizeof (receive_list_entry_t));
2502 ent->data = malloc (network_config_packet_size);
2503 if (ent->data == NULL)
2506 ERROR ("network plugin: malloc failed.");
2509 ent->fd = listen_sockets_pollfd[i].fd;
2512 memcpy (ent->data, buffer, buffer_len);
2513 ent->data_len = buffer_len;
2515 if (private_list_head == NULL)
2516 private_list_head = ent;
2518 private_list_tail->next = ent;
2519 private_list_tail = ent;
2520 private_list_length++;
2522 /* Do not block here. Blocking here has led to
2523 * insufficient performance in the past. */
2524 if (pthread_mutex_trylock (&receive_list_lock) == 0)
2526 assert (((receive_list_head == NULL) && (receive_list_length == 0))
2527 || ((receive_list_head != NULL) && (receive_list_length != 0)));
2529 if (receive_list_head == NULL)
2530 receive_list_head = private_list_head;
2532 receive_list_tail->next = private_list_head;
2533 receive_list_tail = private_list_tail;
2534 receive_list_length += private_list_length;
2536 pthread_cond_signal (&receive_list_cond);
2537 pthread_mutex_unlock (&receive_list_lock);
2539 private_list_head = NULL;
2540 private_list_tail = NULL;
2541 private_list_length = 0;
2543 } /* for (listen_sockets_pollfd) */
2544 } /* while (listen_loop == 0) */
2546 /* Make sure everything is dispatched before exiting. */
2547 if (private_list_head != NULL)
2549 pthread_mutex_lock (&receive_list_lock);
2551 if (receive_list_head == NULL)
2552 receive_list_head = private_list_head;
2554 receive_list_tail->next = private_list_head;
2555 receive_list_tail = private_list_tail;
2556 receive_list_length += private_list_length;
2558 private_list_head = NULL;
2559 private_list_tail = NULL;
2560 private_list_length = 0;
2562 pthread_cond_signal (&receive_list_cond);
2563 pthread_mutex_unlock (&receive_list_lock);
2567 } /* }}} int network_receive */
2569 static void *receive_thread (void __attribute__((unused)) *arg)
2571 return (network_receive () ? (void *) 1 : (void *) 0);
2572 } /* void *receive_thread */
2574 static void network_init_buffer (void)
2576 memset (send_buffer, 0, network_config_packet_size);
2577 send_buffer_ptr = send_buffer;
2578 send_buffer_fill = 0;
2580 memset (&send_buffer_vl, 0, sizeof (send_buffer_vl));
2581 } /* int network_init_buffer */
2583 static void networt_send_buffer_plain (sockent_t *se, /* {{{ */
2584 const char *buffer, size_t buffer_size)
2590 status = sockent_client_connect (se);
2594 status = sendto (se->data.client.fd, buffer, buffer_size,
2596 (struct sockaddr *) se->data.client.addr,
2597 se->data.client.addrlen);
2602 if ((errno == EINTR) || (errno == EAGAIN))
2605 ERROR ("network plugin: sendto failed: %s. Closing sending socket.",
2606 sstrerror (errno, errbuf, sizeof (errbuf)));
2607 sockent_client_disconnect (se);
2613 } /* }}} void networt_send_buffer_plain */
2616 #define BUFFER_ADD(p,s) do { \
2617 memcpy (buffer + buffer_offset, (p), (s)); \
2618 buffer_offset += (s); \
2621 static void networt_send_buffer_signed (sockent_t *se, /* {{{ */
2622 const char *in_buffer, size_t in_buffer_size)
2624 part_signature_sha256_t ps;
2625 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2626 size_t buffer_offset;
2627 size_t username_len;
2631 unsigned char *hash;
2634 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
2637 ERROR ("network plugin: Creating HMAC object failed: %s",
2638 gcry_strerror (err));
2642 err = gcry_md_setkey (hd, se->data.client.password,
2643 strlen (se->data.client.password));
2646 ERROR ("network plugin: gcry_md_setkey failed: %s",
2647 gcry_strerror (err));
2652 username_len = strlen (se->data.client.username);
2653 if (username_len > (BUFF_SIG_SIZE - PART_SIGNATURE_SHA256_SIZE))
2655 ERROR ("network plugin: Username too long: %s",
2656 se->data.client.username);
2660 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE,
2661 se->data.client.username, username_len);
2662 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE + username_len,
2663 in_buffer, in_buffer_size);
2665 /* Initialize the `ps' structure. */
2666 memset (&ps, 0, sizeof (ps));
2667 ps.head.type = htons (TYPE_SIGN_SHA256);
2668 ps.head.length = htons (PART_SIGNATURE_SHA256_SIZE + username_len);
2670 /* Calculate the hash value. */
2671 gcry_md_write (hd, buffer + PART_SIGNATURE_SHA256_SIZE,
2672 username_len + in_buffer_size);
2673 hash = gcry_md_read (hd, GCRY_MD_SHA256);
2676 ERROR ("network plugin: gcry_md_read failed.");
2680 memcpy (ps.hash, hash, sizeof (ps.hash));
2682 /* Add the header */
2685 BUFFER_ADD (&ps.head.type, sizeof (ps.head.type));
2686 BUFFER_ADD (&ps.head.length, sizeof (ps.head.length));
2687 BUFFER_ADD (ps.hash, sizeof (ps.hash));
2689 assert (buffer_offset == PART_SIGNATURE_SHA256_SIZE);
2694 buffer_offset = PART_SIGNATURE_SHA256_SIZE + username_len + in_buffer_size;
2695 networt_send_buffer_plain (se, buffer, buffer_offset);
2696 } /* }}} void networt_send_buffer_signed */
2698 static void networt_send_buffer_encrypted (sockent_t *se, /* {{{ */
2699 const char *in_buffer, size_t in_buffer_size)
2701 part_encryption_aes256_t pea;
2702 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2704 size_t buffer_offset;
2706 size_t username_len;
2708 gcry_cipher_hd_t cypher;
2710 /* Initialize the header fields */
2711 memset (&pea, 0, sizeof (pea));
2712 pea.head.type = htons (TYPE_ENCR_AES256);
2714 pea.username = se->data.client.username;
2716 username_len = strlen (pea.username);
2717 if ((PART_ENCRYPTION_AES256_SIZE + username_len) > BUFF_SIG_SIZE)
2719 ERROR ("network plugin: Username too long: %s", pea.username);
2723 buffer_size = PART_ENCRYPTION_AES256_SIZE + username_len + in_buffer_size;
2724 header_size = PART_ENCRYPTION_AES256_SIZE + username_len
2725 - sizeof (pea.hash);
2727 assert (buffer_size <= sizeof (buffer));
2728 DEBUG ("network plugin: networt_send_buffer_encrypted: "
2729 "buffer_size = %zu;", buffer_size);
2731 pea.head.length = htons ((uint16_t) (PART_ENCRYPTION_AES256_SIZE
2732 + username_len + in_buffer_size));
2733 pea.username_length = htons ((uint16_t) username_len);
2735 /* Chose a random initialization vector. */
2736 gcry_randomize ((void *) &pea.iv, sizeof (pea.iv), GCRY_STRONG_RANDOM);
2738 /* Create hash of the payload */
2739 gcry_md_hash_buffer (GCRY_MD_SHA1, pea.hash, in_buffer, in_buffer_size);
2741 /* Initialize the buffer */
2743 memset (buffer, 0, sizeof (buffer));
2746 BUFFER_ADD (&pea.head.type, sizeof (pea.head.type));
2747 BUFFER_ADD (&pea.head.length, sizeof (pea.head.length));
2748 BUFFER_ADD (&pea.username_length, sizeof (pea.username_length));
2749 BUFFER_ADD (pea.username, username_len);
2750 BUFFER_ADD (pea.iv, sizeof (pea.iv));
2751 assert (buffer_offset == header_size);
2752 BUFFER_ADD (pea.hash, sizeof (pea.hash));
2753 BUFFER_ADD (in_buffer, in_buffer_size);
2755 assert (buffer_offset == buffer_size);
2757 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
2758 se->data.client.password);
2762 /* Encrypt the buffer in-place */
2763 err = gcry_cipher_encrypt (cypher,
2764 buffer + header_size,
2765 buffer_size - header_size,
2766 /* in = */ NULL, /* in len = */ 0);
2769 ERROR ("network plugin: gcry_cipher_encrypt returned: %s",
2770 gcry_strerror (err));
2774 /* Send it out without further modifications */
2775 networt_send_buffer_plain (se, buffer, buffer_size);
2776 } /* }}} void networt_send_buffer_encrypted */
2778 #endif /* HAVE_LIBGCRYPT */
2780 static void network_send_buffer (char *buffer, size_t buffer_len) /* {{{ */
2784 DEBUG ("network plugin: network_send_buffer: buffer_len = %zu", buffer_len);
2786 for (se = sending_sockets; se != NULL; se = se->next)
2789 if (se->data.client.security_level == SECURITY_LEVEL_ENCRYPT)
2790 networt_send_buffer_encrypted (se, buffer, buffer_len);
2791 else if (se->data.client.security_level == SECURITY_LEVEL_SIGN)
2792 networt_send_buffer_signed (se, buffer, buffer_len);
2793 else /* if (se->data.client.security_level == SECURITY_LEVEL_NONE) */
2794 #endif /* HAVE_LIBGCRYPT */
2795 networt_send_buffer_plain (se, buffer, buffer_len);
2796 } /* for (sending_sockets) */
2797 } /* }}} void network_send_buffer */
2799 static int add_to_buffer (char *buffer, int buffer_size, /* {{{ */
2800 value_list_t *vl_def,
2801 const data_set_t *ds, const value_list_t *vl)
2803 char *buffer_orig = buffer;
2805 if (strcmp (vl_def->host, vl->host) != 0)
2807 if (write_part_string (&buffer, &buffer_size, TYPE_HOST,
2808 vl->host, strlen (vl->host)) != 0)
2810 sstrncpy (vl_def->host, vl->host, sizeof (vl_def->host));
2813 if (vl_def->time != vl->time)
2815 if (write_part_number (&buffer, &buffer_size, TYPE_TIME_HR,
2816 (uint64_t) vl->time))
2818 vl_def->time = vl->time;
2821 if (vl_def->interval != vl->interval)
2823 if (write_part_number (&buffer, &buffer_size, TYPE_INTERVAL_HR,
2824 (uint64_t) vl->interval))
2826 vl_def->interval = vl->interval;
2829 if (strcmp (vl_def->plugin, vl->plugin) != 0)
2831 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN,
2832 vl->plugin, strlen (vl->plugin)) != 0)
2834 sstrncpy (vl_def->plugin, vl->plugin, sizeof (vl_def->plugin));
2837 if (strcmp (vl_def->plugin_instance, vl->plugin_instance) != 0)
2839 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
2840 vl->plugin_instance,
2841 strlen (vl->plugin_instance)) != 0)
2843 sstrncpy (vl_def->plugin_instance, vl->plugin_instance, sizeof (vl_def->plugin_instance));
2846 if (strcmp (vl_def->type, vl->type) != 0)
2848 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
2849 vl->type, strlen (vl->type)) != 0)
2851 sstrncpy (vl_def->type, ds->type, sizeof (vl_def->type));
2854 if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
2856 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE_INSTANCE,
2858 strlen (vl->type_instance)) != 0)
2860 sstrncpy (vl_def->type_instance, vl->type_instance, sizeof (vl_def->type_instance));
2863 if (write_part_values (&buffer, &buffer_size, ds, vl) != 0)
2866 return (buffer - buffer_orig);
2867 } /* }}} int add_to_buffer */
2869 static void flush_buffer (void)
2871 DEBUG ("network plugin: flush_buffer: send_buffer_fill = %i",
2874 network_send_buffer (send_buffer, (size_t) send_buffer_fill);
2876 stats_octets_tx += ((uint64_t) send_buffer_fill);
2879 network_init_buffer ();
2882 static int network_write (const data_set_t *ds, const value_list_t *vl,
2883 user_data_t __attribute__((unused)) *user_data)
2887 if (!check_send_okay (vl))
2890 char name[6*DATA_MAX_NAME_LEN];
2891 FORMAT_VL (name, sizeof (name), vl);
2892 name[sizeof (name) - 1] = 0;
2893 DEBUG ("network plugin: network_write: "
2894 "NOT sending %s.", name);
2896 /* Counter is not protected by another lock and may be reached by
2897 * multiple threads */
2898 pthread_mutex_lock (&stats_lock);
2899 stats_values_not_sent++;
2900 pthread_mutex_unlock (&stats_lock);
2904 uc_meta_data_add_unsigned_int (vl,
2905 "network:time_sent", (uint64_t) vl->time);
2907 pthread_mutex_lock (&send_buffer_lock);
2909 status = add_to_buffer (send_buffer_ptr,
2910 network_config_packet_size - (send_buffer_fill + BUFF_SIG_SIZE),
2915 /* status == bytes added to the buffer */
2916 send_buffer_fill += status;
2917 send_buffer_ptr += status;
2919 stats_values_sent++;
2925 status = add_to_buffer (send_buffer_ptr,
2926 network_config_packet_size - (send_buffer_fill + BUFF_SIG_SIZE),
2932 send_buffer_fill += status;
2933 send_buffer_ptr += status;
2935 stats_values_sent++;
2941 ERROR ("network plugin: Unable to append to the "
2942 "buffer for some weird reason");
2944 else if ((network_config_packet_size - send_buffer_fill) < 15)
2949 pthread_mutex_unlock (&send_buffer_lock);
2951 return ((status < 0) ? -1 : 0);
2952 } /* int network_write */
2954 static int network_config_set_boolean (const oconfig_item_t *ci, /* {{{ */
2957 if ((ci->values_num != 1)
2958 || ((ci->values[0].type != OCONFIG_TYPE_BOOLEAN)
2959 && (ci->values[0].type != OCONFIG_TYPE_STRING)))
2961 ERROR ("network plugin: The `%s' config option needs "
2962 "exactly one boolean argument.", ci->key);
2966 if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
2968 if (ci->values[0].value.boolean)
2975 char *str = ci->values[0].value.string;
2979 else if (IS_FALSE (str))
2983 ERROR ("network plugin: Cannot parse string value `%s' of the `%s' "
2984 "option as boolean value.",
2991 } /* }}} int network_config_set_boolean */
2993 static int network_config_set_ttl (const oconfig_item_t *ci) /* {{{ */
2996 if ((ci->values_num != 1)
2997 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
2999 WARNING ("network plugin: The `TimeToLive' config option needs exactly "
3000 "one numeric argument.");
3004 tmp = (int) ci->values[0].value.number;
3005 if ((tmp > 0) && (tmp <= 255))
3006 network_config_ttl = tmp;
3008 WARNING ("network plugin: The `TimeToLive' must be between 1 and 255.");
3013 } /* }}} int network_config_set_ttl */
3015 static int network_config_set_interface (const oconfig_item_t *ci, /* {{{ */
3018 if ((ci->values_num != 1)
3019 || (ci->values[0].type != OCONFIG_TYPE_STRING))
3021 WARNING ("network plugin: The `Interface' config option needs exactly "
3022 "one string argument.");
3026 if (interface == NULL)
3029 *interface = if_nametoindex (ci->values[0].value.string);
3032 } /* }}} int network_config_set_interface */
3034 static int network_config_set_buffer_size (const oconfig_item_t *ci) /* {{{ */
3037 if ((ci->values_num != 1)
3038 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
3040 WARNING ("network plugin: The `MaxPacketSize' config option needs exactly "
3041 "one numeric argument.");
3045 tmp = (int) ci->values[0].value.number;
3046 if ((tmp >= 1024) && (tmp <= 65535))
3047 network_config_packet_size = tmp;
3050 } /* }}} int network_config_set_buffer_size */
3053 static int network_config_set_string (const oconfig_item_t *ci, /* {{{ */
3057 if ((ci->values_num != 1)
3058 || (ci->values[0].type != OCONFIG_TYPE_STRING))
3060 WARNING ("network plugin: The `%s' config option needs exactly "
3061 "one string argument.", ci->key);
3065 tmp = strdup (ci->values[0].value.string);
3069 sfree (*ret_string);
3073 } /* }}} int network_config_set_string */
3074 #endif /* HAVE_LIBGCRYPT */
3077 static int network_config_set_security_level (oconfig_item_t *ci, /* {{{ */
3081 if ((ci->values_num != 1)
3082 || (ci->values[0].type != OCONFIG_TYPE_STRING))
3084 WARNING ("network plugin: The `SecurityLevel' config option needs exactly "
3085 "one string argument.");
3089 str = ci->values[0].value.string;
3090 if (strcasecmp ("Encrypt", str) == 0)
3091 *retval = SECURITY_LEVEL_ENCRYPT;
3092 else if (strcasecmp ("Sign", str) == 0)
3093 *retval = SECURITY_LEVEL_SIGN;
3094 else if (strcasecmp ("None", str) == 0)
3095 *retval = SECURITY_LEVEL_NONE;
3098 WARNING ("network plugin: Unknown security level: %s.", str);
3103 } /* }}} int network_config_set_security_level */
3104 #endif /* HAVE_LIBGCRYPT */
3106 static int network_config_add_listen (const oconfig_item_t *ci) /* {{{ */
3112 if ((ci->values_num < 1) || (ci->values_num > 2)
3113 || (ci->values[0].type != OCONFIG_TYPE_STRING)
3114 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
3116 ERROR ("network plugin: The `%s' config option needs "
3117 "one or two string arguments.", ci->key);
3121 se = sockent_create (SOCKENT_TYPE_SERVER);
3124 ERROR ("network plugin: sockent_create failed.");
3128 se->node = strdup (ci->values[0].value.string);
3129 if (ci->values_num >= 2)
3130 se->service = strdup (ci->values[1].value.string);
3132 for (i = 0; i < ci->children_num; i++)
3134 oconfig_item_t *child = ci->children + i;
3137 if (strcasecmp ("AuthFile", child->key) == 0)
3138 network_config_set_string (child, &se->data.server.auth_file);
3139 else if (strcasecmp ("SecurityLevel", child->key) == 0)
3140 network_config_set_security_level (child,
3141 &se->data.server.security_level);
3143 #endif /* HAVE_LIBGCRYPT */
3144 if (strcasecmp ("Interface", child->key) == 0)
3145 network_config_set_interface (child,
3149 WARNING ("network plugin: Option `%s' is not allowed here.",
3155 if ((se->data.server.security_level > SECURITY_LEVEL_NONE)
3156 && (se->data.server.auth_file == NULL))
3158 ERROR ("network plugin: A security level higher than `none' was "
3159 "requested, but no AuthFile option was given. Cowardly refusing to "
3160 "open this socket!");
3161 sockent_destroy (se);
3164 #endif /* HAVE_LIBGCRYPT */
3166 status = sockent_init_crypto (se);
3169 ERROR ("network plugin: network_config_add_listen: sockent_init_crypto() failed.");
3170 sockent_destroy (se);
3174 status = sockent_server_listen (se);
3177 ERROR ("network plugin: network_config_add_server: sockent_server_listen failed.");
3178 sockent_destroy (se);
3182 status = sockent_add (se);
3185 ERROR ("network plugin: network_config_add_listen: sockent_add failed.");
3186 sockent_destroy (se);
3191 } /* }}} int network_config_add_listen */
3193 static int network_config_add_server (const oconfig_item_t *ci) /* {{{ */
3199 if ((ci->values_num < 1) || (ci->values_num > 2)
3200 || (ci->values[0].type != OCONFIG_TYPE_STRING)
3201 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
3203 ERROR ("network plugin: The `%s' config option needs "
3204 "one or two string arguments.", ci->key);
3208 se = sockent_create (SOCKENT_TYPE_CLIENT);
3211 ERROR ("network plugin: sockent_create failed.");
3215 se->node = strdup (ci->values[0].value.string);
3216 if (ci->values_num >= 2)
3217 se->service = strdup (ci->values[1].value.string);
3219 for (i = 0; i < ci->children_num; i++)
3221 oconfig_item_t *child = ci->children + i;
3224 if (strcasecmp ("Username", child->key) == 0)
3225 network_config_set_string (child, &se->data.client.username);
3226 else if (strcasecmp ("Password", child->key) == 0)
3227 network_config_set_string (child, &se->data.client.password);
3228 else if (strcasecmp ("SecurityLevel", child->key) == 0)
3229 network_config_set_security_level (child,
3230 &se->data.client.security_level);
3232 #endif /* HAVE_LIBGCRYPT */
3233 if (strcasecmp ("Interface", child->key) == 0)
3234 network_config_set_interface (child,
3236 else if (strcasecmp ("ResolveInterval", child->key) == 0)
3237 cf_util_get_cdtime(child, &se->data.client.resolve_interval);
3240 WARNING ("network plugin: Option `%s' is not allowed here.",
3246 if ((se->data.client.security_level > SECURITY_LEVEL_NONE)
3247 && ((se->data.client.username == NULL)
3248 || (se->data.client.password == NULL)))
3250 ERROR ("network plugin: A security level higher than `none' was "
3251 "requested, but no Username or Password option was given. "
3252 "Cowardly refusing to open this socket!");
3253 sockent_destroy (se);
3256 #endif /* HAVE_LIBGCRYPT */
3258 status = sockent_init_crypto (se);
3261 ERROR ("network plugin: network_config_add_server: sockent_init_crypto() failed.");
3262 sockent_destroy (se);
3266 /* No call to sockent_client_connect() here -- it is called from
3267 * networt_send_buffer_plain(). */
3269 status = sockent_add (se);
3272 ERROR ("network plugin: network_config_add_server: sockent_add failed.");
3273 sockent_destroy (se);
3278 } /* }}} int network_config_add_server */
3280 static int network_config (oconfig_item_t *ci) /* {{{ */
3284 /* The options need to be applied first */
3285 for (i = 0; i < ci->children_num; i++)
3287 oconfig_item_t *child = ci->children + i;
3288 if (strcasecmp ("TimeToLive", child->key) == 0)
3289 network_config_set_ttl (child);
3292 for (i = 0; i < ci->children_num; i++)
3294 oconfig_item_t *child = ci->children + i;
3296 if (strcasecmp ("Listen", child->key) == 0)
3297 network_config_add_listen (child);
3298 else if (strcasecmp ("Server", child->key) == 0)
3299 network_config_add_server (child);
3300 else if (strcasecmp ("TimeToLive", child->key) == 0) {
3301 /* Handled earlier */
3303 else if (strcasecmp ("MaxPacketSize", child->key) == 0)
3304 network_config_set_buffer_size (child);
3305 else if (strcasecmp ("Forward", child->key) == 0)
3306 network_config_set_boolean (child, &network_config_forward);
3307 else if (strcasecmp ("ReportStats", child->key) == 0)
3308 network_config_set_boolean (child, &network_config_stats);
3311 WARNING ("network plugin: Option `%s' is not allowed here.",
3317 } /* }}} int network_config */
3319 static int network_notification (const notification_t *n,
3320 user_data_t __attribute__((unused)) *user_data)
3322 char buffer[network_config_packet_size];
3323 char *buffer_ptr = buffer;
3324 int buffer_free = sizeof (buffer);
3327 if (!check_send_notify_okay (n))
3330 memset (buffer, 0, sizeof (buffer));
3332 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_TIME_HR,
3333 (uint64_t) n->time);
3337 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_SEVERITY,
3338 (uint64_t) n->severity);
3342 if (strlen (n->host) > 0)
3344 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_HOST,
3345 n->host, strlen (n->host));
3350 if (strlen (n->plugin) > 0)
3352 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_PLUGIN,
3353 n->plugin, strlen (n->plugin));
3358 if (strlen (n->plugin_instance) > 0)
3360 status = write_part_string (&buffer_ptr, &buffer_free,
3361 TYPE_PLUGIN_INSTANCE,
3362 n->plugin_instance, strlen (n->plugin_instance));
3367 if (strlen (n->type) > 0)
3369 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE,
3370 n->type, strlen (n->type));
3375 if (strlen (n->type_instance) > 0)
3377 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE_INSTANCE,
3378 n->type_instance, strlen (n->type_instance));
3383 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_MESSAGE,
3384 n->message, strlen (n->message));
3388 network_send_buffer (buffer, sizeof (buffer) - buffer_free);
3391 } /* int network_notification */
3393 static int network_shutdown (void)
3399 /* Kill the listening thread */
3400 if (receive_thread_running != 0)
3402 INFO ("network plugin: Stopping receive thread.");
3403 pthread_kill (receive_thread_id, SIGTERM);
3404 pthread_join (receive_thread_id, NULL /* no return value */);
3405 memset (&receive_thread_id, 0, sizeof (receive_thread_id));
3406 receive_thread_running = 0;
3409 /* Shutdown the dispatching thread */
3410 if (dispatch_thread_running != 0)
3412 INFO ("network plugin: Stopping dispatch thread.");
3413 pthread_mutex_lock (&receive_list_lock);
3414 pthread_cond_broadcast (&receive_list_cond);
3415 pthread_mutex_unlock (&receive_list_lock);
3416 pthread_join (dispatch_thread_id, /* ret = */ NULL);
3417 dispatch_thread_running = 0;
3420 sockent_destroy (listen_sockets);
3422 if (send_buffer_fill > 0)
3425 sfree (send_buffer);
3427 for (se = sending_sockets; se != NULL; se = se->next)
3428 sockent_client_disconnect (se);
3429 sockent_destroy (sending_sockets);
3431 plugin_unregister_config ("network");
3432 plugin_unregister_init ("network");
3433 plugin_unregister_write ("network");
3434 plugin_unregister_shutdown ("network");
3437 } /* int network_shutdown */
3439 static int network_stats_read (void) /* {{{ */
3441 derive_t copy_octets_rx;
3442 derive_t copy_octets_tx;
3443 derive_t copy_packets_rx;
3444 derive_t copy_packets_tx;
3445 derive_t copy_values_dispatched;
3446 derive_t copy_values_not_dispatched;
3447 derive_t copy_values_sent;
3448 derive_t copy_values_not_sent;
3449 derive_t copy_receive_list_length;
3450 value_list_t vl = VALUE_LIST_INIT;
3453 copy_octets_rx = stats_octets_rx;
3454 copy_octets_tx = stats_octets_tx;
3455 copy_packets_rx = stats_packets_rx;
3456 copy_packets_tx = stats_packets_tx;
3457 copy_values_dispatched = stats_values_dispatched;
3458 copy_values_not_dispatched = stats_values_not_dispatched;
3459 copy_values_sent = stats_values_sent;
3460 copy_values_not_sent = stats_values_not_sent;
3461 copy_receive_list_length = receive_list_length;
3463 /* Initialize `vl' */
3467 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
3468 sstrncpy (vl.plugin, "network", sizeof (vl.plugin));
3470 /* Octets received / sent */
3471 vl.values[0].derive = (derive_t) copy_octets_rx;
3472 vl.values[1].derive = (derive_t) copy_octets_tx;
3473 sstrncpy (vl.type, "if_octets", sizeof (vl.type));
3474 plugin_dispatch_values (&vl);
3476 /* Packets received / send */
3477 vl.values[0].derive = (derive_t) copy_packets_rx;
3478 vl.values[1].derive = (derive_t) copy_packets_tx;
3479 sstrncpy (vl.type, "if_packets", sizeof (vl.type));
3480 plugin_dispatch_values (&vl);
3482 /* Values (not) dispatched and (not) send */
3483 sstrncpy (vl.type, "total_values", sizeof (vl.type));
3486 vl.values[0].derive = (derive_t) copy_values_dispatched;
3487 sstrncpy (vl.type_instance, "dispatch-accepted",
3488 sizeof (vl.type_instance));
3489 plugin_dispatch_values (&vl);
3491 vl.values[0].derive = (derive_t) copy_values_not_dispatched;
3492 sstrncpy (vl.type_instance, "dispatch-rejected",
3493 sizeof (vl.type_instance));
3494 plugin_dispatch_values (&vl);
3496 vl.values[0].derive = (derive_t) copy_values_sent;
3497 sstrncpy (vl.type_instance, "send-accepted",
3498 sizeof (vl.type_instance));
3499 plugin_dispatch_values (&vl);
3501 vl.values[0].derive = (derive_t) copy_values_not_sent;
3502 sstrncpy (vl.type_instance, "send-rejected",
3503 sizeof (vl.type_instance));
3504 plugin_dispatch_values (&vl);
3506 /* Receive queue length */
3507 vl.values[0].gauge = (gauge_t) copy_receive_list_length;
3508 sstrncpy (vl.type, "queue_length", sizeof (vl.type));
3509 vl.type_instance[0] = 0;
3510 plugin_dispatch_values (&vl);
3513 } /* }}} int network_stats_read */
3515 static int network_init (void)
3517 static _Bool have_init = 0;
3519 /* Check if we were already initialized. If so, just return - there's
3520 * nothing more to do (for now, that is). */
3526 network_init_gcrypt ();
3529 if (network_config_stats != 0)
3530 plugin_register_read ("network", network_stats_read);
3532 plugin_register_shutdown ("network", network_shutdown);
3534 send_buffer = malloc (network_config_packet_size);
3535 if (send_buffer == NULL)
3537 ERROR ("network plugin: malloc failed.");
3540 network_init_buffer ();
3542 /* setup socket(s) and so on */
3543 if (sending_sockets != NULL)
3545 plugin_register_write ("network", network_write,
3546 /* user_data = */ NULL);
3547 plugin_register_notification ("network", network_notification,
3548 /* user_data = */ NULL);
3551 /* If no threads need to be started, return here. */
3552 if ((listen_sockets_num == 0)
3553 || ((dispatch_thread_running != 0)
3554 && (receive_thread_running != 0)))
3557 if (dispatch_thread_running == 0)
3560 status = plugin_thread_create (&dispatch_thread_id,
3561 NULL /* no attributes */,
3563 NULL /* no argument */);
3567 ERROR ("network: pthread_create failed: %s",
3568 sstrerror (errno, errbuf,
3573 dispatch_thread_running = 1;
3577 if (receive_thread_running == 0)
3580 status = plugin_thread_create (&receive_thread_id,
3581 NULL /* no attributes */,
3583 NULL /* no argument */);
3587 ERROR ("network: pthread_create failed: %s",
3588 sstrerror (errno, errbuf,
3593 receive_thread_running = 1;
3598 } /* int network_init */
3601 * The flush option of the network plugin cannot flush individual identifiers.
3602 * All the values are added to a buffer and sent when the buffer is full, the
3603 * requested value may or may not be in there, it's not worth finding out. We
3604 * just send the buffer if `flush' is called - if the requested value was in
3605 * there, good. If not, well, then there is nothing to flush.. -octo
3607 static int network_flush (__attribute__((unused)) cdtime_t timeout,
3608 __attribute__((unused)) const char *identifier,
3609 __attribute__((unused)) user_data_t *user_data)
3611 pthread_mutex_lock (&send_buffer_lock);
3613 if (send_buffer_fill > 0)
3616 pthread_mutex_unlock (&send_buffer_lock);
3619 } /* int network_flush */
3621 void module_register (void)
3623 plugin_register_complex_config ("network", network_config);
3624 plugin_register_init ("network", network_init);
3625 plugin_register_flush ("network", network_flush,
3626 /* user_data = */ NULL);
3627 } /* void module_register */
3629 /* vim: set fdm=marker : */