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"
33 # include <sys/socket.h>
39 # include <netinet/in.h>
42 # include <arpa/inet.h>
48 /* 1500 - 40 - 8 = Ethernet packet - IPv6 header - UDP header */
49 /* #define BUFF_SIZE 1452 */
51 #ifndef IPV6_ADD_MEMBERSHIP
52 # ifdef IPV6_JOIN_GROUP
53 # define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
55 # error "Neither IP_ADD_MEMBERSHIP nor IPV6_JOIN_GROUP is defined"
57 #endif /* !IP_ADD_MEMBERSHIP */
59 #define BUFF_SIZE 1024
64 typedef struct sockent
67 struct sockaddr_storage *addr;
72 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
73 * 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
74 * +-------+-----------------------+-------------------------------+
76 * +-------+-----------------------+-------------------------------+
83 typedef struct part_header_s part_header_t;
85 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
86 * 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
87 * +-------------------------------+-------------------------------+
89 * +-------------------------------+-------------------------------+
90 * : (Length - 4) Bytes :
91 * +---------------------------------------------------------------+
98 typedef struct part_string_s part_string_t;
100 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
101 * 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
102 * +-------------------------------+-------------------------------+
104 * +-------------------------------+-------------------------------+
105 * : (Length - 4 == 2 || 4 || 8) Bytes :
106 * +---------------------------------------------------------------+
113 typedef struct part_number_s part_number_t;
115 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
116 * 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
117 * +-------------------------------+-------------------------------+
119 * +-------------------------------+---------------+---------------+
120 * ! Num of values ! Type0 ! Type1 !
121 * +-------------------------------+---------------+---------------+
124 * +---------------------------------------------------------------+
127 * +---------------------------------------------------------------+
132 uint16_t *num_values;
133 uint8_t *values_types;
136 typedef struct part_values_s part_values_t;
141 static const char *config_keys[] =
148 static int config_keys_num = 3;
150 static int network_config_ttl = 0;
152 static sockent_t *sending_sockets = NULL;
154 static struct pollfd *listen_sockets = NULL;
155 static int listen_sockets_num = 0;
156 static pthread_t listen_thread = 0;
157 static int listen_loop = 0;
159 static char send_buffer[BUFF_SIZE];
160 static char *send_buffer_ptr;
161 static int send_buffer_fill;
162 static value_list_t send_buffer_vl = VALUE_LIST_INIT;
163 static char send_buffer_type[DATA_MAX_NAME_LEN];
168 static int write_part_values (char **ret_buffer, int *ret_buffer_len,
169 const data_set_t *ds, const value_list_t *vl)
174 i = 6 + (9 * vl->values_len);
175 if (*ret_buffer_len < i)
177 *ret_buffer_len -= i;
179 pv.head = (part_header_t *) *ret_buffer;
180 pv.num_values = (uint16_t *) (pv.head + 1);
181 pv.values_types = (uint8_t *) (pv.num_values + 1);
182 pv.values = (value_t *) (pv.values_types + vl->values_len);
183 *ret_buffer = (void *) (pv.values + vl->values_len);
185 pv.head->type = htons (TYPE_VALUES);
186 pv.head->length = htons (6 + (9 * vl->values_len));
187 *pv.num_values = htons ((uint16_t) vl->values_len);
189 for (i = 0; i < vl->values_len; i++)
191 if (ds->ds[i].type == DS_TYPE_COUNTER)
193 pv.values_types[i] = DS_TYPE_COUNTER;
194 pv.values[i].counter = htonll (vl->values[i].counter);
198 pv.values_types[i] = DS_TYPE_GAUGE;
199 pv.values[i].gauge = vl->values[i].gauge;
204 } /* int write_part_values */
206 static int write_part_number (char **ret_buffer, int *ret_buffer_len,
207 int type, uint64_t value)
211 if (*ret_buffer_len < 12)
214 pn.head = (part_header_t *) *ret_buffer;
215 pn.value = (uint64_t *) (pn.head + 1);
217 pn.head->type = htons (type);
218 pn.head->length = htons (12);
219 *pn.value = htonll (value);
221 *ret_buffer = (char *) (pn.value + 1);
222 *ret_buffer_len -= 12;
225 } /* int write_part_number */
227 static int write_part_string (char **ret_buffer, int *ret_buffer_len,
228 int type, const char *str, int str_len)
233 len = 4 + str_len + 1;
234 if (*ret_buffer_len < len)
236 *ret_buffer_len -= len;
238 ps.head = (part_header_t *) *ret_buffer;
239 ps.value = (char *) (ps.head + 1);
241 ps.head->type = htons ((uint16_t) type);
242 ps.head->length = htons ((uint16_t) str_len + 5);
244 memcpy (ps.value, str, str_len);
245 ps.value[str_len] = '\0';
246 *ret_buffer = (void *) (ps.value + (str_len + 1));
249 } /* int write_part_string */
251 static int parse_part_values (void **ret_buffer, int *ret_buffer_len,
252 value_t **ret_values, int *ret_num_values)
254 char *buffer = *ret_buffer;
255 int buffer_len = *ret_buffer_len;
263 if (buffer_len < (15))
265 DEBUG ("packet is too short: buffer_len = %i", buffer_len);
269 pv.head = (part_header_t *) buffer;
270 h_length = ntohs (pv.head->length);
271 h_type = ntohs (pv.head->type);
273 assert (h_type == TYPE_VALUES);
275 pv.num_values = (uint16_t *) (pv.head + 1);
276 h_num = ntohs (*pv.num_values);
278 if (h_num != ((h_length - 6) / 9))
280 DEBUG ("`length' and `num of values' don't match");
284 pv.values_types = (uint8_t *) (pv.num_values + 1);
285 pv.values = (value_t *) (pv.values_types + h_num);
287 for (i = 0; i < h_num; i++)
288 if (pv.values_types[i] == DS_TYPE_COUNTER)
289 pv.values[i].counter = ntohll (pv.values[i].counter);
291 *ret_buffer = (void *) (pv.values + h_num);
292 *ret_buffer_len = buffer_len - h_length;
293 *ret_num_values = h_num;
294 *ret_values = pv.values;
297 } /* int parse_part_values */
299 static int parse_part_number (void **ret_buffer, int *ret_buffer_len,
305 pn.head = (part_header_t *) *ret_buffer;
306 pn.value = (uint64_t *) (pn.head + 1);
308 len = ntohs (pn.head->length);
311 if (len > *ret_buffer_len)
313 *value = ntohll (*pn.value);
315 *ret_buffer = (void *) (pn.value + 1);
316 *ret_buffer_len -= len;
319 } /* int parse_part_number */
321 static int parse_part_string (void **ret_buffer, int *ret_buffer_len,
322 char *output, int output_len)
324 char *buffer = *ret_buffer;
325 int buffer_len = *ret_buffer_len;
331 DEBUG ("ret_buffer = %p; ret_buffer_len = %i; output = %p; output_len = %i;",
332 *ret_buffer, *ret_buffer_len,
333 (void *) output, output_len);
335 ps.head = (part_header_t *) buffer;
337 h_length = ntohs (ps.head->length);
338 h_type = ntohs (ps.head->type);
340 DEBUG ("length = %hu; type = %hu;", h_length, h_type);
342 if (buffer_len < h_length)
344 DEBUG ("packet is too short");
347 assert ((h_type == TYPE_HOST)
348 || (h_type == TYPE_PLUGIN)
349 || (h_type == TYPE_PLUGIN_INSTANCE)
350 || (h_type == TYPE_TYPE)
351 || (h_type == TYPE_TYPE_INSTANCE));
353 ps.value = buffer + 4;
354 if (ps.value[h_length - 5] != '\0')
356 DEBUG ("String does not end with a nullbyte");
360 if (output_len < (h_length - 4))
362 DEBUG ("output buffer is too small");
365 strcpy (output, ps.value);
367 DEBUG ("output = %s", output);
369 *ret_buffer = (void *) (buffer + h_length);
370 *ret_buffer_len = buffer_len - h_length;
373 } /* int parse_part_string */
375 static int parse_packet (void *buffer, int buffer_len)
377 part_header_t *header;
380 value_list_t vl = VALUE_LIST_INIT;
381 char type[DATA_MAX_NAME_LEN];
383 DEBUG ("buffer = %p; buffer_len = %i;", buffer, buffer_len);
385 memset (&vl, '\0', sizeof (vl));
386 memset (&type, '\0', sizeof (type));
389 while ((status == 0) && (buffer_len > sizeof (part_header_t)))
391 header = (part_header_t *) buffer;
393 if (ntohs (header->length) > buffer_len)
396 if (header->type == htons (TYPE_VALUES))
398 status = parse_part_values (&buffer, &buffer_len,
399 &vl.values, &vl.values_len);
403 DEBUG ("parse_part_values failed.");
408 && (strlen (vl.host) > 0)
409 && (strlen (vl.plugin) > 0)
410 && (strlen (type) > 0))
412 DEBUG ("dispatching values");
413 plugin_dispatch_values (type, &vl);
417 DEBUG ("NOT dispatching values");
420 else if (header->type == ntohs (TYPE_TIME))
423 status = parse_part_number (&buffer, &buffer_len, &tmp);
425 vl.time = (time_t) tmp;
427 else if (header->type == ntohs (TYPE_HOST))
429 status = parse_part_string (&buffer, &buffer_len,
430 vl.host, sizeof (vl.host));
432 else if (header->type == ntohs (TYPE_PLUGIN))
434 status = parse_part_string (&buffer, &buffer_len,
435 vl.plugin, sizeof (vl.plugin));
437 else if (header->type == ntohs (TYPE_PLUGIN_INSTANCE))
439 status = parse_part_string (&buffer, &buffer_len,
440 vl.plugin_instance, sizeof (vl.plugin_instance));
442 else if (header->type == ntohs (TYPE_TYPE))
444 status = parse_part_string (&buffer, &buffer_len,
445 type, sizeof (type));
447 else if (header->type == ntohs (TYPE_TYPE_INSTANCE))
449 status = parse_part_string (&buffer, &buffer_len,
450 vl.type_instance, sizeof (vl.type_instance));
454 DEBUG ("Unknown part type: 0x%0hx", header->type);
455 buffer = ((char *) buffer) + header->length;
457 } /* while (buffer_len > sizeof (part_header_t)) */
460 } /* int parse_packet */
462 static void free_sockent (sockent_t *se)
472 } /* void free_sockent */
475 * int network_set_ttl
477 * Set the `IP_MULTICAST_TTL', `IP_TTL', `IPV6_MULTICAST_HOPS' or
478 * `IPV6_UNICAST_HOPS', depending on which option is applicable.
480 * The `struct addrinfo' is used to destinguish between unicast and multicast
483 static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
485 if ((network_config_ttl < 1) || (network_config_ttl > 255))
488 DEBUG ("ttl = %i", network_config_ttl);
490 if (ai->ai_family == AF_INET)
492 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
495 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
496 optname = IP_MULTICAST_TTL;
500 if (setsockopt (se->fd, IPPROTO_IP, optname,
502 sizeof (network_config_ttl)) == -1)
504 ERROR ("setsockopt: %s", strerror (errno));
508 else if (ai->ai_family == AF_INET6)
510 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
511 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
514 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
515 optname = IPV6_MULTICAST_HOPS;
517 optname = IPV6_UNICAST_HOPS;
519 if (setsockopt (se->fd, IPPROTO_IPV6, optname,
521 sizeof (network_config_ttl)) == -1)
523 ERROR ("setsockopt: %s", strerror (errno));
529 } /* int network_set_ttl */
531 static int network_bind_socket (const sockent_t *se, const struct addrinfo *ai)
535 DEBUG ("fd = %i; calling `bind'", se->fd);
537 if (bind (se->fd, ai->ai_addr, ai->ai_addrlen) == -1)
539 ERROR ("bind: %s", strerror (errno));
543 if (ai->ai_family == AF_INET)
545 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
546 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
550 DEBUG ("fd = %i; IPv4 multicast address found", se->fd);
552 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
553 mreq.imr_interface.s_addr = htonl (INADDR_ANY);
555 if (setsockopt (se->fd, IPPROTO_IP, IP_MULTICAST_LOOP,
556 &loop, sizeof (loop)) == -1)
558 ERROR ("setsockopt: %s", strerror (errno));
562 if (setsockopt (se->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
563 &mreq, sizeof (mreq)) == -1)
565 ERROR ("setsockopt: %s", strerror (errno));
570 else if (ai->ai_family == AF_INET6)
572 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
573 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
574 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
576 struct ipv6_mreq mreq;
578 DEBUG ("fd = %i; IPv6 multicast address found", se->fd);
580 memcpy (&mreq.ipv6mr_multiaddr,
582 sizeof (addr->sin6_addr));
584 /* http://developer.apple.com/documentation/Darwin/Reference/ManPages/man4/ip6.4.html
585 * ipv6mr_interface may be set to zeroes to
586 * choose the default multicast interface or to
587 * the index of a particular multicast-capable
588 * interface if the host is multihomed.
589 * Membership is associ-associated with a
590 * single interface; programs running on
591 * multihomed hosts may need to join the same
592 * group on more than one interface.*/
593 mreq.ipv6mr_interface = 0;
595 if (setsockopt (se->fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
596 &loop, sizeof (loop)) == -1)
598 ERROR ("setsockopt: %s", strerror (errno));
602 if (setsockopt (se->fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
603 &mreq, sizeof (mreq)) == -1)
605 ERROR ("setsockopt: %s", strerror (errno));
612 } /* int network_bind_socket */
614 static sockent_t *network_create_socket (const char *node,
618 struct addrinfo ai_hints;
619 struct addrinfo *ai_list, *ai_ptr;
622 sockent_t *se_head = NULL;
623 sockent_t *se_tail = NULL;
625 DEBUG ("node = %s, service = %s", node, service);
627 memset (&ai_hints, '\0', sizeof (ai_hints));
628 ai_hints.ai_flags = 0;
630 ai_hints.ai_flags |= AI_PASSIVE;
633 ai_hints.ai_flags |= AI_ADDRCONFIG;
635 ai_hints.ai_family = AF_UNSPEC;
636 ai_hints.ai_socktype = SOCK_DGRAM;
637 ai_hints.ai_protocol = IPPROTO_UDP;
639 ai_return = getaddrinfo (node, service, &ai_hints, &ai_list);
642 ERROR ("getaddrinfo (%s, %s): %s",
643 (node == NULL) ? "(null)" : node,
644 (service == NULL) ? "(null)" : service,
645 (ai_return == EAI_SYSTEM)
647 : gai_strerror (ai_return));
651 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
655 if ((se = (sockent_t *) malloc (sizeof (sockent_t))) == NULL)
657 ERROR ("malloc: %s", strerror (errno));
661 if ((se->addr = (struct sockaddr_storage *) malloc (sizeof (struct sockaddr_storage))) == NULL)
663 ERROR ("malloc: %s", strerror (errno));
668 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
669 memset (se->addr, '\0', sizeof (struct sockaddr_storage));
670 memcpy (se->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
671 se->addrlen = ai_ptr->ai_addrlen;
673 se->fd = socket (ai_ptr->ai_family,
675 ai_ptr->ai_protocol);
680 ERROR ("socket: %s", strerror (errno));
688 if (network_bind_socket (se, ai_ptr) != 0)
696 else /* listen == 0 */
698 network_set_ttl (se, ai_ptr);
712 /* We don't open more than one write-socket per node/service pair.. */
717 freeaddrinfo (ai_list);
720 } /* sockent_t *network_create_socket */
722 static sockent_t *network_create_default_socket (int listen)
724 sockent_t *se_ptr = NULL;
725 sockent_t *se_head = NULL;
726 sockent_t *se_tail = NULL;
728 se_ptr = network_create_socket (NET_DEFAULT_V6_ADDR,
729 NET_DEFAULT_PORT, listen);
731 /* Don't send to the same machine in IPv6 and IPv4 if both are available. */
732 if ((listen == 0) && (se_ptr != NULL))
739 while (se_tail->next != NULL)
740 se_tail = se_tail->next;
743 se_ptr = network_create_socket (NET_DEFAULT_V4_ADDR, NET_DEFAULT_PORT, listen);
748 se_tail->next = se_ptr;
750 } /* sockent_t *network_create_default_socket */
752 static int network_add_listen_socket (const char *node, const char *service)
759 service = NET_DEFAULT_PORT;
762 se = network_create_default_socket (1 /* listen == true */);
764 se = network_create_socket (node, service, 1 /* listen == true */);
769 for (se_ptr = se; se_ptr != NULL; se_ptr = se_ptr->next)
772 listen_sockets = (struct pollfd *) realloc (listen_sockets,
773 (listen_sockets_num + se_num)
774 * sizeof (struct pollfd));
776 for (se_ptr = se; se_ptr != NULL; se_ptr = se_ptr->next)
778 listen_sockets[listen_sockets_num].fd = se_ptr->fd;
779 listen_sockets[listen_sockets_num].events = POLLIN | POLLPRI;
780 listen_sockets[listen_sockets_num].revents = 0;
781 listen_sockets_num++;
786 } /* int network_add_listen_socket */
788 static int network_add_sending_socket (const char *node, const char *service)
794 service = NET_DEFAULT_PORT;
797 se = network_create_default_socket (0 /* listen == false */);
799 se = network_create_socket (node, service, 0 /* listen == false */);
804 if (sending_sockets == NULL)
806 sending_sockets = se;
810 for (se_ptr = sending_sockets; se_ptr->next != NULL; se_ptr = se_ptr->next)
815 } /* int network_get_listen_socket */
817 int network_receive (void)
819 char buffer[BUFF_SIZE];
825 if (listen_sockets_num == 0)
826 network_add_listen_socket (NULL, NULL);
828 if (listen_sockets_num == 0)
830 ERROR ("network: Failed to open a listening socket.");
834 while (listen_loop == 0)
836 status = poll (listen_sockets, listen_sockets_num, -1);
842 ERROR ("poll failed: %s",
847 for (i = 0; (i < listen_sockets_num) && (status > 0); i++)
849 if ((listen_sockets[i].revents & (POLLIN | POLLPRI)) == 0)
853 buffer_len = recv (listen_sockets[i].fd,
854 buffer, sizeof (buffer),
858 ERROR ("recv failed: %s", strerror (errno));
862 parse_packet (buffer, buffer_len);
863 } /* for (listen_sockets) */
864 } /* while (listen_loop == 0) */
869 static void *receive_thread (void *arg)
871 return (network_receive () ? (void *) 1 : (void *) 0);
872 } /* void *receive_thread */
874 static void network_send_buffer (const char *buffer, int buffer_len)
879 DEBUG ("buffer_len = %i", buffer_len);
881 for (se = sending_sockets; se != NULL; se = se->next)
885 status = sendto (se->fd, buffer, buffer_len, 0 /* no flags */,
886 (struct sockaddr *) se->addr, se->addrlen);
891 ERROR ("network plugin: sendto failed: %s",
898 } /* for (sending_sockets) */
899 } /* void network_send_buffer */
901 static int add_to_buffer (char *buffer, int buffer_size,
902 value_list_t *vl_def, char *type_def,
903 const data_set_t *ds, const value_list_t *vl)
905 if (strcmp (vl_def->host, vl->host) != 0)
907 if (write_part_string (&buffer, &buffer_size, TYPE_HOST,
908 vl->host, strlen (vl->host)) != 0)
910 strcpy (vl_def->host, vl->host);
911 DEBUG ("host = %s", vl->host);
914 if (vl_def->time != vl->time)
916 if (write_part_number (&buffer, &buffer_size, TYPE_TIME,
917 (uint64_t) vl->time))
919 vl_def->time = vl->time;
920 DEBUG ("time = %u", (unsigned int) vl->time);
923 if (strcmp (vl_def->plugin, vl->plugin) != 0)
925 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN,
926 vl->plugin, strlen (vl->plugin)) != 0)
928 strcpy (vl_def->plugin, vl->plugin);
929 DEBUG ("plugin = %s", vl->plugin);
932 if (strcmp (vl_def->plugin_instance, vl->plugin_instance) != 0)
934 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
936 strlen (vl->plugin_instance)) != 0)
938 strcpy (vl_def->plugin_instance, vl->plugin_instance);
939 DEBUG ("plugin_instance = %s", vl->plugin_instance);
942 if (strcmp (type_def, ds->type) != 0)
944 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
945 ds->type, strlen (ds->type)) != 0)
947 strcpy (type_def, ds->type);
948 DEBUG ("type = %s", ds->type);
951 if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
953 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
955 strlen (vl->type_instance)) != 0)
957 strcpy (vl_def->type_instance, vl->type_instance);
958 DEBUG ("type_instance = %s", vl->type_instance);
961 if (write_part_values (&buffer, &buffer_size, ds, vl) != 0)
964 return (buffer_size);
965 } /* int add_to_buffer */
967 static void flush_buffer (void)
969 network_send_buffer (send_buffer, send_buffer_fill);
970 send_buffer_ptr = send_buffer;
971 send_buffer_fill = 0;
972 memset (&send_buffer_vl, '\0', sizeof (send_buffer_vl));
973 memset (send_buffer_type, '\0', sizeof (send_buffer_type));
976 static int network_write (const data_set_t *ds, const value_list_t *vl)
979 /* TODO: lock buffer */
980 status = add_to_buffer (send_buffer_ptr,
981 sizeof (send_buffer) - send_buffer_fill,
982 &send_buffer_vl, send_buffer_type,
986 send_buffer_fill += status;
987 send_buffer_ptr += status;
993 status = add_to_buffer (send_buffer_ptr,
994 sizeof (send_buffer) - send_buffer_fill,
995 &send_buffer_vl, send_buffer_type,
1000 send_buffer_fill += status;
1001 send_buffer_ptr += status;
1007 ERROR ("network plugin: Unable to append to the "
1008 "buffer for some weird reason");
1010 else if ((sizeof (send_buffer) - send_buffer_fill) < 15)
1014 /* TODO: unlock buffer */
1016 return ((status < 0) ? -1 : 0);
1017 } /* int network_write */
1019 static int network_config (const char *key, const char *val)
1027 if ((strcasecmp ("Listen", key) == 0)
1028 || (strcasecmp ("Server", key) == 0))
1030 char *val_cpy = strdup (val);
1031 if (val_cpy == NULL)
1034 service = NET_DEFAULT_PORT;
1035 fields_num = strsplit (val_cpy, fields, 3);
1036 if ((fields_num != 1)
1037 && (fields_num != 2))
1039 else if (fields_num == 2)
1040 service = fields[1];
1043 if (strcasecmp ("Listen", key) == 0)
1044 network_add_listen_socket (node, service);
1046 network_add_sending_socket (node, service);
1048 else if (strcasecmp ("TimeToLive", key) == 0)
1050 int tmp = atoi (val);
1051 if ((tmp > 0) && (tmp < 256))
1052 network_config_ttl = tmp;
1063 static int network_shutdown (void)
1065 DEBUG ("Shutting down.");
1069 pthread_kill (listen_thread, SIGTERM);
1070 pthread_join (listen_thread, NULL /* no return value */);
1077 static int network_init (void)
1079 plugin_register_shutdown ("network", network_shutdown);
1081 send_buffer_ptr = send_buffer;
1082 send_buffer_fill = 0;
1083 memset (&send_buffer_vl, '\0', sizeof (send_buffer_vl));
1084 memset (send_buffer_type, '\0', sizeof (send_buffer_type));
1086 /* setup socket(s) and so on */
1087 if (sending_sockets != NULL)
1088 plugin_register_write ("network", network_write);
1090 if ((listen_sockets_num != 0) && (listen_thread == 0))
1094 status = pthread_create (&listen_thread, NULL /* no attributes */,
1095 receive_thread, NULL /* no argument */);
1098 ERROR ("network: pthread_create failed: %s",
1102 } /* int network_init */
1104 void module_register (void)
1106 plugin_register_config ("network", network_config,
1107 config_keys, config_keys_num);
1108 plugin_register_init ("network", network_init);