diff options
Diffstat (limited to 'src/util/nt.c')
-rw-r--r-- | src/util/nt.c | 438 |
1 files changed, 438 insertions, 0 deletions
diff --git a/src/util/nt.c b/src/util/nt.c new file mode 100644 index 000000000..24471d9ad --- /dev/null +++ b/src/util/nt.c | |||
@@ -0,0 +1,438 @@ | |||
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 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | /** | ||
21 | * @file util/nt.c | ||
22 | * @brief LAN interface scanning to determine IPs in LAN | ||
23 | * @author Christian Grothoff | ||
24 | * @author Matthias Wachs | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | |||
29 | /** | ||
30 | * How frequently do we scan the interfaces for changes to the addresses? | ||
31 | */ | ||
32 | #define INTERFACE_PROCESSING_INTERVAL GNUNET_TIME_relative_multiply ( \ | ||
33 | GNUNET_TIME_UNIT_MINUTES, 2) | ||
34 | |||
35 | |||
36 | const char * | ||
37 | GNUNET_NT_to_string (enum GNUNET_NetworkType net) | ||
38 | { | ||
39 | switch (net) | ||
40 | { | ||
41 | case GNUNET_NT_UNSPECIFIED: | ||
42 | return "UNSPECIFIED"; | ||
43 | |||
44 | case GNUNET_NT_LOOPBACK: | ||
45 | return "LOOPBACK"; | ||
46 | |||
47 | case GNUNET_NT_LAN: | ||
48 | return "LAN"; | ||
49 | |||
50 | case GNUNET_NT_WAN: | ||
51 | return "WAN"; | ||
52 | |||
53 | case GNUNET_NT_WLAN: | ||
54 | return "WLAN"; | ||
55 | |||
56 | case GNUNET_NT_BT: | ||
57 | return "BLUETOOTH"; | ||
58 | |||
59 | default: | ||
60 | return NULL; | ||
61 | } | ||
62 | } | ||
63 | |||
64 | |||
65 | /** | ||
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?). | ||
70 | */ | ||
71 | struct NT_Network | ||
72 | { | ||
73 | /** | ||
74 | * Kept in a DLL. | ||
75 | */ | ||
76 | struct NT_Network *next; | ||
77 | |||
78 | /** | ||
79 | * Kept in a DLL. | ||
80 | */ | ||
81 | struct NT_Network *prev; | ||
82 | |||
83 | /** | ||
84 | * Network address. | ||
85 | */ | ||
86 | struct sockaddr *network; | ||
87 | |||
88 | /** | ||
89 | * Netmask to determine what is in the LAN. | ||
90 | */ | ||
91 | struct sockaddr *netmask; | ||
92 | |||
93 | /** | ||
94 | * How long are @e network and @e netmask? | ||
95 | */ | ||
96 | socklen_t length; | ||
97 | }; | ||
98 | |||
99 | |||
100 | /** | ||
101 | * Handle to the interface scanner. | ||
102 | */ | ||
103 | struct GNUNET_NT_InterfaceScanner | ||
104 | { | ||
105 | /** | ||
106 | * Head of LAN networks list. | ||
107 | */ | ||
108 | struct NT_Network *net_head; | ||
109 | |||
110 | /** | ||
111 | * Tail of LAN networks list. | ||
112 | */ | ||
113 | struct NT_Network *net_tail; | ||
114 | |||
115 | /** | ||
116 | * Task for periodically refreshing our LAN network list. | ||
117 | */ | ||
118 | struct GNUNET_SCHEDULER_Task *interface_task; | ||
119 | }; | ||
120 | |||
121 | |||
122 | /** | ||
123 | * Delete all entries from the current network list. | ||
124 | * | ||
125 | * @param is scanner to clean up | ||
126 | */ | ||
127 | static void | ||
128 | delete_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 | */ | ||
156 | static int | ||
157 | interface_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 | (void) name; | ||
170 | (void) isDefault; | ||
171 | (void) broadcast_addr; | ||
172 | |||
173 | /* Skipping IPv4 loopback addresses since we have special check */ | ||
174 | if (addr->sa_family == AF_INET) | ||
175 | { | ||
176 | const struct sockaddr_in *a4 = (const struct sockaddr_in *) addr; | ||
177 | |||
178 | if ((a4->sin_addr.s_addr & htonl (0xff000000)) == htonl (0x7f000000)) | ||
179 | return GNUNET_OK; | ||
180 | } | ||
181 | /* Skipping IPv6 loopback addresses since we have special check */ | ||
182 | if (addr->sa_family == AF_INET6) | ||
183 | { | ||
184 | const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *) addr; | ||
185 | if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr)) | ||
186 | return GNUNET_OK; | ||
187 | } | ||
188 | |||
189 | if (addr->sa_family == AF_INET) | ||
190 | { | ||
191 | const struct sockaddr_in *addr4 = (const struct sockaddr_in *) addr; | ||
192 | const struct sockaddr_in *netmask4 = (const struct sockaddr_in *) netmask; | ||
193 | struct sockaddr_in *tmp; | ||
194 | struct sockaddr_in network4; | ||
195 | |||
196 | net = GNUNET_malloc (sizeof(struct NT_Network) + 2 * sizeof(struct | ||
197 | 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; | ||
202 | |||
203 | memset (&network4, | ||
204 | 0, | ||
205 | sizeof(network4)); | ||
206 | network4.sin_family = AF_INET; | ||
207 | #if HAVE_SOCKADDR_IN_SIN_LEN | ||
208 | network4.sin_len = sizeof(network4); | ||
209 | #endif | ||
210 | network4.sin_addr.s_addr = (addr4->sin_addr.s_addr | ||
211 | & netmask4->sin_addr.s_addr); | ||
212 | |||
213 | GNUNET_memcpy (net->netmask, | ||
214 | netmask4, | ||
215 | sizeof(struct sockaddr_in)); | ||
216 | GNUNET_memcpy (net->network, | ||
217 | &network4, | ||
218 | sizeof(struct sockaddr_in)); | ||
219 | } | ||
220 | |||
221 | if (addr->sa_family == AF_INET6) | ||
222 | { | ||
223 | const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *) addr; | ||
224 | const struct sockaddr_in6 *netmask6 = (const struct sockaddr_in6 *) netmask; | ||
225 | struct sockaddr_in6 *tmp; | ||
226 | struct sockaddr_in6 network6; | ||
227 | |||
228 | net = GNUNET_malloc (sizeof(struct NT_Network) + 2 * sizeof(struct | ||
229 | sockaddr_in6)); | ||
230 | tmp = (struct sockaddr_in6 *) &net[1]; | ||
231 | net->network = (struct sockaddr *) &tmp[0]; | ||
232 | net->netmask = (struct sockaddr *) &tmp[1]; | ||
233 | net->length = addrlen; | ||
234 | |||
235 | memset (&network6, 0, sizeof(network6)); | ||
236 | network6.sin6_family = AF_INET6; | ||
237 | #if HAVE_SOCKADDR_IN_SIN_LEN | ||
238 | network6.sin6_len = sizeof(network6); | ||
239 | #endif | ||
240 | unsigned int c = 0; | ||
241 | uint32_t *addr_elem = (uint32_t *) &addr6->sin6_addr; | ||
242 | uint32_t *mask_elem = (uint32_t *) &netmask6->sin6_addr; | ||
243 | uint32_t *net_elem = (uint32_t *) &network6.sin6_addr; | ||
244 | for (c = 0; c < 4; c++) | ||
245 | net_elem[c] = addr_elem[c] & mask_elem[c]; | ||
246 | |||
247 | GNUNET_memcpy (net->netmask, | ||
248 | netmask6, | ||
249 | sizeof(struct sockaddr_in6)); | ||
250 | GNUNET_memcpy (net->network, | ||
251 | &network6, | ||
252 | sizeof(struct sockaddr_in6)); | ||
253 | } | ||
254 | if (NULL == net) | ||
255 | return GNUNET_OK; /* odd / unsupported address family */ | ||
256 | |||
257 | /* Store in list */ | ||
258 | #if VERBOSE_NT | ||
259 | char *netmask = GNUNET_strdup (GNUNET_a2s ((struct sockaddr *) net->netmask, | ||
260 | addrlen)); | ||
261 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | ||
262 | "nt", | ||
263 | "Adding network `%s', netmask `%s'\n", | ||
264 | GNUNET_a2s ((struct sockaddr *) net->network, | ||
265 | addrlen), | ||
266 | netmask); | ||
267 | GNUNET_free (netmask); | ||
268 | #endif | ||
269 | GNUNET_CONTAINER_DLL_insert (is->net_head, | ||
270 | is->net_tail, | ||
271 | net); | ||
272 | |||
273 | return GNUNET_OK; | ||
274 | } | ||
275 | |||
276 | |||
277 | /** | ||
278 | * Periodically get list of network addresses from our interfaces. | ||
279 | * | ||
280 | * @param cls closure | ||
281 | */ | ||
282 | static void | ||
283 | get_addresses (void *cls) | ||
284 | { | ||
285 | struct GNUNET_NT_InterfaceScanner *is = cls; | ||
286 | |||
287 | is->interface_task = NULL; | ||
288 | delete_networks (is); | ||
289 | GNUNET_OS_network_interfaces_list (&interface_proc, | ||
290 | is); | ||
291 | is->interface_task = GNUNET_SCHEDULER_add_delayed ( | ||
292 | INTERFACE_PROCESSING_INTERVAL, | ||
293 | &get_addresses, | ||
294 | is); | ||
295 | } | ||
296 | |||
297 | |||
298 | /** | ||
299 | * Returns where the address is located: LAN or WAN or ... | ||
300 | * | ||
301 | * @param is the interface scanner handle | ||
302 | * @param addr address | ||
303 | * @param addrlen address length | ||
304 | * @return type of the network the address belongs to | ||
305 | */ | ||
306 | enum GNUNET_NetworkType | ||
307 | GNUNET_NT_scanner_get_type (struct GNUNET_NT_InterfaceScanner *is, | ||
308 | const struct sockaddr *addr, | ||
309 | socklen_t addrlen) | ||
310 | { | ||
311 | struct NT_Network *cur = is->net_head; | ||
312 | enum GNUNET_NetworkType type = GNUNET_NT_UNSPECIFIED; | ||
313 | |||
314 | switch (addr->sa_family) | ||
315 | { | ||
316 | case AF_UNIX: | ||
317 | type = GNUNET_NT_LOOPBACK; | ||
318 | break; | ||
319 | |||
320 | case AF_INET: | ||
321 | { | ||
322 | const struct sockaddr_in *a4 = (const struct sockaddr_in *) addr; | ||
323 | |||
324 | if ((a4->sin_addr.s_addr & htonl (0xff000000)) == htonl (0x7f000000)) | ||
325 | type = GNUNET_NT_LOOPBACK; | ||
326 | break; | ||
327 | } | ||
328 | |||
329 | case AF_INET6: | ||
330 | { | ||
331 | const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *) addr; | ||
332 | |||
333 | if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr)) | ||
334 | type = GNUNET_NT_LOOPBACK; | ||
335 | break; | ||
336 | } | ||
337 | |||
338 | default: | ||
339 | GNUNET_break (0); | ||
340 | break; | ||
341 | } | ||
342 | |||
343 | /* Check local networks */ | ||
344 | while ((NULL != cur) && (GNUNET_NT_UNSPECIFIED == type)) | ||
345 | { | ||
346 | if (addrlen != cur->length) | ||
347 | { | ||
348 | cur = cur->next; | ||
349 | continue; | ||
350 | } | ||
351 | if (addr->sa_family == AF_INET) | ||
352 | { | ||
353 | const struct sockaddr_in *a4 = (const struct sockaddr_in *) addr; | ||
354 | const struct sockaddr_in *net4 = (const struct | ||
355 | sockaddr_in *) cur->network; | ||
356 | const struct sockaddr_in *mask4 = (const struct | ||
357 | sockaddr_in *) cur->netmask; | ||
358 | |||
359 | if (((a4->sin_addr.s_addr & mask4->sin_addr.s_addr)) == | ||
360 | net4->sin_addr.s_addr) | ||
361 | type = GNUNET_NT_LAN; | ||
362 | } | ||
363 | if (addr->sa_family == AF_INET6) | ||
364 | { | ||
365 | const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *) addr; | ||
366 | const struct sockaddr_in6 *net6 = (const struct | ||
367 | sockaddr_in6 *) cur->network; | ||
368 | const struct sockaddr_in6 *mask6 = (const struct | ||
369 | sockaddr_in6 *) cur->netmask; | ||
370 | |||
371 | int res = GNUNET_YES; | ||
372 | int c = 0; | ||
373 | uint32_t *addr_elem = (uint32_t *) &a6->sin6_addr; | ||
374 | uint32_t *mask_elem = (uint32_t *) &mask6->sin6_addr; | ||
375 | uint32_t *net_elem = (uint32_t *) &net6->sin6_addr; | ||
376 | for (c = 0; c < 4; c++) | ||
377 | if ((addr_elem[c] & mask_elem[c]) != net_elem[c]) | ||
378 | res = GNUNET_NO; | ||
379 | |||
380 | if (res == GNUNET_YES) | ||
381 | type = GNUNET_NT_LAN; | ||
382 | } | ||
383 | cur = cur->next; | ||
384 | } | ||
385 | |||
386 | /* no local network found for this address, default: WAN */ | ||
387 | if (type == GNUNET_NT_UNSPECIFIED) | ||
388 | type = GNUNET_NT_WAN; | ||
389 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | ||
390 | "nt-scanner-api", | ||
391 | "`%s' is in network `%s'\n", | ||
392 | GNUNET_a2s (addr, | ||
393 | addrlen), | ||
394 | GNUNET_NT_to_string (type)); | ||
395 | return type; | ||
396 | } | ||
397 | |||
398 | |||
399 | /** | ||
400 | * Initialize the interface scanner. | ||
401 | * | ||
402 | * @return interface scanner | ||
403 | */ | ||
404 | struct GNUNET_NT_InterfaceScanner * | ||
405 | GNUNET_NT_scanner_init () | ||
406 | { | ||
407 | struct GNUNET_NT_InterfaceScanner *is; | ||
408 | |||
409 | is = GNUNET_new (struct GNUNET_NT_InterfaceScanner); | ||
410 | GNUNET_OS_network_interfaces_list (&interface_proc, | ||
411 | is); | ||
412 | is->interface_task = GNUNET_SCHEDULER_add_delayed ( | ||
413 | INTERFACE_PROCESSING_INTERVAL, | ||
414 | &get_addresses, | ||
415 | is); | ||
416 | return is; | ||
417 | } | ||
418 | |||
419 | |||
420 | /** | ||
421 | * Client is done with the interface scanner, release resources. | ||
422 | * | ||
423 | * @param is handle to release | ||
424 | */ | ||
425 | void | ||
426 | GNUNET_NT_scanner_done (struct GNUNET_NT_InterfaceScanner *is) | ||
427 | { | ||
428 | if (NULL != is->interface_task) | ||
429 | { | ||
430 | GNUNET_SCHEDULER_cancel (is->interface_task); | ||
431 | is->interface_task = NULL; | ||
432 | } | ||
433 | delete_networks (is); | ||
434 | GNUNET_free (is); | ||
435 | } | ||
436 | |||
437 | |||
438 | /* end of nt.c */ | ||