indentation
[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
74       for (i = 0; i < pAddrTable->dwNumEntries; i++)
75       {
76         if (pAddrTable->table[i].dwIndex == 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 = (IP_ADAPTER_INFO *) malloc (sizeof (IP_ADAPTER_INFO));
92         ULONG ulOutBufLen = sizeof (IP_ADAPTER_INFO);
93
94         /* Make an initial call to GetAdaptersInfo to get
95          * the necessary size into the ulOutBufLen variable */
96         if (GGetAdaptersInfo (pAdapterInfo, &ulOutBufLen) ==
97             ERROR_BUFFER_OVERFLOW)
98         {
99           free (pAdapterInfo);
100           pAdapterInfo = (IP_ADAPTER_INFO *) malloc (ulOutBufLen);
101         }
102
103         if ((dwRetVal =
104              GGetAdaptersInfo (pAdapterInfo, &ulOutBufLen)) == NO_ERROR)
105         {
106           pAdapter = pAdapterInfo;
107           while (pAdapter)
108           {
109             if (pTable->table[dwIfIdx].dwIndex == pAdapter->Index)
110             {
111               char szKey[251];
112               long lLen = 250;
113
114               sprintf (szKey,
115                        "SYSTEM\\CurrentControlSet\\Control\\Network\\"
116                        "{4D36E972-E325-11CE-BFC1-08002BE10318}\\%s\\Connection",
117                        pAdapter->AdapterName);
118               pszIfName = (char *) malloc (251);
119               if (QueryRegistry
120                   (HKEY_LOCAL_MACHINE, szKey, "Name", pszIfName,
121                    &lLen) != ERROR_SUCCESS)
122               {
123                 free (pszIfName);
124                 pszIfName = NULL;
125               }
126             }
127             pAdapter = pAdapter->Next;
128           }
129         }
130         free (pAdapterInfo);
131
132         /* Set entry */
133         memset (bPhysAddr, 0, MAXLEN_PHYSADDR);
134         memcpy (bPhysAddr,
135                 pTable->table[dwIfIdx].bPhysAddr,
136                 pTable->table[dwIfIdx].dwPhysAddrLen);
137
138         snprintf (szEntry, 1000, "%s (%s - %I64u)",
139                   pszIfName ? pszIfName : (char *)
140                   pTable->table[dwIfIdx].bDescr, inet_ntop (AF_INET,
141                                                             &dwIP, dst,
142                                                             INET_ADDRSTRLEN),
143                   *((unsigned long long *) bPhysAddr));
144         szEntry[1000] = 0;
145
146         if (pszIfName)
147           free (pszIfName);
148
149         sa.sin_family = AF_INET;
150 #if HAVE_SOCKADDR_IN_SIN_LEN
151         sa.sin_len = (u_char) sizeof (struct sockaddr_in);
152 #endif
153         sa.sin_addr.S_un.S_addr = dwIP;
154
155         if (GNUNET_OK !=
156             proc (proc_cls,
157                   szEntry,
158                   pTable->table[dwIfIdx].dwIndex == dwExternalNIC,
159                   (const struct sockaddr *) &sa, sizeof (sa)))
160           break;
161       }
162     }
163     GlobalFree (pAddrTable);
164     GlobalFree (pTable);
165   }
166
167   return;
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 = (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';
240       continue;
241     }
242     if (!have_ifc)
243       continue;                 /* strange input, hope for the best */
244     start = line;
245     while (('\0' != *start) && (isspace (*start)))
246       start++;
247     if (                        /* Linux */
248          (1 == SSCANF (start, "inet addr:%127s", addrstr)) ||
249          (1 == SSCANF (start, "inet6 addr:%127s", addrstr)) ||
250          /* Solaris, OS X */
251          (1 == SSCANF (start, "inet %127s", addrstr)) ||
252          (1 == SSCANF (start, "inet6 %127s", addrstr)))
253     {
254       /* IPv4 */
255       if (1 == inet_pton (AF_INET, addrstr, &v4))
256       {
257         memset (&a4, 0, sizeof (a4));
258         a4.sin_family = AF_INET;
259 #if HAVE_SOCKADDR_IN_SIN_LEN
260         a4.sin_len = (u_char) sizeof (struct sockaddr_in);
261 #endif
262         a4.sin_addr = v4;
263         if (GNUNET_OK !=
264             proc (proc_cls,
265                   ifc,
266                   0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE),
267                   (const struct sockaddr *) &a4, sizeof (a4)))
268           break;
269         continue;
270       }
271       /* IPv6 */
272       if (1 == inet_pton (AF_INET6, addrstr, &v6))
273       {
274         memset (&a6, 0, sizeof (a6));
275         a6.sin6_family = AF_INET6;
276 #if HAVE_SOCKADDR_IN_SIN_LEN
277         a6.sin6_len = (u_char) sizeof (struct sockaddr_in6);
278 #endif
279         a6.sin6_addr = v6;
280         if (GNUNET_OK !=
281             proc (proc_cls,
282                   ifc,
283                   0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE),
284                   (const struct sockaddr *) &a6, sizeof (a6)))
285           break;
286         continue;
287       }
288     }
289   }
290   pclose (f);
291 #endif
292 }
293
294
295 /* end of os_network.c */