2 * collectd - src/network.c
3 * Copyright (C) 2005-2013 Florian octo Forster
4 * Copyright (C) 2009 Aman Gupta
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; only version 2.1 of the License is
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 * Florian octo Forster <octo at collectd.org>
22 * Aman Gupta <aman at tmm1.net>
25 #define _BSD_SOURCE /* For struct ip_mreq */
30 #include "configfile.h"
31 #include "utils_fbhash.h"
32 #include "utils_avltree.h"
33 #include "utils_cache.h"
34 #include "utils_complain.h"
42 # include <sys/socket.h>
48 # include <netinet/in.h>
51 # include <arpa/inet.h>
62 # if defined __APPLE__
63 /* default xcode compiler throws warnings even when deprecated functionality
64 * is not used. -Werror breaks the build because of erroneous warnings.
65 * http://stackoverflow.com/questions/10556299/compiler-warnings-with-libgcrypt-v1-5-0/12830209#12830209
67 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
69 /* FreeBSD's copy of libgcrypt extends the existing GCRYPT_NO_DEPRECATED
70 * to properly hide all deprecated functionality.
71 * http://svnweb.freebsd.org/ports/head/security/libgcrypt/files/patch-src__gcrypt.h.in
73 # define GCRYPT_NO_DEPRECATED
75 # if defined __APPLE__
76 /* Re enable deprecation warnings */
77 # pragma GCC diagnostic warning "-Wdeprecated-declarations"
79 GCRY_THREAD_OPTION_PTHREAD_IMPL;
82 #ifndef IPV6_ADD_MEMBERSHIP
83 # ifdef IPV6_JOIN_GROUP
84 # define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
86 # error "Neither IP_ADD_MEMBERSHIP nor IPV6_JOIN_GROUP is defined"
88 #endif /* !IP_ADD_MEMBERSHIP */
91 * Maximum size required for encryption / signing:
93 * 42 bytes for the encryption header
94 * + 64 bytes for the username
98 #define BUFF_SIG_SIZE 106
103 #define SECURITY_LEVEL_NONE 0
105 # define SECURITY_LEVEL_SIGN 1
106 # define SECURITY_LEVEL_ENCRYPT 2
108 struct sockent_client
111 struct sockaddr_storage *addr;
117 gcry_cipher_hd_t cypher;
118 unsigned char password_hash[32];
122 struct sockent_server
130 gcry_cipher_hd_t cypher;
134 typedef struct sockent
136 #define SOCKENT_TYPE_CLIENT 1
137 #define SOCKENT_TYPE_SERVER 2
146 struct sockent_client client;
147 struct sockent_server server;
150 struct sockent *next;
153 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
154 * 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
155 * +-------+-----------------------+-------------------------------+
156 * ! Ver. ! ! Length !
157 * +-------+-----------------------+-------------------------------+
164 typedef struct part_header_s part_header_t;
166 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
167 * 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
168 * +-------------------------------+-------------------------------+
170 * +-------------------------------+-------------------------------+
171 * : (Length - 4) Bytes :
172 * +---------------------------------------------------------------+
179 typedef struct part_string_s part_string_t;
181 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
182 * 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
183 * +-------------------------------+-------------------------------+
185 * +-------------------------------+-------------------------------+
186 * : (Length - 4 == 2 || 4 || 8) Bytes :
187 * +---------------------------------------------------------------+
194 typedef struct part_number_s part_number_t;
196 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
197 * 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
198 * +-------------------------------+-------------------------------+
200 * +-------------------------------+---------------+---------------+
201 * ! Num of values ! Type0 ! Type1 !
202 * +-------------------------------+---------------+---------------+
205 * +---------------------------------------------------------------+
208 * +---------------------------------------------------------------+
213 uint16_t *num_values;
214 uint8_t *values_types;
217 typedef struct part_values_s part_values_t;
219 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
220 * 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
221 * +-------------------------------+-------------------------------+
223 * +-------------------------------+-------------------------------+
224 * ! Hash (Bits 0 - 31) !
226 * ! Hash (Bits 224 - 255) !
227 * +---------------------------------------------------------------+
230 #define PART_SIGNATURE_SHA256_SIZE 36
231 struct part_signature_sha256_s
234 unsigned char hash[32];
237 typedef struct part_signature_sha256_s part_signature_sha256_t;
239 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
240 * 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
241 * +-------------------------------+-------------------------------+
243 * +-------------------------------+-------------------------------+
244 * ! Original length ! Padding (0 - 15 bytes) !
245 * +-------------------------------+-------------------------------+
246 * ! Hash (Bits 0 - 31) !
248 * ! Hash (Bits 128 - 159) !
249 * +---------------------------------------------------------------+
252 #define PART_ENCRYPTION_AES256_SIZE 42
253 struct part_encryption_aes256_s
256 uint16_t username_length;
258 unsigned char iv[16];
260 unsigned char hash[20];
264 typedef struct part_encryption_aes256_s part_encryption_aes256_t;
266 struct receive_list_entry_s
271 struct receive_list_entry_s *next;
273 typedef struct receive_list_entry_s receive_list_entry_t;
278 static int network_config_ttl = 0;
279 /* Ethernet - (IPv6 + UDP) = 1500 - (40 + 8) = 1452 */
280 static size_t network_config_packet_size = 1452;
281 static int network_config_forward = 0;
282 static int network_config_stats = 0;
284 static sockent_t *sending_sockets = NULL;
286 static receive_list_entry_t *receive_list_head = NULL;
287 static receive_list_entry_t *receive_list_tail = NULL;
288 static pthread_mutex_t receive_list_lock = PTHREAD_MUTEX_INITIALIZER;
289 static pthread_cond_t receive_list_cond = PTHREAD_COND_INITIALIZER;
290 static uint64_t receive_list_length = 0;
292 static sockent_t *listen_sockets = NULL;
293 static struct pollfd *listen_sockets_pollfd = NULL;
294 static size_t listen_sockets_num = 0;
296 /* The receive and dispatch threads will run as long as `listen_loop' is set to
298 static int listen_loop = 0;
299 static int receive_thread_running = 0;
300 static pthread_t receive_thread_id;
301 static int dispatch_thread_running = 0;
302 static pthread_t dispatch_thread_id;
304 /* Buffer in which to-be-sent network packets are constructed. */
305 static char *send_buffer;
306 static char *send_buffer_ptr;
307 static int send_buffer_fill;
308 static value_list_t send_buffer_vl = VALUE_LIST_STATIC;
309 static pthread_mutex_t send_buffer_lock = PTHREAD_MUTEX_INITIALIZER;
311 /* XXX: These counters are incremented from one place only. The spot in which
312 * the values are incremented is either only reachable by one thread (the
313 * dispatch thread, for example) or locked by some lock (send_buffer_lock for
314 * example). Only if neither is true, the stats_lock is acquired. The counters
315 * are always read without holding a lock in the hope that writing 8 bytes to
316 * memory is an atomic operation. */
317 static derive_t stats_octets_rx = 0;
318 static derive_t stats_octets_tx = 0;
319 static derive_t stats_packets_rx = 0;
320 static derive_t stats_packets_tx = 0;
321 static derive_t stats_values_dispatched = 0;
322 static derive_t stats_values_not_dispatched = 0;
323 static derive_t stats_values_sent = 0;
324 static derive_t stats_values_not_sent = 0;
325 static pthread_mutex_t stats_lock = PTHREAD_MUTEX_INITIALIZER;
330 static _Bool check_receive_okay (const value_list_t *vl) /* {{{ */
332 uint64_t time_sent = 0;
335 status = uc_meta_data_get_unsigned_int (vl,
336 "network:time_sent", &time_sent);
338 /* This is a value we already sent. Don't allow it to be received again in
339 * order to avoid looping. */
340 if ((status == 0) && (time_sent >= ((uint64_t) vl->time)))
344 } /* }}} _Bool check_receive_okay */
346 static _Bool check_send_okay (const value_list_t *vl) /* {{{ */
351 if (network_config_forward != 0)
354 if (vl->meta == NULL)
357 status = meta_data_get_boolean (vl->meta, "network:received", &received);
358 if (status == -ENOENT)
360 else if (status != 0)
362 ERROR ("network plugin: check_send_okay: meta_data_get_boolean failed "
363 "with status %i.", status);
367 /* By default, only *send* value lists that were not *received* by the
370 } /* }}} _Bool check_send_okay */
372 static _Bool check_notify_received (const notification_t *n) /* {{{ */
374 notification_meta_t *ptr;
376 for (ptr = n->meta; ptr != NULL; ptr = ptr->next)
377 if ((strcmp ("network:received", ptr->name) == 0)
378 && (ptr->type == NM_TYPE_BOOLEAN))
379 return ((_Bool) ptr->nm_value.nm_boolean);
382 } /* }}} _Bool check_notify_received */
384 static _Bool check_send_notify_okay (const notification_t *n) /* {{{ */
386 static c_complain_t complain_forwarding = C_COMPLAIN_INIT_STATIC;
392 received = check_notify_received (n);
394 if (network_config_forward && received)
396 c_complain_once (LOG_ERR, &complain_forwarding,
397 "network plugin: A notification has been received via the network "
398 "and forwarding is enabled. Forwarding of notifications is currently "
399 "not supported, because there is not loop-deteciton available. "
400 "Please contact the collectd mailing list if you need this "
404 /* By default, only *send* value lists that were not *received* by the
407 } /* }}} _Bool check_send_notify_okay */
409 static int network_dispatch_values (value_list_t *vl, /* {{{ */
410 const char *username)
415 || (strlen (vl->host) <= 0)
416 || (strlen (vl->plugin) <= 0)
417 || (strlen (vl->type) <= 0))
420 if (!check_receive_okay (vl))
423 char name[6*DATA_MAX_NAME_LEN];
424 FORMAT_VL (name, sizeof (name), vl);
425 name[sizeof (name) - 1] = 0;
426 DEBUG ("network plugin: network_dispatch_values: "
427 "NOT dispatching %s.", name);
429 stats_values_not_dispatched++;
433 assert (vl->meta == NULL);
435 vl->meta = meta_data_create ();
436 if (vl->meta == NULL)
438 ERROR ("network plugin: meta_data_create failed.");
442 status = meta_data_add_boolean (vl->meta, "network:received", 1);
445 ERROR ("network plugin: meta_data_add_boolean failed.");
446 meta_data_destroy (vl->meta);
451 if (username != NULL)
453 status = meta_data_add_string (vl->meta, "network:username", username);
456 ERROR ("network plugin: meta_data_add_string failed.");
457 meta_data_destroy (vl->meta);
463 plugin_dispatch_values (vl);
464 stats_values_dispatched++;
466 meta_data_destroy (vl->meta);
470 } /* }}} int network_dispatch_values */
472 static int network_dispatch_notification (notification_t *n) /* {{{ */
476 assert (n->meta == NULL);
478 status = plugin_notification_meta_add_boolean (n, "network:received", 1);
481 ERROR ("network plugin: plugin_notification_meta_add_boolean failed.");
482 plugin_notification_meta_free (n->meta);
487 status = plugin_dispatch_notification (n);
489 plugin_notification_meta_free (n->meta);
493 } /* }}} int network_dispatch_notification */
496 static void network_init_gcrypt (void) /* {{{ */
498 /* http://lists.gnupg.org/pipermail/gcrypt-devel/2003-August/000458.html
499 * Because you can't know in a library whether another library has
500 * already initialized the library */
501 if (gcry_control (GCRYCTL_ANY_INITIALIZATION_P))
504 /* http://www.gnupg.org/documentation/manuals/gcrypt/Multi_002dThreading.html
505 * To ensure thread-safety, it's important to set GCRYCTL_SET_THREAD_CBS
506 * *before* initalizing Libgcrypt with gcry_check_version(), which itself must
507 * be called before any other gcry_* function. GCRYCTL_ANY_INITIALIZATION_P
508 * above doesn't count, as it doesn't implicitly initalize Libgcrypt.
510 * tl;dr: keep all these gry_* statements in this exact order please. */
511 gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
512 gcry_check_version (NULL);
513 gcry_control (GCRYCTL_INIT_SECMEM, 32768);
514 gcry_control (GCRYCTL_INITIALIZATION_FINISHED);
515 } /* }}} void network_init_gcrypt */
517 static gcry_cipher_hd_t network_get_aes256_cypher (sockent_t *se, /* {{{ */
518 const void *iv, size_t iv_size, const char *username)
521 gcry_cipher_hd_t *cyper_ptr;
522 unsigned char password_hash[32];
524 if (se->type == SOCKENT_TYPE_CLIENT)
526 cyper_ptr = &se->data.client.cypher;
527 memcpy (password_hash, se->data.client.password_hash,
528 sizeof (password_hash));
534 cyper_ptr = &se->data.server.cypher;
536 if (username == NULL)
539 secret = fbh_get (se->data.server.userdb, username);
543 gcry_md_hash_buffer (GCRY_MD_SHA256,
545 secret, strlen (secret));
550 if (*cyper_ptr == NULL)
552 err = gcry_cipher_open (cyper_ptr,
553 GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_OFB, /* flags = */ 0);
556 ERROR ("network plugin: gcry_cipher_open returned: %s",
557 gcry_strerror (err));
564 gcry_cipher_reset (*cyper_ptr);
566 assert (*cyper_ptr != NULL);
568 err = gcry_cipher_setkey (*cyper_ptr,
569 password_hash, sizeof (password_hash));
572 ERROR ("network plugin: gcry_cipher_setkey returned: %s",
573 gcry_strerror (err));
574 gcry_cipher_close (*cyper_ptr);
579 err = gcry_cipher_setiv (*cyper_ptr, iv, iv_size);
582 ERROR ("network plugin: gcry_cipher_setkey returned: %s",
583 gcry_strerror (err));
584 gcry_cipher_close (*cyper_ptr);
590 } /* }}} int network_get_aes256_cypher */
591 #endif /* HAVE_LIBGCRYPT */
593 static int write_part_values (char **ret_buffer, int *ret_buffer_len,
594 const data_set_t *ds, const value_list_t *vl)
600 part_header_t pkg_ph;
601 uint16_t pkg_num_values;
602 uint8_t *pkg_values_types;
608 num_values = vl->values_len;
609 packet_len = sizeof (part_header_t) + sizeof (uint16_t)
610 + (num_values * sizeof (uint8_t))
611 + (num_values * sizeof (value_t));
613 if (*ret_buffer_len < packet_len)
616 pkg_values_types = (uint8_t *) malloc (num_values * sizeof (uint8_t));
617 if (pkg_values_types == NULL)
619 ERROR ("network plugin: write_part_values: malloc failed.");
623 pkg_values = (value_t *) malloc (num_values * sizeof (value_t));
624 if (pkg_values == NULL)
626 free (pkg_values_types);
627 ERROR ("network plugin: write_part_values: malloc failed.");
631 pkg_ph.type = htons (TYPE_VALUES);
632 pkg_ph.length = htons (packet_len);
634 pkg_num_values = htons ((uint16_t) vl->values_len);
636 for (i = 0; i < num_values; i++)
638 pkg_values_types[i] = (uint8_t) ds->ds[i].type;
639 switch (ds->ds[i].type)
641 case DS_TYPE_COUNTER:
642 pkg_values[i].counter = htonll (vl->values[i].counter);
646 pkg_values[i].gauge = htond (vl->values[i].gauge);
650 pkg_values[i].derive = htonll (vl->values[i].derive);
653 case DS_TYPE_ABSOLUTE:
654 pkg_values[i].absolute = htonll (vl->values[i].absolute);
658 free (pkg_values_types);
660 ERROR ("network plugin: write_part_values: "
661 "Unknown data source type: %i",
664 } /* switch (ds->ds[i].type) */
665 } /* for (num_values) */
668 * Use `memcpy' to write everything to the buffer, because the pointer
669 * may be unaligned and some architectures, such as SPARC, can't handle
672 packet_ptr = *ret_buffer;
674 memcpy (packet_ptr + offset, &pkg_ph, sizeof (pkg_ph));
675 offset += sizeof (pkg_ph);
676 memcpy (packet_ptr + offset, &pkg_num_values, sizeof (pkg_num_values));
677 offset += sizeof (pkg_num_values);
678 memcpy (packet_ptr + offset, pkg_values_types, num_values * sizeof (uint8_t));
679 offset += num_values * sizeof (uint8_t);
680 memcpy (packet_ptr + offset, pkg_values, num_values * sizeof (value_t));
681 offset += num_values * sizeof (value_t);
683 assert (offset == packet_len);
685 *ret_buffer = packet_ptr + packet_len;
686 *ret_buffer_len -= packet_len;
688 free (pkg_values_types);
692 } /* int write_part_values */
694 static int write_part_number (char **ret_buffer, int *ret_buffer_len,
695 int type, uint64_t value)
700 part_header_t pkg_head;
705 packet_len = sizeof (pkg_head) + sizeof (pkg_value);
707 if (*ret_buffer_len < packet_len)
710 pkg_head.type = htons (type);
711 pkg_head.length = htons (packet_len);
712 pkg_value = htonll (value);
714 packet_ptr = *ret_buffer;
716 memcpy (packet_ptr + offset, &pkg_head, sizeof (pkg_head));
717 offset += sizeof (pkg_head);
718 memcpy (packet_ptr + offset, &pkg_value, sizeof (pkg_value));
719 offset += sizeof (pkg_value);
721 assert (offset == packet_len);
723 *ret_buffer = packet_ptr + packet_len;
724 *ret_buffer_len -= packet_len;
727 } /* int write_part_number */
729 static int write_part_string (char **ret_buffer, int *ret_buffer_len,
730 int type, const char *str, int str_len)
740 buffer_len = 2 * sizeof (uint16_t) + str_len + 1;
741 if (*ret_buffer_len < buffer_len)
744 pkg_type = htons (type);
745 pkg_length = htons (buffer_len);
747 buffer = *ret_buffer;
749 memcpy (buffer + offset, (void *) &pkg_type, sizeof (pkg_type));
750 offset += sizeof (pkg_type);
751 memcpy (buffer + offset, (void *) &pkg_length, sizeof (pkg_length));
752 offset += sizeof (pkg_length);
753 memcpy (buffer + offset, str, str_len);
755 memset (buffer + offset, '\0', 1);
758 assert (offset == buffer_len);
760 *ret_buffer = buffer + buffer_len;
761 *ret_buffer_len -= buffer_len;
764 } /* int write_part_string */
766 static int parse_part_values (void **ret_buffer, size_t *ret_buffer_len,
767 value_t **ret_values, int *ret_num_values)
769 char *buffer = *ret_buffer;
770 size_t buffer_len = *ret_buffer_len;
785 NOTICE ("network plugin: packet is too short: "
786 "buffer_len = %zu", buffer_len);
790 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
791 buffer += sizeof (tmp16);
792 pkg_type = ntohs (tmp16);
794 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
795 buffer += sizeof (tmp16);
796 pkg_length = ntohs (tmp16);
798 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
799 buffer += sizeof (tmp16);
800 pkg_numval = ntohs (tmp16);
802 assert (pkg_type == TYPE_VALUES);
804 exp_size = 3 * sizeof (uint16_t)
805 + pkg_numval * (sizeof (uint8_t) + sizeof (value_t));
806 if (buffer_len < exp_size)
808 WARNING ("network plugin: parse_part_values: "
810 "Chunk of size %zu expected, "
811 "but buffer has only %zu bytes left.",
812 exp_size, buffer_len);
816 if (pkg_length != exp_size)
818 WARNING ("network plugin: parse_part_values: "
819 "Length and number of values "
820 "in the packet don't match.");
824 pkg_types = (uint8_t *) malloc (pkg_numval * sizeof (uint8_t));
825 pkg_values = (value_t *) malloc (pkg_numval * sizeof (value_t));
826 if ((pkg_types == NULL) || (pkg_values == NULL))
830 ERROR ("network plugin: parse_part_values: malloc failed.");
834 memcpy ((void *) pkg_types, (void *) buffer, pkg_numval * sizeof (uint8_t));
835 buffer += pkg_numval * sizeof (uint8_t);
836 memcpy ((void *) pkg_values, (void *) buffer, pkg_numval * sizeof (value_t));
837 buffer += pkg_numval * sizeof (value_t);
839 for (i = 0; i < pkg_numval; i++)
841 switch (pkg_types[i])
843 case DS_TYPE_COUNTER:
844 pkg_values[i].counter = (counter_t) ntohll (pkg_values[i].counter);
848 pkg_values[i].gauge = (gauge_t) ntohd (pkg_values[i].gauge);
852 pkg_values[i].derive = (derive_t) ntohll (pkg_values[i].derive);
855 case DS_TYPE_ABSOLUTE:
856 pkg_values[i].absolute = (absolute_t) ntohll (pkg_values[i].absolute);
860 NOTICE ("network plugin: parse_part_values: "
861 "Don't know how to handle data source type %"PRIu8,
866 } /* switch (pkg_types[i]) */
869 *ret_buffer = buffer;
870 *ret_buffer_len = buffer_len - pkg_length;
871 *ret_num_values = pkg_numval;
872 *ret_values = pkg_values;
877 } /* int parse_part_values */
879 static int parse_part_number (void **ret_buffer, size_t *ret_buffer_len,
882 char *buffer = *ret_buffer;
883 size_t buffer_len = *ret_buffer_len;
887 size_t exp_size = 2 * sizeof (uint16_t) + sizeof (uint64_t);
891 if (buffer_len < exp_size)
893 WARNING ("network plugin: parse_part_number: "
895 "Chunk of size %zu expected, "
896 "but buffer has only %zu bytes left.",
897 exp_size, buffer_len);
901 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
902 buffer += sizeof (tmp16);
903 /* pkg_type = ntohs (tmp16); */
905 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
906 buffer += sizeof (tmp16);
907 pkg_length = ntohs (tmp16);
909 memcpy ((void *) &tmp64, buffer, sizeof (tmp64));
910 buffer += sizeof (tmp64);
911 *value = ntohll (tmp64);
913 *ret_buffer = buffer;
914 *ret_buffer_len = buffer_len - pkg_length;
917 } /* int parse_part_number */
919 static int parse_part_string (void **ret_buffer, size_t *ret_buffer_len,
920 char *output, int output_len)
922 char *buffer = *ret_buffer;
923 size_t buffer_len = *ret_buffer_len;
926 size_t header_size = 2 * sizeof (uint16_t);
930 if (buffer_len < header_size)
932 WARNING ("network plugin: parse_part_string: "
934 "Chunk of at least size %zu expected, "
935 "but buffer has only %zu bytes left.",
936 header_size, buffer_len);
940 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
941 buffer += sizeof (tmp16);
942 /* pkg_type = ntohs (tmp16); */
944 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
945 buffer += sizeof (tmp16);
946 pkg_length = ntohs (tmp16);
948 /* Check that packet fits in the input buffer */
949 if (pkg_length > buffer_len)
951 WARNING ("network plugin: parse_part_string: "
953 "Chunk of size %"PRIu16" received, "
954 "but buffer has only %zu bytes left.",
955 pkg_length, buffer_len);
959 /* Check that pkg_length is in the valid range */
960 if (pkg_length <= header_size)
962 WARNING ("network plugin: parse_part_string: "
964 "Header claims this packet is only %hu "
965 "bytes long.", pkg_length);
969 /* Check that the package data fits into the output buffer.
970 * The previous if-statement ensures that:
971 * `pkg_length > header_size' */
973 || ((size_t) output_len < ((size_t) pkg_length - header_size)))
975 WARNING ("network plugin: parse_part_string: "
976 "Output buffer too small.");
980 /* All sanity checks successfull, let's copy the data over */
981 output_len = pkg_length - header_size;
982 memcpy ((void *) output, (void *) buffer, output_len);
983 buffer += output_len;
985 /* For some very weird reason '\0' doesn't do the trick on SPARC in
987 if (output[output_len - 1] != 0)
989 WARNING ("network plugin: parse_part_string: "
990 "Received string does not end "
991 "with a NULL-byte.");
995 *ret_buffer = buffer;
996 *ret_buffer_len = buffer_len - pkg_length;
999 } /* int parse_part_string */
1001 /* Forward declaration: parse_part_sign_sha256 and parse_part_encr_aes256 call
1002 * parse_packet and vice versa. */
1003 #define PP_SIGNED 0x01
1004 #define PP_ENCRYPTED 0x02
1005 static int parse_packet (sockent_t *se,
1006 void *buffer, size_t buffer_size, int flags,
1007 const char *username);
1009 #define BUFFER_READ(p,s) do { \
1010 memcpy ((p), buffer + buffer_offset, (s)); \
1011 buffer_offset += (s); \
1015 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
1016 void **ret_buffer, size_t *ret_buffer_len, int flags)
1018 static c_complain_t complain_no_users = C_COMPLAIN_INIT_STATIC;
1022 size_t buffer_offset;
1024 size_t username_len;
1027 part_signature_sha256_t pss;
1028 uint16_t pss_head_length;
1029 char hash[sizeof (pss.hash)];
1033 unsigned char *hash_ptr;
1035 buffer = *ret_buffer;
1036 buffer_len = *ret_buffer_len;
1039 if (se->data.server.userdb == NULL)
1041 c_complain (LOG_NOTICE, &complain_no_users,
1042 "network plugin: Received signed network packet but can't verify it "
1043 "because no user DB has been configured. Will accept it.");
1047 /* Check if the buffer has enough data for this structure. */
1048 if (buffer_len <= PART_SIGNATURE_SHA256_SIZE)
1051 /* Read type and length header */
1052 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
1053 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
1054 pss_head_length = ntohs (pss.head.length);
1056 /* Check if the `pss_head_length' is within bounds. */
1057 if ((pss_head_length <= PART_SIGNATURE_SHA256_SIZE)
1058 || (pss_head_length > buffer_len))
1060 ERROR ("network plugin: HMAC-SHA-256 with invalid length received.");
1064 /* Copy the hash. */
1065 BUFFER_READ (pss.hash, sizeof (pss.hash));
1067 /* Calculate username length (without null byte) and allocate memory */
1068 username_len = pss_head_length - PART_SIGNATURE_SHA256_SIZE;
1069 pss.username = malloc (username_len + 1);
1070 if (pss.username == NULL)
1073 /* Read the username */
1074 BUFFER_READ (pss.username, username_len);
1075 pss.username[username_len] = 0;
1077 assert (buffer_offset == pss_head_length);
1079 /* Query the password */
1080 secret = fbh_get (se->data.server.userdb, pss.username);
1083 ERROR ("network plugin: Unknown user: %s", pss.username);
1084 sfree (pss.username);
1088 /* Create a hash device and check the HMAC */
1090 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
1093 ERROR ("network plugin: Creating HMAC-SHA-256 object failed: %s",
1094 gcry_strerror (err));
1096 sfree (pss.username);
1100 err = gcry_md_setkey (hd, secret, strlen (secret));
1103 ERROR ("network plugin: gcry_md_setkey failed: %s", gcry_strerror (err));
1106 sfree (pss.username);
1111 buffer + PART_SIGNATURE_SHA256_SIZE,
1112 buffer_len - PART_SIGNATURE_SHA256_SIZE);
1113 hash_ptr = gcry_md_read (hd, GCRY_MD_SHA256);
1114 if (hash_ptr == NULL)
1116 ERROR ("network plugin: gcry_md_read failed.");
1119 sfree (pss.username);
1122 memcpy (hash, hash_ptr, sizeof (hash));
1128 if (memcmp (pss.hash, hash, sizeof (pss.hash)) != 0)
1130 WARNING ("network plugin: Verifying HMAC-SHA-256 signature failed: "
1135 parse_packet (se, buffer + buffer_offset, buffer_len - buffer_offset,
1136 flags | PP_SIGNED, pss.username);
1140 sfree (pss.username);
1142 *ret_buffer = buffer + buffer_len;
1143 *ret_buffer_len = 0;
1146 } /* }}} int parse_part_sign_sha256 */
1147 /* #endif HAVE_LIBGCRYPT */
1149 #else /* if !HAVE_LIBGCRYPT */
1150 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
1151 void **ret_buffer, size_t *ret_buffer_size, int flags)
1153 static int warning_has_been_printed = 0;
1157 size_t buffer_offset;
1160 part_signature_sha256_t pss;
1162 buffer = *ret_buffer;
1163 buffer_size = *ret_buffer_size;
1166 if (buffer_size <= PART_SIGNATURE_SHA256_SIZE)
1169 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
1170 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
1171 part_len = ntohs (pss.head.length);
1173 if ((part_len <= PART_SIGNATURE_SHA256_SIZE)
1174 || (part_len > buffer_size))
1177 if (warning_has_been_printed == 0)
1179 WARNING ("network plugin: Received signed packet, but the network "
1180 "plugin was not linked with libgcrypt, so I cannot "
1181 "verify the signature. The packet will be accepted.");
1182 warning_has_been_printed = 1;
1185 parse_packet (se, buffer + part_len, buffer_size - part_len, flags,
1186 /* username = */ NULL);
1188 *ret_buffer = buffer + buffer_size;
1189 *ret_buffer_size = 0;
1192 } /* }}} int parse_part_sign_sha256 */
1193 #endif /* !HAVE_LIBGCRYPT */
1196 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1197 void **ret_buffer, size_t *ret_buffer_len,
1200 char *buffer = *ret_buffer;
1201 size_t buffer_len = *ret_buffer_len;
1204 size_t buffer_offset;
1205 uint16_t username_len;
1206 part_encryption_aes256_t pea;
1207 unsigned char hash[sizeof (pea.hash)];
1209 gcry_cipher_hd_t cypher;
1212 /* Make sure at least the header if available. */
1213 if (buffer_len <= PART_ENCRYPTION_AES256_SIZE)
1215 NOTICE ("network plugin: parse_part_encr_aes256: "
1216 "Discarding short packet.");
1222 /* Copy the unencrypted information into `pea'. */
1223 BUFFER_READ (&pea.head.type, sizeof (pea.head.type));
1224 BUFFER_READ (&pea.head.length, sizeof (pea.head.length));
1226 /* Check the `part size'. */
1227 part_size = ntohs (pea.head.length);
1228 if ((part_size <= PART_ENCRYPTION_AES256_SIZE)
1229 || (part_size > buffer_len))
1231 NOTICE ("network plugin: parse_part_encr_aes256: "
1232 "Discarding part with invalid size.");
1236 /* Read the username */
1237 BUFFER_READ (&username_len, sizeof (username_len));
1238 username_len = ntohs (username_len);
1240 if ((username_len <= 0)
1241 || (username_len > (part_size - (PART_ENCRYPTION_AES256_SIZE + 1))))
1243 NOTICE ("network plugin: parse_part_encr_aes256: "
1244 "Discarding part with invalid username length.");
1248 assert (username_len > 0);
1249 pea.username = malloc (username_len + 1);
1250 if (pea.username == NULL)
1252 BUFFER_READ (pea.username, username_len);
1253 pea.username[username_len] = 0;
1255 /* Last but not least, the initialization vector */
1256 BUFFER_READ (pea.iv, sizeof (pea.iv));
1258 /* Make sure we are at the right position */
1259 assert (buffer_offset == (username_len +
1260 PART_ENCRYPTION_AES256_SIZE - sizeof (pea.hash)));
1262 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
1266 sfree (pea.username);
1270 payload_len = part_size - (PART_ENCRYPTION_AES256_SIZE + username_len);
1271 assert (payload_len > 0);
1273 /* Decrypt the packet in-place */
1274 err = gcry_cipher_decrypt (cypher,
1275 buffer + buffer_offset,
1276 part_size - buffer_offset,
1277 /* in = */ NULL, /* in len = */ 0);
1280 sfree (pea.username);
1281 ERROR ("network plugin: gcry_cipher_decrypt returned: %s",
1282 gcry_strerror (err));
1287 BUFFER_READ (pea.hash, sizeof (pea.hash));
1289 /* Make sure we're at the right position - again */
1290 assert (buffer_offset == (username_len + PART_ENCRYPTION_AES256_SIZE));
1291 assert (buffer_offset == (part_size - payload_len));
1293 /* Check hash sum */
1294 memset (hash, 0, sizeof (hash));
1295 gcry_md_hash_buffer (GCRY_MD_SHA1, hash,
1296 buffer + buffer_offset, payload_len);
1297 if (memcmp (hash, pea.hash, sizeof (hash)) != 0)
1299 sfree (pea.username);
1300 ERROR ("network plugin: Decryption failed: Checksum mismatch.");
1304 parse_packet (se, buffer + buffer_offset, payload_len,
1305 flags | PP_ENCRYPTED, pea.username);
1307 /* XXX: Free pea.username?!? */
1309 /* Update return values */
1310 *ret_buffer = buffer + part_size;
1311 *ret_buffer_len = buffer_len - part_size;
1313 sfree (pea.username);
1316 } /* }}} int parse_part_encr_aes256 */
1317 /* #endif HAVE_LIBGCRYPT */
1319 #else /* if !HAVE_LIBGCRYPT */
1320 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1321 void **ret_buffer, size_t *ret_buffer_size, int flags)
1323 static int warning_has_been_printed = 0;
1327 size_t buffer_offset;
1332 buffer = *ret_buffer;
1333 buffer_size = *ret_buffer_size;
1336 /* parse_packet assures this minimum size. */
1337 assert (buffer_size >= (sizeof (ph.type) + sizeof (ph.length)));
1339 BUFFER_READ (&ph.type, sizeof (ph.type));
1340 BUFFER_READ (&ph.length, sizeof (ph.length));
1341 ph_length = ntohs (ph.length);
1343 if ((ph_length <= PART_ENCRYPTION_AES256_SIZE)
1344 || (ph_length > buffer_size))
1346 ERROR ("network plugin: AES-256 encrypted part "
1347 "with invalid length received.");
1351 if (warning_has_been_printed == 0)
1353 WARNING ("network plugin: Received encrypted packet, but the network "
1354 "plugin was not linked with libgcrypt, so I cannot "
1355 "decrypt it. The part will be discarded.");
1356 warning_has_been_printed = 1;
1359 *ret_buffer += ph_length;
1360 *ret_buffer_size -= ph_length;
1363 } /* }}} int parse_part_encr_aes256 */
1364 #endif /* !HAVE_LIBGCRYPT */
1368 static int parse_packet (sockent_t *se, /* {{{ */
1369 void *buffer, size_t buffer_size, int flags,
1370 const char *username)
1374 value_list_t vl = VALUE_LIST_INIT;
1378 int packet_was_signed = (flags & PP_SIGNED);
1379 int packet_was_encrypted = (flags & PP_ENCRYPTED);
1380 int printed_ignore_warning = 0;
1381 #endif /* HAVE_LIBGCRYPT */
1384 memset (&vl, '\0', sizeof (vl));
1385 memset (&n, '\0', sizeof (n));
1388 while ((status == 0) && (0 < buffer_size)
1389 && ((unsigned int) buffer_size > sizeof (part_header_t)))
1391 uint16_t pkg_length;
1394 memcpy ((void *) &pkg_type,
1397 memcpy ((void *) &pkg_length,
1398 (void *) (buffer + sizeof (pkg_type)),
1399 sizeof (pkg_length));
1401 pkg_length = ntohs (pkg_length);
1402 pkg_type = ntohs (pkg_type);
1404 if (pkg_length > buffer_size)
1406 /* Ensure that this loop terminates eventually */
1407 if (pkg_length < (2 * sizeof (uint16_t)))
1410 if (pkg_type == TYPE_ENCR_AES256)
1412 status = parse_part_encr_aes256 (se,
1413 &buffer, &buffer_size, flags);
1416 ERROR ("network plugin: Decrypting AES256 "
1418 "with status %i.", status);
1423 else if ((se->data.server.security_level == SECURITY_LEVEL_ENCRYPT)
1424 && (packet_was_encrypted == 0))
1426 if (printed_ignore_warning == 0)
1428 INFO ("network plugin: Unencrypted packet or "
1429 "part has been ignored.");
1430 printed_ignore_warning = 1;
1432 buffer = ((char *) buffer) + pkg_length;
1435 #endif /* HAVE_LIBGCRYPT */
1436 else if (pkg_type == TYPE_SIGN_SHA256)
1438 status = parse_part_sign_sha256 (se,
1439 &buffer, &buffer_size, flags);
1442 ERROR ("network plugin: Verifying HMAC-SHA-256 "
1444 "with status %i.", status);
1449 else if ((se->data.server.security_level == SECURITY_LEVEL_SIGN)
1450 && (packet_was_encrypted == 0)
1451 && (packet_was_signed == 0))
1453 if (printed_ignore_warning == 0)
1455 INFO ("network plugin: Unsigned packet or "
1456 "part has been ignored.");
1457 printed_ignore_warning = 1;
1459 buffer = ((char *) buffer) + pkg_length;
1462 #endif /* HAVE_LIBGCRYPT */
1463 else if (pkg_type == TYPE_VALUES)
1465 status = parse_part_values (&buffer, &buffer_size,
1466 &vl.values, &vl.values_len);
1470 network_dispatch_values (&vl, username);
1474 else if (pkg_type == TYPE_TIME)
1477 status = parse_part_number (&buffer, &buffer_size,
1481 vl.time = TIME_T_TO_CDTIME_T (tmp);
1482 n.time = TIME_T_TO_CDTIME_T (tmp);
1485 else if (pkg_type == TYPE_TIME_HR)
1488 status = parse_part_number (&buffer, &buffer_size,
1492 vl.time = (cdtime_t) tmp;
1493 n.time = (cdtime_t) tmp;
1496 else if (pkg_type == TYPE_INTERVAL)
1499 status = parse_part_number (&buffer, &buffer_size,
1502 vl.interval = TIME_T_TO_CDTIME_T (tmp);
1504 else if (pkg_type == TYPE_INTERVAL_HR)
1507 status = parse_part_number (&buffer, &buffer_size,
1510 vl.interval = (cdtime_t) tmp;
1512 else if (pkg_type == TYPE_HOST)
1514 status = parse_part_string (&buffer, &buffer_size,
1515 vl.host, sizeof (vl.host));
1517 sstrncpy (n.host, vl.host, sizeof (n.host));
1519 else if (pkg_type == TYPE_PLUGIN)
1521 status = parse_part_string (&buffer, &buffer_size,
1522 vl.plugin, sizeof (vl.plugin));
1524 sstrncpy (n.plugin, vl.plugin,
1527 else if (pkg_type == TYPE_PLUGIN_INSTANCE)
1529 status = parse_part_string (&buffer, &buffer_size,
1531 sizeof (vl.plugin_instance));
1533 sstrncpy (n.plugin_instance,
1535 sizeof (n.plugin_instance));
1537 else if (pkg_type == TYPE_TYPE)
1539 status = parse_part_string (&buffer, &buffer_size,
1540 vl.type, sizeof (vl.type));
1542 sstrncpy (n.type, vl.type, sizeof (n.type));
1544 else if (pkg_type == TYPE_TYPE_INSTANCE)
1546 status = parse_part_string (&buffer, &buffer_size,
1548 sizeof (vl.type_instance));
1550 sstrncpy (n.type_instance, vl.type_instance,
1551 sizeof (n.type_instance));
1553 else if (pkg_type == TYPE_MESSAGE)
1555 status = parse_part_string (&buffer, &buffer_size,
1556 n.message, sizeof (n.message));
1562 else if ((n.severity != NOTIF_FAILURE)
1563 && (n.severity != NOTIF_WARNING)
1564 && (n.severity != NOTIF_OKAY))
1566 INFO ("network plugin: "
1567 "Ignoring notification with "
1568 "unknown severity %i.",
1571 else if (n.time <= 0)
1573 INFO ("network plugin: "
1574 "Ignoring notification with "
1577 else if (strlen (n.message) <= 0)
1579 INFO ("network plugin: "
1580 "Ignoring notification with "
1581 "an empty message.");
1585 network_dispatch_notification (&n);
1588 else if (pkg_type == TYPE_SEVERITY)
1591 status = parse_part_number (&buffer, &buffer_size,
1594 n.severity = (int) tmp;
1598 DEBUG ("network plugin: parse_packet: Unknown part"
1599 " type: 0x%04hx", pkg_type);
1600 buffer = ((char *) buffer) + pkg_length;
1602 } /* while (buffer_size > sizeof (part_header_t)) */
1604 if (status == 0 && buffer_size > 0)
1605 WARNING ("network plugin: parse_packet: Received truncated "
1606 "packet, try increasing `MaxPacketSize'");
1609 } /* }}} int parse_packet */
1611 static void free_sockent_client (struct sockent_client *sec) /* {{{ */
1620 sfree (sec->username);
1621 sfree (sec->password);
1622 if (sec->cypher != NULL)
1623 gcry_cipher_close (sec->cypher);
1625 } /* }}} void free_sockent_client */
1627 static void free_sockent_server (struct sockent_server *ses) /* {{{ */
1631 for (i = 0; i < ses->fd_num; i++)
1633 if (ses->fd[i] >= 0)
1642 sfree (ses->auth_file);
1643 fbh_destroy (ses->userdb);
1644 if (ses->cypher != NULL)
1645 gcry_cipher_close (ses->cypher);
1647 } /* }}} void free_sockent_server */
1649 static void sockent_destroy (sockent_t *se) /* {{{ */
1653 DEBUG ("network plugin: sockent_destroy (se = %p);", (void *) se);
1660 sfree (se->service);
1662 if (se->type == SOCKENT_TYPE_CLIENT)
1663 free_sockent_client (&se->data.client);
1665 free_sockent_server (&se->data.server);
1670 } /* }}} void sockent_destroy */
1673 * int network_set_ttl
1675 * Set the `IP_MULTICAST_TTL', `IP_TTL', `IPV6_MULTICAST_HOPS' or
1676 * `IPV6_UNICAST_HOPS', depending on which option is applicable.
1678 * The `struct addrinfo' is used to destinguish between unicast and multicast
1681 static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
1683 DEBUG ("network plugin: network_set_ttl: network_config_ttl = %i;",
1684 network_config_ttl);
1686 assert (se->type == SOCKENT_TYPE_CLIENT);
1688 if ((network_config_ttl < 1) || (network_config_ttl > 255))
1691 if (ai->ai_family == AF_INET)
1693 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1696 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1697 optname = IP_MULTICAST_TTL;
1701 if (setsockopt (se->data.client.fd, IPPROTO_IP, optname,
1702 &network_config_ttl,
1703 sizeof (network_config_ttl)) != 0)
1706 ERROR ("network plugin: setsockopt (ipv4-ttl): %s",
1707 sstrerror (errno, errbuf, sizeof (errbuf)));
1711 else if (ai->ai_family == AF_INET6)
1713 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1714 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1717 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1718 optname = IPV6_MULTICAST_HOPS;
1720 optname = IPV6_UNICAST_HOPS;
1722 if (setsockopt (se->data.client.fd, IPPROTO_IPV6, optname,
1723 &network_config_ttl,
1724 sizeof (network_config_ttl)) != 0)
1727 ERROR ("network plugin: setsockopt(ipv6-ttl): %s",
1728 sstrerror (errno, errbuf,
1735 } /* int network_set_ttl */
1737 static int network_set_interface (const sockent_t *se, const struct addrinfo *ai) /* {{{ */
1739 DEBUG ("network plugin: network_set_interface: interface index = %i;",
1742 assert (se->type == SOCKENT_TYPE_CLIENT);
1744 if (ai->ai_family == AF_INET)
1746 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1748 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1750 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1751 /* If possible, use the "ip_mreqn" structure which has
1752 * an "interface index" member. Using the interface
1753 * index is preferred here, because of its similarity
1754 * to the way IPv6 handles this. Unfortunately, it
1755 * appears not to be portable. */
1756 struct ip_mreqn mreq;
1758 memset (&mreq, 0, sizeof (mreq));
1759 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1760 mreq.imr_address.s_addr = ntohl (INADDR_ANY);
1761 mreq.imr_ifindex = se->interface;
1763 struct ip_mreq mreq;
1765 memset (&mreq, 0, sizeof (mreq));
1766 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1767 mreq.imr_interface.s_addr = ntohl (INADDR_ANY);
1770 if (setsockopt (se->data.client.fd, IPPROTO_IP, IP_MULTICAST_IF,
1771 &mreq, sizeof (mreq)) != 0)
1774 ERROR ("network plugin: setsockopt (ipv4-multicast-if): %s",
1775 sstrerror (errno, errbuf, sizeof (errbuf)));
1782 else if (ai->ai_family == AF_INET6)
1784 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1786 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1788 if (setsockopt (se->data.client.fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
1790 sizeof (se->interface)) != 0)
1793 ERROR ("network plugin: setsockopt (ipv6-multicast-if): %s",
1794 sstrerror (errno, errbuf,
1803 /* else: Not a multicast interface. */
1804 if (se->interface != 0)
1806 #if defined(HAVE_IF_INDEXTONAME) && HAVE_IF_INDEXTONAME && defined(SO_BINDTODEVICE)
1807 char interface_name[IFNAMSIZ];
1809 if (if_indextoname (se->interface, interface_name) == NULL)
1812 DEBUG ("network plugin: Binding socket to interface %s", interface_name);
1814 if (setsockopt (se->data.client.fd, SOL_SOCKET, SO_BINDTODEVICE,
1816 sizeof(interface_name)) == -1 )
1819 ERROR ("network plugin: setsockopt (bind-if): %s",
1820 sstrerror (errno, errbuf, sizeof (errbuf)));
1823 /* #endif HAVE_IF_INDEXTONAME && SO_BINDTODEVICE */
1826 WARNING ("network plugin: Cannot set the interface on a unicast "
1828 # if !defined(SO_BINDTODEVICE)
1829 "the \"SO_BINDTODEVICE\" socket option "
1831 "the \"if_indextoname\" function "
1833 "is not available on your system.");
1839 } /* }}} network_set_interface */
1841 static int network_bind_socket (int fd, const struct addrinfo *ai, const int interface_idx)
1850 /* allow multiple sockets to use the same PORT number */
1851 if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR,
1852 &yes, sizeof(yes)) == -1) {
1854 ERROR ("network plugin: setsockopt (reuseaddr): %s",
1855 sstrerror (errno, errbuf, sizeof (errbuf)));
1859 DEBUG ("fd = %i; calling `bind'", fd);
1861 if (bind (fd, ai->ai_addr, ai->ai_addrlen) == -1)
1865 sstrerror (errno, errbuf, sizeof (errbuf)));
1869 if (ai->ai_family == AF_INET)
1871 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1872 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1874 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1875 struct ip_mreqn mreq;
1877 struct ip_mreq mreq;
1880 DEBUG ("fd = %i; IPv4 multicast address found", fd);
1882 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1883 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1884 /* Set the interface using the interface index if
1885 * possible (available). Unfortunately, the struct
1886 * ip_mreqn is not portable. */
1887 mreq.imr_address.s_addr = ntohl (INADDR_ANY);
1888 mreq.imr_ifindex = interface_idx;
1890 mreq.imr_interface.s_addr = ntohl (INADDR_ANY);
1893 if (setsockopt (fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1894 &loop, sizeof (loop)) == -1)
1897 ERROR ("network plugin: setsockopt (multicast-loop): %s",
1898 sstrerror (errno, errbuf,
1903 if (setsockopt (fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1904 &mreq, sizeof (mreq)) == -1)
1907 ERROR ("network plugin: setsockopt (add-membership): %s",
1908 sstrerror (errno, errbuf,
1916 else if (ai->ai_family == AF_INET6)
1918 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1919 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1920 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1922 struct ipv6_mreq mreq;
1924 DEBUG ("fd = %i; IPv6 multicast address found", fd);
1926 memcpy (&mreq.ipv6mr_multiaddr,
1928 sizeof (addr->sin6_addr));
1930 /* http://developer.apple.com/documentation/Darwin/Reference/ManPages/man4/ip6.4.html
1931 * ipv6mr_interface may be set to zeroes to
1932 * choose the default multicast interface or to
1933 * the index of a particular multicast-capable
1934 * interface if the host is multihomed.
1935 * Membership is associ-associated with a
1936 * single interface; programs running on
1937 * multihomed hosts may need to join the same
1938 * group on more than one interface.*/
1939 mreq.ipv6mr_interface = interface_idx;
1941 if (setsockopt (fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
1942 &loop, sizeof (loop)) == -1)
1945 ERROR ("network plugin: setsockopt (ipv6-multicast-loop): %s",
1946 sstrerror (errno, errbuf,
1951 if (setsockopt (fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
1952 &mreq, sizeof (mreq)) == -1)
1955 ERROR ("network plugin: setsockopt (ipv6-add-membership): %s",
1956 sstrerror (errno, errbuf,
1965 #if defined(HAVE_IF_INDEXTONAME) && HAVE_IF_INDEXTONAME && defined(SO_BINDTODEVICE)
1966 /* if a specific interface was set, bind the socket to it. But to avoid
1967 * possible problems with multicast routing, only do that for non-multicast
1969 if (interface_idx != 0)
1971 char interface_name[IFNAMSIZ];
1973 if (if_indextoname (interface_idx, interface_name) == NULL)
1976 DEBUG ("fd = %i; Binding socket to interface %s", fd, interface_name);
1978 if (setsockopt (fd, SOL_SOCKET, SO_BINDTODEVICE,
1980 sizeof(interface_name)) == -1 )
1983 ERROR ("network plugin: setsockopt (bind-if): %s",
1984 sstrerror (errno, errbuf, sizeof (errbuf)));
1988 #endif /* HAVE_IF_INDEXTONAME && SO_BINDTODEVICE */
1991 } /* int network_bind_socket */
1993 /* Initialize a sockent structure. `type' must be either `SOCKENT_TYPE_CLIENT'
1994 * or `SOCKENT_TYPE_SERVER' */
1995 static sockent_t *sockent_create (int type) /* {{{ */
1999 if ((type != SOCKENT_TYPE_CLIENT) && (type != SOCKENT_TYPE_SERVER))
2002 se = malloc (sizeof (*se));
2005 memset (se, 0, sizeof (*se));
2013 if (type == SOCKENT_TYPE_SERVER)
2015 se->data.server.fd = NULL;
2017 se->data.server.security_level = SECURITY_LEVEL_NONE;
2018 se->data.server.auth_file = NULL;
2019 se->data.server.userdb = NULL;
2020 se->data.server.cypher = NULL;
2025 se->data.client.fd = -1;
2026 se->data.client.addr = NULL;
2028 se->data.client.security_level = SECURITY_LEVEL_NONE;
2029 se->data.client.username = NULL;
2030 se->data.client.password = NULL;
2031 se->data.client.cypher = NULL;
2036 } /* }}} sockent_t *sockent_create */
2038 static int sockent_init_crypto (sockent_t *se) /* {{{ */
2040 #if HAVE_LIBGCRYPT /* {{{ */
2041 if (se->type == SOCKENT_TYPE_CLIENT)
2043 if (se->data.client.security_level > SECURITY_LEVEL_NONE)
2045 network_init_gcrypt ();
2047 if ((se->data.client.username == NULL)
2048 || (se->data.client.password == NULL))
2050 ERROR ("network plugin: Client socket with "
2051 "security requested, but no "
2052 "credentials are configured.");
2055 gcry_md_hash_buffer (GCRY_MD_SHA256,
2056 se->data.client.password_hash,
2057 se->data.client.password,
2058 strlen (se->data.client.password));
2061 else /* (se->type == SOCKENT_TYPE_SERVER) */
2063 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
2065 network_init_gcrypt ();
2067 if (se->data.server.auth_file == NULL)
2069 ERROR ("network plugin: Server socket with "
2070 "security requested, but no "
2071 "password file is configured.");
2075 if (se->data.server.auth_file != NULL)
2077 se->data.server.userdb = fbh_create (se->data.server.auth_file);
2078 if (se->data.server.userdb == NULL)
2080 ERROR ("network plugin: Reading password file "
2082 se->data.server.auth_file);
2083 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
2088 #endif /* }}} HAVE_LIBGCRYPT */
2091 } /* }}} int sockent_init_crypto */
2093 static int sockent_client_connect (sockent_t *se) /* {{{ */
2095 static c_complain_t complaint = C_COMPLAIN_INIT_STATIC;
2097 struct sockent_client *client;
2098 struct addrinfo ai_hints;
2099 struct addrinfo *ai_list = NULL, *ai_ptr;
2102 if ((se == NULL) || (se->type != SOCKENT_TYPE_CLIENT))
2105 client = &se->data.client;
2106 if (client->fd >= 0) /* already connected */
2109 memset (&ai_hints, 0, sizeof (ai_hints));
2110 #ifdef AI_ADDRCONFIG
2111 ai_hints.ai_flags |= AI_ADDRCONFIG;
2113 ai_hints.ai_family = AF_UNSPEC;
2114 ai_hints.ai_socktype = SOCK_DGRAM;
2115 ai_hints.ai_protocol = IPPROTO_UDP;
2117 status = getaddrinfo (se->node,
2118 (se->service != NULL) ? se->service : NET_DEFAULT_PORT,
2119 &ai_hints, &ai_list);
2122 c_complain (LOG_ERR, &complaint,
2123 "network plugin: getaddrinfo (%s, %s) failed: %s",
2124 (se->node == NULL) ? "(null)" : se->node,
2125 (se->service == NULL) ? "(null)" : se->service,
2126 gai_strerror (status));
2131 c_release (LOG_NOTICE, &complaint,
2132 "network plugin: Successfully resolved \"%s\".",
2136 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
2138 client->fd = socket (ai_ptr->ai_family,
2139 ai_ptr->ai_socktype,
2140 ai_ptr->ai_protocol);
2144 ERROR ("network plugin: socket(2) failed: %s",
2145 sstrerror (errno, errbuf,
2150 client->addr = malloc (sizeof (*client->addr));
2151 if (client->addr == NULL)
2153 ERROR ("network plugin: malloc failed.");
2159 memset (client->addr, 0, sizeof (*client->addr));
2160 assert (sizeof (*client->addr) >= ai_ptr->ai_addrlen);
2161 memcpy (client->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
2162 client->addrlen = ai_ptr->ai_addrlen;
2164 network_set_ttl (se, ai_ptr);
2165 network_set_interface (se, ai_ptr);
2167 /* We don't open more than one write-socket per
2168 * node/service pair.. */
2172 freeaddrinfo (ai_list);
2176 } /* }}} int sockent_client_connect */
2178 static int sockent_client_disconnect (sockent_t *se) /* {{{ */
2180 struct sockent_client *client;
2182 if ((se == NULL) || (se->type != SOCKENT_TYPE_CLIENT))
2185 client = &se->data.client;
2186 if (client->fd >= 0) /* connected */
2192 sfree (client->addr);
2193 client->addrlen = 0;
2196 } /* }}} int sockent_client_disconnect */
2198 /* Open the file descriptors for a initialized sockent structure. */
2199 static int sockent_server_listen (sockent_t *se) /* {{{ */
2201 struct addrinfo ai_hints;
2202 struct addrinfo *ai_list, *ai_ptr;
2206 const char *service;
2212 service = se->service;
2214 if (service == NULL)
2215 service = NET_DEFAULT_PORT;
2217 DEBUG ("network plugin: sockent_server_listen: node = %s; service = %s;",
2220 memset (&ai_hints, 0, sizeof (ai_hints));
2221 ai_hints.ai_flags = 0;
2223 ai_hints.ai_flags |= AI_PASSIVE;
2225 #ifdef AI_ADDRCONFIG
2226 ai_hints.ai_flags |= AI_ADDRCONFIG;
2228 ai_hints.ai_family = AF_UNSPEC;
2229 ai_hints.ai_socktype = SOCK_DGRAM;
2230 ai_hints.ai_protocol = IPPROTO_UDP;
2232 status = getaddrinfo (node, service, &ai_hints, &ai_list);
2235 ERROR ("network plugin: getaddrinfo (%s, %s) failed: %s",
2236 (se->node == NULL) ? "(null)" : se->node,
2237 (se->service == NULL) ? "(null)" : se->service,
2238 gai_strerror (status));
2242 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
2246 tmp = realloc (se->data.server.fd,
2247 sizeof (*tmp) * (se->data.server.fd_num + 1));
2250 ERROR ("network plugin: realloc failed.");
2253 se->data.server.fd = tmp;
2254 tmp = se->data.server.fd + se->data.server.fd_num;
2256 *tmp = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
2257 ai_ptr->ai_protocol);
2261 ERROR ("network plugin: socket(2) failed: %s",
2262 sstrerror (errno, errbuf,
2267 status = network_bind_socket (*tmp, ai_ptr, se->interface);
2275 se->data.server.fd_num++;
2277 } /* for (ai_list) */
2279 freeaddrinfo (ai_list);
2281 if (se->data.server.fd_num <= 0)
2284 } /* }}} int sockent_server_listen */
2286 /* Add a sockent to the global list of sockets */
2287 static int sockent_add (sockent_t *se) /* {{{ */
2289 sockent_t *last_ptr;
2294 if (se->type == SOCKENT_TYPE_SERVER)
2299 tmp = realloc (listen_sockets_pollfd,
2300 sizeof (*tmp) * (listen_sockets_num
2301 + se->data.server.fd_num));
2304 ERROR ("network plugin: realloc failed.");
2307 listen_sockets_pollfd = tmp;
2308 tmp = listen_sockets_pollfd + listen_sockets_num;
2310 for (i = 0; i < se->data.server.fd_num; i++)
2312 memset (tmp + i, 0, sizeof (*tmp));
2313 tmp[i].fd = se->data.server.fd[i];
2314 tmp[i].events = POLLIN | POLLPRI;
2318 listen_sockets_num += se->data.server.fd_num;
2320 if (listen_sockets == NULL)
2322 listen_sockets = se;
2325 last_ptr = listen_sockets;
2327 else /* if (se->type == SOCKENT_TYPE_CLIENT) */
2329 if (sending_sockets == NULL)
2331 sending_sockets = se;
2334 last_ptr = sending_sockets;
2337 while (last_ptr->next != NULL)
2338 last_ptr = last_ptr->next;
2339 last_ptr->next = se;
2342 } /* }}} int sockent_add */
2344 static void *dispatch_thread (void __attribute__((unused)) *arg) /* {{{ */
2348 receive_list_entry_t *ent;
2351 /* Lock and wait for more data to come in */
2352 pthread_mutex_lock (&receive_list_lock);
2353 while ((listen_loop == 0)
2354 && (receive_list_head == NULL))
2355 pthread_cond_wait (&receive_list_cond, &receive_list_lock);
2357 /* Remove the head entry and unlock */
2358 ent = receive_list_head;
2360 receive_list_head = ent->next;
2361 receive_list_length--;
2362 pthread_mutex_unlock (&receive_list_lock);
2364 /* Check whether we are supposed to exit. We do NOT check `listen_loop'
2365 * because we dispatch all missing packets before shutting down. */
2369 /* Look for the correct `sockent_t' */
2370 se = listen_sockets;
2375 for (i = 0; i < se->data.server.fd_num; i++)
2376 if (se->data.server.fd[i] == ent->fd)
2379 if (i < se->data.server.fd_num)
2387 ERROR ("network plugin: Got packet from FD %i, but can't "
2388 "find an appropriate socket entry.",
2395 parse_packet (se, ent->data, ent->data_len, /* flags = */ 0,
2396 /* username = */ NULL);
2402 } /* }}} void *dispatch_thread */
2404 static int network_receive (void) /* {{{ */
2406 char buffer[network_config_packet_size];
2412 receive_list_entry_t *private_list_head;
2413 receive_list_entry_t *private_list_tail;
2414 uint64_t private_list_length;
2416 assert (listen_sockets_num > 0);
2418 private_list_head = NULL;
2419 private_list_tail = NULL;
2420 private_list_length = 0;
2422 while (listen_loop == 0)
2424 status = poll (listen_sockets_pollfd, listen_sockets_num, -1);
2431 ERROR ("poll failed: %s",
2432 sstrerror (errno, errbuf, sizeof (errbuf)));
2436 for (i = 0; (i < listen_sockets_num) && (status > 0); i++)
2438 receive_list_entry_t *ent;
2440 if ((listen_sockets_pollfd[i].revents
2441 & (POLLIN | POLLPRI)) == 0)
2445 buffer_len = recv (listen_sockets_pollfd[i].fd,
2446 buffer, sizeof (buffer),
2451 ERROR ("recv failed: %s",
2452 sstrerror (errno, errbuf,
2457 stats_octets_rx += ((uint64_t) buffer_len);
2460 /* TODO: Possible performance enhancement: Do not free
2461 * these entries in the dispatch thread but put them in
2462 * another list, so we don't have to allocate more and
2463 * more of these structures. */
2464 ent = malloc (sizeof (receive_list_entry_t));
2467 ERROR ("network plugin: malloc failed.");
2470 memset (ent, 0, sizeof (receive_list_entry_t));
2471 ent->data = malloc (network_config_packet_size);
2472 if (ent->data == NULL)
2475 ERROR ("network plugin: malloc failed.");
2478 ent->fd = listen_sockets_pollfd[i].fd;
2481 memcpy (ent->data, buffer, buffer_len);
2482 ent->data_len = buffer_len;
2484 if (private_list_head == NULL)
2485 private_list_head = ent;
2487 private_list_tail->next = ent;
2488 private_list_tail = ent;
2489 private_list_length++;
2491 /* Do not block here. Blocking here has led to
2492 * insufficient performance in the past. */
2493 if (pthread_mutex_trylock (&receive_list_lock) == 0)
2495 assert (((receive_list_head == NULL) && (receive_list_length == 0))
2496 || ((receive_list_head != NULL) && (receive_list_length != 0)));
2498 if (receive_list_head == NULL)
2499 receive_list_head = private_list_head;
2501 receive_list_tail->next = private_list_head;
2502 receive_list_tail = private_list_tail;
2503 receive_list_length += private_list_length;
2505 pthread_cond_signal (&receive_list_cond);
2506 pthread_mutex_unlock (&receive_list_lock);
2508 private_list_head = NULL;
2509 private_list_tail = NULL;
2510 private_list_length = 0;
2512 } /* for (listen_sockets_pollfd) */
2513 } /* while (listen_loop == 0) */
2515 /* Make sure everything is dispatched before exiting. */
2516 if (private_list_head != NULL)
2518 pthread_mutex_lock (&receive_list_lock);
2520 if (receive_list_head == NULL)
2521 receive_list_head = private_list_head;
2523 receive_list_tail->next = private_list_head;
2524 receive_list_tail = private_list_tail;
2525 receive_list_length += private_list_length;
2527 private_list_head = NULL;
2528 private_list_tail = NULL;
2529 private_list_length = 0;
2531 pthread_cond_signal (&receive_list_cond);
2532 pthread_mutex_unlock (&receive_list_lock);
2536 } /* }}} int network_receive */
2538 static void *receive_thread (void __attribute__((unused)) *arg)
2540 return (network_receive () ? (void *) 1 : (void *) 0);
2541 } /* void *receive_thread */
2543 static void network_init_buffer (void)
2545 memset (send_buffer, 0, network_config_packet_size);
2546 send_buffer_ptr = send_buffer;
2547 send_buffer_fill = 0;
2549 memset (&send_buffer_vl, 0, sizeof (send_buffer_vl));
2550 } /* int network_init_buffer */
2552 static void networt_send_buffer_plain (sockent_t *se, /* {{{ */
2553 const char *buffer, size_t buffer_size)
2559 status = sockent_client_connect (se);
2563 status = sendto (se->data.client.fd, buffer, buffer_size,
2565 (struct sockaddr *) se->data.client.addr,
2566 se->data.client.addrlen);
2571 if ((errno == EINTR) || (errno == EAGAIN))
2574 ERROR ("network plugin: sendto failed: %s. Closing sending socket.",
2575 sstrerror (errno, errbuf, sizeof (errbuf)));
2576 sockent_client_disconnect (se);
2582 } /* }}} void networt_send_buffer_plain */
2585 #define BUFFER_ADD(p,s) do { \
2586 memcpy (buffer + buffer_offset, (p), (s)); \
2587 buffer_offset += (s); \
2590 static void networt_send_buffer_signed (sockent_t *se, /* {{{ */
2591 const char *in_buffer, size_t in_buffer_size)
2593 part_signature_sha256_t ps;
2594 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2595 size_t buffer_offset;
2596 size_t username_len;
2600 unsigned char *hash;
2603 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
2606 ERROR ("network plugin: Creating HMAC object failed: %s",
2607 gcry_strerror (err));
2611 err = gcry_md_setkey (hd, se->data.client.password,
2612 strlen (se->data.client.password));
2615 ERROR ("network plugin: gcry_md_setkey failed: %s",
2616 gcry_strerror (err));
2621 username_len = strlen (se->data.client.username);
2622 if (username_len > (BUFF_SIG_SIZE - PART_SIGNATURE_SHA256_SIZE))
2624 ERROR ("network plugin: Username too long: %s",
2625 se->data.client.username);
2629 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE,
2630 se->data.client.username, username_len);
2631 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE + username_len,
2632 in_buffer, in_buffer_size);
2634 /* Initialize the `ps' structure. */
2635 memset (&ps, 0, sizeof (ps));
2636 ps.head.type = htons (TYPE_SIGN_SHA256);
2637 ps.head.length = htons (PART_SIGNATURE_SHA256_SIZE + username_len);
2639 /* Calculate the hash value. */
2640 gcry_md_write (hd, buffer + PART_SIGNATURE_SHA256_SIZE,
2641 username_len + in_buffer_size);
2642 hash = gcry_md_read (hd, GCRY_MD_SHA256);
2645 ERROR ("network plugin: gcry_md_read failed.");
2649 memcpy (ps.hash, hash, sizeof (ps.hash));
2651 /* Add the header */
2654 BUFFER_ADD (&ps.head.type, sizeof (ps.head.type));
2655 BUFFER_ADD (&ps.head.length, sizeof (ps.head.length));
2656 BUFFER_ADD (ps.hash, sizeof (ps.hash));
2658 assert (buffer_offset == PART_SIGNATURE_SHA256_SIZE);
2663 buffer_offset = PART_SIGNATURE_SHA256_SIZE + username_len + in_buffer_size;
2664 networt_send_buffer_plain (se, buffer, buffer_offset);
2665 } /* }}} void networt_send_buffer_signed */
2667 static void networt_send_buffer_encrypted (sockent_t *se, /* {{{ */
2668 const char *in_buffer, size_t in_buffer_size)
2670 part_encryption_aes256_t pea;
2671 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2673 size_t buffer_offset;
2675 size_t username_len;
2677 gcry_cipher_hd_t cypher;
2679 /* Initialize the header fields */
2680 memset (&pea, 0, sizeof (pea));
2681 pea.head.type = htons (TYPE_ENCR_AES256);
2683 pea.username = se->data.client.username;
2685 username_len = strlen (pea.username);
2686 if ((PART_ENCRYPTION_AES256_SIZE + username_len) > BUFF_SIG_SIZE)
2688 ERROR ("network plugin: Username too long: %s", pea.username);
2692 buffer_size = PART_ENCRYPTION_AES256_SIZE + username_len + in_buffer_size;
2693 header_size = PART_ENCRYPTION_AES256_SIZE + username_len
2694 - sizeof (pea.hash);
2696 assert (buffer_size <= sizeof (buffer));
2697 DEBUG ("network plugin: networt_send_buffer_encrypted: "
2698 "buffer_size = %zu;", buffer_size);
2700 pea.head.length = htons ((uint16_t) (PART_ENCRYPTION_AES256_SIZE
2701 + username_len + in_buffer_size));
2702 pea.username_length = htons ((uint16_t) username_len);
2704 /* Chose a random initialization vector. */
2705 gcry_randomize ((void *) &pea.iv, sizeof (pea.iv), GCRY_STRONG_RANDOM);
2707 /* Create hash of the payload */
2708 gcry_md_hash_buffer (GCRY_MD_SHA1, pea.hash, in_buffer, in_buffer_size);
2710 /* Initialize the buffer */
2712 memset (buffer, 0, sizeof (buffer));
2715 BUFFER_ADD (&pea.head.type, sizeof (pea.head.type));
2716 BUFFER_ADD (&pea.head.length, sizeof (pea.head.length));
2717 BUFFER_ADD (&pea.username_length, sizeof (pea.username_length));
2718 BUFFER_ADD (pea.username, username_len);
2719 BUFFER_ADD (pea.iv, sizeof (pea.iv));
2720 assert (buffer_offset == header_size);
2721 BUFFER_ADD (pea.hash, sizeof (pea.hash));
2722 BUFFER_ADD (in_buffer, in_buffer_size);
2724 assert (buffer_offset == buffer_size);
2726 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
2727 se->data.client.password);
2731 /* Encrypt the buffer in-place */
2732 err = gcry_cipher_encrypt (cypher,
2733 buffer + header_size,
2734 buffer_size - header_size,
2735 /* in = */ NULL, /* in len = */ 0);
2738 ERROR ("network plugin: gcry_cipher_encrypt returned: %s",
2739 gcry_strerror (err));
2743 /* Send it out without further modifications */
2744 networt_send_buffer_plain (se, buffer, buffer_size);
2745 } /* }}} void networt_send_buffer_encrypted */
2747 #endif /* HAVE_LIBGCRYPT */
2749 static void network_send_buffer (char *buffer, size_t buffer_len) /* {{{ */
2753 DEBUG ("network plugin: network_send_buffer: buffer_len = %zu", buffer_len);
2755 for (se = sending_sockets; se != NULL; se = se->next)
2758 if (se->data.client.security_level == SECURITY_LEVEL_ENCRYPT)
2759 networt_send_buffer_encrypted (se, buffer, buffer_len);
2760 else if (se->data.client.security_level == SECURITY_LEVEL_SIGN)
2761 networt_send_buffer_signed (se, buffer, buffer_len);
2762 else /* if (se->data.client.security_level == SECURITY_LEVEL_NONE) */
2763 #endif /* HAVE_LIBGCRYPT */
2764 networt_send_buffer_plain (se, buffer, buffer_len);
2765 } /* for (sending_sockets) */
2766 } /* }}} void network_send_buffer */
2768 static int add_to_buffer (char *buffer, int buffer_size, /* {{{ */
2769 value_list_t *vl_def,
2770 const data_set_t *ds, const value_list_t *vl)
2772 char *buffer_orig = buffer;
2774 if (strcmp (vl_def->host, vl->host) != 0)
2776 if (write_part_string (&buffer, &buffer_size, TYPE_HOST,
2777 vl->host, strlen (vl->host)) != 0)
2779 sstrncpy (vl_def->host, vl->host, sizeof (vl_def->host));
2782 if (vl_def->time != vl->time)
2784 if (write_part_number (&buffer, &buffer_size, TYPE_TIME_HR,
2785 (uint64_t) vl->time))
2787 vl_def->time = vl->time;
2790 if (vl_def->interval != vl->interval)
2792 if (write_part_number (&buffer, &buffer_size, TYPE_INTERVAL_HR,
2793 (uint64_t) vl->interval))
2795 vl_def->interval = vl->interval;
2798 if (strcmp (vl_def->plugin, vl->plugin) != 0)
2800 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN,
2801 vl->plugin, strlen (vl->plugin)) != 0)
2803 sstrncpy (vl_def->plugin, vl->plugin, sizeof (vl_def->plugin));
2806 if (strcmp (vl_def->plugin_instance, vl->plugin_instance) != 0)
2808 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
2809 vl->plugin_instance,
2810 strlen (vl->plugin_instance)) != 0)
2812 sstrncpy (vl_def->plugin_instance, vl->plugin_instance, sizeof (vl_def->plugin_instance));
2815 if (strcmp (vl_def->type, vl->type) != 0)
2817 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
2818 vl->type, strlen (vl->type)) != 0)
2820 sstrncpy (vl_def->type, ds->type, sizeof (vl_def->type));
2823 if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
2825 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE_INSTANCE,
2827 strlen (vl->type_instance)) != 0)
2829 sstrncpy (vl_def->type_instance, vl->type_instance, sizeof (vl_def->type_instance));
2832 if (write_part_values (&buffer, &buffer_size, ds, vl) != 0)
2835 return (buffer - buffer_orig);
2836 } /* }}} int add_to_buffer */
2838 static void flush_buffer (void)
2840 DEBUG ("network plugin: flush_buffer: send_buffer_fill = %i",
2843 network_send_buffer (send_buffer, (size_t) send_buffer_fill);
2845 stats_octets_tx += ((uint64_t) send_buffer_fill);
2848 network_init_buffer ();
2851 static int network_write (const data_set_t *ds, const value_list_t *vl,
2852 user_data_t __attribute__((unused)) *user_data)
2856 if (!check_send_okay (vl))
2859 char name[6*DATA_MAX_NAME_LEN];
2860 FORMAT_VL (name, sizeof (name), vl);
2861 name[sizeof (name) - 1] = 0;
2862 DEBUG ("network plugin: network_write: "
2863 "NOT sending %s.", name);
2865 /* Counter is not protected by another lock and may be reached by
2866 * multiple threads */
2867 pthread_mutex_lock (&stats_lock);
2868 stats_values_not_sent++;
2869 pthread_mutex_unlock (&stats_lock);
2873 uc_meta_data_add_unsigned_int (vl,
2874 "network:time_sent", (uint64_t) vl->time);
2876 pthread_mutex_lock (&send_buffer_lock);
2878 status = add_to_buffer (send_buffer_ptr,
2879 network_config_packet_size - (send_buffer_fill + BUFF_SIG_SIZE),
2884 /* status == bytes added to the buffer */
2885 send_buffer_fill += status;
2886 send_buffer_ptr += status;
2888 stats_values_sent++;
2894 status = add_to_buffer (send_buffer_ptr,
2895 network_config_packet_size - (send_buffer_fill + BUFF_SIG_SIZE),
2901 send_buffer_fill += status;
2902 send_buffer_ptr += status;
2904 stats_values_sent++;
2910 ERROR ("network plugin: Unable to append to the "
2911 "buffer for some weird reason");
2913 else if ((network_config_packet_size - send_buffer_fill) < 15)
2918 pthread_mutex_unlock (&send_buffer_lock);
2920 return ((status < 0) ? -1 : 0);
2921 } /* int network_write */
2923 static int network_config_set_boolean (const oconfig_item_t *ci, /* {{{ */
2926 if ((ci->values_num != 1)
2927 || ((ci->values[0].type != OCONFIG_TYPE_BOOLEAN)
2928 && (ci->values[0].type != OCONFIG_TYPE_STRING)))
2930 ERROR ("network plugin: The `%s' config option needs "
2931 "exactly one boolean argument.", ci->key);
2935 if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
2937 if (ci->values[0].value.boolean)
2944 char *str = ci->values[0].value.string;
2948 else if (IS_FALSE (str))
2952 ERROR ("network plugin: Cannot parse string value `%s' of the `%s' "
2953 "option as boolean value.",
2960 } /* }}} int network_config_set_boolean */
2962 static int network_config_set_ttl (const oconfig_item_t *ci) /* {{{ */
2965 if ((ci->values_num != 1)
2966 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
2968 WARNING ("network plugin: The `TimeToLive' config option needs exactly "
2969 "one numeric argument.");
2973 tmp = (int) ci->values[0].value.number;
2974 if ((tmp > 0) && (tmp <= 255))
2975 network_config_ttl = tmp;
2978 } /* }}} int network_config_set_ttl */
2980 static int network_config_set_interface (const oconfig_item_t *ci, /* {{{ */
2983 if ((ci->values_num != 1)
2984 || (ci->values[0].type != OCONFIG_TYPE_STRING))
2986 WARNING ("network plugin: The `Interface' config option needs exactly "
2987 "one string argument.");
2991 if (interface == NULL)
2994 *interface = if_nametoindex (ci->values[0].value.string);
2997 } /* }}} int network_config_set_interface */
2999 static int network_config_set_buffer_size (const oconfig_item_t *ci) /* {{{ */
3002 if ((ci->values_num != 1)
3003 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
3005 WARNING ("network plugin: The `MaxPacketSize' config option needs exactly "
3006 "one numeric argument.");
3010 tmp = (int) ci->values[0].value.number;
3011 if ((tmp >= 1024) && (tmp <= 65535))
3012 network_config_packet_size = tmp;
3015 } /* }}} int network_config_set_buffer_size */
3018 static int network_config_set_string (const oconfig_item_t *ci, /* {{{ */
3022 if ((ci->values_num != 1)
3023 || (ci->values[0].type != OCONFIG_TYPE_STRING))
3025 WARNING ("network plugin: The `%s' config option needs exactly "
3026 "one string argument.", ci->key);
3030 tmp = strdup (ci->values[0].value.string);
3034 sfree (*ret_string);
3038 } /* }}} int network_config_set_string */
3039 #endif /* HAVE_LIBGCRYPT */
3042 static int network_config_set_security_level (oconfig_item_t *ci, /* {{{ */
3046 if ((ci->values_num != 1)
3047 || (ci->values[0].type != OCONFIG_TYPE_STRING))
3049 WARNING ("network plugin: The `SecurityLevel' config option needs exactly "
3050 "one string argument.");
3054 str = ci->values[0].value.string;
3055 if (strcasecmp ("Encrypt", str) == 0)
3056 *retval = SECURITY_LEVEL_ENCRYPT;
3057 else if (strcasecmp ("Sign", str) == 0)
3058 *retval = SECURITY_LEVEL_SIGN;
3059 else if (strcasecmp ("None", str) == 0)
3060 *retval = SECURITY_LEVEL_NONE;
3063 WARNING ("network plugin: Unknown security level: %s.", str);
3068 } /* }}} int network_config_set_security_level */
3069 #endif /* HAVE_LIBGCRYPT */
3071 static int network_config_add_listen (const oconfig_item_t *ci) /* {{{ */
3077 if ((ci->values_num < 1) || (ci->values_num > 2)
3078 || (ci->values[0].type != OCONFIG_TYPE_STRING)
3079 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
3081 ERROR ("network plugin: The `%s' config option needs "
3082 "one or two string arguments.", ci->key);
3086 se = sockent_create (SOCKENT_TYPE_SERVER);
3089 ERROR ("network plugin: sockent_create failed.");
3093 se->node = strdup (ci->values[0].value.string);
3094 if (ci->values_num >= 2)
3095 se->service = strdup (ci->values[1].value.string);
3097 for (i = 0; i < ci->children_num; i++)
3099 oconfig_item_t *child = ci->children + i;
3102 if (strcasecmp ("AuthFile", child->key) == 0)
3103 network_config_set_string (child, &se->data.server.auth_file);
3104 else if (strcasecmp ("SecurityLevel", child->key) == 0)
3105 network_config_set_security_level (child,
3106 &se->data.server.security_level);
3108 #endif /* HAVE_LIBGCRYPT */
3109 if (strcasecmp ("Interface", child->key) == 0)
3110 network_config_set_interface (child,
3114 WARNING ("network plugin: Option `%s' is not allowed here.",
3120 if ((se->data.server.security_level > SECURITY_LEVEL_NONE)
3121 && (se->data.server.auth_file == NULL))
3123 ERROR ("network plugin: A security level higher than `none' was "
3124 "requested, but no AuthFile option was given. Cowardly refusing to "
3125 "open this socket!");
3126 sockent_destroy (se);
3129 #endif /* HAVE_LIBGCRYPT */
3131 status = sockent_init_crypto (se);
3134 ERROR ("network plugin: network_config_add_listen: sockent_init_crypto() failed.");
3135 sockent_destroy (se);
3139 status = sockent_server_listen (se);
3142 ERROR ("network plugin: network_config_add_server: sockent_server_listen failed.");
3143 sockent_destroy (se);
3147 status = sockent_add (se);
3150 ERROR ("network plugin: network_config_add_listen: sockent_add failed.");
3151 sockent_destroy (se);
3156 } /* }}} int network_config_add_listen */
3158 static int network_config_add_server (const oconfig_item_t *ci) /* {{{ */
3164 if ((ci->values_num < 1) || (ci->values_num > 2)
3165 || (ci->values[0].type != OCONFIG_TYPE_STRING)
3166 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
3168 ERROR ("network plugin: The `%s' config option needs "
3169 "one or two string arguments.", ci->key);
3173 se = sockent_create (SOCKENT_TYPE_CLIENT);
3176 ERROR ("network plugin: sockent_create failed.");
3180 se->node = strdup (ci->values[0].value.string);
3181 if (ci->values_num >= 2)
3182 se->service = strdup (ci->values[1].value.string);
3184 for (i = 0; i < ci->children_num; i++)
3186 oconfig_item_t *child = ci->children + i;
3189 if (strcasecmp ("Username", child->key) == 0)
3190 network_config_set_string (child, &se->data.client.username);
3191 else if (strcasecmp ("Password", child->key) == 0)
3192 network_config_set_string (child, &se->data.client.password);
3193 else if (strcasecmp ("SecurityLevel", child->key) == 0)
3194 network_config_set_security_level (child,
3195 &se->data.client.security_level);
3197 #endif /* HAVE_LIBGCRYPT */
3198 if (strcasecmp ("Interface", child->key) == 0)
3199 network_config_set_interface (child,
3203 WARNING ("network plugin: Option `%s' is not allowed here.",
3209 if ((se->data.client.security_level > SECURITY_LEVEL_NONE)
3210 && ((se->data.client.username == NULL)
3211 || (se->data.client.password == NULL)))
3213 ERROR ("network plugin: A security level higher than `none' was "
3214 "requested, but no Username or Password option was given. "
3215 "Cowardly refusing to open this socket!");
3216 sockent_destroy (se);
3219 #endif /* HAVE_LIBGCRYPT */
3221 status = sockent_init_crypto (se);
3224 ERROR ("network plugin: network_config_add_server: sockent_init_crypto() failed.");
3225 sockent_destroy (se);
3229 /* No call to sockent_client_connect() here -- it is called from
3230 * networt_send_buffer_plain(). */
3232 status = sockent_add (se);
3235 ERROR ("network plugin: network_config_add_server: sockent_add failed.");
3236 sockent_destroy (se);
3241 } /* }}} int network_config_add_server */
3243 static int network_config (oconfig_item_t *ci) /* {{{ */
3247 for (i = 0; i < ci->children_num; i++)
3249 oconfig_item_t *child = ci->children + i;
3251 if (strcasecmp ("Listen", child->key) == 0)
3252 network_config_add_listen (child);
3253 else if (strcasecmp ("Server", child->key) == 0)
3254 network_config_add_server (child);
3255 else if (strcasecmp ("TimeToLive", child->key) == 0)
3256 network_config_set_ttl (child);
3257 else if (strcasecmp ("MaxPacketSize", child->key) == 0)
3258 network_config_set_buffer_size (child);
3259 else if (strcasecmp ("Forward", child->key) == 0)
3260 network_config_set_boolean (child, &network_config_forward);
3261 else if (strcasecmp ("ReportStats", child->key) == 0)
3262 network_config_set_boolean (child, &network_config_stats);
3265 WARNING ("network plugin: Option `%s' is not allowed here.",
3271 } /* }}} int network_config */
3273 static int network_notification (const notification_t *n,
3274 user_data_t __attribute__((unused)) *user_data)
3276 char buffer[network_config_packet_size];
3277 char *buffer_ptr = buffer;
3278 int buffer_free = sizeof (buffer);
3281 if (!check_send_notify_okay (n))
3284 memset (buffer, 0, sizeof (buffer));
3286 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_TIME_HR,
3287 (uint64_t) n->time);
3291 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_SEVERITY,
3292 (uint64_t) n->severity);
3296 if (strlen (n->host) > 0)
3298 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_HOST,
3299 n->host, strlen (n->host));
3304 if (strlen (n->plugin) > 0)
3306 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_PLUGIN,
3307 n->plugin, strlen (n->plugin));
3312 if (strlen (n->plugin_instance) > 0)
3314 status = write_part_string (&buffer_ptr, &buffer_free,
3315 TYPE_PLUGIN_INSTANCE,
3316 n->plugin_instance, strlen (n->plugin_instance));
3321 if (strlen (n->type) > 0)
3323 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE,
3324 n->type, strlen (n->type));
3329 if (strlen (n->type_instance) > 0)
3331 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE_INSTANCE,
3332 n->type_instance, strlen (n->type_instance));
3337 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_MESSAGE,
3338 n->message, strlen (n->message));
3342 network_send_buffer (buffer, sizeof (buffer) - buffer_free);
3345 } /* int network_notification */
3347 static int network_shutdown (void)
3353 /* Kill the listening thread */
3354 if (receive_thread_running != 0)
3356 INFO ("network plugin: Stopping receive thread.");
3357 pthread_kill (receive_thread_id, SIGTERM);
3358 pthread_join (receive_thread_id, NULL /* no return value */);
3359 memset (&receive_thread_id, 0, sizeof (receive_thread_id));
3360 receive_thread_running = 0;
3363 /* Shutdown the dispatching thread */
3364 if (dispatch_thread_running != 0)
3366 INFO ("network plugin: Stopping dispatch thread.");
3367 pthread_mutex_lock (&receive_list_lock);
3368 pthread_cond_broadcast (&receive_list_cond);
3369 pthread_mutex_unlock (&receive_list_lock);
3370 pthread_join (dispatch_thread_id, /* ret = */ NULL);
3371 dispatch_thread_running = 0;
3374 sockent_destroy (listen_sockets);
3376 if (send_buffer_fill > 0)
3379 sfree (send_buffer);
3381 for (se = sending_sockets; se != NULL; se = se->next)
3382 sockent_client_disconnect (se);
3383 sockent_destroy (sending_sockets);
3385 plugin_unregister_config ("network");
3386 plugin_unregister_init ("network");
3387 plugin_unregister_write ("network");
3388 plugin_unregister_shutdown ("network");
3391 } /* int network_shutdown */
3393 static int network_stats_read (void) /* {{{ */
3395 derive_t copy_octets_rx;
3396 derive_t copy_octets_tx;
3397 derive_t copy_packets_rx;
3398 derive_t copy_packets_tx;
3399 derive_t copy_values_dispatched;
3400 derive_t copy_values_not_dispatched;
3401 derive_t copy_values_sent;
3402 derive_t copy_values_not_sent;
3403 derive_t copy_receive_list_length;
3404 value_list_t vl = VALUE_LIST_INIT;
3407 copy_octets_rx = stats_octets_rx;
3408 copy_octets_tx = stats_octets_tx;
3409 copy_packets_rx = stats_packets_rx;
3410 copy_packets_tx = stats_packets_tx;
3411 copy_values_dispatched = stats_values_dispatched;
3412 copy_values_not_dispatched = stats_values_not_dispatched;
3413 copy_values_sent = stats_values_sent;
3414 copy_values_not_sent = stats_values_not_sent;
3415 copy_receive_list_length = receive_list_length;
3417 /* Initialize `vl' */
3421 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
3422 sstrncpy (vl.plugin, "network", sizeof (vl.plugin));
3424 /* Octets received / sent */
3425 vl.values[0].derive = (derive_t) copy_octets_rx;
3426 vl.values[1].derive = (derive_t) copy_octets_tx;
3427 sstrncpy (vl.type, "if_octets", sizeof (vl.type));
3428 plugin_dispatch_values (&vl);
3430 /* Packets received / send */
3431 vl.values[0].derive = (derive_t) copy_packets_rx;
3432 vl.values[1].derive = (derive_t) copy_packets_tx;
3433 sstrncpy (vl.type, "if_packets", sizeof (vl.type));
3434 plugin_dispatch_values (&vl);
3436 /* Values (not) dispatched and (not) send */
3437 sstrncpy (vl.type, "total_values", sizeof (vl.type));
3440 vl.values[0].derive = (derive_t) copy_values_dispatched;
3441 sstrncpy (vl.type_instance, "dispatch-accepted",
3442 sizeof (vl.type_instance));
3443 plugin_dispatch_values (&vl);
3445 vl.values[0].derive = (derive_t) copy_values_not_dispatched;
3446 sstrncpy (vl.type_instance, "dispatch-rejected",
3447 sizeof (vl.type_instance));
3448 plugin_dispatch_values (&vl);
3450 vl.values[0].derive = (derive_t) copy_values_sent;
3451 sstrncpy (vl.type_instance, "send-accepted",
3452 sizeof (vl.type_instance));
3453 plugin_dispatch_values (&vl);
3455 vl.values[0].derive = (derive_t) copy_values_not_sent;
3456 sstrncpy (vl.type_instance, "send-rejected",
3457 sizeof (vl.type_instance));
3458 plugin_dispatch_values (&vl);
3460 /* Receive queue length */
3461 vl.values[0].gauge = (gauge_t) copy_receive_list_length;
3462 sstrncpy (vl.type, "queue_length", sizeof (vl.type));
3463 vl.type_instance[0] = 0;
3464 plugin_dispatch_values (&vl);
3467 } /* }}} int network_stats_read */
3469 static int network_init (void)
3471 static _Bool have_init = 0;
3473 /* Check if we were already initialized. If so, just return - there's
3474 * nothing more to do (for now, that is). */
3480 network_init_gcrypt ();
3483 if (network_config_stats != 0)
3484 plugin_register_read ("network", network_stats_read);
3486 plugin_register_shutdown ("network", network_shutdown);
3488 send_buffer = malloc (network_config_packet_size);
3489 if (send_buffer == NULL)
3491 ERROR ("network plugin: malloc failed.");
3494 network_init_buffer ();
3496 /* setup socket(s) and so on */
3497 if (sending_sockets != NULL)
3499 plugin_register_write ("network", network_write,
3500 /* user_data = */ NULL);
3501 plugin_register_notification ("network", network_notification,
3502 /* user_data = */ NULL);
3505 /* If no threads need to be started, return here. */
3506 if ((listen_sockets_num == 0)
3507 || ((dispatch_thread_running != 0)
3508 && (receive_thread_running != 0)))
3511 if (dispatch_thread_running == 0)
3514 status = plugin_thread_create (&dispatch_thread_id,
3515 NULL /* no attributes */,
3517 NULL /* no argument */);
3521 ERROR ("network: pthread_create failed: %s",
3522 sstrerror (errno, errbuf,
3527 dispatch_thread_running = 1;
3531 if (receive_thread_running == 0)
3534 status = plugin_thread_create (&receive_thread_id,
3535 NULL /* no attributes */,
3537 NULL /* no argument */);
3541 ERROR ("network: pthread_create failed: %s",
3542 sstrerror (errno, errbuf,
3547 receive_thread_running = 1;
3552 } /* int network_init */
3555 * The flush option of the network plugin cannot flush individual identifiers.
3556 * All the values are added to a buffer and sent when the buffer is full, the
3557 * requested value may or may not be in there, it's not worth finding out. We
3558 * just send the buffer if `flush' is called - if the requested value was in
3559 * there, good. If not, well, then there is nothing to flush.. -octo
3561 static int network_flush (__attribute__((unused)) cdtime_t timeout,
3562 __attribute__((unused)) const char *identifier,
3563 __attribute__((unused)) user_data_t *user_data)
3565 pthread_mutex_lock (&send_buffer_lock);
3567 if (send_buffer_fill > 0)
3570 pthread_mutex_unlock (&send_buffer_lock);
3573 } /* int network_flush */
3575 void module_register (void)
3577 plugin_register_complex_config ("network", network_config);
3578 plugin_register_init ("network", network_init);
3579 plugin_register_flush ("network", network_flush,
3580 /* user_data = */ NULL);
3581 } /* void module_register */
3583 /* vim: set fdm=marker : */