-improve UDP logging
[oweals/gnunet.git] / src / ats / ats_api_scanner.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2010-2015 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, 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 a ATS property to a string
66  *
67  * @param type the property type
68  * @return a string or NULL if invalid
69  */
70 const char *
71 GNUNET_ATS_print_property_type (enum GNUNET_ATS_Property type)
72 {
73   switch (type)
74   {
75   case GNUNET_ATS_ARRAY_TERMINATOR:
76     return "TERMINATOR";
77   case GNUNET_ATS_UTILIZATION_OUT:
78     return "UTILIZATION_UP";
79   case GNUNET_ATS_UTILIZATION_IN:
80     return "UTILIZATION_DOWN";
81   case GNUNET_ATS_NETWORK_TYPE:
82     return "NETWORK_TYPE";
83   case GNUNET_ATS_QUALITY_NET_DELAY:
84     return "DELAY";
85   case GNUNET_ATS_QUALITY_NET_DISTANCE:
86     return "DISTANCE";
87   default:
88     GNUNET_break (0);
89     return NULL;
90   }
91 }
92
93
94 /**
95  * We keep a list of our local networks so we can answer
96  * LAN vs. WAN questions.  Note: WLAN is not detected yet.
97  * (maybe we can do that heuristically based on interface
98  * name in the future?).
99  */
100 struct ATS_Network
101 {
102   /**
103    * Kept in a DLL.
104    */
105   struct ATS_Network *next;
106
107   /**
108    * Kept in a DLL.
109    */
110   struct ATS_Network *prev;
111
112   /**
113    * Network address.
114    */
115   struct sockaddr *network;
116
117   /**
118    * Netmask to determine what is in the LAN.
119    */
120   struct sockaddr *netmask;
121
122   /**
123    * How long are @e network and @e netmask?
124    */
125   socklen_t length;
126 };
127
128
129 /**
130  * Handle to the interface scanner.
131  */
132 struct GNUNET_ATS_InterfaceScanner
133 {
134
135   /**
136    * Head of LAN networks list.
137    */
138   struct ATS_Network *net_head;
139
140   /**
141    * Tail of LAN networks list.
142    */
143   struct ATS_Network *net_tail;
144
145   /**
146    * Task for periodically refreshing our LAN network list.
147    */
148   struct GNUNET_SCHEDULER_Task *interface_task;
149
150 };
151
152
153 /**
154  * Delete all entries from the current network list.
155  *
156  * @param is scanner to clean up
157  */
158 static void
159 delete_networks (struct GNUNET_ATS_InterfaceScanner *is)
160 {
161   struct ATS_Network *cur;
162
163   while (NULL != (cur = is->net_head))
164   {
165     GNUNET_CONTAINER_DLL_remove (is->net_head,
166                                  is->net_tail,
167                                  cur);
168     GNUNET_free (cur);
169   }
170 }
171
172
173 /**
174  * Function invoked for each interface found.  Adds the interface's
175  * network addresses to the respective DLL, so we can distinguish
176  * between LAN and WAN.
177  *
178  * @param cls closure with the `struct GNUNET_ATS_InterfaceScanner`
179  * @param name name of the interface (can be NULL for unknown)
180  * @param isDefault is this presumably the default interface
181  * @param addr address of this interface (can be NULL for unknown or unassigned)
182  * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
183  * @param netmask the network mask (can be NULL for unknown or unassigned)
184  * @param addrlen length of the address
185  * @return #GNUNET_OK to continue iteration
186  */
187 static int
188 interface_proc (void *cls,
189                 const char *name,
190                 int isDefault,
191                 const struct sockaddr *addr,
192                 const struct sockaddr *broadcast_addr,
193                 const struct sockaddr *netmask,
194                 socklen_t addrlen)
195 {
196   struct GNUNET_ATS_InterfaceScanner *is = cls;
197   /* Calculate network */
198   struct ATS_Network *net = NULL;
199
200   /* Skipping IPv4 loopback addresses since we have special check  */
201   if  (addr->sa_family == AF_INET)
202   {
203     const struct sockaddr_in *a4 = (const struct sockaddr_in *) addr;
204
205     if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
206        return GNUNET_OK;
207   }
208   /* Skipping IPv6 loopback addresses since we have special check  */
209   if  (addr->sa_family == AF_INET6)
210   {
211     const struct sockaddr_in6 *a6 = (const struct sockaddr_in6 *) addr;
212     if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
213       return GNUNET_OK;
214   }
215
216   if (addr->sa_family == AF_INET)
217   {
218     const struct sockaddr_in *addr4 = (const struct sockaddr_in *) addr;
219     const struct sockaddr_in *netmask4 = (const struct sockaddr_in *) netmask;
220     struct sockaddr_in *tmp;
221     struct sockaddr_in network4;
222
223     net = GNUNET_malloc (sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in));
224     tmp = (struct sockaddr_in *) &net[1];
225     net->network = (struct sockaddr *) &tmp[0];
226     net->netmask = (struct sockaddr *) &tmp[1];
227     net->length = addrlen;
228
229     memset (&network4, 0, sizeof (network4));
230     network4.sin_family = AF_INET;
231 #if HAVE_SOCKADDR_IN_SIN_LEN
232     network4.sin_len = sizeof (network4);
233 #endif
234     network4.sin_addr.s_addr = (addr4->sin_addr.s_addr & netmask4->sin_addr.s_addr);
235
236     memcpy (net->netmask, netmask4, sizeof (struct sockaddr_in));
237     memcpy (net->network, &network4, sizeof (struct sockaddr_in));
238   }
239
240   if (addr->sa_family == AF_INET6)
241   {
242     const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *) addr;
243     const struct sockaddr_in6 *netmask6 = (const struct sockaddr_in6 *) netmask;
244     struct sockaddr_in6 * tmp;
245     struct sockaddr_in6 network6;
246
247     net = GNUNET_malloc (sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in6));
248     tmp = (struct sockaddr_in6 *) &net[1];
249     net->network = (struct sockaddr *) &tmp[0];
250     net->netmask = (struct sockaddr *) &tmp[1];
251     net->length = addrlen;
252
253     memset (&network6, 0, sizeof (network6));
254     network6.sin6_family = AF_INET6;
255 #if HAVE_SOCKADDR_IN_SIN_LEN
256     network6.sin6_len = sizeof (network6);
257 #endif
258     unsigned int c = 0;
259     uint32_t *addr_elem = (uint32_t *) &addr6->sin6_addr;
260     uint32_t *mask_elem = (uint32_t *) &netmask6->sin6_addr;
261     uint32_t *net_elem = (uint32_t *) &network6.sin6_addr;
262     for (c = 0; c < 4; c++)
263       net_elem[c] = addr_elem[c] & mask_elem[c];
264
265     memcpy (net->netmask, netmask6, sizeof (struct sockaddr_in6));
266     memcpy (net->network, &network6, sizeof (struct sockaddr_in6));
267   }
268   if (NULL == net)
269     return GNUNET_OK; /* odd / unsupported address family */
270
271   /* Store in list */
272 #if VERBOSE_ATS
273   char * netmask = GNUNET_strdup (GNUNET_a2s((struct sockaddr *) net->netmask, addrlen));
274   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
275               "Adding network `%s', netmask `%s'\n",
276               GNUNET_a2s ((struct sockaddr *) net->network,
277                           addrlen),
278               netmask);
279   GNUNET_free (netmask);
280 #endif
281   GNUNET_CONTAINER_DLL_insert (is->net_head,
282                                is->net_tail,
283                                net);
284
285   return GNUNET_OK;
286 }
287
288
289 /**
290  * Periodically get list of network addresses from our interfaces.
291  *
292  * @param cls closure
293  * @param tc Task context
294  */
295 static void
296 get_addresses (void *cls,
297                const struct GNUNET_SCHEDULER_TaskContext *tc)
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-scheduling-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 */