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 static int listen_loop = 0;
174 static pthread_t receive_thread_id = 0;
175 static pthread_t dispatch_thread_id = 0;
177 static char send_buffer[BUFF_SIZE];
178 static char *send_buffer_ptr;
179 static int send_buffer_fill;
180 static value_list_t send_buffer_vl = VALUE_LIST_STATIC;
181 static pthread_mutex_t send_buffer_lock = PTHREAD_MUTEX_INITIALIZER;
183 static c_avl_tree_t *cache_tree = NULL;
184 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
185 static time_t cache_flush_last = 0;
186 static int cache_flush_interval = 1800;
191 static int cache_flush (void)
201 c_avl_iterator_t *iter;
203 time_t curtime = time (NULL);
205 iter = c_avl_get_iterator (cache_tree);
206 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
208 if ((curtime - *value) <= cache_flush_interval)
210 tmp = (char **) realloc (keys,
211 (keys_num + 1) * sizeof (char *));
215 c_avl_iterator_destroy (iter);
216 ERROR ("network plugin: cache_flush: realloc"
221 keys[keys_num] = key;
223 } /* while (c_avl_iterator_next) */
224 c_avl_iterator_destroy (iter);
226 for (i = 0; i < keys_num; i++)
228 if (c_avl_remove (cache_tree, keys[i], (void *) &key,
229 (void *) &value) != 0)
231 WARNING ("network plugin: cache_flush: c_avl_remove"
232 " (%s) failed.", keys[i]);
242 DEBUG ("network plugin: cache_flush: Removed %i %s",
243 keys_num, (keys_num == 1) ? "entry" : "entries");
244 cache_flush_last = curtime;
246 } /* int cache_flush */
248 static int cache_check (const value_list_t *vl)
251 time_t *value = NULL;
254 if (cache_tree == NULL)
257 if (format_name (key, sizeof (key), vl->host, vl->plugin,
258 vl->plugin_instance, vl->type, vl->type_instance))
261 pthread_mutex_lock (&cache_lock);
263 if (c_avl_get (cache_tree, key, (void *) &value) == 0)
265 if (*value < vl->time)
272 DEBUG ("network plugin: cache_check: *value = %i >= vl->time = %i",
273 (int) *value, (int) vl->time);
279 char *key_copy = strdup (key);
280 value = malloc (sizeof (time_t));
281 if ((key_copy != NULL) && (value != NULL))
284 c_avl_insert (cache_tree, key_copy, value);
294 if ((time (NULL) - cache_flush_last) > cache_flush_interval)
297 pthread_mutex_unlock (&cache_lock);
299 DEBUG ("network plugin: cache_check: key = %s; time = %i; retval = %i",
300 key, (int) vl->time, retval);
303 } /* int cache_check */
305 static int write_part_values (char **ret_buffer, int *ret_buffer_len,
306 const data_set_t *ds, const value_list_t *vl)
312 part_header_t pkg_ph;
313 uint16_t pkg_num_values;
314 uint8_t *pkg_values_types;
320 num_values = vl->values_len;
321 packet_len = sizeof (part_header_t) + sizeof (uint16_t)
322 + (num_values * sizeof (uint8_t))
323 + (num_values * sizeof (value_t));
325 if (*ret_buffer_len < packet_len)
328 pkg_values_types = (uint8_t *) malloc (num_values * sizeof (uint8_t));
329 if (pkg_values_types == NULL)
331 ERROR ("network plugin: write_part_values: malloc failed.");
335 pkg_values = (value_t *) malloc (num_values * sizeof (value_t));
336 if (pkg_values == NULL)
338 free (pkg_values_types);
339 ERROR ("network plugin: write_part_values: malloc failed.");
343 pkg_ph.type = htons (TYPE_VALUES);
344 pkg_ph.length = htons (packet_len);
346 pkg_num_values = htons ((uint16_t) vl->values_len);
348 for (i = 0; i < num_values; i++)
350 if (ds->ds[i].type == DS_TYPE_COUNTER)
352 pkg_values_types[i] = DS_TYPE_COUNTER;
353 pkg_values[i].counter = htonll (vl->values[i].counter);
357 pkg_values_types[i] = DS_TYPE_GAUGE;
358 pkg_values[i].gauge = htond (vl->values[i].gauge);
363 * Use `memcpy' to write everything to the buffer, because the pointer
364 * may be unaligned and some architectures, such as SPARC, can't handle
367 packet_ptr = *ret_buffer;
369 memcpy (packet_ptr + offset, &pkg_ph, sizeof (pkg_ph));
370 offset += sizeof (pkg_ph);
371 memcpy (packet_ptr + offset, &pkg_num_values, sizeof (pkg_num_values));
372 offset += sizeof (pkg_num_values);
373 memcpy (packet_ptr + offset, pkg_values_types, num_values * sizeof (uint8_t));
374 offset += num_values * sizeof (uint8_t);
375 memcpy (packet_ptr + offset, pkg_values, num_values * sizeof (value_t));
376 offset += num_values * sizeof (value_t);
378 assert (offset == packet_len);
380 *ret_buffer = packet_ptr + packet_len;
381 *ret_buffer_len -= packet_len;
383 free (pkg_values_types);
387 } /* int write_part_values */
389 static int write_part_number (char **ret_buffer, int *ret_buffer_len,
390 int type, uint64_t value)
395 part_header_t pkg_head;
400 packet_len = sizeof (pkg_head) + sizeof (pkg_value);
402 if (*ret_buffer_len < packet_len)
405 pkg_head.type = htons (type);
406 pkg_head.length = htons (packet_len);
407 pkg_value = htonll (value);
409 packet_ptr = *ret_buffer;
411 memcpy (packet_ptr + offset, &pkg_head, sizeof (pkg_head));
412 offset += sizeof (pkg_head);
413 memcpy (packet_ptr + offset, &pkg_value, sizeof (pkg_value));
414 offset += sizeof (pkg_value);
416 assert (offset == packet_len);
418 *ret_buffer = packet_ptr + packet_len;
419 *ret_buffer_len -= packet_len;
422 } /* int write_part_number */
424 static int write_part_string (char **ret_buffer, int *ret_buffer_len,
425 int type, const char *str, int str_len)
435 buffer_len = 2 * sizeof (uint16_t) + str_len + 1;
436 if (*ret_buffer_len < buffer_len)
439 pkg_type = htons (type);
440 pkg_length = htons (buffer_len);
442 buffer = *ret_buffer;
444 memcpy (buffer + offset, (void *) &pkg_type, sizeof (pkg_type));
445 offset += sizeof (pkg_type);
446 memcpy (buffer + offset, (void *) &pkg_length, sizeof (pkg_length));
447 offset += sizeof (pkg_length);
448 memcpy (buffer + offset, str, str_len);
450 memset (buffer + offset, '\0', 1);
453 assert (offset == buffer_len);
455 *ret_buffer = buffer + buffer_len;
456 *ret_buffer_len -= buffer_len;
459 } /* int write_part_string */
461 static int parse_part_values (void **ret_buffer, int *ret_buffer_len,
462 value_t **ret_values, int *ret_num_values)
464 char *buffer = *ret_buffer;
465 int buffer_len = *ret_buffer_len;
478 if (buffer_len < (15))
480 DEBUG ("network plugin: packet is too short: buffer_len = %i",
485 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
486 buffer += sizeof (tmp16);
487 pkg_type = ntohs (tmp16);
489 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
490 buffer += sizeof (tmp16);
491 pkg_length = ntohs (tmp16);
493 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
494 buffer += sizeof (tmp16);
495 pkg_numval = ntohs (tmp16);
497 assert (pkg_type == TYPE_VALUES);
499 exp_size = 3 * sizeof (uint16_t)
500 + pkg_numval * (sizeof (uint8_t) + sizeof (value_t));
501 if ((buffer_len < 0) || ((size_t) buffer_len < exp_size))
503 WARNING ("network plugin: parse_part_values: "
505 "Chunk of size %u expected, "
506 "but buffer has only %i bytes left.",
507 (unsigned int) exp_size, buffer_len);
511 if (pkg_length != exp_size)
513 WARNING ("network plugin: parse_part_values: "
514 "Length and number of values "
515 "in the packet don't match.");
519 pkg_types = (uint8_t *) malloc (pkg_numval * sizeof (uint8_t));
520 pkg_values = (value_t *) malloc (pkg_numval * sizeof (value_t));
521 if ((pkg_types == NULL) || (pkg_values == NULL))
525 ERROR ("network plugin: parse_part_values: malloc failed.");
529 memcpy ((void *) pkg_types, (void *) buffer, pkg_numval * sizeof (uint8_t));
530 buffer += pkg_numval * sizeof (uint8_t);
531 memcpy ((void *) pkg_values, (void *) buffer, pkg_numval * sizeof (value_t));
532 buffer += pkg_numval * sizeof (value_t);
534 for (i = 0; i < pkg_numval; i++)
536 if (pkg_types[i] == DS_TYPE_COUNTER)
537 pkg_values[i].counter = ntohll (pkg_values[i].counter);
538 else if (pkg_types[i] == DS_TYPE_GAUGE)
539 pkg_values[i].gauge = ntohd (pkg_values[i].gauge);
542 *ret_buffer = buffer;
543 *ret_buffer_len = buffer_len - pkg_length;
544 *ret_num_values = pkg_numval;
545 *ret_values = pkg_values;
550 } /* int parse_part_values */
552 static int parse_part_number (void **ret_buffer, int *ret_buffer_len,
555 char *buffer = *ret_buffer;
556 int buffer_len = *ret_buffer_len;
560 size_t exp_size = 2 * sizeof (uint16_t) + sizeof (uint64_t);
565 if ((buffer_len < 0) || ((size_t) buffer_len < exp_size))
567 WARNING ("network plugin: parse_part_number: "
569 "Chunk of size %u expected, "
570 "but buffer has only %i bytes left.",
571 (unsigned int) exp_size, buffer_len);
575 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
576 buffer += sizeof (tmp16);
577 pkg_type = ntohs (tmp16);
579 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
580 buffer += sizeof (tmp16);
581 pkg_length = ntohs (tmp16);
583 memcpy ((void *) &tmp64, buffer, sizeof (tmp64));
584 buffer += sizeof (tmp64);
585 *value = ntohll (tmp64);
587 *ret_buffer = buffer;
588 *ret_buffer_len = buffer_len - pkg_length;
591 } /* int parse_part_number */
593 static int parse_part_string (void **ret_buffer, int *ret_buffer_len,
594 char *output, int output_len)
596 char *buffer = *ret_buffer;
597 int buffer_len = *ret_buffer_len;
600 size_t header_size = 2 * sizeof (uint16_t);
605 if ((buffer_len < 0) || ((size_t) buffer_len < header_size))
607 WARNING ("network plugin: parse_part_string: "
609 "Chunk of at least size %u expected, "
610 "but buffer has only %i bytes left.",
611 (unsigned int) header_size, buffer_len);
615 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
616 buffer += sizeof (tmp16);
617 pkg_type = ntohs (tmp16);
619 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
620 buffer += sizeof (tmp16);
621 pkg_length = ntohs (tmp16);
623 /* Check that packet fits in the input buffer */
624 if (pkg_length > buffer_len)
626 WARNING ("network plugin: parse_part_string: "
628 "Chunk of size %hu received, "
629 "but buffer has only %i bytes left.",
630 pkg_length, buffer_len);
634 /* Check that pkg_length is in the valid range */
635 if (pkg_length <= header_size)
637 WARNING ("network plugin: parse_part_string: "
639 "Header claims this packet is only %hu "
640 "bytes long.", pkg_length);
644 /* Check that the package data fits into the output buffer.
645 * The previous if-statement ensures that:
646 * `pkg_length > header_size' */
648 || ((size_t) output_len < ((size_t) pkg_length - header_size)))
650 WARNING ("network plugin: parse_part_string: "
651 "Output buffer too small.");
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
662 if (output[output_len - 1] != 0)
664 WARNING ("network plugin: parse_part_string: "
665 "Received string does not end "
666 "with a NULL-byte.");
670 *ret_buffer = buffer;
671 *ret_buffer_len = buffer_len - pkg_length;
674 } /* int parse_part_string */
676 static int parse_packet (void *buffer, int buffer_len)
680 value_list_t vl = VALUE_LIST_INIT;
683 DEBUG ("network plugin: parse_packet: buffer = %p; buffer_len = %i;",
686 memset (&vl, '\0', sizeof (vl));
687 memset (&n, '\0', sizeof (n));
690 while ((status == 0) && (0 < buffer_len)
691 && ((unsigned int) buffer_len > sizeof (part_header_t)))
696 memcpy ((void *) &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)
708 /* Ensure that this loop terminates eventually */
709 if (pkg_length < (2 * sizeof (uint16_t)))
712 if (pkg_type == TYPE_VALUES)
714 status = parse_part_values (&buffer, &buffer_len,
715 &vl.values, &vl.values_len);
721 && (strlen (vl.host) > 0)
722 && (strlen (vl.plugin) > 0)
723 && (strlen (vl.type) > 0)
724 && (cache_check (&vl) == 0))
726 plugin_dispatch_values (&vl);
730 DEBUG ("network plugin: parse_packet:"
731 " NOT dispatching values");
736 else if (pkg_type == TYPE_TIME)
739 status = parse_part_number (&buffer, &buffer_len,
743 vl.time = (time_t) tmp;
744 n.time = (time_t) tmp;
747 else if (pkg_type == TYPE_INTERVAL)
750 status = parse_part_number (&buffer, &buffer_len,
753 vl.interval = (int) tmp;
755 else if (pkg_type == TYPE_HOST)
757 status = parse_part_string (&buffer, &buffer_len,
758 vl.host, sizeof (vl.host));
760 sstrncpy (n.host, vl.host, sizeof (n.host));
762 else if (pkg_type == TYPE_PLUGIN)
764 status = parse_part_string (&buffer, &buffer_len,
765 vl.plugin, sizeof (vl.plugin));
767 sstrncpy (n.plugin, vl.plugin,
770 else if (pkg_type == TYPE_PLUGIN_INSTANCE)
772 status = parse_part_string (&buffer, &buffer_len,
774 sizeof (vl.plugin_instance));
776 sstrncpy (n.plugin_instance,
778 sizeof (n.plugin_instance));
780 else if (pkg_type == TYPE_TYPE)
782 status = parse_part_string (&buffer, &buffer_len,
783 vl.type, sizeof (vl.type));
785 sstrncpy (n.type, vl.type, sizeof (n.type));
787 else if (pkg_type == TYPE_TYPE_INSTANCE)
789 status = parse_part_string (&buffer, &buffer_len,
791 sizeof (vl.type_instance));
793 sstrncpy (n.type_instance, vl.type_instance,
794 sizeof (n.type_instance));
796 else if (pkg_type == TYPE_MESSAGE)
798 status = parse_part_string (&buffer, &buffer_len,
799 n.message, sizeof (n.message));
805 else if ((n.severity != NOTIF_FAILURE)
806 && (n.severity != NOTIF_WARNING)
807 && (n.severity != NOTIF_OKAY))
809 INFO ("network plugin: "
810 "Ignoring notification with "
811 "unknown severity %i.",
814 else if (n.time <= 0)
816 INFO ("network plugin: "
817 "Ignoring notification with "
820 else if (strlen (n.message) <= 0)
822 INFO ("network plugin: "
823 "Ignoring notification with "
824 "an empty message.");
828 plugin_dispatch_notification (&n);
831 else if (pkg_type == TYPE_SEVERITY)
834 status = parse_part_number (&buffer, &buffer_len,
837 n.severity = (int) tmp;
841 DEBUG ("network plugin: parse_packet: Unknown part"
842 " type: 0x%04hx", pkg_type);
843 buffer = ((char *) buffer) + pkg_length;
845 } /* while (buffer_len > sizeof (part_header_t)) */
848 } /* int parse_packet */
850 static void free_sockent (sockent_t *se)
860 } /* void free_sockent */
863 * int network_set_ttl
865 * Set the `IP_MULTICAST_TTL', `IP_TTL', `IPV6_MULTICAST_HOPS' or
866 * `IPV6_UNICAST_HOPS', depending on which option is applicable.
868 * The `struct addrinfo' is used to destinguish between unicast and multicast
871 static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
873 if ((network_config_ttl < 1) || (network_config_ttl > 255))
876 DEBUG ("ttl = %i", network_config_ttl);
878 if (ai->ai_family == AF_INET)
880 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
883 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
884 optname = IP_MULTICAST_TTL;
888 if (setsockopt (se->fd, IPPROTO_IP, optname,
890 sizeof (network_config_ttl)) == -1)
893 ERROR ("setsockopt: %s",
894 sstrerror (errno, errbuf, sizeof (errbuf)));
898 else if (ai->ai_family == AF_INET6)
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;
904 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
905 optname = IPV6_MULTICAST_HOPS;
907 optname = IPV6_UNICAST_HOPS;
909 if (setsockopt (se->fd, IPPROTO_IPV6, optname,
911 sizeof (network_config_ttl)) == -1)
914 ERROR ("setsockopt: %s",
915 sstrerror (errno, errbuf,
922 } /* int network_set_ttl */
924 static int network_bind_socket (const sockent_t *se, const struct addrinfo *ai)
929 /* allow multiple sockets to use the same PORT number */
930 if (setsockopt(se->fd, SOL_SOCKET, SO_REUSEADDR,
931 &yes, sizeof(yes)) == -1) {
933 ERROR ("setsockopt: %s",
934 sstrerror (errno, errbuf, sizeof (errbuf)));
938 DEBUG ("fd = %i; calling `bind'", se->fd);
940 if (bind (se->fd, ai->ai_addr, ai->ai_addrlen) == -1)
944 sstrerror (errno, errbuf, sizeof (errbuf)));
948 if (ai->ai_family == AF_INET)
950 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
951 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
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)
964 ERROR ("setsockopt: %s",
965 sstrerror (errno, errbuf,
970 if (setsockopt (se->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
971 &mreq, sizeof (mreq)) == -1)
974 ERROR ("setsockopt: %s",
975 sstrerror (errno, errbuf,
981 else if (ai->ai_family == AF_INET6)
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))
987 struct ipv6_mreq mreq;
989 DEBUG ("fd = %i; IPv6 multicast address found", se->fd);
991 memcpy (&mreq.ipv6mr_multiaddr,
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)
1010 ERROR ("setsockopt: %s",
1011 sstrerror (errno, errbuf,
1016 if (setsockopt (se->fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
1017 &mreq, sizeof (mreq)) == -1)
1020 ERROR ("setsockopt: %s",
1021 sstrerror (errno, errbuf,
1029 } /* int network_bind_socket */
1031 static sockent_t *network_create_socket (const char *node,
1032 const char *service,
1035 struct addrinfo ai_hints;
1036 struct addrinfo *ai_list, *ai_ptr;
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;
1047 ai_hints.ai_flags |= AI_PASSIVE;
1049 #ifdef AI_ADDRCONFIG
1050 ai_hints.ai_flags |= AI_ADDRCONFIG;
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);
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));
1069 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
1073 if ((se = (sockent_t *) malloc (sizeof (sockent_t))) == NULL)
1076 ERROR ("malloc: %s",
1077 sstrerror (errno, errbuf,
1082 if ((se->addr = (struct sockaddr_storage *) malloc (sizeof (struct sockaddr_storage))) == NULL)
1085 ERROR ("malloc: %s",
1086 sstrerror (errno, errbuf,
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);
1105 ERROR ("socket: %s",
1106 sstrerror (errno, errbuf,
1115 if (network_bind_socket (se, ai_ptr) != 0)
1123 else /* listen == 0 */
1125 network_set_ttl (se, ai_ptr);
1128 if (se_tail == NULL)
1139 /* We don't open more than one write-socket per node/service pair.. */
1144 freeaddrinfo (ai_list);
1147 } /* sockent_t *network_create_socket */
1149 static sockent_t *network_create_default_socket (int listen)
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))
1166 while (se_tail->next != NULL)
1167 se_tail = se_tail->next;
1170 se_ptr = network_create_socket (NET_DEFAULT_V4_ADDR, NET_DEFAULT_PORT, listen);
1172 if (se_tail == NULL)
1175 se_tail->next = se_ptr;
1177 } /* sockent_t *network_create_default_socket */
1179 static int network_add_listen_socket (const char *node, const char *service)
1185 if (service == NULL)
1186 service = NET_DEFAULT_PORT;
1189 se = network_create_default_socket (1 /* listen == true */);
1191 se = network_create_socket (node, service, 1 /* listen == true */);
1196 for (se_ptr = se; se_ptr != NULL; se_ptr = se_ptr->next)
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)
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++;
1213 } /* int network_add_listen_socket */
1215 static int network_add_sending_socket (const char *node, const char *service)
1220 if (service == NULL)
1221 service = NET_DEFAULT_PORT;
1224 se = network_create_default_socket (0 /* listen == false */);
1226 se = network_create_socket (node, service, 0 /* listen == false */);
1231 if (sending_sockets == NULL)
1233 sending_sockets = se;
1237 for (se_ptr = sending_sockets; se_ptr->next != NULL; se_ptr = se_ptr->next)
1242 } /* int network_get_listen_socket */
1244 static void *dispatch_thread (void __attribute__((unused)) *arg)
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;
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. */
1267 parse_packet (ent->data, ent->data_len);
1273 } /* void *receive_thread */
1275 static int network_receive (void)
1277 char buffer[BUFF_SIZE];
1283 receive_list_entry_t *private_list_head;
1284 receive_list_entry_t *private_list_tail;
1286 if (listen_sockets_num == 0)
1287 network_add_listen_socket (NULL, NULL);
1289 if (listen_sockets_num == 0)
1291 ERROR ("network: Failed to open a listening socket.");
1295 private_list_head = NULL;
1296 private_list_tail = NULL;
1298 while (listen_loop == 0)
1300 status = poll (listen_sockets, listen_sockets_num, -1);
1307 ERROR ("poll failed: %s",
1308 sstrerror (errno, errbuf, sizeof (errbuf)));
1312 for (i = 0; (i < listen_sockets_num) && (status > 0); i++)
1314 receive_list_entry_t *ent;
1316 if ((listen_sockets[i].revents & (POLLIN | POLLPRI)) == 0)
1320 buffer_len = recv (listen_sockets[i].fd,
1321 buffer, sizeof (buffer),
1326 ERROR ("recv failed: %s",
1327 sstrerror (errno, errbuf,
1332 ent = malloc (sizeof (receive_list_entry_t));
1335 ERROR ("network plugin: malloc failed.");
1338 memset (ent, 0, sizeof (receive_list_entry_t));
1341 /* Hopefully this be optimized out by the compiler. It
1342 * might help prevent stupid bugs in the future though.
1344 assert (sizeof (ent->data) == sizeof (buffer));
1346 memcpy (ent->data, buffer, buffer_len);
1347 ent->data_len = buffer_len;
1349 if (private_list_head == NULL)
1350 private_list_head = ent;
1352 private_list_tail->next = ent;
1353 private_list_tail = ent;
1355 /* Do not block here. Blocking here has led to
1356 * insufficient performance in the past. */
1357 if (pthread_mutex_trylock (&receive_list_lock) == 0)
1359 if (receive_list_head == NULL)
1360 receive_list_head = private_list_head;
1362 receive_list_tail->next = private_list_head;
1363 receive_list_tail = private_list_tail;
1365 private_list_head = NULL;
1366 private_list_tail = NULL;
1368 pthread_cond_signal (&receive_list_cond);
1369 pthread_mutex_unlock (&receive_list_lock);
1371 } /* for (listen_sockets) */
1372 } /* while (listen_loop == 0) */
1374 /* Make sure everything is dispatched before exiting. */
1375 if (private_list_head != NULL)
1377 pthread_mutex_lock (&receive_list_lock);
1379 if (receive_list_head == NULL)
1380 receive_list_head = private_list_head;
1382 receive_list_tail->next = private_list_head;
1383 receive_list_tail = private_list_tail;
1385 private_list_head = NULL;
1386 private_list_tail = NULL;
1388 pthread_cond_signal (&receive_list_cond);
1389 pthread_mutex_unlock (&receive_list_lock);
1395 static void *receive_thread (void __attribute__((unused)) *arg)
1397 return (network_receive () ? (void *) 1 : (void *) 0);
1398 } /* void *receive_thread */
1400 static void network_send_buffer (const char *buffer, int buffer_len)
1405 DEBUG ("network plugin: network_send_buffer: buffer_len = %i", buffer_len);
1407 for (se = sending_sockets; se != NULL; se = se->next)
1411 status = sendto (se->fd, buffer, buffer_len, 0 /* no flags */,
1412 (struct sockaddr *) se->addr, se->addrlen);
1418 ERROR ("network plugin: sendto failed: %s",
1419 sstrerror (errno, errbuf,
1426 } /* for (sending_sockets) */
1427 } /* void network_send_buffer */
1429 static int add_to_buffer (char *buffer, int buffer_size,
1430 value_list_t *vl_def,
1431 const data_set_t *ds, const value_list_t *vl)
1433 char *buffer_orig = buffer;
1435 if (strcmp (vl_def->host, vl->host) != 0)
1437 if (write_part_string (&buffer, &buffer_size, TYPE_HOST,
1438 vl->host, strlen (vl->host)) != 0)
1440 sstrncpy (vl_def->host, vl->host, sizeof (vl_def->host));
1443 if (vl_def->time != vl->time)
1445 if (write_part_number (&buffer, &buffer_size, TYPE_TIME,
1446 (uint64_t) vl->time))
1448 vl_def->time = vl->time;
1451 if (vl_def->interval != vl->interval)
1453 if (write_part_number (&buffer, &buffer_size, TYPE_INTERVAL,
1454 (uint64_t) vl->interval))
1456 vl_def->interval = vl->interval;
1459 if (strcmp (vl_def->plugin, vl->plugin) != 0)
1461 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN,
1462 vl->plugin, strlen (vl->plugin)) != 0)
1464 sstrncpy (vl_def->plugin, vl->plugin, sizeof (vl_def->plugin));
1467 if (strcmp (vl_def->plugin_instance, vl->plugin_instance) != 0)
1469 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
1470 vl->plugin_instance,
1471 strlen (vl->plugin_instance)) != 0)
1473 sstrncpy (vl_def->plugin_instance, vl->plugin_instance, sizeof (vl_def->plugin_instance));
1476 if (strcmp (vl_def->type, vl->type) != 0)
1478 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
1479 vl->type, strlen (vl->type)) != 0)
1481 sstrncpy (vl_def->type, ds->type, sizeof (vl_def->type));
1484 if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
1486 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE_INSTANCE,
1488 strlen (vl->type_instance)) != 0)
1490 sstrncpy (vl_def->type_instance, vl->type_instance, sizeof (vl_def->type_instance));
1493 if (write_part_values (&buffer, &buffer_size, ds, vl) != 0)
1496 return (buffer - buffer_orig);
1497 } /* int add_to_buffer */
1499 static void flush_buffer (void)
1501 DEBUG ("network plugin: flush_buffer: send_buffer_fill = %i",
1504 network_send_buffer (send_buffer, send_buffer_fill);
1505 send_buffer_ptr = send_buffer;
1506 send_buffer_fill = 0;
1507 memset (&send_buffer_vl, 0, sizeof (send_buffer_vl));
1510 static int network_write (const data_set_t *ds, const value_list_t *vl)
1514 /* If the value is already in the cache, we have received it via the
1515 * network. We write it again if forwarding is activated. It's then in
1516 * the cache and should we receive it again we will ignore it. */
1517 status = cache_check (vl);
1518 if ((network_config_forward == 0)
1522 pthread_mutex_lock (&send_buffer_lock);
1524 status = add_to_buffer (send_buffer_ptr,
1525 sizeof (send_buffer) - send_buffer_fill,
1530 /* status == bytes added to the buffer */
1531 send_buffer_fill += status;
1532 send_buffer_ptr += status;
1538 status = add_to_buffer (send_buffer_ptr,
1539 sizeof (send_buffer) - send_buffer_fill,
1545 send_buffer_fill += status;
1546 send_buffer_ptr += status;
1552 ERROR ("network plugin: Unable to append to the "
1553 "buffer for some weird reason");
1555 else if ((sizeof (send_buffer) - send_buffer_fill) < 15)
1560 pthread_mutex_unlock (&send_buffer_lock);
1562 return ((status < 0) ? -1 : 0);
1563 } /* int network_write */
1565 static int network_config (const char *key, const char *val)
1573 if ((strcasecmp ("Listen", key) == 0)
1574 || (strcasecmp ("Server", key) == 0))
1576 char *val_cpy = strdup (val);
1577 if (val_cpy == NULL)
1580 service = NET_DEFAULT_PORT;
1581 fields_num = strsplit (val_cpy, fields, 3);
1582 if ((fields_num != 1)
1583 && (fields_num != 2))
1588 else if (fields_num == 2)
1590 if ((service = strchr (fields[1], '.')) != NULL)
1592 service = fields[1];
1596 if (strcasecmp ("Listen", key) == 0)
1597 network_add_listen_socket (node, service);
1599 network_add_sending_socket (node, service);
1603 else if (strcasecmp ("TimeToLive", key) == 0)
1605 int tmp = atoi (val);
1606 if ((tmp > 0) && (tmp < 256))
1607 network_config_ttl = tmp;
1611 else if (strcasecmp ("Forward", key) == 0)
1613 if ((strcasecmp ("true", val) == 0)
1614 || (strcasecmp ("yes", val) == 0)
1615 || (strcasecmp ("on", val) == 0))
1616 network_config_forward = 1;
1618 network_config_forward = 0;
1620 else if (strcasecmp ("CacheFlush", key) == 0)
1622 int tmp = atoi (val);
1624 cache_flush_interval = tmp;
1632 } /* int network_config */
1634 static int network_notification (const notification_t *n)
1636 char buffer[BUFF_SIZE];
1637 char *buffer_ptr = buffer;
1638 int buffer_free = sizeof (buffer);
1641 memset (buffer, '\0', sizeof (buffer));
1644 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_TIME,
1645 (uint64_t) n->time);
1649 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_SEVERITY,
1650 (uint64_t) n->severity);
1654 if (strlen (n->host) > 0)
1656 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_HOST,
1657 n->host, strlen (n->host));
1662 if (strlen (n->plugin) > 0)
1664 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_PLUGIN,
1665 n->plugin, strlen (n->plugin));
1670 if (strlen (n->plugin_instance) > 0)
1672 status = write_part_string (&buffer_ptr, &buffer_free,
1673 TYPE_PLUGIN_INSTANCE,
1674 n->plugin_instance, strlen (n->plugin_instance));
1679 if (strlen (n->type) > 0)
1681 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE,
1682 n->type, strlen (n->type));
1687 if (strlen (n->type_instance) > 0)
1689 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE_INSTANCE,
1690 n->type_instance, strlen (n->type_instance));
1695 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_MESSAGE,
1696 n->message, strlen (n->message));
1700 network_send_buffer (buffer, sizeof (buffer) - buffer_free);
1703 } /* int network_notification */
1705 static int network_shutdown (void)
1709 /* Kill the listening thread */
1710 if (receive_thread_id != (pthread_t) 0)
1712 pthread_kill (receive_thread_id, SIGTERM);
1713 pthread_join (receive_thread_id, NULL /* no return value */);
1714 receive_thread_id = (pthread_t) 0;
1717 /* Shutdown the dispatching thread */
1718 if (dispatch_thread_id != (pthread_t) 0)
1719 pthread_cond_broadcast (&receive_list_cond);
1721 if (send_buffer_fill > 0)
1724 if (cache_tree != NULL)
1729 while (c_avl_pick (cache_tree, &key, &value) == 0)
1734 c_avl_destroy (cache_tree);
1738 /* TODO: Close `sending_sockets' */
1740 plugin_unregister_config ("network");
1741 plugin_unregister_init ("network");
1742 plugin_unregister_write ("network");
1743 plugin_unregister_shutdown ("network");
1745 /* Let the init function do it's move again ;) */
1746 cache_flush_last = 0;
1749 } /* int network_shutdown */
1751 static int network_init (void)
1753 /* Check if we were already initialized. If so, just return - there's
1754 * nothing more to do (for now, that is). */
1755 if (cache_flush_last != 0)
1758 plugin_register_shutdown ("network", network_shutdown);
1760 send_buffer_ptr = send_buffer;
1761 send_buffer_fill = 0;
1762 memset (&send_buffer_vl, 0, sizeof (send_buffer_vl));
1764 cache_tree = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1765 cache_flush_last = time (NULL);
1767 /* setup socket(s) and so on */
1768 if (sending_sockets != NULL)
1770 plugin_register_write ("network", network_write);
1771 plugin_register_notification ("network", network_notification);
1774 if ((listen_sockets_num != 0) && (receive_thread_id == 0))
1778 status = pthread_create (&dispatch_thread_id,
1779 NULL /* no attributes */,
1781 NULL /* no argument */);
1785 ERROR ("network: pthread_create failed: %s",
1786 sstrerror (errno, errbuf,
1790 status = pthread_create (&receive_thread_id,
1791 NULL /* no attributes */,
1793 NULL /* no argument */);
1797 ERROR ("network: pthread_create failed: %s",
1798 sstrerror (errno, errbuf,
1803 } /* int network_init */
1806 * The flush option of the network plugin cannot flush individual identifiers.
1807 * All the values are added to a buffer and sent when the buffer is full, the
1808 * requested value may or may not be in there, it's not worth finding out. We
1809 * just send the buffer if `flush' is called - if the requested value was in
1810 * there, good. If not, well, then there is nothing to flush.. -octo
1812 static int network_flush (int timeout,
1813 const char __attribute__((unused)) *identifier)
1815 pthread_mutex_lock (&send_buffer_lock);
1817 if (((time (NULL) - cache_flush_last) >= timeout)
1818 && (send_buffer_fill > 0))
1823 pthread_mutex_unlock (&send_buffer_lock);
1826 } /* int network_flush */
1828 void module_register (void)
1830 plugin_register_config ("network", network_config,
1831 config_keys, config_keys_num);
1832 plugin_register_init ("network", network_init);
1833 plugin_register_flush ("network", network_flush);
1834 } /* void module_register */