2 * collectd - src/network.c
3 * Copyright (C) 2005-2008 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <octo at verplant.org>
25 #include "configfile.h"
26 #include "utils_avltree.h"
34 # include <sys/socket.h>
40 # include <netinet/in.h>
43 # include <arpa/inet.h>
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
56 # error "Neither IP_ADD_MEMBERSHIP nor IPV6_JOIN_GROUP is defined"
58 #endif /* !IP_ADD_MEMBERSHIP */
60 #define BUFF_SIZE 1024
65 typedef struct sockent
68 struct sockaddr_storage *addr;
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 * +-------+-----------------------+-------------------------------+
77 * +-------+-----------------------+-------------------------------+
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 * +-------------------------------+-------------------------------+
90 * +-------------------------------+-------------------------------+
91 * : (Length - 4) Bytes :
92 * +---------------------------------------------------------------+
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 * +-------------------------------+-------------------------------+
105 * +-------------------------------+-------------------------------+
106 * : (Length - 4 == 2 || 4 || 8) Bytes :
107 * +---------------------------------------------------------------+
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 * +-------------------------------+-------------------------------+
120 * +-------------------------------+---------------+---------------+
121 * ! Num of values ! Type0 ! Type1 !
122 * +-------------------------------+---------------+---------------+
125 * +---------------------------------------------------------------+
128 * +---------------------------------------------------------------+
133 uint16_t *num_values;
134 uint8_t *values_types;
137 typedef struct part_values_s part_values_t;
139 struct receive_list_entry_s
141 char data[BUFF_SIZE];
143 struct receive_list_entry_s *next;
145 typedef struct receive_list_entry_s receive_list_entry_t;
150 static const char *config_keys[] =
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 /* The receive and dispatch threads will run as long as `listen_loop' is set to
175 static int listen_loop = 0;
176 static int receive_thread_running = 0;
177 static pthread_t receive_thread_id;
178 static int dispatch_thread_running = 0;
179 static pthread_t dispatch_thread_id;
181 /* Buffer in which to-be-sent network packets are constructed. */
182 static char send_buffer[BUFF_SIZE];
183 static char *send_buffer_ptr;
184 static int send_buffer_fill;
185 static value_list_t send_buffer_vl = VALUE_LIST_STATIC;
186 static pthread_mutex_t send_buffer_lock = PTHREAD_MUTEX_INITIALIZER;
188 /* In this cache we store all the values we received, so we can send out only
189 * those values which were *not* received via the network plugin, too. This is
190 * used for the `Forward false' option. */
191 static c_avl_tree_t *cache_tree = NULL;
192 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
193 static time_t cache_flush_last = 0;
194 static int cache_flush_interval = 1800;
199 static int cache_flush (void)
209 c_avl_iterator_t *iter;
211 time_t curtime = time (NULL);
213 iter = c_avl_get_iterator (cache_tree);
214 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
216 if ((curtime - *value) <= cache_flush_interval)
218 tmp = (char **) realloc (keys,
219 (keys_num + 1) * sizeof (char *));
223 c_avl_iterator_destroy (iter);
224 ERROR ("network plugin: cache_flush: realloc"
229 keys[keys_num] = key;
231 } /* while (c_avl_iterator_next) */
232 c_avl_iterator_destroy (iter);
234 for (i = 0; i < keys_num; i++)
236 if (c_avl_remove (cache_tree, keys[i], (void *) &key,
237 (void *) &value) != 0)
239 WARNING ("network plugin: cache_flush: c_avl_remove"
240 " (%s) failed.", keys[i]);
250 DEBUG ("network plugin: cache_flush: Removed %i %s",
251 keys_num, (keys_num == 1) ? "entry" : "entries");
252 cache_flush_last = curtime;
254 } /* int cache_flush */
256 static int cache_check (const value_list_t *vl)
259 time_t *value = NULL;
262 if (cache_tree == NULL)
265 if (format_name (key, sizeof (key), vl->host, vl->plugin,
266 vl->plugin_instance, vl->type, vl->type_instance))
269 pthread_mutex_lock (&cache_lock);
271 if (c_avl_get (cache_tree, key, (void *) &value) == 0)
273 if (*value < vl->time)
280 DEBUG ("network plugin: cache_check: *value = %i >= vl->time = %i",
281 (int) *value, (int) vl->time);
287 char *key_copy = strdup (key);
288 value = malloc (sizeof (time_t));
289 if ((key_copy != NULL) && (value != NULL))
292 c_avl_insert (cache_tree, key_copy, value);
302 if ((time (NULL) - cache_flush_last) > cache_flush_interval)
305 pthread_mutex_unlock (&cache_lock);
307 DEBUG ("network plugin: cache_check: key = %s; time = %i; retval = %i",
308 key, (int) vl->time, retval);
311 } /* int cache_check */
313 static int write_part_values (char **ret_buffer, int *ret_buffer_len,
314 const data_set_t *ds, const value_list_t *vl)
320 part_header_t pkg_ph;
321 uint16_t pkg_num_values;
322 uint8_t *pkg_values_types;
328 num_values = vl->values_len;
329 packet_len = sizeof (part_header_t) + sizeof (uint16_t)
330 + (num_values * sizeof (uint8_t))
331 + (num_values * sizeof (value_t));
333 if (*ret_buffer_len < packet_len)
336 pkg_values_types = (uint8_t *) malloc (num_values * sizeof (uint8_t));
337 if (pkg_values_types == NULL)
339 ERROR ("network plugin: write_part_values: malloc failed.");
343 pkg_values = (value_t *) malloc (num_values * sizeof (value_t));
344 if (pkg_values == NULL)
346 free (pkg_values_types);
347 ERROR ("network plugin: write_part_values: malloc failed.");
351 pkg_ph.type = htons (TYPE_VALUES);
352 pkg_ph.length = htons (packet_len);
354 pkg_num_values = htons ((uint16_t) vl->values_len);
356 for (i = 0; i < num_values; i++)
358 if (ds->ds[i].type == DS_TYPE_COUNTER)
360 pkg_values_types[i] = DS_TYPE_COUNTER;
361 pkg_values[i].counter = htonll (vl->values[i].counter);
365 pkg_values_types[i] = DS_TYPE_GAUGE;
366 pkg_values[i].gauge = htond (vl->values[i].gauge);
371 * Use `memcpy' to write everything to the buffer, because the pointer
372 * may be unaligned and some architectures, such as SPARC, can't handle
375 packet_ptr = *ret_buffer;
377 memcpy (packet_ptr + offset, &pkg_ph, sizeof (pkg_ph));
378 offset += sizeof (pkg_ph);
379 memcpy (packet_ptr + offset, &pkg_num_values, sizeof (pkg_num_values));
380 offset += sizeof (pkg_num_values);
381 memcpy (packet_ptr + offset, pkg_values_types, num_values * sizeof (uint8_t));
382 offset += num_values * sizeof (uint8_t);
383 memcpy (packet_ptr + offset, pkg_values, num_values * sizeof (value_t));
384 offset += num_values * sizeof (value_t);
386 assert (offset == packet_len);
388 *ret_buffer = packet_ptr + packet_len;
389 *ret_buffer_len -= packet_len;
391 free (pkg_values_types);
395 } /* int write_part_values */
397 static int write_part_number (char **ret_buffer, int *ret_buffer_len,
398 int type, uint64_t value)
403 part_header_t pkg_head;
408 packet_len = sizeof (pkg_head) + sizeof (pkg_value);
410 if (*ret_buffer_len < packet_len)
413 pkg_head.type = htons (type);
414 pkg_head.length = htons (packet_len);
415 pkg_value = htonll (value);
417 packet_ptr = *ret_buffer;
419 memcpy (packet_ptr + offset, &pkg_head, sizeof (pkg_head));
420 offset += sizeof (pkg_head);
421 memcpy (packet_ptr + offset, &pkg_value, sizeof (pkg_value));
422 offset += sizeof (pkg_value);
424 assert (offset == packet_len);
426 *ret_buffer = packet_ptr + packet_len;
427 *ret_buffer_len -= packet_len;
430 } /* int write_part_number */
432 static int write_part_string (char **ret_buffer, int *ret_buffer_len,
433 int type, const char *str, int str_len)
443 buffer_len = 2 * sizeof (uint16_t) + str_len + 1;
444 if (*ret_buffer_len < buffer_len)
447 pkg_type = htons (type);
448 pkg_length = htons (buffer_len);
450 buffer = *ret_buffer;
452 memcpy (buffer + offset, (void *) &pkg_type, sizeof (pkg_type));
453 offset += sizeof (pkg_type);
454 memcpy (buffer + offset, (void *) &pkg_length, sizeof (pkg_length));
455 offset += sizeof (pkg_length);
456 memcpy (buffer + offset, str, str_len);
458 memset (buffer + offset, '\0', 1);
461 assert (offset == buffer_len);
463 *ret_buffer = buffer + buffer_len;
464 *ret_buffer_len -= buffer_len;
467 } /* int write_part_string */
469 static int parse_part_values (void **ret_buffer, int *ret_buffer_len,
470 value_t **ret_values, int *ret_num_values)
472 char *buffer = *ret_buffer;
473 int buffer_len = *ret_buffer_len;
486 if (buffer_len < (15))
488 DEBUG ("network plugin: packet is too short: buffer_len = %i",
493 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
494 buffer += sizeof (tmp16);
495 pkg_type = ntohs (tmp16);
497 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
498 buffer += sizeof (tmp16);
499 pkg_length = ntohs (tmp16);
501 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
502 buffer += sizeof (tmp16);
503 pkg_numval = ntohs (tmp16);
505 assert (pkg_type == TYPE_VALUES);
507 exp_size = 3 * sizeof (uint16_t)
508 + pkg_numval * (sizeof (uint8_t) + sizeof (value_t));
509 if ((buffer_len < 0) || ((size_t) buffer_len < exp_size))
511 WARNING ("network plugin: parse_part_values: "
513 "Chunk of size %u expected, "
514 "but buffer has only %i bytes left.",
515 (unsigned int) exp_size, buffer_len);
519 if (pkg_length != exp_size)
521 WARNING ("network plugin: parse_part_values: "
522 "Length and number of values "
523 "in the packet don't match.");
527 pkg_types = (uint8_t *) malloc (pkg_numval * sizeof (uint8_t));
528 pkg_values = (value_t *) malloc (pkg_numval * sizeof (value_t));
529 if ((pkg_types == NULL) || (pkg_values == NULL))
533 ERROR ("network plugin: parse_part_values: malloc failed.");
537 memcpy ((void *) pkg_types, (void *) buffer, pkg_numval * sizeof (uint8_t));
538 buffer += pkg_numval * sizeof (uint8_t);
539 memcpy ((void *) pkg_values, (void *) buffer, pkg_numval * sizeof (value_t));
540 buffer += pkg_numval * sizeof (value_t);
542 for (i = 0; i < pkg_numval; i++)
544 if (pkg_types[i] == DS_TYPE_COUNTER)
545 pkg_values[i].counter = ntohll (pkg_values[i].counter);
546 else if (pkg_types[i] == DS_TYPE_GAUGE)
547 pkg_values[i].gauge = ntohd (pkg_values[i].gauge);
550 *ret_buffer = buffer;
551 *ret_buffer_len = buffer_len - pkg_length;
552 *ret_num_values = pkg_numval;
553 *ret_values = pkg_values;
558 } /* int parse_part_values */
560 static int parse_part_number (void **ret_buffer, int *ret_buffer_len,
563 char *buffer = *ret_buffer;
564 int buffer_len = *ret_buffer_len;
568 size_t exp_size = 2 * sizeof (uint16_t) + sizeof (uint64_t);
573 if ((buffer_len < 0) || ((size_t) buffer_len < exp_size))
575 WARNING ("network plugin: parse_part_number: "
577 "Chunk of size %u expected, "
578 "but buffer has only %i bytes left.",
579 (unsigned int) exp_size, buffer_len);
583 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
584 buffer += sizeof (tmp16);
585 pkg_type = ntohs (tmp16);
587 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
588 buffer += sizeof (tmp16);
589 pkg_length = ntohs (tmp16);
591 memcpy ((void *) &tmp64, buffer, sizeof (tmp64));
592 buffer += sizeof (tmp64);
593 *value = ntohll (tmp64);
595 *ret_buffer = buffer;
596 *ret_buffer_len = buffer_len - pkg_length;
599 } /* int parse_part_number */
601 static int parse_part_string (void **ret_buffer, int *ret_buffer_len,
602 char *output, int output_len)
604 char *buffer = *ret_buffer;
605 int buffer_len = *ret_buffer_len;
608 size_t header_size = 2 * sizeof (uint16_t);
613 if ((buffer_len < 0) || ((size_t) buffer_len < header_size))
615 WARNING ("network plugin: parse_part_string: "
617 "Chunk of at least size %u expected, "
618 "but buffer has only %i bytes left.",
619 (unsigned int) header_size, buffer_len);
623 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
624 buffer += sizeof (tmp16);
625 pkg_type = ntohs (tmp16);
627 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
628 buffer += sizeof (tmp16);
629 pkg_length = ntohs (tmp16);
631 /* Check that packet fits in the input buffer */
632 if (pkg_length > buffer_len)
634 WARNING ("network plugin: parse_part_string: "
636 "Chunk of size %hu received, "
637 "but buffer has only %i bytes left.",
638 pkg_length, buffer_len);
642 /* Check that pkg_length is in the valid range */
643 if (pkg_length <= header_size)
645 WARNING ("network plugin: parse_part_string: "
647 "Header claims this packet is only %hu "
648 "bytes long.", pkg_length);
652 /* Check that the package data fits into the output buffer.
653 * The previous if-statement ensures that:
654 * `pkg_length > header_size' */
656 || ((size_t) output_len < ((size_t) pkg_length - header_size)))
658 WARNING ("network plugin: parse_part_string: "
659 "Output buffer too small.");
663 /* All sanity checks successfull, let's copy the data over */
664 output_len = pkg_length - header_size;
665 memcpy ((void *) output, (void *) buffer, output_len);
666 buffer += output_len;
668 /* For some very weird reason '\0' doesn't do the trick on SPARC in
670 if (output[output_len - 1] != 0)
672 WARNING ("network plugin: parse_part_string: "
673 "Received string does not end "
674 "with a NULL-byte.");
678 *ret_buffer = buffer;
679 *ret_buffer_len = buffer_len - pkg_length;
682 } /* int parse_part_string */
684 static int parse_packet (void *buffer, int buffer_len)
688 value_list_t vl = VALUE_LIST_INIT;
691 DEBUG ("network plugin: parse_packet: buffer = %p; buffer_len = %i;",
694 memset (&vl, '\0', sizeof (vl));
695 memset (&n, '\0', sizeof (n));
698 while ((status == 0) && (0 < buffer_len)
699 && ((unsigned int) buffer_len > sizeof (part_header_t)))
704 memcpy ((void *) &pkg_type,
707 memcpy ((void *) &pkg_length,
708 (void *) (buffer + sizeof (pkg_type)),
709 sizeof (pkg_length));
711 pkg_length = ntohs (pkg_length);
712 pkg_type = ntohs (pkg_type);
714 if (pkg_length > buffer_len)
716 /* Ensure that this loop terminates eventually */
717 if (pkg_length < (2 * sizeof (uint16_t)))
720 if (pkg_type == TYPE_VALUES)
722 status = parse_part_values (&buffer, &buffer_len,
723 &vl.values, &vl.values_len);
729 && (strlen (vl.host) > 0)
730 && (strlen (vl.plugin) > 0)
731 && (strlen (vl.type) > 0)
732 && (cache_check (&vl) == 0))
734 plugin_dispatch_values (&vl);
738 DEBUG ("network plugin: parse_packet:"
739 " NOT dispatching values");
744 else if (pkg_type == TYPE_TIME)
747 status = parse_part_number (&buffer, &buffer_len,
751 vl.time = (time_t) tmp;
752 n.time = (time_t) tmp;
755 else if (pkg_type == TYPE_INTERVAL)
758 status = parse_part_number (&buffer, &buffer_len,
761 vl.interval = (int) tmp;
763 else if (pkg_type == TYPE_HOST)
765 status = parse_part_string (&buffer, &buffer_len,
766 vl.host, sizeof (vl.host));
768 sstrncpy (n.host, vl.host, sizeof (n.host));
770 else if (pkg_type == TYPE_PLUGIN)
772 status = parse_part_string (&buffer, &buffer_len,
773 vl.plugin, sizeof (vl.plugin));
775 sstrncpy (n.plugin, vl.plugin,
778 else if (pkg_type == TYPE_PLUGIN_INSTANCE)
780 status = parse_part_string (&buffer, &buffer_len,
782 sizeof (vl.plugin_instance));
784 sstrncpy (n.plugin_instance,
786 sizeof (n.plugin_instance));
788 else if (pkg_type == TYPE_TYPE)
790 status = parse_part_string (&buffer, &buffer_len,
791 vl.type, sizeof (vl.type));
793 sstrncpy (n.type, vl.type, sizeof (n.type));
795 else if (pkg_type == TYPE_TYPE_INSTANCE)
797 status = parse_part_string (&buffer, &buffer_len,
799 sizeof (vl.type_instance));
801 sstrncpy (n.type_instance, vl.type_instance,
802 sizeof (n.type_instance));
804 else if (pkg_type == TYPE_MESSAGE)
806 status = parse_part_string (&buffer, &buffer_len,
807 n.message, sizeof (n.message));
813 else if ((n.severity != NOTIF_FAILURE)
814 && (n.severity != NOTIF_WARNING)
815 && (n.severity != NOTIF_OKAY))
817 INFO ("network plugin: "
818 "Ignoring notification with "
819 "unknown severity %i.",
822 else if (n.time <= 0)
824 INFO ("network plugin: "
825 "Ignoring notification with "
828 else if (strlen (n.message) <= 0)
830 INFO ("network plugin: "
831 "Ignoring notification with "
832 "an empty message.");
836 plugin_dispatch_notification (&n);
839 else if (pkg_type == TYPE_SEVERITY)
842 status = parse_part_number (&buffer, &buffer_len,
845 n.severity = (int) tmp;
849 DEBUG ("network plugin: parse_packet: Unknown part"
850 " type: 0x%04hx", pkg_type);
851 buffer = ((char *) buffer) + pkg_length;
853 } /* while (buffer_len > sizeof (part_header_t)) */
856 } /* int parse_packet */
858 static void free_sockent (sockent_t *se)
868 } /* void free_sockent */
871 * int network_set_ttl
873 * Set the `IP_MULTICAST_TTL', `IP_TTL', `IPV6_MULTICAST_HOPS' or
874 * `IPV6_UNICAST_HOPS', depending on which option is applicable.
876 * The `struct addrinfo' is used to destinguish between unicast and multicast
879 static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
881 if ((network_config_ttl < 1) || (network_config_ttl > 255))
884 DEBUG ("ttl = %i", network_config_ttl);
886 if (ai->ai_family == AF_INET)
888 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
891 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
892 optname = IP_MULTICAST_TTL;
896 if (setsockopt (se->fd, IPPROTO_IP, optname,
898 sizeof (network_config_ttl)) == -1)
901 ERROR ("setsockopt: %s",
902 sstrerror (errno, errbuf, sizeof (errbuf)));
906 else if (ai->ai_family == AF_INET6)
908 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
909 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
912 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
913 optname = IPV6_MULTICAST_HOPS;
915 optname = IPV6_UNICAST_HOPS;
917 if (setsockopt (se->fd, IPPROTO_IPV6, optname,
919 sizeof (network_config_ttl)) == -1)
922 ERROR ("setsockopt: %s",
923 sstrerror (errno, errbuf,
930 } /* int network_set_ttl */
932 static int network_bind_socket (const sockent_t *se, const struct addrinfo *ai)
937 /* allow multiple sockets to use the same PORT number */
938 if (setsockopt(se->fd, SOL_SOCKET, SO_REUSEADDR,
939 &yes, sizeof(yes)) == -1) {
941 ERROR ("setsockopt: %s",
942 sstrerror (errno, errbuf, sizeof (errbuf)));
946 DEBUG ("fd = %i; calling `bind'", se->fd);
948 if (bind (se->fd, ai->ai_addr, ai->ai_addrlen) == -1)
952 sstrerror (errno, errbuf, sizeof (errbuf)));
956 if (ai->ai_family == AF_INET)
958 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
959 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
963 DEBUG ("fd = %i; IPv4 multicast address found", se->fd);
965 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
966 mreq.imr_interface.s_addr = htonl (INADDR_ANY);
968 if (setsockopt (se->fd, IPPROTO_IP, IP_MULTICAST_LOOP,
969 &loop, sizeof (loop)) == -1)
972 ERROR ("setsockopt: %s",
973 sstrerror (errno, errbuf,
978 if (setsockopt (se->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
979 &mreq, sizeof (mreq)) == -1)
982 ERROR ("setsockopt: %s",
983 sstrerror (errno, errbuf,
989 else if (ai->ai_family == AF_INET6)
991 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
992 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
993 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
995 struct ipv6_mreq mreq;
997 DEBUG ("fd = %i; IPv6 multicast address found", se->fd);
999 memcpy (&mreq.ipv6mr_multiaddr,
1001 sizeof (addr->sin6_addr));
1003 /* http://developer.apple.com/documentation/Darwin/Reference/ManPages/man4/ip6.4.html
1004 * ipv6mr_interface may be set to zeroes to
1005 * choose the default multicast interface or to
1006 * the index of a particular multicast-capable
1007 * interface if the host is multihomed.
1008 * Membership is associ-associated with a
1009 * single interface; programs running on
1010 * multihomed hosts may need to join the same
1011 * group on more than one interface.*/
1012 mreq.ipv6mr_interface = 0;
1014 if (setsockopt (se->fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
1015 &loop, sizeof (loop)) == -1)
1018 ERROR ("setsockopt: %s",
1019 sstrerror (errno, errbuf,
1024 if (setsockopt (se->fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
1025 &mreq, sizeof (mreq)) == -1)
1028 ERROR ("setsockopt: %s",
1029 sstrerror (errno, errbuf,
1037 } /* int network_bind_socket */
1039 static sockent_t *network_create_socket (const char *node,
1040 const char *service,
1043 struct addrinfo ai_hints;
1044 struct addrinfo *ai_list, *ai_ptr;
1047 sockent_t *se_head = NULL;
1048 sockent_t *se_tail = NULL;
1050 DEBUG ("node = %s, service = %s", node, service);
1052 memset (&ai_hints, '\0', sizeof (ai_hints));
1053 ai_hints.ai_flags = 0;
1055 ai_hints.ai_flags |= AI_PASSIVE;
1057 #ifdef AI_ADDRCONFIG
1058 ai_hints.ai_flags |= AI_ADDRCONFIG;
1060 ai_hints.ai_family = AF_UNSPEC;
1061 ai_hints.ai_socktype = SOCK_DGRAM;
1062 ai_hints.ai_protocol = IPPROTO_UDP;
1064 ai_return = getaddrinfo (node, service, &ai_hints, &ai_list);
1068 ERROR ("getaddrinfo (%s, %s): %s",
1069 (node == NULL) ? "(null)" : node,
1070 (service == NULL) ? "(null)" : service,
1071 (ai_return == EAI_SYSTEM)
1072 ? sstrerror (errno, errbuf, sizeof (errbuf))
1073 : gai_strerror (ai_return));
1077 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1081 if ((se = (sockent_t *) malloc (sizeof (sockent_t))) == NULL)
1084 ERROR ("malloc: %s",
1085 sstrerror (errno, errbuf,
1090 if ((se->addr = (struct sockaddr_storage *) malloc (sizeof (struct sockaddr_storage))) == NULL)
1093 ERROR ("malloc: %s",
1094 sstrerror (errno, errbuf,
1100 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
1101 memset (se->addr, '\0', sizeof (struct sockaddr_storage));
1102 memcpy (se->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
1103 se->addrlen = ai_ptr->ai_addrlen;
1105 se->fd = socket (ai_ptr->ai_family,
1106 ai_ptr->ai_socktype,
1107 ai_ptr->ai_protocol);
1113 ERROR ("socket: %s",
1114 sstrerror (errno, errbuf,
1123 if (network_bind_socket (se, ai_ptr) != 0)
1131 else /* listen == 0 */
1133 network_set_ttl (se, ai_ptr);
1136 if (se_tail == NULL)
1147 /* We don't open more than one write-socket per node/service pair.. */
1152 freeaddrinfo (ai_list);
1155 } /* sockent_t *network_create_socket */
1157 static sockent_t *network_create_default_socket (int listen)
1159 sockent_t *se_ptr = NULL;
1160 sockent_t *se_head = NULL;
1161 sockent_t *se_tail = NULL;
1163 se_ptr = network_create_socket (NET_DEFAULT_V6_ADDR,
1164 NET_DEFAULT_PORT, listen);
1166 /* Don't send to the same machine in IPv6 and IPv4 if both are available. */
1167 if ((listen == 0) && (se_ptr != NULL))
1174 while (se_tail->next != NULL)
1175 se_tail = se_tail->next;
1178 se_ptr = network_create_socket (NET_DEFAULT_V4_ADDR, NET_DEFAULT_PORT, listen);
1180 if (se_tail == NULL)
1183 se_tail->next = se_ptr;
1185 } /* sockent_t *network_create_default_socket */
1187 static int network_add_listen_socket (const char *node, const char *service)
1193 if (service == NULL)
1194 service = NET_DEFAULT_PORT;
1197 se = network_create_default_socket (1 /* listen == true */);
1199 se = network_create_socket (node, service, 1 /* listen == true */);
1204 for (se_ptr = se; se_ptr != NULL; se_ptr = se_ptr->next)
1207 listen_sockets = (struct pollfd *) realloc (listen_sockets,
1208 (listen_sockets_num + se_num)
1209 * sizeof (struct pollfd));
1211 for (se_ptr = se; se_ptr != NULL; se_ptr = se_ptr->next)
1213 listen_sockets[listen_sockets_num].fd = se_ptr->fd;
1214 listen_sockets[listen_sockets_num].events = POLLIN | POLLPRI;
1215 listen_sockets[listen_sockets_num].revents = 0;
1216 listen_sockets_num++;
1221 } /* int network_add_listen_socket */
1223 static int network_add_sending_socket (const char *node, const char *service)
1228 if (service == NULL)
1229 service = NET_DEFAULT_PORT;
1232 se = network_create_default_socket (0 /* listen == false */);
1234 se = network_create_socket (node, service, 0 /* listen == false */);
1239 if (sending_sockets == NULL)
1241 sending_sockets = se;
1245 for (se_ptr = sending_sockets; se_ptr->next != NULL; se_ptr = se_ptr->next)
1250 } /* int network_get_listen_socket */
1252 static void *dispatch_thread (void __attribute__((unused)) *arg)
1256 receive_list_entry_t *ent;
1258 /* Lock and wait for more data to come in */
1259 pthread_mutex_lock (&receive_list_lock);
1260 while ((listen_loop == 0)
1261 && (receive_list_head == NULL))
1262 pthread_cond_wait (&receive_list_cond, &receive_list_lock);
1264 /* Remove the head entry and unlock */
1265 ent = receive_list_head;
1267 receive_list_head = ent->next;
1268 pthread_mutex_unlock (&receive_list_lock);
1270 /* Check whether we are supposed to exit. We do NOT check `listen_loop'
1271 * because we dispatch all missing packets before shutting down. */
1275 parse_packet (ent->data, ent->data_len);
1281 } /* void *receive_thread */
1283 static int network_receive (void)
1285 char buffer[BUFF_SIZE];
1291 receive_list_entry_t *private_list_head;
1292 receive_list_entry_t *private_list_tail;
1294 if (listen_sockets_num == 0)
1295 network_add_listen_socket (NULL, NULL);
1297 if (listen_sockets_num == 0)
1299 ERROR ("network: Failed to open a listening socket.");
1303 private_list_head = NULL;
1304 private_list_tail = NULL;
1306 while (listen_loop == 0)
1308 status = poll (listen_sockets, listen_sockets_num, -1);
1315 ERROR ("poll failed: %s",
1316 sstrerror (errno, errbuf, sizeof (errbuf)));
1320 for (i = 0; (i < listen_sockets_num) && (status > 0); i++)
1322 receive_list_entry_t *ent;
1324 if ((listen_sockets[i].revents & (POLLIN | POLLPRI)) == 0)
1328 buffer_len = recv (listen_sockets[i].fd,
1329 buffer, sizeof (buffer),
1334 ERROR ("recv failed: %s",
1335 sstrerror (errno, errbuf,
1340 /* TODO: Possible performance enhancement: Do not free
1341 * these entries in the dispatch thread but put them in
1342 * another list, so we don't have to allocate more and
1343 * more of these structures. */
1344 ent = malloc (sizeof (receive_list_entry_t));
1347 ERROR ("network plugin: malloc failed.");
1350 memset (ent, 0, sizeof (receive_list_entry_t));
1353 /* Hopefully this be optimized out by the compiler. It
1354 * might help prevent stupid bugs in the future though.
1356 assert (sizeof (ent->data) == sizeof (buffer));
1358 memcpy (ent->data, buffer, buffer_len);
1359 ent->data_len = buffer_len;
1361 if (private_list_head == NULL)
1362 private_list_head = ent;
1364 private_list_tail->next = ent;
1365 private_list_tail = ent;
1367 /* Do not block here. Blocking here has led to
1368 * insufficient performance in the past. */
1369 if (pthread_mutex_trylock (&receive_list_lock) == 0)
1371 if (receive_list_head == NULL)
1372 receive_list_head = private_list_head;
1374 receive_list_tail->next = private_list_head;
1375 receive_list_tail = private_list_tail;
1377 private_list_head = NULL;
1378 private_list_tail = NULL;
1380 pthread_cond_signal (&receive_list_cond);
1381 pthread_mutex_unlock (&receive_list_lock);
1383 } /* for (listen_sockets) */
1384 } /* while (listen_loop == 0) */
1386 /* Make sure everything is dispatched before exiting. */
1387 if (private_list_head != NULL)
1389 pthread_mutex_lock (&receive_list_lock);
1391 if (receive_list_head == NULL)
1392 receive_list_head = private_list_head;
1394 receive_list_tail->next = private_list_head;
1395 receive_list_tail = private_list_tail;
1397 private_list_head = NULL;
1398 private_list_tail = NULL;
1400 pthread_cond_signal (&receive_list_cond);
1401 pthread_mutex_unlock (&receive_list_lock);
1407 static void *receive_thread (void __attribute__((unused)) *arg)
1409 return (network_receive () ? (void *) 1 : (void *) 0);
1410 } /* void *receive_thread */
1412 static void network_send_buffer (const char *buffer, int buffer_len)
1417 DEBUG ("network plugin: network_send_buffer: buffer_len = %i", buffer_len);
1419 for (se = sending_sockets; se != NULL; se = se->next)
1423 status = sendto (se->fd, buffer, buffer_len, 0 /* no flags */,
1424 (struct sockaddr *) se->addr, se->addrlen);
1430 ERROR ("network plugin: sendto failed: %s",
1431 sstrerror (errno, errbuf,
1438 } /* for (sending_sockets) */
1439 } /* void network_send_buffer */
1441 static int add_to_buffer (char *buffer, int buffer_size,
1442 value_list_t *vl_def,
1443 const data_set_t *ds, const value_list_t *vl)
1445 char *buffer_orig = buffer;
1447 if (strcmp (vl_def->host, vl->host) != 0)
1449 if (write_part_string (&buffer, &buffer_size, TYPE_HOST,
1450 vl->host, strlen (vl->host)) != 0)
1452 sstrncpy (vl_def->host, vl->host, sizeof (vl_def->host));
1455 if (vl_def->time != vl->time)
1457 if (write_part_number (&buffer, &buffer_size, TYPE_TIME,
1458 (uint64_t) vl->time))
1460 vl_def->time = vl->time;
1463 if (vl_def->interval != vl->interval)
1465 if (write_part_number (&buffer, &buffer_size, TYPE_INTERVAL,
1466 (uint64_t) vl->interval))
1468 vl_def->interval = vl->interval;
1471 if (strcmp (vl_def->plugin, vl->plugin) != 0)
1473 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN,
1474 vl->plugin, strlen (vl->plugin)) != 0)
1476 sstrncpy (vl_def->plugin, vl->plugin, sizeof (vl_def->plugin));
1479 if (strcmp (vl_def->plugin_instance, vl->plugin_instance) != 0)
1481 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
1482 vl->plugin_instance,
1483 strlen (vl->plugin_instance)) != 0)
1485 sstrncpy (vl_def->plugin_instance, vl->plugin_instance, sizeof (vl_def->plugin_instance));
1488 if (strcmp (vl_def->type, vl->type) != 0)
1490 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
1491 vl->type, strlen (vl->type)) != 0)
1493 sstrncpy (vl_def->type, ds->type, sizeof (vl_def->type));
1496 if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
1498 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE_INSTANCE,
1500 strlen (vl->type_instance)) != 0)
1502 sstrncpy (vl_def->type_instance, vl->type_instance, sizeof (vl_def->type_instance));
1505 if (write_part_values (&buffer, &buffer_size, ds, vl) != 0)
1508 return (buffer - buffer_orig);
1509 } /* int add_to_buffer */
1511 static void flush_buffer (void)
1513 DEBUG ("network plugin: flush_buffer: send_buffer_fill = %i",
1516 network_send_buffer (send_buffer, send_buffer_fill);
1517 send_buffer_ptr = send_buffer;
1518 send_buffer_fill = 0;
1519 memset (&send_buffer_vl, 0, sizeof (send_buffer_vl));
1522 static int network_write (const data_set_t *ds, const value_list_t *vl,
1523 user_data_t __attribute__((unused)) *user_data)
1527 /* If the value is already in the cache, we have received it via the
1528 * network. We write it again if forwarding is activated. It's then in
1529 * the cache and should we receive it again we will ignore it. */
1530 status = cache_check (vl);
1531 if ((network_config_forward == 0)
1535 pthread_mutex_lock (&send_buffer_lock);
1537 status = add_to_buffer (send_buffer_ptr,
1538 sizeof (send_buffer) - send_buffer_fill,
1543 /* status == bytes added to the buffer */
1544 send_buffer_fill += status;
1545 send_buffer_ptr += status;
1551 status = add_to_buffer (send_buffer_ptr,
1552 sizeof (send_buffer) - send_buffer_fill,
1558 send_buffer_fill += status;
1559 send_buffer_ptr += status;
1565 ERROR ("network plugin: Unable to append to the "
1566 "buffer for some weird reason");
1568 else if ((sizeof (send_buffer) - send_buffer_fill) < 15)
1573 pthread_mutex_unlock (&send_buffer_lock);
1575 return ((status < 0) ? -1 : 0);
1576 } /* int network_write */
1578 static int network_config (const char *key, const char *val)
1586 if ((strcasecmp ("Listen", key) == 0)
1587 || (strcasecmp ("Server", key) == 0))
1589 char *val_cpy = strdup (val);
1590 if (val_cpy == NULL)
1593 service = NET_DEFAULT_PORT;
1594 fields_num = strsplit (val_cpy, fields, 3);
1595 if ((fields_num != 1)
1596 && (fields_num != 2))
1601 else if (fields_num == 2)
1603 if ((service = strchr (fields[1], '.')) != NULL)
1605 service = fields[1];
1609 if (strcasecmp ("Listen", key) == 0)
1610 network_add_listen_socket (node, service);
1612 network_add_sending_socket (node, service);
1616 else if (strcasecmp ("TimeToLive", key) == 0)
1618 int tmp = atoi (val);
1619 if ((tmp > 0) && (tmp < 256))
1620 network_config_ttl = tmp;
1624 else if (strcasecmp ("Forward", key) == 0)
1626 if ((strcasecmp ("true", val) == 0)
1627 || (strcasecmp ("yes", val) == 0)
1628 || (strcasecmp ("on", val) == 0))
1629 network_config_forward = 1;
1631 network_config_forward = 0;
1633 else if (strcasecmp ("CacheFlush", key) == 0)
1635 int tmp = atoi (val);
1637 cache_flush_interval = tmp;
1645 } /* int network_config */
1647 static int network_notification (const notification_t *n,
1648 user_data_t __attribute__((unused)) *user_data)
1650 char buffer[BUFF_SIZE];
1651 char *buffer_ptr = buffer;
1652 int buffer_free = sizeof (buffer);
1655 memset (buffer, '\0', sizeof (buffer));
1658 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_TIME,
1659 (uint64_t) n->time);
1663 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_SEVERITY,
1664 (uint64_t) n->severity);
1668 if (strlen (n->host) > 0)
1670 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_HOST,
1671 n->host, strlen (n->host));
1676 if (strlen (n->plugin) > 0)
1678 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_PLUGIN,
1679 n->plugin, strlen (n->plugin));
1684 if (strlen (n->plugin_instance) > 0)
1686 status = write_part_string (&buffer_ptr, &buffer_free,
1687 TYPE_PLUGIN_INSTANCE,
1688 n->plugin_instance, strlen (n->plugin_instance));
1693 if (strlen (n->type) > 0)
1695 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE,
1696 n->type, strlen (n->type));
1701 if (strlen (n->type_instance) > 0)
1703 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE_INSTANCE,
1704 n->type_instance, strlen (n->type_instance));
1709 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_MESSAGE,
1710 n->message, strlen (n->message));
1714 network_send_buffer (buffer, sizeof (buffer) - buffer_free);
1717 } /* int network_notification */
1719 static int network_shutdown (void)
1723 /* Kill the listening thread */
1724 if (receive_thread_running != 0)
1726 INFO ("network plugin: Stopping receive thread.");
1727 pthread_kill (receive_thread_id, SIGTERM);
1728 pthread_join (receive_thread_id, NULL /* no return value */);
1729 memset (&receive_thread_id, 0, sizeof (receive_thread_id));
1730 receive_thread_running = 0;
1733 /* Shutdown the dispatching thread */
1734 if (dispatch_thread_running != 0)
1736 INFO ("network plugin: Stopping dispatch thread.");
1737 pthread_join (dispatch_thread_id, /* ret = */ NULL);
1738 pthread_cond_broadcast (&receive_list_cond);
1739 dispatch_thread_running = 0;
1742 if (send_buffer_fill > 0)
1745 if (cache_tree != NULL)
1750 while (c_avl_pick (cache_tree, &key, &value) == 0)
1755 c_avl_destroy (cache_tree);
1759 /* TODO: Close `sending_sockets' */
1761 plugin_unregister_config ("network");
1762 plugin_unregister_init ("network");
1763 plugin_unregister_write ("network");
1764 plugin_unregister_shutdown ("network");
1766 /* Let the init function do it's move again ;) */
1767 cache_flush_last = 0;
1770 } /* int network_shutdown */
1772 static int network_init (void)
1774 /* Check if we were already initialized. If so, just return - there's
1775 * nothing more to do (for now, that is). */
1776 if (cache_flush_last != 0)
1779 plugin_register_shutdown ("network", network_shutdown);
1781 send_buffer_ptr = send_buffer;
1782 send_buffer_fill = 0;
1783 memset (&send_buffer_vl, 0, sizeof (send_buffer_vl));
1785 cache_tree = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1786 cache_flush_last = time (NULL);
1788 /* setup socket(s) and so on */
1789 if (sending_sockets != NULL)
1791 plugin_register_write ("network", network_write,
1792 /* user_data = */ NULL);
1793 plugin_register_notification ("network", network_notification,
1794 /* user_data = */ NULL);
1797 /* If no threads need to be started, return here. */
1798 if ((listen_sockets_num == 0)
1799 || ((dispatch_thread_running != 0)
1800 && (receive_thread_running != 0)))
1803 if (dispatch_thread_running == 0)
1806 status = pthread_create (&dispatch_thread_id,
1807 NULL /* no attributes */,
1809 NULL /* no argument */);
1813 ERROR ("network: pthread_create failed: %s",
1814 sstrerror (errno, errbuf,
1819 dispatch_thread_running = 1;
1823 if (receive_thread_running == 0)
1826 status = pthread_create (&receive_thread_id,
1827 NULL /* no attributes */,
1829 NULL /* no argument */);
1833 ERROR ("network: pthread_create failed: %s",
1834 sstrerror (errno, errbuf,
1839 receive_thread_running = 1;
1844 } /* int network_init */
1847 * The flush option of the network plugin cannot flush individual identifiers.
1848 * All the values are added to a buffer and sent when the buffer is full, the
1849 * requested value may or may not be in there, it's not worth finding out. We
1850 * just send the buffer if `flush' is called - if the requested value was in
1851 * there, good. If not, well, then there is nothing to flush.. -octo
1853 static int network_flush (int timeout,
1854 const char __attribute__((unused)) *identifier,
1855 user_data_t __attribute__((unused)) *user_data)
1857 pthread_mutex_lock (&send_buffer_lock);
1859 if (((time (NULL) - cache_flush_last) >= timeout)
1860 && (send_buffer_fill > 0))
1865 pthread_mutex_unlock (&send_buffer_lock);
1868 } /* int network_flush */
1870 void module_register (void)
1872 plugin_register_config ("network", network_config,
1873 config_keys, config_keys_num);
1874 plugin_register_init ("network", network_init);
1875 plugin_register_flush ("network", network_flush,
1876 /* user_data = */ NULL);
1877 } /* void module_register */