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 ( \
34 GNUNET_TIME_UNIT_MINUTES, 2)
38 * Convert a `enum GNUNET_NetworkType` to a string
40 * @param net the network type
41 * @return a string or NULL if invalid
44 GNUNET_NT_to_string (enum GNUNET_NetworkType net)
48 case GNUNET_NT_UNSPECIFIED:
51 case GNUNET_NT_LOOPBACK:
73 * We keep a list of our local networks so we can answer
74 * LAN vs. WAN questions. Note: WLAN is not detected yet.
75 * (maybe we can do that heuristically based on interface
76 * name in the future?).
83 struct NT_Network *next;
88 struct NT_Network *prev;
93 struct sockaddr *network;
96 * Netmask to determine what is in the LAN.
98 struct sockaddr *netmask;
101 * How long are @e network and @e netmask?
108 * Handle to the interface scanner.
110 struct GNUNET_NT_InterfaceScanner
113 * Head of LAN networks list.
115 struct NT_Network *net_head;
118 * Tail of LAN networks list.
120 struct NT_Network *net_tail;
123 * Task for periodically refreshing our LAN network list.
125 struct GNUNET_SCHEDULER_Task *interface_task;
130 * Delete all entries from the current network list.
132 * @param is scanner to clean up
135 delete_networks (struct GNUNET_NT_InterfaceScanner *is)
137 struct NT_Network *cur;
139 while (NULL != (cur = is->net_head))
141 GNUNET_CONTAINER_DLL_remove (is->net_head,
150 * Function invoked for each interface found. Adds the interface's
151 * network addresses to the respective DLL, so we can distinguish
152 * between LAN and WAN.
154 * @param cls closure with the `struct GNUNET_NT_InterfaceScanner`
155 * @param name name of the interface (can be NULL for unknown)
156 * @param isDefault is this presumably the default interface
157 * @param addr address of this interface (can be NULL for unknown or unassigned)
158 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
159 * @param netmask the network mask (can be NULL for unknown or unassigned)
160 * @param addrlen length of the address
161 * @return #GNUNET_OK to continue iteration
164 interface_proc (void *cls,
167 const struct sockaddr *addr,
168 const struct sockaddr *broadcast_addr,
169 const struct sockaddr *netmask,
172 struct GNUNET_NT_InterfaceScanner *is = cls;
173 /* Calculate network */
174 struct NT_Network *net = NULL;
178 (void) broadcast_addr;
180 /* Skipping IPv4 loopback addresses since we have special check */
181 if (addr->sa_family == AF_INET)
183 const struct sockaddr_in *a4 = (const struct sockaddr_in *) addr;
185 if ((a4->sin_addr.s_addr & htonl (0xff000000)) == htonl (0x7f000000))
188 /* Skipping IPv6 loopback addresses since we have special check */
189 if (addr->sa_family == AF_INET6)
191 const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *) addr;
192 if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
196 if (addr->sa_family == AF_INET)
198 const struct sockaddr_in *addr4 = (const struct sockaddr_in *) addr;
199 const struct sockaddr_in *netmask4 = (const struct sockaddr_in *) netmask;
200 struct sockaddr_in *tmp;
201 struct sockaddr_in network4;
203 net = GNUNET_malloc (sizeof(struct NT_Network) + 2 * sizeof(struct
205 tmp = (struct sockaddr_in *) &net[1];
206 net->network = (struct sockaddr *) &tmp[0];
207 net->netmask = (struct sockaddr *) &tmp[1];
208 net->length = addrlen;
213 network4.sin_family = AF_INET;
214 #if HAVE_SOCKADDR_IN_SIN_LEN
215 network4.sin_len = sizeof(network4);
217 network4.sin_addr.s_addr = (addr4->sin_addr.s_addr
218 & netmask4->sin_addr.s_addr);
220 GNUNET_memcpy (net->netmask,
222 sizeof(struct sockaddr_in));
223 GNUNET_memcpy (net->network,
225 sizeof(struct sockaddr_in));
228 if (addr->sa_family == AF_INET6)
230 const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *) addr;
231 const struct sockaddr_in6 *netmask6 = (const struct sockaddr_in6 *) netmask;
232 struct sockaddr_in6 *tmp;
233 struct sockaddr_in6 network6;
235 net = GNUNET_malloc (sizeof(struct NT_Network) + 2 * sizeof(struct
237 tmp = (struct sockaddr_in6 *) &net[1];
238 net->network = (struct sockaddr *) &tmp[0];
239 net->netmask = (struct sockaddr *) &tmp[1];
240 net->length = addrlen;
242 memset (&network6, 0, sizeof(network6));
243 network6.sin6_family = AF_INET6;
244 #if HAVE_SOCKADDR_IN_SIN_LEN
245 network6.sin6_len = sizeof(network6);
248 uint32_t *addr_elem = (uint32_t *) &addr6->sin6_addr;
249 uint32_t *mask_elem = (uint32_t *) &netmask6->sin6_addr;
250 uint32_t *net_elem = (uint32_t *) &network6.sin6_addr;
251 for (c = 0; c < 4; c++)
252 net_elem[c] = addr_elem[c] & mask_elem[c];
254 GNUNET_memcpy (net->netmask,
256 sizeof(struct sockaddr_in6));
257 GNUNET_memcpy (net->network,
259 sizeof(struct sockaddr_in6));
262 return GNUNET_OK; /* odd / unsupported address family */
266 char *netmask = GNUNET_strdup (GNUNET_a2s ((struct sockaddr *) net->netmask,
268 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
270 "Adding network `%s', netmask `%s'\n",
271 GNUNET_a2s ((struct sockaddr *) net->network,
274 GNUNET_free (netmask);
276 GNUNET_CONTAINER_DLL_insert (is->net_head,
285 * Periodically get list of network addresses from our interfaces.
290 get_addresses (void *cls)
292 struct GNUNET_NT_InterfaceScanner *is = cls;
294 is->interface_task = NULL;
295 delete_networks (is);
296 GNUNET_OS_network_interfaces_list (&interface_proc,
298 is->interface_task = GNUNET_SCHEDULER_add_delayed (
299 INTERFACE_PROCESSING_INTERVAL,
306 * Returns where the address is located: LAN or WAN or ...
308 * @param is the interface scanner handle
309 * @param addr address
310 * @param addrlen address length
311 * @return type of the network the address belongs to
313 enum GNUNET_NetworkType
314 GNUNET_NT_scanner_get_type (struct GNUNET_NT_InterfaceScanner *is,
315 const struct sockaddr *addr,
318 struct NT_Network *cur = is->net_head;
319 enum GNUNET_NetworkType type = GNUNET_NT_UNSPECIFIED;
321 switch (addr->sa_family)
324 type = GNUNET_NT_LOOPBACK;
329 const struct sockaddr_in *a4 = (const struct sockaddr_in *) addr;
331 if ((a4->sin_addr.s_addr & htonl (0xff000000)) == htonl (0x7f000000))
332 type = GNUNET_NT_LOOPBACK;
338 const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *) addr;
340 if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
341 type = GNUNET_NT_LOOPBACK;
350 /* Check local networks */
351 while ((NULL != cur) && (GNUNET_NT_UNSPECIFIED == type))
353 if (addrlen != cur->length)
358 if (addr->sa_family == AF_INET)
360 const struct sockaddr_in *a4 = (const struct sockaddr_in *) addr;
361 const struct sockaddr_in *net4 = (const struct
362 sockaddr_in *) cur->network;
363 const struct sockaddr_in *mask4 = (const struct
364 sockaddr_in *) cur->netmask;
366 if (((a4->sin_addr.s_addr & mask4->sin_addr.s_addr)) ==
367 net4->sin_addr.s_addr)
368 type = GNUNET_NT_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
374 sockaddr_in6 *) cur->network;
375 const struct sockaddr_in6 *mask6 = (const struct
376 sockaddr_in6 *) cur->netmask;
378 int res = GNUNET_YES;
380 uint32_t *addr_elem = (uint32_t *) &a6->sin6_addr;
381 uint32_t *mask_elem = (uint32_t *) &mask6->sin6_addr;
382 uint32_t *net_elem = (uint32_t *) &net6->sin6_addr;
383 for (c = 0; c < 4; c++)
384 if ((addr_elem[c] & mask_elem[c]) != net_elem[c])
387 if (res == GNUNET_YES)
388 type = GNUNET_NT_LAN;
393 /* no local network found for this address, default: WAN */
394 if (type == GNUNET_NT_UNSPECIFIED)
395 type = GNUNET_NT_WAN;
396 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
398 "`%s' is in network `%s'\n",
401 GNUNET_NT_to_string (type));
407 * Initialize the interface scanner.
409 * @return interface scanner
411 struct GNUNET_NT_InterfaceScanner *
412 GNUNET_NT_scanner_init ()
414 struct GNUNET_NT_InterfaceScanner *is;
416 is = GNUNET_new (struct GNUNET_NT_InterfaceScanner);
417 GNUNET_OS_network_interfaces_list (&interface_proc,
419 is->interface_task = GNUNET_SCHEDULER_add_delayed (
420 INTERFACE_PROCESSING_INTERVAL,
428 * Client is done with the interface scanner, release resources.
430 * @param is handle to release
433 GNUNET_NT_scanner_done (struct GNUNET_NT_InterfaceScanner *is)
435 if (NULL != is->interface_task)
437 GNUNET_SCHEDULER_cancel (is->interface_task);
438 is->interface_task = NULL;
440 delete_networks (is);