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