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