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
31 #include "gnunet_common.h"
32 #include "gnunet_os_lib.h"
35 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
39 * @brief Enumerate all network interfaces
41 * @param proc the callback function
42 * @param proc_cls closure for proc
45 GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc,
50 PMIB_IPADDRTABLE pAddrTable;
51 DWORD dwIfIdx, dwExternalNIC;
54 /* Determine our external NIC */
55 theIP = inet_addr ("192.0.34.166"); /* www.example.com */
56 if ((!GNGetBestInterface) ||
57 (GNGetBestInterface (theIP, &dwExternalNIC) != NO_ERROR))
63 EnumNICs (&pTable, &pAddrTable);
67 for (dwIfIdx = 0; dwIfIdx <= pTable->dwNumEntries; dwIfIdx++)
71 PIP_ADAPTER_INFO pAdapterInfo;
72 PIP_ADAPTER_INFO pAdapter = NULL;
78 for (i = 0; i < pAddrTable->dwNumEntries; i++)
80 if (pAddrTable->table[i].dwIndex ==
81 pTable->table[dwIfIdx].dwIndex)
83 dwIP = pAddrTable->table[i].dwAddr;
90 BYTE bPhysAddr[MAXLEN_PHYSADDR];
91 char *pszIfName = NULL;
92 char dst[INET_ADDRSTRLEN];
93 struct sockaddr_in sa;
95 /* Get friendly interface name */
97 (IP_ADAPTER_INFO *) malloc (sizeof (IP_ADAPTER_INFO));
98 ULONG ulOutBufLen = sizeof (IP_ADAPTER_INFO);
100 /* Make an initial call to GetAdaptersInfo to get
101 * the necessary size into the ulOutBufLen variable */
102 if (GGetAdaptersInfo (pAdapterInfo, &ulOutBufLen) ==
103 ERROR_BUFFER_OVERFLOW)
106 pAdapterInfo = (IP_ADAPTER_INFO *) malloc (ulOutBufLen);
110 GGetAdaptersInfo (pAdapterInfo, &ulOutBufLen)) == NO_ERROR)
112 pAdapter = pAdapterInfo;
115 if (pTable->table[dwIfIdx].dwIndex == pAdapter->Index)
121 "SYSTEM\\CurrentControlSet\\Control\\Network\\"
122 "{4D36E972-E325-11CE-BFC1-08002BE10318}\\%s\\Connection",
123 pAdapter->AdapterName);
124 pszIfName = (char *) malloc (251);
126 (HKEY_LOCAL_MACHINE, szKey, "Name", pszIfName,
127 &lLen) != ERROR_SUCCESS)
133 pAdapter = pAdapter->Next;
139 memset (bPhysAddr, 0, MAXLEN_PHYSADDR);
140 memcpy (bPhysAddr, pTable->table[dwIfIdx].bPhysAddr,
141 pTable->table[dwIfIdx].dwPhysAddrLen);
143 snprintf (szEntry, 1000, "%s (%s - %I64u)",
144 pszIfName ? pszIfName : (char *)
145 pTable->table[dwIfIdx].bDescr, inet_ntop (AF_INET,
148 *((unsigned long long *) bPhysAddr));
154 sa.sin_family = AF_INET;
155 #if HAVE_SOCKADDR_IN_SIN_LEN
156 sa.sin_len = (u_char) sizeof (struct sockaddr_in);
158 sa.sin_addr.S_un.S_addr = dwIP;
161 proc (proc_cls, szEntry,
162 pTable->table[dwIfIdx].dwIndex == dwExternalNIC,
163 (const struct sockaddr *) &sa, sizeof (sa)))
167 GlobalFree (pAddrTable);
173 #elif HAVE_GETIFADDRS && HAVE_FREEIFADDRS
175 struct ifaddrs *ifa_first;
176 struct ifaddrs *ifa_ptr;
179 if (getifaddrs (&ifa_first) == 0)
181 for (ifa_ptr = ifa_first; ifa_ptr != NULL; ifa_ptr = ifa_ptr->ifa_next)
183 if (ifa_ptr->ifa_name != NULL && ifa_ptr->ifa_addr != NULL &&
184 (ifa_ptr->ifa_flags & IFF_UP) != 0)
186 if ((ifa_ptr->ifa_addr->sa_family != AF_INET) &&
187 (ifa_ptr->ifa_addr->sa_family != AF_INET6))
189 if (ifa_ptr->ifa_addr->sa_family == AF_INET)
190 alen = sizeof (struct sockaddr_in);
192 alen = sizeof (struct sockaddr_in6);
194 proc (proc_cls, ifa_ptr->ifa_name,
195 0 == strcmp (ifa_ptr->ifa_name,
196 GNUNET_DEFAULT_INTERFACE),
197 ifa_ptr->ifa_addr, alen))
201 freeifaddrs (ifa_first);
210 struct sockaddr_in a4;
211 struct sockaddr_in6 a6;
215 if (system ("ifconfig -a > /dev/null 2> /dev/null"))
216 if (system ("/sbin/ifconfig -a > /dev/null 2> /dev/null") == 0)
217 f = popen ("/sbin/ifconfig -a 2> /dev/null", "r");
221 f = popen ("ifconfig -a 2> /dev/null", "r");
224 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING |
225 GNUNET_ERROR_TYPE_BULK, "popen", "ifconfig");
229 have_ifc = GNUNET_NO;
231 while (NULL != fgets (line, sizeof (line), f))
233 if (strlen (line) == 0)
235 have_ifc = GNUNET_NO;
238 if (!isspace (line[0]))
241 (1 == SSCANF (line, "%11s", ifc)) ? GNUNET_YES : GNUNET_NO;
242 /* would end with ':' on OSX, fix it! */
243 if (ifc[strlen (ifc) - 1] == ':')
244 ifc[strlen (ifc) - 1] = '\0';
248 continue; /* strange input, hope for the best */
250 while (('\0' != *start) && (isspace (*start)))
253 (1 == SSCANF (start, "inet addr:%127s", addrstr)) ||
254 (1 == SSCANF (start, "inet6 addr:%127s", addrstr)) ||
256 (1 == SSCANF (start, "inet %127s", addrstr)) ||
257 (1 == SSCANF (start, "inet6 %127s", addrstr)))
260 if (1 == inet_pton (AF_INET, addrstr, &v4))
262 memset (&a4, 0, sizeof (a4));
263 a4.sin_family = AF_INET;
264 #if HAVE_SOCKADDR_IN_SIN_LEN
265 a4.sin_len = (u_char) sizeof (struct sockaddr_in);
270 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE),
271 (const struct sockaddr *) &a4, sizeof (a4)))
276 if (1 == inet_pton (AF_INET6, addrstr, &v6))
278 memset (&a6, 0, sizeof (a6));
279 a6.sin6_family = AF_INET6;
280 #if HAVE_SOCKADDR_IN_SIN_LEN
281 a6.sin6_len = (u_char) sizeof (struct sockaddr_in6);
286 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE),
287 (const struct sockaddr *) &a6, sizeof (a6)))
298 /* end of os_network.c */