eb32ad15081e811079fe864a1d999e0d09a4d77a
1 /**
2 * collectd - src/network.c
3 * Copyright (C) 2005-2009 Florian octo Forster
4 * Copyright (C) 2009 Aman Gupta
5 *
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
9 * applicable.
10 *
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.
15 *
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
19 *
20 * Authors:
21 * Florian octo Forster <octo at verplant.org>
22 * Aman Gupta <aman at tmm1.net>
23 **/
25 #define _BSD_SOURCE /* For struct ip_mreq */
27 #include "collectd.h"
28 #include "plugin.h"
29 #include "common.h"
30 #include "configfile.h"
31 #include "utils_fbhash.h"
32 #include "utils_avltree.h"
33 #include "utils_cache.h"
35 #include "network.h"
37 #if HAVE_PTHREAD_H
38 # include <pthread.h>
39 #endif
40 #if HAVE_SYS_SOCKET_H
41 # include <sys/socket.h>
42 #endif
43 #if HAVE_NETDB_H
44 # include <netdb.h>
45 #endif
46 #if HAVE_NETINET_IN_H
47 # include <netinet/in.h>
48 #endif
49 #if HAVE_ARPA_INET_H
50 # include <arpa/inet.h>
51 #endif
52 #if HAVE_POLL_H
53 # include <poll.h>
54 #endif
55 #if HAVE_NET_IF_H
56 # include <net/if.h>
57 #endif
59 #if HAVE_LIBGCRYPT
60 # include <gcrypt.h>
61 GCRY_THREAD_OPTION_PTHREAD_IMPL;
62 #endif
64 #ifndef IPV6_ADD_MEMBERSHIP
65 # ifdef IPV6_JOIN_GROUP
66 # define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
67 # else
68 # error "Neither IP_ADD_MEMBERSHIP nor IPV6_JOIN_GROUP is defined"
69 # endif
70 #endif /* !IP_ADD_MEMBERSHIP */
72 /*
73 * Maximum size required for encryption / signing:
74 *
75 * 42 bytes for the encryption header
76 * + 64 bytes for the username
77 * -----------
78 * = 106 bytes
79 */
80 #define BUFF_SIG_SIZE 106
82 /*
83 * Private data types
84 */
85 #define SECURITY_LEVEL_NONE 0
86 #if HAVE_LIBGCRYPT
87 # define SECURITY_LEVEL_SIGN 1
88 # define SECURITY_LEVEL_ENCRYPT 2
89 #endif
90 struct sockent_client
91 {
92 int fd;
93 struct sockaddr_storage *addr;
94 socklen_t addrlen;
95 #if HAVE_LIBGCRYPT
96 int security_level;
97 char *username;
98 char *password;
99 gcry_cipher_hd_t cypher;
100 unsigned char password_hash[32];
101 #endif
102 };
104 struct sockent_server
105 {
106 int *fd;
107 size_t fd_num;
108 #if HAVE_LIBGCRYPT
109 int security_level;
110 char *auth_file;
111 fbhash_t *userdb;
112 gcry_cipher_hd_t cypher;
113 #endif
114 };
116 typedef struct sockent
117 {
118 #define SOCKENT_TYPE_CLIENT 1
119 #define SOCKENT_TYPE_SERVER 2
120 int type;
122 char *node;
123 char *service;
124 int interface;
126 union
127 {
128 struct sockent_client client;
129 struct sockent_server server;
130 } data;
132 struct sockent *next;
133 } sockent_t;
135 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
136 * 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
137 * +-------+-----------------------+-------------------------------+
138 * ! Ver. ! ! Length !
139 * +-------+-----------------------+-------------------------------+
140 */
141 struct part_header_s
142 {
143 uint16_t type;
144 uint16_t length;
145 };
146 typedef struct part_header_s part_header_t;
148 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
149 * 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
150 * +-------------------------------+-------------------------------+
151 * ! Type ! Length !
152 * +-------------------------------+-------------------------------+
153 * : (Length - 4) Bytes :
154 * +---------------------------------------------------------------+
155 */
156 struct part_string_s
157 {
158 part_header_t *head;
159 char *value;
160 };
161 typedef struct part_string_s part_string_t;
163 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
164 * 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
165 * +-------------------------------+-------------------------------+
166 * ! Type ! Length !
167 * +-------------------------------+-------------------------------+
168 * : (Length - 4 == 2 || 4 || 8) Bytes :
169 * +---------------------------------------------------------------+
170 */
171 struct part_number_s
172 {
173 part_header_t *head;
174 uint64_t *value;
175 };
176 typedef struct part_number_s part_number_t;
178 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
179 * 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
180 * +-------------------------------+-------------------------------+
181 * ! Type ! Length !
182 * +-------------------------------+---------------+---------------+
183 * ! Num of values ! Type0 ! Type1 !
184 * +-------------------------------+---------------+---------------+
185 * ! Value0 !
186 * ! !
187 * +---------------------------------------------------------------+
188 * ! Value1 !
189 * ! !
190 * +---------------------------------------------------------------+
191 */
192 struct part_values_s
193 {
194 part_header_t *head;
195 uint16_t *num_values;
196 uint8_t *values_types;
197 value_t *values;
198 };
199 typedef struct part_values_s part_values_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 * +-------------------------------+-------------------------------+
204 * ! Type ! Length !
205 * +-------------------------------+-------------------------------+
206 * ! Hash (Bits 0 - 31) !
207 * : : :
208 * ! Hash (Bits 224 - 255) !
209 * +---------------------------------------------------------------+
210 */
211 /* Minimum size */
212 #define PART_SIGNATURE_SHA256_SIZE 36
213 struct part_signature_sha256_s
214 {
215 part_header_t head;
216 unsigned char hash[32];
217 char *username;
218 };
219 typedef struct part_signature_sha256_s part_signature_sha256_t;
221 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
222 * 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
223 * +-------------------------------+-------------------------------+
224 * ! Type ! Length !
225 * +-------------------------------+-------------------------------+
226 * ! Original length ! Padding (0 - 15 bytes) !
227 * +-------------------------------+-------------------------------+
228 * ! Hash (Bits 0 - 31) !
229 * : : :
230 * ! Hash (Bits 128 - 159) !
231 * +---------------------------------------------------------------+
232 */
233 /* Minimum size */
234 #define PART_ENCRYPTION_AES256_SIZE 42
235 struct part_encryption_aes256_s
236 {
237 part_header_t head;
238 uint16_t username_length;
239 char *username;
240 unsigned char iv[16];
241 /* <encrypted> */
242 unsigned char hash[20];
243 /* <payload /> */
244 /* </encrypted> */
245 };
246 typedef struct part_encryption_aes256_s part_encryption_aes256_t;
248 struct receive_list_entry_s
249 {
250 char *data;
251 int data_len;
252 int fd;
253 struct receive_list_entry_s *next;
254 };
255 typedef struct receive_list_entry_s receive_list_entry_t;
257 /*
258 * Private variables
259 */
260 static int network_config_ttl = 0;
261 static size_t network_config_packet_size = 1452;
262 static int network_config_forward = 0;
263 static int network_config_stats = 0;
265 static sockent_t *sending_sockets = NULL;
267 static receive_list_entry_t *receive_list_head = NULL;
268 static receive_list_entry_t *receive_list_tail = NULL;
269 static pthread_mutex_t receive_list_lock = PTHREAD_MUTEX_INITIALIZER;
270 static pthread_cond_t receive_list_cond = PTHREAD_COND_INITIALIZER;
271 static uint64_t receive_list_length = 0;
273 static sockent_t *listen_sockets = NULL;
274 static struct pollfd *listen_sockets_pollfd = NULL;
275 static size_t listen_sockets_num = 0;
277 /* The receive and dispatch threads will run as long as `listen_loop' is set to
278 * zero. */
279 static int listen_loop = 0;
280 static int receive_thread_running = 0;
281 static pthread_t receive_thread_id;
282 static int dispatch_thread_running = 0;
283 static pthread_t dispatch_thread_id;
285 /* Buffer in which to-be-sent network packets are constructed. */
286 static char *send_buffer;
287 static char *send_buffer_ptr;
288 static int send_buffer_fill;
289 static value_list_t send_buffer_vl = VALUE_LIST_STATIC;
290 static pthread_mutex_t send_buffer_lock = PTHREAD_MUTEX_INITIALIZER;
292 /* XXX: These counters are incremented from one place only. The spot in which
293 * the values are incremented is either only reachable by one thread (the
294 * dispatch thread, for example) or locked by some lock (send_buffer_lock for
295 * example). Only if neither is true, the stats_lock is acquired. The counters
296 * are always read without holding a lock in the hope that writing 8 bytes to
297 * memory is an atomic operation. */
298 static uint64_t stats_octets_rx = 0;
299 static uint64_t stats_octets_tx = 0;
300 static uint64_t stats_packets_rx = 0;
301 static uint64_t stats_packets_tx = 0;
302 static uint64_t stats_values_dispatched = 0;
303 static uint64_t stats_values_not_dispatched = 0;
304 static uint64_t stats_values_sent = 0;
305 static uint64_t stats_values_not_sent = 0;
306 static pthread_mutex_t stats_lock = PTHREAD_MUTEX_INITIALIZER;
308 /*
309 * Private functions
310 */
311 static _Bool check_receive_okay (const value_list_t *vl) /* {{{ */
312 {
313 uint64_t time_sent = 0;
314 int status;
316 status = uc_meta_data_get_unsigned_int (vl,
317 "network:time_sent", &time_sent);
319 /* This is a value we already sent. Don't allow it to be received again in
320 * order to avoid looping. */
321 if ((status == 0) && (time_sent >= ((uint64_t) vl->time)))
322 return (0);
324 return (1);
325 } /* }}} _Bool check_receive_okay */
327 static _Bool check_send_okay (const value_list_t *vl) /* {{{ */
328 {
329 _Bool received = 0;
330 int status;
332 if (network_config_forward != 0)
333 return (1);
335 if (vl->meta == NULL)
336 return (1);
338 status = meta_data_get_boolean (vl->meta, "network:received", &received);
339 if (status == -ENOENT)
340 return (1);
341 else if (status != 0)
342 {
343 ERROR ("network plugin: check_send_okay: meta_data_get_boolean failed "
344 "with status %i.", status);
345 return (1);
346 }
348 /* By default, only *send* value lists that were not *received* by the
349 * network plugin. */
350 return (!received);
351 } /* }}} _Bool check_send_okay */
353 static int network_dispatch_values (value_list_t *vl, /* {{{ */
354 const char *username)
355 {
356 int status;
358 if ((vl->time <= 0)
359 || (strlen (vl->host) <= 0)
360 || (strlen (vl->plugin) <= 0)
361 || (strlen (vl->type) <= 0))
362 return (-EINVAL);
364 if (!check_receive_okay (vl))
365 {
366 #if COLLECT_DEBUG
367 char name[6*DATA_MAX_NAME_LEN];
368 FORMAT_VL (name, sizeof (name), vl);
369 name[sizeof (name) - 1] = 0;
370 DEBUG ("network plugin: network_dispatch_values: "
371 "NOT dispatching %s.", name);
372 #endif
373 stats_values_not_dispatched++;
374 return (0);
375 }
377 assert (vl->meta == NULL);
379 vl->meta = meta_data_create ();
380 if (vl->meta == NULL)
381 {
382 ERROR ("network plugin: meta_data_create failed.");
383 return (-ENOMEM);
384 }
386 status = meta_data_add_boolean (vl->meta, "network:received", 1);
387 if (status != 0)
388 {
389 ERROR ("network plugin: meta_data_add_boolean failed.");
390 meta_data_destroy (vl->meta);
391 vl->meta = NULL;
392 return (status);
393 }
395 if (username != NULL)
396 {
397 status = meta_data_add_string (vl->meta, "network:username", username);
398 if (status != 0)
399 {
400 ERROR ("network plugin: meta_data_add_string failed.");
401 meta_data_destroy (vl->meta);
402 vl->meta = NULL;
403 return (status);
404 }
405 }
407 plugin_dispatch_values (vl);
408 stats_values_dispatched++;
410 meta_data_destroy (vl->meta);
411 vl->meta = NULL;
413 return (0);
414 } /* }}} int network_dispatch_values */
416 #if HAVE_LIBGCRYPT
417 static gcry_cipher_hd_t network_get_aes256_cypher (sockent_t *se, /* {{{ */
418 const void *iv, size_t iv_size, const char *username)
419 {
420 gcry_error_t err;
421 gcry_cipher_hd_t *cyper_ptr;
422 unsigned char password_hash[32];
424 if (se->type == SOCKENT_TYPE_CLIENT)
425 {
426 cyper_ptr = &se->data.client.cypher;
427 memcpy (password_hash, se->data.client.password_hash,
428 sizeof (password_hash));
429 }
430 else
431 {
432 char *secret;
434 cyper_ptr = &se->data.server.cypher;
436 if (username == NULL)
437 return (NULL);
439 secret = fbh_get (se->data.server.userdb, username);
440 if (secret == NULL)
441 return (NULL);
443 gcry_md_hash_buffer (GCRY_MD_SHA256,
444 password_hash,
445 secret, strlen (secret));
447 sfree (secret);
448 }
450 if (*cyper_ptr == NULL)
451 {
452 err = gcry_cipher_open (cyper_ptr,
453 GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_OFB, /* flags = */ 0);
454 if (err != 0)
455 {
456 ERROR ("network plugin: gcry_cipher_open returned: %s",
457 gcry_strerror (err));
458 *cyper_ptr = NULL;
459 return (NULL);
460 }
461 }
462 else
463 {
464 gcry_cipher_reset (*cyper_ptr);
465 }
466 assert (*cyper_ptr != NULL);
468 err = gcry_cipher_setkey (*cyper_ptr,
469 password_hash, sizeof (password_hash));
470 if (err != 0)
471 {
472 ERROR ("network plugin: gcry_cipher_setkey returned: %s",
473 gcry_strerror (err));
474 gcry_cipher_close (*cyper_ptr);
475 *cyper_ptr = NULL;
476 return (NULL);
477 }
479 err = gcry_cipher_setiv (*cyper_ptr, iv, iv_size);
480 if (err != 0)
481 {
482 ERROR ("network plugin: gcry_cipher_setkey returned: %s",
483 gcry_strerror (err));
484 gcry_cipher_close (*cyper_ptr);
485 *cyper_ptr = NULL;
486 return (NULL);
487 }
489 return (*cyper_ptr);
490 } /* }}} int network_get_aes256_cypher */
491 #endif /* HAVE_LIBGCRYPT */
493 static int write_part_values (char **ret_buffer, int *ret_buffer_len,
494 const data_set_t *ds, const value_list_t *vl)
495 {
496 char *packet_ptr;
497 int packet_len;
498 int num_values;
500 part_header_t pkg_ph;
501 uint16_t pkg_num_values;
502 uint8_t *pkg_values_types;
503 value_t *pkg_values;
505 int offset;
506 int i;
508 num_values = vl->values_len;
509 packet_len = sizeof (part_header_t) + sizeof (uint16_t)
510 + (num_values * sizeof (uint8_t))
511 + (num_values * sizeof (value_t));
513 if (*ret_buffer_len < packet_len)
514 return (-1);
516 pkg_values_types = (uint8_t *) malloc (num_values * sizeof (uint8_t));
517 if (pkg_values_types == NULL)
518 {
519 ERROR ("network plugin: write_part_values: malloc failed.");
520 return (-1);
521 }
523 pkg_values = (value_t *) malloc (num_values * sizeof (value_t));
524 if (pkg_values == NULL)
525 {
526 free (pkg_values_types);
527 ERROR ("network plugin: write_part_values: malloc failed.");
528 return (-1);
529 }
531 pkg_ph.type = htons (TYPE_VALUES);
532 pkg_ph.length = htons (packet_len);
534 pkg_num_values = htons ((uint16_t) vl->values_len);
536 for (i = 0; i < num_values; i++)
537 {
538 pkg_values_types[i] = (uint8_t) ds->ds[i].type;
539 switch (ds->ds[i].type)
540 {
541 case DS_TYPE_COUNTER:
542 pkg_values[i].counter = htonll (vl->values[i].counter);
543 break;
545 case DS_TYPE_GAUGE:
546 pkg_values[i].gauge = htond (vl->values[i].gauge);
547 break;
549 case DS_TYPE_DERIVE:
550 pkg_values[i].derive = htonll (vl->values[i].derive);
551 break;
553 case DS_TYPE_ABSOLUTE:
554 pkg_values[i].absolute = htonll (vl->values[i].absolute);
555 break;
557 default:
558 free (pkg_values_types);
559 free (pkg_values);
560 ERROR ("network plugin: write_part_values: "
561 "Unknown data source type: %i",
562 ds->ds[i].type);
563 return (-1);
564 } /* switch (ds->ds[i].type) */
565 } /* for (num_values) */
567 /*
568 * Use `memcpy' to write everything to the buffer, because the pointer
569 * may be unaligned and some architectures, such as SPARC, can't handle
570 * that.
571 */
572 packet_ptr = *ret_buffer;
573 offset = 0;
574 memcpy (packet_ptr + offset, &pkg_ph, sizeof (pkg_ph));
575 offset += sizeof (pkg_ph);
576 memcpy (packet_ptr + offset, &pkg_num_values, sizeof (pkg_num_values));
577 offset += sizeof (pkg_num_values);
578 memcpy (packet_ptr + offset, pkg_values_types, num_values * sizeof (uint8_t));
579 offset += num_values * sizeof (uint8_t);
580 memcpy (packet_ptr + offset, pkg_values, num_values * sizeof (value_t));
581 offset += num_values * sizeof (value_t);
583 assert (offset == packet_len);
585 *ret_buffer = packet_ptr + packet_len;
586 *ret_buffer_len -= packet_len;
588 free (pkg_values_types);
589 free (pkg_values);
591 return (0);
592 } /* int write_part_values */
594 static int write_part_number (char **ret_buffer, int *ret_buffer_len,
595 int type, uint64_t value)
596 {
597 char *packet_ptr;
598 int packet_len;
600 part_header_t pkg_head;
601 uint64_t pkg_value;
603 int offset;
605 packet_len = sizeof (pkg_head) + sizeof (pkg_value);
607 if (*ret_buffer_len < packet_len)
608 return (-1);
610 pkg_head.type = htons (type);
611 pkg_head.length = htons (packet_len);
612 pkg_value = htonll (value);
614 packet_ptr = *ret_buffer;
615 offset = 0;
616 memcpy (packet_ptr + offset, &pkg_head, sizeof (pkg_head));
617 offset += sizeof (pkg_head);
618 memcpy (packet_ptr + offset, &pkg_value, sizeof (pkg_value));
619 offset += sizeof (pkg_value);
621 assert (offset == packet_len);
623 *ret_buffer = packet_ptr + packet_len;
624 *ret_buffer_len -= packet_len;
626 return (0);
627 } /* int write_part_number */
629 static int write_part_string (char **ret_buffer, int *ret_buffer_len,
630 int type, const char *str, int str_len)
631 {
632 char *buffer;
633 int buffer_len;
635 uint16_t pkg_type;
636 uint16_t pkg_length;
638 int offset;
640 buffer_len = 2 * sizeof (uint16_t) + str_len + 1;
641 if (*ret_buffer_len < buffer_len)
642 return (-1);
644 pkg_type = htons (type);
645 pkg_length = htons (buffer_len);
647 buffer = *ret_buffer;
648 offset = 0;
649 memcpy (buffer + offset, (void *) &pkg_type, sizeof (pkg_type));
650 offset += sizeof (pkg_type);
651 memcpy (buffer + offset, (void *) &pkg_length, sizeof (pkg_length));
652 offset += sizeof (pkg_length);
653 memcpy (buffer + offset, str, str_len);
654 offset += str_len;
655 memset (buffer + offset, '\0', 1);
656 offset += 1;
658 assert (offset == buffer_len);
660 *ret_buffer = buffer + buffer_len;
661 *ret_buffer_len -= buffer_len;
663 return (0);
664 } /* int write_part_string */
666 static int parse_part_values (void **ret_buffer, size_t *ret_buffer_len,
667 value_t **ret_values, int *ret_num_values)
668 {
669 char *buffer = *ret_buffer;
670 size_t buffer_len = *ret_buffer_len;
672 uint16_t tmp16;
673 size_t exp_size;
674 int i;
676 uint16_t pkg_length;
677 uint16_t pkg_type;
678 uint16_t pkg_numval;
680 uint8_t *pkg_types;
681 value_t *pkg_values;
683 if (buffer_len < 15)
684 {
685 NOTICE ("network plugin: packet is too short: "
686 "buffer_len = %zu", buffer_len);
687 return (-1);
688 }
690 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
691 buffer += sizeof (tmp16);
692 pkg_type = ntohs (tmp16);
694 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
695 buffer += sizeof (tmp16);
696 pkg_length = ntohs (tmp16);
698 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
699 buffer += sizeof (tmp16);
700 pkg_numval = ntohs (tmp16);
702 assert (pkg_type == TYPE_VALUES);
704 exp_size = 3 * sizeof (uint16_t)
705 + pkg_numval * (sizeof (uint8_t) + sizeof (value_t));
706 if ((buffer_len < 0) || (buffer_len < exp_size))
707 {
708 WARNING ("network plugin: parse_part_values: "
709 "Packet too short: "
710 "Chunk of size %zu expected, "
711 "but buffer has only %zu bytes left.",
712 exp_size, buffer_len);
713 return (-1);
714 }
716 if (pkg_length != exp_size)
717 {
718 WARNING ("network plugin: parse_part_values: "
719 "Length and number of values "
720 "in the packet don't match.");
721 return (-1);
722 }
724 pkg_types = (uint8_t *) malloc (pkg_numval * sizeof (uint8_t));
725 pkg_values = (value_t *) malloc (pkg_numval * sizeof (value_t));
726 if ((pkg_types == NULL) || (pkg_values == NULL))
727 {
728 sfree (pkg_types);
729 sfree (pkg_values);
730 ERROR ("network plugin: parse_part_values: malloc failed.");
731 return (-1);
732 }
734 memcpy ((void *) pkg_types, (void *) buffer, pkg_numval * sizeof (uint8_t));
735 buffer += pkg_numval * sizeof (uint8_t);
736 memcpy ((void *) pkg_values, (void *) buffer, pkg_numval * sizeof (value_t));
737 buffer += pkg_numval * sizeof (value_t);
739 for (i = 0; i < pkg_numval; i++)
740 {
741 switch (pkg_types[i])
742 {
743 case DS_TYPE_COUNTER:
744 pkg_values[i].counter = (counter_t) ntohll (pkg_values[i].counter);
745 break;
747 case DS_TYPE_GAUGE:
748 pkg_values[i].gauge = (gauge_t) ntohd (pkg_values[i].gauge);
749 break;
751 case DS_TYPE_DERIVE:
752 pkg_values[i].derive = (derive_t) ntohll (pkg_values[i].derive);
753 break;
755 case DS_TYPE_ABSOLUTE:
756 pkg_values[i].absolute = (absolute_t) ntohll (pkg_values[i].absolute);
757 break;
759 default:
760 NOTICE ("network plugin: parse_part_values: "
761 "Don't know how to handle data source type %"PRIu8,
762 pkg_types[i]);
763 sfree (pkg_types);
764 sfree (pkg_values);
765 return (-1);
766 } /* switch (pkg_types[i]) */
767 }
769 *ret_buffer = buffer;
770 *ret_buffer_len = buffer_len - pkg_length;
771 *ret_num_values = pkg_numval;
772 *ret_values = pkg_values;
774 sfree (pkg_types);
776 return (0);
777 } /* int parse_part_values */
779 static int parse_part_number (void **ret_buffer, size_t *ret_buffer_len,
780 uint64_t *value)
781 {
782 char *buffer = *ret_buffer;
783 size_t buffer_len = *ret_buffer_len;
785 uint16_t tmp16;
786 uint64_t tmp64;
787 size_t exp_size = 2 * sizeof (uint16_t) + sizeof (uint64_t);
789 uint16_t pkg_length;
790 uint16_t pkg_type;
792 if ((buffer_len < 0) || ((size_t) buffer_len < exp_size))
793 {
794 WARNING ("network plugin: parse_part_number: "
795 "Packet too short: "
796 "Chunk of size %zu expected, "
797 "but buffer has only %zu bytes left.",
798 exp_size, buffer_len);
799 return (-1);
800 }
802 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
803 buffer += sizeof (tmp16);
804 pkg_type = ntohs (tmp16);
806 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
807 buffer += sizeof (tmp16);
808 pkg_length = ntohs (tmp16);
810 memcpy ((void *) &tmp64, buffer, sizeof (tmp64));
811 buffer += sizeof (tmp64);
812 *value = ntohll (tmp64);
814 *ret_buffer = buffer;
815 *ret_buffer_len = buffer_len - pkg_length;
817 return (0);
818 } /* int parse_part_number */
820 static int parse_part_string (void **ret_buffer, size_t *ret_buffer_len,
821 char *output, int output_len)
822 {
823 char *buffer = *ret_buffer;
824 size_t buffer_len = *ret_buffer_len;
826 uint16_t tmp16;
827 size_t header_size = 2 * sizeof (uint16_t);
829 uint16_t pkg_length;
830 uint16_t pkg_type;
832 if ((buffer_len < 0) || (buffer_len < header_size))
833 {
834 WARNING ("network plugin: parse_part_string: "
835 "Packet too short: "
836 "Chunk of at least size %zu expected, "
837 "but buffer has only %zu bytes left.",
838 header_size, buffer_len);
839 return (-1);
840 }
842 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
843 buffer += sizeof (tmp16);
844 pkg_type = ntohs (tmp16);
846 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
847 buffer += sizeof (tmp16);
848 pkg_length = ntohs (tmp16);
850 /* Check that packet fits in the input buffer */
851 if (pkg_length > buffer_len)
852 {
853 WARNING ("network plugin: parse_part_string: "
854 "Packet too big: "
855 "Chunk of size %"PRIu16" received, "
856 "but buffer has only %zu bytes left.",
857 pkg_length, buffer_len);
858 return (-1);
859 }
861 /* Check that pkg_length is in the valid range */
862 if (pkg_length <= header_size)
863 {
864 WARNING ("network plugin: parse_part_string: "
865 "Packet too short: "
866 "Header claims this packet is only %hu "
867 "bytes long.", pkg_length);
868 return (-1);
869 }
871 /* Check that the package data fits into the output buffer.
872 * The previous if-statement ensures that:
873 * `pkg_length > header_size' */
874 if ((output_len < 0)
875 || ((size_t) output_len < ((size_t) pkg_length - header_size)))
876 {
877 WARNING ("network plugin: parse_part_string: "
878 "Output buffer too small.");
879 return (-1);
880 }
882 /* All sanity checks successfull, let's copy the data over */
883 output_len = pkg_length - header_size;
884 memcpy ((void *) output, (void *) buffer, output_len);
885 buffer += output_len;
887 /* For some very weird reason '\0' doesn't do the trick on SPARC in
888 * this statement. */
889 if (output[output_len - 1] != 0)
890 {
891 WARNING ("network plugin: parse_part_string: "
892 "Received string does not end "
893 "with a NULL-byte.");
894 return (-1);
895 }
897 *ret_buffer = buffer;
898 *ret_buffer_len = buffer_len - pkg_length;
900 return (0);
901 } /* int parse_part_string */
903 /* Forward declaration: parse_part_sign_sha256 and parse_part_encr_aes256 call
904 * parse_packet and vice versa. */
905 #define PP_SIGNED 0x01
906 #define PP_ENCRYPTED 0x02
907 static int parse_packet (sockent_t *se,
908 void *buffer, size_t buffer_size, int flags,
909 const char *username);
911 #define BUFFER_READ(p,s) do { \
912 memcpy ((p), buffer + buffer_offset, (s)); \
913 buffer_offset += (s); \
914 } while (0)
916 #if HAVE_LIBGCRYPT
917 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
918 void **ret_buffer, size_t *ret_buffer_len, int flags)
919 {
920 char *buffer;
921 size_t buffer_len;
922 size_t buffer_offset;
924 size_t username_len;
925 char *secret;
927 part_signature_sha256_t pss;
928 uint16_t pss_head_length;
929 char hash[sizeof (pss.hash)];
931 gcry_md_hd_t hd;
932 gcry_error_t err;
933 unsigned char *hash_ptr;
935 buffer = *ret_buffer;
936 buffer_len = *ret_buffer_len;
937 buffer_offset = 0;
939 if (se->data.server.userdb == NULL)
940 {
941 NOTICE ("network plugin: Received signed network packet but can't verify "
942 "it because no user DB has been configured. Will accept it.");
943 return (0);
944 }
946 /* Check if the buffer has enough data for this structure. */
947 if (buffer_len <= PART_SIGNATURE_SHA256_SIZE)
948 return (-ENOMEM);
950 /* Read type and length header */
951 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
952 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
953 pss_head_length = ntohs (pss.head.length);
955 /* Check if the `pss_head_length' is within bounds. */
956 if ((pss_head_length <= PART_SIGNATURE_SHA256_SIZE)
957 || (pss_head_length > buffer_len))
958 {
959 ERROR ("network plugin: HMAC-SHA-256 with invalid length received.");
960 return (-1);
961 }
963 /* Copy the hash. */
964 BUFFER_READ (pss.hash, sizeof (pss.hash));
966 /* Calculate username length (without null byte) and allocate memory */
967 username_len = pss_head_length - PART_SIGNATURE_SHA256_SIZE;
968 pss.username = malloc (username_len + 1);
969 if (pss.username == NULL)
970 return (-ENOMEM);
972 /* Read the username */
973 BUFFER_READ (pss.username, username_len);
974 pss.username[username_len] = 0;
976 assert (buffer_offset == pss_head_length);
978 /* Query the password */
979 secret = fbh_get (se->data.server.userdb, pss.username);
980 if (secret == NULL)
981 {
982 ERROR ("network plugin: Unknown user: %s", pss.username);
983 sfree (pss.username);
984 return (-ENOENT);
985 }
987 /* Create a hash device and check the HMAC */
988 hd = NULL;
989 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
990 if (err != 0)
991 {
992 ERROR ("network plugin: Creating HMAC-SHA-256 object failed: %s",
993 gcry_strerror (err));
994 sfree (secret);
995 sfree (pss.username);
996 return (-1);
997 }
999 err = gcry_md_setkey (hd, secret, strlen (secret));
1000 if (err != 0)
1001 {
1002 ERROR ("network plugin: gcry_md_setkey failed: %s", gcry_strerror (err));
1003 gcry_md_close (hd);
1004 sfree (secret);
1005 sfree (pss.username);
1006 return (-1);
1007 }
1009 gcry_md_write (hd,
1010 buffer + PART_SIGNATURE_SHA256_SIZE,
1011 buffer_len - PART_SIGNATURE_SHA256_SIZE);
1012 hash_ptr = gcry_md_read (hd, GCRY_MD_SHA256);
1013 if (hash_ptr == NULL)
1014 {
1015 ERROR ("network plugin: gcry_md_read failed.");
1016 gcry_md_close (hd);
1017 sfree (secret);
1018 sfree (pss.username);
1019 return (-1);
1020 }
1021 memcpy (hash, hash_ptr, sizeof (hash));
1023 /* Clean up */
1024 gcry_md_close (hd);
1025 hd = NULL;
1027 if (memcmp (pss.hash, hash, sizeof (pss.hash)) != 0)
1028 {
1029 WARNING ("network plugin: Verifying HMAC-SHA-256 signature failed: "
1030 "Hash mismatch.");
1031 }
1032 else
1033 {
1034 parse_packet (se, buffer + buffer_offset, buffer_len - buffer_offset,
1035 flags | PP_SIGNED, pss.username);
1036 }
1038 sfree (secret);
1039 sfree (pss.username);
1041 *ret_buffer = buffer + buffer_len;
1042 *ret_buffer_len = 0;
1044 return (0);
1045 } /* }}} int parse_part_sign_sha256 */
1046 /* #endif HAVE_LIBGCRYPT */
1048 #else /* if !HAVE_LIBGCRYPT */
1049 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
1050 void **ret_buffer, size_t *ret_buffer_size, int flags)
1051 {
1052 static int warning_has_been_printed = 0;
1054 char *buffer;
1055 size_t buffer_size;
1056 size_t buffer_offset;
1057 uint16_t part_len;
1059 part_signature_sha256_t pss;
1061 buffer = *ret_buffer;
1062 buffer_size = *ret_buffer_size;
1063 buffer_offset = 0;
1065 if (buffer_size <= PART_SIGNATURE_SHA256_SIZE)
1066 return (-ENOMEM);
1068 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
1069 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
1070 part_len = ntohs (pss.head.length);
1072 if ((part_len <= PART_SIGNATURE_SHA256_SIZE)
1073 || (part_len > buffer_size))
1074 return (-EINVAL);
1076 if (warning_has_been_printed == 0)
1077 {
1078 WARNING ("network plugin: Received signed packet, but the network "
1079 "plugin was not linked with libgcrypt, so I cannot "
1080 "verify the signature. The packet will be accepted.");
1081 warning_has_been_printed = 1;
1082 }
1084 parse_packet (se, buffer + part_len, buffer_size - part_len, flags,
1085 /* username = */ NULL);
1087 *ret_buffer = buffer + buffer_size;
1088 *ret_buffer_size = 0;
1090 return (0);
1091 } /* }}} int parse_part_sign_sha256 */
1092 #endif /* !HAVE_LIBGCRYPT */
1094 #if HAVE_LIBGCRYPT
1095 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1096 void **ret_buffer, size_t *ret_buffer_len,
1097 int flags)
1098 {
1099 char *buffer = *ret_buffer;
1100 size_t buffer_len = *ret_buffer_len;
1101 size_t payload_len;
1102 size_t part_size;
1103 size_t buffer_offset;
1104 uint16_t username_len;
1105 part_encryption_aes256_t pea;
1106 unsigned char hash[sizeof (pea.hash)];
1108 gcry_cipher_hd_t cypher;
1109 gcry_error_t err;
1111 /* Make sure at least the header if available. */
1112 if (buffer_len <= PART_ENCRYPTION_AES256_SIZE)
1113 {
1114 NOTICE ("network plugin: parse_part_encr_aes256: "
1115 "Discarding short packet.");
1116 return (-1);
1117 }
1119 buffer_offset = 0;
1121 /* Copy the unencrypted information into `pea'. */
1122 BUFFER_READ (&pea.head.type, sizeof (pea.head.type));
1123 BUFFER_READ (&pea.head.length, sizeof (pea.head.length));
1125 /* Check the `part size'. */
1126 part_size = ntohs (pea.head.length);
1127 if ((part_size <= PART_ENCRYPTION_AES256_SIZE)
1128 || (part_size > buffer_len))
1129 {
1130 NOTICE ("network plugin: parse_part_encr_aes256: "
1131 "Discarding part with invalid size.");
1132 return (-1);
1133 }
1135 /* Read the username */
1136 BUFFER_READ (&username_len, sizeof (username_len));
1137 username_len = ntohs (username_len);
1139 if ((username_len <= 0)
1140 || (username_len > (part_size - (PART_ENCRYPTION_AES256_SIZE + 1))))
1141 {
1142 NOTICE ("network plugin: parse_part_encr_aes256: "
1143 "Discarding part with invalid username length.");
1144 return (-1);
1145 }
1147 assert (username_len > 0);
1148 pea.username = malloc (username_len + 1);
1149 if (pea.username == NULL)
1150 return (-ENOMEM);
1151 BUFFER_READ (pea.username, username_len);
1152 pea.username[username_len] = 0;
1154 /* Last but not least, the initialization vector */
1155 BUFFER_READ (pea.iv, sizeof (pea.iv));
1157 /* Make sure we are at the right position */
1158 assert (buffer_offset == (username_len +
1159 PART_ENCRYPTION_AES256_SIZE - sizeof (pea.hash)));
1161 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
1162 pea.username);
1163 if (cypher == NULL)
1164 {
1165 sfree (pea.username);
1166 return (-1);
1167 }
1169 payload_len = part_size - (PART_ENCRYPTION_AES256_SIZE + username_len);
1170 assert (payload_len > 0);
1172 /* Decrypt the packet in-place */
1173 err = gcry_cipher_decrypt (cypher,
1174 buffer + buffer_offset,
1175 part_size - buffer_offset,
1176 /* in = */ NULL, /* in len = */ 0);
1177 if (err != 0)
1178 {
1179 sfree (pea.username);
1180 ERROR ("network plugin: gcry_cipher_decrypt returned: %s",
1181 gcry_strerror (err));
1182 return (-1);
1183 }
1185 /* Read the hash */
1186 BUFFER_READ (pea.hash, sizeof (pea.hash));
1188 /* Make sure we're at the right position - again */
1189 assert (buffer_offset == (username_len + PART_ENCRYPTION_AES256_SIZE));
1190 assert (buffer_offset == (part_size - payload_len));
1192 /* Check hash sum */
1193 memset (hash, 0, sizeof (hash));
1194 gcry_md_hash_buffer (GCRY_MD_SHA1, hash,
1195 buffer + buffer_offset, payload_len);
1196 if (memcmp (hash, pea.hash, sizeof (hash)) != 0)
1197 {
1198 sfree (pea.username);
1199 ERROR ("network plugin: Decryption failed: Checksum mismatch.");
1200 return (-1);
1201 }
1203 parse_packet (se, buffer + buffer_offset, payload_len,
1204 flags | PP_ENCRYPTED, pea.username);
1206 /* XXX: Free pea.username?!? */
1208 /* Update return values */
1209 *ret_buffer = buffer + part_size;
1210 *ret_buffer_len = buffer_len - part_size;
1212 sfree (pea.username);
1214 return (0);
1215 } /* }}} int parse_part_encr_aes256 */
1216 /* #endif HAVE_LIBGCRYPT */
1218 #else /* if !HAVE_LIBGCRYPT */
1219 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1220 void **ret_buffer, size_t *ret_buffer_size, int flags)
1221 {
1222 static int warning_has_been_printed = 0;
1224 char *buffer;
1225 size_t buffer_size;
1226 size_t buffer_offset;
1228 part_header_t ph;
1229 size_t ph_length;
1231 buffer = *ret_buffer;
1232 buffer_size = *ret_buffer_size;
1233 buffer_offset = 0;
1235 /* parse_packet assures this minimum size. */
1236 assert (buffer_size >= (sizeof (ph.type) + sizeof (ph.length)));
1238 BUFFER_READ (&ph.type, sizeof (ph.type));
1239 BUFFER_READ (&ph.length, sizeof (ph.length));
1240 ph_length = ntohs (ph.length);
1242 if ((ph_length <= PART_ENCRYPTION_AES256_SIZE)
1243 || (ph_length > buffer_size))
1244 {
1245 ERROR ("network plugin: AES-256 encrypted part "
1246 "with invalid length received.");
1247 return (-1);
1248 }
1250 if (warning_has_been_printed == 0)
1251 {
1252 WARNING ("network plugin: Received encrypted packet, but the network "
1253 "plugin was not linked with libgcrypt, so I cannot "
1254 "decrypt it. The part will be discarded.");
1255 warning_has_been_printed = 1;
1256 }
1258 *ret_buffer += ph_length;
1259 *ret_buffer_size -= ph_length;
1261 return (0);
1262 } /* }}} int parse_part_encr_aes256 */
1263 #endif /* !HAVE_LIBGCRYPT */
1265 #undef BUFFER_READ
1267 static int parse_packet (sockent_t *se, /* {{{ */
1268 void *buffer, size_t buffer_size, int flags,
1269 const char *username)
1270 {
1271 int status;
1273 value_list_t vl = VALUE_LIST_INIT;
1274 notification_t n;
1276 #if HAVE_LIBGCRYPT
1277 int packet_was_signed = (flags & PP_SIGNED);
1278 int packet_was_encrypted = (flags & PP_ENCRYPTED);
1279 int printed_ignore_warning = 0;
1280 #endif /* HAVE_LIBGCRYPT */
1283 memset (&vl, '\0', sizeof (vl));
1284 memset (&n, '\0', sizeof (n));
1285 status = 0;
1287 while ((status == 0) && (0 < buffer_size)
1288 && ((unsigned int) buffer_size > sizeof (part_header_t)))
1289 {
1290 uint16_t pkg_length;
1291 uint16_t pkg_type;
1293 memcpy ((void *) &pkg_type,
1294 (void *) buffer,
1295 sizeof (pkg_type));
1296 memcpy ((void *) &pkg_length,
1297 (void *) (buffer + sizeof (pkg_type)),
1298 sizeof (pkg_length));
1300 pkg_length = ntohs (pkg_length);
1301 pkg_type = ntohs (pkg_type);
1303 if (pkg_length > buffer_size)
1304 break;
1305 /* Ensure that this loop terminates eventually */
1306 if (pkg_length < (2 * sizeof (uint16_t)))
1307 break;
1309 if (pkg_type == TYPE_ENCR_AES256)
1310 {
1311 status = parse_part_encr_aes256 (se,
1312 &buffer, &buffer_size, flags);
1313 if (status != 0)
1314 {
1315 ERROR ("network plugin: Decrypting AES256 "
1316 "part failed "
1317 "with status %i.", status);
1318 break;
1319 }
1320 }
1321 #if HAVE_LIBGCRYPT
1322 else if ((se->data.server.security_level == SECURITY_LEVEL_ENCRYPT)
1323 && (packet_was_encrypted == 0))
1324 {
1325 if (printed_ignore_warning == 0)
1326 {
1327 INFO ("network plugin: Unencrypted packet or "
1328 "part has been ignored.");
1329 printed_ignore_warning = 1;
1330 }
1331 buffer = ((char *) buffer) + pkg_length;
1332 continue;
1333 }
1334 #endif /* HAVE_LIBGCRYPT */
1335 else if (pkg_type == TYPE_SIGN_SHA256)
1336 {
1337 status = parse_part_sign_sha256 (se,
1338 &buffer, &buffer_size, flags);
1339 if (status != 0)
1340 {
1341 ERROR ("network plugin: Verifying HMAC-SHA-256 "
1342 "signature failed "
1343 "with status %i.", status);
1344 break;
1345 }
1346 }
1347 #if HAVE_LIBGCRYPT
1348 else if ((se->data.server.security_level == SECURITY_LEVEL_SIGN)
1349 && (packet_was_encrypted == 0)
1350 && (packet_was_signed == 0))
1351 {
1352 if (printed_ignore_warning == 0)
1353 {
1354 INFO ("network plugin: Unsigned packet or "
1355 "part has been ignored.");
1356 printed_ignore_warning = 1;
1357 }
1358 buffer = ((char *) buffer) + pkg_length;
1359 continue;
1360 }
1361 #endif /* HAVE_LIBGCRYPT */
1362 else if (pkg_type == TYPE_VALUES)
1363 {
1364 status = parse_part_values (&buffer, &buffer_size,
1365 &vl.values, &vl.values_len);
1366 if (status != 0)
1367 break;
1369 network_dispatch_values (&vl, username);
1371 sfree (vl.values);
1372 }
1373 else if (pkg_type == TYPE_TIME)
1374 {
1375 uint64_t tmp = 0;
1376 status = parse_part_number (&buffer, &buffer_size,
1377 &tmp);
1378 if (status == 0)
1379 {
1380 vl.time = TIME_T_TO_CDTIME_T (tmp);
1381 n.time = TIME_T_TO_CDTIME_T (tmp);
1382 }
1383 }
1384 else if (pkg_type == TYPE_TIME_HR)
1385 {
1386 uint64_t tmp = 0;
1387 status = parse_part_number (&buffer, &buffer_size,
1388 &tmp);
1389 if (status == 0)
1390 {
1391 vl.time = (cdtime_t) tmp;
1392 n.time = (cdtime_t) tmp;
1393 }
1394 }
1395 else if (pkg_type == TYPE_INTERVAL)
1396 {
1397 uint64_t tmp = 0;
1398 status = parse_part_number (&buffer, &buffer_size,
1399 &tmp);
1400 if (status == 0)
1401 vl.interval = TIME_T_TO_CDTIME_T (tmp);
1402 }
1403 else if (pkg_type == TYPE_INTERVAL_HR)
1404 {
1405 uint64_t tmp = 0;
1406 status = parse_part_number (&buffer, &buffer_size,
1407 &tmp);
1408 if (status == 0)
1409 vl.interval = (cdtime_t) tmp;
1410 }
1411 else if (pkg_type == TYPE_HOST)
1412 {
1413 status = parse_part_string (&buffer, &buffer_size,
1414 vl.host, sizeof (vl.host));
1415 if (status == 0)
1416 sstrncpy (n.host, vl.host, sizeof (n.host));
1417 }
1418 else if (pkg_type == TYPE_PLUGIN)
1419 {
1420 status = parse_part_string (&buffer, &buffer_size,
1421 vl.plugin, sizeof (vl.plugin));
1422 if (status == 0)
1423 sstrncpy (n.plugin, vl.plugin,
1424 sizeof (n.plugin));
1425 }
1426 else if (pkg_type == TYPE_PLUGIN_INSTANCE)
1427 {
1428 status = parse_part_string (&buffer, &buffer_size,
1429 vl.plugin_instance,
1430 sizeof (vl.plugin_instance));
1431 if (status == 0)
1432 sstrncpy (n.plugin_instance,
1433 vl.plugin_instance,
1434 sizeof (n.plugin_instance));
1435 }
1436 else if (pkg_type == TYPE_TYPE)
1437 {
1438 status = parse_part_string (&buffer, &buffer_size,
1439 vl.type, sizeof (vl.type));
1440 if (status == 0)
1441 sstrncpy (n.type, vl.type, sizeof (n.type));
1442 }
1443 else if (pkg_type == TYPE_TYPE_INSTANCE)
1444 {
1445 status = parse_part_string (&buffer, &buffer_size,
1446 vl.type_instance,
1447 sizeof (vl.type_instance));
1448 if (status == 0)
1449 sstrncpy (n.type_instance, vl.type_instance,
1450 sizeof (n.type_instance));
1451 }
1452 else if (pkg_type == TYPE_MESSAGE)
1453 {
1454 status = parse_part_string (&buffer, &buffer_size,
1455 n.message, sizeof (n.message));
1457 if (status != 0)
1458 {
1459 /* do nothing */
1460 }
1461 else if ((n.severity != NOTIF_FAILURE)
1462 && (n.severity != NOTIF_WARNING)
1463 && (n.severity != NOTIF_OKAY))
1464 {
1465 INFO ("network plugin: "
1466 "Ignoring notification with "
1467 "unknown severity %i.",
1468 n.severity);
1469 }
1470 else if (n.time <= 0)
1471 {
1472 INFO ("network plugin: "
1473 "Ignoring notification with "
1474 "time == 0.");
1475 }
1476 else if (strlen (n.message) <= 0)
1477 {
1478 INFO ("network plugin: "
1479 "Ignoring notification with "
1480 "an empty message.");
1481 }
1482 else
1483 {
1484 plugin_dispatch_notification (&n);
1485 }
1486 }
1487 else if (pkg_type == TYPE_SEVERITY)
1488 {
1489 uint64_t tmp = 0;
1490 status = parse_part_number (&buffer, &buffer_size,
1491 &tmp);
1492 if (status == 0)
1493 n.severity = (int) tmp;
1494 }
1495 else
1496 {
1497 DEBUG ("network plugin: parse_packet: Unknown part"
1498 " type: 0x%04hx", pkg_type);
1499 buffer = ((char *) buffer) + pkg_length;
1500 }
1501 } /* while (buffer_size > sizeof (part_header_t)) */
1503 if (status == 0 && buffer_size > 0)
1504 WARNING ("network plugin: parse_packet: Received truncated "
1505 "packet, try increasing `MaxPacketSize'");
1507 return (status);
1508 } /* }}} int parse_packet */
1510 static void free_sockent_client (struct sockent_client *sec) /* {{{ */
1511 {
1512 if (sec->fd >= 0)
1513 {
1514 close (sec->fd);
1515 sec->fd = -1;
1516 }
1517 sfree (sec->addr);
1518 #if HAVE_LIBGCRYPT
1519 sfree (sec->username);
1520 sfree (sec->password);
1521 if (sec->cypher != NULL)
1522 gcry_cipher_close (sec->cypher);
1523 #endif
1524 } /* }}} void free_sockent_client */
1526 static void free_sockent_server (struct sockent_server *ses) /* {{{ */
1527 {
1528 size_t i;
1530 for (i = 0; i < ses->fd_num; i++)
1531 {
1532 if (ses->fd[i] >= 0)
1533 {
1534 close (ses->fd[i]);
1535 ses->fd[i] = -1;
1536 }
1537 }
1539 sfree (ses->fd);
1540 #if HAVE_LIBGCRYPT
1541 sfree (ses->auth_file);
1542 fbh_destroy (ses->userdb);
1543 if (ses->cypher != NULL)
1544 gcry_cipher_close (ses->cypher);
1545 #endif
1546 } /* }}} void free_sockent_server */
1548 static void sockent_destroy (sockent_t *se) /* {{{ */
1549 {
1550 sockent_t *next;
1552 DEBUG ("network plugin: sockent_destroy (se = %p);", (void *) se);
1554 while (se != NULL)
1555 {
1556 next = se->next;
1558 sfree (se->node);
1559 sfree (se->service);
1561 if (se->type == SOCKENT_TYPE_CLIENT)
1562 free_sockent_client (&se->data.client);
1563 else
1564 free_sockent_server (&se->data.server);
1566 sfree (se);
1567 se = next;
1568 }
1569 } /* }}} void sockent_destroy */
1571 /*
1572 * int network_set_ttl
1573 *
1574 * Set the `IP_MULTICAST_TTL', `IP_TTL', `IPV6_MULTICAST_HOPS' or
1575 * `IPV6_UNICAST_HOPS', depending on which option is applicable.
1576 *
1577 * The `struct addrinfo' is used to destinguish between unicast and multicast
1578 * sockets.
1579 */
1580 static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
1581 {
1582 DEBUG ("network plugin: network_set_ttl: network_config_ttl = %i;",
1583 network_config_ttl);
1585 assert (se->type == SOCKENT_TYPE_CLIENT);
1587 if ((network_config_ttl < 1) || (network_config_ttl > 255))
1588 return (-1);
1590 if (ai->ai_family == AF_INET)
1591 {
1592 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1593 int optname;
1595 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1596 optname = IP_MULTICAST_TTL;
1597 else
1598 optname = IP_TTL;
1600 if (setsockopt (se->data.client.fd, IPPROTO_IP, optname,
1601 &network_config_ttl,
1602 sizeof (network_config_ttl)) != 0)
1603 {
1604 char errbuf[1024];
1605 ERROR ("setsockopt: %s",
1606 sstrerror (errno, errbuf, sizeof (errbuf)));
1607 return (-1);
1608 }
1609 }
1610 else if (ai->ai_family == AF_INET6)
1611 {
1612 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1613 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1614 int optname;
1616 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1617 optname = IPV6_MULTICAST_HOPS;
1618 else
1619 optname = IPV6_UNICAST_HOPS;
1621 if (setsockopt (se->data.client.fd, IPPROTO_IPV6, optname,
1622 &network_config_ttl,
1623 sizeof (network_config_ttl)) != 0)
1624 {
1625 char errbuf[1024];
1626 ERROR ("setsockopt: %s",
1627 sstrerror (errno, errbuf,
1628 sizeof (errbuf)));
1629 return (-1);
1630 }
1631 }
1633 return (0);
1634 } /* int network_set_ttl */
1636 static int network_set_interface (const sockent_t *se, const struct addrinfo *ai) /* {{{ */
1637 {
1638 DEBUG ("network plugin: network_set_interface: interface index = %i;",
1639 se->interface);
1641 assert (se->type == SOCKENT_TYPE_CLIENT);
1643 if (ai->ai_family == AF_INET)
1644 {
1645 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1647 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1648 {
1649 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1650 /* If possible, use the "ip_mreqn" structure which has
1651 * an "interface index" member. Using the interface
1652 * index is preferred here, because of its similarity
1653 * to the way IPv6 handles this. Unfortunately, it
1654 * appears not to be portable. */
1655 struct ip_mreqn mreq;
1657 memset (&mreq, 0, sizeof (mreq));
1658 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1659 mreq.imr_address.s_addr = ntohl (INADDR_ANY);
1660 mreq.imr_ifindex = se->interface;
1661 #else
1662 struct ip_mreq mreq;
1664 memset (&mreq, 0, sizeof (mreq));
1665 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1666 mreq.imr_interface.s_addr = ntohl (INADDR_ANY);
1667 #endif
1669 if (setsockopt (se->data.client.fd, IPPROTO_IP, IP_MULTICAST_IF,
1670 &mreq, sizeof (mreq)) != 0)
1671 {
1672 char errbuf[1024];
1673 ERROR ("setsockopt: %s",
1674 sstrerror (errno, errbuf, sizeof (errbuf)));
1675 return (-1);
1676 }
1678 return (0);
1679 }
1680 }
1681 else if (ai->ai_family == AF_INET6)
1682 {
1683 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1685 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1686 {
1687 if (setsockopt (se->data.client.fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
1688 &se->interface,
1689 sizeof (se->interface)) != 0)
1690 {
1691 char errbuf[1024];
1692 ERROR ("setsockopt: %s",
1693 sstrerror (errno, errbuf,
1694 sizeof (errbuf)));
1695 return (-1);
1696 }
1698 return (0);
1699 }
1700 }
1702 /* else: Not a multicast interface. */
1703 #if defined(HAVE_IF_INDEXTONAME) && HAVE_IF_INDEXTONAME && defined(SO_BINDTODEVICE)
1704 if (se->interface != 0)
1705 {
1706 char interface_name[IFNAMSIZ];
1708 if (if_indextoname (se->interface, interface_name) == NULL)
1709 return (-1);
1711 DEBUG ("network plugin: Binding socket to interface %s", interface_name);
1713 if (setsockopt (se->data.client.fd, SOL_SOCKET, SO_BINDTODEVICE,
1714 interface_name,
1715 sizeof(interface_name)) == -1 )
1716 {
1717 char errbuf[1024];
1718 ERROR ("setsockopt: %s",
1719 sstrerror (errno, errbuf, sizeof (errbuf)));
1720 return (-1);
1721 }
1722 }
1723 /* #endif HAVE_IF_INDEXTONAME && SO_BINDTODEVICE */
1725 #else
1726 WARNING ("network plugin: Cannot set the interface on a unicast "
1727 "socket because "
1728 # if !defined(SO_BINDTODEVICE)
1729 "the the \"SO_BINDTODEVICE\" socket option "
1730 # else
1731 "the \"if_indextoname\" function "
1732 # endif
1733 "is not available on your system.");
1734 #endif
1736 return (0);
1737 } /* }}} network_set_interface */
1739 static int network_bind_socket (int fd, const struct addrinfo *ai, const int interface_idx)
1740 {
1741 int loop = 0;
1742 int yes = 1;
1744 /* allow multiple sockets to use the same PORT number */
1745 if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR,
1746 &yes, sizeof(yes)) == -1) {
1747 char errbuf[1024];
1748 ERROR ("setsockopt: %s",
1749 sstrerror (errno, errbuf, sizeof (errbuf)));
1750 return (-1);
1751 }
1753 DEBUG ("fd = %i; calling `bind'", fd);
1755 if (bind (fd, ai->ai_addr, ai->ai_addrlen) == -1)
1756 {
1757 char errbuf[1024];
1758 ERROR ("bind: %s",
1759 sstrerror (errno, errbuf, sizeof (errbuf)));
1760 return (-1);
1761 }
1763 if (ai->ai_family == AF_INET)
1764 {
1765 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1766 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1767 {
1768 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1769 struct ip_mreqn mreq;
1770 #else
1771 struct ip_mreq mreq;
1772 #endif
1774 DEBUG ("fd = %i; IPv4 multicast address found", fd);
1776 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1777 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1778 /* Set the interface using the interface index if
1779 * possible (available). Unfortunately, the struct
1780 * ip_mreqn is not portable. */
1781 mreq.imr_address.s_addr = ntohl (INADDR_ANY);
1782 mreq.imr_ifindex = interface_idx;
1783 #else
1784 mreq.imr_interface.s_addr = ntohl (INADDR_ANY);
1785 #endif
1787 if (setsockopt (fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1788 &loop, sizeof (loop)) == -1)
1789 {
1790 char errbuf[1024];
1791 ERROR ("setsockopt: %s",
1792 sstrerror (errno, errbuf,
1793 sizeof (errbuf)));
1794 return (-1);
1795 }
1797 if (setsockopt (fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1798 &mreq, sizeof (mreq)) == -1)
1799 {
1800 char errbuf[1024];
1801 ERROR ("setsockopt: %s",
1802 sstrerror (errno, errbuf,
1803 sizeof (errbuf)));
1804 return (-1);
1805 }
1807 return (0);
1808 }
1809 }
1810 else if (ai->ai_family == AF_INET6)
1811 {
1812 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1813 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1814 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1815 {
1816 struct ipv6_mreq mreq;
1818 DEBUG ("fd = %i; IPv6 multicast address found", fd);
1820 memcpy (&mreq.ipv6mr_multiaddr,
1821 &addr->sin6_addr,
1822 sizeof (addr->sin6_addr));
1824 /* http://developer.apple.com/documentation/Darwin/Reference/ManPages/man4/ip6.4.html
1825 * ipv6mr_interface may be set to zeroes to
1826 * choose the default multicast interface or to
1827 * the index of a particular multicast-capable
1828 * interface if the host is multihomed.
1829 * Membership is associ-associated with a
1830 * single interface; programs running on
1831 * multihomed hosts may need to join the same
1832 * group on more than one interface.*/
1833 mreq.ipv6mr_interface = interface_idx;
1835 if (setsockopt (fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
1836 &loop, sizeof (loop)) == -1)
1837 {
1838 char errbuf[1024];
1839 ERROR ("setsockopt: %s",
1840 sstrerror (errno, errbuf,
1841 sizeof (errbuf)));
1842 return (-1);
1843 }
1845 if (setsockopt (fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
1846 &mreq, sizeof (mreq)) == -1)
1847 {
1848 char errbuf[1024];
1849 ERROR ("setsockopt: %s",
1850 sstrerror (errno, errbuf,
1851 sizeof (errbuf)));
1852 return (-1);
1853 }
1855 return (0);
1856 }
1857 }
1859 #if defined(HAVE_IF_INDEXTONAME) && HAVE_IF_INDEXTONAME && defined(SO_BINDTODEVICE)
1860 /* if a specific interface was set, bind the socket to it. But to avoid
1861 * possible problems with multicast routing, only do that for non-multicast
1862 * addresses */
1863 if (interface_idx != 0)
1864 {
1865 char interface_name[IFNAMSIZ];
1867 if (if_indextoname (interface_idx, interface_name) == NULL)
1868 return (-1);
1870 DEBUG ("fd = %i; Binding socket to interface %s", fd, interface_name);
1872 if (setsockopt (fd, SOL_SOCKET, SO_BINDTODEVICE,
1873 interface_name,
1874 sizeof(interface_name)) == -1 )
1875 {
1876 char errbuf[1024];
1877 ERROR ("setsockopt: %s",
1878 sstrerror (errno, errbuf, sizeof (errbuf)));
1879 return (-1);
1880 }
1881 }
1882 #endif /* HAVE_IF_INDEXTONAME && SO_BINDTODEVICE */
1884 return (0);
1885 } /* int network_bind_socket */
1887 /* Initialize a sockent structure. `type' must be either `SOCKENT_TYPE_CLIENT'
1888 * or `SOCKENT_TYPE_SERVER' */
1889 static int sockent_init (sockent_t *se, int type) /* {{{ */
1890 {
1891 if (se == NULL)
1892 return (-1);
1894 memset (se, 0, sizeof (*se));
1896 se->type = SOCKENT_TYPE_CLIENT;
1897 se->node = NULL;
1898 se->service = NULL;
1899 se->interface = 0;
1900 se->next = NULL;
1902 if (type == SOCKENT_TYPE_SERVER)
1903 {
1904 se->type = SOCKENT_TYPE_SERVER;
1905 se->data.server.fd = NULL;
1906 #if HAVE_LIBGCRYPT
1907 se->data.server.security_level = SECURITY_LEVEL_NONE;
1908 se->data.server.auth_file = NULL;
1909 se->data.server.userdb = NULL;
1910 se->data.server.cypher = NULL;
1911 #endif
1912 }
1913 else
1914 {
1915 se->data.client.fd = -1;
1916 se->data.client.addr = NULL;
1917 #if HAVE_LIBGCRYPT
1918 se->data.client.security_level = SECURITY_LEVEL_NONE;
1919 se->data.client.username = NULL;
1920 se->data.client.password = NULL;
1921 se->data.client.cypher = NULL;
1922 #endif
1923 }
1925 return (0);
1926 } /* }}} int sockent_init */
1928 /* Open the file descriptors for a initialized sockent structure. */
1929 static int sockent_open (sockent_t *se) /* {{{ */
1930 {
1931 struct addrinfo ai_hints;
1932 struct addrinfo *ai_list, *ai_ptr;
1933 int ai_return;
1935 const char *node;
1936 const char *service;
1938 if (se == NULL)
1939 return (-1);
1941 /* Set up the security structures. */
1942 #if HAVE_LIBGCRYPT /* {{{ */
1943 if (se->type == SOCKENT_TYPE_CLIENT)
1944 {
1945 if (se->data.client.security_level > SECURITY_LEVEL_NONE)
1946 {
1947 if ((se->data.client.username == NULL)
1948 || (se->data.client.password == NULL))
1949 {
1950 ERROR ("network plugin: Client socket with "
1951 "security requested, but no "
1952 "credentials are configured.");
1953 return (-1);
1954 }
1955 gcry_md_hash_buffer (GCRY_MD_SHA256,
1956 se->data.client.password_hash,
1957 se->data.client.password,
1958 strlen (se->data.client.password));
1959 }
1960 }
1961 else /* (se->type == SOCKENT_TYPE_SERVER) */
1962 {
1963 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
1964 {
1965 if (se->data.server.auth_file == NULL)
1966 {
1967 ERROR ("network plugin: Server socket with "
1968 "security requested, but no "
1969 "password file is configured.");
1970 return (-1);
1971 }
1972 }
1973 if (se->data.server.auth_file != NULL)
1974 {
1975 se->data.server.userdb = fbh_create (se->data.server.auth_file);
1976 if (se->data.server.userdb == NULL)
1977 {
1978 ERROR ("network plugin: Reading password file "
1979 "`%s' failed.",
1980 se->data.server.auth_file);
1981 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
1982 return (-1);
1983 }
1984 }
1985 }
1986 #endif /* }}} HAVE_LIBGCRYPT */
1988 node = se->node;
1989 service = se->service;
1991 if (service == NULL)
1992 service = NET_DEFAULT_PORT;
1994 DEBUG ("network plugin: sockent_open: node = %s; service = %s;",
1995 node, service);
1997 memset (&ai_hints, 0, sizeof (ai_hints));
1998 ai_hints.ai_flags = 0;
1999 #ifdef AI_PASSIVE
2000 ai_hints.ai_flags |= AI_PASSIVE;
2001 #endif
2002 #ifdef AI_ADDRCONFIG
2003 ai_hints.ai_flags |= AI_ADDRCONFIG;
2004 #endif
2005 ai_hints.ai_family = AF_UNSPEC;
2006 ai_hints.ai_socktype = SOCK_DGRAM;
2007 ai_hints.ai_protocol = IPPROTO_UDP;
2009 ai_return = getaddrinfo (node, service, &ai_hints, &ai_list);
2010 if (ai_return != 0)
2011 {
2012 ERROR ("network plugin: getaddrinfo (%s, %s) failed: %s",
2013 (se->node == NULL) ? "(null)" : se->node,
2014 (se->service == NULL) ? "(null)" : se->service,
2015 gai_strerror (ai_return));
2016 return (-1);
2017 }
2019 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
2020 {
2021 int status;
2023 if (se->type == SOCKENT_TYPE_SERVER) /* {{{ */
2024 {
2025 int *tmp;
2027 tmp = realloc (se->data.server.fd,
2028 sizeof (*tmp) * (se->data.server.fd_num + 1));
2029 if (tmp == NULL)
2030 {
2031 ERROR ("network plugin: realloc failed.");
2032 continue;
2033 }
2034 se->data.server.fd = tmp;
2035 tmp = se->data.server.fd + se->data.server.fd_num;
2037 *tmp = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
2038 ai_ptr->ai_protocol);
2039 if (*tmp < 0)
2040 {
2041 char errbuf[1024];
2042 ERROR ("network plugin: socket(2) failed: %s",
2043 sstrerror (errno, errbuf,
2044 sizeof (errbuf)));
2045 continue;
2046 }
2048 status = network_bind_socket (*tmp, ai_ptr, se->interface);
2049 if (status != 0)
2050 {
2051 close (*tmp);
2052 *tmp = -1;
2053 continue;
2054 }
2056 se->data.server.fd_num++;
2057 continue;
2058 } /* }}} if (se->type == SOCKENT_TYPE_SERVER) */
2059 else /* if (se->type == SOCKENT_TYPE_CLIENT) {{{ */
2060 {
2061 se->data.client.fd = socket (ai_ptr->ai_family,
2062 ai_ptr->ai_socktype,
2063 ai_ptr->ai_protocol);
2064 if (se->data.client.fd < 0)
2065 {
2066 char errbuf[1024];
2067 ERROR ("network plugin: socket(2) failed: %s",
2068 sstrerror (errno, errbuf,
2069 sizeof (errbuf)));
2070 continue;
2071 }
2073 se->data.client.addr = malloc (sizeof (*se->data.client.addr));
2074 if (se->data.client.addr == NULL)
2075 {
2076 ERROR ("network plugin: malloc failed.");
2077 close (se->data.client.fd);
2078 se->data.client.fd = -1;
2079 continue;
2080 }
2082 memset (se->data.client.addr, 0, sizeof (*se->data.client.addr));
2083 assert (sizeof (*se->data.client.addr) >= ai_ptr->ai_addrlen);
2084 memcpy (se->data.client.addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
2085 se->data.client.addrlen = ai_ptr->ai_addrlen;
2087 network_set_ttl (se, ai_ptr);
2088 network_set_interface (se, ai_ptr);
2090 /* We don't open more than one write-socket per
2091 * node/service pair.. */
2092 break;
2093 } /* }}} if (se->type == SOCKENT_TYPE_CLIENT) */
2094 } /* for (ai_list) */
2096 freeaddrinfo (ai_list);
2098 /* Check if all went well. */
2099 if (se->type == SOCKENT_TYPE_SERVER)
2100 {
2101 if (se->data.server.fd_num <= 0)
2102 return (-1);
2103 }
2104 else /* if (se->type == SOCKENT_TYPE_CLIENT) */
2105 {
2106 if (se->data.client.fd < 0)
2107 return (-1);
2108 }
2110 return (0);
2111 } /* }}} int sockent_open */
2113 /* Add a sockent to the global list of sockets */
2114 static int sockent_add (sockent_t *se) /* {{{ */
2115 {
2116 sockent_t *last_ptr;
2118 if (se == NULL)
2119 return (-1);
2121 if (se->type == SOCKENT_TYPE_SERVER)
2122 {
2123 struct pollfd *tmp;
2124 size_t i;
2126 tmp = realloc (listen_sockets_pollfd,
2127 sizeof (*tmp) * (listen_sockets_num
2128 + se->data.server.fd_num));
2129 if (tmp == NULL)
2130 {
2131 ERROR ("network plugin: realloc failed.");
2132 return (-1);
2133 }
2134 listen_sockets_pollfd = tmp;
2135 tmp = listen_sockets_pollfd + listen_sockets_num;
2137 for (i = 0; i < se->data.server.fd_num; i++)
2138 {
2139 memset (tmp + i, 0, sizeof (*tmp));
2140 tmp[i].fd = se->data.server.fd[i];
2141 tmp[i].events = POLLIN | POLLPRI;
2142 tmp[i].revents = 0;
2143 }
2145 listen_sockets_num += se->data.server.fd_num;
2147 if (listen_sockets == NULL)
2148 {
2149 listen_sockets = se;
2150 return (0);
2151 }
2152 last_ptr = listen_sockets;
2153 }
2154 else /* if (se->type == SOCKENT_TYPE_CLIENT) */
2155 {
2156 if (sending_sockets == NULL)
2157 {
2158 sending_sockets = se;
2159 return (0);
2160 }
2161 last_ptr = sending_sockets;
2162 }
2164 while (last_ptr->next != NULL)
2165 last_ptr = last_ptr->next;
2166 last_ptr->next = se;
2168 return (0);
2169 } /* }}} int sockent_add */
2171 static void *dispatch_thread (void __attribute__((unused)) *arg) /* {{{ */
2172 {
2173 while (42)
2174 {
2175 receive_list_entry_t *ent;
2176 sockent_t *se;
2178 /* Lock and wait for more data to come in */
2179 pthread_mutex_lock (&receive_list_lock);
2180 while ((listen_loop == 0)
2181 && (receive_list_head == NULL))
2182 pthread_cond_wait (&receive_list_cond, &receive_list_lock);
2184 /* Remove the head entry and unlock */
2185 ent = receive_list_head;
2186 if (ent != NULL)
2187 receive_list_head = ent->next;
2188 receive_list_length--;
2189 pthread_mutex_unlock (&receive_list_lock);
2191 /* Check whether we are supposed to exit. We do NOT check `listen_loop'
2192 * because we dispatch all missing packets before shutting down. */
2193 if (ent == NULL)
2194 break;
2196 /* Look for the correct `sockent_t' */
2197 se = listen_sockets;
2198 while (se != NULL)
2199 {
2200 size_t i;
2202 for (i = 0; i < se->data.server.fd_num; i++)
2203 if (se->data.server.fd[i] == ent->fd)
2204 break;
2206 if (i < se->data.server.fd_num)
2207 break;
2209 se = se->next;
2210 }
2212 if (se == NULL)
2213 {
2214 ERROR ("network plugin: Got packet from FD %i, but can't "
2215 "find an appropriate socket entry.",
2216 ent->fd);
2217 sfree (ent->data);
2218 sfree (ent);
2219 continue;
2220 }
2222 parse_packet (se, ent->data, ent->data_len, /* flags = */ 0,
2223 /* username = */ NULL);
2224 sfree (ent->data);
2225 sfree (ent);
2226 } /* while (42) */
2228 return (NULL);
2229 } /* }}} void *dispatch_thread */
2231 static int network_receive (void) /* {{{ */
2232 {
2233 char buffer[network_config_packet_size];
2234 int buffer_len;
2236 int i;
2237 int status;
2239 receive_list_entry_t *private_list_head;
2240 receive_list_entry_t *private_list_tail;
2241 uint64_t private_list_length;
2243 assert (listen_sockets_num > 0);
2245 private_list_head = NULL;
2246 private_list_tail = NULL;
2247 private_list_length = 0;
2249 while (listen_loop == 0)
2250 {
2251 status = poll (listen_sockets_pollfd, listen_sockets_num, -1);
2253 if (status <= 0)
2254 {
2255 char errbuf[1024];
2256 if (errno == EINTR)
2257 continue;
2258 ERROR ("poll failed: %s",
2259 sstrerror (errno, errbuf, sizeof (errbuf)));
2260 return (-1);
2261 }
2263 for (i = 0; (i < listen_sockets_num) && (status > 0); i++)
2264 {
2265 receive_list_entry_t *ent;
2267 if ((listen_sockets_pollfd[i].revents
2268 & (POLLIN | POLLPRI)) == 0)
2269 continue;
2270 status--;
2272 buffer_len = recv (listen_sockets_pollfd[i].fd,
2273 buffer, sizeof (buffer),
2274 0 /* no flags */);
2275 if (buffer_len < 0)
2276 {
2277 char errbuf[1024];
2278 ERROR ("recv failed: %s",
2279 sstrerror (errno, errbuf,
2280 sizeof (errbuf)));
2281 return (-1);
2282 }
2284 stats_octets_rx += ((uint64_t) buffer_len);
2285 stats_packets_rx++;
2287 /* TODO: Possible performance enhancement: Do not free
2288 * these entries in the dispatch thread but put them in
2289 * another list, so we don't have to allocate more and
2290 * more of these structures. */
2291 ent = malloc (sizeof (receive_list_entry_t));
2292 if (ent == NULL)
2293 {
2294 ERROR ("network plugin: malloc failed.");
2295 return (-1);
2296 }
2297 memset (ent, 0, sizeof (receive_list_entry_t));
2298 ent->data = malloc (network_config_packet_size);
2299 if (ent->data == NULL)
2300 {
2301 sfree (ent);
2302 ERROR ("network plugin: malloc failed.");
2303 return (-1);
2304 }
2305 ent->fd = listen_sockets_pollfd[i].fd;
2306 ent->next = NULL;
2308 memcpy (ent->data, buffer, buffer_len);
2309 ent->data_len = buffer_len;
2311 if (private_list_head == NULL)
2312 private_list_head = ent;
2313 else
2314 private_list_tail->next = ent;
2315 private_list_tail = ent;
2316 private_list_length++;
2318 /* Do not block here. Blocking here has led to
2319 * insufficient performance in the past. */
2320 if (pthread_mutex_trylock (&receive_list_lock) == 0)
2321 {
2322 assert (((receive_list_head == NULL) && (receive_list_length == 0))
2323 || ((receive_list_head != NULL) && (receive_list_length != 0)));
2325 if (receive_list_head == NULL)
2326 receive_list_head = private_list_head;
2327 else
2328 receive_list_tail->next = private_list_head;
2329 receive_list_tail = private_list_tail;
2330 receive_list_length += private_list_length;
2332 pthread_cond_signal (&receive_list_cond);
2333 pthread_mutex_unlock (&receive_list_lock);
2335 private_list_head = NULL;
2336 private_list_tail = NULL;
2337 private_list_length = 0;
2338 }
2339 } /* for (listen_sockets_pollfd) */
2340 } /* while (listen_loop == 0) */
2342 /* Make sure everything is dispatched before exiting. */
2343 if (private_list_head != NULL)
2344 {
2345 pthread_mutex_lock (&receive_list_lock);
2347 if (receive_list_head == NULL)
2348 receive_list_head = private_list_head;
2349 else
2350 receive_list_tail->next = private_list_head;
2351 receive_list_tail = private_list_tail;
2352 receive_list_length += private_list_length;
2354 private_list_head = NULL;
2355 private_list_tail = NULL;
2356 private_list_length = 0;
2358 pthread_cond_signal (&receive_list_cond);
2359 pthread_mutex_unlock (&receive_list_lock);
2360 }
2362 return (0);
2363 } /* }}} int network_receive */
2365 static void *receive_thread (void __attribute__((unused)) *arg)
2366 {
2367 return (network_receive () ? (void *) 1 : (void *) 0);
2368 } /* void *receive_thread */
2370 static void network_init_buffer (void)
2371 {
2372 memset (send_buffer, 0, network_config_packet_size);
2373 send_buffer_ptr = send_buffer;
2374 send_buffer_fill = 0;
2376 memset (&send_buffer_vl, 0, sizeof (send_buffer_vl));
2377 } /* int network_init_buffer */
2379 static void networt_send_buffer_plain (const sockent_t *se, /* {{{ */
2380 const char *buffer, size_t buffer_size)
2381 {
2382 int status;
2384 while (42)
2385 {
2386 status = sendto (se->data.client.fd, buffer, buffer_size,
2387 /* flags = */ 0,
2388 (struct sockaddr *) se->data.client.addr,
2389 se->data.client.addrlen);
2390 if (status < 0)
2391 {
2392 char errbuf[1024];
2393 if (errno == EINTR)
2394 continue;
2395 ERROR ("network plugin: sendto failed: %s",
2396 sstrerror (errno, errbuf,
2397 sizeof (errbuf)));
2398 break;
2399 }
2401 break;
2402 } /* while (42) */
2403 } /* }}} void networt_send_buffer_plain */
2405 #if HAVE_LIBGCRYPT
2406 #define BUFFER_ADD(p,s) do { \
2407 memcpy (buffer + buffer_offset, (p), (s)); \
2408 buffer_offset += (s); \
2409 } while (0)
2411 static void networt_send_buffer_signed (const sockent_t *se, /* {{{ */
2412 const char *in_buffer, size_t in_buffer_size)
2413 {
2414 part_signature_sha256_t ps;
2415 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2416 size_t buffer_offset;
2417 size_t username_len;
2419 gcry_md_hd_t hd;
2420 gcry_error_t err;
2421 unsigned char *hash;
2423 hd = NULL;
2424 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
2425 if (err != 0)
2426 {
2427 ERROR ("network plugin: Creating HMAC object failed: %s",
2428 gcry_strerror (err));
2429 return;
2430 }
2432 err = gcry_md_setkey (hd, se->data.client.password,
2433 strlen (se->data.client.password));
2434 if (err != 0)
2435 {
2436 ERROR ("network plugin: gcry_md_setkey failed: %s",
2437 gcry_strerror (err));
2438 gcry_md_close (hd);
2439 return;
2440 }
2442 username_len = strlen (se->data.client.username);
2443 if (username_len > (BUFF_SIG_SIZE - PART_SIGNATURE_SHA256_SIZE))
2444 {
2445 ERROR ("network plugin: Username too long: %s",
2446 se->data.client.username);
2447 return;
2448 }
2450 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE,
2451 se->data.client.username, username_len);
2452 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE + username_len,
2453 in_buffer, in_buffer_size);
2455 /* Initialize the `ps' structure. */
2456 memset (&ps, 0, sizeof (ps));
2457 ps.head.type = htons (TYPE_SIGN_SHA256);
2458 ps.head.length = htons (PART_SIGNATURE_SHA256_SIZE + username_len);
2460 /* Calculate the hash value. */
2461 gcry_md_write (hd, buffer + PART_SIGNATURE_SHA256_SIZE,
2462 username_len + in_buffer_size);
2463 hash = gcry_md_read (hd, GCRY_MD_SHA256);
2464 if (hash == NULL)
2465 {
2466 ERROR ("network plugin: gcry_md_read failed.");
2467 gcry_md_close (hd);
2468 return;
2469 }
2470 memcpy (ps.hash, hash, sizeof (ps.hash));
2472 /* Add the header */
2473 buffer_offset = 0;
2475 BUFFER_ADD (&ps.head.type, sizeof (ps.head.type));
2476 BUFFER_ADD (&ps.head.length, sizeof (ps.head.length));
2477 BUFFER_ADD (ps.hash, sizeof (ps.hash));
2479 assert (buffer_offset == PART_SIGNATURE_SHA256_SIZE);
2481 gcry_md_close (hd);
2482 hd = NULL;
2484 buffer_offset = PART_SIGNATURE_SHA256_SIZE + username_len + in_buffer_size;
2485 networt_send_buffer_plain (se, buffer, buffer_offset);
2486 } /* }}} void networt_send_buffer_signed */
2488 static void networt_send_buffer_encrypted (sockent_t *se, /* {{{ */
2489 const char *in_buffer, size_t in_buffer_size)
2490 {
2491 part_encryption_aes256_t pea;
2492 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2493 size_t buffer_size;
2494 size_t buffer_offset;
2495 size_t header_size;
2496 size_t username_len;
2497 gcry_error_t err;
2498 gcry_cipher_hd_t cypher;
2500 /* Initialize the header fields */
2501 memset (&pea, 0, sizeof (pea));
2502 pea.head.type = htons (TYPE_ENCR_AES256);
2504 pea.username = se->data.client.username;
2506 username_len = strlen (pea.username);
2507 if ((PART_ENCRYPTION_AES256_SIZE + username_len) > BUFF_SIG_SIZE)
2508 {
2509 ERROR ("network plugin: Username too long: %s", pea.username);
2510 return;
2511 }
2513 buffer_size = PART_ENCRYPTION_AES256_SIZE + username_len + in_buffer_size;
2514 header_size = PART_ENCRYPTION_AES256_SIZE + username_len
2515 - sizeof (pea.hash);
2517 assert (buffer_size <= sizeof (buffer));
2518 DEBUG ("network plugin: networt_send_buffer_encrypted: "
2519 "buffer_size = %zu;", buffer_size);
2521 pea.head.length = htons ((uint16_t) (PART_ENCRYPTION_AES256_SIZE
2522 + username_len + in_buffer_size));
2523 pea.username_length = htons ((uint16_t) username_len);
2525 /* Chose a random initialization vector. */
2526 gcry_randomize ((void *) &pea.iv, sizeof (pea.iv), GCRY_STRONG_RANDOM);
2528 /* Create hash of the payload */
2529 gcry_md_hash_buffer (GCRY_MD_SHA1, pea.hash, in_buffer, in_buffer_size);
2531 /* Initialize the buffer */
2532 buffer_offset = 0;
2533 memset (buffer, 0, sizeof (buffer));
2536 BUFFER_ADD (&pea.head.type, sizeof (pea.head.type));
2537 BUFFER_ADD (&pea.head.length, sizeof (pea.head.length));
2538 BUFFER_ADD (&pea.username_length, sizeof (pea.username_length));
2539 BUFFER_ADD (pea.username, username_len);
2540 BUFFER_ADD (pea.iv, sizeof (pea.iv));
2541 assert (buffer_offset == header_size);
2542 BUFFER_ADD (pea.hash, sizeof (pea.hash));
2543 BUFFER_ADD (in_buffer, in_buffer_size);
2545 assert (buffer_offset == buffer_size);
2547 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
2548 se->data.client.password);
2549 if (cypher == NULL)
2550 return;
2552 /* Encrypt the buffer in-place */
2553 err = gcry_cipher_encrypt (cypher,
2554 buffer + header_size,
2555 buffer_size - header_size,
2556 /* in = */ NULL, /* in len = */ 0);
2557 if (err != 0)
2558 {
2559 ERROR ("network plugin: gcry_cipher_encrypt returned: %s",
2560 gcry_strerror (err));
2561 return;
2562 }
2564 /* Send it out without further modifications */
2565 networt_send_buffer_plain (se, buffer, buffer_size);
2566 } /* }}} void networt_send_buffer_encrypted */
2567 #undef BUFFER_ADD
2568 #endif /* HAVE_LIBGCRYPT */
2570 static void network_send_buffer (char *buffer, size_t buffer_len) /* {{{ */
2571 {
2572 sockent_t *se;
2574 DEBUG ("network plugin: network_send_buffer: buffer_len = %zu", buffer_len);
2576 for (se = sending_sockets; se != NULL; se = se->next)
2577 {
2578 #if HAVE_LIBGCRYPT
2579 if (se->data.client.security_level == SECURITY_LEVEL_ENCRYPT)
2580 networt_send_buffer_encrypted (se, buffer, buffer_len);
2581 else if (se->data.client.security_level == SECURITY_LEVEL_SIGN)
2582 networt_send_buffer_signed (se, buffer, buffer_len);
2583 else /* if (se->data.client.security_level == SECURITY_LEVEL_NONE) */
2584 #endif /* HAVE_LIBGCRYPT */
2585 networt_send_buffer_plain (se, buffer, buffer_len);
2586 } /* for (sending_sockets) */
2587 } /* }}} void network_send_buffer */
2589 static int add_to_buffer (char *buffer, int buffer_size, /* {{{ */
2590 value_list_t *vl_def,
2591 const data_set_t *ds, const value_list_t *vl)
2592 {
2593 char *buffer_orig = buffer;
2595 if (strcmp (vl_def->host, vl->host) != 0)
2596 {
2597 if (write_part_string (&buffer, &buffer_size, TYPE_HOST,
2598 vl->host, strlen (vl->host)) != 0)
2599 return (-1);
2600 sstrncpy (vl_def->host, vl->host, sizeof (vl_def->host));
2601 }
2603 if (vl_def->time != vl->time)
2604 {
2605 if (write_part_number (&buffer, &buffer_size, TYPE_TIME_HR,
2606 (uint64_t) vl->time))
2607 return (-1);
2608 vl_def->time = vl->time;
2609 }
2611 if (vl_def->interval != vl->interval)
2612 {
2613 if (write_part_number (&buffer, &buffer_size, TYPE_INTERVAL_HR,
2614 (uint64_t) vl->interval))
2615 return (-1);
2616 vl_def->interval = vl->interval;
2617 }
2619 if (strcmp (vl_def->plugin, vl->plugin) != 0)
2620 {
2621 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN,
2622 vl->plugin, strlen (vl->plugin)) != 0)
2623 return (-1);
2624 sstrncpy (vl_def->plugin, vl->plugin, sizeof (vl_def->plugin));
2625 }
2627 if (strcmp (vl_def->plugin_instance, vl->plugin_instance) != 0)
2628 {
2629 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
2630 vl->plugin_instance,
2631 strlen (vl->plugin_instance)) != 0)
2632 return (-1);
2633 sstrncpy (vl_def->plugin_instance, vl->plugin_instance, sizeof (vl_def->plugin_instance));
2634 }
2636 if (strcmp (vl_def->type, vl->type) != 0)
2637 {
2638 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
2639 vl->type, strlen (vl->type)) != 0)
2640 return (-1);
2641 sstrncpy (vl_def->type, ds->type, sizeof (vl_def->type));
2642 }
2644 if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
2645 {
2646 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE_INSTANCE,
2647 vl->type_instance,
2648 strlen (vl->type_instance)) != 0)
2649 return (-1);
2650 sstrncpy (vl_def->type_instance, vl->type_instance, sizeof (vl_def->type_instance));
2651 }
2653 if (write_part_values (&buffer, &buffer_size, ds, vl) != 0)
2654 return (-1);
2656 return (buffer - buffer_orig);
2657 } /* }}} int add_to_buffer */
2659 static void flush_buffer (void)
2660 {
2661 DEBUG ("network plugin: flush_buffer: send_buffer_fill = %i",
2662 send_buffer_fill);
2664 network_send_buffer (send_buffer, (size_t) send_buffer_fill);
2666 stats_octets_tx += ((uint64_t) send_buffer_fill);
2667 stats_packets_tx++;
2669 network_init_buffer ();
2670 }
2672 static int network_write (const data_set_t *ds, const value_list_t *vl,
2673 user_data_t __attribute__((unused)) *user_data)
2674 {
2675 int status;
2677 if (!check_send_okay (vl))
2678 {
2679 #if COLLECT_DEBUG
2680 char name[6*DATA_MAX_NAME_LEN];
2681 FORMAT_VL (name, sizeof (name), vl);
2682 name[sizeof (name) - 1] = 0;
2683 DEBUG ("network plugin: network_write: "
2684 "NOT sending %s.", name);
2685 #endif
2686 /* Counter is not protected by another lock and may be reached by
2687 * multiple threads */
2688 pthread_mutex_lock (&stats_lock);
2689 stats_values_not_sent++;
2690 pthread_mutex_unlock (&stats_lock);
2691 return (0);
2692 }
2694 uc_meta_data_add_unsigned_int (vl,
2695 "network:time_sent", (uint64_t) vl->time);
2697 pthread_mutex_lock (&send_buffer_lock);
2699 status = add_to_buffer (send_buffer_ptr,
2700 network_config_packet_size - (send_buffer_fill + BUFF_SIG_SIZE),
2701 &send_buffer_vl,
2702 ds, vl);
2703 if (status >= 0)
2704 {
2705 /* status == bytes added to the buffer */
2706 send_buffer_fill += status;
2707 send_buffer_ptr += status;
2709 stats_values_sent++;
2710 }
2711 else
2712 {
2713 flush_buffer ();
2715 status = add_to_buffer (send_buffer_ptr,
2716 network_config_packet_size - (send_buffer_fill + BUFF_SIG_SIZE),
2717 &send_buffer_vl,
2718 ds, vl);
2720 if (status >= 0)
2721 {
2722 send_buffer_fill += status;
2723 send_buffer_ptr += status;
2725 stats_values_sent++;
2726 }
2727 }
2729 if (status < 0)
2730 {
2731 ERROR ("network plugin: Unable to append to the "
2732 "buffer for some weird reason");
2733 }
2734 else if ((network_config_packet_size - send_buffer_fill) < 15)
2735 {
2736 flush_buffer ();
2737 }
2739 pthread_mutex_unlock (&send_buffer_lock);
2741 return ((status < 0) ? -1 : 0);
2742 } /* int network_write */
2744 static int network_config_set_boolean (const oconfig_item_t *ci, /* {{{ */
2745 int *retval)
2746 {
2747 if ((ci->values_num != 1)
2748 || ((ci->values[0].type != OCONFIG_TYPE_BOOLEAN)
2749 && (ci->values[0].type != OCONFIG_TYPE_STRING)))
2750 {
2751 ERROR ("network plugin: The `%s' config option needs "
2752 "exactly one boolean argument.", ci->key);
2753 return (-1);
2754 }
2756 if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
2757 {
2758 if (ci->values[0].value.boolean)
2759 *retval = 1;
2760 else
2761 *retval = 0;
2762 }
2763 else
2764 {
2765 char *str = ci->values[0].value.string;
2767 if (IS_TRUE (str))
2768 *retval = 1;
2769 else if (IS_FALSE (str))
2770 *retval = 0;
2771 else
2772 {
2773 ERROR ("network plugin: Cannot parse string value `%s' of the `%s' "
2774 "option as boolean value.",
2775 str, ci->key);
2776 return (-1);
2777 }
2778 }
2780 return (0);
2781 } /* }}} int network_config_set_boolean */
2783 static int network_config_set_ttl (const oconfig_item_t *ci) /* {{{ */
2784 {
2785 int tmp;
2786 if ((ci->values_num != 1)
2787 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
2788 {
2789 WARNING ("network plugin: The `TimeToLive' config option needs exactly "
2790 "one numeric argument.");
2791 return (-1);
2792 }
2794 tmp = (int) ci->values[0].value.number;
2795 if ((tmp > 0) && (tmp <= 255))
2796 network_config_ttl = tmp;
2798 return (0);
2799 } /* }}} int network_config_set_ttl */
2801 static int network_config_set_interface (const oconfig_item_t *ci, /* {{{ */
2802 int *interface)
2803 {
2804 if ((ci->values_num != 1)
2805 || (ci->values[0].type != OCONFIG_TYPE_STRING))
2806 {
2807 WARNING ("network plugin: The `Interface' config option needs exactly "
2808 "one string argument.");
2809 return (-1);
2810 }
2812 if (interface == NULL)
2813 return (-1);
2815 *interface = if_nametoindex (ci->values[0].value.string);
2817 return (0);
2818 } /* }}} int network_config_set_interface */
2820 static int network_config_set_buffer_size (const oconfig_item_t *ci) /* {{{ */
2821 {
2822 int tmp;
2823 if ((ci->values_num != 1)
2824 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
2825 {
2826 WARNING ("network plugin: The `MaxPacketSize' config option needs exactly "
2827 "one numeric argument.");
2828 return (-1);
2829 }
2831 tmp = (int) ci->values[0].value.number;
2832 if ((tmp >= 1024) && (tmp <= 65535))
2833 network_config_packet_size = tmp;
2835 return (0);
2836 } /* }}} int network_config_set_buffer_size */
2838 #if HAVE_LIBGCRYPT
2839 static int network_config_set_string (const oconfig_item_t *ci, /* {{{ */
2840 char **ret_string)
2841 {
2842 char *tmp;
2843 if ((ci->values_num != 1)
2844 || (ci->values[0].type != OCONFIG_TYPE_STRING))
2845 {
2846 WARNING ("network plugin: The `%s' config option needs exactly "
2847 "one string argument.", ci->key);
2848 return (-1);
2849 }
2851 tmp = strdup (ci->values[0].value.string);
2852 if (tmp == NULL)
2853 return (-1);
2855 sfree (*ret_string);
2856 *ret_string = tmp;
2858 return (0);
2859 } /* }}} int network_config_set_string */
2860 #endif /* HAVE_LIBGCRYPT */
2862 #if HAVE_LIBGCRYPT
2863 static int network_config_set_security_level (oconfig_item_t *ci, /* {{{ */
2864 int *retval)
2865 {
2866 char *str;
2867 if ((ci->values_num != 1)
2868 || (ci->values[0].type != OCONFIG_TYPE_STRING))
2869 {
2870 WARNING ("network plugin: The `SecurityLevel' config option needs exactly "
2871 "one string argument.");
2872 return (-1);
2873 }
2875 str = ci->values[0].value.string;
2876 if (strcasecmp ("Encrypt", str) == 0)
2877 *retval = SECURITY_LEVEL_ENCRYPT;
2878 else if (strcasecmp ("Sign", str) == 0)
2879 *retval = SECURITY_LEVEL_SIGN;
2880 else if (strcasecmp ("None", str) == 0)
2881 *retval = SECURITY_LEVEL_NONE;
2882 else
2883 {
2884 WARNING ("network plugin: Unknown security level: %s.", str);
2885 return (-1);
2886 }
2888 return (0);
2889 } /* }}} int network_config_set_security_level */
2890 #endif /* HAVE_LIBGCRYPT */
2892 static int network_config_add_listen (const oconfig_item_t *ci) /* {{{ */
2893 {
2894 sockent_t *se;
2895 int status;
2896 int i;
2898 if ((ci->values_num < 1) || (ci->values_num > 2)
2899 || (ci->values[0].type != OCONFIG_TYPE_STRING)
2900 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
2901 {
2902 ERROR ("network plugin: The `%s' config option needs "
2903 "one or two string arguments.", ci->key);
2904 return (-1);
2905 }
2907 se = malloc (sizeof (*se));
2908 if (se == NULL)
2909 {
2910 ERROR ("network plugin: malloc failed.");
2911 return (-1);
2912 }
2913 sockent_init (se, SOCKENT_TYPE_SERVER);
2915 se->node = strdup (ci->values[0].value.string);
2916 if (ci->values_num >= 2)
2917 se->service = strdup (ci->values[1].value.string);
2919 for (i = 0; i < ci->children_num; i++)
2920 {
2921 oconfig_item_t *child = ci->children + i;
2923 #if HAVE_LIBGCRYPT
2924 if (strcasecmp ("AuthFile", child->key) == 0)
2925 network_config_set_string (child, &se->data.server.auth_file);
2926 else if (strcasecmp ("SecurityLevel", child->key) == 0)
2927 network_config_set_security_level (child,
2928 &se->data.server.security_level);
2929 else
2930 #endif /* HAVE_LIBGCRYPT */
2931 if (strcasecmp ("Interface", child->key) == 0)
2932 network_config_set_interface (child,
2933 &se->interface);
2934 else
2935 {
2936 WARNING ("network plugin: Option `%s' is not allowed here.",
2937 child->key);
2938 }
2939 }
2941 #if HAVE_LIBGCRYPT
2942 if ((se->data.server.security_level > SECURITY_LEVEL_NONE)
2943 && (se->data.server.auth_file == NULL))
2944 {
2945 ERROR ("network plugin: A security level higher than `none' was "
2946 "requested, but no AuthFile option was given. Cowardly refusing to "
2947 "open this socket!");
2948 sockent_destroy (se);
2949 return (-1);
2950 }
2951 #endif /* HAVE_LIBGCRYPT */
2953 status = sockent_open (se);
2954 if (status != 0)
2955 {
2956 ERROR ("network plugin: network_config_add_listen: sockent_open failed.");
2957 sockent_destroy (se);
2958 return (-1);
2959 }
2961 status = sockent_add (se);
2962 if (status != 0)
2963 {
2964 ERROR ("network plugin: network_config_add_listen: sockent_add failed.");
2965 sockent_destroy (se);
2966 return (-1);
2967 }
2969 return (0);
2970 } /* }}} int network_config_add_listen */
2972 static int network_config_add_server (const oconfig_item_t *ci) /* {{{ */
2973 {
2974 sockent_t *se;
2975 int status;
2976 int i;
2978 if ((ci->values_num < 1) || (ci->values_num > 2)
2979 || (ci->values[0].type != OCONFIG_TYPE_STRING)
2980 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
2981 {
2982 ERROR ("network plugin: The `%s' config option needs "
2983 "one or two string arguments.", ci->key);
2984 return (-1);
2985 }
2987 se = malloc (sizeof (*se));
2988 if (se == NULL)
2989 {
2990 ERROR ("network plugin: malloc failed.");
2991 return (-1);
2992 }
2993 sockent_init (se, SOCKENT_TYPE_CLIENT);
2995 se->node = strdup (ci->values[0].value.string);
2996 if (ci->values_num >= 2)
2997 se->service = strdup (ci->values[1].value.string);
2999 for (i = 0; i < ci->children_num; i++)
3000 {
3001 oconfig_item_t *child = ci->children + i;
3003 #if HAVE_LIBGCRYPT
3004 if (strcasecmp ("Username", child->key) == 0)
3005 network_config_set_string (child, &se->data.client.username);
3006 else if (strcasecmp ("Password", child->key) == 0)
3007 network_config_set_string (child, &se->data.client.password);
3008 else if (strcasecmp ("SecurityLevel", child->key) == 0)
3009 network_config_set_security_level (child,
3010 &se->data.client.security_level);
3011 else
3012 #endif /* HAVE_LIBGCRYPT */
3013 if (strcasecmp ("Interface", child->key) == 0)
3014 network_config_set_interface (child,
3015 &se->interface);
3016 else
3017 {
3018 WARNING ("network plugin: Option `%s' is not allowed here.",
3019 child->key);
3020 }
3021 }
3023 #if HAVE_LIBGCRYPT
3024 if ((se->data.client.security_level > SECURITY_LEVEL_NONE)
3025 && ((se->data.client.username == NULL)
3026 || (se->data.client.password == NULL)))
3027 {
3028 ERROR ("network plugin: A security level higher than `none' was "
3029 "requested, but no Username or Password option was given. "
3030 "Cowardly refusing to open this socket!");
3031 sockent_destroy (se);
3032 return (-1);
3033 }
3034 #endif /* HAVE_LIBGCRYPT */
3036 status = sockent_open (se);
3037 if (status != 0)
3038 {
3039 ERROR ("network plugin: network_config_add_server: sockent_open failed.");
3040 sockent_destroy (se);
3041 return (-1);
3042 }
3044 status = sockent_add (se);
3045 if (status != 0)
3046 {
3047 ERROR ("network plugin: network_config_add_server: sockent_add failed.");
3048 sockent_destroy (se);
3049 return (-1);
3050 }
3052 return (0);
3053 } /* }}} int network_config_add_server */
3055 static int network_config (oconfig_item_t *ci) /* {{{ */
3056 {
3057 int i;
3059 for (i = 0; i < ci->children_num; i++)
3060 {
3061 oconfig_item_t *child = ci->children + i;
3063 if (strcasecmp ("Listen", child->key) == 0)
3064 network_config_add_listen (child);
3065 else if (strcasecmp ("Server", child->key) == 0)
3066 network_config_add_server (child);
3067 else if (strcasecmp ("TimeToLive", child->key) == 0)
3068 network_config_set_ttl (child);
3069 else if (strcasecmp ("MaxPacketSize", child->key) == 0)
3070 network_config_set_buffer_size (child);
3071 else if (strcasecmp ("Forward", child->key) == 0)
3072 network_config_set_boolean (child, &network_config_forward);
3073 else if (strcasecmp ("ReportStats", child->key) == 0)
3074 network_config_set_boolean (child, &network_config_stats);
3075 else
3076 {
3077 WARNING ("network plugin: Option `%s' is not allowed here.",
3078 child->key);
3079 }
3080 }
3082 return (0);
3083 } /* }}} int network_config */
3085 static int network_notification (const notification_t *n,
3086 user_data_t __attribute__((unused)) *user_data)
3087 {
3088 char buffer[network_config_packet_size];
3089 char *buffer_ptr = buffer;
3090 int buffer_free = sizeof (buffer);
3091 int status;
3093 memset (buffer, '\0', sizeof (buffer));
3095 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_TIME_HR,
3096 (uint64_t) n->time);
3097 if (status != 0)
3098 return (-1);
3100 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_SEVERITY,
3101 (uint64_t) n->severity);
3102 if (status != 0)
3103 return (-1);
3105 if (strlen (n->host) > 0)
3106 {
3107 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_HOST,
3108 n->host, strlen (n->host));
3109 if (status != 0)
3110 return (-1);
3111 }
3113 if (strlen (n->plugin) > 0)
3114 {
3115 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_PLUGIN,
3116 n->plugin, strlen (n->plugin));
3117 if (status != 0)
3118 return (-1);
3119 }
3121 if (strlen (n->plugin_instance) > 0)
3122 {
3123 status = write_part_string (&buffer_ptr, &buffer_free,
3124 TYPE_PLUGIN_INSTANCE,
3125 n->plugin_instance, strlen (n->plugin_instance));
3126 if (status != 0)
3127 return (-1);
3128 }
3130 if (strlen (n->type) > 0)
3131 {
3132 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE,
3133 n->type, strlen (n->type));
3134 if (status != 0)
3135 return (-1);
3136 }
3138 if (strlen (n->type_instance) > 0)
3139 {
3140 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE_INSTANCE,
3141 n->type_instance, strlen (n->type_instance));
3142 if (status != 0)
3143 return (-1);
3144 }
3146 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_MESSAGE,
3147 n->message, strlen (n->message));
3148 if (status != 0)
3149 return (-1);
3151 network_send_buffer (buffer, sizeof (buffer) - buffer_free);
3153 return (0);
3154 } /* int network_notification */
3156 static int network_shutdown (void)
3157 {
3158 listen_loop++;
3160 /* Kill the listening thread */
3161 if (receive_thread_running != 0)
3162 {
3163 INFO ("network plugin: Stopping receive thread.");
3164 pthread_kill (receive_thread_id, SIGTERM);
3165 pthread_join (receive_thread_id, NULL /* no return value */);
3166 memset (&receive_thread_id, 0, sizeof (receive_thread_id));
3167 receive_thread_running = 0;
3168 }
3170 /* Shutdown the dispatching thread */
3171 if (dispatch_thread_running != 0)
3172 {
3173 INFO ("network plugin: Stopping dispatch thread.");
3174 pthread_mutex_lock (&receive_list_lock);
3175 pthread_cond_broadcast (&receive_list_cond);
3176 pthread_mutex_unlock (&receive_list_lock);
3177 pthread_join (dispatch_thread_id, /* ret = */ NULL);
3178 dispatch_thread_running = 0;
3179 }
3181 sockent_destroy (listen_sockets);
3183 if (send_buffer_fill > 0)
3184 flush_buffer ();
3186 sfree (send_buffer);
3188 /* TODO: Close `sending_sockets' */
3190 plugin_unregister_config ("network");
3191 plugin_unregister_init ("network");
3192 plugin_unregister_write ("network");
3193 plugin_unregister_shutdown ("network");
3195 return (0);
3196 } /* int network_shutdown */
3198 static int network_stats_read (void) /* {{{ */
3199 {
3200 uint64_t copy_octets_rx;
3201 uint64_t copy_octets_tx;
3202 uint64_t copy_packets_rx;
3203 uint64_t copy_packets_tx;
3204 uint64_t copy_values_dispatched;
3205 uint64_t copy_values_not_dispatched;
3206 uint64_t copy_values_sent;
3207 uint64_t copy_values_not_sent;
3208 uint64_t copy_receive_list_length;
3209 value_list_t vl = VALUE_LIST_INIT;
3210 value_t values[2];
3212 copy_octets_rx = stats_octets_rx;
3213 copy_octets_tx = stats_octets_tx;
3214 copy_packets_rx = stats_packets_rx;
3215 copy_packets_tx = stats_packets_tx;
3216 copy_values_dispatched = stats_values_dispatched;
3217 copy_values_not_dispatched = stats_values_not_dispatched;
3218 copy_values_sent = stats_values_sent;
3219 copy_values_not_sent = stats_values_not_sent;
3220 copy_receive_list_length = receive_list_length;
3222 /* Initialize `vl' */
3223 vl.values = values;
3224 vl.values_len = 2;
3225 vl.time = 0;
3226 vl.interval = interval_g;
3227 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
3228 sstrncpy (vl.plugin, "network", sizeof (vl.plugin));
3230 /* Octets received / sent */
3231 vl.values[0].counter = (counter_t) copy_octets_rx;
3232 vl.values[1].counter = (counter_t) copy_octets_tx;
3233 sstrncpy (vl.type, "if_octets", sizeof (vl.type));
3234 plugin_dispatch_values (&vl);
3236 /* Packets received / send */
3237 vl.values[0].counter = (counter_t) copy_packets_rx;
3238 vl.values[1].counter = (counter_t) copy_packets_tx;
3239 sstrncpy (vl.type, "if_packets", sizeof (vl.type));
3240 plugin_dispatch_values (&vl);
3242 /* Values (not) dispatched and (not) send */
3243 sstrncpy (vl.type, "total_values", sizeof (vl.type));
3244 vl.values_len = 1;
3246 vl.values[0].derive = (derive_t) copy_values_dispatched;
3247 sstrncpy (vl.type_instance, "dispatch-accepted",
3248 sizeof (vl.type_instance));
3249 plugin_dispatch_values (&vl);
3251 vl.values[0].derive = (derive_t) copy_values_not_dispatched;
3252 sstrncpy (vl.type_instance, "dispatch-rejected",
3253 sizeof (vl.type_instance));
3254 plugin_dispatch_values (&vl);
3256 vl.values[0].derive = (derive_t) copy_values_sent;
3257 sstrncpy (vl.type_instance, "send-accepted",
3258 sizeof (vl.type_instance));
3259 plugin_dispatch_values (&vl);
3261 vl.values[0].derive = (derive_t) copy_values_not_sent;
3262 sstrncpy (vl.type_instance, "send-rejected",
3263 sizeof (vl.type_instance));
3264 plugin_dispatch_values (&vl);
3266 /* Receive queue length */
3267 vl.values[0].gauge = (gauge_t) copy_receive_list_length;
3268 sstrncpy (vl.type, "queue_length", sizeof (vl.type));
3269 vl.type_instance[0] = 0;
3270 plugin_dispatch_values (&vl);
3272 return (0);
3273 } /* }}} int network_stats_read */
3275 static int network_init (void)
3276 {
3277 static _Bool have_init = 0;
3279 /* Check if we were already initialized. If so, just return - there's
3280 * nothing more to do (for now, that is). */
3281 if (have_init)
3282 return (0);
3283 have_init = 1;
3285 #if HAVE_LIBGCRYPT
3286 gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
3287 gcry_control (GCRYCTL_INIT_SECMEM, 32768, 0);
3288 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
3289 #endif
3291 if (network_config_stats != 0)
3292 plugin_register_read ("network", network_stats_read);
3294 plugin_register_shutdown ("network", network_shutdown);
3296 send_buffer = malloc (network_config_packet_size);
3297 if (send_buffer == NULL)
3298 {
3299 ERROR ("network plugin: malloc failed.");
3300 return (-1);
3301 }
3302 network_init_buffer ();
3304 /* setup socket(s) and so on */
3305 if (sending_sockets != NULL)
3306 {
3307 plugin_register_write ("network", network_write,
3308 /* user_data = */ NULL);
3309 plugin_register_notification ("network", network_notification,
3310 /* user_data = */ NULL);
3311 }
3313 /* If no threads need to be started, return here. */
3314 if ((listen_sockets_num == 0)
3315 || ((dispatch_thread_running != 0)
3316 && (receive_thread_running != 0)))
3317 return (0);
3319 if (dispatch_thread_running == 0)
3320 {
3321 int status;
3322 status = pthread_create (&dispatch_thread_id,
3323 NULL /* no attributes */,
3324 dispatch_thread,
3325 NULL /* no argument */);
3326 if (status != 0)
3327 {
3328 char errbuf[1024];
3329 ERROR ("network: pthread_create failed: %s",
3330 sstrerror (errno, errbuf,
3331 sizeof (errbuf)));
3332 }
3333 else
3334 {
3335 dispatch_thread_running = 1;
3336 }
3337 }
3339 if (receive_thread_running == 0)
3340 {
3341 int status;
3342 status = pthread_create (&receive_thread_id,
3343 NULL /* no attributes */,
3344 receive_thread,
3345 NULL /* no argument */);
3346 if (status != 0)
3347 {
3348 char errbuf[1024];
3349 ERROR ("network: pthread_create failed: %s",
3350 sstrerror (errno, errbuf,
3351 sizeof (errbuf)));
3352 }
3353 else
3354 {
3355 receive_thread_running = 1;
3356 }
3357 }
3359 return (0);
3360 } /* int network_init */
3362 /*
3363 * The flush option of the network plugin cannot flush individual identifiers.
3364 * All the values are added to a buffer and sent when the buffer is full, the
3365 * requested value may or may not be in there, it's not worth finding out. We
3366 * just send the buffer if `flush' is called - if the requested value was in
3367 * there, good. If not, well, then there is nothing to flush.. -octo
3368 */
3369 static int network_flush (__attribute__((unused)) cdtime_t timeout,
3370 __attribute__((unused)) const char *identifier,
3371 __attribute__((unused)) user_data_t *user_data)
3372 {
3373 pthread_mutex_lock (&send_buffer_lock);
3375 if (send_buffer_fill > 0)
3376 flush_buffer ();
3378 pthread_mutex_unlock (&send_buffer_lock);
3380 return (0);
3381 } /* int network_flush */
3383 void module_register (void)
3384 {
3385 plugin_register_complex_config ("network", network_config);
3386 plugin_register_init ("network", network_init);
3387 plugin_register_flush ("network", network_flush,
3388 /* user_data = */ NULL);
3389 } /* void module_register */
3391 /* vim: set fdm=marker : */