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.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * @file ats/ats_api_scanner.c
20 * @brief LAN interface scanning to determine IPs in LAN
21 * @author Christian Grothoff
22 * @author Matthias Wachs
25 #include "gnunet_ats_service.h"
28 * How frequently do we scan the interfaces for changes to the addresses?
30 #define INTERFACE_PROCESSING_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
34 * Convert a `enum GNUNET_ATS_Network_Type` to a string
36 * @param net the network type
37 * @return a string or NULL if invalid
40 GNUNET_ATS_print_network_type (enum GNUNET_ATS_Network_Type net)
44 case GNUNET_ATS_NET_UNSPECIFIED:
46 case GNUNET_ATS_NET_LOOPBACK:
48 case GNUNET_ATS_NET_LAN:
50 case GNUNET_ATS_NET_WAN:
52 case GNUNET_ATS_NET_WLAN:
54 case GNUNET_ATS_NET_BT:
63 * Convert ATS properties from host to network byte order.
65 * @param nbo[OUT] value written
66 * @param hbo value read
69 GNUNET_ATS_properties_hton (struct GNUNET_ATS_PropertiesNBO *nbo,
70 const struct GNUNET_ATS_Properties *hbo)
72 nbo->utilization_out = htonl (hbo->utilization_out);
73 nbo->utilization_in = htonl (hbo->utilization_in);
74 nbo->scope = htonl ((uint32_t) hbo->scope);
75 nbo->distance = htonl (hbo->distance);
76 nbo->delay = GNUNET_TIME_relative_hton (hbo->delay);
81 * Convert ATS properties from network to host byte order.
83 * @param hbo[OUT] value written
84 * @param nbo value read
87 GNUNET_ATS_properties_ntoh (struct GNUNET_ATS_Properties *hbo,
88 const struct GNUNET_ATS_PropertiesNBO *nbo)
90 hbo->utilization_out = ntohl (nbo->utilization_out);
91 hbo->utilization_in = ntohl (nbo->utilization_in);
92 hbo->scope = ntohl ((uint32_t) nbo->scope);
93 hbo->distance = ntohl (nbo->distance);
94 hbo->delay = GNUNET_TIME_relative_ntoh (nbo->delay);
99 * We keep a list of our local networks so we can answer
100 * LAN vs. WAN questions. Note: WLAN is not detected yet.
101 * (maybe we can do that heuristically based on interface
102 * name in the future?).
109 struct ATS_Network *next;
114 struct ATS_Network *prev;
119 struct sockaddr *network;
122 * Netmask to determine what is in the LAN.
124 struct sockaddr *netmask;
127 * How long are @e network and @e netmask?
134 * Handle to the interface scanner.
136 struct GNUNET_ATS_InterfaceScanner
140 * Head of LAN networks list.
142 struct ATS_Network *net_head;
145 * Tail of LAN networks list.
147 struct ATS_Network *net_tail;
150 * Task for periodically refreshing our LAN network list.
152 struct GNUNET_SCHEDULER_Task *interface_task;
158 * Delete all entries from the current network list.
160 * @param is scanner to clean up
163 delete_networks (struct GNUNET_ATS_InterfaceScanner *is)
165 struct ATS_Network *cur;
167 while (NULL != (cur = is->net_head))
169 GNUNET_CONTAINER_DLL_remove (is->net_head,
178 * Function invoked for each interface found. Adds the interface's
179 * network addresses to the respective DLL, so we can distinguish
180 * between LAN and WAN.
182 * @param cls closure with the `struct GNUNET_ATS_InterfaceScanner`
183 * @param name name of the interface (can be NULL for unknown)
184 * @param isDefault is this presumably the default interface
185 * @param addr address of this interface (can be NULL for unknown or unassigned)
186 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
187 * @param netmask the network mask (can be NULL for unknown or unassigned)
188 * @param addrlen length of the address
189 * @return #GNUNET_OK to continue iteration
192 interface_proc (void *cls,
195 const struct sockaddr *addr,
196 const struct sockaddr *broadcast_addr,
197 const struct sockaddr *netmask,
200 struct GNUNET_ATS_InterfaceScanner *is = cls;
201 /* Calculate network */
202 struct ATS_Network *net = NULL;
204 /* Skipping IPv4 loopback addresses since we have special check */
205 if (addr->sa_family == AF_INET)
207 const struct sockaddr_in *a4 = (const struct sockaddr_in *) addr;
209 if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
212 /* Skipping IPv6 loopback addresses since we have special check */
213 if (addr->sa_family == AF_INET6)
215 const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *) addr;
216 if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
220 if (addr->sa_family == AF_INET)
222 const struct sockaddr_in *addr4 = (const struct sockaddr_in *) addr;
223 const struct sockaddr_in *netmask4 = (const struct sockaddr_in *) netmask;
224 struct sockaddr_in *tmp;
225 struct sockaddr_in network4;
227 net = GNUNET_malloc (sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in));
228 tmp = (struct sockaddr_in *) &net[1];
229 net->network = (struct sockaddr *) &tmp[0];
230 net->netmask = (struct sockaddr *) &tmp[1];
231 net->length = addrlen;
233 memset (&network4, 0, sizeof (network4));
234 network4.sin_family = AF_INET;
235 #if HAVE_SOCKADDR_IN_SIN_LEN
236 network4.sin_len = sizeof (network4);
238 network4.sin_addr.s_addr = (addr4->sin_addr.s_addr & netmask4->sin_addr.s_addr);
240 GNUNET_memcpy (net->netmask, netmask4, sizeof (struct sockaddr_in));
241 GNUNET_memcpy (net->network, &network4, sizeof (struct sockaddr_in));
244 if (addr->sa_family == AF_INET6)
246 const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *) addr;
247 const struct sockaddr_in6 *netmask6 = (const struct sockaddr_in6 *) netmask;
248 struct sockaddr_in6 * tmp;
249 struct sockaddr_in6 network6;
251 net = GNUNET_malloc (sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in6));
252 tmp = (struct sockaddr_in6 *) &net[1];
253 net->network = (struct sockaddr *) &tmp[0];
254 net->netmask = (struct sockaddr *) &tmp[1];
255 net->length = addrlen;
257 memset (&network6, 0, sizeof (network6));
258 network6.sin6_family = AF_INET6;
259 #if HAVE_SOCKADDR_IN_SIN_LEN
260 network6.sin6_len = sizeof (network6);
263 uint32_t *addr_elem = (uint32_t *) &addr6->sin6_addr;
264 uint32_t *mask_elem = (uint32_t *) &netmask6->sin6_addr;
265 uint32_t *net_elem = (uint32_t *) &network6.sin6_addr;
266 for (c = 0; c < 4; c++)
267 net_elem[c] = addr_elem[c] & mask_elem[c];
269 GNUNET_memcpy (net->netmask, netmask6, sizeof (struct sockaddr_in6));
270 GNUNET_memcpy (net->network, &network6, sizeof (struct sockaddr_in6));
273 return GNUNET_OK; /* odd / unsupported address family */
277 char * netmask = GNUNET_strdup (GNUNET_a2s((struct sockaddr *) net->netmask, addrlen));
278 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
280 "Adding network `%s', netmask `%s'\n",
281 GNUNET_a2s ((struct sockaddr *) net->network,
284 GNUNET_free (netmask);
286 GNUNET_CONTAINER_DLL_insert (is->net_head,
295 * Periodically get list of network addresses from our interfaces.
300 get_addresses (void *cls)
302 struct GNUNET_ATS_InterfaceScanner *is = cls;
304 is->interface_task = NULL;
305 delete_networks (is);
306 GNUNET_OS_network_interfaces_list (&interface_proc,
308 is->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVAL,
315 * Returns where the address is located: LAN or WAN or ...
317 * @param is the interface scanner handle
318 * @param addr address
319 * @param addrlen address length
320 * @return type of the network the address belongs to
322 enum GNUNET_ATS_Network_Type
323 GNUNET_ATS_scanner_address_get_type (struct GNUNET_ATS_InterfaceScanner *is,
324 const struct sockaddr *addr,
327 struct ATS_Network *cur = is->net_head;
328 enum GNUNET_ATS_Network_Type type = GNUNET_ATS_NET_UNSPECIFIED;
330 switch (addr->sa_family)
333 type = GNUNET_ATS_NET_LOOPBACK;
337 const struct sockaddr_in *a4 = (const struct sockaddr_in *) addr;
339 if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
340 type = GNUNET_ATS_NET_LOOPBACK;
345 const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *) addr;
347 if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
348 type = GNUNET_ATS_NET_LOOPBACK;
356 /* Check local networks */
357 while ((NULL != cur) && (GNUNET_ATS_NET_UNSPECIFIED == type))
359 if (addrlen != cur->length)
364 if (addr->sa_family == AF_INET)
366 const struct sockaddr_in *a4 = (const struct sockaddr_in *) addr;
367 const struct sockaddr_in *net4 = (const struct sockaddr_in *) cur->network;
368 const struct sockaddr_in *mask4 = (const struct sockaddr_in *) cur->netmask;
370 if (((a4->sin_addr.s_addr & mask4->sin_addr.s_addr)) == net4->sin_addr.s_addr)
371 type = GNUNET_ATS_NET_LAN;
373 if (addr->sa_family == AF_INET6)
375 const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *) addr;
376 const struct sockaddr_in6 *net6 = (const struct sockaddr_in6 *) cur->network;
377 const struct sockaddr_in6 *mask6 = (const struct sockaddr_in6 *) cur->netmask;
379 int res = GNUNET_YES;
381 uint32_t *addr_elem = (uint32_t *) &a6->sin6_addr;
382 uint32_t *mask_elem = (uint32_t *) &mask6->sin6_addr;
383 uint32_t *net_elem = (uint32_t *) &net6->sin6_addr;
384 for (c = 0; c < 4; c++)
385 if ((addr_elem[c] & mask_elem[c]) != net_elem[c])
388 if (res == GNUNET_YES)
389 type = GNUNET_ATS_NET_LAN;
394 /* no local network found for this address, default: WAN */
395 if (type == GNUNET_ATS_NET_UNSPECIFIED)
396 type = GNUNET_ATS_NET_WAN;
397 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
399 "`%s' is in network `%s'\n",
402 GNUNET_ATS_print_network_type (type));
408 * Initialize the interface scanner.
410 * @return interface scanner
412 struct GNUNET_ATS_InterfaceScanner *
413 GNUNET_ATS_scanner_init ()
415 struct GNUNET_ATS_InterfaceScanner *is;
417 is = GNUNET_new (struct GNUNET_ATS_InterfaceScanner);
418 GNUNET_OS_network_interfaces_list (&interface_proc,
420 is->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVAL,
428 * Client is done with the interface scanner, release resources.
430 * @param is handle to release
433 GNUNET_ATS_scanner_done (struct GNUNET_ATS_InterfaceScanner *is)
435 if (NULL != is->interface_task)
437 GNUNET_SCHEDULER_cancel (is->interface_task);
438 is->interface_task = NULL;
440 delete_networks (is);
445 /* end of ats_api_scanner.c */