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 * @brief Enumerate all network interfaces
36 * @param callback the callback function
39 GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc,
44 PMIB_IPADDRTABLE pAddrTable;
45 DWORD dwIfIdx, dwExternalNIC;
48 /* Determine our external NIC */
49 theIP = inet_addr ("192.0.34.166"); /* www.example.com */
50 if ((!GNGetBestInterface) ||
51 (GNGetBestInterface (theIP, &dwExternalNIC) != NO_ERROR))
57 EnumNICs (&pTable, &pAddrTable);
61 for (dwIfIdx = 0; dwIfIdx <= pTable->dwNumEntries; dwIfIdx++)
66 PIP_ADAPTER_INFO pAdapterInfo;
67 PIP_ADAPTER_INFO pAdapter = NULL;
72 for (i = 0; i < pAddrTable->dwNumEntries; i++)
74 if (pAddrTable->table[i].dwIndex ==
75 pTable->table[dwIfIdx].dwIndex)
77 dwIP = pAddrTable->table[i].dwAddr;
84 BYTE bPhysAddr[MAXLEN_PHYSADDR];
85 char *pszIfName = NULL;
86 char dst[INET_ADDRSTRLEN];
88 /* Get friendly interface name */
90 (IP_ADAPTER_INFO *) malloc (sizeof (IP_ADAPTER_INFO));
91 ULONG ulOutBufLen = sizeof (IP_ADAPTER_INFO);
93 /* Make an initial call to GetAdaptersInfo to get
94 the necessary size into the ulOutBufLen variable */
95 if (GGetAdaptersInfo (pAdapterInfo, &ulOutBufLen) ==
96 ERROR_BUFFER_OVERFLOW)
99 pAdapterInfo = (IP_ADAPTER_INFO *) malloc (ulOutBufLen);
103 GGetAdaptersInfo (pAdapterInfo, &ulOutBufLen)) == NO_ERROR)
105 pAdapter = pAdapterInfo;
108 if (pTable->table[dwIfIdx].dwIndex == pAdapter->Index)
114 "SYSTEM\\CurrentControlSet\\Control\\Network\\"
115 "{4D36E972-E325-11CE-BFC1-08002BE10318}\\%s\\Connection",
116 pAdapter->AdapterName);
117 pszIfName = (char *) malloc (251);
119 (HKEY_LOCAL_MACHINE, szKey, "Name", pszIfName,
120 &lLen) != ERROR_SUCCESS)
126 pAdapter = pAdapter->Next;
132 memset (bPhysAddr, 0, MAXLEN_PHYSADDR);
134 pTable->table[dwIfIdx].bPhysAddr,
135 pTable->table[dwIfIdx].dwPhysAddrLen);
137 snprintf (szEntry, 1000, "%s (%s - %I64u)",
138 pszIfName ? pszIfName : (char *) pTable->
139 table[dwIfIdx].bDescr, inet_ntop (AF_INET, &dwIP, dst,
141 *((unsigned long long *) bPhysAddr));
150 pAddrTable->table[dwIfIdx].dwIndex == dwExternalNIC,
151 NULL /* FIXME: pass actual IP address! */ ,
156 GlobalFree (pAddrTable);
162 #elif HAVE_GETIFADDRS && HAVE_FREEIFADDRS
164 struct ifaddrs *ifa_first;
167 if (getifaddrs (&ifa_first) == 0)
169 struct ifaddrs *ifa_ptr;
172 for (ifa_ptr = ifa_first; ifa_ptr != NULL; ifa_ptr = ifa_ptr->ifa_next)
174 if (ifa_ptr->ifa_name != NULL &&
175 ifa_ptr->ifa_addr != NULL && (ifa_ptr->ifa_flags & IFF_UP) != 0)
177 if ((ifa_ptr->ifa_addr->sa_family != AF_INET) &&
178 (ifa_ptr->ifa_addr->sa_family != AF_INET6))
180 if (ifa_ptr->ifa_addr->sa_family == AF_INET)
181 alen = sizeof (struct sockaddr_in);
183 alen = sizeof (struct sockaddr_in6);
184 if (GNUNET_OK != proc (cls,
186 0 == strcmp (ifa_ptr->ifa_name,
187 GNUNET_DEFAULT_INTERFACE),
188 ifa_ptr->ifa_addr, alen))
192 freeifaddrs (ifa_first);
201 struct sockaddr_in a4;
202 struct sockaddr_in6 a6;
206 if (system ("ifconfig -a > /dev/null 2> /dev/null"))
207 if (system ("/sbin/ifconfig -a > /dev/null 2> /dev/null") == 0)
208 f = popen ("/sbin/ifconfig -a 2> /dev/null", "r");
212 f = popen ("ifconfig -a 2> /dev/null", "r");
215 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
216 GNUNET_ERROR_TYPE_BULK, "popen", "ifconfig");
220 have_ifc = GNUNET_NO;
222 while (NULL != fgets (line, sizeof (line), f))
224 if (strlen (line) == 0)
226 have_ifc = GNUNET_NO;
229 if (!isspace (line[0]))
232 (1 == SSCANF (line, "%11s", ifc)) ? GNUNET_YES : GNUNET_NO;
233 /* would end with ':' on OSX, fix it! */
234 if (ifc[strlen (ifc) - 1] == ':')
235 ifc[strlen (ifc) - 1] = '\0';
239 continue; /* strange input, hope for the best */
241 while (('\0' != *start) && (isspace (*start)))
244 (1 == SSCANF (start, "inet addr:%127s", addrstr)) ||
245 (1 == SSCANF (start, "inet6 addr:%127s", addrstr)) ||
247 (1 == SSCANF (start, "inet %127s", addrstr)) ||
248 (1 == SSCANF (start, "inet6 %127s", addrstr)))
251 if (1 == inet_pton (AF_INET, addrstr, &v4))
253 memset (&a4, 0, sizeof (a4));
254 a4.sin_family = AF_INET;
259 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE),
260 (const struct sockaddr *) &a4, sizeof (a4)))
265 if (1 == inet_pton (AF_INET6, addrstr, &v6))
267 memset (&a6, 0, sizeof (a6));
268 a6.sin6_family = AF_INET6;
270 fprintf (stderr, "procing %s\n", addrstr);
274 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE),
275 (const struct sockaddr *) &a6, sizeof (a6)))
286 /* end of os_network.c */