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_debug.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 = 3;
151 static int network_config_ttl = 0;
153 static sockent_t *sending_sockets = NULL;
155 static struct pollfd *listen_sockets = NULL;
156 static int listen_sockets_num = 0;
157 static pthread_t listen_thread = 0;
158 static int listen_loop = 0;
160 static char send_buffer[BUFF_SIZE];
161 static char *send_buffer_ptr;
162 static int send_buffer_fill;
163 static value_list_t send_buffer_vl = VALUE_LIST_INIT;
164 static char send_buffer_type[DATA_MAX_NAME_LEN];
169 static int write_part_values (char **ret_buffer, int *ret_buffer_len,
170 const data_set_t *ds, const value_list_t *vl)
175 i = 6 + (9 * vl->values_len);
176 if (*ret_buffer_len < i)
178 *ret_buffer_len -= i;
180 pv.head = (part_header_t *) *ret_buffer;
181 pv.num_values = (uint16_t *) (pv.head + 1);
182 pv.values_types = (uint8_t *) (pv.num_values + 1);
183 pv.values = (value_t *) (pv.values_types + vl->values_len);
184 *ret_buffer = (void *) (pv.values + vl->values_len);
186 pv.head->type = htons (TYPE_VALUES);
187 pv.head->length = htons (6 + (9 * vl->values_len));
188 *pv.num_values = htons ((uint16_t) vl->values_len);
190 for (i = 0; i < vl->values_len; i++)
192 if (ds->ds[i].type == DS_TYPE_COUNTER)
194 pv.values_types[i] = DS_TYPE_COUNTER;
195 pv.values[i].counter = htonll (vl->values[i].counter);
199 pv.values_types[i] = DS_TYPE_GAUGE;
200 pv.values[i].gauge = vl->values[i].gauge;
205 } /* int write_part_values */
207 static int write_part_number (char **ret_buffer, int *ret_buffer_len,
208 int type, uint64_t value)
212 if (*ret_buffer_len < 12)
215 pn.head = (part_header_t *) *ret_buffer;
216 pn.value = (uint64_t *) (pn.head + 1);
218 pn.head->type = htons (type);
219 pn.head->length = htons (12);
220 *pn.value = htonll (value);
222 *ret_buffer = (char *) (pn.value + 1);
223 *ret_buffer_len -= 12;
226 } /* int write_part_number */
228 static int write_part_string (char **ret_buffer, int *ret_buffer_len,
229 int type, const char *str, int str_len)
234 len = 4 + str_len + 1;
235 if (*ret_buffer_len < len)
237 *ret_buffer_len -= len;
239 ps.head = (part_header_t *) *ret_buffer;
240 ps.value = (char *) (ps.head + 1);
242 ps.head->type = htons ((uint16_t) type);
243 ps.head->length = htons ((uint16_t) str_len + 5);
245 memcpy (ps.value, str, str_len);
246 ps.value[str_len] = '\0';
247 *ret_buffer = (void *) (ps.value + (str_len + 1));
250 } /* int write_part_string */
252 static int parse_part_values (void **ret_buffer, int *ret_buffer_len,
253 value_t **ret_values, int *ret_num_values)
255 char *buffer = *ret_buffer;
256 int buffer_len = *ret_buffer_len;
264 if (buffer_len < (15))
266 DBG ("packet is too short: buffer_len = %i", buffer_len);
270 pv.head = (part_header_t *) buffer;
271 h_length = ntohs (pv.head->length);
272 h_type = ntohs (pv.head->type);
274 assert (h_type == TYPE_VALUES);
276 pv.num_values = (uint16_t *) (pv.head + 1);
277 h_num = ntohs (*pv.num_values);
279 if (h_num != ((h_length - 6) / 9))
281 DBG ("`length' and `num of values' don't match");
285 pv.values_types = (uint8_t *) (pv.num_values + 1);
286 pv.values = (value_t *) (pv.values_types + h_num);
288 for (i = 0; i < h_num; i++)
289 if (pv.values_types[i] == DS_TYPE_COUNTER)
290 pv.values[i].counter = ntohll (pv.values[i].counter);
292 *ret_buffer = (void *) (pv.values + h_num);
293 *ret_buffer_len = buffer_len - h_length;
294 *ret_num_values = h_num;
295 *ret_values = pv.values;
298 } /* int parse_part_values */
300 static int parse_part_number (void **ret_buffer, int *ret_buffer_len,
306 pn.head = (part_header_t *) *ret_buffer;
307 pn.value = (uint64_t *) (pn.head + 1);
309 len = ntohs (pn.head->length);
312 if (len > *ret_buffer_len)
314 *value = ntohll (*pn.value);
316 *ret_buffer = (void *) (pn.value + 1);
317 *ret_buffer_len -= len;
320 } /* int parse_part_number */
322 static int parse_part_string (void **ret_buffer, int *ret_buffer_len,
323 char *output, int output_len)
325 char *buffer = *ret_buffer;
326 int buffer_len = *ret_buffer_len;
332 DBG ("ret_buffer = %p; ret_buffer_len = %i; output = %p; output_len = %i;",
333 *ret_buffer, *ret_buffer_len,
334 (void *) output, output_len);
336 ps.head = (part_header_t *) buffer;
338 h_length = ntohs (ps.head->length);
339 h_type = ntohs (ps.head->type);
341 DBG ("length = %hu; type = %hu;", h_length, h_type);
343 if (buffer_len < h_length)
345 DBG ("packet is too short");
348 assert ((h_type == TYPE_HOST)
349 || (h_type == TYPE_PLUGIN)
350 || (h_type == TYPE_PLUGIN_INSTANCE)
351 || (h_type == TYPE_TYPE)
352 || (h_type == TYPE_TYPE_INSTANCE));
354 ps.value = buffer + 4;
355 if (ps.value[h_length - 5] != '\0')
357 DBG ("String does not end with a nullbyte");
361 if (output_len < (h_length - 4))
363 DBG ("output buffer is too small");
366 strcpy (output, ps.value);
368 DBG ("output = %s", output);
370 *ret_buffer = (void *) (buffer + h_length);
371 *ret_buffer_len = buffer_len - h_length;
374 } /* int parse_part_string */
376 static int parse_packet (void *buffer, int buffer_len)
378 part_header_t *header;
381 value_list_t vl = VALUE_LIST_INIT;
382 char type[DATA_MAX_NAME_LEN];
384 DBG ("buffer = %p; buffer_len = %i;", buffer, buffer_len);
386 memset (&vl, '\0', sizeof (vl));
387 memset (&type, '\0', sizeof (type));
390 while ((status == 0) && (buffer_len > sizeof (part_header_t)))
392 header = (part_header_t *) buffer;
394 if (ntohs (header->length) > buffer_len)
397 if (header->type == htons (TYPE_VALUES))
399 status = parse_part_values (&buffer, &buffer_len,
400 &vl.values, &vl.values_len);
404 DBG ("parse_part_values failed.");
409 && (strlen (vl.host) > 0)
410 && (strlen (vl.plugin) > 0)
411 && (strlen (type) > 0))
413 DBG ("dispatching values");
414 plugin_dispatch_values (type, &vl);
418 DBG ("NOT dispatching values");
421 else if (header->type == ntohs (TYPE_TIME))
424 status = parse_part_number (&buffer, &buffer_len, &tmp);
426 vl.time = (time_t) tmp;
428 else if (header->type == ntohs (TYPE_HOST))
430 status = parse_part_string (&buffer, &buffer_len,
431 vl.host, sizeof (vl.host));
433 else if (header->type == ntohs (TYPE_PLUGIN))
435 status = parse_part_string (&buffer, &buffer_len,
436 vl.plugin, sizeof (vl.plugin));
438 else if (header->type == ntohs (TYPE_PLUGIN_INSTANCE))
440 status = parse_part_string (&buffer, &buffer_len,
441 vl.plugin_instance, sizeof (vl.plugin_instance));
443 else if (header->type == ntohs (TYPE_TYPE))
445 status = parse_part_string (&buffer, &buffer_len,
446 type, sizeof (type));
448 else if (header->type == ntohs (TYPE_TYPE_INSTANCE))
450 status = parse_part_string (&buffer, &buffer_len,
451 vl.type_instance, sizeof (vl.type_instance));
455 DBG ("Unknown part type: 0x%0hx", header->type);
456 buffer = ((char *) buffer) + header->length;
458 } /* while (buffer_len > sizeof (part_header_t)) */
461 } /* int parse_packet */
463 static void free_sockent (sockent_t *se)
473 } /* void free_sockent */
476 * int network_set_ttl
478 * Set the `IP_MULTICAST_TTL', `IP_TTL', `IPV6_MULTICAST_HOPS' or
479 * `IPV6_UNICAST_HOPS', depending on which option is applicable.
481 * The `struct addrinfo' is used to destinguish between unicast and multicast
484 static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
486 if ((network_config_ttl < 1) || (network_config_ttl > 255))
489 DBG ("ttl = %i", network_config_ttl);
491 if (ai->ai_family == AF_INET)
493 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
496 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
497 optname = IP_MULTICAST_TTL;
501 if (setsockopt (se->fd, IPPROTO_IP, optname,
503 sizeof (network_config_ttl)) == -1)
505 syslog (LOG_ERR, "setsockopt: %s", strerror (errno));
509 else if (ai->ai_family == AF_INET6)
511 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
512 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
515 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
516 optname = IPV6_MULTICAST_HOPS;
518 optname = IPV6_UNICAST_HOPS;
520 if (setsockopt (se->fd, IPPROTO_IPV6, optname,
522 sizeof (network_config_ttl)) == -1)
524 syslog (LOG_ERR, "setsockopt: %s", strerror (errno));
530 } /* int network_set_ttl */
532 static int network_bind_socket (const sockent_t *se, const struct addrinfo *ai)
536 DBG ("fd = %i; calling `bind'", se->fd);
538 if (bind (se->fd, ai->ai_addr, ai->ai_addrlen) == -1)
540 syslog (LOG_ERR, "bind: %s", strerror (errno));
544 if (ai->ai_family == AF_INET)
546 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
547 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
551 DBG ("fd = %i; IPv4 multicast address found", se->fd);
553 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
554 mreq.imr_interface.s_addr = htonl (INADDR_ANY);
556 if (setsockopt (se->fd, IPPROTO_IP, IP_MULTICAST_LOOP,
557 &loop, sizeof (loop)) == -1)
559 syslog (LOG_ERR, "setsockopt: %s", strerror (errno));
563 if (setsockopt (se->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
564 &mreq, sizeof (mreq)) == -1)
566 syslog (LOG_ERR, "setsockopt: %s", strerror (errno));
571 else if (ai->ai_family == AF_INET6)
573 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
574 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
575 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
577 struct ipv6_mreq mreq;
579 DBG ("fd = %i; IPv6 multicast address found", se->fd);
581 memcpy (&mreq.ipv6mr_multiaddr,
583 sizeof (addr->sin6_addr));
585 /* http://developer.apple.com/documentation/Darwin/Reference/ManPages/man4/ip6.4.html
586 * ipv6mr_interface may be set to zeroes to
587 * choose the default multicast interface or to
588 * the index of a particular multicast-capable
589 * interface if the host is multihomed.
590 * Membership is associ-associated with a
591 * single interface; programs running on
592 * multihomed hosts may need to join the same
593 * group on more than one interface.*/
594 mreq.ipv6mr_interface = 0;
596 if (setsockopt (se->fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
597 &loop, sizeof (loop)) == -1)
599 syslog (LOG_ERR, "setsockopt: %s", strerror (errno));
603 if (setsockopt (se->fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
604 &mreq, sizeof (mreq)) == -1)
606 syslog (LOG_ERR, "setsockopt: %s", strerror (errno));
613 } /* int network_bind_socket */
615 static sockent_t *network_create_socket (const char *node,
619 struct addrinfo ai_hints;
620 struct addrinfo *ai_list, *ai_ptr;
623 sockent_t *se_head = NULL;
624 sockent_t *se_tail = NULL;
626 DBG ("node = %s, service = %s", node, service);
628 memset (&ai_hints, '\0', sizeof (ai_hints));
629 ai_hints.ai_flags = 0;
631 ai_hints.ai_flags |= AI_PASSIVE;
634 ai_hints.ai_flags |= AI_ADDRCONFIG;
636 ai_hints.ai_family = AF_UNSPEC;
637 ai_hints.ai_socktype = SOCK_DGRAM;
638 ai_hints.ai_protocol = IPPROTO_UDP;
640 ai_return = getaddrinfo (node, service, &ai_hints, &ai_list);
643 syslog (LOG_ERR, "getaddrinfo (%s, %s): %s",
644 (node == NULL) ? "(null)" : node,
645 (service == NULL) ? "(null)" : service,
646 (ai_return == EAI_SYSTEM)
648 : gai_strerror (ai_return));
652 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
656 if ((se = (sockent_t *) malloc (sizeof (sockent_t))) == NULL)
658 syslog (LOG_EMERG, "malloc: %s", strerror (errno));
662 if ((se->addr = (struct sockaddr_storage *) malloc (sizeof (struct sockaddr_storage))) == NULL)
664 syslog (LOG_EMERG, "malloc: %s", strerror (errno));
669 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
670 memset (se->addr, '\0', sizeof (struct sockaddr_storage));
671 memcpy (se->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
672 se->addrlen = ai_ptr->ai_addrlen;
674 se->fd = socket (ai_ptr->ai_family,
676 ai_ptr->ai_protocol);
681 syslog (LOG_ERR, "socket: %s", strerror (errno));
689 if (network_bind_socket (se, ai_ptr) != 0)
697 else /* listen == 0 */
699 network_set_ttl (se, ai_ptr);
713 /* We don't open more than one write-socket per node/service pair.. */
718 freeaddrinfo (ai_list);
721 } /* sockent_t *network_create_socket */
723 static sockent_t *network_create_default_socket (int listen)
725 sockent_t *se_ptr = NULL;
726 sockent_t *se_head = NULL;
727 sockent_t *se_tail = NULL;
729 se_ptr = network_create_socket (NET_DEFAULT_V6_ADDR,
730 NET_DEFAULT_PORT, listen);
732 /* Don't send to the same machine in IPv6 and IPv4 if both are available. */
733 if ((listen == 0) && (se_ptr != NULL))
740 while (se_tail->next != NULL)
741 se_tail = se_tail->next;
744 se_ptr = network_create_socket (NET_DEFAULT_V4_ADDR, NET_DEFAULT_PORT, listen);
749 se_tail->next = se_ptr;
751 } /* sockent_t *network_create_default_socket */
753 static int network_add_listen_socket (const char *node, const char *service)
760 service = NET_DEFAULT_PORT;
763 se = network_create_default_socket (1 /* listen == true */);
765 se = network_create_socket (node, service, 1 /* listen == true */);
770 for (se_ptr = se; se_ptr != NULL; se_ptr = se_ptr->next)
773 listen_sockets = (struct pollfd *) realloc (listen_sockets,
774 (listen_sockets_num + se_num)
775 * sizeof (struct pollfd));
777 for (se_ptr = se; se_ptr != NULL; se_ptr = se_ptr->next)
779 listen_sockets[listen_sockets_num].fd = se_ptr->fd;
780 listen_sockets[listen_sockets_num].events = POLLIN | POLLPRI;
781 listen_sockets[listen_sockets_num].revents = 0;
782 listen_sockets_num++;
787 } /* int network_add_listen_socket */
789 static int network_add_sending_socket (const char *node, const char *service)
795 service = NET_DEFAULT_PORT;
798 se = network_create_default_socket (0 /* listen == false */);
800 se = network_create_socket (node, service, 0 /* listen == false */);
805 if (sending_sockets == NULL)
807 sending_sockets = se;
811 for (se_ptr = sending_sockets; se_ptr->next != NULL; se_ptr = se_ptr->next)
816 } /* int network_get_listen_socket */
818 int network_receive (void)
820 char buffer[BUFF_SIZE];
826 if (listen_sockets_num == 0)
827 network_add_listen_socket (NULL, NULL);
829 if (listen_sockets_num == 0)
831 syslog (LOG_ERR, "network: Failed to open a listening socket.");
835 while (listen_loop == 0)
837 status = poll (listen_sockets, listen_sockets_num, -1);
843 syslog (LOG_ERR, "poll failed: %s",
848 for (i = 0; (i < listen_sockets_num) && (status > 0); i++)
850 if ((listen_sockets[i].revents & (POLLIN | POLLPRI)) == 0)
854 buffer_len = recv (listen_sockets[i].fd,
855 buffer, sizeof (buffer),
859 syslog (LOG_ERR, "recv failed: %s", strerror (errno));
863 parse_packet (buffer, buffer_len);
864 } /* for (listen_sockets) */
865 } /* while (listen_loop == 0) */
870 static void *receive_thread (void *arg)
872 return (network_receive () ? (void *) 1 : (void *) 0);
873 } /* void *receive_thread */
875 static void network_send_buffer (const char *buffer, int buffer_len)
880 DBG ("buffer_len = %i", buffer_len);
882 for (se = sending_sockets; se != NULL; se = se->next)
886 status = sendto (se->fd, buffer, buffer_len, 0 /* no flags */,
887 (struct sockaddr *) se->addr, se->addrlen);
892 syslog (LOG_ERR, "network plugin: sendto failed: %s",
899 } /* for (sending_sockets) */
900 } /* void network_send_buffer */
902 static int add_to_buffer (char *buffer, int buffer_size,
903 value_list_t *vl_def, char *type_def,
904 const data_set_t *ds, const value_list_t *vl)
906 if (strcmp (vl_def->host, vl->host) != 0)
908 if (write_part_string (&buffer, &buffer_size, TYPE_HOST,
909 vl->host, strlen (vl->host)) != 0)
911 strcpy (vl_def->host, vl->host);
912 DBG ("host = %s", vl->host);
915 if (vl_def->time != vl->time)
917 if (write_part_number (&buffer, &buffer_size, TYPE_TIME,
918 (uint64_t) vl->time))
920 vl_def->time = vl->time;
921 DBG ("time = %u", (unsigned int) vl->time);
924 if (strcmp (vl_def->plugin, vl->plugin) != 0)
926 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN,
927 vl->plugin, strlen (vl->plugin)) != 0)
929 strcpy (vl_def->plugin, vl->plugin);
930 DBG ("plugin = %s", vl->plugin);
933 if (strcmp (vl_def->plugin_instance, vl->plugin_instance) != 0)
935 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
937 strlen (vl->plugin_instance)) != 0)
939 strcpy (vl_def->plugin_instance, vl->plugin_instance);
940 DBG ("plugin_instance = %s", vl->plugin_instance);
943 if (strcmp (type_def, ds->type) != 0)
945 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
946 ds->type, strlen (ds->type)) != 0)
948 strcpy (type_def, ds->type);
949 DBG ("type = %s", ds->type);
952 if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
954 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
956 strlen (vl->type_instance)) != 0)
958 strcpy (vl_def->type_instance, vl->type_instance);
959 DBG ("type_instance = %s", vl->type_instance);
962 if (write_part_values (&buffer, &buffer_size, ds, vl) != 0)
965 return (buffer_size);
966 } /* int add_to_buffer */
968 static void flush_buffer (void)
970 network_send_buffer (send_buffer, send_buffer_fill);
971 send_buffer_ptr = send_buffer;
972 send_buffer_fill = 0;
973 memset (&send_buffer_vl, '\0', sizeof (send_buffer_vl));
974 memset (send_buffer_type, '\0', sizeof (send_buffer_type));
977 static int network_write (const data_set_t *ds, const value_list_t *vl)
980 /* TODO: lock buffer */
981 status = add_to_buffer (send_buffer_ptr,
982 sizeof (send_buffer) - send_buffer_fill,
983 &send_buffer_vl, send_buffer_type,
987 send_buffer_fill += status;
988 send_buffer_ptr += status;
994 status = add_to_buffer (send_buffer_ptr,
995 sizeof (send_buffer) - send_buffer_fill,
996 &send_buffer_vl, send_buffer_type,
1001 send_buffer_fill += status;
1002 send_buffer_ptr += status;
1008 syslog (LOG_ERR, "network plugin: Unable to append to the "
1009 "buffer for some weird reason");
1011 else if ((sizeof (send_buffer) - send_buffer_fill) < 15)
1015 /* TODO: unlock buffer */
1017 return ((status < 0) ? -1 : 0);
1018 } /* int network_write */
1020 static int network_config (const char *key, const char *val)
1028 if ((strcasecmp ("Listen", key) == 0)
1029 || (strcasecmp ("Server", key) == 0))
1031 char *val_cpy = strdup (val);
1032 if (val_cpy == NULL)
1035 service = NET_DEFAULT_PORT;
1036 fields_num = strsplit (val_cpy, fields, 3);
1037 if ((fields_num != 1)
1038 && (fields_num != 2))
1040 else if (fields_num == 2)
1041 service = fields[1];
1044 if (strcasecmp ("Listen", key) == 0)
1045 network_add_listen_socket (node, service);
1047 network_add_sending_socket (node, service);
1049 else if (strcasecmp ("TimeToLive", key) == 0)
1051 int tmp = atoi (val);
1052 if ((tmp > 0) && (tmp < 256))
1053 network_config_ttl = tmp;
1064 static int network_shutdown (void)
1066 DBG ("Shutting down.");
1070 pthread_kill (listen_thread, SIGTERM);
1071 pthread_join (listen_thread, NULL /* no return value */);
1078 static int network_init (void)
1080 plugin_register_shutdown ("network", network_shutdown);
1082 send_buffer_ptr = send_buffer;
1083 send_buffer_fill = 0;
1084 memset (&send_buffer_vl, '\0', sizeof (send_buffer_vl));
1085 memset (send_buffer_type, '\0', sizeof (send_buffer_type));
1087 /* setup socket(s) and so on */
1088 if (sending_sockets != NULL)
1089 plugin_register_write ("network", network_write);
1091 if ((listen_sockets_num != 0) && (listen_thread == 0))
1095 status = pthread_create (&listen_thread, NULL /* no attributes */,
1096 receive_thread, NULL /* no argument */);
1099 syslog (LOG_ERR, "network: pthread_create failed: %s",
1103 } /* int network_init */
1105 void module_register (void)
1107 plugin_register_config ("network", network_config,
1108 config_keys, config_keys_num);
1109 plugin_register_init ("network", network_init);