1 /**
2 * collectd - src/network.c
3 * Copyright (C) 2005-2009 Florian octo Forster
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * Authors:
19 * Florian octo Forster <octo at verplant.org>
20 **/
22 #include "collectd.h"
23 #include "plugin.h"
24 #include "common.h"
25 #include "configfile.h"
26 #include "utils_avltree.h"
28 #include "network.h"
30 #if HAVE_PTHREAD_H
31 # include <pthread.h>
32 #endif
33 #if HAVE_SYS_SOCKET_H
34 # include <sys/socket.h>
35 #endif
36 #if HAVE_NETDB_H
37 # include <netdb.h>
38 #endif
39 #if HAVE_NETINET_IN_H
40 # include <netinet/in.h>
41 #endif
42 #if HAVE_ARPA_INET_H
43 # include <arpa/inet.h>
44 #endif
45 #if HAVE_POLL_H
46 # include <poll.h>
47 #endif
49 #if HAVE_GCRYPT_H
50 # include <gcrypt.h>
51 #endif
53 /* 1500 - 40 - 8 = Ethernet packet - IPv6 header - UDP header */
54 /* #define BUFF_SIZE 1452 */
56 #ifndef IPV6_ADD_MEMBERSHIP
57 # ifdef IPV6_JOIN_GROUP
58 # define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
59 # else
60 # error "Neither IP_ADD_MEMBERSHIP nor IPV6_JOIN_GROUP is defined"
61 # endif
62 #endif /* !IP_ADD_MEMBERSHIP */
64 /* Buffer size to allocate. */
65 #define BUFF_SIZE 1024
67 /*
68 * Maximum size required for encryption / signing:
69 * Type/length: 4
70 * Hash/orig length: 32
71 * Padding (up to): 16
72 * --------------------
73 * 52
74 */
75 #define BUFF_SIG_SIZE 52
77 /*
78 * Private data types
79 */
80 typedef struct sockent
81 {
82 int fd;
83 struct sockaddr_storage *addr;
84 socklen_t addrlen;
86 #define SECURITY_LEVEL_NONE 0
87 #if HAVE_GCRYPT_H
88 # define SECURITY_LEVEL_SIGN 1
89 # define SECURITY_LEVEL_ENCRYPT 2
90 int security_level;
91 char *shared_secret;
92 gcry_cipher_hd_t cypher;
93 #endif /* HAVE_GCRYPT_H */
95 struct sockent *next;
96 } sockent_t;
98 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
99 * 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
100 * +-------+-----------------------+-------------------------------+
101 * ! Ver. ! ! Length !
102 * +-------+-----------------------+-------------------------------+
103 */
104 struct part_header_s
105 {
106 uint16_t type;
107 uint16_t length;
108 };
109 typedef struct part_header_s part_header_t;
111 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
112 * 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
113 * +-------------------------------+-------------------------------+
114 * ! Type ! Length !
115 * +-------------------------------+-------------------------------+
116 * : (Length - 4) Bytes :
117 * +---------------------------------------------------------------+
118 */
119 struct part_string_s
120 {
121 part_header_t *head;
122 char *value;
123 };
124 typedef struct part_string_s part_string_t;
126 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
127 * 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
128 * +-------------------------------+-------------------------------+
129 * ! Type ! Length !
130 * +-------------------------------+-------------------------------+
131 * : (Length - 4 == 2 || 4 || 8) Bytes :
132 * +---------------------------------------------------------------+
133 */
134 struct part_number_s
135 {
136 part_header_t *head;
137 uint64_t *value;
138 };
139 typedef struct part_number_s part_number_t;
141 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
142 * 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
143 * +-------------------------------+-------------------------------+
144 * ! Type ! Length !
145 * +-------------------------------+---------------+---------------+
146 * ! Num of values ! Type0 ! Type1 !
147 * +-------------------------------+---------------+---------------+
148 * ! Value0 !
149 * ! !
150 * +---------------------------------------------------------------+
151 * ! Value1 !
152 * ! !
153 * +---------------------------------------------------------------+
154 */
155 struct part_values_s
156 {
157 part_header_t *head;
158 uint16_t *num_values;
159 uint8_t *values_types;
160 value_t *values;
161 };
162 typedef struct part_values_s part_values_t;
164 struct part_signature_sha256_s
165 {
166 part_header_t head;
167 char hash[32];
168 };
169 typedef struct part_signature_sha256_s part_signature_sha256_t;
171 struct part_encryption_aes256_s
172 {
173 part_header_t head;
174 uint16_t orig_length;
175 uint16_t random;
176 char hash[28];
177 };
178 typedef struct part_encryption_aes256_s part_encryption_aes256_t;
180 struct receive_list_entry_s
181 {
182 char data[BUFF_SIZE];
183 int data_len;
184 int fd;
185 struct receive_list_entry_s *next;
186 };
187 typedef struct receive_list_entry_s receive_list_entry_t;
189 /*
190 * Private variables
191 */
192 static int network_config_ttl = 0;
193 static int network_config_forward = 0;
195 static sockent_t *sending_sockets = NULL;
197 static receive_list_entry_t *receive_list_head = NULL;
198 static receive_list_entry_t *receive_list_tail = NULL;
199 static pthread_mutex_t receive_list_lock = PTHREAD_MUTEX_INITIALIZER;
200 static pthread_cond_t receive_list_cond = PTHREAD_COND_INITIALIZER;
202 static sockent_t *listen_sockets = NULL;
203 static struct pollfd *listen_sockets_pollfd = NULL;
204 static int listen_sockets_num = 0;
206 /* The receive and dispatch threads will run as long as `listen_loop' is set to
207 * zero. */
208 static int listen_loop = 0;
209 static int receive_thread_running = 0;
210 static pthread_t receive_thread_id;
211 static int dispatch_thread_running = 0;
212 static pthread_t dispatch_thread_id;
214 /* Buffer in which to-be-sent network packets are constructed. */
215 static char send_buffer[BUFF_SIZE];
216 static char *send_buffer_ptr;
217 static int send_buffer_fill;
218 static value_list_t send_buffer_vl = VALUE_LIST_STATIC;
219 static pthread_mutex_t send_buffer_lock = PTHREAD_MUTEX_INITIALIZER;
221 /* In this cache we store all the values we received, so we can send out only
222 * those values which were *not* received via the network plugin, too. This is
223 * used for the `Forward false' option. */
224 static c_avl_tree_t *cache_tree = NULL;
225 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
226 static time_t cache_flush_last = 0;
227 static int cache_flush_interval = 1800;
229 /*
230 * Private functions
231 */
232 static int cache_flush (void)
233 {
234 char **keys = NULL;
235 int keys_num = 0;
237 char **tmp;
238 int i;
240 char *key;
241 time_t *value;
242 c_avl_iterator_t *iter;
244 time_t curtime = time (NULL);
246 iter = c_avl_get_iterator (cache_tree);
247 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
248 {
249 if ((curtime - *value) <= cache_flush_interval)
250 continue;
251 tmp = (char **) realloc (keys,
252 (keys_num + 1) * sizeof (char *));
253 if (tmp == NULL)
254 {
255 sfree (keys);
256 c_avl_iterator_destroy (iter);
257 ERROR ("network plugin: cache_flush: realloc"
258 " failed.");
259 return (-1);
260 }
261 keys = tmp;
262 keys[keys_num] = key;
263 keys_num++;
264 } /* while (c_avl_iterator_next) */
265 c_avl_iterator_destroy (iter);
267 for (i = 0; i < keys_num; i++)
268 {
269 if (c_avl_remove (cache_tree, keys[i], (void *) &key,
270 (void *) &value) != 0)
271 {
272 WARNING ("network plugin: cache_flush: c_avl_remove"
273 " (%s) failed.", keys[i]);
274 continue;
275 }
277 sfree (key);
278 sfree (value);
279 }
281 sfree (keys);
283 DEBUG ("network plugin: cache_flush: Removed %i %s",
284 keys_num, (keys_num == 1) ? "entry" : "entries");
285 cache_flush_last = curtime;
286 return (0);
287 } /* int cache_flush */
289 static int cache_check (const value_list_t *vl)
290 {
291 char key[1024];
292 time_t *value = NULL;
293 int retval = -1;
295 if (cache_tree == NULL)
296 return (-1);
298 if (format_name (key, sizeof (key), vl->host, vl->plugin,
299 vl->plugin_instance, vl->type, vl->type_instance))
300 return (-1);
302 pthread_mutex_lock (&cache_lock);
304 if (c_avl_get (cache_tree, key, (void *) &value) == 0)
305 {
306 if (*value < vl->time)
307 {
308 *value = vl->time;
309 retval = 0;
310 }
311 else
312 {
313 DEBUG ("network plugin: cache_check: *value = %i >= vl->time = %i",
314 (int) *value, (int) vl->time);
315 retval = 1;
316 }
317 }
318 else
319 {
320 char *key_copy = strdup (key);
321 value = malloc (sizeof (time_t));
322 if ((key_copy != NULL) && (value != NULL))
323 {
324 *value = vl->time;
325 c_avl_insert (cache_tree, key_copy, value);
326 retval = 0;
327 }
328 else
329 {
330 sfree (key_copy);
331 sfree (value);
332 }
333 }
335 if ((time (NULL) - cache_flush_last) > cache_flush_interval)
336 cache_flush ();
338 pthread_mutex_unlock (&cache_lock);
340 return (retval);
341 } /* int cache_check */
343 static int write_part_values (char **ret_buffer, int *ret_buffer_len,
344 const data_set_t *ds, const value_list_t *vl)
345 {
346 char *packet_ptr;
347 int packet_len;
348 int num_values;
350 part_header_t pkg_ph;
351 uint16_t pkg_num_values;
352 uint8_t *pkg_values_types;
353 value_t *pkg_values;
355 int offset;
356 int i;
358 num_values = vl->values_len;
359 packet_len = sizeof (part_header_t) + sizeof (uint16_t)
360 + (num_values * sizeof (uint8_t))
361 + (num_values * sizeof (value_t));
363 if (*ret_buffer_len < packet_len)
364 return (-1);
366 pkg_values_types = (uint8_t *) malloc (num_values * sizeof (uint8_t));
367 if (pkg_values_types == NULL)
368 {
369 ERROR ("network plugin: write_part_values: malloc failed.");
370 return (-1);
371 }
373 pkg_values = (value_t *) malloc (num_values * sizeof (value_t));
374 if (pkg_values == NULL)
375 {
376 free (pkg_values_types);
377 ERROR ("network plugin: write_part_values: malloc failed.");
378 return (-1);
379 }
381 pkg_ph.type = htons (TYPE_VALUES);
382 pkg_ph.length = htons (packet_len);
384 pkg_num_values = htons ((uint16_t) vl->values_len);
386 for (i = 0; i < num_values; i++)
387 {
388 if (ds->ds[i].type == DS_TYPE_COUNTER)
389 {
390 pkg_values_types[i] = DS_TYPE_COUNTER;
391 pkg_values[i].counter = htonll (vl->values[i].counter);
392 }
393 else
394 {
395 pkg_values_types[i] = DS_TYPE_GAUGE;
396 pkg_values[i].gauge = htond (vl->values[i].gauge);
397 }
398 }
400 /*
401 * Use `memcpy' to write everything to the buffer, because the pointer
402 * may be unaligned and some architectures, such as SPARC, can't handle
403 * that.
404 */
405 packet_ptr = *ret_buffer;
406 offset = 0;
407 memcpy (packet_ptr + offset, &pkg_ph, sizeof (pkg_ph));
408 offset += sizeof (pkg_ph);
409 memcpy (packet_ptr + offset, &pkg_num_values, sizeof (pkg_num_values));
410 offset += sizeof (pkg_num_values);
411 memcpy (packet_ptr + offset, pkg_values_types, num_values * sizeof (uint8_t));
412 offset += num_values * sizeof (uint8_t);
413 memcpy (packet_ptr + offset, pkg_values, num_values * sizeof (value_t));
414 offset += num_values * sizeof (value_t);
416 assert (offset == packet_len);
418 *ret_buffer = packet_ptr + packet_len;
419 *ret_buffer_len -= packet_len;
421 free (pkg_values_types);
422 free (pkg_values);
424 return (0);
425 } /* int write_part_values */
427 static int write_part_number (char **ret_buffer, int *ret_buffer_len,
428 int type, uint64_t value)
429 {
430 char *packet_ptr;
431 int packet_len;
433 part_header_t pkg_head;
434 uint64_t pkg_value;
436 int offset;
438 packet_len = sizeof (pkg_head) + sizeof (pkg_value);
440 if (*ret_buffer_len < packet_len)
441 return (-1);
443 pkg_head.type = htons (type);
444 pkg_head.length = htons (packet_len);
445 pkg_value = htonll (value);
447 packet_ptr = *ret_buffer;
448 offset = 0;
449 memcpy (packet_ptr + offset, &pkg_head, sizeof (pkg_head));
450 offset += sizeof (pkg_head);
451 memcpy (packet_ptr + offset, &pkg_value, sizeof (pkg_value));
452 offset += sizeof (pkg_value);
454 assert (offset == packet_len);
456 *ret_buffer = packet_ptr + packet_len;
457 *ret_buffer_len -= packet_len;
459 return (0);
460 } /* int write_part_number */
462 static int write_part_string (char **ret_buffer, int *ret_buffer_len,
463 int type, const char *str, int str_len)
464 {
465 char *buffer;
466 int buffer_len;
468 uint16_t pkg_type;
469 uint16_t pkg_length;
471 int offset;
473 buffer_len = 2 * sizeof (uint16_t) + str_len + 1;
474 if (*ret_buffer_len < buffer_len)
475 return (-1);
477 pkg_type = htons (type);
478 pkg_length = htons (buffer_len);
480 buffer = *ret_buffer;
481 offset = 0;
482 memcpy (buffer + offset, (void *) &pkg_type, sizeof (pkg_type));
483 offset += sizeof (pkg_type);
484 memcpy (buffer + offset, (void *) &pkg_length, sizeof (pkg_length));
485 offset += sizeof (pkg_length);
486 memcpy (buffer + offset, str, str_len);
487 offset += str_len;
488 memset (buffer + offset, '\0', 1);
489 offset += 1;
491 assert (offset == buffer_len);
493 *ret_buffer = buffer + buffer_len;
494 *ret_buffer_len -= buffer_len;
496 return (0);
497 } /* int write_part_string */
499 static int parse_part_values (void **ret_buffer, int *ret_buffer_len,
500 value_t **ret_values, int *ret_num_values)
501 {
502 char *buffer = *ret_buffer;
503 int buffer_len = *ret_buffer_len;
505 uint16_t tmp16;
506 size_t exp_size;
507 int i;
509 uint16_t pkg_length;
510 uint16_t pkg_type;
511 uint16_t pkg_numval;
513 uint8_t *pkg_types;
514 value_t *pkg_values;
516 if (buffer_len < (15))
517 {
518 DEBUG ("network plugin: packet is too short: buffer_len = %i",
519 buffer_len);
520 return (-1);
521 }
523 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
524 buffer += sizeof (tmp16);
525 pkg_type = ntohs (tmp16);
527 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
528 buffer += sizeof (tmp16);
529 pkg_length = ntohs (tmp16);
531 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
532 buffer += sizeof (tmp16);
533 pkg_numval = ntohs (tmp16);
535 assert (pkg_type == TYPE_VALUES);
537 exp_size = 3 * sizeof (uint16_t)
538 + pkg_numval * (sizeof (uint8_t) + sizeof (value_t));
539 if ((buffer_len < 0) || ((size_t) buffer_len < exp_size))
540 {
541 WARNING ("network plugin: parse_part_values: "
542 "Packet too short: "
543 "Chunk of size %u expected, "
544 "but buffer has only %i bytes left.",
545 (unsigned int) exp_size, buffer_len);
546 return (-1);
547 }
549 if (pkg_length != exp_size)
550 {
551 WARNING ("network plugin: parse_part_values: "
552 "Length and number of values "
553 "in the packet don't match.");
554 return (-1);
555 }
557 pkg_types = (uint8_t *) malloc (pkg_numval * sizeof (uint8_t));
558 pkg_values = (value_t *) malloc (pkg_numval * sizeof (value_t));
559 if ((pkg_types == NULL) || (pkg_values == NULL))
560 {
561 sfree (pkg_types);
562 sfree (pkg_values);
563 ERROR ("network plugin: parse_part_values: malloc failed.");
564 return (-1);
565 }
567 memcpy ((void *) pkg_types, (void *) buffer, pkg_numval * sizeof (uint8_t));
568 buffer += pkg_numval * sizeof (uint8_t);
569 memcpy ((void *) pkg_values, (void *) buffer, pkg_numval * sizeof (value_t));
570 buffer += pkg_numval * sizeof (value_t);
572 for (i = 0; i < pkg_numval; i++)
573 {
574 if (pkg_types[i] == DS_TYPE_COUNTER)
575 pkg_values[i].counter = ntohll (pkg_values[i].counter);
576 else if (pkg_types[i] == DS_TYPE_GAUGE)
577 pkg_values[i].gauge = ntohd (pkg_values[i].gauge);
578 }
580 *ret_buffer = buffer;
581 *ret_buffer_len = buffer_len - pkg_length;
582 *ret_num_values = pkg_numval;
583 *ret_values = pkg_values;
585 sfree (pkg_types);
587 return (0);
588 } /* int parse_part_values */
590 static int parse_part_number (void **ret_buffer, int *ret_buffer_len,
591 uint64_t *value)
592 {
593 char *buffer = *ret_buffer;
594 int buffer_len = *ret_buffer_len;
596 uint16_t tmp16;
597 uint64_t tmp64;
598 size_t exp_size = 2 * sizeof (uint16_t) + sizeof (uint64_t);
600 uint16_t pkg_length;
601 uint16_t pkg_type;
603 if ((buffer_len < 0) || ((size_t) buffer_len < exp_size))
604 {
605 WARNING ("network plugin: parse_part_number: "
606 "Packet too short: "
607 "Chunk of size %u expected, "
608 "but buffer has only %i bytes left.",
609 (unsigned int) exp_size, buffer_len);
610 return (-1);
611 }
613 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
614 buffer += sizeof (tmp16);
615 pkg_type = ntohs (tmp16);
617 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
618 buffer += sizeof (tmp16);
619 pkg_length = ntohs (tmp16);
621 memcpy ((void *) &tmp64, buffer, sizeof (tmp64));
622 buffer += sizeof (tmp64);
623 *value = ntohll (tmp64);
625 *ret_buffer = buffer;
626 *ret_buffer_len = buffer_len - pkg_length;
628 return (0);
629 } /* int parse_part_number */
631 static int parse_part_string (void **ret_buffer, int *ret_buffer_len,
632 char *output, int output_len)
633 {
634 char *buffer = *ret_buffer;
635 int buffer_len = *ret_buffer_len;
637 uint16_t tmp16;
638 size_t header_size = 2 * sizeof (uint16_t);
640 uint16_t pkg_length;
641 uint16_t pkg_type;
643 if ((buffer_len < 0) || ((size_t) buffer_len < header_size))
644 {
645 WARNING ("network plugin: parse_part_string: "
646 "Packet too short: "
647 "Chunk of at least size %u expected, "
648 "but buffer has only %i bytes left.",
649 (unsigned int) header_size, buffer_len);
650 return (-1);
651 }
653 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
654 buffer += sizeof (tmp16);
655 pkg_type = ntohs (tmp16);
657 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
658 buffer += sizeof (tmp16);
659 pkg_length = ntohs (tmp16);
661 /* Check that packet fits in the input buffer */
662 if (pkg_length > buffer_len)
663 {
664 WARNING ("network plugin: parse_part_string: "
665 "Packet too big: "
666 "Chunk of size %hu received, "
667 "but buffer has only %i bytes left.",
668 pkg_length, buffer_len);
669 return (-1);
670 }
672 /* Check that pkg_length is in the valid range */
673 if (pkg_length <= header_size)
674 {
675 WARNING ("network plugin: parse_part_string: "
676 "Packet too short: "
677 "Header claims this packet is only %hu "
678 "bytes long.", pkg_length);
679 return (-1);
680 }
682 /* Check that the package data fits into the output buffer.
683 * The previous if-statement ensures that:
684 * `pkg_length > header_size' */
685 if ((output_len < 0)
686 || ((size_t) output_len < ((size_t) pkg_length - header_size)))
687 {
688 WARNING ("network plugin: parse_part_string: "
689 "Output buffer too small.");
690 return (-1);
691 }
693 /* All sanity checks successfull, let's copy the data over */
694 output_len = pkg_length - header_size;
695 memcpy ((void *) output, (void *) buffer, output_len);
696 buffer += output_len;
698 /* For some very weird reason '\0' doesn't do the trick on SPARC in
699 * this statement. */
700 if (output[output_len - 1] != 0)
701 {
702 WARNING ("network plugin: parse_part_string: "
703 "Received string does not end "
704 "with a NULL-byte.");
705 return (-1);
706 }
708 *ret_buffer = buffer;
709 *ret_buffer_len = buffer_len - pkg_length;
711 return (0);
712 } /* int parse_part_string */
714 #if HAVE_GCRYPT_H
715 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
716 void **ret_buffer, int *ret_buffer_len)
717 {
718 char *buffer = *ret_buffer;
719 size_t buffer_len = (size_t) *ret_buffer_len;
721 part_signature_sha256_t ps_received;
722 part_signature_sha256_t ps_expected;
724 if (se->shared_secret == NULL)
725 {
726 NOTICE ("network plugin: Received signed network packet but can't verify "
727 "it because no shared secret has been configured. Will accept it.");
728 return (0);
729 }
731 if (buffer_len < sizeof (ps_received))
732 return (-ENOMEM);
734 memcpy (&ps_received, buffer, sizeof (ps_received));
736 memset (&ps_expected, 0, sizeof (ps_expected));
737 ps_expected.head.type = htons (TYPE_SIGN_SHA256);
738 ps_expected.head.length = htons (sizeof (ps_expected));
739 sstrncpy (ps_expected.hash, se->shared_secret, sizeof (ps_expected.hash));
740 memcpy (buffer, &ps_expected, sizeof (ps_expected));
742 gcry_md_hash_buffer (GCRY_MD_SHA256, ps_expected.hash, buffer, buffer_len);
744 *ret_buffer += sizeof (ps_received);
746 if (memcmp (ps_received.hash, ps_expected.hash,
747 sizeof (ps_received.hash)) == 0)
748 return (0);
749 else /* hashes do not match. */
750 return (1);
751 } /* }}} int parse_part_sign_sha256 */
752 /* #endif HAVE_GCRYPT_H */
754 #else /* if !HAVE_GCRYPT_H */
755 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
756 void **ret_buffer, int *ret_buffer_len)
757 {
758 INFO ("network plugin: Received signed packet, but the network "
759 "plugin was not linked with libgcrypt, so I cannot "
760 "verify the signature. The packet will be accepted.");
761 return (0);
762 } /* }}} int parse_part_sign_sha256 */
763 #endif /* !HAVE_GCRYPT_H */
765 #if HAVE_GCRYPT_H
766 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
767 void **ret_buffer, int *ret_buffer_len)
768 {
769 char *buffer = *ret_buffer;
770 int buffer_len = *ret_buffer_len;
771 int orig_buffer_len;
772 part_encryption_aes256_t pea;
773 char hash[28];
774 gcry_error_t err;
776 if (se->cypher == NULL)
777 {
778 NOTICE ("network plugin: Unable to decrypt packet, because no cypher "
779 "instance is present.");
780 return (-1);
781 }
783 /* Decrypt the packet in-place */
784 err = gcry_cipher_decrypt (se->cypher,
785 buffer + sizeof (pea.head), buffer_len - sizeof (pea.head),
786 /* in = */ NULL, /* in len = */ 0);
787 gcry_cipher_reset (se->cypher);
788 if (err != 0)
789 {
790 ERROR ("network plugin: gcry_cipher_decrypt returned: %s",
791 gcry_strerror (err));
792 return (-1);
793 }
795 /* Copy the header information to `pea' */
796 memcpy (&pea, buffer, sizeof (pea));
797 buffer += sizeof (pea);
798 buffer_len -= sizeof (pea);
800 /* Check sanity of the original length */
801 orig_buffer_len = ntohs (pea.orig_length);
802 if (orig_buffer_len > buffer_len)
803 {
804 ERROR ("network plugin: Decryption failed: Invalid original length.");
805 return (-1);
806 }
808 /* Check hash sum */
809 memset (hash, 0, sizeof (hash));
810 gcry_md_hash_buffer (GCRY_MD_SHA224, hash, buffer, orig_buffer_len);
812 if (memcmp (hash, pea.hash, sizeof (hash)) != 0)
813 {
814 ERROR ("network plugin: Decryption failed: Checksum mismatch.");
815 return (-1);
816 }
818 /* Update return values */
819 *ret_buffer = buffer;
820 *ret_buffer_len = orig_buffer_len;
822 return (0);
823 } /* }}} int parse_part_encr_aes256 */
824 /* #endif HAVE_GCRYPT_H */
826 #else /* if !HAVE_GCRYPT_H */
827 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
828 void **ret_buffer, int *ret_buffer_len)
829 {
830 INFO ("network plugin: Received encrypted packet, but the network "
831 "plugin was not linked with libgcrypt, so I cannot "
832 "decrypt it. The packet will be discarded.");
833 return (-1);
834 } /* }}} int parse_part_encr_aes256 */
835 #endif /* !HAVE_GCRYPT_H */
837 static int parse_packet (receive_list_entry_t *rle) /* {{{ */
838 {
839 int status;
841 void *buffer;
842 int buffer_len;
843 sockent_t *se;
845 value_list_t vl = VALUE_LIST_INIT;
846 notification_t n;
848 int packet_was_encrypted = 0;
849 int packet_was_signed = 0;
850 #if HAVE_GCRYPT_H
851 int printed_ignore_warning = 0;
852 #endif /* HAVE_GCRYPT_H */
854 buffer = rle->data;
855 buffer_len = rle->data_len;
857 /* Look for the correct `sockent_t' */
858 se = listen_sockets;
859 while ((se != NULL) && (se->fd != rle->fd))
860 se = se->next;
862 if (se == NULL)
863 {
864 ERROR ("network plugin: Got packet from FD %i, but can't "
865 "find an appropriate socket entry.",
866 rle->fd);
867 return (-1);
868 }
870 memset (&vl, '\0', sizeof (vl));
871 memset (&n, '\0', sizeof (n));
872 status = 0;
874 while ((status == 0) && (0 < buffer_len)
875 && ((unsigned int) buffer_len > sizeof (part_header_t)))
876 {
877 uint16_t pkg_length;
878 uint16_t pkg_type;
880 memcpy ((void *) &pkg_type,
881 (void *) buffer,
882 sizeof (pkg_type));
883 memcpy ((void *) &pkg_length,
884 (void *) (buffer + sizeof (pkg_type)),
885 sizeof (pkg_length));
887 pkg_length = ntohs (pkg_length);
888 pkg_type = ntohs (pkg_type);
890 if (pkg_length > buffer_len)
891 break;
892 /* Ensure that this loop terminates eventually */
893 if (pkg_length < (2 * sizeof (uint16_t)))
894 break;
896 if (pkg_type == TYPE_ENCR_AES256)
897 {
898 status = parse_part_encr_aes256 (se, &buffer, &buffer_len);
899 if (status != 0)
900 {
901 ERROR ("network plugin: Decrypting AES256 "
902 "part failed "
903 "with status %i.", status);
904 break;
905 }
906 else
907 {
908 packet_was_encrypted = 1;
909 }
910 }
911 #if HAVE_GCRYPT_H
912 else if ((se->security_level == SECURITY_LEVEL_ENCRYPT)
913 && (packet_was_encrypted == 0))
914 {
915 if (printed_ignore_warning == 0)
916 {
917 INFO ("network plugin: Unencrypted packet or "
918 "part has been ignored.");
919 printed_ignore_warning = 1;
920 }
921 buffer = ((char *) buffer) + pkg_length;
922 continue;
923 }
924 #endif /* HAVE_GCRYPT_H */
925 else if (pkg_type == TYPE_SIGN_SHA256)
926 {
927 status = parse_part_sign_sha256 (se, &buffer, &buffer_len);
928 if (status < 0)
929 {
930 ERROR ("network plugin: Verifying SHA-256 "
931 "signature failed "
932 "with status %i.", status);
933 break;
934 }
935 else if (status > 0)
936 {
937 ERROR ("network plugin: Ignoring packet with "
938 "invalid SHA-256 signature.");
939 break;
940 }
941 else
942 {
943 packet_was_signed = 1;
944 }
945 }
946 #if HAVE_GCRYPT_H
947 else if ((se->security_level == SECURITY_LEVEL_SIGN)
948 && (packet_was_encrypted == 0)
949 && (packet_was_signed == 0))
950 {
951 if (printed_ignore_warning == 0)
952 {
953 INFO ("network plugin: Unsigned packet or "
954 "part has been ignored.");
955 printed_ignore_warning = 1;
956 }
957 buffer = ((char *) buffer) + pkg_length;
958 continue;
959 }
960 #endif /* HAVE_GCRYPT_H */
961 else if (pkg_type == TYPE_VALUES)
962 {
963 status = parse_part_values (&buffer, &buffer_len,
964 &vl.values, &vl.values_len);
966 if (status != 0)
967 break;
969 if ((vl.time > 0)
970 && (strlen (vl.host) > 0)
971 && (strlen (vl.plugin) > 0)
972 && (strlen (vl.type) > 0)
973 && (cache_check (&vl) == 0))
974 {
975 plugin_dispatch_values (&vl);
976 }
977 else
978 {
979 DEBUG ("network plugin: parse_packet:"
980 " NOT dispatching values");
981 }
983 sfree (vl.values);
984 }
985 else if (pkg_type == TYPE_TIME)
986 {
987 uint64_t tmp = 0;
988 status = parse_part_number (&buffer, &buffer_len,
989 &tmp);
990 if (status == 0)
991 {
992 vl.time = (time_t) tmp;
993 n.time = (time_t) tmp;
994 }
995 }
996 else if (pkg_type == TYPE_INTERVAL)
997 {
998 uint64_t tmp = 0;
999 status = parse_part_number (&buffer, &buffer_len,
1000 &tmp);
1001 if (status == 0)
1002 vl.interval = (int) tmp;
1003 }
1004 else if (pkg_type == TYPE_HOST)
1005 {
1006 status = parse_part_string (&buffer, &buffer_len,
1007 vl.host, sizeof (vl.host));
1008 if (status == 0)
1009 sstrncpy (n.host, vl.host, sizeof (n.host));
1010 }
1011 else if (pkg_type == TYPE_PLUGIN)
1012 {
1013 status = parse_part_string (&buffer, &buffer_len,
1014 vl.plugin, sizeof (vl.plugin));
1015 if (status == 0)
1016 sstrncpy (n.plugin, vl.plugin,
1017 sizeof (n.plugin));
1018 }
1019 else if (pkg_type == TYPE_PLUGIN_INSTANCE)
1020 {
1021 status = parse_part_string (&buffer, &buffer_len,
1022 vl.plugin_instance,
1023 sizeof (vl.plugin_instance));
1024 if (status == 0)
1025 sstrncpy (n.plugin_instance,
1026 vl.plugin_instance,
1027 sizeof (n.plugin_instance));
1028 }
1029 else if (pkg_type == TYPE_TYPE)
1030 {
1031 status = parse_part_string (&buffer, &buffer_len,
1032 vl.type, sizeof (vl.type));
1033 if (status == 0)
1034 sstrncpy (n.type, vl.type, sizeof (n.type));
1035 }
1036 else if (pkg_type == TYPE_TYPE_INSTANCE)
1037 {
1038 status = parse_part_string (&buffer, &buffer_len,
1039 vl.type_instance,
1040 sizeof (vl.type_instance));
1041 if (status == 0)
1042 sstrncpy (n.type_instance, vl.type_instance,
1043 sizeof (n.type_instance));
1044 }
1045 else if (pkg_type == TYPE_MESSAGE)
1046 {
1047 status = parse_part_string (&buffer, &buffer_len,
1048 n.message, sizeof (n.message));
1050 if (status != 0)
1051 {
1052 /* do nothing */
1053 }
1054 else if ((n.severity != NOTIF_FAILURE)
1055 && (n.severity != NOTIF_WARNING)
1056 && (n.severity != NOTIF_OKAY))
1057 {
1058 INFO ("network plugin: "
1059 "Ignoring notification with "
1060 "unknown severity %i.",
1061 n.severity);
1062 }
1063 else if (n.time <= 0)
1064 {
1065 INFO ("network plugin: "
1066 "Ignoring notification with "
1067 "time == 0.");
1068 }
1069 else if (strlen (n.message) <= 0)
1070 {
1071 INFO ("network plugin: "
1072 "Ignoring notification with "
1073 "an empty message.");
1074 }
1075 else
1076 {
1077 plugin_dispatch_notification (&n);
1078 }
1079 }
1080 else if (pkg_type == TYPE_SEVERITY)
1081 {
1082 uint64_t tmp = 0;
1083 status = parse_part_number (&buffer, &buffer_len,
1084 &tmp);
1085 if (status == 0)
1086 n.severity = (int) tmp;
1087 }
1088 else
1089 {
1090 DEBUG ("network plugin: parse_packet: Unknown part"
1091 " type: 0x%04hx", pkg_type);
1092 buffer = ((char *) buffer) + pkg_length;
1093 }
1094 } /* while (buffer_len > sizeof (part_header_t)) */
1096 return (status);
1097 } /* }}} int parse_packet */
1099 static void free_sockent (sockent_t *se) /* {{{ */
1100 {
1101 sockent_t *next;
1102 while (se != NULL)
1103 {
1104 next = se->next;
1106 #if HAVE_GCRYPT_H
1107 if (se->cypher != NULL)
1108 {
1109 gcry_cipher_close (se->cypher);
1110 se->cypher = NULL;
1111 }
1112 free (se->shared_secret);
1113 #endif /* HAVE_GCRYPT_H */
1115 free (se->addr);
1116 free (se);
1118 se = next;
1119 }
1120 } /* }}} void free_sockent */
1122 /*
1123 * int network_set_ttl
1124 *
1125 * Set the `IP_MULTICAST_TTL', `IP_TTL', `IPV6_MULTICAST_HOPS' or
1126 * `IPV6_UNICAST_HOPS', depending on which option is applicable.
1127 *
1128 * The `struct addrinfo' is used to destinguish between unicast and multicast
1129 * sockets.
1130 */
1131 static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
1132 {
1133 if ((network_config_ttl < 1) || (network_config_ttl > 255))
1134 return (-1);
1136 DEBUG ("ttl = %i", network_config_ttl);
1138 if (ai->ai_family == AF_INET)
1139 {
1140 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1141 int optname;
1143 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1144 optname = IP_MULTICAST_TTL;
1145 else
1146 optname = IP_TTL;
1148 if (setsockopt (se->fd, IPPROTO_IP, optname,
1149 &network_config_ttl,
1150 sizeof (network_config_ttl)) == -1)
1151 {
1152 char errbuf[1024];
1153 ERROR ("setsockopt: %s",
1154 sstrerror (errno, errbuf, sizeof (errbuf)));
1155 return (-1);
1156 }
1157 }
1158 else if (ai->ai_family == AF_INET6)
1159 {
1160 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1161 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1162 int optname;
1164 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1165 optname = IPV6_MULTICAST_HOPS;
1166 else
1167 optname = IPV6_UNICAST_HOPS;
1169 if (setsockopt (se->fd, IPPROTO_IPV6, optname,
1170 &network_config_ttl,
1171 sizeof (network_config_ttl)) == -1)
1172 {
1173 char errbuf[1024];
1174 ERROR ("setsockopt: %s",
1175 sstrerror (errno, errbuf,
1176 sizeof (errbuf)));
1177 return (-1);
1178 }
1179 }
1181 return (0);
1182 } /* int network_set_ttl */
1184 #if HAVE_GCRYPT_H
1185 static int network_set_encryption (sockent_t *se, /* {{{ */
1186 const char *shared_secret)
1187 {
1188 char hash[32];
1189 gcry_error_t err;
1191 se->shared_secret = sstrdup (shared_secret);
1193 /*
1194 * We use CBC *without* an initialization vector: The cipher is reset after
1195 * each packet and we would have to re-set the IV each time. The first
1196 * encrypted block will contain the SHA-224 checksum anyway, so this should
1197 * be quite unpredictable. Also, there's a 2 byte field in the header that's
1198 * being filled with random numbers. So we only use CBC so the blocks
1199 * *within* one packet are chained.
1200 */
1201 err = gcry_cipher_open (&se->cypher,
1202 GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CBC, /* flags = */ 0);
1203 if (err != 0)
1204 {
1205 ERROR ("network plugin: gcry_cipher_open returned: %s",
1206 gcry_strerror (err));
1207 return (-1);
1208 }
1210 assert (se->shared_secret != NULL);
1211 gcry_md_hash_buffer (GCRY_MD_SHA256, hash,
1212 se->shared_secret, strlen (se->shared_secret));
1214 err = gcry_cipher_setkey (se->cypher, hash, sizeof (hash));
1215 if (err != 0)
1216 {
1217 DEBUG ("network plugin: gcry_cipher_setkey returned: %s",
1218 gcry_strerror (err));
1219 gcry_cipher_close (se->cypher);
1220 se->cypher = NULL;
1221 return (-1);
1222 }
1224 return (0);
1225 } /* }}} int network_set_encryption */
1226 #endif /* HAVE_GCRYPT_H */
1228 static int network_bind_socket (const sockent_t *se, const struct addrinfo *ai)
1229 {
1230 int loop = 0;
1231 int yes = 1;
1233 /* allow multiple sockets to use the same PORT number */
1234 if (setsockopt(se->fd, SOL_SOCKET, SO_REUSEADDR,
1235 &yes, sizeof(yes)) == -1) {
1236 char errbuf[1024];
1237 ERROR ("setsockopt: %s",
1238 sstrerror (errno, errbuf, sizeof (errbuf)));
1239 return (-1);
1240 }
1242 DEBUG ("fd = %i; calling `bind'", se->fd);
1244 if (bind (se->fd, ai->ai_addr, ai->ai_addrlen) == -1)
1245 {
1246 char errbuf[1024];
1247 ERROR ("bind: %s",
1248 sstrerror (errno, errbuf, sizeof (errbuf)));
1249 return (-1);
1250 }
1252 if (ai->ai_family == AF_INET)
1253 {
1254 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1255 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1256 {
1257 struct ip_mreq mreq;
1259 DEBUG ("fd = %i; IPv4 multicast address found", se->fd);
1261 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1262 mreq.imr_interface.s_addr = htonl (INADDR_ANY);
1264 if (setsockopt (se->fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1265 &loop, sizeof (loop)) == -1)
1266 {
1267 char errbuf[1024];
1268 ERROR ("setsockopt: %s",
1269 sstrerror (errno, errbuf,
1270 sizeof (errbuf)));
1271 return (-1);
1272 }
1274 if (setsockopt (se->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1275 &mreq, sizeof (mreq)) == -1)
1276 {
1277 char errbuf[1024];
1278 ERROR ("setsockopt: %s",
1279 sstrerror (errno, errbuf,
1280 sizeof (errbuf)));
1281 return (-1);
1282 }
1283 }
1284 }
1285 else if (ai->ai_family == AF_INET6)
1286 {
1287 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1288 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1289 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1290 {
1291 struct ipv6_mreq mreq;
1293 DEBUG ("fd = %i; IPv6 multicast address found", se->fd);
1295 memcpy (&mreq.ipv6mr_multiaddr,
1296 &addr->sin6_addr,
1297 sizeof (addr->sin6_addr));
1299 /* http://developer.apple.com/documentation/Darwin/Reference/ManPages/man4/ip6.4.html
1300 * ipv6mr_interface may be set to zeroes to
1301 * choose the default multicast interface or to
1302 * the index of a particular multicast-capable
1303 * interface if the host is multihomed.
1304 * Membership is associ-associated with a
1305 * single interface; programs running on
1306 * multihomed hosts may need to join the same
1307 * group on more than one interface.*/
1308 mreq.ipv6mr_interface = 0;
1310 if (setsockopt (se->fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
1311 &loop, sizeof (loop)) == -1)
1312 {
1313 char errbuf[1024];
1314 ERROR ("setsockopt: %s",
1315 sstrerror (errno, errbuf,
1316 sizeof (errbuf)));
1317 return (-1);
1318 }
1320 if (setsockopt (se->fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
1321 &mreq, sizeof (mreq)) == -1)
1322 {
1323 char errbuf[1024];
1324 ERROR ("setsockopt: %s",
1325 sstrerror (errno, errbuf,
1326 sizeof (errbuf)));
1327 return (-1);
1328 }
1329 }
1330 }
1332 return (0);
1333 } /* int network_bind_socket */
1335 #define CREATE_SOCKET_FLAGS_LISTEN 0x0001
1336 static sockent_t *network_create_socket (const char *node, /* {{{ */
1337 const char *service,
1338 const char *shared_secret,
1339 int security_level,
1340 int flags)
1341 {
1342 struct addrinfo ai_hints;
1343 struct addrinfo *ai_list, *ai_ptr;
1344 int ai_return;
1346 sockent_t *se_head = NULL;
1347 sockent_t *se_tail = NULL;
1349 DEBUG ("node = %s, service = %s", node, service);
1351 memset (&ai_hints, '\0', sizeof (ai_hints));
1352 ai_hints.ai_flags = 0;
1353 #ifdef AI_PASSIVE
1354 ai_hints.ai_flags |= AI_PASSIVE;
1355 #endif
1356 #ifdef AI_ADDRCONFIG
1357 ai_hints.ai_flags |= AI_ADDRCONFIG;
1358 #endif
1359 ai_hints.ai_family = AF_UNSPEC;
1360 ai_hints.ai_socktype = SOCK_DGRAM;
1361 ai_hints.ai_protocol = IPPROTO_UDP;
1363 ai_return = getaddrinfo (node, service, &ai_hints, &ai_list);
1364 if (ai_return != 0)
1365 {
1366 char errbuf[1024];
1367 ERROR ("getaddrinfo (%s, %s): %s",
1368 (node == NULL) ? "(null)" : node,
1369 (service == NULL) ? "(null)" : service,
1370 (ai_return == EAI_SYSTEM)
1371 ? sstrerror (errno, errbuf, sizeof (errbuf))
1372 : gai_strerror (ai_return));
1373 return (NULL);
1374 }
1376 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1377 {
1378 sockent_t *se;
1379 int status;
1381 if ((se = (sockent_t *) malloc (sizeof (sockent_t))) == NULL)
1382 {
1383 char errbuf[1024];
1384 ERROR ("malloc: %s",
1385 sstrerror (errno, errbuf,
1386 sizeof (errbuf)));
1387 continue;
1388 }
1390 if ((se->addr = (struct sockaddr_storage *) malloc (sizeof (struct sockaddr_storage))) == NULL)
1391 {
1392 char errbuf[1024];
1393 ERROR ("malloc: %s",
1394 sstrerror (errno, errbuf,
1395 sizeof (errbuf)));
1396 free (se);
1397 continue;
1398 }
1400 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
1401 memset (se->addr, '\0', sizeof (struct sockaddr_storage));
1402 memcpy (se->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1403 se->addrlen = ai_ptr->ai_addrlen;
1405 se->fd = socket (ai_ptr->ai_family,
1406 ai_ptr->ai_socktype,
1407 ai_ptr->ai_protocol);
1408 se->next = NULL;
1410 if (se->fd == -1)
1411 {
1412 char errbuf[1024];
1413 ERROR ("socket: %s",
1414 sstrerror (errno, errbuf,
1415 sizeof (errbuf)));
1416 free (se->addr);
1417 free (se);
1418 continue;
1419 }
1421 if ((flags & CREATE_SOCKET_FLAGS_LISTEN) != 0)
1422 {
1423 status = network_bind_socket (se, ai_ptr);
1424 if (status != 0)
1425 {
1426 close (se->fd);
1427 free (se->addr);
1428 free (se);
1429 continue;
1430 }
1431 }
1432 else /* sending socket */
1433 {
1434 network_set_ttl (se, ai_ptr);
1435 }
1437 #if HAVE_GCRYPT_H
1438 se->security_level = security_level;
1439 se->shared_secret = NULL;
1440 se->cypher = NULL;
1441 if (shared_secret != NULL)
1442 {
1443 status = network_set_encryption (se, shared_secret);
1444 if ((status != 0) && (security_level <= SECURITY_LEVEL_SIGN))
1445 {
1446 WARNING ("network plugin: Starting cryptograp"
1447 "hic subsystem failed. Since "
1448 "security level `Sign' or "
1449 "`None' is configured I will "
1450 "continue.");
1451 }
1452 else if (status != 0)
1453 {
1454 ERROR ("network plugin: Starting cryptograp"
1455 "hic subsystem failed. "
1456 "Because the security level "
1457 "is set to `Encrypt' I will "
1458 "not continue!");
1459 close (se->fd);
1460 free (se->addr);
1461 free (se);
1462 continue;
1463 }
1464 } /* if (shared_secret != NULL) */
1465 #else
1466 /* Make compiler happy */
1467 security_level = 0;
1468 shared_secret = NULL;
1469 #endif /* HAVE_GCRYPT_H */
1471 if (se_tail == NULL)
1472 {
1473 se_head = se;
1474 se_tail = se;
1475 }
1476 else
1477 {
1478 se_tail->next = se;
1479 se_tail = se;
1480 }
1482 /* We don't open more than one write-socket per node/service pair.. */
1483 if ((flags & CREATE_SOCKET_FLAGS_LISTEN) == 0)
1484 break;
1485 }
1487 freeaddrinfo (ai_list);
1489 return (se_head);
1490 } /* }}} sockent_t *network_create_socket */
1492 static sockent_t *network_create_default_socket (int flags) /* {{{ */
1493 {
1494 sockent_t *se_ptr = NULL;
1495 sockent_t *se_head = NULL;
1496 sockent_t *se_tail = NULL;
1498 se_ptr = network_create_socket (NET_DEFAULT_V6_ADDR, NET_DEFAULT_PORT,
1499 /* shared secret = */ NULL, SECURITY_LEVEL_NONE,
1500 flags);
1502 /* Don't send to the same machine in IPv6 and IPv4 if both are available. */
1503 if (((flags & CREATE_SOCKET_FLAGS_LISTEN) == 0) && (se_ptr != NULL))
1504 return (se_ptr);
1506 if (se_ptr != NULL)
1507 {
1508 se_head = se_ptr;
1509 se_tail = se_ptr;
1510 while (se_tail->next != NULL)
1511 se_tail = se_tail->next;
1512 }
1514 se_ptr = network_create_socket (NET_DEFAULT_V4_ADDR, NET_DEFAULT_PORT,
1515 /* shared secret = */ NULL, SECURITY_LEVEL_NONE,
1516 flags);
1518 if (se_tail == NULL)
1519 return (se_ptr);
1521 se_tail->next = se_ptr;
1522 return (se_head);
1523 } /* }}} sockent_t *network_create_default_socket */
1525 static int network_add_listen_socket (const char *node, /* {{{ */
1526 const char *service, const char *shared_secret, int security_level)
1527 {
1528 sockent_t *se;
1529 sockent_t *se_ptr;
1530 int se_num = 0;
1532 int flags;
1534 flags = CREATE_SOCKET_FLAGS_LISTEN;
1536 if (service == NULL)
1537 service = NET_DEFAULT_PORT;
1539 if (node == NULL)
1540 se = network_create_default_socket (flags);
1541 else
1542 se = network_create_socket (node, service,
1543 shared_secret, security_level, flags);
1545 if (se == NULL)
1546 return (-1);
1548 for (se_ptr = se; se_ptr != NULL; se_ptr = se_ptr->next)
1549 se_num++;
1551 listen_sockets_pollfd = realloc (listen_sockets_pollfd,
1552 (listen_sockets_num + se_num)
1553 * sizeof (struct pollfd));
1555 for (se_ptr = se; se_ptr != NULL; se_ptr = se_ptr->next)
1556 {
1557 listen_sockets_pollfd[listen_sockets_num].fd = se_ptr->fd;
1558 listen_sockets_pollfd[listen_sockets_num].events = POLLIN | POLLPRI;
1559 listen_sockets_pollfd[listen_sockets_num].revents = 0;
1560 listen_sockets_num++;
1561 } /* for (se) */
1563 se_ptr = listen_sockets;
1564 while ((se_ptr != NULL) && (se_ptr->next != NULL))
1565 se_ptr = se_ptr->next;
1567 if (se_ptr == NULL)
1568 listen_sockets = se;
1569 else
1570 se_ptr->next = se;
1572 return (0);
1573 } /* }}} int network_add_listen_socket */
1575 static int network_add_sending_socket (const char *node, /* {{{ */
1576 const char *service, const char *shared_secret, int security_level)
1577 {
1578 sockent_t *se;
1579 sockent_t *se_ptr;
1581 if (service == NULL)
1582 service = NET_DEFAULT_PORT;
1584 if (node == NULL)
1585 se = network_create_default_socket (/* flags = */ 0);
1586 else
1587 se = network_create_socket (node, service,
1588 shared_secret, security_level,
1589 /* flags = */ 0);
1591 if (se == NULL)
1592 return (-1);
1594 if (sending_sockets == NULL)
1595 {
1596 sending_sockets = se;
1597 return (0);
1598 }
1600 for (se_ptr = sending_sockets; se_ptr->next != NULL; se_ptr = se_ptr->next)
1601 /* seek end */;
1603 se_ptr->next = se;
1604 return (0);
1605 } /* }}} int network_add_sending_socket */
1607 static void *dispatch_thread (void __attribute__((unused)) *arg)
1608 {
1609 while (42)
1610 {
1611 receive_list_entry_t *ent;
1613 /* Lock and wait for more data to come in */
1614 pthread_mutex_lock (&receive_list_lock);
1615 while ((listen_loop == 0)
1616 && (receive_list_head == NULL))
1617 pthread_cond_wait (&receive_list_cond, &receive_list_lock);
1619 /* Remove the head entry and unlock */
1620 ent = receive_list_head;
1621 if (ent != NULL)
1622 receive_list_head = ent->next;
1623 pthread_mutex_unlock (&receive_list_lock);
1625 /* Check whether we are supposed to exit. We do NOT check `listen_loop'
1626 * because we dispatch all missing packets before shutting down. */
1627 if (ent == NULL)
1628 break;
1630 parse_packet (ent);
1632 sfree (ent);
1633 } /* while (42) */
1635 return (NULL);
1636 } /* void *dispatch_thread */
1638 static int network_receive (void)
1639 {
1640 char buffer[BUFF_SIZE];
1641 int buffer_len;
1643 int i;
1644 int status;
1646 receive_list_entry_t *private_list_head;
1647 receive_list_entry_t *private_list_tail;
1649 if (listen_sockets_num == 0)
1650 network_add_listen_socket (/* node = */ NULL,
1651 /* service = */ NULL,
1652 /* shared secret = */ NULL,
1653 /* encryption = */ 0);
1655 if (listen_sockets_num == 0)
1656 {
1657 ERROR ("network: Failed to open a listening socket.");
1658 return (-1);
1659 }
1661 private_list_head = NULL;
1662 private_list_tail = NULL;
1664 while (listen_loop == 0)
1665 {
1666 status = poll (listen_sockets_pollfd, listen_sockets_num, -1);
1668 if (status <= 0)
1669 {
1670 char errbuf[1024];
1671 if (errno == EINTR)
1672 continue;
1673 ERROR ("poll failed: %s",
1674 sstrerror (errno, errbuf, sizeof (errbuf)));
1675 return (-1);
1676 }
1678 for (i = 0; (i < listen_sockets_num) && (status > 0); i++)
1679 {
1680 receive_list_entry_t *ent;
1682 if ((listen_sockets_pollfd[i].revents
1683 & (POLLIN | POLLPRI)) == 0)
1684 continue;
1685 status--;
1687 buffer_len = recv (listen_sockets_pollfd[i].fd,
1688 buffer, sizeof (buffer),
1689 0 /* no flags */);
1690 if (buffer_len < 0)
1691 {
1692 char errbuf[1024];
1693 ERROR ("recv failed: %s",
1694 sstrerror (errno, errbuf,
1695 sizeof (errbuf)));
1696 return (-1);
1697 }
1699 /* TODO: Possible performance enhancement: Do not free
1700 * these entries in the dispatch thread but put them in
1701 * another list, so we don't have to allocate more and
1702 * more of these structures. */
1703 ent = malloc (sizeof (receive_list_entry_t));
1704 if (ent == NULL)
1705 {
1706 ERROR ("network plugin: malloc failed.");
1707 return (-1);
1708 }
1709 memset (ent, 0, sizeof (receive_list_entry_t));
1710 ent->fd = listen_sockets_pollfd[i].fd;
1711 ent->next = NULL;
1713 /* Hopefully this be optimized out by the compiler. It
1714 * might help prevent stupid bugs in the future though.
1715 */
1716 assert (sizeof (ent->data) == sizeof (buffer));
1718 memcpy (ent->data, buffer, buffer_len);
1719 ent->data_len = buffer_len;
1721 if (private_list_head == NULL)
1722 private_list_head = ent;
1723 else
1724 private_list_tail->next = ent;
1725 private_list_tail = ent;
1727 /* Do not block here. Blocking here has led to
1728 * insufficient performance in the past. */
1729 if (pthread_mutex_trylock (&receive_list_lock) == 0)
1730 {
1731 if (receive_list_head == NULL)
1732 receive_list_head = private_list_head;
1733 else
1734 receive_list_tail->next = private_list_head;
1735 receive_list_tail = private_list_tail;
1737 private_list_head = NULL;
1738 private_list_tail = NULL;
1740 pthread_cond_signal (&receive_list_cond);
1741 pthread_mutex_unlock (&receive_list_lock);
1742 }
1743 } /* for (listen_sockets_pollfd) */
1744 } /* while (listen_loop == 0) */
1746 /* Make sure everything is dispatched before exiting. */
1747 if (private_list_head != NULL)
1748 {
1749 pthread_mutex_lock (&receive_list_lock);
1751 if (receive_list_head == NULL)
1752 receive_list_head = private_list_head;
1753 else
1754 receive_list_tail->next = private_list_head;
1755 receive_list_tail = private_list_tail;
1757 private_list_head = NULL;
1758 private_list_tail = NULL;
1760 pthread_cond_signal (&receive_list_cond);
1761 pthread_mutex_unlock (&receive_list_lock);
1762 }
1764 return (0);
1765 } /* int network_receive */
1767 static void *receive_thread (void __attribute__((unused)) *arg)
1768 {
1769 return (network_receive () ? (void *) 1 : (void *) 0);
1770 } /* void *receive_thread */
1772 static void network_init_buffer (void)
1773 {
1774 memset (send_buffer, 0, sizeof (send_buffer));
1775 send_buffer_ptr = send_buffer;
1776 send_buffer_fill = 0;
1778 memset (&send_buffer_vl, 0, sizeof (send_buffer_vl));
1779 } /* int network_init_buffer */
1781 static void networt_send_buffer_plain (const sockent_t *se, /* {{{ */
1782 const char *buffer, size_t buffer_size)
1783 {
1784 int status;
1786 while (42)
1787 {
1788 status = sendto (se->fd, buffer, buffer_size, 0 /* no flags */,
1789 (struct sockaddr *) se->addr, se->addrlen);
1790 if (status < 0)
1791 {
1792 char errbuf[1024];
1793 if (errno == EINTR)
1794 continue;
1795 ERROR ("network plugin: sendto failed: %s",
1796 sstrerror (errno, errbuf,
1797 sizeof (errbuf)));
1798 break;
1799 }
1801 break;
1802 } /* while (42) */
1803 } /* }}} void networt_send_buffer_plain */
1805 #if HAVE_GCRYPT_H
1806 static void networt_send_buffer_signed (const sockent_t *se, /* {{{ */
1807 const char *in_buffer, size_t in_buffer_size)
1808 {
1809 part_signature_sha256_t ps;
1810 char buffer[sizeof (ps) + in_buffer_size];
1811 char hash[sizeof (ps.hash)];
1813 /* Initialize the `ps' structure. */
1814 memset (&ps, 0, sizeof (ps));
1815 ps.head.type = htons (TYPE_SIGN_SHA256);
1816 ps.head.length = htons ((uint16_t) sizeof (ps));
1817 sstrncpy (ps.hash, se->shared_secret, sizeof (ps.hash));
1819 /* Prepend the signature. */
1820 memcpy (buffer, &ps, sizeof (ps));
1821 memcpy (buffer + sizeof (ps), in_buffer, in_buffer_size);
1823 /* Calculate the hash value. */
1824 gcry_md_hash_buffer (GCRY_MD_SHA256, hash, buffer, sizeof (buffer));
1826 /* Copy the hash value into the buffer. */
1827 memcpy (ps.hash, hash, sizeof (ps.hash));
1828 memcpy (buffer, &ps, sizeof (ps));
1830 networt_send_buffer_plain (se, buffer, sizeof (buffer));
1831 } /* }}} void networt_send_buffer_signed */
1833 static void networt_send_buffer_encrypted (const sockent_t *se, /* {{{ */
1834 const char *in_buffer, size_t in_buffer_size)
1835 {
1836 part_encryption_aes256_t pea;
1837 char buffer[sizeof (pea) + in_buffer_size + 16];
1838 size_t buffer_size;
1839 gcry_error_t err;
1841 /* Round to the next multiple of 16, because AES has a block size of 128 bit.
1842 * the first four bytes of `pea' are not encrypted and must be subtracted. */
1843 buffer_size = (sizeof (pea) + in_buffer_size + 15 - sizeof (pea.head)) / 16;
1844 buffer_size = buffer_size * 16;
1845 buffer_size += sizeof (pea.head);
1847 DEBUG ("network plugin: networt_send_buffer_encrypted: "
1848 "buffer_size = %zu;", buffer_size);
1850 /* Initialize the header fields */
1851 memset (&pea, 0, sizeof (pea));
1852 pea.head.type = htons (TYPE_ENCR_AES256);
1853 pea.head.length = htons ((uint16_t) buffer_size);
1854 pea.orig_length = htons ((uint16_t) in_buffer_size);
1856 /* Fill the extra field with random values. Some entropy in the encrypted
1857 * data is usually not a bad thing, I hope. */
1858 gcry_randomize (&pea.random, sizeof (pea.random), GCRY_STRONG_RANDOM);
1860 /* Create hash of the payload */
1861 gcry_md_hash_buffer (GCRY_MD_SHA224, pea.hash, in_buffer, in_buffer_size);
1863 /* Initialize the buffer */
1864 memset (buffer, 0, sizeof (buffer));
1865 memcpy (buffer, &pea, sizeof (pea));
1866 memcpy (buffer + sizeof (pea), in_buffer, in_buffer_size);
1868 /* Encrypt the buffer in-place */
1869 err = gcry_cipher_encrypt (se->cypher,
1870 buffer + sizeof (pea.head), buffer_size - sizeof (pea.head),
1871 /* in = */ NULL, /* in len = */ 0);
1872 gcry_cipher_reset (se->cypher);
1873 if (err != 0)
1874 {
1875 ERROR ("network plugin: gcry_cipher_encrypt returned: %s",
1876 gcry_strerror (err));
1877 return;
1878 }
1880 /* Send it out without further modifications */
1881 networt_send_buffer_plain (se, buffer, buffer_size);
1882 } /* }}} void networt_send_buffer_encrypted */
1883 #endif /* HAVE_GCRYPT_H */
1885 static void network_send_buffer (char *buffer, size_t buffer_len) /* {{{ */
1886 {
1887 sockent_t *se;
1889 DEBUG ("network plugin: network_send_buffer: buffer_len = %zu", buffer_len);
1891 for (se = sending_sockets; se != NULL; se = se->next)
1892 {
1893 #if HAVE_GCRYPT_H
1894 if (se->security_level == SECURITY_LEVEL_ENCRYPT)
1895 networt_send_buffer_encrypted (se, buffer, buffer_len);
1896 else if (se->security_level == SECURITY_LEVEL_SIGN)
1897 networt_send_buffer_signed (se, buffer, buffer_len);
1898 else /* if (se->security_level == SECURITY_LEVEL_NONE) */
1899 #endif /* HAVE_GCRYPT_H */
1900 networt_send_buffer_plain (se, buffer, buffer_len);
1901 } /* for (sending_sockets) */
1902 } /* }}} void network_send_buffer */
1904 static int add_to_buffer (char *buffer, int buffer_size, /* {{{ */
1905 value_list_t *vl_def,
1906 const data_set_t *ds, const value_list_t *vl)
1907 {
1908 char *buffer_orig = buffer;
1910 if (strcmp (vl_def->host, vl->host) != 0)
1911 {
1912 if (write_part_string (&buffer, &buffer_size, TYPE_HOST,
1913 vl->host, strlen (vl->host)) != 0)
1914 return (-1);
1915 sstrncpy (vl_def->host, vl->host, sizeof (vl_def->host));
1916 }
1918 if (vl_def->time != vl->time)
1919 {
1920 if (write_part_number (&buffer, &buffer_size, TYPE_TIME,
1921 (uint64_t) vl->time))
1922 return (-1);
1923 vl_def->time = vl->time;
1924 }
1926 if (vl_def->interval != vl->interval)
1927 {
1928 if (write_part_number (&buffer, &buffer_size, TYPE_INTERVAL,
1929 (uint64_t) vl->interval))
1930 return (-1);
1931 vl_def->interval = vl->interval;
1932 }
1934 if (strcmp (vl_def->plugin, vl->plugin) != 0)
1935 {
1936 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN,
1937 vl->plugin, strlen (vl->plugin)) != 0)
1938 return (-1);
1939 sstrncpy (vl_def->plugin, vl->plugin, sizeof (vl_def->plugin));
1940 }
1942 if (strcmp (vl_def->plugin_instance, vl->plugin_instance) != 0)
1943 {
1944 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
1945 vl->plugin_instance,
1946 strlen (vl->plugin_instance)) != 0)
1947 return (-1);
1948 sstrncpy (vl_def->plugin_instance, vl->plugin_instance, sizeof (vl_def->plugin_instance));
1949 }
1951 if (strcmp (vl_def->type, vl->type) != 0)
1952 {
1953 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
1954 vl->type, strlen (vl->type)) != 0)
1955 return (-1);
1956 sstrncpy (vl_def->type, ds->type, sizeof (vl_def->type));
1957 }
1959 if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
1960 {
1961 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE_INSTANCE,
1962 vl->type_instance,
1963 strlen (vl->type_instance)) != 0)
1964 return (-1);
1965 sstrncpy (vl_def->type_instance, vl->type_instance, sizeof (vl_def->type_instance));
1966 }
1968 if (write_part_values (&buffer, &buffer_size, ds, vl) != 0)
1969 return (-1);
1971 return (buffer - buffer_orig);
1972 } /* }}} int add_to_buffer */
1974 static void flush_buffer (void)
1975 {
1976 DEBUG ("network plugin: flush_buffer: send_buffer_fill = %i",
1977 send_buffer_fill);
1979 network_send_buffer (send_buffer, (size_t) send_buffer_fill);
1980 network_init_buffer ();
1981 }
1983 static int network_write (const data_set_t *ds, const value_list_t *vl,
1984 user_data_t __attribute__((unused)) *user_data)
1985 {
1986 int status;
1988 /* If the value is already in the cache, we have received it via the
1989 * network. We write it again if forwarding is activated. It's then in
1990 * the cache and should we receive it again we will ignore it. */
1991 status = cache_check (vl);
1992 if ((network_config_forward == 0)
1993 && (status != 0))
1994 return (0);
1996 pthread_mutex_lock (&send_buffer_lock);
1998 status = add_to_buffer (send_buffer_ptr,
1999 sizeof (send_buffer) - (send_buffer_fill + BUFF_SIG_SIZE),
2000 &send_buffer_vl,
2001 ds, vl);
2002 if (status >= 0)
2003 {
2004 /* status == bytes added to the buffer */
2005 send_buffer_fill += status;
2006 send_buffer_ptr += status;
2007 }
2008 else
2009 {
2010 flush_buffer ();
2012 status = add_to_buffer (send_buffer_ptr,
2013 sizeof (send_buffer) - (send_buffer_fill + BUFF_SIG_SIZE),
2014 &send_buffer_vl,
2015 ds, vl);
2017 if (status >= 0)
2018 {
2019 send_buffer_fill += status;
2020 send_buffer_ptr += status;
2021 }
2022 }
2024 if (status < 0)
2025 {
2026 ERROR ("network plugin: Unable to append to the "
2027 "buffer for some weird reason");
2028 }
2029 else if ((sizeof (send_buffer) - send_buffer_fill) < 15)
2030 {
2031 flush_buffer ();
2032 }
2034 pthread_mutex_unlock (&send_buffer_lock);
2036 return ((status < 0) ? -1 : 0);
2037 } /* int network_write */
2039 static int network_config_set_boolean (const oconfig_item_t *ci, /* {{{ */
2040 int *retval)
2041 {
2042 if ((ci->values_num != 1)
2043 || ((ci->values[0].type != OCONFIG_TYPE_BOOLEAN)
2044 && (ci->values[0].type != OCONFIG_TYPE_STRING)))
2045 {
2046 ERROR ("network plugin: The `%s' config option needs "
2047 "exactly one boolean argument.", ci->key);
2048 return (-1);
2049 }
2051 if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
2052 {
2053 if (ci->values[0].value.boolean)
2054 *retval = 1;
2055 else
2056 *retval = 0;
2057 }
2058 else
2059 {
2060 char *str = ci->values[0].value.string;
2062 if ((strcasecmp ("true", str) == 0)
2063 || (strcasecmp ("yes", str) == 0)
2064 || (strcasecmp ("on", str) == 0))
2065 *retval = 1;
2066 else if ((strcasecmp ("false", str) == 0)
2067 || (strcasecmp ("no", str) == 0)
2068 || (strcasecmp ("off", str) == 0))
2069 *retval = 0;
2070 else
2071 {
2072 ERROR ("network plugin: Cannot parse string value `%s' of the `%s' "
2073 "option as boolean value.",
2074 str, ci->key);
2075 return (-1);
2076 }
2077 }
2079 return (0);
2080 } /* }}} int network_config_set_boolean */
2082 static int network_config_set_ttl (const oconfig_item_t *ci) /* {{{ */
2083 {
2084 int tmp;
2085 if ((ci->values_num != 1)
2086 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
2087 {
2088 WARNING ("network plugin: The `TimeToLive' config option needs exactly "
2089 "one numeric argument.");
2090 return (-1);
2091 }
2093 tmp = (int) ci->values[0].value.number;
2094 if ((tmp > 0) && (tmp <= 255))
2095 network_config_ttl = tmp;
2097 return (0);
2098 } /* }}} int network_config_set_ttl */
2100 #if HAVE_GCRYPT_H
2101 static int network_config_set_security_level (oconfig_item_t *ci, /* {{{ */
2102 int *retval)
2103 {
2104 char *str;
2105 if ((ci->values_num != 1)
2106 || (ci->values[0].type != OCONFIG_TYPE_STRING))
2107 {
2108 WARNING ("network plugin: The `SecurityLevel' config option needs exactly "
2109 "one string argument.");
2110 return (-1);
2111 }
2113 str = ci->values[0].value.string;
2114 if (strcasecmp ("Encrypt", str) == 0)
2115 *retval = SECURITY_LEVEL_ENCRYPT;
2116 else if (strcasecmp ("Sign", str) == 0)
2117 *retval = SECURITY_LEVEL_SIGN;
2118 else if (strcasecmp ("None", str) == 0)
2119 *retval = SECURITY_LEVEL_NONE;
2120 else
2121 {
2122 WARNING ("network plugin: Unknown security level: %s.", str);
2123 return (-1);
2124 }
2126 return (0);
2127 } /* }}} int network_config_set_security_level */
2128 #endif /* HAVE_GCRYPT_H */
2130 static int network_config_listen_server (const oconfig_item_t *ci) /* {{{ */
2131 {
2132 char *node;
2133 char *service;
2134 char *shared_secret = NULL;
2135 int security_level = SECURITY_LEVEL_NONE;
2136 int i;
2138 if ((ci->values_num < 1) || (ci->values_num > 2)
2139 || (ci->values[0].type != OCONFIG_TYPE_STRING)
2140 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
2141 {
2142 ERROR ("network plugin: The `%s' config option needs "
2143 "one or two string arguments.", ci->key);
2144 return (-1);
2145 }
2147 node = ci->values[0].value.string;
2148 if (ci->values_num >= 2)
2149 service = ci->values[1].value.string;
2150 else
2151 service = NULL;
2153 for (i = 0; i < ci->children_num; i++)
2154 {
2155 oconfig_item_t *child = ci->children + i;
2157 #if HAVE_GCRYPT_H
2158 if (strcasecmp ("Secret", child->key) == 0)
2159 {
2160 if ((child->values_num == 1)
2161 && (child->values[0].type == OCONFIG_TYPE_STRING))
2162 shared_secret = child->values[0].value.string;
2163 else
2164 ERROR ("network plugin: The `Secret' option needs exactly one string "
2165 "argument.");
2166 }
2167 else if (strcasecmp ("SecurityLevel", child->key) == 0)
2168 network_config_set_security_level (child, &security_level);
2169 else
2170 #endif /* HAVE_GCRYPT_H */
2171 {
2172 WARNING ("network plugin: Option `%s' is not allowed here.",
2173 child->key);
2174 }
2175 }
2177 if ((security_level > SECURITY_LEVEL_NONE) && (shared_secret == NULL))
2178 {
2179 ERROR ("network plugin: A security level higher than `none' was "
2180 "requested, but no shared key was given. Cowardly refusing to open "
2181 "this socket!");
2182 return (-1);
2183 }
2185 if (strcasecmp ("Listen", ci->key) == 0)
2186 network_add_listen_socket (node, service, shared_secret, security_level);
2187 else
2188 network_add_sending_socket (node, service, shared_secret, security_level);
2190 return (0);
2191 } /* }}} int network_config_listen_server */
2193 static int network_config_set_cache_flush (const oconfig_item_t *ci) /* {{{ */
2194 {
2195 int tmp;
2196 if ((ci->values_num != 1)
2197 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
2198 {
2199 WARNING ("network plugin: The `CacheFlush' config option needs exactly "
2200 "one numeric argument.");
2201 return (-1);
2202 }
2204 tmp = (int) ci->values[0].value.number;
2205 if (tmp > 0)
2206 network_config_ttl = tmp;
2208 return (0);
2209 } /* }}} int network_config_set_cache_flush */
2211 static int network_config (oconfig_item_t *ci) /* {{{ */
2212 {
2213 int i;
2215 for (i = 0; i < ci->children_num; i++)
2216 {
2217 oconfig_item_t *child = ci->children + i;
2219 if ((strcasecmp ("Listen", child->key) == 0)
2220 || (strcasecmp ("Server", child->key) == 0))
2221 network_config_listen_server (child);
2222 else if (strcasecmp ("TimeToLive", child->key) == 0)
2223 network_config_set_ttl (child);
2224 else if (strcasecmp ("Forward", child->key) == 0)
2225 network_config_set_boolean (child, &network_config_forward);
2226 else if (strcasecmp ("CacheFlush", child->key) == 0)
2227 network_config_set_cache_flush (child);
2228 else
2229 {
2230 WARNING ("network plugin: Option `%s' is not allowed here.",
2231 child->key);
2232 }
2233 }
2235 return (0);
2236 } /* }}} int network_config */
2238 static int network_notification (const notification_t *n,
2239 user_data_t __attribute__((unused)) *user_data)
2240 {
2241 char buffer[BUFF_SIZE];
2242 char *buffer_ptr = buffer;
2243 int buffer_free = sizeof (buffer);
2244 int status;
2246 memset (buffer, '\0', sizeof (buffer));
2249 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_TIME,
2250 (uint64_t) n->time);
2251 if (status != 0)
2252 return (-1);
2254 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_SEVERITY,
2255 (uint64_t) n->severity);
2256 if (status != 0)
2257 return (-1);
2259 if (strlen (n->host) > 0)
2260 {
2261 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_HOST,
2262 n->host, strlen (n->host));
2263 if (status != 0)
2264 return (-1);
2265 }
2267 if (strlen (n->plugin) > 0)
2268 {
2269 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_PLUGIN,
2270 n->plugin, strlen (n->plugin));
2271 if (status != 0)
2272 return (-1);
2273 }
2275 if (strlen (n->plugin_instance) > 0)
2276 {
2277 status = write_part_string (&buffer_ptr, &buffer_free,
2278 TYPE_PLUGIN_INSTANCE,
2279 n->plugin_instance, strlen (n->plugin_instance));
2280 if (status != 0)
2281 return (-1);
2282 }
2284 if (strlen (n->type) > 0)
2285 {
2286 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE,
2287 n->type, strlen (n->type));
2288 if (status != 0)
2289 return (-1);
2290 }
2292 if (strlen (n->type_instance) > 0)
2293 {
2294 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE_INSTANCE,
2295 n->type_instance, strlen (n->type_instance));
2296 if (status != 0)
2297 return (-1);
2298 }
2300 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_MESSAGE,
2301 n->message, strlen (n->message));
2302 if (status != 0)
2303 return (-1);
2305 network_send_buffer (buffer, sizeof (buffer) - buffer_free);
2307 return (0);
2308 } /* int network_notification */
2310 static int network_shutdown (void)
2311 {
2312 listen_loop++;
2314 /* Kill the listening thread */
2315 if (receive_thread_running != 0)
2316 {
2317 INFO ("network plugin: Stopping receive thread.");
2318 pthread_kill (receive_thread_id, SIGTERM);
2319 pthread_join (receive_thread_id, NULL /* no return value */);
2320 memset (&receive_thread_id, 0, sizeof (receive_thread_id));
2321 receive_thread_running = 0;
2322 }
2324 /* Shutdown the dispatching thread */
2325 if (dispatch_thread_running != 0)
2326 {
2327 INFO ("network plugin: Stopping dispatch thread.");
2328 pthread_mutex_lock (&receive_list_lock);
2329 pthread_cond_broadcast (&receive_list_cond);
2330 pthread_mutex_unlock (&receive_list_lock);
2331 pthread_join (dispatch_thread_id, /* ret = */ NULL);
2332 dispatch_thread_running = 0;
2333 }
2335 free_sockent (listen_sockets);
2337 if (send_buffer_fill > 0)
2338 flush_buffer ();
2340 if (cache_tree != NULL)
2341 {
2342 void *key;
2343 void *value;
2345 while (c_avl_pick (cache_tree, &key, &value) == 0)
2346 {
2347 sfree (key);
2348 sfree (value);
2349 }
2350 c_avl_destroy (cache_tree);
2351 cache_tree = NULL;
2352 }
2354 /* TODO: Close `sending_sockets' */
2356 plugin_unregister_config ("network");
2357 plugin_unregister_init ("network");
2358 plugin_unregister_write ("network");
2359 plugin_unregister_shutdown ("network");
2361 /* Let the init function do it's move again ;) */
2362 cache_flush_last = 0;
2364 return (0);
2365 } /* int network_shutdown */
2367 static int network_init (void)
2368 {
2369 /* Check if we were already initialized. If so, just return - there's
2370 * nothing more to do (for now, that is). */
2371 if (cache_flush_last != 0)
2372 return (0);
2374 plugin_register_shutdown ("network", network_shutdown);
2376 network_init_buffer ();
2378 cache_tree = c_avl_create ((int (*) (const void *, const void *)) strcmp);
2379 cache_flush_last = time (NULL);
2381 /* setup socket(s) and so on */
2382 if (sending_sockets != NULL)
2383 {
2384 plugin_register_write ("network", network_write,
2385 /* user_data = */ NULL);
2386 plugin_register_notification ("network", network_notification,
2387 /* user_data = */ NULL);
2388 }
2390 /* If no threads need to be started, return here. */
2391 if ((listen_sockets_num == 0)
2392 || ((dispatch_thread_running != 0)
2393 && (receive_thread_running != 0)))
2394 return (0);
2396 if (dispatch_thread_running == 0)
2397 {
2398 int status;
2399 status = pthread_create (&dispatch_thread_id,
2400 NULL /* no attributes */,
2401 dispatch_thread,
2402 NULL /* no argument */);
2403 if (status != 0)
2404 {
2405 char errbuf[1024];
2406 ERROR ("network: pthread_create failed: %s",
2407 sstrerror (errno, errbuf,
2408 sizeof (errbuf)));
2409 }
2410 else
2411 {
2412 dispatch_thread_running = 1;
2413 }
2414 }
2416 if (receive_thread_running == 0)
2417 {
2418 int status;
2419 status = pthread_create (&receive_thread_id,
2420 NULL /* no attributes */,
2421 receive_thread,
2422 NULL /* no argument */);
2423 if (status != 0)
2424 {
2425 char errbuf[1024];
2426 ERROR ("network: pthread_create failed: %s",
2427 sstrerror (errno, errbuf,
2428 sizeof (errbuf)));
2429 }
2430 else
2431 {
2432 receive_thread_running = 1;
2433 }
2434 }
2436 return (0);
2437 } /* int network_init */
2439 /*
2440 * The flush option of the network plugin cannot flush individual identifiers.
2441 * All the values are added to a buffer and sent when the buffer is full, the
2442 * requested value may or may not be in there, it's not worth finding out. We
2443 * just send the buffer if `flush' is called - if the requested value was in
2444 * there, good. If not, well, then there is nothing to flush.. -octo
2445 */
2446 static int network_flush (int timeout,
2447 const char __attribute__((unused)) *identifier,
2448 user_data_t __attribute__((unused)) *user_data)
2449 {
2450 pthread_mutex_lock (&send_buffer_lock);
2452 if (((time (NULL) - cache_flush_last) >= timeout)
2453 && (send_buffer_fill > 0))
2454 {
2455 flush_buffer ();
2456 }
2458 pthread_mutex_unlock (&send_buffer_lock);
2460 return (0);
2461 } /* int network_flush */
2463 void module_register (void)
2464 {
2465 plugin_register_complex_config ("network", network_config);
2466 plugin_register_init ("network", network_init);
2467 plugin_register_flush ("network", network_flush,
2468 /* user_data = */ NULL);
2469 } /* void module_register */
2471 /* vim: set fdm=marker : */