1
/* $Id: getgateway.c,v 1.19 2009/12/19 15:20:45 nanard Exp $ */
3
* Copyright (c) 2007-2009, Thomas BERNARD <miniupnp@free.fr>
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.
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. */
19
#include <netinet/in.h>
21
#if !defined(_MSC_VER)
22
#include <sys/param.h>
24
/* There is no portable method to get the default route gateway.
25
* So below are four (or five ?) differents functions implementing this.
26
* Parsing /proc/net/route is for linux.
27
* sysctl is the way to access such informations on BSD systems.
28
* Many systems should provide route information through raw PF_ROUTE
30
* In MS Windows, default gateway is found by looking into the registry
31
* or by using GetBestRoute(). */
33
#define USE_PROC_NET_ROUTE
34
#undef USE_SOCKET_ROUTE
35
#undef USE_SYSCTL_NET_ROUTE
39
#undef USE_PROC_NET_ROUTE
40
#define USE_SOCKET_ROUTE
41
#undef USE_SYSCTL_NET_ROUTE
45
#undef USE_PROC_NET_ROUTE
46
#undef USE_SOCKET_ROUTE
47
#define USE_SYSCTL_NET_ROUTE
50
#if (defined(sun) && defined(__SVR4))
51
#undef USE_PROC_NET_ROUTE
52
#define USE_SOCKET_ROUTE
53
#undef USE_SYSCTL_NET_ROUTE
57
#undef USE_PROC_NET_ROUTE
58
#undef USE_SOCKET_ROUTE
59
#undef USE_SYSCTL_NET_ROUTE
60
//#define USE_WIN32_CODE
61
#define USE_WIN32_CODE_2
65
#undef USE_PROC_NET_ROUTE
66
#undef USE_SOCKET_ROUTE
67
#undef USE_SYSCTL_NET_ROUTE
68
#define USE_WIN32_CODE
70
#include <w32api/windef.h>
71
#include <w32api/winbase.h>
72
#include <w32api/winreg.h>
79
#include <sys/sockio.h>
80
#define USE_HAIKU_CODE
83
#ifdef USE_SYSCTL_NET_ROUTE
85
#include <sys/sysctl.h>
86
#include <sys/socket.h>
87
#include <net/route.h>
89
#ifdef USE_SOCKET_ROUTE
92
#include <sys/socket.h>
94
#include <net/route.h>
100
#define MAX_KEY_LENGTH 255
101
#define MAX_VALUE_LENGTH 16383
104
#ifdef USE_WIN32_CODE_2
106
#include <iphlpapi.h>
109
#include "getgateway.h"
116
#ifdef USE_PROC_NET_ROUTE
118
parse /proc/net/route which is as follow :
120
Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
121
wlan0 0001A8C0 00000000 0001 0 0 0 00FFFFFF 0 0 0
122
eth0 0000FEA9 00000000 0001 0 0 0 0000FFFF 0 0 0
123
wlan0 00000000 0101A8C0 0003 0 0 0 00000000 0 0 0
124
eth0 00000000 00000000 0001 0 0 1000 00000000 0 0 0
126
One header line, and then one line by route by route table entry.
128
int getdefaultgateway(in_addr_t * addr)
135
f = fopen("/proc/net/route", "r");
138
while(fgets(buf, sizeof(buf), f)) {
139
if(line > 0) { /* skip the first line */
141
/* skip the interface name */
142
while(*p && !isspace(*p))
144
while(*p && isspace(*p))
146
if(sscanf(p, "%lx%lx", &d, &g)==2) {
147
if(d == 0 && g != 0) { /* default */
156
/* default route not found ! */
161
#endif /* #ifdef USE_PROC_NET_ROUTE */
164
#ifdef USE_SYSCTL_NET_ROUTE
167
((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
169
int getdefaultgateway(in_addr_t * addr)
172
/* net.route.0.inet.dump.0.0 ? */
173
int mib[] = {CTL_NET, PF_ROUTE, 0, AF_INET,
174
NET_RT_DUMP, 0, 0/*tableid*/};
176
/* net.route.0.inet.flags.gateway */
177
int mib[] = {CTL_NET, PF_ROUTE, 0, AF_INET,
178
NET_RT_FLAGS, RTF_GATEWAY};
181
struct rt_msghdr * rt;
182
struct sockaddr * sa;
183
struct sockaddr * sa_tab[RTAX_MAX];
186
if(sysctl(mib, sizeof(mib)/sizeof(int), 0, &l, 0, 0) < 0) {
191
if(sysctl(mib, sizeof(mib)/sizeof(int), buf, &l, 0, 0) < 0) {
195
for(p=buf; p<buf+l; p+=rt->rtm_msglen) {
196
rt = (struct rt_msghdr *)p;
197
sa = (struct sockaddr *)(rt + 1);
198
for(i=0; i<RTAX_MAX; i++) {
199
if(rt->rtm_addrs & (1 << i)) {
201
sa = (struct sockaddr *)((char *)sa + ROUNDUP(sa->sa_len));
206
if( ((rt->rtm_addrs & (RTA_DST|RTA_GATEWAY)) == (RTA_DST|RTA_GATEWAY))
207
&& sa_tab[RTAX_DST]->sa_family == AF_INET
208
&& sa_tab[RTAX_GATEWAY]->sa_family == AF_INET) {
209
if(((struct sockaddr_in *)sa_tab[RTAX_DST])->sin_addr.s_addr == 0) {
210
*addr = ((struct sockaddr_in *)(sa_tab[RTAX_GATEWAY]))->sin_addr.s_addr;
219
#endif /* #ifdef USE_SYSCTL_NET_ROUTE */
222
#ifdef USE_SOCKET_ROUTE
223
/* Thanks to Darren Kenny for this code */
224
#define NEXTADDR(w, u) \
225
if (rtm_addrs & (w)) {\
226
l = sizeof(struct sockaddr); memmove(cp, &(u), l); cp += l;\
229
#define rtm m_rtmsg.m_rtm
232
struct rt_msghdr m_rtm;
236
int getdefaultgateway(in_addr_t *addr)
238
int s, seq, l, rtm_addrs, i;
240
struct sockaddr so_dst, so_mask;
241
char *cp = m_rtmsg.m_space;
242
struct sockaddr *gate = NULL, *sa;
243
struct rt_msghdr *msg_hdr;
247
rtm_addrs = RTA_DST | RTA_NETMASK;
249
memset(&so_dst, 0, sizeof(so_dst));
250
memset(&so_mask, 0, sizeof(so_mask));
251
memset(&rtm, 0, sizeof(struct rt_msghdr));
253
rtm.rtm_type = RTM_GET;
254
rtm.rtm_flags = RTF_UP | RTF_GATEWAY;
255
rtm.rtm_version = RTM_VERSION;
257
rtm.rtm_addrs = rtm_addrs;
259
so_dst.sa_family = AF_INET;
260
so_mask.sa_family = AF_INET;
262
NEXTADDR(RTA_DST, so_dst);
263
NEXTADDR(RTA_NETMASK, so_mask);
265
rtm.rtm_msglen = l = cp - (char *)&m_rtmsg;
267
s = socket(PF_ROUTE, SOCK_RAW, 0);
269
if (write(s, (char *)&m_rtmsg, l) < 0) {
275
l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
276
} while (l > 0 && (rtm.rtm_seq != seq || rtm.rtm_pid != pid));
282
cp = ((char *)(msg_hdr + 1));
283
if (msg_hdr->rtm_addrs) {
284
for (i = 1; i; i <<= 1)
285
if (i & msg_hdr->rtm_addrs) {
286
sa = (struct sockaddr *)cp;
287
if (i == RTA_GATEWAY )
290
cp += sizeof(struct sockaddr);
298
*addr = ((struct sockaddr_in *)gate)->sin_addr.s_addr;
304
#endif /* #ifdef USE_SOCKET_ROUTE */
306
#ifdef USE_WIN32_CODE
307
LIBSPEC int getdefaultgateway(in_addr_t * addr)
309
HKEY networkCardsKey;
314
DWORD numSubKeys = 0;
315
TCHAR keyName[MAX_KEY_LENGTH];
316
DWORD keyNameLength = MAX_KEY_LENGTH;
317
TCHAR keyValue[MAX_VALUE_LENGTH];
318
DWORD keyValueLength = MAX_VALUE_LENGTH;
319
DWORD keyValueType = REG_SZ;
320
TCHAR gatewayValue[MAX_VALUE_LENGTH];
321
DWORD gatewayValueLength = MAX_VALUE_LENGTH;
322
DWORD gatewayValueType = REG_MULTI_SZ;
325
//const char * networkCardsPath = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards";
326
//const char * interfacesPath = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces";
328
LPCTSTR networkCardsPath = L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards";
329
LPCTSTR interfacesPath = L"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces";
330
#define STR_SERVICENAME L"ServiceName"
331
#define STR_DHCPDEFAULTGATEWAY L"DhcpDefaultGateway"
332
#define STR_DEFAULTGATEWAY L"DefaultGateway"
334
LPCTSTR networkCardsPath = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards";
335
LPCTSTR interfacesPath = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces";
336
#define STR_SERVICENAME "ServiceName"
337
#define STR_DHCPDEFAULTGATEWAY "DhcpDefaultGateway"
338
#define STR_DEFAULTGATEWAY "DefaultGateway"
340
// The windows registry lists its primary network devices in the following location:
341
// HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards
343
// Each network device has its own subfolder, named with an index, with various properties:
346
// -Description = Broadcom 802.11n Network Adapter
347
// -ServiceName = {E35A72F8-5065-4097-8DFE-C7790774EE4D}
349
// -Description = Marvell Yukon 88E8058 PCI-E Gigabit Ethernet Controller
350
// -ServiceName = {86226414-5545-4335-A9D1-5BD7120119AD}
352
// The above service name is the name of a subfolder within:
353
// HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces
355
// There may be more subfolders in this interfaces path than listed in the network cards path above:
357
// -{3a539854-6a70-11db-887c-806e6f6e6963}
358
// -DhcpIPAddress = 0.0.0.0
360
// -{E35A72F8-5065-4097-8DFE-C7790774EE4D}
361
// -DhcpIPAddress = 10.0.1.4
362
// -DhcpDefaultGateway = 10.0.1.1
364
// -{86226414-5545-4335-A9D1-5BD7120119AD}
365
// -DhcpIpAddress = 10.0.1.5
366
// -DhcpDefaultGateay = 10.0.1.1
369
// In order to extract this information, we enumerate each network card, and extract the ServiceName value.
370
// This is then used to open the interface subfolder, and attempt to extract a DhcpDefaultGateway value.
371
// Once one is found, we're done.
373
// It may be possible to simply enumerate the interface folders until we find one with a DhcpDefaultGateway value.
374
// However, the technique used is the technique most cited on the web, and we assume it to be more correct.
376
if(ERROR_SUCCESS != RegOpenKeyEx(HKEY_LOCAL_MACHINE, // Open registry key or predifined key
377
networkCardsPath, // Name of registry subkey to open
378
0, // Reserved - must be zero
379
KEY_READ, // Mask - desired access rights
380
&networkCardsKey)) // Pointer to output key
382
// Unable to open network cards keys
386
if(ERROR_SUCCESS != RegOpenKeyEx(HKEY_LOCAL_MACHINE, // Open registry key or predefined key
387
interfacesPath, // Name of registry subkey to open
388
0, // Reserved - must be zero
389
KEY_READ, // Mask - desired access rights
390
&interfacesKey)) // Pointer to output key
392
// Unable to open interfaces key
393
RegCloseKey(networkCardsKey);
397
// Figure out how many subfolders are within the NetworkCards folder
398
RegQueryInfoKey(networkCardsKey, NULL, NULL, NULL, &numSubKeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
400
//printf( "Number of subkeys: %u\n", (unsigned int)numSubKeys);
402
// Enumrate through each subfolder within the NetworkCards folder
403
for(i = 0; i < numSubKeys && !done; i++)
405
keyNameLength = MAX_KEY_LENGTH;
406
if(ERROR_SUCCESS == RegEnumKeyEx(networkCardsKey, // Open registry key
407
i, // Index of subkey to retrieve
408
keyName, // Buffer that receives the name of the subkey
409
&keyNameLength, // Variable that receives the size of the above buffer
410
NULL, // Reserved - must be NULL
411
NULL, // Buffer that receives the class string
412
NULL, // Variable that receives the size of the above buffer
413
NULL)) // Variable that receives the last write time of subkey
415
if(RegOpenKeyEx(networkCardsKey, keyName, 0, KEY_READ, &networkCardKey) == ERROR_SUCCESS)
417
keyValueLength = MAX_VALUE_LENGTH;
418
if(ERROR_SUCCESS == RegQueryValueEx(networkCardKey, // Open registry key
419
STR_SERVICENAME, // Name of key to query
420
NULL, // Reserved - must be NULL
421
&keyValueType, // Receives value type
422
(LPBYTE)keyValue, // Receives value
423
&keyValueLength)) // Receives value length in bytes
425
// printf("keyValue: %s\n", keyValue);
426
if(RegOpenKeyEx(interfacesKey, keyValue, 0, KEY_READ, &interfaceKey) == ERROR_SUCCESS)
428
gatewayValueLength = MAX_VALUE_LENGTH;
429
if(ERROR_SUCCESS == RegQueryValueEx(interfaceKey, // Open registry key
430
STR_DHCPDEFAULTGATEWAY, // Name of key to query
431
NULL, // Reserved - must be NULL
432
&gatewayValueType, // Receives value type
433
(LPBYTE)gatewayValue, // Receives value
434
&gatewayValueLength)) // Receives value length in bytes
436
// Check to make sure it's a string
437
if((gatewayValueType == REG_MULTI_SZ || gatewayValueType == REG_SZ) && (gatewayValueLength > 1))
439
//printf("gatewayValue: %s\n", gatewayValue);
443
else if(ERROR_SUCCESS == RegQueryValueEx(interfaceKey, // Open registry key
444
STR_DEFAULTGATEWAY, // Name of key to query
445
NULL, // Reserved - must be NULL
446
&gatewayValueType, // Receives value type
447
(LPBYTE)gatewayValue,// Receives value
448
&gatewayValueLength)) // Receives value length in bytes
450
// Check to make sure it's a string
451
if((gatewayValueType == REG_MULTI_SZ || gatewayValueType == REG_SZ) && (gatewayValueLength > 1))
453
//printf("gatewayValue: %s\n", gatewayValue);
457
RegCloseKey(interfaceKey);
460
RegCloseKey(networkCardKey);
465
RegCloseKey(interfacesKey);
466
RegCloseKey(networkCardsKey);
472
for(i = 0; i < 32; i++) {
473
tmp[i] = (char)gatewayValue[i];
478
*addr = inet_addr(tmp);
480
*addr = inet_addr(gatewayValue);
487
#endif /* #ifdef USE_WIN32_CODE */
489
#ifdef USE_WIN32_CODE_2
490
int getdefaultgateway(in_addr_t *addr)
492
MIB_IPFORWARDROW ip_forward;
493
memset(&ip_forward, 0, sizeof(ip_forward));
494
if(GetBestRoute(inet_addr("0.0.0.0"), 0, &ip_forward) != NO_ERROR)
496
*addr = ip_forward.dwForwardNextHop;
499
#endif /* #ifdef USE_WIN32_CODE_2 */
501
#ifdef USE_HAIKU_CODE
502
int getdefaultgateway(in_addr_t *addr)
505
struct ifconf config;
507
struct ifreq *interface;
509
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
512
if (ioctl(fd, SIOCGRTSIZE, &config, sizeof(config)) != 0) {
515
if (config.ifc_value < 1) {
516
goto fail; /* No routes */
518
if ((buffer = malloc(config.ifc_value)) == NULL) {
521
config.ifc_len = config.ifc_value;
522
config.ifc_buf = buffer;
523
if (ioctl(fd, SIOCGRTTABLE, &config, sizeof(config)) != 0) {
526
for (interface = buffer;
527
(uint8_t *)interface < (uint8_t *)buffer + config.ifc_len; ) {
528
struct route_entry route = interface->ifr_route;
530
if (route.flags & (RTF_GATEWAY | RTF_DEFAULT)) {
531
*addr = ((struct sockaddr_in *)route.gateway)->sin_addr.s_addr;
535
intfSize = sizeof(route) + IF_NAMESIZE;
536
if (route.destination != NULL) {
537
intfSize += route.destination->sa_len;
539
if (route.mask != NULL) {
540
intfSize += route.mask->sa_len;
542
if (route.gateway != NULL) {
543
intfSize += route.gateway->sa_len;
545
interface = (struct ifreq *)((uint8_t *)interface + intfSize);
552
#endif /* #ifdef USE_HAIKU_CODE */