2 * collectd - src/network.c
3 * Copyright (C) 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; either version 2 of the License, or (at your
8 * option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Florian octo Forster <octo at verplant.org>
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
41 * The IPv4 Organization Local Scope -- 239.192.0.0/14
43 * 239.192.0.0/14 is defined to be the IPv4 Organization Local Scope, and is
44 * the space from which an organization should allocate sub-ranges when
45 * defining scopes for private use.
47 * Port 25826 is not assigned as of 2005-09-12
50 #define IPV4_MCAST_GROUP "239.192.74.66"
51 #define UDP_PORT 25826
53 /* 1500 - 40 - 8 = Ethernet packet - IPv6 header - UDP header */
54 #define BUFF_SIZE 1452
56 #define BUFF_SIZE 4096
59 extern int operating_mode;
61 static int operating_mode = MODE_CLIENT;
64 typedef struct sockent
67 struct sockaddr_storage *addr;
72 static sockent_t *socklist_head = NULL;
74 static int network_bind_socket (int fd, const struct addrinfo *ai, const sockent_t *se)
78 if (bind (fd, ai->ai_addr, ai->ai_addrlen) == -1)
80 syslog (LOG_ERR, "bind: %s", strerror (errno));
84 if (ai->ai_family == AF_INET)
86 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
87 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
91 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
92 mreq.imr_interface.s_addr = htonl (INADDR_ANY);
94 if (setsockopt (se->fd, IPPROTO_IP, IP_MULTICAST_LOOP,
95 &loop, sizeof (loop)) == -1)
97 syslog (LOG_ERR, "setsockopt: %s", strerror (errno));
101 if (setsockopt (se->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
102 &mreq, sizeof (mreq)) == -1)
104 syslog (LOG_ERR, "setsockopt: %s", strerror (errno));
109 else if (ai->ai_family == AF_INET6)
111 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
112 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
113 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
115 struct ipv6_mreq mreq;
117 memcpy (&mreq.ipv6mr_multiaddr,
119 sizeof (addr->sin6_addr));
121 /* http://developer.apple.com/documentation/Darwin/Reference/ManPages/man4/ip6.4.html
122 * ipv6mr_interface may be set to zeroes to
123 * choose the default multicast interface or to
124 * the index of a particular multicast-capable
125 * interface if the host is multihomed.
126 * Membership is associ-associated with a
127 * single interface; programs running on
128 * multihomed hosts may need to join the same
129 * group on more than one interface.*/
130 mreq6.ipv6mr_interface = 0;
132 if (setsockopt (se->fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
133 &loop, sizeof (loop)) == -1)
135 syslog (LOG_ERR, "setsockopt: %s", strerror (errno));
139 if (setsockopt (se->fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
140 &mreq, sizeof (mreq)) == -1)
142 syslog (LOG_ERR, "setsockopt: %s", strerror (errno));
151 int network_create_socket (const char *node, const char *service)
153 sockent_t *socklist_tail;
155 struct addrinfo ai_hints;
156 struct addrinfo *ai_list, *ai_ptr;
161 DBG ("node = %s, service = %s", node, service);
163 if (operating_mode == MODE_LOCAL)
166 socklist_tail = socklist_head;
167 while ((socklist_tail != NULL) && (socklist_tail->next != NULL))
168 socklist_tail = socklist_tail->next;
170 memset (&ai_hints, '\0', sizeof (ai_hints));
171 ai_hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
172 ai_hints.ai_family = PF_UNSPEC;
173 ai_hints.ai_socktype = SOCK_DGRAM;
174 ai_hints.ai_protocol = IPPROTO_UDP; /* XXX is this right here?!? */
176 if ((ai_return = getaddrinfo (node, service, &ai_hints, &ai_list)) != 0)
178 syslog (LOG_ERR, "getaddrinfo (%s, %s): %s",
179 node == NULL ? "(null)" : node,
180 service == NULL ? "(null)" : service,
181 ai_return == EAI_SYSTEM ? strerror (errno) : gai_strerror (ai_return));
185 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
189 if ((se = (sockent_t *) malloc (sizeof (sockent_t))) == NULL)
191 syslog (LOG_EMERG, "malloc: %s", strerror (errno));
195 if ((se->addr = (struct sockaddr_storage *) malloc (sizeof (struct sockaddr_storage))) == NULL)
197 syslog (LOG_EMERG, "malloc: %s", strerror (errno));
202 assert (sizeof (struct sockaddr_storage) >= ai_ptr->addrlen);
203 memset (se->addr, '\0', sizeof (struct sockaddr_storage));
204 memcpy (se->addr, ai_ptr->ai_addr, ai_ptr->addrlen);
205 se->addrlen = ai_ptr->addrlen;
207 se->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
212 syslog (LOG_ERR, "socket: %s", strerror (errno));
218 if (operating_mode == MODE_SERVER)
219 if (network_bind_socket (se->fd, ai_ptr, se->addr) != 0)
226 if (socklist_tail == NULL)
228 socklist_head = socklist_tail = se;
232 socklist_tail->next = se;
238 /* We don't open more than one write-socket per node/service pair.. */
239 if (operating_mode == MODE_CLIENT)
243 freeaddrinfo (ai_list);
248 static int network_get_listen_socket (void)
260 for (se = socklist_head; se != NULL; se = se->next)
262 FD_SET (se->fd, &readfds);
263 if (se->fd >= max_fd)
269 syslog (LOG_WARNING, "No listen sockets found!");
273 status = select (max_fd, &readfds, NULL, NULL, NULL);
275 if ((status == -1) && (errno == EINTR))
277 else if (status == -1)
279 syslog (LOG_ERR, "select: %s", strerror (errno));
287 for (se = socklist_head; se != NULL; se = se->next)
288 if (FD_ISSET (se->fd, &readfds))
295 syslog (LOG_WARNING, "No socket ready..?");
301 int network_receive (char **host, char **type, char **inst, char **value)
304 char buffer[BUFF_SIZE];
306 struct sockaddr_storage addr;
311 assert (operating_mode == MODE_SERVER);
318 if ((fd = network_get_listen_socket ()) < 0)
321 if (recvfrom (fd, buffer, BUFF_SIZE, 0, (struct sockaddr *) &addr, sizeof (addr)) == -1)
323 syslog (LOG_ERR, "recvfrom: %s", strerror (errno));
327 if ((*host = (char *) malloc (BUFF_SIZE)) == NULL)
329 syslog (LOG_EMERG, "malloc: %s", strerror (errno));
333 status = getnameinfo ((struct sockaddr *) &addr, sizeof (addr),
334 *host, BUFF_SIZE, NULL, 0, 0);
337 free (*host); *host = NULL;
338 syslog (LOG_ERR, "getnameinfo: %s",
339 status == EAI_SYSTEM ? strerror (errno) : gai_strerror (status));
343 if (strsplit (buffer, fields, 4) != 3)
345 syslog (LOG_WARNING, "Invalid message from `%s'", *host);
346 free (*host); *host = NULL;
350 if ((*type = strdup (fields[0])) == NULL)
352 syslog (LOG_EMERG, "strdup: %s", strerror ());
353 free (*host); *host = NULL;
357 if ((*inst = strdup (fields[1])) == NULL)
359 syslog (LOG_EMERG, "strdup: %s", strerror ());
360 free (*host); *host = NULL;
361 free (*type); *type = NULL;
365 if ((*value = strdup (fields[2])) == NULL)
367 syslog (LOG_EMERG, "strdup: %s", strerror ());
368 free (*host); *host = NULL;
369 free (*type); *type = NULL;
370 free (*inst); *inst = NULL;
374 DBG ("host = %s, type = %s, inst = %s, value = %s",
375 *host, *type, *inst, *value);
380 int network_send (char *type, char *inst, char *value)
390 DBG ("type = %s, inst = %s, value = %s", type, inst, value);
392 assert (operating_mode == MODE_CLIENT);
394 buflen = snprintf (buf, BUFF_SIZE, "%s %s %s", type, inst, value);
395 if ((buflen >= BUFF_SIZE) || (buflen < 1))
397 syslog (LOG_WARNING, "network_send: snprintf failed..");
404 for (se = socklist_head; se != NULL; se = se->next)
406 DBG ("fd = %i", se->fd);
410 status = sendto (se->fd, buf, buflen, 0,
411 (struct sockaddr *) se->addr, se->addrlen);
417 DBG ("sendto was interrupted");
422 syslog (LOG_ERR, "sendto: %s", strerror (errno));
433 syslog (LOG_WARNING, "Message wasn't sent to anybody..");