guix-env: some update.
[oweals/gnunet.git] / src / ats / ats_api_scanner.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2010-2015 GNUnet e.V.
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20 /**
21  * @file ats/ats_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_ats_service.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 (GNUNET_TIME_UNIT_MINUTES, 2)
33
34
35 /**
36  * Convert a `enum GNUNET_ATS_Network_Type` to a string
37  *
38  * @param net the network type
39  * @return a string or NULL if invalid
40  */
41 const char *
42 GNUNET_ATS_print_network_type (enum GNUNET_ATS_Network_Type net)
43 {
44   switch (net)
45     {
46     case GNUNET_ATS_NET_UNSPECIFIED:
47       return "UNSPECIFIED";
48     case GNUNET_ATS_NET_LOOPBACK:
49       return "LOOPBACK";
50     case GNUNET_ATS_NET_LAN:
51       return "LAN";
52     case GNUNET_ATS_NET_WAN:
53       return "WAN";
54     case GNUNET_ATS_NET_WLAN:
55       return "WLAN";
56     case GNUNET_ATS_NET_BT:
57       return "BLUETOOTH";
58     default:
59       return NULL;
60     }
61 }
62
63
64 /**
65  * Convert ATS properties from host to network byte order.
66  *
67  * @param nbo[OUT] value written
68  * @param hbo value read
69  */
70 void
71 GNUNET_ATS_properties_hton (struct GNUNET_ATS_PropertiesNBO *nbo,
72                             const struct GNUNET_ATS_Properties *hbo)
73 {
74   nbo->utilization_out = htonl (hbo->utilization_out);
75   nbo->utilization_in = htonl (hbo->utilization_in);
76   nbo->scope = htonl ((uint32_t) hbo->scope);
77   nbo->distance = htonl (hbo->distance);
78   nbo->delay = GNUNET_TIME_relative_hton (hbo->delay);
79 }
80
81
82 /**
83  * Convert ATS properties from network to host byte order.
84  *
85  * @param hbo[OUT] value written
86  * @param nbo value read
87  */
88 void
89 GNUNET_ATS_properties_ntoh (struct GNUNET_ATS_Properties *hbo,
90                             const struct GNUNET_ATS_PropertiesNBO *nbo)
91 {
92   hbo->utilization_out = ntohl (nbo->utilization_out);
93   hbo->utilization_in = ntohl (nbo->utilization_in);
94   hbo->scope = ntohl ((uint32_t) nbo->scope);
95   hbo->distance = ntohl (nbo->distance);
96   hbo->delay = GNUNET_TIME_relative_ntoh (nbo->delay);
97 }
98
99
100 /**
101  * We keep a list of our local networks so we can answer
102  * LAN vs. WAN questions.  Note: WLAN is not detected yet.
103  * (maybe we can do that heuristically based on interface
104  * name in the future?).
105  */
106 struct ATS_Network
107 {
108   /**
109    * Kept in a DLL.
110    */
111   struct ATS_Network *next;
112
113   /**
114    * Kept in a DLL.
115    */
116   struct ATS_Network *prev;
117
118   /**
119    * Network address.
120    */
121   struct sockaddr *network;
122
123   /**
124    * Netmask to determine what is in the LAN.
125    */
126   struct sockaddr *netmask;
127
128   /**
129    * How long are @e network and @e netmask?
130    */
131   socklen_t length;
132 };
133
134
135 /**
136  * Handle to the interface scanner.
137  */
138 struct GNUNET_ATS_InterfaceScanner
139 {
140
141   /**
142    * Head of LAN networks list.
143    */
144   struct ATS_Network *net_head;
145
146   /**
147    * Tail of LAN networks list.
148    */
149   struct ATS_Network *net_tail;
150
151   /**
152    * Task for periodically refreshing our LAN network list.
153    */
154   struct GNUNET_SCHEDULER_Task *interface_task;
155
156 };
157
158
159 /**
160  * Delete all entries from the current network list.
161  *
162  * @param is scanner to clean up
163  */
164 static void
165 delete_networks (struct GNUNET_ATS_InterfaceScanner *is)
166 {
167   struct ATS_Network *cur;
168
169   while (NULL != (cur = is->net_head))
170   {
171     GNUNET_CONTAINER_DLL_remove (is->net_head,
172                                  is->net_tail,
173                                  cur);
174     GNUNET_free (cur);
175   }
176 }
177
178
179 /**
180  * Function invoked for each interface found.  Adds the interface's
181  * network addresses to the respective DLL, so we can distinguish
182  * between LAN and WAN.
183  *
184  * @param cls closure with the `struct GNUNET_ATS_InterfaceScanner`
185  * @param name name of the interface (can be NULL for unknown)
186  * @param isDefault is this presumably the default interface
187  * @param addr address of this interface (can be NULL for unknown or unassigned)
188  * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
189  * @param netmask the network mask (can be NULL for unknown or unassigned)
190  * @param addrlen length of the address
191  * @return #GNUNET_OK to continue iteration
192  */
193 static int
194 interface_proc (void *cls,
195                 const char *name,
196                 int isDefault,
197                 const struct sockaddr *addr,
198                 const struct sockaddr *broadcast_addr,
199                 const struct sockaddr *netmask,
200                 socklen_t addrlen)
201 {
202   struct GNUNET_ATS_InterfaceScanner *is = cls;
203   /* Calculate network */
204   struct ATS_Network *net = NULL;
205
206   /* Skipping IPv4 loopback addresses since we have special check  */
207   if  (addr->sa_family == AF_INET)
208   {
209     const struct sockaddr_in *a4 = (const struct sockaddr_in *) addr;
210
211     if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
212        return GNUNET_OK;
213   }
214   /* Skipping IPv6 loopback addresses since we have special check  */
215   if  (addr->sa_family == AF_INET6)
216   {
217     const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *) addr;
218     if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
219       return GNUNET_OK;
220   }
221
222   if (addr->sa_family == AF_INET)
223   {
224     const struct sockaddr_in *addr4 = (const struct sockaddr_in *) addr;
225     const struct sockaddr_in *netmask4 = (const struct sockaddr_in *) netmask;
226     struct sockaddr_in *tmp;
227     struct sockaddr_in network4;
228
229     net = GNUNET_malloc (sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in));
230     tmp = (struct sockaddr_in *) &net[1];
231     net->network = (struct sockaddr *) &tmp[0];
232     net->netmask = (struct sockaddr *) &tmp[1];
233     net->length = addrlen;
234
235     memset (&network4, 0, sizeof (network4));
236     network4.sin_family = AF_INET;
237 #if HAVE_SOCKADDR_IN_SIN_LEN
238     network4.sin_len = sizeof (network4);
239 #endif
240     network4.sin_addr.s_addr = (addr4->sin_addr.s_addr & netmask4->sin_addr.s_addr);
241
242     GNUNET_memcpy (net->netmask, netmask4, sizeof (struct sockaddr_in));
243     GNUNET_memcpy (net->network, &network4, sizeof (struct sockaddr_in));
244   }
245
246   if (addr->sa_family == AF_INET6)
247   {
248     const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *) addr;
249     const struct sockaddr_in6 *netmask6 = (const struct sockaddr_in6 *) netmask;
250     struct sockaddr_in6 * tmp;
251     struct sockaddr_in6 network6;
252
253     net = GNUNET_malloc (sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in6));
254     tmp = (struct sockaddr_in6 *) &net[1];
255     net->network = (struct sockaddr *) &tmp[0];
256     net->netmask = (struct sockaddr *) &tmp[1];
257     net->length = addrlen;
258
259     memset (&network6, 0, sizeof (network6));
260     network6.sin6_family = AF_INET6;
261 #if HAVE_SOCKADDR_IN_SIN_LEN
262     network6.sin6_len = sizeof (network6);
263 #endif
264     unsigned int c = 0;
265     uint32_t *addr_elem = (uint32_t *) &addr6->sin6_addr;
266     uint32_t *mask_elem = (uint32_t *) &netmask6->sin6_addr;
267     uint32_t *net_elem = (uint32_t *) &network6.sin6_addr;
268     for (c = 0; c < 4; c++)
269       net_elem[c] = addr_elem[c] & mask_elem[c];
270
271     GNUNET_memcpy (net->netmask, netmask6, sizeof (struct sockaddr_in6));
272     GNUNET_memcpy (net->network, &network6, sizeof (struct sockaddr_in6));
273   }
274   if (NULL == net)
275     return GNUNET_OK; /* odd / unsupported address family */
276
277   /* Store in list */
278 #if VERBOSE_ATS
279   char * netmask = GNUNET_strdup (GNUNET_a2s((struct sockaddr *) net->netmask, addrlen));
280   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
281                    "ats-scanner-api",
282                    "Adding network `%s', netmask `%s'\n",
283                    GNUNET_a2s ((struct sockaddr *) net->network,
284                                addrlen),
285                    netmask);
286   GNUNET_free (netmask);
287 #endif
288   GNUNET_CONTAINER_DLL_insert (is->net_head,
289                                is->net_tail,
290                                net);
291
292   return GNUNET_OK;
293 }
294
295
296 /**
297  * Periodically get list of network addresses from our interfaces.
298  *
299  * @param cls closure
300  */
301 static void
302 get_addresses (void *cls)
303 {
304   struct GNUNET_ATS_InterfaceScanner *is = cls;
305
306   is->interface_task = NULL;
307   delete_networks (is);
308   GNUNET_OS_network_interfaces_list (&interface_proc,
309                                      is);
310   is->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVAL,
311                                                      &get_addresses,
312                                                      is);
313 }
314
315
316 /**
317  * Returns where the address is located: LAN or WAN or ...
318  *
319  * @param is the interface scanner handle
320  * @param addr address
321  * @param addrlen address length
322  * @return type of the network the address belongs to
323  */
324 enum GNUNET_ATS_Network_Type
325 GNUNET_ATS_scanner_address_get_type (struct GNUNET_ATS_InterfaceScanner *is,
326                                      const struct sockaddr *addr,
327                                      socklen_t addrlen)
328 {
329   struct ATS_Network *cur = is->net_head;
330   enum GNUNET_ATS_Network_Type type = GNUNET_ATS_NET_UNSPECIFIED;
331
332   switch (addr->sa_family)
333     {
334     case AF_UNIX:
335       type = GNUNET_ATS_NET_LOOPBACK;
336       break;
337     case AF_INET:
338       {
339         const struct sockaddr_in *a4 = (const struct sockaddr_in *) addr;
340
341         if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
342           type = GNUNET_ATS_NET_LOOPBACK;
343         break;
344       }
345     case AF_INET6:
346       {
347         const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *) addr;
348
349         if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
350           type = GNUNET_ATS_NET_LOOPBACK;
351         break;
352       }
353     default:
354       GNUNET_break (0);
355       break;
356    }
357
358   /* Check local networks */
359   while ((NULL != cur) && (GNUNET_ATS_NET_UNSPECIFIED == type))
360   {
361     if (addrlen != cur->length)
362     {
363       cur = cur->next;
364       continue;
365     }
366     if (addr->sa_family == AF_INET)
367     {
368       const struct sockaddr_in *a4 = (const struct sockaddr_in *) addr;
369       const struct sockaddr_in *net4 = (const struct sockaddr_in *) cur->network;
370       const struct sockaddr_in *mask4 = (const struct sockaddr_in *) cur->netmask;
371
372       if (((a4->sin_addr.s_addr & mask4->sin_addr.s_addr)) == net4->sin_addr.s_addr)
373         type = GNUNET_ATS_NET_LAN;
374     }
375     if (addr->sa_family == AF_INET6)
376     {
377       const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *) addr;
378       const struct sockaddr_in6 *net6 = (const struct sockaddr_in6 *) cur->network;
379       const struct sockaddr_in6 *mask6 = (const struct sockaddr_in6 *) cur->netmask;
380
381       int res = GNUNET_YES;
382       int c = 0;
383       uint32_t *addr_elem = (uint32_t *) &a6->sin6_addr;
384       uint32_t *mask_elem = (uint32_t *) &mask6->sin6_addr;
385       uint32_t *net_elem = (uint32_t *) &net6->sin6_addr;
386       for (c = 0; c < 4; c++)
387         if ((addr_elem[c] & mask_elem[c]) != net_elem[c])
388           res = GNUNET_NO;
389
390       if (res == GNUNET_YES)
391         type = GNUNET_ATS_NET_LAN;
392     }
393     cur = cur->next;
394   }
395
396   /* no local network found for this address, default: WAN */
397   if (type == GNUNET_ATS_NET_UNSPECIFIED)
398     type = GNUNET_ATS_NET_WAN;
399   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
400                    "ats-scanner-api",
401                    "`%s' is in network `%s'\n",
402                    GNUNET_a2s (addr,
403                                addrlen),
404                    GNUNET_ATS_print_network_type (type));
405   return type;
406 }
407
408
409 /**
410  * Initialize the interface scanner.
411  *
412  * @return interface scanner
413  */
414 struct GNUNET_ATS_InterfaceScanner *
415 GNUNET_ATS_scanner_init ()
416 {
417   struct GNUNET_ATS_InterfaceScanner *is;
418
419   is = GNUNET_new (struct GNUNET_ATS_InterfaceScanner);
420   GNUNET_OS_network_interfaces_list (&interface_proc,
421                                      is);
422   is->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVAL,
423                                                      &get_addresses,
424                                                      is);
425   return is;
426 }
427
428
429 /**
430  * Client is done with the interface scanner, release resources.
431  *
432  * @param is handle to release
433  */
434 void
435 GNUNET_ATS_scanner_done (struct GNUNET_ATS_InterfaceScanner *is)
436 {
437   if (NULL != is->interface_task)
438   {
439     GNUNET_SCHEDULER_cancel (is->interface_task);
440     is->interface_task = NULL;
441   }
442   delete_networks (is);
443   GNUNET_free (is);
444 }
445
446
447 /* end of ats_api_scanner.c */