2 This file is part of GNUnet.
3 (C) 2004, 2005, 2006 Christian Grothoff (and other contributing authors)
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 2, 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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
23 * @file util/os_network.c
24 * @brief function to determine available network interfaces
26 * @author Heikki Lindholm
32 #include "gnunet_common.h"
33 #include "gnunet_os_lib.h"
36 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
37 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
40 * @brief Enumerate all network interfaces
42 * @param proc the callback function
43 * @param proc_cls closure for proc
46 GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc,
52 struct EnumNICs3_results *results = NULL;
55 r = EnumNICs3 (&results, &results_count);
59 for (i = 0; i < results_count; i++)
62 proc (proc_cls, results[i].pretty_name, results[i].is_default,
65 flags & ENUMNICS3_BCAST_OK ? &results[i].broadcast : NULL,
66 results[i].flags & ENUMNICS3_MASK_OK ? &results[i].mask : NULL,
67 results[i].addr_size))
70 EnumNICs3_free (results);
73 #elif HAVE_GETIFADDRS && HAVE_FREEIFADDRS
75 struct ifaddrs *ifa_first;
76 struct ifaddrs *ifa_ptr;
79 if (getifaddrs (&ifa_first) == 0)
81 for (ifa_ptr = ifa_first; ifa_ptr != NULL; ifa_ptr = ifa_ptr->ifa_next)
83 if (ifa_ptr->ifa_name != NULL && ifa_ptr->ifa_addr != NULL &&
84 (ifa_ptr->ifa_flags & IFF_UP) != 0)
86 if ((ifa_ptr->ifa_addr->sa_family != AF_INET) &&
87 (ifa_ptr->ifa_addr->sa_family != AF_INET6))
89 if (ifa_ptr->ifa_addr->sa_family == AF_INET)
90 alen = sizeof (struct sockaddr_in);
92 alen = sizeof (struct sockaddr_in6);
94 proc (proc_cls, ifa_ptr->ifa_name,
95 0 == strcmp (ifa_ptr->ifa_name, GNUNET_DEFAULT_INTERFACE),
96 ifa_ptr->ifa_addr, ifa_ptr->ifa_broadaddr,
97 ifa_ptr->ifa_netmask, alen))
101 freeifaddrs (ifa_first);
110 struct sockaddr_in a4;
111 struct sockaddr_in6 a6;
115 if (system ("ifconfig -a > /dev/null 2> /dev/null"))
116 if (system ("/sbin/ifconfig -a > /dev/null 2> /dev/null") == 0)
117 f = popen ("/sbin/ifconfig -a 2> /dev/null", "r");
121 f = popen ("ifconfig -a 2> /dev/null", "r");
124 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
125 "popen", "ifconfig");
129 have_ifc = GNUNET_NO;
131 while (NULL != fgets (line, sizeof (line), f))
133 if (strlen (line) == 0)
135 have_ifc = GNUNET_NO;
138 if (!isspace (line[0]))
140 have_ifc = (1 == SSCANF (line, "%11s", ifc)) ? GNUNET_YES : GNUNET_NO;
141 /* would end with ':' on OSX, fix it! */
142 if (ifc[strlen (ifc) - 1] == ':')
143 ifc[strlen (ifc) - 1] = '\0';
147 continue; /* strange input, hope for the best */
149 while (('\0' != *start) && (isspace (*start)))
152 (1 == SSCANF (start, "inet addr:%127s", addrstr)) ||
153 (1 == SSCANF (start, "inet6 addr:%127s", addrstr)) ||
155 (1 == SSCANF (start, "inet %127s", addrstr)) ||
156 (1 == SSCANF (start, "inet6 %127s", addrstr)))
159 if (1 == inet_pton (AF_INET, addrstr, &v4))
161 memset (&a4, 0, sizeof (a4));
162 a4.sin_family = AF_INET;
163 #if HAVE_SOCKADDR_IN_SIN_LEN
164 a4.sin_len = (u_char) sizeof (struct sockaddr_in);
168 proc (proc_cls, ifc, 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE),
169 (const struct sockaddr *) &a4,
170 /* TODO broadcast and netmask */
171 NULL, NULL, sizeof (a4)))
176 if (1 == inet_pton (AF_INET6, addrstr, &v6))
178 memset (&a6, 0, sizeof (a6));
179 a6.sin6_family = AF_INET6;
180 #if HAVE_SOCKADDR_IN_SIN_LEN
181 a6.sin6_len = (u_char) sizeof (struct sockaddr_in6);
185 proc (proc_cls, ifc, 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE),
186 (const struct sockaddr *) &a6,
187 /* TODO broadcast and netmask */
188 NULL, NULL, sizeof (a6)))
199 /* end of os_network.c */