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];
125 struct sockent_server
133 gcry_cipher_hd_t cypher;
137 typedef struct sockent
139 #define SOCKENT_TYPE_CLIENT 1
140 #define SOCKENT_TYPE_SERVER 2
149 struct sockent_client client;
150 struct sockent_server server;
153 struct sockent *next;
156 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
157 * 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
158 * +-------+-----------------------+-------------------------------+
159 * ! Ver. ! ! Length !
160 * +-------+-----------------------+-------------------------------+
167 typedef struct part_header_s part_header_t;
169 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
170 * 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
171 * +-------------------------------+-------------------------------+
173 * +-------------------------------+-------------------------------+
174 * : (Length - 4) Bytes :
175 * +---------------------------------------------------------------+
182 typedef struct part_string_s part_string_t;
184 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
185 * 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
186 * +-------------------------------+-------------------------------+
188 * +-------------------------------+-------------------------------+
189 * : (Length - 4 == 2 || 4 || 8) Bytes :
190 * +---------------------------------------------------------------+
197 typedef struct part_number_s part_number_t;
199 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
200 * 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
201 * +-------------------------------+-------------------------------+
203 * +-------------------------------+---------------+---------------+
204 * ! Num of values ! Type0 ! Type1 !
205 * +-------------------------------+---------------+---------------+
208 * +---------------------------------------------------------------+
211 * +---------------------------------------------------------------+
216 uint16_t *num_values;
217 uint8_t *values_types;
220 typedef struct part_values_s part_values_t;
222 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
223 * 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
224 * +-------------------------------+-------------------------------+
226 * +-------------------------------+-------------------------------+
227 * ! Hash (Bits 0 - 31) !
229 * ! Hash (Bits 224 - 255) !
230 * +---------------------------------------------------------------+
233 #define PART_SIGNATURE_SHA256_SIZE 36
234 struct part_signature_sha256_s
237 unsigned char hash[32];
240 typedef struct part_signature_sha256_s part_signature_sha256_t;
242 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
243 * 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
244 * +-------------------------------+-------------------------------+
246 * +-------------------------------+-------------------------------+
247 * ! Original length ! Padding (0 - 15 bytes) !
248 * +-------------------------------+-------------------------------+
249 * ! Hash (Bits 0 - 31) !
251 * ! Hash (Bits 128 - 159) !
252 * +---------------------------------------------------------------+
255 #define PART_ENCRYPTION_AES256_SIZE 42
256 struct part_encryption_aes256_s
259 uint16_t username_length;
261 unsigned char iv[16];
263 unsigned char hash[20];
267 typedef struct part_encryption_aes256_s part_encryption_aes256_t;
269 struct receive_list_entry_s
274 struct receive_list_entry_s *next;
276 typedef struct receive_list_entry_s receive_list_entry_t;
281 static int network_config_ttl = 0;
282 /* Ethernet - (IPv6 + UDP) = 1500 - (40 + 8) = 1452 */
283 static size_t network_config_packet_size = 1452;
284 static int network_config_forward = 0;
285 static int network_config_stats = 0;
287 static sockent_t *sending_sockets = NULL;
289 static receive_list_entry_t *receive_list_head = NULL;
290 static receive_list_entry_t *receive_list_tail = NULL;
291 static pthread_mutex_t receive_list_lock = PTHREAD_MUTEX_INITIALIZER;
292 static pthread_cond_t receive_list_cond = PTHREAD_COND_INITIALIZER;
293 static uint64_t receive_list_length = 0;
295 static sockent_t *listen_sockets = NULL;
296 static struct pollfd *listen_sockets_pollfd = NULL;
297 static size_t listen_sockets_num = 0;
299 /* The receive and dispatch threads will run as long as `listen_loop' is set to
301 static int listen_loop = 0;
302 static int receive_thread_running = 0;
303 static pthread_t receive_thread_id;
304 static int dispatch_thread_running = 0;
305 static pthread_t dispatch_thread_id;
307 /* Buffer in which to-be-sent network packets are constructed. */
308 static char *send_buffer;
309 static char *send_buffer_ptr;
310 static int send_buffer_fill;
311 static value_list_t send_buffer_vl = VALUE_LIST_STATIC;
312 static pthread_mutex_t send_buffer_lock = PTHREAD_MUTEX_INITIALIZER;
314 /* XXX: These counters are incremented from one place only. The spot in which
315 * the values are incremented is either only reachable by one thread (the
316 * dispatch thread, for example) or locked by some lock (send_buffer_lock for
317 * example). Only if neither is true, the stats_lock is acquired. The counters
318 * are always read without holding a lock in the hope that writing 8 bytes to
319 * memory is an atomic operation. */
320 static derive_t stats_octets_rx = 0;
321 static derive_t stats_octets_tx = 0;
322 static derive_t stats_packets_rx = 0;
323 static derive_t stats_packets_tx = 0;
324 static derive_t stats_values_dispatched = 0;
325 static derive_t stats_values_not_dispatched = 0;
326 static derive_t stats_values_sent = 0;
327 static derive_t stats_values_not_sent = 0;
328 static pthread_mutex_t stats_lock = PTHREAD_MUTEX_INITIALIZER;
333 static _Bool check_receive_okay (const value_list_t *vl) /* {{{ */
335 uint64_t time_sent = 0;
338 status = uc_meta_data_get_unsigned_int (vl,
339 "network:time_sent", &time_sent);
341 /* This is a value we already sent. Don't allow it to be received again in
342 * order to avoid looping. */
343 if ((status == 0) && (time_sent >= ((uint64_t) vl->time)))
347 } /* }}} _Bool check_receive_okay */
349 static _Bool check_send_okay (const value_list_t *vl) /* {{{ */
354 if (network_config_forward != 0)
357 if (vl->meta == NULL)
360 status = meta_data_get_boolean (vl->meta, "network:received", &received);
361 if (status == -ENOENT)
363 else if (status != 0)
365 ERROR ("network plugin: check_send_okay: meta_data_get_boolean failed "
366 "with status %i.", status);
370 /* By default, only *send* value lists that were not *received* by the
373 } /* }}} _Bool check_send_okay */
375 static _Bool check_notify_received (const notification_t *n) /* {{{ */
377 notification_meta_t *ptr;
379 for (ptr = n->meta; ptr != NULL; ptr = ptr->next)
380 if ((strcmp ("network:received", ptr->name) == 0)
381 && (ptr->type == NM_TYPE_BOOLEAN))
382 return ((_Bool) ptr->nm_value.nm_boolean);
385 } /* }}} _Bool check_notify_received */
387 static _Bool check_send_notify_okay (const notification_t *n) /* {{{ */
389 static c_complain_t complain_forwarding = C_COMPLAIN_INIT_STATIC;
395 received = check_notify_received (n);
397 if (network_config_forward && received)
399 c_complain_once (LOG_ERR, &complain_forwarding,
400 "network plugin: A notification has been received via the network "
401 "and forwarding is enabled. Forwarding of notifications is currently "
402 "not supported, because there is not loop-deteciton available. "
403 "Please contact the collectd mailing list if you need this "
407 /* By default, only *send* value lists that were not *received* by the
410 } /* }}} _Bool check_send_notify_okay */
412 static int network_dispatch_values (value_list_t *vl, /* {{{ */
413 const char *username)
418 || (strlen (vl->host) <= 0)
419 || (strlen (vl->plugin) <= 0)
420 || (strlen (vl->type) <= 0))
423 if (!check_receive_okay (vl))
426 char name[6*DATA_MAX_NAME_LEN];
427 FORMAT_VL (name, sizeof (name), vl);
428 name[sizeof (name) - 1] = 0;
429 DEBUG ("network plugin: network_dispatch_values: "
430 "NOT dispatching %s.", name);
432 stats_values_not_dispatched++;
436 assert (vl->meta == NULL);
438 vl->meta = meta_data_create ();
439 if (vl->meta == NULL)
441 ERROR ("network plugin: meta_data_create failed.");
445 status = meta_data_add_boolean (vl->meta, "network:received", 1);
448 ERROR ("network plugin: meta_data_add_boolean failed.");
449 meta_data_destroy (vl->meta);
454 if (username != NULL)
456 status = meta_data_add_string (vl->meta, "network:username", username);
459 ERROR ("network plugin: meta_data_add_string failed.");
460 meta_data_destroy (vl->meta);
466 plugin_dispatch_values (vl);
467 stats_values_dispatched++;
469 meta_data_destroy (vl->meta);
473 } /* }}} int network_dispatch_values */
475 static int network_dispatch_notification (notification_t *n) /* {{{ */
479 assert (n->meta == NULL);
481 status = plugin_notification_meta_add_boolean (n, "network:received", 1);
484 ERROR ("network plugin: plugin_notification_meta_add_boolean failed.");
485 plugin_notification_meta_free (n->meta);
490 status = plugin_dispatch_notification (n);
492 plugin_notification_meta_free (n->meta);
496 } /* }}} int network_dispatch_notification */
499 static int 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 err = gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
520 ERROR ("network plugin: gcry_control (GCRYCTL_SET_THREAD_CBS) failed: %s", gcry_strerror (err));
525 gcry_check_version (NULL);
527 err = gcry_control (GCRYCTL_INIT_SECMEM, 32768);
530 ERROR ("network plugin: gcry_control (GCRYCTL_INIT_SECMEM) failed: %s", gcry_strerror (err));
534 gcry_control (GCRYCTL_INITIALIZATION_FINISHED);
536 } /* }}} int network_init_gcrypt */
538 static gcry_cipher_hd_t network_get_aes256_cypher (sockent_t *se, /* {{{ */
539 const void *iv, size_t iv_size, const char *username)
542 gcry_cipher_hd_t *cyper_ptr;
543 unsigned char password_hash[32];
545 if (se->type == SOCKENT_TYPE_CLIENT)
547 cyper_ptr = &se->data.client.cypher;
548 memcpy (password_hash, se->data.client.password_hash,
549 sizeof (password_hash));
555 cyper_ptr = &se->data.server.cypher;
557 if (username == NULL)
560 secret = fbh_get (se->data.server.userdb, username);
564 gcry_md_hash_buffer (GCRY_MD_SHA256,
566 secret, strlen (secret));
571 if (*cyper_ptr == NULL)
573 err = gcry_cipher_open (cyper_ptr,
574 GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_OFB, /* flags = */ 0);
577 ERROR ("network plugin: gcry_cipher_open returned: %s",
578 gcry_strerror (err));
585 gcry_cipher_reset (*cyper_ptr);
587 assert (*cyper_ptr != NULL);
589 err = gcry_cipher_setkey (*cyper_ptr,
590 password_hash, sizeof (password_hash));
593 ERROR ("network plugin: gcry_cipher_setkey returned: %s",
594 gcry_strerror (err));
595 gcry_cipher_close (*cyper_ptr);
600 err = gcry_cipher_setiv (*cyper_ptr, iv, iv_size);
603 ERROR ("network plugin: gcry_cipher_setkey returned: %s",
604 gcry_strerror (err));
605 gcry_cipher_close (*cyper_ptr);
611 } /* }}} int network_get_aes256_cypher */
612 #endif /* HAVE_LIBGCRYPT */
614 static int write_part_values (char **ret_buffer, int *ret_buffer_len,
615 const data_set_t *ds, const value_list_t *vl)
621 part_header_t pkg_ph;
622 uint16_t pkg_num_values;
623 uint8_t *pkg_values_types;
629 num_values = vl->values_len;
630 packet_len = sizeof (part_header_t) + sizeof (uint16_t)
631 + (num_values * sizeof (uint8_t))
632 + (num_values * sizeof (value_t));
634 if (*ret_buffer_len < packet_len)
637 pkg_values_types = (uint8_t *) malloc (num_values * sizeof (uint8_t));
638 if (pkg_values_types == NULL)
640 ERROR ("network plugin: write_part_values: malloc failed.");
644 pkg_values = (value_t *) malloc (num_values * sizeof (value_t));
645 if (pkg_values == NULL)
647 free (pkg_values_types);
648 ERROR ("network plugin: write_part_values: malloc failed.");
652 pkg_ph.type = htons (TYPE_VALUES);
653 pkg_ph.length = htons (packet_len);
655 pkg_num_values = htons ((uint16_t) vl->values_len);
657 for (i = 0; i < num_values; i++)
659 pkg_values_types[i] = (uint8_t) ds->ds[i].type;
660 switch (ds->ds[i].type)
662 case DS_TYPE_COUNTER:
663 pkg_values[i].counter = htonll (vl->values[i].counter);
667 pkg_values[i].gauge = htond (vl->values[i].gauge);
671 pkg_values[i].derive = htonll (vl->values[i].derive);
674 case DS_TYPE_ABSOLUTE:
675 pkg_values[i].absolute = htonll (vl->values[i].absolute);
679 free (pkg_values_types);
681 ERROR ("network plugin: write_part_values: "
682 "Unknown data source type: %i",
685 } /* switch (ds->ds[i].type) */
686 } /* for (num_values) */
689 * Use `memcpy' to write everything to the buffer, because the pointer
690 * may be unaligned and some architectures, such as SPARC, can't handle
693 packet_ptr = *ret_buffer;
695 memcpy (packet_ptr + offset, &pkg_ph, sizeof (pkg_ph));
696 offset += sizeof (pkg_ph);
697 memcpy (packet_ptr + offset, &pkg_num_values, sizeof (pkg_num_values));
698 offset += sizeof (pkg_num_values);
699 memcpy (packet_ptr + offset, pkg_values_types, num_values * sizeof (uint8_t));
700 offset += num_values * sizeof (uint8_t);
701 memcpy (packet_ptr + offset, pkg_values, num_values * sizeof (value_t));
702 offset += num_values * sizeof (value_t);
704 assert (offset == packet_len);
706 *ret_buffer = packet_ptr + packet_len;
707 *ret_buffer_len -= packet_len;
709 free (pkg_values_types);
713 } /* int write_part_values */
715 static int write_part_number (char **ret_buffer, int *ret_buffer_len,
716 int type, uint64_t value)
721 part_header_t pkg_head;
726 packet_len = sizeof (pkg_head) + sizeof (pkg_value);
728 if (*ret_buffer_len < packet_len)
731 pkg_head.type = htons (type);
732 pkg_head.length = htons (packet_len);
733 pkg_value = htonll (value);
735 packet_ptr = *ret_buffer;
737 memcpy (packet_ptr + offset, &pkg_head, sizeof (pkg_head));
738 offset += sizeof (pkg_head);
739 memcpy (packet_ptr + offset, &pkg_value, sizeof (pkg_value));
740 offset += sizeof (pkg_value);
742 assert (offset == packet_len);
744 *ret_buffer = packet_ptr + packet_len;
745 *ret_buffer_len -= packet_len;
748 } /* int write_part_number */
750 static int write_part_string (char **ret_buffer, int *ret_buffer_len,
751 int type, const char *str, int str_len)
761 buffer_len = 2 * sizeof (uint16_t) + str_len + 1;
762 if (*ret_buffer_len < buffer_len)
765 pkg_type = htons (type);
766 pkg_length = htons (buffer_len);
768 buffer = *ret_buffer;
770 memcpy (buffer + offset, (void *) &pkg_type, sizeof (pkg_type));
771 offset += sizeof (pkg_type);
772 memcpy (buffer + offset, (void *) &pkg_length, sizeof (pkg_length));
773 offset += sizeof (pkg_length);
774 memcpy (buffer + offset, str, str_len);
776 memset (buffer + offset, '\0', 1);
779 assert (offset == buffer_len);
781 *ret_buffer = buffer + buffer_len;
782 *ret_buffer_len -= buffer_len;
785 } /* int write_part_string */
787 static int parse_part_values (void **ret_buffer, size_t *ret_buffer_len,
788 value_t **ret_values, int *ret_num_values)
790 char *buffer = *ret_buffer;
791 size_t buffer_len = *ret_buffer_len;
806 NOTICE ("network plugin: packet is too short: "
807 "buffer_len = %zu", buffer_len);
811 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
812 buffer += sizeof (tmp16);
813 pkg_type = ntohs (tmp16);
815 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
816 buffer += sizeof (tmp16);
817 pkg_length = ntohs (tmp16);
819 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
820 buffer += sizeof (tmp16);
821 pkg_numval = ntohs (tmp16);
823 assert (pkg_type == TYPE_VALUES);
825 exp_size = 3 * sizeof (uint16_t)
826 + pkg_numval * (sizeof (uint8_t) + sizeof (value_t));
827 if (buffer_len < exp_size)
829 WARNING ("network plugin: parse_part_values: "
831 "Chunk of size %zu expected, "
832 "but buffer has only %zu bytes left.",
833 exp_size, buffer_len);
837 if (pkg_length != exp_size)
839 WARNING ("network plugin: parse_part_values: "
840 "Length and number of values "
841 "in the packet don't match.");
845 pkg_types = (uint8_t *) malloc (pkg_numval * sizeof (uint8_t));
846 pkg_values = (value_t *) malloc (pkg_numval * sizeof (value_t));
847 if ((pkg_types == NULL) || (pkg_values == NULL))
851 ERROR ("network plugin: parse_part_values: malloc failed.");
855 memcpy ((void *) pkg_types, (void *) buffer, pkg_numval * sizeof (uint8_t));
856 buffer += pkg_numval * sizeof (uint8_t);
857 memcpy ((void *) pkg_values, (void *) buffer, pkg_numval * sizeof (value_t));
858 buffer += pkg_numval * sizeof (value_t);
860 for (i = 0; i < pkg_numval; i++)
862 switch (pkg_types[i])
864 case DS_TYPE_COUNTER:
865 pkg_values[i].counter = (counter_t) ntohll (pkg_values[i].counter);
869 pkg_values[i].gauge = (gauge_t) ntohd (pkg_values[i].gauge);
873 pkg_values[i].derive = (derive_t) ntohll (pkg_values[i].derive);
876 case DS_TYPE_ABSOLUTE:
877 pkg_values[i].absolute = (absolute_t) ntohll (pkg_values[i].absolute);
881 NOTICE ("network plugin: parse_part_values: "
882 "Don't know how to handle data source type %"PRIu8,
887 } /* switch (pkg_types[i]) */
890 *ret_buffer = buffer;
891 *ret_buffer_len = buffer_len - pkg_length;
892 *ret_num_values = pkg_numval;
893 *ret_values = pkg_values;
898 } /* int parse_part_values */
900 static int parse_part_number (void **ret_buffer, size_t *ret_buffer_len,
903 char *buffer = *ret_buffer;
904 size_t buffer_len = *ret_buffer_len;
908 size_t exp_size = 2 * sizeof (uint16_t) + sizeof (uint64_t);
912 if (buffer_len < exp_size)
914 WARNING ("network plugin: parse_part_number: "
916 "Chunk of size %zu expected, "
917 "but buffer has only %zu bytes left.",
918 exp_size, buffer_len);
922 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
923 buffer += sizeof (tmp16);
924 /* pkg_type = ntohs (tmp16); */
926 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
927 buffer += sizeof (tmp16);
928 pkg_length = ntohs (tmp16);
930 memcpy ((void *) &tmp64, buffer, sizeof (tmp64));
931 buffer += sizeof (tmp64);
932 *value = ntohll (tmp64);
934 *ret_buffer = buffer;
935 *ret_buffer_len = buffer_len - pkg_length;
938 } /* int parse_part_number */
940 static int parse_part_string (void **ret_buffer, size_t *ret_buffer_len,
941 char *output, int output_len)
943 char *buffer = *ret_buffer;
944 size_t buffer_len = *ret_buffer_len;
947 size_t header_size = 2 * sizeof (uint16_t);
951 if (buffer_len < header_size)
953 WARNING ("network plugin: parse_part_string: "
955 "Chunk of at least size %zu expected, "
956 "but buffer has only %zu bytes left.",
957 header_size, buffer_len);
961 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
962 buffer += sizeof (tmp16);
963 /* pkg_type = ntohs (tmp16); */
965 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
966 buffer += sizeof (tmp16);
967 pkg_length = ntohs (tmp16);
969 /* Check that packet fits in the input buffer */
970 if (pkg_length > buffer_len)
972 WARNING ("network plugin: parse_part_string: "
974 "Chunk of size %"PRIu16" received, "
975 "but buffer has only %zu bytes left.",
976 pkg_length, buffer_len);
980 /* Check that pkg_length is in the valid range */
981 if (pkg_length <= header_size)
983 WARNING ("network plugin: parse_part_string: "
985 "Header claims this packet is only %hu "
986 "bytes long.", pkg_length);
990 /* Check that the package data fits into the output buffer.
991 * The previous if-statement ensures that:
992 * `pkg_length > header_size' */
994 || ((size_t) output_len < ((size_t) pkg_length - header_size)))
996 WARNING ("network plugin: parse_part_string: "
997 "Output buffer too small.");
1001 /* All sanity checks successfull, let's copy the data over */
1002 output_len = pkg_length - header_size;
1003 memcpy ((void *) output, (void *) buffer, output_len);
1004 buffer += output_len;
1006 /* For some very weird reason '\0' doesn't do the trick on SPARC in
1007 * this statement. */
1008 if (output[output_len - 1] != 0)
1010 WARNING ("network plugin: parse_part_string: "
1011 "Received string does not end "
1012 "with a NULL-byte.");
1016 *ret_buffer = buffer;
1017 *ret_buffer_len = buffer_len - pkg_length;
1020 } /* int parse_part_string */
1022 /* Forward declaration: parse_part_sign_sha256 and parse_part_encr_aes256 call
1023 * parse_packet and vice versa. */
1024 #define PP_SIGNED 0x01
1025 #define PP_ENCRYPTED 0x02
1026 static int parse_packet (sockent_t *se,
1027 void *buffer, size_t buffer_size, int flags,
1028 const char *username);
1030 #define BUFFER_READ(p,s) do { \
1031 memcpy ((p), buffer + buffer_offset, (s)); \
1032 buffer_offset += (s); \
1036 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
1037 void **ret_buffer, size_t *ret_buffer_len, int flags)
1039 static c_complain_t complain_no_users = C_COMPLAIN_INIT_STATIC;
1043 size_t buffer_offset;
1045 size_t username_len;
1048 part_signature_sha256_t pss;
1049 uint16_t pss_head_length;
1050 char hash[sizeof (pss.hash)];
1054 unsigned char *hash_ptr;
1056 buffer = *ret_buffer;
1057 buffer_len = *ret_buffer_len;
1060 if (se->data.server.userdb == NULL)
1062 c_complain (LOG_NOTICE, &complain_no_users,
1063 "network plugin: Received signed network packet but can't verify it "
1064 "because no user DB has been configured. Will accept it.");
1068 /* Check if the buffer has enough data for this structure. */
1069 if (buffer_len <= PART_SIGNATURE_SHA256_SIZE)
1072 /* Read type and length header */
1073 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
1074 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
1075 pss_head_length = ntohs (pss.head.length);
1077 /* Check if the `pss_head_length' is within bounds. */
1078 if ((pss_head_length <= PART_SIGNATURE_SHA256_SIZE)
1079 || (pss_head_length > buffer_len))
1081 ERROR ("network plugin: HMAC-SHA-256 with invalid length received.");
1085 /* Copy the hash. */
1086 BUFFER_READ (pss.hash, sizeof (pss.hash));
1088 /* Calculate username length (without null byte) and allocate memory */
1089 username_len = pss_head_length - PART_SIGNATURE_SHA256_SIZE;
1090 pss.username = malloc (username_len + 1);
1091 if (pss.username == NULL)
1094 /* Read the username */
1095 BUFFER_READ (pss.username, username_len);
1096 pss.username[username_len] = 0;
1098 assert (buffer_offset == pss_head_length);
1100 /* Query the password */
1101 secret = fbh_get (se->data.server.userdb, pss.username);
1104 ERROR ("network plugin: Unknown user: %s", pss.username);
1105 sfree (pss.username);
1109 /* Create a hash device and check the HMAC */
1111 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
1114 ERROR ("network plugin: Creating HMAC-SHA-256 object failed: %s",
1115 gcry_strerror (err));
1117 sfree (pss.username);
1121 err = gcry_md_setkey (hd, secret, strlen (secret));
1124 ERROR ("network plugin: gcry_md_setkey failed: %s", gcry_strerror (err));
1127 sfree (pss.username);
1132 buffer + PART_SIGNATURE_SHA256_SIZE,
1133 buffer_len - PART_SIGNATURE_SHA256_SIZE);
1134 hash_ptr = gcry_md_read (hd, GCRY_MD_SHA256);
1135 if (hash_ptr == NULL)
1137 ERROR ("network plugin: gcry_md_read failed.");
1140 sfree (pss.username);
1143 memcpy (hash, hash_ptr, sizeof (hash));
1149 if (memcmp (pss.hash, hash, sizeof (pss.hash)) != 0)
1151 WARNING ("network plugin: Verifying HMAC-SHA-256 signature failed: "
1156 parse_packet (se, buffer + buffer_offset, buffer_len - buffer_offset,
1157 flags | PP_SIGNED, pss.username);
1161 sfree (pss.username);
1163 *ret_buffer = buffer + buffer_len;
1164 *ret_buffer_len = 0;
1167 } /* }}} int parse_part_sign_sha256 */
1168 /* #endif HAVE_LIBGCRYPT */
1170 #else /* if !HAVE_LIBGCRYPT */
1171 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
1172 void **ret_buffer, size_t *ret_buffer_size, int flags)
1174 static int warning_has_been_printed = 0;
1178 size_t buffer_offset;
1181 part_signature_sha256_t pss;
1183 buffer = *ret_buffer;
1184 buffer_size = *ret_buffer_size;
1187 if (buffer_size <= PART_SIGNATURE_SHA256_SIZE)
1190 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
1191 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
1192 part_len = ntohs (pss.head.length);
1194 if ((part_len <= PART_SIGNATURE_SHA256_SIZE)
1195 || (part_len > buffer_size))
1198 if (warning_has_been_printed == 0)
1200 WARNING ("network plugin: Received signed packet, but the network "
1201 "plugin was not linked with libgcrypt, so I cannot "
1202 "verify the signature. The packet will be accepted.");
1203 warning_has_been_printed = 1;
1206 parse_packet (se, buffer + part_len, buffer_size - part_len, flags,
1207 /* username = */ NULL);
1209 *ret_buffer = buffer + buffer_size;
1210 *ret_buffer_size = 0;
1213 } /* }}} int parse_part_sign_sha256 */
1214 #endif /* !HAVE_LIBGCRYPT */
1217 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1218 void **ret_buffer, size_t *ret_buffer_len,
1221 char *buffer = *ret_buffer;
1222 size_t buffer_len = *ret_buffer_len;
1225 size_t buffer_offset;
1226 uint16_t username_len;
1227 part_encryption_aes256_t pea;
1228 unsigned char hash[sizeof (pea.hash)];
1230 gcry_cipher_hd_t cypher;
1233 /* Make sure at least the header if available. */
1234 if (buffer_len <= PART_ENCRYPTION_AES256_SIZE)
1236 NOTICE ("network plugin: parse_part_encr_aes256: "
1237 "Discarding short packet.");
1243 /* Copy the unencrypted information into `pea'. */
1244 BUFFER_READ (&pea.head.type, sizeof (pea.head.type));
1245 BUFFER_READ (&pea.head.length, sizeof (pea.head.length));
1247 /* Check the `part size'. */
1248 part_size = ntohs (pea.head.length);
1249 if ((part_size <= PART_ENCRYPTION_AES256_SIZE)
1250 || (part_size > buffer_len))
1252 NOTICE ("network plugin: parse_part_encr_aes256: "
1253 "Discarding part with invalid size.");
1257 /* Read the username */
1258 BUFFER_READ (&username_len, sizeof (username_len));
1259 username_len = ntohs (username_len);
1261 if ((username_len <= 0)
1262 || (username_len > (part_size - (PART_ENCRYPTION_AES256_SIZE + 1))))
1264 NOTICE ("network plugin: parse_part_encr_aes256: "
1265 "Discarding part with invalid username length.");
1269 assert (username_len > 0);
1270 pea.username = malloc (username_len + 1);
1271 if (pea.username == NULL)
1273 BUFFER_READ (pea.username, username_len);
1274 pea.username[username_len] = 0;
1276 /* Last but not least, the initialization vector */
1277 BUFFER_READ (pea.iv, sizeof (pea.iv));
1279 /* Make sure we are at the right position */
1280 assert (buffer_offset == (username_len +
1281 PART_ENCRYPTION_AES256_SIZE - sizeof (pea.hash)));
1283 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
1287 sfree (pea.username);
1291 payload_len = part_size - (PART_ENCRYPTION_AES256_SIZE + username_len);
1292 assert (payload_len > 0);
1294 /* Decrypt the packet in-place */
1295 err = gcry_cipher_decrypt (cypher,
1296 buffer + buffer_offset,
1297 part_size - buffer_offset,
1298 /* in = */ NULL, /* in len = */ 0);
1301 sfree (pea.username);
1302 ERROR ("network plugin: gcry_cipher_decrypt returned: %s",
1303 gcry_strerror (err));
1308 BUFFER_READ (pea.hash, sizeof (pea.hash));
1310 /* Make sure we're at the right position - again */
1311 assert (buffer_offset == (username_len + PART_ENCRYPTION_AES256_SIZE));
1312 assert (buffer_offset == (part_size - payload_len));
1314 /* Check hash sum */
1315 memset (hash, 0, sizeof (hash));
1316 gcry_md_hash_buffer (GCRY_MD_SHA1, hash,
1317 buffer + buffer_offset, payload_len);
1318 if (memcmp (hash, pea.hash, sizeof (hash)) != 0)
1320 sfree (pea.username);
1321 ERROR ("network plugin: Decryption failed: Checksum mismatch.");
1325 parse_packet (se, buffer + buffer_offset, payload_len,
1326 flags | PP_ENCRYPTED, pea.username);
1328 /* XXX: Free pea.username?!? */
1330 /* Update return values */
1331 *ret_buffer = buffer + part_size;
1332 *ret_buffer_len = buffer_len - part_size;
1334 sfree (pea.username);
1337 } /* }}} int parse_part_encr_aes256 */
1338 /* #endif HAVE_LIBGCRYPT */
1340 #else /* if !HAVE_LIBGCRYPT */
1341 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1342 void **ret_buffer, size_t *ret_buffer_size, int flags)
1344 static int warning_has_been_printed = 0;
1348 size_t buffer_offset;
1353 buffer = *ret_buffer;
1354 buffer_size = *ret_buffer_size;
1357 /* parse_packet assures this minimum size. */
1358 assert (buffer_size >= (sizeof (ph.type) + sizeof (ph.length)));
1360 BUFFER_READ (&ph.type, sizeof (ph.type));
1361 BUFFER_READ (&ph.length, sizeof (ph.length));
1362 ph_length = ntohs (ph.length);
1364 if ((ph_length <= PART_ENCRYPTION_AES256_SIZE)
1365 || (ph_length > buffer_size))
1367 ERROR ("network plugin: AES-256 encrypted part "
1368 "with invalid length received.");
1372 if (warning_has_been_printed == 0)
1374 WARNING ("network plugin: Received encrypted packet, but the network "
1375 "plugin was not linked with libgcrypt, so I cannot "
1376 "decrypt it. The part will be discarded.");
1377 warning_has_been_printed = 1;
1380 *ret_buffer += ph_length;
1381 *ret_buffer_size -= ph_length;
1384 } /* }}} int parse_part_encr_aes256 */
1385 #endif /* !HAVE_LIBGCRYPT */
1389 static int parse_packet (sockent_t *se, /* {{{ */
1390 void *buffer, size_t buffer_size, int flags,
1391 const char *username)
1395 value_list_t vl = VALUE_LIST_INIT;
1399 int packet_was_signed = (flags & PP_SIGNED);
1400 int packet_was_encrypted = (flags & PP_ENCRYPTED);
1401 int printed_ignore_warning = 0;
1402 #endif /* HAVE_LIBGCRYPT */
1405 memset (&vl, '\0', sizeof (vl));
1406 memset (&n, '\0', sizeof (n));
1409 while ((status == 0) && (0 < buffer_size)
1410 && ((unsigned int) buffer_size > sizeof (part_header_t)))
1412 uint16_t pkg_length;
1415 memcpy ((void *) &pkg_type,
1418 memcpy ((void *) &pkg_length,
1419 (void *) (buffer + sizeof (pkg_type)),
1420 sizeof (pkg_length));
1422 pkg_length = ntohs (pkg_length);
1423 pkg_type = ntohs (pkg_type);
1425 if (pkg_length > buffer_size)
1427 /* Ensure that this loop terminates eventually */
1428 if (pkg_length < (2 * sizeof (uint16_t)))
1431 if (pkg_type == TYPE_ENCR_AES256)
1433 status = parse_part_encr_aes256 (se,
1434 &buffer, &buffer_size, flags);
1437 ERROR ("network plugin: Decrypting AES256 "
1439 "with status %i.", status);
1444 else if ((se->data.server.security_level == SECURITY_LEVEL_ENCRYPT)
1445 && (packet_was_encrypted == 0))
1447 if (printed_ignore_warning == 0)
1449 INFO ("network plugin: Unencrypted packet or "
1450 "part has been ignored.");
1451 printed_ignore_warning = 1;
1453 buffer = ((char *) buffer) + pkg_length;
1454 buffer_size -= (size_t) pkg_length;
1457 #endif /* HAVE_LIBGCRYPT */
1458 else if (pkg_type == TYPE_SIGN_SHA256)
1460 status = parse_part_sign_sha256 (se,
1461 &buffer, &buffer_size, flags);
1464 ERROR ("network plugin: Verifying HMAC-SHA-256 "
1466 "with status %i.", status);
1471 else if ((se->data.server.security_level == SECURITY_LEVEL_SIGN)
1472 && (packet_was_encrypted == 0)
1473 && (packet_was_signed == 0))
1475 if (printed_ignore_warning == 0)
1477 INFO ("network plugin: Unsigned packet or "
1478 "part has been ignored.");
1479 printed_ignore_warning = 1;
1481 buffer = ((char *) buffer) + pkg_length;
1482 buffer_size -= (size_t) pkg_length;
1485 #endif /* HAVE_LIBGCRYPT */
1486 else if (pkg_type == TYPE_VALUES)
1488 status = parse_part_values (&buffer, &buffer_size,
1489 &vl.values, &vl.values_len);
1493 network_dispatch_values (&vl, username);
1497 else if (pkg_type == TYPE_TIME)
1500 status = parse_part_number (&buffer, &buffer_size,
1504 vl.time = TIME_T_TO_CDTIME_T (tmp);
1505 n.time = TIME_T_TO_CDTIME_T (tmp);
1508 else if (pkg_type == TYPE_TIME_HR)
1511 status = parse_part_number (&buffer, &buffer_size,
1515 vl.time = (cdtime_t) tmp;
1516 n.time = (cdtime_t) tmp;
1519 else if (pkg_type == TYPE_INTERVAL)
1522 status = parse_part_number (&buffer, &buffer_size,
1525 vl.interval = TIME_T_TO_CDTIME_T (tmp);
1527 else if (pkg_type == TYPE_INTERVAL_HR)
1530 status = parse_part_number (&buffer, &buffer_size,
1533 vl.interval = (cdtime_t) tmp;
1535 else if (pkg_type == TYPE_HOST)
1537 status = parse_part_string (&buffer, &buffer_size,
1538 vl.host, sizeof (vl.host));
1540 sstrncpy (n.host, vl.host, sizeof (n.host));
1542 else if (pkg_type == TYPE_PLUGIN)
1544 status = parse_part_string (&buffer, &buffer_size,
1545 vl.plugin, sizeof (vl.plugin));
1547 sstrncpy (n.plugin, vl.plugin,
1550 else if (pkg_type == TYPE_PLUGIN_INSTANCE)
1552 status = parse_part_string (&buffer, &buffer_size,
1554 sizeof (vl.plugin_instance));
1556 sstrncpy (n.plugin_instance,
1558 sizeof (n.plugin_instance));
1560 else if (pkg_type == TYPE_TYPE)
1562 status = parse_part_string (&buffer, &buffer_size,
1563 vl.type, sizeof (vl.type));
1565 sstrncpy (n.type, vl.type, sizeof (n.type));
1567 else if (pkg_type == TYPE_TYPE_INSTANCE)
1569 status = parse_part_string (&buffer, &buffer_size,
1571 sizeof (vl.type_instance));
1573 sstrncpy (n.type_instance, vl.type_instance,
1574 sizeof (n.type_instance));
1576 else if (pkg_type == TYPE_MESSAGE)
1578 status = parse_part_string (&buffer, &buffer_size,
1579 n.message, sizeof (n.message));
1585 else if ((n.severity != NOTIF_FAILURE)
1586 && (n.severity != NOTIF_WARNING)
1587 && (n.severity != NOTIF_OKAY))
1589 INFO ("network plugin: "
1590 "Ignoring notification with "
1591 "unknown severity %i.",
1594 else if (n.time <= 0)
1596 INFO ("network plugin: "
1597 "Ignoring notification with "
1600 else if (strlen (n.message) <= 0)
1602 INFO ("network plugin: "
1603 "Ignoring notification with "
1604 "an empty message.");
1608 network_dispatch_notification (&n);
1611 else if (pkg_type == TYPE_SEVERITY)
1614 status = parse_part_number (&buffer, &buffer_size,
1617 n.severity = (int) tmp;
1621 DEBUG ("network plugin: parse_packet: Unknown part"
1622 " type: 0x%04hx", pkg_type);
1623 buffer = ((char *) buffer) + pkg_length;
1624 buffer_size -= (size_t) pkg_length;
1626 } /* while (buffer_size > sizeof (part_header_t)) */
1628 if (status == 0 && buffer_size > 0)
1629 WARNING ("network plugin: parse_packet: Received truncated "
1630 "packet, try increasing `MaxPacketSize'");
1633 } /* }}} int parse_packet */
1635 static void free_sockent_client (struct sockent_client *sec) /* {{{ */
1644 sfree (sec->username);
1645 sfree (sec->password);
1646 if (sec->cypher != NULL)
1647 gcry_cipher_close (sec->cypher);
1649 } /* }}} void free_sockent_client */
1651 static void free_sockent_server (struct sockent_server *ses) /* {{{ */
1655 for (i = 0; i < ses->fd_num; i++)
1657 if (ses->fd[i] >= 0)
1666 sfree (ses->auth_file);
1667 fbh_destroy (ses->userdb);
1668 if (ses->cypher != NULL)
1669 gcry_cipher_close (ses->cypher);
1671 } /* }}} void free_sockent_server */
1673 static void sockent_destroy (sockent_t *se) /* {{{ */
1677 DEBUG ("network plugin: sockent_destroy (se = %p);", (void *) se);
1684 sfree (se->service);
1686 if (se->type == SOCKENT_TYPE_CLIENT)
1687 free_sockent_client (&se->data.client);
1689 free_sockent_server (&se->data.server);
1694 } /* }}} void sockent_destroy */
1697 * int network_set_ttl
1699 * Set the `IP_MULTICAST_TTL', `IP_TTL', `IPV6_MULTICAST_HOPS' or
1700 * `IPV6_UNICAST_HOPS', depending on which option is applicable.
1702 * The `struct addrinfo' is used to destinguish between unicast and multicast
1705 static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
1707 DEBUG ("network plugin: network_set_ttl: network_config_ttl = %i;",
1708 network_config_ttl);
1710 assert (se->type == SOCKENT_TYPE_CLIENT);
1712 if ((network_config_ttl < 1) || (network_config_ttl > 255))
1715 if (ai->ai_family == AF_INET)
1717 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1720 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1721 optname = IP_MULTICAST_TTL;
1725 if (setsockopt (se->data.client.fd, IPPROTO_IP, optname,
1726 &network_config_ttl,
1727 sizeof (network_config_ttl)) != 0)
1730 ERROR ("network plugin: setsockopt (ipv4-ttl): %s",
1731 sstrerror (errno, errbuf, sizeof (errbuf)));
1735 else if (ai->ai_family == AF_INET6)
1737 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1738 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1741 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1742 optname = IPV6_MULTICAST_HOPS;
1744 optname = IPV6_UNICAST_HOPS;
1746 if (setsockopt (se->data.client.fd, IPPROTO_IPV6, optname,
1747 &network_config_ttl,
1748 sizeof (network_config_ttl)) != 0)
1751 ERROR ("network plugin: setsockopt(ipv6-ttl): %s",
1752 sstrerror (errno, errbuf,
1759 } /* int network_set_ttl */
1761 static int network_set_interface (const sockent_t *se, const struct addrinfo *ai) /* {{{ */
1763 DEBUG ("network plugin: network_set_interface: interface index = %i;",
1766 assert (se->type == SOCKENT_TYPE_CLIENT);
1768 if (ai->ai_family == AF_INET)
1770 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1772 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1774 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1775 /* If possible, use the "ip_mreqn" structure which has
1776 * an "interface index" member. Using the interface
1777 * index is preferred here, because of its similarity
1778 * to the way IPv6 handles this. Unfortunately, it
1779 * appears not to be portable. */
1780 struct ip_mreqn mreq;
1782 memset (&mreq, 0, sizeof (mreq));
1783 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1784 mreq.imr_address.s_addr = ntohl (INADDR_ANY);
1785 mreq.imr_ifindex = se->interface;
1787 struct ip_mreq mreq;
1789 memset (&mreq, 0, sizeof (mreq));
1790 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1791 mreq.imr_interface.s_addr = ntohl (INADDR_ANY);
1794 if (setsockopt (se->data.client.fd, IPPROTO_IP, IP_MULTICAST_IF,
1795 &mreq, sizeof (mreq)) != 0)
1798 ERROR ("network plugin: setsockopt (ipv4-multicast-if): %s",
1799 sstrerror (errno, errbuf, sizeof (errbuf)));
1806 else if (ai->ai_family == AF_INET6)
1808 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1810 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1812 if (setsockopt (se->data.client.fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
1814 sizeof (se->interface)) != 0)
1817 ERROR ("network plugin: setsockopt (ipv6-multicast-if): %s",
1818 sstrerror (errno, errbuf,
1827 /* else: Not a multicast interface. */
1828 if (se->interface != 0)
1830 #if defined(HAVE_IF_INDEXTONAME) && HAVE_IF_INDEXTONAME && defined(SO_BINDTODEVICE)
1831 char interface_name[IFNAMSIZ];
1833 if (if_indextoname (se->interface, interface_name) == NULL)
1836 DEBUG ("network plugin: Binding socket to interface %s", interface_name);
1838 if (setsockopt (se->data.client.fd, SOL_SOCKET, SO_BINDTODEVICE,
1840 sizeof(interface_name)) == -1 )
1843 ERROR ("network plugin: setsockopt (bind-if): %s",
1844 sstrerror (errno, errbuf, sizeof (errbuf)));
1847 /* #endif HAVE_IF_INDEXTONAME && SO_BINDTODEVICE */
1850 WARNING ("network plugin: Cannot set the interface on a unicast "
1852 # if !defined(SO_BINDTODEVICE)
1853 "the \"SO_BINDTODEVICE\" socket option "
1855 "the \"if_indextoname\" function "
1857 "is not available on your system.");
1863 } /* }}} network_set_interface */
1865 static int network_bind_socket (int fd, const struct addrinfo *ai, const int interface_idx)
1874 /* allow multiple sockets to use the same PORT number */
1875 if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR,
1876 &yes, sizeof(yes)) == -1) {
1878 ERROR ("network plugin: setsockopt (reuseaddr): %s",
1879 sstrerror (errno, errbuf, sizeof (errbuf)));
1883 DEBUG ("fd = %i; calling `bind'", fd);
1885 if (bind (fd, ai->ai_addr, ai->ai_addrlen) == -1)
1889 sstrerror (errno, errbuf, sizeof (errbuf)));
1893 if (ai->ai_family == AF_INET)
1895 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1896 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1898 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1899 struct ip_mreqn mreq;
1901 struct ip_mreq mreq;
1904 DEBUG ("fd = %i; IPv4 multicast address found", fd);
1906 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1907 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1908 /* Set the interface using the interface index if
1909 * possible (available). Unfortunately, the struct
1910 * ip_mreqn is not portable. */
1911 mreq.imr_address.s_addr = ntohl (INADDR_ANY);
1912 mreq.imr_ifindex = interface_idx;
1914 mreq.imr_interface.s_addr = ntohl (INADDR_ANY);
1917 if (setsockopt (fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1918 &loop, sizeof (loop)) == -1)
1921 ERROR ("network plugin: setsockopt (multicast-loop): %s",
1922 sstrerror (errno, errbuf,
1927 if (setsockopt (fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1928 &mreq, sizeof (mreq)) == -1)
1931 ERROR ("network plugin: setsockopt (add-membership): %s",
1932 sstrerror (errno, errbuf,
1940 else if (ai->ai_family == AF_INET6)
1942 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1943 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1944 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1946 struct ipv6_mreq mreq;
1948 DEBUG ("fd = %i; IPv6 multicast address found", fd);
1950 memcpy (&mreq.ipv6mr_multiaddr,
1952 sizeof (addr->sin6_addr));
1954 /* http://developer.apple.com/documentation/Darwin/Reference/ManPages/man4/ip6.4.html
1955 * ipv6mr_interface may be set to zeroes to
1956 * choose the default multicast interface or to
1957 * the index of a particular multicast-capable
1958 * interface if the host is multihomed.
1959 * Membership is associ-associated with a
1960 * single interface; programs running on
1961 * multihomed hosts may need to join the same
1962 * group on more than one interface.*/
1963 mreq.ipv6mr_interface = interface_idx;
1965 if (setsockopt (fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
1966 &loop, sizeof (loop)) == -1)
1969 ERROR ("network plugin: setsockopt (ipv6-multicast-loop): %s",
1970 sstrerror (errno, errbuf,
1975 if (setsockopt (fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
1976 &mreq, sizeof (mreq)) == -1)
1979 ERROR ("network plugin: setsockopt (ipv6-add-membership): %s",
1980 sstrerror (errno, errbuf,
1989 #if defined(HAVE_IF_INDEXTONAME) && HAVE_IF_INDEXTONAME && defined(SO_BINDTODEVICE)
1990 /* if a specific interface was set, bind the socket to it. But to avoid
1991 * possible problems with multicast routing, only do that for non-multicast
1993 if (interface_idx != 0)
1995 char interface_name[IFNAMSIZ];
1997 if (if_indextoname (interface_idx, interface_name) == NULL)
2000 DEBUG ("fd = %i; Binding socket to interface %s", fd, interface_name);
2002 if (setsockopt (fd, SOL_SOCKET, SO_BINDTODEVICE,
2004 sizeof(interface_name)) == -1 )
2007 ERROR ("network plugin: setsockopt (bind-if): %s",
2008 sstrerror (errno, errbuf, sizeof (errbuf)));
2012 #endif /* HAVE_IF_INDEXTONAME && SO_BINDTODEVICE */
2015 } /* int network_bind_socket */
2017 /* Initialize a sockent structure. `type' must be either `SOCKENT_TYPE_CLIENT'
2018 * or `SOCKENT_TYPE_SERVER' */
2019 static sockent_t *sockent_create (int type) /* {{{ */
2023 if ((type != SOCKENT_TYPE_CLIENT) && (type != SOCKENT_TYPE_SERVER))
2026 se = malloc (sizeof (*se));
2029 memset (se, 0, sizeof (*se));
2037 if (type == SOCKENT_TYPE_SERVER)
2039 se->data.server.fd = NULL;
2040 se->data.server.fd_num = 0;
2042 se->data.server.security_level = SECURITY_LEVEL_NONE;
2043 se->data.server.auth_file = NULL;
2044 se->data.server.userdb = NULL;
2045 se->data.server.cypher = NULL;
2050 se->data.client.fd = -1;
2051 se->data.client.addr = NULL;
2053 se->data.client.security_level = SECURITY_LEVEL_NONE;
2054 se->data.client.username = NULL;
2055 se->data.client.password = NULL;
2056 se->data.client.cypher = NULL;
2061 } /* }}} sockent_t *sockent_create */
2063 static int sockent_init_crypto (sockent_t *se) /* {{{ */
2065 #if HAVE_LIBGCRYPT /* {{{ */
2066 if (se->type == SOCKENT_TYPE_CLIENT)
2068 if (se->data.client.security_level > SECURITY_LEVEL_NONE)
2070 if (network_init_gcrypt () < 0)
2072 ERROR ("network plugin: Cannot configure client socket with "
2073 "security: Failed to initialize crypto library.");
2077 if ((se->data.client.username == NULL)
2078 || (se->data.client.password == NULL))
2080 ERROR ("network plugin: Client socket with "
2081 "security requested, but no "
2082 "credentials are configured.");
2085 gcry_md_hash_buffer (GCRY_MD_SHA256,
2086 se->data.client.password_hash,
2087 se->data.client.password,
2088 strlen (se->data.client.password));
2091 else /* (se->type == SOCKENT_TYPE_SERVER) */
2093 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
2095 if (network_init_gcrypt () < 0)
2097 ERROR ("network plugin: Cannot configure server socket with "
2098 "security: Failed to initialize crypto library.");
2102 if (se->data.server.auth_file == NULL)
2104 ERROR ("network plugin: Server socket with "
2105 "security requested, but no "
2106 "password file is configured.");
2110 if (se->data.server.auth_file != NULL)
2112 se->data.server.userdb = fbh_create (se->data.server.auth_file);
2113 if (se->data.server.userdb == NULL)
2115 ERROR ("network plugin: Reading password file "
2117 se->data.server.auth_file);
2118 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
2123 #endif /* }}} HAVE_LIBGCRYPT */
2126 } /* }}} int sockent_init_crypto */
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;
2137 if ((se == NULL) || (se->type != SOCKENT_TYPE_CLIENT))
2140 client = &se->data.client;
2141 if (client->fd >= 0) /* already connected */
2144 memset (&ai_hints, 0, sizeof (ai_hints));
2145 #ifdef AI_ADDRCONFIG
2146 ai_hints.ai_flags |= AI_ADDRCONFIG;
2148 ai_hints.ai_family = AF_UNSPEC;
2149 ai_hints.ai_socktype = SOCK_DGRAM;
2150 ai_hints.ai_protocol = IPPROTO_UDP;
2152 status = getaddrinfo (se->node,
2153 (se->service != NULL) ? se->service : NET_DEFAULT_PORT,
2154 &ai_hints, &ai_list);
2157 c_complain (LOG_ERR, &complaint,
2158 "network plugin: getaddrinfo (%s, %s) failed: %s",
2159 (se->node == NULL) ? "(null)" : se->node,
2160 (se->service == NULL) ? "(null)" : se->service,
2161 gai_strerror (status));
2166 c_release (LOG_NOTICE, &complaint,
2167 "network plugin: Successfully resolved \"%s\".",
2171 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
2173 client->fd = socket (ai_ptr->ai_family,
2174 ai_ptr->ai_socktype,
2175 ai_ptr->ai_protocol);
2179 ERROR ("network plugin: socket(2) failed: %s",
2180 sstrerror (errno, errbuf,
2185 client->addr = malloc (sizeof (*client->addr));
2186 if (client->addr == NULL)
2188 ERROR ("network plugin: malloc failed.");
2194 memset (client->addr, 0, sizeof (*client->addr));
2195 assert (sizeof (*client->addr) >= ai_ptr->ai_addrlen);
2196 memcpy (client->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
2197 client->addrlen = ai_ptr->ai_addrlen;
2199 network_set_ttl (se, ai_ptr);
2200 network_set_interface (se, ai_ptr);
2202 /* We don't open more than one write-socket per
2203 * node/service pair.. */
2207 freeaddrinfo (ai_list);
2211 } /* }}} int sockent_client_connect */
2213 static int sockent_client_disconnect (sockent_t *se) /* {{{ */
2215 struct sockent_client *client;
2217 if ((se == NULL) || (se->type != SOCKENT_TYPE_CLIENT))
2220 client = &se->data.client;
2221 if (client->fd >= 0) /* connected */
2227 sfree (client->addr);
2228 client->addrlen = 0;
2231 } /* }}} int sockent_client_disconnect */
2233 /* Open the file descriptors for a initialized sockent structure. */
2234 static int sockent_server_listen (sockent_t *se) /* {{{ */
2236 struct addrinfo ai_hints;
2237 struct addrinfo *ai_list, *ai_ptr;
2241 const char *service;
2246 assert (se->data.server.fd == NULL);
2247 assert (se->data.server.fd_num == 0);
2250 service = se->service;
2252 if (service == NULL)
2253 service = NET_DEFAULT_PORT;
2255 DEBUG ("network plugin: sockent_server_listen: node = %s; service = %s;",
2258 memset (&ai_hints, 0, sizeof (ai_hints));
2259 ai_hints.ai_flags = 0;
2261 ai_hints.ai_flags |= AI_PASSIVE;
2263 #ifdef AI_ADDRCONFIG
2264 ai_hints.ai_flags |= AI_ADDRCONFIG;
2266 ai_hints.ai_family = AF_UNSPEC;
2267 ai_hints.ai_socktype = SOCK_DGRAM;
2268 ai_hints.ai_protocol = IPPROTO_UDP;
2270 status = getaddrinfo (node, service, &ai_hints, &ai_list);
2273 ERROR ("network plugin: getaddrinfo (%s, %s) failed: %s",
2274 (se->node == NULL) ? "(null)" : se->node,
2275 (se->service == NULL) ? "(null)" : se->service,
2276 gai_strerror (status));
2280 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
2284 tmp = realloc (se->data.server.fd,
2285 sizeof (*tmp) * (se->data.server.fd_num + 1));
2288 ERROR ("network plugin: realloc failed.");
2291 se->data.server.fd = tmp;
2292 tmp = se->data.server.fd + se->data.server.fd_num;
2294 *tmp = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
2295 ai_ptr->ai_protocol);
2299 ERROR ("network plugin: socket(2) failed: %s",
2300 sstrerror (errno, errbuf,
2305 status = network_bind_socket (*tmp, ai_ptr, se->interface);
2313 se->data.server.fd_num++;
2315 } /* for (ai_list) */
2317 freeaddrinfo (ai_list);
2319 if (se->data.server.fd_num <= 0)
2322 } /* }}} int sockent_server_listen */
2324 /* Add a sockent to the global list of sockets */
2325 static int sockent_add (sockent_t *se) /* {{{ */
2327 sockent_t *last_ptr;
2332 if (se->type == SOCKENT_TYPE_SERVER)
2337 tmp = realloc (listen_sockets_pollfd,
2338 sizeof (*tmp) * (listen_sockets_num
2339 + se->data.server.fd_num));
2342 ERROR ("network plugin: realloc failed.");
2345 listen_sockets_pollfd = tmp;
2346 tmp = listen_sockets_pollfd + listen_sockets_num;
2348 for (i = 0; i < se->data.server.fd_num; i++)
2350 memset (tmp + i, 0, sizeof (*tmp));
2351 tmp[i].fd = se->data.server.fd[i];
2352 tmp[i].events = POLLIN | POLLPRI;
2356 listen_sockets_num += se->data.server.fd_num;
2358 if (listen_sockets == NULL)
2360 listen_sockets = se;
2363 last_ptr = listen_sockets;
2365 else /* if (se->type == SOCKENT_TYPE_CLIENT) */
2367 if (sending_sockets == NULL)
2369 sending_sockets = se;
2372 last_ptr = sending_sockets;
2375 while (last_ptr->next != NULL)
2376 last_ptr = last_ptr->next;
2377 last_ptr->next = se;
2380 } /* }}} int sockent_add */
2382 static void *dispatch_thread (void __attribute__((unused)) *arg) /* {{{ */
2386 receive_list_entry_t *ent;
2389 /* Lock and wait for more data to come in */
2390 pthread_mutex_lock (&receive_list_lock);
2391 while ((listen_loop == 0)
2392 && (receive_list_head == NULL))
2393 pthread_cond_wait (&receive_list_cond, &receive_list_lock);
2395 /* Remove the head entry and unlock */
2396 ent = receive_list_head;
2398 receive_list_head = ent->next;
2399 receive_list_length--;
2400 pthread_mutex_unlock (&receive_list_lock);
2402 /* Check whether we are supposed to exit. We do NOT check `listen_loop'
2403 * because we dispatch all missing packets before shutting down. */
2407 /* Look for the correct `sockent_t' */
2408 se = listen_sockets;
2413 for (i = 0; i < se->data.server.fd_num; i++)
2414 if (se->data.server.fd[i] == ent->fd)
2417 if (i < se->data.server.fd_num)
2425 ERROR ("network plugin: Got packet from FD %i, but can't "
2426 "find an appropriate socket entry.",
2433 parse_packet (se, ent->data, ent->data_len, /* flags = */ 0,
2434 /* username = */ NULL);
2440 } /* }}} void *dispatch_thread */
2442 static int network_receive (void) /* {{{ */
2444 char buffer[network_config_packet_size];
2450 receive_list_entry_t *private_list_head;
2451 receive_list_entry_t *private_list_tail;
2452 uint64_t private_list_length;
2454 assert (listen_sockets_num > 0);
2456 private_list_head = NULL;
2457 private_list_tail = NULL;
2458 private_list_length = 0;
2460 while (listen_loop == 0)
2462 status = poll (listen_sockets_pollfd, listen_sockets_num, -1);
2468 ERROR ("network plugin: poll(2) failed: %s",
2469 sstrerror (errno, errbuf, sizeof (errbuf)));
2473 for (i = 0; (i < listen_sockets_num) && (status > 0); i++)
2475 receive_list_entry_t *ent;
2477 if ((listen_sockets_pollfd[i].revents
2478 & (POLLIN | POLLPRI)) == 0)
2482 buffer_len = recv (listen_sockets_pollfd[i].fd,
2483 buffer, sizeof (buffer),
2488 status = (errno != 0) ? errno : -1;
2489 ERROR ("network plugin: recv(2) failed: %s",
2490 sstrerror (errno, errbuf, sizeof (errbuf)));
2494 stats_octets_rx += ((uint64_t) buffer_len);
2497 /* TODO: Possible performance enhancement: Do not free
2498 * these entries in the dispatch thread but put them in
2499 * another list, so we don't have to allocate more and
2500 * more of these structures. */
2501 ent = malloc (sizeof (receive_list_entry_t));
2504 ERROR ("network plugin: malloc failed.");
2508 memset (ent, 0, sizeof (receive_list_entry_t));
2509 ent->data = malloc (network_config_packet_size);
2510 if (ent->data == NULL)
2513 ERROR ("network plugin: malloc failed.");
2517 ent->fd = listen_sockets_pollfd[i].fd;
2520 memcpy (ent->data, buffer, buffer_len);
2521 ent->data_len = buffer_len;
2523 if (private_list_head == NULL)
2524 private_list_head = ent;
2526 private_list_tail->next = ent;
2527 private_list_tail = ent;
2528 private_list_length++;
2530 /* Do not block here. Blocking here has led to
2531 * insufficient performance in the past. */
2532 if (pthread_mutex_trylock (&receive_list_lock) == 0)
2534 assert (((receive_list_head == NULL) && (receive_list_length == 0))
2535 || ((receive_list_head != NULL) && (receive_list_length != 0)));
2537 if (receive_list_head == NULL)
2538 receive_list_head = private_list_head;
2540 receive_list_tail->next = private_list_head;
2541 receive_list_tail = private_list_tail;
2542 receive_list_length += private_list_length;
2544 pthread_cond_signal (&receive_list_cond);
2545 pthread_mutex_unlock (&receive_list_lock);
2547 private_list_head = NULL;
2548 private_list_tail = NULL;
2549 private_list_length = 0;
2553 } /* for (listen_sockets_pollfd) */
2557 } /* while (listen_loop == 0) */
2559 /* Make sure everything is dispatched before exiting. */
2560 if (private_list_head != NULL)
2562 pthread_mutex_lock (&receive_list_lock);
2564 if (receive_list_head == NULL)
2565 receive_list_head = private_list_head;
2567 receive_list_tail->next = private_list_head;
2568 receive_list_tail = private_list_tail;
2569 receive_list_length += private_list_length;
2571 pthread_cond_signal (&receive_list_cond);
2572 pthread_mutex_unlock (&receive_list_lock);
2576 } /* }}} int network_receive */
2578 static void *receive_thread (void __attribute__((unused)) *arg)
2580 return (network_receive () ? (void *) 1 : (void *) 0);
2581 } /* void *receive_thread */
2583 static void network_init_buffer (void)
2585 memset (send_buffer, 0, network_config_packet_size);
2586 send_buffer_ptr = send_buffer;
2587 send_buffer_fill = 0;
2589 memset (&send_buffer_vl, 0, sizeof (send_buffer_vl));
2590 } /* int network_init_buffer */
2592 static void networt_send_buffer_plain (sockent_t *se, /* {{{ */
2593 const char *buffer, size_t buffer_size)
2599 status = sockent_client_connect (se);
2603 status = sendto (se->data.client.fd, buffer, buffer_size,
2605 (struct sockaddr *) se->data.client.addr,
2606 se->data.client.addrlen);
2611 if ((errno == EINTR) || (errno == EAGAIN))
2614 ERROR ("network plugin: sendto failed: %s. Closing sending socket.",
2615 sstrerror (errno, errbuf, sizeof (errbuf)));
2616 sockent_client_disconnect (se);
2622 } /* }}} void networt_send_buffer_plain */
2625 #define BUFFER_ADD(p,s) do { \
2626 memcpy (buffer + buffer_offset, (p), (s)); \
2627 buffer_offset += (s); \
2630 static void networt_send_buffer_signed (sockent_t *se, /* {{{ */
2631 const char *in_buffer, size_t in_buffer_size)
2633 part_signature_sha256_t ps;
2634 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2635 size_t buffer_offset;
2636 size_t username_len;
2640 unsigned char *hash;
2643 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
2646 ERROR ("network plugin: Creating HMAC object failed: %s",
2647 gcry_strerror (err));
2651 err = gcry_md_setkey (hd, se->data.client.password,
2652 strlen (se->data.client.password));
2655 ERROR ("network plugin: gcry_md_setkey failed: %s",
2656 gcry_strerror (err));
2661 username_len = strlen (se->data.client.username);
2662 if (username_len > (BUFF_SIG_SIZE - PART_SIGNATURE_SHA256_SIZE))
2664 ERROR ("network plugin: Username too long: %s",
2665 se->data.client.username);
2669 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE,
2670 se->data.client.username, username_len);
2671 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE + username_len,
2672 in_buffer, in_buffer_size);
2674 /* Initialize the `ps' structure. */
2675 memset (&ps, 0, sizeof (ps));
2676 ps.head.type = htons (TYPE_SIGN_SHA256);
2677 ps.head.length = htons (PART_SIGNATURE_SHA256_SIZE + username_len);
2679 /* Calculate the hash value. */
2680 gcry_md_write (hd, buffer + PART_SIGNATURE_SHA256_SIZE,
2681 username_len + in_buffer_size);
2682 hash = gcry_md_read (hd, GCRY_MD_SHA256);
2685 ERROR ("network plugin: gcry_md_read failed.");
2689 memcpy (ps.hash, hash, sizeof (ps.hash));
2691 /* Add the header */
2694 BUFFER_ADD (&ps.head.type, sizeof (ps.head.type));
2695 BUFFER_ADD (&ps.head.length, sizeof (ps.head.length));
2696 BUFFER_ADD (ps.hash, sizeof (ps.hash));
2698 assert (buffer_offset == PART_SIGNATURE_SHA256_SIZE);
2703 buffer_offset = PART_SIGNATURE_SHA256_SIZE + username_len + in_buffer_size;
2704 networt_send_buffer_plain (se, buffer, buffer_offset);
2705 } /* }}} void networt_send_buffer_signed */
2707 static void networt_send_buffer_encrypted (sockent_t *se, /* {{{ */
2708 const char *in_buffer, size_t in_buffer_size)
2710 part_encryption_aes256_t pea;
2711 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2713 size_t buffer_offset;
2715 size_t username_len;
2717 gcry_cipher_hd_t cypher;
2719 /* Initialize the header fields */
2720 memset (&pea, 0, sizeof (pea));
2721 pea.head.type = htons (TYPE_ENCR_AES256);
2723 pea.username = se->data.client.username;
2725 username_len = strlen (pea.username);
2726 if ((PART_ENCRYPTION_AES256_SIZE + username_len) > BUFF_SIG_SIZE)
2728 ERROR ("network plugin: Username too long: %s", pea.username);
2732 buffer_size = PART_ENCRYPTION_AES256_SIZE + username_len + in_buffer_size;
2733 header_size = PART_ENCRYPTION_AES256_SIZE + username_len
2734 - sizeof (pea.hash);
2736 assert (buffer_size <= sizeof (buffer));
2737 DEBUG ("network plugin: networt_send_buffer_encrypted: "
2738 "buffer_size = %zu;", buffer_size);
2740 pea.head.length = htons ((uint16_t) (PART_ENCRYPTION_AES256_SIZE
2741 + username_len + in_buffer_size));
2742 pea.username_length = htons ((uint16_t) username_len);
2744 /* Chose a random initialization vector. */
2745 gcry_randomize ((void *) &pea.iv, sizeof (pea.iv), GCRY_STRONG_RANDOM);
2747 /* Create hash of the payload */
2748 gcry_md_hash_buffer (GCRY_MD_SHA1, pea.hash, in_buffer, in_buffer_size);
2750 /* Initialize the buffer */
2752 memset (buffer, 0, sizeof (buffer));
2755 BUFFER_ADD (&pea.head.type, sizeof (pea.head.type));
2756 BUFFER_ADD (&pea.head.length, sizeof (pea.head.length));
2757 BUFFER_ADD (&pea.username_length, sizeof (pea.username_length));
2758 BUFFER_ADD (pea.username, username_len);
2759 BUFFER_ADD (pea.iv, sizeof (pea.iv));
2760 assert (buffer_offset == header_size);
2761 BUFFER_ADD (pea.hash, sizeof (pea.hash));
2762 BUFFER_ADD (in_buffer, in_buffer_size);
2764 assert (buffer_offset == buffer_size);
2766 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
2767 se->data.client.password);
2771 /* Encrypt the buffer in-place */
2772 err = gcry_cipher_encrypt (cypher,
2773 buffer + header_size,
2774 buffer_size - header_size,
2775 /* in = */ NULL, /* in len = */ 0);
2778 ERROR ("network plugin: gcry_cipher_encrypt returned: %s",
2779 gcry_strerror (err));
2783 /* Send it out without further modifications */
2784 networt_send_buffer_plain (se, buffer, buffer_size);
2785 } /* }}} void networt_send_buffer_encrypted */
2787 #endif /* HAVE_LIBGCRYPT */
2789 static void network_send_buffer (char *buffer, size_t buffer_len) /* {{{ */
2793 DEBUG ("network plugin: network_send_buffer: buffer_len = %zu", buffer_len);
2795 for (se = sending_sockets; se != NULL; se = se->next)
2798 if (se->data.client.security_level == SECURITY_LEVEL_ENCRYPT)
2799 networt_send_buffer_encrypted (se, buffer, buffer_len);
2800 else if (se->data.client.security_level == SECURITY_LEVEL_SIGN)
2801 networt_send_buffer_signed (se, buffer, buffer_len);
2802 else /* if (se->data.client.security_level == SECURITY_LEVEL_NONE) */
2803 #endif /* HAVE_LIBGCRYPT */
2804 networt_send_buffer_plain (se, buffer, buffer_len);
2805 } /* for (sending_sockets) */
2806 } /* }}} void network_send_buffer */
2808 static int add_to_buffer (char *buffer, int buffer_size, /* {{{ */
2809 value_list_t *vl_def,
2810 const data_set_t *ds, const value_list_t *vl)
2812 char *buffer_orig = buffer;
2814 if (strcmp (vl_def->host, vl->host) != 0)
2816 if (write_part_string (&buffer, &buffer_size, TYPE_HOST,
2817 vl->host, strlen (vl->host)) != 0)
2819 sstrncpy (vl_def->host, vl->host, sizeof (vl_def->host));
2822 if (vl_def->time != vl->time)
2824 if (write_part_number (&buffer, &buffer_size, TYPE_TIME_HR,
2825 (uint64_t) vl->time))
2827 vl_def->time = vl->time;
2830 if (vl_def->interval != vl->interval)
2832 if (write_part_number (&buffer, &buffer_size, TYPE_INTERVAL_HR,
2833 (uint64_t) vl->interval))
2835 vl_def->interval = vl->interval;
2838 if (strcmp (vl_def->plugin, vl->plugin) != 0)
2840 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN,
2841 vl->plugin, strlen (vl->plugin)) != 0)
2843 sstrncpy (vl_def->plugin, vl->plugin, sizeof (vl_def->plugin));
2846 if (strcmp (vl_def->plugin_instance, vl->plugin_instance) != 0)
2848 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
2849 vl->plugin_instance,
2850 strlen (vl->plugin_instance)) != 0)
2852 sstrncpy (vl_def->plugin_instance, vl->plugin_instance, sizeof (vl_def->plugin_instance));
2855 if (strcmp (vl_def->type, vl->type) != 0)
2857 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
2858 vl->type, strlen (vl->type)) != 0)
2860 sstrncpy (vl_def->type, ds->type, sizeof (vl_def->type));
2863 if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
2865 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE_INSTANCE,
2867 strlen (vl->type_instance)) != 0)
2869 sstrncpy (vl_def->type_instance, vl->type_instance, sizeof (vl_def->type_instance));
2872 if (write_part_values (&buffer, &buffer_size, ds, vl) != 0)
2875 return (buffer - buffer_orig);
2876 } /* }}} int add_to_buffer */
2878 static void flush_buffer (void)
2880 DEBUG ("network plugin: flush_buffer: send_buffer_fill = %i",
2883 network_send_buffer (send_buffer, (size_t) send_buffer_fill);
2885 stats_octets_tx += ((uint64_t) send_buffer_fill);
2888 network_init_buffer ();
2891 static int network_write (const data_set_t *ds, const value_list_t *vl,
2892 user_data_t __attribute__((unused)) *user_data)
2896 /* listen_loop is set to non-zero in the shutdown callback, which is
2897 * guaranteed to be called *after* all the write threads have been shut
2899 assert (listen_loop == 0);
2901 if (!check_send_okay (vl))
2904 char name[6*DATA_MAX_NAME_LEN];
2905 FORMAT_VL (name, sizeof (name), vl);
2906 name[sizeof (name) - 1] = 0;
2907 DEBUG ("network plugin: network_write: "
2908 "NOT sending %s.", name);
2910 /* Counter is not protected by another lock and may be reached by
2911 * multiple threads */
2912 pthread_mutex_lock (&stats_lock);
2913 stats_values_not_sent++;
2914 pthread_mutex_unlock (&stats_lock);
2918 uc_meta_data_add_unsigned_int (vl,
2919 "network:time_sent", (uint64_t) vl->time);
2921 pthread_mutex_lock (&send_buffer_lock);
2923 status = add_to_buffer (send_buffer_ptr,
2924 network_config_packet_size - (send_buffer_fill + BUFF_SIG_SIZE),
2929 /* status == bytes added to the buffer */
2930 send_buffer_fill += status;
2931 send_buffer_ptr += status;
2933 stats_values_sent++;
2939 status = add_to_buffer (send_buffer_ptr,
2940 network_config_packet_size - (send_buffer_fill + BUFF_SIG_SIZE),
2946 send_buffer_fill += status;
2947 send_buffer_ptr += status;
2949 stats_values_sent++;
2955 ERROR ("network plugin: Unable to append to the "
2956 "buffer for some weird reason");
2958 else if ((network_config_packet_size - send_buffer_fill) < 15)
2963 pthread_mutex_unlock (&send_buffer_lock);
2965 return ((status < 0) ? -1 : 0);
2966 } /* int network_write */
2968 static int network_config_set_boolean (const oconfig_item_t *ci, /* {{{ */
2971 if ((ci->values_num != 1)
2972 || ((ci->values[0].type != OCONFIG_TYPE_BOOLEAN)
2973 && (ci->values[0].type != OCONFIG_TYPE_STRING)))
2975 ERROR ("network plugin: The `%s' config option needs "
2976 "exactly one boolean argument.", ci->key);
2980 if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
2982 if (ci->values[0].value.boolean)
2989 char *str = ci->values[0].value.string;
2993 else if (IS_FALSE (str))
2997 ERROR ("network plugin: Cannot parse string value `%s' of the `%s' "
2998 "option as boolean value.",
3005 } /* }}} int network_config_set_boolean */
3007 static int network_config_set_ttl (const oconfig_item_t *ci) /* {{{ */
3010 if ((ci->values_num != 1)
3011 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
3013 WARNING ("network plugin: The `TimeToLive' config option needs exactly "
3014 "one numeric argument.");
3018 tmp = (int) ci->values[0].value.number;
3019 if ((tmp > 0) && (tmp <= 255))
3020 network_config_ttl = tmp;
3022 WARNING ("network plugin: The `TimeToLive' must be between 1 and 255.");
3027 } /* }}} int network_config_set_ttl */
3029 static int network_config_set_interface (const oconfig_item_t *ci, /* {{{ */
3032 if ((ci->values_num != 1)
3033 || (ci->values[0].type != OCONFIG_TYPE_STRING))
3035 WARNING ("network plugin: The `Interface' config option needs exactly "
3036 "one string argument.");
3040 if (interface == NULL)
3043 *interface = if_nametoindex (ci->values[0].value.string);
3046 } /* }}} int network_config_set_interface */
3048 static int network_config_set_buffer_size (const oconfig_item_t *ci) /* {{{ */
3051 if ((ci->values_num != 1)
3052 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
3054 WARNING ("network plugin: The `MaxPacketSize' config option needs exactly "
3055 "one numeric argument.");
3059 tmp = (int) ci->values[0].value.number;
3060 if ((tmp >= 1024) && (tmp <= 65535))
3061 network_config_packet_size = tmp;
3064 } /* }}} int network_config_set_buffer_size */
3067 static int network_config_set_string (const oconfig_item_t *ci, /* {{{ */
3071 if ((ci->values_num != 1)
3072 || (ci->values[0].type != OCONFIG_TYPE_STRING))
3074 WARNING ("network plugin: The `%s' config option needs exactly "
3075 "one string argument.", ci->key);
3079 tmp = strdup (ci->values[0].value.string);
3083 sfree (*ret_string);
3087 } /* }}} int network_config_set_string */
3088 #endif /* HAVE_LIBGCRYPT */
3091 static int network_config_set_security_level (oconfig_item_t *ci, /* {{{ */
3095 if ((ci->values_num != 1)
3096 || (ci->values[0].type != OCONFIG_TYPE_STRING))
3098 WARNING ("network plugin: The `SecurityLevel' config option needs exactly "
3099 "one string argument.");
3103 str = ci->values[0].value.string;
3104 if (strcasecmp ("Encrypt", str) == 0)
3105 *retval = SECURITY_LEVEL_ENCRYPT;
3106 else if (strcasecmp ("Sign", str) == 0)
3107 *retval = SECURITY_LEVEL_SIGN;
3108 else if (strcasecmp ("None", str) == 0)
3109 *retval = SECURITY_LEVEL_NONE;
3112 WARNING ("network plugin: Unknown security level: %s.", str);
3117 } /* }}} int network_config_set_security_level */
3118 #endif /* HAVE_LIBGCRYPT */
3120 static int network_config_add_listen (const oconfig_item_t *ci) /* {{{ */
3126 if ((ci->values_num < 1) || (ci->values_num > 2)
3127 || (ci->values[0].type != OCONFIG_TYPE_STRING)
3128 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
3130 ERROR ("network plugin: The `%s' config option needs "
3131 "one or two string arguments.", ci->key);
3135 se = sockent_create (SOCKENT_TYPE_SERVER);
3138 ERROR ("network plugin: sockent_create failed.");
3142 se->node = strdup (ci->values[0].value.string);
3143 if (ci->values_num >= 2)
3144 se->service = strdup (ci->values[1].value.string);
3146 for (i = 0; i < ci->children_num; i++)
3148 oconfig_item_t *child = ci->children + i;
3151 if (strcasecmp ("AuthFile", child->key) == 0)
3152 network_config_set_string (child, &se->data.server.auth_file);
3153 else if (strcasecmp ("SecurityLevel", child->key) == 0)
3154 network_config_set_security_level (child,
3155 &se->data.server.security_level);
3157 #endif /* HAVE_LIBGCRYPT */
3158 if (strcasecmp ("Interface", child->key) == 0)
3159 network_config_set_interface (child,
3163 WARNING ("network plugin: Option `%s' is not allowed here.",
3169 if ((se->data.server.security_level > SECURITY_LEVEL_NONE)
3170 && (se->data.server.auth_file == NULL))
3172 ERROR ("network plugin: A security level higher than `none' was "
3173 "requested, but no AuthFile option was given. Cowardly refusing to "
3174 "open this socket!");
3175 sockent_destroy (se);
3178 #endif /* HAVE_LIBGCRYPT */
3180 status = sockent_init_crypto (se);
3183 ERROR ("network plugin: network_config_add_listen: sockent_init_crypto() failed.");
3184 sockent_destroy (se);
3188 status = sockent_server_listen (se);
3191 ERROR ("network plugin: network_config_add_server: sockent_server_listen failed.");
3192 sockent_destroy (se);
3196 status = sockent_add (se);
3199 ERROR ("network plugin: network_config_add_listen: sockent_add failed.");
3200 sockent_destroy (se);
3205 } /* }}} int network_config_add_listen */
3207 static int network_config_add_server (const oconfig_item_t *ci) /* {{{ */
3213 if ((ci->values_num < 1) || (ci->values_num > 2)
3214 || (ci->values[0].type != OCONFIG_TYPE_STRING)
3215 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
3217 ERROR ("network plugin: The `%s' config option needs "
3218 "one or two string arguments.", ci->key);
3222 se = sockent_create (SOCKENT_TYPE_CLIENT);
3225 ERROR ("network plugin: sockent_create failed.");
3229 se->node = strdup (ci->values[0].value.string);
3230 if (ci->values_num >= 2)
3231 se->service = strdup (ci->values[1].value.string);
3233 for (i = 0; i < ci->children_num; i++)
3235 oconfig_item_t *child = ci->children + i;
3238 if (strcasecmp ("Username", child->key) == 0)
3239 network_config_set_string (child, &se->data.client.username);
3240 else if (strcasecmp ("Password", child->key) == 0)
3241 network_config_set_string (child, &se->data.client.password);
3242 else if (strcasecmp ("SecurityLevel", child->key) == 0)
3243 network_config_set_security_level (child,
3244 &se->data.client.security_level);
3246 #endif /* HAVE_LIBGCRYPT */
3247 if (strcasecmp ("Interface", child->key) == 0)
3248 network_config_set_interface (child,
3252 WARNING ("network plugin: Option `%s' is not allowed here.",
3258 if ((se->data.client.security_level > SECURITY_LEVEL_NONE)
3259 && ((se->data.client.username == NULL)
3260 || (se->data.client.password == NULL)))
3262 ERROR ("network plugin: A security level higher than `none' was "
3263 "requested, but no Username or Password option was given. "
3264 "Cowardly refusing to open this socket!");
3265 sockent_destroy (se);
3268 #endif /* HAVE_LIBGCRYPT */
3270 status = sockent_init_crypto (se);
3273 ERROR ("network plugin: network_config_add_server: sockent_init_crypto() failed.");
3274 sockent_destroy (se);
3278 /* No call to sockent_client_connect() here -- it is called from
3279 * networt_send_buffer_plain(). */
3281 status = sockent_add (se);
3284 ERROR ("network plugin: network_config_add_server: sockent_add failed.");
3285 sockent_destroy (se);
3290 } /* }}} int network_config_add_server */
3292 static int network_config (oconfig_item_t *ci) /* {{{ */
3296 /* The options need to be applied first */
3297 for (i = 0; i < ci->children_num; i++)
3299 oconfig_item_t *child = ci->children + i;
3300 if (strcasecmp ("TimeToLive", child->key) == 0)
3301 network_config_set_ttl (child);
3304 for (i = 0; i < ci->children_num; i++)
3306 oconfig_item_t *child = ci->children + i;
3308 if (strcasecmp ("Listen", child->key) == 0)
3309 network_config_add_listen (child);
3310 else if (strcasecmp ("Server", child->key) == 0)
3311 network_config_add_server (child);
3312 else if (strcasecmp ("TimeToLive", child->key) == 0) {
3313 /* Handled earlier */
3315 else if (strcasecmp ("MaxPacketSize", child->key) == 0)
3316 network_config_set_buffer_size (child);
3317 else if (strcasecmp ("Forward", child->key) == 0)
3318 network_config_set_boolean (child, &network_config_forward);
3319 else if (strcasecmp ("ReportStats", child->key) == 0)
3320 network_config_set_boolean (child, &network_config_stats);
3323 WARNING ("network plugin: Option `%s' is not allowed here.",
3329 } /* }}} int network_config */
3331 static int network_notification (const notification_t *n,
3332 user_data_t __attribute__((unused)) *user_data)
3334 char buffer[network_config_packet_size];
3335 char *buffer_ptr = buffer;
3336 int buffer_free = sizeof (buffer);
3339 if (!check_send_notify_okay (n))
3342 memset (buffer, 0, sizeof (buffer));
3344 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_TIME_HR,
3345 (uint64_t) n->time);
3349 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_SEVERITY,
3350 (uint64_t) n->severity);
3354 if (strlen (n->host) > 0)
3356 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_HOST,
3357 n->host, strlen (n->host));
3362 if (strlen (n->plugin) > 0)
3364 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_PLUGIN,
3365 n->plugin, strlen (n->plugin));
3370 if (strlen (n->plugin_instance) > 0)
3372 status = write_part_string (&buffer_ptr, &buffer_free,
3373 TYPE_PLUGIN_INSTANCE,
3374 n->plugin_instance, strlen (n->plugin_instance));
3379 if (strlen (n->type) > 0)
3381 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE,
3382 n->type, strlen (n->type));
3387 if (strlen (n->type_instance) > 0)
3389 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE_INSTANCE,
3390 n->type_instance, strlen (n->type_instance));
3395 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_MESSAGE,
3396 n->message, strlen (n->message));
3400 network_send_buffer (buffer, sizeof (buffer) - buffer_free);
3403 } /* int network_notification */
3405 static int network_shutdown (void)
3411 /* Kill the listening thread */
3412 if (receive_thread_running != 0)
3414 INFO ("network plugin: Stopping receive thread.");
3415 pthread_kill (receive_thread_id, SIGTERM);
3416 pthread_join (receive_thread_id, NULL /* no return value */);
3417 memset (&receive_thread_id, 0, sizeof (receive_thread_id));
3418 receive_thread_running = 0;
3421 /* Shutdown the dispatching thread */
3422 if (dispatch_thread_running != 0)
3424 INFO ("network plugin: Stopping dispatch thread.");
3425 pthread_mutex_lock (&receive_list_lock);
3426 pthread_cond_broadcast (&receive_list_cond);
3427 pthread_mutex_unlock (&receive_list_lock);
3428 pthread_join (dispatch_thread_id, /* ret = */ NULL);
3429 dispatch_thread_running = 0;
3432 sockent_destroy (listen_sockets);
3434 if (send_buffer_fill > 0)
3437 sfree (send_buffer);
3439 for (se = sending_sockets; se != NULL; se = se->next)
3440 sockent_client_disconnect (se);
3441 sockent_destroy (sending_sockets);
3443 plugin_unregister_config ("network");
3444 plugin_unregister_init ("network");
3445 plugin_unregister_write ("network");
3446 plugin_unregister_shutdown ("network");
3449 } /* int network_shutdown */
3451 static int network_stats_read (void) /* {{{ */
3453 derive_t copy_octets_rx;
3454 derive_t copy_octets_tx;
3455 derive_t copy_packets_rx;
3456 derive_t copy_packets_tx;
3457 derive_t copy_values_dispatched;
3458 derive_t copy_values_not_dispatched;
3459 derive_t copy_values_sent;
3460 derive_t copy_values_not_sent;
3461 derive_t copy_receive_list_length;
3462 value_list_t vl = VALUE_LIST_INIT;
3465 copy_octets_rx = stats_octets_rx;
3466 copy_octets_tx = stats_octets_tx;
3467 copy_packets_rx = stats_packets_rx;
3468 copy_packets_tx = stats_packets_tx;
3469 copy_values_dispatched = stats_values_dispatched;
3470 copy_values_not_dispatched = stats_values_not_dispatched;
3471 copy_values_sent = stats_values_sent;
3472 copy_values_not_sent = stats_values_not_sent;
3473 copy_receive_list_length = receive_list_length;
3475 /* Initialize `vl' */
3479 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
3480 sstrncpy (vl.plugin, "network", sizeof (vl.plugin));
3482 /* Octets received / sent */
3483 vl.values[0].derive = (derive_t) copy_octets_rx;
3484 vl.values[1].derive = (derive_t) copy_octets_tx;
3485 sstrncpy (vl.type, "if_octets", sizeof (vl.type));
3486 plugin_dispatch_values (&vl);
3488 /* Packets received / send */
3489 vl.values[0].derive = (derive_t) copy_packets_rx;
3490 vl.values[1].derive = (derive_t) copy_packets_tx;
3491 sstrncpy (vl.type, "if_packets", sizeof (vl.type));
3492 plugin_dispatch_values (&vl);
3494 /* Values (not) dispatched and (not) send */
3495 sstrncpy (vl.type, "total_values", sizeof (vl.type));
3498 vl.values[0].derive = (derive_t) copy_values_dispatched;
3499 sstrncpy (vl.type_instance, "dispatch-accepted",
3500 sizeof (vl.type_instance));
3501 plugin_dispatch_values (&vl);
3503 vl.values[0].derive = (derive_t) copy_values_not_dispatched;
3504 sstrncpy (vl.type_instance, "dispatch-rejected",
3505 sizeof (vl.type_instance));
3506 plugin_dispatch_values (&vl);
3508 vl.values[0].derive = (derive_t) copy_values_sent;
3509 sstrncpy (vl.type_instance, "send-accepted",
3510 sizeof (vl.type_instance));
3511 plugin_dispatch_values (&vl);
3513 vl.values[0].derive = (derive_t) copy_values_not_sent;
3514 sstrncpy (vl.type_instance, "send-rejected",
3515 sizeof (vl.type_instance));
3516 plugin_dispatch_values (&vl);
3518 /* Receive queue length */
3519 vl.values[0].gauge = (gauge_t) copy_receive_list_length;
3520 sstrncpy (vl.type, "queue_length", sizeof (vl.type));
3521 vl.type_instance[0] = 0;
3522 plugin_dispatch_values (&vl);
3525 } /* }}} int network_stats_read */
3527 static int network_init (void)
3529 static _Bool have_init = 0;
3531 /* Check if we were already initialized. If so, just return - there's
3532 * nothing more to do (for now, that is). */
3538 if (network_init_gcrypt () < 0)
3540 ERROR ("network plugin: Failed to initialize crypto library.");
3545 if (network_config_stats != 0)
3546 plugin_register_read ("network", network_stats_read);
3548 plugin_register_shutdown ("network", network_shutdown);
3550 send_buffer = malloc (network_config_packet_size);
3551 if (send_buffer == NULL)
3553 ERROR ("network plugin: malloc failed.");
3556 network_init_buffer ();
3558 /* setup socket(s) and so on */
3559 if (sending_sockets != NULL)
3561 plugin_register_write ("network", network_write,
3562 /* user_data = */ NULL);
3563 plugin_register_notification ("network", network_notification,
3564 /* user_data = */ NULL);
3567 /* If no threads need to be started, return here. */
3568 if ((listen_sockets_num == 0)
3569 || ((dispatch_thread_running != 0)
3570 && (receive_thread_running != 0)))
3573 if (dispatch_thread_running == 0)
3576 status = plugin_thread_create (&dispatch_thread_id,
3577 NULL /* no attributes */,
3579 NULL /* no argument */);
3583 ERROR ("network: pthread_create failed: %s",
3584 sstrerror (errno, errbuf,
3589 dispatch_thread_running = 1;
3593 if (receive_thread_running == 0)
3596 status = plugin_thread_create (&receive_thread_id,
3597 NULL /* no attributes */,
3599 NULL /* no argument */);
3603 ERROR ("network: pthread_create failed: %s",
3604 sstrerror (errno, errbuf,
3609 receive_thread_running = 1;
3614 } /* int network_init */
3617 * The flush option of the network plugin cannot flush individual identifiers.
3618 * All the values are added to a buffer and sent when the buffer is full, the
3619 * requested value may or may not be in there, it's not worth finding out. We
3620 * just send the buffer if `flush' is called - if the requested value was in
3621 * there, good. If not, well, then there is nothing to flush.. -octo
3623 static int network_flush (__attribute__((unused)) cdtime_t timeout,
3624 __attribute__((unused)) const char *identifier,
3625 __attribute__((unused)) user_data_t *user_data)
3627 pthread_mutex_lock (&send_buffer_lock);
3629 if (send_buffer_fill > 0)
3632 pthread_mutex_unlock (&send_buffer_lock);
3635 } /* int network_flush */
3637 void module_register (void)
3639 plugin_register_complex_config ("network", network_config);
3640 plugin_register_init ("network", network_init);
3641 plugin_register_flush ("network", network_flush,
3642 /* user_data = */ NULL);
3643 } /* void module_register */
3645 /* vim: set fdm=marker : */