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[] =
149 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
151 static int network_config_ttl = 0;
152 static int network_config_forward = 0;
154 static sockent_t *sending_sockets = NULL;
156 static struct pollfd *listen_sockets = NULL;
157 static int listen_sockets_num = 0;
158 static pthread_t listen_thread = 0;
159 static int listen_loop = 0;
161 static char send_buffer[BUFF_SIZE];
162 static char *send_buffer_ptr;
163 static int send_buffer_fill;
164 static value_list_t send_buffer_vl = VALUE_LIST_INIT;
165 static char send_buffer_type[DATA_MAX_NAME_LEN];
166 static pthread_mutex_t send_buffer_lock = PTHREAD_MUTEX_INITIALIZER;
168 static avl_tree_t *cache_tree = NULL;
169 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
174 static int cache_check (const char *type, const value_list_t *vl)
177 time_t *value = NULL;
180 if (cache_tree == NULL)
183 if (format_name (key, sizeof (key), vl->host, vl->plugin,
184 vl->plugin_instance, type, vl->type_instance))
187 pthread_mutex_lock (&cache_lock);
189 if (avl_get (cache_tree, key, (void *) &value) == 0)
191 if (*value < vl->time)
198 DEBUG ("network plugin: cache_check: *value = %i >= vl->time = %i",
199 (int) *value, (int) vl->time);
205 char *key_copy = strdup (key);
206 value = malloc (sizeof (time_t));
207 if ((key_copy != NULL) && (value != NULL))
210 avl_insert (cache_tree, key_copy, value);
220 /* TODO: Flush cache */
222 pthread_mutex_unlock (&cache_lock);
224 DEBUG ("network plugin: cache_check: key = %s; time = %i; retval = %i",
225 key, (int) vl->time, retval);
228 } /* int cache_check */
230 static int write_part_values (char **ret_buffer, int *ret_buffer_len,
231 const data_set_t *ds, const value_list_t *vl)
236 i = 6 + (9 * vl->values_len);
237 if (*ret_buffer_len < i)
239 *ret_buffer_len -= i;
241 pv.head = (part_header_t *) *ret_buffer;
242 pv.num_values = (uint16_t *) (pv.head + 1);
243 pv.values_types = (uint8_t *) (pv.num_values + 1);
244 pv.values = (value_t *) (pv.values_types + vl->values_len);
245 *ret_buffer = (void *) (pv.values + vl->values_len);
247 pv.head->type = htons (TYPE_VALUES);
248 pv.head->length = htons (6 + (9 * vl->values_len));
249 *pv.num_values = htons ((uint16_t) vl->values_len);
251 for (i = 0; i < vl->values_len; i++)
253 if (ds->ds[i].type == DS_TYPE_COUNTER)
255 pv.values_types[i] = DS_TYPE_COUNTER;
256 pv.values[i].counter = htonll (vl->values[i].counter);
260 pv.values_types[i] = DS_TYPE_GAUGE;
261 pv.values[i].gauge = vl->values[i].gauge;
266 } /* int write_part_values */
268 static int write_part_number (char **ret_buffer, int *ret_buffer_len,
269 int type, uint64_t value)
273 if (*ret_buffer_len < 12)
276 pn.head = (part_header_t *) *ret_buffer;
277 pn.value = (uint64_t *) (pn.head + 1);
279 pn.head->type = htons (type);
280 pn.head->length = htons (12);
281 *pn.value = htonll (value);
283 *ret_buffer = (char *) (pn.value + 1);
284 *ret_buffer_len -= 12;
287 } /* int write_part_number */
289 static int write_part_string (char **ret_buffer, int *ret_buffer_len,
290 int type, const char *str, int str_len)
295 len = 4 + str_len + 1;
296 if (*ret_buffer_len < len)
298 *ret_buffer_len -= len;
300 ps.head = (part_header_t *) *ret_buffer;
301 ps.value = (char *) (ps.head + 1);
303 ps.head->type = htons ((uint16_t) type);
304 ps.head->length = htons ((uint16_t) str_len + 5);
306 memcpy (ps.value, str, str_len);
307 ps.value[str_len] = '\0';
308 *ret_buffer = (void *) (ps.value + (str_len + 1));
311 } /* int write_part_string */
313 static int parse_part_values (void **ret_buffer, int *ret_buffer_len,
314 value_t **ret_values, int *ret_num_values)
316 char *buffer = *ret_buffer;
317 int buffer_len = *ret_buffer_len;
325 if (buffer_len < (15))
327 DEBUG ("packet is too short: buffer_len = %i", buffer_len);
331 pv.head = (part_header_t *) buffer;
332 h_length = ntohs (pv.head->length);
333 h_type = ntohs (pv.head->type);
335 assert (h_type == TYPE_VALUES);
337 pv.num_values = (uint16_t *) (pv.head + 1);
338 h_num = ntohs (*pv.num_values);
340 if (h_num != ((h_length - 6) / 9))
342 DEBUG ("`length' and `num of values' don't match");
346 pv.values_types = (uint8_t *) (pv.num_values + 1);
347 pv.values = (value_t *) (pv.values_types + h_num);
349 for (i = 0; i < h_num; i++)
350 if (pv.values_types[i] == DS_TYPE_COUNTER)
351 pv.values[i].counter = ntohll (pv.values[i].counter);
353 *ret_buffer = (void *) (pv.values + h_num);
354 *ret_buffer_len = buffer_len - h_length;
355 *ret_num_values = h_num;
356 *ret_values = pv.values;
359 } /* int parse_part_values */
361 static int parse_part_number (void **ret_buffer, int *ret_buffer_len,
367 pn.head = (part_header_t *) *ret_buffer;
368 pn.value = (uint64_t *) (pn.head + 1);
370 len = ntohs (pn.head->length);
373 if (len > *ret_buffer_len)
375 *value = ntohll (*pn.value);
377 *ret_buffer = (void *) (pn.value + 1);
378 *ret_buffer_len -= len;
381 } /* int parse_part_number */
383 static int parse_part_string (void **ret_buffer, int *ret_buffer_len,
384 char *output, int output_len)
386 char *buffer = *ret_buffer;
387 int buffer_len = *ret_buffer_len;
393 DEBUG ("network plugin: parse_part_string: ret_buffer = %p;"
394 " ret_buffer_len = %i; output = %p; output_len = %i;",
395 *ret_buffer, *ret_buffer_len,
396 (void *) output, output_len);
398 ps.head = (part_header_t *) buffer;
400 h_length = ntohs (ps.head->length);
401 h_type = ntohs (ps.head->type);
403 DEBUG ("network plugin: parse_part_string: length = %hu; type = %hu;",
406 if (buffer_len < h_length)
408 DEBUG ("packet is too short");
411 assert ((h_type == TYPE_HOST)
412 || (h_type == TYPE_PLUGIN)
413 || (h_type == TYPE_PLUGIN_INSTANCE)
414 || (h_type == TYPE_TYPE)
415 || (h_type == TYPE_TYPE_INSTANCE));
417 ps.value = buffer + 4;
418 if (ps.value[h_length - 5] != '\0')
420 DEBUG ("String does not end with a nullbyte");
424 if (output_len < (h_length - 4))
426 DEBUG ("output buffer is too small");
429 strcpy (output, ps.value);
431 DEBUG ("network plugin: parse_part_string: output = %s", output);
433 *ret_buffer = (void *) (buffer + h_length);
434 *ret_buffer_len = buffer_len - h_length;
437 } /* int parse_part_string */
439 static int parse_packet (void *buffer, int buffer_len)
441 part_header_t *header;
444 value_list_t vl = VALUE_LIST_INIT;
445 char type[DATA_MAX_NAME_LEN];
447 DEBUG ("network plugin: parse_packet: buffer = %p; buffer_len = %i;",
450 memset (&vl, '\0', sizeof (vl));
451 memset (&type, '\0', sizeof (type));
454 while ((status == 0) && (buffer_len > sizeof (part_header_t)))
456 header = (part_header_t *) buffer;
458 if (ntohs (header->length) > buffer_len)
460 /* Assure that this loop terminates eventually */
461 if (ntohs (header->length) < 4)
464 if (ntohs (header->type) == TYPE_VALUES)
466 status = parse_part_values (&buffer, &buffer_len,
467 &vl.values, &vl.values_len);
471 DEBUG ("parse_part_values failed.");
476 && (strlen (vl.host) > 0)
477 && (strlen (vl.plugin) > 0)
478 && (strlen (type) > 0)
479 && (cache_check (type, &vl) == 0))
481 DEBUG ("network plugin: parse_packet:"
482 " dispatching values");
483 plugin_dispatch_values (type, &vl);
487 DEBUG ("network plugin: parse_packet:"
488 " NOT dispatching values");
491 else if (ntohs (header->type) == TYPE_TIME)
494 status = parse_part_number (&buffer, &buffer_len, &tmp);
496 vl.time = (time_t) tmp;
498 else if (ntohs (header->type) == TYPE_HOST)
500 status = parse_part_string (&buffer, &buffer_len,
501 vl.host, sizeof (vl.host));
502 DEBUG ("network plugin: parse_packet: vl.host = %s", vl.host);
504 else if (ntohs (header->type) == TYPE_PLUGIN)
506 status = parse_part_string (&buffer, &buffer_len,
507 vl.plugin, sizeof (vl.plugin));
508 DEBUG ("network plugin: parse_packet: vl.plugin = %s", vl.plugin);
510 else if (ntohs (header->type) == TYPE_PLUGIN_INSTANCE)
512 status = parse_part_string (&buffer, &buffer_len,
513 vl.plugin_instance, sizeof (vl.plugin_instance));
514 DEBUG ("network plugin: parse_packet: vl.plugin_instance = %s", vl.plugin_instance);
516 else if (ntohs (header->type) == TYPE_TYPE)
518 status = parse_part_string (&buffer, &buffer_len,
519 type, sizeof (type));
520 DEBUG ("network plugin: parse_packet: type = %s", type);
522 else if (ntohs (header->type) == TYPE_TYPE_INSTANCE)
524 status = parse_part_string (&buffer, &buffer_len,
525 vl.type_instance, sizeof (vl.type_instance));
526 DEBUG ("network plugin: parse_packet: vl.type_instance = %s", vl.type_instance);
530 DEBUG ("network plugin: parse_packet: Unknown part"
531 " type: 0x%0hx", ntohs (header->type));
532 buffer = ((char *) buffer) + ntohs (header->length);
534 } /* while (buffer_len > sizeof (part_header_t)) */
537 } /* int parse_packet */
539 static void free_sockent (sockent_t *se)
549 } /* void free_sockent */
552 * int network_set_ttl
554 * Set the `IP_MULTICAST_TTL', `IP_TTL', `IPV6_MULTICAST_HOPS' or
555 * `IPV6_UNICAST_HOPS', depending on which option is applicable.
557 * The `struct addrinfo' is used to destinguish between unicast and multicast
560 static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
562 if ((network_config_ttl < 1) || (network_config_ttl > 255))
565 DEBUG ("ttl = %i", network_config_ttl);
567 if (ai->ai_family == AF_INET)
569 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
572 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
573 optname = IP_MULTICAST_TTL;
577 if (setsockopt (se->fd, IPPROTO_IP, optname,
579 sizeof (network_config_ttl)) == -1)
582 ERROR ("setsockopt: %s",
583 sstrerror (errno, errbuf, sizeof (errbuf)));
587 else if (ai->ai_family == AF_INET6)
589 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
590 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
593 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
594 optname = IPV6_MULTICAST_HOPS;
596 optname = IPV6_UNICAST_HOPS;
598 if (setsockopt (se->fd, IPPROTO_IPV6, optname,
600 sizeof (network_config_ttl)) == -1)
603 ERROR ("setsockopt: %s",
604 sstrerror (errno, errbuf,
611 } /* int network_set_ttl */
613 static int network_bind_socket (const sockent_t *se, const struct addrinfo *ai)
617 DEBUG ("fd = %i; calling `bind'", se->fd);
619 if (bind (se->fd, ai->ai_addr, ai->ai_addrlen) == -1)
623 sstrerror (errno, errbuf, sizeof (errbuf)));
627 if (ai->ai_family == AF_INET)
629 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
630 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
634 DEBUG ("fd = %i; IPv4 multicast address found", se->fd);
636 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
637 mreq.imr_interface.s_addr = htonl (INADDR_ANY);
639 if (setsockopt (se->fd, IPPROTO_IP, IP_MULTICAST_LOOP,
640 &loop, sizeof (loop)) == -1)
643 ERROR ("setsockopt: %s",
644 sstrerror (errno, errbuf,
649 if (setsockopt (se->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
650 &mreq, sizeof (mreq)) == -1)
653 ERROR ("setsockopt: %s",
654 sstrerror (errno, errbuf,
660 else if (ai->ai_family == AF_INET6)
662 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
663 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
664 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
666 struct ipv6_mreq mreq;
668 DEBUG ("fd = %i; IPv6 multicast address found", se->fd);
670 memcpy (&mreq.ipv6mr_multiaddr,
672 sizeof (addr->sin6_addr));
674 /* http://developer.apple.com/documentation/Darwin/Reference/ManPages/man4/ip6.4.html
675 * ipv6mr_interface may be set to zeroes to
676 * choose the default multicast interface or to
677 * the index of a particular multicast-capable
678 * interface if the host is multihomed.
679 * Membership is associ-associated with a
680 * single interface; programs running on
681 * multihomed hosts may need to join the same
682 * group on more than one interface.*/
683 mreq.ipv6mr_interface = 0;
685 if (setsockopt (se->fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
686 &loop, sizeof (loop)) == -1)
689 ERROR ("setsockopt: %s",
690 sstrerror (errno, errbuf,
695 if (setsockopt (se->fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
696 &mreq, sizeof (mreq)) == -1)
699 ERROR ("setsockopt: %s",
700 sstrerror (errno, errbuf,
708 } /* int network_bind_socket */
710 static sockent_t *network_create_socket (const char *node,
714 struct addrinfo ai_hints;
715 struct addrinfo *ai_list, *ai_ptr;
718 sockent_t *se_head = NULL;
719 sockent_t *se_tail = NULL;
721 DEBUG ("node = %s, service = %s", node, service);
723 memset (&ai_hints, '\0', sizeof (ai_hints));
724 ai_hints.ai_flags = 0;
726 ai_hints.ai_flags |= AI_PASSIVE;
729 ai_hints.ai_flags |= AI_ADDRCONFIG;
731 ai_hints.ai_family = AF_UNSPEC;
732 ai_hints.ai_socktype = SOCK_DGRAM;
733 ai_hints.ai_protocol = IPPROTO_UDP;
735 ai_return = getaddrinfo (node, service, &ai_hints, &ai_list);
739 ERROR ("getaddrinfo (%s, %s): %s",
740 (node == NULL) ? "(null)" : node,
741 (service == NULL) ? "(null)" : service,
742 (ai_return == EAI_SYSTEM)
743 ? sstrerror (errno, errbuf, sizeof (errbuf))
744 : gai_strerror (ai_return));
748 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
752 if ((se = (sockent_t *) malloc (sizeof (sockent_t))) == NULL)
756 sstrerror (errno, errbuf,
761 if ((se->addr = (struct sockaddr_storage *) malloc (sizeof (struct sockaddr_storage))) == NULL)
765 sstrerror (errno, errbuf,
771 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
772 memset (se->addr, '\0', sizeof (struct sockaddr_storage));
773 memcpy (se->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
774 se->addrlen = ai_ptr->ai_addrlen;
776 se->fd = socket (ai_ptr->ai_family,
778 ai_ptr->ai_protocol);
785 sstrerror (errno, errbuf,
794 if (network_bind_socket (se, ai_ptr) != 0)
802 else /* listen == 0 */
804 network_set_ttl (se, ai_ptr);
818 /* We don't open more than one write-socket per node/service pair.. */
823 freeaddrinfo (ai_list);
826 } /* sockent_t *network_create_socket */
828 static sockent_t *network_create_default_socket (int listen)
830 sockent_t *se_ptr = NULL;
831 sockent_t *se_head = NULL;
832 sockent_t *se_tail = NULL;
834 se_ptr = network_create_socket (NET_DEFAULT_V6_ADDR,
835 NET_DEFAULT_PORT, listen);
837 /* Don't send to the same machine in IPv6 and IPv4 if both are available. */
838 if ((listen == 0) && (se_ptr != NULL))
845 while (se_tail->next != NULL)
846 se_tail = se_tail->next;
849 se_ptr = network_create_socket (NET_DEFAULT_V4_ADDR, NET_DEFAULT_PORT, listen);
854 se_tail->next = se_ptr;
856 } /* sockent_t *network_create_default_socket */
858 static int network_add_listen_socket (const char *node, const char *service)
865 service = NET_DEFAULT_PORT;
868 se = network_create_default_socket (1 /* listen == true */);
870 se = network_create_socket (node, service, 1 /* listen == true */);
875 for (se_ptr = se; se_ptr != NULL; se_ptr = se_ptr->next)
878 listen_sockets = (struct pollfd *) realloc (listen_sockets,
879 (listen_sockets_num + se_num)
880 * sizeof (struct pollfd));
882 for (se_ptr = se; se_ptr != NULL; se_ptr = se_ptr->next)
884 listen_sockets[listen_sockets_num].fd = se_ptr->fd;
885 listen_sockets[listen_sockets_num].events = POLLIN | POLLPRI;
886 listen_sockets[listen_sockets_num].revents = 0;
887 listen_sockets_num++;
892 } /* int network_add_listen_socket */
894 static int network_add_sending_socket (const char *node, const char *service)
900 service = NET_DEFAULT_PORT;
903 se = network_create_default_socket (0 /* listen == false */);
905 se = network_create_socket (node, service, 0 /* listen == false */);
910 if (sending_sockets == NULL)
912 sending_sockets = se;
916 for (se_ptr = sending_sockets; se_ptr->next != NULL; se_ptr = se_ptr->next)
921 } /* int network_get_listen_socket */
923 int network_receive (void)
925 char buffer[BUFF_SIZE];
931 if (listen_sockets_num == 0)
932 network_add_listen_socket (NULL, NULL);
934 if (listen_sockets_num == 0)
936 ERROR ("network: Failed to open a listening socket.");
940 while (listen_loop == 0)
942 status = poll (listen_sockets, listen_sockets_num, -1);
949 ERROR ("poll failed: %s",
950 sstrerror (errno, errbuf, sizeof (errbuf)));
954 for (i = 0; (i < listen_sockets_num) && (status > 0); i++)
956 if ((listen_sockets[i].revents & (POLLIN | POLLPRI)) == 0)
960 buffer_len = recv (listen_sockets[i].fd,
961 buffer, sizeof (buffer),
966 ERROR ("recv failed: %s",
967 sstrerror (errno, errbuf,
972 parse_packet (buffer, buffer_len);
973 } /* for (listen_sockets) */
974 } /* while (listen_loop == 0) */
979 static void *receive_thread (void *arg)
981 return (network_receive () ? (void *) 1 : (void *) 0);
982 } /* void *receive_thread */
984 static void network_send_buffer (const char *buffer, int buffer_len)
989 DEBUG ("buffer_len = %i", buffer_len);
991 for (se = sending_sockets; se != NULL; se = se->next)
995 status = sendto (se->fd, buffer, buffer_len, 0 /* no flags */,
996 (struct sockaddr *) se->addr, se->addrlen);
1002 ERROR ("network plugin: sendto failed: %s",
1003 sstrerror (errno, errbuf,
1010 } /* for (sending_sockets) */
1011 } /* void network_send_buffer */
1013 static int add_to_buffer (char *buffer, int buffer_size,
1014 value_list_t *vl_def, char *type_def,
1015 const data_set_t *ds, const value_list_t *vl)
1017 char *buffer_orig = buffer;
1019 if (strcmp (vl_def->host, vl->host) != 0)
1021 if (write_part_string (&buffer, &buffer_size, TYPE_HOST,
1022 vl->host, strlen (vl->host)) != 0)
1024 strcpy (vl_def->host, vl->host);
1025 DEBUG ("network plugin: add_to_buffer: host = %s", vl->host);
1028 if (vl_def->time != vl->time)
1030 if (write_part_number (&buffer, &buffer_size, TYPE_TIME,
1031 (uint64_t) vl->time))
1033 vl_def->time = vl->time;
1034 DEBUG ("network plugin: add_to_buffer: time = %u",
1035 (unsigned int) vl->time);
1038 if (strcmp (vl_def->plugin, vl->plugin) != 0)
1040 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN,
1041 vl->plugin, strlen (vl->plugin)) != 0)
1043 strcpy (vl_def->plugin, vl->plugin);
1044 DEBUG ("network plugin: add_to_buffer: plugin = %s",
1048 if (strcmp (vl_def->plugin_instance, vl->plugin_instance) != 0)
1050 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
1051 vl->plugin_instance,
1052 strlen (vl->plugin_instance)) != 0)
1054 strcpy (vl_def->plugin_instance, vl->plugin_instance);
1055 DEBUG ("network plugin: add_to_buffer: plugin_instance = %s",
1056 vl->plugin_instance);
1059 if (strcmp (type_def, ds->type) != 0)
1061 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
1062 ds->type, strlen (ds->type)) != 0)
1064 strcpy (type_def, ds->type);
1065 DEBUG ("network plugin: add_to_buffer: type = %s", ds->type);
1068 if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
1070 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE_INSTANCE,
1072 strlen (vl->type_instance)) != 0)
1074 strcpy (vl_def->type_instance, vl->type_instance);
1075 DEBUG ("network plugin: add_to_buffer: type_instance = %s",
1079 if (write_part_values (&buffer, &buffer_size, ds, vl) != 0)
1082 return (buffer - buffer_orig);
1083 } /* int add_to_buffer */
1085 static void flush_buffer (void)
1087 DEBUG ("network plugin: flush_buffer: send_buffer_fill = %i",
1090 network_send_buffer (send_buffer, send_buffer_fill);
1091 send_buffer_ptr = send_buffer;
1092 send_buffer_fill = 0;
1093 memset (&send_buffer_vl, '\0', sizeof (send_buffer_vl));
1094 memset (send_buffer_type, '\0', sizeof (send_buffer_type));
1097 static int network_write (const data_set_t *ds, const value_list_t *vl)
1101 /* If the value is already in the cache, we have received it via the
1102 * network. We write it again if forwarding is activated. It's then in
1103 * the cache and should we receive it again we will ignore it. */
1104 status = cache_check (ds->type, vl);
1105 if ((network_config_forward == 0)
1109 pthread_mutex_lock (&send_buffer_lock);
1111 status = add_to_buffer (send_buffer_ptr,
1112 sizeof (send_buffer) - send_buffer_fill,
1113 &send_buffer_vl, send_buffer_type,
1117 /* status == bytes added to the buffer */
1118 send_buffer_fill += status;
1119 send_buffer_ptr += status;
1125 status = add_to_buffer (send_buffer_ptr,
1126 sizeof (send_buffer) - send_buffer_fill,
1127 &send_buffer_vl, send_buffer_type,
1132 send_buffer_fill += status;
1133 send_buffer_ptr += status;
1139 ERROR ("network plugin: Unable to append to the "
1140 "buffer for some weird reason");
1142 else if ((sizeof (send_buffer) - send_buffer_fill) < 15)
1147 pthread_mutex_unlock (&send_buffer_lock);
1149 return ((status < 0) ? -1 : 0);
1150 } /* int network_write */
1152 static int network_config (const char *key, const char *val)
1160 if ((strcasecmp ("Listen", key) == 0)
1161 || (strcasecmp ("Server", key) == 0))
1163 char *val_cpy = strdup (val);
1164 if (val_cpy == NULL)
1167 service = NET_DEFAULT_PORT;
1168 fields_num = strsplit (val_cpy, fields, 3);
1169 if ((fields_num != 1)
1170 && (fields_num != 2))
1172 else if (fields_num == 2)
1173 service = fields[1];
1176 if (strcasecmp ("Listen", key) == 0)
1177 network_add_listen_socket (node, service);
1179 network_add_sending_socket (node, service);
1181 else if (strcasecmp ("TimeToLive", key) == 0)
1183 int tmp = atoi (val);
1184 if ((tmp > 0) && (tmp < 256))
1185 network_config_ttl = tmp;
1189 else if (strcasecmp ("Forward", key) == 0)
1191 if ((strcasecmp ("true", val) == 0)
1192 || (strcasecmp ("yes", val) == 0)
1193 || (strcasecmp ("on", val) == 0))
1194 network_config_forward = 1;
1196 network_config_forward = 0;
1205 static int network_shutdown (void)
1207 DEBUG ("Shutting down.");
1211 if (listen_thread != (pthread_t) 0)
1213 pthread_kill (listen_thread, SIGTERM);
1214 pthread_join (listen_thread, NULL /* no return value */);
1215 listen_thread = (pthread_t) 0;
1220 if (cache_tree != NULL)
1225 while (avl_pick (cache_tree, &key, &value) == 0)
1230 avl_destroy (cache_tree);
1234 /* TODO: Close `sending_sockets' */
1236 plugin_unregister_config ("network");
1237 plugin_unregister_init ("network");
1238 plugin_unregister_write ("network");
1239 plugin_unregister_shutdown ("network");
1242 } /* int network_shutdown */
1244 static int network_init (void)
1246 plugin_register_shutdown ("network", network_shutdown);
1248 send_buffer_ptr = send_buffer;
1249 send_buffer_fill = 0;
1250 memset (&send_buffer_vl, '\0', sizeof (send_buffer_vl));
1251 memset (send_buffer_type, '\0', sizeof (send_buffer_type));
1253 cache_tree = avl_create ((int (*) (const void *, const void *)) strcmp);
1255 /* setup socket(s) and so on */
1256 if (sending_sockets != NULL)
1257 plugin_register_write ("network", network_write);
1259 if ((listen_sockets_num != 0) && (listen_thread == 0))
1263 status = pthread_create (&listen_thread, NULL /* no attributes */,
1264 receive_thread, NULL /* no argument */);
1269 ERROR ("network: pthread_create failed: %s",
1270 sstrerror (errno, errbuf,
1275 } /* int network_init */
1277 void module_register (void)
1279 plugin_register_config ("network", network_config,
1280 config_keys, config_keys_num);
1281 plugin_register_init ("network", network_init);