use putenv instead of setenv for portability
[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, sizeof (sa)))
161           break;
162       }
163     }
164     GlobalFree (pAddrTable);
165     GlobalFree (pTable);
166   }
167
168   return;
169
170 #elif HAVE_GETIFADDRS && HAVE_FREEIFADDRS
171
172   struct ifaddrs *ifa_first;
173   struct ifaddrs *ifa_ptr;
174   socklen_t alen;
175
176   if (getifaddrs (&ifa_first) == 0)
177   {
178     for (ifa_ptr = ifa_first; ifa_ptr != NULL; ifa_ptr = ifa_ptr->ifa_next)
179     {
180       if (ifa_ptr->ifa_name != NULL && ifa_ptr->ifa_addr != NULL &&
181           (ifa_ptr->ifa_flags & IFF_UP) != 0)
182       {
183         if ((ifa_ptr->ifa_addr->sa_family != AF_INET) &&
184             (ifa_ptr->ifa_addr->sa_family != AF_INET6))
185           continue;
186         if (ifa_ptr->ifa_addr->sa_family == AF_INET)
187           alen = sizeof (struct sockaddr_in);
188         else
189           alen = sizeof (struct sockaddr_in6);
190         if (GNUNET_OK !=
191             proc (proc_cls, ifa_ptr->ifa_name,
192                   0 == strcmp (ifa_ptr->ifa_name, 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     LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
221                        "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, ifc, 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE),
265                   (const struct sockaddr *) &a4, sizeof (a4)))
266           break;
267         continue;
268       }
269       /* IPv6 */
270       if (1 == inet_pton (AF_INET6, addrstr, &v6))
271       {
272         memset (&a6, 0, sizeof (a6));
273         a6.sin6_family = AF_INET6;
274 #if HAVE_SOCKADDR_IN_SIN_LEN
275         a6.sin6_len = (u_char) sizeof (struct sockaddr_in6);
276 #endif
277         a6.sin6_addr = v6;
278         if (GNUNET_OK !=
279             proc (proc_cls, ifc, 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE),
280                   (const struct sockaddr *) &a6, sizeof (a6)))
281           break;
282         continue;
283       }
284     }
285   }
286   pclose (f);
287 #endif
288 }
289
290
291 /* end of os_network.c */