src: for every AGPL3.0 file, add SPDX identifier.
[oweals/gnunet.git] / src / nt / nt.c
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 nt/nt_api_scanner.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 #include "gnunet_nt_lib.h"
29
30 /**
31  * How frequently do we scan the interfaces for changes to the addresses?
32  */
33 #define INTERFACE_PROCESSING_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
34
35
36 /**
37  * Convert a `enum GNUNET_NetworkType` to a string
38  *
39  * @param net the network type
40  * @return a string or NULL if invalid
41  */
42 const char *
43 GNUNET_NT_to_string (enum GNUNET_NetworkType net)
44 {
45   switch (net)
46     {
47     case GNUNET_NT_UNSPECIFIED:
48       return "UNSPECIFIED";
49     case GNUNET_NT_LOOPBACK:
50       return "LOOPBACK";
51     case GNUNET_NT_LAN:
52       return "LAN";
53     case GNUNET_NT_WAN:
54       return "WAN";
55     case GNUNET_NT_WLAN:
56       return "WLAN";
57     case GNUNET_NT_BT:
58       return "BLUETOOTH";
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   /**
107    * Head of LAN networks list.
108    */
109   struct NT_Network *net_head;
110
111   /**
112    * Tail of LAN networks list.
113    */
114   struct NT_Network *net_tail;
115
116   /**
117    * Task for periodically refreshing our LAN network list.
118    */
119   struct GNUNET_SCHEDULER_Task *interface_task;
120
121 };
122
123
124 /**
125  * Delete all entries from the current network list.
126  *
127  * @param is scanner to clean up
128  */
129 static void
130 delete_networks (struct GNUNET_NT_InterfaceScanner *is)
131 {
132   struct NT_Network *cur;
133
134   while (NULL != (cur = is->net_head))
135   {
136     GNUNET_CONTAINER_DLL_remove (is->net_head,
137                                  is->net_tail,
138                                  cur);
139     GNUNET_free (cur);
140   }
141 }
142
143
144 /**
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.
148  *
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
157  */
158 static int
159 interface_proc (void *cls,
160                 const char *name,
161                 int isDefault,
162                 const struct sockaddr *addr,
163                 const struct sockaddr *broadcast_addr,
164                 const struct sockaddr *netmask,
165                 socklen_t addrlen)
166 {
167   struct GNUNET_NT_InterfaceScanner *is = cls;
168   /* Calculate network */
169   struct NT_Network *net = NULL;
170   (void) name;
171   (void) isDefault;
172   (void) broadcast_addr;
173
174   /* Skipping IPv4 loopback addresses since we have special check  */
175   if  (addr->sa_family == AF_INET)
176   {
177     const struct sockaddr_in *a4 = (const struct sockaddr_in *) addr;
178
179     if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
180        return GNUNET_OK;
181   }
182   /* Skipping IPv6 loopback addresses since we have special check  */
183   if  (addr->sa_family == AF_INET6)
184   {
185     const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *) addr;
186     if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
187       return GNUNET_OK;
188   }
189
190   if (addr->sa_family == AF_INET)
191   {
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;
196
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;
202
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);
207 #endif
208     network4.sin_addr.s_addr = (addr4->sin_addr.s_addr & netmask4->sin_addr.s_addr);
209
210     GNUNET_memcpy (net->netmask, netmask4, sizeof (struct sockaddr_in));
211     GNUNET_memcpy (net->network, &network4, sizeof (struct sockaddr_in));
212   }
213
214   if (addr->sa_family == AF_INET6)
215   {
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;
220
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;
226
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);
231 #endif
232     unsigned int c = 0;
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];
238
239     GNUNET_memcpy (net->netmask, netmask6, sizeof (struct sockaddr_in6));
240     GNUNET_memcpy (net->network, &network6, sizeof (struct sockaddr_in6));
241   }
242   if (NULL == net)
243     return GNUNET_OK; /* odd / unsupported address family */
244
245   /* Store in list */
246 #if VERBOSE_NT
247   char * netmask = GNUNET_strdup (GNUNET_a2s((struct sockaddr *) net->netmask, addrlen));
248   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
249                    "nt",
250                    "Adding network `%s', netmask `%s'\n",
251                    GNUNET_a2s ((struct sockaddr *) net->network,
252                                addrlen),
253                    netmask);
254   GNUNET_free (netmask);
255 #endif
256   GNUNET_CONTAINER_DLL_insert (is->net_head,
257                                is->net_tail,
258                                net);
259
260   return GNUNET_OK;
261 }
262
263
264 /**
265  * Periodically get list of network addresses from our interfaces.
266  *
267  * @param cls closure
268  */
269 static void
270 get_addresses (void *cls)
271 {
272   struct GNUNET_NT_InterfaceScanner *is = cls;
273
274   is->interface_task = NULL;
275   delete_networks (is);
276   GNUNET_OS_network_interfaces_list (&interface_proc,
277                                      is);
278   is->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVAL,
279                                                      &get_addresses,
280                                                      is);
281 }
282
283
284 /**
285  * Returns where the address is located: LAN or WAN or ...
286  *
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
291  */
292 enum GNUNET_NetworkType
293 GNUNET_NT_scanner_get_type (struct GNUNET_NT_InterfaceScanner *is,
294                             const struct sockaddr *addr,
295                             socklen_t addrlen)
296 {
297   struct NT_Network *cur = is->net_head;
298   enum GNUNET_NetworkType type = GNUNET_NT_UNSPECIFIED;
299
300   switch (addr->sa_family)
301     {
302     case AF_UNIX:
303       type = GNUNET_NT_LOOPBACK;
304       break;
305     case AF_INET:
306       {
307         const struct sockaddr_in *a4 = (const struct sockaddr_in *) addr;
308
309         if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
310           type = GNUNET_NT_LOOPBACK;
311         break;
312       }
313     case AF_INET6:
314       {
315         const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *) addr;
316
317         if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
318           type = GNUNET_NT_LOOPBACK;
319         break;
320       }
321     default:
322       GNUNET_break (0);
323       break;
324    }
325
326   /* Check local networks */
327   while ((NULL != cur) && (GNUNET_NT_UNSPECIFIED == type))
328   {
329     if (addrlen != cur->length)
330     {
331       cur = cur->next;
332       continue;
333     }
334     if (addr->sa_family == AF_INET)
335     {
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;
339
340       if (((a4->sin_addr.s_addr & mask4->sin_addr.s_addr)) == net4->sin_addr.s_addr)
341         type = GNUNET_NT_LAN;
342     }
343     if (addr->sa_family == AF_INET6)
344     {
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;
348
349       int res = GNUNET_YES;
350       int c = 0;
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])
356           res = GNUNET_NO;
357
358       if (res == GNUNET_YES)
359         type = GNUNET_NT_LAN;
360     }
361     cur = cur->next;
362   }
363
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,
368                    "nt-scanner-api",
369                    "`%s' is in network `%s'\n",
370                    GNUNET_a2s (addr,
371                                addrlen),
372                    GNUNET_NT_to_string (type));
373   return type;
374 }
375
376
377 /**
378  * Initialize the interface scanner.
379  *
380  * @return interface scanner
381  */
382 struct GNUNET_NT_InterfaceScanner *
383 GNUNET_NT_scanner_init ()
384 {
385   struct GNUNET_NT_InterfaceScanner *is;
386
387   is = GNUNET_new (struct GNUNET_NT_InterfaceScanner);
388   GNUNET_OS_network_interfaces_list (&interface_proc,
389                                      is);
390   is->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVAL,
391                                                      &get_addresses,
392                                                      is);
393   return is;
394 }
395
396
397 /**
398  * Client is done with the interface scanner, release resources.
399  *
400  * @param is handle to release
401  */
402 void
403 GNUNET_NT_scanner_done (struct GNUNET_NT_InterfaceScanner *is)
404 {
405   if (NULL != is->interface_task)
406   {
407     GNUNET_SCHEDULER_cancel (is->interface_task);
408     is->interface_task = NULL;
409   }
410   delete_networks (is);
411   GNUNET_free (is);
412 }
413
414
415 /* end of nt.c */