From c4dce1d888af723628ee0152d85c8222d3b6afbe Mon Sep 17 00:00:00 2001 From: yanxiaojia Date: Thu, 24 Aug 2023 14:53:02 +0800 Subject: [PATCH 1/4] Complete pnal_get_gateway function read the actual deafult gateway on linux --- src/ports/linux/pnal.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/ports/linux/pnal.c b/src/ports/linux/pnal.c index e7d48c986..ff7db7cdd 100644 --- a/src/ports/linux/pnal.c +++ b/src/ports/linux/pnal.c @@ -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 gateway->sin_addr.s_addr; } int pnal_get_hostname (char * hostname) From e1c5f3b13ced2ecce12bde65f78901d5378db8ef Mon Sep 17 00:00:00 2001 From: yanxiaojia Date: Thu, 24 Aug 2023 17:08:46 +0800 Subject: [PATCH 2/4] Change readed gateway to net endian --- src/ports/linux/pnal.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ports/linux/pnal.c b/src/ports/linux/pnal.c index ff7db7cdd..b28cc1d83 100644 --- a/src/ports/linux/pnal.c +++ b/src/ports/linux/pnal.c @@ -579,7 +579,7 @@ pnal_ipaddr_t pnal_get_gateway (const char * interface_name) close(sockfd); struct sockaddr_in *gateway = (struct sockaddr_in *)&ifr.ifr_dstaddr; - return gateway->sin_addr.s_addr; + return ntohl(gateway->sin_addr.s_addr); } int pnal_get_hostname (char * hostname) From b3b2a51af0780d0e745e73bde5eab929f4dd3b8c Mon Sep 17 00:00:00 2001 From: yanxiaojia Date: Mon, 28 Aug 2023 14:37:52 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E7=BD=91=E5=85=B3=E7=9A=84=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ports/linux/pnal.c | 43 ++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/src/ports/linux/pnal.c b/src/ports/linux/pnal.c index b28cc1d83..f159d4052 100644 --- a/src/ports/linux/pnal.c +++ b/src/ports/linux/pnal.c @@ -560,26 +560,41 @@ pnal_ipaddr_t pnal_get_netmask (const char * interface_name) pnal_ipaddr_t pnal_get_gateway (const char * interface_name) { - int sockfd = socket(AF_INET, SOCK_DGRAM, 0); - if (sockfd < 0) + char line[1024]; + + // Open the route file + FILE* fp = fopen("/proc/net/route", "r"); + if (fp == NULL) { - return PNAL_IPADDR_INVALID; + return 0; } - struct ifreq ifr; - memset(&ifr, 0, sizeof(ifr)); - strncpy(ifr.ifr_name, interface_name, IFNAMSIZ - 1); - - if (ioctl(sockfd, SIOCGIFDSTADDR, &ifr) < 0) + // Skip the first line (header) + if(fgets(line, sizeof(line), fp) == NULL) { - close(sockfd); - return PNAL_IPADDR_INVALID; + return 0; } - close(sockfd); - - struct sockaddr_in *gateway = (struct sockaddr_in *)&ifr.ifr_dstaddr; - return ntohl(gateway->sin_addr.s_addr); + // 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) + { + return htonl(gateway); + } + } + } + fclose(fp); + return 0; } int pnal_get_hostname (char * hostname) From 318eca9816c230e4385a0ce077c33a21af06acd5 Mon Sep 17 00:00:00 2001 From: yanxiaojia Date: Tue, 29 Aug 2023 11:50:23 +0800 Subject: [PATCH 4/4] Fix file not close and use PNAL_IPADDR_INVALID replace 0 --- src/ports/linux/pnal.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/ports/linux/pnal.c b/src/ports/linux/pnal.c index f159d4052..d02535ad1 100644 --- a/src/ports/linux/pnal.c +++ b/src/ports/linux/pnal.c @@ -566,13 +566,14 @@ pnal_ipaddr_t pnal_get_gateway (const char * interface_name) FILE* fp = fopen("/proc/net/route", "r"); if (fp == NULL) { - return 0; + return PNAL_IPADDR_INVALID; } // Skip the first line (header) if(fgets(line, sizeof(line), fp) == NULL) { - return 0; + fclose(fp); + return PNAL_IPADDR_INVALID; } // Read the route entries @@ -589,12 +590,13 @@ pnal_ipaddr_t pnal_get_gateway (const char * interface_name) { if (!strcmp(iface, interface_name) && gateway != 0) { + fclose(fp); return htonl(gateway); } } } fclose(fp); - return 0; + return PNAL_IPADDR_INVALID; } int pnal_get_hostname (char * hostname)