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_UTILIZATION_PAYLOAD_OUT:
82 return "UTILIZATION_PAYLOAD_UP";
83 case GNUNET_ATS_UTILIZATION_PAYLOAD_IN:
84 return "UTILIZATION_PAYLOAD_DOWN";
85 case GNUNET_ATS_NETWORK_TYPE:
86 return "NETWORK_TYPE";
87 case GNUNET_ATS_QUALITY_NET_DELAY:
89 case GNUNET_ATS_QUALITY_NET_DISTANCE:
91 case GNUNET_ATS_COST_WAN:
93 case GNUNET_ATS_COST_LAN:
95 case GNUNET_ATS_COST_WLAN:
104 * We keep a list of our local networks so we can answer
105 * LAN vs. WAN questions. Note: WLAN is not detected yet.
106 * (maybe we can do that heuristically based on interface
107 * name in the future?).
114 struct ATS_Network *next;
119 struct ATS_Network *prev;
124 struct sockaddr *network;
127 * Netmask to determine what is in the LAN.
129 struct sockaddr *netmask;
132 * How long are @e network and @e netmask?
139 * Handle to the interface scanner.
141 struct GNUNET_ATS_InterfaceScanner
145 * Head of LAN networks list.
147 struct ATS_Network *net_head;
150 * Tail of LAN networks list.
152 struct ATS_Network *net_tail;
155 * Task for periodically refreshing our LAN network list.
157 struct GNUNET_SCHEDULER_Task *interface_task;
163 * Delete all entries from the current network list.
165 * @param is scanner to clean up
168 delete_networks (struct GNUNET_ATS_InterfaceScanner *is)
170 struct ATS_Network *cur;
172 while (NULL != (cur = is->net_head))
174 GNUNET_CONTAINER_DLL_remove (is->net_head,
183 * Function invoked for each interface found. Adds the interface's
184 * network addresses to the respective DLL, so we can distinguish
185 * between LAN and WAN.
187 * @param cls closure with the `struct GNUNET_ATS_InterfaceScanner`
188 * @param name name of the interface (can be NULL for unknown)
189 * @param isDefault is this presumably the default interface
190 * @param addr address of this interface (can be NULL for unknown or unassigned)
191 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
192 * @param netmask the network mask (can be NULL for unknown or unassigned)
193 * @param addrlen length of the address
194 * @return #GNUNET_OK to continue iteration
197 interface_proc (void *cls,
200 const struct sockaddr *addr,
201 const struct sockaddr *broadcast_addr,
202 const struct sockaddr *netmask,
205 struct GNUNET_ATS_InterfaceScanner *is = cls;
206 /* Calculate network */
207 struct ATS_Network *net = NULL;
209 /* Skipping IPv4 loopback addresses since we have special check */
210 if (addr->sa_family == AF_INET)
212 const struct sockaddr_in *a4 = (const struct sockaddr_in *) addr;
214 if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
217 /* Skipping IPv6 loopback addresses since we have special check */
218 if (addr->sa_family == AF_INET6)
220 const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *) addr;
221 if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
225 if (addr->sa_family == AF_INET)
227 const struct sockaddr_in *addr4 = (const struct sockaddr_in *) addr;
228 const struct sockaddr_in *netmask4 = (const struct sockaddr_in *) netmask;
229 struct sockaddr_in *tmp;
230 struct sockaddr_in network4;
232 net = GNUNET_malloc (sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in));
233 tmp = (struct sockaddr_in *) &net[1];
234 net->network = (struct sockaddr *) &tmp[0];
235 net->netmask = (struct sockaddr *) &tmp[1];
236 net->length = addrlen;
238 memset (&network4, 0, sizeof (network4));
239 network4.sin_family = AF_INET;
240 #if HAVE_SOCKADDR_IN_SIN_LEN
241 network4.sin_len = sizeof (network4);
243 network4.sin_addr.s_addr = (addr4->sin_addr.s_addr & netmask4->sin_addr.s_addr);
245 memcpy (net->netmask, netmask4, sizeof (struct sockaddr_in));
246 memcpy (net->network, &network4, sizeof (struct sockaddr_in));
249 if (addr->sa_family == AF_INET6)
251 const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *) addr;
252 const struct sockaddr_in6 *netmask6 = (const struct sockaddr_in6 *) netmask;
253 struct sockaddr_in6 * tmp;
254 struct sockaddr_in6 network6;
256 net = GNUNET_malloc (sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in6));
257 tmp = (struct sockaddr_in6 *) &net[1];
258 net->network = (struct sockaddr *) &tmp[0];
259 net->netmask = (struct sockaddr *) &tmp[1];
260 net->length = addrlen;
262 memset (&network6, 0, sizeof (network6));
263 network6.sin6_family = AF_INET6;
264 #if HAVE_SOCKADDR_IN_SIN_LEN
265 network6.sin6_len = sizeof (network6);
268 uint32_t *addr_elem = (uint32_t *) &addr6->sin6_addr;
269 uint32_t *mask_elem = (uint32_t *) &netmask6->sin6_addr;
270 uint32_t *net_elem = (uint32_t *) &network6.sin6_addr;
271 for (c = 0; c < 4; c++)
272 net_elem[c] = addr_elem[c] & mask_elem[c];
274 memcpy (net->netmask, netmask6, sizeof (struct sockaddr_in6));
275 memcpy (net->network, &network6, sizeof (struct sockaddr_in6));
278 return GNUNET_OK; /* odd / unsupported address family */
282 char * netmask = GNUNET_strdup (GNUNET_a2s((struct sockaddr *) net->netmask, addrlen));
283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
284 "Adding network `%s', netmask `%s'\n",
285 GNUNET_a2s ((struct sockaddr *) net->network,
288 GNUNET_free (netmask);
290 GNUNET_CONTAINER_DLL_insert (is->net_head,
299 * Periodically get list of network addresses from our interfaces.
302 * @param tc Task context
305 get_addresses (void *cls,
306 const struct GNUNET_SCHEDULER_TaskContext *tc)
308 struct GNUNET_ATS_InterfaceScanner *is = cls;
310 is->interface_task = NULL;
311 delete_networks (is);
312 GNUNET_OS_network_interfaces_list (&interface_proc,
314 is->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVAL,
321 * Returns where the address is located: LAN or WAN or ...
323 * @param is the interface scanner handle
324 * @param addr address
325 * @param addrlen address length
326 * @return type of the network the address belongs to
328 enum GNUNET_ATS_Network_Type
329 GNUNET_ATS_scanner_address_get_type (struct GNUNET_ATS_InterfaceScanner *is,
330 const struct sockaddr *addr,
333 struct ATS_Network *cur = is->net_head;
334 enum GNUNET_ATS_Network_Type type = GNUNET_ATS_NET_UNSPECIFIED;
336 switch (addr->sa_family)
339 type = GNUNET_ATS_NET_LOOPBACK;
343 const struct sockaddr_in *a4 = (const struct sockaddr_in *) addr;
345 if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
346 type = GNUNET_ATS_NET_LOOPBACK;
351 const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *) addr;
353 if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
354 type = GNUNET_ATS_NET_LOOPBACK;
362 /* Check local networks */
363 while ((NULL != cur) && (GNUNET_ATS_NET_UNSPECIFIED == type))
365 if (addrlen != cur->length)
370 if (addr->sa_family == AF_INET)
372 const struct sockaddr_in *a4 = (const struct sockaddr_in *) addr;
373 const struct sockaddr_in *net4 = (const struct sockaddr_in *) cur->network;
374 const struct sockaddr_in *mask4 = (const struct sockaddr_in *) cur->netmask;
376 if (((a4->sin_addr.s_addr & mask4->sin_addr.s_addr)) == net4->sin_addr.s_addr)
377 type = GNUNET_ATS_NET_LAN;
379 if (addr->sa_family == AF_INET6)
381 const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *) addr;
382 const struct sockaddr_in6 *net6 = (const struct sockaddr_in6 *) cur->network;
383 const struct sockaddr_in6 *mask6 = (const struct sockaddr_in6 *) cur->netmask;
385 int res = GNUNET_YES;
387 uint32_t *addr_elem = (uint32_t *) &a6->sin6_addr;
388 uint32_t *mask_elem = (uint32_t *) &mask6->sin6_addr;
389 uint32_t *net_elem = (uint32_t *) &net6->sin6_addr;
390 for (c = 0; c < 4; c++)
391 if ((addr_elem[c] & mask_elem[c]) != net_elem[c])
394 if (res == GNUNET_YES)
395 type = GNUNET_ATS_NET_LAN;
400 /* no local network found for this address, default: WAN */
401 if (type == GNUNET_ATS_NET_UNSPECIFIED)
402 type = GNUNET_ATS_NET_WAN;
403 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
404 "ats-scheduling-api",
405 "`%s' is in network `%s'\n",
408 GNUNET_ATS_print_network_type (type));
414 * Initialize the interface scanner.
416 * @return interface scanner
418 struct GNUNET_ATS_InterfaceScanner *
419 GNUNET_ATS_scanner_init ()
421 struct GNUNET_ATS_InterfaceScanner *is;
423 is = GNUNET_new (struct GNUNET_ATS_InterfaceScanner);
424 GNUNET_OS_network_interfaces_list (&interface_proc,
426 is->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVAL,
434 * Client is done with the interface scanner, release resources.
436 * @param is handle to release
439 GNUNET_ATS_scanner_done (struct GNUNET_ATS_InterfaceScanner *is)
441 if (NULL != is->interface_task)
443 GNUNET_SCHEDULER_cancel (is->interface_task);
444 is->interface_task = NULL;
446 delete_networks (is);
451 /* end of ats_api_scanner.c */