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, \
36 filename) GNUNET_log_from_strerror_file (kind, \
42 #if ! (HAVE_GETIFADDRS && HAVE_FREEIFADDRS)
44 * Try to enumerate all network interfaces using 'ifconfig'.
46 * @param proc the callback function
47 * @param proc_cls closure for @a proc
48 * @return #GNUNET_OK if it worked
51 try_ifconfig (GNUNET_OS_NetworkInterfaceProcessor proc,
64 struct sockaddr_in a4;
65 struct sockaddr_in6 a6;
68 struct sockaddr_in bcaddr;
69 struct sockaddr_in netmask;
70 struct sockaddr_in6 netmask6;
71 struct sockaddr *pass_bcaddr;
72 struct sockaddr *pass_netmask;
78 const char *sbin_ifconfig;
81 if (0 == access (IFCONFIG, X_OK))
82 sbin_ifconfig = IFCONFIG;
85 if (0 == access ("/sbin/ifconfig", X_OK))
86 sbin_ifconfig = "/sbin/ifconfig";
87 else if (0 == access ("/usr/sbin/ifconfig", X_OK))
88 sbin_ifconfig = "/usr/sbin/ifconfig";
90 sbin_ifconfig = "ifconfig";
91 GNUNET_asprintf (&pcall,
95 f = popen (pcall, "r");
98 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
102 return GNUNET_SYSERR;
105 have_ifc = GNUNET_NO;
107 while (NULL != fgets (line, sizeof(line), f))
109 if (strlen (line) == 0)
111 have_ifc = GNUNET_NO;
114 if (! isspace (line[0]))
116 have_ifc = (1 == sscanf (line, "%11s", ifc)) ? GNUNET_YES : GNUNET_NO;
117 /* would end with ':' on OSX, fix it! */
118 if (ifc[strlen (ifc) - 1] == ':')
119 ifc[strlen (ifc) - 1] = '\0';
123 continue; /* strange input, hope for the best */
125 /* make parsing of ipv6 addresses easier */
126 for (replace = line; *replace != '\0'; replace++)
134 while (('\0' != *start) && (isspace (*start)))
137 /* Zero-out stack allocated values */
138 memset (addrstr, 0, 128);
139 memset (netmaskstr, 0, 128);
140 memset (bcstr, 0, 128);
144 (3 == sscanf (start, "inet addr:%127s Bcast:%127s Mask:%127s", addrstr,
145 bcstr, netmaskstr)) ||
146 (2 == sscanf (start, "inet addr:%127s Mask:%127s", addrstr,
148 (2 == sscanf (start, "inet6 addr:%127s %d", addrstr, &prefixlen)) ||
150 (1 == sscanf (start, "inet %127s", addrstr)) ||
151 (1 == sscanf (start, "inet6 %127s", addrstr)))
154 if (1 == inet_pton (AF_INET, addrstr, &v4))
156 memset (&a4, 0, sizeof(a4));
157 a4.sin_family = AF_INET;
158 #if HAVE_SOCKADDR_IN_SIN_LEN
159 a4.sin_len = (u_char) sizeof(struct sockaddr_in);
165 if (1 == inet_pton (AF_INET, bcstr, &v4))
167 memset (&bcaddr, 0, sizeof(bcaddr));
168 bcaddr.sin_family = AF_INET;
169 #if HAVE_SOCKADDR_IN_SIN_LEN
170 bcaddr.sin_len = (u_char) sizeof(struct sockaddr_in);
172 bcaddr.sin_addr = v4;
173 pass_bcaddr = (struct sockaddr *) &bcaddr;
175 if (1 == inet_pton (AF_INET, netmaskstr, &v4))
177 memset (&netmask, 0, sizeof(netmask));
178 netmask.sin_family = AF_INET;
179 #if HAVE_SOCKADDR_IN_SIN_LEN
180 netmask.sin_len = (u_char) sizeof(struct sockaddr_in);
182 netmask.sin_addr = v4;
183 pass_netmask = (struct sockaddr *) &netmask;
188 proc (proc_cls, ifc,(0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE)),
189 (const struct sockaddr *) &a4,
190 pass_bcaddr, pass_netmask, sizeof(a4)))
195 if (1 == inet_pton (AF_INET6, addrstr, &v6))
197 memset (&a6, 0, sizeof(a6));
198 a6.sin6_family = AF_INET6;
199 #if HAVE_SOCKADDR_IN_SIN_LEN
200 a6.sin6_len = (u_char) sizeof(struct sockaddr_in6);
207 memset (v6.s6_addr, 0, sizeof(v6.s6_addr));
208 for (i = 0; i < prefixlen; i++)
210 v6.s6_addr[i >> 3] |= 1 << (i & 7);
212 memset (&netmask6, 0, sizeof(netmask6));
213 netmask6.sin6_family = AF_INET6;
214 #if HAVE_SOCKADDR_IN_SIN_LEN
215 netmask6.sin6_len = (u_char) sizeof(struct sockaddr_in6);
217 netmask6.sin6_addr = v6;
219 pass_netmask = (struct sockaddr *) &netmask6;
223 proc (proc_cls, ifc,(0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE)),
224 (const struct sockaddr *) &a6,
225 NULL, pass_netmask, sizeof(a6)))
237 * Try to enumerate all network interfaces using 'ip'.
239 * @param proc the callback function
240 * @param proc_cls closure for @a proc
241 * @return #GNUNET_OK if it worked
244 try_ip (GNUNET_OS_NetworkInterfaceProcessor proc,
253 struct sockaddr_in a4;
254 struct sockaddr_in6 a6;
257 struct sockaddr_in netmask;
258 struct sockaddr_in6 netmask6;
260 unsigned int prefixlen;
268 if (0 == access (PATH_TO_IP, X_OK))
269 sbin_ip = PATH_TO_IP;
272 if (0 == access ("/sbin/ip", X_OK))
273 sbin_ip = "/sbin/ip";
274 else if (0 == access ("/usr/sbin/ip", X_OK))
275 sbin_ip = "/usr/sbin/ip";
278 GNUNET_asprintf (&pcall,
279 "%s -o add 2> /dev/null",
282 f = popen (pcall, "r");
285 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
288 return GNUNET_SYSERR;
291 while (NULL != fgets (line, sizeof(line), f))
293 /* make parsing easier */
294 for (replace = line; *replace != '\0'; replace++)
299 /* Zero-out stack allocated values */
300 memset (ifname, 0, 64);
301 memset (afstr, 0, 6);
302 memset (addrstr, 0, 128);
303 if (4 != sscanf (line,
304 "%*u: %63s %5s %127s %6u",
311 if ((0 == strcasecmp ("inet",
313 (1 == inet_pton (AF_INET,
317 memset (&a4, 0, sizeof(a4));
318 a4.sin_family = AF_INET;
319 #if HAVE_SOCKADDR_IN_SIN_LEN
320 a4.sin_len = (u_char) sizeof(struct sockaddr_in);
324 memset (&v4.s_addr, 0, sizeof(v4.s_addr));
325 for (i = 0; i < prefixlen; i++)
326 v4.s_addr |= 1 << (i & 7);
327 memset (&netmask, 0, sizeof(netmask));
328 netmask.sin_family = AF_INET;
329 #if HAVE_SOCKADDR_IN_SIN_LEN
330 netmask.sin_len = (u_char) sizeof(struct sockaddr_in);
332 netmask.sin_addr = v4;
337 (0 == strcmp (ifname,
338 GNUNET_DEFAULT_INTERFACE)),
339 (const struct sockaddr *) &a4,
341 (const struct sockaddr *) &netmask,
346 if ((0 == strcasecmp ("inet6",
348 (1 == inet_pton (AF_INET6,
352 memset (&a6, 0, sizeof(a6));
353 a6.sin6_family = AF_INET6;
354 #if HAVE_SOCKADDR_IN_SIN_LEN
355 a6.sin6_len = (u_char) sizeof(struct sockaddr_in6);
359 memset (v6.s6_addr, 0, sizeof(v6.s6_addr));
360 for (i = 0; i < prefixlen; i++)
361 v6.s6_addr[i >> 3] |= 1 << (i & 7);
362 memset (&netmask6, 0, sizeof(netmask6));
363 netmask6.sin6_family = AF_INET6;
364 #if HAVE_SOCKADDR_IN_SIN_LEN
365 netmask6.sin6_len = (u_char) sizeof(struct sockaddr_in6);
367 netmask6.sin6_addr = v6;
372 (0 == strcmp (ifname,
373 GNUNET_DEFAULT_INTERFACE)),
374 (const struct sockaddr *) &a6,
376 (const struct sockaddr *) &netmask6,
390 * @brief Enumerate all network interfaces
392 * @param proc the callback function
393 * @param proc_cls closure for @a proc
396 GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc,
399 #if HAVE_GETIFADDRS && HAVE_FREEIFADDRS
400 struct ifaddrs *ifa_first;
401 struct ifaddrs *ifa_ptr;
404 if (getifaddrs (&ifa_first) == 0)
406 for (ifa_ptr = ifa_first; ifa_ptr != NULL; ifa_ptr = ifa_ptr->ifa_next)
408 if ((ifa_ptr->ifa_name != NULL) && (ifa_ptr->ifa_addr != NULL) &&
409 ( (ifa_ptr->ifa_flags & IFF_UP) != 0) )
411 if ((ifa_ptr->ifa_addr->sa_family != AF_INET) &&
412 (ifa_ptr->ifa_addr->sa_family != AF_INET6))
414 if (ifa_ptr->ifa_addr->sa_family == AF_INET)
415 alen = sizeof(struct sockaddr_in);
417 alen = sizeof(struct sockaddr_in6);
419 proc (proc_cls, ifa_ptr->ifa_name,
420 (0 == strcmp (ifa_ptr->ifa_name, GNUNET_DEFAULT_INTERFACE)),
421 ifa_ptr->ifa_addr, ifa_ptr->ifa_broadaddr,
422 ifa_ptr->ifa_netmask, alen))
426 freeifaddrs (ifa_first);
437 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
438 "Failed to enumerate network interfaces\n");
443 /* end of os_network.c */