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 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;
187 static int cache_flush_interval = 1800;
192 static int cache_flush (void)
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)
209 if ((curtime - *value) <= cache_flush_interval)
211 tmp = (char **) realloc (keys,
212 (keys_num + 1) * sizeof (char *));
216 c_avl_iterator_destroy (iter);
217 ERROR ("network plugin: cache_flush: realloc"
222 keys[keys_num] = key;
224 } /* while (c_avl_iterator_next) */
225 c_avl_iterator_destroy (iter);
227 for (i = 0; i < keys_num; i++)
229 if (c_avl_remove (cache_tree, keys[i], (void *) &key,
230 (void *) &value) != 0)
232 WARNING ("network plugin: cache_flush: c_avl_remove"
233 " (%s) failed.", keys[i]);
243 DEBUG ("network plugin: cache_flush: Removed %i %s",
244 keys_num, (keys_num == 1) ? "entry" : "entries");
245 cache_flush_last = curtime;
247 } /* int cache_flush */
249 static int cache_check (const char *type, const value_list_t *vl)
252 time_t *value = NULL;
255 if (cache_tree == NULL)
258 if (format_name (key, sizeof (key), vl->host, vl->plugin,
259 vl->plugin_instance, type, vl->type_instance))
262 pthread_mutex_lock (&cache_lock);
264 if (c_avl_get (cache_tree, key, (void *) &value) == 0)
266 if (*value < vl->time)
273 DEBUG ("network plugin: cache_check: *value = %i >= vl->time = %i",
274 (int) *value, (int) vl->time);
280 char *key_copy = strdup (key);
281 value = malloc (sizeof (time_t));
282 if ((key_copy != NULL) && (value != NULL))
285 c_avl_insert (cache_tree, key_copy, value);
295 if ((time (NULL) - cache_flush_last) > cache_flush_interval)
298 pthread_mutex_unlock (&cache_lock);
300 DEBUG ("network plugin: cache_check: key = %s; time = %i; retval = %i",
301 key, (int) vl->time, 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)
312 i = 6 + (9 * vl->values_len);
313 if (*ret_buffer_len < i)
315 *ret_buffer_len -= i;
317 pv.head = (part_header_t *) *ret_buffer;
318 pv.num_values = (uint16_t *) (pv.head + 1);
319 pv.values_types = (uint8_t *) (pv.num_values + 1);
320 pv.values = (value_t *) (pv.values_types + vl->values_len);
321 *ret_buffer = (void *) (pv.values + vl->values_len);
323 pv.head->type = htons (TYPE_VALUES);
324 pv.head->length = htons (6 + (9 * vl->values_len));
325 *pv.num_values = htons ((uint16_t) vl->values_len);
327 for (i = 0; i < vl->values_len; i++)
329 if (ds->ds[i].type == DS_TYPE_COUNTER)
331 pv.values_types[i] = DS_TYPE_COUNTER;
332 pv.values[i].counter = htonll (vl->values[i].counter);
336 pv.values_types[i] = DS_TYPE_GAUGE;
337 pv.values[i].gauge = vl->values[i].gauge;
342 } /* int write_part_values */
344 static int write_part_number (char **ret_buffer, int *ret_buffer_len,
345 int type, uint64_t value)
349 if (*ret_buffer_len < 12)
352 pn.head = (part_header_t *) *ret_buffer;
353 pn.value = (uint64_t *) (pn.head + 1);
355 pn.head->type = htons (type);
356 pn.head->length = htons (12);
357 *pn.value = htonll (value);
359 *ret_buffer = (char *) (pn.value + 1);
360 *ret_buffer_len -= 12;
363 } /* int write_part_number */
365 static int write_part_string (char **ret_buffer, int *ret_buffer_len,
366 int type, const char *str, int str_len)
371 len = 4 + str_len + 1;
372 if (*ret_buffer_len < len)
374 *ret_buffer_len -= len;
376 ps.head = (part_header_t *) *ret_buffer;
377 ps.value = (char *) (ps.head + 1);
379 ps.head->type = htons ((uint16_t) type);
380 ps.head->length = htons ((uint16_t) str_len + 5);
382 memcpy (ps.value, str, str_len);
383 ps.value[str_len] = '\0';
384 *ret_buffer = (void *) (ps.value + (str_len + 1));
387 } /* int write_part_string */
389 static int parse_part_values (void **ret_buffer, int *ret_buffer_len,
390 value_t **ret_values, int *ret_num_values)
392 char *buffer = *ret_buffer;
393 int buffer_len = *ret_buffer_len;
401 if (buffer_len < (15))
403 DEBUG ("network plugin: packet is too short: buffer_len = %i",
408 pv.head = (part_header_t *) buffer;
409 h_length = ntohs (pv.head->length);
410 h_type = ntohs (pv.head->type);
412 assert (h_type == TYPE_VALUES);
414 pv.num_values = (uint16_t *) (pv.head + 1);
415 h_num = ntohs (*pv.num_values);
417 if (h_num != ((h_length - 6) / 9))
419 DEBUG ("`length' and `num of values' don't match");
423 pv.values_types = (uint8_t *) (pv.num_values + 1);
424 pv.values = (value_t *) (pv.values_types + h_num);
426 for (i = 0; i < h_num; i++)
427 if (pv.values_types[i] == DS_TYPE_COUNTER)
428 pv.values[i].counter = ntohll (pv.values[i].counter);
430 *ret_buffer = (void *) (pv.values + h_num);
431 *ret_buffer_len = buffer_len - h_length;
432 *ret_num_values = h_num;
433 *ret_values = pv.values;
436 } /* int parse_part_values */
438 static int parse_part_number (void **ret_buffer, int *ret_buffer_len,
444 pn.head = (part_header_t *) *ret_buffer;
445 pn.value = (uint64_t *) (pn.head + 1);
447 len = ntohs (pn.head->length);
450 if (len > *ret_buffer_len)
452 *value = ntohll (*pn.value);
454 *ret_buffer = (void *) (pn.value + 1);
455 *ret_buffer_len -= len;
458 } /* int parse_part_number */
460 static int parse_part_string (void **ret_buffer, int *ret_buffer_len,
461 char *output, int output_len)
463 char *buffer = *ret_buffer;
464 int buffer_len = *ret_buffer_len;
470 DEBUG ("network plugin: parse_part_string: ret_buffer = %p;"
471 " ret_buffer_len = %i; output = %p; output_len = %i;",
472 *ret_buffer, *ret_buffer_len,
473 (void *) output, output_len);
475 ps.head = (part_header_t *) buffer;
477 h_length = ntohs (ps.head->length);
478 h_type = ntohs (ps.head->type);
480 DEBUG ("network plugin: parse_part_string: length = %hu; type = %hu;",
483 if (buffer_len < h_length)
485 DEBUG ("packet is too short");
488 assert ((h_type == TYPE_HOST)
489 || (h_type == TYPE_PLUGIN)
490 || (h_type == TYPE_PLUGIN_INSTANCE)
491 || (h_type == TYPE_TYPE)
492 || (h_type == TYPE_TYPE_INSTANCE)
493 || (h_type == TYPE_MESSAGE));
495 ps.value = buffer + 4;
496 if (ps.value[h_length - 5] != '\0')
498 DEBUG ("String does not end with a nullbyte");
502 if (output_len < (h_length - 4))
504 DEBUG ("output buffer is too small");
507 strcpy (output, ps.value);
509 DEBUG ("network plugin: parse_part_string: output = %s", output);
511 *ret_buffer = (void *) (buffer + h_length);
512 *ret_buffer_len = buffer_len - h_length;
515 } /* int parse_part_string */
517 static int parse_packet (void *buffer, int buffer_len)
519 part_header_t *header;
522 value_list_t vl = VALUE_LIST_INIT;
523 char type[DATA_MAX_NAME_LEN];
526 DEBUG ("network plugin: parse_packet: buffer = %p; buffer_len = %i;",
529 memset (&vl, '\0', sizeof (vl));
530 memset (&type, '\0', sizeof (type));
531 memset (&n, '\0', sizeof (n));
534 while ((status == 0) && (0 < buffer_len)
535 && ((unsigned int) buffer_len > sizeof (part_header_t)))
537 header = (part_header_t *) buffer;
539 if (ntohs (header->length) > buffer_len)
541 /* Assure that this loop terminates eventually */
542 if (ntohs (header->length) < 4)
545 if (ntohs (header->type) == TYPE_VALUES)
547 status = parse_part_values (&buffer, &buffer_len,
548 &vl.values, &vl.values_len);
552 DEBUG ("parse_part_values failed.");
557 && (strlen (vl.host) > 0)
558 && (strlen (vl.plugin) > 0)
559 && (strlen (type) > 0)
560 && (cache_check (type, &vl) == 0))
562 DEBUG ("network plugin: parse_packet:"
563 " dispatching values");
564 plugin_dispatch_values (type, &vl);
568 DEBUG ("network plugin: parse_packet:"
569 " NOT dispatching values");
572 else if (ntohs (header->type) == TYPE_TIME)
575 status = parse_part_number (&buffer, &buffer_len, &tmp);
578 vl.time = (time_t) tmp;
579 n.time = (time_t) tmp;
582 else if (ntohs (header->type) == TYPE_INTERVAL)
585 status = parse_part_number (&buffer, &buffer_len, &tmp);
587 vl.interval = (int) tmp;
589 else if (ntohs (header->type) == TYPE_HOST)
591 status = parse_part_string (&buffer, &buffer_len,
592 vl.host, sizeof (vl.host));
593 strncpy (n.host, vl.host, sizeof (n.host));
594 n.host[sizeof (n.host) - 1] = '\0';
595 DEBUG ("network plugin: parse_packet: vl.host = %s",
598 else if (ntohs (header->type) == TYPE_PLUGIN)
600 status = parse_part_string (&buffer, &buffer_len,
601 vl.plugin, sizeof (vl.plugin));
602 strncpy (n.plugin, vl.plugin, sizeof (n.plugin));
603 n.plugin[sizeof (n.plugin) - 1] = '\0';
604 DEBUG ("network plugin: parse_packet: vl.plugin = %s",
607 else if (ntohs (header->type) == TYPE_PLUGIN_INSTANCE)
609 status = parse_part_string (&buffer, &buffer_len,
611 sizeof (vl.plugin_instance));
612 strncpy (n.plugin_instance, vl.plugin_instance,
613 sizeof (n.plugin_instance));
614 n.plugin_instance[sizeof (n.plugin_instance) - 1] = '\0';
615 DEBUG ("network plugin: parse_packet: "
616 "vl.plugin_instance = %s",
619 else if (ntohs (header->type) == TYPE_TYPE)
621 status = parse_part_string (&buffer, &buffer_len,
622 type, sizeof (type));
623 strncpy (n.type, type, sizeof (n.type));
624 n.type[sizeof (n.type) - 1] = '\0';
625 DEBUG ("network plugin: parse_packet: type = %s",
628 else if (ntohs (header->type) == TYPE_TYPE_INSTANCE)
630 status = parse_part_string (&buffer, &buffer_len,
632 sizeof (vl.type_instance));
633 strncpy (n.type_instance, vl.type_instance,
634 sizeof (n.type_instance));
635 n.type_instance[sizeof (n.type_instance) - 1] = '\0';
636 DEBUG ("network plugin: parse_packet: "
637 "vl.type_instance = %s",
640 else if (ntohs (header->type) == TYPE_MESSAGE)
642 status = parse_part_string (&buffer, &buffer_len,
643 n.message, sizeof (n.message));
644 DEBUG ("network plugin: parse_packet: n.message = %s",
647 if ((n.severity != NOTIF_FAILURE)
648 && (n.severity != NOTIF_WARNING)
649 && (n.severity != NOTIF_OKAY))
651 INFO ("network plugin: "
652 "Ignoring notification with "
653 "unknown severity %s.",
656 else if (n.time <= 0)
658 INFO ("network plugin: "
659 "Ignoring notification with "
662 else if (strlen (n.message) <= 0)
664 INFO ("network plugin: "
665 "Ignoring notification with "
666 "an empty message.");
671 * TODO: Let this do a separate thread so that
672 * no packets are lost if this takes too long.
674 plugin_dispatch_notification (&n);
677 else if (ntohs (header->type) == TYPE_SEVERITY)
680 status = parse_part_number (&buffer, &buffer_len, &tmp);
682 n.severity = (int) tmp;
686 DEBUG ("network plugin: parse_packet: Unknown part"
687 " type: 0x%0hx", ntohs (header->type));
688 buffer = ((char *) buffer) + ntohs (header->length);
690 } /* while (buffer_len > sizeof (part_header_t)) */
693 } /* int parse_packet */
695 static void free_sockent (sockent_t *se)
705 } /* void free_sockent */
708 * int network_set_ttl
710 * Set the `IP_MULTICAST_TTL', `IP_TTL', `IPV6_MULTICAST_HOPS' or
711 * `IPV6_UNICAST_HOPS', depending on which option is applicable.
713 * The `struct addrinfo' is used to destinguish between unicast and multicast
716 static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
718 if ((network_config_ttl < 1) || (network_config_ttl > 255))
721 DEBUG ("ttl = %i", network_config_ttl);
723 if (ai->ai_family == AF_INET)
725 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
728 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
729 optname = IP_MULTICAST_TTL;
733 if (setsockopt (se->fd, IPPROTO_IP, optname,
735 sizeof (network_config_ttl)) == -1)
738 ERROR ("setsockopt: %s",
739 sstrerror (errno, errbuf, sizeof (errbuf)));
743 else if (ai->ai_family == AF_INET6)
745 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
746 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
749 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
750 optname = IPV6_MULTICAST_HOPS;
752 optname = IPV6_UNICAST_HOPS;
754 if (setsockopt (se->fd, IPPROTO_IPV6, optname,
756 sizeof (network_config_ttl)) == -1)
759 ERROR ("setsockopt: %s",
760 sstrerror (errno, errbuf,
767 } /* int network_set_ttl */
769 static int network_bind_socket (const sockent_t *se, const struct addrinfo *ai)
774 /* allow multiple sockets to use the same PORT number */
775 if (setsockopt(se->fd, SOL_SOCKET, SO_REUSEADDR,
776 &yes, sizeof(yes)) == -1) {
778 ERROR ("setsockopt: %s",
779 sstrerror (errno, errbuf, sizeof (errbuf)));
783 DEBUG ("fd = %i; calling `bind'", se->fd);
785 if (bind (se->fd, ai->ai_addr, ai->ai_addrlen) == -1)
789 sstrerror (errno, errbuf, sizeof (errbuf)));
793 if (ai->ai_family == AF_INET)
795 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
796 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
800 DEBUG ("fd = %i; IPv4 multicast address found", se->fd);
802 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
803 mreq.imr_interface.s_addr = htonl (INADDR_ANY);
805 if (setsockopt (se->fd, IPPROTO_IP, IP_MULTICAST_LOOP,
806 &loop, sizeof (loop)) == -1)
809 ERROR ("setsockopt: %s",
810 sstrerror (errno, errbuf,
815 if (setsockopt (se->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
816 &mreq, sizeof (mreq)) == -1)
819 ERROR ("setsockopt: %s",
820 sstrerror (errno, errbuf,
826 else if (ai->ai_family == AF_INET6)
828 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
829 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
830 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
832 struct ipv6_mreq mreq;
834 DEBUG ("fd = %i; IPv6 multicast address found", se->fd);
836 memcpy (&mreq.ipv6mr_multiaddr,
838 sizeof (addr->sin6_addr));
840 /* http://developer.apple.com/documentation/Darwin/Reference/ManPages/man4/ip6.4.html
841 * ipv6mr_interface may be set to zeroes to
842 * choose the default multicast interface or to
843 * the index of a particular multicast-capable
844 * interface if the host is multihomed.
845 * Membership is associ-associated with a
846 * single interface; programs running on
847 * multihomed hosts may need to join the same
848 * group on more than one interface.*/
849 mreq.ipv6mr_interface = 0;
851 if (setsockopt (se->fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
852 &loop, sizeof (loop)) == -1)
855 ERROR ("setsockopt: %s",
856 sstrerror (errno, errbuf,
861 if (setsockopt (se->fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
862 &mreq, sizeof (mreq)) == -1)
865 ERROR ("setsockopt: %s",
866 sstrerror (errno, errbuf,
874 } /* int network_bind_socket */
876 static sockent_t *network_create_socket (const char *node,
880 struct addrinfo ai_hints;
881 struct addrinfo *ai_list, *ai_ptr;
884 sockent_t *se_head = NULL;
885 sockent_t *se_tail = NULL;
887 DEBUG ("node = %s, service = %s", node, service);
889 memset (&ai_hints, '\0', sizeof (ai_hints));
890 ai_hints.ai_flags = 0;
892 ai_hints.ai_flags |= AI_PASSIVE;
895 ai_hints.ai_flags |= AI_ADDRCONFIG;
897 ai_hints.ai_family = AF_UNSPEC;
898 ai_hints.ai_socktype = SOCK_DGRAM;
899 ai_hints.ai_protocol = IPPROTO_UDP;
901 ai_return = getaddrinfo (node, service, &ai_hints, &ai_list);
905 ERROR ("getaddrinfo (%s, %s): %s",
906 (node == NULL) ? "(null)" : node,
907 (service == NULL) ? "(null)" : service,
908 (ai_return == EAI_SYSTEM)
909 ? sstrerror (errno, errbuf, sizeof (errbuf))
910 : gai_strerror (ai_return));
914 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
918 if ((se = (sockent_t *) malloc (sizeof (sockent_t))) == NULL)
922 sstrerror (errno, errbuf,
927 if ((se->addr = (struct sockaddr_storage *) malloc (sizeof (struct sockaddr_storage))) == NULL)
931 sstrerror (errno, errbuf,
937 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
938 memset (se->addr, '\0', sizeof (struct sockaddr_storage));
939 memcpy (se->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
940 se->addrlen = ai_ptr->ai_addrlen;
942 se->fd = socket (ai_ptr->ai_family,
944 ai_ptr->ai_protocol);
951 sstrerror (errno, errbuf,
960 if (network_bind_socket (se, ai_ptr) != 0)
968 else /* listen == 0 */
970 network_set_ttl (se, ai_ptr);
984 /* We don't open more than one write-socket per node/service pair.. */
989 freeaddrinfo (ai_list);
992 } /* sockent_t *network_create_socket */
994 static sockent_t *network_create_default_socket (int listen)
996 sockent_t *se_ptr = NULL;
997 sockent_t *se_head = NULL;
998 sockent_t *se_tail = NULL;
1000 se_ptr = network_create_socket (NET_DEFAULT_V6_ADDR,
1001 NET_DEFAULT_PORT, listen);
1003 /* Don't send to the same machine in IPv6 and IPv4 if both are available. */
1004 if ((listen == 0) && (se_ptr != NULL))
1011 while (se_tail->next != NULL)
1012 se_tail = se_tail->next;
1015 se_ptr = network_create_socket (NET_DEFAULT_V4_ADDR, NET_DEFAULT_PORT, listen);
1017 if (se_tail == NULL)
1020 se_tail->next = se_ptr;
1022 } /* sockent_t *network_create_default_socket */
1024 static int network_add_listen_socket (const char *node, const char *service)
1030 if (service == NULL)
1031 service = NET_DEFAULT_PORT;
1034 se = network_create_default_socket (1 /* listen == true */);
1036 se = network_create_socket (node, service, 1 /* listen == true */);
1041 for (se_ptr = se; se_ptr != NULL; se_ptr = se_ptr->next)
1044 listen_sockets = (struct pollfd *) realloc (listen_sockets,
1045 (listen_sockets_num + se_num)
1046 * sizeof (struct pollfd));
1048 for (se_ptr = se; se_ptr != NULL; se_ptr = se_ptr->next)
1050 listen_sockets[listen_sockets_num].fd = se_ptr->fd;
1051 listen_sockets[listen_sockets_num].events = POLLIN | POLLPRI;
1052 listen_sockets[listen_sockets_num].revents = 0;
1053 listen_sockets_num++;
1058 } /* int network_add_listen_socket */
1060 static int network_add_sending_socket (const char *node, const char *service)
1065 if (service == NULL)
1066 service = NET_DEFAULT_PORT;
1069 se = network_create_default_socket (0 /* listen == false */);
1071 se = network_create_socket (node, service, 0 /* listen == false */);
1076 if (sending_sockets == NULL)
1078 sending_sockets = se;
1082 for (se_ptr = sending_sockets; se_ptr->next != NULL; se_ptr = se_ptr->next)
1087 } /* int network_get_listen_socket */
1089 static void *dispatch_thread (void *arg)
1093 receive_list_entry_t *ent;
1095 /* Lock and wait for more data to come in */
1096 pthread_mutex_lock (&receive_list_lock);
1097 while ((listen_loop == 0)
1098 && (receive_list_head == NULL))
1099 pthread_cond_wait (&receive_list_cond, &receive_list_lock);
1101 /* Remove the head entry and unlock */
1102 ent = receive_list_head;
1104 receive_list_head = ent->next;
1105 pthread_mutex_unlock (&receive_list_lock);
1107 /* Check whether we are supposed to exit. We do NOT check `listen_loop'
1108 * because we dispatch all missing packets before shutting down. */
1112 parse_packet (ent->data, ent->data_len);
1118 } /* void *receive_thread */
1120 static int network_receive (void)
1122 char buffer[BUFF_SIZE];
1128 if (listen_sockets_num == 0)
1129 network_add_listen_socket (NULL, NULL);
1131 if (listen_sockets_num == 0)
1133 ERROR ("network: Failed to open a listening socket.");
1137 while (listen_loop == 0)
1139 status = poll (listen_sockets, listen_sockets_num, -1);
1146 ERROR ("poll failed: %s",
1147 sstrerror (errno, errbuf, sizeof (errbuf)));
1151 for (i = 0; (i < listen_sockets_num) && (status > 0); i++)
1153 receive_list_entry_t *ent;
1155 if ((listen_sockets[i].revents & (POLLIN | POLLPRI)) == 0)
1159 buffer_len = recv (listen_sockets[i].fd,
1160 buffer, sizeof (buffer),
1165 ERROR ("recv failed: %s",
1166 sstrerror (errno, errbuf,
1171 ent = malloc (sizeof (receive_list_entry_t));
1174 ERROR ("network plugin: malloc failed.");
1177 memset (ent, '\0', sizeof (receive_list_entry_t));
1179 /* Hopefully this be optimized out by the compiler. It
1180 * might help prevent stupid bugs in the future though.
1182 assert (sizeof (ent->data) == sizeof (buffer));
1184 memcpy (ent->data, buffer, buffer_len);
1185 ent->data_len = buffer_len;
1187 pthread_mutex_lock (&receive_list_lock);
1188 if (receive_list_head == NULL)
1190 receive_list_head = ent;
1191 receive_list_tail = ent;
1195 receive_list_tail->next = ent;
1196 receive_list_tail = ent;
1198 pthread_cond_signal (&receive_list_cond);
1199 pthread_mutex_unlock (&receive_list_lock);
1200 } /* for (listen_sockets) */
1201 } /* while (listen_loop == 0) */
1206 static void *receive_thread (void *arg)
1208 return (network_receive () ? (void *) 1 : (void *) 0);
1209 } /* void *receive_thread */
1211 static void network_send_buffer (const char *buffer, int buffer_len)
1216 DEBUG ("network plugin: network_send_buffer: buffer_len = %i", buffer_len);
1218 for (se = sending_sockets; se != NULL; se = se->next)
1222 status = sendto (se->fd, buffer, buffer_len, 0 /* no flags */,
1223 (struct sockaddr *) se->addr, se->addrlen);
1229 ERROR ("network plugin: sendto failed: %s",
1230 sstrerror (errno, errbuf,
1237 } /* for (sending_sockets) */
1238 } /* void network_send_buffer */
1240 static int add_to_buffer (char *buffer, int buffer_size,
1241 value_list_t *vl_def, char *type_def,
1242 const data_set_t *ds, const value_list_t *vl)
1244 char *buffer_orig = buffer;
1246 if (strcmp (vl_def->host, vl->host) != 0)
1248 if (write_part_string (&buffer, &buffer_size, TYPE_HOST,
1249 vl->host, strlen (vl->host)) != 0)
1251 strcpy (vl_def->host, vl->host);
1254 if (vl_def->time != vl->time)
1256 if (write_part_number (&buffer, &buffer_size, TYPE_TIME,
1257 (uint64_t) vl->time))
1259 vl_def->time = vl->time;
1262 if (vl_def->interval != vl->interval)
1264 if (write_part_number (&buffer, &buffer_size, TYPE_INTERVAL,
1265 (uint64_t) vl->interval))
1267 vl_def->interval = vl->interval;
1270 if (strcmp (vl_def->plugin, vl->plugin) != 0)
1272 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN,
1273 vl->plugin, strlen (vl->plugin)) != 0)
1275 strcpy (vl_def->plugin, vl->plugin);
1278 if (strcmp (vl_def->plugin_instance, vl->plugin_instance) != 0)
1280 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
1281 vl->plugin_instance,
1282 strlen (vl->plugin_instance)) != 0)
1284 strcpy (vl_def->plugin_instance, vl->plugin_instance);
1287 if (strcmp (type_def, ds->type) != 0)
1289 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
1290 ds->type, strlen (ds->type)) != 0)
1292 strcpy (type_def, ds->type);
1295 if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
1297 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE_INSTANCE,
1299 strlen (vl->type_instance)) != 0)
1301 strcpy (vl_def->type_instance, vl->type_instance);
1304 if (write_part_values (&buffer, &buffer_size, ds, vl) != 0)
1307 return (buffer - buffer_orig);
1308 } /* int add_to_buffer */
1310 static void flush_buffer (void)
1312 DEBUG ("network plugin: flush_buffer: send_buffer_fill = %i",
1315 network_send_buffer (send_buffer, send_buffer_fill);
1316 send_buffer_ptr = send_buffer;
1317 send_buffer_fill = 0;
1318 memset (&send_buffer_vl, '\0', sizeof (send_buffer_vl));
1319 memset (send_buffer_type, '\0', sizeof (send_buffer_type));
1322 static int network_write (const data_set_t *ds, const value_list_t *vl)
1326 /* If the value is already in the cache, we have received it via the
1327 * network. We write it again if forwarding is activated. It's then in
1328 * the cache and should we receive it again we will ignore it. */
1329 status = cache_check (ds->type, vl);
1330 if ((network_config_forward == 0)
1334 pthread_mutex_lock (&send_buffer_lock);
1336 status = add_to_buffer (send_buffer_ptr,
1337 sizeof (send_buffer) - send_buffer_fill,
1338 &send_buffer_vl, send_buffer_type,
1342 /* status == bytes added to the buffer */
1343 send_buffer_fill += status;
1344 send_buffer_ptr += status;
1350 status = add_to_buffer (send_buffer_ptr,
1351 sizeof (send_buffer) - send_buffer_fill,
1352 &send_buffer_vl, send_buffer_type,
1357 send_buffer_fill += status;
1358 send_buffer_ptr += status;
1364 ERROR ("network plugin: Unable to append to the "
1365 "buffer for some weird reason");
1367 else if ((sizeof (send_buffer) - send_buffer_fill) < 15)
1372 pthread_mutex_unlock (&send_buffer_lock);
1374 return ((status < 0) ? -1 : 0);
1375 } /* int network_write */
1377 static int network_config (const char *key, const char *val)
1385 if ((strcasecmp ("Listen", key) == 0)
1386 || (strcasecmp ("Server", key) == 0))
1388 char *val_cpy = strdup (val);
1389 if (val_cpy == NULL)
1392 service = NET_DEFAULT_PORT;
1393 fields_num = strsplit (val_cpy, fields, 3);
1394 if ((fields_num != 1)
1395 && (fields_num != 2))
1397 else if (fields_num == 2)
1399 if ((service = strchr (fields[1], '.')) != NULL)
1401 service = fields[1];
1405 if (strcasecmp ("Listen", key) == 0)
1406 network_add_listen_socket (node, service);
1408 network_add_sending_socket (node, service);
1410 else if (strcasecmp ("TimeToLive", key) == 0)
1412 int tmp = atoi (val);
1413 if ((tmp > 0) && (tmp < 256))
1414 network_config_ttl = tmp;
1418 else if (strcasecmp ("Forward", key) == 0)
1420 if ((strcasecmp ("true", val) == 0)
1421 || (strcasecmp ("yes", val) == 0)
1422 || (strcasecmp ("on", val) == 0))
1423 network_config_forward = 1;
1425 network_config_forward = 0;
1427 else if (strcasecmp ("CacheFlush", key) == 0)
1429 int tmp = atoi (val);
1431 cache_flush_interval = tmp;
1439 } /* int network_config */
1441 static int network_notification (const notification_t *n)
1443 char buffer[BUFF_SIZE];
1444 char *buffer_ptr = buffer;
1445 int buffer_free = sizeof (buffer);
1448 memset (buffer, '\0', sizeof (buffer));
1451 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_TIME,
1452 (uint64_t) n->time);
1456 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_SEVERITY,
1457 (uint64_t) n->severity);
1461 if (strlen (n->host) > 0)
1463 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_HOST,
1464 n->host, strlen (n->host));
1469 if (strlen (n->plugin) > 0)
1471 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_PLUGIN,
1472 n->plugin, strlen (n->plugin));
1477 if (strlen (n->plugin_instance) > 0)
1479 status = write_part_string (&buffer_ptr, &buffer_free,
1480 TYPE_PLUGIN_INSTANCE,
1481 n->plugin_instance, strlen (n->plugin_instance));
1486 if (strlen (n->type) > 0)
1488 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE,
1489 n->type, strlen (n->type));
1494 if (strlen (n->type_instance) > 0)
1496 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE_INSTANCE,
1497 n->type_instance, strlen (n->type_instance));
1502 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_MESSAGE,
1503 n->message, strlen (n->message));
1507 network_send_buffer (buffer, sizeof (buffer) - buffer_free);
1510 } /* int network_notification */
1512 static int network_shutdown (void)
1516 /* Kill the listening thread */
1517 if (receive_thread_id != (pthread_t) 0)
1519 pthread_kill (receive_thread_id, SIGTERM);
1520 pthread_join (receive_thread_id, NULL /* no return value */);
1521 receive_thread_id = (pthread_t) 0;
1524 /* Shutdown the dispatching thread */
1525 if (dispatch_thread_id != (pthread_t) 0)
1526 pthread_cond_broadcast (&receive_list_cond);
1528 if (send_buffer_fill > 0)
1531 if (cache_tree != NULL)
1536 while (c_avl_pick (cache_tree, &key, &value) == 0)
1541 c_avl_destroy (cache_tree);
1545 /* TODO: Close `sending_sockets' */
1547 plugin_unregister_config ("network");
1548 plugin_unregister_init ("network");
1549 plugin_unregister_write ("network");
1550 plugin_unregister_shutdown ("network");
1553 } /* int network_shutdown */
1555 static int network_init (void)
1557 plugin_register_shutdown ("network", network_shutdown);
1559 send_buffer_ptr = send_buffer;
1560 send_buffer_fill = 0;
1561 memset (&send_buffer_vl, '\0', sizeof (send_buffer_vl));
1562 memset (send_buffer_type, '\0', sizeof (send_buffer_type));
1564 cache_tree = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1565 cache_flush_last = time (NULL);
1567 /* setup socket(s) and so on */
1568 if (sending_sockets != NULL)
1570 plugin_register_write ("network", network_write);
1571 plugin_register_notification ("network", network_notification);
1574 if ((listen_sockets_num != 0) && (receive_thread_id == 0))
1578 status = pthread_create (&dispatch_thread_id,
1579 NULL /* no attributes */,
1581 NULL /* no argument */);
1585 ERROR ("network: pthread_create failed: %s",
1586 sstrerror (errno, errbuf,
1590 status = pthread_create (&receive_thread_id,
1591 NULL /* no attributes */,
1593 NULL /* no argument */);
1597 ERROR ("network: pthread_create failed: %s",
1598 sstrerror (errno, errbuf,
1603 } /* int network_init */
1605 void module_register (void)
1607 plugin_register_config ("network", network_config,
1608 config_keys, config_keys_num);
1609 plugin_register_init ("network", network_init);
1610 } /* void module_register */