9e391bb2dfd7c91b1cfd641fa5db40ea4da18b01
1 /**
2 * collectd - src/network.c
3 * Copyright (C) 2005-2008 Florian octo Forster
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * Authors:
19 * Florian octo Forster <octo at verplant.org>
20 **/
22 #include "collectd.h"
23 #include "plugin.h"
24 #include "common.h"
25 #include "configfile.h"
26 #include "utils_avltree.h"
28 #include "network.h"
30 #if HAVE_PTHREAD_H
31 # include <pthread.h>
32 #endif
33 #if HAVE_SYS_SOCKET_H
34 # include <sys/socket.h>
35 #endif
36 #if HAVE_NETDB_H
37 # include <netdb.h>
38 #endif
39 #if HAVE_NETINET_IN_H
40 # include <netinet/in.h>
41 #endif
42 #if HAVE_ARPA_INET_H
43 # include <arpa/inet.h>
44 #endif
45 #if HAVE_POLL_H
46 # include <poll.h>
47 #endif
49 /* 1500 - 40 - 8 = Ethernet packet - IPv6 header - UDP header */
50 /* #define BUFF_SIZE 1452 */
52 #ifndef IPV6_ADD_MEMBERSHIP
53 # ifdef IPV6_JOIN_GROUP
54 # define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
55 # else
56 # error "Neither IP_ADD_MEMBERSHIP nor IPV6_JOIN_GROUP is defined"
57 # endif
58 #endif /* !IP_ADD_MEMBERSHIP */
60 #define BUFF_SIZE 1024
62 /*
63 * Private data types
64 */
65 typedef struct sockent
66 {
67 int fd;
68 struct sockaddr_storage *addr;
69 socklen_t addrlen;
70 struct sockent *next;
71 } sockent_t;
73 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
74 * 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
75 * +-------+-----------------------+-------------------------------+
76 * ! Ver. ! ! Length !
77 * +-------+-----------------------+-------------------------------+
78 */
79 struct part_header_s
80 {
81 uint16_t type;
82 uint16_t length;
83 };
84 typedef struct part_header_s part_header_t;
86 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
87 * 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
88 * +-------------------------------+-------------------------------+
89 * ! Type ! Length !
90 * +-------------------------------+-------------------------------+
91 * : (Length - 4) Bytes :
92 * +---------------------------------------------------------------+
93 */
94 struct part_string_s
95 {
96 part_header_t *head;
97 char *value;
98 };
99 typedef struct part_string_s part_string_t;
101 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
102 * 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
103 * +-------------------------------+-------------------------------+
104 * ! Type ! Length !
105 * +-------------------------------+-------------------------------+
106 * : (Length - 4 == 2 || 4 || 8) Bytes :
107 * +---------------------------------------------------------------+
108 */
109 struct part_number_s
110 {
111 part_header_t *head;
112 uint64_t *value;
113 };
114 typedef struct part_number_s part_number_t;
116 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
117 * 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
118 * +-------------------------------+-------------------------------+
119 * ! Type ! Length !
120 * +-------------------------------+---------------+---------------+
121 * ! Num of values ! Type0 ! Type1 !
122 * +-------------------------------+---------------+---------------+
123 * ! Value0 !
124 * ! !
125 * +---------------------------------------------------------------+
126 * ! Value1 !
127 * ! !
128 * +---------------------------------------------------------------+
129 */
130 struct part_values_s
131 {
132 part_header_t *head;
133 uint16_t *num_values;
134 uint8_t *values_types;
135 value_t *values;
136 };
137 typedef struct part_values_s part_values_t;
139 struct receive_list_entry_s
140 {
141 char data[BUFF_SIZE];
142 int data_len;
143 struct receive_list_entry_s *next;
144 };
145 typedef struct receive_list_entry_s receive_list_entry_t;
147 /*
148 * Private variables
149 */
150 static const char *config_keys[] =
151 {
152 "CacheFlush",
153 "Listen",
154 "Server",
155 "TimeToLive",
156 "Forward"
157 };
158 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
160 static int network_config_ttl = 0;
161 static int network_config_forward = 0;
163 static sockent_t *sending_sockets = NULL;
165 static receive_list_entry_t *receive_list_head = NULL;
166 static receive_list_entry_t *receive_list_tail = NULL;
167 static pthread_mutex_t receive_list_lock = PTHREAD_MUTEX_INITIALIZER;
168 static pthread_cond_t receive_list_cond = PTHREAD_COND_INITIALIZER;
170 static struct pollfd *listen_sockets = NULL;
171 static int listen_sockets_num = 0;
173 static int listen_loop = 0;
174 static pthread_t receive_thread_id = 0;
175 static pthread_t dispatch_thread_id = 0;
177 static char send_buffer[BUFF_SIZE];
178 static char *send_buffer_ptr;
179 static int send_buffer_fill;
180 static value_list_t send_buffer_vl = VALUE_LIST_STATIC;
181 static pthread_mutex_t send_buffer_lock = PTHREAD_MUTEX_INITIALIZER;
183 static c_avl_tree_t *cache_tree = NULL;
184 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
185 static time_t cache_flush_last = 0;
186 static int cache_flush_interval = 1800;
188 /*
189 * Private functions
190 */
191 static int cache_flush (void)
192 {
193 char **keys = NULL;
194 int keys_num = 0;
196 char **tmp;
197 int i;
199 char *key;
200 time_t *value;
201 c_avl_iterator_t *iter;
203 time_t curtime = time (NULL);
205 iter = c_avl_get_iterator (cache_tree);
206 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
207 {
208 if ((curtime - *value) <= cache_flush_interval)
209 continue;
210 tmp = (char **) realloc (keys,
211 (keys_num + 1) * sizeof (char *));
212 if (tmp == NULL)
213 {
214 sfree (keys);
215 c_avl_iterator_destroy (iter);
216 ERROR ("network plugin: cache_flush: realloc"
217 " failed.");
218 return (-1);
219 }
220 keys = tmp;
221 keys[keys_num] = key;
222 keys_num++;
223 } /* while (c_avl_iterator_next) */
224 c_avl_iterator_destroy (iter);
226 for (i = 0; i < keys_num; i++)
227 {
228 if (c_avl_remove (cache_tree, keys[i], (void *) &key,
229 (void *) &value) != 0)
230 {
231 WARNING ("network plugin: cache_flush: c_avl_remove"
232 " (%s) failed.", keys[i]);
233 continue;
234 }
236 sfree (key);
237 sfree (value);
238 }
240 sfree (keys);
242 DEBUG ("network plugin: cache_flush: Removed %i %s",
243 keys_num, (keys_num == 1) ? "entry" : "entries");
244 cache_flush_last = curtime;
245 return (0);
246 } /* int cache_flush */
248 static int cache_check (const value_list_t *vl)
249 {
250 char key[1024];
251 time_t *value = NULL;
252 int retval = -1;
254 if (cache_tree == NULL)
255 return (-1);
257 if (format_name (key, sizeof (key), vl->host, vl->plugin,
258 vl->plugin_instance, vl->type, vl->type_instance))
259 return (-1);
261 pthread_mutex_lock (&cache_lock);
263 if (c_avl_get (cache_tree, key, (void *) &value) == 0)
264 {
265 if (*value < vl->time)
266 {
267 *value = vl->time;
268 retval = 0;
269 }
270 else
271 {
272 DEBUG ("network plugin: cache_check: *value = %i >= vl->time = %i",
273 (int) *value, (int) vl->time);
274 retval = 1;
275 }
276 }
277 else
278 {
279 char *key_copy = strdup (key);
280 value = malloc (sizeof (time_t));
281 if ((key_copy != NULL) && (value != NULL))
282 {
283 *value = vl->time;
284 c_avl_insert (cache_tree, key_copy, value);
285 retval = 0;
286 }
287 else
288 {
289 sfree (key_copy);
290 sfree (value);
291 }
292 }
294 if ((time (NULL) - cache_flush_last) > cache_flush_interval)
295 cache_flush ();
297 pthread_mutex_unlock (&cache_lock);
299 DEBUG ("network plugin: cache_check: key = %s; time = %i; retval = %i",
300 key, (int) vl->time, retval);
302 return (retval);
303 } /* int cache_check */
305 static int write_part_values (char **ret_buffer, int *ret_buffer_len,
306 const data_set_t *ds, const value_list_t *vl)
307 {
308 char *packet_ptr;
309 int packet_len;
310 int num_values;
312 part_header_t pkg_ph;
313 uint16_t pkg_num_values;
314 uint8_t *pkg_values_types;
315 value_t *pkg_values;
317 int offset;
318 int i;
320 num_values = vl->values_len;
321 packet_len = sizeof (part_header_t) + sizeof (uint16_t)
322 + (num_values * sizeof (uint8_t))
323 + (num_values * sizeof (value_t));
325 if (*ret_buffer_len < packet_len)
326 return (-1);
328 pkg_values_types = (uint8_t *) malloc (num_values * sizeof (uint8_t));
329 if (pkg_values_types == NULL)
330 {
331 ERROR ("network plugin: write_part_values: malloc failed.");
332 return (-1);
333 }
335 pkg_values = (value_t *) malloc (num_values * sizeof (value_t));
336 if (pkg_values == NULL)
337 {
338 free (pkg_values_types);
339 ERROR ("network plugin: write_part_values: malloc failed.");
340 return (-1);
341 }
343 pkg_ph.type = htons (TYPE_VALUES);
344 pkg_ph.length = htons (packet_len);
346 pkg_num_values = htons ((uint16_t) vl->values_len);
348 for (i = 0; i < num_values; i++)
349 {
350 if (ds->ds[i].type == DS_TYPE_COUNTER)
351 {
352 pkg_values_types[i] = DS_TYPE_COUNTER;
353 pkg_values[i].counter = htonll (vl->values[i].counter);
354 }
355 else
356 {
357 pkg_values_types[i] = DS_TYPE_GAUGE;
358 pkg_values[i].gauge = htond (vl->values[i].gauge);
359 }
360 }
362 /*
363 * Use `memcpy' to write everything to the buffer, because the pointer
364 * may be unaligned and some architectures, such as SPARC, can't handle
365 * that.
366 */
367 packet_ptr = *ret_buffer;
368 offset = 0;
369 memcpy (packet_ptr + offset, &pkg_ph, sizeof (pkg_ph));
370 offset += sizeof (pkg_ph);
371 memcpy (packet_ptr + offset, &pkg_num_values, sizeof (pkg_num_values));
372 offset += sizeof (pkg_num_values);
373 memcpy (packet_ptr + offset, pkg_values_types, num_values * sizeof (uint8_t));
374 offset += num_values * sizeof (uint8_t);
375 memcpy (packet_ptr + offset, pkg_values, num_values * sizeof (value_t));
376 offset += num_values * sizeof (value_t);
378 assert (offset == packet_len);
380 *ret_buffer = packet_ptr + packet_len;
381 *ret_buffer_len -= packet_len;
383 free (pkg_values_types);
384 free (pkg_values);
386 return (0);
387 } /* int write_part_values */
389 static int write_part_number (char **ret_buffer, int *ret_buffer_len,
390 int type, uint64_t value)
391 {
392 char *packet_ptr;
393 int packet_len;
395 part_header_t pkg_head;
396 uint64_t pkg_value;
398 int offset;
400 packet_len = sizeof (pkg_head) + sizeof (pkg_value);
402 if (*ret_buffer_len < packet_len)
403 return (-1);
405 pkg_head.type = htons (type);
406 pkg_head.length = htons (packet_len);
407 pkg_value = htonll (value);
409 packet_ptr = *ret_buffer;
410 offset = 0;
411 memcpy (packet_ptr + offset, &pkg_head, sizeof (pkg_head));
412 offset += sizeof (pkg_head);
413 memcpy (packet_ptr + offset, &pkg_value, sizeof (pkg_value));
414 offset += sizeof (pkg_value);
416 assert (offset == packet_len);
418 *ret_buffer = packet_ptr + packet_len;
419 *ret_buffer_len -= packet_len;
421 return (0);
422 } /* int write_part_number */
424 static int write_part_string (char **ret_buffer, int *ret_buffer_len,
425 int type, const char *str, int str_len)
426 {
427 char *buffer;
428 int buffer_len;
430 uint16_t pkg_type;
431 uint16_t pkg_length;
433 int offset;
435 buffer_len = 2 * sizeof (uint16_t) + str_len + 1;
436 if (*ret_buffer_len < buffer_len)
437 return (-1);
439 pkg_type = htons (type);
440 pkg_length = htons (buffer_len);
442 buffer = *ret_buffer;
443 offset = 0;
444 memcpy (buffer + offset, (void *) &pkg_type, sizeof (pkg_type));
445 offset += sizeof (pkg_type);
446 memcpy (buffer + offset, (void *) &pkg_length, sizeof (pkg_length));
447 offset += sizeof (pkg_length);
448 memcpy (buffer + offset, str, str_len);
449 offset += str_len;
450 memset (buffer + offset, '\0', 1);
451 offset += 1;
453 assert (offset == buffer_len);
455 *ret_buffer = buffer + buffer_len;
456 *ret_buffer_len -= buffer_len;
458 return (0);
459 } /* int write_part_string */
461 static int parse_part_values (void **ret_buffer, int *ret_buffer_len,
462 value_t **ret_values, int *ret_num_values)
463 {
464 char *buffer = *ret_buffer;
465 int buffer_len = *ret_buffer_len;
467 uint16_t tmp16;
468 size_t exp_size;
469 int i;
471 uint16_t pkg_length;
472 uint16_t pkg_type;
473 uint16_t pkg_numval;
475 uint8_t *pkg_types;
476 value_t *pkg_values;
478 if (buffer_len < (15))
479 {
480 DEBUG ("network plugin: packet is too short: buffer_len = %i",
481 buffer_len);
482 return (-1);
483 }
485 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
486 buffer += sizeof (tmp16);
487 pkg_type = ntohs (tmp16);
489 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
490 buffer += sizeof (tmp16);
491 pkg_length = ntohs (tmp16);
493 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
494 buffer += sizeof (tmp16);
495 pkg_numval = ntohs (tmp16);
497 assert (pkg_type == TYPE_VALUES);
499 exp_size = 3 * sizeof (uint16_t)
500 + pkg_numval * (sizeof (uint8_t) + sizeof (value_t));
501 if (buffer_len < exp_size)
502 {
503 WARNING ("network plugin: parse_part_values: "
504 "Packet too short: "
505 "Chunk of size %u expected, "
506 "but buffer has only %i bytes left.",
507 (unsigned int) exp_size, buffer_len);
508 return (-1);
509 }
511 if (pkg_length != exp_size)
512 {
513 WARNING ("network plugin: parse_part_values: "
514 "Length and number of values "
515 "in the packet don't match.");
516 return (-1);
517 }
519 pkg_types = (uint8_t *) malloc (pkg_numval * sizeof (uint8_t));
520 pkg_values = (value_t *) malloc (pkg_numval * sizeof (value_t));
521 if ((pkg_types == NULL) || (pkg_values == NULL))
522 {
523 sfree (pkg_types);
524 sfree (pkg_values);
525 ERROR ("network plugin: parse_part_values: malloc failed.");
526 return (-1);
527 }
529 memcpy ((void *) pkg_types, (void *) buffer, pkg_numval * sizeof (uint8_t));
530 buffer += pkg_numval * sizeof (uint8_t);
531 memcpy ((void *) pkg_values, (void *) buffer, pkg_numval * sizeof (value_t));
532 buffer += pkg_numval * sizeof (value_t);
534 for (i = 0; i < pkg_numval; i++)
535 {
536 if (pkg_types[i] == DS_TYPE_COUNTER)
537 pkg_values[i].counter = ntohll (pkg_values[i].counter);
538 else if (pkg_types[i] == DS_TYPE_GAUGE)
539 pkg_values[i].gauge = ntohd (pkg_values[i].gauge);
540 }
542 *ret_buffer = buffer;
543 *ret_buffer_len = buffer_len - pkg_length;
544 *ret_num_values = pkg_numval;
545 *ret_values = pkg_values;
547 sfree (pkg_types);
549 return (0);
550 } /* int parse_part_values */
552 static int parse_part_number (void **ret_buffer, int *ret_buffer_len,
553 uint64_t *value)
554 {
555 char *buffer = *ret_buffer;
556 int buffer_len = *ret_buffer_len;
558 uint16_t tmp16;
559 uint64_t tmp64;
560 size_t exp_size = 2 * sizeof (uint16_t) + sizeof (uint64_t);
562 uint16_t pkg_length;
563 uint16_t pkg_type;
565 if (buffer_len < exp_size)
566 {
567 WARNING ("network plugin: parse_part_number: "
568 "Packet too short: "
569 "Chunk of size %u expected, "
570 "but buffer has only %i bytes left.",
571 (unsigned int) exp_size, buffer_len);
572 return (-1);
573 }
575 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
576 buffer += sizeof (tmp16);
577 pkg_type = ntohs (tmp16);
579 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
580 buffer += sizeof (tmp16);
581 pkg_length = ntohs (tmp16);
583 memcpy ((void *) &tmp64, buffer, sizeof (tmp64));
584 buffer += sizeof (tmp64);
585 *value = ntohll (tmp64);
587 *ret_buffer = buffer;
588 *ret_buffer_len = buffer_len - pkg_length;
590 return (0);
591 } /* int parse_part_number */
593 static int parse_part_string (void **ret_buffer, int *ret_buffer_len,
594 char *output, int output_len)
595 {
596 char *buffer = *ret_buffer;
597 int buffer_len = *ret_buffer_len;
599 uint16_t tmp16;
600 size_t header_size = 2 * sizeof (uint16_t);
602 uint16_t pkg_length;
603 uint16_t pkg_type;
605 if (buffer_len < header_size)
606 {
607 WARNING ("network plugin: parse_part_string: "
608 "Packet too short: "
609 "Chunk of at least size %u expected, "
610 "but buffer has only %i bytes left.",
611 (unsigned int) header_size, buffer_len);
612 return (-1);
613 }
615 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
616 buffer += sizeof (tmp16);
617 pkg_type = ntohs (tmp16);
619 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
620 buffer += sizeof (tmp16);
621 pkg_length = ntohs (tmp16);
623 /* Check that packet fits in the input buffer */
624 if (pkg_length > buffer_len)
625 {
626 WARNING ("network plugin: parse_part_string: "
627 "Packet too big: "
628 "Chunk of size %hu received, "
629 "but buffer has only %i bytes left.",
630 pkg_length, buffer_len);
631 return (-1);
632 }
634 /* Check that pkg_length is in the valid range */
635 if (pkg_length <= header_size)
636 {
637 WARNING ("network plugin: parse_part_string: "
638 "Packet too short: "
639 "Header claims this packet is only %hu "
640 "bytes long.", pkg_length);
641 return (-1);
642 }
644 /* Check that the package data fits into the output buffer.
645 * The previous if-statement ensures that:
646 * `pkg_length > header_size' */
647 if ((pkg_length - header_size) > output_len)
648 {
649 WARNING ("network plugin: parse_part_string: "
650 "Output buffer too small.");
651 return (-1);
652 }
654 /* All sanity checks successfull, let's copy the data over */
655 output_len = pkg_length - header_size;
656 memcpy ((void *) output, (void *) buffer, output_len);
657 buffer += output_len;
659 /* For some very weird reason '\0' doesn't do the trick on SPARC in
660 * this statement. */
661 if (output[output_len - 1] != 0)
662 {
663 WARNING ("network plugin: parse_part_string: "
664 "Received string does not end "
665 "with a NULL-byte.");
666 return (-1);
667 }
669 *ret_buffer = buffer;
670 *ret_buffer_len = buffer_len - pkg_length;
672 return (0);
673 } /* int parse_part_string */
675 static int parse_packet (void *buffer, int buffer_len)
676 {
677 int status;
679 value_list_t vl = VALUE_LIST_INIT;
680 notification_t n;
682 DEBUG ("network plugin: parse_packet: buffer = %p; buffer_len = %i;",
683 buffer, buffer_len);
685 memset (&vl, '\0', sizeof (vl));
686 memset (&n, '\0', sizeof (n));
687 status = 0;
689 while ((status == 0) && (0 < buffer_len)
690 && ((unsigned int) buffer_len > sizeof (part_header_t)))
691 {
692 uint16_t pkg_length;
693 uint16_t pkg_type;
695 memcpy ((void *) &pkg_type,
696 (void *) buffer,
697 sizeof (pkg_type));
698 memcpy ((void *) &pkg_length,
699 (void *) (buffer + sizeof (pkg_type)),
700 sizeof (pkg_length));
702 pkg_length = ntohs (pkg_length);
703 pkg_type = ntohs (pkg_type);
705 if (pkg_length > buffer_len)
706 break;
707 /* Ensure that this loop terminates eventually */
708 if (pkg_length < (2 * sizeof (uint16_t)))
709 break;
711 if (pkg_type == TYPE_VALUES)
712 {
713 status = parse_part_values (&buffer, &buffer_len,
714 &vl.values, &vl.values_len);
716 if (status != 0)
717 break;
719 if ((vl.time > 0)
720 && (strlen (vl.host) > 0)
721 && (strlen (vl.plugin) > 0)
722 && (strlen (vl.type) > 0)
723 && (cache_check (&vl) == 0))
724 {
725 plugin_dispatch_values (&vl);
726 }
727 else
728 {
729 DEBUG ("network plugin: parse_packet:"
730 " NOT dispatching values");
731 }
733 sfree (vl.values);
734 }
735 else if (pkg_type == TYPE_TIME)
736 {
737 uint64_t tmp = 0;
738 status = parse_part_number (&buffer, &buffer_len,
739 &tmp);
740 if (status == 0)
741 {
742 vl.time = (time_t) tmp;
743 n.time = (time_t) tmp;
744 }
745 }
746 else if (pkg_type == TYPE_INTERVAL)
747 {
748 uint64_t tmp = 0;
749 status = parse_part_number (&buffer, &buffer_len,
750 &tmp);
751 if (status == 0)
752 vl.interval = (int) tmp;
753 }
754 else if (pkg_type == TYPE_HOST)
755 {
756 status = parse_part_string (&buffer, &buffer_len,
757 vl.host, sizeof (vl.host));
758 if (status == 0)
759 sstrncpy (n.host, vl.host, sizeof (n.host));
760 }
761 else if (pkg_type == TYPE_PLUGIN)
762 {
763 status = parse_part_string (&buffer, &buffer_len,
764 vl.plugin, sizeof (vl.plugin));
765 if (status == 0)
766 sstrncpy (n.plugin, vl.plugin,
767 sizeof (n.plugin));
768 }
769 else if (pkg_type == TYPE_PLUGIN_INSTANCE)
770 {
771 status = parse_part_string (&buffer, &buffer_len,
772 vl.plugin_instance,
773 sizeof (vl.plugin_instance));
774 if (status == 0)
775 sstrncpy (n.plugin_instance,
776 vl.plugin_instance,
777 sizeof (n.plugin_instance));
778 }
779 else if (pkg_type == TYPE_TYPE)
780 {
781 status = parse_part_string (&buffer, &buffer_len,
782 vl.type, sizeof (vl.type));
783 if (status == 0)
784 sstrncpy (n.type, vl.type, sizeof (n.type));
785 }
786 else if (pkg_type == TYPE_TYPE_INSTANCE)
787 {
788 status = parse_part_string (&buffer, &buffer_len,
789 vl.type_instance,
790 sizeof (vl.type_instance));
791 if (status == 0)
792 sstrncpy (n.type_instance, vl.type_instance,
793 sizeof (n.type_instance));
794 }
795 else if (pkg_type == TYPE_MESSAGE)
796 {
797 status = parse_part_string (&buffer, &buffer_len,
798 n.message, sizeof (n.message));
800 if (status != 0)
801 {
802 /* do nothing */
803 }
804 else if ((n.severity != NOTIF_FAILURE)
805 && (n.severity != NOTIF_WARNING)
806 && (n.severity != NOTIF_OKAY))
807 {
808 INFO ("network plugin: "
809 "Ignoring notification with "
810 "unknown severity %i.",
811 n.severity);
812 }
813 else if (n.time <= 0)
814 {
815 INFO ("network plugin: "
816 "Ignoring notification with "
817 "time == 0.");
818 }
819 else if (strlen (n.message) <= 0)
820 {
821 INFO ("network plugin: "
822 "Ignoring notification with "
823 "an empty message.");
824 }
825 else
826 {
827 plugin_dispatch_notification (&n);
828 }
829 }
830 else if (pkg_type == TYPE_SEVERITY)
831 {
832 uint64_t tmp = 0;
833 status = parse_part_number (&buffer, &buffer_len,
834 &tmp);
835 if (status == 0)
836 n.severity = (int) tmp;
837 }
838 else
839 {
840 DEBUG ("network plugin: parse_packet: Unknown part"
841 " type: 0x%04hx", pkg_type);
842 buffer = ((char *) buffer) + pkg_length;
843 }
844 } /* while (buffer_len > sizeof (part_header_t)) */
846 return (status);
847 } /* int parse_packet */
849 static void free_sockent (sockent_t *se)
850 {
851 sockent_t *next;
852 while (se != NULL)
853 {
854 next = se->next;
855 free (se->addr);
856 free (se);
857 se = next;
858 }
859 } /* void free_sockent */
861 /*
862 * int network_set_ttl
863 *
864 * Set the `IP_MULTICAST_TTL', `IP_TTL', `IPV6_MULTICAST_HOPS' or
865 * `IPV6_UNICAST_HOPS', depending on which option is applicable.
866 *
867 * The `struct addrinfo' is used to destinguish between unicast and multicast
868 * sockets.
869 */
870 static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
871 {
872 if ((network_config_ttl < 1) || (network_config_ttl > 255))
873 return (-1);
875 DEBUG ("ttl = %i", network_config_ttl);
877 if (ai->ai_family == AF_INET)
878 {
879 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
880 int optname;
882 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
883 optname = IP_MULTICAST_TTL;
884 else
885 optname = IP_TTL;
887 if (setsockopt (se->fd, IPPROTO_IP, optname,
888 &network_config_ttl,
889 sizeof (network_config_ttl)) == -1)
890 {
891 char errbuf[1024];
892 ERROR ("setsockopt: %s",
893 sstrerror (errno, errbuf, sizeof (errbuf)));
894 return (-1);
895 }
896 }
897 else if (ai->ai_family == AF_INET6)
898 {
899 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
900 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
901 int optname;
903 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
904 optname = IPV6_MULTICAST_HOPS;
905 else
906 optname = IPV6_UNICAST_HOPS;
908 if (setsockopt (se->fd, IPPROTO_IPV6, optname,
909 &network_config_ttl,
910 sizeof (network_config_ttl)) == -1)
911 {
912 char errbuf[1024];
913 ERROR ("setsockopt: %s",
914 sstrerror (errno, errbuf,
915 sizeof (errbuf)));
916 return (-1);
917 }
918 }
920 return (0);
921 } /* int network_set_ttl */
923 static int network_bind_socket (const sockent_t *se, const struct addrinfo *ai)
924 {
925 int loop = 0;
926 int yes = 1;
928 /* allow multiple sockets to use the same PORT number */
929 if (setsockopt(se->fd, SOL_SOCKET, SO_REUSEADDR,
930 &yes, sizeof(yes)) == -1) {
931 char errbuf[1024];
932 ERROR ("setsockopt: %s",
933 sstrerror (errno, errbuf, sizeof (errbuf)));
934 return (-1);
935 }
937 DEBUG ("fd = %i; calling `bind'", se->fd);
939 if (bind (se->fd, ai->ai_addr, ai->ai_addrlen) == -1)
940 {
941 char errbuf[1024];
942 ERROR ("bind: %s",
943 sstrerror (errno, errbuf, sizeof (errbuf)));
944 return (-1);
945 }
947 if (ai->ai_family == AF_INET)
948 {
949 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
950 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
951 {
952 struct ip_mreq mreq;
954 DEBUG ("fd = %i; IPv4 multicast address found", se->fd);
956 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
957 mreq.imr_interface.s_addr = htonl (INADDR_ANY);
959 if (setsockopt (se->fd, IPPROTO_IP, IP_MULTICAST_LOOP,
960 &loop, sizeof (loop)) == -1)
961 {
962 char errbuf[1024];
963 ERROR ("setsockopt: %s",
964 sstrerror (errno, errbuf,
965 sizeof (errbuf)));
966 return (-1);
967 }
969 if (setsockopt (se->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
970 &mreq, sizeof (mreq)) == -1)
971 {
972 char errbuf[1024];
973 ERROR ("setsockopt: %s",
974 sstrerror (errno, errbuf,
975 sizeof (errbuf)));
976 return (-1);
977 }
978 }
979 }
980 else if (ai->ai_family == AF_INET6)
981 {
982 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
983 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
984 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
985 {
986 struct ipv6_mreq mreq;
988 DEBUG ("fd = %i; IPv6 multicast address found", se->fd);
990 memcpy (&mreq.ipv6mr_multiaddr,
991 &addr->sin6_addr,
992 sizeof (addr->sin6_addr));
994 /* http://developer.apple.com/documentation/Darwin/Reference/ManPages/man4/ip6.4.html
995 * ipv6mr_interface may be set to zeroes to
996 * choose the default multicast interface or to
997 * the index of a particular multicast-capable
998 * interface if the host is multihomed.
999 * Membership is associ-associated with a
1000 * single interface; programs running on
1001 * multihomed hosts may need to join the same
1002 * group on more than one interface.*/
1003 mreq.ipv6mr_interface = 0;
1005 if (setsockopt (se->fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
1006 &loop, sizeof (loop)) == -1)
1007 {
1008 char errbuf[1024];
1009 ERROR ("setsockopt: %s",
1010 sstrerror (errno, errbuf,
1011 sizeof (errbuf)));
1012 return (-1);
1013 }
1015 if (setsockopt (se->fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
1016 &mreq, sizeof (mreq)) == -1)
1017 {
1018 char errbuf[1024];
1019 ERROR ("setsockopt: %s",
1020 sstrerror (errno, errbuf,
1021 sizeof (errbuf)));
1022 return (-1);
1023 }
1024 }
1025 }
1027 return (0);
1028 } /* int network_bind_socket */
1030 static sockent_t *network_create_socket (const char *node,
1031 const char *service,
1032 int listen)
1033 {
1034 struct addrinfo ai_hints;
1035 struct addrinfo *ai_list, *ai_ptr;
1036 int ai_return;
1038 sockent_t *se_head = NULL;
1039 sockent_t *se_tail = NULL;
1041 DEBUG ("node = %s, service = %s", node, service);
1043 memset (&ai_hints, '\0', sizeof (ai_hints));
1044 ai_hints.ai_flags = 0;
1045 #ifdef AI_PASSIVE
1046 ai_hints.ai_flags |= AI_PASSIVE;
1047 #endif
1048 #ifdef AI_ADDRCONFIG
1049 ai_hints.ai_flags |= AI_ADDRCONFIG;
1050 #endif
1051 ai_hints.ai_family = AF_UNSPEC;
1052 ai_hints.ai_socktype = SOCK_DGRAM;
1053 ai_hints.ai_protocol = IPPROTO_UDP;
1055 ai_return = getaddrinfo (node, service, &ai_hints, &ai_list);
1056 if (ai_return != 0)
1057 {
1058 char errbuf[1024];
1059 ERROR ("getaddrinfo (%s, %s): %s",
1060 (node == NULL) ? "(null)" : node,
1061 (service == NULL) ? "(null)" : service,
1062 (ai_return == EAI_SYSTEM)
1063 ? sstrerror (errno, errbuf, sizeof (errbuf))
1064 : gai_strerror (ai_return));
1065 return (NULL);
1066 }
1068 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1069 {
1070 sockent_t *se;
1072 if ((se = (sockent_t *) malloc (sizeof (sockent_t))) == NULL)
1073 {
1074 char errbuf[1024];
1075 ERROR ("malloc: %s",
1076 sstrerror (errno, errbuf,
1077 sizeof (errbuf)));
1078 continue;
1079 }
1081 if ((se->addr = (struct sockaddr_storage *) malloc (sizeof (struct sockaddr_storage))) == NULL)
1082 {
1083 char errbuf[1024];
1084 ERROR ("malloc: %s",
1085 sstrerror (errno, errbuf,
1086 sizeof (errbuf)));
1087 free (se);
1088 continue;
1089 }
1091 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
1092 memset (se->addr, '\0', sizeof (struct sockaddr_storage));
1093 memcpy (se->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1094 se->addrlen = ai_ptr->ai_addrlen;
1096 se->fd = socket (ai_ptr->ai_family,
1097 ai_ptr->ai_socktype,
1098 ai_ptr->ai_protocol);
1099 se->next = NULL;
1101 if (se->fd == -1)
1102 {
1103 char errbuf[1024];
1104 ERROR ("socket: %s",
1105 sstrerror (errno, errbuf,
1106 sizeof (errbuf)));
1107 free (se->addr);
1108 free (se);
1109 continue;
1110 }
1112 if (listen != 0)
1113 {
1114 if (network_bind_socket (se, ai_ptr) != 0)
1115 {
1116 close (se->fd);
1117 free (se->addr);
1118 free (se);
1119 continue;
1120 }
1121 }
1122 else /* listen == 0 */
1123 {
1124 network_set_ttl (se, ai_ptr);
1125 }
1127 if (se_tail == NULL)
1128 {
1129 se_head = se;
1130 se_tail = se;
1131 }
1132 else
1133 {
1134 se_tail->next = se;
1135 se_tail = se;
1136 }
1138 /* We don't open more than one write-socket per node/service pair.. */
1139 if (listen == 0)
1140 break;
1141 }
1143 freeaddrinfo (ai_list);
1145 return (se_head);
1146 } /* sockent_t *network_create_socket */
1148 static sockent_t *network_create_default_socket (int listen)
1149 {
1150 sockent_t *se_ptr = NULL;
1151 sockent_t *se_head = NULL;
1152 sockent_t *se_tail = NULL;
1154 se_ptr = network_create_socket (NET_DEFAULT_V6_ADDR,
1155 NET_DEFAULT_PORT, listen);
1157 /* Don't send to the same machine in IPv6 and IPv4 if both are available. */
1158 if ((listen == 0) && (se_ptr != NULL))
1159 return (se_ptr);
1161 if (se_ptr != NULL)
1162 {
1163 se_head = se_ptr;
1164 se_tail = se_ptr;
1165 while (se_tail->next != NULL)
1166 se_tail = se_tail->next;
1167 }
1169 se_ptr = network_create_socket (NET_DEFAULT_V4_ADDR, NET_DEFAULT_PORT, listen);
1171 if (se_tail == NULL)
1172 return (se_ptr);
1174 se_tail->next = se_ptr;
1175 return (se_head);
1176 } /* sockent_t *network_create_default_socket */
1178 static int network_add_listen_socket (const char *node, const char *service)
1179 {
1180 sockent_t *se;
1181 sockent_t *se_ptr;
1182 int se_num = 0;
1184 if (service == NULL)
1185 service = NET_DEFAULT_PORT;
1187 if (node == NULL)
1188 se = network_create_default_socket (1 /* listen == true */);
1189 else
1190 se = network_create_socket (node, service, 1 /* listen == true */);
1192 if (se == NULL)
1193 return (-1);
1195 for (se_ptr = se; se_ptr != NULL; se_ptr = se_ptr->next)
1196 se_num++;
1198 listen_sockets = (struct pollfd *) realloc (listen_sockets,
1199 (listen_sockets_num + se_num)
1200 * sizeof (struct pollfd));
1202 for (se_ptr = se; se_ptr != NULL; se_ptr = se_ptr->next)
1203 {
1204 listen_sockets[listen_sockets_num].fd = se_ptr->fd;
1205 listen_sockets[listen_sockets_num].events = POLLIN | POLLPRI;
1206 listen_sockets[listen_sockets_num].revents = 0;
1207 listen_sockets_num++;
1208 } /* for (se) */
1210 free_sockent (se);
1211 return (0);
1212 } /* int network_add_listen_socket */
1214 static int network_add_sending_socket (const char *node, const char *service)
1215 {
1216 sockent_t *se;
1217 sockent_t *se_ptr;
1219 if (service == NULL)
1220 service = NET_DEFAULT_PORT;
1222 if (node == NULL)
1223 se = network_create_default_socket (0 /* listen == false */);
1224 else
1225 se = network_create_socket (node, service, 0 /* listen == false */);
1227 if (se == NULL)
1228 return (-1);
1230 if (sending_sockets == NULL)
1231 {
1232 sending_sockets = se;
1233 return (0);
1234 }
1236 for (se_ptr = sending_sockets; se_ptr->next != NULL; se_ptr = se_ptr->next)
1237 /* seek end */;
1239 se_ptr->next = se;
1240 return (0);
1241 } /* int network_get_listen_socket */
1243 static void *dispatch_thread (void *arg)
1244 {
1245 while (42)
1246 {
1247 receive_list_entry_t *ent;
1249 /* Lock and wait for more data to come in */
1250 pthread_mutex_lock (&receive_list_lock);
1251 while ((listen_loop == 0)
1252 && (receive_list_head == NULL))
1253 pthread_cond_wait (&receive_list_cond, &receive_list_lock);
1255 /* Remove the head entry and unlock */
1256 ent = receive_list_head;
1257 if (ent != NULL)
1258 receive_list_head = ent->next;
1259 pthread_mutex_unlock (&receive_list_lock);
1261 /* Check whether we are supposed to exit. We do NOT check `listen_loop'
1262 * because we dispatch all missing packets before shutting down. */
1263 if (ent == NULL)
1264 break;
1266 parse_packet (ent->data, ent->data_len);
1268 sfree (ent);
1269 } /* while (42) */
1271 return (NULL);
1272 } /* void *receive_thread */
1274 static int network_receive (void)
1275 {
1276 char buffer[BUFF_SIZE];
1277 int buffer_len;
1279 int i;
1280 int status;
1282 if (listen_sockets_num == 0)
1283 network_add_listen_socket (NULL, NULL);
1285 if (listen_sockets_num == 0)
1286 {
1287 ERROR ("network: Failed to open a listening socket.");
1288 return (-1);
1289 }
1291 while (listen_loop == 0)
1292 {
1293 status = poll (listen_sockets, listen_sockets_num, -1);
1295 if (status <= 0)
1296 {
1297 char errbuf[1024];
1298 if (errno == EINTR)
1299 continue;
1300 ERROR ("poll failed: %s",
1301 sstrerror (errno, errbuf, sizeof (errbuf)));
1302 return (-1);
1303 }
1305 for (i = 0; (i < listen_sockets_num) && (status > 0); i++)
1306 {
1307 receive_list_entry_t *ent;
1309 if ((listen_sockets[i].revents & (POLLIN | POLLPRI)) == 0)
1310 continue;
1311 status--;
1313 buffer_len = recv (listen_sockets[i].fd,
1314 buffer, sizeof (buffer),
1315 0 /* no flags */);
1316 if (buffer_len < 0)
1317 {
1318 char errbuf[1024];
1319 ERROR ("recv failed: %s",
1320 sstrerror (errno, errbuf,
1321 sizeof (errbuf)));
1322 return (-1);
1323 }
1325 ent = malloc (sizeof (receive_list_entry_t));
1326 if (ent == NULL)
1327 {
1328 ERROR ("network plugin: malloc failed.");
1329 return (-1);
1330 }
1331 memset (ent, '\0', sizeof (receive_list_entry_t));
1333 /* Hopefully this be optimized out by the compiler. It
1334 * might help prevent stupid bugs in the future though.
1335 */
1336 assert (sizeof (ent->data) == sizeof (buffer));
1338 memcpy (ent->data, buffer, buffer_len);
1339 ent->data_len = buffer_len;
1341 pthread_mutex_lock (&receive_list_lock);
1342 if (receive_list_head == NULL)
1343 {
1344 receive_list_head = ent;
1345 receive_list_tail = ent;
1346 }
1347 else
1348 {
1349 receive_list_tail->next = ent;
1350 receive_list_tail = ent;
1351 }
1352 pthread_cond_signal (&receive_list_cond);
1353 pthread_mutex_unlock (&receive_list_lock);
1354 } /* for (listen_sockets) */
1355 } /* while (listen_loop == 0) */
1357 return (0);
1358 }
1360 static void *receive_thread (void *arg)
1361 {
1362 return (network_receive () ? (void *) 1 : (void *) 0);
1363 } /* void *receive_thread */
1365 static void network_send_buffer (const char *buffer, int buffer_len)
1366 {
1367 sockent_t *se;
1368 int status;
1370 DEBUG ("network plugin: network_send_buffer: buffer_len = %i", buffer_len);
1372 for (se = sending_sockets; se != NULL; se = se->next)
1373 {
1374 while (42)
1375 {
1376 status = sendto (se->fd, buffer, buffer_len, 0 /* no flags */,
1377 (struct sockaddr *) se->addr, se->addrlen);
1378 if (status < 0)
1379 {
1380 char errbuf[1024];
1381 if (errno == EINTR)
1382 continue;
1383 ERROR ("network plugin: sendto failed: %s",
1384 sstrerror (errno, errbuf,
1385 sizeof (errbuf)));
1386 break;
1387 }
1389 break;
1390 } /* while (42) */
1391 } /* for (sending_sockets) */
1392 } /* void network_send_buffer */
1394 static int add_to_buffer (char *buffer, int buffer_size,
1395 value_list_t *vl_def,
1396 const data_set_t *ds, const value_list_t *vl)
1397 {
1398 char *buffer_orig = buffer;
1400 if (strcmp (vl_def->host, vl->host) != 0)
1401 {
1402 if (write_part_string (&buffer, &buffer_size, TYPE_HOST,
1403 vl->host, strlen (vl->host)) != 0)
1404 return (-1);
1405 sstrncpy (vl_def->host, vl->host, sizeof (vl_def->host));
1406 }
1408 if (vl_def->time != vl->time)
1409 {
1410 if (write_part_number (&buffer, &buffer_size, TYPE_TIME,
1411 (uint64_t) vl->time))
1412 return (-1);
1413 vl_def->time = vl->time;
1414 }
1416 if (vl_def->interval != vl->interval)
1417 {
1418 if (write_part_number (&buffer, &buffer_size, TYPE_INTERVAL,
1419 (uint64_t) vl->interval))
1420 return (-1);
1421 vl_def->interval = vl->interval;
1422 }
1424 if (strcmp (vl_def->plugin, vl->plugin) != 0)
1425 {
1426 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN,
1427 vl->plugin, strlen (vl->plugin)) != 0)
1428 return (-1);
1429 sstrncpy (vl_def->plugin, vl->plugin, sizeof (vl_def->plugin));
1430 }
1432 if (strcmp (vl_def->plugin_instance, vl->plugin_instance) != 0)
1433 {
1434 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
1435 vl->plugin_instance,
1436 strlen (vl->plugin_instance)) != 0)
1437 return (-1);
1438 sstrncpy (vl_def->plugin_instance, vl->plugin_instance, sizeof (vl_def->plugin_instance));
1439 }
1441 if (strcmp (vl_def->type, vl->type) != 0)
1442 {
1443 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
1444 vl->type, strlen (vl->type)) != 0)
1445 return (-1);
1446 sstrncpy (vl_def->type, ds->type, sizeof (vl_def->type));
1447 }
1449 if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
1450 {
1451 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE_INSTANCE,
1452 vl->type_instance,
1453 strlen (vl->type_instance)) != 0)
1454 return (-1);
1455 sstrncpy (vl_def->type_instance, vl->type_instance, sizeof (vl_def->type_instance));
1456 }
1458 if (write_part_values (&buffer, &buffer_size, ds, vl) != 0)
1459 return (-1);
1461 return (buffer - buffer_orig);
1462 } /* int add_to_buffer */
1464 static void flush_buffer (void)
1465 {
1466 DEBUG ("network plugin: flush_buffer: send_buffer_fill = %i",
1467 send_buffer_fill);
1469 network_send_buffer (send_buffer, send_buffer_fill);
1470 send_buffer_ptr = send_buffer;
1471 send_buffer_fill = 0;
1472 memset (&send_buffer_vl, 0, sizeof (send_buffer_vl));
1473 }
1475 static int network_write (const data_set_t *ds, const value_list_t *vl)
1476 {
1477 int status;
1479 /* If the value is already in the cache, we have received it via the
1480 * network. We write it again if forwarding is activated. It's then in
1481 * the cache and should we receive it again we will ignore it. */
1482 status = cache_check (vl);
1483 if ((network_config_forward == 0)
1484 && (status != 0))
1485 return (0);
1487 pthread_mutex_lock (&send_buffer_lock);
1489 status = add_to_buffer (send_buffer_ptr,
1490 sizeof (send_buffer) - send_buffer_fill,
1491 &send_buffer_vl,
1492 ds, vl);
1493 if (status >= 0)
1494 {
1495 /* status == bytes added to the buffer */
1496 send_buffer_fill += status;
1497 send_buffer_ptr += status;
1498 }
1499 else
1500 {
1501 flush_buffer ();
1503 status = add_to_buffer (send_buffer_ptr,
1504 sizeof (send_buffer) - send_buffer_fill,
1505 &send_buffer_vl,
1506 ds, vl);
1508 if (status >= 0)
1509 {
1510 send_buffer_fill += status;
1511 send_buffer_ptr += status;
1512 }
1513 }
1515 if (status < 0)
1516 {
1517 ERROR ("network plugin: Unable to append to the "
1518 "buffer for some weird reason");
1519 }
1520 else if ((sizeof (send_buffer) - send_buffer_fill) < 15)
1521 {
1522 flush_buffer ();
1523 }
1525 pthread_mutex_unlock (&send_buffer_lock);
1527 return ((status < 0) ? -1 : 0);
1528 } /* int network_write */
1530 static int network_config (const char *key, const char *val)
1531 {
1532 char *node;
1533 char *service;
1535 char *fields[3];
1536 int fields_num;
1538 if ((strcasecmp ("Listen", key) == 0)
1539 || (strcasecmp ("Server", key) == 0))
1540 {
1541 char *val_cpy = strdup (val);
1542 if (val_cpy == NULL)
1543 return (1);
1545 service = NET_DEFAULT_PORT;
1546 fields_num = strsplit (val_cpy, fields, 3);
1547 if ((fields_num != 1)
1548 && (fields_num != 2))
1549 {
1550 sfree (val_cpy);
1551 return (1);
1552 }
1553 else if (fields_num == 2)
1554 {
1555 if ((service = strchr (fields[1], '.')) != NULL)
1556 *service = '\0';
1557 service = fields[1];
1558 }
1559 node = fields[0];
1561 if (strcasecmp ("Listen", key) == 0)
1562 network_add_listen_socket (node, service);
1563 else
1564 network_add_sending_socket (node, service);
1566 sfree (val_cpy);
1567 }
1568 else if (strcasecmp ("TimeToLive", key) == 0)
1569 {
1570 int tmp = atoi (val);
1571 if ((tmp > 0) && (tmp < 256))
1572 network_config_ttl = tmp;
1573 else
1574 return (1);
1575 }
1576 else if (strcasecmp ("Forward", key) == 0)
1577 {
1578 if ((strcasecmp ("true", val) == 0)
1579 || (strcasecmp ("yes", val) == 0)
1580 || (strcasecmp ("on", val) == 0))
1581 network_config_forward = 1;
1582 else
1583 network_config_forward = 0;
1584 }
1585 else if (strcasecmp ("CacheFlush", key) == 0)
1586 {
1587 int tmp = atoi (val);
1588 if (tmp > 0)
1589 cache_flush_interval = tmp;
1590 else return (1);
1591 }
1592 else
1593 {
1594 return (-1);
1595 }
1596 return (0);
1597 } /* int network_config */
1599 static int network_notification (const notification_t *n)
1600 {
1601 char buffer[BUFF_SIZE];
1602 char *buffer_ptr = buffer;
1603 int buffer_free = sizeof (buffer);
1604 int status;
1606 memset (buffer, '\0', sizeof (buffer));
1609 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_TIME,
1610 (uint64_t) n->time);
1611 if (status != 0)
1612 return (-1);
1614 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_SEVERITY,
1615 (uint64_t) n->severity);
1616 if (status != 0)
1617 return (-1);
1619 if (strlen (n->host) > 0)
1620 {
1621 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_HOST,
1622 n->host, strlen (n->host));
1623 if (status != 0)
1624 return (-1);
1625 }
1627 if (strlen (n->plugin) > 0)
1628 {
1629 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_PLUGIN,
1630 n->plugin, strlen (n->plugin));
1631 if (status != 0)
1632 return (-1);
1633 }
1635 if (strlen (n->plugin_instance) > 0)
1636 {
1637 status = write_part_string (&buffer_ptr, &buffer_free,
1638 TYPE_PLUGIN_INSTANCE,
1639 n->plugin_instance, strlen (n->plugin_instance));
1640 if (status != 0)
1641 return (-1);
1642 }
1644 if (strlen (n->type) > 0)
1645 {
1646 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE,
1647 n->type, strlen (n->type));
1648 if (status != 0)
1649 return (-1);
1650 }
1652 if (strlen (n->type_instance) > 0)
1653 {
1654 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE_INSTANCE,
1655 n->type_instance, strlen (n->type_instance));
1656 if (status != 0)
1657 return (-1);
1658 }
1660 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_MESSAGE,
1661 n->message, strlen (n->message));
1662 if (status != 0)
1663 return (-1);
1665 network_send_buffer (buffer, sizeof (buffer) - buffer_free);
1667 return (0);
1668 } /* int network_notification */
1670 static int network_shutdown (void)
1671 {
1672 listen_loop++;
1674 /* Kill the listening thread */
1675 if (receive_thread_id != (pthread_t) 0)
1676 {
1677 pthread_kill (receive_thread_id, SIGTERM);
1678 pthread_join (receive_thread_id, NULL /* no return value */);
1679 receive_thread_id = (pthread_t) 0;
1680 }
1682 /* Shutdown the dispatching thread */
1683 if (dispatch_thread_id != (pthread_t) 0)
1684 pthread_cond_broadcast (&receive_list_cond);
1686 if (send_buffer_fill > 0)
1687 flush_buffer ();
1689 if (cache_tree != NULL)
1690 {
1691 void *key;
1692 void *value;
1694 while (c_avl_pick (cache_tree, &key, &value) == 0)
1695 {
1696 sfree (key);
1697 sfree (value);
1698 }
1699 c_avl_destroy (cache_tree);
1700 cache_tree = NULL;
1701 }
1703 /* TODO: Close `sending_sockets' */
1705 plugin_unregister_config ("network");
1706 plugin_unregister_init ("network");
1707 plugin_unregister_write ("network");
1708 plugin_unregister_shutdown ("network");
1710 /* Let the init function do it's move again ;) */
1711 cache_flush_last = 0;
1713 return (0);
1714 } /* int network_shutdown */
1716 static int network_init (void)
1717 {
1718 /* Check if we were already initialized. If so, just return - there's
1719 * nothing more to do (for now, that is). */
1720 if (cache_flush_last != 0)
1721 return (0);
1723 plugin_register_shutdown ("network", network_shutdown);
1725 send_buffer_ptr = send_buffer;
1726 send_buffer_fill = 0;
1727 memset (&send_buffer_vl, 0, sizeof (send_buffer_vl));
1729 cache_tree = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1730 cache_flush_last = time (NULL);
1732 /* setup socket(s) and so on */
1733 if (sending_sockets != NULL)
1734 {
1735 plugin_register_write ("network", network_write);
1736 plugin_register_notification ("network", network_notification);
1737 }
1739 if ((listen_sockets_num != 0) && (receive_thread_id == 0))
1740 {
1741 int status;
1743 status = pthread_create (&dispatch_thread_id,
1744 NULL /* no attributes */,
1745 dispatch_thread,
1746 NULL /* no argument */);
1747 if (status != 0)
1748 {
1749 char errbuf[1024];
1750 ERROR ("network: pthread_create failed: %s",
1751 sstrerror (errno, errbuf,
1752 sizeof (errbuf)));
1753 }
1755 status = pthread_create (&receive_thread_id,
1756 NULL /* no attributes */,
1757 receive_thread,
1758 NULL /* no argument */);
1759 if (status != 0)
1760 {
1761 char errbuf[1024];
1762 ERROR ("network: pthread_create failed: %s",
1763 sstrerror (errno, errbuf,
1764 sizeof (errbuf)));
1765 }
1766 }
1767 return (0);
1768 } /* int network_init */
1770 /*
1771 * The flush option of the network plugin cannot flush individual identifiers.
1772 * All the values are added to a buffer and sent when the buffer is full, the
1773 * requested value may or may not be in there, it's not worth finding out. We
1774 * just send the buffer if `flush' is called - if the requested value was in
1775 * there, good. If not, well, then there is nothing to flush.. -octo
1776 */
1777 static int network_flush (int timeout, const char *identifier)
1778 {
1779 pthread_mutex_lock (&send_buffer_lock);
1781 if (((time (NULL) - cache_flush_last) >= timeout)
1782 && (send_buffer_fill > 0))
1783 {
1784 flush_buffer ();
1785 }
1787 pthread_mutex_unlock (&send_buffer_lock);
1789 return (0);
1790 } /* int network_flush */
1792 void module_register (void)
1793 {
1794 plugin_register_config ("network", network_config,
1795 config_keys, config_keys_num);
1796 plugin_register_init ("network", network_init);
1797 plugin_register_flush ("network", network_flush);
1798 } /* void module_register */