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