Code

network plugin: Changed the old network code to work as a plugin.
[collectd.git] / src / network.c
1 /**
2  * collectd - src/network.c
3  * Copyright (C) 2005,2006  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_debug.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 4096
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  * ! Num of values                 ! Type0         ! Type1         !
107  * +-------------------------------+---------------+---------------+
108  * ! Value0                                                        !
109  * !                                                               !
110  * +---------------------------------------------------------------+
111  * ! Value1                                                        !
112  * !                                                               !
113  * +---------------------------------------------------------------+
114  */
115 struct part_values_s
117         part_header_t *head;
118         uint16_t *num_values;
119         uint8_t  *values_types;
120         value_t  *values;
121 };
122 typedef struct part_values_s part_values_t;
124 /*
125  * Private variables
126  */
127 static const char *config_keys[] =
129         "Listen",
130         "Server",
131         "TimeToLive",
132         NULL
133 };
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;
145 /*
146  * Private functions
147  */
148 static int write_part_values (char **ret_buffer, int *ret_buffer_len,
149                 const data_set_t *ds, const value_list_t *vl)
151         part_values_t pv;
152         int i;
154         i = 6 + (9 * vl->values_len);
155         if (*ret_buffer_len < i)
156                 return (-1);
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);
168         
169         for (i = 0; i < vl->values_len; i++)
170         {
171                 if (ds->ds[i].type == DS_TYPE_COUNTER)
172                 {
173                         pv.values_types[i] = DS_TYPE_COUNTER;
174                         pv.values[i].counter = htonll (vl->values[i].counter);
175                 }
176                 else
177                 {
178                         pv.values_types[i] = DS_TYPE_GAUGE;
179                         pv.values[i].gauge = vl->values[i].gauge;
180                 }
181         } /* for (values) */
183         return (0);
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)
189         part_string_t ps;
190         int len;
192         if (str_len < 1)
193                 return (-1);
195         len = 4 + str_len + 1;
196         if (*ret_buffer_len < len)
197                 return (-1);
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);
209         return (0);
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;
218         int   i;
220         if (buffer_len < (15))
221         {
222                 DBG ("packet is too short");
223                 return (-1);
224         }
226         pvalues = (part_values_t *) malloc (sizeof (part_values_t));
227         if (pvalues == NULL)
228                 return (-1);
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))
236         {
237                 DBG ("`length' and `num of values' don't match");
238                 free (pvalues);
239                 return (-1);
240         }
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;
254         free (pvalues);
256         return (0);
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)
268         {
269                 DBG ("packet is too short");
270                 return (-1);
271         }
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')
280         {
281                 DBG ("String does not end with a nullbyte");
282                 return (-1);
283         }
285         if (output_len < (part_string.head->length - 4))
286         {
287                 DBG ("output buffer is too small");
288                 return (-1);
289         }
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;
295         return (0);
296 } /* int parse_part_string */
298 static int parse_packet (void *buffer, int buffer_len)
300         part_header_t *header;
301         int status;
303         value_list_t vl;
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))
310         {
311                 header = (part_header_t *) buffer;
313                 if (header->length > buffer_len)
314                         break;
316                 if (header->type == TYPE_VALUES)
317                 {
318                         status = parse_part_values (&buffer, &buffer_len,
319                                         &vl.values, &vl.values_len);
321                         if ((status == 0)
322                                         && (strlen (vl.host) > 0)
323                                         && (strlen (vl.plugin) > 0)
324                                         && (strlen (type) > 0))
325                                 plugin_dispatch_values (type, &vl);
326                 }
327                 else if (header->type == TYPE_HOST)
328                 {
329                         status = parse_part_string (&buffer, &buffer_len,
330                                         vl.host, sizeof (vl.host));
331                 }
332                 else if (header->type == TYPE_PLUGIN)
333                 {
334                         status = parse_part_string (&buffer, &buffer_len,
335                                         vl.plugin, sizeof (vl.plugin));
336                 }
337                 else if (header->type == TYPE_PLUGIN_INSTANCE)
338                 {
339                         status = parse_part_string (&buffer, &buffer_len,
340                                         vl.plugin_instance, sizeof (vl.plugin_instance));
341                 }
342                 else if (header->type == TYPE_TYPE)
343                 {
344                         status = parse_part_string (&buffer, &buffer_len,
345                                         type, sizeof (type));
346                 }
347                 else if (header->type == TYPE_TYPE_INSTANCE)
348                 {
349                         status = parse_part_string (&buffer, &buffer_len,
350                                         vl.type_instance, sizeof (vl.type_instance));
351                 }
352                 else
353                 {
354                         DBG ("Unknown part type: 0x%0hx", header->type);
355                         buffer = ((char *) buffer) + header->length;
356                 }
357         } /* while (buffer_len > sizeof (part_header_t)) */
359         return (0);
360 } /* int parse_packet */
362 static void free_sockent (sockent_t *se)
364         sockent_t *next;
365         while (se != NULL)
366         {
367                 next = se->next;
368                 free (se->addr);
369                 free (se);
370                 se = next;
371         }
372 } /* void free_sockent */
374 /*
375  * int network_set_ttl
376  *
377  * Set the `IP_MULTICAST_TTL', `IP_TTL', `IPV6_MULTICAST_HOPS' or
378  * `IPV6_UNICAST_HOPS', depending on which option is applicable.
379  *
380  * The `struct addrinfo' is used to destinguish between unicast and multicast
381  * sockets.
382  */
383 static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
385         if ((network_config_ttl < 1) || (network_config_ttl > 255))
386                 return (-1);
388         DBG ("ttl = %i", network_config_ttl);
390         if (ai->ai_family == AF_INET)
391         {
392                 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
393                 int optname;
395                 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
396                         optname = IP_MULTICAST_TTL;
397                 else
398                         optname = IP_TTL;
400                 if (setsockopt (se->fd, IPPROTO_IP, optname,
401                                         &network_config_ttl,
402                                         sizeof (network_config_ttl)) == -1)
403                 {
404                         syslog (LOG_ERR, "setsockopt: %s", strerror (errno));
405                         return (-1);
406                 }
407         }
408         else if (ai->ai_family == AF_INET6)
409         {
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;
412                 int optname;
414                 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
415                         optname = IPV6_MULTICAST_HOPS;
416                 else
417                         optname = IPV6_UNICAST_HOPS;
419                 if (setsockopt (se->fd, IPPROTO_IPV6, optname,
420                                         &network_config_ttl,
421                                         sizeof (network_config_ttl)) == -1)
422                 {
423                         syslog (LOG_ERR, "setsockopt: %s", strerror (errno));
424                         return (-1);
425                 }
426         }
428         return (0);
429 } /* int network_set_ttl */
431 static int network_bind_socket (const sockent_t *se, const struct addrinfo *ai)
433         int loop = 1;
435         DBG ("fd = %i; calling `bind'", se->fd);
437         if (bind (se->fd, ai->ai_addr, ai->ai_addrlen) == -1)
438         {
439                 syslog (LOG_ERR, "bind: %s", strerror (errno));
440                 return (-1);
441         }
443         if (ai->ai_family == AF_INET)
444         {
445                 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
446                 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
447                 {
448                         struct ip_mreq mreq;
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)
457                         {
458                                 syslog (LOG_ERR, "setsockopt: %s", strerror (errno));
459                                 return (-1);
460                         }
462                         if (setsockopt (se->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
463                                                 &mreq, sizeof (mreq)) == -1)
464                         {
465                                 syslog (LOG_ERR, "setsockopt: %s", strerror (errno));
466                                 return (-1);
467                         }
468                 }
469         }
470         else if (ai->ai_family == AF_INET6)
471         {
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))
475                 {
476                         struct ipv6_mreq mreq;
478                         DBG ("fd = %i; IPv6 multicast address found", se->fd);
480                         memcpy (&mreq.ipv6mr_multiaddr,
481                                         &addr->sin6_addr,
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)
497                         {
498                                 syslog (LOG_ERR, "setsockopt: %s", strerror (errno));
499                                 return (-1);
500                         }
502                         if (setsockopt (se->fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
503                                                 &mreq, sizeof (mreq)) == -1)
504                         {
505                                 syslog (LOG_ERR, "setsockopt: %s", strerror (errno));
506                                 return (-1);
507                         }
508                 }
509         }
511         return (0);
512 } /* int network_bind_socket */
514 static sockent_t *network_create_socket (const char *node,
515                 const char *service,
516                 int listen)
518         struct addrinfo  ai_hints;
519         struct addrinfo *ai_list, *ai_ptr;
520         int              ai_return;
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;
529 #ifdef AI_PASSIVE
530         ai_hints.ai_flags |= AI_PASSIVE;
531 #endif
532 #ifdef AI_ADDRCONFIG
533         ai_hints.ai_flags |= AI_ADDRCONFIG;
534 #endif
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);
540         if (ai_return != 0)
541         {
542                 syslog (LOG_ERR, "getaddrinfo (%s, %s): %s",
543                                 (node == NULL) ? "(null)" : node,
544                                 (service == NULL) ? "(null)" : service,
545                                 (ai_return == EAI_SYSTEM)
546                                 ? strerror (errno)
547                                 : gai_strerror (ai_return));
548                 return (NULL);
549         }
551         for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
552         {
553                 sockent_t *se;
555                 if ((se = (sockent_t *) malloc (sizeof (sockent_t))) == NULL)
556                 {
557                         syslog (LOG_EMERG, "malloc: %s", strerror (errno));
558                         continue;
559                 }
561                 if ((se->addr = (struct sockaddr_storage *) malloc (sizeof (struct sockaddr_storage))) == NULL)
562                 {
563                         syslog (LOG_EMERG, "malloc: %s", strerror (errno));
564                         free (se);
565                         continue;
566                 }
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,
574                                 ai_ptr->ai_socktype,
575                                 ai_ptr->ai_protocol);
576                 se->next = NULL;
578                 if (se->fd == -1)
579                 {
580                         syslog (LOG_ERR, "socket: %s", strerror (errno));
581                         free (se->addr);
582                         free (se);
583                         continue;
584                 }
586                 if (listen != 0)
587                 {
588                         if (network_bind_socket (se, ai_ptr) != 0)
589                         {
590                                 free (se->addr);
591                                 free (se);
592                                 continue;
593                         }
594                 }
595                 else /* listen == 0 */
596                 {
597                         network_set_ttl (se, ai_ptr);
598                 }
600                 if (se_tail == NULL)
601                 {
602                         se_head = se;
603                         se_tail = se;
604                 }
605                 else
606                 {
607                         se_tail->next = se;
608                         se_tail = se;
609                 }
611                 /* We don't open more than one write-socket per node/service pair.. */
612                 if (listen == 0)
613                         break;
614         }
616         freeaddrinfo (ai_list);
618         return (se_head);
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))
632                 return (se_ptr);
634         if (se_ptr != NULL)
635         {
636                 se_head = se_ptr;
637                 se_tail = se_ptr;
638                 while (se_tail->next != NULL)
639                         se_tail = se_tail->next;
640         }
642         se_ptr = network_create_socket (NET_DEFAULT_V4_ADDR, NET_DEFAULT_PORT, listen);
644         if (se_tail == NULL)
645                 return (se_ptr);
647         se_tail->next = se_ptr;
648         return (se_head);
649 } /* sockent_t *network_create_default_socket */
651 static int network_add_listen_socket (const char *node, const char *service)
653         sockent_t *se;
654         sockent_t *se_ptr;
655         int se_num = 0;
657         if (service == NULL)
658                 service = NET_DEFAULT_PORT;
660         if (node == NULL)
661                 se = network_create_default_socket (1 /* listen == true */);
662         else
663                 se = network_create_socket (node, service, 1 /* listen == true */);
665         if (se == NULL)
666                 return (-1);
668         for (se_ptr = se; se_ptr != NULL; se_ptr = se_ptr->next)
669                 se_num++;
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)
676         {
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++;
681         } /* for (se) */
683         free_sockent (se);
684         return (0);
685 } /* int network_add_listen_socket */
687 static int network_add_sending_socket (const char *node, const char *service)
689         sockent_t *se;
690         sockent_t *se_ptr;
692         if (service == NULL)
693                 service = NET_DEFAULT_PORT;
695         if (node == NULL)
696                 se = network_create_default_socket (0 /* listen == false */);
697         else
698                 se = network_create_socket (node, service, 0 /* listen == false */);
700         if (se == NULL)
701                 return (-1);
703         if (sending_sockets == NULL)
704         {
705                 sending_sockets = se;
706                 return (0);
707         }
709         for (se_ptr = sending_sockets; se_ptr->next != NULL; se_ptr = se_ptr->next)
710                 /* seek end */;
712         se_ptr->next = se;
713         return (0);
714 } /* int network_get_listen_socket */
716 int network_receive (void)
718         char buffer[BUFF_SIZE];
719         int  buffer_len;
721         int i;
722         int status;
724         if (listen_sockets_num == 0)
725                 network_add_listen_socket (NULL, NULL);
727         if (listen_sockets_num == 0)
728         {
729                 syslog (LOG_ERR, "network: Failed to open a listening socket.");
730                 return (-1);
731         }
733         while (listen_loop == 0)
734         {
735                 status = poll (listen_sockets, listen_sockets_num, -1);
737                 if (status <= 0)
738                 {
739                         if (errno == EINTR)
740                                 continue;
741                         syslog (LOG_ERR, "poll failed: %s",
742                                         strerror (errno));
743                         return (-1);
744                 }
746                 for (i = 0; (i < listen_sockets_num) && (status > 0); i++)
747                 {
748                         if ((listen_sockets[i].revents & (POLLIN | POLLPRI)) == 0)
749                                 continue;
750                         status--;
752                         buffer_len = recv (listen_sockets[i].fd,
753                                         buffer, sizeof (buffer),
754                                         0 /* no flags */);
755                         if (buffer_len < 0)
756                         {
757                                 syslog (LOG_ERR, "recv failed: %s", strerror (errno));
758                                 return (-1);
759                         }
761                         parse_packet (buffer, buffer_len);
762                 } /* for (listen_sockets) */
763         } /* while (listen_loop == 0) */
765         return (0);
768 static void *receive_thread (void *arg)
770         return ((void *) network_receive ());
771 } /* void *receive_thread */
773 #if 0
774 int network_send (char *type, char *inst, char *value)
776         char buf[BUFF_SIZE];
777         int buflen;
779         sockent_t *se;
781         int ret;
782         int status;
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))
790         {
791                 syslog (LOG_WARNING, "network_send: snprintf failed..");
792                 return (-1);
793         }
794         buf[buflen] = '\0';
795         buflen++;
797         if (socklist_head == NULL)
798                 network_create_default_socket (0 /* listen == false */);
800         ret = 0;
801         for (se = socklist_head; se != NULL; se = se->next)
802         {
803                 while (1)
804                 {
805                         status = sendto (se->fd, buf, buflen, 0,
806                                         (struct sockaddr *) se->addr, se->addrlen);
808                         if (status == -1)
809                         {
810                                 if (errno == EINTR)
811                                 {
812                                         DBG ("sendto was interrupted");
813                                         continue;
814                                 }
815                                 else
816                                 {
817                                         syslog (LOG_ERR, "sendto: %s", strerror (errno));
818                                         ret = -1;
819                                         break;
820                                 }
821                         }
822                         else if (ret >= 0)
823                                 ret++;
824                         break;
825                 }
826         }
828         if (ret == 0)
829                 syslog (LOG_WARNING, "Message wasn't sent to anybody..");
831         return (ret);
832 } /* int network_send */
833 #endif
835 static int network_write (const data_set_t *ds, const value_list_t *vl)
837         char  buf[BUFF_SIZE];
838         char *buf_ptr;
839         int   buf_len;
841         sockent_t *se;
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);
847         buf_ptr = buf;
848         if (write_part_string (&buf_ptr, &buf_len, TYPE_HOST,
849                                 vl->host, strlen (vl->host)) != 0)
850                 return (-1);
851         if (write_part_string (&buf_ptr, &buf_len, TYPE_PLUGIN,
852                                 vl->plugin, strlen (vl->plugin)) != 0)
853                 return (-1);
854         if (strlen (vl->plugin_instance) > 0)
855                 if (write_part_string (&buf_ptr, &buf_len, TYPE_PLUGIN_INSTANCE,
856                                         vl->plugin_instance,
857                                         strlen (vl->plugin_instance)) != 0)
858                         return (-1);
859         if (write_part_string (&buf_ptr, &buf_len, TYPE_TYPE,
860                                 ds->type, strlen (ds->type)) != 0)
861                 return (-1);
862         if (strlen (vl->type_instance) > 0)
863                 if (write_part_string (&buf_ptr, &buf_len, TYPE_PLUGIN_INSTANCE,
864                                         vl->type_instance,
865                                         strlen (vl->type_instance)) != 0)
866                         return (-1);
867         
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)
873         {
874                 int status;
876                 while (42)
877                 {
878                         status = sendto (se->fd, buf, buf_len, 0 /* no flags */,
879                                         (struct sockaddr *) se->addr, se->addrlen);
880                         if (status < 0)
881                         {
882                                 if (errno == EINTR)
883                                         continue;
884                                 syslog (LOG_ERR, "network: sendto failed: %s",
885                                                 strerror (errno));
886                                 break;
887                         }
889                         break;
890                 } /* while (42) */
891         } /* for (sending_sockets) */
893         return 0;
896 static int network_config (const char *key, const char *val)
898         char *node;
899         char *service;
901         char *fields[3];
902         int   fields_num;
904         if ((strcasecmp ("Listen", key) == 0)
905                         || (strcasecmp ("Server", key) == 0))
906         {
907                 char *val_cpy = strdup (val);
908                 if (val_cpy == NULL)
909                         return (1);
911                 service = NET_DEFAULT_PORT;
912                 fields_num = strsplit (val_cpy, fields, 3);
913                 if ((fields_num != 1)
914                                 && (fields_num != 2))
915                         return (1);
916                 else if (fields_num == 2)
917                         service = fields[1];
918                 node = fields[0];
920                 if (strcasecmp ("Listen", key) == 0)
921                         network_add_listen_socket (node, service);
922                 else
923                         network_add_sending_socket (node, service);
924         }
925         else if (strcasecmp ("TimeToLive", key) == 0)
926         {
927                 int tmp = atoi (val);
928                 if ((tmp > 0) && (tmp < 256))
929                         network_config_ttl = tmp;
930                 else
931                         return (1);
932         }
933         else
934         {
935                 return (-1);
936         }
937         return (0);
940 static int network_shutdown (void)
942         DBG ("Shutting down.");
944         listen_loop++;
946         pthread_kill (listen_thread, SIGTERM);
947         pthread_join (listen_thread, NULL /* no return value */);
949         listen_thread = 0;
951         return (0);
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))
963         {
964                 int status;
966                 status = pthread_create (&listen_thread, NULL /* no attributes */,
967                                 receive_thread, NULL /* no argument */);
969                 if (status != 0)
970                         syslog (LOG_ERR, "network: pthread_create failed: %s",
971                                         strerror (errno));
972         }
973         return (0);
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);