api change for network interface iterations to support broadcast address and network...
[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 == pTable->table[dwIfIdx].dwIndex)
81         {
82           dwIP = pAddrTable->table[i].dwAddr;
83           break;
84         }
85       }
86
87       if (dwIP)
88       {
89         BYTE bPhysAddr[MAXLEN_PHYSADDR];
90         char *pszIfName = NULL;
91         char dst[INET_ADDRSTRLEN];
92         struct sockaddr_in sa;
93
94         /* Get friendly interface name */
95         pAdapterInfo = (IP_ADAPTER_INFO *) malloc (sizeof (IP_ADAPTER_INFO));
96         ULONG ulOutBufLen = sizeof (IP_ADAPTER_INFO);
97
98         /* Make an initial call to GetAdaptersInfo to get
99          * the necessary size into the ulOutBufLen variable */
100         if (GGetAdaptersInfo (pAdapterInfo, &ulOutBufLen) ==
101             ERROR_BUFFER_OVERFLOW)
102         {
103           free (pAdapterInfo);
104           pAdapterInfo = (IP_ADAPTER_INFO *) malloc (ulOutBufLen);
105         }
106
107         if ((dwRetVal =
108              GGetAdaptersInfo (pAdapterInfo, &ulOutBufLen)) == NO_ERROR)
109         {
110           pAdapter = pAdapterInfo;
111           while (pAdapter)
112           {
113             if (pTable->table[dwIfIdx].dwIndex == pAdapter->Index)
114             {
115               char szKey[251];
116               long lLen = 250;
117
118               sprintf (szKey,
119                        "SYSTEM\\CurrentControlSet\\Control\\Network\\"
120                        "{4D36E972-E325-11CE-BFC1-08002BE10318}\\%s\\Connection",
121                        pAdapter->AdapterName);
122               pszIfName = (char *) malloc (251);
123               if (QueryRegistry
124                   (HKEY_LOCAL_MACHINE, szKey, "Name", pszIfName,
125                    &lLen) != ERROR_SUCCESS)
126               {
127                 free (pszIfName);
128                 pszIfName = NULL;
129               }
130             }
131             pAdapter = pAdapter->Next;
132           }
133         }
134         free (pAdapterInfo);
135
136         /* Set entry */
137         memset (bPhysAddr, 0, MAXLEN_PHYSADDR);
138         memcpy (bPhysAddr, pTable->table[dwIfIdx].bPhysAddr,
139                 pTable->table[dwIfIdx].dwPhysAddrLen);
140
141         snprintf (szEntry, 1000, "%s (%s - %I64u)",
142                   pszIfName ? pszIfName : (char *) pTable->
143                   table[dwIfIdx].bDescr, inet_ntop (AF_INET, &dwIP, dst,
144                                                     INET_ADDRSTRLEN),
145                   *((unsigned long long *) bPhysAddr));
146         szEntry[1000] = 0;
147
148         if (pszIfName)
149           free (pszIfName);
150
151         sa.sin_family = AF_INET;
152 #if HAVE_SOCKADDR_IN_SIN_LEN
153         sa.sin_len = (u_char) sizeof (struct sockaddr_in);
154 #endif
155         sa.sin_addr.S_un.S_addr = dwIP;
156
157         if (GNUNET_OK !=
158             proc (proc_cls, szEntry,
159                   pTable->table[dwIfIdx].dwIndex == dwExternalNIC,
160                   (const struct sockaddr *) &sa,
161                   NULL,
162                   NULL,
163                   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, GNUNET_DEFAULT_INTERFACE),
196                   ifa_ptr->ifa_addr,
197                   NULL,
198                   NULL,
199                   alen))
200           break;
201       }
202     }
203     freeifaddrs (ifa_first);
204   }
205 #else
206   char line[1024];
207   const char *start;
208   char ifc[12];
209   char addrstr[128];
210   FILE *f;
211   int have_ifc;
212   struct sockaddr_in a4;
213   struct sockaddr_in6 a6;
214   struct in_addr v4;
215   struct in6_addr v6;
216
217   if (system ("ifconfig -a > /dev/null 2> /dev/null"))
218     if (system ("/sbin/ifconfig -a > /dev/null 2> /dev/null") == 0)
219       f = popen ("/sbin/ifconfig -a 2> /dev/null", "r");
220     else
221       f = NULL;
222   else
223     f = popen ("ifconfig -a 2> /dev/null", "r");
224   if (!f)
225   {
226     LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
227                        "popen", "ifconfig");
228     return;
229   }
230
231   have_ifc = GNUNET_NO;
232   ifc[11] = '\0';
233   while (NULL != fgets (line, sizeof (line), f))
234   {
235     if (strlen (line) == 0)
236     {
237       have_ifc = GNUNET_NO;
238       continue;
239     }
240     if (!isspace (line[0]))
241     {
242       have_ifc = (1 == SSCANF (line, "%11s", ifc)) ? GNUNET_YES : GNUNET_NO;
243       /* would end with ':' on OSX, fix it! */
244       if (ifc[strlen (ifc) - 1] == ':')
245         ifc[strlen (ifc) - 1] = '\0';
246       continue;
247     }
248     if (!have_ifc)
249       continue;                 /* strange input, hope for the best */
250     start = line;
251     while (('\0' != *start) && (isspace (*start)))
252       start++;
253     if (                        /* Linux */
254          (1 == SSCANF (start, "inet addr:%127s", addrstr)) ||
255          (1 == SSCANF (start, "inet6 addr:%127s", addrstr)) ||
256          /* Solaris, OS X */
257          (1 == SSCANF (start, "inet %127s", addrstr)) ||
258          (1 == SSCANF (start, "inet6 %127s", addrstr)))
259     {
260       /* IPv4 */
261       if (1 == inet_pton (AF_INET, addrstr, &v4))
262       {
263         memset (&a4, 0, sizeof (a4));
264         a4.sin_family = AF_INET;
265 #if HAVE_SOCKADDR_IN_SIN_LEN
266         a4.sin_len = (u_char) sizeof (struct sockaddr_in);
267 #endif
268         a4.sin_addr = v4;
269         if (GNUNET_OK !=
270             proc (proc_cls, ifc, 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE),
271                   (const struct sockaddr *) &a4,
272                   NULL,
273                   NULL,
274                   sizeof (a4)))
275           break;
276         continue;
277       }
278       /* IPv6 */
279       if (1 == inet_pton (AF_INET6, addrstr, &v6))
280       {
281         memset (&a6, 0, sizeof (a6));
282         a6.sin6_family = AF_INET6;
283 #if HAVE_SOCKADDR_IN_SIN_LEN
284         a6.sin6_len = (u_char) sizeof (struct sockaddr_in6);
285 #endif
286         a6.sin6_addr = v6;
287         if (GNUNET_OK !=
288             proc (proc_cls, ifc, 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE),
289                   (const struct sockaddr *) &a6,
290                   NULL,
291                   NULL,
292                   sizeof (a6)))
293           break;
294         continue;
295       }
296     }
297   }
298   pclose (f);
299 #endif
300 }
301
302
303 /* end of os_network.c */