Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read actual default gateway (Linux) #564

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions src/ports/linux/pnal.c
Original file line number Diff line number Diff line change
Expand Up @@ -560,20 +560,43 @@ pnal_ipaddr_t pnal_get_netmask (const char * interface_name)

pnal_ipaddr_t pnal_get_gateway (const char * interface_name)
{
/* TODO Read the actual default gateway (somewhat complicated) */
char line[1024];

pnal_ipaddr_t ip;
pnal_ipaddr_t gateway;

ip = pnal_get_ip_address (interface_name);
if (ip == PNAL_IPADDR_INVALID)
// Open the route file
FILE* fp = fopen("/proc/net/route", "r");
if (fp == NULL)
{
return PNAL_IPADDR_INVALID;
}

gateway = (ip & 0xFFFFFF00) | 0x00000001;
// Skip the first line (header)
if(fgets(line, sizeof(line), fp) == NULL)
{
fclose(fp);
return PNAL_IPADDR_INVALID;
}

return gateway;
// Read the route entries
while (fgets(line, sizeof(line), fp))
{
char iface[1024], dest[1024];
uint32_t flags, refcnt, use, metric, mask, mtu, window, irtt, gateway;

int ret = sscanf(line, "%s %s %x %x %d %d %d %x %d %d %d\n",
iface, dest, &gateway, &flags, &refcnt, &use,
&metric, &mask, &mtu, &window, &irtt);

if (ret == 11)
{
if (!strcmp(iface, interface_name) && gateway != 0)
{
fclose(fp);
return htonl(gateway);
}
}
}
fclose(fp);
return PNAL_IPADDR_INVALID;
}

int pnal_get_hostname (char * hostname)
Expand Down