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