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 #define _BSD_SOURCE /* For struct ip_mreq */
24 #include "collectd.h"
25 #include "plugin.h"
26 #include "common.h"
27 #include "configfile.h"
28 #include "utils_fbhash.h"
29 #include "utils_avltree.h"
31 #include "network.h"
33 #if HAVE_PTHREAD_H
34 # include <pthread.h>
35 #endif
36 #if HAVE_SYS_SOCKET_H
37 # include <sys/socket.h>
38 #endif
39 #if HAVE_NETDB_H
40 # include <netdb.h>
41 #endif
42 #if HAVE_NETINET_IN_H
43 # include <netinet/in.h>
44 #endif
45 #if HAVE_ARPA_INET_H
46 # include <arpa/inet.h>
47 #endif
48 #if HAVE_POLL_H
49 # include <poll.h>
50 #endif
52 #if HAVE_LIBGCRYPT
53 # include <gcrypt.h>
54 #endif
56 /* 1500 - 40 - 8 = Ethernet packet - IPv6 header - UDP header */
57 /* #define BUFF_SIZE 1452 */
59 #ifndef IPV6_ADD_MEMBERSHIP
60 # ifdef IPV6_JOIN_GROUP
61 # define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
62 # else
63 # error "Neither IP_ADD_MEMBERSHIP nor IPV6_JOIN_GROUP is defined"
64 # endif
65 #endif /* !IP_ADD_MEMBERSHIP */
67 /* Buffer size to allocate. */
68 #define BUFF_SIZE 1024
70 /*
71 * Maximum size required for encryption / signing:
72 *
73 * 42 bytes for the encryption header
74 * + 64 bytes for the username
75 * -----------
76 * = 106 bytes
77 */
78 #define BUFF_SIG_SIZE 106
80 /*
81 * Private data types
82 */
83 #define SECURITY_LEVEL_NONE 0
84 #if HAVE_LIBGCRYPT
85 # define SECURITY_LEVEL_SIGN 1
86 # define SECURITY_LEVEL_ENCRYPT 2
87 #endif
88 struct sockent_client
89 {
90 int fd;
91 struct sockaddr_storage *addr;
92 socklen_t addrlen;
93 #if HAVE_LIBGCRYPT
94 int security_level;
95 char *username;
96 char *password;
97 gcry_cipher_hd_t cypher;
98 unsigned char password_hash[32];
99 #endif
100 };
102 struct sockent_server
103 {
104 int *fd;
105 size_t fd_num;
106 #if HAVE_LIBGCRYPT
107 int security_level;
108 char *auth_file;
109 fbhash_t *userdb;
110 gcry_cipher_hd_t cypher;
111 #endif
112 };
114 typedef struct sockent
115 {
116 #define SOCKENT_TYPE_CLIENT 1
117 #define SOCKENT_TYPE_SERVER 2
118 int type;
120 char *node;
121 char *service;
123 union
124 {
125 struct sockent_client client;
126 struct sockent_server server;
127 } data;
129 struct sockent *next;
130 } sockent_t;
132 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
133 * 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
134 * +-------+-----------------------+-------------------------------+
135 * ! Ver. ! ! Length !
136 * +-------+-----------------------+-------------------------------+
137 */
138 struct part_header_s
139 {
140 uint16_t type;
141 uint16_t length;
142 };
143 typedef struct part_header_s part_header_t;
145 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
146 * 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
147 * +-------------------------------+-------------------------------+
148 * ! Type ! Length !
149 * +-------------------------------+-------------------------------+
150 * : (Length - 4) Bytes :
151 * +---------------------------------------------------------------+
152 */
153 struct part_string_s
154 {
155 part_header_t *head;
156 char *value;
157 };
158 typedef struct part_string_s part_string_t;
160 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
161 * 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
162 * +-------------------------------+-------------------------------+
163 * ! Type ! Length !
164 * +-------------------------------+-------------------------------+
165 * : (Length - 4 == 2 || 4 || 8) Bytes :
166 * +---------------------------------------------------------------+
167 */
168 struct part_number_s
169 {
170 part_header_t *head;
171 uint64_t *value;
172 };
173 typedef struct part_number_s part_number_t;
175 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
176 * 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
177 * +-------------------------------+-------------------------------+
178 * ! Type ! Length !
179 * +-------------------------------+---------------+---------------+
180 * ! Num of values ! Type0 ! Type1 !
181 * +-------------------------------+---------------+---------------+
182 * ! Value0 !
183 * ! !
184 * +---------------------------------------------------------------+
185 * ! Value1 !
186 * ! !
187 * +---------------------------------------------------------------+
188 */
189 struct part_values_s
190 {
191 part_header_t *head;
192 uint16_t *num_values;
193 uint8_t *values_types;
194 value_t *values;
195 };
196 typedef struct part_values_s part_values_t;
198 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
199 * 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
200 * +-------------------------------+-------------------------------+
201 * ! Type ! Length !
202 * +-------------------------------+-------------------------------+
203 * ! Hash (Bits 0 - 31) !
204 * : : :
205 * ! Hash (Bits 224 - 255) !
206 * +---------------------------------------------------------------+
207 */
208 /* Minimum size */
209 #define PART_SIGNATURE_SHA256_SIZE 36
210 struct part_signature_sha256_s
211 {
212 part_header_t head;
213 unsigned char hash[32];
214 char *username;
215 };
216 typedef struct part_signature_sha256_s part_signature_sha256_t;
218 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
219 * 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
220 * +-------------------------------+-------------------------------+
221 * ! Type ! Length !
222 * +-------------------------------+-------------------------------+
223 * ! Original length ! Padding (0 - 15 bytes) !
224 * +-------------------------------+-------------------------------+
225 * ! Hash (Bits 0 - 31) !
226 * : : :
227 * ! Hash (Bits 128 - 159) !
228 * +---------------------------------------------------------------+
229 */
230 /* Minimum size */
231 #define PART_ENCRYPTION_AES256_SIZE 42
232 struct part_encryption_aes256_s
233 {
234 part_header_t head;
235 uint16_t username_length;
236 char *username;
237 unsigned char iv[16];
238 /* <encrypted> */
239 unsigned char hash[20];
240 /* <payload /> */
241 /* </encrypted> */
242 };
243 typedef struct part_encryption_aes256_s part_encryption_aes256_t;
245 struct receive_list_entry_s
246 {
247 char data[BUFF_SIZE];
248 int data_len;
249 int fd;
250 struct receive_list_entry_s *next;
251 };
252 typedef struct receive_list_entry_s receive_list_entry_t;
254 /*
255 * Private variables
256 */
257 static int network_config_ttl = 0;
258 static int network_config_forward = 0;
260 static sockent_t *sending_sockets = NULL;
262 static receive_list_entry_t *receive_list_head = NULL;
263 static receive_list_entry_t *receive_list_tail = NULL;
264 static pthread_mutex_t receive_list_lock = PTHREAD_MUTEX_INITIALIZER;
265 static pthread_cond_t receive_list_cond = PTHREAD_COND_INITIALIZER;
267 static sockent_t *listen_sockets = NULL;
268 static struct pollfd *listen_sockets_pollfd = NULL;
269 static size_t listen_sockets_num = 0;
271 /* The receive and dispatch threads will run as long as `listen_loop' is set to
272 * zero. */
273 static int listen_loop = 0;
274 static int receive_thread_running = 0;
275 static pthread_t receive_thread_id;
276 static int dispatch_thread_running = 0;
277 static pthread_t dispatch_thread_id;
279 /* Buffer in which to-be-sent network packets are constructed. */
280 static char send_buffer[BUFF_SIZE];
281 static char *send_buffer_ptr;
282 static int send_buffer_fill;
283 static value_list_t send_buffer_vl = VALUE_LIST_STATIC;
284 static pthread_mutex_t send_buffer_lock = PTHREAD_MUTEX_INITIALIZER;
286 /* In this cache we store all the values we received, so we can send out only
287 * those values which were *not* received via the network plugin, too. This is
288 * used for the `Forward false' option. */
289 static c_avl_tree_t *cache_tree = NULL;
290 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
291 static time_t cache_flush_last = 0;
292 static int cache_flush_interval = 1800;
294 /*
295 * Private functions
296 */
297 static int cache_flush (void)
298 {
299 char **keys = NULL;
300 int keys_num = 0;
302 char **tmp;
303 int i;
305 char *key;
306 time_t *value;
307 c_avl_iterator_t *iter;
309 time_t curtime = time (NULL);
311 iter = c_avl_get_iterator (cache_tree);
312 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
313 {
314 if ((curtime - *value) <= cache_flush_interval)
315 continue;
316 tmp = (char **) realloc (keys,
317 (keys_num + 1) * sizeof (char *));
318 if (tmp == NULL)
319 {
320 sfree (keys);
321 c_avl_iterator_destroy (iter);
322 ERROR ("network plugin: cache_flush: realloc"
323 " failed.");
324 return (-1);
325 }
326 keys = tmp;
327 keys[keys_num] = key;
328 keys_num++;
329 } /* while (c_avl_iterator_next) */
330 c_avl_iterator_destroy (iter);
332 for (i = 0; i < keys_num; i++)
333 {
334 if (c_avl_remove (cache_tree, keys[i], (void *) &key,
335 (void *) &value) != 0)
336 {
337 WARNING ("network plugin: cache_flush: c_avl_remove"
338 " (%s) failed.", keys[i]);
339 continue;
340 }
342 sfree (key);
343 sfree (value);
344 }
346 sfree (keys);
348 DEBUG ("network plugin: cache_flush: Removed %i %s",
349 keys_num, (keys_num == 1) ? "entry" : "entries");
350 cache_flush_last = curtime;
351 return (0);
352 } /* int cache_flush */
354 static int cache_check (const value_list_t *vl)
355 {
356 char key[1024];
357 time_t *value = NULL;
358 int retval = -1;
360 if (cache_tree == NULL)
361 return (-1);
363 if (format_name (key, sizeof (key), vl->host, vl->plugin,
364 vl->plugin_instance, vl->type, vl->type_instance))
365 return (-1);
367 pthread_mutex_lock (&cache_lock);
369 if (c_avl_get (cache_tree, key, (void *) &value) == 0)
370 {
371 if (*value < vl->time)
372 {
373 *value = vl->time;
374 retval = 0;
375 }
376 else
377 {
378 DEBUG ("network plugin: cache_check: *value = %i >= vl->time = %i",
379 (int) *value, (int) vl->time);
380 retval = 1;
381 }
382 }
383 else
384 {
385 char *key_copy = strdup (key);
386 value = malloc (sizeof (time_t));
387 if ((key_copy != NULL) && (value != NULL))
388 {
389 *value = vl->time;
390 c_avl_insert (cache_tree, key_copy, value);
391 retval = 0;
392 }
393 else
394 {
395 sfree (key_copy);
396 sfree (value);
397 }
398 }
400 if ((time (NULL) - cache_flush_last) > cache_flush_interval)
401 cache_flush ();
403 pthread_mutex_unlock (&cache_lock);
405 return (retval);
406 } /* int cache_check */
408 #if HAVE_LIBGCRYPT
409 static gcry_cipher_hd_t network_get_aes256_cypher (sockent_t *se, /* {{{ */
410 const void *iv, size_t iv_size, const char *username)
411 {
412 gcry_error_t err;
413 gcry_cipher_hd_t *cyper_ptr;
414 unsigned char password_hash[32];
416 if (se->type == SOCKENT_TYPE_CLIENT)
417 {
418 cyper_ptr = &se->data.client.cypher;
419 memcpy (password_hash, se->data.client.password_hash,
420 sizeof (password_hash));
421 }
422 else
423 {
424 char *secret;
426 cyper_ptr = &se->data.server.cypher;
428 if (username == NULL)
429 return (NULL);
431 secret = fbh_get (se->data.server.userdb, username);
432 if (secret == NULL)
433 return (NULL);
435 gcry_md_hash_buffer (GCRY_MD_SHA256,
436 password_hash,
437 secret, strlen (secret));
439 sfree (secret);
440 }
442 if (*cyper_ptr == NULL)
443 {
444 err = gcry_cipher_open (cyper_ptr,
445 GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_OFB, /* flags = */ 0);
446 if (err != 0)
447 {
448 ERROR ("network plugin: gcry_cipher_open returned: %s",
449 gcry_strerror (err));
450 *cyper_ptr = NULL;
451 return (NULL);
452 }
453 }
454 else
455 {
456 gcry_cipher_reset (*cyper_ptr);
457 }
458 assert (*cyper_ptr != NULL);
460 err = gcry_cipher_setkey (*cyper_ptr,
461 password_hash, sizeof (password_hash));
462 if (err != 0)
463 {
464 ERROR ("network plugin: gcry_cipher_setkey returned: %s",
465 gcry_strerror (err));
466 gcry_cipher_close (*cyper_ptr);
467 *cyper_ptr = NULL;
468 return (NULL);
469 }
471 err = gcry_cipher_setiv (*cyper_ptr, iv, iv_size);
472 if (err != 0)
473 {
474 ERROR ("network plugin: gcry_cipher_setkey returned: %s",
475 gcry_strerror (err));
476 gcry_cipher_close (*cyper_ptr);
477 *cyper_ptr = NULL;
478 return (NULL);
479 }
481 return (*cyper_ptr);
482 } /* }}} int network_get_aes256_cypher */
483 #endif /* HAVE_LIBGCRYPT */
485 static int write_part_values (char **ret_buffer, int *ret_buffer_len,
486 const data_set_t *ds, const value_list_t *vl)
487 {
488 char *packet_ptr;
489 int packet_len;
490 int num_values;
492 part_header_t pkg_ph;
493 uint16_t pkg_num_values;
494 uint8_t *pkg_values_types;
495 value_t *pkg_values;
497 int offset;
498 int i;
500 num_values = vl->values_len;
501 packet_len = sizeof (part_header_t) + sizeof (uint16_t)
502 + (num_values * sizeof (uint8_t))
503 + (num_values * sizeof (value_t));
505 if (*ret_buffer_len < packet_len)
506 return (-1);
508 pkg_values_types = (uint8_t *) malloc (num_values * sizeof (uint8_t));
509 if (pkg_values_types == NULL)
510 {
511 ERROR ("network plugin: write_part_values: malloc failed.");
512 return (-1);
513 }
515 pkg_values = (value_t *) malloc (num_values * sizeof (value_t));
516 if (pkg_values == NULL)
517 {
518 free (pkg_values_types);
519 ERROR ("network plugin: write_part_values: malloc failed.");
520 return (-1);
521 }
523 pkg_ph.type = htons (TYPE_VALUES);
524 pkg_ph.length = htons (packet_len);
526 pkg_num_values = htons ((uint16_t) vl->values_len);
528 for (i = 0; i < num_values; i++)
529 {
530 pkg_values_types[i] = (uint8_t) ds->ds[i].type;
531 switch (ds->ds[i].type)
532 {
533 case DS_TYPE_COUNTER:
534 pkg_values[i].counter = htonll (vl->values[i].counter);
535 break;
537 case DS_TYPE_GAUGE:
538 pkg_values[i].gauge = htond (vl->values[i].gauge);
539 break;
541 case DS_TYPE_DERIVE:
542 pkg_values[i].derive = htonll (vl->values[i].derive);
543 break;
545 case DS_TYPE_ABSOLUTE:
546 pkg_values[i].absolute = htonll (vl->values[i].absolute);
547 break;
549 default:
550 free (pkg_values_types);
551 free (pkg_values);
552 ERROR ("network plugin: write_part_values: "
553 "Unknown data source type: %i",
554 ds->ds[i].type);
555 return (-1);
556 } /* switch (ds->ds[i].type) */
557 } /* for (num_values) */
559 /*
560 * Use `memcpy' to write everything to the buffer, because the pointer
561 * may be unaligned and some architectures, such as SPARC, can't handle
562 * that.
563 */
564 packet_ptr = *ret_buffer;
565 offset = 0;
566 memcpy (packet_ptr + offset, &pkg_ph, sizeof (pkg_ph));
567 offset += sizeof (pkg_ph);
568 memcpy (packet_ptr + offset, &pkg_num_values, sizeof (pkg_num_values));
569 offset += sizeof (pkg_num_values);
570 memcpy (packet_ptr + offset, pkg_values_types, num_values * sizeof (uint8_t));
571 offset += num_values * sizeof (uint8_t);
572 memcpy (packet_ptr + offset, pkg_values, num_values * sizeof (value_t));
573 offset += num_values * sizeof (value_t);
575 assert (offset == packet_len);
577 *ret_buffer = packet_ptr + packet_len;
578 *ret_buffer_len -= packet_len;
580 free (pkg_values_types);
581 free (pkg_values);
583 return (0);
584 } /* int write_part_values */
586 static int write_part_number (char **ret_buffer, int *ret_buffer_len,
587 int type, uint64_t value)
588 {
589 char *packet_ptr;
590 int packet_len;
592 part_header_t pkg_head;
593 uint64_t pkg_value;
595 int offset;
597 packet_len = sizeof (pkg_head) + sizeof (pkg_value);
599 if (*ret_buffer_len < packet_len)
600 return (-1);
602 pkg_head.type = htons (type);
603 pkg_head.length = htons (packet_len);
604 pkg_value = htonll (value);
606 packet_ptr = *ret_buffer;
607 offset = 0;
608 memcpy (packet_ptr + offset, &pkg_head, sizeof (pkg_head));
609 offset += sizeof (pkg_head);
610 memcpy (packet_ptr + offset, &pkg_value, sizeof (pkg_value));
611 offset += sizeof (pkg_value);
613 assert (offset == packet_len);
615 *ret_buffer = packet_ptr + packet_len;
616 *ret_buffer_len -= packet_len;
618 return (0);
619 } /* int write_part_number */
621 static int write_part_string (char **ret_buffer, int *ret_buffer_len,
622 int type, const char *str, int str_len)
623 {
624 char *buffer;
625 int buffer_len;
627 uint16_t pkg_type;
628 uint16_t pkg_length;
630 int offset;
632 buffer_len = 2 * sizeof (uint16_t) + str_len + 1;
633 if (*ret_buffer_len < buffer_len)
634 return (-1);
636 pkg_type = htons (type);
637 pkg_length = htons (buffer_len);
639 buffer = *ret_buffer;
640 offset = 0;
641 memcpy (buffer + offset, (void *) &pkg_type, sizeof (pkg_type));
642 offset += sizeof (pkg_type);
643 memcpy (buffer + offset, (void *) &pkg_length, sizeof (pkg_length));
644 offset += sizeof (pkg_length);
645 memcpy (buffer + offset, str, str_len);
646 offset += str_len;
647 memset (buffer + offset, '\0', 1);
648 offset += 1;
650 assert (offset == buffer_len);
652 *ret_buffer = buffer + buffer_len;
653 *ret_buffer_len -= buffer_len;
655 return (0);
656 } /* int write_part_string */
658 static int parse_part_values (void **ret_buffer, size_t *ret_buffer_len,
659 value_t **ret_values, int *ret_num_values)
660 {
661 char *buffer = *ret_buffer;
662 size_t buffer_len = *ret_buffer_len;
664 uint16_t tmp16;
665 size_t exp_size;
666 int i;
668 uint16_t pkg_length;
669 uint16_t pkg_type;
670 uint16_t pkg_numval;
672 uint8_t *pkg_types;
673 value_t *pkg_values;
675 if (buffer_len < 15)
676 {
677 NOTICE ("network plugin: packet is too short: "
678 "buffer_len = %zu", buffer_len);
679 return (-1);
680 }
682 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
683 buffer += sizeof (tmp16);
684 pkg_type = ntohs (tmp16);
686 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
687 buffer += sizeof (tmp16);
688 pkg_length = ntohs (tmp16);
690 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
691 buffer += sizeof (tmp16);
692 pkg_numval = ntohs (tmp16);
694 assert (pkg_type == TYPE_VALUES);
696 exp_size = 3 * sizeof (uint16_t)
697 + pkg_numval * (sizeof (uint8_t) + sizeof (value_t));
698 if ((buffer_len < 0) || (buffer_len < exp_size))
699 {
700 WARNING ("network plugin: parse_part_values: "
701 "Packet too short: "
702 "Chunk of size %zu expected, "
703 "but buffer has only %zu bytes left.",
704 exp_size, buffer_len);
705 return (-1);
706 }
708 if (pkg_length != exp_size)
709 {
710 WARNING ("network plugin: parse_part_values: "
711 "Length and number of values "
712 "in the packet don't match.");
713 return (-1);
714 }
716 pkg_types = (uint8_t *) malloc (pkg_numval * sizeof (uint8_t));
717 pkg_values = (value_t *) malloc (pkg_numval * sizeof (value_t));
718 if ((pkg_types == NULL) || (pkg_values == NULL))
719 {
720 sfree (pkg_types);
721 sfree (pkg_values);
722 ERROR ("network plugin: parse_part_values: malloc failed.");
723 return (-1);
724 }
726 memcpy ((void *) pkg_types, (void *) buffer, pkg_numval * sizeof (uint8_t));
727 buffer += pkg_numval * sizeof (uint8_t);
728 memcpy ((void *) pkg_values, (void *) buffer, pkg_numval * sizeof (value_t));
729 buffer += pkg_numval * sizeof (value_t);
731 for (i = 0; i < pkg_numval; i++)
732 {
733 switch (pkg_types[i])
734 {
735 case DS_TYPE_COUNTER:
736 pkg_values[i].counter = ntohll (pkg_values[i].counter);
737 break;
739 case DS_TYPE_GAUGE:
740 pkg_values[i].gauge = ntohd (pkg_values[i].gauge);
741 break;
743 case DS_TYPE_DERIVE:
744 pkg_values[i].derive = ntohll (pkg_values[i].derive);
745 break;
747 case DS_TYPE_ABSOLUTE:
748 pkg_values[i].absolute = ntohll (pkg_values[i].absolute);
749 break;
751 default:
752 sfree (pkg_types);
753 sfree (pkg_values);
754 NOTICE ("network plugin: parse_part_values: "
755 "Don't know how to handle data source type %"PRIu8,
756 pkg_types[i]);
757 return (-1);
758 } /* switch (pkg_types[i]) */
760 if (pkg_types[i] == DS_TYPE_COUNTER)
761 pkg_values[i].counter = ntohll (pkg_values[i].counter);
762 else if (pkg_types[i] == DS_TYPE_GAUGE)
763 pkg_values[i].gauge = ntohd (pkg_values[i].gauge);
764 }
766 *ret_buffer = buffer;
767 *ret_buffer_len = buffer_len - pkg_length;
768 *ret_num_values = pkg_numval;
769 *ret_values = pkg_values;
771 sfree (pkg_types);
773 return (0);
774 } /* int parse_part_values */
776 static int parse_part_number (void **ret_buffer, size_t *ret_buffer_len,
777 uint64_t *value)
778 {
779 char *buffer = *ret_buffer;
780 size_t buffer_len = *ret_buffer_len;
782 uint16_t tmp16;
783 uint64_t tmp64;
784 size_t exp_size = 2 * sizeof (uint16_t) + sizeof (uint64_t);
786 uint16_t pkg_length;
787 uint16_t pkg_type;
789 if ((buffer_len < 0) || ((size_t) buffer_len < exp_size))
790 {
791 WARNING ("network plugin: parse_part_number: "
792 "Packet too short: "
793 "Chunk of size %zu expected, "
794 "but buffer has only %zu bytes left.",
795 exp_size, buffer_len);
796 return (-1);
797 }
799 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
800 buffer += sizeof (tmp16);
801 pkg_type = ntohs (tmp16);
803 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
804 buffer += sizeof (tmp16);
805 pkg_length = ntohs (tmp16);
807 memcpy ((void *) &tmp64, buffer, sizeof (tmp64));
808 buffer += sizeof (tmp64);
809 *value = ntohll (tmp64);
811 *ret_buffer = buffer;
812 *ret_buffer_len = buffer_len - pkg_length;
814 return (0);
815 } /* int parse_part_number */
817 static int parse_part_string (void **ret_buffer, size_t *ret_buffer_len,
818 char *output, int output_len)
819 {
820 char *buffer = *ret_buffer;
821 size_t buffer_len = *ret_buffer_len;
823 uint16_t tmp16;
824 size_t header_size = 2 * sizeof (uint16_t);
826 uint16_t pkg_length;
827 uint16_t pkg_type;
829 if ((buffer_len < 0) || (buffer_len < header_size))
830 {
831 WARNING ("network plugin: parse_part_string: "
832 "Packet too short: "
833 "Chunk of at least size %zu expected, "
834 "but buffer has only %zu bytes left.",
835 header_size, buffer_len);
836 return (-1);
837 }
839 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
840 buffer += sizeof (tmp16);
841 pkg_type = ntohs (tmp16);
843 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
844 buffer += sizeof (tmp16);
845 pkg_length = ntohs (tmp16);
847 /* Check that packet fits in the input buffer */
848 if (pkg_length > buffer_len)
849 {
850 WARNING ("network plugin: parse_part_string: "
851 "Packet too big: "
852 "Chunk of size %"PRIu16" received, "
853 "but buffer has only %zu bytes left.",
854 pkg_length, buffer_len);
855 return (-1);
856 }
858 /* Check that pkg_length is in the valid range */
859 if (pkg_length <= header_size)
860 {
861 WARNING ("network plugin: parse_part_string: "
862 "Packet too short: "
863 "Header claims this packet is only %hu "
864 "bytes long.", pkg_length);
865 return (-1);
866 }
868 /* Check that the package data fits into the output buffer.
869 * The previous if-statement ensures that:
870 * `pkg_length > header_size' */
871 if ((output_len < 0)
872 || ((size_t) output_len < ((size_t) pkg_length - header_size)))
873 {
874 WARNING ("network plugin: parse_part_string: "
875 "Output buffer too small.");
876 return (-1);
877 }
879 /* All sanity checks successfull, let's copy the data over */
880 output_len = pkg_length - header_size;
881 memcpy ((void *) output, (void *) buffer, output_len);
882 buffer += output_len;
884 /* For some very weird reason '\0' doesn't do the trick on SPARC in
885 * this statement. */
886 if (output[output_len - 1] != 0)
887 {
888 WARNING ("network plugin: parse_part_string: "
889 "Received string does not end "
890 "with a NULL-byte.");
891 return (-1);
892 }
894 *ret_buffer = buffer;
895 *ret_buffer_len = buffer_len - pkg_length;
897 return (0);
898 } /* int parse_part_string */
900 /* Forward declaration: parse_part_sign_sha256 and parse_part_encr_aes256 call
901 * parse_packet and vice versa. */
902 #define PP_SIGNED 0x01
903 #define PP_ENCRYPTED 0x02
904 static int parse_packet (sockent_t *se,
905 void *buffer, size_t buffer_size, int flags);
907 #define BUFFER_READ(p,s) do { \
908 memcpy ((p), buffer + buffer_offset, (s)); \
909 buffer_offset += (s); \
910 } while (0)
912 #if HAVE_LIBGCRYPT
913 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
914 void **ret_buffer, size_t *ret_buffer_len, int flags)
915 {
916 char *buffer;
917 size_t buffer_len;
918 size_t buffer_offset;
920 size_t username_len;
921 char *secret;
923 part_signature_sha256_t pss;
924 uint16_t pss_head_length;
925 char hash[sizeof (pss.hash)];
927 gcry_md_hd_t hd;
928 gcry_error_t err;
929 unsigned char *hash_ptr;
931 buffer = *ret_buffer;
932 buffer_len = *ret_buffer_len;
933 buffer_offset = 0;
935 if (se->data.server.userdb == NULL)
936 {
937 NOTICE ("network plugin: Received signed network packet but can't verify "
938 "it because no user DB has been configured. Will accept it.");
939 return (0);
940 }
942 /* Check if the buffer has enough data for this structure. */
943 if (buffer_len <= PART_SIGNATURE_SHA256_SIZE)
944 return (-ENOMEM);
946 /* Read type and length header */
947 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
948 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
949 pss_head_length = ntohs (pss.head.length);
951 /* Check if the `pss_head_length' is within bounds. */
952 if ((pss_head_length <= PART_SIGNATURE_SHA256_SIZE)
953 || (pss_head_length > buffer_len))
954 {
955 ERROR ("network plugin: HMAC-SHA-256 with invalid length received.");
956 return (-1);
957 }
959 /* Copy the hash. */
960 BUFFER_READ (pss.hash, sizeof (pss.hash));
962 /* Calculate username length (without null byte) and allocate memory */
963 username_len = pss_head_length - PART_SIGNATURE_SHA256_SIZE;
964 pss.username = malloc (username_len + 1);
965 if (pss.username == NULL)
966 return (-ENOMEM);
968 /* Read the username */
969 BUFFER_READ (pss.username, username_len);
970 pss.username[username_len] = 0;
972 assert (buffer_offset == pss_head_length);
974 /* Query the password */
975 secret = fbh_get (se->data.server.userdb, pss.username);
976 if (secret == NULL)
977 {
978 ERROR ("network plugin: Unknown user: %s", pss.username);
979 sfree (pss.username);
980 return (-ENOENT);
981 }
983 /* Create a hash device and check the HMAC */
984 hd = NULL;
985 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
986 if (err != 0)
987 {
988 ERROR ("network plugin: Creating HMAC-SHA-256 object failed: %s",
989 gcry_strerror (err));
990 sfree (secret);
991 sfree (pss.username);
992 return (-1);
993 }
995 err = gcry_md_setkey (hd, secret, strlen (secret));
996 if (err != 0)
997 {
998 ERROR ("network plugin: gcry_md_setkey failed: %s", gcry_strerror (err));
999 gcry_md_close (hd);
1000 return (-1);
1001 }
1003 gcry_md_write (hd,
1004 buffer + PART_SIGNATURE_SHA256_SIZE,
1005 buffer_len - PART_SIGNATURE_SHA256_SIZE);
1006 hash_ptr = gcry_md_read (hd, GCRY_MD_SHA256);
1007 if (hash_ptr == NULL)
1008 {
1009 ERROR ("network plugin: gcry_md_read failed.");
1010 gcry_md_close (hd);
1011 sfree (secret);
1012 sfree (pss.username);
1013 return (-1);
1014 }
1015 memcpy (hash, hash_ptr, sizeof (hash));
1017 /* Clean up */
1018 gcry_md_close (hd);
1019 hd = NULL;
1021 sfree (secret);
1022 sfree (pss.username);
1024 if (memcmp (pss.hash, hash, sizeof (pss.hash)) != 0)
1025 {
1026 WARNING ("network plugin: Verifying HMAC-SHA-256 signature failed: "
1027 "Hash mismatch.");
1028 }
1029 else
1030 {
1031 parse_packet (se, buffer + buffer_offset, buffer_len - buffer_offset,
1032 flags | PP_SIGNED);
1033 }
1035 *ret_buffer = buffer + buffer_len;
1036 *ret_buffer_len = 0;
1038 return (0);
1039 } /* }}} int parse_part_sign_sha256 */
1040 /* #endif HAVE_LIBGCRYPT */
1042 #else /* if !HAVE_LIBGCRYPT */
1043 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
1044 void **ret_buffer, size_t *ret_buffer_size, int flags)
1045 {
1046 static int warning_has_been_printed = 0;
1048 char *buffer;
1049 size_t buffer_size;
1050 size_t buffer_offset;
1051 uint16_t part_len;
1053 part_signature_sha256_t pss;
1055 buffer = *ret_buffer;
1056 buffer_size = *ret_buffer_size;
1057 buffer_offset = 0;
1059 if (buffer_size <= PART_SIGNATURE_SHA256_SIZE)
1060 return (-ENOMEM);
1062 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
1063 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
1064 part_len = ntohs (pss.head.length);
1066 if ((part_len <= PART_SIGNATURE_SHA256_SIZE)
1067 || (part_len > buffer_size))
1068 return (-EINVAL);
1070 if (warning_has_been_printed == 0)
1071 {
1072 WARNING ("network plugin: Received signed packet, but the network "
1073 "plugin was not linked with libgcrypt, so I cannot "
1074 "verify the signature. The packet will be accepted.");
1075 warning_has_been_printed = 1;
1076 }
1078 parse_packet (se, buffer + part_len, buffer_size - part_len, flags);
1080 *ret_buffer = buffer + buffer_size;
1081 *ret_buffer_size = 0;
1083 return (0);
1084 } /* }}} int parse_part_sign_sha256 */
1085 #endif /* !HAVE_LIBGCRYPT */
1087 #if HAVE_LIBGCRYPT
1088 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1089 void **ret_buffer, size_t *ret_buffer_len,
1090 int flags)
1091 {
1092 char *buffer = *ret_buffer;
1093 size_t buffer_len = *ret_buffer_len;
1094 size_t payload_len;
1095 size_t part_size;
1096 size_t buffer_offset;
1097 uint16_t username_len;
1098 part_encryption_aes256_t pea;
1099 unsigned char hash[sizeof (pea.hash)];
1101 gcry_cipher_hd_t cypher;
1102 gcry_error_t err;
1104 /* Make sure at least the header if available. */
1105 if (buffer_len <= PART_ENCRYPTION_AES256_SIZE)
1106 {
1107 NOTICE ("network plugin: parse_part_encr_aes256: "
1108 "Discarding short packet.");
1109 return (-1);
1110 }
1112 buffer_offset = 0;
1114 /* Copy the unencrypted information into `pea'. */
1115 BUFFER_READ (&pea.head.type, sizeof (pea.head.type));
1116 BUFFER_READ (&pea.head.length, sizeof (pea.head.length));
1118 /* Check the `part size'. */
1119 part_size = ntohs (pea.head.length);
1120 if ((part_size <= PART_ENCRYPTION_AES256_SIZE)
1121 || (part_size > buffer_len))
1122 {
1123 NOTICE ("network plugin: parse_part_encr_aes256: "
1124 "Discarding part with invalid size.");
1125 return (-1);
1126 }
1128 /* Read the username */
1129 BUFFER_READ (&username_len, sizeof (username_len));
1130 username_len = ntohs (username_len);
1132 if ((username_len <= 0)
1133 || (username_len > (part_size - (PART_ENCRYPTION_AES256_SIZE + 1))))
1134 {
1135 NOTICE ("network plugin: parse_part_encr_aes256: "
1136 "Discarding part with invalid username length.");
1137 return (-1);
1138 }
1140 assert (username_len > 0);
1141 pea.username = malloc (username_len + 1);
1142 if (pea.username == NULL)
1143 return (-ENOMEM);
1144 BUFFER_READ (pea.username, username_len);
1145 pea.username[username_len] = 0;
1147 /* Last but not least, the initialization vector */
1148 BUFFER_READ (pea.iv, sizeof (pea.iv));
1150 /* Make sure we are at the right position */
1151 assert (buffer_offset == (username_len +
1152 PART_ENCRYPTION_AES256_SIZE - sizeof (pea.hash)));
1154 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
1155 pea.username);
1156 if (cypher == NULL)
1157 return (-1);
1159 payload_len = part_size - (PART_ENCRYPTION_AES256_SIZE + username_len);
1160 assert (payload_len > 0);
1162 /* Decrypt the packet in-place */
1163 err = gcry_cipher_decrypt (cypher,
1164 buffer + buffer_offset,
1165 part_size - buffer_offset,
1166 /* in = */ NULL, /* in len = */ 0);
1167 if (err != 0)
1168 {
1169 ERROR ("network plugin: gcry_cipher_decrypt returned: %s",
1170 gcry_strerror (err));
1171 return (-1);
1172 }
1174 /* Read the hash */
1175 BUFFER_READ (pea.hash, sizeof (pea.hash));
1177 /* Make sure we're at the right position - again */
1178 assert (buffer_offset == (username_len + PART_ENCRYPTION_AES256_SIZE));
1179 assert (buffer_offset == (part_size - payload_len));
1181 /* Check hash sum */
1182 memset (hash, 0, sizeof (hash));
1183 gcry_md_hash_buffer (GCRY_MD_SHA1, hash,
1184 buffer + buffer_offset, payload_len);
1185 if (memcmp (hash, pea.hash, sizeof (hash)) != 0)
1186 {
1187 ERROR ("network plugin: Decryption failed: Checksum mismatch.");
1188 return (-1);
1189 }
1191 parse_packet (se, buffer + buffer_offset, payload_len,
1192 flags | PP_ENCRYPTED);
1194 /* Update return values */
1195 *ret_buffer = buffer + part_size;
1196 *ret_buffer_len = buffer_len - part_size;
1198 return (0);
1199 } /* }}} int parse_part_encr_aes256 */
1200 /* #endif HAVE_LIBGCRYPT */
1202 #else /* if !HAVE_LIBGCRYPT */
1203 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1204 void **ret_buffer, size_t *ret_buffer_size, int flags)
1205 {
1206 static int warning_has_been_printed = 0;
1208 char *buffer;
1209 size_t buffer_size;
1210 size_t buffer_offset;
1212 part_header_t ph;
1213 size_t ph_length;
1215 buffer = *ret_buffer;
1216 buffer_size = *ret_buffer_size;
1217 buffer_offset = 0;
1219 /* parse_packet assures this minimum size. */
1220 assert (buffer_size >= (sizeof (ph.type) + sizeof (ph.length)));
1222 BUFFER_READ (&ph.type, sizeof (ph.type));
1223 BUFFER_READ (&ph.length, sizeof (ph.length));
1224 ph_length = ntohs (ph.length);
1226 if ((ph_length <= PART_ENCRYPTION_AES256_SIZE)
1227 || (ph_length > buffer_size))
1228 {
1229 ERROR ("network plugin: AES-256 encrypted part "
1230 "with invalid length received.");
1231 return (-1);
1232 }
1234 if (warning_has_been_printed == 0)
1235 {
1236 WARNING ("network plugin: Received encrypted packet, but the network "
1237 "plugin was not linked with libgcrypt, so I cannot "
1238 "decrypt it. The part will be discarded.");
1239 warning_has_been_printed = 1;
1240 }
1242 *ret_buffer += ph_length;
1243 *ret_buffer_size -= ph_length;
1245 return (0);
1246 } /* }}} int parse_part_encr_aes256 */
1247 #endif /* !HAVE_LIBGCRYPT */
1249 #undef BUFFER_READ
1251 static int parse_packet (sockent_t *se, /* {{{ */
1252 void *buffer, size_t buffer_size, int flags)
1253 {
1254 int status;
1256 value_list_t vl = VALUE_LIST_INIT;
1257 notification_t n;
1259 #if HAVE_LIBGCRYPT
1260 int packet_was_signed = (flags & PP_SIGNED);
1261 int packet_was_encrypted = (flags & PP_ENCRYPTED);
1262 int printed_ignore_warning = 0;
1263 #endif /* HAVE_LIBGCRYPT */
1266 memset (&vl, '\0', sizeof (vl));
1267 memset (&n, '\0', sizeof (n));
1268 status = 0;
1270 while ((status == 0) && (0 < buffer_size)
1271 && ((unsigned int) buffer_size > sizeof (part_header_t)))
1272 {
1273 uint16_t pkg_length;
1274 uint16_t pkg_type;
1276 memcpy ((void *) &pkg_type,
1277 (void *) buffer,
1278 sizeof (pkg_type));
1279 memcpy ((void *) &pkg_length,
1280 (void *) (buffer + sizeof (pkg_type)),
1281 sizeof (pkg_length));
1283 pkg_length = ntohs (pkg_length);
1284 pkg_type = ntohs (pkg_type);
1286 if (pkg_length > buffer_size)
1287 break;
1288 /* Ensure that this loop terminates eventually */
1289 if (pkg_length < (2 * sizeof (uint16_t)))
1290 break;
1292 if (pkg_type == TYPE_ENCR_AES256)
1293 {
1294 status = parse_part_encr_aes256 (se,
1295 &buffer, &buffer_size, flags);
1296 if (status != 0)
1297 {
1298 ERROR ("network plugin: Decrypting AES256 "
1299 "part failed "
1300 "with status %i.", status);
1301 break;
1302 }
1303 }
1304 #if HAVE_LIBGCRYPT
1305 else if ((se->data.server.security_level == SECURITY_LEVEL_ENCRYPT)
1306 && (packet_was_encrypted == 0))
1307 {
1308 if (printed_ignore_warning == 0)
1309 {
1310 INFO ("network plugin: Unencrypted packet or "
1311 "part has been ignored.");
1312 printed_ignore_warning = 1;
1313 }
1314 buffer = ((char *) buffer) + pkg_length;
1315 continue;
1316 }
1317 #endif /* HAVE_LIBGCRYPT */
1318 else if (pkg_type == TYPE_SIGN_SHA256)
1319 {
1320 status = parse_part_sign_sha256 (se,
1321 &buffer, &buffer_size, flags);
1322 if (status != 0)
1323 {
1324 ERROR ("network plugin: Verifying HMAC-SHA-256 "
1325 "signature failed "
1326 "with status %i.", status);
1327 break;
1328 }
1329 }
1330 #if HAVE_LIBGCRYPT
1331 else if ((se->data.server.security_level == SECURITY_LEVEL_SIGN)
1332 && (packet_was_encrypted == 0)
1333 && (packet_was_signed == 0))
1334 {
1335 if (printed_ignore_warning == 0)
1336 {
1337 INFO ("network plugin: Unsigned packet or "
1338 "part has been ignored.");
1339 printed_ignore_warning = 1;
1340 }
1341 buffer = ((char *) buffer) + pkg_length;
1342 continue;
1343 }
1344 #endif /* HAVE_LIBGCRYPT */
1345 else if (pkg_type == TYPE_VALUES)
1346 {
1347 status = parse_part_values (&buffer, &buffer_size,
1348 &vl.values, &vl.values_len);
1350 if (status != 0)
1351 break;
1353 if ((vl.time > 0)
1354 && (strlen (vl.host) > 0)
1355 && (strlen (vl.plugin) > 0)
1356 && (strlen (vl.type) > 0)
1357 && (cache_check (&vl) == 0))
1358 {
1359 plugin_dispatch_values (&vl);
1360 }
1361 else
1362 {
1363 DEBUG ("network plugin: parse_packet:"
1364 " NOT dispatching values");
1365 }
1367 sfree (vl.values);
1368 }
1369 else if (pkg_type == TYPE_TIME)
1370 {
1371 uint64_t tmp = 0;
1372 status = parse_part_number (&buffer, &buffer_size,
1373 &tmp);
1374 if (status == 0)
1375 {
1376 vl.time = (time_t) tmp;
1377 n.time = (time_t) tmp;
1378 }
1379 }
1380 else if (pkg_type == TYPE_INTERVAL)
1381 {
1382 uint64_t tmp = 0;
1383 status = parse_part_number (&buffer, &buffer_size,
1384 &tmp);
1385 if (status == 0)
1386 vl.interval = (int) tmp;
1387 }
1388 else if (pkg_type == TYPE_HOST)
1389 {
1390 status = parse_part_string (&buffer, &buffer_size,
1391 vl.host, sizeof (vl.host));
1392 if (status == 0)
1393 sstrncpy (n.host, vl.host, sizeof (n.host));
1394 }
1395 else if (pkg_type == TYPE_PLUGIN)
1396 {
1397 status = parse_part_string (&buffer, &buffer_size,
1398 vl.plugin, sizeof (vl.plugin));
1399 if (status == 0)
1400 sstrncpy (n.plugin, vl.plugin,
1401 sizeof (n.plugin));
1402 }
1403 else if (pkg_type == TYPE_PLUGIN_INSTANCE)
1404 {
1405 status = parse_part_string (&buffer, &buffer_size,
1406 vl.plugin_instance,
1407 sizeof (vl.plugin_instance));
1408 if (status == 0)
1409 sstrncpy (n.plugin_instance,
1410 vl.plugin_instance,
1411 sizeof (n.plugin_instance));
1412 }
1413 else if (pkg_type == TYPE_TYPE)
1414 {
1415 status = parse_part_string (&buffer, &buffer_size,
1416 vl.type, sizeof (vl.type));
1417 if (status == 0)
1418 sstrncpy (n.type, vl.type, sizeof (n.type));
1419 }
1420 else if (pkg_type == TYPE_TYPE_INSTANCE)
1421 {
1422 status = parse_part_string (&buffer, &buffer_size,
1423 vl.type_instance,
1424 sizeof (vl.type_instance));
1425 if (status == 0)
1426 sstrncpy (n.type_instance, vl.type_instance,
1427 sizeof (n.type_instance));
1428 }
1429 else if (pkg_type == TYPE_MESSAGE)
1430 {
1431 status = parse_part_string (&buffer, &buffer_size,
1432 n.message, sizeof (n.message));
1434 if (status != 0)
1435 {
1436 /* do nothing */
1437 }
1438 else if ((n.severity != NOTIF_FAILURE)
1439 && (n.severity != NOTIF_WARNING)
1440 && (n.severity != NOTIF_OKAY))
1441 {
1442 INFO ("network plugin: "
1443 "Ignoring notification with "
1444 "unknown severity %i.",
1445 n.severity);
1446 }
1447 else if (n.time <= 0)
1448 {
1449 INFO ("network plugin: "
1450 "Ignoring notification with "
1451 "time == 0.");
1452 }
1453 else if (strlen (n.message) <= 0)
1454 {
1455 INFO ("network plugin: "
1456 "Ignoring notification with "
1457 "an empty message.");
1458 }
1459 else
1460 {
1461 plugin_dispatch_notification (&n);
1462 }
1463 }
1464 else if (pkg_type == TYPE_SEVERITY)
1465 {
1466 uint64_t tmp = 0;
1467 status = parse_part_number (&buffer, &buffer_size,
1468 &tmp);
1469 if (status == 0)
1470 n.severity = (int) tmp;
1471 }
1472 else
1473 {
1474 DEBUG ("network plugin: parse_packet: Unknown part"
1475 " type: 0x%04hx", pkg_type);
1476 buffer = ((char *) buffer) + pkg_length;
1477 }
1478 } /* while (buffer_size > sizeof (part_header_t)) */
1480 return (status);
1481 } /* }}} int parse_packet */
1483 static void free_sockent_client (struct sockent_client *sec) /* {{{ */
1484 {
1485 if (sec->fd >= 0)
1486 {
1487 close (sec->fd);
1488 sec->fd = -1;
1489 }
1490 sfree (sec->addr);
1491 #if HAVE_LIBGCRYPT
1492 sfree (sec->username);
1493 sfree (sec->password);
1494 if (sec->cypher != NULL)
1495 gcry_cipher_close (sec->cypher);
1496 #endif
1497 } /* }}} void free_sockent_client */
1499 static void free_sockent_server (struct sockent_server *ses) /* {{{ */
1500 {
1501 size_t i;
1503 for (i = 0; i < ses->fd_num; i++)
1504 {
1505 if (ses->fd[i] >= 0)
1506 {
1507 close (ses->fd[i]);
1508 ses->fd[i] = -1;
1509 }
1510 }
1512 sfree (ses->fd);
1513 #if HAVE_LIBGCRYPT
1514 sfree (ses->auth_file);
1515 fbh_destroy (ses->userdb);
1516 if (ses->cypher != NULL)
1517 gcry_cipher_close (ses->cypher);
1518 #endif
1519 } /* }}} void free_sockent_server */
1521 static void sockent_destroy (sockent_t *se) /* {{{ */
1522 {
1523 sockent_t *next;
1525 DEBUG ("network plugin: sockent_destroy (se = %p);", (void *) se);
1527 while (se != NULL)
1528 {
1529 next = se->next;
1531 sfree (se->node);
1532 sfree (se->service);
1534 if (se->type == SOCKENT_TYPE_CLIENT)
1535 free_sockent_client (&se->data.client);
1536 else
1537 free_sockent_server (&se->data.server);
1539 sfree (se);
1540 se = next;
1541 }
1542 } /* }}} void sockent_destroy */
1544 /*
1545 * int network_set_ttl
1546 *
1547 * Set the `IP_MULTICAST_TTL', `IP_TTL', `IPV6_MULTICAST_HOPS' or
1548 * `IPV6_UNICAST_HOPS', depending on which option is applicable.
1549 *
1550 * The `struct addrinfo' is used to destinguish between unicast and multicast
1551 * sockets.
1552 */
1553 static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
1554 {
1555 DEBUG ("network plugin: network_set_ttl: network_config_ttl = %i;",
1556 network_config_ttl);
1558 assert (se->type == SOCKENT_TYPE_CLIENT);
1560 if ((network_config_ttl < 1) || (network_config_ttl > 255))
1561 return (-1);
1563 if (ai->ai_family == AF_INET)
1564 {
1565 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1566 int optname;
1568 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1569 optname = IP_MULTICAST_TTL;
1570 else
1571 optname = IP_TTL;
1573 if (setsockopt (se->data.client.fd, IPPROTO_IP, optname,
1574 &network_config_ttl,
1575 sizeof (network_config_ttl)) == -1)
1576 {
1577 char errbuf[1024];
1578 ERROR ("setsockopt: %s",
1579 sstrerror (errno, errbuf, sizeof (errbuf)));
1580 return (-1);
1581 }
1582 }
1583 else if (ai->ai_family == AF_INET6)
1584 {
1585 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1586 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1587 int optname;
1589 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1590 optname = IPV6_MULTICAST_HOPS;
1591 else
1592 optname = IPV6_UNICAST_HOPS;
1594 if (setsockopt (se->data.client.fd, IPPROTO_IPV6, optname,
1595 &network_config_ttl,
1596 sizeof (network_config_ttl)) == -1)
1597 {
1598 char errbuf[1024];
1599 ERROR ("setsockopt: %s",
1600 sstrerror (errno, errbuf,
1601 sizeof (errbuf)));
1602 return (-1);
1603 }
1604 }
1606 return (0);
1607 } /* int network_set_ttl */
1609 static int network_bind_socket (int fd, const struct addrinfo *ai)
1610 {
1611 int loop = 0;
1612 int yes = 1;
1614 /* allow multiple sockets to use the same PORT number */
1615 if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR,
1616 &yes, sizeof(yes)) == -1) {
1617 char errbuf[1024];
1618 ERROR ("setsockopt: %s",
1619 sstrerror (errno, errbuf, sizeof (errbuf)));
1620 return (-1);
1621 }
1623 DEBUG ("fd = %i; calling `bind'", fd);
1625 if (bind (fd, ai->ai_addr, ai->ai_addrlen) == -1)
1626 {
1627 char errbuf[1024];
1628 ERROR ("bind: %s",
1629 sstrerror (errno, errbuf, sizeof (errbuf)));
1630 return (-1);
1631 }
1633 if (ai->ai_family == AF_INET)
1634 {
1635 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1636 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1637 {
1638 struct ip_mreq mreq;
1640 DEBUG ("fd = %i; IPv4 multicast address found", fd);
1642 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1643 mreq.imr_interface.s_addr = htonl (INADDR_ANY);
1645 if (setsockopt (fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1646 &loop, sizeof (loop)) == -1)
1647 {
1648 char errbuf[1024];
1649 ERROR ("setsockopt: %s",
1650 sstrerror (errno, errbuf,
1651 sizeof (errbuf)));
1652 return (-1);
1653 }
1655 if (setsockopt (fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1656 &mreq, sizeof (mreq)) == -1)
1657 {
1658 char errbuf[1024];
1659 ERROR ("setsockopt: %s",
1660 sstrerror (errno, errbuf,
1661 sizeof (errbuf)));
1662 return (-1);
1663 }
1664 }
1665 }
1666 else if (ai->ai_family == AF_INET6)
1667 {
1668 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1669 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1670 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1671 {
1672 struct ipv6_mreq mreq;
1674 DEBUG ("fd = %i; IPv6 multicast address found", fd);
1676 memcpy (&mreq.ipv6mr_multiaddr,
1677 &addr->sin6_addr,
1678 sizeof (addr->sin6_addr));
1680 /* http://developer.apple.com/documentation/Darwin/Reference/ManPages/man4/ip6.4.html
1681 * ipv6mr_interface may be set to zeroes to
1682 * choose the default multicast interface or to
1683 * the index of a particular multicast-capable
1684 * interface if the host is multihomed.
1685 * Membership is associ-associated with a
1686 * single interface; programs running on
1687 * multihomed hosts may need to join the same
1688 * group on more than one interface.*/
1689 mreq.ipv6mr_interface = 0;
1691 if (setsockopt (fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
1692 &loop, sizeof (loop)) == -1)
1693 {
1694 char errbuf[1024];
1695 ERROR ("setsockopt: %s",
1696 sstrerror (errno, errbuf,
1697 sizeof (errbuf)));
1698 return (-1);
1699 }
1701 if (setsockopt (fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
1702 &mreq, sizeof (mreq)) == -1)
1703 {
1704 char errbuf[1024];
1705 ERROR ("setsockopt: %s",
1706 sstrerror (errno, errbuf,
1707 sizeof (errbuf)));
1708 return (-1);
1709 }
1710 }
1711 }
1713 return (0);
1714 } /* int network_bind_socket */
1716 /* Initialize a sockent structure. `type' must be either `SOCKENT_TYPE_CLIENT'
1717 * or `SOCKENT_TYPE_SERVER' */
1718 static int sockent_init (sockent_t *se, int type) /* {{{ */
1719 {
1720 if (se == NULL)
1721 return (-1);
1723 memset (se, 0, sizeof (*se));
1725 se->type = SOCKENT_TYPE_CLIENT;
1726 se->node = NULL;
1727 se->service = NULL;
1728 se->next = NULL;
1730 if (type == SOCKENT_TYPE_SERVER)
1731 {
1732 se->type = SOCKENT_TYPE_SERVER;
1733 se->data.server.fd = NULL;
1734 #if HAVE_LIBGCRYPT
1735 se->data.server.security_level = SECURITY_LEVEL_NONE;
1736 se->data.server.auth_file = NULL;
1737 se->data.server.userdb = NULL;
1738 se->data.server.cypher = NULL;
1739 #endif
1740 }
1741 else
1742 {
1743 se->data.client.fd = -1;
1744 se->data.client.addr = NULL;
1745 #if HAVE_LIBGCRYPT
1746 se->data.client.security_level = SECURITY_LEVEL_NONE;
1747 se->data.client.username = NULL;
1748 se->data.client.password = NULL;
1749 se->data.client.cypher = NULL;
1750 #endif
1751 }
1753 return (0);
1754 } /* }}} int sockent_init */
1756 /* Open the file descriptors for a initialized sockent structure. */
1757 static int sockent_open (sockent_t *se) /* {{{ */
1758 {
1759 struct addrinfo ai_hints;
1760 struct addrinfo *ai_list, *ai_ptr;
1761 int ai_return;
1763 const char *node;
1764 const char *service;
1766 if (se == NULL)
1767 return (-1);
1769 /* Set up the security structures. */
1770 #if HAVE_LIBGCRYPT /* {{{ */
1771 if (se->type == SOCKENT_TYPE_CLIENT)
1772 {
1773 if (se->data.client.security_level > SECURITY_LEVEL_NONE)
1774 {
1775 if ((se->data.client.username == NULL)
1776 || (se->data.client.password == NULL))
1777 {
1778 ERROR ("network plugin: Client socket with "
1779 "security requested, but no "
1780 "credentials are configured.");
1781 return (-1);
1782 }
1783 gcry_md_hash_buffer (GCRY_MD_SHA256,
1784 se->data.client.password_hash,
1785 se->data.client.password,
1786 strlen (se->data.client.password));
1787 }
1788 }
1789 else /* (se->type == SOCKENT_TYPE_SERVER) */
1790 {
1791 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
1792 {
1793 if (se->data.server.auth_file == NULL)
1794 {
1795 ERROR ("network plugin: Server socket with "
1796 "security requested, but no "
1797 "password file is configured.");
1798 return (-1);
1799 }
1800 }
1801 if (se->data.server.auth_file != NULL)
1802 {
1803 se->data.server.userdb = fbh_create (se->data.server.auth_file);
1804 if (se->data.server.userdb == NULL)
1805 {
1806 ERROR ("network plugin: Reading password file "
1807 "`%s' failed.",
1808 se->data.server.auth_file);
1809 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
1810 return (-1);
1811 }
1812 }
1813 }
1814 #endif /* }}} HAVE_LIBGCRYPT */
1816 node = se->node;
1817 service = se->service;
1819 if (service == NULL)
1820 service = NET_DEFAULT_PORT;
1822 DEBUG ("network plugin: sockent_open: node = %s; service = %s;",
1823 node, service);
1825 memset (&ai_hints, 0, sizeof (ai_hints));
1826 ai_hints.ai_flags = 0;
1827 #ifdef AI_PASSIVE
1828 ai_hints.ai_flags |= AI_PASSIVE;
1829 #endif
1830 #ifdef AI_ADDRCONFIG
1831 ai_hints.ai_flags |= AI_ADDRCONFIG;
1832 #endif
1833 ai_hints.ai_family = AF_UNSPEC;
1834 ai_hints.ai_socktype = SOCK_DGRAM;
1835 ai_hints.ai_protocol = IPPROTO_UDP;
1837 ai_return = getaddrinfo (node, service, &ai_hints, &ai_list);
1838 if (ai_return != 0)
1839 {
1840 ERROR ("network plugin: getaddrinfo (%s, %s) failed: %s",
1841 (se->node == NULL) ? "(null)" : se->node,
1842 (se->service == NULL) ? "(null)" : se->service,
1843 gai_strerror (ai_return));
1844 return (-1);
1845 }
1847 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1848 {
1849 int status;
1851 if (se->type == SOCKENT_TYPE_SERVER) /* {{{ */
1852 {
1853 int *tmp;
1855 tmp = realloc (se->data.server.fd,
1856 sizeof (*tmp) * (se->data.server.fd_num + 1));
1857 if (tmp == NULL)
1858 {
1859 ERROR ("network plugin: realloc failed.");
1860 continue;
1861 }
1862 se->data.server.fd = tmp;
1863 tmp = se->data.server.fd + se->data.server.fd_num;
1865 *tmp = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
1866 ai_ptr->ai_protocol);
1867 if (*tmp < 0)
1868 {
1869 char errbuf[1024];
1870 ERROR ("network plugin: socket(2) failed: %s",
1871 sstrerror (errno, errbuf,
1872 sizeof (errbuf)));
1873 continue;
1874 }
1876 status = network_bind_socket (*tmp, ai_ptr);
1877 if (status != 0)
1878 {
1879 close (*tmp);
1880 *tmp = -1;
1881 continue;
1882 }
1884 se->data.server.fd_num++;
1885 continue;
1886 } /* }}} if (se->type == SOCKENT_TYPE_SERVER) */
1887 else /* if (se->type == SOCKENT_TYPE_CLIENT) {{{ */
1888 {
1889 se->data.client.fd = socket (ai_ptr->ai_family,
1890 ai_ptr->ai_socktype,
1891 ai_ptr->ai_protocol);
1892 if (se->data.client.fd < 0)
1893 {
1894 char errbuf[1024];
1895 ERROR ("network plugin: socket(2) failed: %s",
1896 sstrerror (errno, errbuf,
1897 sizeof (errbuf)));
1898 continue;
1899 }
1901 se->data.client.addr = malloc (sizeof (*se->data.client.addr));
1902 if (se->data.client.addr == NULL)
1903 {
1904 ERROR ("network plugin: malloc failed.");
1905 close (se->data.client.fd);
1906 se->data.client.fd = -1;
1907 continue;
1908 }
1910 memset (se->data.client.addr, 0, sizeof (*se->data.client.addr));
1911 assert (sizeof (*se->data.client.addr) >= ai_ptr->ai_addrlen);
1912 memcpy (se->data.client.addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1913 se->data.client.addrlen = ai_ptr->ai_addrlen;
1915 network_set_ttl (se, ai_ptr);
1917 /* We don't open more than one write-socket per
1918 * node/service pair.. */
1919 break;
1920 } /* }}} if (se->type == SOCKENT_TYPE_CLIENT) */
1921 } /* for (ai_list) */
1923 freeaddrinfo (ai_list);
1925 /* Check if all went well. */
1926 if (se->type == SOCKENT_TYPE_SERVER)
1927 {
1928 if (se->data.server.fd_num <= 0)
1929 return (-1);
1930 }
1931 else /* if (se->type == SOCKENT_TYPE_CLIENT) */
1932 {
1933 if (se->data.client.fd < 0)
1934 return (-1);
1935 }
1937 return (0);
1938 } /* }}} int sockent_open */
1940 /* Add a sockent to the global list of sockets */
1941 static int sockent_add (sockent_t *se) /* {{{ */
1942 {
1943 sockent_t *last_ptr;
1945 if (se == NULL)
1946 return (-1);
1948 if (se->type == SOCKENT_TYPE_SERVER)
1949 {
1950 struct pollfd *tmp;
1951 size_t i;
1953 tmp = realloc (listen_sockets_pollfd,
1954 sizeof (*tmp) * (listen_sockets_num
1955 + se->data.server.fd_num));
1956 if (tmp == NULL)
1957 {
1958 ERROR ("network plugin: realloc failed.");
1959 return (-1);
1960 }
1961 listen_sockets_pollfd = tmp;
1962 tmp = listen_sockets_pollfd + listen_sockets_num;
1964 for (i = 0; i < se->data.server.fd_num; i++)
1965 {
1966 memset (tmp + i, 0, sizeof (*tmp));
1967 tmp[i].fd = se->data.server.fd[i];
1968 tmp[i].events = POLLIN | POLLPRI;
1969 tmp[i].revents = 0;
1970 }
1972 listen_sockets_num += se->data.server.fd_num;
1974 if (listen_sockets == NULL)
1975 {
1976 listen_sockets = se;
1977 return (0);
1978 }
1979 last_ptr = listen_sockets;
1980 }
1981 else /* if (se->type == SOCKENT_TYPE_CLIENT) */
1982 {
1983 if (sending_sockets == NULL)
1984 {
1985 sending_sockets = se;
1986 return (0);
1987 }
1988 last_ptr = sending_sockets;
1989 }
1991 while (last_ptr->next != NULL)
1992 last_ptr = last_ptr->next;
1993 last_ptr->next = se;
1995 return (0);
1996 } /* }}} int sockent_add */
1998 static void *dispatch_thread (void __attribute__((unused)) *arg) /* {{{ */
1999 {
2000 while (42)
2001 {
2002 receive_list_entry_t *ent;
2003 sockent_t *se;
2005 /* Lock and wait for more data to come in */
2006 pthread_mutex_lock (&receive_list_lock);
2007 while ((listen_loop == 0)
2008 && (receive_list_head == NULL))
2009 pthread_cond_wait (&receive_list_cond, &receive_list_lock);
2011 /* Remove the head entry and unlock */
2012 ent = receive_list_head;
2013 if (ent != NULL)
2014 receive_list_head = ent->next;
2015 pthread_mutex_unlock (&receive_list_lock);
2017 /* Check whether we are supposed to exit. We do NOT check `listen_loop'
2018 * because we dispatch all missing packets before shutting down. */
2019 if (ent == NULL)
2020 break;
2022 /* Look for the correct `sockent_t' */
2023 se = listen_sockets;
2024 while (se != NULL)
2025 {
2026 size_t i;
2028 for (i = 0; i < se->data.server.fd_num; i++)
2029 if (se->data.server.fd[i] == ent->fd)
2030 break;
2032 if (i < se->data.server.fd_num)
2033 break;
2035 se = se->next;
2036 }
2038 if (se == NULL)
2039 {
2040 ERROR ("network plugin: Got packet from FD %i, but can't "
2041 "find an appropriate socket entry.",
2042 ent->fd);
2043 sfree (ent);
2044 continue;
2045 }
2047 parse_packet (se, ent->data, ent->data_len, /* flags = */ 0);
2048 sfree (ent);
2049 } /* while (42) */
2051 return (NULL);
2052 } /* }}} void *dispatch_thread */
2054 static int network_receive (void) /* {{{ */
2055 {
2056 char buffer[BUFF_SIZE];
2057 int buffer_len;
2059 int i;
2060 int status;
2062 receive_list_entry_t *private_list_head;
2063 receive_list_entry_t *private_list_tail;
2065 assert (listen_sockets_num > 0);
2067 private_list_head = NULL;
2068 private_list_tail = NULL;
2070 while (listen_loop == 0)
2071 {
2072 status = poll (listen_sockets_pollfd, listen_sockets_num, -1);
2074 if (status <= 0)
2075 {
2076 char errbuf[1024];
2077 if (errno == EINTR)
2078 continue;
2079 ERROR ("poll failed: %s",
2080 sstrerror (errno, errbuf, sizeof (errbuf)));
2081 return (-1);
2082 }
2084 for (i = 0; (i < listen_sockets_num) && (status > 0); i++)
2085 {
2086 receive_list_entry_t *ent;
2088 if ((listen_sockets_pollfd[i].revents
2089 & (POLLIN | POLLPRI)) == 0)
2090 continue;
2091 status--;
2093 buffer_len = recv (listen_sockets_pollfd[i].fd,
2094 buffer, sizeof (buffer),
2095 0 /* no flags */);
2096 if (buffer_len < 0)
2097 {
2098 char errbuf[1024];
2099 ERROR ("recv failed: %s",
2100 sstrerror (errno, errbuf,
2101 sizeof (errbuf)));
2102 return (-1);
2103 }
2105 /* TODO: Possible performance enhancement: Do not free
2106 * these entries in the dispatch thread but put them in
2107 * another list, so we don't have to allocate more and
2108 * more of these structures. */
2109 ent = malloc (sizeof (receive_list_entry_t));
2110 if (ent == NULL)
2111 {
2112 ERROR ("network plugin: malloc failed.");
2113 return (-1);
2114 }
2115 memset (ent, 0, sizeof (receive_list_entry_t));
2116 ent->fd = listen_sockets_pollfd[i].fd;
2117 ent->next = NULL;
2119 /* Hopefully this be optimized out by the compiler. It
2120 * might help prevent stupid bugs in the future though.
2121 */
2122 assert (sizeof (ent->data) == sizeof (buffer));
2124 memcpy (ent->data, buffer, buffer_len);
2125 ent->data_len = buffer_len;
2127 if (private_list_head == NULL)
2128 private_list_head = ent;
2129 else
2130 private_list_tail->next = ent;
2131 private_list_tail = ent;
2133 /* Do not block here. Blocking here has led to
2134 * insufficient performance in the past. */
2135 if (pthread_mutex_trylock (&receive_list_lock) == 0)
2136 {
2137 if (receive_list_head == NULL)
2138 receive_list_head = private_list_head;
2139 else
2140 receive_list_tail->next = private_list_head;
2141 receive_list_tail = private_list_tail;
2143 private_list_head = NULL;
2144 private_list_tail = NULL;
2146 pthread_cond_signal (&receive_list_cond);
2147 pthread_mutex_unlock (&receive_list_lock);
2148 }
2149 } /* for (listen_sockets_pollfd) */
2150 } /* while (listen_loop == 0) */
2152 /* Make sure everything is dispatched before exiting. */
2153 if (private_list_head != NULL)
2154 {
2155 pthread_mutex_lock (&receive_list_lock);
2157 if (receive_list_head == NULL)
2158 receive_list_head = private_list_head;
2159 else
2160 receive_list_tail->next = private_list_head;
2161 receive_list_tail = private_list_tail;
2163 private_list_head = NULL;
2164 private_list_tail = NULL;
2166 pthread_cond_signal (&receive_list_cond);
2167 pthread_mutex_unlock (&receive_list_lock);
2168 }
2170 return (0);
2171 } /* }}} int network_receive */
2173 static void *receive_thread (void __attribute__((unused)) *arg)
2174 {
2175 return (network_receive () ? (void *) 1 : (void *) 0);
2176 } /* void *receive_thread */
2178 static void network_init_buffer (void)
2179 {
2180 memset (send_buffer, 0, sizeof (send_buffer));
2181 send_buffer_ptr = send_buffer;
2182 send_buffer_fill = 0;
2184 memset (&send_buffer_vl, 0, sizeof (send_buffer_vl));
2185 } /* int network_init_buffer */
2187 static void networt_send_buffer_plain (const sockent_t *se, /* {{{ */
2188 const char *buffer, size_t buffer_size)
2189 {
2190 int status;
2192 while (42)
2193 {
2194 status = sendto (se->data.client.fd, buffer, buffer_size,
2195 /* flags = */ 0,
2196 (struct sockaddr *) se->data.client.addr,
2197 se->data.client.addrlen);
2198 if (status < 0)
2199 {
2200 char errbuf[1024];
2201 if (errno == EINTR)
2202 continue;
2203 ERROR ("network plugin: sendto failed: %s",
2204 sstrerror (errno, errbuf,
2205 sizeof (errbuf)));
2206 break;
2207 }
2209 break;
2210 } /* while (42) */
2211 } /* }}} void networt_send_buffer_plain */
2213 #if HAVE_LIBGCRYPT
2214 #define BUFFER_ADD(p,s) do { \
2215 memcpy (buffer + buffer_offset, (p), (s)); \
2216 buffer_offset += (s); \
2217 } while (0)
2219 static void networt_send_buffer_signed (const sockent_t *se, /* {{{ */
2220 const char *in_buffer, size_t in_buffer_size)
2221 {
2222 part_signature_sha256_t ps;
2223 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2224 size_t buffer_offset;
2225 size_t username_len;
2227 gcry_md_hd_t hd;
2228 gcry_error_t err;
2229 unsigned char *hash;
2231 hd = NULL;
2232 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
2233 if (err != 0)
2234 {
2235 ERROR ("network plugin: Creating HMAC object failed: %s",
2236 gcry_strerror (err));
2237 return;
2238 }
2240 err = gcry_md_setkey (hd, se->data.client.password,
2241 strlen (se->data.client.password));
2242 if (err != 0)
2243 {
2244 ERROR ("network plugin: gcry_md_setkey failed: %s",
2245 gcry_strerror (err));
2246 gcry_md_close (hd);
2247 return;
2248 }
2250 username_len = strlen (se->data.client.username);
2251 if (username_len > (BUFF_SIG_SIZE - PART_SIGNATURE_SHA256_SIZE))
2252 {
2253 ERROR ("network plugin: Username too long: %s",
2254 se->data.client.username);
2255 return;
2256 }
2258 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE,
2259 se->data.client.username, username_len);
2260 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE + username_len,
2261 in_buffer, in_buffer_size);
2263 /* Initialize the `ps' structure. */
2264 memset (&ps, 0, sizeof (ps));
2265 ps.head.type = htons (TYPE_SIGN_SHA256);
2266 ps.head.length = htons (PART_SIGNATURE_SHA256_SIZE + username_len);
2268 /* Calculate the hash value. */
2269 gcry_md_write (hd, buffer + PART_SIGNATURE_SHA256_SIZE,
2270 username_len + in_buffer_size);
2271 hash = gcry_md_read (hd, GCRY_MD_SHA256);
2272 if (hash == NULL)
2273 {
2274 ERROR ("network plugin: gcry_md_read failed.");
2275 gcry_md_close (hd);
2276 return;
2277 }
2278 memcpy (ps.hash, hash, sizeof (ps.hash));
2280 /* Add the header */
2281 buffer_offset = 0;
2283 BUFFER_ADD (&ps.head.type, sizeof (ps.head.type));
2284 BUFFER_ADD (&ps.head.length, sizeof (ps.head.length));
2285 BUFFER_ADD (ps.hash, sizeof (ps.hash));
2287 assert (buffer_offset == PART_SIGNATURE_SHA256_SIZE);
2289 gcry_md_close (hd);
2290 hd = NULL;
2292 buffer_offset = PART_SIGNATURE_SHA256_SIZE + username_len + in_buffer_size;
2293 networt_send_buffer_plain (se, buffer, buffer_offset);
2294 } /* }}} void networt_send_buffer_signed */
2296 static void networt_send_buffer_encrypted (sockent_t *se, /* {{{ */
2297 const char *in_buffer, size_t in_buffer_size)
2298 {
2299 part_encryption_aes256_t pea;
2300 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2301 size_t buffer_size;
2302 size_t buffer_offset;
2303 size_t header_size;
2304 size_t username_len;
2305 gcry_error_t err;
2306 gcry_cipher_hd_t cypher;
2308 /* Initialize the header fields */
2309 memset (&pea, 0, sizeof (pea));
2310 pea.head.type = htons (TYPE_ENCR_AES256);
2312 pea.username = se->data.client.username;
2314 username_len = strlen (pea.username);
2315 if ((PART_ENCRYPTION_AES256_SIZE + username_len) > BUFF_SIG_SIZE)
2316 {
2317 ERROR ("network plugin: Username too long: %s", pea.username);
2318 return;
2319 }
2321 buffer_size = PART_ENCRYPTION_AES256_SIZE + username_len + in_buffer_size;
2322 header_size = PART_ENCRYPTION_AES256_SIZE + username_len
2323 - sizeof (pea.hash);
2325 assert (buffer_size <= sizeof (buffer));
2326 DEBUG ("network plugin: networt_send_buffer_encrypted: "
2327 "buffer_size = %zu;", buffer_size);
2329 pea.head.length = htons ((uint16_t) (PART_ENCRYPTION_AES256_SIZE
2330 + username_len + in_buffer_size));
2331 pea.username_length = htons ((uint16_t) username_len);
2333 /* Chose a random initialization vector. */
2334 gcry_randomize ((void *) &pea.iv, sizeof (pea.iv), GCRY_STRONG_RANDOM);
2336 /* Create hash of the payload */
2337 gcry_md_hash_buffer (GCRY_MD_SHA1, pea.hash, in_buffer, in_buffer_size);
2339 /* Initialize the buffer */
2340 buffer_offset = 0;
2341 memset (buffer, 0, sizeof (buffer));
2344 BUFFER_ADD (&pea.head.type, sizeof (pea.head.type));
2345 BUFFER_ADD (&pea.head.length, sizeof (pea.head.length));
2346 BUFFER_ADD (&pea.username_length, sizeof (pea.username_length));
2347 BUFFER_ADD (pea.username, username_len);
2348 BUFFER_ADD (pea.iv, sizeof (pea.iv));
2349 assert (buffer_offset == header_size);
2350 BUFFER_ADD (pea.hash, sizeof (pea.hash));
2351 BUFFER_ADD (in_buffer, in_buffer_size);
2353 assert (buffer_offset == buffer_size);
2355 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
2356 se->data.client.password);
2357 if (cypher == NULL)
2358 return;
2360 /* Encrypt the buffer in-place */
2361 err = gcry_cipher_encrypt (cypher,
2362 buffer + header_size,
2363 buffer_size - header_size,
2364 /* in = */ NULL, /* in len = */ 0);
2365 if (err != 0)
2366 {
2367 ERROR ("network plugin: gcry_cipher_encrypt returned: %s",
2368 gcry_strerror (err));
2369 return;
2370 }
2372 /* Send it out without further modifications */
2373 networt_send_buffer_plain (se, buffer, buffer_size);
2374 } /* }}} void networt_send_buffer_encrypted */
2375 #undef BUFFER_ADD
2376 #endif /* HAVE_LIBGCRYPT */
2378 static void network_send_buffer (char *buffer, size_t buffer_len) /* {{{ */
2379 {
2380 sockent_t *se;
2382 DEBUG ("network plugin: network_send_buffer: buffer_len = %zu", buffer_len);
2384 for (se = sending_sockets; se != NULL; se = se->next)
2385 {
2386 #if HAVE_LIBGCRYPT
2387 if (se->data.client.security_level == SECURITY_LEVEL_ENCRYPT)
2388 networt_send_buffer_encrypted (se, buffer, buffer_len);
2389 else if (se->data.client.security_level == SECURITY_LEVEL_SIGN)
2390 networt_send_buffer_signed (se, buffer, buffer_len);
2391 else /* if (se->data.client.security_level == SECURITY_LEVEL_NONE) */
2392 #endif /* HAVE_LIBGCRYPT */
2393 networt_send_buffer_plain (se, buffer, buffer_len);
2394 } /* for (sending_sockets) */
2395 } /* }}} void network_send_buffer */
2397 static int add_to_buffer (char *buffer, int buffer_size, /* {{{ */
2398 value_list_t *vl_def,
2399 const data_set_t *ds, const value_list_t *vl)
2400 {
2401 char *buffer_orig = buffer;
2403 if (strcmp (vl_def->host, vl->host) != 0)
2404 {
2405 if (write_part_string (&buffer, &buffer_size, TYPE_HOST,
2406 vl->host, strlen (vl->host)) != 0)
2407 return (-1);
2408 sstrncpy (vl_def->host, vl->host, sizeof (vl_def->host));
2409 }
2411 if (vl_def->time != vl->time)
2412 {
2413 if (write_part_number (&buffer, &buffer_size, TYPE_TIME,
2414 (uint64_t) vl->time))
2415 return (-1);
2416 vl_def->time = vl->time;
2417 }
2419 if (vl_def->interval != vl->interval)
2420 {
2421 if (write_part_number (&buffer, &buffer_size, TYPE_INTERVAL,
2422 (uint64_t) vl->interval))
2423 return (-1);
2424 vl_def->interval = vl->interval;
2425 }
2427 if (strcmp (vl_def->plugin, vl->plugin) != 0)
2428 {
2429 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN,
2430 vl->plugin, strlen (vl->plugin)) != 0)
2431 return (-1);
2432 sstrncpy (vl_def->plugin, vl->plugin, sizeof (vl_def->plugin));
2433 }
2435 if (strcmp (vl_def->plugin_instance, vl->plugin_instance) != 0)
2436 {
2437 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
2438 vl->plugin_instance,
2439 strlen (vl->plugin_instance)) != 0)
2440 return (-1);
2441 sstrncpy (vl_def->plugin_instance, vl->plugin_instance, sizeof (vl_def->plugin_instance));
2442 }
2444 if (strcmp (vl_def->type, vl->type) != 0)
2445 {
2446 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
2447 vl->type, strlen (vl->type)) != 0)
2448 return (-1);
2449 sstrncpy (vl_def->type, ds->type, sizeof (vl_def->type));
2450 }
2452 if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
2453 {
2454 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE_INSTANCE,
2455 vl->type_instance,
2456 strlen (vl->type_instance)) != 0)
2457 return (-1);
2458 sstrncpy (vl_def->type_instance, vl->type_instance, sizeof (vl_def->type_instance));
2459 }
2461 if (write_part_values (&buffer, &buffer_size, ds, vl) != 0)
2462 return (-1);
2464 return (buffer - buffer_orig);
2465 } /* }}} int add_to_buffer */
2467 static void flush_buffer (void)
2468 {
2469 DEBUG ("network plugin: flush_buffer: send_buffer_fill = %i",
2470 send_buffer_fill);
2472 network_send_buffer (send_buffer, (size_t) send_buffer_fill);
2473 network_init_buffer ();
2474 }
2476 static int network_write (const data_set_t *ds, const value_list_t *vl,
2477 user_data_t __attribute__((unused)) *user_data)
2478 {
2479 int status;
2481 /* If the value is already in the cache, we have received it via the
2482 * network. We write it again if forwarding is activated. It's then in
2483 * the cache and should we receive it again we will ignore it. */
2484 status = cache_check (vl);
2485 if ((network_config_forward == 0)
2486 && (status != 0))
2487 return (0);
2489 pthread_mutex_lock (&send_buffer_lock);
2491 status = add_to_buffer (send_buffer_ptr,
2492 sizeof (send_buffer) - (send_buffer_fill + BUFF_SIG_SIZE),
2493 &send_buffer_vl,
2494 ds, vl);
2495 if (status >= 0)
2496 {
2497 /* status == bytes added to the buffer */
2498 send_buffer_fill += status;
2499 send_buffer_ptr += status;
2500 }
2501 else
2502 {
2503 flush_buffer ();
2505 status = add_to_buffer (send_buffer_ptr,
2506 sizeof (send_buffer) - (send_buffer_fill + BUFF_SIG_SIZE),
2507 &send_buffer_vl,
2508 ds, vl);
2510 if (status >= 0)
2511 {
2512 send_buffer_fill += status;
2513 send_buffer_ptr += status;
2514 }
2515 }
2517 if (status < 0)
2518 {
2519 ERROR ("network plugin: Unable to append to the "
2520 "buffer for some weird reason");
2521 }
2522 else if ((sizeof (send_buffer) - send_buffer_fill) < 15)
2523 {
2524 flush_buffer ();
2525 }
2527 pthread_mutex_unlock (&send_buffer_lock);
2529 return ((status < 0) ? -1 : 0);
2530 } /* int network_write */
2532 static int network_config_set_boolean (const oconfig_item_t *ci, /* {{{ */
2533 int *retval)
2534 {
2535 if ((ci->values_num != 1)
2536 || ((ci->values[0].type != OCONFIG_TYPE_BOOLEAN)
2537 && (ci->values[0].type != OCONFIG_TYPE_STRING)))
2538 {
2539 ERROR ("network plugin: The `%s' config option needs "
2540 "exactly one boolean argument.", ci->key);
2541 return (-1);
2542 }
2544 if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
2545 {
2546 if (ci->values[0].value.boolean)
2547 *retval = 1;
2548 else
2549 *retval = 0;
2550 }
2551 else
2552 {
2553 char *str = ci->values[0].value.string;
2555 if ((strcasecmp ("true", str) == 0)
2556 || (strcasecmp ("yes", str) == 0)
2557 || (strcasecmp ("on", str) == 0))
2558 *retval = 1;
2559 else if ((strcasecmp ("false", str) == 0)
2560 || (strcasecmp ("no", str) == 0)
2561 || (strcasecmp ("off", str) == 0))
2562 *retval = 0;
2563 else
2564 {
2565 ERROR ("network plugin: Cannot parse string value `%s' of the `%s' "
2566 "option as boolean value.",
2567 str, ci->key);
2568 return (-1);
2569 }
2570 }
2572 return (0);
2573 } /* }}} int network_config_set_boolean */
2575 static int network_config_set_ttl (const oconfig_item_t *ci) /* {{{ */
2576 {
2577 int tmp;
2578 if ((ci->values_num != 1)
2579 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
2580 {
2581 WARNING ("network plugin: The `TimeToLive' config option needs exactly "
2582 "one numeric argument.");
2583 return (-1);
2584 }
2586 tmp = (int) ci->values[0].value.number;
2587 if ((tmp > 0) && (tmp <= 255))
2588 network_config_ttl = tmp;
2590 return (0);
2591 } /* }}} int network_config_set_ttl */
2593 #if HAVE_LIBGCRYPT
2594 static int network_config_set_string (const oconfig_item_t *ci, /* {{{ */
2595 char **ret_string)
2596 {
2597 char *tmp;
2598 if ((ci->values_num != 1)
2599 || (ci->values[0].type != OCONFIG_TYPE_STRING))
2600 {
2601 WARNING ("network plugin: The `%s' config option needs exactly "
2602 "one string argument.", ci->key);
2603 return (-1);
2604 }
2606 tmp = strdup (ci->values[0].value.string);
2607 if (tmp == NULL)
2608 return (-1);
2610 sfree (*ret_string);
2611 *ret_string = tmp;
2613 return (0);
2614 } /* }}} int network_config_set_string */
2615 #endif /* HAVE_LIBGCRYPT */
2617 #if HAVE_LIBGCRYPT
2618 static int network_config_set_security_level (oconfig_item_t *ci, /* {{{ */
2619 int *retval)
2620 {
2621 char *str;
2622 if ((ci->values_num != 1)
2623 || (ci->values[0].type != OCONFIG_TYPE_STRING))
2624 {
2625 WARNING ("network plugin: The `SecurityLevel' config option needs exactly "
2626 "one string argument.");
2627 return (-1);
2628 }
2630 str = ci->values[0].value.string;
2631 if (strcasecmp ("Encrypt", str) == 0)
2632 *retval = SECURITY_LEVEL_ENCRYPT;
2633 else if (strcasecmp ("Sign", str) == 0)
2634 *retval = SECURITY_LEVEL_SIGN;
2635 else if (strcasecmp ("None", str) == 0)
2636 *retval = SECURITY_LEVEL_NONE;
2637 else
2638 {
2639 WARNING ("network plugin: Unknown security level: %s.", str);
2640 return (-1);
2641 }
2643 return (0);
2644 } /* }}} int network_config_set_security_level */
2645 #endif /* HAVE_LIBGCRYPT */
2647 static int network_config_add_listen (const oconfig_item_t *ci) /* {{{ */
2648 {
2649 sockent_t *se;
2650 int status;
2651 int i;
2653 if ((ci->values_num < 1) || (ci->values_num > 2)
2654 || (ci->values[0].type != OCONFIG_TYPE_STRING)
2655 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
2656 {
2657 ERROR ("network plugin: The `%s' config option needs "
2658 "one or two string arguments.", ci->key);
2659 return (-1);
2660 }
2662 se = malloc (sizeof (*se));
2663 if (se == NULL)
2664 {
2665 ERROR ("network plugin: malloc failed.");
2666 return (-1);
2667 }
2668 sockent_init (se, SOCKENT_TYPE_SERVER);
2670 se->node = strdup (ci->values[0].value.string);
2671 if (ci->values_num >= 2)
2672 se->service = strdup (ci->values[1].value.string);
2674 for (i = 0; i < ci->children_num; i++)
2675 {
2676 oconfig_item_t *child = ci->children + i;
2678 #if HAVE_LIBGCRYPT
2679 if (strcasecmp ("AuthFile", child->key) == 0)
2680 network_config_set_string (child, &se->data.server.auth_file);
2681 else if (strcasecmp ("SecurityLevel", child->key) == 0)
2682 network_config_set_security_level (child,
2683 &se->data.server.security_level);
2684 else
2685 #endif /* HAVE_LIBGCRYPT */
2686 {
2687 WARNING ("network plugin: Option `%s' is not allowed here.",
2688 child->key);
2689 }
2690 }
2692 #if HAVE_LIBGCRYPT
2693 if ((se->data.server.security_level > SECURITY_LEVEL_NONE)
2694 && (se->data.server.auth_file == NULL))
2695 {
2696 ERROR ("network plugin: A security level higher than `none' was "
2697 "requested, but no AuthFile option was given. Cowardly refusing to "
2698 "open this socket!");
2699 sockent_destroy (se);
2700 return (-1);
2701 }
2702 #endif /* HAVE_LIBGCRYPT */
2704 status = sockent_open (se);
2705 if (status != 0)
2706 {
2707 ERROR ("network plugin: network_config_add_listen: sockent_open failed.");
2708 sockent_destroy (se);
2709 return (-1);
2710 }
2712 status = sockent_add (se);
2713 if (status != 0)
2714 {
2715 ERROR ("network plugin: network_config_add_listen: sockent_add failed.");
2716 sockent_destroy (se);
2717 return (-1);
2718 }
2720 return (0);
2721 } /* }}} int network_config_add_listen */
2723 static int network_config_add_server (const oconfig_item_t *ci) /* {{{ */
2724 {
2725 sockent_t *se;
2726 int status;
2727 int i;
2729 if ((ci->values_num < 1) || (ci->values_num > 2)
2730 || (ci->values[0].type != OCONFIG_TYPE_STRING)
2731 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
2732 {
2733 ERROR ("network plugin: The `%s' config option needs "
2734 "one or two string arguments.", ci->key);
2735 return (-1);
2736 }
2738 se = malloc (sizeof (*se));
2739 if (se == NULL)
2740 {
2741 ERROR ("network plugin: malloc failed.");
2742 return (-1);
2743 }
2744 sockent_init (se, SOCKENT_TYPE_CLIENT);
2746 se->node = strdup (ci->values[0].value.string);
2747 if (ci->values_num >= 2)
2748 se->service = strdup (ci->values[1].value.string);
2750 for (i = 0; i < ci->children_num; i++)
2751 {
2752 oconfig_item_t *child = ci->children + i;
2754 #if HAVE_LIBGCRYPT
2755 if (strcasecmp ("Username", child->key) == 0)
2756 network_config_set_string (child, &se->data.client.username);
2757 else if (strcasecmp ("Password", child->key) == 0)
2758 network_config_set_string (child, &se->data.client.password);
2759 else if (strcasecmp ("SecurityLevel", child->key) == 0)
2760 network_config_set_security_level (child,
2761 &se->data.client.security_level);
2762 else
2763 #endif /* HAVE_LIBGCRYPT */
2764 {
2765 WARNING ("network plugin: Option `%s' is not allowed here.",
2766 child->key);
2767 }
2768 }
2770 #if HAVE_LIBGCRYPT
2771 if ((se->data.client.security_level > SECURITY_LEVEL_NONE)
2772 && ((se->data.client.username == NULL)
2773 || (se->data.client.password == NULL)))
2774 {
2775 ERROR ("network plugin: A security level higher than `none' was "
2776 "requested, but no Username or Password option was given. "
2777 "Cowardly refusing to open this socket!");
2778 sockent_destroy (se);
2779 return (-1);
2780 }
2781 #endif /* HAVE_LIBGCRYPT */
2783 status = sockent_open (se);
2784 if (status != 0)
2785 {
2786 ERROR ("network plugin: network_config_add_server: sockent_open failed.");
2787 sockent_destroy (se);
2788 return (-1);
2789 }
2791 status = sockent_add (se);
2792 if (status != 0)
2793 {
2794 ERROR ("network plugin: network_config_add_server: sockent_add failed.");
2795 sockent_destroy (se);
2796 return (-1);
2797 }
2799 return (0);
2800 } /* }}} int network_config_add_server */
2802 static int network_config_set_cache_flush (const oconfig_item_t *ci) /* {{{ */
2803 {
2804 int tmp;
2805 if ((ci->values_num != 1)
2806 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
2807 {
2808 WARNING ("network plugin: The `CacheFlush' config option needs exactly "
2809 "one numeric argument.");
2810 return (-1);
2811 }
2813 tmp = (int) ci->values[0].value.number;
2814 if (tmp > 0)
2815 network_config_ttl = tmp;
2817 return (0);
2818 } /* }}} int network_config_set_cache_flush */
2820 static int network_config (oconfig_item_t *ci) /* {{{ */
2821 {
2822 int i;
2824 for (i = 0; i < ci->children_num; i++)
2825 {
2826 oconfig_item_t *child = ci->children + i;
2828 if (strcasecmp ("Listen", child->key) == 0)
2829 network_config_add_listen (child);
2830 else if (strcasecmp ("Server", child->key) == 0)
2831 network_config_add_server (child);
2832 else if (strcasecmp ("TimeToLive", child->key) == 0)
2833 network_config_set_ttl (child);
2834 else if (strcasecmp ("Forward", child->key) == 0)
2835 network_config_set_boolean (child, &network_config_forward);
2836 else if (strcasecmp ("CacheFlush", child->key) == 0)
2837 network_config_set_cache_flush (child);
2838 else
2839 {
2840 WARNING ("network plugin: Option `%s' is not allowed here.",
2841 child->key);
2842 }
2843 }
2845 return (0);
2846 } /* }}} int network_config */
2848 static int network_notification (const notification_t *n,
2849 user_data_t __attribute__((unused)) *user_data)
2850 {
2851 char buffer[BUFF_SIZE];
2852 char *buffer_ptr = buffer;
2853 int buffer_free = sizeof (buffer);
2854 int status;
2856 memset (buffer, '\0', sizeof (buffer));
2859 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_TIME,
2860 (uint64_t) n->time);
2861 if (status != 0)
2862 return (-1);
2864 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_SEVERITY,
2865 (uint64_t) n->severity);
2866 if (status != 0)
2867 return (-1);
2869 if (strlen (n->host) > 0)
2870 {
2871 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_HOST,
2872 n->host, strlen (n->host));
2873 if (status != 0)
2874 return (-1);
2875 }
2877 if (strlen (n->plugin) > 0)
2878 {
2879 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_PLUGIN,
2880 n->plugin, strlen (n->plugin));
2881 if (status != 0)
2882 return (-1);
2883 }
2885 if (strlen (n->plugin_instance) > 0)
2886 {
2887 status = write_part_string (&buffer_ptr, &buffer_free,
2888 TYPE_PLUGIN_INSTANCE,
2889 n->plugin_instance, strlen (n->plugin_instance));
2890 if (status != 0)
2891 return (-1);
2892 }
2894 if (strlen (n->type) > 0)
2895 {
2896 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE,
2897 n->type, strlen (n->type));
2898 if (status != 0)
2899 return (-1);
2900 }
2902 if (strlen (n->type_instance) > 0)
2903 {
2904 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE_INSTANCE,
2905 n->type_instance, strlen (n->type_instance));
2906 if (status != 0)
2907 return (-1);
2908 }
2910 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_MESSAGE,
2911 n->message, strlen (n->message));
2912 if (status != 0)
2913 return (-1);
2915 network_send_buffer (buffer, sizeof (buffer) - buffer_free);
2917 return (0);
2918 } /* int network_notification */
2920 static int network_shutdown (void)
2921 {
2922 listen_loop++;
2924 /* Kill the listening thread */
2925 if (receive_thread_running != 0)
2926 {
2927 INFO ("network plugin: Stopping receive thread.");
2928 pthread_kill (receive_thread_id, SIGTERM);
2929 pthread_join (receive_thread_id, NULL /* no return value */);
2930 memset (&receive_thread_id, 0, sizeof (receive_thread_id));
2931 receive_thread_running = 0;
2932 }
2934 /* Shutdown the dispatching thread */
2935 if (dispatch_thread_running != 0)
2936 {
2937 INFO ("network plugin: Stopping dispatch thread.");
2938 pthread_mutex_lock (&receive_list_lock);
2939 pthread_cond_broadcast (&receive_list_cond);
2940 pthread_mutex_unlock (&receive_list_lock);
2941 pthread_join (dispatch_thread_id, /* ret = */ NULL);
2942 dispatch_thread_running = 0;
2943 }
2945 sockent_destroy (listen_sockets);
2947 if (send_buffer_fill > 0)
2948 flush_buffer ();
2950 if (cache_tree != NULL)
2951 {
2952 void *key;
2953 void *value;
2955 while (c_avl_pick (cache_tree, &key, &value) == 0)
2956 {
2957 sfree (key);
2958 sfree (value);
2959 }
2960 c_avl_destroy (cache_tree);
2961 cache_tree = NULL;
2962 }
2964 /* TODO: Close `sending_sockets' */
2966 plugin_unregister_config ("network");
2967 plugin_unregister_init ("network");
2968 plugin_unregister_write ("network");
2969 plugin_unregister_shutdown ("network");
2971 /* Let the init function do it's move again ;) */
2972 cache_flush_last = 0;
2974 return (0);
2975 } /* int network_shutdown */
2977 static int network_init (void)
2978 {
2979 /* Check if we were already initialized. If so, just return - there's
2980 * nothing more to do (for now, that is). */
2981 if (cache_flush_last != 0)
2982 return (0);
2984 plugin_register_shutdown ("network", network_shutdown);
2986 network_init_buffer ();
2988 cache_tree = c_avl_create ((int (*) (const void *, const void *)) strcmp);
2989 cache_flush_last = time (NULL);
2991 /* setup socket(s) and so on */
2992 if (sending_sockets != NULL)
2993 {
2994 plugin_register_write ("network", network_write,
2995 /* user_data = */ NULL);
2996 plugin_register_notification ("network", network_notification,
2997 /* user_data = */ NULL);
2998 }
3000 /* If no threads need to be started, return here. */
3001 if ((listen_sockets_num == 0)
3002 || ((dispatch_thread_running != 0)
3003 && (receive_thread_running != 0)))
3004 return (0);
3006 if (dispatch_thread_running == 0)
3007 {
3008 int status;
3009 status = pthread_create (&dispatch_thread_id,
3010 NULL /* no attributes */,
3011 dispatch_thread,
3012 NULL /* no argument */);
3013 if (status != 0)
3014 {
3015 char errbuf[1024];
3016 ERROR ("network: pthread_create failed: %s",
3017 sstrerror (errno, errbuf,
3018 sizeof (errbuf)));
3019 }
3020 else
3021 {
3022 dispatch_thread_running = 1;
3023 }
3024 }
3026 if (receive_thread_running == 0)
3027 {
3028 int status;
3029 status = pthread_create (&receive_thread_id,
3030 NULL /* no attributes */,
3031 receive_thread,
3032 NULL /* no argument */);
3033 if (status != 0)
3034 {
3035 char errbuf[1024];
3036 ERROR ("network: pthread_create failed: %s",
3037 sstrerror (errno, errbuf,
3038 sizeof (errbuf)));
3039 }
3040 else
3041 {
3042 receive_thread_running = 1;
3043 }
3044 }
3046 return (0);
3047 } /* int network_init */
3049 /*
3050 * The flush option of the network plugin cannot flush individual identifiers.
3051 * All the values are added to a buffer and sent when the buffer is full, the
3052 * requested value may or may not be in there, it's not worth finding out. We
3053 * just send the buffer if `flush' is called - if the requested value was in
3054 * there, good. If not, well, then there is nothing to flush.. -octo
3055 */
3056 static int network_flush (int timeout,
3057 const char __attribute__((unused)) *identifier,
3058 user_data_t __attribute__((unused)) *user_data)
3059 {
3060 pthread_mutex_lock (&send_buffer_lock);
3062 if (((time (NULL) - cache_flush_last) >= timeout)
3063 && (send_buffer_fill > 0))
3064 {
3065 flush_buffer ();
3066 }
3068 pthread_mutex_unlock (&send_buffer_lock);
3070 return (0);
3071 } /* int network_flush */
3073 void module_register (void)
3074 {
3075 plugin_register_complex_config ("network", network_config);
3076 plugin_register_init ("network", network_init);
3077 plugin_register_flush ("network", network_flush,
3078 /* user_data = */ NULL);
3079 } /* void module_register */
3081 /* vim: set fdm=marker : */