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 <netinet/in.h>
46 # include <arpa/inet.h>
56 # if defined __APPLE__
57 /* default xcode compiler throws warnings even when deprecated functionality
58 * is not used. -Werror breaks the build because of erroneous warnings.
59 * http://stackoverflow.com/questions/10556299/compiler-warnings-with-libgcrypt-v1-5-0/12830209#12830209
61 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
63 /* FreeBSD's copy of libgcrypt extends the existing GCRYPT_NO_DEPRECATED
64 * to properly hide all deprecated functionality.
65 * http://svnweb.freebsd.org/ports/head/security/libgcrypt/files/patch-src__gcrypt.h.in
67 # define GCRYPT_NO_DEPRECATED
69 # if defined __APPLE__
70 /* Re enable deprecation warnings */
71 # pragma GCC diagnostic warning "-Wdeprecated-declarations"
73 # if GCRYPT_VERSION_NUMBER < 0x010600
74 GCRY_THREAD_OPTION_PTHREAD_IMPL;
78 #ifndef IPV6_ADD_MEMBERSHIP
79 # ifdef IPV6_JOIN_GROUP
80 # define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
82 # error "Neither IP_ADD_MEMBERSHIP nor IPV6_JOIN_GROUP is defined"
84 #endif /* !IP_ADD_MEMBERSHIP */
87 * Maximum size required for encryption / signing:
89 * 42 bytes for the encryption header
90 * + 64 bytes for the username
94 #define BUFF_SIG_SIZE 106
99 #define SECURITY_LEVEL_NONE 0
101 # define SECURITY_LEVEL_SIGN 1
102 # define SECURITY_LEVEL_ENCRYPT 2
104 struct sockent_client
107 struct sockaddr_storage *addr;
113 gcry_cipher_hd_t cypher;
114 unsigned char password_hash[32];
116 cdtime_t next_resolve_reconnect;
117 cdtime_t resolve_interval;
120 struct sockent_server
128 gcry_cipher_hd_t cypher;
132 typedef struct sockent
134 #define SOCKENT_TYPE_CLIENT 1
135 #define SOCKENT_TYPE_SERVER 2
144 struct sockent_client client;
145 struct sockent_server server;
148 struct sockent *next;
151 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
152 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
153 * +-------+-----------------------+-------------------------------+
154 * ! Ver. ! ! Length !
155 * +-------+-----------------------+-------------------------------+
162 typedef struct part_header_s part_header_t;
164 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
165 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
166 * +-------------------------------+-------------------------------+
168 * +-------------------------------+-------------------------------+
169 * : (Length - 4) Bytes :
170 * +---------------------------------------------------------------+
177 typedef struct part_string_s part_string_t;
179 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
180 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
181 * +-------------------------------+-------------------------------+
183 * +-------------------------------+-------------------------------+
184 * : (Length - 4 == 2 || 4 || 8) Bytes :
185 * +---------------------------------------------------------------+
192 typedef struct part_number_s part_number_t;
194 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
195 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
196 * +-------------------------------+-------------------------------+
198 * +-------------------------------+---------------+---------------+
199 * ! Num of values ! Type0 ! Type1 !
200 * +-------------------------------+---------------+---------------+
203 * +---------------------------------------------------------------+
206 * +---------------------------------------------------------------+
211 uint16_t *num_values;
212 uint8_t *values_types;
215 typedef struct part_values_s part_values_t;
217 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
218 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
219 * +-------------------------------+-------------------------------+
221 * +-------------------------------+-------------------------------+
222 * ! Hash (Bits 0 - 31) !
224 * ! Hash (Bits 224 - 255) !
225 * +---------------------------------------------------------------+
228 #define PART_SIGNATURE_SHA256_SIZE 36
229 struct part_signature_sha256_s
232 unsigned char hash[32];
235 typedef struct part_signature_sha256_s part_signature_sha256_t;
237 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
238 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
239 * +-------------------------------+-------------------------------+
241 * +-------------------------------+-------------------------------+
242 * ! Original length ! Padding (0 - 15 bytes) !
243 * +-------------------------------+-------------------------------+
244 * ! Hash (Bits 0 - 31) !
246 * ! Hash (Bits 128 - 159) !
247 * +---------------------------------------------------------------+
250 #define PART_ENCRYPTION_AES256_SIZE 42
251 struct part_encryption_aes256_s
254 uint16_t username_length;
256 unsigned char iv[16];
258 unsigned char hash[20];
262 typedef struct part_encryption_aes256_s part_encryption_aes256_t;
264 struct receive_list_entry_s
269 struct receive_list_entry_s *next;
271 typedef struct receive_list_entry_s receive_list_entry_t;
276 static int network_config_ttl = 0;
277 /* Ethernet - (IPv6 + UDP) = 1500 - (40 + 8) = 1452 */
278 static size_t network_config_packet_size = 1452;
279 static _Bool network_config_forward = 0;
280 static _Bool network_config_stats = 0;
282 static sockent_t *sending_sockets = NULL;
284 static receive_list_entry_t *receive_list_head = NULL;
285 static receive_list_entry_t *receive_list_tail = NULL;
286 static pthread_mutex_t receive_list_lock = PTHREAD_MUTEX_INITIALIZER;
287 static pthread_cond_t receive_list_cond = PTHREAD_COND_INITIALIZER;
288 static uint64_t receive_list_length = 0;
290 static sockent_t *listen_sockets = NULL;
291 static struct pollfd *listen_sockets_pollfd = NULL;
292 static size_t listen_sockets_num = 0;
294 /* The receive and dispatch threads will run as long as `listen_loop' is set to
296 static int listen_loop = 0;
297 static int receive_thread_running = 0;
298 static pthread_t receive_thread_id;
299 static int dispatch_thread_running = 0;
300 static pthread_t dispatch_thread_id;
302 /* Buffer in which to-be-sent network packets are constructed. */
303 static char *send_buffer;
304 static char *send_buffer_ptr;
305 static int send_buffer_fill;
306 static cdtime_t send_buffer_last_update;
307 static value_list_t send_buffer_vl = VALUE_LIST_STATIC;
308 static pthread_mutex_t send_buffer_lock = PTHREAD_MUTEX_INITIALIZER;
310 /* XXX: These counters are incremented from one place only. The spot in which
311 * the values are incremented is either only reachable by one thread (the
312 * dispatch thread, for example) or locked by some lock (send_buffer_lock for
313 * example). Only if neither is true, the stats_lock is acquired. The counters
314 * are always read without holding a lock in the hope that writing 8 bytes to
315 * memory is an atomic operation. */
316 static derive_t stats_octets_rx = 0;
317 static derive_t stats_octets_tx = 0;
318 static derive_t stats_packets_rx = 0;
319 static derive_t stats_packets_tx = 0;
320 static derive_t stats_values_dispatched = 0;
321 static derive_t stats_values_not_dispatched = 0;
322 static derive_t stats_values_sent = 0;
323 static derive_t stats_values_not_sent = 0;
324 static pthread_mutex_t stats_lock = PTHREAD_MUTEX_INITIALIZER;
329 static _Bool check_receive_okay (const value_list_t *vl) /* {{{ */
331 uint64_t time_sent = 0;
334 status = uc_meta_data_get_unsigned_int (vl,
335 "network:time_sent", &time_sent);
337 /* This is a value we already sent. Don't allow it to be received again in
338 * order to avoid looping. */
339 if ((status == 0) && (time_sent >= ((uint64_t) vl->time)))
343 } /* }}} _Bool check_receive_okay */
345 static _Bool check_send_okay (const value_list_t *vl) /* {{{ */
350 if (network_config_forward)
353 if (vl->meta == NULL)
356 status = meta_data_get_boolean (vl->meta, "network:received", &received);
357 if (status == -ENOENT)
359 else if (status != 0)
361 ERROR ("network plugin: check_send_okay: meta_data_get_boolean failed "
362 "with status %i.", status);
366 /* By default, only *send* value lists that were not *received* by the
369 } /* }}} _Bool check_send_okay */
371 static _Bool check_notify_received (const notification_t *n) /* {{{ */
373 notification_meta_t *ptr;
375 for (ptr = n->meta; ptr != NULL; ptr = ptr->next)
376 if ((strcmp ("network:received", ptr->name) == 0)
377 && (ptr->type == NM_TYPE_BOOLEAN))
378 return ((_Bool) ptr->nm_value.nm_boolean);
381 } /* }}} _Bool check_notify_received */
383 static _Bool check_send_notify_okay (const notification_t *n) /* {{{ */
385 static c_complain_t complain_forwarding = C_COMPLAIN_INIT_STATIC;
391 received = check_notify_received (n);
393 if (network_config_forward && received)
395 c_complain_once (LOG_ERR, &complain_forwarding,
396 "network plugin: A notification has been received via the network "
397 "and forwarding is enabled. Forwarding of notifications is currently "
398 "not supported, because there is not loop-deteciton available. "
399 "Please contact the collectd mailing list if you need this "
403 /* By default, only *send* value lists that were not *received* by the
406 } /* }}} _Bool check_send_notify_okay */
408 static int network_dispatch_values (value_list_t *vl, /* {{{ */
409 const char *username)
414 || (strlen (vl->host) <= 0)
415 || (strlen (vl->plugin) <= 0)
416 || (strlen (vl->type) <= 0))
419 if (!check_receive_okay (vl))
422 char name[6*DATA_MAX_NAME_LEN];
423 FORMAT_VL (name, sizeof (name), vl);
424 name[sizeof (name) - 1] = 0;
425 DEBUG ("network plugin: network_dispatch_values: "
426 "NOT dispatching %s.", name);
428 stats_values_not_dispatched++;
432 assert (vl->meta == NULL);
434 vl->meta = meta_data_create ();
435 if (vl->meta == NULL)
437 ERROR ("network plugin: meta_data_create failed.");
441 status = meta_data_add_boolean (vl->meta, "network:received", 1);
444 ERROR ("network plugin: meta_data_add_boolean failed.");
445 meta_data_destroy (vl->meta);
450 if (username != NULL)
452 status = meta_data_add_string (vl->meta, "network:username", username);
455 ERROR ("network plugin: meta_data_add_string failed.");
456 meta_data_destroy (vl->meta);
462 plugin_dispatch_values (vl);
463 stats_values_dispatched++;
465 meta_data_destroy (vl->meta);
469 } /* }}} int network_dispatch_values */
471 static int network_dispatch_notification (notification_t *n) /* {{{ */
475 assert (n->meta == NULL);
477 status = plugin_notification_meta_add_boolean (n, "network:received", 1);
480 ERROR ("network plugin: plugin_notification_meta_add_boolean failed.");
481 plugin_notification_meta_free (n->meta);
486 status = plugin_dispatch_notification (n);
488 plugin_notification_meta_free (n->meta);
492 } /* }}} int network_dispatch_notification */
495 static void network_init_gcrypt (void) /* {{{ */
497 /* http://lists.gnupg.org/pipermail/gcrypt-devel/2003-August/000458.html
498 * Because you can't know in a library whether another library has
499 * already initialized the library */
500 if (gcry_control (GCRYCTL_ANY_INITIALIZATION_P))
503 /* http://www.gnupg.org/documentation/manuals/gcrypt/Multi_002dThreading.html
504 * To ensure thread-safety, it's important to set GCRYCTL_SET_THREAD_CBS
505 * *before* initalizing Libgcrypt with gcry_check_version(), which itself must
506 * be called before any other gcry_* function. GCRYCTL_ANY_INITIALIZATION_P
507 * above doesn't count, as it doesn't implicitly initalize Libgcrypt.
509 * tl;dr: keep all these gry_* statements in this exact order please. */
510 # if GCRYPT_VERSION_NUMBER < 0x010600
511 gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
513 gcry_check_version (NULL);
514 gcry_control (GCRYCTL_INIT_SECMEM, 32768);
515 gcry_control (GCRYCTL_INITIALIZATION_FINISHED);
516 } /* }}} void network_init_gcrypt */
518 static gcry_cipher_hd_t network_get_aes256_cypher (sockent_t *se, /* {{{ */
519 const void *iv, size_t iv_size, const char *username)
522 gcry_cipher_hd_t *cyper_ptr;
523 unsigned char password_hash[32];
525 if (se->type == SOCKENT_TYPE_CLIENT)
527 cyper_ptr = &se->data.client.cypher;
528 memcpy (password_hash, se->data.client.password_hash,
529 sizeof (password_hash));
535 cyper_ptr = &se->data.server.cypher;
537 if (username == NULL)
540 secret = fbh_get (se->data.server.userdb, username);
544 gcry_md_hash_buffer (GCRY_MD_SHA256,
546 secret, strlen (secret));
551 if (*cyper_ptr == NULL)
553 err = gcry_cipher_open (cyper_ptr,
554 GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_OFB, /* flags = */ 0);
557 ERROR ("network plugin: gcry_cipher_open returned: %s",
558 gcry_strerror (err));
565 gcry_cipher_reset (*cyper_ptr);
567 assert (*cyper_ptr != NULL);
569 err = gcry_cipher_setkey (*cyper_ptr,
570 password_hash, sizeof (password_hash));
573 ERROR ("network plugin: gcry_cipher_setkey returned: %s",
574 gcry_strerror (err));
575 gcry_cipher_close (*cyper_ptr);
580 err = gcry_cipher_setiv (*cyper_ptr, iv, iv_size);
583 ERROR ("network plugin: gcry_cipher_setkey returned: %s",
584 gcry_strerror (err));
585 gcry_cipher_close (*cyper_ptr);
591 } /* }}} int network_get_aes256_cypher */
592 #endif /* HAVE_LIBGCRYPT */
594 static int write_part_values (char **ret_buffer, int *ret_buffer_len,
595 const data_set_t *ds, const value_list_t *vl)
601 part_header_t pkg_ph;
602 uint16_t pkg_num_values;
603 uint8_t *pkg_values_types;
609 num_values = vl->values_len;
610 packet_len = sizeof (part_header_t) + sizeof (uint16_t)
611 + (num_values * sizeof (uint8_t))
612 + (num_values * sizeof (value_t));
614 if (*ret_buffer_len < packet_len)
617 pkg_values_types = malloc (num_values * sizeof (*pkg_values_types));
618 if (pkg_values_types == NULL)
620 ERROR ("network plugin: write_part_values: malloc failed.");
624 pkg_values = malloc (num_values * sizeof (*pkg_values));
625 if (pkg_values == NULL)
627 free (pkg_values_types);
628 ERROR ("network plugin: write_part_values: malloc failed.");
632 pkg_ph.type = htons (TYPE_VALUES);
633 pkg_ph.length = htons (packet_len);
635 pkg_num_values = htons ((uint16_t) vl->values_len);
637 for (i = 0; i < num_values; i++)
639 pkg_values_types[i] = (uint8_t) ds->ds[i].type;
640 switch (ds->ds[i].type)
642 case DS_TYPE_COUNTER:
643 pkg_values[i].counter = htonll (vl->values[i].counter);
647 pkg_values[i].gauge = htond (vl->values[i].gauge);
651 pkg_values[i].derive = htonll (vl->values[i].derive);
654 case DS_TYPE_ABSOLUTE:
655 pkg_values[i].absolute = htonll (vl->values[i].absolute);
659 free (pkg_values_types);
661 ERROR ("network plugin: write_part_values: "
662 "Unknown data source type: %i",
665 } /* switch (ds->ds[i].type) */
666 } /* for (num_values) */
669 * Use `memcpy' to write everything to the buffer, because the pointer
670 * may be unaligned and some architectures, such as SPARC, can't handle
673 packet_ptr = *ret_buffer;
675 memcpy (packet_ptr + offset, &pkg_ph, sizeof (pkg_ph));
676 offset += sizeof (pkg_ph);
677 memcpy (packet_ptr + offset, &pkg_num_values, sizeof (pkg_num_values));
678 offset += sizeof (pkg_num_values);
679 memcpy (packet_ptr + offset, pkg_values_types, num_values * sizeof (uint8_t));
680 offset += num_values * sizeof (uint8_t);
681 memcpy (packet_ptr + offset, pkg_values, num_values * sizeof (value_t));
682 offset += num_values * sizeof (value_t);
684 assert (offset == packet_len);
686 *ret_buffer = packet_ptr + packet_len;
687 *ret_buffer_len -= packet_len;
689 free (pkg_values_types);
693 } /* int write_part_values */
695 static int write_part_number (char **ret_buffer, int *ret_buffer_len,
696 int type, uint64_t value)
701 part_header_t pkg_head;
706 packet_len = sizeof (pkg_head) + sizeof (pkg_value);
708 if (*ret_buffer_len < packet_len)
711 pkg_head.type = htons (type);
712 pkg_head.length = htons (packet_len);
713 pkg_value = htonll (value);
715 packet_ptr = *ret_buffer;
717 memcpy (packet_ptr + offset, &pkg_head, sizeof (pkg_head));
718 offset += sizeof (pkg_head);
719 memcpy (packet_ptr + offset, &pkg_value, sizeof (pkg_value));
720 offset += sizeof (pkg_value);
722 assert (offset == packet_len);
724 *ret_buffer = packet_ptr + packet_len;
725 *ret_buffer_len -= packet_len;
728 } /* int write_part_number */
730 static int write_part_string (char **ret_buffer, int *ret_buffer_len,
731 int type, const char *str, int str_len)
741 buffer_len = 2 * sizeof (uint16_t) + str_len + 1;
742 if (*ret_buffer_len < buffer_len)
745 pkg_type = htons (type);
746 pkg_length = htons (buffer_len);
748 buffer = *ret_buffer;
750 memcpy (buffer + offset, (void *) &pkg_type, sizeof (pkg_type));
751 offset += sizeof (pkg_type);
752 memcpy (buffer + offset, (void *) &pkg_length, sizeof (pkg_length));
753 offset += sizeof (pkg_length);
754 memcpy (buffer + offset, str, str_len);
756 memset (buffer + offset, '\0', 1);
759 assert (offset == buffer_len);
761 *ret_buffer = buffer + buffer_len;
762 *ret_buffer_len -= buffer_len;
765 } /* int write_part_string */
767 static int parse_part_values (void **ret_buffer, size_t *ret_buffer_len,
768 value_t **ret_values, size_t *ret_num_values)
770 char *buffer = *ret_buffer;
771 size_t buffer_len = *ret_buffer_len;
786 NOTICE ("network plugin: packet is too short: "
787 "buffer_len = %zu", buffer_len);
791 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
792 buffer += sizeof (tmp16);
793 pkg_type = ntohs (tmp16);
795 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
796 buffer += sizeof (tmp16);
797 pkg_length = ntohs (tmp16);
799 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
800 buffer += sizeof (tmp16);
801 pkg_numval = (size_t) ntohs (tmp16);
803 assert (pkg_type == TYPE_VALUES);
805 exp_size = 3 * sizeof (uint16_t)
806 + pkg_numval * (sizeof (uint8_t) + sizeof (value_t));
807 if (buffer_len < exp_size)
809 WARNING ("network plugin: parse_part_values: "
811 "Chunk of size %zu expected, "
812 "but buffer has only %zu bytes left.",
813 exp_size, buffer_len);
816 assert (pkg_numval <= ((buffer_len - 6) / 9));
818 if (pkg_length != exp_size)
820 WARNING ("network plugin: parse_part_values: "
821 "Length and number of values "
822 "in the packet don't match.");
826 pkg_types = calloc (pkg_numval, sizeof (*pkg_types));
827 pkg_values = calloc (pkg_numval, sizeof (*pkg_values));
828 if ((pkg_types == NULL) || (pkg_values == NULL))
832 ERROR ("network plugin: parse_part_values: calloc failed.");
836 memcpy (pkg_types, buffer, pkg_numval * sizeof (*pkg_types));
837 buffer += pkg_numval * sizeof (*pkg_types);
838 memcpy (pkg_values, buffer, pkg_numval * sizeof (*pkg_values));
839 buffer += pkg_numval * sizeof (*pkg_values);
841 for (i = 0; i < pkg_numval; i++)
843 switch (pkg_types[i])
845 case DS_TYPE_COUNTER:
846 pkg_values[i].counter = (counter_t) ntohll (pkg_values[i].counter);
850 pkg_values[i].gauge = (gauge_t) ntohd (pkg_values[i].gauge);
854 pkg_values[i].derive = (derive_t) ntohll (pkg_values[i].derive);
857 case DS_TYPE_ABSOLUTE:
858 pkg_values[i].absolute = (absolute_t) ntohll (pkg_values[i].absolute);
862 NOTICE ("network plugin: parse_part_values: "
863 "Don't know how to handle data source type %"PRIu8,
868 } /* switch (pkg_types[i]) */
871 *ret_buffer = buffer;
872 *ret_buffer_len = buffer_len - pkg_length;
873 *ret_num_values = pkg_numval;
874 *ret_values = pkg_values;
879 } /* int parse_part_values */
881 static int parse_part_number (void **ret_buffer, size_t *ret_buffer_len,
884 char *buffer = *ret_buffer;
885 size_t buffer_len = *ret_buffer_len;
889 size_t exp_size = 2 * sizeof (uint16_t) + sizeof (uint64_t);
893 if (buffer_len < exp_size)
895 WARNING ("network plugin: parse_part_number: "
897 "Chunk of size %zu expected, "
898 "but buffer has only %zu bytes left.",
899 exp_size, buffer_len);
903 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
904 buffer += sizeof (tmp16);
905 /* pkg_type = ntohs (tmp16); */
907 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
908 buffer += sizeof (tmp16);
909 pkg_length = ntohs (tmp16);
911 memcpy ((void *) &tmp64, buffer, sizeof (tmp64));
912 buffer += sizeof (tmp64);
913 *value = ntohll (tmp64);
915 *ret_buffer = buffer;
916 *ret_buffer_len = buffer_len - pkg_length;
919 } /* int parse_part_number */
921 static int parse_part_string (void **ret_buffer, size_t *ret_buffer_len,
922 char *output, size_t const output_len)
924 char *buffer = *ret_buffer;
925 size_t buffer_len = *ret_buffer_len;
928 size_t const header_size = 2 * sizeof (uint16_t);
936 if (buffer_len < header_size)
938 WARNING ("network plugin: parse_part_string: "
940 "Chunk of at least size %zu expected, "
941 "but buffer has only %zu bytes left.",
942 header_size, buffer_len);
946 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
947 buffer += sizeof (tmp16);
948 /* pkg_type = ntohs (tmp16); */
950 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
951 buffer += sizeof (tmp16);
952 pkg_length = ntohs (tmp16);
953 payload_size = ((size_t) pkg_length) - header_size;
955 /* Check that packet fits in the input buffer */
956 if (pkg_length > buffer_len)
958 WARNING ("network plugin: parse_part_string: "
960 "Chunk of size %"PRIu16" received, "
961 "but buffer has only %zu bytes left.",
962 pkg_length, buffer_len);
966 /* Check that pkg_length is in the valid range */
967 if (pkg_length <= header_size)
969 WARNING ("network plugin: parse_part_string: "
971 "Header claims this packet is only %hu "
972 "bytes long.", pkg_length);
976 /* Check that the package data fits into the output buffer.
977 * The previous if-statement ensures that:
978 * `pkg_length > header_size' */
979 if (output_len < payload_size)
981 WARNING ("network plugin: parse_part_string: "
983 "Output buffer holds %zu bytes, "
984 "which is too small to hold the received "
986 output_len, payload_size);
990 /* All sanity checks successfull, let's copy the data over */
991 memcpy ((void *) output, (void *) buffer, payload_size);
992 buffer += payload_size;
994 /* For some very weird reason '\0' doesn't do the trick on SPARC in
996 if (output[payload_size - 1] != 0)
998 WARNING ("network plugin: parse_part_string: "
999 "Received string does not end "
1000 "with a NULL-byte.");
1004 *ret_buffer = buffer;
1005 *ret_buffer_len = buffer_len - pkg_length;
1008 } /* int parse_part_string */
1010 /* Forward declaration: parse_part_sign_sha256 and parse_part_encr_aes256 call
1011 * parse_packet and vice versa. */
1012 #define PP_SIGNED 0x01
1013 #define PP_ENCRYPTED 0x02
1014 static int parse_packet (sockent_t *se,
1015 void *buffer, size_t buffer_size, int flags,
1016 const char *username);
1018 #define BUFFER_READ(p,s) do { \
1019 memcpy ((p), buffer + buffer_offset, (s)); \
1020 buffer_offset += (s); \
1024 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
1025 void **ret_buffer, size_t *ret_buffer_len, int flags)
1027 static c_complain_t complain_no_users = C_COMPLAIN_INIT_STATIC;
1031 size_t buffer_offset;
1033 size_t username_len;
1036 part_signature_sha256_t pss;
1037 uint16_t pss_head_length;
1038 char hash[sizeof (pss.hash)];
1042 unsigned char *hash_ptr;
1044 buffer = *ret_buffer;
1045 buffer_len = *ret_buffer_len;
1048 if (se->data.server.userdb == NULL)
1050 c_complain (LOG_NOTICE, &complain_no_users,
1051 "network plugin: Received signed network packet but can't verify it "
1052 "because no user DB has been configured. Will accept it.");
1056 /* Check if the buffer has enough data for this structure. */
1057 if (buffer_len <= PART_SIGNATURE_SHA256_SIZE)
1060 /* Read type and length header */
1061 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
1062 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
1063 pss_head_length = ntohs (pss.head.length);
1065 /* Check if the `pss_head_length' is within bounds. */
1066 if ((pss_head_length <= PART_SIGNATURE_SHA256_SIZE)
1067 || (pss_head_length > buffer_len))
1069 ERROR ("network plugin: HMAC-SHA-256 with invalid length received.");
1073 /* Copy the hash. */
1074 BUFFER_READ (pss.hash, sizeof (pss.hash));
1076 /* Calculate username length (without null byte) and allocate memory */
1077 username_len = pss_head_length - PART_SIGNATURE_SHA256_SIZE;
1078 pss.username = malloc (username_len + 1);
1079 if (pss.username == NULL)
1082 /* Read the username */
1083 BUFFER_READ (pss.username, username_len);
1084 pss.username[username_len] = 0;
1086 assert (buffer_offset == pss_head_length);
1088 /* Query the password */
1089 secret = fbh_get (se->data.server.userdb, pss.username);
1092 ERROR ("network plugin: Unknown user: %s", pss.username);
1093 sfree (pss.username);
1097 /* Create a hash device and check the HMAC */
1099 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
1102 ERROR ("network plugin: Creating HMAC-SHA-256 object failed: %s",
1103 gcry_strerror (err));
1105 sfree (pss.username);
1109 err = gcry_md_setkey (hd, secret, strlen (secret));
1112 ERROR ("network plugin: gcry_md_setkey failed: %s", gcry_strerror (err));
1115 sfree (pss.username);
1120 buffer + PART_SIGNATURE_SHA256_SIZE,
1121 buffer_len - PART_SIGNATURE_SHA256_SIZE);
1122 hash_ptr = gcry_md_read (hd, GCRY_MD_SHA256);
1123 if (hash_ptr == NULL)
1125 ERROR ("network plugin: gcry_md_read failed.");
1128 sfree (pss.username);
1131 memcpy (hash, hash_ptr, sizeof (hash));
1137 if (memcmp (pss.hash, hash, sizeof (pss.hash)) != 0)
1139 WARNING ("network plugin: Verifying HMAC-SHA-256 signature failed: "
1144 parse_packet (se, buffer + buffer_offset, buffer_len - buffer_offset,
1145 flags | PP_SIGNED, pss.username);
1149 sfree (pss.username);
1151 *ret_buffer = buffer + buffer_len;
1152 *ret_buffer_len = 0;
1155 } /* }}} int parse_part_sign_sha256 */
1156 /* #endif HAVE_LIBGCRYPT */
1158 #else /* if !HAVE_LIBGCRYPT */
1159 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
1160 void **ret_buffer, size_t *ret_buffer_size, int flags)
1162 static int warning_has_been_printed = 0;
1166 size_t buffer_offset;
1169 part_signature_sha256_t pss;
1171 buffer = *ret_buffer;
1172 buffer_size = *ret_buffer_size;
1175 if (buffer_size <= PART_SIGNATURE_SHA256_SIZE)
1178 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
1179 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
1180 part_len = ntohs (pss.head.length);
1182 if ((part_len <= PART_SIGNATURE_SHA256_SIZE)
1183 || (part_len > buffer_size))
1186 if (warning_has_been_printed == 0)
1188 WARNING ("network plugin: Received signed packet, but the network "
1189 "plugin was not linked with libgcrypt, so I cannot "
1190 "verify the signature. The packet will be accepted.");
1191 warning_has_been_printed = 1;
1194 parse_packet (se, buffer + part_len, buffer_size - part_len, flags,
1195 /* username = */ NULL);
1197 *ret_buffer = buffer + buffer_size;
1198 *ret_buffer_size = 0;
1201 } /* }}} int parse_part_sign_sha256 */
1202 #endif /* !HAVE_LIBGCRYPT */
1205 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1206 void **ret_buffer, size_t *ret_buffer_len,
1209 char *buffer = *ret_buffer;
1210 size_t buffer_len = *ret_buffer_len;
1213 size_t buffer_offset;
1214 uint16_t username_len;
1215 part_encryption_aes256_t pea;
1216 unsigned char hash[sizeof (pea.hash)];
1218 gcry_cipher_hd_t cypher;
1221 /* Make sure at least the header if available. */
1222 if (buffer_len <= PART_ENCRYPTION_AES256_SIZE)
1224 NOTICE ("network plugin: parse_part_encr_aes256: "
1225 "Discarding short packet.");
1231 /* Copy the unencrypted information into `pea'. */
1232 BUFFER_READ (&pea.head.type, sizeof (pea.head.type));
1233 BUFFER_READ (&pea.head.length, sizeof (pea.head.length));
1235 /* Check the `part size'. */
1236 part_size = ntohs (pea.head.length);
1237 if ((part_size <= PART_ENCRYPTION_AES256_SIZE)
1238 || (part_size > buffer_len))
1240 NOTICE ("network plugin: parse_part_encr_aes256: "
1241 "Discarding part with invalid size.");
1245 /* Read the username */
1246 BUFFER_READ (&username_len, sizeof (username_len));
1247 username_len = ntohs (username_len);
1249 if ((username_len <= 0)
1250 || (username_len > (part_size - (PART_ENCRYPTION_AES256_SIZE + 1))))
1252 NOTICE ("network plugin: parse_part_encr_aes256: "
1253 "Discarding part with invalid username length.");
1257 assert (username_len > 0);
1258 pea.username = malloc (username_len + 1);
1259 if (pea.username == NULL)
1261 BUFFER_READ (pea.username, username_len);
1262 pea.username[username_len] = 0;
1264 /* Last but not least, the initialization vector */
1265 BUFFER_READ (pea.iv, sizeof (pea.iv));
1267 /* Make sure we are at the right position */
1268 assert (buffer_offset == (username_len +
1269 PART_ENCRYPTION_AES256_SIZE - sizeof (pea.hash)));
1271 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
1275 sfree (pea.username);
1279 payload_len = part_size - (PART_ENCRYPTION_AES256_SIZE + username_len);
1280 assert (payload_len > 0);
1282 /* Decrypt the packet in-place */
1283 err = gcry_cipher_decrypt (cypher,
1284 buffer + buffer_offset,
1285 part_size - buffer_offset,
1286 /* in = */ NULL, /* in len = */ 0);
1289 sfree (pea.username);
1290 ERROR ("network plugin: gcry_cipher_decrypt returned: %s",
1291 gcry_strerror (err));
1296 BUFFER_READ (pea.hash, sizeof (pea.hash));
1298 /* Make sure we're at the right position - again */
1299 assert (buffer_offset == (username_len + PART_ENCRYPTION_AES256_SIZE));
1300 assert (buffer_offset == (part_size - payload_len));
1302 /* Check hash sum */
1303 memset (hash, 0, sizeof (hash));
1304 gcry_md_hash_buffer (GCRY_MD_SHA1, hash,
1305 buffer + buffer_offset, payload_len);
1306 if (memcmp (hash, pea.hash, sizeof (hash)) != 0)
1308 sfree (pea.username);
1309 ERROR ("network plugin: Decryption failed: Checksum mismatch.");
1313 parse_packet (se, buffer + buffer_offset, payload_len,
1314 flags | PP_ENCRYPTED, pea.username);
1316 /* XXX: Free pea.username?!? */
1318 /* Update return values */
1319 *ret_buffer = buffer + part_size;
1320 *ret_buffer_len = buffer_len - part_size;
1322 sfree (pea.username);
1325 } /* }}} int parse_part_encr_aes256 */
1326 /* #endif HAVE_LIBGCRYPT */
1328 #else /* if !HAVE_LIBGCRYPT */
1329 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1330 void **ret_buffer, size_t *ret_buffer_size, int flags)
1332 static int warning_has_been_printed = 0;
1336 size_t buffer_offset;
1341 buffer = *ret_buffer;
1342 buffer_size = *ret_buffer_size;
1345 /* parse_packet assures this minimum size. */
1346 assert (buffer_size >= (sizeof (ph.type) + sizeof (ph.length)));
1348 BUFFER_READ (&ph.type, sizeof (ph.type));
1349 BUFFER_READ (&ph.length, sizeof (ph.length));
1350 ph_length = ntohs (ph.length);
1352 if ((ph_length <= PART_ENCRYPTION_AES256_SIZE)
1353 || (ph_length > buffer_size))
1355 ERROR ("network plugin: AES-256 encrypted part "
1356 "with invalid length received.");
1360 if (warning_has_been_printed == 0)
1362 WARNING ("network plugin: Received encrypted packet, but the network "
1363 "plugin was not linked with libgcrypt, so I cannot "
1364 "decrypt it. The part will be discarded.");
1365 warning_has_been_printed = 1;
1368 *ret_buffer = (void *) (((char *) *ret_buffer) + ph_length);
1369 *ret_buffer_size -= ph_length;
1372 } /* }}} int parse_part_encr_aes256 */
1373 #endif /* !HAVE_LIBGCRYPT */
1377 static int parse_packet (sockent_t *se, /* {{{ */
1378 void *buffer, size_t buffer_size, int flags,
1379 const char *username)
1383 value_list_t vl = VALUE_LIST_INIT;
1387 int packet_was_signed = (flags & PP_SIGNED);
1388 int packet_was_encrypted = (flags & PP_ENCRYPTED);
1389 int printed_ignore_warning = 0;
1390 #endif /* HAVE_LIBGCRYPT */
1393 memset (&vl, '\0', sizeof (vl));
1394 memset (&n, '\0', sizeof (n));
1397 while ((status == 0) && (0 < buffer_size)
1398 && ((unsigned int) buffer_size > sizeof (part_header_t)))
1400 uint16_t pkg_length;
1403 memcpy ((void *) &pkg_type,
1406 memcpy ((void *) &pkg_length,
1407 (void *) (((char *) buffer) + sizeof (pkg_type)),
1408 sizeof (pkg_length));
1410 pkg_length = ntohs (pkg_length);
1411 pkg_type = ntohs (pkg_type);
1413 if (pkg_length > buffer_size)
1415 /* Ensure that this loop terminates eventually */
1416 if (pkg_length < (2 * sizeof (uint16_t)))
1419 if (pkg_type == TYPE_ENCR_AES256)
1421 status = parse_part_encr_aes256 (se,
1422 &buffer, &buffer_size, flags);
1425 ERROR ("network plugin: Decrypting AES256 "
1427 "with status %i.", status);
1432 else if ((se->data.server.security_level == SECURITY_LEVEL_ENCRYPT)
1433 && (packet_was_encrypted == 0))
1435 if (printed_ignore_warning == 0)
1437 INFO ("network plugin: Unencrypted packet or "
1438 "part has been ignored.");
1439 printed_ignore_warning = 1;
1441 buffer = ((char *) buffer) + pkg_length;
1444 #endif /* HAVE_LIBGCRYPT */
1445 else if (pkg_type == TYPE_SIGN_SHA256)
1447 status = parse_part_sign_sha256 (se,
1448 &buffer, &buffer_size, flags);
1451 ERROR ("network plugin: Verifying HMAC-SHA-256 "
1453 "with status %i.", status);
1458 else if ((se->data.server.security_level == SECURITY_LEVEL_SIGN)
1459 && (packet_was_encrypted == 0)
1460 && (packet_was_signed == 0))
1462 if (printed_ignore_warning == 0)
1464 INFO ("network plugin: Unsigned packet or "
1465 "part has been ignored.");
1466 printed_ignore_warning = 1;
1468 buffer = ((char *) buffer) + pkg_length;
1471 #endif /* HAVE_LIBGCRYPT */
1472 else if (pkg_type == TYPE_VALUES)
1474 status = parse_part_values (&buffer, &buffer_size,
1475 &vl.values, &vl.values_len);
1479 network_dispatch_values (&vl, username);
1483 else if (pkg_type == TYPE_TIME)
1486 status = parse_part_number (&buffer, &buffer_size,
1490 vl.time = TIME_T_TO_CDTIME_T (tmp);
1491 n.time = TIME_T_TO_CDTIME_T (tmp);
1494 else if (pkg_type == TYPE_TIME_HR)
1497 status = parse_part_number (&buffer, &buffer_size,
1501 vl.time = (cdtime_t) tmp;
1502 n.time = (cdtime_t) tmp;
1505 else if (pkg_type == TYPE_INTERVAL)
1508 status = parse_part_number (&buffer, &buffer_size,
1511 vl.interval = TIME_T_TO_CDTIME_T (tmp);
1513 else if (pkg_type == TYPE_INTERVAL_HR)
1516 status = parse_part_number (&buffer, &buffer_size,
1519 vl.interval = (cdtime_t) tmp;
1521 else if (pkg_type == TYPE_HOST)
1523 status = parse_part_string (&buffer, &buffer_size,
1524 vl.host, sizeof (vl.host));
1526 sstrncpy (n.host, vl.host, sizeof (n.host));
1528 else if (pkg_type == TYPE_PLUGIN)
1530 status = parse_part_string (&buffer, &buffer_size,
1531 vl.plugin, sizeof (vl.plugin));
1533 sstrncpy (n.plugin, vl.plugin,
1536 else if (pkg_type == TYPE_PLUGIN_INSTANCE)
1538 status = parse_part_string (&buffer, &buffer_size,
1540 sizeof (vl.plugin_instance));
1542 sstrncpy (n.plugin_instance,
1544 sizeof (n.plugin_instance));
1546 else if (pkg_type == TYPE_TYPE)
1548 status = parse_part_string (&buffer, &buffer_size,
1549 vl.type, sizeof (vl.type));
1551 sstrncpy (n.type, vl.type, sizeof (n.type));
1553 else if (pkg_type == TYPE_TYPE_INSTANCE)
1555 status = parse_part_string (&buffer, &buffer_size,
1557 sizeof (vl.type_instance));
1559 sstrncpy (n.type_instance, vl.type_instance,
1560 sizeof (n.type_instance));
1562 else if (pkg_type == TYPE_MESSAGE)
1564 status = parse_part_string (&buffer, &buffer_size,
1565 n.message, sizeof (n.message));
1571 else if ((n.severity != NOTIF_FAILURE)
1572 && (n.severity != NOTIF_WARNING)
1573 && (n.severity != NOTIF_OKAY))
1575 INFO ("network plugin: "
1576 "Ignoring notification with "
1577 "unknown severity %i.",
1580 else if (n.time <= 0)
1582 INFO ("network plugin: "
1583 "Ignoring notification with "
1586 else if (strlen (n.message) <= 0)
1588 INFO ("network plugin: "
1589 "Ignoring notification with "
1590 "an empty message.");
1594 network_dispatch_notification (&n);
1597 else if (pkg_type == TYPE_SEVERITY)
1600 status = parse_part_number (&buffer, &buffer_size,
1603 n.severity = (int) tmp;
1607 DEBUG ("network plugin: parse_packet: Unknown part"
1608 " type: 0x%04hx", pkg_type);
1609 buffer = ((char *) buffer) + pkg_length;
1611 } /* while (buffer_size > sizeof (part_header_t)) */
1613 if (status == 0 && buffer_size > 0)
1614 WARNING ("network plugin: parse_packet: Received truncated "
1615 "packet, try increasing `MaxPacketSize'");
1618 } /* }}} int parse_packet */
1620 static void free_sockent_client (struct sockent_client *sec) /* {{{ */
1629 sfree (sec->username);
1630 sfree (sec->password);
1631 if (sec->cypher != NULL)
1632 gcry_cipher_close (sec->cypher);
1634 } /* }}} void free_sockent_client */
1636 static void free_sockent_server (struct sockent_server *ses) /* {{{ */
1640 for (i = 0; i < ses->fd_num; i++)
1642 if (ses->fd[i] >= 0)
1651 sfree (ses->auth_file);
1652 fbh_destroy (ses->userdb);
1653 if (ses->cypher != NULL)
1654 gcry_cipher_close (ses->cypher);
1656 } /* }}} void free_sockent_server */
1658 static void sockent_destroy (sockent_t *se) /* {{{ */
1662 DEBUG ("network plugin: sockent_destroy (se = %p);", (void *) se);
1669 sfree (se->service);
1671 if (se->type == SOCKENT_TYPE_CLIENT)
1672 free_sockent_client (&se->data.client);
1674 free_sockent_server (&se->data.server);
1679 } /* }}} void sockent_destroy */
1682 * int network_set_ttl
1684 * Set the `IP_MULTICAST_TTL', `IP_TTL', `IPV6_MULTICAST_HOPS' or
1685 * `IPV6_UNICAST_HOPS', depending on which option is applicable.
1687 * The `struct addrinfo' is used to destinguish between unicast and multicast
1690 static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
1692 DEBUG ("network plugin: network_set_ttl: network_config_ttl = %i;",
1693 network_config_ttl);
1695 assert (se->type == SOCKENT_TYPE_CLIENT);
1697 if ((network_config_ttl < 1) || (network_config_ttl > 255))
1700 if (ai->ai_family == AF_INET)
1702 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1705 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1706 optname = IP_MULTICAST_TTL;
1710 if (setsockopt (se->data.client.fd, IPPROTO_IP, optname,
1711 &network_config_ttl,
1712 sizeof (network_config_ttl)) != 0)
1715 ERROR ("network plugin: setsockopt (ipv4-ttl): %s",
1716 sstrerror (errno, errbuf, sizeof (errbuf)));
1720 else if (ai->ai_family == AF_INET6)
1722 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1723 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1726 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1727 optname = IPV6_MULTICAST_HOPS;
1729 optname = IPV6_UNICAST_HOPS;
1731 if (setsockopt (se->data.client.fd, IPPROTO_IPV6, optname,
1732 &network_config_ttl,
1733 sizeof (network_config_ttl)) != 0)
1736 ERROR ("network plugin: setsockopt(ipv6-ttl): %s",
1737 sstrerror (errno, errbuf,
1744 } /* int network_set_ttl */
1746 static int network_set_interface (const sockent_t *se, const struct addrinfo *ai) /* {{{ */
1748 DEBUG ("network plugin: network_set_interface: interface index = %i;",
1751 assert (se->type == SOCKENT_TYPE_CLIENT);
1753 if (ai->ai_family == AF_INET)
1755 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1757 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1759 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1760 /* If possible, use the "ip_mreqn" structure which has
1761 * an "interface index" member. Using the interface
1762 * index is preferred here, because of its similarity
1763 * to the way IPv6 handles this. Unfortunately, it
1764 * appears not to be portable. */
1765 struct ip_mreqn mreq;
1767 memset (&mreq, 0, sizeof (mreq));
1768 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1769 mreq.imr_address.s_addr = ntohl (INADDR_ANY);
1770 mreq.imr_ifindex = se->interface;
1772 struct ip_mreq mreq;
1774 memset (&mreq, 0, sizeof (mreq));
1775 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1776 mreq.imr_interface.s_addr = ntohl (INADDR_ANY);
1779 if (setsockopt (se->data.client.fd, IPPROTO_IP, IP_MULTICAST_IF,
1780 &mreq, sizeof (mreq)) != 0)
1783 ERROR ("network plugin: setsockopt (ipv4-multicast-if): %s",
1784 sstrerror (errno, errbuf, sizeof (errbuf)));
1791 else if (ai->ai_family == AF_INET6)
1793 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1795 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1797 if (setsockopt (se->data.client.fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
1799 sizeof (se->interface)) != 0)
1802 ERROR ("network plugin: setsockopt (ipv6-multicast-if): %s",
1803 sstrerror (errno, errbuf,
1812 /* else: Not a multicast interface. */
1813 if (se->interface != 0)
1815 #if defined(HAVE_IF_INDEXTONAME) && HAVE_IF_INDEXTONAME && defined(SO_BINDTODEVICE)
1816 char interface_name[IFNAMSIZ];
1818 if (if_indextoname (se->interface, interface_name) == NULL)
1821 DEBUG ("network plugin: Binding socket to interface %s", interface_name);
1823 if (setsockopt (se->data.client.fd, SOL_SOCKET, SO_BINDTODEVICE,
1825 sizeof(interface_name)) == -1 )
1828 ERROR ("network plugin: setsockopt (bind-if): %s",
1829 sstrerror (errno, errbuf, sizeof (errbuf)));
1832 /* #endif HAVE_IF_INDEXTONAME && SO_BINDTODEVICE */
1835 WARNING ("network plugin: Cannot set the interface on a unicast "
1837 # if !defined(SO_BINDTODEVICE)
1838 "the \"SO_BINDTODEVICE\" socket option "
1840 "the \"if_indextoname\" function "
1842 "is not available on your system.");
1848 } /* }}} network_set_interface */
1850 static int network_bind_socket (int fd, const struct addrinfo *ai, const int interface_idx)
1859 /* allow multiple sockets to use the same PORT number */
1860 if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR,
1861 &yes, sizeof(yes)) == -1) {
1863 ERROR ("network plugin: setsockopt (reuseaddr): %s",
1864 sstrerror (errno, errbuf, sizeof (errbuf)));
1868 DEBUG ("fd = %i; calling `bind'", fd);
1870 if (bind (fd, ai->ai_addr, ai->ai_addrlen) == -1)
1874 sstrerror (errno, errbuf, sizeof (errbuf)));
1878 if (ai->ai_family == AF_INET)
1880 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1881 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1883 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1884 struct ip_mreqn mreq;
1886 struct ip_mreq mreq;
1889 DEBUG ("fd = %i; IPv4 multicast address found", fd);
1891 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1892 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1893 /* Set the interface using the interface index if
1894 * possible (available). Unfortunately, the struct
1895 * ip_mreqn is not portable. */
1896 mreq.imr_address.s_addr = ntohl (INADDR_ANY);
1897 mreq.imr_ifindex = interface_idx;
1899 mreq.imr_interface.s_addr = ntohl (INADDR_ANY);
1902 if (setsockopt (fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1903 &loop, sizeof (loop)) == -1)
1906 ERROR ("network plugin: setsockopt (multicast-loop): %s",
1907 sstrerror (errno, errbuf,
1912 if (setsockopt (fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1913 &mreq, sizeof (mreq)) == -1)
1916 ERROR ("network plugin: setsockopt (add-membership): %s",
1917 sstrerror (errno, errbuf,
1925 else if (ai->ai_family == AF_INET6)
1927 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1928 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1929 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1931 struct ipv6_mreq mreq;
1933 DEBUG ("fd = %i; IPv6 multicast address found", fd);
1935 memcpy (&mreq.ipv6mr_multiaddr,
1937 sizeof (addr->sin6_addr));
1939 /* http://developer.apple.com/documentation/Darwin/Reference/ManPages/man4/ip6.4.html
1940 * ipv6mr_interface may be set to zeroes to
1941 * choose the default multicast interface or to
1942 * the index of a particular multicast-capable
1943 * interface if the host is multihomed.
1944 * Membership is associ-associated with a
1945 * single interface; programs running on
1946 * multihomed hosts may need to join the same
1947 * group on more than one interface.*/
1948 mreq.ipv6mr_interface = interface_idx;
1950 if (setsockopt (fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
1951 &loop, sizeof (loop)) == -1)
1954 ERROR ("network plugin: setsockopt (ipv6-multicast-loop): %s",
1955 sstrerror (errno, errbuf,
1960 if (setsockopt (fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
1961 &mreq, sizeof (mreq)) == -1)
1964 ERROR ("network plugin: setsockopt (ipv6-add-membership): %s",
1965 sstrerror (errno, errbuf,
1974 #if defined(HAVE_IF_INDEXTONAME) && HAVE_IF_INDEXTONAME && defined(SO_BINDTODEVICE)
1975 /* if a specific interface was set, bind the socket to it. But to avoid
1976 * possible problems with multicast routing, only do that for non-multicast
1978 if (interface_idx != 0)
1980 char interface_name[IFNAMSIZ];
1982 if (if_indextoname (interface_idx, interface_name) == NULL)
1985 DEBUG ("fd = %i; Binding socket to interface %s", fd, interface_name);
1987 if (setsockopt (fd, SOL_SOCKET, SO_BINDTODEVICE,
1989 sizeof(interface_name)) == -1 )
1992 ERROR ("network plugin: setsockopt (bind-if): %s",
1993 sstrerror (errno, errbuf, sizeof (errbuf)));
1997 #endif /* HAVE_IF_INDEXTONAME && SO_BINDTODEVICE */
2000 } /* int network_bind_socket */
2002 /* Initialize a sockent structure. `type' must be either `SOCKENT_TYPE_CLIENT'
2003 * or `SOCKENT_TYPE_SERVER' */
2004 static sockent_t *sockent_create (int type) /* {{{ */
2008 if ((type != SOCKENT_TYPE_CLIENT) && (type != SOCKENT_TYPE_SERVER))
2011 se = calloc (1, sizeof (*se));
2021 if (type == SOCKENT_TYPE_SERVER)
2023 se->data.server.fd = NULL;
2024 se->data.server.fd_num = 0;
2026 se->data.server.security_level = SECURITY_LEVEL_NONE;
2027 se->data.server.auth_file = NULL;
2028 se->data.server.userdb = NULL;
2029 se->data.server.cypher = NULL;
2034 se->data.client.fd = -1;
2035 se->data.client.addr = NULL;
2036 se->data.client.resolve_interval = 0;
2037 se->data.client.next_resolve_reconnect = 0;
2039 se->data.client.security_level = SECURITY_LEVEL_NONE;
2040 se->data.client.username = NULL;
2041 se->data.client.password = NULL;
2042 se->data.client.cypher = NULL;
2047 } /* }}} sockent_t *sockent_create */
2049 static int sockent_init_crypto (sockent_t *se) /* {{{ */
2051 #if HAVE_LIBGCRYPT /* {{{ */
2052 if (se->type == SOCKENT_TYPE_CLIENT)
2054 if (se->data.client.security_level > SECURITY_LEVEL_NONE)
2056 network_init_gcrypt ();
2058 if ((se->data.client.username == NULL)
2059 || (se->data.client.password == NULL))
2061 ERROR ("network plugin: Client socket with "
2062 "security requested, but no "
2063 "credentials are configured.");
2066 gcry_md_hash_buffer (GCRY_MD_SHA256,
2067 se->data.client.password_hash,
2068 se->data.client.password,
2069 strlen (se->data.client.password));
2072 else /* (se->type == SOCKENT_TYPE_SERVER) */
2074 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
2076 network_init_gcrypt ();
2078 if (se->data.server.auth_file == NULL)
2080 ERROR ("network plugin: Server socket with "
2081 "security requested, but no "
2082 "password file is configured.");
2086 if (se->data.server.auth_file != NULL)
2088 se->data.server.userdb = fbh_create (se->data.server.auth_file);
2089 if (se->data.server.userdb == NULL)
2091 ERROR ("network plugin: Reading password file "
2093 se->data.server.auth_file);
2094 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
2099 #endif /* }}} HAVE_LIBGCRYPT */
2102 } /* }}} int sockent_init_crypto */
2104 static int sockent_client_disconnect (sockent_t *se) /* {{{ */
2106 struct sockent_client *client;
2108 if ((se == NULL) || (se->type != SOCKENT_TYPE_CLIENT))
2111 client = &se->data.client;
2112 if (client->fd >= 0) /* connected */
2118 sfree (client->addr);
2119 client->addrlen = 0;
2122 } /* }}} int sockent_client_disconnect */
2124 static int sockent_client_connect (sockent_t *se) /* {{{ */
2126 static c_complain_t complaint = C_COMPLAIN_INIT_STATIC;
2128 struct sockent_client *client;
2129 struct addrinfo ai_hints;
2130 struct addrinfo *ai_list = NULL, *ai_ptr;
2132 _Bool reconnect = 0;
2135 if ((se == NULL) || (se->type != SOCKENT_TYPE_CLIENT))
2138 client = &se->data.client;
2141 if (client->resolve_interval != 0 && client->next_resolve_reconnect < now) {
2142 DEBUG("network plugin: Reconnecting socket, resolve_interval = %lf, next_resolve_reconnect = %lf",
2143 CDTIME_T_TO_DOUBLE(client->resolve_interval), CDTIME_T_TO_DOUBLE(client->next_resolve_reconnect));
2147 if (client->fd >= 0 && !reconnect) /* already connected and not stale*/
2150 memset (&ai_hints, 0, sizeof (ai_hints));
2151 #ifdef AI_ADDRCONFIG
2152 ai_hints.ai_flags |= AI_ADDRCONFIG;
2154 ai_hints.ai_family = AF_UNSPEC;
2155 ai_hints.ai_socktype = SOCK_DGRAM;
2156 ai_hints.ai_protocol = IPPROTO_UDP;
2158 status = getaddrinfo (se->node,
2159 (se->service != NULL) ? se->service : NET_DEFAULT_PORT,
2160 &ai_hints, &ai_list);
2163 c_complain (LOG_ERR, &complaint,
2164 "network plugin: getaddrinfo (%s, %s) failed: %s",
2165 (se->node == NULL) ? "(null)" : se->node,
2166 (se->service == NULL) ? "(null)" : se->service,
2167 gai_strerror (status));
2172 c_release (LOG_NOTICE, &complaint,
2173 "network plugin: Successfully resolved \"%s\".",
2177 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
2179 if (client->fd >= 0) /* when we reconnect */
2180 sockent_client_disconnect(se);
2182 client->fd = socket (ai_ptr->ai_family,
2183 ai_ptr->ai_socktype,
2184 ai_ptr->ai_protocol);
2188 ERROR ("network plugin: socket(2) failed: %s",
2189 sstrerror (errno, errbuf,
2194 client->addr = calloc (1, sizeof (*client->addr));
2195 if (client->addr == NULL)
2197 ERROR ("network plugin: calloc failed.");
2203 assert (sizeof (*client->addr) >= ai_ptr->ai_addrlen);
2204 memcpy (client->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
2205 client->addrlen = ai_ptr->ai_addrlen;
2207 network_set_ttl (se, ai_ptr);
2208 network_set_interface (se, ai_ptr);
2210 /* We don't open more than one write-socket per
2211 * node/service pair.. */
2215 freeaddrinfo (ai_list);
2219 if (client->resolve_interval > 0)
2220 client->next_resolve_reconnect = now + client->resolve_interval;
2222 } /* }}} int sockent_client_connect */
2224 /* Open the file descriptors for a initialized sockent structure. */
2225 static int sockent_server_listen (sockent_t *se) /* {{{ */
2227 struct addrinfo ai_hints;
2228 struct addrinfo *ai_list, *ai_ptr;
2232 const char *service;
2237 assert (se->data.server.fd == NULL);
2238 assert (se->data.server.fd_num == 0);
2241 service = se->service;
2243 if (service == NULL)
2244 service = NET_DEFAULT_PORT;
2246 DEBUG ("network plugin: sockent_server_listen: node = %s; service = %s;",
2249 memset (&ai_hints, 0, sizeof (ai_hints));
2250 ai_hints.ai_flags = 0;
2252 ai_hints.ai_flags |= AI_PASSIVE;
2254 #ifdef AI_ADDRCONFIG
2255 ai_hints.ai_flags |= AI_ADDRCONFIG;
2257 ai_hints.ai_family = AF_UNSPEC;
2258 ai_hints.ai_socktype = SOCK_DGRAM;
2259 ai_hints.ai_protocol = IPPROTO_UDP;
2261 status = getaddrinfo (node, service, &ai_hints, &ai_list);
2264 ERROR ("network plugin: getaddrinfo (%s, %s) failed: %s",
2265 (se->node == NULL) ? "(null)" : se->node,
2266 (se->service == NULL) ? "(null)" : se->service,
2267 gai_strerror (status));
2271 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
2275 tmp = realloc (se->data.server.fd,
2276 sizeof (*tmp) * (se->data.server.fd_num + 1));
2279 ERROR ("network plugin: realloc failed.");
2282 se->data.server.fd = tmp;
2283 tmp = se->data.server.fd + se->data.server.fd_num;
2285 *tmp = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
2286 ai_ptr->ai_protocol);
2290 ERROR ("network plugin: socket(2) failed: %s",
2291 sstrerror (errno, errbuf,
2296 status = network_bind_socket (*tmp, ai_ptr, se->interface);
2304 se->data.server.fd_num++;
2306 } /* for (ai_list) */
2308 freeaddrinfo (ai_list);
2310 if (se->data.server.fd_num == 0)
2313 } /* }}} int sockent_server_listen */
2315 /* Add a sockent to the global list of sockets */
2316 static int sockent_add (sockent_t *se) /* {{{ */
2318 sockent_t *last_ptr;
2323 if (se->type == SOCKENT_TYPE_SERVER)
2328 tmp = realloc (listen_sockets_pollfd,
2329 sizeof (*tmp) * (listen_sockets_num
2330 + se->data.server.fd_num));
2333 ERROR ("network plugin: realloc failed.");
2336 listen_sockets_pollfd = tmp;
2337 tmp = listen_sockets_pollfd + listen_sockets_num;
2339 for (i = 0; i < se->data.server.fd_num; i++)
2341 memset (tmp + i, 0, sizeof (*tmp));
2342 tmp[i].fd = se->data.server.fd[i];
2343 tmp[i].events = POLLIN | POLLPRI;
2347 listen_sockets_num += se->data.server.fd_num;
2349 if (listen_sockets == NULL)
2351 listen_sockets = se;
2354 last_ptr = listen_sockets;
2356 else /* if (se->type == SOCKENT_TYPE_CLIENT) */
2358 if (sending_sockets == NULL)
2360 sending_sockets = se;
2363 last_ptr = sending_sockets;
2366 while (last_ptr->next != NULL)
2367 last_ptr = last_ptr->next;
2368 last_ptr->next = se;
2371 } /* }}} int sockent_add */
2373 static void *dispatch_thread (void __attribute__((unused)) *arg) /* {{{ */
2377 receive_list_entry_t *ent;
2380 /* Lock and wait for more data to come in */
2381 pthread_mutex_lock (&receive_list_lock);
2382 while ((listen_loop == 0)
2383 && (receive_list_head == NULL))
2384 pthread_cond_wait (&receive_list_cond, &receive_list_lock);
2386 /* Remove the head entry and unlock */
2387 ent = receive_list_head;
2389 receive_list_head = ent->next;
2390 receive_list_length--;
2391 pthread_mutex_unlock (&receive_list_lock);
2393 /* Check whether we are supposed to exit. We do NOT check `listen_loop'
2394 * because we dispatch all missing packets before shutting down. */
2398 /* Look for the correct `sockent_t' */
2399 se = listen_sockets;
2404 for (i = 0; i < se->data.server.fd_num; i++)
2405 if (se->data.server.fd[i] == ent->fd)
2408 if (i < se->data.server.fd_num)
2416 ERROR ("network plugin: Got packet from FD %i, but can't "
2417 "find an appropriate socket entry.",
2424 parse_packet (se, ent->data, ent->data_len, /* flags = */ 0,
2425 /* username = */ NULL);
2431 } /* }}} void *dispatch_thread */
2433 static int network_receive (void) /* {{{ */
2435 char buffer[network_config_packet_size];
2441 receive_list_entry_t *private_list_head;
2442 receive_list_entry_t *private_list_tail;
2443 uint64_t private_list_length;
2445 assert (listen_sockets_num > 0);
2447 private_list_head = NULL;
2448 private_list_tail = NULL;
2449 private_list_length = 0;
2451 while (listen_loop == 0)
2453 status = poll (listen_sockets_pollfd, listen_sockets_num, -1);
2459 ERROR ("network plugin: poll(2) failed: %s",
2460 sstrerror (errno, errbuf, sizeof (errbuf)));
2464 for (i = 0; (i < listen_sockets_num) && (status > 0); i++)
2466 receive_list_entry_t *ent;
2468 if ((listen_sockets_pollfd[i].revents
2469 & (POLLIN | POLLPRI)) == 0)
2473 buffer_len = recv (listen_sockets_pollfd[i].fd,
2474 buffer, sizeof (buffer),
2479 status = (errno != 0) ? errno : -1;
2480 ERROR ("network plugin: recv(2) failed: %s",
2481 sstrerror (errno, errbuf, sizeof (errbuf)));
2485 stats_octets_rx += ((uint64_t) buffer_len);
2488 /* TODO: Possible performance enhancement: Do not free
2489 * these entries in the dispatch thread but put them in
2490 * another list, so we don't have to allocate more and
2491 * more of these structures. */
2492 ent = calloc (1, sizeof (*ent));
2495 ERROR ("network plugin: calloc failed.");
2500 ent->data = malloc (network_config_packet_size);
2501 if (ent->data == NULL)
2504 ERROR ("network plugin: malloc failed.");
2508 ent->fd = listen_sockets_pollfd[i].fd;
2511 memcpy (ent->data, buffer, buffer_len);
2512 ent->data_len = buffer_len;
2514 if (private_list_head == NULL)
2515 private_list_head = ent;
2517 private_list_tail->next = ent;
2518 private_list_tail = ent;
2519 private_list_length++;
2521 /* Do not block here. Blocking here has led to
2522 * insufficient performance in the past. */
2523 if (pthread_mutex_trylock (&receive_list_lock) == 0)
2525 assert (((receive_list_head == NULL) && (receive_list_length == 0))
2526 || ((receive_list_head != NULL) && (receive_list_length != 0)));
2528 if (receive_list_head == NULL)
2529 receive_list_head = private_list_head;
2531 receive_list_tail->next = private_list_head;
2532 receive_list_tail = private_list_tail;
2533 receive_list_length += private_list_length;
2535 pthread_cond_signal (&receive_list_cond);
2536 pthread_mutex_unlock (&receive_list_lock);
2538 private_list_head = NULL;
2539 private_list_tail = NULL;
2540 private_list_length = 0;
2544 } /* for (listen_sockets_pollfd) */
2548 } /* while (listen_loop == 0) */
2550 /* Make sure everything is dispatched before exiting. */
2551 if (private_list_head != NULL)
2553 pthread_mutex_lock (&receive_list_lock);
2555 if (receive_list_head == NULL)
2556 receive_list_head = private_list_head;
2558 receive_list_tail->next = private_list_head;
2559 receive_list_tail = private_list_tail;
2560 receive_list_length += private_list_length;
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;
2579 send_buffer_last_update = 0;
2581 memset (&send_buffer_vl, 0, sizeof (send_buffer_vl));
2582 } /* int network_init_buffer */
2584 static void network_send_buffer_plain (sockent_t *se, /* {{{ */
2585 const char *buffer, size_t buffer_size)
2591 status = sockent_client_connect (se);
2595 status = sendto (se->data.client.fd, buffer, buffer_size,
2597 (struct sockaddr *) se->data.client.addr,
2598 se->data.client.addrlen);
2603 if ((errno == EINTR) || (errno == EAGAIN))
2606 ERROR ("network plugin: sendto failed: %s. Closing sending socket.",
2607 sstrerror (errno, errbuf, sizeof (errbuf)));
2608 sockent_client_disconnect (se);
2614 } /* }}} void network_send_buffer_plain */
2617 #define BUFFER_ADD(p,s) do { \
2618 memcpy (buffer + buffer_offset, (p), (s)); \
2619 buffer_offset += (s); \
2622 static void network_send_buffer_signed (sockent_t *se, /* {{{ */
2623 const char *in_buffer, size_t in_buffer_size)
2625 part_signature_sha256_t ps;
2626 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2627 size_t buffer_offset;
2628 size_t username_len;
2632 unsigned char *hash;
2635 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
2638 ERROR ("network plugin: Creating HMAC object failed: %s",
2639 gcry_strerror (err));
2643 err = gcry_md_setkey (hd, se->data.client.password,
2644 strlen (se->data.client.password));
2647 ERROR ("network plugin: gcry_md_setkey failed: %s",
2648 gcry_strerror (err));
2653 username_len = strlen (se->data.client.username);
2654 if (username_len > (BUFF_SIG_SIZE - PART_SIGNATURE_SHA256_SIZE))
2656 ERROR ("network plugin: Username too long: %s",
2657 se->data.client.username);
2661 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE,
2662 se->data.client.username, username_len);
2663 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE + username_len,
2664 in_buffer, in_buffer_size);
2666 /* Initialize the `ps' structure. */
2667 memset (&ps, 0, sizeof (ps));
2668 ps.head.type = htons (TYPE_SIGN_SHA256);
2669 ps.head.length = htons (PART_SIGNATURE_SHA256_SIZE + username_len);
2671 /* Calculate the hash value. */
2672 gcry_md_write (hd, buffer + PART_SIGNATURE_SHA256_SIZE,
2673 username_len + in_buffer_size);
2674 hash = gcry_md_read (hd, GCRY_MD_SHA256);
2677 ERROR ("network plugin: gcry_md_read failed.");
2681 memcpy (ps.hash, hash, sizeof (ps.hash));
2683 /* Add the header */
2686 BUFFER_ADD (&ps.head.type, sizeof (ps.head.type));
2687 BUFFER_ADD (&ps.head.length, sizeof (ps.head.length));
2688 BUFFER_ADD (ps.hash, sizeof (ps.hash));
2690 assert (buffer_offset == PART_SIGNATURE_SHA256_SIZE);
2695 buffer_offset = PART_SIGNATURE_SHA256_SIZE + username_len + in_buffer_size;
2696 network_send_buffer_plain (se, buffer, buffer_offset);
2697 } /* }}} void network_send_buffer_signed */
2699 static void network_send_buffer_encrypted (sockent_t *se, /* {{{ */
2700 const char *in_buffer, size_t in_buffer_size)
2702 part_encryption_aes256_t pea;
2703 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2705 size_t buffer_offset;
2707 size_t username_len;
2709 gcry_cipher_hd_t cypher;
2711 /* Initialize the header fields */
2712 memset (&pea, 0, sizeof (pea));
2713 pea.head.type = htons (TYPE_ENCR_AES256);
2715 pea.username = se->data.client.username;
2717 username_len = strlen (pea.username);
2718 if ((PART_ENCRYPTION_AES256_SIZE + username_len) > BUFF_SIG_SIZE)
2720 ERROR ("network plugin: Username too long: %s", pea.username);
2724 buffer_size = PART_ENCRYPTION_AES256_SIZE + username_len + in_buffer_size;
2725 header_size = PART_ENCRYPTION_AES256_SIZE + username_len
2726 - sizeof (pea.hash);
2728 assert (buffer_size <= sizeof (buffer));
2729 DEBUG ("network plugin: network_send_buffer_encrypted: "
2730 "buffer_size = %zu;", buffer_size);
2732 pea.head.length = htons ((uint16_t) (PART_ENCRYPTION_AES256_SIZE
2733 + username_len + in_buffer_size));
2734 pea.username_length = htons ((uint16_t) username_len);
2736 /* Chose a random initialization vector. */
2737 gcry_randomize ((void *) &pea.iv, sizeof (pea.iv), GCRY_STRONG_RANDOM);
2739 /* Create hash of the payload */
2740 gcry_md_hash_buffer (GCRY_MD_SHA1, pea.hash, in_buffer, in_buffer_size);
2742 /* Initialize the buffer */
2744 memset (buffer, 0, sizeof (buffer));
2747 BUFFER_ADD (&pea.head.type, sizeof (pea.head.type));
2748 BUFFER_ADD (&pea.head.length, sizeof (pea.head.length));
2749 BUFFER_ADD (&pea.username_length, sizeof (pea.username_length));
2750 BUFFER_ADD (pea.username, username_len);
2751 BUFFER_ADD (pea.iv, sizeof (pea.iv));
2752 assert (buffer_offset == header_size);
2753 BUFFER_ADD (pea.hash, sizeof (pea.hash));
2754 BUFFER_ADD (in_buffer, in_buffer_size);
2756 assert (buffer_offset == buffer_size);
2758 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
2759 se->data.client.password);
2763 /* Encrypt the buffer in-place */
2764 err = gcry_cipher_encrypt (cypher,
2765 buffer + header_size,
2766 buffer_size - header_size,
2767 /* in = */ NULL, /* in len = */ 0);
2770 ERROR ("network plugin: gcry_cipher_encrypt returned: %s",
2771 gcry_strerror (err));
2775 /* Send it out without further modifications */
2776 network_send_buffer_plain (se, buffer, buffer_size);
2777 } /* }}} void network_send_buffer_encrypted */
2779 #endif /* HAVE_LIBGCRYPT */
2781 static void network_send_buffer (char *buffer, size_t buffer_len) /* {{{ */
2785 DEBUG ("network plugin: network_send_buffer: buffer_len = %zu", buffer_len);
2787 for (se = sending_sockets; se != NULL; se = se->next)
2790 if (se->data.client.security_level == SECURITY_LEVEL_ENCRYPT)
2791 network_send_buffer_encrypted (se, buffer, buffer_len);
2792 else if (se->data.client.security_level == SECURITY_LEVEL_SIGN)
2793 network_send_buffer_signed (se, buffer, buffer_len);
2794 else /* if (se->data.client.security_level == SECURITY_LEVEL_NONE) */
2795 #endif /* HAVE_LIBGCRYPT */
2796 network_send_buffer_plain (se, buffer, buffer_len);
2797 } /* for (sending_sockets) */
2798 } /* }}} void network_send_buffer */
2800 static int add_to_buffer (char *buffer, int buffer_size, /* {{{ */
2801 value_list_t *vl_def,
2802 const data_set_t *ds, const value_list_t *vl)
2804 char *buffer_orig = buffer;
2806 if (strcmp (vl_def->host, vl->host) != 0)
2808 if (write_part_string (&buffer, &buffer_size, TYPE_HOST,
2809 vl->host, strlen (vl->host)) != 0)
2811 sstrncpy (vl_def->host, vl->host, sizeof (vl_def->host));
2814 if (vl_def->time != vl->time)
2816 if (write_part_number (&buffer, &buffer_size, TYPE_TIME_HR,
2817 (uint64_t) vl->time))
2819 vl_def->time = vl->time;
2822 if (vl_def->interval != vl->interval)
2824 if (write_part_number (&buffer, &buffer_size, TYPE_INTERVAL_HR,
2825 (uint64_t) vl->interval))
2827 vl_def->interval = vl->interval;
2830 if (strcmp (vl_def->plugin, vl->plugin) != 0)
2832 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN,
2833 vl->plugin, strlen (vl->plugin)) != 0)
2835 sstrncpy (vl_def->plugin, vl->plugin, sizeof (vl_def->plugin));
2838 if (strcmp (vl_def->plugin_instance, vl->plugin_instance) != 0)
2840 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
2841 vl->plugin_instance,
2842 strlen (vl->plugin_instance)) != 0)
2844 sstrncpy (vl_def->plugin_instance, vl->plugin_instance, sizeof (vl_def->plugin_instance));
2847 if (strcmp (vl_def->type, vl->type) != 0)
2849 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
2850 vl->type, strlen (vl->type)) != 0)
2852 sstrncpy (vl_def->type, ds->type, sizeof (vl_def->type));
2855 if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
2857 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE_INSTANCE,
2859 strlen (vl->type_instance)) != 0)
2861 sstrncpy (vl_def->type_instance, vl->type_instance, sizeof (vl_def->type_instance));
2864 if (write_part_values (&buffer, &buffer_size, ds, vl) != 0)
2867 return (buffer - buffer_orig);
2868 } /* }}} int add_to_buffer */
2870 static void flush_buffer (void)
2872 DEBUG ("network plugin: flush_buffer: send_buffer_fill = %i",
2875 network_send_buffer (send_buffer, (size_t) send_buffer_fill);
2877 stats_octets_tx += ((uint64_t) send_buffer_fill);
2880 network_init_buffer ();
2883 static int network_write (const data_set_t *ds, const value_list_t *vl,
2884 user_data_t __attribute__((unused)) *user_data)
2888 if (!check_send_okay (vl))
2891 char name[6*DATA_MAX_NAME_LEN];
2892 FORMAT_VL (name, sizeof (name), vl);
2893 name[sizeof (name) - 1] = 0;
2894 DEBUG ("network plugin: network_write: "
2895 "NOT sending %s.", name);
2897 /* Counter is not protected by another lock and may be reached by
2898 * multiple threads */
2899 pthread_mutex_lock (&stats_lock);
2900 stats_values_not_sent++;
2901 pthread_mutex_unlock (&stats_lock);
2905 uc_meta_data_add_unsigned_int (vl,
2906 "network:time_sent", (uint64_t) vl->time);
2908 pthread_mutex_lock (&send_buffer_lock);
2910 status = add_to_buffer (send_buffer_ptr,
2911 network_config_packet_size - (send_buffer_fill + BUFF_SIG_SIZE),
2916 /* status == bytes added to the buffer */
2917 send_buffer_fill += status;
2918 send_buffer_ptr += status;
2919 send_buffer_last_update = cdtime();
2921 stats_values_sent++;
2927 status = add_to_buffer (send_buffer_ptr,
2928 network_config_packet_size - (send_buffer_fill + BUFF_SIG_SIZE),
2934 send_buffer_fill += status;
2935 send_buffer_ptr += status;
2937 stats_values_sent++;
2943 ERROR ("network plugin: Unable to append to the "
2944 "buffer for some weird reason");
2946 else if ((network_config_packet_size - send_buffer_fill) < 15)
2951 pthread_mutex_unlock (&send_buffer_lock);
2953 return ((status < 0) ? -1 : 0);
2954 } /* int network_write */
2956 static int network_config_set_ttl (const oconfig_item_t *ci) /* {{{ */
2960 if (cf_util_get_int (ci, &tmp) != 0)
2962 else if ((tmp > 0) && (tmp <= 255))
2963 network_config_ttl = tmp;
2965 WARNING ("network plugin: The `TimeToLive' must be between 1 and 255.");
2970 } /* }}} int network_config_set_ttl */
2972 static int network_config_set_interface (const oconfig_item_t *ci, /* {{{ */
2977 if (cf_util_get_string_buffer (ci, if_name, sizeof (if_name)) != 0)
2980 *interface = if_nametoindex (if_name);
2982 } /* }}} int network_config_set_interface */
2984 static int network_config_set_buffer_size (const oconfig_item_t *ci) /* {{{ */
2988 if (cf_util_get_int (ci, &tmp) != 0)
2990 else if ((tmp >= 1024) && (tmp <= 65535))
2991 network_config_packet_size = tmp;
2993 WARNING ("network plugin: The `MaxPacketSize' must be between 1024 and 65535.");
2998 } /* }}} int network_config_set_buffer_size */
3001 static int network_config_set_security_level (oconfig_item_t *ci, /* {{{ */
3005 if ((ci->values_num != 1)
3006 || (ci->values[0].type != OCONFIG_TYPE_STRING))
3008 WARNING ("network plugin: The `SecurityLevel' config option needs exactly "
3009 "one string argument.");
3013 str = ci->values[0].value.string;
3014 if (strcasecmp ("Encrypt", str) == 0)
3015 *retval = SECURITY_LEVEL_ENCRYPT;
3016 else if (strcasecmp ("Sign", str) == 0)
3017 *retval = SECURITY_LEVEL_SIGN;
3018 else if (strcasecmp ("None", str) == 0)
3019 *retval = SECURITY_LEVEL_NONE;
3022 WARNING ("network plugin: Unknown security level: %s.", str);
3027 } /* }}} int network_config_set_security_level */
3028 #endif /* HAVE_LIBGCRYPT */
3030 static int network_config_add_listen (const oconfig_item_t *ci) /* {{{ */
3036 if ((ci->values_num < 1) || (ci->values_num > 2)
3037 || (ci->values[0].type != OCONFIG_TYPE_STRING)
3038 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
3040 ERROR ("network plugin: The `%s' config option needs "
3041 "one or two string arguments.", ci->key);
3045 se = sockent_create (SOCKENT_TYPE_SERVER);
3048 ERROR ("network plugin: sockent_create failed.");
3052 se->node = strdup (ci->values[0].value.string);
3053 if (ci->values_num >= 2)
3054 se->service = strdup (ci->values[1].value.string);
3056 for (i = 0; i < ci->children_num; i++)
3058 oconfig_item_t *child = ci->children + i;
3061 if (strcasecmp ("AuthFile", child->key) == 0)
3062 cf_util_get_string (child, &se->data.server.auth_file);
3063 else if (strcasecmp ("SecurityLevel", child->key) == 0)
3064 network_config_set_security_level (child,
3065 &se->data.server.security_level);
3067 #endif /* HAVE_LIBGCRYPT */
3068 if (strcasecmp ("Interface", child->key) == 0)
3069 network_config_set_interface (child, &se->interface);
3072 WARNING ("network plugin: Option `%s' is not allowed here.",
3078 if ((se->data.server.security_level > SECURITY_LEVEL_NONE)
3079 && (se->data.server.auth_file == NULL))
3081 ERROR ("network plugin: A security level higher than `none' was "
3082 "requested, but no AuthFile option was given. Cowardly refusing to "
3083 "open this socket!");
3084 sockent_destroy (se);
3087 #endif /* HAVE_LIBGCRYPT */
3089 status = sockent_init_crypto (se);
3092 ERROR ("network plugin: network_config_add_listen: sockent_init_crypto() failed.");
3093 sockent_destroy (se);
3097 status = sockent_server_listen (se);
3100 ERROR ("network plugin: network_config_add_listen: sockent_server_listen failed.");
3101 sockent_destroy (se);
3105 status = sockent_add (se);
3108 ERROR ("network plugin: network_config_add_listen: sockent_add failed.");
3109 sockent_destroy (se);
3114 } /* }}} int network_config_add_listen */
3116 static int network_config_add_server (const oconfig_item_t *ci) /* {{{ */
3122 if ((ci->values_num < 1) || (ci->values_num > 2)
3123 || (ci->values[0].type != OCONFIG_TYPE_STRING)
3124 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
3126 ERROR ("network plugin: The `%s' config option needs "
3127 "one or two string arguments.", ci->key);
3131 se = sockent_create (SOCKENT_TYPE_CLIENT);
3134 ERROR ("network plugin: sockent_create failed.");
3138 se->node = strdup (ci->values[0].value.string);
3139 if (ci->values_num >= 2)
3140 se->service = strdup (ci->values[1].value.string);
3142 for (i = 0; i < ci->children_num; i++)
3144 oconfig_item_t *child = ci->children + i;
3147 if (strcasecmp ("Username", child->key) == 0)
3148 cf_util_get_string (child, &se->data.client.username);
3149 else if (strcasecmp ("Password", child->key) == 0)
3150 cf_util_get_string (child, &se->data.client.password);
3151 else if (strcasecmp ("SecurityLevel", child->key) == 0)
3152 network_config_set_security_level (child,
3153 &se->data.client.security_level);
3155 #endif /* HAVE_LIBGCRYPT */
3156 if (strcasecmp ("Interface", child->key) == 0)
3157 network_config_set_interface (child, &se->interface);
3158 else if (strcasecmp ("ResolveInterval", child->key) == 0)
3159 cf_util_get_cdtime(child, &se->data.client.resolve_interval);
3162 WARNING ("network plugin: Option `%s' is not allowed here.",
3168 if ((se->data.client.security_level > SECURITY_LEVEL_NONE)
3169 && ((se->data.client.username == NULL)
3170 || (se->data.client.password == NULL)))
3172 ERROR ("network plugin: A security level higher than `none' was "
3173 "requested, but no Username or Password option was given. "
3174 "Cowardly refusing to open this socket!");
3175 sockent_destroy (se);
3178 #endif /* HAVE_LIBGCRYPT */
3180 status = sockent_init_crypto (se);
3183 ERROR ("network plugin: network_config_add_server: sockent_init_crypto() failed.");
3184 sockent_destroy (se);
3188 /* No call to sockent_client_connect() here -- it is called from
3189 * network_send_buffer_plain(). */
3191 status = sockent_add (se);
3194 ERROR ("network plugin: network_config_add_server: sockent_add failed.");
3195 sockent_destroy (se);
3200 } /* }}} int network_config_add_server */
3202 static int network_config (oconfig_item_t *ci) /* {{{ */
3206 /* The options need to be applied first */
3207 for (i = 0; i < ci->children_num; i++)
3209 oconfig_item_t *child = ci->children + i;
3210 if (strcasecmp ("TimeToLive", child->key) == 0)
3211 network_config_set_ttl (child);
3214 for (i = 0; i < ci->children_num; i++)
3216 oconfig_item_t *child = ci->children + i;
3218 if (strcasecmp ("Listen", child->key) == 0)
3219 network_config_add_listen (child);
3220 else if (strcasecmp ("Server", child->key) == 0)
3221 network_config_add_server (child);
3222 else if (strcasecmp ("TimeToLive", child->key) == 0) {
3223 /* Handled earlier */
3225 else if (strcasecmp ("MaxPacketSize", child->key) == 0)
3226 network_config_set_buffer_size (child);
3227 else if (strcasecmp ("Forward", child->key) == 0)
3228 cf_util_get_boolean (child, &network_config_forward);
3229 else if (strcasecmp ("ReportStats", child->key) == 0)
3230 cf_util_get_boolean (child, &network_config_stats);
3233 WARNING ("network plugin: Option `%s' is not allowed here.",
3239 } /* }}} int network_config */
3241 static int network_notification (const notification_t *n,
3242 user_data_t __attribute__((unused)) *user_data)
3244 char buffer[network_config_packet_size];
3245 char *buffer_ptr = buffer;
3246 int buffer_free = sizeof (buffer);
3249 if (!check_send_notify_okay (n))
3252 memset (buffer, 0, sizeof (buffer));
3254 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_TIME_HR,
3255 (uint64_t) n->time);
3259 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_SEVERITY,
3260 (uint64_t) n->severity);
3264 if (strlen (n->host) > 0)
3266 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_HOST,
3267 n->host, strlen (n->host));
3272 if (strlen (n->plugin) > 0)
3274 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_PLUGIN,
3275 n->plugin, strlen (n->plugin));
3280 if (strlen (n->plugin_instance) > 0)
3282 status = write_part_string (&buffer_ptr, &buffer_free,
3283 TYPE_PLUGIN_INSTANCE,
3284 n->plugin_instance, strlen (n->plugin_instance));
3289 if (strlen (n->type) > 0)
3291 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE,
3292 n->type, strlen (n->type));
3297 if (strlen (n->type_instance) > 0)
3299 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE_INSTANCE,
3300 n->type_instance, strlen (n->type_instance));
3305 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_MESSAGE,
3306 n->message, strlen (n->message));
3310 network_send_buffer (buffer, sizeof (buffer) - buffer_free);
3313 } /* int network_notification */
3315 static int network_shutdown (void)
3321 /* Kill the listening thread */
3322 if (receive_thread_running != 0)
3324 INFO ("network plugin: Stopping receive thread.");
3325 pthread_kill (receive_thread_id, SIGTERM);
3326 pthread_join (receive_thread_id, NULL /* no return value */);
3327 memset (&receive_thread_id, 0, sizeof (receive_thread_id));
3328 receive_thread_running = 0;
3331 /* Shutdown the dispatching thread */
3332 if (dispatch_thread_running != 0)
3334 INFO ("network plugin: Stopping dispatch thread.");
3335 pthread_mutex_lock (&receive_list_lock);
3336 pthread_cond_broadcast (&receive_list_cond);
3337 pthread_mutex_unlock (&receive_list_lock);
3338 pthread_join (dispatch_thread_id, /* ret = */ NULL);
3339 dispatch_thread_running = 0;
3342 sockent_destroy (listen_sockets);
3344 if (send_buffer_fill > 0)
3347 sfree (send_buffer);
3349 for (se = sending_sockets; se != NULL; se = se->next)
3350 sockent_client_disconnect (se);
3351 sockent_destroy (sending_sockets);
3353 plugin_unregister_config ("network");
3354 plugin_unregister_init ("network");
3355 plugin_unregister_write ("network");
3356 plugin_unregister_shutdown ("network");
3359 } /* int network_shutdown */
3361 static int network_stats_read (void) /* {{{ */
3363 derive_t copy_octets_rx;
3364 derive_t copy_octets_tx;
3365 derive_t copy_packets_rx;
3366 derive_t copy_packets_tx;
3367 derive_t copy_values_dispatched;
3368 derive_t copy_values_not_dispatched;
3369 derive_t copy_values_sent;
3370 derive_t copy_values_not_sent;
3371 derive_t copy_receive_list_length;
3372 value_list_t vl = VALUE_LIST_INIT;
3375 copy_octets_rx = stats_octets_rx;
3376 copy_octets_tx = stats_octets_tx;
3377 copy_packets_rx = stats_packets_rx;
3378 copy_packets_tx = stats_packets_tx;
3379 copy_values_dispatched = stats_values_dispatched;
3380 copy_values_not_dispatched = stats_values_not_dispatched;
3381 copy_values_sent = stats_values_sent;
3382 copy_values_not_sent = stats_values_not_sent;
3383 copy_receive_list_length = receive_list_length;
3385 /* Initialize `vl' */
3389 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
3390 sstrncpy (vl.plugin, "network", sizeof (vl.plugin));
3392 /* Octets received / sent */
3393 vl.values[0].derive = (derive_t) copy_octets_rx;
3394 vl.values[1].derive = (derive_t) copy_octets_tx;
3395 sstrncpy (vl.type, "if_octets", sizeof (vl.type));
3396 plugin_dispatch_values (&vl);
3398 /* Packets received / send */
3399 vl.values[0].derive = (derive_t) copy_packets_rx;
3400 vl.values[1].derive = (derive_t) copy_packets_tx;
3401 sstrncpy (vl.type, "if_packets", sizeof (vl.type));
3402 plugin_dispatch_values (&vl);
3404 /* Values (not) dispatched and (not) send */
3405 sstrncpy (vl.type, "total_values", sizeof (vl.type));
3408 vl.values[0].derive = (derive_t) copy_values_dispatched;
3409 sstrncpy (vl.type_instance, "dispatch-accepted",
3410 sizeof (vl.type_instance));
3411 plugin_dispatch_values (&vl);
3413 vl.values[0].derive = (derive_t) copy_values_not_dispatched;
3414 sstrncpy (vl.type_instance, "dispatch-rejected",
3415 sizeof (vl.type_instance));
3416 plugin_dispatch_values (&vl);
3418 vl.values[0].derive = (derive_t) copy_values_sent;
3419 sstrncpy (vl.type_instance, "send-accepted",
3420 sizeof (vl.type_instance));
3421 plugin_dispatch_values (&vl);
3423 vl.values[0].derive = (derive_t) copy_values_not_sent;
3424 sstrncpy (vl.type_instance, "send-rejected",
3425 sizeof (vl.type_instance));
3426 plugin_dispatch_values (&vl);
3428 /* Receive queue length */
3429 vl.values[0].gauge = (gauge_t) copy_receive_list_length;
3430 sstrncpy (vl.type, "queue_length", sizeof (vl.type));
3431 vl.type_instance[0] = 0;
3432 plugin_dispatch_values (&vl);
3435 } /* }}} int network_stats_read */
3437 static int network_init (void)
3439 static _Bool have_init = 0;
3441 /* Check if we were already initialized. If so, just return - there's
3442 * nothing more to do (for now, that is). */
3448 network_init_gcrypt ();
3451 if (network_config_stats)
3452 plugin_register_read ("network", network_stats_read);
3454 plugin_register_shutdown ("network", network_shutdown);
3456 send_buffer = malloc (network_config_packet_size);
3457 if (send_buffer == NULL)
3459 ERROR ("network plugin: malloc failed.");
3462 network_init_buffer ();
3464 /* setup socket(s) and so on */
3465 if (sending_sockets != NULL)
3467 plugin_register_write ("network", network_write,
3468 /* user_data = */ NULL);
3469 plugin_register_notification ("network", network_notification,
3470 /* user_data = */ NULL);
3473 /* If no threads need to be started, return here. */
3474 if ((listen_sockets_num == 0)
3475 || ((dispatch_thread_running != 0)
3476 && (receive_thread_running != 0)))
3479 if (dispatch_thread_running == 0)
3482 status = plugin_thread_create (&dispatch_thread_id,
3483 NULL /* no attributes */,
3485 NULL /* no argument */);
3489 ERROR ("network: pthread_create failed: %s",
3490 sstrerror (errno, errbuf,
3495 dispatch_thread_running = 1;
3499 if (receive_thread_running == 0)
3502 status = plugin_thread_create (&receive_thread_id,
3503 NULL /* no attributes */,
3505 NULL /* no argument */);
3509 ERROR ("network: pthread_create failed: %s",
3510 sstrerror (errno, errbuf,
3515 receive_thread_running = 1;
3520 } /* int network_init */
3523 * The flush option of the network plugin cannot flush individual identifiers.
3524 * All the values are added to a buffer and sent when the buffer is full, the
3525 * requested value may or may not be in there, it's not worth finding out. We
3526 * just send the buffer if `flush' is called - if the requested value was in
3527 * there, good. If not, well, then there is nothing to flush.. -octo
3529 static int network_flush (cdtime_t timeout,
3530 __attribute__((unused)) const char *identifier,
3531 __attribute__((unused)) user_data_t *user_data)
3533 pthread_mutex_lock (&send_buffer_lock);
3535 if (send_buffer_fill > 0)
3539 cdtime_t now = cdtime ();
3540 if ((send_buffer_last_update + timeout) > now)
3542 pthread_mutex_unlock (&send_buffer_lock);
3548 pthread_mutex_unlock (&send_buffer_lock);
3551 } /* int network_flush */
3553 void module_register (void)
3555 plugin_register_complex_config ("network", network_config);
3556 plugin_register_init ("network", network_init);
3557 plugin_register_flush ("network", network_flush,
3558 /* user_data = */ NULL);
3559 } /* void module_register */
3561 /* vim: set fdm=marker : */