aboutsummaryrefslogtreecommitdiff
path: root/src/util/os_network.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2019-10-05 15:09:28 +0200
committerChristian Grothoff <christian@grothoff.org>2019-10-05 15:09:28 +0200
commitc4e9ba925ffd758aaa3feee2ccfc0b76f26fe207 (patch)
treecac3ce030d77b4cbe7c7dc62ed58cfe6d24f73e1 /src/util/os_network.c
parentfbb71d527c7d6babf269a8fefce1db291b9f7068 (diff)
downloadgnunet-c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207.tar.gz
gnunet-c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207.zip
global reindent, now with uncrustify hook enabled
Diffstat (limited to 'src/util/os_network.c')
-rw-r--r--src/util/os_network.c544
1 files changed, 275 insertions, 269 deletions
diff --git a/src/util/os_network.c b/src/util/os_network.c
index 526678047..e310455e0 100644
--- a/src/util/os_network.c
+++ b/src/util/os_network.c
@@ -31,11 +31,15 @@
31#include "gnunet_util_lib.h" 31#include "gnunet_util_lib.h"
32 32
33 33
34#define LOG(kind, ...) GNUNET_log_from(kind, "util-os-network", __VA_ARGS__) 34#define LOG(kind, ...) GNUNET_log_from (kind, "util-os-network", __VA_ARGS__)
35#define LOG_STRERROR_FILE(kind, syscall, filename) GNUNET_log_from_strerror_file(kind, "util-os-network", syscall, filename) 35#define LOG_STRERROR_FILE(kind, syscall, \
36 filename) GNUNET_log_from_strerror_file (kind, \
37 "util-os-network", \
38 syscall, \
39 filename)
36 40
37 41
38#if !(HAVE_GETIFADDRS && HAVE_FREEIFADDRS) 42#if ! (HAVE_GETIFADDRS && HAVE_FREEIFADDRS)
39/** 43/**
40 * Try to enumerate all network interfaces using 'ifconfig'. 44 * Try to enumerate all network interfaces using 'ifconfig'.
41 * 45 *
@@ -44,8 +48,8 @@
44 * @return #GNUNET_OK if it worked 48 * @return #GNUNET_OK if it worked
45 */ 49 */
46static int 50static int
47try_ifconfig(GNUNET_OS_NetworkInterfaceProcessor proc, 51try_ifconfig (GNUNET_OS_NetworkInterfaceProcessor proc,
48 void *proc_cls) 52 void *proc_cls)
49{ 53{
50 int i; 54 int i;
51 char line[1024]; 55 char line[1024];
@@ -70,159 +74,161 @@ try_ifconfig(GNUNET_OS_NetworkInterfaceProcessor proc,
70 static char *pcall; 74 static char *pcall;
71 75
72 if (NULL == pcall) 76 if (NULL == pcall)
73 { 77 {
74 const char *sbin_ifconfig; 78 const char *sbin_ifconfig;
75 79
76#ifdef IFCONFIG 80#ifdef IFCONFIG
77 if (0 == access(IFCONFIG, X_OK)) 81 if (0 == access (IFCONFIG, X_OK))
78 sbin_ifconfig = IFCONFIG; 82 sbin_ifconfig = IFCONFIG;
79 else 83 else
80#endif 84#endif
81 if (0 == access("/sbin/ifconfig", X_OK)) 85 if (0 == access ("/sbin/ifconfig", X_OK))
82 sbin_ifconfig = "/sbin/ifconfig"; 86 sbin_ifconfig = "/sbin/ifconfig";
83 else if (0 == access("/usr/sbin/ifconfig", X_OK)) 87 else if (0 == access ("/usr/sbin/ifconfig", X_OK))
84 sbin_ifconfig = "/usr/sbin/ifconfig"; 88 sbin_ifconfig = "/usr/sbin/ifconfig";
85 else 89 else
86 sbin_ifconfig = "ifconfig"; 90 sbin_ifconfig = "ifconfig";
87 GNUNET_asprintf(&pcall, 91 GNUNET_asprintf (&pcall,
88 "%s -a 2> /dev/null", 92 "%s -a 2> /dev/null",
89 sbin_ifconfig); 93 sbin_ifconfig);
90 } 94 }
91 f = popen(pcall, "r"); 95 f = popen (pcall, "r");
92 if (NULL == f) 96 if (NULL == f)
93 { 97 {
94 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, 98 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
95 "popen", 99 "popen",
96 "ifconfig"); 100 "ifconfig");
97 101
98 return GNUNET_SYSERR; 102 return GNUNET_SYSERR;
99 } 103 }
100 104
101 have_ifc = GNUNET_NO; 105 have_ifc = GNUNET_NO;
102 ifc[11] = '\0'; 106 ifc[11] = '\0';
103 while (NULL != fgets(line, sizeof(line), f)) 107 while (NULL != fgets (line, sizeof(line), f))
108 {
109 if (strlen (line) == 0)
104 { 110 {
105 if (strlen(line) == 0) 111 have_ifc = GNUNET_NO;
106 { 112 continue;
107 have_ifc = GNUNET_NO; 113 }
108 continue; 114 if (! isspace (line[0]))
109 } 115 {
110 if (!isspace(line[0])) 116 have_ifc = (1 == sscanf (line, "%11s", ifc)) ? GNUNET_YES : GNUNET_NO;
111 { 117 /* would end with ':' on OSX, fix it! */
112 have_ifc = (1 == sscanf(line, "%11s", ifc)) ? GNUNET_YES : GNUNET_NO; 118 if (ifc[strlen (ifc) - 1] == ':')
113 /* would end with ':' on OSX, fix it! */ 119 ifc[strlen (ifc) - 1] = '\0';
114 if (ifc[strlen(ifc) - 1] == ':') 120 continue;
115 ifc[strlen(ifc) - 1] = '\0'; 121 }
116 continue; 122 if (! have_ifc)
117 } 123 continue; /* strange input, hope for the best */
118 if (!have_ifc)
119 continue; /* strange input, hope for the best */
120 124
121 /* make parsing of ipv6 addresses easier */ 125 /* make parsing of ipv6 addresses easier */
122 for (replace = line; *replace != '\0'; replace++) 126 for (replace = line; *replace != '\0'; replace++)
123 { 127 {
124 if (*replace == '/') 128 if (*replace == '/')
125 *replace = ' '; 129 *replace = ' ';
126 } 130 }
127 prefixlen = -1; 131 prefixlen = -1;
128 132
129 start = line; 133 start = line;
130 while (('\0' != *start) && (isspace(*start))) 134 while (('\0' != *start) && (isspace (*start)))
131 start++; 135 start++;
132 136
133 /* Zero-out stack allocated values */ 137 /* Zero-out stack allocated values */
134 memset(addrstr, 0, 128); 138 memset (addrstr, 0, 128);
135 memset(netmaskstr, 0, 128); 139 memset (netmaskstr, 0, 128);
136 memset(bcstr, 0, 128); 140 memset (bcstr, 0, 128);
137 prefixlen = 0; 141 prefixlen = 0;
138 142
139 if ( /* Linux */ 143 if ( /* Linux */
140 (3 == sscanf(start, "inet addr:%127s Bcast:%127s Mask:%127s", addrstr, bcstr, netmaskstr)) || 144 (3 == sscanf (start, "inet addr:%127s Bcast:%127s Mask:%127s", addrstr,
141 (2 == sscanf(start, "inet addr:%127s Mask:%127s", addrstr, netmaskstr)) || 145 bcstr, netmaskstr)) ||
142 (2 == sscanf(start, "inet6 addr:%127s %d", addrstr, &prefixlen)) || 146 (2 == sscanf (start, "inet addr:%127s Mask:%127s", addrstr,
143 /* Solaris, OS X */ 147 netmaskstr)) ||
144 (1 == sscanf(start, "inet %127s", addrstr)) || 148 (2 == sscanf (start, "inet6 addr:%127s %d", addrstr, &prefixlen)) ||
145 (1 == sscanf(start, "inet6 %127s", addrstr))) 149 /* Solaris, OS X */
146 { 150 (1 == sscanf (start, "inet %127s", addrstr)) ||
147 /* IPv4 */ 151 (1 == sscanf (start, "inet6 %127s", addrstr)))
148 if (1 == inet_pton(AF_INET, addrstr, &v4)) 152 {
149 { 153 /* IPv4 */
150 memset(&a4, 0, sizeof(a4)); 154 if (1 == inet_pton (AF_INET, addrstr, &v4))
151 a4.sin_family = AF_INET; 155 {
156 memset (&a4, 0, sizeof(a4));
157 a4.sin_family = AF_INET;
152#if HAVE_SOCKADDR_IN_SIN_LEN 158#if HAVE_SOCKADDR_IN_SIN_LEN
153 a4.sin_len = (u_char)sizeof(struct sockaddr_in); 159 a4.sin_len = (u_char) sizeof(struct sockaddr_in);
154#endif 160#endif
155 a4.sin_addr = v4; 161 a4.sin_addr = v4;
156 162
157 pass_bcaddr = NULL; 163 pass_bcaddr = NULL;
158 pass_netmask = NULL; 164 pass_netmask = NULL;
159 if (1 == inet_pton(AF_INET, bcstr, &v4)) 165 if (1 == inet_pton (AF_INET, bcstr, &v4))
160 { 166 {
161 memset(&bcaddr, 0, sizeof(bcaddr)); 167 memset (&bcaddr, 0, sizeof(bcaddr));
162 bcaddr.sin_family = AF_INET; 168 bcaddr.sin_family = AF_INET;
163#if HAVE_SOCKADDR_IN_SIN_LEN 169#if HAVE_SOCKADDR_IN_SIN_LEN
164 bcaddr.sin_len = (u_char)sizeof(struct sockaddr_in); 170 bcaddr.sin_len = (u_char) sizeof(struct sockaddr_in);
165#endif 171#endif
166 bcaddr.sin_addr = v4; 172 bcaddr.sin_addr = v4;
167 pass_bcaddr = (struct sockaddr *)&bcaddr; 173 pass_bcaddr = (struct sockaddr *) &bcaddr;
168 } 174 }
169 if (1 == inet_pton(AF_INET, netmaskstr, &v4)) 175 if (1 == inet_pton (AF_INET, netmaskstr, &v4))
170 { 176 {
171 memset(&netmask, 0, sizeof(netmask)); 177 memset (&netmask, 0, sizeof(netmask));
172 netmask.sin_family = AF_INET; 178 netmask.sin_family = AF_INET;
173#if HAVE_SOCKADDR_IN_SIN_LEN 179#if HAVE_SOCKADDR_IN_SIN_LEN
174 netmask.sin_len = (u_char)sizeof(struct sockaddr_in); 180 netmask.sin_len = (u_char) sizeof(struct sockaddr_in);
175#endif 181#endif
176 netmask.sin_addr = v4; 182 netmask.sin_addr = v4;
177 pass_netmask = (struct sockaddr *)&netmask; 183 pass_netmask = (struct sockaddr *) &netmask;
178 } 184 }
179 185
180 186
181 if (GNUNET_OK != 187 if (GNUNET_OK !=
182 proc(proc_cls, ifc, 0 == strcmp(ifc, GNUNET_DEFAULT_INTERFACE), 188 proc (proc_cls, ifc,(0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE)),
183 (const struct sockaddr *)&a4, 189 (const struct sockaddr *) &a4,
184 pass_bcaddr, pass_netmask, sizeof(a4))) 190 pass_bcaddr, pass_netmask, sizeof(a4)))
185 break; 191 break;
186 continue; 192 continue;
187 } 193 }
188 /* IPv6 */ 194 /* IPv6 */
189 if (1 == inet_pton(AF_INET6, addrstr, &v6)) 195 if (1 == inet_pton (AF_INET6, addrstr, &v6))
190 { 196 {
191 memset(&a6, 0, sizeof(a6)); 197 memset (&a6, 0, sizeof(a6));
192 a6.sin6_family = AF_INET6; 198 a6.sin6_family = AF_INET6;
193#if HAVE_SOCKADDR_IN_SIN_LEN 199#if HAVE_SOCKADDR_IN_SIN_LEN
194 a6.sin6_len = (u_char)sizeof(struct sockaddr_in6); 200 a6.sin6_len = (u_char) sizeof(struct sockaddr_in6);
195#endif 201#endif
196 a6.sin6_addr = v6; 202 a6.sin6_addr = v6;
197 203
198 pass_netmask = NULL; 204 pass_netmask = NULL;
199 if (prefixlen != -1) 205 if (prefixlen != -1)
200 { 206 {
201 memset(v6.s6_addr, 0, sizeof(v6.s6_addr)); 207 memset (v6.s6_addr, 0, sizeof(v6.s6_addr));
202 for (i = 0; i < prefixlen; i++) 208 for (i = 0; i < prefixlen; i++)
203 { 209 {
204 v6.s6_addr[i >> 3] |= 1 << (i & 7); 210 v6.s6_addr[i >> 3] |= 1 << (i & 7);
205 } 211 }
206 memset(&netmask6, 0, sizeof(netmask6)); 212 memset (&netmask6, 0, sizeof(netmask6));
207 netmask6.sin6_family = AF_INET6; 213 netmask6.sin6_family = AF_INET6;
208#if HAVE_SOCKADDR_IN_SIN_LEN 214#if HAVE_SOCKADDR_IN_SIN_LEN
209 netmask6.sin6_len = (u_char)sizeof(struct sockaddr_in6); 215 netmask6.sin6_len = (u_char) sizeof(struct sockaddr_in6);
210#endif 216#endif
211 netmask6.sin6_addr = v6; 217 netmask6.sin6_addr = v6;
212 218
213 pass_netmask = (struct sockaddr *)&netmask6; 219 pass_netmask = (struct sockaddr *) &netmask6;
214 }
215
216 if (GNUNET_OK !=
217 proc(proc_cls, ifc, 0 == strcmp(ifc, GNUNET_DEFAULT_INTERFACE),
218 (const struct sockaddr *)&a6,
219 NULL, pass_netmask, sizeof(a6)))
220 break;
221 continue;
222 }
223 } 220 }
221
222 if (GNUNET_OK !=
223 proc (proc_cls, ifc,(0 == strcmp (ifc, GNUNET_DEFAULT_INTERFACE)),
224 (const struct sockaddr *) &a6,
225 NULL, pass_netmask, sizeof(a6)))
226 break;
227 continue;
228 }
224 } 229 }
225 pclose(f); 230 }
231 pclose (f);
226 return GNUNET_OK; 232 return GNUNET_OK;
227} 233}
228 234
@@ -235,8 +241,8 @@ try_ifconfig(GNUNET_OS_NetworkInterfaceProcessor proc,
235 * @return #GNUNET_OK if it worked 241 * @return #GNUNET_OK if it worked
236 */ 242 */
237static int 243static int
238try_ip(GNUNET_OS_NetworkInterfaceProcessor proc, 244try_ip (GNUNET_OS_NetworkInterfaceProcessor proc,
239 void *proc_cls) 245 void *proc_cls)
240{ 246{
241 char line[1024]; 247 char line[1024];
242 char *replace; 248 char *replace;
@@ -255,124 +261,124 @@ try_ip(GNUNET_OS_NetworkInterfaceProcessor proc,
255 static char *pcall; 261 static char *pcall;
256 262
257 if (NULL == pcall) 263 if (NULL == pcall)
258 { 264 {
259 const char *sbin_ip; 265 const char *sbin_ip;
260 266
261#ifdef IFCONFIG 267#ifdef IFCONFIG
262 if (0 == access(PATH_TO_IP, X_OK)) 268 if (0 == access (PATH_TO_IP, X_OK))
263 sbin_ip = PATH_TO_IP; 269 sbin_ip = PATH_TO_IP;
264 else 270 else
265#endif 271#endif
266 if (0 == access("/sbin/ip", X_OK)) 272 if (0 == access ("/sbin/ip", X_OK))
267 sbin_ip = "/sbin/ip"; 273 sbin_ip = "/sbin/ip";
268 else if (0 == access("/usr/sbin/ip", X_OK)) 274 else if (0 == access ("/usr/sbin/ip", X_OK))
269 sbin_ip = "/usr/sbin/ip"; 275 sbin_ip = "/usr/sbin/ip";
270 else 276 else
271 sbin_ip = "if"; 277 sbin_ip = "if";
272 GNUNET_asprintf(&pcall, 278 GNUNET_asprintf (&pcall,
273 "%s -o add 2> /dev/null", 279 "%s -o add 2> /dev/null",
274 sbin_ip); 280 sbin_ip);
275 } 281 }
276 f = popen(pcall, "r"); 282 f = popen (pcall, "r");
277 if (!f) 283 if (! f)
284 {
285 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
286 "popen",
287 "ip");
288 return GNUNET_SYSERR;
289 }
290
291 while (NULL != fgets (line, sizeof(line), f))
292 {
293 /* make parsing easier */
294 for (replace = line; *replace != '\0'; replace++)
278 { 295 {
279 LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, 296 if (*replace == '/')
280 "popen", 297 *replace = ' ';
281 "ip");
282 return GNUNET_SYSERR;
283 } 298 }
284 299 /* Zero-out stack allocated values */
285 while (NULL != fgets(line, sizeof(line), f)) 300 memset (ifname, 0, 64);
301 memset (afstr, 0, 6);
302 memset (addrstr, 0, 128);
303 if (4 != sscanf (line,
304 "%*u: %63s %5s %127s %6u",
305 ifname,
306 afstr,
307 addrstr,
308 &prefixlen))
309 continue;
310 /* IPv4 */
311 if ((0 == strcasecmp ("inet",
312 afstr)) &&
313 (1 == inet_pton (AF_INET,
314 addrstr,
315 &v4)))
286 { 316 {
287 /* make parsing easier */ 317 memset (&a4, 0, sizeof(a4));
288 for (replace = line; *replace != '\0'; replace++) 318 a4.sin_family = AF_INET;
289 {
290 if (*replace == '/')
291 *replace = ' ';
292 }
293 /* Zero-out stack allocated values */
294 memset(ifname, 0, 64);
295 memset(afstr, 0, 6);
296 memset(addrstr, 0, 128);
297 if (4 != sscanf(line,
298 "%*u: %63s %5s %127s %6u",
299 ifname,
300 afstr,
301 addrstr,
302 &prefixlen))
303 continue;
304 /* IPv4 */
305 if ((0 == strcasecmp("inet",
306 afstr)) &&
307 (1 == inet_pton(AF_INET,
308 addrstr,
309 &v4)))
310 {
311 memset(&a4, 0, sizeof(a4));
312 a4.sin_family = AF_INET;
313#if HAVE_SOCKADDR_IN_SIN_LEN 319#if HAVE_SOCKADDR_IN_SIN_LEN
314 a4.sin_len = (u_char)sizeof(struct sockaddr_in); 320 a4.sin_len = (u_char) sizeof(struct sockaddr_in);
315#endif 321#endif
316 a4.sin_addr = v4; 322 a4.sin_addr = v4;
317 323
318 memset(&v4.s_addr, 0, sizeof(v4.s_addr)); 324 memset (&v4.s_addr, 0, sizeof(v4.s_addr));
319 for (i = 0; i < prefixlen; i++) 325 for (i = 0; i < prefixlen; i++)
320 v4.s_addr |= 1 << (i & 7); 326 v4.s_addr |= 1 << (i & 7);
321 memset(&netmask, 0, sizeof(netmask)); 327 memset (&netmask, 0, sizeof(netmask));
322 netmask.sin_family = AF_INET; 328 netmask.sin_family = AF_INET;
323#if HAVE_SOCKADDR_IN_SIN_LEN 329#if HAVE_SOCKADDR_IN_SIN_LEN
324 netmask.sin_len = (u_char)sizeof(struct sockaddr_in); 330 netmask.sin_len = (u_char) sizeof(struct sockaddr_in);
325#endif 331#endif
326 netmask.sin_addr = v4; 332 netmask.sin_addr = v4;
327 333
328 if (GNUNET_OK != 334 if (GNUNET_OK !=
329 proc(proc_cls, 335 proc (proc_cls,
330 ifname, 336 ifname,
331 (0 == strcmp(ifname, 337 (0 == strcmp (ifname,
332 GNUNET_DEFAULT_INTERFACE)), 338 GNUNET_DEFAULT_INTERFACE)),
333 (const struct sockaddr *)&a4, 339 (const struct sockaddr *) &a4,
334 NULL, 340 NULL,
335 (const struct sockaddr *)&netmask, 341 (const struct sockaddr *) &netmask,
336 sizeof(a4))) 342 sizeof(a4)))
337 break; 343 break;
338 } 344 }
339 /* IPv6 */ 345 /* IPv6 */
340 if ((0 == strcasecmp("inet6", 346 if ((0 == strcasecmp ("inet6",
341 afstr)) && 347 afstr)) &&
342 (1 == inet_pton(AF_INET6, 348 (1 == inet_pton (AF_INET6,
343 addrstr, 349 addrstr,
344 &v6))) 350 &v6)))
345 { 351 {
346 memset(&a6, 0, sizeof(a6)); 352 memset (&a6, 0, sizeof(a6));
347 a6.sin6_family = AF_INET6; 353 a6.sin6_family = AF_INET6;
348#if HAVE_SOCKADDR_IN_SIN_LEN 354#if HAVE_SOCKADDR_IN_SIN_LEN
349 a6.sin6_len = (u_char)sizeof(struct sockaddr_in6); 355 a6.sin6_len = (u_char) sizeof(struct sockaddr_in6);
350#endif 356#endif
351 a6.sin6_addr = v6; 357 a6.sin6_addr = v6;
352 358
353 memset(v6.s6_addr, 0, sizeof(v6.s6_addr)); 359 memset (v6.s6_addr, 0, sizeof(v6.s6_addr));
354 for (i = 0; i < prefixlen; i++) 360 for (i = 0; i < prefixlen; i++)
355 v6.s6_addr[i >> 3] |= 1 << (i & 7); 361 v6.s6_addr[i >> 3] |= 1 << (i & 7);
356 memset(&netmask6, 0, sizeof(netmask6)); 362 memset (&netmask6, 0, sizeof(netmask6));
357 netmask6.sin6_family = AF_INET6; 363 netmask6.sin6_family = AF_INET6;
358#if HAVE_SOCKADDR_IN_SIN_LEN 364#if HAVE_SOCKADDR_IN_SIN_LEN
359 netmask6.sin6_len = (u_char)sizeof(struct sockaddr_in6); 365 netmask6.sin6_len = (u_char) sizeof(struct sockaddr_in6);
360#endif 366#endif
361 netmask6.sin6_addr = v6; 367 netmask6.sin6_addr = v6;
362 368
363 if (GNUNET_OK != 369 if (GNUNET_OK !=
364 proc(proc_cls, 370 proc (proc_cls,
365 ifname, 371 ifname,
366 (0 == strcmp(ifname, 372 (0 == strcmp (ifname,
367 GNUNET_DEFAULT_INTERFACE)), 373 GNUNET_DEFAULT_INTERFACE)),
368 (const struct sockaddr *)&a6, 374 (const struct sockaddr *) &a6,
369 NULL, 375 NULL,
370 (const struct sockaddr *)&netmask6, 376 (const struct sockaddr *) &netmask6,
371 sizeof(a6))) 377 sizeof(a6)))
372 break; 378 break;
373 }
374 } 379 }
375 pclose(f); 380 }
381 pclose (f);
376 return GNUNET_OK; 382 return GNUNET_OK;
377} 383}
378#endif 384#endif
@@ -385,49 +391,49 @@ try_ip(GNUNET_OS_NetworkInterfaceProcessor proc,
385 * @param proc_cls closure for @a proc 391 * @param proc_cls closure for @a proc
386 */ 392 */
387void 393void
388GNUNET_OS_network_interfaces_list(GNUNET_OS_NetworkInterfaceProcessor proc, 394GNUNET_OS_network_interfaces_list (GNUNET_OS_NetworkInterfaceProcessor proc,
389 void *proc_cls) 395 void *proc_cls)
390{ 396{
391#if HAVE_GETIFADDRS && HAVE_FREEIFADDRS 397#if HAVE_GETIFADDRS && HAVE_FREEIFADDRS
392 struct ifaddrs *ifa_first; 398 struct ifaddrs *ifa_first;
393 struct ifaddrs *ifa_ptr; 399 struct ifaddrs *ifa_ptr;
394 socklen_t alen; 400 socklen_t alen;
395 401
396 if (getifaddrs(&ifa_first) == 0) 402 if (getifaddrs (&ifa_first) == 0)
403 {
404 for (ifa_ptr = ifa_first; ifa_ptr != NULL; ifa_ptr = ifa_ptr->ifa_next)
397 { 405 {
398 for (ifa_ptr = ifa_first; ifa_ptr != NULL; ifa_ptr = ifa_ptr->ifa_next) 406 if ((ifa_ptr->ifa_name != NULL) &&(ifa_ptr->ifa_addr != NULL) &&
399 { 407 ( (ifa_ptr->ifa_flags & IFF_UP) != 0) )
400 if (ifa_ptr->ifa_name != NULL && ifa_ptr->ifa_addr != NULL && 408 {
401 (ifa_ptr->ifa_flags & IFF_UP) != 0) 409 if ((ifa_ptr->ifa_addr->sa_family != AF_INET) &&
402 { 410 (ifa_ptr->ifa_addr->sa_family != AF_INET6))
403 if ((ifa_ptr->ifa_addr->sa_family != AF_INET) && 411 continue;
404 (ifa_ptr->ifa_addr->sa_family != AF_INET6)) 412 if (ifa_ptr->ifa_addr->sa_family == AF_INET)
405 continue; 413 alen = sizeof(struct sockaddr_in);
406 if (ifa_ptr->ifa_addr->sa_family == AF_INET) 414 else
407 alen = sizeof(struct sockaddr_in); 415 alen = sizeof(struct sockaddr_in6);
408 else 416 if (GNUNET_OK !=
409 alen = sizeof(struct sockaddr_in6); 417 proc (proc_cls, ifa_ptr->ifa_name,
410 if (GNUNET_OK != 418 (0 == strcmp (ifa_ptr->ifa_name, GNUNET_DEFAULT_INTERFACE)),
411 proc(proc_cls, ifa_ptr->ifa_name, 419 ifa_ptr->ifa_addr, ifa_ptr->ifa_broadaddr,
412 0 == strcmp(ifa_ptr->ifa_name, GNUNET_DEFAULT_INTERFACE), 420 ifa_ptr->ifa_netmask, alen))
413 ifa_ptr->ifa_addr, ifa_ptr->ifa_broadaddr, 421 break;
414 ifa_ptr->ifa_netmask, alen)) 422 }
415 break;
416 }
417 }
418 freeifaddrs(ifa_first);
419 } 423 }
424 freeifaddrs (ifa_first);
425 }
420#else 426#else
421 if (GNUNET_OK == 427 if (GNUNET_OK ==
422 try_ip(proc, 428 try_ip (proc,
423 proc_cls)) 429 proc_cls))
424 return; 430 return;
425 if (GNUNET_OK == 431 if (GNUNET_OK ==
426 try_ifconfig(proc, 432 try_ifconfig (proc,
427 proc_cls)) 433 proc_cls))
428 return; 434 return;
429 LOG(GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, 435 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
430 "Failed to enumerate network interfaces\n"); 436 "Failed to enumerate network interfaces\n");
431#endif 437#endif
432} 438}
433 439