glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / util / os_network.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2004, 2005, 2006, 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 /**
17  * @file util/os_network.c
18  * @brief function to determine available network interfaces
19  * @author Nils Durner
20  * @author Heikki Lindholm
21  * @author Jake Dust
22  * @author LRN
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27
28
29 #define LOG(kind,...) GNUNET_log_from (kind, "util-os-network", __VA_ARGS__)
30 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util-os-network", syscall, filename)
31
32
33 #if ! (HAVE_GETIFADDRS && HAVE_FREEIFADDRS) && !MINGW
34 /**
35  * Try to enumerate all network interfaces using 'ifconfig'.
36  *
37  * @param proc the callback function
38  * @param proc_cls closure for @a proc
39  * @return #GNUNET_OK if it worked
40  */
41 static int
42 try_ifconfig (GNUNET_OS_NetworkInterfaceProcessor proc,
43               void *proc_cls)
44 {
45   int i;
46   char line[1024];
47   char *replace;
48   const char *start;
49   char ifc[12];
50   char addrstr[128];
51   char bcstr[128];
52   char netmaskstr[128];
53   FILE *f;
54   int have_ifc;
55   struct sockaddr_in a4;
56   struct sockaddr_in6 a6;
57   struct in_addr v4;
58   struct in6_addr v6;
59   struct sockaddr_in bcaddr;
60   struct sockaddr_in netmask;
61   struct sockaddr_in6 netmask6;
62   struct sockaddr *pass_bcaddr;
63   struct sockaddr *pass_netmask;
64   int prefixlen;
65
66   if (system ("ifconfig -a > /dev/null 2> /dev/null"))
67     if (0 == system ("/sbin/ifconfig -a > /dev/null 2> /dev/null"))
68       f = popen ("/sbin/ifconfig -a 2> /dev/null", "r");
69     else
70       f = NULL;
71   else
72     f = popen ("ifconfig -a 2> /dev/null", "r");
73   if (! f)
74   {
75     LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
76                        "popen",
77                        "ifconfig");
78
79     return GNUNET_SYSERR;
80   }
81
82   have_ifc = GNUNET_NO;
83   ifc[11] = '\0';
84   while (NULL != fgets (line, sizeof (line), f))
85   {
86     if (strlen (line) == 0)
87     {
88       have_ifc = GNUNET_NO;
89       continue;
90     }
91     if (!isspace (line[0]))
92     {
93       have_ifc = (1 == SSCANF (line, "%11s", ifc)) ? GNUNET_YES : GNUNET_NO;
94       /* would end with ':' on OSX, fix it! */
95       if (ifc[strlen (ifc) - 1] == ':')
96         ifc[strlen (ifc) - 1] = '\0';
97       continue;
98     }
99     if (!have_ifc)
100       continue;                 /* strange input, hope for the best */
101
102     /* make parsing of ipv6 addresses easier */
103     for (replace = line; *replace != '\0'; replace++)
104     {
105       if (*replace == '/')
106         *replace = ' ';
107     }
108     prefixlen = -1;
109
110     start = line;
111     while (('\0' != *start) && (isspace (*start)))
112       start++;
113
114     /* Zero-out stack allocated values */
115     memset (addrstr, 0, 128);
116     memset (netmaskstr, 0, 128);
117     memset (bcstr, 0, 128);
118     prefixlen = 0;
119
120     if ( /* Linux */
121          (3 == SSCANF (start, "inet addr:%127s Bcast:%127s Mask:%127s", addrstr, bcstr, netmaskstr)) ||
122          (2 == SSCANF (start, "inet addr:%127s Mask:%127s", addrstr, netmaskstr)) ||
123          (2 == SSCANF (start, "inet6 addr:%127s %d", addrstr, &prefixlen)) ||
124          /* Solaris, OS X */
125          (1 == SSCANF (start, "inet %127s", addrstr)) ||
126          (1 == SSCANF (start, "inet6 %127s", addrstr)))
127     {
128       /* IPv4 */
129       if (1 == inet_pton (AF_INET, addrstr, &v4))
130       {
131         memset (&a4, 0, sizeof (a4));
132         a4.sin_family = AF_INET;
133 #if HAVE_SOCKADDR_IN_SIN_LEN
134         a4.sin_len = (u_char) sizeof (struct sockaddr_in);
135 #endif
136         a4.sin_addr = v4;
137
138         pass_bcaddr = NULL;
139         pass_netmask = NULL;
140         if (1 == inet_pton (AF_INET, bcstr, &v4))
141         {
142           memset (&bcaddr, 0, sizeof (bcaddr));
143           bcaddr.sin_family = AF_INET;
144 #if HAVE_SOCKADDR_IN_SIN_LEN
145           bcaddr.sin_len = (u_char) sizeof (struct sockaddr_in);
146 #endif
147           bcaddr.sin_addr = v4;
148           pass_bcaddr = (struct sockaddr *) &bcaddr;
149         }
150         if (1 == inet_pton (AF_INET, netmaskstr, &v4))
151         {
152           memset (&netmask, 0, sizeof (netmask));
153           netmask.sin_family = AF_INET;
154 #if HAVE_SOCKADDR_IN_SIN_LEN
155           netmask.sin_len = (u_char) sizeof (struct sockaddr_in);
156 #endif
157           netmask.sin_addr = v4;
158           pass_netmask = (struct sockaddr *) &netmask;
159         }
160
161
162         if (GNUNET_OK !=
163             proc (proc_cls, ifc, 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE),
164                   (const struct sockaddr *) &a4,
165                   pass_bcaddr, pass_netmask, sizeof (a4)))
166           break;
167         continue;
168       }
169       /* IPv6 */
170       if (1 == inet_pton (AF_INET6, addrstr, &v6))
171       {
172         memset (&a6, 0, sizeof (a6));
173         a6.sin6_family = AF_INET6;
174 #if HAVE_SOCKADDR_IN_SIN_LEN
175         a6.sin6_len = (u_char) sizeof (struct sockaddr_in6);
176 #endif
177         a6.sin6_addr = v6;
178
179         pass_netmask = NULL;
180         if (prefixlen != -1)
181         {
182           memset (v6.s6_addr, 0, sizeof (v6.s6_addr));
183           for (i = 0; i < prefixlen; i++)
184           {
185             v6.s6_addr[i >> 3] |= 1 << (i & 7);
186           }
187           memset (&netmask6, 0, sizeof (netmask6));
188           netmask6.sin6_family = AF_INET6;
189 #if HAVE_SOCKADDR_IN_SIN_LEN
190           netmask6.sin6_len = (u_char) sizeof (struct sockaddr_in6);
191 #endif
192           netmask6.sin6_addr = v6;
193
194           pass_netmask = (struct sockaddr *) &netmask6;
195         }
196
197         if (GNUNET_OK !=
198             proc (proc_cls, ifc, 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE),
199                   (const struct sockaddr *) &a6,
200                   NULL, pass_netmask, sizeof (a6)))
201           break;
202         continue;
203       }
204     }
205   }
206   pclose (f);
207   return GNUNET_OK;
208 }
209
210
211 /**
212  * Try to enumerate all network interfaces using 'ip'.
213  *
214  * @param proc the callback function
215  * @param proc_cls closure for @a proc
216  * @return #GNUNET_OK if it worked
217  */
218 static int
219 try_ip (GNUNET_OS_NetworkInterfaceProcessor proc,
220         void *proc_cls)
221 {
222   char line[1024];
223   char *replace;
224   char ifname[64];
225   char afstr[6];
226   char addrstr[128];
227   FILE *f;
228   struct sockaddr_in a4;
229   struct sockaddr_in6 a6;
230   struct in_addr v4;
231   struct in6_addr v6;
232   struct sockaddr_in netmask;
233   struct sockaddr_in6 netmask6;
234   unsigned int i;
235   unsigned int prefixlen;
236
237   f = popen ("ip -o add 2> /dev/null", "r");
238   if (! f)
239   {
240     LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
241                        "popen",
242                        "ip");
243     return GNUNET_SYSERR;
244   }
245
246   while (NULL != fgets (line, sizeof (line), f))
247   {
248     /* make parsing easier */
249     for (replace = line; *replace != '\0'; replace++)
250     {
251       if (*replace == '/')
252         *replace = ' ';
253     }
254     /* Zero-out stack allocated values */
255     memset (ifname, 0, 64);
256     memset (afstr, 0, 6);
257     memset (addrstr, 0, 128);
258     if (4 != SSCANF (line,
259                      "%*u: %63s %5s %127s %6u",
260                      ifname,
261                      afstr,
262                      addrstr,
263                      &prefixlen))
264       continue;
265     /* IPv4 */
266     if ( (0 == strcasecmp ("inet",
267                            afstr)) &&
268          (1 == inet_pton (AF_INET,
269                           addrstr,
270                           &v4)) )
271     {
272       memset (&a4, 0, sizeof (a4));
273       a4.sin_family = AF_INET;
274 #if HAVE_SOCKADDR_IN_SIN_LEN
275       a4.sin_len = (u_char) sizeof (struct sockaddr_in);
276 #endif
277       a4.sin_addr = v4;
278
279       memset (&v4.s_addr, 0, sizeof (v4.s_addr));
280       for (i = 0; i < prefixlen; i++)
281         v4.s_addr |= 1 << (i & 7);
282       memset (&netmask, 0, sizeof (netmask));
283       netmask.sin_family = AF_INET;
284 #if HAVE_SOCKADDR_IN_SIN_LEN
285       netmask.sin_len = (u_char) sizeof (struct sockaddr_in);
286 #endif
287       netmask.sin_addr = v4;
288
289       if (GNUNET_OK !=
290           proc (proc_cls,
291                 ifname,
292                 (0 == strcmp (ifname,
293                               GNUNET_DEFAULT_INTERFACE)),
294                 (const struct sockaddr *) &a4,
295                 NULL,
296                 (const struct sockaddr *) &netmask,
297                 sizeof (a4)))
298         break;
299     }
300     /* IPv6 */
301     if ( (0 == strcasecmp ("inet6",
302                            afstr)) &&
303          (1 == inet_pton (AF_INET6,
304                           addrstr,
305                           &v6)) )
306     {
307       memset (&a6, 0, sizeof (a6));
308       a6.sin6_family = AF_INET6;
309 #if HAVE_SOCKADDR_IN_SIN_LEN
310       a6.sin6_len = (u_char) sizeof (struct sockaddr_in6);
311 #endif
312       a6.sin6_addr = v6;
313
314       memset (v6.s6_addr, 0, sizeof (v6.s6_addr));
315       for (i = 0; i < prefixlen; i++)
316         v6.s6_addr[i >> 3] |= 1 << (i & 7);
317       memset (&netmask6, 0, sizeof (netmask6));
318       netmask6.sin6_family = AF_INET6;
319 #if HAVE_SOCKADDR_IN_SIN_LEN
320       netmask6.sin6_len = (u_char) sizeof (struct sockaddr_in6);
321 #endif
322       netmask6.sin6_addr = v6;
323
324       if (GNUNET_OK !=
325           proc (proc_cls,
326                 ifname,
327                 (0 == strcmp (ifname,
328                               GNUNET_DEFAULT_INTERFACE)),
329                 (const struct sockaddr *) &a6,
330                 NULL,
331                 (const struct sockaddr *) &netmask6,
332                 sizeof (a6)))
333         break;
334     }
335   }
336   pclose (f);
337   return GNUNET_OK;
338 }
339 #endif
340
341
342 /**
343  * @brief Enumerate all network interfaces
344  *
345  * @param proc the callback function
346  * @param proc_cls closure for @a proc
347  */
348 void
349 GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc,
350                                    void *proc_cls)
351 {
352 #ifdef MINGW
353   int r;
354   int i;
355   struct EnumNICs3_results *results = NULL;
356   int results_count;
357
358   r = EnumNICs3 (&results, &results_count);
359   if (r != GNUNET_OK)
360     return;
361
362   for (i = 0; i < results_count; i++)
363   {
364     if (GNUNET_OK !=
365         proc (proc_cls, results[i].pretty_name, results[i].is_default,
366               (const struct sockaddr *) &results[i].address,
367               results[i].
368               flags & ENUMNICS3_BCAST_OK ?
369               (const struct sockaddr *) &results[i].broadcast : NULL,
370               results[i].flags & ENUMNICS3_MASK_OK ?
371               (const struct sockaddr *) &results[i].mask : NULL,
372               results[i].addr_size))
373       break;
374   }
375   EnumNICs3_free (results);
376   return;
377
378 #elif HAVE_GETIFADDRS && HAVE_FREEIFADDRS
379
380   struct ifaddrs *ifa_first;
381   struct ifaddrs *ifa_ptr;
382   socklen_t alen;
383
384   if (getifaddrs (&ifa_first) == 0)
385   {
386     for (ifa_ptr = ifa_first; ifa_ptr != NULL; ifa_ptr = ifa_ptr->ifa_next)
387     {
388       if (ifa_ptr->ifa_name != NULL && ifa_ptr->ifa_addr != NULL &&
389           (ifa_ptr->ifa_flags & IFF_UP) != 0)
390       {
391         if ((ifa_ptr->ifa_addr->sa_family != AF_INET) &&
392             (ifa_ptr->ifa_addr->sa_family != AF_INET6))
393           continue;
394         if (ifa_ptr->ifa_addr->sa_family == AF_INET)
395           alen = sizeof (struct sockaddr_in);
396         else
397           alen = sizeof (struct sockaddr_in6);
398         if (GNUNET_OK !=
399             proc (proc_cls, ifa_ptr->ifa_name,
400                   0 == strcmp (ifa_ptr->ifa_name, GNUNET_DEFAULT_INTERFACE),
401                   ifa_ptr->ifa_addr, ifa_ptr->ifa_broadaddr,
402                   ifa_ptr->ifa_netmask, alen))
403           break;
404       }
405     }
406     freeifaddrs (ifa_first);
407   }
408 #else
409   if (GNUNET_OK ==
410       try_ip (proc,
411               proc_cls))
412     return;
413   if (GNUNET_OK ==
414       try_ifconfig (proc,
415                     proc_cls))
416     return;
417   LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
418        "Failed to enumerate network interfaces\n");
419 #endif
420 }
421
422
423 /* end of os_network.c */