LRN: new code for GNUNET_OS_network_interfaces_list on W32 improving support for...
[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 != proc (proc_cls, results[i].pretty_name,
62         results[i].is_default,
63         &results[i].address,
64         results[i].flags & ENUMNICS3_MASK_OK ? &results[i].mask : NULL,
65         results[i].flags & ENUMNICS3_BCAST_OK ? &results[i].broadcast : 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,
96                   ifa_ptr->ifa_broadaddr,
97                   ifa_ptr->ifa_netmask,
98                   alen))
99           break;
100       }
101     }
102     freeifaddrs (ifa_first);
103   }
104 #else
105   char line[1024];
106   const char *start;
107   char ifc[12];
108   char addrstr[128];
109   FILE *f;
110   int have_ifc;
111   struct sockaddr_in a4;
112   struct sockaddr_in6 a6;
113   struct in_addr v4;
114   struct in6_addr v6;
115
116   if (system ("ifconfig -a > /dev/null 2> /dev/null"))
117     if (system ("/sbin/ifconfig -a > /dev/null 2> /dev/null") == 0)
118       f = popen ("/sbin/ifconfig -a 2> /dev/null", "r");
119     else
120       f = NULL;
121   else
122     f = popen ("ifconfig -a 2> /dev/null", "r");
123   if (!f)
124   {
125     LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
126                        "popen", "ifconfig");
127     return;
128   }
129
130   have_ifc = GNUNET_NO;
131   ifc[11] = '\0';
132   while (NULL != fgets (line, sizeof (line), f))
133   {
134     if (strlen (line) == 0)
135     {
136       have_ifc = GNUNET_NO;
137       continue;
138     }
139     if (!isspace (line[0]))
140     {
141       have_ifc = (1 == SSCANF (line, "%11s", ifc)) ? GNUNET_YES : GNUNET_NO;
142       /* would end with ':' on OSX, fix it! */
143       if (ifc[strlen (ifc) - 1] == ':')
144         ifc[strlen (ifc) - 1] = '\0';
145       continue;
146     }
147     if (!have_ifc)
148       continue;                 /* strange input, hope for the best */
149     start = line;
150     while (('\0' != *start) && (isspace (*start)))
151       start++;
152     if (                        /* Linux */
153          (1 == SSCANF (start, "inet addr:%127s", addrstr)) ||
154          (1 == SSCANF (start, "inet6 addr:%127s", addrstr)) ||
155          /* Solaris, OS X */
156          (1 == SSCANF (start, "inet %127s", addrstr)) ||
157          (1 == SSCANF (start, "inet6 %127s", addrstr)))
158     {
159       /* IPv4 */
160       if (1 == inet_pton (AF_INET, addrstr, &v4))
161       {
162         memset (&a4, 0, sizeof (a4));
163         a4.sin_family = AF_INET;
164 #if HAVE_SOCKADDR_IN_SIN_LEN
165         a4.sin_len = (u_char) sizeof (struct sockaddr_in);
166 #endif
167         a4.sin_addr = v4;
168         if (GNUNET_OK !=
169             proc (proc_cls, ifc, 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE),
170                   (const struct sockaddr *) &a4,
171                   /* TODO broadcast and netmask */
172                   NULL,
173                   NULL,
174                   sizeof (a4)))
175           break;
176         continue;
177       }
178       /* IPv6 */
179       if (1 == inet_pton (AF_INET6, addrstr, &v6))
180       {
181         memset (&a6, 0, sizeof (a6));
182         a6.sin6_family = AF_INET6;
183 #if HAVE_SOCKADDR_IN_SIN_LEN
184         a6.sin6_len = (u_char) sizeof (struct sockaddr_in6);
185 #endif
186         a6.sin6_addr = v6;
187         if (GNUNET_OK !=
188             proc (proc_cls, ifc, 0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE),
189                   (const struct sockaddr *) &a6,
190                   /* TODO broadcast and netmask */
191                   NULL,
192                   NULL,
193                   sizeof (a6)))
194           break;
195         continue;
196       }
197     }
198   }
199   pclose (f);
200 #endif
201 }
202
203
204 /* end of os_network.c */