2 * collectd - src/network.c
3 * Copyright (C) 2005-2013 Florian octo Forster
4 * Copyright (C) 2009 Aman Gupta
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; only version 2.1 of the License is
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 * Florian octo Forster <octo at collectd.org>
22 * Aman Gupta <aman at tmm1.net>
25 #define _DEFAULT_SOURCE
26 #define _BSD_SOURCE /* For struct ip_mreq */
31 #include "configfile.h"
32 #include "utils_fbhash.h"
33 #include "utils_avltree.h"
34 #include "utils_cache.h"
35 #include "utils_complain.h"
43 # include <sys/socket.h>
49 # include <netinet/in.h>
52 # include <arpa/inet.h>
63 # if defined __APPLE__
64 /* default xcode compiler throws warnings even when deprecated functionality
65 * is not used. -Werror breaks the build because of erroneous warnings.
66 * http://stackoverflow.com/questions/10556299/compiler-warnings-with-libgcrypt-v1-5-0/12830209#12830209
68 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
70 /* FreeBSD's copy of libgcrypt extends the existing GCRYPT_NO_DEPRECATED
71 * to properly hide all deprecated functionality.
72 * http://svnweb.freebsd.org/ports/head/security/libgcrypt/files/patch-src__gcrypt.h.in
74 # define GCRYPT_NO_DEPRECATED
76 # if defined __APPLE__
77 /* Re enable deprecation warnings */
78 # pragma GCC diagnostic warning "-Wdeprecated-declarations"
80 # if GCRYPT_VERSION_NUMBER < 0x010600
81 GCRY_THREAD_OPTION_PTHREAD_IMPL;
85 #ifndef IPV6_ADD_MEMBERSHIP
86 # ifdef IPV6_JOIN_GROUP
87 # define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
89 # error "Neither IP_ADD_MEMBERSHIP nor IPV6_JOIN_GROUP is defined"
91 #endif /* !IP_ADD_MEMBERSHIP */
94 * Maximum size required for encryption / signing:
96 * 42 bytes for the encryption header
97 * + 64 bytes for the username
101 #define BUFF_SIG_SIZE 106
106 #define SECURITY_LEVEL_NONE 0
108 # define SECURITY_LEVEL_SIGN 1
109 # define SECURITY_LEVEL_ENCRYPT 2
111 struct sockent_client
114 struct sockaddr_storage *addr;
120 gcry_cipher_hd_t cypher;
121 unsigned char password_hash[32];
123 cdtime_t next_resolve_reconnect;
124 cdtime_t resolve_interval;
127 struct sockent_server
135 gcry_cipher_hd_t cypher;
139 typedef struct sockent
141 #define SOCKENT_TYPE_CLIENT 1
142 #define SOCKENT_TYPE_SERVER 2
151 struct sockent_client client;
152 struct sockent_server server;
155 struct sockent *next;
158 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
159 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
160 * +-------+-----------------------+-------------------------------+
161 * ! Ver. ! ! Length !
162 * +-------+-----------------------+-------------------------------+
169 typedef struct part_header_s part_header_t;
171 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
172 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
173 * +-------------------------------+-------------------------------+
175 * +-------------------------------+-------------------------------+
176 * : (Length - 4) Bytes :
177 * +---------------------------------------------------------------+
184 typedef struct part_string_s part_string_t;
186 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
187 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
188 * +-------------------------------+-------------------------------+
190 * +-------------------------------+-------------------------------+
191 * : (Length - 4 == 2 || 4 || 8) Bytes :
192 * +---------------------------------------------------------------+
199 typedef struct part_number_s part_number_t;
201 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
202 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
203 * +-------------------------------+-------------------------------+
205 * +-------------------------------+---------------+---------------+
206 * ! Num of values ! Type0 ! Type1 !
207 * +-------------------------------+---------------+---------------+
210 * +---------------------------------------------------------------+
213 * +---------------------------------------------------------------+
218 uint16_t *num_values;
219 uint8_t *values_types;
222 typedef struct part_values_s part_values_t;
224 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
225 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
226 * +-------------------------------+-------------------------------+
228 * +-------------------------------+-------------------------------+
229 * ! Hash (Bits 0 - 31) !
231 * ! Hash (Bits 224 - 255) !
232 * +---------------------------------------------------------------+
235 #define PART_SIGNATURE_SHA256_SIZE 36
236 struct part_signature_sha256_s
239 unsigned char hash[32];
242 typedef struct part_signature_sha256_s part_signature_sha256_t;
244 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
245 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
246 * +-------------------------------+-------------------------------+
248 * +-------------------------------+-------------------------------+
249 * ! Original length ! Padding (0 - 15 bytes) !
250 * +-------------------------------+-------------------------------+
251 * ! Hash (Bits 0 - 31) !
253 * ! Hash (Bits 128 - 159) !
254 * +---------------------------------------------------------------+
257 #define PART_ENCRYPTION_AES256_SIZE 42
258 struct part_encryption_aes256_s
261 uint16_t username_length;
263 unsigned char iv[16];
265 unsigned char hash[20];
269 typedef struct part_encryption_aes256_s part_encryption_aes256_t;
271 struct receive_list_entry_s
276 struct receive_list_entry_s *next;
278 typedef struct receive_list_entry_s receive_list_entry_t;
283 static int network_config_ttl = 0;
284 /* Ethernet - (IPv6 + UDP) = 1500 - (40 + 8) = 1452 */
285 static size_t network_config_packet_size = 1452;
286 static _Bool network_config_forward = 0;
287 static _Bool network_config_stats = 0;
289 static sockent_t *sending_sockets = NULL;
291 static receive_list_entry_t *receive_list_head = NULL;
292 static receive_list_entry_t *receive_list_tail = NULL;
293 static pthread_mutex_t receive_list_lock = PTHREAD_MUTEX_INITIALIZER;
294 static pthread_cond_t receive_list_cond = PTHREAD_COND_INITIALIZER;
295 static uint64_t receive_list_length = 0;
297 static sockent_t *listen_sockets = NULL;
298 static struct pollfd *listen_sockets_pollfd = NULL;
299 static size_t listen_sockets_num = 0;
301 /* The receive and dispatch threads will run as long as `listen_loop' is set to
303 static int listen_loop = 0;
304 static int receive_thread_running = 0;
305 static pthread_t receive_thread_id;
306 static int dispatch_thread_running = 0;
307 static pthread_t dispatch_thread_id;
309 /* Buffer in which to-be-sent network packets are constructed. */
310 static char *send_buffer;
311 static char *send_buffer_ptr;
312 static int send_buffer_fill;
313 static cdtime_t send_buffer_last_update;
314 static value_list_t send_buffer_vl = VALUE_LIST_STATIC;
315 static pthread_mutex_t send_buffer_lock = PTHREAD_MUTEX_INITIALIZER;
317 /* XXX: These counters are incremented from one place only. The spot in which
318 * the values are incremented is either only reachable by one thread (the
319 * dispatch thread, for example) or locked by some lock (send_buffer_lock for
320 * example). Only if neither is true, the stats_lock is acquired. The counters
321 * are always read without holding a lock in the hope that writing 8 bytes to
322 * memory is an atomic operation. */
323 static derive_t stats_octets_rx = 0;
324 static derive_t stats_octets_tx = 0;
325 static derive_t stats_packets_rx = 0;
326 static derive_t stats_packets_tx = 0;
327 static derive_t stats_values_dispatched = 0;
328 static derive_t stats_values_not_dispatched = 0;
329 static derive_t stats_values_sent = 0;
330 static derive_t stats_values_not_sent = 0;
331 static pthread_mutex_t stats_lock = PTHREAD_MUTEX_INITIALIZER;
336 static _Bool check_receive_okay (const value_list_t *vl) /* {{{ */
338 uint64_t time_sent = 0;
341 status = uc_meta_data_get_unsigned_int (vl,
342 "network:time_sent", &time_sent);
344 /* This is a value we already sent. Don't allow it to be received again in
345 * order to avoid looping. */
346 if ((status == 0) && (time_sent >= ((uint64_t) vl->time)))
350 } /* }}} _Bool check_receive_okay */
352 static _Bool check_send_okay (const value_list_t *vl) /* {{{ */
357 if (network_config_forward)
360 if (vl->meta == NULL)
363 status = meta_data_get_boolean (vl->meta, "network:received", &received);
364 if (status == -ENOENT)
366 else if (status != 0)
368 ERROR ("network plugin: check_send_okay: meta_data_get_boolean failed "
369 "with status %i.", status);
373 /* By default, only *send* value lists that were not *received* by the
376 } /* }}} _Bool check_send_okay */
378 static _Bool check_notify_received (const notification_t *n) /* {{{ */
380 notification_meta_t *ptr;
382 for (ptr = n->meta; ptr != NULL; ptr = ptr->next)
383 if ((strcmp ("network:received", ptr->name) == 0)
384 && (ptr->type == NM_TYPE_BOOLEAN))
385 return ((_Bool) ptr->nm_value.nm_boolean);
388 } /* }}} _Bool check_notify_received */
390 static _Bool check_send_notify_okay (const notification_t *n) /* {{{ */
392 static c_complain_t complain_forwarding = C_COMPLAIN_INIT_STATIC;
398 received = check_notify_received (n);
400 if (network_config_forward && received)
402 c_complain_once (LOG_ERR, &complain_forwarding,
403 "network plugin: A notification has been received via the network "
404 "and forwarding is enabled. Forwarding of notifications is currently "
405 "not supported, because there is not loop-deteciton available. "
406 "Please contact the collectd mailing list if you need this "
410 /* By default, only *send* value lists that were not *received* by the
413 } /* }}} _Bool check_send_notify_okay */
415 static int network_dispatch_values (value_list_t *vl, /* {{{ */
416 const char *username)
421 || (strlen (vl->host) <= 0)
422 || (strlen (vl->plugin) <= 0)
423 || (strlen (vl->type) <= 0))
426 if (!check_receive_okay (vl))
429 char name[6*DATA_MAX_NAME_LEN];
430 FORMAT_VL (name, sizeof (name), vl);
431 name[sizeof (name) - 1] = 0;
432 DEBUG ("network plugin: network_dispatch_values: "
433 "NOT dispatching %s.", name);
435 stats_values_not_dispatched++;
439 assert (vl->meta == NULL);
441 vl->meta = meta_data_create ();
442 if (vl->meta == NULL)
444 ERROR ("network plugin: meta_data_create failed.");
448 status = meta_data_add_boolean (vl->meta, "network:received", 1);
451 ERROR ("network plugin: meta_data_add_boolean failed.");
452 meta_data_destroy (vl->meta);
457 if (username != NULL)
459 status = meta_data_add_string (vl->meta, "network:username", username);
462 ERROR ("network plugin: meta_data_add_string failed.");
463 meta_data_destroy (vl->meta);
469 plugin_dispatch_values (vl);
470 stats_values_dispatched++;
472 meta_data_destroy (vl->meta);
476 } /* }}} int network_dispatch_values */
478 static int network_dispatch_notification (notification_t *n) /* {{{ */
482 assert (n->meta == NULL);
484 status = plugin_notification_meta_add_boolean (n, "network:received", 1);
487 ERROR ("network plugin: plugin_notification_meta_add_boolean failed.");
488 plugin_notification_meta_free (n->meta);
493 status = plugin_dispatch_notification (n);
495 plugin_notification_meta_free (n->meta);
499 } /* }}} int network_dispatch_notification */
502 static void network_init_gcrypt (void) /* {{{ */
504 /* http://lists.gnupg.org/pipermail/gcrypt-devel/2003-August/000458.html
505 * Because you can't know in a library whether another library has
506 * already initialized the library */
507 if (gcry_control (GCRYCTL_ANY_INITIALIZATION_P))
510 /* http://www.gnupg.org/documentation/manuals/gcrypt/Multi_002dThreading.html
511 * To ensure thread-safety, it's important to set GCRYCTL_SET_THREAD_CBS
512 * *before* initalizing Libgcrypt with gcry_check_version(), which itself must
513 * be called before any other gcry_* function. GCRYCTL_ANY_INITIALIZATION_P
514 * above doesn't count, as it doesn't implicitly initalize Libgcrypt.
516 * tl;dr: keep all these gry_* statements in this exact order please. */
517 # if GCRYPT_VERSION_NUMBER < 0x010600
518 gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
520 gcry_check_version (NULL);
521 gcry_control (GCRYCTL_INIT_SECMEM, 32768);
522 gcry_control (GCRYCTL_INITIALIZATION_FINISHED);
523 } /* }}} void network_init_gcrypt */
525 static gcry_cipher_hd_t network_get_aes256_cypher (sockent_t *se, /* {{{ */
526 const void *iv, size_t iv_size, const char *username)
529 gcry_cipher_hd_t *cyper_ptr;
530 unsigned char password_hash[32];
532 if (se->type == SOCKENT_TYPE_CLIENT)
534 cyper_ptr = &se->data.client.cypher;
535 memcpy (password_hash, se->data.client.password_hash,
536 sizeof (password_hash));
542 cyper_ptr = &se->data.server.cypher;
544 if (username == NULL)
547 secret = fbh_get (se->data.server.userdb, username);
551 gcry_md_hash_buffer (GCRY_MD_SHA256,
553 secret, strlen (secret));
558 if (*cyper_ptr == NULL)
560 err = gcry_cipher_open (cyper_ptr,
561 GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_OFB, /* flags = */ 0);
564 ERROR ("network plugin: gcry_cipher_open returned: %s",
565 gcry_strerror (err));
572 gcry_cipher_reset (*cyper_ptr);
574 assert (*cyper_ptr != NULL);
576 err = gcry_cipher_setkey (*cyper_ptr,
577 password_hash, sizeof (password_hash));
580 ERROR ("network plugin: gcry_cipher_setkey returned: %s",
581 gcry_strerror (err));
582 gcry_cipher_close (*cyper_ptr);
587 err = gcry_cipher_setiv (*cyper_ptr, iv, iv_size);
590 ERROR ("network plugin: gcry_cipher_setkey returned: %s",
591 gcry_strerror (err));
592 gcry_cipher_close (*cyper_ptr);
598 } /* }}} int network_get_aes256_cypher */
599 #endif /* HAVE_LIBGCRYPT */
601 static int write_part_values (char **ret_buffer, int *ret_buffer_len,
602 const data_set_t *ds, const value_list_t *vl)
608 part_header_t pkg_ph;
609 uint16_t pkg_num_values;
610 uint8_t *pkg_values_types;
616 num_values = vl->values_len;
617 packet_len = sizeof (part_header_t) + sizeof (uint16_t)
618 + (num_values * sizeof (uint8_t))
619 + (num_values * sizeof (value_t));
621 if (*ret_buffer_len < packet_len)
624 pkg_values_types = (uint8_t *) malloc (num_values * sizeof (uint8_t));
625 if (pkg_values_types == NULL)
627 ERROR ("network plugin: write_part_values: malloc failed.");
631 pkg_values = (value_t *) malloc (num_values * sizeof (value_t));
632 if (pkg_values == NULL)
634 free (pkg_values_types);
635 ERROR ("network plugin: write_part_values: malloc failed.");
639 pkg_ph.type = htons (TYPE_VALUES);
640 pkg_ph.length = htons (packet_len);
642 pkg_num_values = htons ((uint16_t) vl->values_len);
644 for (i = 0; i < num_values; i++)
646 pkg_values_types[i] = (uint8_t) ds->ds[i].type;
647 switch (ds->ds[i].type)
649 case DS_TYPE_COUNTER:
650 pkg_values[i].counter = htonll (vl->values[i].counter);
654 pkg_values[i].gauge = htond (vl->values[i].gauge);
658 pkg_values[i].derive = htonll (vl->values[i].derive);
661 case DS_TYPE_ABSOLUTE:
662 pkg_values[i].absolute = htonll (vl->values[i].absolute);
666 free (pkg_values_types);
668 ERROR ("network plugin: write_part_values: "
669 "Unknown data source type: %i",
672 } /* switch (ds->ds[i].type) */
673 } /* for (num_values) */
676 * Use `memcpy' to write everything to the buffer, because the pointer
677 * may be unaligned and some architectures, such as SPARC, can't handle
680 packet_ptr = *ret_buffer;
682 memcpy (packet_ptr + offset, &pkg_ph, sizeof (pkg_ph));
683 offset += sizeof (pkg_ph);
684 memcpy (packet_ptr + offset, &pkg_num_values, sizeof (pkg_num_values));
685 offset += sizeof (pkg_num_values);
686 memcpy (packet_ptr + offset, pkg_values_types, num_values * sizeof (uint8_t));
687 offset += num_values * sizeof (uint8_t);
688 memcpy (packet_ptr + offset, pkg_values, num_values * sizeof (value_t));
689 offset += num_values * sizeof (value_t);
691 assert (offset == packet_len);
693 *ret_buffer = packet_ptr + packet_len;
694 *ret_buffer_len -= packet_len;
696 free (pkg_values_types);
700 } /* int write_part_values */
702 static int write_part_number (char **ret_buffer, int *ret_buffer_len,
703 int type, uint64_t value)
708 part_header_t pkg_head;
713 packet_len = sizeof (pkg_head) + sizeof (pkg_value);
715 if (*ret_buffer_len < packet_len)
718 pkg_head.type = htons (type);
719 pkg_head.length = htons (packet_len);
720 pkg_value = htonll (value);
722 packet_ptr = *ret_buffer;
724 memcpy (packet_ptr + offset, &pkg_head, sizeof (pkg_head));
725 offset += sizeof (pkg_head);
726 memcpy (packet_ptr + offset, &pkg_value, sizeof (pkg_value));
727 offset += sizeof (pkg_value);
729 assert (offset == packet_len);
731 *ret_buffer = packet_ptr + packet_len;
732 *ret_buffer_len -= packet_len;
735 } /* int write_part_number */
737 static int write_part_string (char **ret_buffer, int *ret_buffer_len,
738 int type, const char *str, int str_len)
748 buffer_len = 2 * sizeof (uint16_t) + str_len + 1;
749 if (*ret_buffer_len < buffer_len)
752 pkg_type = htons (type);
753 pkg_length = htons (buffer_len);
755 buffer = *ret_buffer;
757 memcpy (buffer + offset, (void *) &pkg_type, sizeof (pkg_type));
758 offset += sizeof (pkg_type);
759 memcpy (buffer + offset, (void *) &pkg_length, sizeof (pkg_length));
760 offset += sizeof (pkg_length);
761 memcpy (buffer + offset, str, str_len);
763 memset (buffer + offset, '\0', 1);
766 assert (offset == buffer_len);
768 *ret_buffer = buffer + buffer_len;
769 *ret_buffer_len -= buffer_len;
772 } /* int write_part_string */
774 static int parse_part_values (void **ret_buffer, size_t *ret_buffer_len,
775 value_t **ret_values, int *ret_num_values)
777 char *buffer = *ret_buffer;
778 size_t buffer_len = *ret_buffer_len;
793 NOTICE ("network plugin: packet is too short: "
794 "buffer_len = %zu", buffer_len);
798 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
799 buffer += sizeof (tmp16);
800 pkg_type = ntohs (tmp16);
802 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
803 buffer += sizeof (tmp16);
804 pkg_length = ntohs (tmp16);
806 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
807 buffer += sizeof (tmp16);
808 pkg_numval = ntohs (tmp16);
810 assert (pkg_type == TYPE_VALUES);
812 exp_size = 3 * sizeof (uint16_t)
813 + pkg_numval * (sizeof (uint8_t) + sizeof (value_t));
814 if (buffer_len < exp_size)
816 WARNING ("network plugin: parse_part_values: "
818 "Chunk of size %zu expected, "
819 "but buffer has only %zu bytes left.",
820 exp_size, buffer_len);
824 if (pkg_length != exp_size)
826 WARNING ("network plugin: parse_part_values: "
827 "Length and number of values "
828 "in the packet don't match.");
832 pkg_types = (uint8_t *) malloc (pkg_numval * sizeof (uint8_t));
833 pkg_values = (value_t *) malloc (pkg_numval * sizeof (value_t));
834 if ((pkg_types == NULL) || (pkg_values == NULL))
838 ERROR ("network plugin: parse_part_values: malloc failed.");
842 memcpy ((void *) pkg_types, (void *) buffer, pkg_numval * sizeof (uint8_t));
843 buffer += pkg_numval * sizeof (uint8_t);
844 memcpy ((void *) pkg_values, (void *) buffer, pkg_numval * sizeof (value_t));
845 buffer += pkg_numval * sizeof (value_t);
847 for (i = 0; i < pkg_numval; i++)
849 switch (pkg_types[i])
851 case DS_TYPE_COUNTER:
852 pkg_values[i].counter = (counter_t) ntohll (pkg_values[i].counter);
856 pkg_values[i].gauge = (gauge_t) ntohd (pkg_values[i].gauge);
860 pkg_values[i].derive = (derive_t) ntohll (pkg_values[i].derive);
863 case DS_TYPE_ABSOLUTE:
864 pkg_values[i].absolute = (absolute_t) ntohll (pkg_values[i].absolute);
868 NOTICE ("network plugin: parse_part_values: "
869 "Don't know how to handle data source type %"PRIu8,
874 } /* switch (pkg_types[i]) */
877 *ret_buffer = buffer;
878 *ret_buffer_len = buffer_len - pkg_length;
879 *ret_num_values = pkg_numval;
880 *ret_values = pkg_values;
885 } /* int parse_part_values */
887 static int parse_part_number (void **ret_buffer, size_t *ret_buffer_len,
890 char *buffer = *ret_buffer;
891 size_t buffer_len = *ret_buffer_len;
895 size_t exp_size = 2 * sizeof (uint16_t) + sizeof (uint64_t);
899 if (buffer_len < exp_size)
901 WARNING ("network plugin: parse_part_number: "
903 "Chunk of size %zu expected, "
904 "but buffer has only %zu bytes left.",
905 exp_size, buffer_len);
909 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
910 buffer += sizeof (tmp16);
911 /* pkg_type = ntohs (tmp16); */
913 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
914 buffer += sizeof (tmp16);
915 pkg_length = ntohs (tmp16);
917 memcpy ((void *) &tmp64, buffer, sizeof (tmp64));
918 buffer += sizeof (tmp64);
919 *value = ntohll (tmp64);
921 *ret_buffer = buffer;
922 *ret_buffer_len = buffer_len - pkg_length;
925 } /* int parse_part_number */
927 static int parse_part_string (void **ret_buffer, size_t *ret_buffer_len,
928 char *output, size_t const output_len)
930 char *buffer = *ret_buffer;
931 size_t buffer_len = *ret_buffer_len;
934 size_t const header_size = 2 * sizeof (uint16_t);
942 if (buffer_len < header_size)
944 WARNING ("network plugin: parse_part_string: "
946 "Chunk of at least size %zu expected, "
947 "but buffer has only %zu bytes left.",
948 header_size, buffer_len);
952 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
953 buffer += sizeof (tmp16);
954 /* pkg_type = ntohs (tmp16); */
956 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
957 buffer += sizeof (tmp16);
958 pkg_length = ntohs (tmp16);
959 payload_size = ((size_t) pkg_length) - header_size;
961 /* Check that packet fits in the input buffer */
962 if (pkg_length > buffer_len)
964 WARNING ("network plugin: parse_part_string: "
966 "Chunk of size %"PRIu16" received, "
967 "but buffer has only %zu bytes left.",
968 pkg_length, buffer_len);
972 /* Check that pkg_length is in the valid range */
973 if (pkg_length <= header_size)
975 WARNING ("network plugin: parse_part_string: "
977 "Header claims this packet is only %hu "
978 "bytes long.", pkg_length);
982 /* Check that the package data fits into the output buffer.
983 * The previous if-statement ensures that:
984 * `pkg_length > header_size' */
985 if (output_len < payload_size)
987 WARNING ("network plugin: parse_part_string: "
989 "Output buffer holds %zu bytes, "
990 "which is too small to hold the received "
992 output_len, payload_size);
996 /* All sanity checks successfull, let's copy the data over */
997 memcpy ((void *) output, (void *) buffer, payload_size);
998 buffer += payload_size;
1000 /* For some very weird reason '\0' doesn't do the trick on SPARC in
1001 * this statement. */
1002 if (output[payload_size - 1] != 0)
1004 WARNING ("network plugin: parse_part_string: "
1005 "Received string does not end "
1006 "with a NULL-byte.");
1010 *ret_buffer = buffer;
1011 *ret_buffer_len = buffer_len - pkg_length;
1014 } /* int parse_part_string */
1016 /* Forward declaration: parse_part_sign_sha256 and parse_part_encr_aes256 call
1017 * parse_packet and vice versa. */
1018 #define PP_SIGNED 0x01
1019 #define PP_ENCRYPTED 0x02
1020 static int parse_packet (sockent_t *se,
1021 void *buffer, size_t buffer_size, int flags,
1022 const char *username);
1024 #define BUFFER_READ(p,s) do { \
1025 memcpy ((p), buffer + buffer_offset, (s)); \
1026 buffer_offset += (s); \
1030 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
1031 void **ret_buffer, size_t *ret_buffer_len, int flags)
1033 static c_complain_t complain_no_users = C_COMPLAIN_INIT_STATIC;
1037 size_t buffer_offset;
1039 size_t username_len;
1042 part_signature_sha256_t pss;
1043 uint16_t pss_head_length;
1044 char hash[sizeof (pss.hash)];
1048 unsigned char *hash_ptr;
1050 buffer = *ret_buffer;
1051 buffer_len = *ret_buffer_len;
1054 if (se->data.server.userdb == NULL)
1056 c_complain (LOG_NOTICE, &complain_no_users,
1057 "network plugin: Received signed network packet but can't verify it "
1058 "because no user DB has been configured. Will accept it.");
1062 /* Check if the buffer has enough data for this structure. */
1063 if (buffer_len <= PART_SIGNATURE_SHA256_SIZE)
1066 /* Read type and length header */
1067 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
1068 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
1069 pss_head_length = ntohs (pss.head.length);
1071 /* Check if the `pss_head_length' is within bounds. */
1072 if ((pss_head_length <= PART_SIGNATURE_SHA256_SIZE)
1073 || (pss_head_length > buffer_len))
1075 ERROR ("network plugin: HMAC-SHA-256 with invalid length received.");
1079 /* Copy the hash. */
1080 BUFFER_READ (pss.hash, sizeof (pss.hash));
1082 /* Calculate username length (without null byte) and allocate memory */
1083 username_len = pss_head_length - PART_SIGNATURE_SHA256_SIZE;
1084 pss.username = malloc (username_len + 1);
1085 if (pss.username == NULL)
1088 /* Read the username */
1089 BUFFER_READ (pss.username, username_len);
1090 pss.username[username_len] = 0;
1092 assert (buffer_offset == pss_head_length);
1094 /* Query the password */
1095 secret = fbh_get (se->data.server.userdb, pss.username);
1098 ERROR ("network plugin: Unknown user: %s", pss.username);
1099 sfree (pss.username);
1103 /* Create a hash device and check the HMAC */
1105 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
1108 ERROR ("network plugin: Creating HMAC-SHA-256 object failed: %s",
1109 gcry_strerror (err));
1111 sfree (pss.username);
1115 err = gcry_md_setkey (hd, secret, strlen (secret));
1118 ERROR ("network plugin: gcry_md_setkey failed: %s", gcry_strerror (err));
1121 sfree (pss.username);
1126 buffer + PART_SIGNATURE_SHA256_SIZE,
1127 buffer_len - PART_SIGNATURE_SHA256_SIZE);
1128 hash_ptr = gcry_md_read (hd, GCRY_MD_SHA256);
1129 if (hash_ptr == NULL)
1131 ERROR ("network plugin: gcry_md_read failed.");
1134 sfree (pss.username);
1137 memcpy (hash, hash_ptr, sizeof (hash));
1143 if (memcmp (pss.hash, hash, sizeof (pss.hash)) != 0)
1145 WARNING ("network plugin: Verifying HMAC-SHA-256 signature failed: "
1150 parse_packet (se, buffer + buffer_offset, buffer_len - buffer_offset,
1151 flags | PP_SIGNED, pss.username);
1155 sfree (pss.username);
1157 *ret_buffer = buffer + buffer_len;
1158 *ret_buffer_len = 0;
1161 } /* }}} int parse_part_sign_sha256 */
1162 /* #endif HAVE_LIBGCRYPT */
1164 #else /* if !HAVE_LIBGCRYPT */
1165 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
1166 void **ret_buffer, size_t *ret_buffer_size, int flags)
1168 static int warning_has_been_printed = 0;
1172 size_t buffer_offset;
1175 part_signature_sha256_t pss;
1177 buffer = *ret_buffer;
1178 buffer_size = *ret_buffer_size;
1181 if (buffer_size <= PART_SIGNATURE_SHA256_SIZE)
1184 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
1185 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
1186 part_len = ntohs (pss.head.length);
1188 if ((part_len <= PART_SIGNATURE_SHA256_SIZE)
1189 || (part_len > buffer_size))
1192 if (warning_has_been_printed == 0)
1194 WARNING ("network plugin: Received signed packet, but the network "
1195 "plugin was not linked with libgcrypt, so I cannot "
1196 "verify the signature. The packet will be accepted.");
1197 warning_has_been_printed = 1;
1200 parse_packet (se, buffer + part_len, buffer_size - part_len, flags,
1201 /* username = */ NULL);
1203 *ret_buffer = buffer + buffer_size;
1204 *ret_buffer_size = 0;
1207 } /* }}} int parse_part_sign_sha256 */
1208 #endif /* !HAVE_LIBGCRYPT */
1211 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1212 void **ret_buffer, size_t *ret_buffer_len,
1215 char *buffer = *ret_buffer;
1216 size_t buffer_len = *ret_buffer_len;
1219 size_t buffer_offset;
1220 uint16_t username_len;
1221 part_encryption_aes256_t pea;
1222 unsigned char hash[sizeof (pea.hash)];
1224 gcry_cipher_hd_t cypher;
1227 /* Make sure at least the header if available. */
1228 if (buffer_len <= PART_ENCRYPTION_AES256_SIZE)
1230 NOTICE ("network plugin: parse_part_encr_aes256: "
1231 "Discarding short packet.");
1237 /* Copy the unencrypted information into `pea'. */
1238 BUFFER_READ (&pea.head.type, sizeof (pea.head.type));
1239 BUFFER_READ (&pea.head.length, sizeof (pea.head.length));
1241 /* Check the `part size'. */
1242 part_size = ntohs (pea.head.length);
1243 if ((part_size <= PART_ENCRYPTION_AES256_SIZE)
1244 || (part_size > buffer_len))
1246 NOTICE ("network plugin: parse_part_encr_aes256: "
1247 "Discarding part with invalid size.");
1251 /* Read the username */
1252 BUFFER_READ (&username_len, sizeof (username_len));
1253 username_len = ntohs (username_len);
1255 if ((username_len <= 0)
1256 || (username_len > (part_size - (PART_ENCRYPTION_AES256_SIZE + 1))))
1258 NOTICE ("network plugin: parse_part_encr_aes256: "
1259 "Discarding part with invalid username length.");
1263 assert (username_len > 0);
1264 pea.username = malloc (username_len + 1);
1265 if (pea.username == NULL)
1267 BUFFER_READ (pea.username, username_len);
1268 pea.username[username_len] = 0;
1270 /* Last but not least, the initialization vector */
1271 BUFFER_READ (pea.iv, sizeof (pea.iv));
1273 /* Make sure we are at the right position */
1274 assert (buffer_offset == (username_len +
1275 PART_ENCRYPTION_AES256_SIZE - sizeof (pea.hash)));
1277 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
1281 sfree (pea.username);
1285 payload_len = part_size - (PART_ENCRYPTION_AES256_SIZE + username_len);
1286 assert (payload_len > 0);
1288 /* Decrypt the packet in-place */
1289 err = gcry_cipher_decrypt (cypher,
1290 buffer + buffer_offset,
1291 part_size - buffer_offset,
1292 /* in = */ NULL, /* in len = */ 0);
1295 sfree (pea.username);
1296 ERROR ("network plugin: gcry_cipher_decrypt returned: %s",
1297 gcry_strerror (err));
1302 BUFFER_READ (pea.hash, sizeof (pea.hash));
1304 /* Make sure we're at the right position - again */
1305 assert (buffer_offset == (username_len + PART_ENCRYPTION_AES256_SIZE));
1306 assert (buffer_offset == (part_size - payload_len));
1308 /* Check hash sum */
1309 memset (hash, 0, sizeof (hash));
1310 gcry_md_hash_buffer (GCRY_MD_SHA1, hash,
1311 buffer + buffer_offset, payload_len);
1312 if (memcmp (hash, pea.hash, sizeof (hash)) != 0)
1314 sfree (pea.username);
1315 ERROR ("network plugin: Decryption failed: Checksum mismatch.");
1319 parse_packet (se, buffer + buffer_offset, payload_len,
1320 flags | PP_ENCRYPTED, pea.username);
1322 /* XXX: Free pea.username?!? */
1324 /* Update return values */
1325 *ret_buffer = buffer + part_size;
1326 *ret_buffer_len = buffer_len - part_size;
1328 sfree (pea.username);
1331 } /* }}} int parse_part_encr_aes256 */
1332 /* #endif HAVE_LIBGCRYPT */
1334 #else /* if !HAVE_LIBGCRYPT */
1335 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1336 void **ret_buffer, size_t *ret_buffer_size, int flags)
1338 static int warning_has_been_printed = 0;
1342 size_t buffer_offset;
1347 buffer = *ret_buffer;
1348 buffer_size = *ret_buffer_size;
1351 /* parse_packet assures this minimum size. */
1352 assert (buffer_size >= (sizeof (ph.type) + sizeof (ph.length)));
1354 BUFFER_READ (&ph.type, sizeof (ph.type));
1355 BUFFER_READ (&ph.length, sizeof (ph.length));
1356 ph_length = ntohs (ph.length);
1358 if ((ph_length <= PART_ENCRYPTION_AES256_SIZE)
1359 || (ph_length > buffer_size))
1361 ERROR ("network plugin: AES-256 encrypted part "
1362 "with invalid length received.");
1366 if (warning_has_been_printed == 0)
1368 WARNING ("network plugin: Received encrypted packet, but the network "
1369 "plugin was not linked with libgcrypt, so I cannot "
1370 "decrypt it. The part will be discarded.");
1371 warning_has_been_printed = 1;
1374 *ret_buffer += ph_length;
1375 *ret_buffer_size -= ph_length;
1378 } /* }}} int parse_part_encr_aes256 */
1379 #endif /* !HAVE_LIBGCRYPT */
1383 static int parse_packet (sockent_t *se, /* {{{ */
1384 void *buffer, size_t buffer_size, int flags,
1385 const char *username)
1389 value_list_t vl = VALUE_LIST_INIT;
1393 int packet_was_signed = (flags & PP_SIGNED);
1394 int packet_was_encrypted = (flags & PP_ENCRYPTED);
1395 int printed_ignore_warning = 0;
1396 #endif /* HAVE_LIBGCRYPT */
1399 memset (&vl, '\0', sizeof (vl));
1400 memset (&n, '\0', sizeof (n));
1403 while ((status == 0) && (0 < buffer_size)
1404 && ((unsigned int) buffer_size > sizeof (part_header_t)))
1406 uint16_t pkg_length;
1409 memcpy ((void *) &pkg_type,
1412 memcpy ((void *) &pkg_length,
1413 (void *) (buffer + sizeof (pkg_type)),
1414 sizeof (pkg_length));
1416 pkg_length = ntohs (pkg_length);
1417 pkg_type = ntohs (pkg_type);
1419 if (pkg_length > buffer_size)
1421 /* Ensure that this loop terminates eventually */
1422 if (pkg_length < (2 * sizeof (uint16_t)))
1425 if (pkg_type == TYPE_ENCR_AES256)
1427 status = parse_part_encr_aes256 (se,
1428 &buffer, &buffer_size, flags);
1431 ERROR ("network plugin: Decrypting AES256 "
1433 "with status %i.", status);
1438 else if ((se->data.server.security_level == SECURITY_LEVEL_ENCRYPT)
1439 && (packet_was_encrypted == 0))
1441 if (printed_ignore_warning == 0)
1443 INFO ("network plugin: Unencrypted packet or "
1444 "part has been ignored.");
1445 printed_ignore_warning = 1;
1447 buffer = ((char *) buffer) + pkg_length;
1450 #endif /* HAVE_LIBGCRYPT */
1451 else if (pkg_type == TYPE_SIGN_SHA256)
1453 status = parse_part_sign_sha256 (se,
1454 &buffer, &buffer_size, flags);
1457 ERROR ("network plugin: Verifying HMAC-SHA-256 "
1459 "with status %i.", status);
1464 else if ((se->data.server.security_level == SECURITY_LEVEL_SIGN)
1465 && (packet_was_encrypted == 0)
1466 && (packet_was_signed == 0))
1468 if (printed_ignore_warning == 0)
1470 INFO ("network plugin: Unsigned packet or "
1471 "part has been ignored.");
1472 printed_ignore_warning = 1;
1474 buffer = ((char *) buffer) + pkg_length;
1477 #endif /* HAVE_LIBGCRYPT */
1478 else if (pkg_type == TYPE_VALUES)
1480 status = parse_part_values (&buffer, &buffer_size,
1481 &vl.values, &vl.values_len);
1485 network_dispatch_values (&vl, username);
1489 else if (pkg_type == TYPE_TIME)
1492 status = parse_part_number (&buffer, &buffer_size,
1496 vl.time = TIME_T_TO_CDTIME_T (tmp);
1497 n.time = TIME_T_TO_CDTIME_T (tmp);
1500 else if (pkg_type == TYPE_TIME_HR)
1503 status = parse_part_number (&buffer, &buffer_size,
1507 vl.time = (cdtime_t) tmp;
1508 n.time = (cdtime_t) tmp;
1511 else if (pkg_type == TYPE_INTERVAL)
1514 status = parse_part_number (&buffer, &buffer_size,
1517 vl.interval = TIME_T_TO_CDTIME_T (tmp);
1519 else if (pkg_type == TYPE_INTERVAL_HR)
1522 status = parse_part_number (&buffer, &buffer_size,
1525 vl.interval = (cdtime_t) tmp;
1527 else if (pkg_type == TYPE_HOST)
1529 status = parse_part_string (&buffer, &buffer_size,
1530 vl.host, sizeof (vl.host));
1532 sstrncpy (n.host, vl.host, sizeof (n.host));
1534 else if (pkg_type == TYPE_PLUGIN)
1536 status = parse_part_string (&buffer, &buffer_size,
1537 vl.plugin, sizeof (vl.plugin));
1539 sstrncpy (n.plugin, vl.plugin,
1542 else if (pkg_type == TYPE_PLUGIN_INSTANCE)
1544 status = parse_part_string (&buffer, &buffer_size,
1546 sizeof (vl.plugin_instance));
1548 sstrncpy (n.plugin_instance,
1550 sizeof (n.plugin_instance));
1552 else if (pkg_type == TYPE_TYPE)
1554 status = parse_part_string (&buffer, &buffer_size,
1555 vl.type, sizeof (vl.type));
1557 sstrncpy (n.type, vl.type, sizeof (n.type));
1559 else if (pkg_type == TYPE_TYPE_INSTANCE)
1561 status = parse_part_string (&buffer, &buffer_size,
1563 sizeof (vl.type_instance));
1565 sstrncpy (n.type_instance, vl.type_instance,
1566 sizeof (n.type_instance));
1568 else if (pkg_type == TYPE_MESSAGE)
1570 status = parse_part_string (&buffer, &buffer_size,
1571 n.message, sizeof (n.message));
1577 else if ((n.severity != NOTIF_FAILURE)
1578 && (n.severity != NOTIF_WARNING)
1579 && (n.severity != NOTIF_OKAY))
1581 INFO ("network plugin: "
1582 "Ignoring notification with "
1583 "unknown severity %i.",
1586 else if (n.time <= 0)
1588 INFO ("network plugin: "
1589 "Ignoring notification with "
1592 else if (strlen (n.message) <= 0)
1594 INFO ("network plugin: "
1595 "Ignoring notification with "
1596 "an empty message.");
1600 network_dispatch_notification (&n);
1603 else if (pkg_type == TYPE_SEVERITY)
1606 status = parse_part_number (&buffer, &buffer_size,
1609 n.severity = (int) tmp;
1613 DEBUG ("network plugin: parse_packet: Unknown part"
1614 " type: 0x%04hx", pkg_type);
1615 buffer = ((char *) buffer) + pkg_length;
1617 } /* while (buffer_size > sizeof (part_header_t)) */
1619 if (status == 0 && buffer_size > 0)
1620 WARNING ("network plugin: parse_packet: Received truncated "
1621 "packet, try increasing `MaxPacketSize'");
1624 } /* }}} int parse_packet */
1626 static void free_sockent_client (struct sockent_client *sec) /* {{{ */
1635 sfree (sec->username);
1636 sfree (sec->password);
1637 if (sec->cypher != NULL)
1638 gcry_cipher_close (sec->cypher);
1640 } /* }}} void free_sockent_client */
1642 static void free_sockent_server (struct sockent_server *ses) /* {{{ */
1646 for (i = 0; i < ses->fd_num; i++)
1648 if (ses->fd[i] >= 0)
1657 sfree (ses->auth_file);
1658 fbh_destroy (ses->userdb);
1659 if (ses->cypher != NULL)
1660 gcry_cipher_close (ses->cypher);
1662 } /* }}} void free_sockent_server */
1664 static void sockent_destroy (sockent_t *se) /* {{{ */
1668 DEBUG ("network plugin: sockent_destroy (se = %p);", (void *) se);
1675 sfree (se->service);
1677 if (se->type == SOCKENT_TYPE_CLIENT)
1678 free_sockent_client (&se->data.client);
1680 free_sockent_server (&se->data.server);
1685 } /* }}} void sockent_destroy */
1688 * int network_set_ttl
1690 * Set the `IP_MULTICAST_TTL', `IP_TTL', `IPV6_MULTICAST_HOPS' or
1691 * `IPV6_UNICAST_HOPS', depending on which option is applicable.
1693 * The `struct addrinfo' is used to destinguish between unicast and multicast
1696 static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
1698 DEBUG ("network plugin: network_set_ttl: network_config_ttl = %i;",
1699 network_config_ttl);
1701 assert (se->type == SOCKENT_TYPE_CLIENT);
1703 if ((network_config_ttl < 1) || (network_config_ttl > 255))
1706 if (ai->ai_family == AF_INET)
1708 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1711 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1712 optname = IP_MULTICAST_TTL;
1716 if (setsockopt (se->data.client.fd, IPPROTO_IP, optname,
1717 &network_config_ttl,
1718 sizeof (network_config_ttl)) != 0)
1721 ERROR ("network plugin: setsockopt (ipv4-ttl): %s",
1722 sstrerror (errno, errbuf, sizeof (errbuf)));
1726 else if (ai->ai_family == AF_INET6)
1728 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1729 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1732 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1733 optname = IPV6_MULTICAST_HOPS;
1735 optname = IPV6_UNICAST_HOPS;
1737 if (setsockopt (se->data.client.fd, IPPROTO_IPV6, optname,
1738 &network_config_ttl,
1739 sizeof (network_config_ttl)) != 0)
1742 ERROR ("network plugin: setsockopt(ipv6-ttl): %s",
1743 sstrerror (errno, errbuf,
1750 } /* int network_set_ttl */
1752 static int network_set_interface (const sockent_t *se, const struct addrinfo *ai) /* {{{ */
1754 DEBUG ("network plugin: network_set_interface: interface index = %i;",
1757 assert (se->type == SOCKENT_TYPE_CLIENT);
1759 if (ai->ai_family == AF_INET)
1761 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1763 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1765 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1766 /* If possible, use the "ip_mreqn" structure which has
1767 * an "interface index" member. Using the interface
1768 * index is preferred here, because of its similarity
1769 * to the way IPv6 handles this. Unfortunately, it
1770 * appears not to be portable. */
1771 struct ip_mreqn mreq;
1773 memset (&mreq, 0, sizeof (mreq));
1774 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1775 mreq.imr_address.s_addr = ntohl (INADDR_ANY);
1776 mreq.imr_ifindex = se->interface;
1778 struct ip_mreq mreq;
1780 memset (&mreq, 0, sizeof (mreq));
1781 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1782 mreq.imr_interface.s_addr = ntohl (INADDR_ANY);
1785 if (setsockopt (se->data.client.fd, IPPROTO_IP, IP_MULTICAST_IF,
1786 &mreq, sizeof (mreq)) != 0)
1789 ERROR ("network plugin: setsockopt (ipv4-multicast-if): %s",
1790 sstrerror (errno, errbuf, sizeof (errbuf)));
1797 else if (ai->ai_family == AF_INET6)
1799 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1801 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1803 if (setsockopt (se->data.client.fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
1805 sizeof (se->interface)) != 0)
1808 ERROR ("network plugin: setsockopt (ipv6-multicast-if): %s",
1809 sstrerror (errno, errbuf,
1818 /* else: Not a multicast interface. */
1819 if (se->interface != 0)
1821 #if defined(HAVE_IF_INDEXTONAME) && HAVE_IF_INDEXTONAME && defined(SO_BINDTODEVICE)
1822 char interface_name[IFNAMSIZ];
1824 if (if_indextoname (se->interface, interface_name) == NULL)
1827 DEBUG ("network plugin: Binding socket to interface %s", interface_name);
1829 if (setsockopt (se->data.client.fd, SOL_SOCKET, SO_BINDTODEVICE,
1831 sizeof(interface_name)) == -1 )
1834 ERROR ("network plugin: setsockopt (bind-if): %s",
1835 sstrerror (errno, errbuf, sizeof (errbuf)));
1838 /* #endif HAVE_IF_INDEXTONAME && SO_BINDTODEVICE */
1841 WARNING ("network plugin: Cannot set the interface on a unicast "
1843 # if !defined(SO_BINDTODEVICE)
1844 "the \"SO_BINDTODEVICE\" socket option "
1846 "the \"if_indextoname\" function "
1848 "is not available on your system.");
1854 } /* }}} network_set_interface */
1856 static int network_bind_socket (int fd, const struct addrinfo *ai, const int interface_idx)
1865 /* allow multiple sockets to use the same PORT number */
1866 if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR,
1867 &yes, sizeof(yes)) == -1) {
1869 ERROR ("network plugin: setsockopt (reuseaddr): %s",
1870 sstrerror (errno, errbuf, sizeof (errbuf)));
1874 DEBUG ("fd = %i; calling `bind'", fd);
1876 if (bind (fd, ai->ai_addr, ai->ai_addrlen) == -1)
1880 sstrerror (errno, errbuf, sizeof (errbuf)));
1884 if (ai->ai_family == AF_INET)
1886 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1887 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1889 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1890 struct ip_mreqn mreq;
1892 struct ip_mreq mreq;
1895 DEBUG ("fd = %i; IPv4 multicast address found", fd);
1897 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1898 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1899 /* Set the interface using the interface index if
1900 * possible (available). Unfortunately, the struct
1901 * ip_mreqn is not portable. */
1902 mreq.imr_address.s_addr = ntohl (INADDR_ANY);
1903 mreq.imr_ifindex = interface_idx;
1905 mreq.imr_interface.s_addr = ntohl (INADDR_ANY);
1908 if (setsockopt (fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1909 &loop, sizeof (loop)) == -1)
1912 ERROR ("network plugin: setsockopt (multicast-loop): %s",
1913 sstrerror (errno, errbuf,
1918 if (setsockopt (fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1919 &mreq, sizeof (mreq)) == -1)
1922 ERROR ("network plugin: setsockopt (add-membership): %s",
1923 sstrerror (errno, errbuf,
1931 else if (ai->ai_family == AF_INET6)
1933 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1934 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1935 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1937 struct ipv6_mreq mreq;
1939 DEBUG ("fd = %i; IPv6 multicast address found", fd);
1941 memcpy (&mreq.ipv6mr_multiaddr,
1943 sizeof (addr->sin6_addr));
1945 /* http://developer.apple.com/documentation/Darwin/Reference/ManPages/man4/ip6.4.html
1946 * ipv6mr_interface may be set to zeroes to
1947 * choose the default multicast interface or to
1948 * the index of a particular multicast-capable
1949 * interface if the host is multihomed.
1950 * Membership is associ-associated with a
1951 * single interface; programs running on
1952 * multihomed hosts may need to join the same
1953 * group on more than one interface.*/
1954 mreq.ipv6mr_interface = interface_idx;
1956 if (setsockopt (fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
1957 &loop, sizeof (loop)) == -1)
1960 ERROR ("network plugin: setsockopt (ipv6-multicast-loop): %s",
1961 sstrerror (errno, errbuf,
1966 if (setsockopt (fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
1967 &mreq, sizeof (mreq)) == -1)
1970 ERROR ("network plugin: setsockopt (ipv6-add-membership): %s",
1971 sstrerror (errno, errbuf,
1980 #if defined(HAVE_IF_INDEXTONAME) && HAVE_IF_INDEXTONAME && defined(SO_BINDTODEVICE)
1981 /* if a specific interface was set, bind the socket to it. But to avoid
1982 * possible problems with multicast routing, only do that for non-multicast
1984 if (interface_idx != 0)
1986 char interface_name[IFNAMSIZ];
1988 if (if_indextoname (interface_idx, interface_name) == NULL)
1991 DEBUG ("fd = %i; Binding socket to interface %s", fd, interface_name);
1993 if (setsockopt (fd, SOL_SOCKET, SO_BINDTODEVICE,
1995 sizeof(interface_name)) == -1 )
1998 ERROR ("network plugin: setsockopt (bind-if): %s",
1999 sstrerror (errno, errbuf, sizeof (errbuf)));
2003 #endif /* HAVE_IF_INDEXTONAME && SO_BINDTODEVICE */
2006 } /* int network_bind_socket */
2008 /* Initialize a sockent structure. `type' must be either `SOCKENT_TYPE_CLIENT'
2009 * or `SOCKENT_TYPE_SERVER' */
2010 static sockent_t *sockent_create (int type) /* {{{ */
2014 if ((type != SOCKENT_TYPE_CLIENT) && (type != SOCKENT_TYPE_SERVER))
2017 se = malloc (sizeof (*se));
2020 memset (se, 0, sizeof (*se));
2028 if (type == SOCKENT_TYPE_SERVER)
2030 se->data.server.fd = NULL;
2032 se->data.server.security_level = SECURITY_LEVEL_NONE;
2033 se->data.server.auth_file = NULL;
2034 se->data.server.userdb = NULL;
2035 se->data.server.cypher = NULL;
2040 se->data.client.fd = -1;
2041 se->data.client.addr = NULL;
2042 se->data.client.resolve_interval = 0;
2043 se->data.client.next_resolve_reconnect = 0;
2045 se->data.client.security_level = SECURITY_LEVEL_NONE;
2046 se->data.client.username = NULL;
2047 se->data.client.password = NULL;
2048 se->data.client.cypher = NULL;
2053 } /* }}} sockent_t *sockent_create */
2055 static int sockent_init_crypto (sockent_t *se) /* {{{ */
2057 #if HAVE_LIBGCRYPT /* {{{ */
2058 if (se->type == SOCKENT_TYPE_CLIENT)
2060 if (se->data.client.security_level > SECURITY_LEVEL_NONE)
2062 network_init_gcrypt ();
2064 if ((se->data.client.username == NULL)
2065 || (se->data.client.password == NULL))
2067 ERROR ("network plugin: Client socket with "
2068 "security requested, but no "
2069 "credentials are configured.");
2072 gcry_md_hash_buffer (GCRY_MD_SHA256,
2073 se->data.client.password_hash,
2074 se->data.client.password,
2075 strlen (se->data.client.password));
2078 else /* (se->type == SOCKENT_TYPE_SERVER) */
2080 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
2082 network_init_gcrypt ();
2084 if (se->data.server.auth_file == NULL)
2086 ERROR ("network plugin: Server socket with "
2087 "security requested, but no "
2088 "password file is configured.");
2092 if (se->data.server.auth_file != NULL)
2094 se->data.server.userdb = fbh_create (se->data.server.auth_file);
2095 if (se->data.server.userdb == NULL)
2097 ERROR ("network plugin: Reading password file "
2099 se->data.server.auth_file);
2100 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
2105 #endif /* }}} HAVE_LIBGCRYPT */
2108 } /* }}} int sockent_init_crypto */
2110 static int sockent_client_disconnect (sockent_t *se) /* {{{ */
2112 struct sockent_client *client;
2114 if ((se == NULL) || (se->type != SOCKENT_TYPE_CLIENT))
2117 client = &se->data.client;
2118 if (client->fd >= 0) /* connected */
2124 sfree (client->addr);
2125 client->addrlen = 0;
2128 } /* }}} int sockent_client_disconnect */
2130 static int sockent_client_connect (sockent_t *se) /* {{{ */
2132 static c_complain_t complaint = C_COMPLAIN_INIT_STATIC;
2134 struct sockent_client *client;
2135 struct addrinfo ai_hints;
2136 struct addrinfo *ai_list = NULL, *ai_ptr;
2138 _Bool reconnect = 0;
2141 if ((se == NULL) || (se->type != SOCKENT_TYPE_CLIENT))
2144 client = &se->data.client;
2147 if (client->resolve_interval != 0 && client->next_resolve_reconnect < now) {
2148 DEBUG("network plugin: Reconnecting socket, resolve_interval = %lf, next_resolve_reconnect = %lf",
2149 CDTIME_T_TO_DOUBLE(client->resolve_interval), CDTIME_T_TO_DOUBLE(client->next_resolve_reconnect));
2153 if (client->fd >= 0 && !reconnect) /* already connected and not stale*/
2156 memset (&ai_hints, 0, sizeof (ai_hints));
2157 #ifdef AI_ADDRCONFIG
2158 ai_hints.ai_flags |= AI_ADDRCONFIG;
2160 ai_hints.ai_family = AF_UNSPEC;
2161 ai_hints.ai_socktype = SOCK_DGRAM;
2162 ai_hints.ai_protocol = IPPROTO_UDP;
2164 status = getaddrinfo (se->node,
2165 (se->service != NULL) ? se->service : NET_DEFAULT_PORT,
2166 &ai_hints, &ai_list);
2169 c_complain (LOG_ERR, &complaint,
2170 "network plugin: getaddrinfo (%s, %s) failed: %s",
2171 (se->node == NULL) ? "(null)" : se->node,
2172 (se->service == NULL) ? "(null)" : se->service,
2173 gai_strerror (status));
2178 c_release (LOG_NOTICE, &complaint,
2179 "network plugin: Successfully resolved \"%s\".",
2183 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
2185 if (client->fd >= 0) /* when we reconnect */
2186 sockent_client_disconnect(se);
2188 client->fd = socket (ai_ptr->ai_family,
2189 ai_ptr->ai_socktype,
2190 ai_ptr->ai_protocol);
2194 ERROR ("network plugin: socket(2) failed: %s",
2195 sstrerror (errno, errbuf,
2200 client->addr = malloc (sizeof (*client->addr));
2201 if (client->addr == NULL)
2203 ERROR ("network plugin: malloc failed.");
2209 memset (client->addr, 0, sizeof (*client->addr));
2210 assert (sizeof (*client->addr) >= ai_ptr->ai_addrlen);
2211 memcpy (client->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
2212 client->addrlen = ai_ptr->ai_addrlen;
2214 network_set_ttl (se, ai_ptr);
2215 network_set_interface (se, ai_ptr);
2217 /* We don't open more than one write-socket per
2218 * node/service pair.. */
2222 freeaddrinfo (ai_list);
2226 if (client->resolve_interval > 0)
2227 client->next_resolve_reconnect = now + client->resolve_interval;
2229 } /* }}} int sockent_client_connect */
2231 /* Open the file descriptors for a initialized sockent structure. */
2232 static int sockent_server_listen (sockent_t *se) /* {{{ */
2234 struct addrinfo ai_hints;
2235 struct addrinfo *ai_list, *ai_ptr;
2239 const char *service;
2245 service = se->service;
2247 if (service == NULL)
2248 service = NET_DEFAULT_PORT;
2250 DEBUG ("network plugin: sockent_server_listen: node = %s; service = %s;",
2253 memset (&ai_hints, 0, sizeof (ai_hints));
2254 ai_hints.ai_flags = 0;
2256 ai_hints.ai_flags |= AI_PASSIVE;
2258 #ifdef AI_ADDRCONFIG
2259 ai_hints.ai_flags |= AI_ADDRCONFIG;
2261 ai_hints.ai_family = AF_UNSPEC;
2262 ai_hints.ai_socktype = SOCK_DGRAM;
2263 ai_hints.ai_protocol = IPPROTO_UDP;
2265 status = getaddrinfo (node, service, &ai_hints, &ai_list);
2268 ERROR ("network plugin: getaddrinfo (%s, %s) failed: %s",
2269 (se->node == NULL) ? "(null)" : se->node,
2270 (se->service == NULL) ? "(null)" : se->service,
2271 gai_strerror (status));
2275 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
2279 tmp = realloc (se->data.server.fd,
2280 sizeof (*tmp) * (se->data.server.fd_num + 1));
2283 ERROR ("network plugin: realloc failed.");
2286 se->data.server.fd = tmp;
2287 tmp = se->data.server.fd + se->data.server.fd_num;
2289 *tmp = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
2290 ai_ptr->ai_protocol);
2294 ERROR ("network plugin: socket(2) failed: %s",
2295 sstrerror (errno, errbuf,
2300 status = network_bind_socket (*tmp, ai_ptr, se->interface);
2308 se->data.server.fd_num++;
2310 } /* for (ai_list) */
2312 freeaddrinfo (ai_list);
2314 if (se->data.server.fd_num <= 0)
2317 } /* }}} int sockent_server_listen */
2319 /* Add a sockent to the global list of sockets */
2320 static int sockent_add (sockent_t *se) /* {{{ */
2322 sockent_t *last_ptr;
2327 if (se->type == SOCKENT_TYPE_SERVER)
2332 tmp = realloc (listen_sockets_pollfd,
2333 sizeof (*tmp) * (listen_sockets_num
2334 + se->data.server.fd_num));
2337 ERROR ("network plugin: realloc failed.");
2340 listen_sockets_pollfd = tmp;
2341 tmp = listen_sockets_pollfd + listen_sockets_num;
2343 for (i = 0; i < se->data.server.fd_num; i++)
2345 memset (tmp + i, 0, sizeof (*tmp));
2346 tmp[i].fd = se->data.server.fd[i];
2347 tmp[i].events = POLLIN | POLLPRI;
2351 listen_sockets_num += se->data.server.fd_num;
2353 if (listen_sockets == NULL)
2355 listen_sockets = se;
2358 last_ptr = listen_sockets;
2360 else /* if (se->type == SOCKENT_TYPE_CLIENT) */
2362 if (sending_sockets == NULL)
2364 sending_sockets = se;
2367 last_ptr = sending_sockets;
2370 while (last_ptr->next != NULL)
2371 last_ptr = last_ptr->next;
2372 last_ptr->next = se;
2375 } /* }}} int sockent_add */
2377 static void *dispatch_thread (void __attribute__((unused)) *arg) /* {{{ */
2381 receive_list_entry_t *ent;
2384 /* Lock and wait for more data to come in */
2385 pthread_mutex_lock (&receive_list_lock);
2386 while ((listen_loop == 0)
2387 && (receive_list_head == NULL))
2388 pthread_cond_wait (&receive_list_cond, &receive_list_lock);
2390 /* Remove the head entry and unlock */
2391 ent = receive_list_head;
2393 receive_list_head = ent->next;
2394 receive_list_length--;
2395 pthread_mutex_unlock (&receive_list_lock);
2397 /* Check whether we are supposed to exit. We do NOT check `listen_loop'
2398 * because we dispatch all missing packets before shutting down. */
2402 /* Look for the correct `sockent_t' */
2403 se = listen_sockets;
2408 for (i = 0; i < se->data.server.fd_num; i++)
2409 if (se->data.server.fd[i] == ent->fd)
2412 if (i < se->data.server.fd_num)
2420 ERROR ("network plugin: Got packet from FD %i, but can't "
2421 "find an appropriate socket entry.",
2428 parse_packet (se, ent->data, ent->data_len, /* flags = */ 0,
2429 /* username = */ NULL);
2435 } /* }}} void *dispatch_thread */
2437 static int network_receive (void) /* {{{ */
2439 char buffer[network_config_packet_size];
2445 receive_list_entry_t *private_list_head;
2446 receive_list_entry_t *private_list_tail;
2447 uint64_t private_list_length;
2449 assert (listen_sockets_num > 0);
2451 private_list_head = NULL;
2452 private_list_tail = NULL;
2453 private_list_length = 0;
2455 while (listen_loop == 0)
2457 status = poll (listen_sockets_pollfd, listen_sockets_num, -1);
2464 ERROR ("poll failed: %s",
2465 sstrerror (errno, errbuf, sizeof (errbuf)));
2469 for (i = 0; (i < listen_sockets_num) && (status > 0); i++)
2471 receive_list_entry_t *ent;
2473 if ((listen_sockets_pollfd[i].revents
2474 & (POLLIN | POLLPRI)) == 0)
2478 buffer_len = recv (listen_sockets_pollfd[i].fd,
2479 buffer, sizeof (buffer),
2484 ERROR ("recv failed: %s",
2485 sstrerror (errno, errbuf,
2490 stats_octets_rx += ((uint64_t) buffer_len);
2493 /* TODO: Possible performance enhancement: Do not free
2494 * these entries in the dispatch thread but put them in
2495 * another list, so we don't have to allocate more and
2496 * more of these structures. */
2497 ent = malloc (sizeof (receive_list_entry_t));
2500 ERROR ("network plugin: malloc failed.");
2503 memset (ent, 0, sizeof (receive_list_entry_t));
2504 ent->data = malloc (network_config_packet_size);
2505 if (ent->data == NULL)
2508 ERROR ("network plugin: malloc failed.");
2511 ent->fd = listen_sockets_pollfd[i].fd;
2514 memcpy (ent->data, buffer, buffer_len);
2515 ent->data_len = buffer_len;
2517 if (private_list_head == NULL)
2518 private_list_head = ent;
2520 private_list_tail->next = ent;
2521 private_list_tail = ent;
2522 private_list_length++;
2524 /* Do not block here. Blocking here has led to
2525 * insufficient performance in the past. */
2526 if (pthread_mutex_trylock (&receive_list_lock) == 0)
2528 assert (((receive_list_head == NULL) && (receive_list_length == 0))
2529 || ((receive_list_head != NULL) && (receive_list_length != 0)));
2531 if (receive_list_head == NULL)
2532 receive_list_head = private_list_head;
2534 receive_list_tail->next = private_list_head;
2535 receive_list_tail = private_list_tail;
2536 receive_list_length += private_list_length;
2538 pthread_cond_signal (&receive_list_cond);
2539 pthread_mutex_unlock (&receive_list_lock);
2541 private_list_head = NULL;
2542 private_list_tail = NULL;
2543 private_list_length = 0;
2545 } /* for (listen_sockets_pollfd) */
2546 } /* while (listen_loop == 0) */
2548 /* Make sure everything is dispatched before exiting. */
2549 if (private_list_head != NULL)
2551 pthread_mutex_lock (&receive_list_lock);
2553 if (receive_list_head == NULL)
2554 receive_list_head = private_list_head;
2556 receive_list_tail->next = private_list_head;
2557 receive_list_tail = private_list_tail;
2558 receive_list_length += private_list_length;
2560 private_list_head = NULL;
2561 private_list_tail = NULL;
2562 private_list_length = 0;
2564 pthread_cond_signal (&receive_list_cond);
2565 pthread_mutex_unlock (&receive_list_lock);
2569 } /* }}} int network_receive */
2571 static void *receive_thread (void __attribute__((unused)) *arg)
2573 return (network_receive () ? (void *) 1 : (void *) 0);
2574 } /* void *receive_thread */
2576 static void network_init_buffer (void)
2578 memset (send_buffer, 0, network_config_packet_size);
2579 send_buffer_ptr = send_buffer;
2580 send_buffer_fill = 0;
2581 send_buffer_last_update = 0;
2583 memset (&send_buffer_vl, 0, sizeof (send_buffer_vl));
2584 } /* int network_init_buffer */
2586 static void networt_send_buffer_plain (sockent_t *se, /* {{{ */
2587 const char *buffer, size_t buffer_size)
2593 status = sockent_client_connect (se);
2597 status = sendto (se->data.client.fd, buffer, buffer_size,
2599 (struct sockaddr *) se->data.client.addr,
2600 se->data.client.addrlen);
2605 if ((errno == EINTR) || (errno == EAGAIN))
2608 ERROR ("network plugin: sendto failed: %s. Closing sending socket.",
2609 sstrerror (errno, errbuf, sizeof (errbuf)));
2610 sockent_client_disconnect (se);
2616 } /* }}} void networt_send_buffer_plain */
2619 #define BUFFER_ADD(p,s) do { \
2620 memcpy (buffer + buffer_offset, (p), (s)); \
2621 buffer_offset += (s); \
2624 static void networt_send_buffer_signed (sockent_t *se, /* {{{ */
2625 const char *in_buffer, size_t in_buffer_size)
2627 part_signature_sha256_t ps;
2628 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2629 size_t buffer_offset;
2630 size_t username_len;
2634 unsigned char *hash;
2637 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
2640 ERROR ("network plugin: Creating HMAC object failed: %s",
2641 gcry_strerror (err));
2645 err = gcry_md_setkey (hd, se->data.client.password,
2646 strlen (se->data.client.password));
2649 ERROR ("network plugin: gcry_md_setkey failed: %s",
2650 gcry_strerror (err));
2655 username_len = strlen (se->data.client.username);
2656 if (username_len > (BUFF_SIG_SIZE - PART_SIGNATURE_SHA256_SIZE))
2658 ERROR ("network plugin: Username too long: %s",
2659 se->data.client.username);
2663 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE,
2664 se->data.client.username, username_len);
2665 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE + username_len,
2666 in_buffer, in_buffer_size);
2668 /* Initialize the `ps' structure. */
2669 memset (&ps, 0, sizeof (ps));
2670 ps.head.type = htons (TYPE_SIGN_SHA256);
2671 ps.head.length = htons (PART_SIGNATURE_SHA256_SIZE + username_len);
2673 /* Calculate the hash value. */
2674 gcry_md_write (hd, buffer + PART_SIGNATURE_SHA256_SIZE,
2675 username_len + in_buffer_size);
2676 hash = gcry_md_read (hd, GCRY_MD_SHA256);
2679 ERROR ("network plugin: gcry_md_read failed.");
2683 memcpy (ps.hash, hash, sizeof (ps.hash));
2685 /* Add the header */
2688 BUFFER_ADD (&ps.head.type, sizeof (ps.head.type));
2689 BUFFER_ADD (&ps.head.length, sizeof (ps.head.length));
2690 BUFFER_ADD (ps.hash, sizeof (ps.hash));
2692 assert (buffer_offset == PART_SIGNATURE_SHA256_SIZE);
2697 buffer_offset = PART_SIGNATURE_SHA256_SIZE + username_len + in_buffer_size;
2698 networt_send_buffer_plain (se, buffer, buffer_offset);
2699 } /* }}} void networt_send_buffer_signed */
2701 static void networt_send_buffer_encrypted (sockent_t *se, /* {{{ */
2702 const char *in_buffer, size_t in_buffer_size)
2704 part_encryption_aes256_t pea;
2705 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2707 size_t buffer_offset;
2709 size_t username_len;
2711 gcry_cipher_hd_t cypher;
2713 /* Initialize the header fields */
2714 memset (&pea, 0, sizeof (pea));
2715 pea.head.type = htons (TYPE_ENCR_AES256);
2717 pea.username = se->data.client.username;
2719 username_len = strlen (pea.username);
2720 if ((PART_ENCRYPTION_AES256_SIZE + username_len) > BUFF_SIG_SIZE)
2722 ERROR ("network plugin: Username too long: %s", pea.username);
2726 buffer_size = PART_ENCRYPTION_AES256_SIZE + username_len + in_buffer_size;
2727 header_size = PART_ENCRYPTION_AES256_SIZE + username_len
2728 - sizeof (pea.hash);
2730 assert (buffer_size <= sizeof (buffer));
2731 DEBUG ("network plugin: networt_send_buffer_encrypted: "
2732 "buffer_size = %zu;", buffer_size);
2734 pea.head.length = htons ((uint16_t) (PART_ENCRYPTION_AES256_SIZE
2735 + username_len + in_buffer_size));
2736 pea.username_length = htons ((uint16_t) username_len);
2738 /* Chose a random initialization vector. */
2739 gcry_randomize ((void *) &pea.iv, sizeof (pea.iv), GCRY_STRONG_RANDOM);
2741 /* Create hash of the payload */
2742 gcry_md_hash_buffer (GCRY_MD_SHA1, pea.hash, in_buffer, in_buffer_size);
2744 /* Initialize the buffer */
2746 memset (buffer, 0, sizeof (buffer));
2749 BUFFER_ADD (&pea.head.type, sizeof (pea.head.type));
2750 BUFFER_ADD (&pea.head.length, sizeof (pea.head.length));
2751 BUFFER_ADD (&pea.username_length, sizeof (pea.username_length));
2752 BUFFER_ADD (pea.username, username_len);
2753 BUFFER_ADD (pea.iv, sizeof (pea.iv));
2754 assert (buffer_offset == header_size);
2755 BUFFER_ADD (pea.hash, sizeof (pea.hash));
2756 BUFFER_ADD (in_buffer, in_buffer_size);
2758 assert (buffer_offset == buffer_size);
2760 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
2761 se->data.client.password);
2765 /* Encrypt the buffer in-place */
2766 err = gcry_cipher_encrypt (cypher,
2767 buffer + header_size,
2768 buffer_size - header_size,
2769 /* in = */ NULL, /* in len = */ 0);
2772 ERROR ("network plugin: gcry_cipher_encrypt returned: %s",
2773 gcry_strerror (err));
2777 /* Send it out without further modifications */
2778 networt_send_buffer_plain (se, buffer, buffer_size);
2779 } /* }}} void networt_send_buffer_encrypted */
2781 #endif /* HAVE_LIBGCRYPT */
2783 static void network_send_buffer (char *buffer, size_t buffer_len) /* {{{ */
2787 DEBUG ("network plugin: network_send_buffer: buffer_len = %zu", buffer_len);
2789 for (se = sending_sockets; se != NULL; se = se->next)
2792 if (se->data.client.security_level == SECURITY_LEVEL_ENCRYPT)
2793 networt_send_buffer_encrypted (se, buffer, buffer_len);
2794 else if (se->data.client.security_level == SECURITY_LEVEL_SIGN)
2795 networt_send_buffer_signed (se, buffer, buffer_len);
2796 else /* if (se->data.client.security_level == SECURITY_LEVEL_NONE) */
2797 #endif /* HAVE_LIBGCRYPT */
2798 networt_send_buffer_plain (se, buffer, buffer_len);
2799 } /* for (sending_sockets) */
2800 } /* }}} void network_send_buffer */
2802 static int add_to_buffer (char *buffer, int buffer_size, /* {{{ */
2803 value_list_t *vl_def,
2804 const data_set_t *ds, const value_list_t *vl)
2806 char *buffer_orig = buffer;
2808 if (strcmp (vl_def->host, vl->host) != 0)
2810 if (write_part_string (&buffer, &buffer_size, TYPE_HOST,
2811 vl->host, strlen (vl->host)) != 0)
2813 sstrncpy (vl_def->host, vl->host, sizeof (vl_def->host));
2816 if (vl_def->time != vl->time)
2818 if (write_part_number (&buffer, &buffer_size, TYPE_TIME_HR,
2819 (uint64_t) vl->time))
2821 vl_def->time = vl->time;
2824 if (vl_def->interval != vl->interval)
2826 if (write_part_number (&buffer, &buffer_size, TYPE_INTERVAL_HR,
2827 (uint64_t) vl->interval))
2829 vl_def->interval = vl->interval;
2832 if (strcmp (vl_def->plugin, vl->plugin) != 0)
2834 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN,
2835 vl->plugin, strlen (vl->plugin)) != 0)
2837 sstrncpy (vl_def->plugin, vl->plugin, sizeof (vl_def->plugin));
2840 if (strcmp (vl_def->plugin_instance, vl->plugin_instance) != 0)
2842 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
2843 vl->plugin_instance,
2844 strlen (vl->plugin_instance)) != 0)
2846 sstrncpy (vl_def->plugin_instance, vl->plugin_instance, sizeof (vl_def->plugin_instance));
2849 if (strcmp (vl_def->type, vl->type) != 0)
2851 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
2852 vl->type, strlen (vl->type)) != 0)
2854 sstrncpy (vl_def->type, ds->type, sizeof (vl_def->type));
2857 if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
2859 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE_INSTANCE,
2861 strlen (vl->type_instance)) != 0)
2863 sstrncpy (vl_def->type_instance, vl->type_instance, sizeof (vl_def->type_instance));
2866 if (write_part_values (&buffer, &buffer_size, ds, vl) != 0)
2869 return (buffer - buffer_orig);
2870 } /* }}} int add_to_buffer */
2872 static void flush_buffer (void)
2874 DEBUG ("network plugin: flush_buffer: send_buffer_fill = %i",
2877 network_send_buffer (send_buffer, (size_t) send_buffer_fill);
2879 stats_octets_tx += ((uint64_t) send_buffer_fill);
2882 network_init_buffer ();
2885 static int network_write (const data_set_t *ds, const value_list_t *vl,
2886 user_data_t __attribute__((unused)) *user_data)
2890 if (!check_send_okay (vl))
2893 char name[6*DATA_MAX_NAME_LEN];
2894 FORMAT_VL (name, sizeof (name), vl);
2895 name[sizeof (name) - 1] = 0;
2896 DEBUG ("network plugin: network_write: "
2897 "NOT sending %s.", name);
2899 /* Counter is not protected by another lock and may be reached by
2900 * multiple threads */
2901 pthread_mutex_lock (&stats_lock);
2902 stats_values_not_sent++;
2903 pthread_mutex_unlock (&stats_lock);
2907 uc_meta_data_add_unsigned_int (vl,
2908 "network:time_sent", (uint64_t) vl->time);
2910 pthread_mutex_lock (&send_buffer_lock);
2912 status = add_to_buffer (send_buffer_ptr,
2913 network_config_packet_size - (send_buffer_fill + BUFF_SIG_SIZE),
2918 /* status == bytes added to the buffer */
2919 send_buffer_fill += status;
2920 send_buffer_ptr += status;
2921 send_buffer_last_update = cdtime();
2923 stats_values_sent++;
2929 status = add_to_buffer (send_buffer_ptr,
2930 network_config_packet_size - (send_buffer_fill + BUFF_SIG_SIZE),
2936 send_buffer_fill += status;
2937 send_buffer_ptr += status;
2939 stats_values_sent++;
2945 ERROR ("network plugin: Unable to append to the "
2946 "buffer for some weird reason");
2948 else if ((network_config_packet_size - send_buffer_fill) < 15)
2953 pthread_mutex_unlock (&send_buffer_lock);
2955 return ((status < 0) ? -1 : 0);
2956 } /* int network_write */
2958 static int network_config_set_ttl (const oconfig_item_t *ci) /* {{{ */
2962 if (cf_util_get_int (ci, &tmp) != 0)
2964 else if ((tmp > 0) && (tmp <= 255))
2965 network_config_ttl = tmp;
2967 WARNING ("network plugin: The `TimeToLive' must be between 1 and 255.");
2972 } /* }}} int network_config_set_ttl */
2974 static int network_config_set_interface (const oconfig_item_t *ci, /* {{{ */
2979 if (cf_util_get_string_buffer (ci, if_name, sizeof (if_name)) != 0)
2982 *interface = if_nametoindex (if_name);
2984 } /* }}} int network_config_set_interface */
2986 static int network_config_set_buffer_size (const oconfig_item_t *ci) /* {{{ */
2990 if (cf_util_get_int (ci, &tmp) != 0)
2992 else if ((tmp >= 1024) && (tmp <= 65535))
2993 network_config_packet_size = tmp;
2995 WARNING ("network plugin: The `MaxPacketSize' must be between 1024 and 65535.");
3000 } /* }}} int network_config_set_buffer_size */
3003 static int network_config_set_security_level (oconfig_item_t *ci, /* {{{ */
3007 if ((ci->values_num != 1)
3008 || (ci->values[0].type != OCONFIG_TYPE_STRING))
3010 WARNING ("network plugin: The `SecurityLevel' config option needs exactly "
3011 "one string argument.");
3015 str = ci->values[0].value.string;
3016 if (strcasecmp ("Encrypt", str) == 0)
3017 *retval = SECURITY_LEVEL_ENCRYPT;
3018 else if (strcasecmp ("Sign", str) == 0)
3019 *retval = SECURITY_LEVEL_SIGN;
3020 else if (strcasecmp ("None", str) == 0)
3021 *retval = SECURITY_LEVEL_NONE;
3024 WARNING ("network plugin: Unknown security level: %s.", str);
3029 } /* }}} int network_config_set_security_level */
3030 #endif /* HAVE_LIBGCRYPT */
3032 static int network_config_add_listen (const oconfig_item_t *ci) /* {{{ */
3038 if ((ci->values_num < 1) || (ci->values_num > 2)
3039 || (ci->values[0].type != OCONFIG_TYPE_STRING)
3040 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
3042 ERROR ("network plugin: The `%s' config option needs "
3043 "one or two string arguments.", ci->key);
3047 se = sockent_create (SOCKENT_TYPE_SERVER);
3050 ERROR ("network plugin: sockent_create failed.");
3054 se->node = strdup (ci->values[0].value.string);
3055 if (ci->values_num >= 2)
3056 se->service = strdup (ci->values[1].value.string);
3058 for (i = 0; i < ci->children_num; i++)
3060 oconfig_item_t *child = ci->children + i;
3063 if (strcasecmp ("AuthFile", child->key) == 0)
3064 cf_util_get_string (child, &se->data.server.auth_file);
3065 else if (strcasecmp ("SecurityLevel", child->key) == 0)
3066 network_config_set_security_level (child,
3067 &se->data.server.security_level);
3069 #endif /* HAVE_LIBGCRYPT */
3070 if (strcasecmp ("Interface", child->key) == 0)
3071 network_config_set_interface (child, &se->interface);
3074 WARNING ("network plugin: Option `%s' is not allowed here.",
3080 if ((se->data.server.security_level > SECURITY_LEVEL_NONE)
3081 && (se->data.server.auth_file == NULL))
3083 ERROR ("network plugin: A security level higher than `none' was "
3084 "requested, but no AuthFile option was given. Cowardly refusing to "
3085 "open this socket!");
3086 sockent_destroy (se);
3089 #endif /* HAVE_LIBGCRYPT */
3091 status = sockent_init_crypto (se);
3094 ERROR ("network plugin: network_config_add_listen: sockent_init_crypto() failed.");
3095 sockent_destroy (se);
3099 status = sockent_server_listen (se);
3102 ERROR ("network plugin: network_config_add_server: sockent_server_listen failed.");
3103 sockent_destroy (se);
3107 status = sockent_add (se);
3110 ERROR ("network plugin: network_config_add_listen: sockent_add failed.");
3111 sockent_destroy (se);
3116 } /* }}} int network_config_add_listen */
3118 static int network_config_add_server (const oconfig_item_t *ci) /* {{{ */
3124 if ((ci->values_num < 1) || (ci->values_num > 2)
3125 || (ci->values[0].type != OCONFIG_TYPE_STRING)
3126 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
3128 ERROR ("network plugin: The `%s' config option needs "
3129 "one or two string arguments.", ci->key);
3133 se = sockent_create (SOCKENT_TYPE_CLIENT);
3136 ERROR ("network plugin: sockent_create failed.");
3140 se->node = strdup (ci->values[0].value.string);
3141 if (ci->values_num >= 2)
3142 se->service = strdup (ci->values[1].value.string);
3144 for (i = 0; i < ci->children_num; i++)
3146 oconfig_item_t *child = ci->children + i;
3149 if (strcasecmp ("Username", child->key) == 0)
3150 cf_util_get_string (child, &se->data.client.username);
3151 else if (strcasecmp ("Password", child->key) == 0)
3152 cf_util_get_string (child, &se->data.client.password);
3153 else if (strcasecmp ("SecurityLevel", child->key) == 0)
3154 network_config_set_security_level (child,
3155 &se->data.client.security_level);
3157 #endif /* HAVE_LIBGCRYPT */
3158 if (strcasecmp ("Interface", child->key) == 0)
3159 network_config_set_interface (child, &se->interface);
3160 else if (strcasecmp ("ResolveInterval", child->key) == 0)
3161 cf_util_get_cdtime(child, &se->data.client.resolve_interval);
3164 WARNING ("network plugin: Option `%s' is not allowed here.",
3170 if ((se->data.client.security_level > SECURITY_LEVEL_NONE)
3171 && ((se->data.client.username == NULL)
3172 || (se->data.client.password == NULL)))
3174 ERROR ("network plugin: A security level higher than `none' was "
3175 "requested, but no Username or Password option was given. "
3176 "Cowardly refusing to open this socket!");
3177 sockent_destroy (se);
3180 #endif /* HAVE_LIBGCRYPT */
3182 status = sockent_init_crypto (se);
3185 ERROR ("network plugin: network_config_add_server: sockent_init_crypto() failed.");
3186 sockent_destroy (se);
3190 /* No call to sockent_client_connect() here -- it is called from
3191 * networt_send_buffer_plain(). */
3193 status = sockent_add (se);
3196 ERROR ("network plugin: network_config_add_server: sockent_add failed.");
3197 sockent_destroy (se);
3202 } /* }}} int network_config_add_server */
3204 static int network_config (oconfig_item_t *ci) /* {{{ */
3208 /* The options need to be applied first */
3209 for (i = 0; i < ci->children_num; i++)
3211 oconfig_item_t *child = ci->children + i;
3212 if (strcasecmp ("TimeToLive", child->key) == 0)
3213 network_config_set_ttl (child);
3216 for (i = 0; i < ci->children_num; i++)
3218 oconfig_item_t *child = ci->children + i;
3220 if (strcasecmp ("Listen", child->key) == 0)
3221 network_config_add_listen (child);
3222 else if (strcasecmp ("Server", child->key) == 0)
3223 network_config_add_server (child);
3224 else if (strcasecmp ("TimeToLive", child->key) == 0) {
3225 /* Handled earlier */
3227 else if (strcasecmp ("MaxPacketSize", child->key) == 0)
3228 network_config_set_buffer_size (child);
3229 else if (strcasecmp ("Forward", child->key) == 0)
3230 cf_util_get_boolean (child, &network_config_forward);
3231 else if (strcasecmp ("ReportStats", child->key) == 0)
3232 cf_util_get_boolean (child, &network_config_stats);
3235 WARNING ("network plugin: Option `%s' is not allowed here.",
3241 } /* }}} int network_config */
3243 static int network_notification (const notification_t *n,
3244 user_data_t __attribute__((unused)) *user_data)
3246 char buffer[network_config_packet_size];
3247 char *buffer_ptr = buffer;
3248 int buffer_free = sizeof (buffer);
3251 if (!check_send_notify_okay (n))
3254 memset (buffer, 0, sizeof (buffer));
3256 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_TIME_HR,
3257 (uint64_t) n->time);
3261 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_SEVERITY,
3262 (uint64_t) n->severity);
3266 if (strlen (n->host) > 0)
3268 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_HOST,
3269 n->host, strlen (n->host));
3274 if (strlen (n->plugin) > 0)
3276 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_PLUGIN,
3277 n->plugin, strlen (n->plugin));
3282 if (strlen (n->plugin_instance) > 0)
3284 status = write_part_string (&buffer_ptr, &buffer_free,
3285 TYPE_PLUGIN_INSTANCE,
3286 n->plugin_instance, strlen (n->plugin_instance));
3291 if (strlen (n->type) > 0)
3293 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE,
3294 n->type, strlen (n->type));
3299 if (strlen (n->type_instance) > 0)
3301 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE_INSTANCE,
3302 n->type_instance, strlen (n->type_instance));
3307 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_MESSAGE,
3308 n->message, strlen (n->message));
3312 network_send_buffer (buffer, sizeof (buffer) - buffer_free);
3315 } /* int network_notification */
3317 static int network_shutdown (void)
3323 /* Kill the listening thread */
3324 if (receive_thread_running != 0)
3326 INFO ("network plugin: Stopping receive thread.");
3327 pthread_kill (receive_thread_id, SIGTERM);
3328 pthread_join (receive_thread_id, NULL /* no return value */);
3329 memset (&receive_thread_id, 0, sizeof (receive_thread_id));
3330 receive_thread_running = 0;
3333 /* Shutdown the dispatching thread */
3334 if (dispatch_thread_running != 0)
3336 INFO ("network plugin: Stopping dispatch thread.");
3337 pthread_mutex_lock (&receive_list_lock);
3338 pthread_cond_broadcast (&receive_list_cond);
3339 pthread_mutex_unlock (&receive_list_lock);
3340 pthread_join (dispatch_thread_id, /* ret = */ NULL);
3341 dispatch_thread_running = 0;
3344 sockent_destroy (listen_sockets);
3346 if (send_buffer_fill > 0)
3349 sfree (send_buffer);
3351 for (se = sending_sockets; se != NULL; se = se->next)
3352 sockent_client_disconnect (se);
3353 sockent_destroy (sending_sockets);
3355 plugin_unregister_config ("network");
3356 plugin_unregister_init ("network");
3357 plugin_unregister_write ("network");
3358 plugin_unregister_shutdown ("network");
3361 } /* int network_shutdown */
3363 static int network_stats_read (void) /* {{{ */
3365 derive_t copy_octets_rx;
3366 derive_t copy_octets_tx;
3367 derive_t copy_packets_rx;
3368 derive_t copy_packets_tx;
3369 derive_t copy_values_dispatched;
3370 derive_t copy_values_not_dispatched;
3371 derive_t copy_values_sent;
3372 derive_t copy_values_not_sent;
3373 derive_t copy_receive_list_length;
3374 value_list_t vl = VALUE_LIST_INIT;
3377 copy_octets_rx = stats_octets_rx;
3378 copy_octets_tx = stats_octets_tx;
3379 copy_packets_rx = stats_packets_rx;
3380 copy_packets_tx = stats_packets_tx;
3381 copy_values_dispatched = stats_values_dispatched;
3382 copy_values_not_dispatched = stats_values_not_dispatched;
3383 copy_values_sent = stats_values_sent;
3384 copy_values_not_sent = stats_values_not_sent;
3385 copy_receive_list_length = receive_list_length;
3387 /* Initialize `vl' */
3391 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
3392 sstrncpy (vl.plugin, "network", sizeof (vl.plugin));
3394 /* Octets received / sent */
3395 vl.values[0].derive = (derive_t) copy_octets_rx;
3396 vl.values[1].derive = (derive_t) copy_octets_tx;
3397 sstrncpy (vl.type, "if_octets", sizeof (vl.type));
3398 plugin_dispatch_values (&vl);
3400 /* Packets received / send */
3401 vl.values[0].derive = (derive_t) copy_packets_rx;
3402 vl.values[1].derive = (derive_t) copy_packets_tx;
3403 sstrncpy (vl.type, "if_packets", sizeof (vl.type));
3404 plugin_dispatch_values (&vl);
3406 /* Values (not) dispatched and (not) send */
3407 sstrncpy (vl.type, "total_values", sizeof (vl.type));
3410 vl.values[0].derive = (derive_t) copy_values_dispatched;
3411 sstrncpy (vl.type_instance, "dispatch-accepted",
3412 sizeof (vl.type_instance));
3413 plugin_dispatch_values (&vl);
3415 vl.values[0].derive = (derive_t) copy_values_not_dispatched;
3416 sstrncpy (vl.type_instance, "dispatch-rejected",
3417 sizeof (vl.type_instance));
3418 plugin_dispatch_values (&vl);
3420 vl.values[0].derive = (derive_t) copy_values_sent;
3421 sstrncpy (vl.type_instance, "send-accepted",
3422 sizeof (vl.type_instance));
3423 plugin_dispatch_values (&vl);
3425 vl.values[0].derive = (derive_t) copy_values_not_sent;
3426 sstrncpy (vl.type_instance, "send-rejected",
3427 sizeof (vl.type_instance));
3428 plugin_dispatch_values (&vl);
3430 /* Receive queue length */
3431 vl.values[0].gauge = (gauge_t) copy_receive_list_length;
3432 sstrncpy (vl.type, "queue_length", sizeof (vl.type));
3433 vl.type_instance[0] = 0;
3434 plugin_dispatch_values (&vl);
3437 } /* }}} int network_stats_read */
3439 static int network_init (void)
3441 static _Bool have_init = 0;
3443 /* Check if we were already initialized. If so, just return - there's
3444 * nothing more to do (for now, that is). */
3450 network_init_gcrypt ();
3453 if (network_config_stats)
3454 plugin_register_read ("network", network_stats_read);
3456 plugin_register_shutdown ("network", network_shutdown);
3458 send_buffer = malloc (network_config_packet_size);
3459 if (send_buffer == NULL)
3461 ERROR ("network plugin: malloc failed.");
3464 network_init_buffer ();
3466 /* setup socket(s) and so on */
3467 if (sending_sockets != NULL)
3469 plugin_register_write ("network", network_write,
3470 /* user_data = */ NULL);
3471 plugin_register_notification ("network", network_notification,
3472 /* user_data = */ NULL);
3475 /* If no threads need to be started, return here. */
3476 if ((listen_sockets_num == 0)
3477 || ((dispatch_thread_running != 0)
3478 && (receive_thread_running != 0)))
3481 if (dispatch_thread_running == 0)
3484 status = plugin_thread_create (&dispatch_thread_id,
3485 NULL /* no attributes */,
3487 NULL /* no argument */);
3491 ERROR ("network: pthread_create failed: %s",
3492 sstrerror (errno, errbuf,
3497 dispatch_thread_running = 1;
3501 if (receive_thread_running == 0)
3504 status = plugin_thread_create (&receive_thread_id,
3505 NULL /* no attributes */,
3507 NULL /* no argument */);
3511 ERROR ("network: pthread_create failed: %s",
3512 sstrerror (errno, errbuf,
3517 receive_thread_running = 1;
3522 } /* int network_init */
3525 * The flush option of the network plugin cannot flush individual identifiers.
3526 * All the values are added to a buffer and sent when the buffer is full, the
3527 * requested value may or may not be in there, it's not worth finding out. We
3528 * just send the buffer if `flush' is called - if the requested value was in
3529 * there, good. If not, well, then there is nothing to flush.. -octo
3531 static int network_flush (cdtime_t timeout,
3532 __attribute__((unused)) const char *identifier,
3533 __attribute__((unused)) user_data_t *user_data)
3535 pthread_mutex_lock (&send_buffer_lock);
3537 if (send_buffer_fill > 0)
3541 cdtime_t now = cdtime ();
3542 if ((send_buffer_last_update + timeout) > now)
3544 pthread_mutex_unlock (&send_buffer_lock);
3550 pthread_mutex_unlock (&send_buffer_lock);
3553 } /* int network_flush */
3555 void module_register (void)
3557 plugin_register_complex_config ("network", network_config);
3558 plugin_register_init ("network", network_init);
3559 plugin_register_flush ("network", network_flush,
3560 /* user_data = */ NULL);
3561 } /* void module_register */
3563 /* vim: set fdm=marker : */