1 /**
2 * collectd - src/network.c
3 * Copyright (C) 2005-2007 Florian octo Forster
4 *
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.
8 *
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.
13 *
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
17 *
18 * Authors:
19 * Florian octo Forster <octo at verplant.org>
20 **/
22 #include "collectd.h"
23 #include "plugin.h"
24 #include "common.h"
25 #include "configfile.h"
26 #include "utils_avltree.h"
28 #include "network.h"
30 #if HAVE_PTHREAD_H
31 # include <pthread.h>
32 #endif
33 #if HAVE_SYS_SOCKET_H
34 # include <sys/socket.h>
35 #endif
36 #if HAVE_NETDB_H
37 # include <netdb.h>
38 #endif
39 #if HAVE_NETINET_IN_H
40 # include <netinet/in.h>
41 #endif
42 #if HAVE_ARPA_INET_H
43 # include <arpa/inet.h>
44 #endif
45 #if HAVE_POLL_H
46 # include <poll.h>
47 #endif
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
55 # else
56 # error "Neither IP_ADD_MEMBERSHIP nor IPV6_JOIN_GROUP is defined"
57 # endif
58 #endif /* !IP_ADD_MEMBERSHIP */
60 #define BUFF_SIZE 1024
62 /*
63 * Private data types
64 */
65 typedef struct sockent
66 {
67 int fd;
68 struct sockaddr_storage *addr;
69 socklen_t addrlen;
70 struct sockent *next;
71 } sockent_t;
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 * +-------+-----------------------+-------------------------------+
76 * ! Ver. ! ! Length !
77 * +-------+-----------------------+-------------------------------+
78 */
79 struct part_header_s
80 {
81 uint16_t type;
82 uint16_t length;
83 };
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 * +-------------------------------+-------------------------------+
89 * ! Type ! Length !
90 * +-------------------------------+-------------------------------+
91 * : (Length - 4) Bytes :
92 * +---------------------------------------------------------------+
93 */
94 struct part_string_s
95 {
96 part_header_t *head;
97 char *value;
98 };
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 * +-------------------------------+-------------------------------+
104 * ! Type ! Length !
105 * +-------------------------------+-------------------------------+
106 * : (Length - 4 == 2 || 4 || 8) Bytes :
107 * +---------------------------------------------------------------+
108 */
109 struct part_number_s
110 {
111 part_header_t *head;
112 uint64_t *value;
113 };
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 * +-------------------------------+-------------------------------+
119 * ! Type ! Length !
120 * +-------------------------------+---------------+---------------+
121 * ! Num of values ! Type0 ! Type1 !
122 * +-------------------------------+---------------+---------------+
123 * ! Value0 !
124 * ! !
125 * +---------------------------------------------------------------+
126 * ! Value1 !
127 * ! !
128 * +---------------------------------------------------------------+
129 */
130 struct part_values_s
131 {
132 part_header_t *head;
133 uint16_t *num_values;
134 uint8_t *values_types;
135 value_t *values;
136 };
137 typedef struct part_values_s part_values_t;
139 struct receive_list_entry_s
140 {
141 char data[BUFF_SIZE];
142 int data_len;
143 struct receive_list_entry_s *next;
144 };
145 typedef struct receive_list_entry_s receive_list_entry_t;
147 /*
148 * Private variables
149 */
150 static const char *config_keys[] =
151 {
152 "CacheFlush",
153 "Listen",
154 "Server",
155 "TimeToLive",
156 "Forward"
157 };
158 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
160 static int network_config_ttl = 0;
161 static int network_config_forward = 0;
163 static sockent_t *sending_sockets = NULL;
165 static receive_list_entry_t *receive_list_head = NULL;
166 static receive_list_entry_t *receive_list_tail = NULL;
167 static pthread_mutex_t receive_list_lock = PTHREAD_MUTEX_INITIALIZER;
168 static pthread_cond_t receive_list_cond = PTHREAD_COND_INITIALIZER;
170 static struct pollfd *listen_sockets = NULL;
171 static int listen_sockets_num = 0;
173 static int listen_loop = 0;
174 static pthread_t receive_thread_id = 0;
175 static pthread_t dispatch_thread_id = 0;
177 static char send_buffer[BUFF_SIZE];
178 static char *send_buffer_ptr;
179 static int send_buffer_fill;
180 static value_list_t send_buffer_vl = VALUE_LIST_STATIC;
181 static char send_buffer_type[DATA_MAX_NAME_LEN];
182 static pthread_mutex_t send_buffer_lock = PTHREAD_MUTEX_INITIALIZER;
184 static c_avl_tree_t *cache_tree = NULL;
185 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
186 static time_t cache_flush_last;
187 static int cache_flush_interval = 1800;
189 /*
190 * Private functions
191 */
192 static int cache_flush (void)
193 {
194 char **keys = NULL;
195 int keys_num = 0;
197 char **tmp;
198 int i;
200 char *key;
201 time_t *value;
202 c_avl_iterator_t *iter;
204 time_t curtime = time (NULL);
206 iter = c_avl_get_iterator (cache_tree);
207 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
208 {
209 if ((curtime - *value) <= cache_flush_interval)
210 continue;
211 tmp = (char **) realloc (keys,
212 (keys_num + 1) * sizeof (char *));
213 if (tmp == NULL)
214 {
215 sfree (keys);
216 c_avl_iterator_destroy (iter);
217 ERROR ("network plugin: cache_flush: realloc"
218 " failed.");
219 return (-1);
220 }
221 keys = tmp;
222 keys[keys_num] = key;
223 keys_num++;
224 } /* while (c_avl_iterator_next) */
225 c_avl_iterator_destroy (iter);
227 for (i = 0; i < keys_num; i++)
228 {
229 if (c_avl_remove (cache_tree, keys[i], (void *) &key,
230 (void *) &value) != 0)
231 {
232 WARNING ("network plugin: cache_flush: c_avl_remove"
233 " (%s) failed.", keys[i]);
234 continue;
235 }
237 sfree (key);
238 sfree (value);
239 }
241 sfree (keys);
243 DEBUG ("network plugin: cache_flush: Removed %i %s",
244 keys_num, (keys_num == 1) ? "entry" : "entries");
245 cache_flush_last = curtime;
246 return (0);
247 } /* int cache_flush */
249 static int cache_check (const char *type, const value_list_t *vl)
250 {
251 char key[1024];
252 time_t *value = NULL;
253 int retval = -1;
255 if (cache_tree == NULL)
256 return (-1);
258 if (format_name (key, sizeof (key), vl->host, vl->plugin,
259 vl->plugin_instance, type, vl->type_instance))
260 return (-1);
262 pthread_mutex_lock (&cache_lock);
264 if (c_avl_get (cache_tree, key, (void *) &value) == 0)
265 {
266 if (*value < vl->time)
267 {
268 *value = vl->time;
269 retval = 0;
270 }
271 else
272 {
273 DEBUG ("network plugin: cache_check: *value = %i >= vl->time = %i",
274 (int) *value, (int) vl->time);
275 retval = 1;
276 }
277 }
278 else
279 {
280 char *key_copy = strdup (key);
281 value = malloc (sizeof (time_t));
282 if ((key_copy != NULL) && (value != NULL))
283 {
284 *value = vl->time;
285 c_avl_insert (cache_tree, key_copy, value);
286 retval = 0;
287 }
288 else
289 {
290 sfree (key_copy);
291 sfree (value);
292 }
293 }
295 if ((time (NULL) - cache_flush_last) > cache_flush_interval)
296 cache_flush ();
298 pthread_mutex_unlock (&cache_lock);
300 DEBUG ("network plugin: cache_check: key = %s; time = %i; retval = %i",
301 key, (int) vl->time, retval);
303 return (retval);
304 } /* int cache_check */
306 static int write_part_values (char **ret_buffer, int *ret_buffer_len,
307 const data_set_t *ds, const value_list_t *vl)
308 {
309 part_values_t pv;
310 int i;
312 i = 6 + (9 * vl->values_len);
313 if (*ret_buffer_len < i)
314 return (-1);
315 *ret_buffer_len -= i;
317 pv.head = (part_header_t *) *ret_buffer;
318 pv.num_values = (uint16_t *) (pv.head + 1);
319 pv.values_types = (uint8_t *) (pv.num_values + 1);
320 pv.values = (value_t *) (pv.values_types + vl->values_len);
321 *ret_buffer = (void *) (pv.values + vl->values_len);
323 pv.head->type = htons (TYPE_VALUES);
324 pv.head->length = htons (6 + (9 * vl->values_len));
325 *pv.num_values = htons ((uint16_t) vl->values_len);
327 for (i = 0; i < vl->values_len; i++)
328 {
329 if (ds->ds[i].type == DS_TYPE_COUNTER)
330 {
331 pv.values_types[i] = DS_TYPE_COUNTER;
332 pv.values[i].counter = htonll (vl->values[i].counter);
333 }
334 else
335 {
336 pv.values_types[i] = DS_TYPE_GAUGE;
337 pv.values[i].gauge = vl->values[i].gauge;
338 }
339 } /* for (values) */
341 return (0);
342 } /* int write_part_values */
344 static int write_part_number (char **ret_buffer, int *ret_buffer_len,
345 int type, uint64_t value)
346 {
347 part_number_t pn;
349 if (*ret_buffer_len < 12)
350 return (-1);
352 pn.head = (part_header_t *) *ret_buffer;
353 pn.value = (uint64_t *) (pn.head + 1);
355 pn.head->type = htons (type);
356 pn.head->length = htons (12);
357 *pn.value = htonll (value);
359 *ret_buffer = (char *) (pn.value + 1);
360 *ret_buffer_len -= 12;
362 return (0);
363 } /* int write_part_number */
365 static int write_part_string (char **ret_buffer, int *ret_buffer_len,
366 int type, const char *str, int str_len)
367 {
368 part_string_t ps;
369 int len;
371 len = 4 + str_len + 1;
372 if (*ret_buffer_len < len)
373 return (-1);
374 *ret_buffer_len -= len;
376 ps.head = (part_header_t *) *ret_buffer;
377 ps.value = (char *) (ps.head + 1);
379 ps.head->type = htons ((uint16_t) type);
380 ps.head->length = htons ((uint16_t) str_len + 5);
381 if (str_len > 0)
382 memcpy (ps.value, str, str_len);
383 ps.value[str_len] = '\0';
384 *ret_buffer = (void *) (ps.value + (str_len + 1));
386 return (0);
387 } /* int write_part_string */
389 static int parse_part_values (void **ret_buffer, int *ret_buffer_len,
390 value_t **ret_values, int *ret_num_values)
391 {
392 char *buffer = *ret_buffer;
393 int buffer_len = *ret_buffer_len;
394 part_values_t pv;
395 int i;
397 uint16_t h_length;
398 uint16_t h_type;
399 uint16_t h_num;
401 if (buffer_len < (15))
402 {
403 DEBUG ("network plugin: packet is too short: buffer_len = %i",
404 buffer_len);
405 return (-1);
406 }
408 pv.head = (part_header_t *) buffer;
409 h_length = ntohs (pv.head->length);
410 h_type = ntohs (pv.head->type);
412 assert (h_type == TYPE_VALUES);
414 pv.num_values = (uint16_t *) (pv.head + 1);
415 h_num = ntohs (*pv.num_values);
417 if (h_num != ((h_length - 6) / 9))
418 {
419 DEBUG ("`length' and `num of values' don't match");
420 return (-1);
421 }
423 pv.values_types = (uint8_t *) (pv.num_values + 1);
424 pv.values = (value_t *) (pv.values_types + h_num);
426 for (i = 0; i < h_num; i++)
427 if (pv.values_types[i] == DS_TYPE_COUNTER)
428 pv.values[i].counter = ntohll (pv.values[i].counter);
430 *ret_buffer = (void *) (pv.values + h_num);
431 *ret_buffer_len = buffer_len - h_length;
432 *ret_num_values = h_num;
433 *ret_values = pv.values;
435 return (0);
436 } /* int parse_part_values */
438 static int parse_part_number (void **ret_buffer, int *ret_buffer_len,
439 uint64_t *value)
440 {
441 part_number_t pn;
442 uint16_t len;
444 pn.head = (part_header_t *) *ret_buffer;
445 pn.value = (uint64_t *) (pn.head + 1);
447 len = ntohs (pn.head->length);
448 if (len != 12)
449 return (-1);
450 if (len > *ret_buffer_len)
451 return (-1);
452 *value = ntohll (*pn.value);
454 *ret_buffer = (void *) (pn.value + 1);
455 *ret_buffer_len -= len;
457 return (0);
458 } /* int parse_part_number */
460 static int parse_part_string (void **ret_buffer, int *ret_buffer_len,
461 char *output, int output_len)
462 {
463 char *buffer = *ret_buffer;
464 int buffer_len = *ret_buffer_len;
465 part_string_t ps;
467 uint16_t h_length;
468 uint16_t h_type;
470 DEBUG ("network plugin: parse_part_string: ret_buffer = %p;"
471 " ret_buffer_len = %i; output = %p; output_len = %i;",
472 *ret_buffer, *ret_buffer_len,
473 (void *) output, output_len);
475 ps.head = (part_header_t *) buffer;
477 h_length = ntohs (ps.head->length);
478 h_type = ntohs (ps.head->type);
480 DEBUG ("network plugin: parse_part_string: length = %hu; type = %hu;",
481 h_length, h_type);
483 if (buffer_len < h_length)
484 {
485 DEBUG ("packet is too short");
486 return (-1);
487 }
488 assert ((h_type == TYPE_HOST)
489 || (h_type == TYPE_PLUGIN)
490 || (h_type == TYPE_PLUGIN_INSTANCE)
491 || (h_type == TYPE_TYPE)
492 || (h_type == TYPE_TYPE_INSTANCE));
494 ps.value = buffer + 4;
495 if (ps.value[h_length - 5] != '\0')
496 {
497 DEBUG ("String does not end with a nullbyte");
498 return (-1);
499 }
501 if (output_len < (h_length - 4))
502 {
503 DEBUG ("output buffer is too small");
504 return (-1);
505 }
506 strcpy (output, ps.value);
508 DEBUG ("network plugin: parse_part_string: output = %s", output);
510 *ret_buffer = (void *) (buffer + h_length);
511 *ret_buffer_len = buffer_len - h_length;
513 return (0);
514 } /* int parse_part_string */
516 static int parse_packet (void *buffer, int buffer_len)
517 {
518 part_header_t *header;
519 int status;
521 value_list_t vl = VALUE_LIST_INIT;
522 char type[DATA_MAX_NAME_LEN];
524 DEBUG ("network plugin: parse_packet: buffer = %p; buffer_len = %i;",
525 buffer, buffer_len);
527 memset (&vl, '\0', sizeof (vl));
528 memset (&type, '\0', sizeof (type));
529 status = 0;
531 while ((status == 0) && (buffer_len > sizeof (part_header_t)))
532 {
533 header = (part_header_t *) buffer;
535 if (ntohs (header->length) > buffer_len)
536 break;
537 /* Assure that this loop terminates eventually */
538 if (ntohs (header->length) < 4)
539 break;
541 if (ntohs (header->type) == TYPE_VALUES)
542 {
543 status = parse_part_values (&buffer, &buffer_len,
544 &vl.values, &vl.values_len);
546 if (status != 0)
547 {
548 DEBUG ("parse_part_values failed.");
549 break;
550 }
552 if ((vl.time > 0)
553 && (strlen (vl.host) > 0)
554 && (strlen (vl.plugin) > 0)
555 && (strlen (type) > 0)
556 && (cache_check (type, &vl) == 0))
557 {
558 DEBUG ("network plugin: parse_packet:"
559 " dispatching values");
560 plugin_dispatch_values (type, &vl);
561 }
562 else
563 {
564 DEBUG ("network plugin: parse_packet:"
565 " NOT dispatching values");
566 }
567 }
568 else if (ntohs (header->type) == TYPE_TIME)
569 {
570 uint64_t tmp = 0;
571 status = parse_part_number (&buffer, &buffer_len, &tmp);
572 if (status == 0)
573 vl.time = (time_t) tmp;
574 }
575 else if (ntohs (header->type) == TYPE_INTERVAL)
576 {
577 uint64_t tmp = 0;
578 status = parse_part_number (&buffer, &buffer_len, &tmp);
579 if (status == 0)
580 vl.interval = (int) tmp;
581 }
582 else if (ntohs (header->type) == TYPE_HOST)
583 {
584 status = parse_part_string (&buffer, &buffer_len,
585 vl.host, sizeof (vl.host));
586 DEBUG ("network plugin: parse_packet: vl.host = %s", vl.host);
587 }
588 else if (ntohs (header->type) == TYPE_PLUGIN)
589 {
590 status = parse_part_string (&buffer, &buffer_len,
591 vl.plugin, sizeof (vl.plugin));
592 DEBUG ("network plugin: parse_packet: vl.plugin = %s", vl.plugin);
593 }
594 else if (ntohs (header->type) == TYPE_PLUGIN_INSTANCE)
595 {
596 status = parse_part_string (&buffer, &buffer_len,
597 vl.plugin_instance, sizeof (vl.plugin_instance));
598 DEBUG ("network plugin: parse_packet: vl.plugin_instance = %s", vl.plugin_instance);
599 }
600 else if (ntohs (header->type) == TYPE_TYPE)
601 {
602 status = parse_part_string (&buffer, &buffer_len,
603 type, sizeof (type));
604 DEBUG ("network plugin: parse_packet: type = %s", type);
605 }
606 else if (ntohs (header->type) == TYPE_TYPE_INSTANCE)
607 {
608 status = parse_part_string (&buffer, &buffer_len,
609 vl.type_instance, sizeof (vl.type_instance));
610 DEBUG ("network plugin: parse_packet: vl.type_instance = %s", vl.type_instance);
611 }
612 else
613 {
614 DEBUG ("network plugin: parse_packet: Unknown part"
615 " type: 0x%0hx", ntohs (header->type));
616 buffer = ((char *) buffer) + ntohs (header->length);
617 }
618 } /* while (buffer_len > sizeof (part_header_t)) */
620 return (0);
621 } /* int parse_packet */
623 static void free_sockent (sockent_t *se)
624 {
625 sockent_t *next;
626 while (se != NULL)
627 {
628 next = se->next;
629 free (se->addr);
630 free (se);
631 se = next;
632 }
633 } /* void free_sockent */
635 /*
636 * int network_set_ttl
637 *
638 * Set the `IP_MULTICAST_TTL', `IP_TTL', `IPV6_MULTICAST_HOPS' or
639 * `IPV6_UNICAST_HOPS', depending on which option is applicable.
640 *
641 * The `struct addrinfo' is used to destinguish between unicast and multicast
642 * sockets.
643 */
644 static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
645 {
646 if ((network_config_ttl < 1) || (network_config_ttl > 255))
647 return (-1);
649 DEBUG ("ttl = %i", network_config_ttl);
651 if (ai->ai_family == AF_INET)
652 {
653 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
654 int optname;
656 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
657 optname = IP_MULTICAST_TTL;
658 else
659 optname = IP_TTL;
661 if (setsockopt (se->fd, IPPROTO_IP, optname,
662 &network_config_ttl,
663 sizeof (network_config_ttl)) == -1)
664 {
665 char errbuf[1024];
666 ERROR ("setsockopt: %s",
667 sstrerror (errno, errbuf, sizeof (errbuf)));
668 return (-1);
669 }
670 }
671 else if (ai->ai_family == AF_INET6)
672 {
673 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
674 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
675 int optname;
677 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
678 optname = IPV6_MULTICAST_HOPS;
679 else
680 optname = IPV6_UNICAST_HOPS;
682 if (setsockopt (se->fd, IPPROTO_IPV6, optname,
683 &network_config_ttl,
684 sizeof (network_config_ttl)) == -1)
685 {
686 char errbuf[1024];
687 ERROR ("setsockopt: %s",
688 sstrerror (errno, errbuf,
689 sizeof (errbuf)));
690 return (-1);
691 }
692 }
694 return (0);
695 } /* int network_set_ttl */
697 static int network_bind_socket (const sockent_t *se, const struct addrinfo *ai)
698 {
699 int loop = 0;
701 DEBUG ("fd = %i; calling `bind'", se->fd);
703 if (bind (se->fd, ai->ai_addr, ai->ai_addrlen) == -1)
704 {
705 char errbuf[1024];
706 ERROR ("bind: %s",
707 sstrerror (errno, errbuf, sizeof (errbuf)));
708 return (-1);
709 }
711 if (ai->ai_family == AF_INET)
712 {
713 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
714 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
715 {
716 struct ip_mreq mreq;
718 DEBUG ("fd = %i; IPv4 multicast address found", se->fd);
720 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
721 mreq.imr_interface.s_addr = htonl (INADDR_ANY);
723 if (setsockopt (se->fd, IPPROTO_IP, IP_MULTICAST_LOOP,
724 &loop, sizeof (loop)) == -1)
725 {
726 char errbuf[1024];
727 ERROR ("setsockopt: %s",
728 sstrerror (errno, errbuf,
729 sizeof (errbuf)));
730 return (-1);
731 }
733 if (setsockopt (se->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
734 &mreq, sizeof (mreq)) == -1)
735 {
736 char errbuf[1024];
737 ERROR ("setsockopt: %s",
738 sstrerror (errno, errbuf,
739 sizeof (errbuf)));
740 return (-1);
741 }
742 }
743 }
744 else if (ai->ai_family == AF_INET6)
745 {
746 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
747 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
748 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
749 {
750 struct ipv6_mreq mreq;
752 DEBUG ("fd = %i; IPv6 multicast address found", se->fd);
754 memcpy (&mreq.ipv6mr_multiaddr,
755 &addr->sin6_addr,
756 sizeof (addr->sin6_addr));
758 /* http://developer.apple.com/documentation/Darwin/Reference/ManPages/man4/ip6.4.html
759 * ipv6mr_interface may be set to zeroes to
760 * choose the default multicast interface or to
761 * the index of a particular multicast-capable
762 * interface if the host is multihomed.
763 * Membership is associ-associated with a
764 * single interface; programs running on
765 * multihomed hosts may need to join the same
766 * group on more than one interface.*/
767 mreq.ipv6mr_interface = 0;
769 if (setsockopt (se->fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
770 &loop, sizeof (loop)) == -1)
771 {
772 char errbuf[1024];
773 ERROR ("setsockopt: %s",
774 sstrerror (errno, errbuf,
775 sizeof (errbuf)));
776 return (-1);
777 }
779 if (setsockopt (se->fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
780 &mreq, sizeof (mreq)) == -1)
781 {
782 char errbuf[1024];
783 ERROR ("setsockopt: %s",
784 sstrerror (errno, errbuf,
785 sizeof (errbuf)));
786 return (-1);
787 }
788 }
789 }
791 return (0);
792 } /* int network_bind_socket */
794 static sockent_t *network_create_socket (const char *node,
795 const char *service,
796 int listen)
797 {
798 struct addrinfo ai_hints;
799 struct addrinfo *ai_list, *ai_ptr;
800 int ai_return;
802 sockent_t *se_head = NULL;
803 sockent_t *se_tail = NULL;
805 DEBUG ("node = %s, service = %s", node, service);
807 memset (&ai_hints, '\0', sizeof (ai_hints));
808 ai_hints.ai_flags = 0;
809 #ifdef AI_PASSIVE
810 ai_hints.ai_flags |= AI_PASSIVE;
811 #endif
812 #ifdef AI_ADDRCONFIG
813 ai_hints.ai_flags |= AI_ADDRCONFIG;
814 #endif
815 ai_hints.ai_family = AF_UNSPEC;
816 ai_hints.ai_socktype = SOCK_DGRAM;
817 ai_hints.ai_protocol = IPPROTO_UDP;
819 ai_return = getaddrinfo (node, service, &ai_hints, &ai_list);
820 if (ai_return != 0)
821 {
822 char errbuf[1024];
823 ERROR ("getaddrinfo (%s, %s): %s",
824 (node == NULL) ? "(null)" : node,
825 (service == NULL) ? "(null)" : service,
826 (ai_return == EAI_SYSTEM)
827 ? sstrerror (errno, errbuf, sizeof (errbuf))
828 : gai_strerror (ai_return));
829 return (NULL);
830 }
832 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
833 {
834 sockent_t *se;
836 if ((se = (sockent_t *) malloc (sizeof (sockent_t))) == NULL)
837 {
838 char errbuf[1024];
839 ERROR ("malloc: %s",
840 sstrerror (errno, errbuf,
841 sizeof (errbuf)));
842 continue;
843 }
845 if ((se->addr = (struct sockaddr_storage *) malloc (sizeof (struct sockaddr_storage))) == NULL)
846 {
847 char errbuf[1024];
848 ERROR ("malloc: %s",
849 sstrerror (errno, errbuf,
850 sizeof (errbuf)));
851 free (se);
852 continue;
853 }
855 assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
856 memset (se->addr, '\0', sizeof (struct sockaddr_storage));
857 memcpy (se->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
858 se->addrlen = ai_ptr->ai_addrlen;
860 se->fd = socket (ai_ptr->ai_family,
861 ai_ptr->ai_socktype,
862 ai_ptr->ai_protocol);
863 se->next = NULL;
865 if (se->fd == -1)
866 {
867 char errbuf[1024];
868 ERROR ("socket: %s",
869 sstrerror (errno, errbuf,
870 sizeof (errbuf)));
871 free (se->addr);
872 free (se);
873 continue;
874 }
876 if (listen != 0)
877 {
878 if (network_bind_socket (se, ai_ptr) != 0)
879 {
880 close (se->fd);
881 free (se->addr);
882 free (se);
883 continue;
884 }
885 }
886 else /* listen == 0 */
887 {
888 network_set_ttl (se, ai_ptr);
889 }
891 if (se_tail == NULL)
892 {
893 se_head = se;
894 se_tail = se;
895 }
896 else
897 {
898 se_tail->next = se;
899 se_tail = se;
900 }
902 /* We don't open more than one write-socket per node/service pair.. */
903 if (listen == 0)
904 break;
905 }
907 freeaddrinfo (ai_list);
909 return (se_head);
910 } /* sockent_t *network_create_socket */
912 static sockent_t *network_create_default_socket (int listen)
913 {
914 sockent_t *se_ptr = NULL;
915 sockent_t *se_head = NULL;
916 sockent_t *se_tail = NULL;
918 se_ptr = network_create_socket (NET_DEFAULT_V6_ADDR,
919 NET_DEFAULT_PORT, listen);
921 /* Don't send to the same machine in IPv6 and IPv4 if both are available. */
922 if ((listen == 0) && (se_ptr != NULL))
923 return (se_ptr);
925 if (se_ptr != NULL)
926 {
927 se_head = se_ptr;
928 se_tail = se_ptr;
929 while (se_tail->next != NULL)
930 se_tail = se_tail->next;
931 }
933 se_ptr = network_create_socket (NET_DEFAULT_V4_ADDR, NET_DEFAULT_PORT, listen);
935 if (se_tail == NULL)
936 return (se_ptr);
938 se_tail->next = se_ptr;
939 return (se_head);
940 } /* sockent_t *network_create_default_socket */
942 static int network_add_listen_socket (const char *node, const char *service)
943 {
944 sockent_t *se;
945 sockent_t *se_ptr;
946 int se_num = 0;
948 if (service == NULL)
949 service = NET_DEFAULT_PORT;
951 if (node == NULL)
952 se = network_create_default_socket (1 /* listen == true */);
953 else
954 se = network_create_socket (node, service, 1 /* listen == true */);
956 if (se == NULL)
957 return (-1);
959 for (se_ptr = se; se_ptr != NULL; se_ptr = se_ptr->next)
960 se_num++;
962 listen_sockets = (struct pollfd *) realloc (listen_sockets,
963 (listen_sockets_num + se_num)
964 * sizeof (struct pollfd));
966 for (se_ptr = se; se_ptr != NULL; se_ptr = se_ptr->next)
967 {
968 listen_sockets[listen_sockets_num].fd = se_ptr->fd;
969 listen_sockets[listen_sockets_num].events = POLLIN | POLLPRI;
970 listen_sockets[listen_sockets_num].revents = 0;
971 listen_sockets_num++;
972 } /* for (se) */
974 free_sockent (se);
975 return (0);
976 } /* int network_add_listen_socket */
978 static int network_add_sending_socket (const char *node, const char *service)
979 {
980 sockent_t *se;
981 sockent_t *se_ptr;
983 if (service == NULL)
984 service = NET_DEFAULT_PORT;
986 if (node == NULL)
987 se = network_create_default_socket (0 /* listen == false */);
988 else
989 se = network_create_socket (node, service, 0 /* listen == false */);
991 if (se == NULL)
992 return (-1);
994 if (sending_sockets == NULL)
995 {
996 sending_sockets = se;
997 return (0);
998 }
1000 for (se_ptr = sending_sockets; se_ptr->next != NULL; se_ptr = se_ptr->next)
1001 /* seek end */;
1003 se_ptr->next = se;
1004 return (0);
1005 } /* int network_get_listen_socket */
1007 static void *dispatch_thread (void *arg)
1008 {
1009 while (42)
1010 {
1011 receive_list_entry_t *ent;
1013 /* Lock and wait for more data to come in */
1014 pthread_mutex_lock (&receive_list_lock);
1015 while ((listen_loop == 0)
1016 && (receive_list_head == NULL))
1017 pthread_cond_wait (&receive_list_cond, &receive_list_lock);
1019 /* Remove the head entry and unlock */
1020 ent = receive_list_head;
1021 if (ent != NULL)
1022 receive_list_head = ent->next;
1023 pthread_mutex_unlock (&receive_list_lock);
1025 /* Check whether we are supposed to exit. We do NOT check `listen_loop'
1026 * because we dispatch all missing packets before shutting down. */
1027 if (ent == NULL)
1028 break;
1030 parse_packet (ent->data, ent->data_len);
1032 sfree (ent);
1033 } /* while (42) */
1035 return (NULL);
1036 } /* void *receive_thread */
1038 static int network_receive (void)
1039 {
1040 char buffer[BUFF_SIZE];
1041 int buffer_len;
1043 int i;
1044 int status;
1046 if (listen_sockets_num == 0)
1047 network_add_listen_socket (NULL, NULL);
1049 if (listen_sockets_num == 0)
1050 {
1051 ERROR ("network: Failed to open a listening socket.");
1052 return (-1);
1053 }
1055 while (listen_loop == 0)
1056 {
1057 status = poll (listen_sockets, listen_sockets_num, -1);
1059 if (status <= 0)
1060 {
1061 char errbuf[1024];
1062 if (errno == EINTR)
1063 continue;
1064 ERROR ("poll failed: %s",
1065 sstrerror (errno, errbuf, sizeof (errbuf)));
1066 return (-1);
1067 }
1069 for (i = 0; (i < listen_sockets_num) && (status > 0); i++)
1070 {
1071 receive_list_entry_t *ent;
1073 if ((listen_sockets[i].revents & (POLLIN | POLLPRI)) == 0)
1074 continue;
1075 status--;
1077 buffer_len = recv (listen_sockets[i].fd,
1078 buffer, sizeof (buffer),
1079 0 /* no flags */);
1080 if (buffer_len < 0)
1081 {
1082 char errbuf[1024];
1083 ERROR ("recv failed: %s",
1084 sstrerror (errno, errbuf,
1085 sizeof (errbuf)));
1086 return (-1);
1087 }
1089 ent = malloc (sizeof (receive_list_entry_t));
1090 if (ent == NULL)
1091 {
1092 ERROR ("network plugin: malloc failed.");
1093 return (-1);
1094 }
1095 memset (ent, '\0', sizeof (receive_list_entry_t));
1097 /* Hopefully this be optimized out by the compiler. It
1098 * might help prevent stupid bugs in the future though.
1099 */
1100 assert (sizeof (ent->data) == sizeof (buffer));
1102 memcpy (ent->data, buffer, buffer_len);
1103 ent->data_len = buffer_len;
1105 pthread_mutex_lock (&receive_list_lock);
1106 if (receive_list_head == NULL)
1107 {
1108 receive_list_head = ent;
1109 receive_list_tail = ent;
1110 }
1111 else
1112 {
1113 receive_list_tail->next = ent;
1114 receive_list_tail = ent;
1115 }
1116 pthread_cond_signal (&receive_list_cond);
1117 pthread_mutex_unlock (&receive_list_lock);
1118 } /* for (listen_sockets) */
1119 } /* while (listen_loop == 0) */
1121 return (0);
1122 }
1124 static void *receive_thread (void *arg)
1125 {
1126 return (network_receive () ? (void *) 1 : (void *) 0);
1127 } /* void *receive_thread */
1129 static void network_send_buffer (const char *buffer, int buffer_len)
1130 {
1131 sockent_t *se;
1132 int status;
1134 DEBUG ("network plugin: network_send_buffer: buffer_len = %i", buffer_len);
1136 for (se = sending_sockets; se != NULL; se = se->next)
1137 {
1138 while (42)
1139 {
1140 status = sendto (se->fd, buffer, buffer_len, 0 /* no flags */,
1141 (struct sockaddr *) se->addr, se->addrlen);
1142 if (status < 0)
1143 {
1144 char errbuf[1024];
1145 if (errno == EINTR)
1146 continue;
1147 ERROR ("network plugin: sendto failed: %s",
1148 sstrerror (errno, errbuf,
1149 sizeof (errbuf)));
1150 break;
1151 }
1153 break;
1154 } /* while (42) */
1155 } /* for (sending_sockets) */
1156 } /* void network_send_buffer */
1158 static int add_to_buffer (char *buffer, int buffer_size,
1159 value_list_t *vl_def, char *type_def,
1160 const data_set_t *ds, const value_list_t *vl)
1161 {
1162 char *buffer_orig = buffer;
1164 if (strcmp (vl_def->host, vl->host) != 0)
1165 {
1166 if (write_part_string (&buffer, &buffer_size, TYPE_HOST,
1167 vl->host, strlen (vl->host)) != 0)
1168 return (-1);
1169 strcpy (vl_def->host, vl->host);
1170 }
1172 if (vl_def->time != vl->time)
1173 {
1174 if (write_part_number (&buffer, &buffer_size, TYPE_TIME,
1175 (uint64_t) vl->time))
1176 return (-1);
1177 vl_def->time = vl->time;
1178 }
1180 if (vl_def->interval != vl->interval)
1181 {
1182 if (write_part_number (&buffer, &buffer_size, TYPE_INTERVAL,
1183 (uint64_t) vl->interval))
1184 return (-1);
1185 vl_def->interval = vl->interval;
1186 }
1188 if (strcmp (vl_def->plugin, vl->plugin) != 0)
1189 {
1190 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN,
1191 vl->plugin, strlen (vl->plugin)) != 0)
1192 return (-1);
1193 strcpy (vl_def->plugin, vl->plugin);
1194 }
1196 if (strcmp (vl_def->plugin_instance, vl->plugin_instance) != 0)
1197 {
1198 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
1199 vl->plugin_instance,
1200 strlen (vl->plugin_instance)) != 0)
1201 return (-1);
1202 strcpy (vl_def->plugin_instance, vl->plugin_instance);
1203 }
1205 if (strcmp (type_def, ds->type) != 0)
1206 {
1207 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
1208 ds->type, strlen (ds->type)) != 0)
1209 return (-1);
1210 strcpy (type_def, ds->type);
1211 }
1213 if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
1214 {
1215 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE_INSTANCE,
1216 vl->type_instance,
1217 strlen (vl->type_instance)) != 0)
1218 return (-1);
1219 strcpy (vl_def->type_instance, vl->type_instance);
1220 }
1222 if (write_part_values (&buffer, &buffer_size, ds, vl) != 0)
1223 return (-1);
1225 return (buffer - buffer_orig);
1226 } /* int add_to_buffer */
1228 static void flush_buffer (void)
1229 {
1230 DEBUG ("network plugin: flush_buffer: send_buffer_fill = %i",
1231 send_buffer_fill);
1233 network_send_buffer (send_buffer, send_buffer_fill);
1234 send_buffer_ptr = send_buffer;
1235 send_buffer_fill = 0;
1236 memset (&send_buffer_vl, '\0', sizeof (send_buffer_vl));
1237 memset (send_buffer_type, '\0', sizeof (send_buffer_type));
1238 }
1240 static int network_write (const data_set_t *ds, const value_list_t *vl)
1241 {
1242 int status;
1244 /* If the value is already in the cache, we have received it via the
1245 * network. We write it again if forwarding is activated. It's then in
1246 * the cache and should we receive it again we will ignore it. */
1247 status = cache_check (ds->type, vl);
1248 if ((network_config_forward == 0)
1249 && (status != 0))
1250 return (0);
1252 pthread_mutex_lock (&send_buffer_lock);
1254 status = add_to_buffer (send_buffer_ptr,
1255 sizeof (send_buffer) - send_buffer_fill,
1256 &send_buffer_vl, send_buffer_type,
1257 ds, vl);
1258 if (status >= 0)
1259 {
1260 /* status == bytes added to the buffer */
1261 send_buffer_fill += status;
1262 send_buffer_ptr += status;
1263 }
1264 else
1265 {
1266 flush_buffer ();
1268 status = add_to_buffer (send_buffer_ptr,
1269 sizeof (send_buffer) - send_buffer_fill,
1270 &send_buffer_vl, send_buffer_type,
1271 ds, vl);
1273 if (status >= 0)
1274 {
1275 send_buffer_fill += status;
1276 send_buffer_ptr += status;
1277 }
1278 }
1280 if (status < 0)
1281 {
1282 ERROR ("network plugin: Unable to append to the "
1283 "buffer for some weird reason");
1284 }
1285 else if ((sizeof (send_buffer) - send_buffer_fill) < 15)
1286 {
1287 flush_buffer ();
1288 }
1290 pthread_mutex_unlock (&send_buffer_lock);
1292 return ((status < 0) ? -1 : 0);
1293 } /* int network_write */
1295 static int network_config (const char *key, const char *val)
1296 {
1297 char *node;
1298 char *service;
1300 char *fields[3];
1301 int fields_num;
1303 if ((strcasecmp ("Listen", key) == 0)
1304 || (strcasecmp ("Server", key) == 0))
1305 {
1306 char *val_cpy = strdup (val);
1307 if (val_cpy == NULL)
1308 return (1);
1310 service = NET_DEFAULT_PORT;
1311 fields_num = strsplit (val_cpy, fields, 3);
1312 if ((fields_num != 1)
1313 && (fields_num != 2))
1314 return (1);
1315 else if (fields_num == 2)
1316 {
1317 if ((service = strchr (fields[1], '.')) != NULL)
1318 *service = '\0';
1319 service = fields[1];
1320 }
1321 node = fields[0];
1323 if (strcasecmp ("Listen", key) == 0)
1324 network_add_listen_socket (node, service);
1325 else
1326 network_add_sending_socket (node, service);
1327 }
1328 else if (strcasecmp ("TimeToLive", key) == 0)
1329 {
1330 int tmp = atoi (val);
1331 if ((tmp > 0) && (tmp < 256))
1332 network_config_ttl = tmp;
1333 else
1334 return (1);
1335 }
1336 else if (strcasecmp ("Forward", key) == 0)
1337 {
1338 if ((strcasecmp ("true", val) == 0)
1339 || (strcasecmp ("yes", val) == 0)
1340 || (strcasecmp ("on", val) == 0))
1341 network_config_forward = 1;
1342 else
1343 network_config_forward = 0;
1344 }
1345 else if (strcasecmp ("CacheFlush", key) == 0)
1346 {
1347 int tmp = atoi (val);
1348 if (tmp > 0)
1349 cache_flush_interval = tmp;
1350 else return (1);
1351 }
1352 else
1353 {
1354 return (-1);
1355 }
1356 return (0);
1357 } /* int network_config */
1359 static int network_shutdown (void)
1360 {
1361 listen_loop++;
1363 /* Kill the listening thread */
1364 if (receive_thread_id != (pthread_t) 0)
1365 {
1366 pthread_kill (receive_thread_id, SIGTERM);
1367 pthread_join (receive_thread_id, NULL /* no return value */);
1368 receive_thread_id = (pthread_t) 0;
1369 }
1371 /* Shutdown the dispatching thread */
1372 if (dispatch_thread_id != (pthread_t) 0)
1373 pthread_cond_broadcast (&receive_list_cond);
1375 if (send_buffer_fill > 0)
1376 flush_buffer ();
1378 if (cache_tree != NULL)
1379 {
1380 void *key;
1381 void *value;
1383 while (c_avl_pick (cache_tree, &key, &value) == 0)
1384 {
1385 sfree (key);
1386 sfree (value);
1387 }
1388 c_avl_destroy (cache_tree);
1389 cache_tree = NULL;
1390 }
1392 /* TODO: Close `sending_sockets' */
1394 plugin_unregister_config ("network");
1395 plugin_unregister_init ("network");
1396 plugin_unregister_write ("network");
1397 plugin_unregister_shutdown ("network");
1399 return (0);
1400 } /* int network_shutdown */
1402 static int network_init (void)
1403 {
1404 plugin_register_shutdown ("network", network_shutdown);
1406 send_buffer_ptr = send_buffer;
1407 send_buffer_fill = 0;
1408 memset (&send_buffer_vl, '\0', sizeof (send_buffer_vl));
1409 memset (send_buffer_type, '\0', sizeof (send_buffer_type));
1411 cache_tree = c_avl_create ((int (*) (const void *, const void *)) strcmp);
1412 cache_flush_last = time (NULL);
1414 /* setup socket(s) and so on */
1415 if (sending_sockets != NULL)
1416 plugin_register_write ("network", network_write);
1418 if ((listen_sockets_num != 0) && (receive_thread_id == 0))
1419 {
1420 int status;
1422 status = pthread_create (&dispatch_thread_id,
1423 NULL /* no attributes */,
1424 dispatch_thread,
1425 NULL /* no argument */);
1426 if (status != 0)
1427 {
1428 char errbuf[1024];
1429 ERROR ("network: pthread_create failed: %s",
1430 sstrerror (errno, errbuf,
1431 sizeof (errbuf)));
1432 }
1434 status = pthread_create (&receive_thread_id,
1435 NULL /* no attributes */,
1436 receive_thread,
1437 NULL /* no argument */);
1438 if (status != 0)
1439 {
1440 char errbuf[1024];
1441 ERROR ("network: pthread_create failed: %s",
1442 sstrerror (errno, errbuf,
1443 sizeof (errbuf)));
1444 }
1445 }
1446 return (0);
1447 } /* int network_init */
1449 void module_register (void)
1450 {
1451 plugin_register_config ("network", network_config,
1452 config_keys, config_keys_num);
1453 plugin_register_init ("network", network_init);
1454 } /* void module_register */