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 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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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;
74 const char *sbin_ifconfig;
77 if (0 == access (IFCONFIG, X_OK))
78 sbin_ifconfig = IFCONFIG;
81 if (0 == access ("/sbin/ifconfig", X_OK))
82 sbin_ifconfig = "/sbin/ifconfig";
83 else if (0 == access ("/usr/sbin/ifconfig", X_OK))
84 sbin_ifconfig = "/usr/sbin/ifconfig";
86 sbin_ifconfig = "ifconfig";
87 GNUNET_asprintf (&pcall,
91 f = popen (pcall, "r");
94 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
101 have_ifc = GNUNET_NO;
103 while (NULL != fgets (line, sizeof (line), f))
105 if (strlen (line) == 0)
107 have_ifc = GNUNET_NO;
110 if (!isspace (line[0]))
112 have_ifc = (1 == SSCANF (line, "%11s", ifc)) ? GNUNET_YES : GNUNET_NO;
113 /* would end with ':' on OSX, fix it! */
114 if (ifc[strlen (ifc) - 1] == ':')
115 ifc[strlen (ifc) - 1] = '\0';
119 continue; /* strange input, hope for the best */
121 /* make parsing of ipv6 addresses easier */
122 for (replace = line; *replace != '\0'; replace++)
130 while (('\0' != *start) && (isspace (*start)))
133 /* Zero-out stack allocated values */
134 memset (addrstr, 0, 128);
135 memset (netmaskstr, 0, 128);
136 memset (bcstr, 0, 128);
140 (3 == SSCANF (start, "inet addr:%127s Bcast:%127s Mask:%127s", addrstr, bcstr, netmaskstr)) ||
141 (2 == SSCANF (start, "inet addr:%127s Mask:%127s", addrstr, netmaskstr)) ||
142 (2 == SSCANF (start, "inet6 addr:%127s %d", addrstr, &prefixlen)) ||
144 (1 == SSCANF (start, "inet %127s", addrstr)) ||
145 (1 == SSCANF (start, "inet6 %127s", addrstr)))
148 if (1 == inet_pton (AF_INET, addrstr, &v4))
150 memset (&a4, 0, sizeof (a4));
151 a4.sin_family = AF_INET;
152 #if HAVE_SOCKADDR_IN_SIN_LEN
153 a4.sin_len = (u_char) sizeof (struct sockaddr_in);
159 if (1 == inet_pton (AF_INET, bcstr, &v4))
161 memset (&bcaddr, 0, sizeof (bcaddr));
162 bcaddr.sin_family = AF_INET;
163 #if HAVE_SOCKADDR_IN_SIN_LEN
164 bcaddr.sin_len = (u_char) sizeof (struct sockaddr_in);
166 bcaddr.sin_addr = v4;
167 pass_bcaddr = (struct sockaddr *) &bcaddr;
169 if (1 == inet_pton (AF_INET, netmaskstr, &v4))
171 memset (&netmask, 0, sizeof (netmask));
172 netmask.sin_family = AF_INET;
173 #if HAVE_SOCKADDR_IN_SIN_LEN
174 netmask.sin_len = (u_char) sizeof (struct sockaddr_in);
176 netmask.sin_addr = v4;
177 pass_netmask = (struct sockaddr *) &netmask;
182 proc (proc_cls, ifc, 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE),
183 (const struct sockaddr *) &a4,
184 pass_bcaddr, pass_netmask, sizeof (a4)))
189 if (1 == inet_pton (AF_INET6, addrstr, &v6))
191 memset (&a6, 0, sizeof (a6));
192 a6.sin6_family = AF_INET6;
193 #if HAVE_SOCKADDR_IN_SIN_LEN
194 a6.sin6_len = (u_char) sizeof (struct sockaddr_in6);
201 memset (v6.s6_addr, 0, sizeof (v6.s6_addr));
202 for (i = 0; i < prefixlen; i++)
204 v6.s6_addr[i >> 3] |= 1 << (i & 7);
206 memset (&netmask6, 0, sizeof (netmask6));
207 netmask6.sin6_family = AF_INET6;
208 #if HAVE_SOCKADDR_IN_SIN_LEN
209 netmask6.sin6_len = (u_char) sizeof (struct sockaddr_in6);
211 netmask6.sin6_addr = v6;
213 pass_netmask = (struct sockaddr *) &netmask6;
217 proc (proc_cls, ifc, 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE),
218 (const struct sockaddr *) &a6,
219 NULL, pass_netmask, sizeof (a6)))
231 * Try to enumerate all network interfaces using 'ip'.
233 * @param proc the callback function
234 * @param proc_cls closure for @a proc
235 * @return #GNUNET_OK if it worked
238 try_ip (GNUNET_OS_NetworkInterfaceProcessor proc,
247 struct sockaddr_in a4;
248 struct sockaddr_in6 a6;
251 struct sockaddr_in netmask;
252 struct sockaddr_in6 netmask6;
254 unsigned int prefixlen;
262 if (0 == access (PATH_TO_IP, X_OK))
263 sbin_ip = PATH_TO_IP;
266 if (0 == access ("/sbin/ip", X_OK))
267 sbin_ip = "/sbin/ip";
268 else if (0 == access ("/usr/sbin/ip", X_OK))
269 sbin_ip = "/usr/sbin/ip";
272 GNUNET_asprintf (&pcall,
273 "%s -o add 2> /dev/null",
276 f = popen (pcall, "r");
279 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
282 return GNUNET_SYSERR;
285 while (NULL != fgets (line, sizeof (line), f))
287 /* make parsing easier */
288 for (replace = line; *replace != '\0'; replace++)
293 /* Zero-out stack allocated values */
294 memset (ifname, 0, 64);
295 memset (afstr, 0, 6);
296 memset (addrstr, 0, 128);
297 if (4 != SSCANF (line,
298 "%*u: %63s %5s %127s %6u",
305 if ( (0 == strcasecmp ("inet",
307 (1 == inet_pton (AF_INET,
311 memset (&a4, 0, sizeof (a4));
312 a4.sin_family = AF_INET;
313 #if HAVE_SOCKADDR_IN_SIN_LEN
314 a4.sin_len = (u_char) sizeof (struct sockaddr_in);
318 memset (&v4.s_addr, 0, sizeof (v4.s_addr));
319 for (i = 0; i < prefixlen; i++)
320 v4.s_addr |= 1 << (i & 7);
321 memset (&netmask, 0, sizeof (netmask));
322 netmask.sin_family = AF_INET;
323 #if HAVE_SOCKADDR_IN_SIN_LEN
324 netmask.sin_len = (u_char) sizeof (struct sockaddr_in);
326 netmask.sin_addr = v4;
331 (0 == strcmp (ifname,
332 GNUNET_DEFAULT_INTERFACE)),
333 (const struct sockaddr *) &a4,
335 (const struct sockaddr *) &netmask,
340 if ( (0 == strcasecmp ("inet6",
342 (1 == inet_pton (AF_INET6,
346 memset (&a6, 0, sizeof (a6));
347 a6.sin6_family = AF_INET6;
348 #if HAVE_SOCKADDR_IN_SIN_LEN
349 a6.sin6_len = (u_char) sizeof (struct sockaddr_in6);
353 memset (v6.s6_addr, 0, sizeof (v6.s6_addr));
354 for (i = 0; i < prefixlen; i++)
355 v6.s6_addr[i >> 3] |= 1 << (i & 7);
356 memset (&netmask6, 0, sizeof (netmask6));
357 netmask6.sin6_family = AF_INET6;
358 #if HAVE_SOCKADDR_IN_SIN_LEN
359 netmask6.sin6_len = (u_char) sizeof (struct sockaddr_in6);
361 netmask6.sin6_addr = v6;
366 (0 == strcmp (ifname,
367 GNUNET_DEFAULT_INTERFACE)),
368 (const struct sockaddr *) &a6,
370 (const struct sockaddr *) &netmask6,
382 * @brief Enumerate all network interfaces
384 * @param proc the callback function
385 * @param proc_cls closure for @a proc
388 GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc,
394 struct EnumNICs3_results *results = NULL;
397 r = EnumNICs3 (&results, &results_count);
401 for (i = 0; i < results_count; i++)
404 proc (proc_cls, results[i].pretty_name, results[i].is_default,
405 (const struct sockaddr *) &results[i].address,
407 flags & ENUMNICS3_BCAST_OK ?
408 (const struct sockaddr *) &results[i].broadcast : NULL,
409 results[i].flags & ENUMNICS3_MASK_OK ?
410 (const struct sockaddr *) &results[i].mask : NULL,
411 results[i].addr_size))
414 EnumNICs3_free (results);
417 #elif HAVE_GETIFADDRS && HAVE_FREEIFADDRS
419 struct ifaddrs *ifa_first;
420 struct ifaddrs *ifa_ptr;
423 if (getifaddrs (&ifa_first) == 0)
425 for (ifa_ptr = ifa_first; ifa_ptr != NULL; ifa_ptr = ifa_ptr->ifa_next)
427 if (ifa_ptr->ifa_name != NULL && ifa_ptr->ifa_addr != NULL &&
428 (ifa_ptr->ifa_flags & IFF_UP) != 0)
430 if ((ifa_ptr->ifa_addr->sa_family != AF_INET) &&
431 (ifa_ptr->ifa_addr->sa_family != AF_INET6))
433 if (ifa_ptr->ifa_addr->sa_family == AF_INET)
434 alen = sizeof (struct sockaddr_in);
436 alen = sizeof (struct sockaddr_in6);
438 proc (proc_cls, ifa_ptr->ifa_name,
439 0 == strcmp (ifa_ptr->ifa_name, GNUNET_DEFAULT_INTERFACE),
440 ifa_ptr->ifa_addr, ifa_ptr->ifa_broadaddr,
441 ifa_ptr->ifa_netmask, alen))
445 freeifaddrs (ifa_first);
456 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
457 "Failed to enumerate network interfaces\n");
462 /* end of os_network.c */