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