3a52a43d74db22c3457cb1358e48eaf68a6f2b4c
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 char *type, 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, 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 char type[DATA_MAX_NAME_LEN];
682 notification_t n;
684 DEBUG ("network plugin: parse_packet: buffer = %p; buffer_len = %i;",
685 buffer, buffer_len);
687 memset (&vl, '\0', sizeof (vl));
688 memset (&type, '\0', sizeof (type));
689 memset (&n, '\0', sizeof (n));
690 status = 0;
692 while ((status == 0) && (0 < buffer_len)
693 && ((unsigned int) buffer_len > sizeof (part_header_t)))
694 {
695 uint16_t pkg_length;
696 uint16_t pkg_type;
698 memcpy ((void *) &pkg_type,
699 (void *) buffer,
700 sizeof (pkg_type));
701 memcpy ((void *) &pkg_length,
702 (void *) (buffer + sizeof (pkg_type)),
703 sizeof (pkg_length));
705 pkg_length = ntohs (pkg_length);
706 pkg_type = ntohs (pkg_type);
708 if (pkg_length > buffer_len)
709 break;
710 /* Ensure that this loop terminates eventually */
711 if (pkg_length < (2 * sizeof (uint16_t)))
712 break;
714 if (pkg_type == TYPE_VALUES)
715 {
716 status = parse_part_values (&buffer, &buffer_len,
717 &vl.values, &vl.values_len);
719 if (status != 0)
720 break;
722 if ((vl.time > 0)
723 && (strlen (vl.host) > 0)
724 && (strlen (vl.plugin) > 0)
725 && (strlen (type) > 0)
726 && (cache_check (type, &vl) == 0))
727 {
728 plugin_dispatch_values (type, &vl);
729 }
730 else
731 {
732 DEBUG ("network plugin: parse_packet:"
733 " NOT dispatching values");
734 }
736 sfree (vl.values);
737 }
738 else if (pkg_type == TYPE_TIME)
739 {
740 uint64_t tmp = 0;
741 status = parse_part_number (&buffer, &buffer_len,
742 &tmp);
743 if (status == 0)
744 {
745 vl.time = (time_t) tmp;
746 n.time = (time_t) tmp;
747 }
748 }
749 else if (pkg_type == TYPE_INTERVAL)
750 {
751 uint64_t tmp = 0;
752 status = parse_part_number (&buffer, &buffer_len,
753 &tmp);
754 if (status == 0)
755 vl.interval = (int) tmp;
756 }
757 else if (pkg_type == TYPE_HOST)
758 {
759 status = parse_part_string (&buffer, &buffer_len,
760 vl.host, sizeof (vl.host));
761 if (status == 0)
762 sstrncpy (n.host, vl.host, sizeof (n.host));
763 }
764 else if (pkg_type == TYPE_PLUGIN)
765 {
766 status = parse_part_string (&buffer, &buffer_len,
767 vl.plugin, sizeof (vl.plugin));
768 if (status == 0)
769 sstrncpy (n.plugin, vl.plugin,
770 sizeof (n.plugin));
771 }
772 else if (pkg_type == TYPE_PLUGIN_INSTANCE)
773 {
774 status = parse_part_string (&buffer, &buffer_len,
775 vl.plugin_instance,
776 sizeof (vl.plugin_instance));
777 if (status == 0)
778 sstrncpy (n.plugin_instance,
779 vl.plugin_instance,
780 sizeof (n.plugin_instance));
781 }
782 else if (pkg_type == TYPE_TYPE)
783 {
784 status = parse_part_string (&buffer, &buffer_len,
785 type, sizeof (type));
786 if (status == 0)
787 sstrncpy (n.type, type, sizeof (n.type));
788 }
789 else if (pkg_type == TYPE_TYPE_INSTANCE)
790 {
791 status = parse_part_string (&buffer, &buffer_len,
792 vl.type_instance,
793 sizeof (vl.type_instance));
794 if (status == 0)
795 sstrncpy (n.type_instance, vl.type_instance,
796 sizeof (n.type_instance));
797 }
798 else if (pkg_type == TYPE_MESSAGE)
799 {
800 status = parse_part_string (&buffer, &buffer_len,
801 n.message, sizeof (n.message));
803 if (status != 0)
804 {
805 /* do nothing */
806 }
807 else if ((n.severity != NOTIF_FAILURE)
808 && (n.severity != NOTIF_WARNING)
809 && (n.severity != NOTIF_OKAY))
810 {
811 INFO ("network plugin: "
812 "Ignoring notification with "
813 "unknown severity %i.",
814 n.severity);
815 }
816 else if (n.time <= 0)
817 {
818 INFO ("network plugin: "
819 "Ignoring notification with "
820 "time == 0.");
821 }
822 else if (strlen (n.message) <= 0)
823 {
824 INFO ("network plugin: "
825 "Ignoring notification with "
826 "an empty message.");
827 }
828 else
829 {
830 plugin_dispatch_notification (&n);
831 }
832 }
833 else if (pkg_type == TYPE_SEVERITY)
834 {
835 uint64_t tmp = 0;
836 status = parse_part_number (&buffer, &buffer_len,
837 &tmp);
838 if (status == 0)
839 n.severity = (int) tmp;
840 }
841 else
842 {
843 DEBUG ("network plugin: parse_packet: Unknown part"
844 " type: 0x%04hx", pkg_type);
845 buffer = ((char *) buffer) + pkg_length;
846 }
847 } /* while (buffer_len > sizeof (part_header_t)) */
849 return (status);
850 } /* int parse_packet */
852 static void free_sockent (sockent_t *se)
853 {
854 sockent_t *next;
855 while (se != NULL)
856 {
857 next = se->next;
858 free (se->addr);
859 free (se);
860 se = next;
861 }
862 } /* void free_sockent */
864 /*
865 * int network_set_ttl
866 *
867 * Set the `IP_MULTICAST_TTL', `IP_TTL', `IPV6_MULTICAST_HOPS' or
868 * `IPV6_UNICAST_HOPS', depending on which option is applicable.
869 *
870 * The `struct addrinfo' is used to destinguish between unicast and multicast
871 * sockets.
872 */
873 static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
874 {
875 if ((network_config_ttl < 1) || (network_config_ttl > 255))
876 return (-1);
878 DEBUG ("ttl = %i", network_config_ttl);
880 if (ai->ai_family == AF_INET)
881 {
882 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
883 int optname;
885 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
886 optname = IP_MULTICAST_TTL;
887 else
888 optname = IP_TTL;
890 if (setsockopt (se->fd, IPPROTO_IP, optname,
891 &network_config_ttl,
892 sizeof (network_config_ttl)) == -1)
893 {
894 char errbuf[1024];
895 ERROR ("setsockopt: %s",
896 sstrerror (errno, errbuf, sizeof (errbuf)));
897 return (-1);
898 }
899 }
900 else if (ai->ai_family == AF_INET6)
901 {
902 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
903 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
904 int optname;
906 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
907 optname = IPV6_MULTICAST_HOPS;
908 else
909 optname = IPV6_UNICAST_HOPS;
911 if (setsockopt (se->fd, IPPROTO_IPV6, optname,
912 &network_config_ttl,
913 sizeof (network_config_ttl)) == -1)
914 {
915 char errbuf[1024];
916 ERROR ("setsockopt: %s",
917 sstrerror (errno, errbuf,
918 sizeof (errbuf)));
919 return (-1);
920 }
921 }
923 return (0);
924 } /* int network_set_ttl */
926 static int network_bind_socket (const sockent_t *se, const struct addrinfo *ai)
927 {
928 int loop = 0;
929 int yes = 1;
931 /* allow multiple sockets to use the same PORT number */
932 if (setsockopt(se->fd, SOL_SOCKET, SO_REUSEADDR,
933 &yes, sizeof(yes)) == -1) {
934 char errbuf[1024];
935 ERROR ("setsockopt: %s",
936 sstrerror (errno, errbuf, sizeof (errbuf)));
937 return (-1);
938 }
940 DEBUG ("fd = %i; calling `bind'", se->fd);
942 if (bind (se->fd, ai->ai_addr, ai->ai_addrlen) == -1)
943 {
944 char errbuf[1024];
945 ERROR ("bind: %s",
946 sstrerror (errno, errbuf, sizeof (errbuf)));
947 return (-1);
948 }
950 if (ai->ai_family == AF_INET)
951 {
952 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
953 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
954 {
955 struct ip_mreq mreq;
957 DEBUG ("fd = %i; IPv4 multicast address found", se->fd);
959 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
960 mreq.imr_interface.s_addr = htonl (INADDR_ANY);
962 if (setsockopt (se->fd, IPPROTO_IP, IP_MULTICAST_LOOP,
963 &loop, sizeof (loop)) == -1)
964 {
965 char errbuf[1024];
966 ERROR ("setsockopt: %s",
967 sstrerror (errno, errbuf,
968 sizeof (errbuf)));
969 return (-1);
970 }
972 if (setsockopt (se->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
973 &mreq, sizeof (mreq)) == -1)
974 {
975 char errbuf[1024];
976 ERROR ("setsockopt: %s",
977 sstrerror (errno, errbuf,
978 sizeof (errbuf)));
979 return (-1);
980 }
981 }
982 }
983 else if (ai->ai_family == AF_INET6)
984 {
985 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
986 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
987 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
988 {
989 struct ipv6_mreq mreq;
991 DEBUG ("fd = %i; IPv6 multicast address found", se->fd);
993 memcpy (&mreq.ipv6mr_multiaddr,
994 &addr->sin6_addr,
995 sizeof (addr->sin6_addr));
997 /* http://developer.apple.com/documentation/Darwin/Reference/ManPages/man4/ip6.4.html
998 * ipv6mr_interface may be set to zeroes to
999 * choose the default multicast interface or to
1000 * the index of a particular multicast-capable
1001 * interface if the host is multihomed.
1002 * Membership is associ-associated with a
1003 * single interface; programs running on
1004 * multihomed hosts may need to join the same
1005 * group on more than one interface.*/
1006 mreq.ipv6mr_interface = 0;
1008 if (setsockopt (se->fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
1009 &loop, sizeof (loop)) == -1)
1010 {
1011 char errbuf[1024];
1012 ERROR ("setsockopt: %s",
1013 sstrerror (errno, errbuf,
1014 sizeof (errbuf)));
1015 return (-1);
1016 }
1018 if (setsockopt (se->fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
1019 &mreq, sizeof (mreq)) == -1)
1020 {
1021 char errbuf[1024];
1022 ERROR ("setsockopt: %s",
1023 sstrerror (errno, errbuf,
1024 sizeof (errbuf)));
1025 return (-1);
1026 }
1027 }
1028 }
1030 return (0);
1031 } /* int network_bind_socket */
1033 static sockent_t *network_create_socket (const char *node,
1034 const char *service,
1035 int listen)
1036 {
1037 struct addrinfo ai_hints;
1038 struct addrinfo *ai_list, *ai_ptr;
1039 int ai_return;
1041 sockent_t *se_head = NULL;
1042 sockent_t *se_tail = NULL;
1044 DEBUG ("node = %s, service = %s", node, service);
1046 memset (&ai_hints, '\0', sizeof (ai_hints));
1047 ai_hints.ai_flags = 0;
1048 #ifdef AI_PASSIVE
1049 ai_hints.ai_flags |= AI_PASSIVE;
1050 #endif
1051 #ifdef AI_ADDRCONFIG
1052 ai_hints.ai_flags |= AI_ADDRCONFIG;
1053 #endif
1054 ai_hints.ai_family = AF_UNSPEC;
1055 ai_hints.ai_socktype = SOCK_DGRAM;
1056 ai_hints.ai_protocol = IPPROTO_UDP;
1058 ai_return = getaddrinfo (node, service, &ai_hints, &ai_list);
1059 if (ai_return != 0)
1060 {
1061 char errbuf[1024];
1062 ERROR ("getaddrinfo (%s, %s): %s",
1063 (node == NULL) ? "(null)" : node,
1064 (service == NULL) ? "(null)" : service,
1065 (ai_return == EAI_SYSTEM)
1066 ? sstrerror (errno, errbuf, sizeof (errbuf))
1067 : gai_strerror (ai_return));
1068 return (NULL);
1069 }
1071 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1072 {
1073 sockent_t *se;
1075 if ((se = (sockent_t *) malloc (sizeof (sockent_t))) == NULL)
1076 {
1077 char errbuf[1024];
1078 ERROR ("malloc: %s",
1079 sstrerror (errno, errbuf,
1080 sizeof (errbuf)));
1081 continue;
1082 }
1084 if ((se->addr = (struct sockaddr_storage *) malloc (sizeof (struct sockaddr_storage))) == NULL)
1085 {
1086 char errbuf[1024];
1087 ERROR ("malloc: %s",
1088 sstrerror (errno, errbuf,
1089 sizeof (errbuf)));
1090 free (se);
1091 continue;
1092 }
1094 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
1095 memset (se->addr, '\0', sizeof (struct sockaddr_storage));
1096 memcpy (se->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1097 se->addrlen = ai_ptr->ai_addrlen;
1099 se->fd = socket (ai_ptr->ai_family,
1100 ai_ptr->ai_socktype,
1101 ai_ptr->ai_protocol);
1102 se->next = NULL;
1104 if (se->fd == -1)
1105 {
1106 char errbuf[1024];
1107 ERROR ("socket: %s",
1108 sstrerror (errno, errbuf,
1109 sizeof (errbuf)));
1110 free (se->addr);
1111 free (se);
1112 continue;
1113 }
1115 if (listen != 0)
1116 {
1117 if (network_bind_socket (se, ai_ptr) != 0)
1118 {
1119 close (se->fd);
1120 free (se->addr);
1121 free (se);
1122 continue;
1123 }
1124 }
1125 else /* listen == 0 */
1126 {
1127 network_set_ttl (se, ai_ptr);
1128 }
1130 if (se_tail == NULL)
1131 {
1132 se_head = se;
1133 se_tail = se;
1134 }
1135 else
1136 {
1137 se_tail->next = se;
1138 se_tail = se;
1139 }
1141 /* We don't open more than one write-socket per node/service pair.. */
1142 if (listen == 0)
1143 break;
1144 }
1146 freeaddrinfo (ai_list);
1148 return (se_head);
1149 } /* sockent_t *network_create_socket */
1151 static sockent_t *network_create_default_socket (int listen)
1152 {
1153 sockent_t *se_ptr = NULL;
1154 sockent_t *se_head = NULL;
1155 sockent_t *se_tail = NULL;
1157 se_ptr = network_create_socket (NET_DEFAULT_V6_ADDR,
1158 NET_DEFAULT_PORT, listen);
1160 /* Don't send to the same machine in IPv6 and IPv4 if both are available. */
1161 if ((listen == 0) && (se_ptr != NULL))
1162 return (se_ptr);
1164 if (se_ptr != NULL)
1165 {
1166 se_head = se_ptr;
1167 se_tail = se_ptr;
1168 while (se_tail->next != NULL)
1169 se_tail = se_tail->next;
1170 }
1172 se_ptr = network_create_socket (NET_DEFAULT_V4_ADDR, NET_DEFAULT_PORT, listen);
1174 if (se_tail == NULL)
1175 return (se_ptr);
1177 se_tail->next = se_ptr;
1178 return (se_head);
1179 } /* sockent_t *network_create_default_socket */
1181 static int network_add_listen_socket (const char *node, const char *service)
1182 {
1183 sockent_t *se;
1184 sockent_t *se_ptr;
1185 int se_num = 0;
1187 if (service == NULL)
1188 service = NET_DEFAULT_PORT;
1190 if (node == NULL)
1191 se = network_create_default_socket (1 /* listen == true */);
1192 else
1193 se = network_create_socket (node, service, 1 /* listen == true */);
1195 if (se == NULL)
1196 return (-1);
1198 for (se_ptr = se; se_ptr != NULL; se_ptr = se_ptr->next)
1199 se_num++;
1201 listen_sockets = (struct pollfd *) realloc (listen_sockets,
1202 (listen_sockets_num + se_num)
1203 * sizeof (struct pollfd));
1205 for (se_ptr = se; se_ptr != NULL; se_ptr = se_ptr->next)
1206 {
1207 listen_sockets[listen_sockets_num].fd = se_ptr->fd;
1208 listen_sockets[listen_sockets_num].events = POLLIN | POLLPRI;
1209 listen_sockets[listen_sockets_num].revents = 0;
1210 listen_sockets_num++;
1211 } /* for (se) */
1213 free_sockent (se);
1214 return (0);
1215 } /* int network_add_listen_socket */
1217 static int network_add_sending_socket (const char *node, const char *service)
1218 {
1219 sockent_t *se;
1220 sockent_t *se_ptr;
1222 if (service == NULL)
1223 service = NET_DEFAULT_PORT;
1225 if (node == NULL)
1226 se = network_create_default_socket (0 /* listen == false */);
1227 else
1228 se = network_create_socket (node, service, 0 /* listen == false */);
1230 if (se == NULL)
1231 return (-1);
1233 if (sending_sockets == NULL)
1234 {
1235 sending_sockets = se;
1236 return (0);
1237 }
1239 for (se_ptr = sending_sockets; se_ptr->next != NULL; se_ptr = se_ptr->next)
1240 /* seek end */;
1242 se_ptr->next = se;
1243 return (0);
1244 } /* int network_get_listen_socket */
1246 static void *dispatch_thread (void *arg)
1247 {
1248 while (42)
1249 {
1250 receive_list_entry_t *ent;
1252 /* Lock and wait for more data to come in */
1253 pthread_mutex_lock (&receive_list_lock);
1254 while ((listen_loop == 0)
1255 && (receive_list_head == NULL))
1256 pthread_cond_wait (&receive_list_cond, &receive_list_lock);
1258 /* Remove the head entry and unlock */
1259 ent = receive_list_head;
1260 if (ent != NULL)
1261 receive_list_head = ent->next;
1262 pthread_mutex_unlock (&receive_list_lock);
1264 /* Check whether we are supposed to exit. We do NOT check `listen_loop'
1265 * because we dispatch all missing packets before shutting down. */
1266 if (ent == NULL)
1267 break;
1269 parse_packet (ent->data, ent->data_len);
1271 sfree (ent);
1272 } /* while (42) */
1274 return (NULL);
1275 } /* void *receive_thread */
1277 static int network_receive (void)
1278 {
1279 char buffer[BUFF_SIZE];
1280 int buffer_len;
1282 int i;
1283 int status;
1285 if (listen_sockets_num == 0)
1286 network_add_listen_socket (NULL, NULL);
1288 if (listen_sockets_num == 0)
1289 {
1290 ERROR ("network: Failed to open a listening socket.");
1291 return (-1);
1292 }
1294 while (listen_loop == 0)
1295 {
1296 status = poll (listen_sockets, listen_sockets_num, -1);
1298 if (status <= 0)
1299 {
1300 char errbuf[1024];
1301 if (errno == EINTR)
1302 continue;
1303 ERROR ("poll failed: %s",
1304 sstrerror (errno, errbuf, sizeof (errbuf)));
1305 return (-1);
1306 }
1308 for (i = 0; (i < listen_sockets_num) && (status > 0); i++)
1309 {
1310 receive_list_entry_t *ent;
1312 if ((listen_sockets[i].revents & (POLLIN | POLLPRI)) == 0)
1313 continue;
1314 status--;
1316 buffer_len = recv (listen_sockets[i].fd,
1317 buffer, sizeof (buffer),
1318 0 /* no flags */);
1319 if (buffer_len < 0)
1320 {
1321 char errbuf[1024];
1322 ERROR ("recv failed: %s",
1323 sstrerror (errno, errbuf,
1324 sizeof (errbuf)));
1325 return (-1);
1326 }
1328 ent = malloc (sizeof (receive_list_entry_t));
1329 if (ent == NULL)
1330 {
1331 ERROR ("network plugin: malloc failed.");
1332 return (-1);
1333 }
1334 memset (ent, '\0', sizeof (receive_list_entry_t));
1336 /* Hopefully this be optimized out by the compiler. It
1337 * might help prevent stupid bugs in the future though.
1338 */
1339 assert (sizeof (ent->data) == sizeof (buffer));
1341 memcpy (ent->data, buffer, buffer_len);
1342 ent->data_len = buffer_len;
1344 pthread_mutex_lock (&receive_list_lock);
1345 if (receive_list_head == NULL)
1346 {
1347 receive_list_head = ent;
1348 receive_list_tail = ent;
1349 }
1350 else
1351 {
1352 receive_list_tail->next = ent;
1353 receive_list_tail = ent;
1354 }
1355 pthread_cond_signal (&receive_list_cond);
1356 pthread_mutex_unlock (&receive_list_lock);
1357 } /* for (listen_sockets) */
1358 } /* while (listen_loop == 0) */
1360 return (0);
1361 }
1363 static void *receive_thread (void *arg)
1364 {
1365 return (network_receive () ? (void *) 1 : (void *) 0);
1366 } /* void *receive_thread */
1368 static void network_send_buffer (const char *buffer, int buffer_len)
1369 {
1370 sockent_t *se;
1371 int status;
1373 DEBUG ("network plugin: network_send_buffer: buffer_len = %i", buffer_len);
1375 for (se = sending_sockets; se != NULL; se = se->next)
1376 {
1377 while (42)
1378 {
1379 status = sendto (se->fd, buffer, buffer_len, 0 /* no flags */,
1380 (struct sockaddr *) se->addr, se->addrlen);
1381 if (status < 0)
1382 {
1383 char errbuf[1024];
1384 if (errno == EINTR)
1385 continue;
1386 ERROR ("network plugin: sendto failed: %s",
1387 sstrerror (errno, errbuf,
1388 sizeof (errbuf)));
1389 break;
1390 }
1392 break;
1393 } /* while (42) */
1394 } /* for (sending_sockets) */
1395 } /* void network_send_buffer */
1397 static int add_to_buffer (char *buffer, int buffer_size,
1398 value_list_t *vl_def, char *type_def, size_t type_def_size,
1399 const data_set_t *ds, const value_list_t *vl)
1400 {
1401 char *buffer_orig = buffer;
1403 if (strcmp (vl_def->host, vl->host) != 0)
1404 {
1405 if (write_part_string (&buffer, &buffer_size, TYPE_HOST,
1406 vl->host, strlen (vl->host)) != 0)
1407 return (-1);
1408 sstrncpy (vl_def->host, vl->host, sizeof (vl_def->host));
1409 }
1411 if (vl_def->time != vl->time)
1412 {
1413 if (write_part_number (&buffer, &buffer_size, TYPE_TIME,
1414 (uint64_t) vl->time))
1415 return (-1);
1416 vl_def->time = vl->time;
1417 }
1419 if (vl_def->interval != vl->interval)
1420 {
1421 if (write_part_number (&buffer, &buffer_size, TYPE_INTERVAL,
1422 (uint64_t) vl->interval))
1423 return (-1);
1424 vl_def->interval = vl->interval;
1425 }
1427 if (strcmp (vl_def->plugin, vl->plugin) != 0)
1428 {
1429 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN,
1430 vl->plugin, strlen (vl->plugin)) != 0)
1431 return (-1);
1432 sstrncpy (vl_def->plugin, vl->plugin, sizeof (vl_def->plugin));
1433 }
1435 if (strcmp (vl_def->plugin_instance, vl->plugin_instance) != 0)
1436 {
1437 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
1438 vl->plugin_instance,
1439 strlen (vl->plugin_instance)) != 0)
1440 return (-1);
1441 sstrncpy (vl_def->plugin_instance, vl->plugin_instance, sizeof (vl_def->plugin_instance));
1442 }
1444 if (strcmp (type_def, ds->type) != 0)
1445 {
1446 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
1447 ds->type, strlen (ds->type)) != 0)
1448 return (-1);
1449 sstrncpy (type_def, ds->type, type_def_size);
1450 }
1452 if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
1453 {
1454 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE_INSTANCE,
1455 vl->type_instance,
1456 strlen (vl->type_instance)) != 0)
1457 return (-1);
1458 sstrncpy (vl_def->type_instance, vl->type_instance, sizeof (vl_def->type_instance));
1459 }
1461 if (write_part_values (&buffer, &buffer_size, ds, vl) != 0)
1462 return (-1);
1464 return (buffer - buffer_orig);
1465 } /* int add_to_buffer */
1467 static void flush_buffer (void)
1468 {
1469 DEBUG ("network plugin: flush_buffer: send_buffer_fill = %i",
1470 send_buffer_fill);
1472 network_send_buffer (send_buffer, send_buffer_fill);
1473 send_buffer_ptr = send_buffer;
1474 send_buffer_fill = 0;
1475 memset (&send_buffer_vl, '\0', sizeof (send_buffer_vl));
1476 memset (send_buffer_type, '\0', sizeof (send_buffer_type));
1477 }
1479 static int network_write (const data_set_t *ds, const value_list_t *vl)
1480 {
1481 int status;
1483 /* If the value is already in the cache, we have received it via the
1484 * network. We write it again if forwarding is activated. It's then in
1485 * the cache and should we receive it again we will ignore it. */
1486 status = cache_check (ds->type, vl);
1487 if ((network_config_forward == 0)
1488 && (status != 0))
1489 return (0);
1491 pthread_mutex_lock (&send_buffer_lock);
1493 status = add_to_buffer (send_buffer_ptr,
1494 sizeof (send_buffer) - send_buffer_fill,
1495 &send_buffer_vl,
1496 send_buffer_type, sizeof (send_buffer_type),
1497 ds, vl);
1498 if (status >= 0)
1499 {
1500 /* status == bytes added to the buffer */
1501 send_buffer_fill += status;
1502 send_buffer_ptr += status;
1503 }
1504 else
1505 {
1506 flush_buffer ();
1508 status = add_to_buffer (send_buffer_ptr,
1509 sizeof (send_buffer) - send_buffer_fill,
1510 &send_buffer_vl,
1511 send_buffer_type, sizeof (send_buffer_type),
1512 ds, vl);
1514 if (status >= 0)
1515 {
1516 send_buffer_fill += status;
1517 send_buffer_ptr += status;
1518 }
1519 }
1521 if (status < 0)
1522 {
1523 ERROR ("network plugin: Unable to append to the "
1524 "buffer for some weird reason");
1525 }
1526 else if ((sizeof (send_buffer) - send_buffer_fill) < 15)
1527 {
1528 flush_buffer ();
1529 }
1531 pthread_mutex_unlock (&send_buffer_lock);
1533 return ((status < 0) ? -1 : 0);
1534 } /* int network_write */
1536 static int network_config (const char *key, const char *val)
1537 {
1538 char *node;
1539 char *service;
1541 char *fields[3];
1542 int fields_num;
1544 if ((strcasecmp ("Listen", key) == 0)
1545 || (strcasecmp ("Server", key) == 0))
1546 {
1547 char *val_cpy = strdup (val);
1548 if (val_cpy == NULL)
1549 return (1);
1551 service = NET_DEFAULT_PORT;
1552 fields_num = strsplit (val_cpy, fields, 3);
1553 if ((fields_num != 1)
1554 && (fields_num != 2))
1555 {
1556 sfree (val_cpy);
1557 return (1);
1558 }
1559 else if (fields_num == 2)
1560 {
1561 if ((service = strchr (fields[1], '.')) != NULL)
1562 *service = '\0';
1563 service = fields[1];
1564 }
1565 node = fields[0];
1567 if (strcasecmp ("Listen", key) == 0)
1568 network_add_listen_socket (node, service);
1569 else
1570 network_add_sending_socket (node, service);
1572 sfree (val_cpy);
1573 }
1574 else if (strcasecmp ("TimeToLive", key) == 0)
1575 {
1576 int tmp = atoi (val);
1577 if ((tmp > 0) && (tmp < 256))
1578 network_config_ttl = tmp;
1579 else
1580 return (1);
1581 }
1582 else if (strcasecmp ("Forward", key) == 0)
1583 {
1584 if ((strcasecmp ("true", val) == 0)
1585 || (strcasecmp ("yes", val) == 0)
1586 || (strcasecmp ("on", val) == 0))
1587 network_config_forward = 1;
1588 else
1589 network_config_forward = 0;
1590 }
1591 else if (strcasecmp ("CacheFlush", key) == 0)
1592 {
1593 int tmp = atoi (val);
1594 if (tmp > 0)
1595 cache_flush_interval = tmp;
1596 else return (1);
1597 }
1598 else
1599 {
1600 return (-1);
1601 }
1602 return (0);
1603 } /* int network_config */
1605 static int network_notification (const notification_t *n)
1606 {
1607 char buffer[BUFF_SIZE];
1608 char *buffer_ptr = buffer;
1609 int buffer_free = sizeof (buffer);
1610 int status;
1612 memset (buffer, '\0', sizeof (buffer));
1615 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_TIME,
1616 (uint64_t) n->time);
1617 if (status != 0)
1618 return (-1);
1620 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_SEVERITY,
1621 (uint64_t) n->severity);
1622 if (status != 0)
1623 return (-1);
1625 if (strlen (n->host) > 0)
1626 {
1627 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_HOST,
1628 n->host, strlen (n->host));
1629 if (status != 0)
1630 return (-1);
1631 }
1633 if (strlen (n->plugin) > 0)
1634 {
1635 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_PLUGIN,
1636 n->plugin, strlen (n->plugin));
1637 if (status != 0)
1638 return (-1);
1639 }
1641 if (strlen (n->plugin_instance) > 0)
1642 {
1643 status = write_part_string (&buffer_ptr, &buffer_free,
1644 TYPE_PLUGIN_INSTANCE,
1645 n->plugin_instance, strlen (n->plugin_instance));
1646 if (status != 0)
1647 return (-1);
1648 }
1650 if (strlen (n->type) > 0)
1651 {
1652 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE,
1653 n->type, strlen (n->type));
1654 if (status != 0)
1655 return (-1);
1656 }
1658 if (strlen (n->type_instance) > 0)
1659 {
1660 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE_INSTANCE,
1661 n->type_instance, strlen (n->type_instance));
1662 if (status != 0)
1663 return (-1);
1664 }
1666 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_MESSAGE,
1667 n->message, strlen (n->message));
1668 if (status != 0)
1669 return (-1);
1671 network_send_buffer (buffer, sizeof (buffer) - buffer_free);
1673 return (0);
1674 } /* int network_notification */
1676 static int network_shutdown (void)
1677 {
1678 listen_loop++;
1680 /* Kill the listening thread */
1681 if (receive_thread_id != (pthread_t) 0)
1682 {
1683 pthread_kill (receive_thread_id, SIGTERM);
1684 pthread_join (receive_thread_id, NULL /* no return value */);
1685 receive_thread_id = (pthread_t) 0;
1686 }
1688 /* Shutdown the dispatching thread */
1689 if (dispatch_thread_id != (pthread_t) 0)
1690 pthread_cond_broadcast (&receive_list_cond);
1692 if (send_buffer_fill > 0)
1693 flush_buffer ();
1695 if (cache_tree != NULL)
1696 {
1697 void *key;
1698 void *value;
1700 while (c_avl_pick (cache_tree, &key, &value) == 0)
1701 {
1702 sfree (key);
1703 sfree (value);
1704 }
1705 c_avl_destroy (cache_tree);
1706 cache_tree = NULL;
1707 }
1709 /* TODO: Close `sending_sockets' */
1711 plugin_unregister_config ("network");
1712 plugin_unregister_init ("network");
1713 plugin_unregister_write ("network");
1714 plugin_unregister_shutdown ("network");
1716 /* Let the init function do it's move again ;) */
1717 cache_flush_last = 0;
1719 return (0);
1720 } /* int network_shutdown */
1722 static int network_init (void)
1723 {
1724 /* Check if we were already initialized. If so, just return - there's
1725 * nothing more to do (for now, that is). */
1726 if (cache_flush_last != 0)
1727 return (0);
1729 plugin_register_shutdown ("network", network_shutdown);
1731 send_buffer_ptr = send_buffer;
1732 send_buffer_fill = 0;
1733 memset (&send_buffer_vl, '\0', sizeof (send_buffer_vl));
1734 memset (send_buffer_type, '\0', sizeof (send_buffer_type));
1736 cache_tree = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1737 cache_flush_last = time (NULL);
1739 /* setup socket(s) and so on */
1740 if (sending_sockets != NULL)
1741 {
1742 plugin_register_write ("network", network_write);
1743 plugin_register_notification ("network", network_notification);
1744 }
1746 if ((listen_sockets_num != 0) && (receive_thread_id == 0))
1747 {
1748 int status;
1750 status = pthread_create (&dispatch_thread_id,
1751 NULL /* no attributes */,
1752 dispatch_thread,
1753 NULL /* no argument */);
1754 if (status != 0)
1755 {
1756 char errbuf[1024];
1757 ERROR ("network: pthread_create failed: %s",
1758 sstrerror (errno, errbuf,
1759 sizeof (errbuf)));
1760 }
1762 status = pthread_create (&receive_thread_id,
1763 NULL /* no attributes */,
1764 receive_thread,
1765 NULL /* no argument */);
1766 if (status != 0)
1767 {
1768 char errbuf[1024];
1769 ERROR ("network: pthread_create failed: %s",
1770 sstrerror (errno, errbuf,
1771 sizeof (errbuf)));
1772 }
1773 }
1774 return (0);
1775 } /* int network_init */
1777 static int network_flush (int timeout)
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 */