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;
142 static const char *config_keys[] =
150 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
152 static int network_config_ttl = 0;
153 static int network_config_forward = 0;
155 static sockent_t *sending_sockets = NULL;
157 static struct pollfd *listen_sockets = NULL;
158 static int listen_sockets_num = 0;
159 static pthread_t listen_thread = 0;
160 static int listen_loop = 0;
162 static char send_buffer[BUFF_SIZE];
163 static char *send_buffer_ptr;
164 static int send_buffer_fill;
165 static value_list_t send_buffer_vl = VALUE_LIST_INIT;
166 static char send_buffer_type[DATA_MAX_NAME_LEN];
167 static pthread_mutex_t send_buffer_lock = PTHREAD_MUTEX_INITIALIZER;
169 static avl_tree_t *cache_tree = NULL;
170 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
171 static time_t cache_flush_last;
172 static int cache_flush_interval = 1800;
177 static int cache_flush (void)
187 avl_iterator_t *iter;
189 time_t curtime = time (NULL);
191 iter = avl_get_iterator (cache_tree);
192 while (avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
194 if ((curtime - *value) <= cache_flush_interval)
196 tmp = (char **) realloc (keys,
197 (keys_num + 1) * sizeof (char *));
201 avl_iterator_destroy (iter);
202 ERROR ("network plugin: cache_flush: realloc"
207 keys[keys_num] = key;
209 } /* while (avl_iterator_next) */
210 avl_iterator_destroy (iter);
212 for (i = 0; i < keys_num; i++)
214 if (avl_remove (cache_tree, keys[i], (void *) &key,
215 (void *) &value) != 0)
217 WARNING ("network plugin: cache_flush: avl_remove"
218 " (%s) failed.", keys[i]);
228 DEBUG ("network plugin: cache_flush: Removed %i %s",
229 keys_num, (keys_num == 1) ? "entry" : "entries");
230 cache_flush_last = curtime;
232 } /* int cache_flush */
234 static int cache_check (const char *type, const value_list_t *vl)
237 time_t *value = NULL;
240 if (cache_tree == NULL)
243 if (format_name (key, sizeof (key), vl->host, vl->plugin,
244 vl->plugin_instance, type, vl->type_instance))
247 pthread_mutex_lock (&cache_lock);
249 if (avl_get (cache_tree, key, (void *) &value) == 0)
251 if (*value < vl->time)
258 DEBUG ("network plugin: cache_check: *value = %i >= vl->time = %i",
259 (int) *value, (int) vl->time);
265 char *key_copy = strdup (key);
266 value = malloc (sizeof (time_t));
267 if ((key_copy != NULL) && (value != NULL))
270 avl_insert (cache_tree, key_copy, value);
280 if ((time (NULL) - cache_flush_last) > cache_flush_interval)
283 pthread_mutex_unlock (&cache_lock);
285 DEBUG ("network plugin: cache_check: key = %s; time = %i; retval = %i",
286 key, (int) vl->time, retval);
289 } /* int cache_check */
291 static int write_part_values (char **ret_buffer, int *ret_buffer_len,
292 const data_set_t *ds, const value_list_t *vl)
297 i = 6 + (9 * vl->values_len);
298 if (*ret_buffer_len < i)
300 *ret_buffer_len -= i;
302 pv.head = (part_header_t *) *ret_buffer;
303 pv.num_values = (uint16_t *) (pv.head + 1);
304 pv.values_types = (uint8_t *) (pv.num_values + 1);
305 pv.values = (value_t *) (pv.values_types + vl->values_len);
306 *ret_buffer = (void *) (pv.values + vl->values_len);
308 pv.head->type = htons (TYPE_VALUES);
309 pv.head->length = htons (6 + (9 * vl->values_len));
310 *pv.num_values = htons ((uint16_t) vl->values_len);
312 for (i = 0; i < vl->values_len; i++)
314 if (ds->ds[i].type == DS_TYPE_COUNTER)
316 pv.values_types[i] = DS_TYPE_COUNTER;
317 pv.values[i].counter = htonll (vl->values[i].counter);
321 pv.values_types[i] = DS_TYPE_GAUGE;
322 pv.values[i].gauge = vl->values[i].gauge;
327 } /* int write_part_values */
329 static int write_part_number (char **ret_buffer, int *ret_buffer_len,
330 int type, uint64_t value)
334 if (*ret_buffer_len < 12)
337 pn.head = (part_header_t *) *ret_buffer;
338 pn.value = (uint64_t *) (pn.head + 1);
340 pn.head->type = htons (type);
341 pn.head->length = htons (12);
342 *pn.value = htonll (value);
344 *ret_buffer = (char *) (pn.value + 1);
345 *ret_buffer_len -= 12;
348 } /* int write_part_number */
350 static int write_part_string (char **ret_buffer, int *ret_buffer_len,
351 int type, const char *str, int str_len)
356 len = 4 + str_len + 1;
357 if (*ret_buffer_len < len)
359 *ret_buffer_len -= len;
361 ps.head = (part_header_t *) *ret_buffer;
362 ps.value = (char *) (ps.head + 1);
364 ps.head->type = htons ((uint16_t) type);
365 ps.head->length = htons ((uint16_t) str_len + 5);
367 memcpy (ps.value, str, str_len);
368 ps.value[str_len] = '\0';
369 *ret_buffer = (void *) (ps.value + (str_len + 1));
372 } /* int write_part_string */
374 static int parse_part_values (void **ret_buffer, int *ret_buffer_len,
375 value_t **ret_values, int *ret_num_values)
377 char *buffer = *ret_buffer;
378 int buffer_len = *ret_buffer_len;
386 if (buffer_len < (15))
388 DEBUG ("network plugin: packet is too short: buffer_len = %i",
393 pv.head = (part_header_t *) buffer;
394 h_length = ntohs (pv.head->length);
395 h_type = ntohs (pv.head->type);
397 assert (h_type == TYPE_VALUES);
399 pv.num_values = (uint16_t *) (pv.head + 1);
400 h_num = ntohs (*pv.num_values);
402 if (h_num != ((h_length - 6) / 9))
404 DEBUG ("`length' and `num of values' don't match");
408 pv.values_types = (uint8_t *) (pv.num_values + 1);
409 pv.values = (value_t *) (pv.values_types + h_num);
411 for (i = 0; i < h_num; i++)
412 if (pv.values_types[i] == DS_TYPE_COUNTER)
413 pv.values[i].counter = ntohll (pv.values[i].counter);
415 *ret_buffer = (void *) (pv.values + h_num);
416 *ret_buffer_len = buffer_len - h_length;
417 *ret_num_values = h_num;
418 *ret_values = pv.values;
421 } /* int parse_part_values */
423 static int parse_part_number (void **ret_buffer, int *ret_buffer_len,
429 pn.head = (part_header_t *) *ret_buffer;
430 pn.value = (uint64_t *) (pn.head + 1);
432 len = ntohs (pn.head->length);
435 if (len > *ret_buffer_len)
437 *value = ntohll (*pn.value);
439 *ret_buffer = (void *) (pn.value + 1);
440 *ret_buffer_len -= len;
443 } /* int parse_part_number */
445 static int parse_part_string (void **ret_buffer, int *ret_buffer_len,
446 char *output, int output_len)
448 char *buffer = *ret_buffer;
449 int buffer_len = *ret_buffer_len;
455 DEBUG ("network plugin: parse_part_string: ret_buffer = %p;"
456 " ret_buffer_len = %i; output = %p; output_len = %i;",
457 *ret_buffer, *ret_buffer_len,
458 (void *) output, output_len);
460 ps.head = (part_header_t *) buffer;
462 h_length = ntohs (ps.head->length);
463 h_type = ntohs (ps.head->type);
465 DEBUG ("network plugin: parse_part_string: length = %hu; type = %hu;",
468 if (buffer_len < h_length)
470 DEBUG ("packet is too short");
473 assert ((h_type == TYPE_HOST)
474 || (h_type == TYPE_PLUGIN)
475 || (h_type == TYPE_PLUGIN_INSTANCE)
476 || (h_type == TYPE_TYPE)
477 || (h_type == TYPE_TYPE_INSTANCE));
479 ps.value = buffer + 4;
480 if (ps.value[h_length - 5] != '\0')
482 DEBUG ("String does not end with a nullbyte");
486 if (output_len < (h_length - 4))
488 DEBUG ("output buffer is too small");
491 strcpy (output, ps.value);
493 DEBUG ("network plugin: parse_part_string: output = %s", output);
495 *ret_buffer = (void *) (buffer + h_length);
496 *ret_buffer_len = buffer_len - h_length;
499 } /* int parse_part_string */
501 static int parse_packet (void *buffer, int buffer_len)
503 part_header_t *header;
506 value_list_t vl = VALUE_LIST_INIT;
507 char type[DATA_MAX_NAME_LEN];
509 DEBUG ("network plugin: parse_packet: buffer = %p; buffer_len = %i;",
512 memset (&vl, '\0', sizeof (vl));
513 memset (&type, '\0', sizeof (type));
516 while ((status == 0) && (buffer_len > sizeof (part_header_t)))
518 header = (part_header_t *) buffer;
520 if (ntohs (header->length) > buffer_len)
522 /* Assure that this loop terminates eventually */
523 if (ntohs (header->length) < 4)
526 if (ntohs (header->type) == TYPE_VALUES)
528 status = parse_part_values (&buffer, &buffer_len,
529 &vl.values, &vl.values_len);
533 DEBUG ("parse_part_values failed.");
538 && (strlen (vl.host) > 0)
539 && (strlen (vl.plugin) > 0)
540 && (strlen (type) > 0)
541 && (cache_check (type, &vl) == 0))
543 DEBUG ("network plugin: parse_packet:"
544 " dispatching values");
545 plugin_dispatch_values (type, &vl);
549 DEBUG ("network plugin: parse_packet:"
550 " NOT dispatching values");
553 else if (ntohs (header->type) == TYPE_TIME)
556 status = parse_part_number (&buffer, &buffer_len, &tmp);
558 vl.time = (time_t) tmp;
560 else if (ntohs (header->type) == TYPE_HOST)
562 status = parse_part_string (&buffer, &buffer_len,
563 vl.host, sizeof (vl.host));
564 DEBUG ("network plugin: parse_packet: vl.host = %s", vl.host);
566 else if (ntohs (header->type) == TYPE_PLUGIN)
568 status = parse_part_string (&buffer, &buffer_len,
569 vl.plugin, sizeof (vl.plugin));
570 DEBUG ("network plugin: parse_packet: vl.plugin = %s", vl.plugin);
572 else if (ntohs (header->type) == TYPE_PLUGIN_INSTANCE)
574 status = parse_part_string (&buffer, &buffer_len,
575 vl.plugin_instance, sizeof (vl.plugin_instance));
576 DEBUG ("network plugin: parse_packet: vl.plugin_instance = %s", vl.plugin_instance);
578 else if (ntohs (header->type) == TYPE_TYPE)
580 status = parse_part_string (&buffer, &buffer_len,
581 type, sizeof (type));
582 DEBUG ("network plugin: parse_packet: type = %s", type);
584 else if (ntohs (header->type) == TYPE_TYPE_INSTANCE)
586 status = parse_part_string (&buffer, &buffer_len,
587 vl.type_instance, sizeof (vl.type_instance));
588 DEBUG ("network plugin: parse_packet: vl.type_instance = %s", vl.type_instance);
592 DEBUG ("network plugin: parse_packet: Unknown part"
593 " type: 0x%0hx", ntohs (header->type));
594 buffer = ((char *) buffer) + ntohs (header->length);
596 } /* while (buffer_len > sizeof (part_header_t)) */
599 } /* int parse_packet */
601 static void free_sockent (sockent_t *se)
611 } /* void free_sockent */
614 * int network_set_ttl
616 * Set the `IP_MULTICAST_TTL', `IP_TTL', `IPV6_MULTICAST_HOPS' or
617 * `IPV6_UNICAST_HOPS', depending on which option is applicable.
619 * The `struct addrinfo' is used to destinguish between unicast and multicast
622 static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
624 if ((network_config_ttl < 1) || (network_config_ttl > 255))
627 DEBUG ("ttl = %i", network_config_ttl);
629 if (ai->ai_family == AF_INET)
631 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
634 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
635 optname = IP_MULTICAST_TTL;
639 if (setsockopt (se->fd, IPPROTO_IP, optname,
641 sizeof (network_config_ttl)) == -1)
644 ERROR ("setsockopt: %s",
645 sstrerror (errno, errbuf, sizeof (errbuf)));
649 else if (ai->ai_family == AF_INET6)
651 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
652 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
655 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
656 optname = IPV6_MULTICAST_HOPS;
658 optname = IPV6_UNICAST_HOPS;
660 if (setsockopt (se->fd, IPPROTO_IPV6, optname,
662 sizeof (network_config_ttl)) == -1)
665 ERROR ("setsockopt: %s",
666 sstrerror (errno, errbuf,
673 } /* int network_set_ttl */
675 static int network_bind_socket (const sockent_t *se, const struct addrinfo *ai)
679 DEBUG ("fd = %i; calling `bind'", se->fd);
681 if (bind (se->fd, ai->ai_addr, ai->ai_addrlen) == -1)
685 sstrerror (errno, errbuf, sizeof (errbuf)));
689 if (ai->ai_family == AF_INET)
691 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
692 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
696 DEBUG ("fd = %i; IPv4 multicast address found", se->fd);
698 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
699 mreq.imr_interface.s_addr = htonl (INADDR_ANY);
701 if (setsockopt (se->fd, IPPROTO_IP, IP_MULTICAST_LOOP,
702 &loop, sizeof (loop)) == -1)
705 ERROR ("setsockopt: %s",
706 sstrerror (errno, errbuf,
711 if (setsockopt (se->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
712 &mreq, sizeof (mreq)) == -1)
715 ERROR ("setsockopt: %s",
716 sstrerror (errno, errbuf,
722 else if (ai->ai_family == AF_INET6)
724 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
725 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
726 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
728 struct ipv6_mreq mreq;
730 DEBUG ("fd = %i; IPv6 multicast address found", se->fd);
732 memcpy (&mreq.ipv6mr_multiaddr,
734 sizeof (addr->sin6_addr));
736 /* http://developer.apple.com/documentation/Darwin/Reference/ManPages/man4/ip6.4.html
737 * ipv6mr_interface may be set to zeroes to
738 * choose the default multicast interface or to
739 * the index of a particular multicast-capable
740 * interface if the host is multihomed.
741 * Membership is associ-associated with a
742 * single interface; programs running on
743 * multihomed hosts may need to join the same
744 * group on more than one interface.*/
745 mreq.ipv6mr_interface = 0;
747 if (setsockopt (se->fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
748 &loop, sizeof (loop)) == -1)
751 ERROR ("setsockopt: %s",
752 sstrerror (errno, errbuf,
757 if (setsockopt (se->fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
758 &mreq, sizeof (mreq)) == -1)
761 ERROR ("setsockopt: %s",
762 sstrerror (errno, errbuf,
770 } /* int network_bind_socket */
772 static sockent_t *network_create_socket (const char *node,
776 struct addrinfo ai_hints;
777 struct addrinfo *ai_list, *ai_ptr;
780 sockent_t *se_head = NULL;
781 sockent_t *se_tail = NULL;
783 DEBUG ("node = %s, service = %s", node, service);
785 memset (&ai_hints, '\0', sizeof (ai_hints));
786 ai_hints.ai_flags = 0;
788 ai_hints.ai_flags |= AI_PASSIVE;
791 ai_hints.ai_flags |= AI_ADDRCONFIG;
793 ai_hints.ai_family = AF_UNSPEC;
794 ai_hints.ai_socktype = SOCK_DGRAM;
795 ai_hints.ai_protocol = IPPROTO_UDP;
797 ai_return = getaddrinfo (node, service, &ai_hints, &ai_list);
801 ERROR ("getaddrinfo (%s, %s): %s",
802 (node == NULL) ? "(null)" : node,
803 (service == NULL) ? "(null)" : service,
804 (ai_return == EAI_SYSTEM)
805 ? sstrerror (errno, errbuf, sizeof (errbuf))
806 : gai_strerror (ai_return));
810 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
814 if ((se = (sockent_t *) malloc (sizeof (sockent_t))) == NULL)
818 sstrerror (errno, errbuf,
823 if ((se->addr = (struct sockaddr_storage *) malloc (sizeof (struct sockaddr_storage))) == NULL)
827 sstrerror (errno, errbuf,
833 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
834 memset (se->addr, '\0', sizeof (struct sockaddr_storage));
835 memcpy (se->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
836 se->addrlen = ai_ptr->ai_addrlen;
838 se->fd = socket (ai_ptr->ai_family,
840 ai_ptr->ai_protocol);
847 sstrerror (errno, errbuf,
856 if (network_bind_socket (se, ai_ptr) != 0)
864 else /* listen == 0 */
866 network_set_ttl (se, ai_ptr);
880 /* We don't open more than one write-socket per node/service pair.. */
885 freeaddrinfo (ai_list);
888 } /* sockent_t *network_create_socket */
890 static sockent_t *network_create_default_socket (int listen)
892 sockent_t *se_ptr = NULL;
893 sockent_t *se_head = NULL;
894 sockent_t *se_tail = NULL;
896 se_ptr = network_create_socket (NET_DEFAULT_V6_ADDR,
897 NET_DEFAULT_PORT, listen);
899 /* Don't send to the same machine in IPv6 and IPv4 if both are available. */
900 if ((listen == 0) && (se_ptr != NULL))
907 while (se_tail->next != NULL)
908 se_tail = se_tail->next;
911 se_ptr = network_create_socket (NET_DEFAULT_V4_ADDR, NET_DEFAULT_PORT, listen);
916 se_tail->next = se_ptr;
918 } /* sockent_t *network_create_default_socket */
920 static int network_add_listen_socket (const char *node, const char *service)
927 service = NET_DEFAULT_PORT;
930 se = network_create_default_socket (1 /* listen == true */);
932 se = network_create_socket (node, service, 1 /* listen == true */);
937 for (se_ptr = se; se_ptr != NULL; se_ptr = se_ptr->next)
940 listen_sockets = (struct pollfd *) realloc (listen_sockets,
941 (listen_sockets_num + se_num)
942 * sizeof (struct pollfd));
944 for (se_ptr = se; se_ptr != NULL; se_ptr = se_ptr->next)
946 listen_sockets[listen_sockets_num].fd = se_ptr->fd;
947 listen_sockets[listen_sockets_num].events = POLLIN | POLLPRI;
948 listen_sockets[listen_sockets_num].revents = 0;
949 listen_sockets_num++;
954 } /* int network_add_listen_socket */
956 static int network_add_sending_socket (const char *node, const char *service)
962 service = NET_DEFAULT_PORT;
965 se = network_create_default_socket (0 /* listen == false */);
967 se = network_create_socket (node, service, 0 /* listen == false */);
972 if (sending_sockets == NULL)
974 sending_sockets = se;
978 for (se_ptr = sending_sockets; se_ptr->next != NULL; se_ptr = se_ptr->next)
983 } /* int network_get_listen_socket */
985 int network_receive (void)
987 char buffer[BUFF_SIZE];
993 if (listen_sockets_num == 0)
994 network_add_listen_socket (NULL, NULL);
996 if (listen_sockets_num == 0)
998 ERROR ("network: Failed to open a listening socket.");
1002 while (listen_loop == 0)
1004 status = poll (listen_sockets, listen_sockets_num, -1);
1011 ERROR ("poll failed: %s",
1012 sstrerror (errno, errbuf, sizeof (errbuf)));
1016 for (i = 0; (i < listen_sockets_num) && (status > 0); i++)
1018 if ((listen_sockets[i].revents & (POLLIN | POLLPRI)) == 0)
1022 buffer_len = recv (listen_sockets[i].fd,
1023 buffer, sizeof (buffer),
1028 ERROR ("recv failed: %s",
1029 sstrerror (errno, errbuf,
1034 parse_packet (buffer, buffer_len);
1035 } /* for (listen_sockets) */
1036 } /* while (listen_loop == 0) */
1041 static void *receive_thread (void *arg)
1043 return (network_receive () ? (void *) 1 : (void *) 0);
1044 } /* void *receive_thread */
1046 static void network_send_buffer (const char *buffer, int buffer_len)
1051 DEBUG ("network plugin: network_send_buffer: buffer_len = %i", buffer_len);
1053 for (se = sending_sockets; se != NULL; se = se->next)
1057 status = sendto (se->fd, buffer, buffer_len, 0 /* no flags */,
1058 (struct sockaddr *) se->addr, se->addrlen);
1064 ERROR ("network plugin: sendto failed: %s",
1065 sstrerror (errno, errbuf,
1072 } /* for (sending_sockets) */
1073 } /* void network_send_buffer */
1075 static int add_to_buffer (char *buffer, int buffer_size,
1076 value_list_t *vl_def, char *type_def,
1077 const data_set_t *ds, const value_list_t *vl)
1079 char *buffer_orig = buffer;
1081 if (strcmp (vl_def->host, vl->host) != 0)
1083 if (write_part_string (&buffer, &buffer_size, TYPE_HOST,
1084 vl->host, strlen (vl->host)) != 0)
1086 strcpy (vl_def->host, vl->host);
1087 DEBUG ("network plugin: add_to_buffer: host = %s", vl->host);
1090 if (vl_def->time != vl->time)
1092 if (write_part_number (&buffer, &buffer_size, TYPE_TIME,
1093 (uint64_t) vl->time))
1095 vl_def->time = vl->time;
1096 DEBUG ("network plugin: add_to_buffer: time = %u",
1097 (unsigned int) vl->time);
1100 if (strcmp (vl_def->plugin, vl->plugin) != 0)
1102 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN,
1103 vl->plugin, strlen (vl->plugin)) != 0)
1105 strcpy (vl_def->plugin, vl->plugin);
1106 DEBUG ("network plugin: add_to_buffer: plugin = %s",
1110 if (strcmp (vl_def->plugin_instance, vl->plugin_instance) != 0)
1112 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
1113 vl->plugin_instance,
1114 strlen (vl->plugin_instance)) != 0)
1116 strcpy (vl_def->plugin_instance, vl->plugin_instance);
1117 DEBUG ("network plugin: add_to_buffer: plugin_instance = %s",
1118 vl->plugin_instance);
1121 if (strcmp (type_def, ds->type) != 0)
1123 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
1124 ds->type, strlen (ds->type)) != 0)
1126 strcpy (type_def, ds->type);
1127 DEBUG ("network plugin: add_to_buffer: type = %s", ds->type);
1130 if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
1132 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE_INSTANCE,
1134 strlen (vl->type_instance)) != 0)
1136 strcpy (vl_def->type_instance, vl->type_instance);
1137 DEBUG ("network plugin: add_to_buffer: type_instance = %s",
1141 if (write_part_values (&buffer, &buffer_size, ds, vl) != 0)
1144 return (buffer - buffer_orig);
1145 } /* int add_to_buffer */
1147 static void flush_buffer (void)
1149 DEBUG ("network plugin: flush_buffer: send_buffer_fill = %i",
1152 network_send_buffer (send_buffer, send_buffer_fill);
1153 send_buffer_ptr = send_buffer;
1154 send_buffer_fill = 0;
1155 memset (&send_buffer_vl, '\0', sizeof (send_buffer_vl));
1156 memset (send_buffer_type, '\0', sizeof (send_buffer_type));
1159 static int network_write (const data_set_t *ds, const value_list_t *vl)
1163 /* If the value is already in the cache, we have received it via the
1164 * network. We write it again if forwarding is activated. It's then in
1165 * the cache and should we receive it again we will ignore it. */
1166 status = cache_check (ds->type, vl);
1167 if ((network_config_forward == 0)
1171 pthread_mutex_lock (&send_buffer_lock);
1173 status = add_to_buffer (send_buffer_ptr,
1174 sizeof (send_buffer) - send_buffer_fill,
1175 &send_buffer_vl, send_buffer_type,
1179 /* status == bytes added to the buffer */
1180 send_buffer_fill += status;
1181 send_buffer_ptr += status;
1187 status = add_to_buffer (send_buffer_ptr,
1188 sizeof (send_buffer) - send_buffer_fill,
1189 &send_buffer_vl, send_buffer_type,
1194 send_buffer_fill += status;
1195 send_buffer_ptr += status;
1201 ERROR ("network plugin: Unable to append to the "
1202 "buffer for some weird reason");
1204 else if ((sizeof (send_buffer) - send_buffer_fill) < 15)
1209 pthread_mutex_unlock (&send_buffer_lock);
1211 return ((status < 0) ? -1 : 0);
1212 } /* int network_write */
1214 static int network_config (const char *key, const char *val)
1222 if ((strcasecmp ("Listen", key) == 0)
1223 || (strcasecmp ("Server", key) == 0))
1225 char *val_cpy = strdup (val);
1226 if (val_cpy == NULL)
1229 service = NET_DEFAULT_PORT;
1230 fields_num = strsplit (val_cpy, fields, 3);
1231 if ((fields_num != 1)
1232 && (fields_num != 2))
1234 else if (fields_num == 2)
1236 if ((service = strchr (fields[1], '.')) != NULL)
1238 service = fields[1];
1242 if (strcasecmp ("Listen", key) == 0)
1243 network_add_listen_socket (node, service);
1245 network_add_sending_socket (node, service);
1247 else if (strcasecmp ("TimeToLive", key) == 0)
1249 int tmp = atoi (val);
1250 if ((tmp > 0) && (tmp < 256))
1251 network_config_ttl = tmp;
1255 else if (strcasecmp ("Forward", key) == 0)
1257 if ((strcasecmp ("true", val) == 0)
1258 || (strcasecmp ("yes", val) == 0)
1259 || (strcasecmp ("on", val) == 0))
1260 network_config_forward = 1;
1262 network_config_forward = 0;
1264 else if (strcasecmp ("CacheFlush", key) == 0)
1266 int tmp = atoi (val);
1268 cache_flush_interval = tmp;
1276 } /* int network_config */
1278 static int network_shutdown (void)
1280 DEBUG ("Shutting down.");
1284 if (listen_thread != (pthread_t) 0)
1286 pthread_kill (listen_thread, SIGTERM);
1287 pthread_join (listen_thread, NULL /* no return value */);
1288 listen_thread = (pthread_t) 0;
1293 if (cache_tree != NULL)
1298 while (avl_pick (cache_tree, &key, &value) == 0)
1303 avl_destroy (cache_tree);
1307 /* TODO: Close `sending_sockets' */
1309 plugin_unregister_config ("network");
1310 plugin_unregister_init ("network");
1311 plugin_unregister_write ("network");
1312 plugin_unregister_shutdown ("network");
1315 } /* int network_shutdown */
1317 static int network_init (void)
1319 plugin_register_shutdown ("network", network_shutdown);
1321 send_buffer_ptr = send_buffer;
1322 send_buffer_fill = 0;
1323 memset (&send_buffer_vl, '\0', sizeof (send_buffer_vl));
1324 memset (send_buffer_type, '\0', sizeof (send_buffer_type));
1326 cache_tree = avl_create ((int (*) (const void *, const void *)) strcmp);
1327 cache_flush_last = time (NULL);
1329 /* setup socket(s) and so on */
1330 if (sending_sockets != NULL)
1331 plugin_register_write ("network", network_write);
1333 if ((listen_sockets_num != 0) && (listen_thread == 0))
1337 status = pthread_create (&listen_thread, NULL /* no attributes */,
1338 receive_thread, NULL /* no argument */);
1343 ERROR ("network: pthread_create failed: %s",
1344 sstrerror (errno, errbuf,
1349 } /* int network_init */
1351 void module_register (void)
1353 plugin_register_config ("network", network_config,
1354 config_keys, config_keys_num);
1355 plugin_register_init ("network", network_init);
1356 } /* void module_register */