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 char send_buffer_type[DATA_MAX_NAME_LEN];
182 static pthread_mutex_t send_buffer_lock = PTHREAD_MUTEX_INITIALIZER;
184 static c_avl_tree_t *cache_tree = NULL;
185 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
186 static time_t cache_flush_last = 0;
187 static int cache_flush_interval = 1800;
189 /*
190 * Private functions
191 */
192 static int cache_flush (void)
193 {
194 char **keys = NULL;
195 int keys_num = 0;
197 char **tmp;
198 int i;
200 char *key;
201 time_t *value;
202 c_avl_iterator_t *iter;
204 time_t curtime = time (NULL);
206 iter = c_avl_get_iterator (cache_tree);
207 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
208 {
209 if ((curtime - *value) <= cache_flush_interval)
210 continue;
211 tmp = (char **) realloc (keys,
212 (keys_num + 1) * sizeof (char *));
213 if (tmp == NULL)
214 {
215 sfree (keys);
216 c_avl_iterator_destroy (iter);
217 ERROR ("network plugin: cache_flush: realloc"
218 " failed.");
219 return (-1);
220 }
221 keys = tmp;
222 keys[keys_num] = key;
223 keys_num++;
224 } /* while (c_avl_iterator_next) */
225 c_avl_iterator_destroy (iter);
227 for (i = 0; i < keys_num; i++)
228 {
229 if (c_avl_remove (cache_tree, keys[i], (void *) &key,
230 (void *) &value) != 0)
231 {
232 WARNING ("network plugin: cache_flush: c_avl_remove"
233 " (%s) failed.", keys[i]);
234 continue;
235 }
237 sfree (key);
238 sfree (value);
239 }
241 sfree (keys);
243 DEBUG ("network plugin: cache_flush: Removed %i %s",
244 keys_num, (keys_num == 1) ? "entry" : "entries");
245 cache_flush_last = curtime;
246 return (0);
247 } /* int cache_flush */
249 static int cache_check (const value_list_t *vl)
250 {
251 char key[1024];
252 time_t *value = NULL;
253 int retval = -1;
255 if (cache_tree == NULL)
256 return (-1);
258 if (format_name (key, sizeof (key), vl->host, vl->plugin,
259 vl->plugin_instance, vl->type, vl->type_instance))
260 return (-1);
262 pthread_mutex_lock (&cache_lock);
264 if (c_avl_get (cache_tree, key, (void *) &value) == 0)
265 {
266 if (*value < vl->time)
267 {
268 *value = vl->time;
269 retval = 0;
270 }
271 else
272 {
273 DEBUG ("network plugin: cache_check: *value = %i >= vl->time = %i",
274 (int) *value, (int) vl->time);
275 retval = 1;
276 }
277 }
278 else
279 {
280 char *key_copy = strdup (key);
281 value = malloc (sizeof (time_t));
282 if ((key_copy != NULL) && (value != NULL))
283 {
284 *value = vl->time;
285 c_avl_insert (cache_tree, key_copy, value);
286 retval = 0;
287 }
288 else
289 {
290 sfree (key_copy);
291 sfree (value);
292 }
293 }
295 if ((time (NULL) - cache_flush_last) > cache_flush_interval)
296 cache_flush ();
298 pthread_mutex_unlock (&cache_lock);
300 DEBUG ("network plugin: cache_check: key = %s; time = %i; retval = %i",
301 key, (int) vl->time, retval);
303 return (retval);
304 } /* int cache_check */
306 static int write_part_values (char **ret_buffer, int *ret_buffer_len,
307 const data_set_t *ds, const value_list_t *vl)
308 {
309 char *packet_ptr;
310 int packet_len;
311 int num_values;
313 part_header_t pkg_ph;
314 uint16_t pkg_num_values;
315 uint8_t *pkg_values_types;
316 value_t *pkg_values;
318 int offset;
319 int i;
321 num_values = vl->values_len;
322 packet_len = sizeof (part_header_t) + sizeof (uint16_t)
323 + (num_values * sizeof (uint8_t))
324 + (num_values * sizeof (value_t));
326 if (*ret_buffer_len < packet_len)
327 return (-1);
329 pkg_values_types = (uint8_t *) malloc (num_values * sizeof (uint8_t));
330 if (pkg_values_types == NULL)
331 {
332 ERROR ("network plugin: write_part_values: malloc failed.");
333 return (-1);
334 }
336 pkg_values = (value_t *) malloc (num_values * sizeof (value_t));
337 if (pkg_values == NULL)
338 {
339 free (pkg_values_types);
340 ERROR ("network plugin: write_part_values: malloc failed.");
341 return (-1);
342 }
344 pkg_ph.type = htons (TYPE_VALUES);
345 pkg_ph.length = htons (packet_len);
347 pkg_num_values = htons ((uint16_t) vl->values_len);
349 for (i = 0; i < num_values; i++)
350 {
351 if (ds->ds[i].type == DS_TYPE_COUNTER)
352 {
353 pkg_values_types[i] = DS_TYPE_COUNTER;
354 pkg_values[i].counter = htonll (vl->values[i].counter);
355 }
356 else
357 {
358 pkg_values_types[i] = DS_TYPE_GAUGE;
359 pkg_values[i].gauge = htond (vl->values[i].gauge);
360 }
361 }
363 /*
364 * Use `memcpy' to write everything to the buffer, because the pointer
365 * may be unaligned and some architectures, such as SPARC, can't handle
366 * that.
367 */
368 packet_ptr = *ret_buffer;
369 offset = 0;
370 memcpy (packet_ptr + offset, &pkg_ph, sizeof (pkg_ph));
371 offset += sizeof (pkg_ph);
372 memcpy (packet_ptr + offset, &pkg_num_values, sizeof (pkg_num_values));
373 offset += sizeof (pkg_num_values);
374 memcpy (packet_ptr + offset, pkg_values_types, num_values * sizeof (uint8_t));
375 offset += num_values * sizeof (uint8_t);
376 memcpy (packet_ptr + offset, pkg_values, num_values * sizeof (value_t));
377 offset += num_values * sizeof (value_t);
379 assert (offset == packet_len);
381 *ret_buffer = packet_ptr + packet_len;
382 *ret_buffer_len -= packet_len;
384 free (pkg_values_types);
385 free (pkg_values);
387 return (0);
388 } /* int write_part_values */
390 static int write_part_number (char **ret_buffer, int *ret_buffer_len,
391 int type, uint64_t value)
392 {
393 char *packet_ptr;
394 int packet_len;
396 part_header_t pkg_head;
397 uint64_t pkg_value;
399 int offset;
401 packet_len = sizeof (pkg_head) + sizeof (pkg_value);
403 if (*ret_buffer_len < packet_len)
404 return (-1);
406 pkg_head.type = htons (type);
407 pkg_head.length = htons (packet_len);
408 pkg_value = htonll (value);
410 packet_ptr = *ret_buffer;
411 offset = 0;
412 memcpy (packet_ptr + offset, &pkg_head, sizeof (pkg_head));
413 offset += sizeof (pkg_head);
414 memcpy (packet_ptr + offset, &pkg_value, sizeof (pkg_value));
415 offset += sizeof (pkg_value);
417 assert (offset == packet_len);
419 *ret_buffer = packet_ptr + packet_len;
420 *ret_buffer_len -= packet_len;
422 return (0);
423 } /* int write_part_number */
425 static int write_part_string (char **ret_buffer, int *ret_buffer_len,
426 int type, const char *str, int str_len)
427 {
428 char *buffer;
429 int buffer_len;
431 uint16_t pkg_type;
432 uint16_t pkg_length;
434 int offset;
436 buffer_len = 2 * sizeof (uint16_t) + str_len + 1;
437 if (*ret_buffer_len < buffer_len)
438 return (-1);
440 pkg_type = htons (type);
441 pkg_length = htons (buffer_len);
443 buffer = *ret_buffer;
444 offset = 0;
445 memcpy (buffer + offset, (void *) &pkg_type, sizeof (pkg_type));
446 offset += sizeof (pkg_type);
447 memcpy (buffer + offset, (void *) &pkg_length, sizeof (pkg_length));
448 offset += sizeof (pkg_length);
449 memcpy (buffer + offset, str, str_len);
450 offset += str_len;
451 memset (buffer + offset, '\0', 1);
452 offset += 1;
454 assert (offset == buffer_len);
456 *ret_buffer = buffer + buffer_len;
457 *ret_buffer_len -= buffer_len;
459 return (0);
460 } /* int write_part_string */
462 static int parse_part_values (void **ret_buffer, int *ret_buffer_len,
463 value_t **ret_values, int *ret_num_values)
464 {
465 char *buffer = *ret_buffer;
466 int buffer_len = *ret_buffer_len;
468 uint16_t tmp16;
469 size_t exp_size;
470 int i;
472 uint16_t pkg_length;
473 uint16_t pkg_type;
474 uint16_t pkg_numval;
476 uint8_t *pkg_types;
477 value_t *pkg_values;
479 if (buffer_len < (15))
480 {
481 DEBUG ("network plugin: packet is too short: buffer_len = %i",
482 buffer_len);
483 return (-1);
484 }
486 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
487 buffer += sizeof (tmp16);
488 pkg_type = ntohs (tmp16);
490 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
491 buffer += sizeof (tmp16);
492 pkg_length = ntohs (tmp16);
494 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
495 buffer += sizeof (tmp16);
496 pkg_numval = ntohs (tmp16);
498 assert (pkg_type == TYPE_VALUES);
500 exp_size = 3 * sizeof (uint16_t)
501 + pkg_numval * (sizeof (uint8_t) + sizeof (value_t));
502 if (buffer_len < exp_size)
503 {
504 WARNING ("network plugin: parse_part_values: "
505 "Packet too short: "
506 "Chunk of size %u expected, "
507 "but buffer has only %i bytes left.",
508 (unsigned int) exp_size, buffer_len);
509 return (-1);
510 }
512 if (pkg_length != exp_size)
513 {
514 WARNING ("network plugin: parse_part_values: "
515 "Length and number of values "
516 "in the packet don't match.");
517 return (-1);
518 }
520 pkg_types = (uint8_t *) malloc (pkg_numval * sizeof (uint8_t));
521 pkg_values = (value_t *) malloc (pkg_numval * sizeof (value_t));
522 if ((pkg_types == NULL) || (pkg_values == NULL))
523 {
524 sfree (pkg_types);
525 sfree (pkg_values);
526 ERROR ("network plugin: parse_part_values: malloc failed.");
527 return (-1);
528 }
530 memcpy ((void *) pkg_types, (void *) buffer, pkg_numval * sizeof (uint8_t));
531 buffer += pkg_numval * sizeof (uint8_t);
532 memcpy ((void *) pkg_values, (void *) buffer, pkg_numval * sizeof (value_t));
533 buffer += pkg_numval * sizeof (value_t);
535 for (i = 0; i < pkg_numval; i++)
536 {
537 if (pkg_types[i] == DS_TYPE_COUNTER)
538 pkg_values[i].counter = ntohll (pkg_values[i].counter);
539 else if (pkg_types[i] == DS_TYPE_GAUGE)
540 pkg_values[i].gauge = ntohd (pkg_values[i].gauge);
541 }
543 *ret_buffer = buffer;
544 *ret_buffer_len = buffer_len - pkg_length;
545 *ret_num_values = pkg_numval;
546 *ret_values = pkg_values;
548 sfree (pkg_types);
550 return (0);
551 } /* int parse_part_values */
553 static int parse_part_number (void **ret_buffer, int *ret_buffer_len,
554 uint64_t *value)
555 {
556 char *buffer = *ret_buffer;
557 int buffer_len = *ret_buffer_len;
559 uint16_t tmp16;
560 uint64_t tmp64;
561 size_t exp_size = 2 * sizeof (uint16_t) + sizeof (uint64_t);
563 uint16_t pkg_length;
564 uint16_t pkg_type;
566 if (buffer_len < exp_size)
567 {
568 WARNING ("network plugin: parse_part_number: "
569 "Packet too short: "
570 "Chunk of size %u expected, "
571 "but buffer has only %i bytes left.",
572 (unsigned int) exp_size, buffer_len);
573 return (-1);
574 }
576 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
577 buffer += sizeof (tmp16);
578 pkg_type = ntohs (tmp16);
580 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
581 buffer += sizeof (tmp16);
582 pkg_length = ntohs (tmp16);
584 memcpy ((void *) &tmp64, buffer, sizeof (tmp64));
585 buffer += sizeof (tmp64);
586 *value = ntohll (tmp64);
588 *ret_buffer = buffer;
589 *ret_buffer_len = buffer_len - pkg_length;
591 return (0);
592 } /* int parse_part_number */
594 static int parse_part_string (void **ret_buffer, int *ret_buffer_len,
595 char *output, int output_len)
596 {
597 char *buffer = *ret_buffer;
598 int buffer_len = *ret_buffer_len;
600 uint16_t tmp16;
601 size_t header_size = 2 * sizeof (uint16_t);
603 uint16_t pkg_length;
604 uint16_t pkg_type;
606 if (buffer_len < header_size)
607 {
608 WARNING ("network plugin: parse_part_string: "
609 "Packet too short: "
610 "Chunk of at least size %u expected, "
611 "but buffer has only %i bytes left.",
612 (unsigned int) header_size, buffer_len);
613 return (-1);
614 }
616 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
617 buffer += sizeof (tmp16);
618 pkg_type = ntohs (tmp16);
620 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
621 buffer += sizeof (tmp16);
622 pkg_length = ntohs (tmp16);
624 /* Check that packet fits in the input buffer */
625 if (pkg_length > buffer_len)
626 {
627 WARNING ("network plugin: parse_part_string: "
628 "Packet too big: "
629 "Chunk of size %hu received, "
630 "but buffer has only %i bytes left.",
631 pkg_length, buffer_len);
632 return (-1);
633 }
635 /* Check that pkg_length is in the valid range */
636 if (pkg_length <= header_size)
637 {
638 WARNING ("network plugin: parse_part_string: "
639 "Packet too short: "
640 "Header claims this packet is only %hu "
641 "bytes long.", pkg_length);
642 return (-1);
643 }
645 /* Check that the package data fits into the output buffer.
646 * The previous if-statement ensures that:
647 * `pkg_length > header_size' */
648 if ((pkg_length - header_size) > output_len)
649 {
650 WARNING ("network plugin: parse_part_string: "
651 "Output buffer too small.");
652 return (-1);
653 }
655 /* All sanity checks successfull, let's copy the data over */
656 output_len = pkg_length - header_size;
657 memcpy ((void *) output, (void *) buffer, output_len);
658 buffer += output_len;
660 /* For some very weird reason '\0' doesn't do the trick on SPARC in
661 * this statement. */
662 if (output[output_len - 1] != 0)
663 {
664 WARNING ("network plugin: parse_part_string: "
665 "Received string does not end "
666 "with a NULL-byte.");
667 return (-1);
668 }
670 *ret_buffer = buffer;
671 *ret_buffer_len = buffer_len - pkg_length;
673 return (0);
674 } /* int parse_part_string */
676 static int parse_packet (void *buffer, int buffer_len)
677 {
678 int status;
680 value_list_t vl = VALUE_LIST_INIT;
681 notification_t n;
683 DEBUG ("network plugin: parse_packet: buffer = %p; buffer_len = %i;",
684 buffer, buffer_len);
686 memset (&vl, '\0', sizeof (vl));
687 memset (&n, '\0', sizeof (n));
688 status = 0;
690 while ((status == 0) && (0 < buffer_len)
691 && ((unsigned int) buffer_len > sizeof (part_header_t)))
692 {
693 uint16_t pkg_length;
694 uint16_t pkg_type;
696 memcpy ((void *) &pkg_type,
697 (void *) buffer,
698 sizeof (pkg_type));
699 memcpy ((void *) &pkg_length,
700 (void *) (buffer + sizeof (pkg_type)),
701 sizeof (pkg_length));
703 pkg_length = ntohs (pkg_length);
704 pkg_type = ntohs (pkg_type);
706 if (pkg_length > buffer_len)
707 break;
708 /* Ensure that this loop terminates eventually */
709 if (pkg_length < (2 * sizeof (uint16_t)))
710 break;
712 if (pkg_type == TYPE_VALUES)
713 {
714 status = parse_part_values (&buffer, &buffer_len,
715 &vl.values, &vl.values_len);
717 if (status != 0)
718 break;
720 if ((vl.time > 0)
721 && (strlen (vl.host) > 0)
722 && (strlen (vl.plugin) > 0)
723 && (strlen (vl.type) > 0)
724 && (cache_check (&vl) == 0))
725 {
726 plugin_dispatch_values (&vl);
727 }
728 else
729 {
730 DEBUG ("network plugin: parse_packet:"
731 " NOT dispatching values");
732 }
734 sfree (vl.values);
735 }
736 else if (pkg_type == TYPE_TIME)
737 {
738 uint64_t tmp = 0;
739 status = parse_part_number (&buffer, &buffer_len,
740 &tmp);
741 if (status == 0)
742 {
743 vl.time = (time_t) tmp;
744 n.time = (time_t) tmp;
745 }
746 }
747 else if (pkg_type == TYPE_INTERVAL)
748 {
749 uint64_t tmp = 0;
750 status = parse_part_number (&buffer, &buffer_len,
751 &tmp);
752 if (status == 0)
753 vl.interval = (int) tmp;
754 }
755 else if (pkg_type == TYPE_HOST)
756 {
757 status = parse_part_string (&buffer, &buffer_len,
758 vl.host, sizeof (vl.host));
759 if (status == 0)
760 sstrncpy (n.host, vl.host, sizeof (n.host));
761 }
762 else if (pkg_type == TYPE_PLUGIN)
763 {
764 status = parse_part_string (&buffer, &buffer_len,
765 vl.plugin, sizeof (vl.plugin));
766 if (status == 0)
767 sstrncpy (n.plugin, vl.plugin,
768 sizeof (n.plugin));
769 }
770 else if (pkg_type == TYPE_PLUGIN_INSTANCE)
771 {
772 status = parse_part_string (&buffer, &buffer_len,
773 vl.plugin_instance,
774 sizeof (vl.plugin_instance));
775 if (status == 0)
776 sstrncpy (n.plugin_instance,
777 vl.plugin_instance,
778 sizeof (n.plugin_instance));
779 }
780 else if (pkg_type == TYPE_TYPE)
781 {
782 status = parse_part_string (&buffer, &buffer_len,
783 vl.type, sizeof (vl.type));
784 if (status == 0)
785 sstrncpy (n.type, vl.type, sizeof (n.type));
786 }
787 else if (pkg_type == TYPE_TYPE_INSTANCE)
788 {
789 status = parse_part_string (&buffer, &buffer_len,
790 vl.type_instance,
791 sizeof (vl.type_instance));
792 if (status == 0)
793 sstrncpy (n.type_instance, vl.type_instance,
794 sizeof (n.type_instance));
795 }
796 else if (pkg_type == TYPE_MESSAGE)
797 {
798 status = parse_part_string (&buffer, &buffer_len,
799 n.message, sizeof (n.message));
801 if (status != 0)
802 {
803 /* do nothing */
804 }
805 else if ((n.severity != NOTIF_FAILURE)
806 && (n.severity != NOTIF_WARNING)
807 && (n.severity != NOTIF_OKAY))
808 {
809 INFO ("network plugin: "
810 "Ignoring notification with "
811 "unknown severity %i.",
812 n.severity);
813 }
814 else if (n.time <= 0)
815 {
816 INFO ("network plugin: "
817 "Ignoring notification with "
818 "time == 0.");
819 }
820 else if (strlen (n.message) <= 0)
821 {
822 INFO ("network plugin: "
823 "Ignoring notification with "
824 "an empty message.");
825 }
826 else
827 {
828 plugin_dispatch_notification (&n);
829 }
830 }
831 else if (pkg_type == TYPE_SEVERITY)
832 {
833 uint64_t tmp = 0;
834 status = parse_part_number (&buffer, &buffer_len,
835 &tmp);
836 if (status == 0)
837 n.severity = (int) tmp;
838 }
839 else
840 {
841 DEBUG ("network plugin: parse_packet: Unknown part"
842 " type: 0x%04hx", pkg_type);
843 buffer = ((char *) buffer) + pkg_length;
844 }
845 } /* while (buffer_len > sizeof (part_header_t)) */
847 return (status);
848 } /* int parse_packet */
850 static void free_sockent (sockent_t *se)
851 {
852 sockent_t *next;
853 while (se != NULL)
854 {
855 next = se->next;
856 free (se->addr);
857 free (se);
858 se = next;
859 }
860 } /* void free_sockent */
862 /*
863 * int network_set_ttl
864 *
865 * Set the `IP_MULTICAST_TTL', `IP_TTL', `IPV6_MULTICAST_HOPS' or
866 * `IPV6_UNICAST_HOPS', depending on which option is applicable.
867 *
868 * The `struct addrinfo' is used to destinguish between unicast and multicast
869 * sockets.
870 */
871 static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
872 {
873 if ((network_config_ttl < 1) || (network_config_ttl > 255))
874 return (-1);
876 DEBUG ("ttl = %i", network_config_ttl);
878 if (ai->ai_family == AF_INET)
879 {
880 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
881 int optname;
883 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
884 optname = IP_MULTICAST_TTL;
885 else
886 optname = IP_TTL;
888 if (setsockopt (se->fd, IPPROTO_IP, optname,
889 &network_config_ttl,
890 sizeof (network_config_ttl)) == -1)
891 {
892 char errbuf[1024];
893 ERROR ("setsockopt: %s",
894 sstrerror (errno, errbuf, sizeof (errbuf)));
895 return (-1);
896 }
897 }
898 else if (ai->ai_family == AF_INET6)
899 {
900 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
901 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
902 int optname;
904 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
905 optname = IPV6_MULTICAST_HOPS;
906 else
907 optname = IPV6_UNICAST_HOPS;
909 if (setsockopt (se->fd, IPPROTO_IPV6, optname,
910 &network_config_ttl,
911 sizeof (network_config_ttl)) == -1)
912 {
913 char errbuf[1024];
914 ERROR ("setsockopt: %s",
915 sstrerror (errno, errbuf,
916 sizeof (errbuf)));
917 return (-1);
918 }
919 }
921 return (0);
922 } /* int network_set_ttl */
924 static int network_bind_socket (const sockent_t *se, const struct addrinfo *ai)
925 {
926 int loop = 0;
927 int yes = 1;
929 /* allow multiple sockets to use the same PORT number */
930 if (setsockopt(se->fd, SOL_SOCKET, SO_REUSEADDR,
931 &yes, sizeof(yes)) == -1) {
932 char errbuf[1024];
933 ERROR ("setsockopt: %s",
934 sstrerror (errno, errbuf, sizeof (errbuf)));
935 return (-1);
936 }
938 DEBUG ("fd = %i; calling `bind'", se->fd);
940 if (bind (se->fd, ai->ai_addr, ai->ai_addrlen) == -1)
941 {
942 char errbuf[1024];
943 ERROR ("bind: %s",
944 sstrerror (errno, errbuf, sizeof (errbuf)));
945 return (-1);
946 }
948 if (ai->ai_family == AF_INET)
949 {
950 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
951 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
952 {
953 struct ip_mreq mreq;
955 DEBUG ("fd = %i; IPv4 multicast address found", se->fd);
957 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
958 mreq.imr_interface.s_addr = htonl (INADDR_ANY);
960 if (setsockopt (se->fd, IPPROTO_IP, IP_MULTICAST_LOOP,
961 &loop, sizeof (loop)) == -1)
962 {
963 char errbuf[1024];
964 ERROR ("setsockopt: %s",
965 sstrerror (errno, errbuf,
966 sizeof (errbuf)));
967 return (-1);
968 }
970 if (setsockopt (se->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
971 &mreq, sizeof (mreq)) == -1)
972 {
973 char errbuf[1024];
974 ERROR ("setsockopt: %s",
975 sstrerror (errno, errbuf,
976 sizeof (errbuf)));
977 return (-1);
978 }
979 }
980 }
981 else if (ai->ai_family == AF_INET6)
982 {
983 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
984 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
985 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
986 {
987 struct ipv6_mreq mreq;
989 DEBUG ("fd = %i; IPv6 multicast address found", se->fd);
991 memcpy (&mreq.ipv6mr_multiaddr,
992 &addr->sin6_addr,
993 sizeof (addr->sin6_addr));
995 /* http://developer.apple.com/documentation/Darwin/Reference/ManPages/man4/ip6.4.html
996 * ipv6mr_interface may be set to zeroes to
997 * choose the default multicast interface or to
998 * the index of a particular multicast-capable
999 * interface if the host is multihomed.
1000 * Membership is associ-associated with a
1001 * single interface; programs running on
1002 * multihomed hosts may need to join the same
1003 * group on more than one interface.*/
1004 mreq.ipv6mr_interface = 0;
1006 if (setsockopt (se->fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
1007 &loop, sizeof (loop)) == -1)
1008 {
1009 char errbuf[1024];
1010 ERROR ("setsockopt: %s",
1011 sstrerror (errno, errbuf,
1012 sizeof (errbuf)));
1013 return (-1);
1014 }
1016 if (setsockopt (se->fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
1017 &mreq, sizeof (mreq)) == -1)
1018 {
1019 char errbuf[1024];
1020 ERROR ("setsockopt: %s",
1021 sstrerror (errno, errbuf,
1022 sizeof (errbuf)));
1023 return (-1);
1024 }
1025 }
1026 }
1028 return (0);
1029 } /* int network_bind_socket */
1031 static sockent_t *network_create_socket (const char *node,
1032 const char *service,
1033 int listen)
1034 {
1035 struct addrinfo ai_hints;
1036 struct addrinfo *ai_list, *ai_ptr;
1037 int ai_return;
1039 sockent_t *se_head = NULL;
1040 sockent_t *se_tail = NULL;
1042 DEBUG ("node = %s, service = %s", node, service);
1044 memset (&ai_hints, '\0', sizeof (ai_hints));
1045 ai_hints.ai_flags = 0;
1046 #ifdef AI_PASSIVE
1047 ai_hints.ai_flags |= AI_PASSIVE;
1048 #endif
1049 #ifdef AI_ADDRCONFIG
1050 ai_hints.ai_flags |= AI_ADDRCONFIG;
1051 #endif
1052 ai_hints.ai_family = AF_UNSPEC;
1053 ai_hints.ai_socktype = SOCK_DGRAM;
1054 ai_hints.ai_protocol = IPPROTO_UDP;
1056 ai_return = getaddrinfo (node, service, &ai_hints, &ai_list);
1057 if (ai_return != 0)
1058 {
1059 char errbuf[1024];
1060 ERROR ("getaddrinfo (%s, %s): %s",
1061 (node == NULL) ? "(null)" : node,
1062 (service == NULL) ? "(null)" : service,
1063 (ai_return == EAI_SYSTEM)
1064 ? sstrerror (errno, errbuf, sizeof (errbuf))
1065 : gai_strerror (ai_return));
1066 return (NULL);
1067 }
1069 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1070 {
1071 sockent_t *se;
1073 if ((se = (sockent_t *) malloc (sizeof (sockent_t))) == NULL)
1074 {
1075 char errbuf[1024];
1076 ERROR ("malloc: %s",
1077 sstrerror (errno, errbuf,
1078 sizeof (errbuf)));
1079 continue;
1080 }
1082 if ((se->addr = (struct sockaddr_storage *) malloc (sizeof (struct sockaddr_storage))) == NULL)
1083 {
1084 char errbuf[1024];
1085 ERROR ("malloc: %s",
1086 sstrerror (errno, errbuf,
1087 sizeof (errbuf)));
1088 free (se);
1089 continue;
1090 }
1092 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
1093 memset (se->addr, '\0', sizeof (struct sockaddr_storage));
1094 memcpy (se->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1095 se->addrlen = ai_ptr->ai_addrlen;
1097 se->fd = socket (ai_ptr->ai_family,
1098 ai_ptr->ai_socktype,
1099 ai_ptr->ai_protocol);
1100 se->next = NULL;
1102 if (se->fd == -1)
1103 {
1104 char errbuf[1024];
1105 ERROR ("socket: %s",
1106 sstrerror (errno, errbuf,
1107 sizeof (errbuf)));
1108 free (se->addr);
1109 free (se);
1110 continue;
1111 }
1113 if (listen != 0)
1114 {
1115 if (network_bind_socket (se, ai_ptr) != 0)
1116 {
1117 close (se->fd);
1118 free (se->addr);
1119 free (se);
1120 continue;
1121 }
1122 }
1123 else /* listen == 0 */
1124 {
1125 network_set_ttl (se, ai_ptr);
1126 }
1128 if (se_tail == NULL)
1129 {
1130 se_head = se;
1131 se_tail = se;
1132 }
1133 else
1134 {
1135 se_tail->next = se;
1136 se_tail = se;
1137 }
1139 /* We don't open more than one write-socket per node/service pair.. */
1140 if (listen == 0)
1141 break;
1142 }
1144 freeaddrinfo (ai_list);
1146 return (se_head);
1147 } /* sockent_t *network_create_socket */
1149 static sockent_t *network_create_default_socket (int listen)
1150 {
1151 sockent_t *se_ptr = NULL;
1152 sockent_t *se_head = NULL;
1153 sockent_t *se_tail = NULL;
1155 se_ptr = network_create_socket (NET_DEFAULT_V6_ADDR,
1156 NET_DEFAULT_PORT, listen);
1158 /* Don't send to the same machine in IPv6 and IPv4 if both are available. */
1159 if ((listen == 0) && (se_ptr != NULL))
1160 return (se_ptr);
1162 if (se_ptr != NULL)
1163 {
1164 se_head = se_ptr;
1165 se_tail = se_ptr;
1166 while (se_tail->next != NULL)
1167 se_tail = se_tail->next;
1168 }
1170 se_ptr = network_create_socket (NET_DEFAULT_V4_ADDR, NET_DEFAULT_PORT, listen);
1172 if (se_tail == NULL)
1173 return (se_ptr);
1175 se_tail->next = se_ptr;
1176 return (se_head);
1177 } /* sockent_t *network_create_default_socket */
1179 static int network_add_listen_socket (const char *node, const char *service)
1180 {
1181 sockent_t *se;
1182 sockent_t *se_ptr;
1183 int se_num = 0;
1185 if (service == NULL)
1186 service = NET_DEFAULT_PORT;
1188 if (node == NULL)
1189 se = network_create_default_socket (1 /* listen == true */);
1190 else
1191 se = network_create_socket (node, service, 1 /* listen == true */);
1193 if (se == NULL)
1194 return (-1);
1196 for (se_ptr = se; se_ptr != NULL; se_ptr = se_ptr->next)
1197 se_num++;
1199 listen_sockets = (struct pollfd *) realloc (listen_sockets,
1200 (listen_sockets_num + se_num)
1201 * sizeof (struct pollfd));
1203 for (se_ptr = se; se_ptr != NULL; se_ptr = se_ptr->next)
1204 {
1205 listen_sockets[listen_sockets_num].fd = se_ptr->fd;
1206 listen_sockets[listen_sockets_num].events = POLLIN | POLLPRI;
1207 listen_sockets[listen_sockets_num].revents = 0;
1208 listen_sockets_num++;
1209 } /* for (se) */
1211 free_sockent (se);
1212 return (0);
1213 } /* int network_add_listen_socket */
1215 static int network_add_sending_socket (const char *node, const char *service)
1216 {
1217 sockent_t *se;
1218 sockent_t *se_ptr;
1220 if (service == NULL)
1221 service = NET_DEFAULT_PORT;
1223 if (node == NULL)
1224 se = network_create_default_socket (0 /* listen == false */);
1225 else
1226 se = network_create_socket (node, service, 0 /* listen == false */);
1228 if (se == NULL)
1229 return (-1);
1231 if (sending_sockets == NULL)
1232 {
1233 sending_sockets = se;
1234 return (0);
1235 }
1237 for (se_ptr = sending_sockets; se_ptr->next != NULL; se_ptr = se_ptr->next)
1238 /* seek end */;
1240 se_ptr->next = se;
1241 return (0);
1242 } /* int network_get_listen_socket */
1244 static void *dispatch_thread (void *arg)
1245 {
1246 while (42)
1247 {
1248 receive_list_entry_t *ent;
1250 /* Lock and wait for more data to come in */
1251 pthread_mutex_lock (&receive_list_lock);
1252 while ((listen_loop == 0)
1253 && (receive_list_head == NULL))
1254 pthread_cond_wait (&receive_list_cond, &receive_list_lock);
1256 /* Remove the head entry and unlock */
1257 ent = receive_list_head;
1258 if (ent != NULL)
1259 receive_list_head = ent->next;
1260 pthread_mutex_unlock (&receive_list_lock);
1262 /* Check whether we are supposed to exit. We do NOT check `listen_loop'
1263 * because we dispatch all missing packets before shutting down. */
1264 if (ent == NULL)
1265 break;
1267 parse_packet (ent->data, ent->data_len);
1269 sfree (ent);
1270 } /* while (42) */
1272 return (NULL);
1273 } /* void *receive_thread */
1275 static int network_receive (void)
1276 {
1277 char buffer[BUFF_SIZE];
1278 int buffer_len;
1280 int i;
1281 int status;
1283 if (listen_sockets_num == 0)
1284 network_add_listen_socket (NULL, NULL);
1286 if (listen_sockets_num == 0)
1287 {
1288 ERROR ("network: Failed to open a listening socket.");
1289 return (-1);
1290 }
1292 while (listen_loop == 0)
1293 {
1294 status = poll (listen_sockets, listen_sockets_num, -1);
1296 if (status <= 0)
1297 {
1298 char errbuf[1024];
1299 if (errno == EINTR)
1300 continue;
1301 ERROR ("poll failed: %s",
1302 sstrerror (errno, errbuf, sizeof (errbuf)));
1303 return (-1);
1304 }
1306 for (i = 0; (i < listen_sockets_num) && (status > 0); i++)
1307 {
1308 receive_list_entry_t *ent;
1310 if ((listen_sockets[i].revents & (POLLIN | POLLPRI)) == 0)
1311 continue;
1312 status--;
1314 buffer_len = recv (listen_sockets[i].fd,
1315 buffer, sizeof (buffer),
1316 0 /* no flags */);
1317 if (buffer_len < 0)
1318 {
1319 char errbuf[1024];
1320 ERROR ("recv failed: %s",
1321 sstrerror (errno, errbuf,
1322 sizeof (errbuf)));
1323 return (-1);
1324 }
1326 ent = malloc (sizeof (receive_list_entry_t));
1327 if (ent == NULL)
1328 {
1329 ERROR ("network plugin: malloc failed.");
1330 return (-1);
1331 }
1332 memset (ent, '\0', sizeof (receive_list_entry_t));
1334 /* Hopefully this be optimized out by the compiler. It
1335 * might help prevent stupid bugs in the future though.
1336 */
1337 assert (sizeof (ent->data) == sizeof (buffer));
1339 memcpy (ent->data, buffer, buffer_len);
1340 ent->data_len = buffer_len;
1342 pthread_mutex_lock (&receive_list_lock);
1343 if (receive_list_head == NULL)
1344 {
1345 receive_list_head = ent;
1346 receive_list_tail = ent;
1347 }
1348 else
1349 {
1350 receive_list_tail->next = ent;
1351 receive_list_tail = ent;
1352 }
1353 pthread_cond_signal (&receive_list_cond);
1354 pthread_mutex_unlock (&receive_list_lock);
1355 } /* for (listen_sockets) */
1356 } /* while (listen_loop == 0) */
1358 return (0);
1359 }
1361 static void *receive_thread (void *arg)
1362 {
1363 return (network_receive () ? (void *) 1 : (void *) 0);
1364 } /* void *receive_thread */
1366 static void network_send_buffer (const char *buffer, int buffer_len)
1367 {
1368 sockent_t *se;
1369 int status;
1371 DEBUG ("network plugin: network_send_buffer: buffer_len = %i", buffer_len);
1373 for (se = sending_sockets; se != NULL; se = se->next)
1374 {
1375 while (42)
1376 {
1377 status = sendto (se->fd, buffer, buffer_len, 0 /* no flags */,
1378 (struct sockaddr *) se->addr, se->addrlen);
1379 if (status < 0)
1380 {
1381 char errbuf[1024];
1382 if (errno == EINTR)
1383 continue;
1384 ERROR ("network plugin: sendto failed: %s",
1385 sstrerror (errno, errbuf,
1386 sizeof (errbuf)));
1387 break;
1388 }
1390 break;
1391 } /* while (42) */
1392 } /* for (sending_sockets) */
1393 } /* void network_send_buffer */
1395 static int add_to_buffer (char *buffer, int buffer_size,
1396 value_list_t *vl_def, char *type_def,
1397 const data_set_t *ds, const value_list_t *vl)
1398 {
1399 char *buffer_orig = buffer;
1401 if (strcmp (vl_def->host, vl->host) != 0)
1402 {
1403 if (write_part_string (&buffer, &buffer_size, TYPE_HOST,
1404 vl->host, strlen (vl->host)) != 0)
1405 return (-1);
1406 sstrncpy (vl_def->host, vl->host, sizeof (vl_def->host));
1407 }
1409 if (vl_def->time != vl->time)
1410 {
1411 if (write_part_number (&buffer, &buffer_size, TYPE_TIME,
1412 (uint64_t) vl->time))
1413 return (-1);
1414 vl_def->time = vl->time;
1415 }
1417 if (vl_def->interval != vl->interval)
1418 {
1419 if (write_part_number (&buffer, &buffer_size, TYPE_INTERVAL,
1420 (uint64_t) vl->interval))
1421 return (-1);
1422 vl_def->interval = vl->interval;
1423 }
1425 if (strcmp (vl_def->plugin, vl->plugin) != 0)
1426 {
1427 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN,
1428 vl->plugin, strlen (vl->plugin)) != 0)
1429 return (-1);
1430 sstrncpy (vl_def->plugin, vl->plugin, sizeof (vl_def->plugin));
1431 }
1433 if (strcmp (vl_def->plugin_instance, vl->plugin_instance) != 0)
1434 {
1435 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
1436 vl->plugin_instance,
1437 strlen (vl->plugin_instance)) != 0)
1438 return (-1);
1439 sstrncpy (vl_def->plugin_instance, vl->plugin_instance, sizeof (vl_def->plugin_instance));
1440 }
1442 if (strcmp (type_def, vl->type) != 0)
1443 {
1444 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
1445 vl->type, strlen (vl->type)) != 0)
1446 return (-1);
1447 sstrncpy (type_def, vl->type, sizeof (type_def));
1448 }
1450 if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
1451 {
1452 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE_INSTANCE,
1453 vl->type_instance,
1454 strlen (vl->type_instance)) != 0)
1455 return (-1);
1456 sstrncpy (vl_def->type_instance, vl->type_instance, sizeof (vl_def->type_instance));
1457 }
1459 if (write_part_values (&buffer, &buffer_size, ds, vl) != 0)
1460 return (-1);
1462 return (buffer - buffer_orig);
1463 } /* int add_to_buffer */
1465 static void flush_buffer (void)
1466 {
1467 DEBUG ("network plugin: flush_buffer: send_buffer_fill = %i",
1468 send_buffer_fill);
1470 network_send_buffer (send_buffer, send_buffer_fill);
1471 send_buffer_ptr = send_buffer;
1472 send_buffer_fill = 0;
1473 memset (&send_buffer_vl, '\0', sizeof (send_buffer_vl));
1474 memset (send_buffer_type, '\0', sizeof (send_buffer_type));
1475 }
1477 static int network_write (const data_set_t *ds, const value_list_t *vl)
1478 {
1479 int status;
1481 /* If the value is already in the cache, we have received it via the
1482 * network. We write it again if forwarding is activated. It's then in
1483 * the cache and should we receive it again we will ignore it. */
1484 status = cache_check (vl);
1485 if ((network_config_forward == 0)
1486 && (status != 0))
1487 return (0);
1489 pthread_mutex_lock (&send_buffer_lock);
1491 status = add_to_buffer (send_buffer_ptr,
1492 sizeof (send_buffer) - send_buffer_fill,
1493 &send_buffer_vl, send_buffer_type,
1494 ds, vl);
1495 if (status >= 0)
1496 {
1497 /* status == bytes added to the buffer */
1498 send_buffer_fill += status;
1499 send_buffer_ptr += status;
1500 }
1501 else
1502 {
1503 flush_buffer ();
1505 status = add_to_buffer (send_buffer_ptr,
1506 sizeof (send_buffer) - send_buffer_fill,
1507 &send_buffer_vl, send_buffer_type,
1508 ds, vl);
1510 if (status >= 0)
1511 {
1512 send_buffer_fill += status;
1513 send_buffer_ptr += status;
1514 }
1515 }
1517 if (status < 0)
1518 {
1519 ERROR ("network plugin: Unable to append to the "
1520 "buffer for some weird reason");
1521 }
1522 else if ((sizeof (send_buffer) - send_buffer_fill) < 15)
1523 {
1524 flush_buffer ();
1525 }
1527 pthread_mutex_unlock (&send_buffer_lock);
1529 return ((status < 0) ? -1 : 0);
1530 } /* int network_write */
1532 static int network_config (const char *key, const char *val)
1533 {
1534 char *node;
1535 char *service;
1537 char *fields[3];
1538 int fields_num;
1540 if ((strcasecmp ("Listen", key) == 0)
1541 || (strcasecmp ("Server", key) == 0))
1542 {
1543 char *val_cpy = strdup (val);
1544 if (val_cpy == NULL)
1545 return (1);
1547 service = NET_DEFAULT_PORT;
1548 fields_num = strsplit (val_cpy, fields, 3);
1549 if ((fields_num != 1)
1550 && (fields_num != 2))
1551 return (1);
1552 else if (fields_num == 2)
1553 {
1554 if ((service = strchr (fields[1], '.')) != NULL)
1555 *service = '\0';
1556 service = fields[1];
1557 }
1558 node = fields[0];
1560 if (strcasecmp ("Listen", key) == 0)
1561 network_add_listen_socket (node, service);
1562 else
1563 network_add_sending_socket (node, service);
1564 }
1565 else if (strcasecmp ("TimeToLive", key) == 0)
1566 {
1567 int tmp = atoi (val);
1568 if ((tmp > 0) && (tmp < 256))
1569 network_config_ttl = tmp;
1570 else
1571 return (1);
1572 }
1573 else if (strcasecmp ("Forward", key) == 0)
1574 {
1575 if ((strcasecmp ("true", val) == 0)
1576 || (strcasecmp ("yes", val) == 0)
1577 || (strcasecmp ("on", val) == 0))
1578 network_config_forward = 1;
1579 else
1580 network_config_forward = 0;
1581 }
1582 else if (strcasecmp ("CacheFlush", key) == 0)
1583 {
1584 int tmp = atoi (val);
1585 if (tmp > 0)
1586 cache_flush_interval = tmp;
1587 else return (1);
1588 }
1589 else
1590 {
1591 return (-1);
1592 }
1593 return (0);
1594 } /* int network_config */
1596 static int network_notification (const notification_t *n)
1597 {
1598 char buffer[BUFF_SIZE];
1599 char *buffer_ptr = buffer;
1600 int buffer_free = sizeof (buffer);
1601 int status;
1603 memset (buffer, '\0', sizeof (buffer));
1606 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_TIME,
1607 (uint64_t) n->time);
1608 if (status != 0)
1609 return (-1);
1611 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_SEVERITY,
1612 (uint64_t) n->severity);
1613 if (status != 0)
1614 return (-1);
1616 if (strlen (n->host) > 0)
1617 {
1618 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_HOST,
1619 n->host, strlen (n->host));
1620 if (status != 0)
1621 return (-1);
1622 }
1624 if (strlen (n->plugin) > 0)
1625 {
1626 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_PLUGIN,
1627 n->plugin, strlen (n->plugin));
1628 if (status != 0)
1629 return (-1);
1630 }
1632 if (strlen (n->plugin_instance) > 0)
1633 {
1634 status = write_part_string (&buffer_ptr, &buffer_free,
1635 TYPE_PLUGIN_INSTANCE,
1636 n->plugin_instance, strlen (n->plugin_instance));
1637 if (status != 0)
1638 return (-1);
1639 }
1641 if (strlen (n->type) > 0)
1642 {
1643 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE,
1644 n->type, strlen (n->type));
1645 if (status != 0)
1646 return (-1);
1647 }
1649 if (strlen (n->type_instance) > 0)
1650 {
1651 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE_INSTANCE,
1652 n->type_instance, strlen (n->type_instance));
1653 if (status != 0)
1654 return (-1);
1655 }
1657 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_MESSAGE,
1658 n->message, strlen (n->message));
1659 if (status != 0)
1660 return (-1);
1662 network_send_buffer (buffer, sizeof (buffer) - buffer_free);
1664 return (0);
1665 } /* int network_notification */
1667 static int network_shutdown (void)
1668 {
1669 listen_loop++;
1671 /* Kill the listening thread */
1672 if (receive_thread_id != (pthread_t) 0)
1673 {
1674 pthread_kill (receive_thread_id, SIGTERM);
1675 pthread_join (receive_thread_id, NULL /* no return value */);
1676 receive_thread_id = (pthread_t) 0;
1677 }
1679 /* Shutdown the dispatching thread */
1680 if (dispatch_thread_id != (pthread_t) 0)
1681 pthread_cond_broadcast (&receive_list_cond);
1683 if (send_buffer_fill > 0)
1684 flush_buffer ();
1686 if (cache_tree != NULL)
1687 {
1688 void *key;
1689 void *value;
1691 while (c_avl_pick (cache_tree, &key, &value) == 0)
1692 {
1693 sfree (key);
1694 sfree (value);
1695 }
1696 c_avl_destroy (cache_tree);
1697 cache_tree = NULL;
1698 }
1700 /* TODO: Close `sending_sockets' */
1702 plugin_unregister_config ("network");
1703 plugin_unregister_init ("network");
1704 plugin_unregister_write ("network");
1705 plugin_unregister_shutdown ("network");
1707 /* Let the init function do it's move again ;) */
1708 cache_flush_last = 0;
1710 return (0);
1711 } /* int network_shutdown */
1713 static int network_init (void)
1714 {
1715 /* Check if we were already initialized. If so, just return - there's
1716 * nothing more to do (for now, that is). */
1717 if (cache_flush_last != 0)
1718 return (0);
1720 plugin_register_shutdown ("network", network_shutdown);
1722 send_buffer_ptr = send_buffer;
1723 send_buffer_fill = 0;
1724 memset (&send_buffer_vl, '\0', sizeof (send_buffer_vl));
1725 memset (send_buffer_type, '\0', sizeof (send_buffer_type));
1727 cache_tree = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1728 cache_flush_last = time (NULL);
1730 /* setup socket(s) and so on */
1731 if (sending_sockets != NULL)
1732 {
1733 plugin_register_write ("network", network_write);
1734 plugin_register_notification ("network", network_notification);
1735 }
1737 if ((listen_sockets_num != 0) && (receive_thread_id == 0))
1738 {
1739 int status;
1741 status = pthread_create (&dispatch_thread_id,
1742 NULL /* no attributes */,
1743 dispatch_thread,
1744 NULL /* no argument */);
1745 if (status != 0)
1746 {
1747 char errbuf[1024];
1748 ERROR ("network: pthread_create failed: %s",
1749 sstrerror (errno, errbuf,
1750 sizeof (errbuf)));
1751 }
1753 status = pthread_create (&receive_thread_id,
1754 NULL /* no attributes */,
1755 receive_thread,
1756 NULL /* no argument */);
1757 if (status != 0)
1758 {
1759 char errbuf[1024];
1760 ERROR ("network: pthread_create failed: %s",
1761 sstrerror (errno, errbuf,
1762 sizeof (errbuf)));
1763 }
1764 }
1765 return (0);
1766 } /* int network_init */
1768 /*
1769 * The flush option of the network plugin cannot flush individual identifiers.
1770 * All the values are added to a buffer and sent when the buffer is full, the
1771 * requested value may or may not be in there, it's not worth finding out. We
1772 * just send the buffer if `flush' is called - if the requested value was in
1773 * there, good. If not, well, then there is nothing to flush.. -octo
1774 */
1775 static int network_flush (int timeout, const char *identifier)
1776 {
1777 pthread_mutex_lock (&send_buffer_lock);
1779 if (((time (NULL) - cache_flush_last) >= timeout)
1780 && (send_buffer_fill > 0))
1781 {
1782 flush_buffer ();
1783 }
1785 pthread_mutex_unlock (&send_buffer_lock);
1787 return (0);
1788 } /* int network_flush */
1790 void module_register (void)
1791 {
1792 plugin_register_config ("network", network_config,
1793 config_keys, config_keys_num);
1794 plugin_register_init ("network", network_init);
1795 plugin_register_flush ("network", network_flush);
1796 } /* void module_register */