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