2 This file is part of GNUnet.
3 Copyright (C) 2010-2015 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet 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 Affero General Public License for more details.
16 * @file ats/ats_api_scanner.c
17 * @brief LAN interface scanning to determine IPs in LAN
18 * @author Christian Grothoff
19 * @author Matthias Wachs
22 #include "gnunet_ats_service.h"
25 * How frequently do we scan the interfaces for changes to the addresses?
27 #define INTERFACE_PROCESSING_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
31 * Convert a `enum GNUNET_ATS_Network_Type` to a string
33 * @param net the network type
34 * @return a string or NULL if invalid
37 GNUNET_ATS_print_network_type (enum GNUNET_ATS_Network_Type net)
41 case GNUNET_ATS_NET_UNSPECIFIED:
43 case GNUNET_ATS_NET_LOOPBACK:
45 case GNUNET_ATS_NET_LAN:
47 case GNUNET_ATS_NET_WAN:
49 case GNUNET_ATS_NET_WLAN:
51 case GNUNET_ATS_NET_BT:
60 * Convert ATS properties from host to network byte order.
62 * @param nbo[OUT] value written
63 * @param hbo value read
66 GNUNET_ATS_properties_hton (struct GNUNET_ATS_PropertiesNBO *nbo,
67 const struct GNUNET_ATS_Properties *hbo)
69 nbo->utilization_out = htonl (hbo->utilization_out);
70 nbo->utilization_in = htonl (hbo->utilization_in);
71 nbo->scope = htonl ((uint32_t) hbo->scope);
72 nbo->distance = htonl (hbo->distance);
73 nbo->delay = GNUNET_TIME_relative_hton (hbo->delay);
78 * Convert ATS properties from network to host byte order.
80 * @param hbo[OUT] value written
81 * @param nbo value read
84 GNUNET_ATS_properties_ntoh (struct GNUNET_ATS_Properties *hbo,
85 const struct GNUNET_ATS_PropertiesNBO *nbo)
87 hbo->utilization_out = ntohl (nbo->utilization_out);
88 hbo->utilization_in = ntohl (nbo->utilization_in);
89 hbo->scope = ntohl ((uint32_t) nbo->scope);
90 hbo->distance = ntohl (nbo->distance);
91 hbo->delay = GNUNET_TIME_relative_ntoh (nbo->delay);
96 * We keep a list of our local networks so we can answer
97 * LAN vs. WAN questions. Note: WLAN is not detected yet.
98 * (maybe we can do that heuristically based on interface
99 * name in the future?).
106 struct ATS_Network *next;
111 struct ATS_Network *prev;
116 struct sockaddr *network;
119 * Netmask to determine what is in the LAN.
121 struct sockaddr *netmask;
124 * How long are @e network and @e netmask?
131 * Handle to the interface scanner.
133 struct GNUNET_ATS_InterfaceScanner
137 * Head of LAN networks list.
139 struct ATS_Network *net_head;
142 * Tail of LAN networks list.
144 struct ATS_Network *net_tail;
147 * Task for periodically refreshing our LAN network list.
149 struct GNUNET_SCHEDULER_Task *interface_task;
155 * Delete all entries from the current network list.
157 * @param is scanner to clean up
160 delete_networks (struct GNUNET_ATS_InterfaceScanner *is)
162 struct ATS_Network *cur;
164 while (NULL != (cur = is->net_head))
166 GNUNET_CONTAINER_DLL_remove (is->net_head,
175 * Function invoked for each interface found. Adds the interface's
176 * network addresses to the respective DLL, so we can distinguish
177 * between LAN and WAN.
179 * @param cls closure with the `struct GNUNET_ATS_InterfaceScanner`
180 * @param name name of the interface (can be NULL for unknown)
181 * @param isDefault is this presumably the default interface
182 * @param addr address of this interface (can be NULL for unknown or unassigned)
183 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
184 * @param netmask the network mask (can be NULL for unknown or unassigned)
185 * @param addrlen length of the address
186 * @return #GNUNET_OK to continue iteration
189 interface_proc (void *cls,
192 const struct sockaddr *addr,
193 const struct sockaddr *broadcast_addr,
194 const struct sockaddr *netmask,
197 struct GNUNET_ATS_InterfaceScanner *is = cls;
198 /* Calculate network */
199 struct ATS_Network *net = NULL;
201 /* Skipping IPv4 loopback addresses since we have special check */
202 if (addr->sa_family == AF_INET)
204 const struct sockaddr_in *a4 = (const struct sockaddr_in *) addr;
206 if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
209 /* Skipping IPv6 loopback addresses since we have special check */
210 if (addr->sa_family == AF_INET6)
212 const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *) addr;
213 if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
217 if (addr->sa_family == AF_INET)
219 const struct sockaddr_in *addr4 = (const struct sockaddr_in *) addr;
220 const struct sockaddr_in *netmask4 = (const struct sockaddr_in *) netmask;
221 struct sockaddr_in *tmp;
222 struct sockaddr_in network4;
224 net = GNUNET_malloc (sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in));
225 tmp = (struct sockaddr_in *) &net[1];
226 net->network = (struct sockaddr *) &tmp[0];
227 net->netmask = (struct sockaddr *) &tmp[1];
228 net->length = addrlen;
230 memset (&network4, 0, sizeof (network4));
231 network4.sin_family = AF_INET;
232 #if HAVE_SOCKADDR_IN_SIN_LEN
233 network4.sin_len = sizeof (network4);
235 network4.sin_addr.s_addr = (addr4->sin_addr.s_addr & netmask4->sin_addr.s_addr);
237 GNUNET_memcpy (net->netmask, netmask4, sizeof (struct sockaddr_in));
238 GNUNET_memcpy (net->network, &network4, sizeof (struct sockaddr_in));
241 if (addr->sa_family == AF_INET6)
243 const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *) addr;
244 const struct sockaddr_in6 *netmask6 = (const struct sockaddr_in6 *) netmask;
245 struct sockaddr_in6 * tmp;
246 struct sockaddr_in6 network6;
248 net = GNUNET_malloc (sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in6));
249 tmp = (struct sockaddr_in6 *) &net[1];
250 net->network = (struct sockaddr *) &tmp[0];
251 net->netmask = (struct sockaddr *) &tmp[1];
252 net->length = addrlen;
254 memset (&network6, 0, sizeof (network6));
255 network6.sin6_family = AF_INET6;
256 #if HAVE_SOCKADDR_IN_SIN_LEN
257 network6.sin6_len = sizeof (network6);
260 uint32_t *addr_elem = (uint32_t *) &addr6->sin6_addr;
261 uint32_t *mask_elem = (uint32_t *) &netmask6->sin6_addr;
262 uint32_t *net_elem = (uint32_t *) &network6.sin6_addr;
263 for (c = 0; c < 4; c++)
264 net_elem[c] = addr_elem[c] & mask_elem[c];
266 GNUNET_memcpy (net->netmask, netmask6, sizeof (struct sockaddr_in6));
267 GNUNET_memcpy (net->network, &network6, sizeof (struct sockaddr_in6));
270 return GNUNET_OK; /* odd / unsupported address family */
274 char * netmask = GNUNET_strdup (GNUNET_a2s((struct sockaddr *) net->netmask, addrlen));
275 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
277 "Adding network `%s', netmask `%s'\n",
278 GNUNET_a2s ((struct sockaddr *) net->network,
281 GNUNET_free (netmask);
283 GNUNET_CONTAINER_DLL_insert (is->net_head,
292 * Periodically get list of network addresses from our interfaces.
297 get_addresses (void *cls)
299 struct GNUNET_ATS_InterfaceScanner *is = cls;
301 is->interface_task = NULL;
302 delete_networks (is);
303 GNUNET_OS_network_interfaces_list (&interface_proc,
305 is->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVAL,
312 * Returns where the address is located: LAN or WAN or ...
314 * @param is the interface scanner handle
315 * @param addr address
316 * @param addrlen address length
317 * @return type of the network the address belongs to
319 enum GNUNET_ATS_Network_Type
320 GNUNET_ATS_scanner_address_get_type (struct GNUNET_ATS_InterfaceScanner *is,
321 const struct sockaddr *addr,
324 struct ATS_Network *cur = is->net_head;
325 enum GNUNET_ATS_Network_Type type = GNUNET_ATS_NET_UNSPECIFIED;
327 switch (addr->sa_family)
330 type = GNUNET_ATS_NET_LOOPBACK;
334 const struct sockaddr_in *a4 = (const struct sockaddr_in *) addr;
336 if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
337 type = GNUNET_ATS_NET_LOOPBACK;
342 const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *) addr;
344 if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
345 type = GNUNET_ATS_NET_LOOPBACK;
353 /* Check local networks */
354 while ((NULL != cur) && (GNUNET_ATS_NET_UNSPECIFIED == type))
356 if (addrlen != cur->length)
361 if (addr->sa_family == AF_INET)
363 const struct sockaddr_in *a4 = (const struct sockaddr_in *) addr;
364 const struct sockaddr_in *net4 = (const struct sockaddr_in *) cur->network;
365 const struct sockaddr_in *mask4 = (const struct sockaddr_in *) cur->netmask;
367 if (((a4->sin_addr.s_addr & mask4->sin_addr.s_addr)) == net4->sin_addr.s_addr)
368 type = GNUNET_ATS_NET_LAN;
370 if (addr->sa_family == AF_INET6)
372 const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *) addr;
373 const struct sockaddr_in6 *net6 = (const struct sockaddr_in6 *) cur->network;
374 const struct sockaddr_in6 *mask6 = (const struct sockaddr_in6 *) cur->netmask;
376 int res = GNUNET_YES;
378 uint32_t *addr_elem = (uint32_t *) &a6->sin6_addr;
379 uint32_t *mask_elem = (uint32_t *) &mask6->sin6_addr;
380 uint32_t *net_elem = (uint32_t *) &net6->sin6_addr;
381 for (c = 0; c < 4; c++)
382 if ((addr_elem[c] & mask_elem[c]) != net_elem[c])
385 if (res == GNUNET_YES)
386 type = GNUNET_ATS_NET_LAN;
391 /* no local network found for this address, default: WAN */
392 if (type == GNUNET_ATS_NET_UNSPECIFIED)
393 type = GNUNET_ATS_NET_WAN;
394 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
396 "`%s' is in network `%s'\n",
399 GNUNET_ATS_print_network_type (type));
405 * Initialize the interface scanner.
407 * @return interface scanner
409 struct GNUNET_ATS_InterfaceScanner *
410 GNUNET_ATS_scanner_init ()
412 struct GNUNET_ATS_InterfaceScanner *is;
414 is = GNUNET_new (struct GNUNET_ATS_InterfaceScanner);
415 GNUNET_OS_network_interfaces_list (&interface_proc,
417 is->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVAL,
425 * Client is done with the interface scanner, release resources.
427 * @param is handle to release
430 GNUNET_ATS_scanner_done (struct GNUNET_ATS_InterfaceScanner *is)
432 if (NULL != is->interface_task)
434 GNUNET_SCHEDULER_cancel (is->interface_task);
435 is->interface_task = NULL;
437 delete_networks (is);
442 /* end of ats_api_scanner.c */