1 | /* $Id: getgateway.c,v 1.15 2009/07/13 08:36:02 nanard Exp $ */ |
---|
2 | /* libnatpmp |
---|
3 | * Copyright (c) 2007-2008, Thomas BERNARD <miniupnp@free.fr> |
---|
4 | * |
---|
5 | * Permission to use, copy, modify, and/or distribute this software for any |
---|
6 | * purpose with or without fee is hereby granted, provided that the above |
---|
7 | * copyright notice and this permission notice appear in all copies. |
---|
8 | * |
---|
9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
---|
10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
---|
11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
---|
12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
---|
13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
---|
14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
---|
15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
---|
16 | #include <stdio.h> |
---|
17 | #include <ctype.h> |
---|
18 | #ifndef WIN32 |
---|
19 | #include <netinet/in.h> |
---|
20 | #endif |
---|
21 | #include <sys/param.h> |
---|
22 | /* There is no portable method to get the default route gateway. |
---|
23 | * So below are three differents functions implementing this. |
---|
24 | * Parsing /proc/net/route is for linux. |
---|
25 | * sysctl is the way to access such informations on BSD systems. |
---|
26 | * Many systems should provide route information through raw PF_ROUTE |
---|
27 | * sockets. */ |
---|
28 | #ifdef __linux__ |
---|
29 | #define USE_PROC_NET_ROUTE |
---|
30 | #undef USE_SOCKET_ROUTE |
---|
31 | #undef USE_SYSCTL_NET_ROUTE |
---|
32 | #endif |
---|
33 | |
---|
34 | #ifdef BSD |
---|
35 | #undef USE_PROC_NET_ROUTE |
---|
36 | #define USE_SOCKET_ROUTE |
---|
37 | #undef USE_SYSCTL_NET_ROUTE |
---|
38 | #endif |
---|
39 | |
---|
40 | #ifdef __APPLE__ |
---|
41 | #undef USE_PROC_NET_ROUTE |
---|
42 | #undef USE_SOCKET_ROUTE |
---|
43 | #define USE_SYSCTL_NET_ROUTE |
---|
44 | #endif |
---|
45 | |
---|
46 | #if (defined(sun) && defined(__SVR4)) |
---|
47 | #undef USE_PROC_NET_ROUTE |
---|
48 | #define USE_SOCKET_ROUTE |
---|
49 | #undef USE_SYSCTL_NET_ROUTE |
---|
50 | #endif |
---|
51 | |
---|
52 | #ifdef WIN32 |
---|
53 | #undef USE_PROC_NET_ROUTE |
---|
54 | #undef USE_SOCKET_ROUTE |
---|
55 | #undef USE_SYSCTL_NET_ROUTE |
---|
56 | #define USE_WIN32_CODE |
---|
57 | #endif |
---|
58 | |
---|
59 | #ifdef __CYGWIN__ |
---|
60 | #undef USE_PROC_NET_ROUTE |
---|
61 | #undef USE_SOCKET_ROUTE |
---|
62 | #undef USE_SYSCTL_NET_ROUTE |
---|
63 | #define USE_WIN32_CODE |
---|
64 | #include <stdarg.h> |
---|
65 | #include <w32api/windef.h> |
---|
66 | #include <w32api/winbase.h> |
---|
67 | #include <w32api/winreg.h> |
---|
68 | #endif |
---|
69 | |
---|
70 | #ifdef __HAIKU__ |
---|
71 | #include <stdlib.h> |
---|
72 | #include <unistd.h> |
---|
73 | #include <net/if.h> |
---|
74 | #include <sys/sockio.h> |
---|
75 | #define USE_HAIKU_CODE |
---|
76 | #endif |
---|
77 | |
---|
78 | #ifdef USE_SYSCTL_NET_ROUTE |
---|
79 | #include <stdlib.h> |
---|
80 | #include <sys/sysctl.h> |
---|
81 | #include <sys/socket.h> |
---|
82 | #include <net/route.h> |
---|
83 | #endif |
---|
84 | #ifdef USE_SOCKET_ROUTE |
---|
85 | #include <unistd.h> |
---|
86 | #include <string.h> |
---|
87 | #include <sys/socket.h> |
---|
88 | #include <net/if.h> |
---|
89 | #include <net/route.h> |
---|
90 | #endif |
---|
91 | #ifdef WIN32 |
---|
92 | #include <unknwn.h> |
---|
93 | #include <winreg.h> |
---|
94 | #define MAX_KEY_LENGTH 255 |
---|
95 | #define MAX_VALUE_LENGTH 16383 |
---|
96 | #endif |
---|
97 | #include "getgateway.h" |
---|
98 | |
---|
99 | #ifndef WIN32 |
---|
100 | #define SUCCESS (0) |
---|
101 | #define FAILED (-1) |
---|
102 | #endif |
---|
103 | |
---|
104 | #ifdef USE_PROC_NET_ROUTE |
---|
105 | int getdefaultgateway(in_addr_t * addr) |
---|
106 | { |
---|
107 | long d, g; |
---|
108 | char buf[256]; |
---|
109 | int line = 0; |
---|
110 | FILE * f; |
---|
111 | char * p; |
---|
112 | f = fopen("/proc/net/route", "r"); |
---|
113 | if(!f) |
---|
114 | return FAILED; |
---|
115 | while(fgets(buf, sizeof(buf), f)) { |
---|
116 | if(line > 0) { |
---|
117 | p = buf; |
---|
118 | while(*p && !isspace(*p)) |
---|
119 | p++; |
---|
120 | while(*p && isspace(*p)) |
---|
121 | p++; |
---|
122 | if(sscanf(p, "%lx%lx", &d, &g)==2) { |
---|
123 | if(d == 0) { /* default */ |
---|
124 | *addr = g; |
---|
125 | fclose(f); |
---|
126 | return SUCCESS; |
---|
127 | } |
---|
128 | } |
---|
129 | } |
---|
130 | line++; |
---|
131 | } |
---|
132 | /* default route not found ! */ |
---|
133 | if(f) |
---|
134 | fclose(f); |
---|
135 | return FAILED; |
---|
136 | } |
---|
137 | #endif /* #ifdef USE_PROC_NET_ROUTE */ |
---|
138 | |
---|
139 | |
---|
140 | #ifdef USE_SYSCTL_NET_ROUTE |
---|
141 | |
---|
142 | #define ROUNDUP(a) \ |
---|
143 | ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) |
---|
144 | |
---|
145 | int getdefaultgateway(in_addr_t * addr) |
---|
146 | { |
---|
147 | #if 0 |
---|
148 | /* net.route.0.inet.dump.0.0 ? */ |
---|
149 | int mib[] = {CTL_NET, PF_ROUTE, 0, AF_INET, |
---|
150 | NET_RT_DUMP, 0, 0/*tableid*/}; |
---|
151 | #endif |
---|
152 | /* net.route.0.inet.flags.gateway */ |
---|
153 | int mib[] = {CTL_NET, PF_ROUTE, 0, AF_INET, |
---|
154 | NET_RT_FLAGS, RTF_GATEWAY}; |
---|
155 | size_t l; |
---|
156 | char * buf, * p; |
---|
157 | struct rt_msghdr * rt; |
---|
158 | struct sockaddr * sa; |
---|
159 | struct sockaddr * sa_tab[RTAX_MAX]; |
---|
160 | int i; |
---|
161 | int r = FAILED; |
---|
162 | if(sysctl(mib, sizeof(mib)/sizeof(int), 0, &l, 0, 0) < 0) { |
---|
163 | return FAILED; |
---|
164 | } |
---|
165 | if(l>0) { |
---|
166 | buf = malloc(l); |
---|
167 | if(sysctl(mib, sizeof(mib)/sizeof(int), buf, &l, 0, 0) < 0) { |
---|
168 | free(buf); |
---|
169 | return FAILED; |
---|
170 | } |
---|
171 | for(p=buf; p<buf+l; p+=rt->rtm_msglen) { |
---|
172 | rt = (struct rt_msghdr *)p; |
---|
173 | sa = (struct sockaddr *)(rt + 1); |
---|
174 | for(i=0; i<RTAX_MAX; i++) { |
---|
175 | if(rt->rtm_addrs & (1 << i)) { |
---|
176 | sa_tab[i] = sa; |
---|
177 | sa = (struct sockaddr *)((char *)sa + ROUNDUP(sa->sa_len)); |
---|
178 | } else { |
---|
179 | sa_tab[i] = NULL; |
---|
180 | } |
---|
181 | } |
---|
182 | if( ((rt->rtm_addrs & (RTA_DST|RTA_GATEWAY)) == (RTA_DST|RTA_GATEWAY)) |
---|
183 | && sa_tab[RTAX_DST]->sa_family == AF_INET |
---|
184 | && sa_tab[RTAX_GATEWAY]->sa_family == AF_INET) { |
---|
185 | if(((struct sockaddr_in *)sa_tab[RTAX_DST])->sin_addr.s_addr == 0) { |
---|
186 | *addr = ((struct sockaddr_in *)(sa_tab[RTAX_GATEWAY]))->sin_addr.s_addr; |
---|
187 | r = SUCCESS; |
---|
188 | } |
---|
189 | } |
---|
190 | } |
---|
191 | free(buf); |
---|
192 | } |
---|
193 | return r; |
---|
194 | } |
---|
195 | #endif /* #ifdef USE_SYSCTL_NET_ROUTE */ |
---|
196 | |
---|
197 | |
---|
198 | #ifdef USE_SOCKET_ROUTE |
---|
199 | /* Thanks to Darren Kenny for this code */ |
---|
200 | #define NEXTADDR(w, u) \ |
---|
201 | if (rtm_addrs & (w)) {\ |
---|
202 | l = sizeof(struct sockaddr); memmove(cp, &(u), l); cp += l;\ |
---|
203 | } |
---|
204 | |
---|
205 | #define rtm m_rtmsg.m_rtm |
---|
206 | |
---|
207 | struct { |
---|
208 | struct rt_msghdr m_rtm; |
---|
209 | char m_space[512]; |
---|
210 | } m_rtmsg; |
---|
211 | |
---|
212 | int getdefaultgateway(in_addr_t *addr) |
---|
213 | { |
---|
214 | int s, seq, l, rtm_addrs, i; |
---|
215 | pid_t pid; |
---|
216 | struct sockaddr so_dst, so_mask; |
---|
217 | char *cp = m_rtmsg.m_space; |
---|
218 | struct sockaddr *gate = NULL, *sa; |
---|
219 | struct rt_msghdr *msg_hdr; |
---|
220 | |
---|
221 | pid = getpid(); |
---|
222 | seq = 0; |
---|
223 | rtm_addrs = RTA_DST | RTA_NETMASK; |
---|
224 | |
---|
225 | memset(&so_dst, 0, sizeof(so_dst)); |
---|
226 | memset(&so_mask, 0, sizeof(so_mask)); |
---|
227 | memset(&rtm, 0, sizeof(struct rt_msghdr)); |
---|
228 | |
---|
229 | rtm.rtm_type = RTM_GET; |
---|
230 | rtm.rtm_flags = RTF_UP | RTF_GATEWAY; |
---|
231 | rtm.rtm_version = RTM_VERSION; |
---|
232 | rtm.rtm_seq = ++seq; |
---|
233 | rtm.rtm_addrs = rtm_addrs; |
---|
234 | |
---|
235 | so_dst.sa_family = AF_INET; |
---|
236 | so_mask.sa_family = AF_INET; |
---|
237 | |
---|
238 | NEXTADDR(RTA_DST, so_dst); |
---|
239 | NEXTADDR(RTA_NETMASK, so_mask); |
---|
240 | |
---|
241 | rtm.rtm_msglen = l = cp - (char *)&m_rtmsg; |
---|
242 | |
---|
243 | s = socket(PF_ROUTE, SOCK_RAW, 0); |
---|
244 | |
---|
245 | if (write(s, (char *)&m_rtmsg, l) < 0) { |
---|
246 | close(s); |
---|
247 | return FAILED; |
---|
248 | } |
---|
249 | |
---|
250 | do { |
---|
251 | l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg)); |
---|
252 | } while (l > 0 && (rtm.rtm_seq != seq || rtm.rtm_pid != pid)); |
---|
253 | |
---|
254 | close(s); |
---|
255 | |
---|
256 | msg_hdr = &rtm; |
---|
257 | |
---|
258 | cp = ((char *)(msg_hdr + 1)); |
---|
259 | if (msg_hdr->rtm_addrs) { |
---|
260 | for (i = 1; i; i <<= 1) |
---|
261 | if (i & msg_hdr->rtm_addrs) { |
---|
262 | sa = (struct sockaddr *)cp; |
---|
263 | if (i == RTA_GATEWAY ) |
---|
264 | gate = sa; |
---|
265 | |
---|
266 | cp += sizeof(struct sockaddr); |
---|
267 | } |
---|
268 | } else { |
---|
269 | return FAILED; |
---|
270 | } |
---|
271 | |
---|
272 | |
---|
273 | if (gate != NULL ) { |
---|
274 | *addr = ((struct sockaddr_in *)gate)->sin_addr.s_addr; |
---|
275 | return SUCCESS; |
---|
276 | } else { |
---|
277 | return FAILED; |
---|
278 | } |
---|
279 | } |
---|
280 | #endif /* #ifdef USE_SOCKET_ROUTE */ |
---|
281 | |
---|
282 | #ifdef USE_WIN32_CODE |
---|
283 | LIBSPEC int getdefaultgateway(in_addr_t * addr) |
---|
284 | { |
---|
285 | HKEY networkCardsKey; |
---|
286 | HKEY networkCardKey; |
---|
287 | HKEY interfacesKey; |
---|
288 | HKEY interfaceKey; |
---|
289 | DWORD i = 0; |
---|
290 | DWORD numSubKeys = 0; |
---|
291 | TCHAR keyName[MAX_KEY_LENGTH]; |
---|
292 | DWORD keyNameLength = MAX_KEY_LENGTH; |
---|
293 | TCHAR keyValue[MAX_VALUE_LENGTH]; |
---|
294 | DWORD keyValueLength = MAX_VALUE_LENGTH; |
---|
295 | DWORD keyValueType = REG_SZ; |
---|
296 | TCHAR gatewayValue[MAX_VALUE_LENGTH]; |
---|
297 | DWORD gatewayValueLength = MAX_VALUE_LENGTH; |
---|
298 | DWORD gatewayValueType = REG_MULTI_SZ; |
---|
299 | int done = 0; |
---|
300 | |
---|
301 | char networkCardsPath[] = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards"; |
---|
302 | char interfacesPath[] = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces"; |
---|
303 | |
---|
304 | // The windows registry lists its primary network devices in the following location: |
---|
305 | // HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards |
---|
306 | // |
---|
307 | // Each network device has its own subfolder, named with an index, with various properties: |
---|
308 | // -NetworkCards |
---|
309 | // -5 |
---|
310 | // -Description = Broadcom 802.11n Network Adapter |
---|
311 | // -ServiceName = {E35A72F8-5065-4097-8DFE-C7790774EE4D} |
---|
312 | // -8 |
---|
313 | // -Description = Marvell Yukon 88E8058 PCI-E Gigabit Ethernet Controller |
---|
314 | // -ServiceName = {86226414-5545-4335-A9D1-5BD7120119AD} |
---|
315 | // |
---|
316 | // The above service name is the name of a subfolder within: |
---|
317 | // HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces |
---|
318 | // |
---|
319 | // There may be more subfolders in this interfaces path than listed in the network cards path above: |
---|
320 | // -Interfaces |
---|
321 | // -{3a539854-6a70-11db-887c-806e6f6e6963} |
---|
322 | // -DhcpIPAddress = 0.0.0.0 |
---|
323 | // -[more] |
---|
324 | // -{E35A72F8-5065-4097-8DFE-C7790774EE4D} |
---|
325 | // -DhcpIPAddress = 10.0.1.4 |
---|
326 | // -DhcpDefaultGateway = 10.0.1.1 |
---|
327 | // -[more] |
---|
328 | // -{86226414-5545-4335-A9D1-5BD7120119AD} |
---|
329 | // -DhcpIpAddress = 10.0.1.5 |
---|
330 | // -DhcpDefaultGateay = 10.0.1.1 |
---|
331 | // -[more] |
---|
332 | // |
---|
333 | // In order to extract this information, we enumerate each network card, and extract the ServiceName value. |
---|
334 | // This is then used to open the interface subfolder, and attempt to extract a DhcpDefaultGateway value. |
---|
335 | // Once one is found, we're done. |
---|
336 | // |
---|
337 | // It may be possible to simply enumerate the interface folders until we find one with a DhcpDefaultGateway value. |
---|
338 | // However, the technique used is the technique most cited on the web, and we assume it to be more correct. |
---|
339 | |
---|
340 | if(ERROR_SUCCESS != RegOpenKeyEx(HKEY_LOCAL_MACHINE, // Open registry key or predifined key |
---|
341 | networkCardsPath, // Name of registry subkey to open |
---|
342 | 0, // Reserved - must be zero |
---|
343 | KEY_READ, // Mask - desired access rights |
---|
344 | &networkCardsKey)) // Pointer to output key |
---|
345 | { |
---|
346 | // Unable to open network cards keys |
---|
347 | return -1; |
---|
348 | } |
---|
349 | |
---|
350 | if(ERROR_SUCCESS != RegOpenKeyEx(HKEY_LOCAL_MACHINE, // Open registry key or predefined key |
---|
351 | interfacesPath, // Name of registry subkey to open |
---|
352 | 0, // Reserved - must be zero |
---|
353 | KEY_READ, // Mask - desired access rights |
---|
354 | &interfacesKey)) // Pointer to output key |
---|
355 | { |
---|
356 | // Unable to open interfaces key |
---|
357 | RegCloseKey(networkCardsKey); |
---|
358 | return -1; |
---|
359 | } |
---|
360 | |
---|
361 | // Figure out how many subfolders are within the NetworkCards folder |
---|
362 | RegQueryInfoKey(networkCardsKey, NULL, NULL, NULL, &numSubKeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL); |
---|
363 | |
---|
364 | //printf( "Number of subkeys: %u\n", (unsigned int)numSubKeys); |
---|
365 | |
---|
366 | // Enumrate through each subfolder within the NetworkCards folder |
---|
367 | for(i = 0; i < numSubKeys && !done; i++) |
---|
368 | { |
---|
369 | keyNameLength = MAX_KEY_LENGTH; |
---|
370 | if(ERROR_SUCCESS == RegEnumKeyEx(networkCardsKey, // Open registry key |
---|
371 | i, // Index of subkey to retrieve |
---|
372 | keyName, // Buffer that receives the name of the subkey |
---|
373 | &keyNameLength, // Variable that receives the size of the above buffer |
---|
374 | NULL, // Reserved - must be NULL |
---|
375 | NULL, // Buffer that receives the class string |
---|
376 | NULL, // Variable that receives the size of the above buffer |
---|
377 | NULL)) // Variable that receives the last write time of subkey |
---|
378 | { |
---|
379 | if(RegOpenKeyEx(networkCardsKey, keyName, 0, KEY_READ, &networkCardKey) == ERROR_SUCCESS) |
---|
380 | { |
---|
381 | keyValueLength = MAX_VALUE_LENGTH; |
---|
382 | if(ERROR_SUCCESS == RegQueryValueEx(networkCardKey, // Open registry key |
---|
383 | "ServiceName", // Name of key to query |
---|
384 | NULL, // Reserved - must be NULL |
---|
385 | &keyValueType, // Receives value type |
---|
386 | keyValue, // Receives value |
---|
387 | &keyValueLength)) // Receives value length in bytes |
---|
388 | { |
---|
389 | //printf("keyValue: %s\n", keyValue); |
---|
390 | |
---|
391 | if(RegOpenKeyEx(interfacesKey, keyValue, 0, KEY_READ, &interfaceKey) == ERROR_SUCCESS) |
---|
392 | { |
---|
393 | gatewayValueLength = MAX_VALUE_LENGTH; |
---|
394 | if(ERROR_SUCCESS == RegQueryValueEx(interfaceKey, // Open registry key |
---|
395 | "DhcpDefaultGateway", // Name of key to query |
---|
396 | NULL, // Reserved - must be NULL |
---|
397 | &gatewayValueType, // Receives value type |
---|
398 | gatewayValue, // Receives value |
---|
399 | &gatewayValueLength)) // Receives value length in bytes |
---|
400 | { |
---|
401 | // Check to make sure it's a string |
---|
402 | if(gatewayValueType == REG_MULTI_SZ || gatewayValueType == REG_SZ) |
---|
403 | { |
---|
404 | //printf("gatewayValue: %s\n", gatewayValue); |
---|
405 | done = 1; |
---|
406 | } |
---|
407 | } |
---|
408 | else if(ERROR_SUCCESS == RegQueryValueEx(interfaceKey, // Open registry key |
---|
409 | "DefaultGateway", // Name of key to query |
---|
410 | NULL, // Reserved - must be NULL |
---|
411 | &gatewayValueType, // Receives value type |
---|
412 | gatewayValue, // Receives value |
---|
413 | &gatewayValueLength)) // Receives value length in bytes |
---|
414 | { |
---|
415 | // Check to make sure it's a string |
---|
416 | if(gatewayValueType == REG_MULTI_SZ || gatewayValueType == REG_SZ) |
---|
417 | { |
---|
418 | //printf("gatewayValue: %s\n", gatewayValue); |
---|
419 | done = 1; |
---|
420 | } |
---|
421 | } |
---|
422 | RegCloseKey(interfaceKey); |
---|
423 | } |
---|
424 | } |
---|
425 | RegCloseKey(networkCardKey); |
---|
426 | } |
---|
427 | } |
---|
428 | } |
---|
429 | |
---|
430 | RegCloseKey(interfacesKey); |
---|
431 | RegCloseKey(networkCardsKey); |
---|
432 | |
---|
433 | if(done) |
---|
434 | { |
---|
435 | *addr = inet_addr(gatewayValue); |
---|
436 | return 0; |
---|
437 | } |
---|
438 | |
---|
439 | return -1; |
---|
440 | } |
---|
441 | #endif /* #ifdef USE_WIN32_CODE */ |
---|
442 | |
---|
443 | #ifdef USE_HAIKU_CODE |
---|
444 | int getdefaultgateway(in_addr_t *addr) |
---|
445 | { |
---|
446 | int fd, ret = -1; |
---|
447 | struct ifconf config; |
---|
448 | void *buffer = NULL; |
---|
449 | struct ifreq *interface; |
---|
450 | |
---|
451 | if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { |
---|
452 | return -1; |
---|
453 | } |
---|
454 | if (ioctl(fd, SIOCGRTSIZE, &config, sizeof(config)) != 0) { |
---|
455 | goto fail; |
---|
456 | } |
---|
457 | if (config.ifc_value < 1) { |
---|
458 | goto fail; /* No routes */ |
---|
459 | } |
---|
460 | if ((buffer = malloc(config.ifc_value)) == NULL) { |
---|
461 | goto fail; |
---|
462 | } |
---|
463 | config.ifc_len = config.ifc_value; |
---|
464 | config.ifc_buf = buffer; |
---|
465 | if (ioctl(fd, SIOCGRTTABLE, &config, sizeof(config)) != 0) { |
---|
466 | goto fail; |
---|
467 | } |
---|
468 | for (interface = buffer; |
---|
469 | (uint8_t *)interface < (uint8_t *)buffer + config.ifc_len; ) { |
---|
470 | struct route_entry route = interface->ifr_route; |
---|
471 | int intfSize; |
---|
472 | if (route.flags & (RTF_GATEWAY | RTF_DEFAULT)) { |
---|
473 | *addr = ((struct sockaddr_in *)route.gateway)->sin_addr.s_addr; |
---|
474 | ret = 0; |
---|
475 | break; |
---|
476 | } |
---|
477 | intfSize = sizeof(route) + IF_NAMESIZE; |
---|
478 | if (route.destination != NULL) { |
---|
479 | intfSize += route.destination->sa_len; |
---|
480 | } |
---|
481 | if (route.mask != NULL) { |
---|
482 | intfSize += route.mask->sa_len; |
---|
483 | } |
---|
484 | if (route.gateway != NULL) { |
---|
485 | intfSize += route.gateway->sa_len; |
---|
486 | } |
---|
487 | interface = (struct ifreq *)((uint8_t *)interface + intfSize); |
---|
488 | } |
---|
489 | fail: |
---|
490 | free(buffer); |
---|
491 | close(fd); |
---|
492 | return ret; |
---|
493 | } |
---|
494 | #endif /* #ifdef USE_HAIKU_CODE */ |
---|
495 | |
---|
496 | |
---|