2 This file is part of GNUnet.
3 Copyright (C) 2004, 2005, 2006, 2015 GNUnet e.V.
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.
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.
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.
22 * @file util/os_network.c
23 * @brief function to determine available network interfaces
25 * @author Heikki Lindholm
28 * @author Christian Grothoff
31 #include "gnunet_util_lib.h"
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)
38 #if ! (HAVE_GETIFADDRS && HAVE_FREEIFADDRS) && !MINGW
40 * Try to enumerate all network interfaces using 'ifconfig'.
42 * @param proc the callback function
43 * @param proc_cls closure for @a proc
44 * @return #GNUNET_OK if it worked
47 try_ifconfig (GNUNET_OS_NetworkInterfaceProcessor proc,
60 struct sockaddr_in a4;
61 struct sockaddr_in6 a6;
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;
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");
77 f = popen ("ifconfig -a 2> /dev/null", "r");
80 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
89 while (NULL != fgets (line, sizeof (line), f))
91 if (strlen (line) == 0)
96 if (!isspace (line[0]))
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';
105 continue; /* strange input, hope for the best */
107 /* make parsing of ipv6 addresses easier */
108 for (replace = line; *replace != '\0'; replace++)
116 while (('\0' != *start) && (isspace (*start)))
119 /* Zero-out stack allocated values */
120 memset (addrstr, 0, 128);
121 memset (netmaskstr, 0, 128);
122 memset (bcstr, 0, 128);
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)) ||
130 (1 == SSCANF (start, "inet %127s", addrstr)) ||
131 (1 == SSCANF (start, "inet6 %127s", addrstr)))
134 if (1 == inet_pton (AF_INET, addrstr, &v4))
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);
145 if (1 == inet_pton (AF_INET, bcstr, &v4))
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);
152 bcaddr.sin_addr = v4;
153 pass_bcaddr = (struct sockaddr *) &bcaddr;
155 if (1 == inet_pton (AF_INET, netmaskstr, &v4))
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);
162 netmask.sin_addr = v4;
163 pass_netmask = (struct sockaddr *) &netmask;
168 proc (proc_cls, ifc, 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE),
169 (const struct sockaddr *) &a4,
170 pass_bcaddr, pass_netmask, sizeof (a4)))
175 if (1 == inet_pton (AF_INET6, addrstr, &v6))
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);
187 memset (v6.s6_addr, 0, sizeof (v6.s6_addr));
188 for (i = 0; i < prefixlen; i++)
190 v6.s6_addr[i >> 3] |= 1 << (i & 7);
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);
197 netmask6.sin6_addr = v6;
199 pass_netmask = (struct sockaddr *) &netmask6;
203 proc (proc_cls, ifc, 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE),
204 (const struct sockaddr *) &a6,
205 NULL, pass_netmask, sizeof (a6)))
217 * Try to enumerate all network interfaces using 'ip'.
219 * @param proc the callback function
220 * @param proc_cls closure for @a proc
221 * @return #GNUNET_OK if it worked
224 try_ip (GNUNET_OS_NetworkInterfaceProcessor proc,
233 struct sockaddr_in a4;
234 struct sockaddr_in6 a6;
237 struct sockaddr_in netmask;
238 struct sockaddr_in6 netmask6;
240 unsigned int prefixlen;
242 f = popen ("ip -o add 2> /dev/null", "r");
245 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
248 return GNUNET_SYSERR;
251 while (NULL != fgets (line, sizeof (line), f))
253 /* make parsing easier */
254 for (replace = line; *replace != '\0'; replace++)
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",
271 if ( (0 == strcasecmp ("inet",
273 (1 == inet_pton (AF_INET,
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);
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);
292 netmask.sin_addr = v4;
297 (0 == strcmp (ifname,
298 GNUNET_DEFAULT_INTERFACE)),
299 (const struct sockaddr *) &a4,
301 (const struct sockaddr *) &netmask,
306 if ( (0 == strcasecmp ("inet6",
308 (1 == inet_pton (AF_INET6,
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);
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);
327 netmask6.sin6_addr = v6;
332 (0 == strcmp (ifname,
333 GNUNET_DEFAULT_INTERFACE)),
334 (const struct sockaddr *) &a6,
336 (const struct sockaddr *) &netmask6,
348 * @brief Enumerate all network interfaces
350 * @param proc the callback function
351 * @param proc_cls closure for @a proc
354 GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc,
360 struct EnumNICs3_results *results = NULL;
363 r = EnumNICs3 (&results, &results_count);
367 for (i = 0; i < results_count; i++)
370 proc (proc_cls, results[i].pretty_name, results[i].is_default,
371 (const struct sockaddr *) &results[i].address,
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))
380 EnumNICs3_free (results);
383 #elif HAVE_GETIFADDRS && HAVE_FREEIFADDRS
385 struct ifaddrs *ifa_first;
386 struct ifaddrs *ifa_ptr;
389 if (getifaddrs (&ifa_first) == 0)
391 for (ifa_ptr = ifa_first; ifa_ptr != NULL; ifa_ptr = ifa_ptr->ifa_next)
393 if (ifa_ptr->ifa_name != NULL && ifa_ptr->ifa_addr != NULL &&
394 (ifa_ptr->ifa_flags & IFF_UP) != 0)
396 if ((ifa_ptr->ifa_addr->sa_family != AF_INET) &&
397 (ifa_ptr->ifa_addr->sa_family != AF_INET6))
399 if (ifa_ptr->ifa_addr->sa_family == AF_INET)
400 alen = sizeof (struct sockaddr_in);
402 alen = sizeof (struct sockaddr_in6);
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))
411 freeifaddrs (ifa_first);
422 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
423 "Failed to enumerate network interfaces\n");
428 /* end of os_network.c */