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, pTable->table[dwIfIdx].bPhysAddr,
135                 pTable->table[dwIfIdx].dwPhysAddrLen);
136
137         snprintf (szEntry, 1000, "%s (%s - %I64u)",
138                   pszIfName ? pszIfName : (char *) pTable->table[dwIfIdx].
139                   bDescr, inet_ntop (AF_INET, &dwIP, dst, INET_ADDRSTRLEN),
140                   *((unsigned long long *) bPhysAddr));
141         szEntry[1000] = 0;
142
143         if (pszIfName)
144           free (pszIfName);
145
146         sa.sin_family = AF_INET;
147 #if HAVE_SOCKADDR_IN_SIN_LEN
148         sa.sin_len = (u_char) sizeof (struct sockaddr_in);
149 #endif
150         sa.sin_addr.S_un.S_addr = dwIP;
151
152         if (GNUNET_OK !=
153             proc (proc_cls, szEntry,
154                   pTable->table[dwIfIdx].dwIndex == dwExternalNIC,
155                   (const struct sockaddr *) &sa, sizeof (sa)))
156           break;
157       }
158     }
159     GlobalFree (pAddrTable);
160     GlobalFree (pTable);
161   }
162
163   return;
164
165 #elif HAVE_GETIFADDRS && HAVE_FREEIFADDRS
166
167   struct ifaddrs *ifa_first;
168   struct ifaddrs *ifa_ptr;
169   socklen_t alen;
170
171   if (getifaddrs (&ifa_first) == 0)
172   {
173     for (ifa_ptr = ifa_first; ifa_ptr != NULL; ifa_ptr = ifa_ptr->ifa_next)
174     {
175       if (ifa_ptr->ifa_name != NULL && ifa_ptr->ifa_addr != NULL &&
176           (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 !=
186             proc (proc_cls, ifa_ptr->ifa_name,
187                   0 == strcmp (ifa_ptr->ifa_name, GNUNET_DEFAULT_INTERFACE),
188                   ifa_ptr->ifa_addr, alen))
189           break;
190       }
191     }
192     freeifaddrs (ifa_first);
193   }
194 #else
195   char line[1024];
196   const char *start;
197   char ifc[12];
198   char addrstr[128];
199   FILE *f;
200   int have_ifc;
201   struct sockaddr_in a4;
202   struct sockaddr_in6 a6;
203   struct in_addr v4;
204   struct in6_addr v6;
205
206   if (system ("ifconfig -a > /dev/null 2> /dev/null"))
207     if (system ("/sbin/ifconfig -a > /dev/null 2> /dev/null") == 0)
208       f = popen ("/sbin/ifconfig -a 2> /dev/null", "r");
209     else
210       f = NULL;
211   else
212     f = popen ("ifconfig -a 2> /dev/null", "r");
213   if (!f)
214   {
215     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
216                               GNUNET_ERROR_TYPE_BULK, "popen", "ifconfig");
217     return;
218   }
219
220   have_ifc = GNUNET_NO;
221   ifc[11] = '\0';
222   while (NULL != fgets (line, sizeof (line), f))
223   {
224     if (strlen (line) == 0)
225     {
226       have_ifc = GNUNET_NO;
227       continue;
228     }
229     if (!isspace (line[0]))
230     {
231       have_ifc = (1 == SSCANF (line, "%11s", ifc)) ? GNUNET_YES : GNUNET_NO;
232       /* would end with ':' on OSX, fix it! */
233       if (ifc[strlen (ifc) - 1] == ':')
234         ifc[strlen (ifc) - 1] = '\0';
235       continue;
236     }
237     if (!have_ifc)
238       continue;                 /* strange input, hope for the best */
239     start = line;
240     while (('\0' != *start) && (isspace (*start)))
241       start++;
242     if (                        /* Linux */
243          (1 == SSCANF (start, "inet addr:%127s", addrstr)) ||
244          (1 == SSCANF (start, "inet6 addr:%127s", addrstr)) ||
245          /* Solaris, OS X */
246          (1 == SSCANF (start, "inet %127s", addrstr)) ||
247          (1 == SSCANF (start, "inet6 %127s", addrstr)))
248     {
249       /* IPv4 */
250       if (1 == inet_pton (AF_INET, addrstr, &v4))
251       {
252         memset (&a4, 0, sizeof (a4));
253         a4.sin_family = AF_INET;
254 #if HAVE_SOCKADDR_IN_SIN_LEN
255         a4.sin_len = (u_char) sizeof (struct sockaddr_in);
256 #endif
257         a4.sin_addr = v4;
258         if (GNUNET_OK !=
259             proc (proc_cls, ifc, 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE),
260                   (const struct sockaddr *) &a4, sizeof (a4)))
261           break;
262         continue;
263       }
264       /* IPv6 */
265       if (1 == inet_pton (AF_INET6, addrstr, &v6))
266       {
267         memset (&a6, 0, sizeof (a6));
268         a6.sin6_family = AF_INET6;
269 #if HAVE_SOCKADDR_IN_SIN_LEN
270         a6.sin6_len = (u_char) sizeof (struct sockaddr_in6);
271 #endif
272         a6.sin6_addr = v6;
273         if (GNUNET_OK !=
274             proc (proc_cls, ifc, 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE),
275                   (const struct sockaddr *) &a6, sizeof (a6)))
276           break;
277         continue;
278       }
279     }
280   }
281   pclose (f);
282 #endif
283 }
284
285
286 /* end of os_network.c */