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
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file ats/ats_api_scanner.c
22 * @brief LAN interface scanning to determine IPs in LAN
23 * @author Christian Grothoff
24 * @author Matthias Wachs
27 #include "gnunet_ats_service.h"
30 * How frequently do we scan the interfaces for changes to the addresses?
32 #define INTERFACE_PROCESSING_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
36 * Convert a `enum GNUNET_ATS_Network_Type` to a string
38 * @param net the network type
39 * @return a string or NULL if invalid
42 GNUNET_ATS_print_network_type (enum GNUNET_ATS_Network_Type net)
46 case GNUNET_ATS_NET_UNSPECIFIED:
48 case GNUNET_ATS_NET_LOOPBACK:
50 case GNUNET_ATS_NET_LAN:
52 case GNUNET_ATS_NET_WAN:
54 case GNUNET_ATS_NET_WLAN:
56 case GNUNET_ATS_NET_BT:
65 * Convert ATS properties from host to network byte order.
67 * @param nbo[OUT] value written
68 * @param hbo value read
71 GNUNET_ATS_properties_hton (struct GNUNET_ATS_PropertiesNBO *nbo,
72 const struct GNUNET_ATS_Properties *hbo)
74 nbo->utilization_out = htonl (hbo->utilization_out);
75 nbo->utilization_in = htonl (hbo->utilization_in);
76 nbo->scope = htonl ((uint32_t) hbo->scope);
77 nbo->distance = htonl (hbo->distance);
78 nbo->delay = GNUNET_TIME_relative_hton (hbo->delay);
83 * Convert ATS properties from network to host byte order.
85 * @param hbo[OUT] value written
86 * @param nbo value read
89 GNUNET_ATS_properties_ntoh (struct GNUNET_ATS_Properties *hbo,
90 const struct GNUNET_ATS_PropertiesNBO *nbo)
92 hbo->utilization_out = ntohl (nbo->utilization_out);
93 hbo->utilization_in = ntohl (nbo->utilization_in);
94 hbo->scope = ntohl ((uint32_t) nbo->scope);
95 hbo->distance = ntohl (nbo->distance);
96 hbo->delay = GNUNET_TIME_relative_ntoh (nbo->delay);
101 * We keep a list of our local networks so we can answer
102 * LAN vs. WAN questions. Note: WLAN is not detected yet.
103 * (maybe we can do that heuristically based on interface
104 * name in the future?).
111 struct ATS_Network *next;
116 struct ATS_Network *prev;
121 struct sockaddr *network;
124 * Netmask to determine what is in the LAN.
126 struct sockaddr *netmask;
129 * How long are @e network and @e netmask?
136 * Handle to the interface scanner.
138 struct GNUNET_ATS_InterfaceScanner
142 * Head of LAN networks list.
144 struct ATS_Network *net_head;
147 * Tail of LAN networks list.
149 struct ATS_Network *net_tail;
152 * Task for periodically refreshing our LAN network list.
154 struct GNUNET_SCHEDULER_Task *interface_task;
160 * Delete all entries from the current network list.
162 * @param is scanner to clean up
165 delete_networks (struct GNUNET_ATS_InterfaceScanner *is)
167 struct ATS_Network *cur;
169 while (NULL != (cur = is->net_head))
171 GNUNET_CONTAINER_DLL_remove (is->net_head,
180 * Function invoked for each interface found. Adds the interface's
181 * network addresses to the respective DLL, so we can distinguish
182 * between LAN and WAN.
184 * @param cls closure with the `struct GNUNET_ATS_InterfaceScanner`
185 * @param name name of the interface (can be NULL for unknown)
186 * @param isDefault is this presumably the default interface
187 * @param addr address of this interface (can be NULL for unknown or unassigned)
188 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
189 * @param netmask the network mask (can be NULL for unknown or unassigned)
190 * @param addrlen length of the address
191 * @return #GNUNET_OK to continue iteration
194 interface_proc (void *cls,
197 const struct sockaddr *addr,
198 const struct sockaddr *broadcast_addr,
199 const struct sockaddr *netmask,
202 struct GNUNET_ATS_InterfaceScanner *is = cls;
203 /* Calculate network */
204 struct ATS_Network *net = NULL;
206 /* Skipping IPv4 loopback addresses since we have special check */
207 if (addr->sa_family == AF_INET)
209 const struct sockaddr_in *a4 = (const struct sockaddr_in *) addr;
211 if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
214 /* Skipping IPv6 loopback addresses since we have special check */
215 if (addr->sa_family == AF_INET6)
217 const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *) addr;
218 if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
222 if (addr->sa_family == AF_INET)
224 const struct sockaddr_in *addr4 = (const struct sockaddr_in *) addr;
225 const struct sockaddr_in *netmask4 = (const struct sockaddr_in *) netmask;
226 struct sockaddr_in *tmp;
227 struct sockaddr_in network4;
229 net = GNUNET_malloc (sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in));
230 tmp = (struct sockaddr_in *) &net[1];
231 net->network = (struct sockaddr *) &tmp[0];
232 net->netmask = (struct sockaddr *) &tmp[1];
233 net->length = addrlen;
235 memset (&network4, 0, sizeof (network4));
236 network4.sin_family = AF_INET;
237 #if HAVE_SOCKADDR_IN_SIN_LEN
238 network4.sin_len = sizeof (network4);
240 network4.sin_addr.s_addr = (addr4->sin_addr.s_addr & netmask4->sin_addr.s_addr);
242 GNUNET_memcpy (net->netmask, netmask4, sizeof (struct sockaddr_in));
243 GNUNET_memcpy (net->network, &network4, sizeof (struct sockaddr_in));
246 if (addr->sa_family == AF_INET6)
248 const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *) addr;
249 const struct sockaddr_in6 *netmask6 = (const struct sockaddr_in6 *) netmask;
250 struct sockaddr_in6 * tmp;
251 struct sockaddr_in6 network6;
253 net = GNUNET_malloc (sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in6));
254 tmp = (struct sockaddr_in6 *) &net[1];
255 net->network = (struct sockaddr *) &tmp[0];
256 net->netmask = (struct sockaddr *) &tmp[1];
257 net->length = addrlen;
259 memset (&network6, 0, sizeof (network6));
260 network6.sin6_family = AF_INET6;
261 #if HAVE_SOCKADDR_IN_SIN_LEN
262 network6.sin6_len = sizeof (network6);
265 uint32_t *addr_elem = (uint32_t *) &addr6->sin6_addr;
266 uint32_t *mask_elem = (uint32_t *) &netmask6->sin6_addr;
267 uint32_t *net_elem = (uint32_t *) &network6.sin6_addr;
268 for (c = 0; c < 4; c++)
269 net_elem[c] = addr_elem[c] & mask_elem[c];
271 GNUNET_memcpy (net->netmask, netmask6, sizeof (struct sockaddr_in6));
272 GNUNET_memcpy (net->network, &network6, sizeof (struct sockaddr_in6));
275 return GNUNET_OK; /* odd / unsupported address family */
279 char * netmask = GNUNET_strdup (GNUNET_a2s((struct sockaddr *) net->netmask, addrlen));
280 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
282 "Adding network `%s', netmask `%s'\n",
283 GNUNET_a2s ((struct sockaddr *) net->network,
286 GNUNET_free (netmask);
288 GNUNET_CONTAINER_DLL_insert (is->net_head,
297 * Periodically get list of network addresses from our interfaces.
302 get_addresses (void *cls)
304 struct GNUNET_ATS_InterfaceScanner *is = cls;
306 is->interface_task = NULL;
307 delete_networks (is);
308 GNUNET_OS_network_interfaces_list (&interface_proc,
310 is->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVAL,
317 * Returns where the address is located: LAN or WAN or ...
319 * @param is the interface scanner handle
320 * @param addr address
321 * @param addrlen address length
322 * @return type of the network the address belongs to
324 enum GNUNET_ATS_Network_Type
325 GNUNET_ATS_scanner_address_get_type (struct GNUNET_ATS_InterfaceScanner *is,
326 const struct sockaddr *addr,
329 struct ATS_Network *cur = is->net_head;
330 enum GNUNET_ATS_Network_Type type = GNUNET_ATS_NET_UNSPECIFIED;
332 switch (addr->sa_family)
335 type = GNUNET_ATS_NET_LOOPBACK;
339 const struct sockaddr_in *a4 = (const struct sockaddr_in *) addr;
341 if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
342 type = GNUNET_ATS_NET_LOOPBACK;
347 const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *) addr;
349 if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
350 type = GNUNET_ATS_NET_LOOPBACK;
358 /* Check local networks */
359 while ((NULL != cur) && (GNUNET_ATS_NET_UNSPECIFIED == type))
361 if (addrlen != cur->length)
366 if (addr->sa_family == AF_INET)
368 const struct sockaddr_in *a4 = (const struct sockaddr_in *) addr;
369 const struct sockaddr_in *net4 = (const struct sockaddr_in *) cur->network;
370 const struct sockaddr_in *mask4 = (const struct sockaddr_in *) cur->netmask;
372 if (((a4->sin_addr.s_addr & mask4->sin_addr.s_addr)) == net4->sin_addr.s_addr)
373 type = GNUNET_ATS_NET_LAN;
375 if (addr->sa_family == AF_INET6)
377 const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *) addr;
378 const struct sockaddr_in6 *net6 = (const struct sockaddr_in6 *) cur->network;
379 const struct sockaddr_in6 *mask6 = (const struct sockaddr_in6 *) cur->netmask;
381 int res = GNUNET_YES;
383 uint32_t *addr_elem = (uint32_t *) &a6->sin6_addr;
384 uint32_t *mask_elem = (uint32_t *) &mask6->sin6_addr;
385 uint32_t *net_elem = (uint32_t *) &net6->sin6_addr;
386 for (c = 0; c < 4; c++)
387 if ((addr_elem[c] & mask_elem[c]) != net_elem[c])
390 if (res == GNUNET_YES)
391 type = GNUNET_ATS_NET_LAN;
396 /* no local network found for this address, default: WAN */
397 if (type == GNUNET_ATS_NET_UNSPECIFIED)
398 type = GNUNET_ATS_NET_WAN;
399 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
401 "`%s' is in network `%s'\n",
404 GNUNET_ATS_print_network_type (type));
410 * Initialize the interface scanner.
412 * @return interface scanner
414 struct GNUNET_ATS_InterfaceScanner *
415 GNUNET_ATS_scanner_init ()
417 struct GNUNET_ATS_InterfaceScanner *is;
419 is = GNUNET_new (struct GNUNET_ATS_InterfaceScanner);
420 GNUNET_OS_network_interfaces_list (&interface_proc,
422 is->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVAL,
430 * Client is done with the interface scanner, release resources.
432 * @param is handle to release
435 GNUNET_ATS_scanner_done (struct GNUNET_ATS_InterfaceScanner *is)
437 if (NULL != is->interface_task)
439 GNUNET_SCHEDULER_cancel (is->interface_task);
440 is->interface_task = NULL;
442 delete_networks (is);
447 /* end of ats_api_scanner.c */