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