1 /**
2 * collectd - src/network.c
3 * Copyright (C) 2005-2009 Florian octo Forster
4 * Copyright (C) 2009 Aman Gupta
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; only version 2.1 of the License is
9 * applicable.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * Authors:
21 * Florian octo Forster <octo at verplant.org>
22 * Aman Gupta <aman at tmm1.net>
23 **/
25 #define _BSD_SOURCE /* For struct ip_mreq */
27 #include "collectd.h"
28 #include "plugin.h"
29 #include "common.h"
30 #include "configfile.h"
31 #include "utils_fbhash.h"
32 #include "utils_avltree.h"
33 #include "utils_cache.h"
35 #include "network.h"
37 #if HAVE_PTHREAD_H
38 # include <pthread.h>
39 #endif
40 #if HAVE_SYS_SOCKET_H
41 # include <sys/socket.h>
42 #endif
43 #if HAVE_NETDB_H
44 # include <netdb.h>
45 #endif
46 #if HAVE_NETINET_IN_H
47 # include <netinet/in.h>
48 #endif
49 #if HAVE_ARPA_INET_H
50 # include <arpa/inet.h>
51 #endif
52 #if HAVE_POLL_H
53 # include <poll.h>
54 #endif
56 #if HAVE_LIBGCRYPT
57 # include <gcrypt.h>
58 GCRY_THREAD_OPTION_PTHREAD_IMPL;
59 #endif
61 #ifndef IPV6_ADD_MEMBERSHIP
62 # ifdef IPV6_JOIN_GROUP
63 # define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
64 # else
65 # error "Neither IP_ADD_MEMBERSHIP nor IPV6_JOIN_GROUP is defined"
66 # endif
67 #endif /* !IP_ADD_MEMBERSHIP */
69 /*
70 * Maximum size required for encryption / signing:
71 *
72 * 42 bytes for the encryption header
73 * + 64 bytes for the username
74 * -----------
75 * = 106 bytes
76 */
77 #define BUFF_SIG_SIZE 106
79 /*
80 * Private data types
81 */
82 #define SECURITY_LEVEL_NONE 0
83 #if HAVE_LIBGCRYPT
84 # define SECURITY_LEVEL_SIGN 1
85 # define SECURITY_LEVEL_ENCRYPT 2
86 #endif
87 struct sockent_client
88 {
89 int fd;
90 struct sockaddr_storage *addr;
91 socklen_t addrlen;
92 #if HAVE_LIBGCRYPT
93 int security_level;
94 char *username;
95 char *password;
96 gcry_cipher_hd_t cypher;
97 unsigned char password_hash[32];
98 #endif
99 };
101 struct sockent_server
102 {
103 int *fd;
104 size_t fd_num;
105 #if HAVE_LIBGCRYPT
106 int security_level;
107 char *auth_file;
108 fbhash_t *userdb;
109 gcry_cipher_hd_t cypher;
110 #endif
111 };
113 typedef struct sockent
114 {
115 #define SOCKENT_TYPE_CLIENT 1
116 #define SOCKENT_TYPE_SERVER 2
117 int type;
119 char *node;
120 char *service;
122 union
123 {
124 struct sockent_client client;
125 struct sockent_server server;
126 } data;
128 struct sockent *next;
129 } sockent_t;
131 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
132 * 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
133 * +-------+-----------------------+-------------------------------+
134 * ! Ver. ! ! Length !
135 * +-------+-----------------------+-------------------------------+
136 */
137 struct part_header_s
138 {
139 uint16_t type;
140 uint16_t length;
141 };
142 typedef struct part_header_s part_header_t;
144 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
145 * 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
146 * +-------------------------------+-------------------------------+
147 * ! Type ! Length !
148 * +-------------------------------+-------------------------------+
149 * : (Length - 4) Bytes :
150 * +---------------------------------------------------------------+
151 */
152 struct part_string_s
153 {
154 part_header_t *head;
155 char *value;
156 };
157 typedef struct part_string_s part_string_t;
159 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
160 * 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
161 * +-------------------------------+-------------------------------+
162 * ! Type ! Length !
163 * +-------------------------------+-------------------------------+
164 * : (Length - 4 == 2 || 4 || 8) Bytes :
165 * +---------------------------------------------------------------+
166 */
167 struct part_number_s
168 {
169 part_header_t *head;
170 uint64_t *value;
171 };
172 typedef struct part_number_s part_number_t;
174 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
175 * 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
176 * +-------------------------------+-------------------------------+
177 * ! Type ! Length !
178 * +-------------------------------+---------------+---------------+
179 * ! Num of values ! Type0 ! Type1 !
180 * +-------------------------------+---------------+---------------+
181 * ! Value0 !
182 * ! !
183 * +---------------------------------------------------------------+
184 * ! Value1 !
185 * ! !
186 * +---------------------------------------------------------------+
187 */
188 struct part_values_s
189 {
190 part_header_t *head;
191 uint16_t *num_values;
192 uint8_t *values_types;
193 value_t *values;
194 };
195 typedef struct part_values_s part_values_t;
197 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
198 * 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
199 * +-------------------------------+-------------------------------+
200 * ! Type ! Length !
201 * +-------------------------------+-------------------------------+
202 * ! Hash (Bits 0 - 31) !
203 * : : :
204 * ! Hash (Bits 224 - 255) !
205 * +---------------------------------------------------------------+
206 */
207 /* Minimum size */
208 #define PART_SIGNATURE_SHA256_SIZE 36
209 struct part_signature_sha256_s
210 {
211 part_header_t head;
212 unsigned char hash[32];
213 char *username;
214 };
215 typedef struct part_signature_sha256_s part_signature_sha256_t;
217 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
218 * 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
219 * +-------------------------------+-------------------------------+
220 * ! Type ! Length !
221 * +-------------------------------+-------------------------------+
222 * ! Original length ! Padding (0 - 15 bytes) !
223 * +-------------------------------+-------------------------------+
224 * ! Hash (Bits 0 - 31) !
225 * : : :
226 * ! Hash (Bits 128 - 159) !
227 * +---------------------------------------------------------------+
228 */
229 /* Minimum size */
230 #define PART_ENCRYPTION_AES256_SIZE 42
231 struct part_encryption_aes256_s
232 {
233 part_header_t head;
234 uint16_t username_length;
235 char *username;
236 unsigned char iv[16];
237 /* <encrypted> */
238 unsigned char hash[20];
239 /* <payload /> */
240 /* </encrypted> */
241 };
242 typedef struct part_encryption_aes256_s part_encryption_aes256_t;
244 struct receive_list_entry_s
245 {
246 char *data;
247 int data_len;
248 int fd;
249 struct receive_list_entry_s *next;
250 };
251 typedef struct receive_list_entry_s receive_list_entry_t;
253 /*
254 * Private variables
255 */
256 static int network_config_ttl = 0;
257 static size_t network_config_packet_size = 1024;
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;
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 /*
287 * Private functions
288 */
289 static _Bool check_receive_okay (const value_list_t *vl) /* {{{ */
290 {
291 uint64_t time_sent = 0;
292 int status;
294 status = uc_meta_data_get_unsigned_int (vl,
295 "network:time_sent", &time_sent);
297 /* This is a value we already sent. Don't allow it to be received again in
298 * order to avoid looping. */
299 if ((status == 0) && (time_sent >= ((uint64_t) vl->time)))
300 return (false);
302 return (true);
303 } /* }}} _Bool check_receive_okay */
305 static _Bool check_send_okay (const value_list_t *vl) /* {{{ */
306 {
307 _Bool received = false;
308 int status;
310 if (network_config_forward != 0)
311 return (true);
313 if (vl->meta == NULL)
314 return (true);
316 status = meta_data_get_boolean (vl->meta, "network:received", &received);
317 if (status == -ENOENT)
318 return (true);
319 else if (status != 0)
320 {
321 ERROR ("network plugin: check_send_okay: meta_data_get_boolean failed "
322 "with status %i.", status);
323 return (true);
324 }
326 /* By default, only *send* value lists that were not *received* by the
327 * network plugin. */
328 return (!received);
329 } /* }}} _Bool check_send_okay */
331 static int network_dispatch_values (value_list_t *vl) /* {{{ */
332 {
333 int status;
335 if ((vl->time <= 0)
336 || (strlen (vl->host) <= 0)
337 || (strlen (vl->plugin) <= 0)
338 || (strlen (vl->type) <= 0))
339 return (-EINVAL);
341 if (!check_receive_okay (vl))
342 {
343 #if COLLECT_DEBUG
344 char name[6*DATA_MAX_NAME_LEN];
345 FORMAT_VL (name, sizeof (name), vl);
346 name[sizeof (name) - 1] = 0;
347 DEBUG ("network plugin: network_dispatch_values: "
348 "NOT dispatching %s.", name);
349 #endif
350 return (0);
351 }
353 assert (vl->meta == NULL);
355 vl->meta = meta_data_create ();
356 if (vl->meta == NULL)
357 {
358 ERROR ("network plugin: meta_data_create failed.");
359 return (-ENOMEM);
360 }
362 status = meta_data_add_boolean (vl->meta, "network:received", true);
363 if (status != 0)
364 {
365 ERROR ("network plugin: meta_data_add_boolean failed.");
366 meta_data_destroy (vl->meta);
367 vl->meta = NULL;
368 return (status);
369 }
371 plugin_dispatch_values (vl);
373 meta_data_destroy (vl->meta);
374 vl->meta = NULL;
376 return (0);
377 } /* }}} int network_dispatch_values */
379 #if HAVE_LIBGCRYPT
380 static gcry_cipher_hd_t network_get_aes256_cypher (sockent_t *se, /* {{{ */
381 const void *iv, size_t iv_size, const char *username)
382 {
383 gcry_error_t err;
384 gcry_cipher_hd_t *cyper_ptr;
385 unsigned char password_hash[32];
387 if (se->type == SOCKENT_TYPE_CLIENT)
388 {
389 cyper_ptr = &se->data.client.cypher;
390 memcpy (password_hash, se->data.client.password_hash,
391 sizeof (password_hash));
392 }
393 else
394 {
395 char *secret;
397 cyper_ptr = &se->data.server.cypher;
399 if (username == NULL)
400 return (NULL);
402 secret = fbh_get (se->data.server.userdb, username);
403 if (secret == NULL)
404 return (NULL);
406 gcry_md_hash_buffer (GCRY_MD_SHA256,
407 password_hash,
408 secret, strlen (secret));
410 sfree (secret);
411 }
413 if (*cyper_ptr == NULL)
414 {
415 err = gcry_cipher_open (cyper_ptr,
416 GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_OFB, /* flags = */ 0);
417 if (err != 0)
418 {
419 ERROR ("network plugin: gcry_cipher_open returned: %s",
420 gcry_strerror (err));
421 *cyper_ptr = NULL;
422 return (NULL);
423 }
424 }
425 else
426 {
427 gcry_cipher_reset (*cyper_ptr);
428 }
429 assert (*cyper_ptr != NULL);
431 err = gcry_cipher_setkey (*cyper_ptr,
432 password_hash, sizeof (password_hash));
433 if (err != 0)
434 {
435 ERROR ("network plugin: gcry_cipher_setkey returned: %s",
436 gcry_strerror (err));
437 gcry_cipher_close (*cyper_ptr);
438 *cyper_ptr = NULL;
439 return (NULL);
440 }
442 err = gcry_cipher_setiv (*cyper_ptr, iv, iv_size);
443 if (err != 0)
444 {
445 ERROR ("network plugin: gcry_cipher_setkey returned: %s",
446 gcry_strerror (err));
447 gcry_cipher_close (*cyper_ptr);
448 *cyper_ptr = NULL;
449 return (NULL);
450 }
452 return (*cyper_ptr);
453 } /* }}} int network_get_aes256_cypher */
454 #endif /* HAVE_LIBGCRYPT */
456 static int write_part_values (char **ret_buffer, int *ret_buffer_len,
457 const data_set_t *ds, const value_list_t *vl)
458 {
459 char *packet_ptr;
460 int packet_len;
461 int num_values;
463 part_header_t pkg_ph;
464 uint16_t pkg_num_values;
465 uint8_t *pkg_values_types;
466 value_t *pkg_values;
468 int offset;
469 int i;
471 num_values = vl->values_len;
472 packet_len = sizeof (part_header_t) + sizeof (uint16_t)
473 + (num_values * sizeof (uint8_t))
474 + (num_values * sizeof (value_t));
476 if (*ret_buffer_len < packet_len)
477 return (-1);
479 pkg_values_types = (uint8_t *) malloc (num_values * sizeof (uint8_t));
480 if (pkg_values_types == NULL)
481 {
482 ERROR ("network plugin: write_part_values: malloc failed.");
483 return (-1);
484 }
486 pkg_values = (value_t *) malloc (num_values * sizeof (value_t));
487 if (pkg_values == NULL)
488 {
489 free (pkg_values_types);
490 ERROR ("network plugin: write_part_values: malloc failed.");
491 return (-1);
492 }
494 pkg_ph.type = htons (TYPE_VALUES);
495 pkg_ph.length = htons (packet_len);
497 pkg_num_values = htons ((uint16_t) vl->values_len);
499 for (i = 0; i < num_values; i++)
500 {
501 pkg_values_types[i] = (uint8_t) ds->ds[i].type;
502 switch (ds->ds[i].type)
503 {
504 case DS_TYPE_COUNTER:
505 pkg_values[i].counter = htonll (vl->values[i].counter);
506 break;
508 case DS_TYPE_GAUGE:
509 pkg_values[i].gauge = htond (vl->values[i].gauge);
510 break;
512 case DS_TYPE_DERIVE:
513 pkg_values[i].derive = htonll (vl->values[i].derive);
514 break;
516 case DS_TYPE_ABSOLUTE:
517 pkg_values[i].absolute = htonll (vl->values[i].absolute);
518 break;
520 default:
521 free (pkg_values_types);
522 free (pkg_values);
523 ERROR ("network plugin: write_part_values: "
524 "Unknown data source type: %i",
525 ds->ds[i].type);
526 return (-1);
527 } /* switch (ds->ds[i].type) */
528 } /* for (num_values) */
530 /*
531 * Use `memcpy' to write everything to the buffer, because the pointer
532 * may be unaligned and some architectures, such as SPARC, can't handle
533 * that.
534 */
535 packet_ptr = *ret_buffer;
536 offset = 0;
537 memcpy (packet_ptr + offset, &pkg_ph, sizeof (pkg_ph));
538 offset += sizeof (pkg_ph);
539 memcpy (packet_ptr + offset, &pkg_num_values, sizeof (pkg_num_values));
540 offset += sizeof (pkg_num_values);
541 memcpy (packet_ptr + offset, pkg_values_types, num_values * sizeof (uint8_t));
542 offset += num_values * sizeof (uint8_t);
543 memcpy (packet_ptr + offset, pkg_values, num_values * sizeof (value_t));
544 offset += num_values * sizeof (value_t);
546 assert (offset == packet_len);
548 *ret_buffer = packet_ptr + packet_len;
549 *ret_buffer_len -= packet_len;
551 free (pkg_values_types);
552 free (pkg_values);
554 return (0);
555 } /* int write_part_values */
557 static int write_part_number (char **ret_buffer, int *ret_buffer_len,
558 int type, uint64_t value)
559 {
560 char *packet_ptr;
561 int packet_len;
563 part_header_t pkg_head;
564 uint64_t pkg_value;
566 int offset;
568 packet_len = sizeof (pkg_head) + sizeof (pkg_value);
570 if (*ret_buffer_len < packet_len)
571 return (-1);
573 pkg_head.type = htons (type);
574 pkg_head.length = htons (packet_len);
575 pkg_value = htonll (value);
577 packet_ptr = *ret_buffer;
578 offset = 0;
579 memcpy (packet_ptr + offset, &pkg_head, sizeof (pkg_head));
580 offset += sizeof (pkg_head);
581 memcpy (packet_ptr + offset, &pkg_value, sizeof (pkg_value));
582 offset += sizeof (pkg_value);
584 assert (offset == packet_len);
586 *ret_buffer = packet_ptr + packet_len;
587 *ret_buffer_len -= packet_len;
589 return (0);
590 } /* int write_part_number */
592 static int write_part_string (char **ret_buffer, int *ret_buffer_len,
593 int type, const char *str, int str_len)
594 {
595 char *buffer;
596 int buffer_len;
598 uint16_t pkg_type;
599 uint16_t pkg_length;
601 int offset;
603 buffer_len = 2 * sizeof (uint16_t) + str_len + 1;
604 if (*ret_buffer_len < buffer_len)
605 return (-1);
607 pkg_type = htons (type);
608 pkg_length = htons (buffer_len);
610 buffer = *ret_buffer;
611 offset = 0;
612 memcpy (buffer + offset, (void *) &pkg_type, sizeof (pkg_type));
613 offset += sizeof (pkg_type);
614 memcpy (buffer + offset, (void *) &pkg_length, sizeof (pkg_length));
615 offset += sizeof (pkg_length);
616 memcpy (buffer + offset, str, str_len);
617 offset += str_len;
618 memset (buffer + offset, '\0', 1);
619 offset += 1;
621 assert (offset == buffer_len);
623 *ret_buffer = buffer + buffer_len;
624 *ret_buffer_len -= buffer_len;
626 return (0);
627 } /* int write_part_string */
629 static int parse_part_values (void **ret_buffer, size_t *ret_buffer_len,
630 value_t **ret_values, int *ret_num_values)
631 {
632 char *buffer = *ret_buffer;
633 size_t buffer_len = *ret_buffer_len;
635 uint16_t tmp16;
636 size_t exp_size;
637 int i;
639 uint16_t pkg_length;
640 uint16_t pkg_type;
641 uint16_t pkg_numval;
643 uint8_t *pkg_types;
644 value_t *pkg_values;
646 if (buffer_len < 15)
647 {
648 NOTICE ("network plugin: packet is too short: "
649 "buffer_len = %zu", 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 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
662 buffer += sizeof (tmp16);
663 pkg_numval = ntohs (tmp16);
665 assert (pkg_type == TYPE_VALUES);
667 exp_size = 3 * sizeof (uint16_t)
668 + pkg_numval * (sizeof (uint8_t) + sizeof (value_t));
669 if ((buffer_len < 0) || (buffer_len < exp_size))
670 {
671 WARNING ("network plugin: parse_part_values: "
672 "Packet too short: "
673 "Chunk of size %zu expected, "
674 "but buffer has only %zu bytes left.",
675 exp_size, buffer_len);
676 return (-1);
677 }
679 if (pkg_length != exp_size)
680 {
681 WARNING ("network plugin: parse_part_values: "
682 "Length and number of values "
683 "in the packet don't match.");
684 return (-1);
685 }
687 pkg_types = (uint8_t *) malloc (pkg_numval * sizeof (uint8_t));
688 pkg_values = (value_t *) malloc (pkg_numval * sizeof (value_t));
689 if ((pkg_types == NULL) || (pkg_values == NULL))
690 {
691 sfree (pkg_types);
692 sfree (pkg_values);
693 ERROR ("network plugin: parse_part_values: malloc failed.");
694 return (-1);
695 }
697 memcpy ((void *) pkg_types, (void *) buffer, pkg_numval * sizeof (uint8_t));
698 buffer += pkg_numval * sizeof (uint8_t);
699 memcpy ((void *) pkg_values, (void *) buffer, pkg_numval * sizeof (value_t));
700 buffer += pkg_numval * sizeof (value_t);
702 for (i = 0; i < pkg_numval; i++)
703 {
704 switch (pkg_types[i])
705 {
706 case DS_TYPE_COUNTER:
707 pkg_values[i].counter = (counter_t) ntohll (pkg_values[i].counter);
708 break;
710 case DS_TYPE_GAUGE:
711 pkg_values[i].gauge = (gauge_t) ntohd (pkg_values[i].gauge);
712 break;
714 case DS_TYPE_DERIVE:
715 pkg_values[i].derive = (derive_t) ntohll (pkg_values[i].derive);
716 break;
718 case DS_TYPE_ABSOLUTE:
719 pkg_values[i].absolute = (absolute_t) ntohll (pkg_values[i].absolute);
720 break;
722 default:
723 NOTICE ("network plugin: parse_part_values: "
724 "Don't know how to handle data source type %"PRIu8,
725 pkg_types[i]);
726 sfree (pkg_types);
727 sfree (pkg_values);
728 return (-1);
729 } /* switch (pkg_types[i]) */
730 }
732 *ret_buffer = buffer;
733 *ret_buffer_len = buffer_len - pkg_length;
734 *ret_num_values = pkg_numval;
735 *ret_values = pkg_values;
737 sfree (pkg_types);
739 return (0);
740 } /* int parse_part_values */
742 static int parse_part_number (void **ret_buffer, size_t *ret_buffer_len,
743 uint64_t *value)
744 {
745 char *buffer = *ret_buffer;
746 size_t buffer_len = *ret_buffer_len;
748 uint16_t tmp16;
749 uint64_t tmp64;
750 size_t exp_size = 2 * sizeof (uint16_t) + sizeof (uint64_t);
752 uint16_t pkg_length;
753 uint16_t pkg_type;
755 if ((buffer_len < 0) || ((size_t) buffer_len < exp_size))
756 {
757 WARNING ("network plugin: parse_part_number: "
758 "Packet too short: "
759 "Chunk of size %zu expected, "
760 "but buffer has only %zu bytes left.",
761 exp_size, buffer_len);
762 return (-1);
763 }
765 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
766 buffer += sizeof (tmp16);
767 pkg_type = ntohs (tmp16);
769 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
770 buffer += sizeof (tmp16);
771 pkg_length = ntohs (tmp16);
773 memcpy ((void *) &tmp64, buffer, sizeof (tmp64));
774 buffer += sizeof (tmp64);
775 *value = ntohll (tmp64);
777 *ret_buffer = buffer;
778 *ret_buffer_len = buffer_len - pkg_length;
780 return (0);
781 } /* int parse_part_number */
783 static int parse_part_string (void **ret_buffer, size_t *ret_buffer_len,
784 char *output, int output_len)
785 {
786 char *buffer = *ret_buffer;
787 size_t buffer_len = *ret_buffer_len;
789 uint16_t tmp16;
790 size_t header_size = 2 * sizeof (uint16_t);
792 uint16_t pkg_length;
793 uint16_t pkg_type;
795 if ((buffer_len < 0) || (buffer_len < header_size))
796 {
797 WARNING ("network plugin: parse_part_string: "
798 "Packet too short: "
799 "Chunk of at least size %zu expected, "
800 "but buffer has only %zu bytes left.",
801 header_size, buffer_len);
802 return (-1);
803 }
805 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
806 buffer += sizeof (tmp16);
807 pkg_type = ntohs (tmp16);
809 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
810 buffer += sizeof (tmp16);
811 pkg_length = ntohs (tmp16);
813 /* Check that packet fits in the input buffer */
814 if (pkg_length > buffer_len)
815 {
816 WARNING ("network plugin: parse_part_string: "
817 "Packet too big: "
818 "Chunk of size %"PRIu16" received, "
819 "but buffer has only %zu bytes left.",
820 pkg_length, buffer_len);
821 return (-1);
822 }
824 /* Check that pkg_length is in the valid range */
825 if (pkg_length <= header_size)
826 {
827 WARNING ("network plugin: parse_part_string: "
828 "Packet too short: "
829 "Header claims this packet is only %hu "
830 "bytes long.", pkg_length);
831 return (-1);
832 }
834 /* Check that the package data fits into the output buffer.
835 * The previous if-statement ensures that:
836 * `pkg_length > header_size' */
837 if ((output_len < 0)
838 || ((size_t) output_len < ((size_t) pkg_length - header_size)))
839 {
840 WARNING ("network plugin: parse_part_string: "
841 "Output buffer too small.");
842 return (-1);
843 }
845 /* All sanity checks successfull, let's copy the data over */
846 output_len = pkg_length - header_size;
847 memcpy ((void *) output, (void *) buffer, output_len);
848 buffer += output_len;
850 /* For some very weird reason '\0' doesn't do the trick on SPARC in
851 * this statement. */
852 if (output[output_len - 1] != 0)
853 {
854 WARNING ("network plugin: parse_part_string: "
855 "Received string does not end "
856 "with a NULL-byte.");
857 return (-1);
858 }
860 *ret_buffer = buffer;
861 *ret_buffer_len = buffer_len - pkg_length;
863 return (0);
864 } /* int parse_part_string */
866 /* Forward declaration: parse_part_sign_sha256 and parse_part_encr_aes256 call
867 * parse_packet and vice versa. */
868 #define PP_SIGNED 0x01
869 #define PP_ENCRYPTED 0x02
870 static int parse_packet (sockent_t *se,
871 void *buffer, size_t buffer_size, int flags);
873 #define BUFFER_READ(p,s) do { \
874 memcpy ((p), buffer + buffer_offset, (s)); \
875 buffer_offset += (s); \
876 } while (0)
878 #if HAVE_LIBGCRYPT
879 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
880 void **ret_buffer, size_t *ret_buffer_len, int flags)
881 {
882 char *buffer;
883 size_t buffer_len;
884 size_t buffer_offset;
886 size_t username_len;
887 char *secret;
889 part_signature_sha256_t pss;
890 uint16_t pss_head_length;
891 char hash[sizeof (pss.hash)];
893 gcry_md_hd_t hd;
894 gcry_error_t err;
895 unsigned char *hash_ptr;
897 buffer = *ret_buffer;
898 buffer_len = *ret_buffer_len;
899 buffer_offset = 0;
901 if (se->data.server.userdb == NULL)
902 {
903 NOTICE ("network plugin: Received signed network packet but can't verify "
904 "it because no user DB has been configured. Will accept it.");
905 return (0);
906 }
908 /* Check if the buffer has enough data for this structure. */
909 if (buffer_len <= PART_SIGNATURE_SHA256_SIZE)
910 return (-ENOMEM);
912 /* Read type and length header */
913 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
914 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
915 pss_head_length = ntohs (pss.head.length);
917 /* Check if the `pss_head_length' is within bounds. */
918 if ((pss_head_length <= PART_SIGNATURE_SHA256_SIZE)
919 || (pss_head_length > buffer_len))
920 {
921 ERROR ("network plugin: HMAC-SHA-256 with invalid length received.");
922 return (-1);
923 }
925 /* Copy the hash. */
926 BUFFER_READ (pss.hash, sizeof (pss.hash));
928 /* Calculate username length (without null byte) and allocate memory */
929 username_len = pss_head_length - PART_SIGNATURE_SHA256_SIZE;
930 pss.username = malloc (username_len + 1);
931 if (pss.username == NULL)
932 return (-ENOMEM);
934 /* Read the username */
935 BUFFER_READ (pss.username, username_len);
936 pss.username[username_len] = 0;
938 assert (buffer_offset == pss_head_length);
940 /* Query the password */
941 secret = fbh_get (se->data.server.userdb, pss.username);
942 if (secret == NULL)
943 {
944 ERROR ("network plugin: Unknown user: %s", pss.username);
945 sfree (pss.username);
946 return (-ENOENT);
947 }
949 /* Create a hash device and check the HMAC */
950 hd = NULL;
951 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
952 if (err != 0)
953 {
954 ERROR ("network plugin: Creating HMAC-SHA-256 object failed: %s",
955 gcry_strerror (err));
956 sfree (secret);
957 sfree (pss.username);
958 return (-1);
959 }
961 err = gcry_md_setkey (hd, secret, strlen (secret));
962 if (err != 0)
963 {
964 ERROR ("network plugin: gcry_md_setkey failed: %s", gcry_strerror (err));
965 gcry_md_close (hd);
966 return (-1);
967 }
969 gcry_md_write (hd,
970 buffer + PART_SIGNATURE_SHA256_SIZE,
971 buffer_len - PART_SIGNATURE_SHA256_SIZE);
972 hash_ptr = gcry_md_read (hd, GCRY_MD_SHA256);
973 if (hash_ptr == NULL)
974 {
975 ERROR ("network plugin: gcry_md_read failed.");
976 gcry_md_close (hd);
977 sfree (secret);
978 sfree (pss.username);
979 return (-1);
980 }
981 memcpy (hash, hash_ptr, sizeof (hash));
983 /* Clean up */
984 gcry_md_close (hd);
985 hd = NULL;
987 sfree (secret);
988 sfree (pss.username);
990 if (memcmp (pss.hash, hash, sizeof (pss.hash)) != 0)
991 {
992 WARNING ("network plugin: Verifying HMAC-SHA-256 signature failed: "
993 "Hash mismatch.");
994 }
995 else
996 {
997 parse_packet (se, buffer + buffer_offset, buffer_len - buffer_offset,
998 flags | PP_SIGNED);
999 }
1001 *ret_buffer = buffer + buffer_len;
1002 *ret_buffer_len = 0;
1004 return (0);
1005 } /* }}} int parse_part_sign_sha256 */
1006 /* #endif HAVE_LIBGCRYPT */
1008 #else /* if !HAVE_LIBGCRYPT */
1009 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
1010 void **ret_buffer, size_t *ret_buffer_size, int flags)
1011 {
1012 static int warning_has_been_printed = 0;
1014 char *buffer;
1015 size_t buffer_size;
1016 size_t buffer_offset;
1017 uint16_t part_len;
1019 part_signature_sha256_t pss;
1021 buffer = *ret_buffer;
1022 buffer_size = *ret_buffer_size;
1023 buffer_offset = 0;
1025 if (buffer_size <= PART_SIGNATURE_SHA256_SIZE)
1026 return (-ENOMEM);
1028 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
1029 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
1030 part_len = ntohs (pss.head.length);
1032 if ((part_len <= PART_SIGNATURE_SHA256_SIZE)
1033 || (part_len > buffer_size))
1034 return (-EINVAL);
1036 if (warning_has_been_printed == 0)
1037 {
1038 WARNING ("network plugin: Received signed packet, but the network "
1039 "plugin was not linked with libgcrypt, so I cannot "
1040 "verify the signature. The packet will be accepted.");
1041 warning_has_been_printed = 1;
1042 }
1044 parse_packet (se, buffer + part_len, buffer_size - part_len, flags);
1046 *ret_buffer = buffer + buffer_size;
1047 *ret_buffer_size = 0;
1049 return (0);
1050 } /* }}} int parse_part_sign_sha256 */
1051 #endif /* !HAVE_LIBGCRYPT */
1053 #if HAVE_LIBGCRYPT
1054 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1055 void **ret_buffer, size_t *ret_buffer_len,
1056 int flags)
1057 {
1058 char *buffer = *ret_buffer;
1059 size_t buffer_len = *ret_buffer_len;
1060 size_t payload_len;
1061 size_t part_size;
1062 size_t buffer_offset;
1063 uint16_t username_len;
1064 part_encryption_aes256_t pea;
1065 unsigned char hash[sizeof (pea.hash)];
1067 gcry_cipher_hd_t cypher;
1068 gcry_error_t err;
1070 /* Make sure at least the header if available. */
1071 if (buffer_len <= PART_ENCRYPTION_AES256_SIZE)
1072 {
1073 NOTICE ("network plugin: parse_part_encr_aes256: "
1074 "Discarding short packet.");
1075 return (-1);
1076 }
1078 buffer_offset = 0;
1080 /* Copy the unencrypted information into `pea'. */
1081 BUFFER_READ (&pea.head.type, sizeof (pea.head.type));
1082 BUFFER_READ (&pea.head.length, sizeof (pea.head.length));
1084 /* Check the `part size'. */
1085 part_size = ntohs (pea.head.length);
1086 if ((part_size <= PART_ENCRYPTION_AES256_SIZE)
1087 || (part_size > buffer_len))
1088 {
1089 NOTICE ("network plugin: parse_part_encr_aes256: "
1090 "Discarding part with invalid size.");
1091 return (-1);
1092 }
1094 /* Read the username */
1095 BUFFER_READ (&username_len, sizeof (username_len));
1096 username_len = ntohs (username_len);
1098 if ((username_len <= 0)
1099 || (username_len > (part_size - (PART_ENCRYPTION_AES256_SIZE + 1))))
1100 {
1101 NOTICE ("network plugin: parse_part_encr_aes256: "
1102 "Discarding part with invalid username length.");
1103 return (-1);
1104 }
1106 assert (username_len > 0);
1107 pea.username = malloc (username_len + 1);
1108 if (pea.username == NULL)
1109 return (-ENOMEM);
1110 BUFFER_READ (pea.username, username_len);
1111 pea.username[username_len] = 0;
1113 /* Last but not least, the initialization vector */
1114 BUFFER_READ (pea.iv, sizeof (pea.iv));
1116 /* Make sure we are at the right position */
1117 assert (buffer_offset == (username_len +
1118 PART_ENCRYPTION_AES256_SIZE - sizeof (pea.hash)));
1120 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
1121 pea.username);
1122 if (cypher == NULL)
1123 {
1124 sfree (pea.username);
1125 return (-1);
1126 }
1128 payload_len = part_size - (PART_ENCRYPTION_AES256_SIZE + username_len);
1129 assert (payload_len > 0);
1131 /* Decrypt the packet in-place */
1132 err = gcry_cipher_decrypt (cypher,
1133 buffer + buffer_offset,
1134 part_size - buffer_offset,
1135 /* in = */ NULL, /* in len = */ 0);
1136 if (err != 0)
1137 {
1138 sfree (pea.username);
1139 ERROR ("network plugin: gcry_cipher_decrypt returned: %s",
1140 gcry_strerror (err));
1141 return (-1);
1142 }
1144 /* Read the hash */
1145 BUFFER_READ (pea.hash, sizeof (pea.hash));
1147 /* Make sure we're at the right position - again */
1148 assert (buffer_offset == (username_len + PART_ENCRYPTION_AES256_SIZE));
1149 assert (buffer_offset == (part_size - payload_len));
1151 /* Check hash sum */
1152 memset (hash, 0, sizeof (hash));
1153 gcry_md_hash_buffer (GCRY_MD_SHA1, hash,
1154 buffer + buffer_offset, payload_len);
1155 if (memcmp (hash, pea.hash, sizeof (hash)) != 0)
1156 {
1157 sfree (pea.username);
1158 ERROR ("network plugin: Decryption failed: Checksum mismatch.");
1159 return (-1);
1160 }
1162 parse_packet (se, buffer + buffer_offset, payload_len,
1163 flags | PP_ENCRYPTED);
1165 /* Update return values */
1166 *ret_buffer = buffer + part_size;
1167 *ret_buffer_len = buffer_len - part_size;
1169 sfree (pea.username);
1171 return (0);
1172 } /* }}} int parse_part_encr_aes256 */
1173 /* #endif HAVE_LIBGCRYPT */
1175 #else /* if !HAVE_LIBGCRYPT */
1176 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1177 void **ret_buffer, size_t *ret_buffer_size, int flags)
1178 {
1179 static int warning_has_been_printed = 0;
1181 char *buffer;
1182 size_t buffer_size;
1183 size_t buffer_offset;
1185 part_header_t ph;
1186 size_t ph_length;
1188 buffer = *ret_buffer;
1189 buffer_size = *ret_buffer_size;
1190 buffer_offset = 0;
1192 /* parse_packet assures this minimum size. */
1193 assert (buffer_size >= (sizeof (ph.type) + sizeof (ph.length)));
1195 BUFFER_READ (&ph.type, sizeof (ph.type));
1196 BUFFER_READ (&ph.length, sizeof (ph.length));
1197 ph_length = ntohs (ph.length);
1199 if ((ph_length <= PART_ENCRYPTION_AES256_SIZE)
1200 || (ph_length > buffer_size))
1201 {
1202 ERROR ("network plugin: AES-256 encrypted part "
1203 "with invalid length received.");
1204 return (-1);
1205 }
1207 if (warning_has_been_printed == 0)
1208 {
1209 WARNING ("network plugin: Received encrypted packet, but the network "
1210 "plugin was not linked with libgcrypt, so I cannot "
1211 "decrypt it. The part will be discarded.");
1212 warning_has_been_printed = 1;
1213 }
1215 *ret_buffer += ph_length;
1216 *ret_buffer_size -= ph_length;
1218 return (0);
1219 } /* }}} int parse_part_encr_aes256 */
1220 #endif /* !HAVE_LIBGCRYPT */
1222 #undef BUFFER_READ
1224 static int parse_packet (sockent_t *se, /* {{{ */
1225 void *buffer, size_t buffer_size, int flags)
1226 {
1227 int status;
1229 value_list_t vl = VALUE_LIST_INIT;
1230 notification_t n;
1232 #if HAVE_LIBGCRYPT
1233 int packet_was_signed = (flags & PP_SIGNED);
1234 int packet_was_encrypted = (flags & PP_ENCRYPTED);
1235 int printed_ignore_warning = 0;
1236 #endif /* HAVE_LIBGCRYPT */
1239 memset (&vl, '\0', sizeof (vl));
1240 memset (&n, '\0', sizeof (n));
1241 status = 0;
1243 while ((status == 0) && (0 < buffer_size)
1244 && ((unsigned int) buffer_size > sizeof (part_header_t)))
1245 {
1246 uint16_t pkg_length;
1247 uint16_t pkg_type;
1249 memcpy ((void *) &pkg_type,
1250 (void *) buffer,
1251 sizeof (pkg_type));
1252 memcpy ((void *) &pkg_length,
1253 (void *) (buffer + sizeof (pkg_type)),
1254 sizeof (pkg_length));
1256 pkg_length = ntohs (pkg_length);
1257 pkg_type = ntohs (pkg_type);
1259 if (pkg_length > buffer_size)
1260 break;
1261 /* Ensure that this loop terminates eventually */
1262 if (pkg_length < (2 * sizeof (uint16_t)))
1263 break;
1265 if (pkg_type == TYPE_ENCR_AES256)
1266 {
1267 status = parse_part_encr_aes256 (se,
1268 &buffer, &buffer_size, flags);
1269 if (status != 0)
1270 {
1271 ERROR ("network plugin: Decrypting AES256 "
1272 "part failed "
1273 "with status %i.", status);
1274 break;
1275 }
1276 }
1277 #if HAVE_LIBGCRYPT
1278 else if ((se->data.server.security_level == SECURITY_LEVEL_ENCRYPT)
1279 && (packet_was_encrypted == 0))
1280 {
1281 if (printed_ignore_warning == 0)
1282 {
1283 INFO ("network plugin: Unencrypted packet or "
1284 "part has been ignored.");
1285 printed_ignore_warning = 1;
1286 }
1287 buffer = ((char *) buffer) + pkg_length;
1288 continue;
1289 }
1290 #endif /* HAVE_LIBGCRYPT */
1291 else if (pkg_type == TYPE_SIGN_SHA256)
1292 {
1293 status = parse_part_sign_sha256 (se,
1294 &buffer, &buffer_size, flags);
1295 if (status != 0)
1296 {
1297 ERROR ("network plugin: Verifying HMAC-SHA-256 "
1298 "signature failed "
1299 "with status %i.", status);
1300 break;
1301 }
1302 }
1303 #if HAVE_LIBGCRYPT
1304 else if ((se->data.server.security_level == SECURITY_LEVEL_SIGN)
1305 && (packet_was_encrypted == 0)
1306 && (packet_was_signed == 0))
1307 {
1308 if (printed_ignore_warning == 0)
1309 {
1310 INFO ("network plugin: Unsigned 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_VALUES)
1319 {
1320 status = parse_part_values (&buffer, &buffer_size,
1321 &vl.values, &vl.values_len);
1322 if (status != 0)
1323 break;
1325 network_dispatch_values (&vl);
1327 sfree (vl.values);
1328 }
1329 else if (pkg_type == TYPE_TIME)
1330 {
1331 uint64_t tmp = 0;
1332 status = parse_part_number (&buffer, &buffer_size,
1333 &tmp);
1334 if (status == 0)
1335 {
1336 vl.time = (time_t) tmp;
1337 n.time = (time_t) tmp;
1338 }
1339 }
1340 else if (pkg_type == TYPE_INTERVAL)
1341 {
1342 uint64_t tmp = 0;
1343 status = parse_part_number (&buffer, &buffer_size,
1344 &tmp);
1345 if (status == 0)
1346 vl.interval = (int) tmp;
1347 }
1348 else if (pkg_type == TYPE_HOST)
1349 {
1350 status = parse_part_string (&buffer, &buffer_size,
1351 vl.host, sizeof (vl.host));
1352 if (status == 0)
1353 sstrncpy (n.host, vl.host, sizeof (n.host));
1354 }
1355 else if (pkg_type == TYPE_PLUGIN)
1356 {
1357 status = parse_part_string (&buffer, &buffer_size,
1358 vl.plugin, sizeof (vl.plugin));
1359 if (status == 0)
1360 sstrncpy (n.plugin, vl.plugin,
1361 sizeof (n.plugin));
1362 }
1363 else if (pkg_type == TYPE_PLUGIN_INSTANCE)
1364 {
1365 status = parse_part_string (&buffer, &buffer_size,
1366 vl.plugin_instance,
1367 sizeof (vl.plugin_instance));
1368 if (status == 0)
1369 sstrncpy (n.plugin_instance,
1370 vl.plugin_instance,
1371 sizeof (n.plugin_instance));
1372 }
1373 else if (pkg_type == TYPE_TYPE)
1374 {
1375 status = parse_part_string (&buffer, &buffer_size,
1376 vl.type, sizeof (vl.type));
1377 if (status == 0)
1378 sstrncpy (n.type, vl.type, sizeof (n.type));
1379 }
1380 else if (pkg_type == TYPE_TYPE_INSTANCE)
1381 {
1382 status = parse_part_string (&buffer, &buffer_size,
1383 vl.type_instance,
1384 sizeof (vl.type_instance));
1385 if (status == 0)
1386 sstrncpy (n.type_instance, vl.type_instance,
1387 sizeof (n.type_instance));
1388 }
1389 else if (pkg_type == TYPE_MESSAGE)
1390 {
1391 status = parse_part_string (&buffer, &buffer_size,
1392 n.message, sizeof (n.message));
1394 if (status != 0)
1395 {
1396 /* do nothing */
1397 }
1398 else if ((n.severity != NOTIF_FAILURE)
1399 && (n.severity != NOTIF_WARNING)
1400 && (n.severity != NOTIF_OKAY))
1401 {
1402 INFO ("network plugin: "
1403 "Ignoring notification with "
1404 "unknown severity %i.",
1405 n.severity);
1406 }
1407 else if (n.time <= 0)
1408 {
1409 INFO ("network plugin: "
1410 "Ignoring notification with "
1411 "time == 0.");
1412 }
1413 else if (strlen (n.message) <= 0)
1414 {
1415 INFO ("network plugin: "
1416 "Ignoring notification with "
1417 "an empty message.");
1418 }
1419 else
1420 {
1421 plugin_dispatch_notification (&n);
1422 }
1423 }
1424 else if (pkg_type == TYPE_SEVERITY)
1425 {
1426 uint64_t tmp = 0;
1427 status = parse_part_number (&buffer, &buffer_size,
1428 &tmp);
1429 if (status == 0)
1430 n.severity = (int) tmp;
1431 }
1432 else
1433 {
1434 DEBUG ("network plugin: parse_packet: Unknown part"
1435 " type: 0x%04hx", pkg_type);
1436 buffer = ((char *) buffer) + pkg_length;
1437 }
1438 } /* while (buffer_size > sizeof (part_header_t)) */
1440 if (status == 0 && buffer_size > 0)
1441 WARNING ("network plugin: parse_packet: Received truncated "
1442 "packet, try increasing `MaxPacketSize'");
1444 return (status);
1445 } /* }}} int parse_packet */
1447 static void free_sockent_client (struct sockent_client *sec) /* {{{ */
1448 {
1449 if (sec->fd >= 0)
1450 {
1451 close (sec->fd);
1452 sec->fd = -1;
1453 }
1454 sfree (sec->addr);
1455 #if HAVE_LIBGCRYPT
1456 sfree (sec->username);
1457 sfree (sec->password);
1458 if (sec->cypher != NULL)
1459 gcry_cipher_close (sec->cypher);
1460 #endif
1461 } /* }}} void free_sockent_client */
1463 static void free_sockent_server (struct sockent_server *ses) /* {{{ */
1464 {
1465 size_t i;
1467 for (i = 0; i < ses->fd_num; i++)
1468 {
1469 if (ses->fd[i] >= 0)
1470 {
1471 close (ses->fd[i]);
1472 ses->fd[i] = -1;
1473 }
1474 }
1476 sfree (ses->fd);
1477 #if HAVE_LIBGCRYPT
1478 sfree (ses->auth_file);
1479 fbh_destroy (ses->userdb);
1480 if (ses->cypher != NULL)
1481 gcry_cipher_close (ses->cypher);
1482 #endif
1483 } /* }}} void free_sockent_server */
1485 static void sockent_destroy (sockent_t *se) /* {{{ */
1486 {
1487 sockent_t *next;
1489 DEBUG ("network plugin: sockent_destroy (se = %p);", (void *) se);
1491 while (se != NULL)
1492 {
1493 next = se->next;
1495 sfree (se->node);
1496 sfree (se->service);
1498 if (se->type == SOCKENT_TYPE_CLIENT)
1499 free_sockent_client (&se->data.client);
1500 else
1501 free_sockent_server (&se->data.server);
1503 sfree (se);
1504 se = next;
1505 }
1506 } /* }}} void sockent_destroy */
1508 /*
1509 * int network_set_ttl
1510 *
1511 * Set the `IP_MULTICAST_TTL', `IP_TTL', `IPV6_MULTICAST_HOPS' or
1512 * `IPV6_UNICAST_HOPS', depending on which option is applicable.
1513 *
1514 * The `struct addrinfo' is used to destinguish between unicast and multicast
1515 * sockets.
1516 */
1517 static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
1518 {
1519 DEBUG ("network plugin: network_set_ttl: network_config_ttl = %i;",
1520 network_config_ttl);
1522 assert (se->type == SOCKENT_TYPE_CLIENT);
1524 if ((network_config_ttl < 1) || (network_config_ttl > 255))
1525 return (-1);
1527 if (ai->ai_family == AF_INET)
1528 {
1529 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1530 int optname;
1532 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1533 optname = IP_MULTICAST_TTL;
1534 else
1535 optname = IP_TTL;
1537 if (setsockopt (se->data.client.fd, IPPROTO_IP, optname,
1538 &network_config_ttl,
1539 sizeof (network_config_ttl)) != 0)
1540 {
1541 char errbuf[1024];
1542 ERROR ("setsockopt: %s",
1543 sstrerror (errno, errbuf, sizeof (errbuf)));
1544 return (-1);
1545 }
1546 }
1547 else if (ai->ai_family == AF_INET6)
1548 {
1549 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1550 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1551 int optname;
1553 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1554 optname = IPV6_MULTICAST_HOPS;
1555 else
1556 optname = IPV6_UNICAST_HOPS;
1558 if (setsockopt (se->data.client.fd, IPPROTO_IPV6, optname,
1559 &network_config_ttl,
1560 sizeof (network_config_ttl)) != 0)
1561 {
1562 char errbuf[1024];
1563 ERROR ("setsockopt: %s",
1564 sstrerror (errno, errbuf,
1565 sizeof (errbuf)));
1566 return (-1);
1567 }
1568 }
1570 return (0);
1571 } /* int network_set_ttl */
1573 static int network_bind_socket (int fd, const struct addrinfo *ai)
1574 {
1575 int loop = 0;
1576 int yes = 1;
1578 /* allow multiple sockets to use the same PORT number */
1579 if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR,
1580 &yes, sizeof(yes)) == -1) {
1581 char errbuf[1024];
1582 ERROR ("setsockopt: %s",
1583 sstrerror (errno, errbuf, sizeof (errbuf)));
1584 return (-1);
1585 }
1587 DEBUG ("fd = %i; calling `bind'", fd);
1589 if (bind (fd, ai->ai_addr, ai->ai_addrlen) == -1)
1590 {
1591 char errbuf[1024];
1592 ERROR ("bind: %s",
1593 sstrerror (errno, errbuf, sizeof (errbuf)));
1594 return (-1);
1595 }
1597 if (ai->ai_family == AF_INET)
1598 {
1599 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1600 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1601 {
1602 struct ip_mreq mreq;
1604 DEBUG ("fd = %i; IPv4 multicast address found", fd);
1606 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1607 mreq.imr_interface.s_addr = htonl (INADDR_ANY);
1609 if (setsockopt (fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1610 &loop, sizeof (loop)) == -1)
1611 {
1612 char errbuf[1024];
1613 ERROR ("setsockopt: %s",
1614 sstrerror (errno, errbuf,
1615 sizeof (errbuf)));
1616 return (-1);
1617 }
1619 if (setsockopt (fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1620 &mreq, sizeof (mreq)) == -1)
1621 {
1622 char errbuf[1024];
1623 ERROR ("setsockopt: %s",
1624 sstrerror (errno, errbuf,
1625 sizeof (errbuf)));
1626 return (-1);
1627 }
1628 }
1629 }
1630 else if (ai->ai_family == AF_INET6)
1631 {
1632 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1633 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1634 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1635 {
1636 struct ipv6_mreq mreq;
1638 DEBUG ("fd = %i; IPv6 multicast address found", fd);
1640 memcpy (&mreq.ipv6mr_multiaddr,
1641 &addr->sin6_addr,
1642 sizeof (addr->sin6_addr));
1644 /* http://developer.apple.com/documentation/Darwin/Reference/ManPages/man4/ip6.4.html
1645 * ipv6mr_interface may be set to zeroes to
1646 * choose the default multicast interface or to
1647 * the index of a particular multicast-capable
1648 * interface if the host is multihomed.
1649 * Membership is associ-associated with a
1650 * single interface; programs running on
1651 * multihomed hosts may need to join the same
1652 * group on more than one interface.*/
1653 mreq.ipv6mr_interface = 0;
1655 if (setsockopt (fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
1656 &loop, sizeof (loop)) == -1)
1657 {
1658 char errbuf[1024];
1659 ERROR ("setsockopt: %s",
1660 sstrerror (errno, errbuf,
1661 sizeof (errbuf)));
1662 return (-1);
1663 }
1665 if (setsockopt (fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
1666 &mreq, sizeof (mreq)) == -1)
1667 {
1668 char errbuf[1024];
1669 ERROR ("setsockopt: %s",
1670 sstrerror (errno, errbuf,
1671 sizeof (errbuf)));
1672 return (-1);
1673 }
1674 }
1675 }
1677 return (0);
1678 } /* int network_bind_socket */
1680 /* Initialize a sockent structure. `type' must be either `SOCKENT_TYPE_CLIENT'
1681 * or `SOCKENT_TYPE_SERVER' */
1682 static int sockent_init (sockent_t *se, int type) /* {{{ */
1683 {
1684 if (se == NULL)
1685 return (-1);
1687 memset (se, 0, sizeof (*se));
1689 se->type = SOCKENT_TYPE_CLIENT;
1690 se->node = NULL;
1691 se->service = NULL;
1692 se->next = NULL;
1694 if (type == SOCKENT_TYPE_SERVER)
1695 {
1696 se->type = SOCKENT_TYPE_SERVER;
1697 se->data.server.fd = NULL;
1698 #if HAVE_LIBGCRYPT
1699 se->data.server.security_level = SECURITY_LEVEL_NONE;
1700 se->data.server.auth_file = NULL;
1701 se->data.server.userdb = NULL;
1702 se->data.server.cypher = NULL;
1703 #endif
1704 }
1705 else
1706 {
1707 se->data.client.fd = -1;
1708 se->data.client.addr = NULL;
1709 #if HAVE_LIBGCRYPT
1710 se->data.client.security_level = SECURITY_LEVEL_NONE;
1711 se->data.client.username = NULL;
1712 se->data.client.password = NULL;
1713 se->data.client.cypher = NULL;
1714 #endif
1715 }
1717 return (0);
1718 } /* }}} int sockent_init */
1720 /* Open the file descriptors for a initialized sockent structure. */
1721 static int sockent_open (sockent_t *se) /* {{{ */
1722 {
1723 struct addrinfo ai_hints;
1724 struct addrinfo *ai_list, *ai_ptr;
1725 int ai_return;
1727 const char *node;
1728 const char *service;
1730 if (se == NULL)
1731 return (-1);
1733 /* Set up the security structures. */
1734 #if HAVE_LIBGCRYPT /* {{{ */
1735 if (se->type == SOCKENT_TYPE_CLIENT)
1736 {
1737 if (se->data.client.security_level > SECURITY_LEVEL_NONE)
1738 {
1739 if ((se->data.client.username == NULL)
1740 || (se->data.client.password == NULL))
1741 {
1742 ERROR ("network plugin: Client socket with "
1743 "security requested, but no "
1744 "credentials are configured.");
1745 return (-1);
1746 }
1747 gcry_md_hash_buffer (GCRY_MD_SHA256,
1748 se->data.client.password_hash,
1749 se->data.client.password,
1750 strlen (se->data.client.password));
1751 }
1752 }
1753 else /* (se->type == SOCKENT_TYPE_SERVER) */
1754 {
1755 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
1756 {
1757 if (se->data.server.auth_file == NULL)
1758 {
1759 ERROR ("network plugin: Server socket with "
1760 "security requested, but no "
1761 "password file is configured.");
1762 return (-1);
1763 }
1764 }
1765 if (se->data.server.auth_file != NULL)
1766 {
1767 se->data.server.userdb = fbh_create (se->data.server.auth_file);
1768 if (se->data.server.userdb == NULL)
1769 {
1770 ERROR ("network plugin: Reading password file "
1771 "`%s' failed.",
1772 se->data.server.auth_file);
1773 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
1774 return (-1);
1775 }
1776 }
1777 }
1778 #endif /* }}} HAVE_LIBGCRYPT */
1780 node = se->node;
1781 service = se->service;
1783 if (service == NULL)
1784 service = NET_DEFAULT_PORT;
1786 DEBUG ("network plugin: sockent_open: node = %s; service = %s;",
1787 node, service);
1789 memset (&ai_hints, 0, sizeof (ai_hints));
1790 ai_hints.ai_flags = 0;
1791 #ifdef AI_PASSIVE
1792 ai_hints.ai_flags |= AI_PASSIVE;
1793 #endif
1794 #ifdef AI_ADDRCONFIG
1795 ai_hints.ai_flags |= AI_ADDRCONFIG;
1796 #endif
1797 ai_hints.ai_family = AF_UNSPEC;
1798 ai_hints.ai_socktype = SOCK_DGRAM;
1799 ai_hints.ai_protocol = IPPROTO_UDP;
1801 ai_return = getaddrinfo (node, service, &ai_hints, &ai_list);
1802 if (ai_return != 0)
1803 {
1804 ERROR ("network plugin: getaddrinfo (%s, %s) failed: %s",
1805 (se->node == NULL) ? "(null)" : se->node,
1806 (se->service == NULL) ? "(null)" : se->service,
1807 gai_strerror (ai_return));
1808 return (-1);
1809 }
1811 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1812 {
1813 int status;
1815 if (se->type == SOCKENT_TYPE_SERVER) /* {{{ */
1816 {
1817 int *tmp;
1819 tmp = realloc (se->data.server.fd,
1820 sizeof (*tmp) * (se->data.server.fd_num + 1));
1821 if (tmp == NULL)
1822 {
1823 ERROR ("network plugin: realloc failed.");
1824 continue;
1825 }
1826 se->data.server.fd = tmp;
1827 tmp = se->data.server.fd + se->data.server.fd_num;
1829 *tmp = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
1830 ai_ptr->ai_protocol);
1831 if (*tmp < 0)
1832 {
1833 char errbuf[1024];
1834 ERROR ("network plugin: socket(2) failed: %s",
1835 sstrerror (errno, errbuf,
1836 sizeof (errbuf)));
1837 continue;
1838 }
1840 status = network_bind_socket (*tmp, ai_ptr);
1841 if (status != 0)
1842 {
1843 close (*tmp);
1844 *tmp = -1;
1845 continue;
1846 }
1848 se->data.server.fd_num++;
1849 continue;
1850 } /* }}} if (se->type == SOCKENT_TYPE_SERVER) */
1851 else /* if (se->type == SOCKENT_TYPE_CLIENT) {{{ */
1852 {
1853 se->data.client.fd = socket (ai_ptr->ai_family,
1854 ai_ptr->ai_socktype,
1855 ai_ptr->ai_protocol);
1856 if (se->data.client.fd < 0)
1857 {
1858 char errbuf[1024];
1859 ERROR ("network plugin: socket(2) failed: %s",
1860 sstrerror (errno, errbuf,
1861 sizeof (errbuf)));
1862 continue;
1863 }
1865 se->data.client.addr = malloc (sizeof (*se->data.client.addr));
1866 if (se->data.client.addr == NULL)
1867 {
1868 ERROR ("network plugin: malloc failed.");
1869 close (se->data.client.fd);
1870 se->data.client.fd = -1;
1871 continue;
1872 }
1874 memset (se->data.client.addr, 0, sizeof (*se->data.client.addr));
1875 assert (sizeof (*se->data.client.addr) >= ai_ptr->ai_addrlen);
1876 memcpy (se->data.client.addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1877 se->data.client.addrlen = ai_ptr->ai_addrlen;
1879 network_set_ttl (se, ai_ptr);
1881 /* We don't open more than one write-socket per
1882 * node/service pair.. */
1883 break;
1884 } /* }}} if (se->type == SOCKENT_TYPE_CLIENT) */
1885 } /* for (ai_list) */
1887 freeaddrinfo (ai_list);
1889 /* Check if all went well. */
1890 if (se->type == SOCKENT_TYPE_SERVER)
1891 {
1892 if (se->data.server.fd_num <= 0)
1893 return (-1);
1894 }
1895 else /* if (se->type == SOCKENT_TYPE_CLIENT) */
1896 {
1897 if (se->data.client.fd < 0)
1898 return (-1);
1899 }
1901 return (0);
1902 } /* }}} int sockent_open */
1904 /* Add a sockent to the global list of sockets */
1905 static int sockent_add (sockent_t *se) /* {{{ */
1906 {
1907 sockent_t *last_ptr;
1909 if (se == NULL)
1910 return (-1);
1912 if (se->type == SOCKENT_TYPE_SERVER)
1913 {
1914 struct pollfd *tmp;
1915 size_t i;
1917 tmp = realloc (listen_sockets_pollfd,
1918 sizeof (*tmp) * (listen_sockets_num
1919 + se->data.server.fd_num));
1920 if (tmp == NULL)
1921 {
1922 ERROR ("network plugin: realloc failed.");
1923 return (-1);
1924 }
1925 listen_sockets_pollfd = tmp;
1926 tmp = listen_sockets_pollfd + listen_sockets_num;
1928 for (i = 0; i < se->data.server.fd_num; i++)
1929 {
1930 memset (tmp + i, 0, sizeof (*tmp));
1931 tmp[i].fd = se->data.server.fd[i];
1932 tmp[i].events = POLLIN | POLLPRI;
1933 tmp[i].revents = 0;
1934 }
1936 listen_sockets_num += se->data.server.fd_num;
1938 if (listen_sockets == NULL)
1939 {
1940 listen_sockets = se;
1941 return (0);
1942 }
1943 last_ptr = listen_sockets;
1944 }
1945 else /* if (se->type == SOCKENT_TYPE_CLIENT) */
1946 {
1947 if (sending_sockets == NULL)
1948 {
1949 sending_sockets = se;
1950 return (0);
1951 }
1952 last_ptr = sending_sockets;
1953 }
1955 while (last_ptr->next != NULL)
1956 last_ptr = last_ptr->next;
1957 last_ptr->next = se;
1959 return (0);
1960 } /* }}} int sockent_add */
1962 static void *dispatch_thread (void __attribute__((unused)) *arg) /* {{{ */
1963 {
1964 while (42)
1965 {
1966 receive_list_entry_t *ent;
1967 sockent_t *se;
1969 /* Lock and wait for more data to come in */
1970 pthread_mutex_lock (&receive_list_lock);
1971 while ((listen_loop == 0)
1972 && (receive_list_head == NULL))
1973 pthread_cond_wait (&receive_list_cond, &receive_list_lock);
1975 /* Remove the head entry and unlock */
1976 ent = receive_list_head;
1977 if (ent != NULL)
1978 receive_list_head = ent->next;
1979 pthread_mutex_unlock (&receive_list_lock);
1981 /* Check whether we are supposed to exit. We do NOT check `listen_loop'
1982 * because we dispatch all missing packets before shutting down. */
1983 if (ent == NULL)
1984 break;
1986 /* Look for the correct `sockent_t' */
1987 se = listen_sockets;
1988 while (se != NULL)
1989 {
1990 size_t i;
1992 for (i = 0; i < se->data.server.fd_num; i++)
1993 if (se->data.server.fd[i] == ent->fd)
1994 break;
1996 if (i < se->data.server.fd_num)
1997 break;
1999 se = se->next;
2000 }
2002 if (se == NULL)
2003 {
2004 ERROR ("network plugin: Got packet from FD %i, but can't "
2005 "find an appropriate socket entry.",
2006 ent->fd);
2007 sfree (ent->data);
2008 sfree (ent);
2009 continue;
2010 }
2012 parse_packet (se, ent->data, ent->data_len, /* flags = */ 0);
2013 sfree (ent->data);
2014 sfree (ent);
2015 } /* while (42) */
2017 return (NULL);
2018 } /* }}} void *dispatch_thread */
2020 static int network_receive (void) /* {{{ */
2021 {
2022 char buffer[network_config_packet_size];
2023 int buffer_len;
2025 int i;
2026 int status;
2028 receive_list_entry_t *private_list_head;
2029 receive_list_entry_t *private_list_tail;
2031 assert (listen_sockets_num > 0);
2033 private_list_head = NULL;
2034 private_list_tail = NULL;
2036 while (listen_loop == 0)
2037 {
2038 status = poll (listen_sockets_pollfd, listen_sockets_num, -1);
2040 if (status <= 0)
2041 {
2042 char errbuf[1024];
2043 if (errno == EINTR)
2044 continue;
2045 ERROR ("poll failed: %s",
2046 sstrerror (errno, errbuf, sizeof (errbuf)));
2047 return (-1);
2048 }
2050 for (i = 0; (i < listen_sockets_num) && (status > 0); i++)
2051 {
2052 receive_list_entry_t *ent;
2054 if ((listen_sockets_pollfd[i].revents
2055 & (POLLIN | POLLPRI)) == 0)
2056 continue;
2057 status--;
2059 buffer_len = recv (listen_sockets_pollfd[i].fd,
2060 buffer, sizeof (buffer),
2061 0 /* no flags */);
2062 if (buffer_len < 0)
2063 {
2064 char errbuf[1024];
2065 ERROR ("recv failed: %s",
2066 sstrerror (errno, errbuf,
2067 sizeof (errbuf)));
2068 return (-1);
2069 }
2071 /* TODO: Possible performance enhancement: Do not free
2072 * these entries in the dispatch thread but put them in
2073 * another list, so we don't have to allocate more and
2074 * more of these structures. */
2075 ent = malloc (sizeof (receive_list_entry_t));
2076 if (ent == NULL)
2077 {
2078 ERROR ("network plugin: malloc failed.");
2079 return (-1);
2080 }
2081 memset (ent, 0, sizeof (receive_list_entry_t));
2082 ent->data = malloc (network_config_packet_size);
2083 if (ent->data == NULL)
2084 {
2085 ERROR ("network plugin: malloc failed.");
2086 return (-1);
2087 }
2088 ent->fd = listen_sockets_pollfd[i].fd;
2089 ent->next = NULL;
2091 memcpy (ent->data, buffer, buffer_len);
2092 ent->data_len = buffer_len;
2094 if (private_list_head == NULL)
2095 private_list_head = ent;
2096 else
2097 private_list_tail->next = ent;
2098 private_list_tail = ent;
2100 /* Do not block here. Blocking here has led to
2101 * insufficient performance in the past. */
2102 if (pthread_mutex_trylock (&receive_list_lock) == 0)
2103 {
2104 if (receive_list_head == NULL)
2105 receive_list_head = private_list_head;
2106 else
2107 receive_list_tail->next = private_list_head;
2108 receive_list_tail = private_list_tail;
2110 private_list_head = NULL;
2111 private_list_tail = NULL;
2113 pthread_cond_signal (&receive_list_cond);
2114 pthread_mutex_unlock (&receive_list_lock);
2115 }
2116 } /* for (listen_sockets_pollfd) */
2117 } /* while (listen_loop == 0) */
2119 /* Make sure everything is dispatched before exiting. */
2120 if (private_list_head != NULL)
2121 {
2122 pthread_mutex_lock (&receive_list_lock);
2124 if (receive_list_head == NULL)
2125 receive_list_head = private_list_head;
2126 else
2127 receive_list_tail->next = private_list_head;
2128 receive_list_tail = private_list_tail;
2130 private_list_head = NULL;
2131 private_list_tail = NULL;
2133 pthread_cond_signal (&receive_list_cond);
2134 pthread_mutex_unlock (&receive_list_lock);
2135 }
2137 return (0);
2138 } /* }}} int network_receive */
2140 static void *receive_thread (void __attribute__((unused)) *arg)
2141 {
2142 return (network_receive () ? (void *) 1 : (void *) 0);
2143 } /* void *receive_thread */
2145 static void network_init_buffer (void)
2146 {
2147 memset (send_buffer, 0, network_config_packet_size);
2148 send_buffer_ptr = send_buffer;
2149 send_buffer_fill = 0;
2151 memset (&send_buffer_vl, 0, sizeof (send_buffer_vl));
2152 } /* int network_init_buffer */
2154 static void networt_send_buffer_plain (const sockent_t *se, /* {{{ */
2155 const char *buffer, size_t buffer_size)
2156 {
2157 int status;
2159 while (42)
2160 {
2161 status = sendto (se->data.client.fd, buffer, buffer_size,
2162 /* flags = */ 0,
2163 (struct sockaddr *) se->data.client.addr,
2164 se->data.client.addrlen);
2165 if (status < 0)
2166 {
2167 char errbuf[1024];
2168 if (errno == EINTR)
2169 continue;
2170 ERROR ("network plugin: sendto failed: %s",
2171 sstrerror (errno, errbuf,
2172 sizeof (errbuf)));
2173 break;
2174 }
2176 break;
2177 } /* while (42) */
2178 } /* }}} void networt_send_buffer_plain */
2180 #if HAVE_LIBGCRYPT
2181 #define BUFFER_ADD(p,s) do { \
2182 memcpy (buffer + buffer_offset, (p), (s)); \
2183 buffer_offset += (s); \
2184 } while (0)
2186 static void networt_send_buffer_signed (const sockent_t *se, /* {{{ */
2187 const char *in_buffer, size_t in_buffer_size)
2188 {
2189 part_signature_sha256_t ps;
2190 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2191 size_t buffer_offset;
2192 size_t username_len;
2194 gcry_md_hd_t hd;
2195 gcry_error_t err;
2196 unsigned char *hash;
2198 hd = NULL;
2199 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
2200 if (err != 0)
2201 {
2202 ERROR ("network plugin: Creating HMAC object failed: %s",
2203 gcry_strerror (err));
2204 return;
2205 }
2207 err = gcry_md_setkey (hd, se->data.client.password,
2208 strlen (se->data.client.password));
2209 if (err != 0)
2210 {
2211 ERROR ("network plugin: gcry_md_setkey failed: %s",
2212 gcry_strerror (err));
2213 gcry_md_close (hd);
2214 return;
2215 }
2217 username_len = strlen (se->data.client.username);
2218 if (username_len > (BUFF_SIG_SIZE - PART_SIGNATURE_SHA256_SIZE))
2219 {
2220 ERROR ("network plugin: Username too long: %s",
2221 se->data.client.username);
2222 return;
2223 }
2225 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE,
2226 se->data.client.username, username_len);
2227 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE + username_len,
2228 in_buffer, in_buffer_size);
2230 /* Initialize the `ps' structure. */
2231 memset (&ps, 0, sizeof (ps));
2232 ps.head.type = htons (TYPE_SIGN_SHA256);
2233 ps.head.length = htons (PART_SIGNATURE_SHA256_SIZE + username_len);
2235 /* Calculate the hash value. */
2236 gcry_md_write (hd, buffer + PART_SIGNATURE_SHA256_SIZE,
2237 username_len + in_buffer_size);
2238 hash = gcry_md_read (hd, GCRY_MD_SHA256);
2239 if (hash == NULL)
2240 {
2241 ERROR ("network plugin: gcry_md_read failed.");
2242 gcry_md_close (hd);
2243 return;
2244 }
2245 memcpy (ps.hash, hash, sizeof (ps.hash));
2247 /* Add the header */
2248 buffer_offset = 0;
2250 BUFFER_ADD (&ps.head.type, sizeof (ps.head.type));
2251 BUFFER_ADD (&ps.head.length, sizeof (ps.head.length));
2252 BUFFER_ADD (ps.hash, sizeof (ps.hash));
2254 assert (buffer_offset == PART_SIGNATURE_SHA256_SIZE);
2256 gcry_md_close (hd);
2257 hd = NULL;
2259 buffer_offset = PART_SIGNATURE_SHA256_SIZE + username_len + in_buffer_size;
2260 networt_send_buffer_plain (se, buffer, buffer_offset);
2261 } /* }}} void networt_send_buffer_signed */
2263 static void networt_send_buffer_encrypted (sockent_t *se, /* {{{ */
2264 const char *in_buffer, size_t in_buffer_size)
2265 {
2266 part_encryption_aes256_t pea;
2267 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2268 size_t buffer_size;
2269 size_t buffer_offset;
2270 size_t header_size;
2271 size_t username_len;
2272 gcry_error_t err;
2273 gcry_cipher_hd_t cypher;
2275 /* Initialize the header fields */
2276 memset (&pea, 0, sizeof (pea));
2277 pea.head.type = htons (TYPE_ENCR_AES256);
2279 pea.username = se->data.client.username;
2281 username_len = strlen (pea.username);
2282 if ((PART_ENCRYPTION_AES256_SIZE + username_len) > BUFF_SIG_SIZE)
2283 {
2284 ERROR ("network plugin: Username too long: %s", pea.username);
2285 return;
2286 }
2288 buffer_size = PART_ENCRYPTION_AES256_SIZE + username_len + in_buffer_size;
2289 header_size = PART_ENCRYPTION_AES256_SIZE + username_len
2290 - sizeof (pea.hash);
2292 assert (buffer_size <= sizeof (buffer));
2293 DEBUG ("network plugin: networt_send_buffer_encrypted: "
2294 "buffer_size = %zu;", buffer_size);
2296 pea.head.length = htons ((uint16_t) (PART_ENCRYPTION_AES256_SIZE
2297 + username_len + in_buffer_size));
2298 pea.username_length = htons ((uint16_t) username_len);
2300 /* Chose a random initialization vector. */
2301 gcry_randomize ((void *) &pea.iv, sizeof (pea.iv), GCRY_STRONG_RANDOM);
2303 /* Create hash of the payload */
2304 gcry_md_hash_buffer (GCRY_MD_SHA1, pea.hash, in_buffer, in_buffer_size);
2306 /* Initialize the buffer */
2307 buffer_offset = 0;
2308 memset (buffer, 0, sizeof (buffer));
2311 BUFFER_ADD (&pea.head.type, sizeof (pea.head.type));
2312 BUFFER_ADD (&pea.head.length, sizeof (pea.head.length));
2313 BUFFER_ADD (&pea.username_length, sizeof (pea.username_length));
2314 BUFFER_ADD (pea.username, username_len);
2315 BUFFER_ADD (pea.iv, sizeof (pea.iv));
2316 assert (buffer_offset == header_size);
2317 BUFFER_ADD (pea.hash, sizeof (pea.hash));
2318 BUFFER_ADD (in_buffer, in_buffer_size);
2320 assert (buffer_offset == buffer_size);
2322 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
2323 se->data.client.password);
2324 if (cypher == NULL)
2325 return;
2327 /* Encrypt the buffer in-place */
2328 err = gcry_cipher_encrypt (cypher,
2329 buffer + header_size,
2330 buffer_size - header_size,
2331 /* in = */ NULL, /* in len = */ 0);
2332 if (err != 0)
2333 {
2334 ERROR ("network plugin: gcry_cipher_encrypt returned: %s",
2335 gcry_strerror (err));
2336 return;
2337 }
2339 /* Send it out without further modifications */
2340 networt_send_buffer_plain (se, buffer, buffer_size);
2341 } /* }}} void networt_send_buffer_encrypted */
2342 #undef BUFFER_ADD
2343 #endif /* HAVE_LIBGCRYPT */
2345 static void network_send_buffer (char *buffer, size_t buffer_len) /* {{{ */
2346 {
2347 sockent_t *se;
2349 DEBUG ("network plugin: network_send_buffer: buffer_len = %zu", buffer_len);
2351 for (se = sending_sockets; se != NULL; se = se->next)
2352 {
2353 #if HAVE_LIBGCRYPT
2354 if (se->data.client.security_level == SECURITY_LEVEL_ENCRYPT)
2355 networt_send_buffer_encrypted (se, buffer, buffer_len);
2356 else if (se->data.client.security_level == SECURITY_LEVEL_SIGN)
2357 networt_send_buffer_signed (se, buffer, buffer_len);
2358 else /* if (se->data.client.security_level == SECURITY_LEVEL_NONE) */
2359 #endif /* HAVE_LIBGCRYPT */
2360 networt_send_buffer_plain (se, buffer, buffer_len);
2361 } /* for (sending_sockets) */
2362 } /* }}} void network_send_buffer */
2364 static int add_to_buffer (char *buffer, int buffer_size, /* {{{ */
2365 value_list_t *vl_def,
2366 const data_set_t *ds, const value_list_t *vl)
2367 {
2368 char *buffer_orig = buffer;
2370 if (strcmp (vl_def->host, vl->host) != 0)
2371 {
2372 if (write_part_string (&buffer, &buffer_size, TYPE_HOST,
2373 vl->host, strlen (vl->host)) != 0)
2374 return (-1);
2375 sstrncpy (vl_def->host, vl->host, sizeof (vl_def->host));
2376 }
2378 if (vl_def->time != vl->time)
2379 {
2380 if (write_part_number (&buffer, &buffer_size, TYPE_TIME,
2381 (uint64_t) vl->time))
2382 return (-1);
2383 vl_def->time = vl->time;
2384 }
2386 if (vl_def->interval != vl->interval)
2387 {
2388 if (write_part_number (&buffer, &buffer_size, TYPE_INTERVAL,
2389 (uint64_t) vl->interval))
2390 return (-1);
2391 vl_def->interval = vl->interval;
2392 }
2394 if (strcmp (vl_def->plugin, vl->plugin) != 0)
2395 {
2396 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN,
2397 vl->plugin, strlen (vl->plugin)) != 0)
2398 return (-1);
2399 sstrncpy (vl_def->plugin, vl->plugin, sizeof (vl_def->plugin));
2400 }
2402 if (strcmp (vl_def->plugin_instance, vl->plugin_instance) != 0)
2403 {
2404 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
2405 vl->plugin_instance,
2406 strlen (vl->plugin_instance)) != 0)
2407 return (-1);
2408 sstrncpy (vl_def->plugin_instance, vl->plugin_instance, sizeof (vl_def->plugin_instance));
2409 }
2411 if (strcmp (vl_def->type, vl->type) != 0)
2412 {
2413 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
2414 vl->type, strlen (vl->type)) != 0)
2415 return (-1);
2416 sstrncpy (vl_def->type, ds->type, sizeof (vl_def->type));
2417 }
2419 if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
2420 {
2421 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE_INSTANCE,
2422 vl->type_instance,
2423 strlen (vl->type_instance)) != 0)
2424 return (-1);
2425 sstrncpy (vl_def->type_instance, vl->type_instance, sizeof (vl_def->type_instance));
2426 }
2428 if (write_part_values (&buffer, &buffer_size, ds, vl) != 0)
2429 return (-1);
2431 return (buffer - buffer_orig);
2432 } /* }}} int add_to_buffer */
2434 static void flush_buffer (void)
2435 {
2436 DEBUG ("network plugin: flush_buffer: send_buffer_fill = %i",
2437 send_buffer_fill);
2439 network_send_buffer (send_buffer, (size_t) send_buffer_fill);
2440 network_init_buffer ();
2441 }
2443 static int network_write (const data_set_t *ds, const value_list_t *vl,
2444 user_data_t __attribute__((unused)) *user_data)
2445 {
2446 int status;
2448 if (!check_send_okay (vl))
2449 {
2450 #if COLLECT_DEBUG
2451 char name[6*DATA_MAX_NAME_LEN];
2452 FORMAT_VL (name, sizeof (name), vl);
2453 name[sizeof (name) - 1] = 0;
2454 DEBUG ("network plugin: network_write: "
2455 "NOT sending %s.", name);
2456 #endif
2457 return (0);
2458 }
2460 uc_meta_data_add_unsigned_int (vl,
2461 "network:time_sent", (uint64_t) vl->time);
2463 pthread_mutex_lock (&send_buffer_lock);
2465 status = add_to_buffer (send_buffer_ptr,
2466 network_config_packet_size - (send_buffer_fill + BUFF_SIG_SIZE),
2467 &send_buffer_vl,
2468 ds, vl);
2469 if (status >= 0)
2470 {
2471 /* status == bytes added to the buffer */
2472 send_buffer_fill += status;
2473 send_buffer_ptr += status;
2474 }
2475 else
2476 {
2477 flush_buffer ();
2479 status = add_to_buffer (send_buffer_ptr,
2480 network_config_packet_size - (send_buffer_fill + BUFF_SIG_SIZE),
2481 &send_buffer_vl,
2482 ds, vl);
2484 if (status >= 0)
2485 {
2486 send_buffer_fill += status;
2487 send_buffer_ptr += status;
2488 }
2489 }
2491 if (status < 0)
2492 {
2493 ERROR ("network plugin: Unable to append to the "
2494 "buffer for some weird reason");
2495 }
2496 else if ((network_config_packet_size - send_buffer_fill) < 15)
2497 {
2498 flush_buffer ();
2499 }
2501 pthread_mutex_unlock (&send_buffer_lock);
2503 return ((status < 0) ? -1 : 0);
2504 } /* int network_write */
2506 static int network_config_set_boolean (const oconfig_item_t *ci, /* {{{ */
2507 int *retval)
2508 {
2509 if ((ci->values_num != 1)
2510 || ((ci->values[0].type != OCONFIG_TYPE_BOOLEAN)
2511 && (ci->values[0].type != OCONFIG_TYPE_STRING)))
2512 {
2513 ERROR ("network plugin: The `%s' config option needs "
2514 "exactly one boolean argument.", ci->key);
2515 return (-1);
2516 }
2518 if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
2519 {
2520 if (ci->values[0].value.boolean)
2521 *retval = 1;
2522 else
2523 *retval = 0;
2524 }
2525 else
2526 {
2527 char *str = ci->values[0].value.string;
2529 if ((strcasecmp ("true", str) == 0)
2530 || (strcasecmp ("yes", str) == 0)
2531 || (strcasecmp ("on", str) == 0))
2532 *retval = 1;
2533 else if ((strcasecmp ("false", str) == 0)
2534 || (strcasecmp ("no", str) == 0)
2535 || (strcasecmp ("off", str) == 0))
2536 *retval = 0;
2537 else
2538 {
2539 ERROR ("network plugin: Cannot parse string value `%s' of the `%s' "
2540 "option as boolean value.",
2541 str, ci->key);
2542 return (-1);
2543 }
2544 }
2546 return (0);
2547 } /* }}} int network_config_set_boolean */
2549 static int network_config_set_ttl (const oconfig_item_t *ci) /* {{{ */
2550 {
2551 int tmp;
2552 if ((ci->values_num != 1)
2553 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
2554 {
2555 WARNING ("network plugin: The `TimeToLive' config option needs exactly "
2556 "one numeric argument.");
2557 return (-1);
2558 }
2560 tmp = (int) ci->values[0].value.number;
2561 if ((tmp > 0) && (tmp <= 255))
2562 network_config_ttl = tmp;
2564 return (0);
2565 } /* }}} int network_config_set_ttl */
2567 static int network_config_set_buffer_size (const oconfig_item_t *ci) /* {{{ */
2568 {
2569 int tmp;
2570 if ((ci->values_num != 1)
2571 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
2572 {
2573 WARNING ("network plugin: The `MaxPacketSize' config option needs exactly "
2574 "one numeric argument.");
2575 return (-1);
2576 }
2578 tmp = (int) ci->values[0].value.number;
2579 if ((tmp >= 1024) && (tmp <= 65535))
2580 network_config_packet_size = tmp;
2582 return (0);
2583 } /* }}} int network_config_set_buffer_size */
2585 #if HAVE_LIBGCRYPT
2586 static int network_config_set_string (const oconfig_item_t *ci, /* {{{ */
2587 char **ret_string)
2588 {
2589 char *tmp;
2590 if ((ci->values_num != 1)
2591 || (ci->values[0].type != OCONFIG_TYPE_STRING))
2592 {
2593 WARNING ("network plugin: The `%s' config option needs exactly "
2594 "one string argument.", ci->key);
2595 return (-1);
2596 }
2598 tmp = strdup (ci->values[0].value.string);
2599 if (tmp == NULL)
2600 return (-1);
2602 sfree (*ret_string);
2603 *ret_string = tmp;
2605 return (0);
2606 } /* }}} int network_config_set_string */
2607 #endif /* HAVE_LIBGCRYPT */
2609 #if HAVE_LIBGCRYPT
2610 static int network_config_set_security_level (oconfig_item_t *ci, /* {{{ */
2611 int *retval)
2612 {
2613 char *str;
2614 if ((ci->values_num != 1)
2615 || (ci->values[0].type != OCONFIG_TYPE_STRING))
2616 {
2617 WARNING ("network plugin: The `SecurityLevel' config option needs exactly "
2618 "one string argument.");
2619 return (-1);
2620 }
2622 str = ci->values[0].value.string;
2623 if (strcasecmp ("Encrypt", str) == 0)
2624 *retval = SECURITY_LEVEL_ENCRYPT;
2625 else if (strcasecmp ("Sign", str) == 0)
2626 *retval = SECURITY_LEVEL_SIGN;
2627 else if (strcasecmp ("None", str) == 0)
2628 *retval = SECURITY_LEVEL_NONE;
2629 else
2630 {
2631 WARNING ("network plugin: Unknown security level: %s.", str);
2632 return (-1);
2633 }
2635 return (0);
2636 } /* }}} int network_config_set_security_level */
2637 #endif /* HAVE_LIBGCRYPT */
2639 static int network_config_add_listen (const oconfig_item_t *ci) /* {{{ */
2640 {
2641 sockent_t *se;
2642 int status;
2643 int i;
2645 if ((ci->values_num < 1) || (ci->values_num > 2)
2646 || (ci->values[0].type != OCONFIG_TYPE_STRING)
2647 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
2648 {
2649 ERROR ("network plugin: The `%s' config option needs "
2650 "one or two string arguments.", ci->key);
2651 return (-1);
2652 }
2654 se = malloc (sizeof (*se));
2655 if (se == NULL)
2656 {
2657 ERROR ("network plugin: malloc failed.");
2658 return (-1);
2659 }
2660 sockent_init (se, SOCKENT_TYPE_SERVER);
2662 se->node = strdup (ci->values[0].value.string);
2663 if (ci->values_num >= 2)
2664 se->service = strdup (ci->values[1].value.string);
2666 for (i = 0; i < ci->children_num; i++)
2667 {
2668 oconfig_item_t *child = ci->children + i;
2670 #if HAVE_LIBGCRYPT
2671 if (strcasecmp ("AuthFile", child->key) == 0)
2672 network_config_set_string (child, &se->data.server.auth_file);
2673 else if (strcasecmp ("SecurityLevel", child->key) == 0)
2674 network_config_set_security_level (child,
2675 &se->data.server.security_level);
2676 else
2677 #endif /* HAVE_LIBGCRYPT */
2678 {
2679 WARNING ("network plugin: Option `%s' is not allowed here.",
2680 child->key);
2681 }
2682 }
2684 #if HAVE_LIBGCRYPT
2685 if ((se->data.server.security_level > SECURITY_LEVEL_NONE)
2686 && (se->data.server.auth_file == NULL))
2687 {
2688 ERROR ("network plugin: A security level higher than `none' was "
2689 "requested, but no AuthFile option was given. Cowardly refusing to "
2690 "open this socket!");
2691 sockent_destroy (se);
2692 return (-1);
2693 }
2694 #endif /* HAVE_LIBGCRYPT */
2696 status = sockent_open (se);
2697 if (status != 0)
2698 {
2699 ERROR ("network plugin: network_config_add_listen: sockent_open failed.");
2700 sockent_destroy (se);
2701 return (-1);
2702 }
2704 status = sockent_add (se);
2705 if (status != 0)
2706 {
2707 ERROR ("network plugin: network_config_add_listen: sockent_add failed.");
2708 sockent_destroy (se);
2709 return (-1);
2710 }
2712 return (0);
2713 } /* }}} int network_config_add_listen */
2715 static int network_config_add_server (const oconfig_item_t *ci) /* {{{ */
2716 {
2717 sockent_t *se;
2718 int status;
2719 int i;
2721 if ((ci->values_num < 1) || (ci->values_num > 2)
2722 || (ci->values[0].type != OCONFIG_TYPE_STRING)
2723 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
2724 {
2725 ERROR ("network plugin: The `%s' config option needs "
2726 "one or two string arguments.", ci->key);
2727 return (-1);
2728 }
2730 se = malloc (sizeof (*se));
2731 if (se == NULL)
2732 {
2733 ERROR ("network plugin: malloc failed.");
2734 return (-1);
2735 }
2736 sockent_init (se, SOCKENT_TYPE_CLIENT);
2738 se->node = strdup (ci->values[0].value.string);
2739 if (ci->values_num >= 2)
2740 se->service = strdup (ci->values[1].value.string);
2742 for (i = 0; i < ci->children_num; i++)
2743 {
2744 oconfig_item_t *child = ci->children + i;
2746 #if HAVE_LIBGCRYPT
2747 if (strcasecmp ("Username", child->key) == 0)
2748 network_config_set_string (child, &se->data.client.username);
2749 else if (strcasecmp ("Password", child->key) == 0)
2750 network_config_set_string (child, &se->data.client.password);
2751 else if (strcasecmp ("SecurityLevel", child->key) == 0)
2752 network_config_set_security_level (child,
2753 &se->data.client.security_level);
2754 else
2755 #endif /* HAVE_LIBGCRYPT */
2756 {
2757 WARNING ("network plugin: Option `%s' is not allowed here.",
2758 child->key);
2759 }
2760 }
2762 #if HAVE_LIBGCRYPT
2763 if ((se->data.client.security_level > SECURITY_LEVEL_NONE)
2764 && ((se->data.client.username == NULL)
2765 || (se->data.client.password == NULL)))
2766 {
2767 ERROR ("network plugin: A security level higher than `none' was "
2768 "requested, but no Username or Password option was given. "
2769 "Cowardly refusing to open this socket!");
2770 sockent_destroy (se);
2771 return (-1);
2772 }
2773 #endif /* HAVE_LIBGCRYPT */
2775 status = sockent_open (se);
2776 if (status != 0)
2777 {
2778 ERROR ("network plugin: network_config_add_server: sockent_open failed.");
2779 sockent_destroy (se);
2780 return (-1);
2781 }
2783 status = sockent_add (se);
2784 if (status != 0)
2785 {
2786 ERROR ("network plugin: network_config_add_server: sockent_add failed.");
2787 sockent_destroy (se);
2788 return (-1);
2789 }
2791 return (0);
2792 } /* }}} int network_config_add_server */
2794 static int network_config (oconfig_item_t *ci) /* {{{ */
2795 {
2796 int i;
2798 for (i = 0; i < ci->children_num; i++)
2799 {
2800 oconfig_item_t *child = ci->children + i;
2802 if (strcasecmp ("Listen", child->key) == 0)
2803 network_config_add_listen (child);
2804 else if (strcasecmp ("Server", child->key) == 0)
2805 network_config_add_server (child);
2806 else if (strcasecmp ("TimeToLive", child->key) == 0)
2807 network_config_set_ttl (child);
2808 else if (strcasecmp ("MaxPacketSize", child->key) == 0)
2809 network_config_set_buffer_size (child);
2810 else if (strcasecmp ("Forward", child->key) == 0)
2811 network_config_set_boolean (child, &network_config_forward);
2812 else if (strcasecmp ("CacheFlush", child->key) == 0)
2813 /* no op for backwards compatibility only */;
2814 else
2815 {
2816 WARNING ("network plugin: Option `%s' is not allowed here.",
2817 child->key);
2818 }
2819 }
2821 return (0);
2822 } /* }}} int network_config */
2824 static int network_notification (const notification_t *n,
2825 user_data_t __attribute__((unused)) *user_data)
2826 {
2827 char buffer[network_config_packet_size];
2828 char *buffer_ptr = buffer;
2829 int buffer_free = sizeof (buffer);
2830 int status;
2832 memset (buffer, '\0', sizeof (buffer));
2835 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_TIME,
2836 (uint64_t) n->time);
2837 if (status != 0)
2838 return (-1);
2840 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_SEVERITY,
2841 (uint64_t) n->severity);
2842 if (status != 0)
2843 return (-1);
2845 if (strlen (n->host) > 0)
2846 {
2847 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_HOST,
2848 n->host, strlen (n->host));
2849 if (status != 0)
2850 return (-1);
2851 }
2853 if (strlen (n->plugin) > 0)
2854 {
2855 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_PLUGIN,
2856 n->plugin, strlen (n->plugin));
2857 if (status != 0)
2858 return (-1);
2859 }
2861 if (strlen (n->plugin_instance) > 0)
2862 {
2863 status = write_part_string (&buffer_ptr, &buffer_free,
2864 TYPE_PLUGIN_INSTANCE,
2865 n->plugin_instance, strlen (n->plugin_instance));
2866 if (status != 0)
2867 return (-1);
2868 }
2870 if (strlen (n->type) > 0)
2871 {
2872 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE,
2873 n->type, strlen (n->type));
2874 if (status != 0)
2875 return (-1);
2876 }
2878 if (strlen (n->type_instance) > 0)
2879 {
2880 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE_INSTANCE,
2881 n->type_instance, strlen (n->type_instance));
2882 if (status != 0)
2883 return (-1);
2884 }
2886 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_MESSAGE,
2887 n->message, strlen (n->message));
2888 if (status != 0)
2889 return (-1);
2891 network_send_buffer (buffer, sizeof (buffer) - buffer_free);
2893 return (0);
2894 } /* int network_notification */
2896 static int network_shutdown (void)
2897 {
2898 listen_loop++;
2900 /* Kill the listening thread */
2901 if (receive_thread_running != 0)
2902 {
2903 INFO ("network plugin: Stopping receive thread.");
2904 pthread_kill (receive_thread_id, SIGTERM);
2905 pthread_join (receive_thread_id, NULL /* no return value */);
2906 memset (&receive_thread_id, 0, sizeof (receive_thread_id));
2907 receive_thread_running = 0;
2908 }
2910 /* Shutdown the dispatching thread */
2911 if (dispatch_thread_running != 0)
2912 {
2913 INFO ("network plugin: Stopping dispatch thread.");
2914 pthread_mutex_lock (&receive_list_lock);
2915 pthread_cond_broadcast (&receive_list_cond);
2916 pthread_mutex_unlock (&receive_list_lock);
2917 pthread_join (dispatch_thread_id, /* ret = */ NULL);
2918 dispatch_thread_running = 0;
2919 }
2921 sockent_destroy (listen_sockets);
2923 if (send_buffer_fill > 0)
2924 flush_buffer ();
2926 sfree (send_buffer);
2928 /* TODO: Close `sending_sockets' */
2930 plugin_unregister_config ("network");
2931 plugin_unregister_init ("network");
2932 plugin_unregister_write ("network");
2933 plugin_unregister_shutdown ("network");
2935 return (0);
2936 } /* int network_shutdown */
2938 static int network_init (void)
2939 {
2940 static _Bool have_init = false;
2942 /* Check if we were already initialized. If so, just return - there's
2943 * nothing more to do (for now, that is). */
2944 if (have_init)
2945 return (0);
2946 have_init = true;
2948 #if HAVE_LIBGCRYPT
2949 gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
2950 gcry_control (GCRYCTL_INIT_SECMEM, 32768, 0);
2951 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
2952 #endif
2954 plugin_register_shutdown ("network", network_shutdown);
2956 send_buffer = malloc (network_config_packet_size);
2957 if (send_buffer == NULL)
2958 {
2959 ERROR ("network plugin: malloc failed.");
2960 return (-1);
2961 }
2962 network_init_buffer ();
2964 /* setup socket(s) and so on */
2965 if (sending_sockets != NULL)
2966 {
2967 plugin_register_write ("network", network_write,
2968 /* user_data = */ NULL);
2969 plugin_register_notification ("network", network_notification,
2970 /* user_data = */ NULL);
2971 }
2973 /* If no threads need to be started, return here. */
2974 if ((listen_sockets_num == 0)
2975 || ((dispatch_thread_running != 0)
2976 && (receive_thread_running != 0)))
2977 return (0);
2979 if (dispatch_thread_running == 0)
2980 {
2981 int status;
2982 status = pthread_create (&dispatch_thread_id,
2983 NULL /* no attributes */,
2984 dispatch_thread,
2985 NULL /* no argument */);
2986 if (status != 0)
2987 {
2988 char errbuf[1024];
2989 ERROR ("network: pthread_create failed: %s",
2990 sstrerror (errno, errbuf,
2991 sizeof (errbuf)));
2992 }
2993 else
2994 {
2995 dispatch_thread_running = 1;
2996 }
2997 }
2999 if (receive_thread_running == 0)
3000 {
3001 int status;
3002 status = pthread_create (&receive_thread_id,
3003 NULL /* no attributes */,
3004 receive_thread,
3005 NULL /* no argument */);
3006 if (status != 0)
3007 {
3008 char errbuf[1024];
3009 ERROR ("network: pthread_create failed: %s",
3010 sstrerror (errno, errbuf,
3011 sizeof (errbuf)));
3012 }
3013 else
3014 {
3015 receive_thread_running = 1;
3016 }
3017 }
3019 return (0);
3020 } /* int network_init */
3022 /*
3023 * The flush option of the network plugin cannot flush individual identifiers.
3024 * All the values are added to a buffer and sent when the buffer is full, the
3025 * requested value may or may not be in there, it's not worth finding out. We
3026 * just send the buffer if `flush' is called - if the requested value was in
3027 * there, good. If not, well, then there is nothing to flush.. -octo
3028 */
3029 static int network_flush (int timeout,
3030 const char __attribute__((unused)) *identifier,
3031 user_data_t __attribute__((unused)) *user_data)
3032 {
3033 pthread_mutex_lock (&send_buffer_lock);
3035 if (send_buffer_fill > 0)
3036 flush_buffer ();
3038 pthread_mutex_unlock (&send_buffer_lock);
3040 return (0);
3041 } /* int network_flush */
3043 void module_register (void)
3044 {
3045 plugin_register_complex_config ("network", network_config);
3046 plugin_register_init ("network", network_init);
3047 plugin_register_flush ("network", network_flush,
3048 /* user_data = */ NULL);
3049 } /* void module_register */
3051 /* vim: set fdm=marker : */