mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-13 02:42:09 +02:00
LocalIP: cache default route value
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
#include "netif.h"
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <net/if.h>
|
||||
|
||||
bool ffNetifGetDefaultRouteImpl(char iface[IF_NAMESIZE + 1]);
|
||||
|
||||
const char* ffNetifGetDefaultRoute()
|
||||
{
|
||||
static char iface[IF_NAMESIZE + 1];
|
||||
|
||||
if (*(uint16_t*) iface == 0)
|
||||
{
|
||||
if (!ffNetifGetDefaultRouteImpl(iface))
|
||||
iface[1] = 1;
|
||||
}
|
||||
|
||||
return iface;
|
||||
}
|
||||
#else
|
||||
bool ffNetifGetDefaultRouteImpl(uint32_t* ifIndex);
|
||||
|
||||
uint32_t ffNetifGetDefaultRoute()
|
||||
{
|
||||
static uint32_t ifIndex = (uint32_t) -1;
|
||||
|
||||
if (ifIndex == (uint32_t) -1)
|
||||
{
|
||||
if (!ffNetifGetDefaultRouteImpl(&ifIndex))
|
||||
ifIndex = (uint32_t) -2;
|
||||
}
|
||||
|
||||
return ifIndex;
|
||||
}
|
||||
#endif
|
||||
@@ -3,7 +3,7 @@
|
||||
#include "fastfetch.h"
|
||||
|
||||
#ifndef _WIN32
|
||||
bool ffNetifGetDefaultRoute(FFstrbuf* iface);
|
||||
const char* ffNetifGetDefaultRoute();
|
||||
#else
|
||||
bool ffNetifGetDefaultRoute(uint32_t* ifIndex);
|
||||
uint32_t ffNetifGetDefaultRoute();
|
||||
#endif
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "common/io/io.h"
|
||||
|
||||
#include <net/if.h>
|
||||
#include <net/if_dl.h>
|
||||
#include <net/route.h>
|
||||
#include <netinet/in.h>
|
||||
@@ -38,9 +39,7 @@ get_rt_address(struct rt_msghdr *rtm, int desired)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool ffNetifGetDefaultRoute(FFstrbuf* iface)
|
||||
bool ffNetifGetDefaultRouteImpl(char iface[IF_NAMESIZE + 1])
|
||||
{
|
||||
//https://github.com/hashPirate/copenheimer-masscan-fork/blob/36f1ed9f7b751a7dccd5ed27874e2e703db7d481/src/rawsock-getif.c#L104
|
||||
|
||||
@@ -86,7 +85,9 @@ bool ffNetifGetDefaultRoute(FFstrbuf* iface)
|
||||
struct sockaddr_dl* sdl = (struct sockaddr_dl *)get_rt_address(&rtmsg.hdr, RTA_IFP);
|
||||
if (sdl)
|
||||
{
|
||||
ffStrbufSetNS(iface, sdl->sdl_nlen, sdl->sdl_data);
|
||||
assert(sdl->sdl_nlen <= IF_NAMESIZE);
|
||||
memcpy(iface, sdl->sdl_data, sdl->sdl_nlen);
|
||||
iface[sdl->sdl_nlen] = '\0';
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
#include "netif.h"
|
||||
#include "common/io/io.h"
|
||||
|
||||
#include <net/if.h>
|
||||
#include <stdio.h>
|
||||
|
||||
bool ffNetifGetDefaultRoute(FFstrbuf* iface)
|
||||
#define FF_STR_INDIR(x) #x
|
||||
#define FF_STR(x) FF_STR_INDIR(x)
|
||||
|
||||
bool ffNetifGetDefaultRouteImpl(char iface[IF_NAMESIZE + 1])
|
||||
{
|
||||
FILE* FF_AUTO_CLOSE_FILE netRoute = fopen("/proc/net/route", "r");
|
||||
if (!netRoute) return false;
|
||||
@@ -14,11 +18,9 @@ bool ffNetifGetDefaultRoute(FFstrbuf* iface)
|
||||
funlockfile(netRoute);
|
||||
unsigned long long destination; //, gateway, flags, refCount, use, metric, mask, mtu,
|
||||
|
||||
char buf[16 /*IF_NAMESIZE*/ + 1];
|
||||
while (fscanf(netRoute, "%16s%llx%*[^\n]", buf, &destination) == 2)
|
||||
while (fscanf(netRoute, "%" FF_STR(IF_NAMESIZE) "s%llx%*[^\n]", iface, &destination) == 2)
|
||||
{
|
||||
if (destination != 0) continue;
|
||||
ffStrbufSetS(iface, buf);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
#include <iphlpapi.h>
|
||||
|
||||
bool ffNetifGetDefaultRoute(uint32_t* ifIndex)
|
||||
bool ffNetifGetDefaultRouteImpl(uint32_t* ifIndex)
|
||||
{
|
||||
ULONG size = 0;
|
||||
if (GetIpForwardTable(NULL, &size, TRUE) != ERROR_INSUFFICIENT_BUFFER)
|
||||
|
||||
Reference in New Issue
Block a user