speedup mechanism to manipulate gnunet time
[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  * @author LRN
29  */
30
31 #include "platform.h"
32 #include "gnunet_common.h"
33 #include "gnunet_os_lib.h"
34
35
36 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
37 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
38
39 /**
40  * @brief Enumerate all network interfaces
41  *
42  * @param proc the callback function
43  * @param proc_cls closure for proc
44  */
45 void
46 GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc,
47                                    void *proc_cls)
48 {
49 #ifdef MINGW
50   int r;
51   int i;
52   struct EnumNICs3_results *results = NULL;
53   int results_count;
54
55   r = EnumNICs3 (&results, &results_count);
56   if (r != GNUNET_OK)
57     return;
58
59   for (i = 0; i < results_count; i++)
60   {
61     if (GNUNET_OK !=
62         proc (proc_cls, results[i].pretty_name, results[i].is_default,
63               (const struct sockaddr *) &results[i].address,
64               results[i].
65               flags & ENUMNICS3_BCAST_OK ?
66               (const struct sockaddr *) &results[i].broadcast : NULL,
67               results[i].flags & ENUMNICS3_MASK_OK ?
68               (const struct sockaddr *) &results[i].mask : NULL,
69               results[i].addr_size))
70       break;
71   }
72   EnumNICs3_free (results);
73   return;
74
75 #elif HAVE_GETIFADDRS && HAVE_FREEIFADDRS
76
77   struct ifaddrs *ifa_first;
78   struct ifaddrs *ifa_ptr;
79   socklen_t alen;
80
81   if (getifaddrs (&ifa_first) == 0)
82   {
83     for (ifa_ptr = ifa_first; ifa_ptr != NULL; ifa_ptr = ifa_ptr->ifa_next)
84     {
85       if (ifa_ptr->ifa_name != NULL && ifa_ptr->ifa_addr != NULL &&
86           (ifa_ptr->ifa_flags & IFF_UP) != 0)
87       {
88         if ((ifa_ptr->ifa_addr->sa_family != AF_INET) &&
89             (ifa_ptr->ifa_addr->sa_family != AF_INET6))
90           continue;
91         if (ifa_ptr->ifa_addr->sa_family == AF_INET)
92           alen = sizeof (struct sockaddr_in);
93         else
94           alen = sizeof (struct sockaddr_in6);
95         if (GNUNET_OK !=
96             proc (proc_cls, ifa_ptr->ifa_name,
97                   0 == strcmp (ifa_ptr->ifa_name, GNUNET_DEFAULT_INTERFACE),
98                   ifa_ptr->ifa_addr, ifa_ptr->ifa_broadaddr,
99                   ifa_ptr->ifa_netmask, alen))
100           break;
101       }
102     }
103     freeifaddrs (ifa_first);
104   }
105 #else
106   int i;
107   char line[1024];
108   char *replace;
109   const char *start;
110   char ifc[12];
111   char addrstr[128];
112   char bcstr[128];
113   char netmaskstr[128];
114   FILE *f;
115   int have_ifc;
116   struct sockaddr_in a4;
117   struct sockaddr_in6 a6;
118   struct in_addr v4;
119   struct in6_addr v6;
120   struct sockaddr_in bcaddr;
121   struct sockaddr_in netmask;
122   struct sockaddr_in6 netmask6;
123   struct sockaddr *pass_bcaddr;
124   struct sockaddr *pass_netmask;
125   int prefixlen;
126
127   if (system ("ifconfig -a > /dev/null 2> /dev/null"))
128     if (system ("/sbin/ifconfig -a > /dev/null 2> /dev/null") == 0)
129       f = popen ("/sbin/ifconfig -a 2> /dev/null", "r");
130     else
131       f = NULL;
132   else
133     f = popen ("ifconfig -a 2> /dev/null", "r");
134   if (!f)
135   {
136     LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
137                        "popen", "ifconfig");
138     return;
139   }
140
141   have_ifc = GNUNET_NO;
142   ifc[11] = '\0';
143   while (NULL != fgets (line, sizeof (line), f))
144   {
145     if (strlen (line) == 0)
146     {
147       have_ifc = GNUNET_NO;
148       continue;
149     }
150     if (!isspace (line[0]))
151     {
152       have_ifc = (1 == SSCANF (line, "%11s", ifc)) ? GNUNET_YES : GNUNET_NO;
153       /* would end with ':' on OSX, fix it! */
154       if (ifc[strlen (ifc) - 1] == ':')
155         ifc[strlen (ifc) - 1] = '\0';
156       continue;
157     }
158     if (!have_ifc)
159       continue;                 /* strange input, hope for the best */
160
161     /* make parsing of ipv6 addresses easier */
162     for (replace = line; *replace != '\0'; replace++)
163     {
164       if (*replace == '/')
165         *replace = ' ';
166     }
167     prefixlen = -1;
168
169     start = line;
170     while (('\0' != *start) && (isspace (*start)))
171       start++;
172
173     /* Zero-out stack allocated values */
174     memset (addrstr, 0, 128);
175     memset (netmaskstr, 0, 128);
176     memset (bcstr, 0, 128);
177     prefixlen = 0;
178
179     if ( /* Linux */
180          (3 == SSCANF (start, "inet addr:%127s Bcast:%127s Mask:%127s", addrstr, bcstr, netmaskstr)) ||
181          (2 == SSCANF (start, "inet addr:%127s Mask:%127s", addrstr, netmaskstr)) ||
182          (2 == SSCANF (start, "inet6 addr:%127s %d", addrstr, &prefixlen)) ||
183          /* Solaris, OS X */
184          (1 == SSCANF (start, "inet %127s", addrstr)) ||
185          (1 == SSCANF (start, "inet6 %127s", addrstr)))
186     {
187       /* IPv4 */
188       if (1 == inet_pton (AF_INET, addrstr, &v4))
189       {
190         memset (&a4, 0, sizeof (a4));
191         a4.sin_family = AF_INET;
192 #if HAVE_SOCKADDR_IN_SIN_LEN
193         a4.sin_len = (u_char) sizeof (struct sockaddr_in);
194 #endif
195         a4.sin_addr = v4;
196
197         pass_bcaddr = NULL;
198         pass_netmask = NULL;
199         if (1 == inet_pton (AF_INET, bcstr, &v4))
200         {
201           memset (&bcaddr, 0, sizeof (bcaddr));
202           bcaddr.sin_family = AF_INET;
203 #if HAVE_SOCKADDR_IN_SIN_LEN
204           bcaddr.sin_len = (u_char) sizeof (struct sockaddr_in);
205 #endif
206           bcaddr.sin_addr = v4;
207           pass_bcaddr = (struct sockaddr *) &bcaddr;
208         }
209         if (1 == inet_pton (AF_INET, netmaskstr, &v4))
210         {
211           memset (&netmask, 0, sizeof (netmask));
212           netmask.sin_family = AF_INET;
213 #if HAVE_SOCKADDR_IN_SIN_LEN
214           netmask.sin_len = (u_char) sizeof (struct sockaddr_in);
215 #endif
216           netmask.sin_addr = v4;
217           pass_netmask = (struct sockaddr *) &netmask;
218         }
219
220
221         if (GNUNET_OK !=
222             proc (proc_cls, ifc, 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE),
223                   (const struct sockaddr *) &a4,
224                   pass_bcaddr, pass_netmask, sizeof (a4)))
225           break;
226         continue;
227       }
228       /* IPv6 */
229       if (1 == inet_pton (AF_INET6, addrstr, &v6))
230       {
231         memset (&a6, 0, sizeof (a6));
232         a6.sin6_family = AF_INET6;
233 #if HAVE_SOCKADDR_IN_SIN_LEN
234         a6.sin6_len = (u_char) sizeof (struct sockaddr_in6);
235 #endif
236         a6.sin6_addr = v6;
237
238         pass_netmask = NULL;
239         if (prefixlen != -1)
240         {
241           memset (v6.s6_addr, 0, sizeof (v6.s6_addr));
242           for (i = 0; i < prefixlen; i++)
243           {
244             v6.s6_addr[i >> 3] |= 1 << (i & 7);
245           }
246           memset (&netmask6, 0, sizeof (netmask6));
247           netmask6.sin6_family = AF_INET6;
248 #if HAVE_SOCKADDR_IN_SIN_LEN
249           netmask6.sin6_len = (u_char) sizeof (struct sockaddr_in6);
250 #endif
251           netmask6.sin6_addr = v6;
252
253           pass_netmask = (struct sockaddr *) &netmask6;
254         }
255         
256         if (GNUNET_OK !=
257             proc (proc_cls, ifc, 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE),
258                   (const struct sockaddr *) &a6,
259                   NULL, pass_netmask, sizeof (a6)))
260           break;
261         continue;
262       }
263     }
264   }
265   pclose (f);
266 #endif
267 }
268
269
270 /* end of os_network.c */