2 This file is part of GNUnet.
3 Copyright (C) 2010-2015 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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 a ATS property to a string
67 * @param type the property type
68 * @return a string or NULL if invalid
71 GNUNET_ATS_print_property_type (enum GNUNET_ATS_Property type)
75 case GNUNET_ATS_ARRAY_TERMINATOR:
77 case GNUNET_ATS_UTILIZATION_OUT:
78 return "UTILIZATION_UP";
79 case GNUNET_ATS_UTILIZATION_IN:
80 return "UTILIZATION_DOWN";
81 case GNUNET_ATS_NETWORK_TYPE:
82 return "NETWORK_TYPE";
83 case GNUNET_ATS_QUALITY_NET_DELAY:
85 case GNUNET_ATS_QUALITY_NET_DISTANCE:
95 * We keep a list of our local networks so we can answer
96 * LAN vs. WAN questions. Note: WLAN is not detected yet.
97 * (maybe we can do that heuristically based on interface
98 * name in the future?).
105 struct ATS_Network *next;
110 struct ATS_Network *prev;
115 struct sockaddr *network;
118 * Netmask to determine what is in the LAN.
120 struct sockaddr *netmask;
123 * How long are @e network and @e netmask?
130 * Handle to the interface scanner.
132 struct GNUNET_ATS_InterfaceScanner
136 * Head of LAN networks list.
138 struct ATS_Network *net_head;
141 * Tail of LAN networks list.
143 struct ATS_Network *net_tail;
146 * Task for periodically refreshing our LAN network list.
148 struct GNUNET_SCHEDULER_Task *interface_task;
154 * Delete all entries from the current network list.
156 * @param is scanner to clean up
159 delete_networks (struct GNUNET_ATS_InterfaceScanner *is)
161 struct ATS_Network *cur;
163 while (NULL != (cur = is->net_head))
165 GNUNET_CONTAINER_DLL_remove (is->net_head,
174 * Function invoked for each interface found. Adds the interface's
175 * network addresses to the respective DLL, so we can distinguish
176 * between LAN and WAN.
178 * @param cls closure with the `struct GNUNET_ATS_InterfaceScanner`
179 * @param name name of the interface (can be NULL for unknown)
180 * @param isDefault is this presumably the default interface
181 * @param addr address of this interface (can be NULL for unknown or unassigned)
182 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
183 * @param netmask the network mask (can be NULL for unknown or unassigned)
184 * @param addrlen length of the address
185 * @return #GNUNET_OK to continue iteration
188 interface_proc (void *cls,
191 const struct sockaddr *addr,
192 const struct sockaddr *broadcast_addr,
193 const struct sockaddr *netmask,
196 struct GNUNET_ATS_InterfaceScanner *is = cls;
197 /* Calculate network */
198 struct ATS_Network *net = NULL;
200 /* Skipping IPv4 loopback addresses since we have special check */
201 if (addr->sa_family == AF_INET)
203 const struct sockaddr_in *a4 = (const struct sockaddr_in *) addr;
205 if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
208 /* Skipping IPv6 loopback addresses since we have special check */
209 if (addr->sa_family == AF_INET6)
211 const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *) addr;
212 if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
216 if (addr->sa_family == AF_INET)
218 const struct sockaddr_in *addr4 = (const struct sockaddr_in *) addr;
219 const struct sockaddr_in *netmask4 = (const struct sockaddr_in *) netmask;
220 struct sockaddr_in *tmp;
221 struct sockaddr_in network4;
223 net = GNUNET_malloc (sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in));
224 tmp = (struct sockaddr_in *) &net[1];
225 net->network = (struct sockaddr *) &tmp[0];
226 net->netmask = (struct sockaddr *) &tmp[1];
227 net->length = addrlen;
229 memset (&network4, 0, sizeof (network4));
230 network4.sin_family = AF_INET;
231 #if HAVE_SOCKADDR_IN_SIN_LEN
232 network4.sin_len = sizeof (network4);
234 network4.sin_addr.s_addr = (addr4->sin_addr.s_addr & netmask4->sin_addr.s_addr);
236 memcpy (net->netmask, netmask4, sizeof (struct sockaddr_in));
237 memcpy (net->network, &network4, sizeof (struct sockaddr_in));
240 if (addr->sa_family == AF_INET6)
242 const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *) addr;
243 const struct sockaddr_in6 *netmask6 = (const struct sockaddr_in6 *) netmask;
244 struct sockaddr_in6 * tmp;
245 struct sockaddr_in6 network6;
247 net = GNUNET_malloc (sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in6));
248 tmp = (struct sockaddr_in6 *) &net[1];
249 net->network = (struct sockaddr *) &tmp[0];
250 net->netmask = (struct sockaddr *) &tmp[1];
251 net->length = addrlen;
253 memset (&network6, 0, sizeof (network6));
254 network6.sin6_family = AF_INET6;
255 #if HAVE_SOCKADDR_IN_SIN_LEN
256 network6.sin6_len = sizeof (network6);
259 uint32_t *addr_elem = (uint32_t *) &addr6->sin6_addr;
260 uint32_t *mask_elem = (uint32_t *) &netmask6->sin6_addr;
261 uint32_t *net_elem = (uint32_t *) &network6.sin6_addr;
262 for (c = 0; c < 4; c++)
263 net_elem[c] = addr_elem[c] & mask_elem[c];
265 memcpy (net->netmask, netmask6, sizeof (struct sockaddr_in6));
266 memcpy (net->network, &network6, sizeof (struct sockaddr_in6));
269 return GNUNET_OK; /* odd / unsupported address family */
273 char * netmask = GNUNET_strdup (GNUNET_a2s((struct sockaddr *) net->netmask, addrlen));
274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
275 "Adding network `%s', netmask `%s'\n",
276 GNUNET_a2s ((struct sockaddr *) net->network,
279 GNUNET_free (netmask);
281 GNUNET_CONTAINER_DLL_insert (is->net_head,
290 * Periodically get list of network addresses from our interfaces.
293 * @param tc Task context
296 get_addresses (void *cls,
297 const struct GNUNET_SCHEDULER_TaskContext *tc)
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,
395 "ats-scheduling-api",
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 */