2 * collectd - src/network.c
3 * Copyright (C) 2005-2007 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));
494 ps.value = buffer + 4;
495 if (ps.value[h_length - 5] != '\0')
497 DEBUG ("String does not end with a nullbyte");
501 if (output_len < (h_length - 4))
503 DEBUG ("output buffer is too small");
506 strcpy (output, ps.value);
508 DEBUG ("network plugin: parse_part_string: output = %s", output);
510 *ret_buffer = (void *) (buffer + h_length);
511 *ret_buffer_len = buffer_len - h_length;
514 } /* int parse_part_string */
516 static int parse_packet (void *buffer, int buffer_len)
518 part_header_t *header;
521 value_list_t vl = VALUE_LIST_INIT;
522 char type[DATA_MAX_NAME_LEN];
524 DEBUG ("network plugin: parse_packet: buffer = %p; buffer_len = %i;",
527 memset (&vl, '\0', sizeof (vl));
528 memset (&type, '\0', sizeof (type));
531 while ((status == 0) && (buffer_len > sizeof (part_header_t)))
533 header = (part_header_t *) buffer;
535 if (ntohs (header->length) > buffer_len)
537 /* Assure that this loop terminates eventually */
538 if (ntohs (header->length) < 4)
541 if (ntohs (header->type) == TYPE_VALUES)
543 status = parse_part_values (&buffer, &buffer_len,
544 &vl.values, &vl.values_len);
548 DEBUG ("parse_part_values failed.");
553 && (strlen (vl.host) > 0)
554 && (strlen (vl.plugin) > 0)
555 && (strlen (type) > 0)
556 && (cache_check (type, &vl) == 0))
558 DEBUG ("network plugin: parse_packet:"
559 " dispatching values");
560 plugin_dispatch_values (type, &vl);
564 DEBUG ("network plugin: parse_packet:"
565 " NOT dispatching values");
568 else if (ntohs (header->type) == TYPE_TIME)
571 status = parse_part_number (&buffer, &buffer_len, &tmp);
573 vl.time = (time_t) tmp;
575 else if (ntohs (header->type) == TYPE_INTERVAL)
578 status = parse_part_number (&buffer, &buffer_len, &tmp);
580 vl.interval = (int) tmp;
582 else if (ntohs (header->type) == TYPE_HOST)
584 status = parse_part_string (&buffer, &buffer_len,
585 vl.host, sizeof (vl.host));
586 DEBUG ("network plugin: parse_packet: vl.host = %s", vl.host);
588 else if (ntohs (header->type) == TYPE_PLUGIN)
590 status = parse_part_string (&buffer, &buffer_len,
591 vl.plugin, sizeof (vl.plugin));
592 DEBUG ("network plugin: parse_packet: vl.plugin = %s", vl.plugin);
594 else if (ntohs (header->type) == TYPE_PLUGIN_INSTANCE)
596 status = parse_part_string (&buffer, &buffer_len,
597 vl.plugin_instance, sizeof (vl.plugin_instance));
598 DEBUG ("network plugin: parse_packet: vl.plugin_instance = %s", vl.plugin_instance);
600 else if (ntohs (header->type) == TYPE_TYPE)
602 status = parse_part_string (&buffer, &buffer_len,
603 type, sizeof (type));
604 DEBUG ("network plugin: parse_packet: type = %s", type);
606 else if (ntohs (header->type) == TYPE_TYPE_INSTANCE)
608 status = parse_part_string (&buffer, &buffer_len,
609 vl.type_instance, sizeof (vl.type_instance));
610 DEBUG ("network plugin: parse_packet: vl.type_instance = %s", vl.type_instance);
614 DEBUG ("network plugin: parse_packet: Unknown part"
615 " type: 0x%0hx", ntohs (header->type));
616 buffer = ((char *) buffer) + ntohs (header->length);
618 } /* while (buffer_len > sizeof (part_header_t)) */
621 } /* int parse_packet */
623 static void free_sockent (sockent_t *se)
633 } /* void free_sockent */
636 * int network_set_ttl
638 * Set the `IP_MULTICAST_TTL', `IP_TTL', `IPV6_MULTICAST_HOPS' or
639 * `IPV6_UNICAST_HOPS', depending on which option is applicable.
641 * The `struct addrinfo' is used to destinguish between unicast and multicast
644 static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
646 if ((network_config_ttl < 1) || (network_config_ttl > 255))
649 DEBUG ("ttl = %i", network_config_ttl);
651 if (ai->ai_family == AF_INET)
653 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
656 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
657 optname = IP_MULTICAST_TTL;
661 if (setsockopt (se->fd, IPPROTO_IP, optname,
663 sizeof (network_config_ttl)) == -1)
666 ERROR ("setsockopt: %s",
667 sstrerror (errno, errbuf, sizeof (errbuf)));
671 else if (ai->ai_family == AF_INET6)
673 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
674 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
677 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
678 optname = IPV6_MULTICAST_HOPS;
680 optname = IPV6_UNICAST_HOPS;
682 if (setsockopt (se->fd, IPPROTO_IPV6, optname,
684 sizeof (network_config_ttl)) == -1)
687 ERROR ("setsockopt: %s",
688 sstrerror (errno, errbuf,
695 } /* int network_set_ttl */
697 static int network_bind_socket (const sockent_t *se, const struct addrinfo *ai)
701 DEBUG ("fd = %i; calling `bind'", se->fd);
703 if (bind (se->fd, ai->ai_addr, ai->ai_addrlen) == -1)
707 sstrerror (errno, errbuf, sizeof (errbuf)));
711 if (ai->ai_family == AF_INET)
713 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
714 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
718 DEBUG ("fd = %i; IPv4 multicast address found", se->fd);
720 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
721 mreq.imr_interface.s_addr = htonl (INADDR_ANY);
723 if (setsockopt (se->fd, IPPROTO_IP, IP_MULTICAST_LOOP,
724 &loop, sizeof (loop)) == -1)
727 ERROR ("setsockopt: %s",
728 sstrerror (errno, errbuf,
733 if (setsockopt (se->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
734 &mreq, sizeof (mreq)) == -1)
737 ERROR ("setsockopt: %s",
738 sstrerror (errno, errbuf,
744 else if (ai->ai_family == AF_INET6)
746 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
747 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
748 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
750 struct ipv6_mreq mreq;
752 DEBUG ("fd = %i; IPv6 multicast address found", se->fd);
754 memcpy (&mreq.ipv6mr_multiaddr,
756 sizeof (addr->sin6_addr));
758 /* http://developer.apple.com/documentation/Darwin/Reference/ManPages/man4/ip6.4.html
759 * ipv6mr_interface may be set to zeroes to
760 * choose the default multicast interface or to
761 * the index of a particular multicast-capable
762 * interface if the host is multihomed.
763 * Membership is associ-associated with a
764 * single interface; programs running on
765 * multihomed hosts may need to join the same
766 * group on more than one interface.*/
767 mreq.ipv6mr_interface = 0;
769 if (setsockopt (se->fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
770 &loop, sizeof (loop)) == -1)
773 ERROR ("setsockopt: %s",
774 sstrerror (errno, errbuf,
779 if (setsockopt (se->fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
780 &mreq, sizeof (mreq)) == -1)
783 ERROR ("setsockopt: %s",
784 sstrerror (errno, errbuf,
792 } /* int network_bind_socket */
794 static sockent_t *network_create_socket (const char *node,
798 struct addrinfo ai_hints;
799 struct addrinfo *ai_list, *ai_ptr;
802 sockent_t *se_head = NULL;
803 sockent_t *se_tail = NULL;
805 DEBUG ("node = %s, service = %s", node, service);
807 memset (&ai_hints, '\0', sizeof (ai_hints));
808 ai_hints.ai_flags = 0;
810 ai_hints.ai_flags |= AI_PASSIVE;
813 ai_hints.ai_flags |= AI_ADDRCONFIG;
815 ai_hints.ai_family = AF_UNSPEC;
816 ai_hints.ai_socktype = SOCK_DGRAM;
817 ai_hints.ai_protocol = IPPROTO_UDP;
819 ai_return = getaddrinfo (node, service, &ai_hints, &ai_list);
823 ERROR ("getaddrinfo (%s, %s): %s",
824 (node == NULL) ? "(null)" : node,
825 (service == NULL) ? "(null)" : service,
826 (ai_return == EAI_SYSTEM)
827 ? sstrerror (errno, errbuf, sizeof (errbuf))
828 : gai_strerror (ai_return));
832 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
836 if ((se = (sockent_t *) malloc (sizeof (sockent_t))) == NULL)
840 sstrerror (errno, errbuf,
845 if ((se->addr = (struct sockaddr_storage *) malloc (sizeof (struct sockaddr_storage))) == NULL)
849 sstrerror (errno, errbuf,
855 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
856 memset (se->addr, '\0', sizeof (struct sockaddr_storage));
857 memcpy (se->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
858 se->addrlen = ai_ptr->ai_addrlen;
860 se->fd = socket (ai_ptr->ai_family,
862 ai_ptr->ai_protocol);
869 sstrerror (errno, errbuf,
878 if (network_bind_socket (se, ai_ptr) != 0)
886 else /* listen == 0 */
888 network_set_ttl (se, ai_ptr);
902 /* We don't open more than one write-socket per node/service pair.. */
907 freeaddrinfo (ai_list);
910 } /* sockent_t *network_create_socket */
912 static sockent_t *network_create_default_socket (int listen)
914 sockent_t *se_ptr = NULL;
915 sockent_t *se_head = NULL;
916 sockent_t *se_tail = NULL;
918 se_ptr = network_create_socket (NET_DEFAULT_V6_ADDR,
919 NET_DEFAULT_PORT, listen);
921 /* Don't send to the same machine in IPv6 and IPv4 if both are available. */
922 if ((listen == 0) && (se_ptr != NULL))
929 while (se_tail->next != NULL)
930 se_tail = se_tail->next;
933 se_ptr = network_create_socket (NET_DEFAULT_V4_ADDR, NET_DEFAULT_PORT, listen);
938 se_tail->next = se_ptr;
940 } /* sockent_t *network_create_default_socket */
942 static int network_add_listen_socket (const char *node, const char *service)
949 service = NET_DEFAULT_PORT;
952 se = network_create_default_socket (1 /* listen == true */);
954 se = network_create_socket (node, service, 1 /* listen == true */);
959 for (se_ptr = se; se_ptr != NULL; se_ptr = se_ptr->next)
962 listen_sockets = (struct pollfd *) realloc (listen_sockets,
963 (listen_sockets_num + se_num)
964 * sizeof (struct pollfd));
966 for (se_ptr = se; se_ptr != NULL; se_ptr = se_ptr->next)
968 listen_sockets[listen_sockets_num].fd = se_ptr->fd;
969 listen_sockets[listen_sockets_num].events = POLLIN | POLLPRI;
970 listen_sockets[listen_sockets_num].revents = 0;
971 listen_sockets_num++;
976 } /* int network_add_listen_socket */
978 static int network_add_sending_socket (const char *node, const char *service)
984 service = NET_DEFAULT_PORT;
987 se = network_create_default_socket (0 /* listen == false */);
989 se = network_create_socket (node, service, 0 /* listen == false */);
994 if (sending_sockets == NULL)
996 sending_sockets = se;
1000 for (se_ptr = sending_sockets; se_ptr->next != NULL; se_ptr = se_ptr->next)
1005 } /* int network_get_listen_socket */
1007 static void *dispatch_thread (void *arg)
1011 receive_list_entry_t *ent;
1013 /* Lock and wait for more data to come in */
1014 pthread_mutex_lock (&receive_list_lock);
1015 while ((listen_loop == 0)
1016 && (receive_list_head == NULL))
1017 pthread_cond_wait (&receive_list_cond, &receive_list_lock);
1019 /* Remove the head entry and unlock */
1020 ent = receive_list_head;
1022 receive_list_head = ent->next;
1023 pthread_mutex_unlock (&receive_list_lock);
1025 /* Check whether we are supposed to exit. We do NOT check `listen_loop'
1026 * because we dispatch all missing packets before shutting down. */
1030 parse_packet (ent->data, ent->data_len);
1036 } /* void *receive_thread */
1038 static int network_receive (void)
1040 char buffer[BUFF_SIZE];
1046 if (listen_sockets_num == 0)
1047 network_add_listen_socket (NULL, NULL);
1049 if (listen_sockets_num == 0)
1051 ERROR ("network: Failed to open a listening socket.");
1055 while (listen_loop == 0)
1057 status = poll (listen_sockets, listen_sockets_num, -1);
1064 ERROR ("poll failed: %s",
1065 sstrerror (errno, errbuf, sizeof (errbuf)));
1069 for (i = 0; (i < listen_sockets_num) && (status > 0); i++)
1071 receive_list_entry_t *ent;
1073 if ((listen_sockets[i].revents & (POLLIN | POLLPRI)) == 0)
1077 buffer_len = recv (listen_sockets[i].fd,
1078 buffer, sizeof (buffer),
1083 ERROR ("recv failed: %s",
1084 sstrerror (errno, errbuf,
1089 ent = malloc (sizeof (receive_list_entry_t));
1092 ERROR ("network plugin: malloc failed.");
1095 memset (ent, '\0', sizeof (receive_list_entry_t));
1097 /* Hopefully this be optimized out by the compiler. It
1098 * might help prevent stupid bugs in the future though.
1100 assert (sizeof (ent->data) == sizeof (buffer));
1102 memcpy (ent->data, buffer, buffer_len);
1103 ent->data_len = buffer_len;
1105 pthread_mutex_lock (&receive_list_lock);
1106 if (receive_list_head == NULL)
1108 receive_list_head = ent;
1109 receive_list_tail = ent;
1113 receive_list_tail->next = ent;
1114 receive_list_tail = ent;
1116 pthread_cond_signal (&receive_list_cond);
1117 pthread_mutex_unlock (&receive_list_lock);
1118 } /* for (listen_sockets) */
1119 } /* while (listen_loop == 0) */
1124 static void *receive_thread (void *arg)
1126 return (network_receive () ? (void *) 1 : (void *) 0);
1127 } /* void *receive_thread */
1129 static void network_send_buffer (const char *buffer, int buffer_len)
1134 DEBUG ("network plugin: network_send_buffer: buffer_len = %i", buffer_len);
1136 for (se = sending_sockets; se != NULL; se = se->next)
1140 status = sendto (se->fd, buffer, buffer_len, 0 /* no flags */,
1141 (struct sockaddr *) se->addr, se->addrlen);
1147 ERROR ("network plugin: sendto failed: %s",
1148 sstrerror (errno, errbuf,
1155 } /* for (sending_sockets) */
1156 } /* void network_send_buffer */
1158 static int add_to_buffer (char *buffer, int buffer_size,
1159 value_list_t *vl_def, char *type_def,
1160 const data_set_t *ds, const value_list_t *vl)
1162 char *buffer_orig = buffer;
1164 if (strcmp (vl_def->host, vl->host) != 0)
1166 if (write_part_string (&buffer, &buffer_size, TYPE_HOST,
1167 vl->host, strlen (vl->host)) != 0)
1169 strcpy (vl_def->host, vl->host);
1172 if (vl_def->time != vl->time)
1174 if (write_part_number (&buffer, &buffer_size, TYPE_TIME,
1175 (uint64_t) vl->time))
1177 vl_def->time = vl->time;
1180 if (vl_def->interval != vl->interval)
1182 if (write_part_number (&buffer, &buffer_size, TYPE_INTERVAL,
1183 (uint64_t) vl->interval))
1185 vl_def->interval = vl->interval;
1188 if (strcmp (vl_def->plugin, vl->plugin) != 0)
1190 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN,
1191 vl->plugin, strlen (vl->plugin)) != 0)
1193 strcpy (vl_def->plugin, vl->plugin);
1196 if (strcmp (vl_def->plugin_instance, vl->plugin_instance) != 0)
1198 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
1199 vl->plugin_instance,
1200 strlen (vl->plugin_instance)) != 0)
1202 strcpy (vl_def->plugin_instance, vl->plugin_instance);
1205 if (strcmp (type_def, ds->type) != 0)
1207 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
1208 ds->type, strlen (ds->type)) != 0)
1210 strcpy (type_def, ds->type);
1213 if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
1215 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE_INSTANCE,
1217 strlen (vl->type_instance)) != 0)
1219 strcpy (vl_def->type_instance, vl->type_instance);
1222 if (write_part_values (&buffer, &buffer_size, ds, vl) != 0)
1225 return (buffer - buffer_orig);
1226 } /* int add_to_buffer */
1228 static void flush_buffer (void)
1230 DEBUG ("network plugin: flush_buffer: send_buffer_fill = %i",
1233 network_send_buffer (send_buffer, send_buffer_fill);
1234 send_buffer_ptr = send_buffer;
1235 send_buffer_fill = 0;
1236 memset (&send_buffer_vl, '\0', sizeof (send_buffer_vl));
1237 memset (send_buffer_type, '\0', sizeof (send_buffer_type));
1240 static int network_write (const data_set_t *ds, const value_list_t *vl)
1244 /* If the value is already in the cache, we have received it via the
1245 * network. We write it again if forwarding is activated. It's then in
1246 * the cache and should we receive it again we will ignore it. */
1247 status = cache_check (ds->type, vl);
1248 if ((network_config_forward == 0)
1252 pthread_mutex_lock (&send_buffer_lock);
1254 status = add_to_buffer (send_buffer_ptr,
1255 sizeof (send_buffer) - send_buffer_fill,
1256 &send_buffer_vl, send_buffer_type,
1260 /* status == bytes added to the buffer */
1261 send_buffer_fill += status;
1262 send_buffer_ptr += status;
1268 status = add_to_buffer (send_buffer_ptr,
1269 sizeof (send_buffer) - send_buffer_fill,
1270 &send_buffer_vl, send_buffer_type,
1275 send_buffer_fill += status;
1276 send_buffer_ptr += status;
1282 ERROR ("network plugin: Unable to append to the "
1283 "buffer for some weird reason");
1285 else if ((sizeof (send_buffer) - send_buffer_fill) < 15)
1290 pthread_mutex_unlock (&send_buffer_lock);
1292 return ((status < 0) ? -1 : 0);
1293 } /* int network_write */
1295 static int network_config (const char *key, const char *val)
1303 if ((strcasecmp ("Listen", key) == 0)
1304 || (strcasecmp ("Server", key) == 0))
1306 char *val_cpy = strdup (val);
1307 if (val_cpy == NULL)
1310 service = NET_DEFAULT_PORT;
1311 fields_num = strsplit (val_cpy, fields, 3);
1312 if ((fields_num != 1)
1313 && (fields_num != 2))
1315 else if (fields_num == 2)
1317 if ((service = strchr (fields[1], '.')) != NULL)
1319 service = fields[1];
1323 if (strcasecmp ("Listen", key) == 0)
1324 network_add_listen_socket (node, service);
1326 network_add_sending_socket (node, service);
1328 else if (strcasecmp ("TimeToLive", key) == 0)
1330 int tmp = atoi (val);
1331 if ((tmp > 0) && (tmp < 256))
1332 network_config_ttl = tmp;
1336 else if (strcasecmp ("Forward", key) == 0)
1338 if ((strcasecmp ("true", val) == 0)
1339 || (strcasecmp ("yes", val) == 0)
1340 || (strcasecmp ("on", val) == 0))
1341 network_config_forward = 1;
1343 network_config_forward = 0;
1345 else if (strcasecmp ("CacheFlush", key) == 0)
1347 int tmp = atoi (val);
1349 cache_flush_interval = tmp;
1357 } /* int network_config */
1359 static int network_shutdown (void)
1363 /* Kill the listening thread */
1364 if (receive_thread_id != (pthread_t) 0)
1366 pthread_kill (receive_thread_id, SIGTERM);
1367 pthread_join (receive_thread_id, NULL /* no return value */);
1368 receive_thread_id = (pthread_t) 0;
1371 /* Shutdown the dispatching thread */
1372 if (dispatch_thread_id != (pthread_t) 0)
1373 pthread_cond_broadcast (&receive_list_cond);
1375 if (send_buffer_fill > 0)
1378 if (cache_tree != NULL)
1383 while (c_avl_pick (cache_tree, &key, &value) == 0)
1388 c_avl_destroy (cache_tree);
1392 /* TODO: Close `sending_sockets' */
1394 plugin_unregister_config ("network");
1395 plugin_unregister_init ("network");
1396 plugin_unregister_write ("network");
1397 plugin_unregister_shutdown ("network");
1400 } /* int network_shutdown */
1402 static int network_init (void)
1404 plugin_register_shutdown ("network", network_shutdown);
1406 send_buffer_ptr = send_buffer;
1407 send_buffer_fill = 0;
1408 memset (&send_buffer_vl, '\0', sizeof (send_buffer_vl));
1409 memset (send_buffer_type, '\0', sizeof (send_buffer_type));
1411 cache_tree = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1412 cache_flush_last = time (NULL);
1414 /* setup socket(s) and so on */
1415 if (sending_sockets != NULL)
1416 plugin_register_write ("network", network_write);
1418 if ((listen_sockets_num != 0) && (receive_thread_id == 0))
1422 status = pthread_create (&dispatch_thread_id,
1423 NULL /* no attributes */,
1425 NULL /* no argument */);
1429 ERROR ("network: pthread_create failed: %s",
1430 sstrerror (errno, errbuf,
1434 status = pthread_create (&receive_thread_id,
1435 NULL /* no attributes */,
1437 NULL /* no argument */);
1441 ERROR ("network: pthread_create failed: %s",
1442 sstrerror (errno, errbuf,
1447 } /* int network_init */
1449 void module_register (void)
1451 plugin_register_config ("network", network_config,
1452 config_keys, config_keys_num);
1453 plugin_register_init ("network", network_init);
1454 } /* void module_register */