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>
40 * The IPv4 Organization Local Scope -- 239.192.0.0/14
42 * 239.192.0.0/14 is defined to be the IPv4 Organization Local Scope, and is
43 * the space from which an organization should allocate sub-ranges when
44 * defining scopes for private use.
46 * Port 25826 is not assigned as of 2005-09-12
49 #define IPV4_MCAST_GROUP "239.192.74.66"
50 #define UDP_PORT 25826
52 /* 1500 - 40 - 8 = Ethernet packet - IPv6 header - UDP header */
53 #define BUFF_SIZE 1452
55 typedef struct socklist
58 struct socklist *next;
61 static socklist_t *listen_socks_head = NULL;
63 uint16_t get_port (void)
69 port_str = cf_get_option ("Port", NULL);
73 port_int = atoi (port_str);
78 ret = htons (port_int);
82 int network_create_listen_socket (const char *node, const char *service)
84 socklist_t *socklist_tail;
86 struct addrinfo ai_hints;
87 struct addrinfo *ai_list, *ai_ptr;
92 socklist_tail = listen_socks_head;
93 while ((socklist_tail != NULL) && (socklist_tail->next != NULL))
94 socklist_tail = socklist_tail->next;
96 memset (&ai_hints, '\0', sizeof (ai_hints));
97 ai_hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
98 ai_hints.ai_family = AF_UNSPEC;
99 ai_hints.ai_socktype = SOCK_DGRAM;
100 ai_hints.ai_protocol = IPPROTO_UDP;
102 if ((ai_return = getaddrinfo (node, service, &ai_hints, &ai_list)) != 0)
104 syslog (LOG_ERR, "getaddrinfo (%s, %s): %s",
105 node == NULL ? "(null)" : node,
106 service == NULL ? "(null)" : service,
107 ai_return == EAI_SYSTEM ? strerror (errno) : gai_strerror (ai_return));
111 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
113 socklist_t *socklist_ent;
115 if ((socklist_ent = (socklist_t *) malloc (sizeof (socklist_t))) == NULL)
117 syslog (LOG_EMERG, "malloc: %s", strerror (errno));
121 socklist_ent->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
122 socklist_ent->next = NULL;
124 if (socklist_ent->fd == -1)
126 syslog (LOG_ERR, "socket: %s", strerror (errno));
131 if (bind (socklist_ent->fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen) == -1)
133 syslog (LOG_ERR, "bind: %s", strerror (errno));
134 close (socklist_ent->fd);
139 if (ai_ptr->ai_family == AF_INET)
141 struct sockaddr_in *addr = (struct sockaddr_in *) ai_ptr->ai_addr;
142 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
146 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
147 mreq.imr_interface.s_addr = htonl (INADDR_ANY);
149 if (setsockopt (socklist_ent->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
150 &mreq, sizeof (mreq)) == -1)
152 syslog (LOG_ERR, "setsockopt: %s", strerror (errno));
153 close (socklist_ent->fd);
159 else if (ai_ptr->ai_family == AF_INET6)
161 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
162 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai_ptr->ai_addr;
163 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
165 struct ipv6_mreq mreq;
167 memcpy (&mreq.ipv6mr_multiaddr,
169 sizeof (addr->sin6_addr));
171 /* FIXME What do I need here? `netdevice(7)'
172 * doesn't tell me either.. */
173 mreq6.ipv6mr_interface = 0;
175 if (setsockopt (socklist_ent->fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
176 &mreq, sizeof (mreq)) == -1)
178 syslog (LOG_ERR, "setsockopt: %s", strerror (errno));
179 close (socklist_ent->fd);
186 if (socklist_tail == NULL)
188 listen_socks_head = socklist_tail = socklist_ent;
192 socklist_tail->next = socklist_ent;
193 socklist_tail = socklist_ent;
198 freeaddrinfo (ai_list);
203 int get_write_socket (void)
210 if ((sd = socket (AF_INET, SOCK_DGRAM, 0)) == -1)
212 syslog (LOG_ERR, "socket: %s", strerror (errno));
219 char *addr_to_host (struct sockaddr_in *addr)
224 if ((he = gethostbyaddr ((char *) &addr->sin_addr, sizeof (addr->sin_addr), AF_INET)) != NULL)
226 host = strdup (he->h_name);
230 char *tmp = inet_ntoa (addr->sin_addr);
237 int network_receive (char **host, char **type, char **instance, char **value)
239 int sd = get_read_socket ();
241 char buffer[BUFF_SIZE];
243 struct sockaddr_in addr;
256 addr_size = sizeof (addr);
258 if (recvfrom (sd, buffer, BUFF_SIZE, 0, (struct sockaddr *) &addr, &addr_size) == -1)
260 syslog (LOG_ERR, "recvfrom: %s", strerror (errno));
264 if (strsplit (buffer, fields, 4) != 3)
267 *host = addr_to_host (&addr);
268 *type = strdup (fields[0]);
269 *instance = strdup (fields[1]);
270 *value = strdup (fields[2]);
272 if (*host == NULL || *type == NULL || *instance == NULL || *value == NULL)
278 int network_send (char *type, char *instance, char *value)
280 int sd = get_write_socket ();
281 struct sockaddr_in addr;
289 if ((buflen = snprintf (buf, BUFF_SIZE, "%s %s %s", type, instance, value)) >= BUFF_SIZE)
291 syslog (LOG_WARNING, "network_send: Output truncated..");
294 buf[buflen++] = '\0';
296 memset(&addr, '\0', sizeof (addr));
297 addr.sin_family = AF_INET;
298 addr.sin_addr.s_addr = inet_addr (IPV4_MCAST_GROUP);
299 addr.sin_port = get_port ();
301 return (sendto (sd, buf, buflen, 0, (struct sockaddr *) &addr, sizeof (addr)));