aboutsummaryrefslogtreecommitdiff
path: root/src/nt/nt.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2018-11-22 22:04:48 +0100
committerChristian Grothoff <christian@grothoff.org>2018-11-22 22:04:48 +0100
commitc87f73a07df468eccedbe1fdfa82bdd5b633a0d5 (patch)
tree0bb3f63e727a65dc84c33a03229b6760edee82ee /src/nt/nt.c
parentdd7379052f3749d87d8c35969ec94b4580e998b5 (diff)
downloadgnunet-c87f73a07df468eccedbe1fdfa82bdd5b633a0d5.tar.gz
gnunet-c87f73a07df468eccedbe1fdfa82bdd5b633a0d5.zip
move network type logic out of ATS code, move performance preferences into MQ
Diffstat (limited to 'src/nt/nt.c')
-rw-r--r--src/nt/nt.c410
1 files changed, 410 insertions, 0 deletions
diff --git a/src/nt/nt.c b/src/nt/nt.c
new file mode 100644
index 000000000..5764f8f4a
--- /dev/null
+++ b/src/nt/nt.c
@@ -0,0 +1,410 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2010-2015, 2018 GNUnet e.V.
4
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.
9
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.
14
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/>.
17*/
18/**
19 * @file nt/nt_api_scanner.c
20 * @brief LAN interface scanning to determine IPs in LAN
21 * @author Christian Grothoff
22 * @author Matthias Wachs
23 */
24#include "platform.h"
25#include "gnunet_util_lib.h"
26#include "gnunet_nt_lib.h"
27
28/**
29 * How frequently do we scan the interfaces for changes to the addresses?
30 */
31#define INTERFACE_PROCESSING_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
32
33
34/**
35 * Convert a `enum GNUNET_NetworkType` to a string
36 *
37 * @param net the network type
38 * @return a string or NULL if invalid
39 */
40const char *
41GNUNET_NT_to_string (enum GNUNET_NetworkType net)
42{
43 switch (net)
44 {
45 case GNUNET_NT_UNSPECIFIED:
46 return "UNSPECIFIED";
47 case GNUNET_NT_LOOPBACK:
48 return "LOOPBACK";
49 case GNUNET_NT_LAN:
50 return "LAN";
51 case GNUNET_NT_WAN:
52 return "WAN";
53 case GNUNET_NT_WLAN:
54 return "WLAN";
55 case GNUNET_NT_BT:
56 return "BLUETOOTH";
57 default:
58 return NULL;
59 }
60}
61
62
63/**
64 * We keep a list of our local networks so we can answer
65 * LAN vs. WAN questions. Note: WLAN is not detected yet.
66 * (maybe we can do that heuristically based on interface
67 * name in the future?).
68 */
69struct NT_Network
70{
71 /**
72 * Kept in a DLL.
73 */
74 struct NT_Network *next;
75
76 /**
77 * Kept in a DLL.
78 */
79 struct NT_Network *prev;
80
81 /**
82 * Network address.
83 */
84 struct sockaddr *network;
85
86 /**
87 * Netmask to determine what is in the LAN.
88 */
89 struct sockaddr *netmask;
90
91 /**
92 * How long are @e network and @e netmask?
93 */
94 socklen_t length;
95};
96
97
98/**
99 * Handle to the interface scanner.
100 */
101struct GNUNET_NT_InterfaceScanner
102{
103
104 /**
105 * Head of LAN networks list.
106 */
107 struct NT_Network *net_head;
108
109 /**
110 * Tail of LAN networks list.
111 */
112 struct NT_Network *net_tail;
113
114 /**
115 * Task for periodically refreshing our LAN network list.
116 */
117 struct GNUNET_SCHEDULER_Task *interface_task;
118
119};
120
121
122/**
123 * Delete all entries from the current network list.
124 *
125 * @param is scanner to clean up
126 */
127static void
128delete_networks (struct GNUNET_NT_InterfaceScanner *is)
129{
130 struct NT_Network *cur;
131
132 while (NULL != (cur = is->net_head))
133 {
134 GNUNET_CONTAINER_DLL_remove (is->net_head,
135 is->net_tail,
136 cur);
137 GNUNET_free (cur);
138 }
139}
140
141
142/**
143 * Function invoked for each interface found. Adds the interface's
144 * network addresses to the respective DLL, so we can distinguish
145 * between LAN and WAN.
146 *
147 * @param cls closure with the `struct GNUNET_NT_InterfaceScanner`
148 * @param name name of the interface (can be NULL for unknown)
149 * @param isDefault is this presumably the default interface
150 * @param addr address of this interface (can be NULL for unknown or unassigned)
151 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
152 * @param netmask the network mask (can be NULL for unknown or unassigned)
153 * @param addrlen length of the address
154 * @return #GNUNET_OK to continue iteration
155 */
156static int
157interface_proc (void *cls,
158 const char *name,
159 int isDefault,
160 const struct sockaddr *addr,
161 const struct sockaddr *broadcast_addr,
162 const struct sockaddr *netmask,
163 socklen_t addrlen)
164{
165 struct GNUNET_NT_InterfaceScanner *is = cls;
166 /* Calculate network */
167 struct NT_Network *net = NULL;
168
169 /* Skipping IPv4 loopback addresses since we have special check */
170 if (addr->sa_family == AF_INET)
171 {
172 const struct sockaddr_in *a4 = (const struct sockaddr_in *) addr;
173
174 if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
175 return GNUNET_OK;
176 }
177 /* Skipping IPv6 loopback addresses since we have special check */
178 if (addr->sa_family == AF_INET6)
179 {
180 const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *) addr;
181 if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
182 return GNUNET_OK;
183 }
184
185 if (addr->sa_family == AF_INET)
186 {
187 const struct sockaddr_in *addr4 = (const struct sockaddr_in *) addr;
188 const struct sockaddr_in *netmask4 = (const struct sockaddr_in *) netmask;
189 struct sockaddr_in *tmp;
190 struct sockaddr_in network4;
191
192 net = GNUNET_malloc (sizeof (struct NT_Network) + 2 * sizeof (struct sockaddr_in));
193 tmp = (struct sockaddr_in *) &net[1];
194 net->network = (struct sockaddr *) &tmp[0];
195 net->netmask = (struct sockaddr *) &tmp[1];
196 net->length = addrlen;
197
198 memset (&network4, 0, sizeof (network4));
199 network4.sin_family = AF_INET;
200#if HAVE_SOCKADDR_IN_SIN_LEN
201 network4.sin_len = sizeof (network4);
202#endif
203 network4.sin_addr.s_addr = (addr4->sin_addr.s_addr & netmask4->sin_addr.s_addr);
204
205 GNUNET_memcpy (net->netmask, netmask4, sizeof (struct sockaddr_in));
206 GNUNET_memcpy (net->network, &network4, sizeof (struct sockaddr_in));
207 }
208
209 if (addr->sa_family == AF_INET6)
210 {
211 const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *) addr;
212 const struct sockaddr_in6 *netmask6 = (const struct sockaddr_in6 *) netmask;
213 struct sockaddr_in6 * tmp;
214 struct sockaddr_in6 network6;
215
216 net = GNUNET_malloc (sizeof (struct NT_Network) + 2 * sizeof (struct sockaddr_in6));
217 tmp = (struct sockaddr_in6 *) &net[1];
218 net->network = (struct sockaddr *) &tmp[0];
219 net->netmask = (struct sockaddr *) &tmp[1];
220 net->length = addrlen;
221
222 memset (&network6, 0, sizeof (network6));
223 network6.sin6_family = AF_INET6;
224#if HAVE_SOCKADDR_IN_SIN_LEN
225 network6.sin6_len = sizeof (network6);
226#endif
227 unsigned int c = 0;
228 uint32_t *addr_elem = (uint32_t *) &addr6->sin6_addr;
229 uint32_t *mask_elem = (uint32_t *) &netmask6->sin6_addr;
230 uint32_t *net_elem = (uint32_t *) &network6.sin6_addr;
231 for (c = 0; c < 4; c++)
232 net_elem[c] = addr_elem[c] & mask_elem[c];
233
234 GNUNET_memcpy (net->netmask, netmask6, sizeof (struct sockaddr_in6));
235 GNUNET_memcpy (net->network, &network6, sizeof (struct sockaddr_in6));
236 }
237 if (NULL == net)
238 return GNUNET_OK; /* odd / unsupported address family */
239
240 /* Store in list */
241#if VERBOSE_NT
242 char * netmask = GNUNET_strdup (GNUNET_a2s((struct sockaddr *) net->netmask, addrlen));
243 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
244 "nt",
245 "Adding network `%s', netmask `%s'\n",
246 GNUNET_a2s ((struct sockaddr *) net->network,
247 addrlen),
248 netmask);
249 GNUNET_free (netmask);
250#endif
251 GNUNET_CONTAINER_DLL_insert (is->net_head,
252 is->net_tail,
253 net);
254
255 return GNUNET_OK;
256}
257
258
259/**
260 * Periodically get list of network addresses from our interfaces.
261 *
262 * @param cls closure
263 */
264static void
265get_addresses (void *cls)
266{
267 struct GNUNET_NT_InterfaceScanner *is = cls;
268
269 is->interface_task = NULL;
270 delete_networks (is);
271 GNUNET_OS_network_interfaces_list (&interface_proc,
272 is);
273 is->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVAL,
274 &get_addresses,
275 is);
276}
277
278
279/**
280 * Returns where the address is located: LAN or WAN or ...
281 *
282 * @param is the interface scanner handle
283 * @param addr address
284 * @param addrlen address length
285 * @return type of the network the address belongs to
286 */
287enum GNUNET_NetworkType
288GNUNET_NT_scanner_address_get_type (struct GNUNET_NT_InterfaceScanner *is,
289 const struct sockaddr *addr,
290 socklen_t addrlen)
291{
292 struct NT_Network *cur = is->net_head;
293 enum GNUNET_NetworkType type = GNUNET_NT_UNSPECIFIED;
294
295 switch (addr->sa_family)
296 {
297 case AF_UNIX:
298 type = GNUNET_NT_LOOPBACK;
299 break;
300 case AF_INET:
301 {
302 const struct sockaddr_in *a4 = (const struct sockaddr_in *) addr;
303
304 if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
305 type = GNUNET_NT_LOOPBACK;
306 break;
307 }
308 case AF_INET6:
309 {
310 const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *) addr;
311
312 if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
313 type = GNUNET_NT_LOOPBACK;
314 break;
315 }
316 default:
317 GNUNET_break (0);
318 break;
319 }
320
321 /* Check local networks */
322 while ((NULL != cur) && (GNUNET_NT_UNSPECIFIED == type))
323 {
324 if (addrlen != cur->length)
325 {
326 cur = cur->next;
327 continue;
328 }
329 if (addr->sa_family == AF_INET)
330 {
331 const struct sockaddr_in *a4 = (const struct sockaddr_in *) addr;
332 const struct sockaddr_in *net4 = (const struct sockaddr_in *) cur->network;
333 const struct sockaddr_in *mask4 = (const struct sockaddr_in *) cur->netmask;
334
335 if (((a4->sin_addr.s_addr & mask4->sin_addr.s_addr)) == net4->sin_addr.s_addr)
336 type = GNUNET_NT_LAN;
337 }
338 if (addr->sa_family == AF_INET6)
339 {
340 const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *) addr;
341 const struct sockaddr_in6 *net6 = (const struct sockaddr_in6 *) cur->network;
342 const struct sockaddr_in6 *mask6 = (const struct sockaddr_in6 *) cur->netmask;
343
344 int res = GNUNET_YES;
345 int c = 0;
346 uint32_t *addr_elem = (uint32_t *) &a6->sin6_addr;
347 uint32_t *mask_elem = (uint32_t *) &mask6->sin6_addr;
348 uint32_t *net_elem = (uint32_t *) &net6->sin6_addr;
349 for (c = 0; c < 4; c++)
350 if ((addr_elem[c] & mask_elem[c]) != net_elem[c])
351 res = GNUNET_NO;
352
353 if (res == GNUNET_YES)
354 type = GNUNET_NT_LAN;
355 }
356 cur = cur->next;
357 }
358
359 /* no local network found for this address, default: WAN */
360 if (type == GNUNET_NT_UNSPECIFIED)
361 type = GNUNET_NT_WAN;
362 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
363 "nt-scanner-api",
364 "`%s' is in network `%s'\n",
365 GNUNET_a2s (addr,
366 addrlen),
367 GNUNET_NT_to_string (type));
368 return type;
369}
370
371
372/**
373 * Initialize the interface scanner.
374 *
375 * @return interface scanner
376 */
377struct GNUNET_NT_InterfaceScanner *
378GNUNET_NT_scanner_init ()
379{
380 struct GNUNET_NT_InterfaceScanner *is;
381
382 is = GNUNET_new (struct GNUNET_NT_InterfaceScanner);
383 GNUNET_OS_network_interfaces_list (&interface_proc,
384 is);
385 is->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVAL,
386 &get_addresses,
387 is);
388 return is;
389}
390
391
392/**
393 * Client is done with the interface scanner, release resources.
394 *
395 * @param is handle to release
396 */
397void
398GNUNET_NT_scanner_done (struct GNUNET_NT_InterfaceScanner *is)
399{
400 if (NULL != is->interface_task)
401 {
402 GNUNET_SCHEDULER_cancel (is->interface_task);
403 is->interface_task = NULL;
404 }
405 delete_networks (is);
406 GNUNET_free (is);
407}
408
409
410/* end of nt.c */