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"
30 #include "utils_cache.h"
32 #include "network.h"
34 #if HAVE_PTHREAD_H
35 # include <pthread.h>
36 #endif
37 #if HAVE_SYS_SOCKET_H
38 # include <sys/socket.h>
39 #endif
40 #if HAVE_NETDB_H
41 # include <netdb.h>
42 #endif
43 #if HAVE_NETINET_IN_H
44 # include <netinet/in.h>
45 #endif
46 #if HAVE_ARPA_INET_H
47 # include <arpa/inet.h>
48 #endif
49 #if HAVE_POLL_H
50 # include <poll.h>
51 #endif
53 #if HAVE_LIBGCRYPT
54 # include <gcrypt.h>
55 #endif
57 #ifndef IPV6_ADD_MEMBERSHIP
58 # ifdef IPV6_JOIN_GROUP
59 # define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
60 # else
61 # error "Neither IP_ADD_MEMBERSHIP nor IPV6_JOIN_GROUP is defined"
62 # endif
63 #endif /* !IP_ADD_MEMBERSHIP */
65 /*
66 * Maximum size required for encryption / signing:
67 *
68 * 42 bytes for the encryption header
69 * + 64 bytes for the username
70 * -----------
71 * = 106 bytes
72 */
73 #define BUFF_SIG_SIZE 106
75 /*
76 * Private data types
77 */
78 #define SECURITY_LEVEL_NONE 0
79 #if HAVE_LIBGCRYPT
80 # define SECURITY_LEVEL_SIGN 1
81 # define SECURITY_LEVEL_ENCRYPT 2
82 #endif
83 struct sockent_client
84 {
85 int fd;
86 struct sockaddr_storage *addr;
87 socklen_t addrlen;
88 #if HAVE_LIBGCRYPT
89 int security_level;
90 char *username;
91 char *password;
92 gcry_cipher_hd_t cypher;
93 unsigned char password_hash[32];
94 #endif
95 };
97 struct sockent_server
98 {
99 int *fd;
100 size_t fd_num;
101 #if HAVE_LIBGCRYPT
102 int security_level;
103 char *auth_file;
104 fbhash_t *userdb;
105 gcry_cipher_hd_t cypher;
106 #endif
107 };
109 typedef struct sockent
110 {
111 #define SOCKENT_TYPE_CLIENT 1
112 #define SOCKENT_TYPE_SERVER 2
113 int type;
115 char *node;
116 char *service;
118 union
119 {
120 struct sockent_client client;
121 struct sockent_server server;
122 } data;
124 struct sockent *next;
125 } sockent_t;
127 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
128 * 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
129 * +-------+-----------------------+-------------------------------+
130 * ! Ver. ! ! Length !
131 * +-------+-----------------------+-------------------------------+
132 */
133 struct part_header_s
134 {
135 uint16_t type;
136 uint16_t length;
137 };
138 typedef struct part_header_s part_header_t;
140 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
141 * 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
142 * +-------------------------------+-------------------------------+
143 * ! Type ! Length !
144 * +-------------------------------+-------------------------------+
145 * : (Length - 4) Bytes :
146 * +---------------------------------------------------------------+
147 */
148 struct part_string_s
149 {
150 part_header_t *head;
151 char *value;
152 };
153 typedef struct part_string_s part_string_t;
155 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
156 * 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
157 * +-------------------------------+-------------------------------+
158 * ! Type ! Length !
159 * +-------------------------------+-------------------------------+
160 * : (Length - 4 == 2 || 4 || 8) Bytes :
161 * +---------------------------------------------------------------+
162 */
163 struct part_number_s
164 {
165 part_header_t *head;
166 uint64_t *value;
167 };
168 typedef struct part_number_s part_number_t;
170 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
171 * 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
172 * +-------------------------------+-------------------------------+
173 * ! Type ! Length !
174 * +-------------------------------+---------------+---------------+
175 * ! Num of values ! Type0 ! Type1 !
176 * +-------------------------------+---------------+---------------+
177 * ! Value0 !
178 * ! !
179 * +---------------------------------------------------------------+
180 * ! Value1 !
181 * ! !
182 * +---------------------------------------------------------------+
183 */
184 struct part_values_s
185 {
186 part_header_t *head;
187 uint16_t *num_values;
188 uint8_t *values_types;
189 value_t *values;
190 };
191 typedef struct part_values_s part_values_t;
193 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
194 * 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
195 * +-------------------------------+-------------------------------+
196 * ! Type ! Length !
197 * +-------------------------------+-------------------------------+
198 * ! Hash (Bits 0 - 31) !
199 * : : :
200 * ! Hash (Bits 224 - 255) !
201 * +---------------------------------------------------------------+
202 */
203 /* Minimum size */
204 #define PART_SIGNATURE_SHA256_SIZE 36
205 struct part_signature_sha256_s
206 {
207 part_header_t head;
208 unsigned char hash[32];
209 char *username;
210 };
211 typedef struct part_signature_sha256_s part_signature_sha256_t;
213 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
214 * 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
215 * +-------------------------------+-------------------------------+
216 * ! Type ! Length !
217 * +-------------------------------+-------------------------------+
218 * ! Original length ! Padding (0 - 15 bytes) !
219 * +-------------------------------+-------------------------------+
220 * ! Hash (Bits 0 - 31) !
221 * : : :
222 * ! Hash (Bits 128 - 159) !
223 * +---------------------------------------------------------------+
224 */
225 /* Minimum size */
226 #define PART_ENCRYPTION_AES256_SIZE 42
227 struct part_encryption_aes256_s
228 {
229 part_header_t head;
230 uint16_t username_length;
231 char *username;
232 unsigned char iv[16];
233 /* <encrypted> */
234 unsigned char hash[20];
235 /* <payload /> */
236 /* </encrypted> */
237 };
238 typedef struct part_encryption_aes256_s part_encryption_aes256_t;
240 struct receive_list_entry_s
241 {
242 char *data;
243 int data_len;
244 int fd;
245 struct receive_list_entry_s *next;
246 };
247 typedef struct receive_list_entry_s receive_list_entry_t;
249 /*
250 * Private variables
251 */
252 static int network_config_ttl = 0;
253 static size_t network_config_packet_size = 1024;
254 static int network_config_forward = 0;
256 static sockent_t *sending_sockets = NULL;
258 static receive_list_entry_t *receive_list_head = NULL;
259 static receive_list_entry_t *receive_list_tail = NULL;
260 static pthread_mutex_t receive_list_lock = PTHREAD_MUTEX_INITIALIZER;
261 static pthread_cond_t receive_list_cond = PTHREAD_COND_INITIALIZER;
263 static sockent_t *listen_sockets = NULL;
264 static struct pollfd *listen_sockets_pollfd = NULL;
265 static size_t listen_sockets_num = 0;
267 /* The receive and dispatch threads will run as long as `listen_loop' is set to
268 * zero. */
269 static int listen_loop = 0;
270 static int receive_thread_running = 0;
271 static pthread_t receive_thread_id;
272 static int dispatch_thread_running = 0;
273 static pthread_t dispatch_thread_id;
275 /* Buffer in which to-be-sent network packets are constructed. */
276 static char *send_buffer;
277 static char *send_buffer_ptr;
278 static int send_buffer_fill;
279 static value_list_t send_buffer_vl = VALUE_LIST_STATIC;
280 static pthread_mutex_t send_buffer_lock = PTHREAD_MUTEX_INITIALIZER;
282 /*
283 * Private functions
284 */
285 static _Bool check_receive_okay (const value_list_t *vl) /* {{{ */
286 {
287 uint64_t time_sent = 0;
288 int status;
290 status = uc_meta_data_get_unsigned_int (vl,
291 "network:time_sent", &time_sent);
293 /* This is a value we already sent. Don't allow it to be received again in
294 * order to avoid looping. */
295 if ((status == 0) && (time_sent >= ((uint64_t) vl->time)))
296 return (false);
298 return (true);
299 } /* }}} _Bool check_receive_okay */
301 static _Bool check_send_okay (const value_list_t *vl) /* {{{ */
302 {
303 _Bool received = false;
304 int status;
306 if (network_config_forward != 0)
307 return (true);
309 if (vl->meta == NULL)
310 return (true);
312 status = meta_data_get_boolean (vl->meta, "network:received", &received);
313 if (status == -ENOENT)
314 return (true);
315 else if (status != 0)
316 {
317 ERROR ("network plugin: check_send_okay: meta_data_get_boolean failed "
318 "with status %i.", status);
319 return (true);
320 }
322 /* By default, only *send* value lists that were not *received* by the
323 * network plugin. */
324 return (!received);
325 } /* }}} _Bool check_send_okay */
327 static int network_dispatch_values (value_list_t *vl) /* {{{ */
328 {
329 int status;
331 if ((vl->time <= 0)
332 || (strlen (vl->host) <= 0)
333 || (strlen (vl->plugin) <= 0)
334 || (strlen (vl->type) <= 0))
335 return (-EINVAL);
337 if (!check_receive_okay (vl))
338 {
339 #if COLLECT_DEBUG
340 char name[6*DATA_MAX_NAME_LEN];
341 FORMAT_VL (name, sizeof (name), vl);
342 name[sizeof (name) - 1] = 0;
343 DEBUG ("network plugin: network_dispatch_values: "
344 "NOT dispatching %s.", name);
345 #endif
346 return (0);
347 }
349 assert (vl->meta == NULL);
351 vl->meta = meta_data_create ();
352 if (vl->meta == NULL)
353 {
354 ERROR ("network plugin: meta_data_create failed.");
355 return (-ENOMEM);
356 }
358 status = meta_data_add_boolean (vl->meta, "network:received", true);
359 if (status != 0)
360 {
361 ERROR ("network plugin: meta_data_add_boolean failed.");
362 meta_data_destroy (vl->meta);
363 vl->meta = NULL;
364 return (status);
365 }
367 plugin_dispatch_values (vl);
369 meta_data_destroy (vl->meta);
370 vl->meta = NULL;
372 return (0);
373 } /* }}} int network_dispatch_values */
375 #if HAVE_LIBGCRYPT
376 static gcry_cipher_hd_t network_get_aes256_cypher (sockent_t *se, /* {{{ */
377 const void *iv, size_t iv_size, const char *username)
378 {
379 gcry_error_t err;
380 gcry_cipher_hd_t *cyper_ptr;
381 unsigned char password_hash[32];
383 if (se->type == SOCKENT_TYPE_CLIENT)
384 {
385 cyper_ptr = &se->data.client.cypher;
386 memcpy (password_hash, se->data.client.password_hash,
387 sizeof (password_hash));
388 }
389 else
390 {
391 char *secret;
393 cyper_ptr = &se->data.server.cypher;
395 if (username == NULL)
396 return (NULL);
398 secret = fbh_get (se->data.server.userdb, username);
399 if (secret == NULL)
400 return (NULL);
402 gcry_md_hash_buffer (GCRY_MD_SHA256,
403 password_hash,
404 secret, strlen (secret));
406 sfree (secret);
407 }
409 if (*cyper_ptr == NULL)
410 {
411 err = gcry_cipher_open (cyper_ptr,
412 GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_OFB, /* flags = */ 0);
413 if (err != 0)
414 {
415 ERROR ("network plugin: gcry_cipher_open returned: %s",
416 gcry_strerror (err));
417 *cyper_ptr = NULL;
418 return (NULL);
419 }
420 }
421 else
422 {
423 gcry_cipher_reset (*cyper_ptr);
424 }
425 assert (*cyper_ptr != NULL);
427 err = gcry_cipher_setkey (*cyper_ptr,
428 password_hash, sizeof (password_hash));
429 if (err != 0)
430 {
431 ERROR ("network plugin: gcry_cipher_setkey returned: %s",
432 gcry_strerror (err));
433 gcry_cipher_close (*cyper_ptr);
434 *cyper_ptr = NULL;
435 return (NULL);
436 }
438 err = gcry_cipher_setiv (*cyper_ptr, iv, iv_size);
439 if (err != 0)
440 {
441 ERROR ("network plugin: gcry_cipher_setkey returned: %s",
442 gcry_strerror (err));
443 gcry_cipher_close (*cyper_ptr);
444 *cyper_ptr = NULL;
445 return (NULL);
446 }
448 return (*cyper_ptr);
449 } /* }}} int network_get_aes256_cypher */
450 #endif /* HAVE_LIBGCRYPT */
452 static int write_part_values (char **ret_buffer, int *ret_buffer_len,
453 const data_set_t *ds, const value_list_t *vl)
454 {
455 char *packet_ptr;
456 int packet_len;
457 int num_values;
459 part_header_t pkg_ph;
460 uint16_t pkg_num_values;
461 uint8_t *pkg_values_types;
462 value_t *pkg_values;
464 int offset;
465 int i;
467 num_values = vl->values_len;
468 packet_len = sizeof (part_header_t) + sizeof (uint16_t)
469 + (num_values * sizeof (uint8_t))
470 + (num_values * sizeof (value_t));
472 if (*ret_buffer_len < packet_len)
473 return (-1);
475 pkg_values_types = (uint8_t *) malloc (num_values * sizeof (uint8_t));
476 if (pkg_values_types == NULL)
477 {
478 ERROR ("network plugin: write_part_values: malloc failed.");
479 return (-1);
480 }
482 pkg_values = (value_t *) malloc (num_values * sizeof (value_t));
483 if (pkg_values == NULL)
484 {
485 free (pkg_values_types);
486 ERROR ("network plugin: write_part_values: malloc failed.");
487 return (-1);
488 }
490 pkg_ph.type = htons (TYPE_VALUES);
491 pkg_ph.length = htons (packet_len);
493 pkg_num_values = htons ((uint16_t) vl->values_len);
495 for (i = 0; i < num_values; i++)
496 {
497 pkg_values_types[i] = (uint8_t) ds->ds[i].type;
498 switch (ds->ds[i].type)
499 {
500 case DS_TYPE_COUNTER:
501 pkg_values[i].counter = htonll (vl->values[i].counter);
502 break;
504 case DS_TYPE_GAUGE:
505 pkg_values[i].gauge = htond (vl->values[i].gauge);
506 break;
508 case DS_TYPE_DERIVE:
509 pkg_values[i].derive = htonll (vl->values[i].derive);
510 break;
512 case DS_TYPE_ABSOLUTE:
513 pkg_values[i].absolute = htonll (vl->values[i].absolute);
514 break;
516 default:
517 free (pkg_values_types);
518 free (pkg_values);
519 ERROR ("network plugin: write_part_values: "
520 "Unknown data source type: %i",
521 ds->ds[i].type);
522 return (-1);
523 } /* switch (ds->ds[i].type) */
524 } /* for (num_values) */
526 /*
527 * Use `memcpy' to write everything to the buffer, because the pointer
528 * may be unaligned and some architectures, such as SPARC, can't handle
529 * that.
530 */
531 packet_ptr = *ret_buffer;
532 offset = 0;
533 memcpy (packet_ptr + offset, &pkg_ph, sizeof (pkg_ph));
534 offset += sizeof (pkg_ph);
535 memcpy (packet_ptr + offset, &pkg_num_values, sizeof (pkg_num_values));
536 offset += sizeof (pkg_num_values);
537 memcpy (packet_ptr + offset, pkg_values_types, num_values * sizeof (uint8_t));
538 offset += num_values * sizeof (uint8_t);
539 memcpy (packet_ptr + offset, pkg_values, num_values * sizeof (value_t));
540 offset += num_values * sizeof (value_t);
542 assert (offset == packet_len);
544 *ret_buffer = packet_ptr + packet_len;
545 *ret_buffer_len -= packet_len;
547 free (pkg_values_types);
548 free (pkg_values);
550 return (0);
551 } /* int write_part_values */
553 static int write_part_number (char **ret_buffer, int *ret_buffer_len,
554 int type, uint64_t value)
555 {
556 char *packet_ptr;
557 int packet_len;
559 part_header_t pkg_head;
560 uint64_t pkg_value;
562 int offset;
564 packet_len = sizeof (pkg_head) + sizeof (pkg_value);
566 if (*ret_buffer_len < packet_len)
567 return (-1);
569 pkg_head.type = htons (type);
570 pkg_head.length = htons (packet_len);
571 pkg_value = htonll (value);
573 packet_ptr = *ret_buffer;
574 offset = 0;
575 memcpy (packet_ptr + offset, &pkg_head, sizeof (pkg_head));
576 offset += sizeof (pkg_head);
577 memcpy (packet_ptr + offset, &pkg_value, sizeof (pkg_value));
578 offset += sizeof (pkg_value);
580 assert (offset == packet_len);
582 *ret_buffer = packet_ptr + packet_len;
583 *ret_buffer_len -= packet_len;
585 return (0);
586 } /* int write_part_number */
588 static int write_part_string (char **ret_buffer, int *ret_buffer_len,
589 int type, const char *str, int str_len)
590 {
591 char *buffer;
592 int buffer_len;
594 uint16_t pkg_type;
595 uint16_t pkg_length;
597 int offset;
599 buffer_len = 2 * sizeof (uint16_t) + str_len + 1;
600 if (*ret_buffer_len < buffer_len)
601 return (-1);
603 pkg_type = htons (type);
604 pkg_length = htons (buffer_len);
606 buffer = *ret_buffer;
607 offset = 0;
608 memcpy (buffer + offset, (void *) &pkg_type, sizeof (pkg_type));
609 offset += sizeof (pkg_type);
610 memcpy (buffer + offset, (void *) &pkg_length, sizeof (pkg_length));
611 offset += sizeof (pkg_length);
612 memcpy (buffer + offset, str, str_len);
613 offset += str_len;
614 memset (buffer + offset, '\0', 1);
615 offset += 1;
617 assert (offset == buffer_len);
619 *ret_buffer = buffer + buffer_len;
620 *ret_buffer_len -= buffer_len;
622 return (0);
623 } /* int write_part_string */
625 static int parse_part_values (void **ret_buffer, size_t *ret_buffer_len,
626 value_t **ret_values, int *ret_num_values)
627 {
628 char *buffer = *ret_buffer;
629 size_t buffer_len = *ret_buffer_len;
631 uint16_t tmp16;
632 size_t exp_size;
633 int i;
635 uint16_t pkg_length;
636 uint16_t pkg_type;
637 uint16_t pkg_numval;
639 uint8_t *pkg_types;
640 value_t *pkg_values;
642 if (buffer_len < 15)
643 {
644 NOTICE ("network plugin: packet is too short: "
645 "buffer_len = %zu", buffer_len);
646 return (-1);
647 }
649 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
650 buffer += sizeof (tmp16);
651 pkg_type = ntohs (tmp16);
653 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
654 buffer += sizeof (tmp16);
655 pkg_length = ntohs (tmp16);
657 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
658 buffer += sizeof (tmp16);
659 pkg_numval = ntohs (tmp16);
661 assert (pkg_type == TYPE_VALUES);
663 exp_size = 3 * sizeof (uint16_t)
664 + pkg_numval * (sizeof (uint8_t) + sizeof (value_t));
665 if ((buffer_len < 0) || (buffer_len < exp_size))
666 {
667 WARNING ("network plugin: parse_part_values: "
668 "Packet too short: "
669 "Chunk of size %zu expected, "
670 "but buffer has only %zu bytes left.",
671 exp_size, buffer_len);
672 return (-1);
673 }
675 if (pkg_length != exp_size)
676 {
677 WARNING ("network plugin: parse_part_values: "
678 "Length and number of values "
679 "in the packet don't match.");
680 return (-1);
681 }
683 pkg_types = (uint8_t *) malloc (pkg_numval * sizeof (uint8_t));
684 pkg_values = (value_t *) malloc (pkg_numval * sizeof (value_t));
685 if ((pkg_types == NULL) || (pkg_values == NULL))
686 {
687 sfree (pkg_types);
688 sfree (pkg_values);
689 ERROR ("network plugin: parse_part_values: malloc failed.");
690 return (-1);
691 }
693 memcpy ((void *) pkg_types, (void *) buffer, pkg_numval * sizeof (uint8_t));
694 buffer += pkg_numval * sizeof (uint8_t);
695 memcpy ((void *) pkg_values, (void *) buffer, pkg_numval * sizeof (value_t));
696 buffer += pkg_numval * sizeof (value_t);
698 for (i = 0; i < pkg_numval; i++)
699 {
700 switch (pkg_types[i])
701 {
702 case DS_TYPE_COUNTER:
703 pkg_values[i].counter = (counter_t) ntohll (pkg_values[i].counter);
704 break;
706 case DS_TYPE_GAUGE:
707 pkg_values[i].gauge = (gauge_t) ntohd (pkg_values[i].gauge);
708 break;
710 case DS_TYPE_DERIVE:
711 pkg_values[i].derive = (derive_t) ntohll (pkg_values[i].derive);
712 break;
714 case DS_TYPE_ABSOLUTE:
715 pkg_values[i].absolute = (absolute_t) ntohll (pkg_values[i].absolute);
716 break;
718 default:
719 sfree (pkg_types);
720 sfree (pkg_values);
721 NOTICE ("network plugin: parse_part_values: "
722 "Don't know how to handle data source type %"PRIu8,
723 pkg_types[i]);
724 return (-1);
725 } /* switch (pkg_types[i]) */
726 }
728 *ret_buffer = buffer;
729 *ret_buffer_len = buffer_len - pkg_length;
730 *ret_num_values = pkg_numval;
731 *ret_values = pkg_values;
733 sfree (pkg_types);
735 return (0);
736 } /* int parse_part_values */
738 static int parse_part_number (void **ret_buffer, size_t *ret_buffer_len,
739 uint64_t *value)
740 {
741 char *buffer = *ret_buffer;
742 size_t buffer_len = *ret_buffer_len;
744 uint16_t tmp16;
745 uint64_t tmp64;
746 size_t exp_size = 2 * sizeof (uint16_t) + sizeof (uint64_t);
748 uint16_t pkg_length;
749 uint16_t pkg_type;
751 if ((buffer_len < 0) || ((size_t) buffer_len < exp_size))
752 {
753 WARNING ("network plugin: parse_part_number: "
754 "Packet too short: "
755 "Chunk of size %zu expected, "
756 "but buffer has only %zu bytes left.",
757 exp_size, buffer_len);
758 return (-1);
759 }
761 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
762 buffer += sizeof (tmp16);
763 pkg_type = ntohs (tmp16);
765 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
766 buffer += sizeof (tmp16);
767 pkg_length = ntohs (tmp16);
769 memcpy ((void *) &tmp64, buffer, sizeof (tmp64));
770 buffer += sizeof (tmp64);
771 *value = ntohll (tmp64);
773 *ret_buffer = buffer;
774 *ret_buffer_len = buffer_len - pkg_length;
776 return (0);
777 } /* int parse_part_number */
779 static int parse_part_string (void **ret_buffer, size_t *ret_buffer_len,
780 char *output, int output_len)
781 {
782 char *buffer = *ret_buffer;
783 size_t buffer_len = *ret_buffer_len;
785 uint16_t tmp16;
786 size_t header_size = 2 * sizeof (uint16_t);
788 uint16_t pkg_length;
789 uint16_t pkg_type;
791 if ((buffer_len < 0) || (buffer_len < header_size))
792 {
793 WARNING ("network plugin: parse_part_string: "
794 "Packet too short: "
795 "Chunk of at least size %zu expected, "
796 "but buffer has only %zu bytes left.",
797 header_size, buffer_len);
798 return (-1);
799 }
801 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
802 buffer += sizeof (tmp16);
803 pkg_type = ntohs (tmp16);
805 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
806 buffer += sizeof (tmp16);
807 pkg_length = ntohs (tmp16);
809 /* Check that packet fits in the input buffer */
810 if (pkg_length > buffer_len)
811 {
812 WARNING ("network plugin: parse_part_string: "
813 "Packet too big: "
814 "Chunk of size %"PRIu16" received, "
815 "but buffer has only %zu bytes left.",
816 pkg_length, buffer_len);
817 return (-1);
818 }
820 /* Check that pkg_length is in the valid range */
821 if (pkg_length <= header_size)
822 {
823 WARNING ("network plugin: parse_part_string: "
824 "Packet too short: "
825 "Header claims this packet is only %hu "
826 "bytes long.", pkg_length);
827 return (-1);
828 }
830 /* Check that the package data fits into the output buffer.
831 * The previous if-statement ensures that:
832 * `pkg_length > header_size' */
833 if ((output_len < 0)
834 || ((size_t) output_len < ((size_t) pkg_length - header_size)))
835 {
836 WARNING ("network plugin: parse_part_string: "
837 "Output buffer too small.");
838 return (-1);
839 }
841 /* All sanity checks successfull, let's copy the data over */
842 output_len = pkg_length - header_size;
843 memcpy ((void *) output, (void *) buffer, output_len);
844 buffer += output_len;
846 /* For some very weird reason '\0' doesn't do the trick on SPARC in
847 * this statement. */
848 if (output[output_len - 1] != 0)
849 {
850 WARNING ("network plugin: parse_part_string: "
851 "Received string does not end "
852 "with a NULL-byte.");
853 return (-1);
854 }
856 *ret_buffer = buffer;
857 *ret_buffer_len = buffer_len - pkg_length;
859 return (0);
860 } /* int parse_part_string */
862 /* Forward declaration: parse_part_sign_sha256 and parse_part_encr_aes256 call
863 * parse_packet and vice versa. */
864 #define PP_SIGNED 0x01
865 #define PP_ENCRYPTED 0x02
866 static int parse_packet (sockent_t *se,
867 void *buffer, size_t buffer_size, int flags);
869 #define BUFFER_READ(p,s) do { \
870 memcpy ((p), buffer + buffer_offset, (s)); \
871 buffer_offset += (s); \
872 } while (0)
874 #if HAVE_LIBGCRYPT
875 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
876 void **ret_buffer, size_t *ret_buffer_len, int flags)
877 {
878 char *buffer;
879 size_t buffer_len;
880 size_t buffer_offset;
882 size_t username_len;
883 char *secret;
885 part_signature_sha256_t pss;
886 uint16_t pss_head_length;
887 char hash[sizeof (pss.hash)];
889 gcry_md_hd_t hd;
890 gcry_error_t err;
891 unsigned char *hash_ptr;
893 buffer = *ret_buffer;
894 buffer_len = *ret_buffer_len;
895 buffer_offset = 0;
897 if (se->data.server.userdb == NULL)
898 {
899 NOTICE ("network plugin: Received signed network packet but can't verify "
900 "it because no user DB has been configured. Will accept it.");
901 return (0);
902 }
904 /* Check if the buffer has enough data for this structure. */
905 if (buffer_len <= PART_SIGNATURE_SHA256_SIZE)
906 return (-ENOMEM);
908 /* Read type and length header */
909 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
910 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
911 pss_head_length = ntohs (pss.head.length);
913 /* Check if the `pss_head_length' is within bounds. */
914 if ((pss_head_length <= PART_SIGNATURE_SHA256_SIZE)
915 || (pss_head_length > buffer_len))
916 {
917 ERROR ("network plugin: HMAC-SHA-256 with invalid length received.");
918 return (-1);
919 }
921 /* Copy the hash. */
922 BUFFER_READ (pss.hash, sizeof (pss.hash));
924 /* Calculate username length (without null byte) and allocate memory */
925 username_len = pss_head_length - PART_SIGNATURE_SHA256_SIZE;
926 pss.username = malloc (username_len + 1);
927 if (pss.username == NULL)
928 return (-ENOMEM);
930 /* Read the username */
931 BUFFER_READ (pss.username, username_len);
932 pss.username[username_len] = 0;
934 assert (buffer_offset == pss_head_length);
936 /* Query the password */
937 secret = fbh_get (se->data.server.userdb, pss.username);
938 if (secret == NULL)
939 {
940 ERROR ("network plugin: Unknown user: %s", pss.username);
941 sfree (pss.username);
942 return (-ENOENT);
943 }
945 /* Create a hash device and check the HMAC */
946 hd = NULL;
947 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
948 if (err != 0)
949 {
950 ERROR ("network plugin: Creating HMAC-SHA-256 object failed: %s",
951 gcry_strerror (err));
952 sfree (secret);
953 sfree (pss.username);
954 return (-1);
955 }
957 err = gcry_md_setkey (hd, secret, strlen (secret));
958 if (err != 0)
959 {
960 ERROR ("network plugin: gcry_md_setkey failed: %s", gcry_strerror (err));
961 gcry_md_close (hd);
962 return (-1);
963 }
965 gcry_md_write (hd,
966 buffer + PART_SIGNATURE_SHA256_SIZE,
967 buffer_len - PART_SIGNATURE_SHA256_SIZE);
968 hash_ptr = gcry_md_read (hd, GCRY_MD_SHA256);
969 if (hash_ptr == NULL)
970 {
971 ERROR ("network plugin: gcry_md_read failed.");
972 gcry_md_close (hd);
973 sfree (secret);
974 sfree (pss.username);
975 return (-1);
976 }
977 memcpy (hash, hash_ptr, sizeof (hash));
979 /* Clean up */
980 gcry_md_close (hd);
981 hd = NULL;
983 sfree (secret);
984 sfree (pss.username);
986 if (memcmp (pss.hash, hash, sizeof (pss.hash)) != 0)
987 {
988 WARNING ("network plugin: Verifying HMAC-SHA-256 signature failed: "
989 "Hash mismatch.");
990 }
991 else
992 {
993 parse_packet (se, buffer + buffer_offset, buffer_len - buffer_offset,
994 flags | PP_SIGNED);
995 }
997 *ret_buffer = buffer + buffer_len;
998 *ret_buffer_len = 0;
1000 return (0);
1001 } /* }}} int parse_part_sign_sha256 */
1002 /* #endif HAVE_LIBGCRYPT */
1004 #else /* if !HAVE_LIBGCRYPT */
1005 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
1006 void **ret_buffer, size_t *ret_buffer_size, int flags)
1007 {
1008 static int warning_has_been_printed = 0;
1010 char *buffer;
1011 size_t buffer_size;
1012 size_t buffer_offset;
1013 uint16_t part_len;
1015 part_signature_sha256_t pss;
1017 buffer = *ret_buffer;
1018 buffer_size = *ret_buffer_size;
1019 buffer_offset = 0;
1021 if (buffer_size <= PART_SIGNATURE_SHA256_SIZE)
1022 return (-ENOMEM);
1024 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
1025 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
1026 part_len = ntohs (pss.head.length);
1028 if ((part_len <= PART_SIGNATURE_SHA256_SIZE)
1029 || (part_len > buffer_size))
1030 return (-EINVAL);
1032 if (warning_has_been_printed == 0)
1033 {
1034 WARNING ("network plugin: Received signed packet, but the network "
1035 "plugin was not linked with libgcrypt, so I cannot "
1036 "verify the signature. The packet will be accepted.");
1037 warning_has_been_printed = 1;
1038 }
1040 parse_packet (se, buffer + part_len, buffer_size - part_len, flags);
1042 *ret_buffer = buffer + buffer_size;
1043 *ret_buffer_size = 0;
1045 return (0);
1046 } /* }}} int parse_part_sign_sha256 */
1047 #endif /* !HAVE_LIBGCRYPT */
1049 #if HAVE_LIBGCRYPT
1050 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1051 void **ret_buffer, size_t *ret_buffer_len,
1052 int flags)
1053 {
1054 char *buffer = *ret_buffer;
1055 size_t buffer_len = *ret_buffer_len;
1056 size_t payload_len;
1057 size_t part_size;
1058 size_t buffer_offset;
1059 uint16_t username_len;
1060 part_encryption_aes256_t pea;
1061 unsigned char hash[sizeof (pea.hash)];
1063 gcry_cipher_hd_t cypher;
1064 gcry_error_t err;
1066 /* Make sure at least the header if available. */
1067 if (buffer_len <= PART_ENCRYPTION_AES256_SIZE)
1068 {
1069 NOTICE ("network plugin: parse_part_encr_aes256: "
1070 "Discarding short packet.");
1071 return (-1);
1072 }
1074 buffer_offset = 0;
1076 /* Copy the unencrypted information into `pea'. */
1077 BUFFER_READ (&pea.head.type, sizeof (pea.head.type));
1078 BUFFER_READ (&pea.head.length, sizeof (pea.head.length));
1080 /* Check the `part size'. */
1081 part_size = ntohs (pea.head.length);
1082 if ((part_size <= PART_ENCRYPTION_AES256_SIZE)
1083 || (part_size > buffer_len))
1084 {
1085 NOTICE ("network plugin: parse_part_encr_aes256: "
1086 "Discarding part with invalid size.");
1087 return (-1);
1088 }
1090 /* Read the username */
1091 BUFFER_READ (&username_len, sizeof (username_len));
1092 username_len = ntohs (username_len);
1094 if ((username_len <= 0)
1095 || (username_len > (part_size - (PART_ENCRYPTION_AES256_SIZE + 1))))
1096 {
1097 NOTICE ("network plugin: parse_part_encr_aes256: "
1098 "Discarding part with invalid username length.");
1099 return (-1);
1100 }
1102 assert (username_len > 0);
1103 pea.username = malloc (username_len + 1);
1104 if (pea.username == NULL)
1105 return (-ENOMEM);
1106 BUFFER_READ (pea.username, username_len);
1107 pea.username[username_len] = 0;
1109 /* Last but not least, the initialization vector */
1110 BUFFER_READ (pea.iv, sizeof (pea.iv));
1112 /* Make sure we are at the right position */
1113 assert (buffer_offset == (username_len +
1114 PART_ENCRYPTION_AES256_SIZE - sizeof (pea.hash)));
1116 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
1117 pea.username);
1118 if (cypher == NULL)
1119 return (-1);
1121 payload_len = part_size - (PART_ENCRYPTION_AES256_SIZE + username_len);
1122 assert (payload_len > 0);
1124 /* Decrypt the packet in-place */
1125 err = gcry_cipher_decrypt (cypher,
1126 buffer + buffer_offset,
1127 part_size - buffer_offset,
1128 /* in = */ NULL, /* in len = */ 0);
1129 if (err != 0)
1130 {
1131 ERROR ("network plugin: gcry_cipher_decrypt returned: %s",
1132 gcry_strerror (err));
1133 return (-1);
1134 }
1136 /* Read the hash */
1137 BUFFER_READ (pea.hash, sizeof (pea.hash));
1139 /* Make sure we're at the right position - again */
1140 assert (buffer_offset == (username_len + PART_ENCRYPTION_AES256_SIZE));
1141 assert (buffer_offset == (part_size - payload_len));
1143 /* Check hash sum */
1144 memset (hash, 0, sizeof (hash));
1145 gcry_md_hash_buffer (GCRY_MD_SHA1, hash,
1146 buffer + buffer_offset, payload_len);
1147 if (memcmp (hash, pea.hash, sizeof (hash)) != 0)
1148 {
1149 ERROR ("network plugin: Decryption failed: Checksum mismatch.");
1150 return (-1);
1151 }
1153 parse_packet (se, buffer + buffer_offset, payload_len,
1154 flags | PP_ENCRYPTED);
1156 /* Update return values */
1157 *ret_buffer = buffer + part_size;
1158 *ret_buffer_len = buffer_len - part_size;
1160 return (0);
1161 } /* }}} int parse_part_encr_aes256 */
1162 /* #endif HAVE_LIBGCRYPT */
1164 #else /* if !HAVE_LIBGCRYPT */
1165 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1166 void **ret_buffer, size_t *ret_buffer_size, int flags)
1167 {
1168 static int warning_has_been_printed = 0;
1170 char *buffer;
1171 size_t buffer_size;
1172 size_t buffer_offset;
1174 part_header_t ph;
1175 size_t ph_length;
1177 buffer = *ret_buffer;
1178 buffer_size = *ret_buffer_size;
1179 buffer_offset = 0;
1181 /* parse_packet assures this minimum size. */
1182 assert (buffer_size >= (sizeof (ph.type) + sizeof (ph.length)));
1184 BUFFER_READ (&ph.type, sizeof (ph.type));
1185 BUFFER_READ (&ph.length, sizeof (ph.length));
1186 ph_length = ntohs (ph.length);
1188 if ((ph_length <= PART_ENCRYPTION_AES256_SIZE)
1189 || (ph_length > buffer_size))
1190 {
1191 ERROR ("network plugin: AES-256 encrypted part "
1192 "with invalid length received.");
1193 return (-1);
1194 }
1196 if (warning_has_been_printed == 0)
1197 {
1198 WARNING ("network plugin: Received encrypted packet, but the network "
1199 "plugin was not linked with libgcrypt, so I cannot "
1200 "decrypt it. The part will be discarded.");
1201 warning_has_been_printed = 1;
1202 }
1204 *ret_buffer += ph_length;
1205 *ret_buffer_size -= ph_length;
1207 return (0);
1208 } /* }}} int parse_part_encr_aes256 */
1209 #endif /* !HAVE_LIBGCRYPT */
1211 #undef BUFFER_READ
1213 static int parse_packet (sockent_t *se, /* {{{ */
1214 void *buffer, size_t buffer_size, int flags)
1215 {
1216 int status;
1218 value_list_t vl = VALUE_LIST_INIT;
1219 notification_t n;
1221 #if HAVE_LIBGCRYPT
1222 int packet_was_signed = (flags & PP_SIGNED);
1223 int packet_was_encrypted = (flags & PP_ENCRYPTED);
1224 int printed_ignore_warning = 0;
1225 #endif /* HAVE_LIBGCRYPT */
1228 memset (&vl, '\0', sizeof (vl));
1229 memset (&n, '\0', sizeof (n));
1230 status = 0;
1232 while ((status == 0) && (0 < buffer_size)
1233 && ((unsigned int) buffer_size > sizeof (part_header_t)))
1234 {
1235 uint16_t pkg_length;
1236 uint16_t pkg_type;
1238 memcpy ((void *) &pkg_type,
1239 (void *) buffer,
1240 sizeof (pkg_type));
1241 memcpy ((void *) &pkg_length,
1242 (void *) (buffer + sizeof (pkg_type)),
1243 sizeof (pkg_length));
1245 pkg_length = ntohs (pkg_length);
1246 pkg_type = ntohs (pkg_type);
1248 if (pkg_length > buffer_size)
1249 break;
1250 /* Ensure that this loop terminates eventually */
1251 if (pkg_length < (2 * sizeof (uint16_t)))
1252 break;
1254 if (pkg_type == TYPE_ENCR_AES256)
1255 {
1256 status = parse_part_encr_aes256 (se,
1257 &buffer, &buffer_size, flags);
1258 if (status != 0)
1259 {
1260 ERROR ("network plugin: Decrypting AES256 "
1261 "part failed "
1262 "with status %i.", status);
1263 break;
1264 }
1265 }
1266 #if HAVE_LIBGCRYPT
1267 else if ((se->data.server.security_level == SECURITY_LEVEL_ENCRYPT)
1268 && (packet_was_encrypted == 0))
1269 {
1270 if (printed_ignore_warning == 0)
1271 {
1272 INFO ("network plugin: Unencrypted packet or "
1273 "part has been ignored.");
1274 printed_ignore_warning = 1;
1275 }
1276 buffer = ((char *) buffer) + pkg_length;
1277 continue;
1278 }
1279 #endif /* HAVE_LIBGCRYPT */
1280 else if (pkg_type == TYPE_SIGN_SHA256)
1281 {
1282 status = parse_part_sign_sha256 (se,
1283 &buffer, &buffer_size, flags);
1284 if (status != 0)
1285 {
1286 ERROR ("network plugin: Verifying HMAC-SHA-256 "
1287 "signature failed "
1288 "with status %i.", status);
1289 break;
1290 }
1291 }
1292 #if HAVE_LIBGCRYPT
1293 else if ((se->data.server.security_level == SECURITY_LEVEL_SIGN)
1294 && (packet_was_encrypted == 0)
1295 && (packet_was_signed == 0))
1296 {
1297 if (printed_ignore_warning == 0)
1298 {
1299 INFO ("network plugin: Unsigned packet or "
1300 "part has been ignored.");
1301 printed_ignore_warning = 1;
1302 }
1303 buffer = ((char *) buffer) + pkg_length;
1304 continue;
1305 }
1306 #endif /* HAVE_LIBGCRYPT */
1307 else if (pkg_type == TYPE_VALUES)
1308 {
1309 status = parse_part_values (&buffer, &buffer_size,
1310 &vl.values, &vl.values_len);
1311 if (status != 0)
1312 break;
1314 network_dispatch_values (&vl);
1316 sfree (vl.values);
1317 }
1318 else if (pkg_type == TYPE_TIME)
1319 {
1320 uint64_t tmp = 0;
1321 status = parse_part_number (&buffer, &buffer_size,
1322 &tmp);
1323 if (status == 0)
1324 {
1325 vl.time = (time_t) tmp;
1326 n.time = (time_t) tmp;
1327 }
1328 }
1329 else if (pkg_type == TYPE_INTERVAL)
1330 {
1331 uint64_t tmp = 0;
1332 status = parse_part_number (&buffer, &buffer_size,
1333 &tmp);
1334 if (status == 0)
1335 vl.interval = (int) tmp;
1336 }
1337 else if (pkg_type == TYPE_HOST)
1338 {
1339 status = parse_part_string (&buffer, &buffer_size,
1340 vl.host, sizeof (vl.host));
1341 if (status == 0)
1342 sstrncpy (n.host, vl.host, sizeof (n.host));
1343 }
1344 else if (pkg_type == TYPE_PLUGIN)
1345 {
1346 status = parse_part_string (&buffer, &buffer_size,
1347 vl.plugin, sizeof (vl.plugin));
1348 if (status == 0)
1349 sstrncpy (n.plugin, vl.plugin,
1350 sizeof (n.plugin));
1351 }
1352 else if (pkg_type == TYPE_PLUGIN_INSTANCE)
1353 {
1354 status = parse_part_string (&buffer, &buffer_size,
1355 vl.plugin_instance,
1356 sizeof (vl.plugin_instance));
1357 if (status == 0)
1358 sstrncpy (n.plugin_instance,
1359 vl.plugin_instance,
1360 sizeof (n.plugin_instance));
1361 }
1362 else if (pkg_type == TYPE_TYPE)
1363 {
1364 status = parse_part_string (&buffer, &buffer_size,
1365 vl.type, sizeof (vl.type));
1366 if (status == 0)
1367 sstrncpy (n.type, vl.type, sizeof (n.type));
1368 }
1369 else if (pkg_type == TYPE_TYPE_INSTANCE)
1370 {
1371 status = parse_part_string (&buffer, &buffer_size,
1372 vl.type_instance,
1373 sizeof (vl.type_instance));
1374 if (status == 0)
1375 sstrncpy (n.type_instance, vl.type_instance,
1376 sizeof (n.type_instance));
1377 }
1378 else if (pkg_type == TYPE_MESSAGE)
1379 {
1380 status = parse_part_string (&buffer, &buffer_size,
1381 n.message, sizeof (n.message));
1383 if (status != 0)
1384 {
1385 /* do nothing */
1386 }
1387 else if ((n.severity != NOTIF_FAILURE)
1388 && (n.severity != NOTIF_WARNING)
1389 && (n.severity != NOTIF_OKAY))
1390 {
1391 INFO ("network plugin: "
1392 "Ignoring notification with "
1393 "unknown severity %i.",
1394 n.severity);
1395 }
1396 else if (n.time <= 0)
1397 {
1398 INFO ("network plugin: "
1399 "Ignoring notification with "
1400 "time == 0.");
1401 }
1402 else if (strlen (n.message) <= 0)
1403 {
1404 INFO ("network plugin: "
1405 "Ignoring notification with "
1406 "an empty message.");
1407 }
1408 else
1409 {
1410 plugin_dispatch_notification (&n);
1411 }
1412 }
1413 else if (pkg_type == TYPE_SEVERITY)
1414 {
1415 uint64_t tmp = 0;
1416 status = parse_part_number (&buffer, &buffer_size,
1417 &tmp);
1418 if (status == 0)
1419 n.severity = (int) tmp;
1420 }
1421 else
1422 {
1423 DEBUG ("network plugin: parse_packet: Unknown part"
1424 " type: 0x%04hx", pkg_type);
1425 buffer = ((char *) buffer) + pkg_length;
1426 }
1427 } /* while (buffer_size > sizeof (part_header_t)) */
1429 if (status == 0 && buffer_size > 0)
1430 WARNING ("network plugin: parse_packet: Received truncated "
1431 "packet, try increasing `MaxPacketSize'");
1433 return (status);
1434 } /* }}} int parse_packet */
1436 static void free_sockent_client (struct sockent_client *sec) /* {{{ */
1437 {
1438 if (sec->fd >= 0)
1439 {
1440 close (sec->fd);
1441 sec->fd = -1;
1442 }
1443 sfree (sec->addr);
1444 #if HAVE_LIBGCRYPT
1445 sfree (sec->username);
1446 sfree (sec->password);
1447 if (sec->cypher != NULL)
1448 gcry_cipher_close (sec->cypher);
1449 #endif
1450 } /* }}} void free_sockent_client */
1452 static void free_sockent_server (struct sockent_server *ses) /* {{{ */
1453 {
1454 size_t i;
1456 for (i = 0; i < ses->fd_num; i++)
1457 {
1458 if (ses->fd[i] >= 0)
1459 {
1460 close (ses->fd[i]);
1461 ses->fd[i] = -1;
1462 }
1463 }
1465 sfree (ses->fd);
1466 #if HAVE_LIBGCRYPT
1467 sfree (ses->auth_file);
1468 fbh_destroy (ses->userdb);
1469 if (ses->cypher != NULL)
1470 gcry_cipher_close (ses->cypher);
1471 #endif
1472 } /* }}} void free_sockent_server */
1474 static void sockent_destroy (sockent_t *se) /* {{{ */
1475 {
1476 sockent_t *next;
1478 DEBUG ("network plugin: sockent_destroy (se = %p);", (void *) se);
1480 while (se != NULL)
1481 {
1482 next = se->next;
1484 sfree (se->node);
1485 sfree (se->service);
1487 if (se->type == SOCKENT_TYPE_CLIENT)
1488 free_sockent_client (&se->data.client);
1489 else
1490 free_sockent_server (&se->data.server);
1492 sfree (se);
1493 se = next;
1494 }
1495 } /* }}} void sockent_destroy */
1497 /*
1498 * int network_set_ttl
1499 *
1500 * Set the `IP_MULTICAST_TTL', `IP_TTL', `IPV6_MULTICAST_HOPS' or
1501 * `IPV6_UNICAST_HOPS', depending on which option is applicable.
1502 *
1503 * The `struct addrinfo' is used to destinguish between unicast and multicast
1504 * sockets.
1505 */
1506 static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
1507 {
1508 DEBUG ("network plugin: network_set_ttl: network_config_ttl = %i;",
1509 network_config_ttl);
1511 assert (se->type == SOCKENT_TYPE_CLIENT);
1513 if ((network_config_ttl < 1) || (network_config_ttl > 255))
1514 return (-1);
1516 if (ai->ai_family == AF_INET)
1517 {
1518 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1519 int optname;
1521 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1522 optname = IP_MULTICAST_TTL;
1523 else
1524 optname = IP_TTL;
1526 if (setsockopt (se->data.client.fd, IPPROTO_IP, optname,
1527 &network_config_ttl,
1528 sizeof (network_config_ttl)) == -1)
1529 {
1530 char errbuf[1024];
1531 ERROR ("setsockopt: %s",
1532 sstrerror (errno, errbuf, sizeof (errbuf)));
1533 return (-1);
1534 }
1535 }
1536 else if (ai->ai_family == AF_INET6)
1537 {
1538 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1539 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1540 int optname;
1542 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1543 optname = IPV6_MULTICAST_HOPS;
1544 else
1545 optname = IPV6_UNICAST_HOPS;
1547 if (setsockopt (se->data.client.fd, IPPROTO_IPV6, optname,
1548 &network_config_ttl,
1549 sizeof (network_config_ttl)) == -1)
1550 {
1551 char errbuf[1024];
1552 ERROR ("setsockopt: %s",
1553 sstrerror (errno, errbuf,
1554 sizeof (errbuf)));
1555 return (-1);
1556 }
1557 }
1559 return (0);
1560 } /* int network_set_ttl */
1562 static int network_bind_socket (int fd, const struct addrinfo *ai)
1563 {
1564 int loop = 0;
1565 int yes = 1;
1567 /* allow multiple sockets to use the same PORT number */
1568 if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR,
1569 &yes, sizeof(yes)) == -1) {
1570 char errbuf[1024];
1571 ERROR ("setsockopt: %s",
1572 sstrerror (errno, errbuf, sizeof (errbuf)));
1573 return (-1);
1574 }
1576 DEBUG ("fd = %i; calling `bind'", fd);
1578 if (bind (fd, ai->ai_addr, ai->ai_addrlen) == -1)
1579 {
1580 char errbuf[1024];
1581 ERROR ("bind: %s",
1582 sstrerror (errno, errbuf, sizeof (errbuf)));
1583 return (-1);
1584 }
1586 if (ai->ai_family == AF_INET)
1587 {
1588 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1589 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1590 {
1591 struct ip_mreq mreq;
1593 DEBUG ("fd = %i; IPv4 multicast address found", fd);
1595 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1596 mreq.imr_interface.s_addr = htonl (INADDR_ANY);
1598 if (setsockopt (fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1599 &loop, sizeof (loop)) == -1)
1600 {
1601 char errbuf[1024];
1602 ERROR ("setsockopt: %s",
1603 sstrerror (errno, errbuf,
1604 sizeof (errbuf)));
1605 return (-1);
1606 }
1608 if (setsockopt (fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1609 &mreq, sizeof (mreq)) == -1)
1610 {
1611 char errbuf[1024];
1612 ERROR ("setsockopt: %s",
1613 sstrerror (errno, errbuf,
1614 sizeof (errbuf)));
1615 return (-1);
1616 }
1617 }
1618 }
1619 else if (ai->ai_family == AF_INET6)
1620 {
1621 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1622 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1623 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1624 {
1625 struct ipv6_mreq mreq;
1627 DEBUG ("fd = %i; IPv6 multicast address found", fd);
1629 memcpy (&mreq.ipv6mr_multiaddr,
1630 &addr->sin6_addr,
1631 sizeof (addr->sin6_addr));
1633 /* http://developer.apple.com/documentation/Darwin/Reference/ManPages/man4/ip6.4.html
1634 * ipv6mr_interface may be set to zeroes to
1635 * choose the default multicast interface or to
1636 * the index of a particular multicast-capable
1637 * interface if the host is multihomed.
1638 * Membership is associ-associated with a
1639 * single interface; programs running on
1640 * multihomed hosts may need to join the same
1641 * group on more than one interface.*/
1642 mreq.ipv6mr_interface = 0;
1644 if (setsockopt (fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
1645 &loop, sizeof (loop)) == -1)
1646 {
1647 char errbuf[1024];
1648 ERROR ("setsockopt: %s",
1649 sstrerror (errno, errbuf,
1650 sizeof (errbuf)));
1651 return (-1);
1652 }
1654 if (setsockopt (fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
1655 &mreq, sizeof (mreq)) == -1)
1656 {
1657 char errbuf[1024];
1658 ERROR ("setsockopt: %s",
1659 sstrerror (errno, errbuf,
1660 sizeof (errbuf)));
1661 return (-1);
1662 }
1663 }
1664 }
1666 return (0);
1667 } /* int network_bind_socket */
1669 /* Initialize a sockent structure. `type' must be either `SOCKENT_TYPE_CLIENT'
1670 * or `SOCKENT_TYPE_SERVER' */
1671 static int sockent_init (sockent_t *se, int type) /* {{{ */
1672 {
1673 if (se == NULL)
1674 return (-1);
1676 memset (se, 0, sizeof (*se));
1678 se->type = SOCKENT_TYPE_CLIENT;
1679 se->node = NULL;
1680 se->service = NULL;
1681 se->next = NULL;
1683 if (type == SOCKENT_TYPE_SERVER)
1684 {
1685 se->type = SOCKENT_TYPE_SERVER;
1686 se->data.server.fd = NULL;
1687 #if HAVE_LIBGCRYPT
1688 se->data.server.security_level = SECURITY_LEVEL_NONE;
1689 se->data.server.auth_file = NULL;
1690 se->data.server.userdb = NULL;
1691 se->data.server.cypher = NULL;
1692 #endif
1693 }
1694 else
1695 {
1696 se->data.client.fd = -1;
1697 se->data.client.addr = NULL;
1698 #if HAVE_LIBGCRYPT
1699 se->data.client.security_level = SECURITY_LEVEL_NONE;
1700 se->data.client.username = NULL;
1701 se->data.client.password = NULL;
1702 se->data.client.cypher = NULL;
1703 #endif
1704 }
1706 return (0);
1707 } /* }}} int sockent_init */
1709 /* Open the file descriptors for a initialized sockent structure. */
1710 static int sockent_open (sockent_t *se) /* {{{ */
1711 {
1712 struct addrinfo ai_hints;
1713 struct addrinfo *ai_list, *ai_ptr;
1714 int ai_return;
1716 const char *node;
1717 const char *service;
1719 if (se == NULL)
1720 return (-1);
1722 /* Set up the security structures. */
1723 #if HAVE_LIBGCRYPT /* {{{ */
1724 if (se->type == SOCKENT_TYPE_CLIENT)
1725 {
1726 if (se->data.client.security_level > SECURITY_LEVEL_NONE)
1727 {
1728 if ((se->data.client.username == NULL)
1729 || (se->data.client.password == NULL))
1730 {
1731 ERROR ("network plugin: Client socket with "
1732 "security requested, but no "
1733 "credentials are configured.");
1734 return (-1);
1735 }
1736 gcry_md_hash_buffer (GCRY_MD_SHA256,
1737 se->data.client.password_hash,
1738 se->data.client.password,
1739 strlen (se->data.client.password));
1740 }
1741 }
1742 else /* (se->type == SOCKENT_TYPE_SERVER) */
1743 {
1744 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
1745 {
1746 if (se->data.server.auth_file == NULL)
1747 {
1748 ERROR ("network plugin: Server socket with "
1749 "security requested, but no "
1750 "password file is configured.");
1751 return (-1);
1752 }
1753 }
1754 if (se->data.server.auth_file != NULL)
1755 {
1756 se->data.server.userdb = fbh_create (se->data.server.auth_file);
1757 if (se->data.server.userdb == NULL)
1758 {
1759 ERROR ("network plugin: Reading password file "
1760 "`%s' failed.",
1761 se->data.server.auth_file);
1762 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
1763 return (-1);
1764 }
1765 }
1766 }
1767 #endif /* }}} HAVE_LIBGCRYPT */
1769 node = se->node;
1770 service = se->service;
1772 if (service == NULL)
1773 service = NET_DEFAULT_PORT;
1775 DEBUG ("network plugin: sockent_open: node = %s; service = %s;",
1776 node, service);
1778 memset (&ai_hints, 0, sizeof (ai_hints));
1779 ai_hints.ai_flags = 0;
1780 #ifdef AI_PASSIVE
1781 ai_hints.ai_flags |= AI_PASSIVE;
1782 #endif
1783 #ifdef AI_ADDRCONFIG
1784 ai_hints.ai_flags |= AI_ADDRCONFIG;
1785 #endif
1786 ai_hints.ai_family = AF_UNSPEC;
1787 ai_hints.ai_socktype = SOCK_DGRAM;
1788 ai_hints.ai_protocol = IPPROTO_UDP;
1790 ai_return = getaddrinfo (node, service, &ai_hints, &ai_list);
1791 if (ai_return != 0)
1792 {
1793 ERROR ("network plugin: getaddrinfo (%s, %s) failed: %s",
1794 (se->node == NULL) ? "(null)" : se->node,
1795 (se->service == NULL) ? "(null)" : se->service,
1796 gai_strerror (ai_return));
1797 return (-1);
1798 }
1800 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1801 {
1802 int status;
1804 if (se->type == SOCKENT_TYPE_SERVER) /* {{{ */
1805 {
1806 int *tmp;
1808 tmp = realloc (se->data.server.fd,
1809 sizeof (*tmp) * (se->data.server.fd_num + 1));
1810 if (tmp == NULL)
1811 {
1812 ERROR ("network plugin: realloc failed.");
1813 continue;
1814 }
1815 se->data.server.fd = tmp;
1816 tmp = se->data.server.fd + se->data.server.fd_num;
1818 *tmp = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
1819 ai_ptr->ai_protocol);
1820 if (*tmp < 0)
1821 {
1822 char errbuf[1024];
1823 ERROR ("network plugin: socket(2) failed: %s",
1824 sstrerror (errno, errbuf,
1825 sizeof (errbuf)));
1826 continue;
1827 }
1829 status = network_bind_socket (*tmp, ai_ptr);
1830 if (status != 0)
1831 {
1832 close (*tmp);
1833 *tmp = -1;
1834 continue;
1835 }
1837 se->data.server.fd_num++;
1838 continue;
1839 } /* }}} if (se->type == SOCKENT_TYPE_SERVER) */
1840 else /* if (se->type == SOCKENT_TYPE_CLIENT) {{{ */
1841 {
1842 se->data.client.fd = socket (ai_ptr->ai_family,
1843 ai_ptr->ai_socktype,
1844 ai_ptr->ai_protocol);
1845 if (se->data.client.fd < 0)
1846 {
1847 char errbuf[1024];
1848 ERROR ("network plugin: socket(2) failed: %s",
1849 sstrerror (errno, errbuf,
1850 sizeof (errbuf)));
1851 continue;
1852 }
1854 se->data.client.addr = malloc (sizeof (*se->data.client.addr));
1855 if (se->data.client.addr == NULL)
1856 {
1857 ERROR ("network plugin: malloc failed.");
1858 close (se->data.client.fd);
1859 se->data.client.fd = -1;
1860 continue;
1861 }
1863 memset (se->data.client.addr, 0, sizeof (*se->data.client.addr));
1864 assert (sizeof (*se->data.client.addr) >= ai_ptr->ai_addrlen);
1865 memcpy (se->data.client.addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1866 se->data.client.addrlen = ai_ptr->ai_addrlen;
1868 network_set_ttl (se, ai_ptr);
1870 /* We don't open more than one write-socket per
1871 * node/service pair.. */
1872 break;
1873 } /* }}} if (se->type == SOCKENT_TYPE_CLIENT) */
1874 } /* for (ai_list) */
1876 freeaddrinfo (ai_list);
1878 /* Check if all went well. */
1879 if (se->type == SOCKENT_TYPE_SERVER)
1880 {
1881 if (se->data.server.fd_num <= 0)
1882 return (-1);
1883 }
1884 else /* if (se->type == SOCKENT_TYPE_CLIENT) */
1885 {
1886 if (se->data.client.fd < 0)
1887 return (-1);
1888 }
1890 return (0);
1891 } /* }}} int sockent_open */
1893 /* Add a sockent to the global list of sockets */
1894 static int sockent_add (sockent_t *se) /* {{{ */
1895 {
1896 sockent_t *last_ptr;
1898 if (se == NULL)
1899 return (-1);
1901 if (se->type == SOCKENT_TYPE_SERVER)
1902 {
1903 struct pollfd *tmp;
1904 size_t i;
1906 tmp = realloc (listen_sockets_pollfd,
1907 sizeof (*tmp) * (listen_sockets_num
1908 + se->data.server.fd_num));
1909 if (tmp == NULL)
1910 {
1911 ERROR ("network plugin: realloc failed.");
1912 return (-1);
1913 }
1914 listen_sockets_pollfd = tmp;
1915 tmp = listen_sockets_pollfd + listen_sockets_num;
1917 for (i = 0; i < se->data.server.fd_num; i++)
1918 {
1919 memset (tmp + i, 0, sizeof (*tmp));
1920 tmp[i].fd = se->data.server.fd[i];
1921 tmp[i].events = POLLIN | POLLPRI;
1922 tmp[i].revents = 0;
1923 }
1925 listen_sockets_num += se->data.server.fd_num;
1927 if (listen_sockets == NULL)
1928 {
1929 listen_sockets = se;
1930 return (0);
1931 }
1932 last_ptr = listen_sockets;
1933 }
1934 else /* if (se->type == SOCKENT_TYPE_CLIENT) */
1935 {
1936 if (sending_sockets == NULL)
1937 {
1938 sending_sockets = se;
1939 return (0);
1940 }
1941 last_ptr = sending_sockets;
1942 }
1944 while (last_ptr->next != NULL)
1945 last_ptr = last_ptr->next;
1946 last_ptr->next = se;
1948 return (0);
1949 } /* }}} int sockent_add */
1951 static void *dispatch_thread (void __attribute__((unused)) *arg) /* {{{ */
1952 {
1953 while (42)
1954 {
1955 receive_list_entry_t *ent;
1956 sockent_t *se;
1958 /* Lock and wait for more data to come in */
1959 pthread_mutex_lock (&receive_list_lock);
1960 while ((listen_loop == 0)
1961 && (receive_list_head == NULL))
1962 pthread_cond_wait (&receive_list_cond, &receive_list_lock);
1964 /* Remove the head entry and unlock */
1965 ent = receive_list_head;
1966 if (ent != NULL)
1967 receive_list_head = ent->next;
1968 pthread_mutex_unlock (&receive_list_lock);
1970 /* Check whether we are supposed to exit. We do NOT check `listen_loop'
1971 * because we dispatch all missing packets before shutting down. */
1972 if (ent == NULL)
1973 break;
1975 /* Look for the correct `sockent_t' */
1976 se = listen_sockets;
1977 while (se != NULL)
1978 {
1979 size_t i;
1981 for (i = 0; i < se->data.server.fd_num; i++)
1982 if (se->data.server.fd[i] == ent->fd)
1983 break;
1985 if (i < se->data.server.fd_num)
1986 break;
1988 se = se->next;
1989 }
1991 if (se == NULL)
1992 {
1993 ERROR ("network plugin: Got packet from FD %i, but can't "
1994 "find an appropriate socket entry.",
1995 ent->fd);
1996 sfree (ent->data);
1997 sfree (ent);
1998 continue;
1999 }
2001 parse_packet (se, ent->data, ent->data_len, /* flags = */ 0);
2002 sfree (ent->data);
2003 sfree (ent);
2004 } /* while (42) */
2006 return (NULL);
2007 } /* }}} void *dispatch_thread */
2009 static int network_receive (void) /* {{{ */
2010 {
2011 char buffer[network_config_packet_size];
2012 int buffer_len;
2014 int i;
2015 int status;
2017 receive_list_entry_t *private_list_head;
2018 receive_list_entry_t *private_list_tail;
2020 assert (listen_sockets_num > 0);
2022 private_list_head = NULL;
2023 private_list_tail = NULL;
2025 while (listen_loop == 0)
2026 {
2027 status = poll (listen_sockets_pollfd, listen_sockets_num, -1);
2029 if (status <= 0)
2030 {
2031 char errbuf[1024];
2032 if (errno == EINTR)
2033 continue;
2034 ERROR ("poll failed: %s",
2035 sstrerror (errno, errbuf, sizeof (errbuf)));
2036 return (-1);
2037 }
2039 for (i = 0; (i < listen_sockets_num) && (status > 0); i++)
2040 {
2041 receive_list_entry_t *ent;
2043 if ((listen_sockets_pollfd[i].revents
2044 & (POLLIN | POLLPRI)) == 0)
2045 continue;
2046 status--;
2048 buffer_len = recv (listen_sockets_pollfd[i].fd,
2049 buffer, sizeof (buffer),
2050 0 /* no flags */);
2051 if (buffer_len < 0)
2052 {
2053 char errbuf[1024];
2054 ERROR ("recv failed: %s",
2055 sstrerror (errno, errbuf,
2056 sizeof (errbuf)));
2057 return (-1);
2058 }
2060 /* TODO: Possible performance enhancement: Do not free
2061 * these entries in the dispatch thread but put them in
2062 * another list, so we don't have to allocate more and
2063 * more of these structures. */
2064 ent = malloc (sizeof (receive_list_entry_t));
2065 if (ent == NULL)
2066 {
2067 ERROR ("network plugin: malloc failed.");
2068 return (-1);
2069 }
2070 memset (ent, 0, sizeof (receive_list_entry_t));
2071 ent->data = malloc (network_config_packet_size);
2072 if (ent->data == NULL)
2073 {
2074 ERROR ("network plugin: malloc failed.");
2075 return (-1);
2076 }
2077 ent->fd = listen_sockets_pollfd[i].fd;
2078 ent->next = NULL;
2080 memcpy (ent->data, buffer, buffer_len);
2081 ent->data_len = buffer_len;
2083 if (private_list_head == NULL)
2084 private_list_head = ent;
2085 else
2086 private_list_tail->next = ent;
2087 private_list_tail = ent;
2089 /* Do not block here. Blocking here has led to
2090 * insufficient performance in the past. */
2091 if (pthread_mutex_trylock (&receive_list_lock) == 0)
2092 {
2093 if (receive_list_head == NULL)
2094 receive_list_head = private_list_head;
2095 else
2096 receive_list_tail->next = private_list_head;
2097 receive_list_tail = private_list_tail;
2099 private_list_head = NULL;
2100 private_list_tail = NULL;
2102 pthread_cond_signal (&receive_list_cond);
2103 pthread_mutex_unlock (&receive_list_lock);
2104 }
2105 } /* for (listen_sockets_pollfd) */
2106 } /* while (listen_loop == 0) */
2108 /* Make sure everything is dispatched before exiting. */
2109 if (private_list_head != NULL)
2110 {
2111 pthread_mutex_lock (&receive_list_lock);
2113 if (receive_list_head == NULL)
2114 receive_list_head = private_list_head;
2115 else
2116 receive_list_tail->next = private_list_head;
2117 receive_list_tail = private_list_tail;
2119 private_list_head = NULL;
2120 private_list_tail = NULL;
2122 pthread_cond_signal (&receive_list_cond);
2123 pthread_mutex_unlock (&receive_list_lock);
2124 }
2126 return (0);
2127 } /* }}} int network_receive */
2129 static void *receive_thread (void __attribute__((unused)) *arg)
2130 {
2131 return (network_receive () ? (void *) 1 : (void *) 0);
2132 } /* void *receive_thread */
2134 static void network_init_buffer (void)
2135 {
2136 memset (send_buffer, 0, network_config_packet_size);
2137 send_buffer_ptr = send_buffer;
2138 send_buffer_fill = 0;
2140 memset (&send_buffer_vl, 0, sizeof (send_buffer_vl));
2141 } /* int network_init_buffer */
2143 static void networt_send_buffer_plain (const sockent_t *se, /* {{{ */
2144 const char *buffer, size_t buffer_size)
2145 {
2146 int status;
2148 while (42)
2149 {
2150 status = sendto (se->data.client.fd, buffer, buffer_size,
2151 /* flags = */ 0,
2152 (struct sockaddr *) se->data.client.addr,
2153 se->data.client.addrlen);
2154 if (status < 0)
2155 {
2156 char errbuf[1024];
2157 if (errno == EINTR)
2158 continue;
2159 ERROR ("network plugin: sendto failed: %s",
2160 sstrerror (errno, errbuf,
2161 sizeof (errbuf)));
2162 break;
2163 }
2165 break;
2166 } /* while (42) */
2167 } /* }}} void networt_send_buffer_plain */
2169 #if HAVE_LIBGCRYPT
2170 #define BUFFER_ADD(p,s) do { \
2171 memcpy (buffer + buffer_offset, (p), (s)); \
2172 buffer_offset += (s); \
2173 } while (0)
2175 static void networt_send_buffer_signed (const sockent_t *se, /* {{{ */
2176 const char *in_buffer, size_t in_buffer_size)
2177 {
2178 part_signature_sha256_t ps;
2179 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2180 size_t buffer_offset;
2181 size_t username_len;
2183 gcry_md_hd_t hd;
2184 gcry_error_t err;
2185 unsigned char *hash;
2187 hd = NULL;
2188 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
2189 if (err != 0)
2190 {
2191 ERROR ("network plugin: Creating HMAC object failed: %s",
2192 gcry_strerror (err));
2193 return;
2194 }
2196 err = gcry_md_setkey (hd, se->data.client.password,
2197 strlen (se->data.client.password));
2198 if (err != 0)
2199 {
2200 ERROR ("network plugin: gcry_md_setkey failed: %s",
2201 gcry_strerror (err));
2202 gcry_md_close (hd);
2203 return;
2204 }
2206 username_len = strlen (se->data.client.username);
2207 if (username_len > (BUFF_SIG_SIZE - PART_SIGNATURE_SHA256_SIZE))
2208 {
2209 ERROR ("network plugin: Username too long: %s",
2210 se->data.client.username);
2211 return;
2212 }
2214 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE,
2215 se->data.client.username, username_len);
2216 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE + username_len,
2217 in_buffer, in_buffer_size);
2219 /* Initialize the `ps' structure. */
2220 memset (&ps, 0, sizeof (ps));
2221 ps.head.type = htons (TYPE_SIGN_SHA256);
2222 ps.head.length = htons (PART_SIGNATURE_SHA256_SIZE + username_len);
2224 /* Calculate the hash value. */
2225 gcry_md_write (hd, buffer + PART_SIGNATURE_SHA256_SIZE,
2226 username_len + in_buffer_size);
2227 hash = gcry_md_read (hd, GCRY_MD_SHA256);
2228 if (hash == NULL)
2229 {
2230 ERROR ("network plugin: gcry_md_read failed.");
2231 gcry_md_close (hd);
2232 return;
2233 }
2234 memcpy (ps.hash, hash, sizeof (ps.hash));
2236 /* Add the header */
2237 buffer_offset = 0;
2239 BUFFER_ADD (&ps.head.type, sizeof (ps.head.type));
2240 BUFFER_ADD (&ps.head.length, sizeof (ps.head.length));
2241 BUFFER_ADD (ps.hash, sizeof (ps.hash));
2243 assert (buffer_offset == PART_SIGNATURE_SHA256_SIZE);
2245 gcry_md_close (hd);
2246 hd = NULL;
2248 buffer_offset = PART_SIGNATURE_SHA256_SIZE + username_len + in_buffer_size;
2249 networt_send_buffer_plain (se, buffer, buffer_offset);
2250 } /* }}} void networt_send_buffer_signed */
2252 static void networt_send_buffer_encrypted (sockent_t *se, /* {{{ */
2253 const char *in_buffer, size_t in_buffer_size)
2254 {
2255 part_encryption_aes256_t pea;
2256 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2257 size_t buffer_size;
2258 size_t buffer_offset;
2259 size_t header_size;
2260 size_t username_len;
2261 gcry_error_t err;
2262 gcry_cipher_hd_t cypher;
2264 /* Initialize the header fields */
2265 memset (&pea, 0, sizeof (pea));
2266 pea.head.type = htons (TYPE_ENCR_AES256);
2268 pea.username = se->data.client.username;
2270 username_len = strlen (pea.username);
2271 if ((PART_ENCRYPTION_AES256_SIZE + username_len) > BUFF_SIG_SIZE)
2272 {
2273 ERROR ("network plugin: Username too long: %s", pea.username);
2274 return;
2275 }
2277 buffer_size = PART_ENCRYPTION_AES256_SIZE + username_len + in_buffer_size;
2278 header_size = PART_ENCRYPTION_AES256_SIZE + username_len
2279 - sizeof (pea.hash);
2281 assert (buffer_size <= sizeof (buffer));
2282 DEBUG ("network plugin: networt_send_buffer_encrypted: "
2283 "buffer_size = %zu;", buffer_size);
2285 pea.head.length = htons ((uint16_t) (PART_ENCRYPTION_AES256_SIZE
2286 + username_len + in_buffer_size));
2287 pea.username_length = htons ((uint16_t) username_len);
2289 /* Chose a random initialization vector. */
2290 gcry_randomize ((void *) &pea.iv, sizeof (pea.iv), GCRY_STRONG_RANDOM);
2292 /* Create hash of the payload */
2293 gcry_md_hash_buffer (GCRY_MD_SHA1, pea.hash, in_buffer, in_buffer_size);
2295 /* Initialize the buffer */
2296 buffer_offset = 0;
2297 memset (buffer, 0, sizeof (buffer));
2300 BUFFER_ADD (&pea.head.type, sizeof (pea.head.type));
2301 BUFFER_ADD (&pea.head.length, sizeof (pea.head.length));
2302 BUFFER_ADD (&pea.username_length, sizeof (pea.username_length));
2303 BUFFER_ADD (pea.username, username_len);
2304 BUFFER_ADD (pea.iv, sizeof (pea.iv));
2305 assert (buffer_offset == header_size);
2306 BUFFER_ADD (pea.hash, sizeof (pea.hash));
2307 BUFFER_ADD (in_buffer, in_buffer_size);
2309 assert (buffer_offset == buffer_size);
2311 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
2312 se->data.client.password);
2313 if (cypher == NULL)
2314 return;
2316 /* Encrypt the buffer in-place */
2317 err = gcry_cipher_encrypt (cypher,
2318 buffer + header_size,
2319 buffer_size - header_size,
2320 /* in = */ NULL, /* in len = */ 0);
2321 if (err != 0)
2322 {
2323 ERROR ("network plugin: gcry_cipher_encrypt returned: %s",
2324 gcry_strerror (err));
2325 return;
2326 }
2328 /* Send it out without further modifications */
2329 networt_send_buffer_plain (se, buffer, buffer_size);
2330 } /* }}} void networt_send_buffer_encrypted */
2331 #undef BUFFER_ADD
2332 #endif /* HAVE_LIBGCRYPT */
2334 static void network_send_buffer (char *buffer, size_t buffer_len) /* {{{ */
2335 {
2336 sockent_t *se;
2338 DEBUG ("network plugin: network_send_buffer: buffer_len = %zu", buffer_len);
2340 for (se = sending_sockets; se != NULL; se = se->next)
2341 {
2342 #if HAVE_LIBGCRYPT
2343 if (se->data.client.security_level == SECURITY_LEVEL_ENCRYPT)
2344 networt_send_buffer_encrypted (se, buffer, buffer_len);
2345 else if (se->data.client.security_level == SECURITY_LEVEL_SIGN)
2346 networt_send_buffer_signed (se, buffer, buffer_len);
2347 else /* if (se->data.client.security_level == SECURITY_LEVEL_NONE) */
2348 #endif /* HAVE_LIBGCRYPT */
2349 networt_send_buffer_plain (se, buffer, buffer_len);
2350 } /* for (sending_sockets) */
2351 } /* }}} void network_send_buffer */
2353 static int add_to_buffer (char *buffer, int buffer_size, /* {{{ */
2354 value_list_t *vl_def,
2355 const data_set_t *ds, const value_list_t *vl)
2356 {
2357 char *buffer_orig = buffer;
2359 if (strcmp (vl_def->host, vl->host) != 0)
2360 {
2361 if (write_part_string (&buffer, &buffer_size, TYPE_HOST,
2362 vl->host, strlen (vl->host)) != 0)
2363 return (-1);
2364 sstrncpy (vl_def->host, vl->host, sizeof (vl_def->host));
2365 }
2367 if (vl_def->time != vl->time)
2368 {
2369 if (write_part_number (&buffer, &buffer_size, TYPE_TIME,
2370 (uint64_t) vl->time))
2371 return (-1);
2372 vl_def->time = vl->time;
2373 }
2375 if (vl_def->interval != vl->interval)
2376 {
2377 if (write_part_number (&buffer, &buffer_size, TYPE_INTERVAL,
2378 (uint64_t) vl->interval))
2379 return (-1);
2380 vl_def->interval = vl->interval;
2381 }
2383 if (strcmp (vl_def->plugin, vl->plugin) != 0)
2384 {
2385 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN,
2386 vl->plugin, strlen (vl->plugin)) != 0)
2387 return (-1);
2388 sstrncpy (vl_def->plugin, vl->plugin, sizeof (vl_def->plugin));
2389 }
2391 if (strcmp (vl_def->plugin_instance, vl->plugin_instance) != 0)
2392 {
2393 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
2394 vl->plugin_instance,
2395 strlen (vl->plugin_instance)) != 0)
2396 return (-1);
2397 sstrncpy (vl_def->plugin_instance, vl->plugin_instance, sizeof (vl_def->plugin_instance));
2398 }
2400 if (strcmp (vl_def->type, vl->type) != 0)
2401 {
2402 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
2403 vl->type, strlen (vl->type)) != 0)
2404 return (-1);
2405 sstrncpy (vl_def->type, ds->type, sizeof (vl_def->type));
2406 }
2408 if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
2409 {
2410 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE_INSTANCE,
2411 vl->type_instance,
2412 strlen (vl->type_instance)) != 0)
2413 return (-1);
2414 sstrncpy (vl_def->type_instance, vl->type_instance, sizeof (vl_def->type_instance));
2415 }
2417 if (write_part_values (&buffer, &buffer_size, ds, vl) != 0)
2418 return (-1);
2420 return (buffer - buffer_orig);
2421 } /* }}} int add_to_buffer */
2423 static void flush_buffer (void)
2424 {
2425 DEBUG ("network plugin: flush_buffer: send_buffer_fill = %i",
2426 send_buffer_fill);
2428 network_send_buffer (send_buffer, (size_t) send_buffer_fill);
2429 network_init_buffer ();
2430 }
2432 static int network_write (const data_set_t *ds, const value_list_t *vl,
2433 user_data_t __attribute__((unused)) *user_data)
2434 {
2435 int status;
2437 if (!check_send_okay (vl))
2438 {
2439 #if COLLECT_DEBUG
2440 char name[6*DATA_MAX_NAME_LEN];
2441 FORMAT_VL (name, sizeof (name), vl);
2442 name[sizeof (name) - 1] = 0;
2443 DEBUG ("network plugin: network_write: "
2444 "NOT sending %s.", name);
2445 #endif
2446 return (0);
2447 }
2449 uc_meta_data_add_unsigned_int (vl,
2450 "network:time_sent", (uint64_t) vl->time);
2452 pthread_mutex_lock (&send_buffer_lock);
2454 status = add_to_buffer (send_buffer_ptr,
2455 network_config_packet_size - (send_buffer_fill + BUFF_SIG_SIZE),
2456 &send_buffer_vl,
2457 ds, vl);
2458 if (status >= 0)
2459 {
2460 /* status == bytes added to the buffer */
2461 send_buffer_fill += status;
2462 send_buffer_ptr += status;
2463 }
2464 else
2465 {
2466 flush_buffer ();
2468 status = add_to_buffer (send_buffer_ptr,
2469 network_config_packet_size - (send_buffer_fill + BUFF_SIG_SIZE),
2470 &send_buffer_vl,
2471 ds, vl);
2473 if (status >= 0)
2474 {
2475 send_buffer_fill += status;
2476 send_buffer_ptr += status;
2477 }
2478 }
2480 if (status < 0)
2481 {
2482 ERROR ("network plugin: Unable to append to the "
2483 "buffer for some weird reason");
2484 }
2485 else if ((network_config_packet_size - send_buffer_fill) < 15)
2486 {
2487 flush_buffer ();
2488 }
2490 pthread_mutex_unlock (&send_buffer_lock);
2492 return ((status < 0) ? -1 : 0);
2493 } /* int network_write */
2495 static int network_config_set_boolean (const oconfig_item_t *ci, /* {{{ */
2496 int *retval)
2497 {
2498 if ((ci->values_num != 1)
2499 || ((ci->values[0].type != OCONFIG_TYPE_BOOLEAN)
2500 && (ci->values[0].type != OCONFIG_TYPE_STRING)))
2501 {
2502 ERROR ("network plugin: The `%s' config option needs "
2503 "exactly one boolean argument.", ci->key);
2504 return (-1);
2505 }
2507 if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
2508 {
2509 if (ci->values[0].value.boolean)
2510 *retval = 1;
2511 else
2512 *retval = 0;
2513 }
2514 else
2515 {
2516 char *str = ci->values[0].value.string;
2518 if ((strcasecmp ("true", str) == 0)
2519 || (strcasecmp ("yes", str) == 0)
2520 || (strcasecmp ("on", str) == 0))
2521 *retval = 1;
2522 else if ((strcasecmp ("false", str) == 0)
2523 || (strcasecmp ("no", str) == 0)
2524 || (strcasecmp ("off", str) == 0))
2525 *retval = 0;
2526 else
2527 {
2528 ERROR ("network plugin: Cannot parse string value `%s' of the `%s' "
2529 "option as boolean value.",
2530 str, ci->key);
2531 return (-1);
2532 }
2533 }
2535 return (0);
2536 } /* }}} int network_config_set_boolean */
2538 static int network_config_set_ttl (const oconfig_item_t *ci) /* {{{ */
2539 {
2540 int tmp;
2541 if ((ci->values_num != 1)
2542 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
2543 {
2544 WARNING ("network plugin: The `TimeToLive' config option needs exactly "
2545 "one numeric argument.");
2546 return (-1);
2547 }
2549 tmp = (int) ci->values[0].value.number;
2550 if ((tmp > 0) && (tmp <= 255))
2551 network_config_ttl = tmp;
2553 return (0);
2554 } /* }}} int network_config_set_ttl */
2556 static int network_config_set_buffer_size (const oconfig_item_t *ci) /* {{{ */
2557 {
2558 int tmp;
2559 if ((ci->values_num != 1)
2560 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
2561 {
2562 WARNING ("network plugin: The `MaxPacketSize' config option needs exactly "
2563 "one numeric argument.");
2564 return (-1);
2565 }
2567 tmp = (int) ci->values[0].value.number;
2568 if ((tmp >= 1024) && (tmp <= 65535))
2569 network_config_packet_size = tmp;
2571 return (0);
2572 } /* }}} int network_config_set_buffer_size */
2574 #if HAVE_LIBGCRYPT
2575 static int network_config_set_string (const oconfig_item_t *ci, /* {{{ */
2576 char **ret_string)
2577 {
2578 char *tmp;
2579 if ((ci->values_num != 1)
2580 || (ci->values[0].type != OCONFIG_TYPE_STRING))
2581 {
2582 WARNING ("network plugin: The `%s' config option needs exactly "
2583 "one string argument.", ci->key);
2584 return (-1);
2585 }
2587 tmp = strdup (ci->values[0].value.string);
2588 if (tmp == NULL)
2589 return (-1);
2591 sfree (*ret_string);
2592 *ret_string = tmp;
2594 return (0);
2595 } /* }}} int network_config_set_string */
2596 #endif /* HAVE_LIBGCRYPT */
2598 #if HAVE_LIBGCRYPT
2599 static int network_config_set_security_level (oconfig_item_t *ci, /* {{{ */
2600 int *retval)
2601 {
2602 char *str;
2603 if ((ci->values_num != 1)
2604 || (ci->values[0].type != OCONFIG_TYPE_STRING))
2605 {
2606 WARNING ("network plugin: The `SecurityLevel' config option needs exactly "
2607 "one string argument.");
2608 return (-1);
2609 }
2611 str = ci->values[0].value.string;
2612 if (strcasecmp ("Encrypt", str) == 0)
2613 *retval = SECURITY_LEVEL_ENCRYPT;
2614 else if (strcasecmp ("Sign", str) == 0)
2615 *retval = SECURITY_LEVEL_SIGN;
2616 else if (strcasecmp ("None", str) == 0)
2617 *retval = SECURITY_LEVEL_NONE;
2618 else
2619 {
2620 WARNING ("network plugin: Unknown security level: %s.", str);
2621 return (-1);
2622 }
2624 return (0);
2625 } /* }}} int network_config_set_security_level */
2626 #endif /* HAVE_LIBGCRYPT */
2628 static int network_config_add_listen (const oconfig_item_t *ci) /* {{{ */
2629 {
2630 sockent_t *se;
2631 int status;
2632 int i;
2634 if ((ci->values_num < 1) || (ci->values_num > 2)
2635 || (ci->values[0].type != OCONFIG_TYPE_STRING)
2636 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
2637 {
2638 ERROR ("network plugin: The `%s' config option needs "
2639 "one or two string arguments.", ci->key);
2640 return (-1);
2641 }
2643 se = malloc (sizeof (*se));
2644 if (se == NULL)
2645 {
2646 ERROR ("network plugin: malloc failed.");
2647 return (-1);
2648 }
2649 sockent_init (se, SOCKENT_TYPE_SERVER);
2651 se->node = strdup (ci->values[0].value.string);
2652 if (ci->values_num >= 2)
2653 se->service = strdup (ci->values[1].value.string);
2655 for (i = 0; i < ci->children_num; i++)
2656 {
2657 oconfig_item_t *child = ci->children + i;
2659 #if HAVE_LIBGCRYPT
2660 if (strcasecmp ("AuthFile", child->key) == 0)
2661 network_config_set_string (child, &se->data.server.auth_file);
2662 else if (strcasecmp ("SecurityLevel", child->key) == 0)
2663 network_config_set_security_level (child,
2664 &se->data.server.security_level);
2665 else
2666 #endif /* HAVE_LIBGCRYPT */
2667 {
2668 WARNING ("network plugin: Option `%s' is not allowed here.",
2669 child->key);
2670 }
2671 }
2673 #if HAVE_LIBGCRYPT
2674 if ((se->data.server.security_level > SECURITY_LEVEL_NONE)
2675 && (se->data.server.auth_file == NULL))
2676 {
2677 ERROR ("network plugin: A security level higher than `none' was "
2678 "requested, but no AuthFile option was given. Cowardly refusing to "
2679 "open this socket!");
2680 sockent_destroy (se);
2681 return (-1);
2682 }
2683 #endif /* HAVE_LIBGCRYPT */
2685 status = sockent_open (se);
2686 if (status != 0)
2687 {
2688 ERROR ("network plugin: network_config_add_listen: sockent_open failed.");
2689 sockent_destroy (se);
2690 return (-1);
2691 }
2693 status = sockent_add (se);
2694 if (status != 0)
2695 {
2696 ERROR ("network plugin: network_config_add_listen: sockent_add failed.");
2697 sockent_destroy (se);
2698 return (-1);
2699 }
2701 return (0);
2702 } /* }}} int network_config_add_listen */
2704 static int network_config_add_server (const oconfig_item_t *ci) /* {{{ */
2705 {
2706 sockent_t *se;
2707 int status;
2708 int i;
2710 if ((ci->values_num < 1) || (ci->values_num > 2)
2711 || (ci->values[0].type != OCONFIG_TYPE_STRING)
2712 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
2713 {
2714 ERROR ("network plugin: The `%s' config option needs "
2715 "one or two string arguments.", ci->key);
2716 return (-1);
2717 }
2719 se = malloc (sizeof (*se));
2720 if (se == NULL)
2721 {
2722 ERROR ("network plugin: malloc failed.");
2723 return (-1);
2724 }
2725 sockent_init (se, SOCKENT_TYPE_CLIENT);
2727 se->node = strdup (ci->values[0].value.string);
2728 if (ci->values_num >= 2)
2729 se->service = strdup (ci->values[1].value.string);
2731 for (i = 0; i < ci->children_num; i++)
2732 {
2733 oconfig_item_t *child = ci->children + i;
2735 #if HAVE_LIBGCRYPT
2736 if (strcasecmp ("Username", child->key) == 0)
2737 network_config_set_string (child, &se->data.client.username);
2738 else if (strcasecmp ("Password", child->key) == 0)
2739 network_config_set_string (child, &se->data.client.password);
2740 else if (strcasecmp ("SecurityLevel", child->key) == 0)
2741 network_config_set_security_level (child,
2742 &se->data.client.security_level);
2743 else
2744 #endif /* HAVE_LIBGCRYPT */
2745 {
2746 WARNING ("network plugin: Option `%s' is not allowed here.",
2747 child->key);
2748 }
2749 }
2751 #if HAVE_LIBGCRYPT
2752 if ((se->data.client.security_level > SECURITY_LEVEL_NONE)
2753 && ((se->data.client.username == NULL)
2754 || (se->data.client.password == NULL)))
2755 {
2756 ERROR ("network plugin: A security level higher than `none' was "
2757 "requested, but no Username or Password option was given. "
2758 "Cowardly refusing to open this socket!");
2759 sockent_destroy (se);
2760 return (-1);
2761 }
2762 #endif /* HAVE_LIBGCRYPT */
2764 status = sockent_open (se);
2765 if (status != 0)
2766 {
2767 ERROR ("network plugin: network_config_add_server: sockent_open failed.");
2768 sockent_destroy (se);
2769 return (-1);
2770 }
2772 status = sockent_add (se);
2773 if (status != 0)
2774 {
2775 ERROR ("network plugin: network_config_add_server: sockent_add failed.");
2776 sockent_destroy (se);
2777 return (-1);
2778 }
2780 return (0);
2781 } /* }}} int network_config_add_server */
2783 static int network_config (oconfig_item_t *ci) /* {{{ */
2784 {
2785 int i;
2787 for (i = 0; i < ci->children_num; i++)
2788 {
2789 oconfig_item_t *child = ci->children + i;
2791 if (strcasecmp ("Listen", child->key) == 0)
2792 network_config_add_listen (child);
2793 else if (strcasecmp ("Server", child->key) == 0)
2794 network_config_add_server (child);
2795 else if (strcasecmp ("TimeToLive", child->key) == 0)
2796 network_config_set_ttl (child);
2797 else if (strcasecmp ("MaxPacketSize", child->key) == 0)
2798 network_config_set_buffer_size (child);
2799 else if (strcasecmp ("Forward", child->key) == 0)
2800 network_config_set_boolean (child, &network_config_forward);
2801 else if (strcasecmp ("CacheFlush", child->key) == 0)
2802 /* no op for backwards compatibility only */;
2803 else
2804 {
2805 WARNING ("network plugin: Option `%s' is not allowed here.",
2806 child->key);
2807 }
2808 }
2810 return (0);
2811 } /* }}} int network_config */
2813 static int network_notification (const notification_t *n,
2814 user_data_t __attribute__((unused)) *user_data)
2815 {
2816 char buffer[network_config_packet_size];
2817 char *buffer_ptr = buffer;
2818 int buffer_free = sizeof (buffer);
2819 int status;
2821 memset (buffer, '\0', sizeof (buffer));
2824 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_TIME,
2825 (uint64_t) n->time);
2826 if (status != 0)
2827 return (-1);
2829 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_SEVERITY,
2830 (uint64_t) n->severity);
2831 if (status != 0)
2832 return (-1);
2834 if (strlen (n->host) > 0)
2835 {
2836 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_HOST,
2837 n->host, strlen (n->host));
2838 if (status != 0)
2839 return (-1);
2840 }
2842 if (strlen (n->plugin) > 0)
2843 {
2844 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_PLUGIN,
2845 n->plugin, strlen (n->plugin));
2846 if (status != 0)
2847 return (-1);
2848 }
2850 if (strlen (n->plugin_instance) > 0)
2851 {
2852 status = write_part_string (&buffer_ptr, &buffer_free,
2853 TYPE_PLUGIN_INSTANCE,
2854 n->plugin_instance, strlen (n->plugin_instance));
2855 if (status != 0)
2856 return (-1);
2857 }
2859 if (strlen (n->type) > 0)
2860 {
2861 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE,
2862 n->type, strlen (n->type));
2863 if (status != 0)
2864 return (-1);
2865 }
2867 if (strlen (n->type_instance) > 0)
2868 {
2869 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE_INSTANCE,
2870 n->type_instance, strlen (n->type_instance));
2871 if (status != 0)
2872 return (-1);
2873 }
2875 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_MESSAGE,
2876 n->message, strlen (n->message));
2877 if (status != 0)
2878 return (-1);
2880 network_send_buffer (buffer, sizeof (buffer) - buffer_free);
2882 return (0);
2883 } /* int network_notification */
2885 static int network_shutdown (void)
2886 {
2887 listen_loop++;
2889 /* Kill the listening thread */
2890 if (receive_thread_running != 0)
2891 {
2892 INFO ("network plugin: Stopping receive thread.");
2893 pthread_kill (receive_thread_id, SIGTERM);
2894 pthread_join (receive_thread_id, NULL /* no return value */);
2895 memset (&receive_thread_id, 0, sizeof (receive_thread_id));
2896 receive_thread_running = 0;
2897 }
2899 /* Shutdown the dispatching thread */
2900 if (dispatch_thread_running != 0)
2901 {
2902 INFO ("network plugin: Stopping dispatch thread.");
2903 pthread_mutex_lock (&receive_list_lock);
2904 pthread_cond_broadcast (&receive_list_cond);
2905 pthread_mutex_unlock (&receive_list_lock);
2906 pthread_join (dispatch_thread_id, /* ret = */ NULL);
2907 dispatch_thread_running = 0;
2908 }
2910 sockent_destroy (listen_sockets);
2912 if (send_buffer_fill > 0)
2913 flush_buffer ();
2915 sfree (send_buffer);
2917 /* TODO: Close `sending_sockets' */
2919 plugin_unregister_config ("network");
2920 plugin_unregister_init ("network");
2921 plugin_unregister_write ("network");
2922 plugin_unregister_shutdown ("network");
2924 return (0);
2925 } /* int network_shutdown */
2927 static int network_init (void)
2928 {
2929 static _Bool have_init = false;
2931 /* Check if we were already initialized. If so, just return - there's
2932 * nothing more to do (for now, that is). */
2933 if (have_init)
2934 return (0);
2935 have_init = true;
2937 plugin_register_shutdown ("network", network_shutdown);
2939 send_buffer = malloc (network_config_packet_size);
2940 if (send_buffer == NULL)
2941 {
2942 ERROR ("network plugin: malloc failed.");
2943 return (-1);
2944 }
2945 network_init_buffer ();
2947 /* setup socket(s) and so on */
2948 if (sending_sockets != NULL)
2949 {
2950 plugin_register_write ("network", network_write,
2951 /* user_data = */ NULL);
2952 plugin_register_notification ("network", network_notification,
2953 /* user_data = */ NULL);
2954 }
2956 /* If no threads need to be started, return here. */
2957 if ((listen_sockets_num == 0)
2958 || ((dispatch_thread_running != 0)
2959 && (receive_thread_running != 0)))
2960 return (0);
2962 if (dispatch_thread_running == 0)
2963 {
2964 int status;
2965 status = pthread_create (&dispatch_thread_id,
2966 NULL /* no attributes */,
2967 dispatch_thread,
2968 NULL /* no argument */);
2969 if (status != 0)
2970 {
2971 char errbuf[1024];
2972 ERROR ("network: pthread_create failed: %s",
2973 sstrerror (errno, errbuf,
2974 sizeof (errbuf)));
2975 }
2976 else
2977 {
2978 dispatch_thread_running = 1;
2979 }
2980 }
2982 if (receive_thread_running == 0)
2983 {
2984 int status;
2985 status = pthread_create (&receive_thread_id,
2986 NULL /* no attributes */,
2987 receive_thread,
2988 NULL /* no argument */);
2989 if (status != 0)
2990 {
2991 char errbuf[1024];
2992 ERROR ("network: pthread_create failed: %s",
2993 sstrerror (errno, errbuf,
2994 sizeof (errbuf)));
2995 }
2996 else
2997 {
2998 receive_thread_running = 1;
2999 }
3000 }
3002 return (0);
3003 } /* int network_init */
3005 /*
3006 * The flush option of the network plugin cannot flush individual identifiers.
3007 * All the values are added to a buffer and sent when the buffer is full, the
3008 * requested value may or may not be in there, it's not worth finding out. We
3009 * just send the buffer if `flush' is called - if the requested value was in
3010 * there, good. If not, well, then there is nothing to flush.. -octo
3011 */
3012 static int network_flush (int timeout,
3013 const char __attribute__((unused)) *identifier,
3014 user_data_t __attribute__((unused)) *user_data)
3015 {
3016 pthread_mutex_lock (&send_buffer_lock);
3018 if (send_buffer_fill > 0)
3019 flush_buffer ();
3021 pthread_mutex_unlock (&send_buffer_lock);
3023 return (0);
3024 } /* int network_flush */
3026 void module_register (void)
3027 {
3028 plugin_register_complex_config ("network", network_config);
3029 plugin_register_init ("network", network_init);
3030 plugin_register_flush ("network", network_flush,
3031 /* user_data = */ NULL);
3032 } /* void module_register */
3034 /* vim: set fdm=marker : */