pass interface address to enumeration callback
[oweals/gnunet.git] / src / util / os_network.c
1 /*
2      This file is part of GNUnet.
3      (C) 2004, 2005, 2006 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19
20 */
21
22 /**
23  * @file util/os_network.c
24  * @brief function to determine available network interfaces
25  * @author Nils Durner
26  * @author Heikki Lindholm
27  * @author Jake Dust
28  */
29
30 #include "platform.h"
31 #include "gnunet_common.h"
32 #include "gnunet_os_lib.h"
33
34 /**
35  * @brief Enumerate all network interfaces
36  *
37  * @param proc the callback function
38  * @param proc_cls closure for proc
39  */
40 void
41 GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc,
42                                    void *proc_cls)
43 {
44 #ifdef MINGW
45   PMIB_IFTABLE pTable;
46   PMIB_IPADDRTABLE pAddrTable;
47   DWORD dwIfIdx, dwExternalNIC;
48   IPAddr theIP;
49
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))
54     {
55       dwExternalNIC = 0;
56     }
57
58   /* Enumerate NICs */
59   EnumNICs (&pTable, &pAddrTable);
60
61   if (pTable)
62     {
63       for (dwIfIdx = 0; dwIfIdx <= pTable->dwNumEntries; dwIfIdx++)
64         {
65           char szEntry[1001];
66           DWORD dwIP = 0;
67           int iItm;
68           PIP_ADAPTER_INFO pAdapterInfo;
69           PIP_ADAPTER_INFO pAdapter = NULL;
70           DWORD dwRetVal = 0;
71
72           /* Get IP-Address */
73           int i;
74           for (i = 0; i < pAddrTable->dwNumEntries; i++)
75             {
76               if (pAddrTable->table[i].dwIndex ==
77                   pTable->table[dwIfIdx].dwIndex)
78                 {
79                   dwIP = pAddrTable->table[i].dwAddr;
80                   break;
81                 }
82             }
83
84           if (dwIP)
85             {
86               BYTE bPhysAddr[MAXLEN_PHYSADDR];
87               char *pszIfName = NULL;
88               char dst[INET_ADDRSTRLEN];
89               struct sockaddr_in sa;
90
91               /* Get friendly interface name */
92               pAdapterInfo =
93                 (IP_ADAPTER_INFO *) malloc (sizeof (IP_ADAPTER_INFO));
94               ULONG ulOutBufLen = sizeof (IP_ADAPTER_INFO);
95
96               /* Make an initial call to GetAdaptersInfo to get
97                  the necessary size into the ulOutBufLen variable */
98               if (GGetAdaptersInfo (pAdapterInfo, &ulOutBufLen) ==
99                   ERROR_BUFFER_OVERFLOW)
100                 {
101                   free (pAdapterInfo);
102                   pAdapterInfo = (IP_ADAPTER_INFO *) malloc (ulOutBufLen);
103                 }
104
105               if ((dwRetVal =
106                    GGetAdaptersInfo (pAdapterInfo, &ulOutBufLen)) == NO_ERROR)
107                 {
108                   pAdapter = pAdapterInfo;
109                   while (pAdapter)
110                     {
111                       if (pTable->table[dwIfIdx].dwIndex == pAdapter->Index)
112                         {
113                           char szKey[251];
114                           long lLen = 250;
115
116                           sprintf (szKey,
117                                    "SYSTEM\\CurrentControlSet\\Control\\Network\\"
118                                    "{4D36E972-E325-11CE-BFC1-08002BE10318}\\%s\\Connection",
119                                    pAdapter->AdapterName);
120                           pszIfName = (char *) malloc (251);
121                           if (QueryRegistry
122                               (HKEY_LOCAL_MACHINE, szKey, "Name", pszIfName,
123                                &lLen) != ERROR_SUCCESS)
124                             {
125                               free (pszIfName);
126                               pszIfName = NULL;
127                             }
128                         }
129                       pAdapter = pAdapter->Next;
130                     }
131                 }
132               free (pAdapterInfo);
133
134               /* Set entry */
135               memset (bPhysAddr, 0, MAXLEN_PHYSADDR);
136               memcpy (bPhysAddr,
137                       pTable->table[dwIfIdx].bPhysAddr,
138                       pTable->table[dwIfIdx].dwPhysAddrLen);
139
140               snprintf (szEntry, 1000, "%s (%s - %I64u)",
141                         pszIfName ? pszIfName : (char *)
142                         pTable->table[dwIfIdx].bDescr, inet_ntop (AF_INET,
143                                                                   &dwIP, dst,
144                                                                   INET_ADDRSTRLEN),
145                         *((unsigned long long *) bPhysAddr));
146               szEntry[1000] = 0;
147
148               if (pszIfName)
149                 free (pszIfName);
150
151               sa.sin_family = AF_INET;
152               sa.sin_addr.S_un.S_addr = dwIP;
153
154               if (GNUNET_OK !=
155                   proc (proc_cls,
156                         szEntry,
157                         pAddrTable->table[dwIfIdx].dwIndex == dwExternalNIC,
158                         &sa,
159                         0))
160                 break;
161             }
162         }
163       GlobalFree (pAddrTable);
164       GlobalFree (pTable);
165     }
166
167   return GNUNET_YES;
168
169 #elif HAVE_GETIFADDRS && HAVE_FREEIFADDRS
170
171   struct ifaddrs *ifa_first;
172   struct ifaddrs *ifa_ptr;
173   socklen_t alen;
174
175   if (getifaddrs (&ifa_first) == 0)
176     {
177       for (ifa_ptr = ifa_first; ifa_ptr != NULL; ifa_ptr = ifa_ptr->ifa_next)
178         {
179           if (ifa_ptr->ifa_name != NULL &&
180               ifa_ptr->ifa_addr != NULL && (ifa_ptr->ifa_flags & IFF_UP) != 0)
181             {
182               if ((ifa_ptr->ifa_addr->sa_family != AF_INET) &&
183                   (ifa_ptr->ifa_addr->sa_family != AF_INET6))
184                 continue;
185               if (ifa_ptr->ifa_addr->sa_family == AF_INET)
186                 alen = sizeof (struct sockaddr_in);
187               else
188                 alen = sizeof (struct sockaddr_in6);
189               if (GNUNET_OK != proc (proc_cls,
190                                      ifa_ptr->ifa_name,
191                                      0 == strcmp (ifa_ptr->ifa_name,
192                                                   GNUNET_DEFAULT_INTERFACE),
193                                      ifa_ptr->ifa_addr, alen))
194                 break;
195             }
196         }
197       freeifaddrs (ifa_first);
198     }
199 #else
200   char line[1024];
201   const char *start;
202   char ifc[12];
203   char addrstr[128];
204   FILE *f;
205   int have_ifc;
206   struct sockaddr_in a4;
207   struct sockaddr_in6 a6;
208   struct in_addr v4;
209   struct in6_addr v6;
210
211   if (system ("ifconfig -a > /dev/null 2> /dev/null"))
212     if (system ("/sbin/ifconfig -a > /dev/null 2> /dev/null") == 0)
213       f = popen ("/sbin/ifconfig -a 2> /dev/null", "r");
214     else
215       f = NULL;
216   else
217     f = popen ("ifconfig -a 2> /dev/null", "r");
218   if (!f)
219     {
220       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
221                                 GNUNET_ERROR_TYPE_BULK, "popen", "ifconfig");
222       return;
223     }
224
225   have_ifc = GNUNET_NO;
226   ifc[11] = '\0';
227   while (NULL != fgets (line, sizeof (line), f))
228     {
229       if (strlen (line) == 0)
230         {
231           have_ifc = GNUNET_NO;
232           continue;
233         }
234       if (!isspace (line[0]))
235         {
236           have_ifc =
237             (1 == SSCANF (line, "%11s", ifc)) ? GNUNET_YES : GNUNET_NO;
238           /* would end with ':' on OSX, fix it! */
239           if (ifc[strlen (ifc) - 1] == ':')
240             ifc[strlen (ifc) - 1] = '\0';
241           continue;
242         }
243       if (!have_ifc)
244         continue;               /* strange input, hope for the best */
245       start = line;
246       while (('\0' != *start) && (isspace (*start)))
247         start++;
248       if (                      /* Linux */
249            (1 == SSCANF (start, "inet addr:%127s", addrstr)) ||
250            (1 == SSCANF (start, "inet6 addr:%127s", addrstr)) ||
251            /* Solaris, OS X */
252            (1 == SSCANF (start, "inet %127s", addrstr)) ||
253            (1 == SSCANF (start, "inet6 %127s", addrstr)))
254         {
255           /* IPv4 */
256           if (1 == inet_pton (AF_INET, addrstr, &v4))
257             {
258               memset (&a4, 0, sizeof (a4));
259               a4.sin_family = AF_INET;
260               a4.sin_addr = v4;
261               if (GNUNET_OK !=
262                   proc (proc_cls,
263                         ifc,
264                         0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE),
265                         (const struct sockaddr *) &a4, sizeof (a4)))
266                 break;
267               continue;
268             }
269           /* IPv6 */
270           if (1 == inet_pton (AF_INET6, addrstr, &v6))
271             {
272               memset (&a6, 0, sizeof (a6));
273               a6.sin6_family = AF_INET6;
274               a6.sin6_addr = v6;
275               fprintf (stderr, "procing %s\n", addrstr);
276               if (GNUNET_OK !=
277                   proc (proc_cls,
278                         ifc,
279                         0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE),
280                         (const struct sockaddr *) &a6, sizeof (a6)))
281                 break;
282               continue;
283             }
284         }
285     }
286   pclose (f);
287 #endif
288 }
289
290
291 /* end of os_network.c */