Revision 662a3ab9 src/net_helpers.c
src/net_helpers.c | ||
---|---|---|
42 | 42 |
#include <string.h> |
43 | 43 |
|
44 | 44 |
#include "net_helpers.h" |
45 |
extern enum L3PROTOCOL {IPv4, IPv6} l3; |
|
46 | 45 |
|
47 |
char *iface_addr(const char *iface) |
|
46 |
char *iface_addr(const char *iface, enum L3PROTOCOL l3)
|
|
48 | 47 |
{ |
49 | 48 |
#ifndef _WIN32 |
50 | 49 |
struct ifaddrs *if_addr, *ifap; |
... | ... | |
66 | 65 |
continue; |
67 | 66 |
} |
68 | 67 |
family = ifap->ifa_addr->sa_family; |
69 |
if (l3 == IPv4 && family == AF_INET && !strcmp (ifap->ifa_name, iface))
|
|
68 |
if (l3 == IP4 && family == AF_INET && !strcmp (ifap->ifa_name, iface)) |
|
70 | 69 |
{ |
71 | 70 |
host_addr = malloc((size_t)INET_ADDRSTRLEN); |
72 | 71 |
if (!host_addr) |
... | ... | |
82 | 81 |
} |
83 | 82 |
break; |
84 | 83 |
} |
85 |
if (l3 == IPv6 && family == AF_INET6 && !strcmp (ifap->ifa_name, iface))
|
|
84 |
if (l3 == IP6 && family == AF_INET6 && !strcmp (ifap->ifa_name, iface)) |
|
86 | 85 |
{ |
87 | 86 |
host_addr = malloc((size_t)INET6_ADDRSTRLEN); |
88 | 87 |
if (!host_addr) |
... | ... | |
108 | 107 |
{ |
109 | 108 |
switch (l3) |
110 | 109 |
{ |
111 |
case IPv4:
|
|
110 |
case IP4: |
|
112 | 111 |
res = malloc (INET_ADDRSTRLEN); |
113 | 112 |
strcpy(res, "127.0.0.1"); |
114 | 113 |
break; |
115 |
case IPv6:
|
|
114 |
case IP6: |
|
116 | 115 |
res = malloc (INET6_ADDRSTRLEN); |
117 | 116 |
strcpy(res, "::1"); |
118 | 117 |
break; |
... | ... | |
195 | 194 |
for (res = result; res != NULL ; res = res->ai_next) |
196 | 195 |
{ |
197 | 196 |
dtprintf(stderr, "Address Family is %d...", res->ai_family); |
198 |
if ( (res->ai_family == AF_INET && l3 == IPv4) ||
|
|
199 |
(res->ai_family == AF_INET6 && l3 == IPv6))
|
|
197 |
if ( (res->ai_family == AF_INET && l3 == IP4) || |
|
198 |
(res->ai_family == AF_INET6 && l3 == IP6)) |
|
200 | 199 |
{ |
201 | 200 |
if (res->ai_family == AF_INET) |
202 | 201 |
{ |
... | ... | |
221 | 220 |
#endif |
222 | 221 |
} |
223 | 222 |
|
224 |
char *autodetect_ip_address() { |
|
223 |
char *autodetect_ip_address(enum L3PROTOCOL l3) {
|
|
225 | 224 |
#ifdef __linux__ |
226 | 225 |
|
227 |
// static char addr[128] = ""; |
|
228 | 226 |
char iface[IFNAMSIZ] = ""; |
229 | 227 |
char line[128] = "x"; |
230 |
// struct ifaddrs *ifaddr, *ifa; |
|
231 |
// char *host_addr; |
|
232 |
// int res; |
|
233 | 228 |
|
234 | 229 |
FILE *r = fopen("/proc/net/route", "r"); |
235 | 230 |
if (!r) return NULL; |
... | ... | |
247 | 242 |
} |
248 | 243 |
fclose(r); |
249 | 244 |
|
250 |
return iface_addr(iface); |
|
251 |
// if (iface[0] == 0) return NULL; |
|
252 |
// |
|
253 |
// if (getifaddrs(&ifaddr) < 0) { |
|
254 |
// perror("getifaddrs"); |
|
255 |
// return NULL; |
|
256 |
// } |
|
257 |
// |
|
258 |
// ifa = ifaddr; |
|
259 |
// while (ifa) { |
|
260 |
// if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET && |
|
261 |
// ifa->ifa_name && !strcmp(ifa->ifa_name, iface)) { |
|
262 |
// if (l3 == IPv4 && ifa->ifa_addr->sa_family == AF_INET) |
|
263 |
// { |
|
264 |
// host_addr = malloc((size_t)INET_ADDRSTRLEN); |
|
265 |
// if (!host_addr) |
|
266 |
// { |
|
267 |
// perror("malloc host_addr"); |
|
268 |
// return NULL; |
|
269 |
// } |
|
270 |
// if (!inet_ntop(ifa->ifa_addr->sa_family, (void *)&(((struct sockaddr_in *)(ifa->ifa_addr))->sin_addr), host_addr, INET_ADDRSTRLEN)) |
|
271 |
// { |
|
272 |
// perror("inet_ntop"); |
|
273 |
// return NULL; |
|
274 |
// } |
|
275 |
// break; |
|
276 |
// } |
|
277 |
// void *tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr; |
|
278 |
// |
|
279 |
// res = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), line, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); |
|
280 |
// printf("dev: %-8s address: <%s> \n", ifa->ifa_name, line); |
|
281 |
// if (inet_ntop(AF_INET, tmpAddrPtr, addr, 127)) { |
|
282 |
// ret = addr; |
|
283 |
// } else { |
|
284 |
// perror("inet_ntop error"); |
|
285 |
// ret = NULL; |
|
286 |
// } |
|
287 |
// break; |
|
288 |
// } |
|
289 |
// if (l3 == IPv6 && ifa->ifa_addr->sa_family == AF_INET6){ |
|
290 |
// void *tmpAddrPtr=&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr; |
|
291 |
// if (inet_ntop(AF_INET6, tmpAddrPtr, addr, 127)) { |
|
292 |
// ret = addr; |
|
293 |
// } else { |
|
294 |
// perror("inet_ntop error"); |
|
295 |
// ret = NULL; |
|
296 |
// } |
|
297 |
// break; |
|
298 |
// } |
|
299 |
// break; |
|
300 |
// } |
|
301 |
// ifa=ifa->ifa_next; |
|
302 |
// } |
|
303 |
// |
|
304 |
// freeifaddrs(ifaddr); |
|
305 |
// return ret; |
|
245 |
return iface_addr(iface, l3); |
|
306 | 246 |
#else |
307 | 247 |
return hostname_ip_addr(); |
308 |
// return simple_ip_addr(); |
|
309 | 248 |
#endif |
310 | 249 |
} |
311 | 250 |
|
312 |
char *default_ip_addr() |
|
251 |
char *default_ip_addr(enum L3PROTOCOL l3)
|
|
313 | 252 |
{ |
314 | 253 |
char *ip = NULL; |
315 | 254 |
|
316 | 255 |
dtprintf("Trying to guess IP ..."); |
317 | 256 |
|
318 |
ip = autodetect_ip_address(); |
|
257 |
ip = autodetect_ip_address(l3);
|
|
319 | 258 |
|
320 | 259 |
if (!ip) { |
321 | 260 |
dtprintf("cannot detect IP!\n"); |
Also available in: Unified diff