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 ("buffer = %p; buffer_len = %i;", buffer, buffer_len);
449 memset (&vl, '\0', sizeof (vl));
450 memset (&type, '\0', sizeof (type));
453 while ((status == 0) && (buffer_len > sizeof (part_header_t)))
455 header = (part_header_t *) buffer;
457 if (ntohs (header->length) > buffer_len)
459 /* Assure that this loop terminates eventually */
460 if (ntohs (header->length) < 4)
463 if (ntohs (header->type) == TYPE_VALUES)
465 status = parse_part_values (&buffer, &buffer_len,
466 &vl.values, &vl.values_len);
470 DEBUG ("parse_part_values failed.");
475 && (strlen (vl.host) > 0)
476 && (strlen (vl.plugin) > 0)
477 && (strlen (type) > 0)
478 && (cache_check (type, &vl) == 0))
480 DEBUG ("dispatching values");
481 plugin_dispatch_values (type, &vl);
485 DEBUG ("NOT dispatching values");
488 else if (ntohs (header->type) == TYPE_TIME)
491 status = parse_part_number (&buffer, &buffer_len, &tmp);
493 vl.time = (time_t) tmp;
495 else if (ntohs (header->type) == TYPE_HOST)
497 status = parse_part_string (&buffer, &buffer_len,
498 vl.host, sizeof (vl.host));
499 DEBUG ("network plugin: parse_packet: vl.host = %s", vl.host);
501 else if (ntohs (header->type) == TYPE_PLUGIN)
503 status = parse_part_string (&buffer, &buffer_len,
504 vl.plugin, sizeof (vl.plugin));
505 DEBUG ("network plugin: parse_packet: vl.plugin = %s", vl.plugin);
507 else if (ntohs (header->type) == TYPE_PLUGIN_INSTANCE)
509 status = parse_part_string (&buffer, &buffer_len,
510 vl.plugin_instance, sizeof (vl.plugin_instance));
511 DEBUG ("network plugin: parse_packet: vl.plugin_instance = %s", vl.plugin_instance);
513 else if (ntohs (header->type) == TYPE_TYPE)
515 status = parse_part_string (&buffer, &buffer_len,
516 type, sizeof (type));
517 DEBUG ("network plugin: parse_packet: type = %s", type);
519 else if (ntohs (header->type) == TYPE_TYPE_INSTANCE)
521 status = parse_part_string (&buffer, &buffer_len,
522 vl.type_instance, sizeof (vl.type_instance));
523 DEBUG ("network plugin: parse_packet: vl.type_instance = %s", vl.type_instance);
527 DEBUG ("Unknown part type: 0x%0hx", ntohs (header->type));
528 buffer = ((char *) buffer) + ntohs (header->length);
530 } /* while (buffer_len > sizeof (part_header_t)) */
533 } /* int parse_packet */
535 static void free_sockent (sockent_t *se)
545 } /* void free_sockent */
548 * int network_set_ttl
550 * Set the `IP_MULTICAST_TTL', `IP_TTL', `IPV6_MULTICAST_HOPS' or
551 * `IPV6_UNICAST_HOPS', depending on which option is applicable.
553 * The `struct addrinfo' is used to destinguish between unicast and multicast
556 static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
558 if ((network_config_ttl < 1) || (network_config_ttl > 255))
561 DEBUG ("ttl = %i", network_config_ttl);
563 if (ai->ai_family == AF_INET)
565 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
568 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
569 optname = IP_MULTICAST_TTL;
573 if (setsockopt (se->fd, IPPROTO_IP, optname,
575 sizeof (network_config_ttl)) == -1)
578 ERROR ("setsockopt: %s",
579 sstrerror (errno, errbuf, sizeof (errbuf)));
583 else if (ai->ai_family == AF_INET6)
585 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
586 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
589 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
590 optname = IPV6_MULTICAST_HOPS;
592 optname = IPV6_UNICAST_HOPS;
594 if (setsockopt (se->fd, IPPROTO_IPV6, optname,
596 sizeof (network_config_ttl)) == -1)
599 ERROR ("setsockopt: %s",
600 sstrerror (errno, errbuf,
607 } /* int network_set_ttl */
609 static int network_bind_socket (const sockent_t *se, const struct addrinfo *ai)
613 DEBUG ("fd = %i; calling `bind'", se->fd);
615 if (bind (se->fd, ai->ai_addr, ai->ai_addrlen) == -1)
619 sstrerror (errno, errbuf, sizeof (errbuf)));
623 if (ai->ai_family == AF_INET)
625 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
626 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
630 DEBUG ("fd = %i; IPv4 multicast address found", se->fd);
632 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
633 mreq.imr_interface.s_addr = htonl (INADDR_ANY);
635 if (setsockopt (se->fd, IPPROTO_IP, IP_MULTICAST_LOOP,
636 &loop, sizeof (loop)) == -1)
639 ERROR ("setsockopt: %s",
640 sstrerror (errno, errbuf,
645 if (setsockopt (se->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
646 &mreq, sizeof (mreq)) == -1)
649 ERROR ("setsockopt: %s",
650 sstrerror (errno, errbuf,
656 else if (ai->ai_family == AF_INET6)
658 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
659 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
660 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
662 struct ipv6_mreq mreq;
664 DEBUG ("fd = %i; IPv6 multicast address found", se->fd);
666 memcpy (&mreq.ipv6mr_multiaddr,
668 sizeof (addr->sin6_addr));
670 /* http://developer.apple.com/documentation/Darwin/Reference/ManPages/man4/ip6.4.html
671 * ipv6mr_interface may be set to zeroes to
672 * choose the default multicast interface or to
673 * the index of a particular multicast-capable
674 * interface if the host is multihomed.
675 * Membership is associ-associated with a
676 * single interface; programs running on
677 * multihomed hosts may need to join the same
678 * group on more than one interface.*/
679 mreq.ipv6mr_interface = 0;
681 if (setsockopt (se->fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
682 &loop, sizeof (loop)) == -1)
685 ERROR ("setsockopt: %s",
686 sstrerror (errno, errbuf,
691 if (setsockopt (se->fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
692 &mreq, sizeof (mreq)) == -1)
695 ERROR ("setsockopt: %s",
696 sstrerror (errno, errbuf,
704 } /* int network_bind_socket */
706 static sockent_t *network_create_socket (const char *node,
710 struct addrinfo ai_hints;
711 struct addrinfo *ai_list, *ai_ptr;
714 sockent_t *se_head = NULL;
715 sockent_t *se_tail = NULL;
717 DEBUG ("node = %s, service = %s", node, service);
719 memset (&ai_hints, '\0', sizeof (ai_hints));
720 ai_hints.ai_flags = 0;
722 ai_hints.ai_flags |= AI_PASSIVE;
725 ai_hints.ai_flags |= AI_ADDRCONFIG;
727 ai_hints.ai_family = AF_UNSPEC;
728 ai_hints.ai_socktype = SOCK_DGRAM;
729 ai_hints.ai_protocol = IPPROTO_UDP;
731 ai_return = getaddrinfo (node, service, &ai_hints, &ai_list);
735 ERROR ("getaddrinfo (%s, %s): %s",
736 (node == NULL) ? "(null)" : node,
737 (service == NULL) ? "(null)" : service,
738 (ai_return == EAI_SYSTEM)
739 ? sstrerror (errno, errbuf, sizeof (errbuf))
740 : gai_strerror (ai_return));
744 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
748 if ((se = (sockent_t *) malloc (sizeof (sockent_t))) == NULL)
752 sstrerror (errno, errbuf,
757 if ((se->addr = (struct sockaddr_storage *) malloc (sizeof (struct sockaddr_storage))) == NULL)
761 sstrerror (errno, errbuf,
767 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
768 memset (se->addr, '\0', sizeof (struct sockaddr_storage));
769 memcpy (se->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
770 se->addrlen = ai_ptr->ai_addrlen;
772 se->fd = socket (ai_ptr->ai_family,
774 ai_ptr->ai_protocol);
781 sstrerror (errno, errbuf,
790 if (network_bind_socket (se, ai_ptr) != 0)
798 else /* listen == 0 */
800 network_set_ttl (se, ai_ptr);
814 /* We don't open more than one write-socket per node/service pair.. */
819 freeaddrinfo (ai_list);
822 } /* sockent_t *network_create_socket */
824 static sockent_t *network_create_default_socket (int listen)
826 sockent_t *se_ptr = NULL;
827 sockent_t *se_head = NULL;
828 sockent_t *se_tail = NULL;
830 se_ptr = network_create_socket (NET_DEFAULT_V6_ADDR,
831 NET_DEFAULT_PORT, listen);
833 /* Don't send to the same machine in IPv6 and IPv4 if both are available. */
834 if ((listen == 0) && (se_ptr != NULL))
841 while (se_tail->next != NULL)
842 se_tail = se_tail->next;
845 se_ptr = network_create_socket (NET_DEFAULT_V4_ADDR, NET_DEFAULT_PORT, listen);
850 se_tail->next = se_ptr;
852 } /* sockent_t *network_create_default_socket */
854 static int network_add_listen_socket (const char *node, const char *service)
861 service = NET_DEFAULT_PORT;
864 se = network_create_default_socket (1 /* listen == true */);
866 se = network_create_socket (node, service, 1 /* listen == true */);
871 for (se_ptr = se; se_ptr != NULL; se_ptr = se_ptr->next)
874 listen_sockets = (struct pollfd *) realloc (listen_sockets,
875 (listen_sockets_num + se_num)
876 * sizeof (struct pollfd));
878 for (se_ptr = se; se_ptr != NULL; se_ptr = se_ptr->next)
880 listen_sockets[listen_sockets_num].fd = se_ptr->fd;
881 listen_sockets[listen_sockets_num].events = POLLIN | POLLPRI;
882 listen_sockets[listen_sockets_num].revents = 0;
883 listen_sockets_num++;
888 } /* int network_add_listen_socket */
890 static int network_add_sending_socket (const char *node, const char *service)
896 service = NET_DEFAULT_PORT;
899 se = network_create_default_socket (0 /* listen == false */);
901 se = network_create_socket (node, service, 0 /* listen == false */);
906 if (sending_sockets == NULL)
908 sending_sockets = se;
912 for (se_ptr = sending_sockets; se_ptr->next != NULL; se_ptr = se_ptr->next)
917 } /* int network_get_listen_socket */
919 int network_receive (void)
921 char buffer[BUFF_SIZE];
927 if (listen_sockets_num == 0)
928 network_add_listen_socket (NULL, NULL);
930 if (listen_sockets_num == 0)
932 ERROR ("network: Failed to open a listening socket.");
936 while (listen_loop == 0)
938 status = poll (listen_sockets, listen_sockets_num, -1);
945 ERROR ("poll failed: %s",
946 sstrerror (errno, errbuf, sizeof (errbuf)));
950 for (i = 0; (i < listen_sockets_num) && (status > 0); i++)
952 if ((listen_sockets[i].revents & (POLLIN | POLLPRI)) == 0)
956 buffer_len = recv (listen_sockets[i].fd,
957 buffer, sizeof (buffer),
962 ERROR ("recv failed: %s",
963 sstrerror (errno, errbuf,
968 parse_packet (buffer, buffer_len);
969 } /* for (listen_sockets) */
970 } /* while (listen_loop == 0) */
975 static void *receive_thread (void *arg)
977 return (network_receive () ? (void *) 1 : (void *) 0);
978 } /* void *receive_thread */
980 static void network_send_buffer (const char *buffer, int buffer_len)
985 DEBUG ("buffer_len = %i", buffer_len);
987 for (se = sending_sockets; se != NULL; se = se->next)
991 status = sendto (se->fd, buffer, buffer_len, 0 /* no flags */,
992 (struct sockaddr *) se->addr, se->addrlen);
998 ERROR ("network plugin: sendto failed: %s",
999 sstrerror (errno, errbuf,
1006 } /* for (sending_sockets) */
1007 } /* void network_send_buffer */
1009 static int add_to_buffer (char *buffer, int buffer_size,
1010 value_list_t *vl_def, char *type_def,
1011 const data_set_t *ds, const value_list_t *vl)
1013 char *buffer_orig = buffer;
1015 if (strcmp (vl_def->host, vl->host) != 0)
1017 if (write_part_string (&buffer, &buffer_size, TYPE_HOST,
1018 vl->host, strlen (vl->host)) != 0)
1020 strcpy (vl_def->host, vl->host);
1021 DEBUG ("network plugin: add_to_buffer: host = %s", vl->host);
1024 if (vl_def->time != vl->time)
1026 if (write_part_number (&buffer, &buffer_size, TYPE_TIME,
1027 (uint64_t) vl->time))
1029 vl_def->time = vl->time;
1030 DEBUG ("network plugin: add_to_buffer: time = %u",
1031 (unsigned int) vl->time);
1034 if (strcmp (vl_def->plugin, vl->plugin) != 0)
1036 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN,
1037 vl->plugin, strlen (vl->plugin)) != 0)
1039 strcpy (vl_def->plugin, vl->plugin);
1040 DEBUG ("network plugin: add_to_buffer: plugin = %s",
1044 if (strcmp (vl_def->plugin_instance, vl->plugin_instance) != 0)
1046 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
1047 vl->plugin_instance,
1048 strlen (vl->plugin_instance)) != 0)
1050 strcpy (vl_def->plugin_instance, vl->plugin_instance);
1051 DEBUG ("network plugin: add_to_buffer: plugin_instance = %s",
1052 vl->plugin_instance);
1055 if (strcmp (type_def, ds->type) != 0)
1057 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
1058 ds->type, strlen (ds->type)) != 0)
1060 strcpy (type_def, ds->type);
1061 DEBUG ("network plugin: add_to_buffer: type = %s", ds->type);
1064 if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
1066 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE_INSTANCE,
1068 strlen (vl->type_instance)) != 0)
1070 strcpy (vl_def->type_instance, vl->type_instance);
1071 DEBUG ("network plugin: add_to_buffer: type_instance = %s",
1075 if (write_part_values (&buffer, &buffer_size, ds, vl) != 0)
1078 return (buffer - buffer_orig);
1079 } /* int add_to_buffer */
1081 static void flush_buffer (void)
1083 network_send_buffer (send_buffer, send_buffer_fill);
1084 send_buffer_ptr = send_buffer;
1085 send_buffer_fill = 0;
1086 memset (&send_buffer_vl, '\0', sizeof (send_buffer_vl));
1087 memset (send_buffer_type, '\0', sizeof (send_buffer_type));
1090 static int network_write (const data_set_t *ds, const value_list_t *vl)
1094 /* If the value is already in the cache, we have received it via the
1095 * network. We write it again if forwarding is activated. It's then in
1096 * the cache and should we receive it again we will ignore it. */
1097 status = cache_check (ds->type, vl);
1098 if ((network_config_forward == 0)
1102 pthread_mutex_lock (&send_buffer_lock);
1104 status = add_to_buffer (send_buffer_ptr,
1105 sizeof (send_buffer) - send_buffer_fill,
1106 &send_buffer_vl, send_buffer_type,
1110 /* status == bytes added to the buffer */
1111 send_buffer_fill += status;
1112 send_buffer_ptr += status;
1118 status = add_to_buffer (send_buffer_ptr,
1119 sizeof (send_buffer) - send_buffer_fill,
1120 &send_buffer_vl, send_buffer_type,
1125 send_buffer_fill += status;
1126 send_buffer_ptr += status;
1132 ERROR ("network plugin: Unable to append to the "
1133 "buffer for some weird reason");
1135 else if ((sizeof (send_buffer) - send_buffer_fill) < 15)
1140 pthread_mutex_unlock (&send_buffer_lock);
1142 return ((status < 0) ? -1 : 0);
1143 } /* int network_write */
1145 static int network_config (const char *key, const char *val)
1153 if ((strcasecmp ("Listen", key) == 0)
1154 || (strcasecmp ("Server", key) == 0))
1156 char *val_cpy = strdup (val);
1157 if (val_cpy == NULL)
1160 service = NET_DEFAULT_PORT;
1161 fields_num = strsplit (val_cpy, fields, 3);
1162 if ((fields_num != 1)
1163 && (fields_num != 2))
1165 else if (fields_num == 2)
1166 service = fields[1];
1169 if (strcasecmp ("Listen", key) == 0)
1170 network_add_listen_socket (node, service);
1172 network_add_sending_socket (node, service);
1174 else if (strcasecmp ("TimeToLive", key) == 0)
1176 int tmp = atoi (val);
1177 if ((tmp > 0) && (tmp < 256))
1178 network_config_ttl = tmp;
1182 else if (strcasecmp ("Forward", key) == 0)
1184 if ((strcasecmp ("true", value) == 0)
1185 || (strcasecmp ("yes", value) == 0)
1186 || (strcasecmp ("on", value) == 0))
1187 network_config_forward = 1;
1189 network_config_forward = 0;
1198 static int network_shutdown (void)
1200 DEBUG ("Shutting down.");
1204 if (listen_thread != (pthread_t) 0)
1206 pthread_kill (listen_thread, SIGTERM);
1207 pthread_join (listen_thread, NULL /* no return value */);
1208 listen_thread = (pthread_t) 0;
1213 if (cache_tree != NULL)
1218 while (avl_pick (cache_tree, &key, &value) == 0)
1223 avl_destroy (cache_tree);
1227 /* TODO: Close `sending_sockets' */
1229 plugin_unregister_config ("network");
1230 plugin_unregister_init ("network");
1231 plugin_unregister_write ("network");
1232 plugin_unregister_shutdown ("network");
1235 } /* int network_shutdown */
1237 static int network_init (void)
1239 plugin_register_shutdown ("network", network_shutdown);
1241 send_buffer_ptr = send_buffer;
1242 send_buffer_fill = 0;
1243 memset (&send_buffer_vl, '\0', sizeof (send_buffer_vl));
1244 memset (send_buffer_type, '\0', sizeof (send_buffer_type));
1246 cache_tree = avl_create ((int (*) (const void *, const void *)) strcmp);
1248 /* setup socket(s) and so on */
1249 if (sending_sockets != NULL)
1250 plugin_register_write ("network", network_write);
1252 if ((listen_sockets_num != 0) && (listen_thread == 0))
1256 status = pthread_create (&listen_thread, NULL /* no attributes */,
1257 receive_thread, NULL /* no argument */);
1262 ERROR ("network: pthread_create failed: %s",
1263 sstrerror (errno, errbuf,
1268 } /* int network_init */
1270 void module_register (void)
1272 plugin_register_config ("network", network_config,
1273 config_keys, config_keys_num);
1274 plugin_register_init ("network", network_init);