Last active
July 8, 2024 06:36
-
-
Save javiermon/6272065 to your computer and use it in GitHub Desktop.
get default gateway
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/socket.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <linux/netlink.h> | |
#include <linux/rtnetlink.h> | |
#include <arpa/inet.h> | |
#include <unistd.h> | |
#include <net/if.h> | |
#define BUFFER_SIZE 4096 | |
int getgatewayandiface() | |
{ | |
int received_bytes = 0, msg_len = 0, route_attribute_len = 0; | |
int sock = -1, msgseq = 0; | |
struct nlmsghdr *nlh, *nlmsg; | |
struct rtmsg *route_entry; | |
// This struct contain route attributes (route type) | |
struct rtattr *route_attribute; | |
char gateway_address[INET_ADDRSTRLEN], interface[IF_NAMESIZE]; | |
char msgbuf[BUFFER_SIZE], buffer[BUFFER_SIZE]; | |
char *ptr = buffer; | |
struct timeval tv; | |
if ((sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) < 0) { | |
perror("socket failed"); | |
return EXIT_FAILURE; | |
} | |
memset(msgbuf, 0, sizeof(msgbuf)); | |
memset(gateway_address, 0, sizeof(gateway_address)); | |
memset(interface, 0, sizeof(interface)); | |
memset(buffer, 0, sizeof(buffer)); | |
/* point the header and the msg structure pointers into the buffer */ | |
nlmsg = (struct nlmsghdr *)msgbuf; | |
/* Fill in the nlmsg header*/ | |
nlmsg->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); | |
nlmsg->nlmsg_type = RTM_GETROUTE; // Get the routes from kernel routing table . | |
nlmsg->nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST; // The message is a request for dump. | |
nlmsg->nlmsg_seq = msgseq++; // Sequence of the message packet. | |
nlmsg->nlmsg_pid = getpid(); // PID of process sending the request. | |
/* 1 Sec Timeout to avoid stall */ | |
tv.tv_sec = 1; | |
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (struct timeval *)&tv, sizeof(struct timeval)); | |
/* send msg */ | |
if (send(sock, nlmsg, nlmsg->nlmsg_len, 0) < 0) { | |
perror("send failed"); | |
return EXIT_FAILURE; | |
} | |
/* receive response */ | |
do | |
{ | |
received_bytes = recv(sock, ptr, sizeof(buffer) - msg_len, 0); | |
if (received_bytes < 0) { | |
perror("Error in recv"); | |
return EXIT_FAILURE; | |
} | |
nlh = (struct nlmsghdr *) ptr; | |
/* Check if the header is valid */ | |
if((NLMSG_OK(nlmsg, received_bytes) == 0) || | |
(nlmsg->nlmsg_type == NLMSG_ERROR)) | |
{ | |
perror("Error in received packet"); | |
return EXIT_FAILURE; | |
} | |
/* If we received all data break */ | |
if (nlh->nlmsg_type == NLMSG_DONE) | |
break; | |
else { | |
ptr += received_bytes; | |
msg_len += received_bytes; | |
} | |
/* Break if its not a multi part message */ | |
if ((nlmsg->nlmsg_flags & NLM_F_MULTI) == 0) | |
break; | |
} | |
while ((nlmsg->nlmsg_seq != msgseq) || (nlmsg->nlmsg_pid != getpid())); | |
/* parse response */ | |
for ( ; NLMSG_OK(nlh, received_bytes); nlh = NLMSG_NEXT(nlh, received_bytes)) | |
{ | |
/* Get the route data */ | |
route_entry = (struct rtmsg *) NLMSG_DATA(nlh); | |
/* We are just interested in main routing table */ | |
if (route_entry->rtm_table != RT_TABLE_MAIN) | |
continue; | |
route_attribute = (struct rtattr *) RTM_RTA(route_entry); | |
route_attribute_len = RTM_PAYLOAD(nlh); | |
/* Loop through all attributes */ | |
for ( ; RTA_OK(route_attribute, route_attribute_len); | |
route_attribute = RTA_NEXT(route_attribute, route_attribute_len)) | |
{ | |
switch(route_attribute->rta_type) { | |
case RTA_OIF: | |
if_indextoname(*(int *)RTA_DATA(route_attribute), interface); | |
break; | |
case RTA_GATEWAY: | |
inet_ntop(AF_INET, RTA_DATA(route_attribute), | |
gateway_address, sizeof(gateway_address)); | |
break; | |
default: | |
break; | |
} | |
} | |
if ((*gateway_address) && (*interface)) { | |
fprintf(stdout, "Gateway %s for interface %s\n", gateway_address, interface); | |
break; | |
} | |
} | |
close(sock); | |
return 0; | |
} | |
int main(int argc, char **argv) | |
{ | |
getgatewayandiface(); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <errno.h> | |
#include <fcntl.h> | |
#include <ctype.h> | |
#include <netinet/in.h> | |
#include <arpa/inet.h> | |
#include <string.h> | |
#include <net/if.h> | |
#define BUFFER_SIZE 4096 | |
int getgatewayandiface(in_addr_t * addr, char *interface) | |
{ | |
long destination, gateway; | |
char iface[IF_NAMESIZE]; | |
char buf[BUFFER_SIZE]; | |
FILE * file; | |
memset(iface, 0, sizeof(iface)); | |
memset(buf, 0, sizeof(buf)); | |
file = fopen("/proc/net/route", "r"); | |
if (!file) | |
return -1; | |
while (fgets(buf, sizeof(buf), file)) { | |
if (sscanf(buf, "%s %lx %lx", iface, &destination, &gateway) == 3) { | |
if (destination == 0) { /* default */ | |
*addr = gateway; | |
strcpy(interface, iface); | |
fclose(file); | |
return 0; | |
} | |
} | |
} | |
/* default route not found */ | |
if (file) | |
fclose(file); | |
return -1; | |
} | |
int main(int argc, char **argv) | |
{ | |
in_addr_t addr = 0; | |
char iface[IF_NAMESIZE]; | |
memset(iface, 0, sizeof(iface)); | |
getgatewayandiface(&addr, iface); | |
printf("%s\n", inet_ntoa(*(struct in_addr *) &addr)); | |
printf("%s\n", iface); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. |
Why making an even worse license that's compatible with just gplv3?
We need it for a gplv2+ project.
Changed to MIT License, I hope this works for everyone and we can all move on with our lives 😌
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Changed to Apache 2.0 license. I hope this suits your needs.