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