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

Complete pnal_get_gateway() TODO, but just Linux #563

Closed
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
20 changes: 13 additions & 7 deletions src/ports/linux/pnal.c
Original file line number Diff line number Diff line change
Expand Up @@ -560,20 +560,26 @@ 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) */
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0)
{
return PNAL_IPADDR_INVALID;
}

pnal_ipaddr_t ip;
pnal_ipaddr_t gateway;
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, interface_name, IFNAMSIZ - 1);

ip = pnal_get_ip_address (interface_name);
if (ip == PNAL_IPADDR_INVALID)
if (ioctl(sockfd, SIOCGIFDSTADDR, &ifr) < 0)
{
close(sockfd);
return PNAL_IPADDR_INVALID;
}

gateway = (ip & 0xFFFFFF00) | 0x00000001;
close(sockfd);

return gateway;
struct sockaddr_in *gateway = (struct sockaddr_in *)&ifr.ifr_dstaddr;
return ntohl(gateway->sin_addr.s_addr);
}

int pnal_get_hostname (char * hostname)
Expand Down