2 * collectd - src/network.c
3 * Copyright (C) 2005,2006 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 4096
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 * ! Num of values ! Type0 ! Type1 !
107 * +-------------------------------+---------------+---------------+
110 * +---------------------------------------------------------------+
113 * +---------------------------------------------------------------+
118 uint16_t *num_values;
119 uint8_t *values_types;
122 typedef struct part_values_s part_values_t;
127 static const char *config_keys[] =
134 static int config_keys_num = 3;
136 static int network_config_ttl = 0;
138 static sockent_t *sending_sockets = NULL;
140 static struct pollfd *listen_sockets = NULL;
141 static int listen_sockets_num = 0;
142 static pthread_t listen_thread = 0;
143 static int listen_loop = 0;
148 static int write_part_values (char **ret_buffer, int *ret_buffer_len,
149 const data_set_t *ds, const value_list_t *vl)
154 i = 6 + (9 * vl->values_len);
155 if (*ret_buffer_len < i)
157 *ret_buffer_len -= i;
159 pv.head = (part_header_t *) *ret_buffer;
160 pv.num_values = (uint16_t *) (pv.head + 1);
161 pv.values_types = (uint8_t *) (pv.num_values + 1);
162 pv.values = (value_t *) (pv.values_types + vl->values_len);
163 *ret_buffer = (void *) (pv.values + vl->values_len);
165 pv.head->type = htons (TYPE_VALUES);
166 pv.head->length = htons (6 + (9 * vl->values_len));
167 *pv.num_values = htons ((uint16_t) vl->values_len);
169 for (i = 0; i < vl->values_len; i++)
171 if (ds->ds[i].type == DS_TYPE_COUNTER)
173 pv.values_types[i] = DS_TYPE_COUNTER;
174 pv.values[i].counter = htonll (vl->values[i].counter);
178 pv.values_types[i] = DS_TYPE_GAUGE;
179 pv.values[i].gauge = vl->values[i].gauge;
184 } /* int write_part_values */
186 static int write_part_string (char **ret_buffer, int *ret_buffer_len,
187 int type, const char *str, int str_len)
195 len = 4 + str_len + 1;
196 if (*ret_buffer_len < len)
198 *ret_buffer_len -= len;
200 ps.head = (part_header_t *) *ret_buffer;
201 ps.value = (char *) (ps.head + 1);
203 ps.head->type = htons ((uint16_t) type);
204 ps.head->length = htons ((uint16_t) str_len + 4);
205 memcpy (ps.value, str, str_len);
206 ps.value[str_len] = '\0';
207 *ret_buffer = (void *) (ps.value + str_len);
210 } /* int write_part_string */
212 static int parse_part_values (void **ret_buffer, int *ret_buffer_len,
213 value_t **ret_values, int *ret_num_values)
215 char *buffer = *ret_buffer;
216 int buffer_len = *ret_buffer_len;
217 part_values_t *pvalues;
220 if (buffer_len < (15))
222 DBG ("packet is too short");
226 pvalues = (part_values_t *) malloc (sizeof (part_values_t));
230 pvalues->head = (part_header_t *) buffer;
231 assert (pvalues->head->type == htons (TYPE_VALUES));
233 pvalues->num_values = (uint16_t *) (buffer + 4);
234 if (ntohs (*pvalues->num_values)
235 != ((ntohs (pvalues->head->length) - 6) / 9))
237 DBG ("`length' and `num of values' don't match");
242 pvalues->values_types = (uint8_t *) (buffer + 6);
243 pvalues->values = (value_t *) (buffer + 6 + *pvalues->num_values);
245 for (i = 0; i < *pvalues->num_values; i++)
246 if (pvalues->values_types[i] == DS_TYPE_COUNTER)
247 pvalues->values[i].counter = ntohll (pvalues->values[i].counter);
249 *ret_buffer = (void *) buffer;
250 *ret_buffer_len = buffer_len - pvalues->head->length;
251 *ret_num_values = *pvalues->num_values;
252 *ret_values = pvalues->values;
257 } /* int parse_part_values */
259 static int parse_part_string (void **ret_buffer, int *ret_buffer_len,
260 char *output, int output_len)
262 char *buffer = *ret_buffer;
263 int buffer_len = *ret_buffer_len;
264 part_string_t part_string;
266 part_string.head = (part_header_t *) buffer;
267 if (buffer_len < part_string.head->length)
269 DBG ("packet is too short");
272 assert ((part_string.head->type == htons (TYPE_HOST))
273 || (part_string.head->type == htons (TYPE_PLUGIN))
274 || (part_string.head->type == htons (TYPE_PLUGIN_INSTANCE))
275 || (part_string.head->type == htons (TYPE_TYPE))
276 || (part_string.head->type == htons (TYPE_TYPE_INSTANCE)));
278 part_string.value = buffer + 4;
279 if (part_string.value[part_string.head->length - 5] != '\0')
281 DBG ("String does not end with a nullbyte");
285 if (output_len < (part_string.head->length - 4))
287 DBG ("output buffer is too small");
290 strcpy (output, part_string.value);
292 *ret_buffer = (void *) (buffer + part_string.head->length);
293 *ret_buffer_len = buffer_len - part_string.head->length;
296 } /* int parse_part_string */
298 static int parse_packet (void *buffer, int buffer_len)
300 part_header_t *header;
304 char type[DATA_MAX_NAME_LEN];
306 memset (&vl, '\0', sizeof (vl));
307 memset (&type, '\0', sizeof (type));
309 while (buffer_len > sizeof (part_header_t))
311 header = (part_header_t *) buffer;
313 if (header->length > buffer_len)
316 if (header->type == TYPE_VALUES)
318 status = parse_part_values (&buffer, &buffer_len,
319 &vl.values, &vl.values_len);
322 && (strlen (vl.host) > 0)
323 && (strlen (vl.plugin) > 0)
324 && (strlen (type) > 0))
325 plugin_dispatch_values (type, &vl);
327 else if (header->type == TYPE_HOST)
329 status = parse_part_string (&buffer, &buffer_len,
330 vl.host, sizeof (vl.host));
332 else if (header->type == TYPE_PLUGIN)
334 status = parse_part_string (&buffer, &buffer_len,
335 vl.plugin, sizeof (vl.plugin));
337 else if (header->type == TYPE_PLUGIN_INSTANCE)
339 status = parse_part_string (&buffer, &buffer_len,
340 vl.plugin_instance, sizeof (vl.plugin_instance));
342 else if (header->type == TYPE_TYPE)
344 status = parse_part_string (&buffer, &buffer_len,
345 type, sizeof (type));
347 else if (header->type == TYPE_TYPE_INSTANCE)
349 status = parse_part_string (&buffer, &buffer_len,
350 vl.type_instance, sizeof (vl.type_instance));
354 DBG ("Unknown part type: 0x%0hx", header->type);
355 buffer = ((char *) buffer) + header->length;
357 } /* while (buffer_len > sizeof (part_header_t)) */
360 } /* int parse_packet */
362 static void free_sockent (sockent_t *se)
372 } /* void free_sockent */
375 * int network_set_ttl
377 * Set the `IP_MULTICAST_TTL', `IP_TTL', `IPV6_MULTICAST_HOPS' or
378 * `IPV6_UNICAST_HOPS', depending on which option is applicable.
380 * The `struct addrinfo' is used to destinguish between unicast and multicast
383 static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
385 if ((network_config_ttl < 1) || (network_config_ttl > 255))
388 DBG ("ttl = %i", network_config_ttl);
390 if (ai->ai_family == AF_INET)
392 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
395 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
396 optname = IP_MULTICAST_TTL;
400 if (setsockopt (se->fd, IPPROTO_IP, optname,
402 sizeof (network_config_ttl)) == -1)
404 syslog (LOG_ERR, "setsockopt: %s", strerror (errno));
408 else if (ai->ai_family == AF_INET6)
410 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
411 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
414 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
415 optname = IPV6_MULTICAST_HOPS;
417 optname = IPV6_UNICAST_HOPS;
419 if (setsockopt (se->fd, IPPROTO_IPV6, optname,
421 sizeof (network_config_ttl)) == -1)
423 syslog (LOG_ERR, "setsockopt: %s", strerror (errno));
429 } /* int network_set_ttl */
431 static int network_bind_socket (const sockent_t *se, const struct addrinfo *ai)
435 DBG ("fd = %i; calling `bind'", se->fd);
437 if (bind (se->fd, ai->ai_addr, ai->ai_addrlen) == -1)
439 syslog (LOG_ERR, "bind: %s", strerror (errno));
443 if (ai->ai_family == AF_INET)
445 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
446 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
450 DBG ("fd = %i; IPv4 multicast address found", se->fd);
452 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
453 mreq.imr_interface.s_addr = htonl (INADDR_ANY);
455 if (setsockopt (se->fd, IPPROTO_IP, IP_MULTICAST_LOOP,
456 &loop, sizeof (loop)) == -1)
458 syslog (LOG_ERR, "setsockopt: %s", strerror (errno));
462 if (setsockopt (se->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
463 &mreq, sizeof (mreq)) == -1)
465 syslog (LOG_ERR, "setsockopt: %s", strerror (errno));
470 else if (ai->ai_family == AF_INET6)
472 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
473 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
474 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
476 struct ipv6_mreq mreq;
478 DBG ("fd = %i; IPv6 multicast address found", se->fd);
480 memcpy (&mreq.ipv6mr_multiaddr,
482 sizeof (addr->sin6_addr));
484 /* http://developer.apple.com/documentation/Darwin/Reference/ManPages/man4/ip6.4.html
485 * ipv6mr_interface may be set to zeroes to
486 * choose the default multicast interface or to
487 * the index of a particular multicast-capable
488 * interface if the host is multihomed.
489 * Membership is associ-associated with a
490 * single interface; programs running on
491 * multihomed hosts may need to join the same
492 * group on more than one interface.*/
493 mreq.ipv6mr_interface = 0;
495 if (setsockopt (se->fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
496 &loop, sizeof (loop)) == -1)
498 syslog (LOG_ERR, "setsockopt: %s", strerror (errno));
502 if (setsockopt (se->fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
503 &mreq, sizeof (mreq)) == -1)
505 syslog (LOG_ERR, "setsockopt: %s", strerror (errno));
512 } /* int network_bind_socket */
514 static sockent_t *network_create_socket (const char *node,
518 struct addrinfo ai_hints;
519 struct addrinfo *ai_list, *ai_ptr;
522 sockent_t *se_head = NULL;
523 sockent_t *se_tail = NULL;
525 DBG ("node = %s, service = %s", node, service);
527 memset (&ai_hints, '\0', sizeof (ai_hints));
528 ai_hints.ai_flags = 0;
530 ai_hints.ai_flags |= AI_PASSIVE;
533 ai_hints.ai_flags |= AI_ADDRCONFIG;
535 ai_hints.ai_family = AF_UNSPEC;
536 ai_hints.ai_socktype = SOCK_DGRAM;
537 ai_hints.ai_protocol = IPPROTO_UDP;
539 ai_return = getaddrinfo (node, service, &ai_hints, &ai_list);
542 syslog (LOG_ERR, "getaddrinfo (%s, %s): %s",
543 (node == NULL) ? "(null)" : node,
544 (service == NULL) ? "(null)" : service,
545 (ai_return == EAI_SYSTEM)
547 : gai_strerror (ai_return));
551 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
555 if ((se = (sockent_t *) malloc (sizeof (sockent_t))) == NULL)
557 syslog (LOG_EMERG, "malloc: %s", strerror (errno));
561 if ((se->addr = (struct sockaddr_storage *) malloc (sizeof (struct sockaddr_storage))) == NULL)
563 syslog (LOG_EMERG, "malloc: %s", strerror (errno));
568 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
569 memset (se->addr, '\0', sizeof (struct sockaddr_storage));
570 memcpy (se->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
571 se->addrlen = ai_ptr->ai_addrlen;
573 se->fd = socket (ai_ptr->ai_family,
575 ai_ptr->ai_protocol);
580 syslog (LOG_ERR, "socket: %s", strerror (errno));
588 if (network_bind_socket (se, ai_ptr) != 0)
595 else /* listen == 0 */
597 network_set_ttl (se, ai_ptr);
611 /* We don't open more than one write-socket per node/service pair.. */
616 freeaddrinfo (ai_list);
619 } /* sockent_t *network_create_socket */
621 static sockent_t *network_create_default_socket (int listen)
623 sockent_t *se_ptr = NULL;
624 sockent_t *se_head = NULL;
625 sockent_t *se_tail = NULL;
627 se_ptr = network_create_socket (NET_DEFAULT_V6_ADDR,
628 NET_DEFAULT_PORT, listen);
630 /* Don't send to the same machine in IPv6 and IPv4 if both are available. */
631 if ((listen == 0) && (se_ptr != NULL))
638 while (se_tail->next != NULL)
639 se_tail = se_tail->next;
642 se_ptr = network_create_socket (NET_DEFAULT_V4_ADDR, NET_DEFAULT_PORT, listen);
647 se_tail->next = se_ptr;
649 } /* sockent_t *network_create_default_socket */
651 static int network_add_listen_socket (const char *node, const char *service)
658 service = NET_DEFAULT_PORT;
661 se = network_create_default_socket (1 /* listen == true */);
663 se = network_create_socket (node, service, 1 /* listen == true */);
668 for (se_ptr = se; se_ptr != NULL; se_ptr = se_ptr->next)
671 listen_sockets = (struct pollfd *) realloc (listen_sockets,
672 (listen_sockets_num + se_num)
673 * sizeof (struct pollfd));
675 for (se_ptr = se; se_ptr != NULL; se_ptr = se_ptr->next)
677 listen_sockets[listen_sockets_num].fd = se_ptr->fd;
678 listen_sockets[listen_sockets_num].events = POLLIN | POLLPRI;
679 listen_sockets[listen_sockets_num].revents = 0;
680 listen_sockets_num++;
685 } /* int network_add_listen_socket */
687 static int network_add_sending_socket (const char *node, const char *service)
693 service = NET_DEFAULT_PORT;
696 se = network_create_default_socket (0 /* listen == false */);
698 se = network_create_socket (node, service, 0 /* listen == false */);
703 if (sending_sockets == NULL)
705 sending_sockets = se;
709 for (se_ptr = sending_sockets; se_ptr->next != NULL; se_ptr = se_ptr->next)
714 } /* int network_get_listen_socket */
716 int network_receive (void)
718 char buffer[BUFF_SIZE];
724 if (listen_sockets_num == 0)
725 network_add_listen_socket (NULL, NULL);
727 if (listen_sockets_num == 0)
729 syslog (LOG_ERR, "network: Failed to open a listening socket.");
733 while (listen_loop == 0)
735 status = poll (listen_sockets, listen_sockets_num, -1);
741 syslog (LOG_ERR, "poll failed: %s",
746 for (i = 0; (i < listen_sockets_num) && (status > 0); i++)
748 if ((listen_sockets[i].revents & (POLLIN | POLLPRI)) == 0)
752 buffer_len = recv (listen_sockets[i].fd,
753 buffer, sizeof (buffer),
757 syslog (LOG_ERR, "recv failed: %s", strerror (errno));
761 parse_packet (buffer, buffer_len);
762 } /* for (listen_sockets) */
763 } /* while (listen_loop == 0) */
768 static void *receive_thread (void *arg)
770 return ((void *) network_receive ());
771 } /* void *receive_thread */
774 int network_send (char *type, char *inst, char *value)
784 DBG ("type = %s, inst = %s, value = %s", type, inst, value);
786 assert (operating_mode == MODE_CLIENT);
788 buflen = snprintf (buf, BUFF_SIZE, "%s %s %s", type, inst, value);
789 if ((buflen >= BUFF_SIZE) || (buflen < 1))
791 syslog (LOG_WARNING, "network_send: snprintf failed..");
797 if (socklist_head == NULL)
798 network_create_default_socket (0 /* listen == false */);
801 for (se = socklist_head; se != NULL; se = se->next)
805 status = sendto (se->fd, buf, buflen, 0,
806 (struct sockaddr *) se->addr, se->addrlen);
812 DBG ("sendto was interrupted");
817 syslog (LOG_ERR, "sendto: %s", strerror (errno));
829 syslog (LOG_WARNING, "Message wasn't sent to anybody..");
832 } /* int network_send */
835 static int network_write (const data_set_t *ds, const value_list_t *vl)
843 DBG ("host = %s; plugin = %s; plugin_instance = %s; type = %s; type_instance = %s;",
844 vl->host, vl->plugin, vl->plugin_instance, ds->type, vl->type_instance);
846 buf_len = sizeof (buf);
848 if (write_part_string (&buf_ptr, &buf_len, TYPE_HOST,
849 vl->host, strlen (vl->host)) != 0)
851 if (write_part_string (&buf_ptr, &buf_len, TYPE_PLUGIN,
852 vl->plugin, strlen (vl->plugin)) != 0)
854 if (strlen (vl->plugin_instance) > 0)
855 if (write_part_string (&buf_ptr, &buf_len, TYPE_PLUGIN_INSTANCE,
857 strlen (vl->plugin_instance)) != 0)
859 if (write_part_string (&buf_ptr, &buf_len, TYPE_TYPE,
860 ds->type, strlen (ds->type)) != 0)
862 if (strlen (vl->type_instance) > 0)
863 if (write_part_string (&buf_ptr, &buf_len, TYPE_PLUGIN_INSTANCE,
865 strlen (vl->type_instance)) != 0)
868 write_part_values (&buf_ptr, &buf_len, ds, vl);
870 buf_len = sizeof (buf) - buf_len;
872 for (se = sending_sockets; se != NULL; se = se->next)
878 status = sendto (se->fd, buf, buf_len, 0 /* no flags */,
879 (struct sockaddr *) se->addr, se->addrlen);
884 syslog (LOG_ERR, "network: sendto failed: %s",
891 } /* for (sending_sockets) */
896 static int network_config (const char *key, const char *val)
904 if ((strcasecmp ("Listen", key) == 0)
905 || (strcasecmp ("Server", key) == 0))
907 char *val_cpy = strdup (val);
911 service = NET_DEFAULT_PORT;
912 fields_num = strsplit (val_cpy, fields, 3);
913 if ((fields_num != 1)
914 && (fields_num != 2))
916 else if (fields_num == 2)
920 if (strcasecmp ("Listen", key) == 0)
921 network_add_listen_socket (node, service);
923 network_add_sending_socket (node, service);
925 else if (strcasecmp ("TimeToLive", key) == 0)
927 int tmp = atoi (val);
928 if ((tmp > 0) && (tmp < 256))
929 network_config_ttl = tmp;
940 static int network_shutdown (void)
942 DBG ("Shutting down.");
946 pthread_kill (listen_thread, SIGTERM);
947 pthread_join (listen_thread, NULL /* no return value */);
954 static int network_init (void)
956 plugin_register_shutdown ("network", network_shutdown);
958 /* setup socket(s) and so on */
959 if (sending_sockets != NULL)
960 plugin_register_write ("network", network_write);
962 if ((listen_sockets_num != 0) && (listen_thread == 0))
966 status = pthread_create (&listen_thread, NULL /* no attributes */,
967 receive_thread, NULL /* no argument */);
970 syslog (LOG_ERR, "network: pthread_create failed: %s",
974 } /* int network_init */
976 void module_register (void)
978 plugin_register_config ("network", network_config,
979 config_keys, config_keys_num);
980 plugin_register_init ("network", network_init);