2 * collectd - src/libcollectdclient/network.c
3 * Copyright (C) 2005-2015 Florian Forster
4 * Copyright (C) 2010 Max Henkel
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 * Florian Forster <octo at collectd.org>
26 * Max Henkel <henkel at gmx.at>
38 #include <sys/types.h>
39 #include <sys/socket.h>
43 # include <netinet/in.h>
50 #include "collectd/network.h"
51 #include "collectd/network_buffer.h"
58 lcc_server_t *servers;
67 lcc_security_level_t security_level;
75 lcc_network_buffer_t *buffer;
83 static int server_close_socket (lcc_server_t *srv) /* {{{ */
98 } /* }}} int server_close_socket */
100 static void int_server_destroy (lcc_server_t *srv) /* {{{ */
107 server_close_socket (srv);
113 free (srv->username);
114 free (srv->password);
117 int_server_destroy (next);
118 } /* }}} void int_server_destroy */
120 static int server_open_socket (lcc_server_t *srv) /* {{{ */
122 struct addrinfo ai_hints = { 0 };
123 struct addrinfo *ai_list = NULL;
124 struct addrinfo *ai_ptr;
131 server_close_socket (srv);
134 ai_hints.ai_flags |= AI_ADDRCONFIG;
136 ai_hints.ai_family = AF_UNSPEC;
137 ai_hints.ai_socktype = SOCK_DGRAM;
139 status = getaddrinfo (srv->node, srv->service, &ai_hints, &ai_list);
142 assert (ai_list != NULL);
144 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
146 srv->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
150 if (ai_ptr->ai_family == AF_INET)
152 struct sockaddr_in *addr = (struct sockaddr_in *) ai_ptr->ai_addr;
155 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
156 optname = IP_MULTICAST_TTL;
160 status = setsockopt (srv->fd, IPPROTO_IP, optname,
161 &srv->ttl, sizeof (srv->ttl));
163 else if (ai_ptr->ai_family == AF_INET6)
165 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
166 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai_ptr->ai_addr;
169 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
170 optname = IPV6_MULTICAST_HOPS;
172 optname = IPV6_UNICAST_HOPS;
174 status = setsockopt (srv->fd, IPPROTO_IPV6, optname,
175 &srv->ttl, sizeof (srv->ttl));
179 /* setsockopt failed. */
185 srv->sa = malloc (ai_ptr->ai_addrlen);
193 memcpy (srv->sa, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
194 srv->sa_len = ai_ptr->ai_addrlen;
198 freeaddrinfo (ai_list);
203 } /* }}} int server_open_socket */
205 static int server_send_buffer (lcc_server_t *srv) /* {{{ */
207 char buffer[LCC_NETWORK_BUFFER_SIZE_DEFAULT];
213 status = server_open_socket (srv);
218 memset (buffer, 0, sizeof (buffer));
219 buffer_size = sizeof (buffer);
221 status = lcc_network_buffer_finalize (srv->buffer);
224 lcc_network_buffer_initialize (srv->buffer);
228 status = lcc_network_buffer_get (srv->buffer, buffer, &buffer_size);
229 lcc_network_buffer_initialize (srv->buffer);
234 if (buffer_size > sizeof (buffer))
235 buffer_size = sizeof (buffer);
239 assert (srv->fd >= 0);
240 assert (srv->sa != NULL);
241 status = (int) sendto (srv->fd, buffer, buffer_size, /* flags = */ 0,
242 srv->sa, srv->sa_len);
243 if ((status < 0) && ((errno == EINTR) || (errno == EAGAIN)))
252 } /* }}} int server_send_buffer */
254 static int server_value_add (lcc_server_t *srv, /* {{{ */
255 const lcc_value_list_t *vl)
259 status = lcc_network_buffer_add_value (srv->buffer, vl);
263 server_send_buffer (srv);
264 return (lcc_network_buffer_add_value (srv->buffer, vl));
265 } /* }}} int server_value_add */
270 lcc_network_t *lcc_network_create (void) /* {{{ */
274 net = malloc (sizeof (*net));
277 memset (net, 0, sizeof (*net));
282 } /* }}} lcc_network_t *lcc_network_create */
284 void lcc_network_destroy (lcc_network_t *net) /* {{{ */
288 int_server_destroy (net->servers);
290 } /* }}} void lcc_network_destroy */
292 lcc_server_t *lcc_server_create (lcc_network_t *net, /* {{{ */
293 const char *node, const char *service)
297 if ((net == NULL) || (node == NULL))
300 service = NET_DEFAULT_PORT;
302 srv = malloc (sizeof (*srv));
305 memset (srv, 0, sizeof (*srv));
308 srv->security_level = NONE;
309 srv->username = NULL;
310 srv->password = NULL;
313 srv->node = strdup (node);
314 if (srv->node == NULL)
320 srv->service = strdup (service);
321 if (srv->service == NULL)
328 srv->buffer = lcc_network_buffer_create (/* size = */ 0);
329 if (srv->buffer == NULL)
337 if (net->servers == NULL)
343 lcc_server_t *last = net->servers;
345 while (last->next != NULL)
352 } /* }}} lcc_server_t *lcc_server_create */
354 int lcc_server_destroy (lcc_network_t *net, lcc_server_t *srv) /* {{{ */
356 if ((net == NULL) || (srv == NULL))
359 if (net->servers == srv)
361 net->servers = srv->next;
366 lcc_server_t *prev = net->servers;
368 while ((prev != NULL) && (prev->next != srv))
374 prev->next = srv->next;
378 int_server_destroy (srv);
381 } /* }}} int lcc_server_destroy */
383 int lcc_server_set_ttl (lcc_server_t *srv, uint8_t ttl) /* {{{ */
388 srv->ttl = (int) ttl;
391 } /* }}} int lcc_server_set_ttl */
393 int lcc_server_set_interface (lcc_server_t *srv, char const *interface) /* {{{ */
395 unsigned int if_index;
398 if ((srv == NULL) || (interface == NULL))
401 if_index = if_nametoindex (interface);
406 if (srv->sa->sa_family == AF_INET)
408 struct sockaddr_in *addr = (struct sockaddr_in *) srv->sa;
410 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
412 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
413 /* If possible, use the "ip_mreqn" structure which has
414 * an "interface index" member. Using the interface
415 * index is preferred here, because of its similarity
416 * to the way IPv6 handles this. Unfortunately, it
417 * appears not to be portable. */
418 struct ip_mreqn mreq;
420 memset (&mreq, 0, sizeof (mreq));
421 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
422 mreq.imr_address.s_addr = ntohl (INADDR_ANY);
423 mreq.imr_ifindex = (int) if_index;
427 memset (&mreq, 0, sizeof (mreq));
428 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
429 mreq.imr_interface.s_addr = ntohl (INADDR_ANY);
432 status = setsockopt (srv->fd, IPPROTO_IP, IP_MULTICAST_IF,
433 &mreq, sizeof (mreq));
442 if (srv->sa->sa_family == AF_INET6)
444 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) srv->sa;
446 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
448 status = setsockopt (srv->fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
449 &if_index, sizeof (if_index));
457 /* else: Not a multicast interface. */
458 #if defined(SO_BINDTODEVICE)
459 status = setsockopt (srv->fd, SOL_SOCKET, SO_BINDTODEVICE, interface,
460 (socklen_t) (strlen (interface) + 1));
466 } /* }}} int lcc_server_set_interface */
468 int lcc_server_set_security_level (lcc_server_t *srv, /* {{{ */
469 lcc_security_level_t level,
470 const char *username, const char *password)
472 return (lcc_network_buffer_set_security_level (srv->buffer,
473 level, username, password));
474 } /* }}} int lcc_server_set_security_level */
476 int lcc_network_values_send (lcc_network_t *net, /* {{{ */
477 const lcc_value_list_t *vl)
481 if ((net == NULL) || (vl == NULL))
484 for (srv = net->servers; srv != NULL; srv = srv->next)
485 server_value_add (srv, vl);
488 } /* }}} int lcc_network_values_send */
490 /* vim: set sw=2 sts=2 et fdm=marker : */