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
37 * @param proc the callback function
38 * @param proc_cls closure for proc
41 GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc,
46 PMIB_IPADDRTABLE pAddrTable;
47 DWORD dwIfIdx, dwExternalNIC;
50 /* Determine our external NIC */
51 theIP = inet_addr ("192.0.34.166"); /* www.example.com */
52 if ((!GNGetBestInterface) ||
53 (GNGetBestInterface (theIP, &dwExternalNIC) != NO_ERROR))
59 EnumNICs (&pTable, &pAddrTable);
63 for (dwIfIdx = 0; dwIfIdx <= pTable->dwNumEntries; dwIfIdx++)
67 PIP_ADAPTER_INFO pAdapterInfo;
68 PIP_ADAPTER_INFO pAdapter = NULL;
73 for (i = 0; i < pAddrTable->dwNumEntries; i++)
75 if (pAddrTable->table[i].dwIndex ==
76 pTable->table[dwIfIdx].dwIndex)
78 dwIP = pAddrTable->table[i].dwAddr;
85 BYTE bPhysAddr[MAXLEN_PHYSADDR];
86 char *pszIfName = NULL;
87 char dst[INET_ADDRSTRLEN];
88 struct sockaddr_in sa;
90 /* Get friendly interface name */
92 (IP_ADAPTER_INFO *) malloc (sizeof (IP_ADAPTER_INFO));
93 ULONG ulOutBufLen = sizeof (IP_ADAPTER_INFO);
95 /* Make an initial call to GetAdaptersInfo to get
96 the necessary size into the ulOutBufLen variable */
97 if (GGetAdaptersInfo (pAdapterInfo, &ulOutBufLen) ==
98 ERROR_BUFFER_OVERFLOW)
101 pAdapterInfo = (IP_ADAPTER_INFO *) malloc (ulOutBufLen);
105 GGetAdaptersInfo (pAdapterInfo, &ulOutBufLen)) == NO_ERROR)
107 pAdapter = pAdapterInfo;
110 if (pTable->table[dwIfIdx].dwIndex == pAdapter->Index)
116 "SYSTEM\\CurrentControlSet\\Control\\Network\\"
117 "{4D36E972-E325-11CE-BFC1-08002BE10318}\\%s\\Connection",
118 pAdapter->AdapterName);
119 pszIfName = (char *) malloc (251);
121 (HKEY_LOCAL_MACHINE, szKey, "Name", pszIfName,
122 &lLen) != ERROR_SUCCESS)
128 pAdapter = pAdapter->Next;
134 memset (bPhysAddr, 0, MAXLEN_PHYSADDR);
136 pTable->table[dwIfIdx].bPhysAddr,
137 pTable->table[dwIfIdx].dwPhysAddrLen);
139 snprintf (szEntry, 1000, "%s (%s - %I64u)",
140 pszIfName ? pszIfName : (char *)
141 pTable->table[dwIfIdx].bDescr, inet_ntop (AF_INET,
144 *((unsigned long long *) bPhysAddr));
150 sa.sin_family = AF_INET;
151 sa.sin_addr.S_un.S_addr = dwIP;
156 pTable->table[dwIfIdx].dwIndex == dwExternalNIC,
157 (const struct sockaddr *) &sa,
162 GlobalFree (pAddrTable);
168 #elif HAVE_GETIFADDRS && HAVE_FREEIFADDRS
170 struct ifaddrs *ifa_first;
171 struct ifaddrs *ifa_ptr;
174 if (getifaddrs (&ifa_first) == 0)
176 for (ifa_ptr = ifa_first; ifa_ptr != NULL; ifa_ptr = ifa_ptr->ifa_next)
178 if (ifa_ptr->ifa_name != NULL &&
179 ifa_ptr->ifa_addr != NULL && (ifa_ptr->ifa_flags & IFF_UP) != 0)
181 if ((ifa_ptr->ifa_addr->sa_family != AF_INET) &&
182 (ifa_ptr->ifa_addr->sa_family != AF_INET6))
184 if (ifa_ptr->ifa_addr->sa_family == AF_INET)
185 alen = sizeof (struct sockaddr_in);
187 alen = sizeof (struct sockaddr_in6);
188 if (GNUNET_OK != proc (proc_cls,
190 0 == strcmp (ifa_ptr->ifa_name,
191 GNUNET_DEFAULT_INTERFACE),
192 ifa_ptr->ifa_addr, alen))
196 freeifaddrs (ifa_first);
205 struct sockaddr_in a4;
206 struct sockaddr_in6 a6;
210 if (system ("ifconfig -a > /dev/null 2> /dev/null"))
211 if (system ("/sbin/ifconfig -a > /dev/null 2> /dev/null") == 0)
212 f = popen ("/sbin/ifconfig -a 2> /dev/null", "r");
216 f = popen ("ifconfig -a 2> /dev/null", "r");
219 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
220 GNUNET_ERROR_TYPE_BULK, "popen", "ifconfig");
224 have_ifc = GNUNET_NO;
226 while (NULL != fgets (line, sizeof (line), f))
228 if (strlen (line) == 0)
230 have_ifc = GNUNET_NO;
233 if (!isspace (line[0]))
236 (1 == SSCANF (line, "%11s", ifc)) ? GNUNET_YES : GNUNET_NO;
237 /* would end with ':' on OSX, fix it! */
238 if (ifc[strlen (ifc) - 1] == ':')
239 ifc[strlen (ifc) - 1] = '\0';
243 continue; /* strange input, hope for the best */
245 while (('\0' != *start) && (isspace (*start)))
248 (1 == SSCANF (start, "inet addr:%127s", addrstr)) ||
249 (1 == SSCANF (start, "inet6 addr:%127s", addrstr)) ||
251 (1 == SSCANF (start, "inet %127s", addrstr)) ||
252 (1 == SSCANF (start, "inet6 %127s", addrstr)))
255 if (1 == inet_pton (AF_INET, addrstr, &v4))
257 memset (&a4, 0, sizeof (a4));
258 a4.sin_family = AF_INET;
263 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE),
264 (const struct sockaddr *) &a4, sizeof (a4)))
269 if (1 == inet_pton (AF_INET6, addrstr, &v6))
271 memset (&a6, 0, sizeof (a6));
272 a6.sin6_family = AF_INET6;
274 fprintf (stderr, "procing %s\n", addrstr);
278 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE),
279 (const struct sockaddr *) &a6, sizeof (a6)))
290 /* end of os_network.c */