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