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];
164 static pthread_mutex_t send_buffer_lock = PTHREAD_MUTEX_INITIALIZER;
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 DEBUG ("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 DEBUG ("`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 DEBUG ("network plugin: parse_part_string: ret_buffer = %p;"
333 " ret_buffer_len = %i; output = %p; output_len = %i;",
334 *ret_buffer, *ret_buffer_len,
335 (void *) output, output_len);
337 ps.head = (part_header_t *) buffer;
339 h_length = ntohs (ps.head->length);
340 h_type = ntohs (ps.head->type);
342 DEBUG ("network plugin: parse_part_string: length = %hu; type = %hu;",
345 if (buffer_len < h_length)
347 DEBUG ("packet is too short");
350 assert ((h_type == TYPE_HOST)
351 || (h_type == TYPE_PLUGIN)
352 || (h_type == TYPE_PLUGIN_INSTANCE)
353 || (h_type == TYPE_TYPE)
354 || (h_type == TYPE_TYPE_INSTANCE));
356 ps.value = buffer + 4;
357 if (ps.value[h_length - 5] != '\0')
359 DEBUG ("String does not end with a nullbyte");
363 if (output_len < (h_length - 4))
365 DEBUG ("output buffer is too small");
368 strcpy (output, ps.value);
370 DEBUG ("network plugin: parse_part_string: output = %s", output);
372 *ret_buffer = (void *) (buffer + h_length);
373 *ret_buffer_len = buffer_len - h_length;
376 } /* int parse_part_string */
378 static int parse_packet (void *buffer, int buffer_len)
380 part_header_t *header;
383 value_list_t vl = VALUE_LIST_INIT;
384 char type[DATA_MAX_NAME_LEN];
386 DEBUG ("buffer = %p; buffer_len = %i;", buffer, buffer_len);
388 memset (&vl, '\0', sizeof (vl));
389 memset (&type, '\0', sizeof (type));
392 while ((status == 0) && (buffer_len > sizeof (part_header_t)))
394 header = (part_header_t *) buffer;
396 if (ntohs (header->length) > buffer_len)
398 /* Assure that this loop terminates eventually */
399 if (ntohs (header->length) < 4)
402 if (ntohs (header->type) == TYPE_VALUES)
404 status = parse_part_values (&buffer, &buffer_len,
405 &vl.values, &vl.values_len);
409 DEBUG ("parse_part_values failed.");
414 && (strlen (vl.host) > 0)
415 && (strlen (vl.plugin) > 0)
416 && (strlen (type) > 0))
418 DEBUG ("dispatching values");
419 plugin_dispatch_values (type, &vl);
423 DEBUG ("NOT dispatching values");
426 else if (ntohs (header->type) == TYPE_TIME)
429 status = parse_part_number (&buffer, &buffer_len, &tmp);
431 vl.time = (time_t) tmp;
433 else if (ntohs (header->type) == TYPE_HOST)
435 status = parse_part_string (&buffer, &buffer_len,
436 vl.host, sizeof (vl.host));
437 DEBUG ("network plugin: parse_packet: vl.host = %s", vl.host);
439 else if (ntohs (header->type) == TYPE_PLUGIN)
441 status = parse_part_string (&buffer, &buffer_len,
442 vl.plugin, sizeof (vl.plugin));
443 DEBUG ("network plugin: parse_packet: vl.plugin = %s", vl.plugin);
445 else if (ntohs (header->type) == TYPE_PLUGIN_INSTANCE)
447 status = parse_part_string (&buffer, &buffer_len,
448 vl.plugin_instance, sizeof (vl.plugin_instance));
449 DEBUG ("network plugin: parse_packet: vl.plugin_instance = %s", vl.plugin_instance);
451 else if (ntohs (header->type) == TYPE_TYPE)
453 status = parse_part_string (&buffer, &buffer_len,
454 type, sizeof (type));
455 DEBUG ("network plugin: parse_packet: type = %s", type);
457 else if (ntohs (header->type) == TYPE_TYPE_INSTANCE)
459 status = parse_part_string (&buffer, &buffer_len,
460 vl.type_instance, sizeof (vl.type_instance));
461 DEBUG ("network type: parse_packet: vl.type_instance = %s", vl.type_instance);
465 DEBUG ("Unknown part type: 0x%0hx", ntohs (header->type));
466 buffer = ((char *) buffer) + ntohs (header->length);
468 } /* while (buffer_len > sizeof (part_header_t)) */
471 } /* int parse_packet */
473 static void free_sockent (sockent_t *se)
483 } /* void free_sockent */
486 * int network_set_ttl
488 * Set the `IP_MULTICAST_TTL', `IP_TTL', `IPV6_MULTICAST_HOPS' or
489 * `IPV6_UNICAST_HOPS', depending on which option is applicable.
491 * The `struct addrinfo' is used to destinguish between unicast and multicast
494 static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
496 if ((network_config_ttl < 1) || (network_config_ttl > 255))
499 DEBUG ("ttl = %i", network_config_ttl);
501 if (ai->ai_family == AF_INET)
503 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
506 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
507 optname = IP_MULTICAST_TTL;
511 if (setsockopt (se->fd, IPPROTO_IP, optname,
513 sizeof (network_config_ttl)) == -1)
516 ERROR ("setsockopt: %s",
517 sstrerror (errno, errbuf, sizeof (errbuf)));
521 else if (ai->ai_family == AF_INET6)
523 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
524 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
527 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
528 optname = IPV6_MULTICAST_HOPS;
530 optname = IPV6_UNICAST_HOPS;
532 if (setsockopt (se->fd, IPPROTO_IPV6, optname,
534 sizeof (network_config_ttl)) == -1)
537 ERROR ("setsockopt: %s",
538 sstrerror (errno, errbuf,
545 } /* int network_set_ttl */
547 static int network_bind_socket (const sockent_t *se, const struct addrinfo *ai)
551 DEBUG ("fd = %i; calling `bind'", se->fd);
553 if (bind (se->fd, ai->ai_addr, ai->ai_addrlen) == -1)
557 sstrerror (errno, errbuf, sizeof (errbuf)));
561 if (ai->ai_family == AF_INET)
563 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
564 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
568 DEBUG ("fd = %i; IPv4 multicast address found", se->fd);
570 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
571 mreq.imr_interface.s_addr = htonl (INADDR_ANY);
573 if (setsockopt (se->fd, IPPROTO_IP, IP_MULTICAST_LOOP,
574 &loop, sizeof (loop)) == -1)
577 ERROR ("setsockopt: %s",
578 sstrerror (errno, errbuf,
583 if (setsockopt (se->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
584 &mreq, sizeof (mreq)) == -1)
587 ERROR ("setsockopt: %s",
588 sstrerror (errno, errbuf,
594 else if (ai->ai_family == AF_INET6)
596 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
597 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
598 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
600 struct ipv6_mreq mreq;
602 DEBUG ("fd = %i; IPv6 multicast address found", se->fd);
604 memcpy (&mreq.ipv6mr_multiaddr,
606 sizeof (addr->sin6_addr));
608 /* http://developer.apple.com/documentation/Darwin/Reference/ManPages/man4/ip6.4.html
609 * ipv6mr_interface may be set to zeroes to
610 * choose the default multicast interface or to
611 * the index of a particular multicast-capable
612 * interface if the host is multihomed.
613 * Membership is associ-associated with a
614 * single interface; programs running on
615 * multihomed hosts may need to join the same
616 * group on more than one interface.*/
617 mreq.ipv6mr_interface = 0;
619 if (setsockopt (se->fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
620 &loop, sizeof (loop)) == -1)
623 ERROR ("setsockopt: %s",
624 sstrerror (errno, errbuf,
629 if (setsockopt (se->fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
630 &mreq, sizeof (mreq)) == -1)
633 ERROR ("setsockopt: %s",
634 sstrerror (errno, errbuf,
642 } /* int network_bind_socket */
644 static sockent_t *network_create_socket (const char *node,
648 struct addrinfo ai_hints;
649 struct addrinfo *ai_list, *ai_ptr;
652 sockent_t *se_head = NULL;
653 sockent_t *se_tail = NULL;
655 DEBUG ("node = %s, service = %s", node, service);
657 memset (&ai_hints, '\0', sizeof (ai_hints));
658 ai_hints.ai_flags = 0;
660 ai_hints.ai_flags |= AI_PASSIVE;
663 ai_hints.ai_flags |= AI_ADDRCONFIG;
665 ai_hints.ai_family = AF_UNSPEC;
666 ai_hints.ai_socktype = SOCK_DGRAM;
667 ai_hints.ai_protocol = IPPROTO_UDP;
669 ai_return = getaddrinfo (node, service, &ai_hints, &ai_list);
673 ERROR ("getaddrinfo (%s, %s): %s",
674 (node == NULL) ? "(null)" : node,
675 (service == NULL) ? "(null)" : service,
676 (ai_return == EAI_SYSTEM)
677 ? sstrerror (errno, errbuf, sizeof (errbuf))
678 : gai_strerror (ai_return));
682 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
686 if ((se = (sockent_t *) malloc (sizeof (sockent_t))) == NULL)
690 sstrerror (errno, errbuf,
695 if ((se->addr = (struct sockaddr_storage *) malloc (sizeof (struct sockaddr_storage))) == NULL)
699 sstrerror (errno, errbuf,
705 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
706 memset (se->addr, '\0', sizeof (struct sockaddr_storage));
707 memcpy (se->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
708 se->addrlen = ai_ptr->ai_addrlen;
710 se->fd = socket (ai_ptr->ai_family,
712 ai_ptr->ai_protocol);
719 sstrerror (errno, errbuf,
728 if (network_bind_socket (se, ai_ptr) != 0)
736 else /* listen == 0 */
738 network_set_ttl (se, ai_ptr);
752 /* We don't open more than one write-socket per node/service pair.. */
757 freeaddrinfo (ai_list);
760 } /* sockent_t *network_create_socket */
762 static sockent_t *network_create_default_socket (int listen)
764 sockent_t *se_ptr = NULL;
765 sockent_t *se_head = NULL;
766 sockent_t *se_tail = NULL;
768 se_ptr = network_create_socket (NET_DEFAULT_V6_ADDR,
769 NET_DEFAULT_PORT, listen);
771 /* Don't send to the same machine in IPv6 and IPv4 if both are available. */
772 if ((listen == 0) && (se_ptr != NULL))
779 while (se_tail->next != NULL)
780 se_tail = se_tail->next;
783 se_ptr = network_create_socket (NET_DEFAULT_V4_ADDR, NET_DEFAULT_PORT, listen);
788 se_tail->next = se_ptr;
790 } /* sockent_t *network_create_default_socket */
792 static int network_add_listen_socket (const char *node, const char *service)
799 service = NET_DEFAULT_PORT;
802 se = network_create_default_socket (1 /* listen == true */);
804 se = network_create_socket (node, service, 1 /* listen == true */);
809 for (se_ptr = se; se_ptr != NULL; se_ptr = se_ptr->next)
812 listen_sockets = (struct pollfd *) realloc (listen_sockets,
813 (listen_sockets_num + se_num)
814 * sizeof (struct pollfd));
816 for (se_ptr = se; se_ptr != NULL; se_ptr = se_ptr->next)
818 listen_sockets[listen_sockets_num].fd = se_ptr->fd;
819 listen_sockets[listen_sockets_num].events = POLLIN | POLLPRI;
820 listen_sockets[listen_sockets_num].revents = 0;
821 listen_sockets_num++;
826 } /* int network_add_listen_socket */
828 static int network_add_sending_socket (const char *node, const char *service)
834 service = NET_DEFAULT_PORT;
837 se = network_create_default_socket (0 /* listen == false */);
839 se = network_create_socket (node, service, 0 /* listen == false */);
844 if (sending_sockets == NULL)
846 sending_sockets = se;
850 for (se_ptr = sending_sockets; se_ptr->next != NULL; se_ptr = se_ptr->next)
855 } /* int network_get_listen_socket */
857 int network_receive (void)
859 char buffer[BUFF_SIZE];
865 if (listen_sockets_num == 0)
866 network_add_listen_socket (NULL, NULL);
868 if (listen_sockets_num == 0)
870 ERROR ("network: Failed to open a listening socket.");
874 while (listen_loop == 0)
876 status = poll (listen_sockets, listen_sockets_num, -1);
883 ERROR ("poll failed: %s",
884 sstrerror (errno, errbuf, sizeof (errbuf)));
888 for (i = 0; (i < listen_sockets_num) && (status > 0); i++)
890 if ((listen_sockets[i].revents & (POLLIN | POLLPRI)) == 0)
894 buffer_len = recv (listen_sockets[i].fd,
895 buffer, sizeof (buffer),
900 ERROR ("recv failed: %s",
901 sstrerror (errno, errbuf,
906 parse_packet (buffer, buffer_len);
907 } /* for (listen_sockets) */
908 } /* while (listen_loop == 0) */
913 static void *receive_thread (void *arg)
915 return (network_receive () ? (void *) 1 : (void *) 0);
916 } /* void *receive_thread */
918 static void network_send_buffer (const char *buffer, int buffer_len)
923 DEBUG ("buffer_len = %i", buffer_len);
925 for (se = sending_sockets; se != NULL; se = se->next)
929 status = sendto (se->fd, buffer, buffer_len, 0 /* no flags */,
930 (struct sockaddr *) se->addr, se->addrlen);
936 ERROR ("network plugin: sendto failed: %s",
937 sstrerror (errno, errbuf,
944 } /* for (sending_sockets) */
945 } /* void network_send_buffer */
947 static int add_to_buffer (char *buffer, int buffer_size,
948 value_list_t *vl_def, char *type_def,
949 const data_set_t *ds, const value_list_t *vl)
951 char *buffer_orig = buffer;
953 if (strcmp (vl_def->host, vl->host) != 0)
955 if (write_part_string (&buffer, &buffer_size, TYPE_HOST,
956 vl->host, strlen (vl->host)) != 0)
958 strcpy (vl_def->host, vl->host);
959 DEBUG ("host = %s", vl->host);
962 if (vl_def->time != vl->time)
964 if (write_part_number (&buffer, &buffer_size, TYPE_TIME,
965 (uint64_t) vl->time))
967 vl_def->time = vl->time;
968 DEBUG ("time = %u", (unsigned int) vl->time);
971 if (strcmp (vl_def->plugin, vl->plugin) != 0)
973 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN,
974 vl->plugin, strlen (vl->plugin)) != 0)
976 strcpy (vl_def->plugin, vl->plugin);
977 DEBUG ("plugin = %s", vl->plugin);
980 if (strcmp (vl_def->plugin_instance, vl->plugin_instance) != 0)
982 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
984 strlen (vl->plugin_instance)) != 0)
986 strcpy (vl_def->plugin_instance, vl->plugin_instance);
987 DEBUG ("plugin_instance = %s", vl->plugin_instance);
990 if (strcmp (type_def, ds->type) != 0)
992 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
993 ds->type, strlen (ds->type)) != 0)
995 strcpy (type_def, ds->type);
996 DEBUG ("type = %s", ds->type);
999 if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
1001 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE_INSTANCE,
1003 strlen (vl->type_instance)) != 0)
1005 strcpy (vl_def->type_instance, vl->type_instance);
1006 DEBUG ("type_instance = %s", vl->type_instance);
1009 if (write_part_values (&buffer, &buffer_size, ds, vl) != 0)
1012 return (buffer - buffer_orig);
1013 } /* int add_to_buffer */
1015 static void flush_buffer (void)
1017 network_send_buffer (send_buffer, send_buffer_fill);
1018 send_buffer_ptr = send_buffer;
1019 send_buffer_fill = 0;
1020 memset (&send_buffer_vl, '\0', sizeof (send_buffer_vl));
1021 memset (send_buffer_type, '\0', sizeof (send_buffer_type));
1024 static int network_write (const data_set_t *ds, const value_list_t *vl)
1028 pthread_mutex_lock (&send_buffer_lock);
1030 status = add_to_buffer (send_buffer_ptr,
1031 sizeof (send_buffer) - send_buffer_fill,
1032 &send_buffer_vl, send_buffer_type,
1036 /* status == bytes added to the buffer */
1037 send_buffer_fill += status;
1038 send_buffer_ptr += status;
1044 status = add_to_buffer (send_buffer_ptr,
1045 sizeof (send_buffer) - send_buffer_fill,
1046 &send_buffer_vl, send_buffer_type,
1051 send_buffer_fill += status;
1052 send_buffer_ptr += status;
1058 ERROR ("network plugin: Unable to append to the "
1059 "buffer for some weird reason");
1061 else if ((sizeof (send_buffer) - send_buffer_fill) < 15)
1066 pthread_mutex_unlock (&send_buffer_lock);
1068 return ((status < 0) ? -1 : 0);
1069 } /* int network_write */
1071 static int network_config (const char *key, const char *val)
1079 if ((strcasecmp ("Listen", key) == 0)
1080 || (strcasecmp ("Server", key) == 0))
1082 char *val_cpy = strdup (val);
1083 if (val_cpy == NULL)
1086 service = NET_DEFAULT_PORT;
1087 fields_num = strsplit (val_cpy, fields, 3);
1088 if ((fields_num != 1)
1089 && (fields_num != 2))
1091 else if (fields_num == 2)
1092 service = fields[1];
1095 if (strcasecmp ("Listen", key) == 0)
1096 network_add_listen_socket (node, service);
1098 network_add_sending_socket (node, service);
1100 else if (strcasecmp ("TimeToLive", key) == 0)
1102 int tmp = atoi (val);
1103 if ((tmp > 0) && (tmp < 256))
1104 network_config_ttl = tmp;
1115 static int network_shutdown (void)
1117 DEBUG ("Shutting down.");
1121 if (listen_thread != (pthread_t) 0)
1123 pthread_kill (listen_thread, SIGTERM);
1124 pthread_join (listen_thread, NULL /* no return value */);
1125 listen_thread = (pthread_t) 0;
1130 /* TODO: Close `sending_sockets' */
1132 plugin_unregister_config ("network");
1133 plugin_unregister_init ("network");
1134 plugin_unregister_write ("network");
1135 plugin_unregister_shutdown ("network");
1138 } /* int network_shutdown */
1140 static int network_init (void)
1142 plugin_register_shutdown ("network", network_shutdown);
1144 send_buffer_ptr = send_buffer;
1145 send_buffer_fill = 0;
1146 memset (&send_buffer_vl, '\0', sizeof (send_buffer_vl));
1147 memset (send_buffer_type, '\0', sizeof (send_buffer_type));
1149 /* setup socket(s) and so on */
1150 if (sending_sockets != NULL)
1151 plugin_register_write ("network", network_write);
1153 if ((listen_sockets_num != 0) && (listen_thread == 0))
1157 status = pthread_create (&listen_thread, NULL /* no attributes */,
1158 receive_thread, NULL /* no argument */);
1163 ERROR ("network: pthread_create failed: %s",
1164 sstrerror (errno, errbuf,
1169 } /* int network_init */
1171 void module_register (void)
1173 plugin_register_config ("network", network_config,
1174 config_keys, config_keys_num);
1175 plugin_register_init ("network", network_init);