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/>.
20 * @file util/os_network.c
21 * @brief function to determine available network interfaces
23 * @author Heikki Lindholm
26 * @author Christian Grothoff
29 #include "gnunet_util_lib.h"
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)
36 #if ! (HAVE_GETIFADDRS && HAVE_FREEIFADDRS) && !MINGW
38 * Try to enumerate all network interfaces using 'ifconfig'.
40 * @param proc the callback function
41 * @param proc_cls closure for @a proc
42 * @return #GNUNET_OK if it worked
45 try_ifconfig (GNUNET_OS_NetworkInterfaceProcessor proc,
58 struct sockaddr_in a4;
59 struct sockaddr_in6 a6;
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;
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");
75 f = popen ("ifconfig -a 2> /dev/null", "r");
78 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
87 while (NULL != fgets (line, sizeof (line), f))
89 if (strlen (line) == 0)
94 if (!isspace (line[0]))
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';
103 continue; /* strange input, hope for the best */
105 /* make parsing of ipv6 addresses easier */
106 for (replace = line; *replace != '\0'; replace++)
114 while (('\0' != *start) && (isspace (*start)))
117 /* Zero-out stack allocated values */
118 memset (addrstr, 0, 128);
119 memset (netmaskstr, 0, 128);
120 memset (bcstr, 0, 128);
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)) ||
128 (1 == SSCANF (start, "inet %127s", addrstr)) ||
129 (1 == SSCANF (start, "inet6 %127s", addrstr)))
132 if (1 == inet_pton (AF_INET, addrstr, &v4))
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);
143 if (1 == inet_pton (AF_INET, bcstr, &v4))
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);
150 bcaddr.sin_addr = v4;
151 pass_bcaddr = (struct sockaddr *) &bcaddr;
153 if (1 == inet_pton (AF_INET, netmaskstr, &v4))
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);
160 netmask.sin_addr = v4;
161 pass_netmask = (struct sockaddr *) &netmask;
166 proc (proc_cls, ifc, 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE),
167 (const struct sockaddr *) &a4,
168 pass_bcaddr, pass_netmask, sizeof (a4)))
173 if (1 == inet_pton (AF_INET6, addrstr, &v6))
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);
185 memset (v6.s6_addr, 0, sizeof (v6.s6_addr));
186 for (i = 0; i < prefixlen; i++)
188 v6.s6_addr[i >> 3] |= 1 << (i & 7);
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);
195 netmask6.sin6_addr = v6;
197 pass_netmask = (struct sockaddr *) &netmask6;
201 proc (proc_cls, ifc, 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE),
202 (const struct sockaddr *) &a6,
203 NULL, pass_netmask, sizeof (a6)))
215 * Try to enumerate all network interfaces using 'ip'.
217 * @param proc the callback function
218 * @param proc_cls closure for @a proc
219 * @return #GNUNET_OK if it worked
222 try_ip (GNUNET_OS_NetworkInterfaceProcessor proc,
231 struct sockaddr_in a4;
232 struct sockaddr_in6 a6;
235 struct sockaddr_in netmask;
236 struct sockaddr_in6 netmask6;
238 unsigned int prefixlen;
240 f = popen ("ip -o add 2> /dev/null", "r");
243 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
246 return GNUNET_SYSERR;
249 while (NULL != fgets (line, sizeof (line), f))
251 /* make parsing easier */
252 for (replace = line; *replace != '\0'; replace++)
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",
269 if ( (0 == strcasecmp ("inet",
271 (1 == inet_pton (AF_INET,
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);
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);
290 netmask.sin_addr = v4;
295 (0 == strcmp (ifname,
296 GNUNET_DEFAULT_INTERFACE)),
297 (const struct sockaddr *) &a4,
299 (const struct sockaddr *) &netmask,
304 if ( (0 == strcasecmp ("inet6",
306 (1 == inet_pton (AF_INET6,
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);
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);
325 netmask6.sin6_addr = v6;
330 (0 == strcmp (ifname,
331 GNUNET_DEFAULT_INTERFACE)),
332 (const struct sockaddr *) &a6,
334 (const struct sockaddr *) &netmask6,
346 * @brief Enumerate all network interfaces
348 * @param proc the callback function
349 * @param proc_cls closure for @a proc
352 GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc,
358 struct EnumNICs3_results *results = NULL;
361 r = EnumNICs3 (&results, &results_count);
365 for (i = 0; i < results_count; i++)
368 proc (proc_cls, results[i].pretty_name, results[i].is_default,
369 (const struct sockaddr *) &results[i].address,
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))
378 EnumNICs3_free (results);
381 #elif HAVE_GETIFADDRS && HAVE_FREEIFADDRS
383 struct ifaddrs *ifa_first;
384 struct ifaddrs *ifa_ptr;
387 if (getifaddrs (&ifa_first) == 0)
389 for (ifa_ptr = ifa_first; ifa_ptr != NULL; ifa_ptr = ifa_ptr->ifa_next)
391 if (ifa_ptr->ifa_name != NULL && ifa_ptr->ifa_addr != NULL &&
392 (ifa_ptr->ifa_flags & IFF_UP) != 0)
394 if ((ifa_ptr->ifa_addr->sa_family != AF_INET) &&
395 (ifa_ptr->ifa_addr->sa_family != AF_INET6))
397 if (ifa_ptr->ifa_addr->sa_family == AF_INET)
398 alen = sizeof (struct sockaddr_in);
400 alen = sizeof (struct sockaddr_in6);
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))
409 freeifaddrs (ifa_first);
420 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
421 "Failed to enumerate network interfaces\n");
426 /* end of os_network.c */