2 This file is part of GNUnet.
3 Copyright (C) 2010-2015, 2018 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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file nt/nt_api_scanner.c
22 * @brief LAN interface scanning to determine IPs in LAN
23 * @author Christian Grothoff
24 * @author Matthias Wachs
27 #include "gnunet_util_lib.h"
28 #include "gnunet_nt_lib.h"
31 * How frequently do we scan the interfaces for changes to the addresses?
33 #define INTERFACE_PROCESSING_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
37 * Convert a `enum GNUNET_NetworkType` to a string
39 * @param net the network type
40 * @return a string or NULL if invalid
43 GNUNET_NT_to_string (enum GNUNET_NetworkType net)
47 case GNUNET_NT_UNSPECIFIED:
49 case GNUNET_NT_LOOPBACK:
66 * We keep a list of our local networks so we can answer
67 * LAN vs. WAN questions. Note: WLAN is not detected yet.
68 * (maybe we can do that heuristically based on interface
69 * name in the future?).
76 struct NT_Network *next;
81 struct NT_Network *prev;
86 struct sockaddr *network;
89 * Netmask to determine what is in the LAN.
91 struct sockaddr *netmask;
94 * How long are @e network and @e netmask?
101 * Handle to the interface scanner.
103 struct GNUNET_NT_InterfaceScanner
107 * Head of LAN networks list.
109 struct NT_Network *net_head;
112 * Tail of LAN networks list.
114 struct NT_Network *net_tail;
117 * Task for periodically refreshing our LAN network list.
119 struct GNUNET_SCHEDULER_Task *interface_task;
125 * Delete all entries from the current network list.
127 * @param is scanner to clean up
130 delete_networks (struct GNUNET_NT_InterfaceScanner *is)
132 struct NT_Network *cur;
134 while (NULL != (cur = is->net_head))
136 GNUNET_CONTAINER_DLL_remove (is->net_head,
145 * Function invoked for each interface found. Adds the interface's
146 * network addresses to the respective DLL, so we can distinguish
147 * between LAN and WAN.
149 * @param cls closure with the `struct GNUNET_NT_InterfaceScanner`
150 * @param name name of the interface (can be NULL for unknown)
151 * @param isDefault is this presumably the default interface
152 * @param addr address of this interface (can be NULL for unknown or unassigned)
153 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
154 * @param netmask the network mask (can be NULL for unknown or unassigned)
155 * @param addrlen length of the address
156 * @return #GNUNET_OK to continue iteration
159 interface_proc (void *cls,
162 const struct sockaddr *addr,
163 const struct sockaddr *broadcast_addr,
164 const struct sockaddr *netmask,
167 struct GNUNET_NT_InterfaceScanner *is = cls;
168 /* Calculate network */
169 struct NT_Network *net = NULL;
172 (void) broadcast_addr;
174 /* Skipping IPv4 loopback addresses since we have special check */
175 if (addr->sa_family == AF_INET)
177 const struct sockaddr_in *a4 = (const struct sockaddr_in *) addr;
179 if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
182 /* Skipping IPv6 loopback addresses since we have special check */
183 if (addr->sa_family == AF_INET6)
185 const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *) addr;
186 if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
190 if (addr->sa_family == AF_INET)
192 const struct sockaddr_in *addr4 = (const struct sockaddr_in *) addr;
193 const struct sockaddr_in *netmask4 = (const struct sockaddr_in *) netmask;
194 struct sockaddr_in *tmp;
195 struct sockaddr_in network4;
197 net = GNUNET_malloc (sizeof (struct NT_Network) + 2 * sizeof (struct sockaddr_in));
198 tmp = (struct sockaddr_in *) &net[1];
199 net->network = (struct sockaddr *) &tmp[0];
200 net->netmask = (struct sockaddr *) &tmp[1];
201 net->length = addrlen;
203 memset (&network4, 0, sizeof (network4));
204 network4.sin_family = AF_INET;
205 #if HAVE_SOCKADDR_IN_SIN_LEN
206 network4.sin_len = sizeof (network4);
208 network4.sin_addr.s_addr = (addr4->sin_addr.s_addr & netmask4->sin_addr.s_addr);
210 GNUNET_memcpy (net->netmask, netmask4, sizeof (struct sockaddr_in));
211 GNUNET_memcpy (net->network, &network4, sizeof (struct sockaddr_in));
214 if (addr->sa_family == AF_INET6)
216 const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *) addr;
217 const struct sockaddr_in6 *netmask6 = (const struct sockaddr_in6 *) netmask;
218 struct sockaddr_in6 * tmp;
219 struct sockaddr_in6 network6;
221 net = GNUNET_malloc (sizeof (struct NT_Network) + 2 * sizeof (struct sockaddr_in6));
222 tmp = (struct sockaddr_in6 *) &net[1];
223 net->network = (struct sockaddr *) &tmp[0];
224 net->netmask = (struct sockaddr *) &tmp[1];
225 net->length = addrlen;
227 memset (&network6, 0, sizeof (network6));
228 network6.sin6_family = AF_INET6;
229 #if HAVE_SOCKADDR_IN_SIN_LEN
230 network6.sin6_len = sizeof (network6);
233 uint32_t *addr_elem = (uint32_t *) &addr6->sin6_addr;
234 uint32_t *mask_elem = (uint32_t *) &netmask6->sin6_addr;
235 uint32_t *net_elem = (uint32_t *) &network6.sin6_addr;
236 for (c = 0; c < 4; c++)
237 net_elem[c] = addr_elem[c] & mask_elem[c];
239 GNUNET_memcpy (net->netmask, netmask6, sizeof (struct sockaddr_in6));
240 GNUNET_memcpy (net->network, &network6, sizeof (struct sockaddr_in6));
243 return GNUNET_OK; /* odd / unsupported address family */
247 char * netmask = GNUNET_strdup (GNUNET_a2s((struct sockaddr *) net->netmask, addrlen));
248 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
250 "Adding network `%s', netmask `%s'\n",
251 GNUNET_a2s ((struct sockaddr *) net->network,
254 GNUNET_free (netmask);
256 GNUNET_CONTAINER_DLL_insert (is->net_head,
265 * Periodically get list of network addresses from our interfaces.
270 get_addresses (void *cls)
272 struct GNUNET_NT_InterfaceScanner *is = cls;
274 is->interface_task = NULL;
275 delete_networks (is);
276 GNUNET_OS_network_interfaces_list (&interface_proc,
278 is->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVAL,
285 * Returns where the address is located: LAN or WAN or ...
287 * @param is the interface scanner handle
288 * @param addr address
289 * @param addrlen address length
290 * @return type of the network the address belongs to
292 enum GNUNET_NetworkType
293 GNUNET_NT_scanner_get_type (struct GNUNET_NT_InterfaceScanner *is,
294 const struct sockaddr *addr,
297 struct NT_Network *cur = is->net_head;
298 enum GNUNET_NetworkType type = GNUNET_NT_UNSPECIFIED;
300 switch (addr->sa_family)
303 type = GNUNET_NT_LOOPBACK;
307 const struct sockaddr_in *a4 = (const struct sockaddr_in *) addr;
309 if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
310 type = GNUNET_NT_LOOPBACK;
315 const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *) addr;
317 if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
318 type = GNUNET_NT_LOOPBACK;
326 /* Check local networks */
327 while ((NULL != cur) && (GNUNET_NT_UNSPECIFIED == type))
329 if (addrlen != cur->length)
334 if (addr->sa_family == AF_INET)
336 const struct sockaddr_in *a4 = (const struct sockaddr_in *) addr;
337 const struct sockaddr_in *net4 = (const struct sockaddr_in *) cur->network;
338 const struct sockaddr_in *mask4 = (const struct sockaddr_in *) cur->netmask;
340 if (((a4->sin_addr.s_addr & mask4->sin_addr.s_addr)) == net4->sin_addr.s_addr)
341 type = GNUNET_NT_LAN;
343 if (addr->sa_family == AF_INET6)
345 const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *) addr;
346 const struct sockaddr_in6 *net6 = (const struct sockaddr_in6 *) cur->network;
347 const struct sockaddr_in6 *mask6 = (const struct sockaddr_in6 *) cur->netmask;
349 int res = GNUNET_YES;
351 uint32_t *addr_elem = (uint32_t *) &a6->sin6_addr;
352 uint32_t *mask_elem = (uint32_t *) &mask6->sin6_addr;
353 uint32_t *net_elem = (uint32_t *) &net6->sin6_addr;
354 for (c = 0; c < 4; c++)
355 if ((addr_elem[c] & mask_elem[c]) != net_elem[c])
358 if (res == GNUNET_YES)
359 type = GNUNET_NT_LAN;
364 /* no local network found for this address, default: WAN */
365 if (type == GNUNET_NT_UNSPECIFIED)
366 type = GNUNET_NT_WAN;
367 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
369 "`%s' is in network `%s'\n",
372 GNUNET_NT_to_string (type));
378 * Initialize the interface scanner.
380 * @return interface scanner
382 struct GNUNET_NT_InterfaceScanner *
383 GNUNET_NT_scanner_init ()
385 struct GNUNET_NT_InterfaceScanner *is;
387 is = GNUNET_new (struct GNUNET_NT_InterfaceScanner);
388 GNUNET_OS_network_interfaces_list (&interface_proc,
390 is->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVAL,
398 * Client is done with the interface scanner, release resources.
400 * @param is handle to release
403 GNUNET_NT_scanner_done (struct GNUNET_NT_InterfaceScanner *is)
405 if (NULL != is->interface_task)
407 GNUNET_SCHEDULER_cancel (is->interface_task);
408 is->interface_task = NULL;
410 delete_networks (is);