mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-11-04 12:14:19 +01:00
NetUsage: finish module
This commit is contained in:
@@ -2,31 +2,33 @@
|
||||
|
||||
#include "common/time.h"
|
||||
|
||||
const char* ffNetUsageGetIoCounters(FFlist* result);
|
||||
const char* ffNetUsageGetIoCounters(FFlist* result, FFNetUsageOptions* options);
|
||||
|
||||
static FFlist ioCounters1;
|
||||
static uint64_t time1;
|
||||
|
||||
void ffPrepareNetUsage(void)
|
||||
void ffPrepareNetUsage(FFNetUsageOptions* options)
|
||||
{
|
||||
ffListInit(&ioCounters1, sizeof(FFNetUsageIoCounters));
|
||||
ffNetUsageGetIoCounters(&ioCounters1);
|
||||
ffNetUsageGetIoCounters(&ioCounters1, options);
|
||||
time1 = ffTimeGetNow();
|
||||
}
|
||||
|
||||
const char* ffDetectNetUsage(FFlist* result)
|
||||
const char* ffDetectNetUsage(FFlist* result, FFNetUsageOptions* options)
|
||||
{
|
||||
const char* error = NULL;
|
||||
if (time1 == 0)
|
||||
{
|
||||
ffListInit(&ioCounters1, sizeof(FFNetUsageIoCounters));
|
||||
error = ffNetUsageGetIoCounters(&ioCounters1);
|
||||
error = ffNetUsageGetIoCounters(&ioCounters1, options);
|
||||
if (error)
|
||||
return error;
|
||||
time1 = ffTimeGetNow();
|
||||
ffTimeSleep(1000);
|
||||
}
|
||||
|
||||
if (ioCounters1.length == 0)
|
||||
return "No network interfaces found";
|
||||
|
||||
uint64_t time2 = ffTimeGetNow();
|
||||
while (time2 - time1 < 1000)
|
||||
{
|
||||
@@ -34,7 +36,7 @@ const char* ffDetectNetUsage(FFlist* result)
|
||||
time2 = ffTimeGetNow();
|
||||
}
|
||||
|
||||
error = ffNetUsageGetIoCounters(result);
|
||||
error = ffNetUsageGetIoCounters(result, options);
|
||||
if (error)
|
||||
return error;
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
typedef struct FFNetUsageIoCounters
|
||||
{
|
||||
FFstrbuf name;
|
||||
bool defaultRoute;
|
||||
uint64_t txBytes;
|
||||
uint64_t rxBytes;
|
||||
uint64_t txPackets;
|
||||
@@ -13,4 +14,4 @@ typedef struct FFNetUsageIoCounters
|
||||
uint64_t txDrops;
|
||||
} FFNetUsageIoCounters;
|
||||
|
||||
const char* ffDetectNetUsage(FFlist* result);
|
||||
const char* ffDetectNetUsage(FFlist* result, FFNetUsageOptions* options);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "netusage.h"
|
||||
|
||||
#include "common/io/io.h"
|
||||
#include "common/netif/netif.h"
|
||||
#include "util/mallocHelper.h"
|
||||
|
||||
#include <net/if.h>
|
||||
@@ -10,7 +11,7 @@
|
||||
#include <sys/sysctl.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
const char* ffNetUsageGetIoCounters(FFlist* result)
|
||||
const char* ffNetUsageGetIoCounters(FFlist* result, FFNetUsageOptions* options)
|
||||
{
|
||||
size_t bufSize = 0;
|
||||
if (sysctl((int[]) { CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST2, 0 }, 6, NULL, &bufSize, 0, 0) < 0)
|
||||
@@ -20,6 +21,8 @@ const char* ffNetUsageGetIoCounters(FFlist* result)
|
||||
if (sysctl((int[]) { CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST2, 0 }, 6, buf, &bufSize, 0, 0) < 0)
|
||||
return "sysctl({ CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST2, 0 }, 6, buf, &bufSize, 0, 0) failed";
|
||||
|
||||
const char* defaultRouteIface = ffNetifGetDefaultRoute();
|
||||
|
||||
for (struct if_msghdr2* ifm = buf;
|
||||
ifm < (struct if_msghdr2*) ((uint8_t*) buf + bufSize);
|
||||
ifm = (struct if_msghdr2*) ((uint8_t*) ifm + ifm->ifm_msglen))
|
||||
@@ -29,9 +32,19 @@ const char* ffNetUsageGetIoCounters(FFlist* result)
|
||||
struct sockaddr_dl* sdl = (struct sockaddr_dl*) (ifm + 1);
|
||||
assert(sdl->sdl_family == AF_LINK);
|
||||
if (sdl->sdl_type != IFT_ETHER && !(ifm->ifm_flags & IFF_LOOPBACK)) continue;
|
||||
FFNetUsageIoCounters* counters = (FFNetUsageIoCounters*) ffListAdd(result);
|
||||
|
||||
sdl->sdl_data[sdl->sdl_nlen] = 0;
|
||||
|
||||
bool isDefaultRoute = ffStrEquals(sdl->sdl_data, defaultRouteIface);
|
||||
if(options->defaultRouteOnly && !isDefaultRoute)
|
||||
continue;
|
||||
|
||||
if (options->namePrefix.length && strncmp(sdl->sdl_data, options->namePrefix.chars, options->namePrefix.length) != 0)
|
||||
continue;
|
||||
|
||||
FFNetUsageIoCounters* counters = (FFNetUsageIoCounters*) ffListAdd(result);
|
||||
*counters = (FFNetUsageIoCounters) {
|
||||
.name = ffStrbufCreateNS(sdl->sdl_nlen, sdl->sdl_data),
|
||||
.txBytes = ifm->ifm_data.ifi_obytes,
|
||||
.rxBytes = ifm->ifm_data.ifi_ibytes,
|
||||
.txPackets = ifm->ifm_data.ifi_opackets,
|
||||
@@ -40,9 +53,8 @@ const char* ffNetUsageGetIoCounters(FFlist* result)
|
||||
.rxErrors = ifm->ifm_data.ifi_ierrors,
|
||||
.txDrops = 0, // unsupported
|
||||
.rxDrops = ifm->ifm_data.ifi_iqdrops,
|
||||
.defaultRoute = isDefaultRoute,
|
||||
};
|
||||
|
||||
ffStrbufInitNS(&counters->name, sdl->sdl_nlen, sdl->sdl_data);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
#include "netusage.h"
|
||||
|
||||
#include "common/io/io.h"
|
||||
#include "common/netif/netif.h"
|
||||
#include "util/stringUtils.h"
|
||||
|
||||
#include <net/if.h>
|
||||
|
||||
const char* ffNetUsageGetIoCounters(FFlist* result)
|
||||
const char* ffNetUsageGetIoCounters(FFlist* result, FFNetUsageOptions* options)
|
||||
{
|
||||
FF_AUTO_CLOSE_DIR DIR* dirp = opendir("/sys/class/net");
|
||||
if (!dirp) return "opendir(\"/sys/class/net\") == NULL";
|
||||
@@ -12,20 +14,30 @@ const char* ffNetUsageGetIoCounters(FFlist* result)
|
||||
FF_STRBUF_AUTO_DESTROY path = ffStrbufCreateA(64);
|
||||
FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate();
|
||||
|
||||
const char* defaultRouteIface = ffNetifGetDefaultRoute();
|
||||
|
||||
struct dirent* entry;
|
||||
while((entry = readdir(dirp)) != NULL)
|
||||
{
|
||||
if(entry->d_name[0] == '.')
|
||||
continue;
|
||||
|
||||
bool isDefaultRoute = ffStrEquals(entry->d_name, defaultRouteIface);
|
||||
if(options->defaultRouteOnly && !isDefaultRoute)
|
||||
continue;
|
||||
|
||||
if (options->namePrefix.length && strncmp(entry->d_name, options->namePrefix.chars, options->namePrefix.length) != 0)
|
||||
continue;
|
||||
|
||||
ffStrbufSetF(&path, "/sys/class/net/%s/operstate", entry->d_name);
|
||||
if(!ffReadFileBuffer(path.chars, &buffer) || !ffStrbufEqualS(&buffer, "up"))
|
||||
continue;
|
||||
|
||||
FFNetUsageIoCounters* counters = (FFNetUsageIoCounters*) ffListAdd(result);
|
||||
ffStrbufInitS(&counters->name, entry->d_name);
|
||||
counters->defaultRoute = isDefaultRoute;
|
||||
|
||||
ffStrbufSetF(&path, "/sys/class/net/%s/statistics/");
|
||||
ffStrbufSetF(&path, "/sys/class/net/%s/statistics/", entry->d_name);
|
||||
uint32_t statLen = path.length;
|
||||
|
||||
ffStrbufAppendS(&path, "rx_bytes");
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
#include "netusage.h"
|
||||
|
||||
#include "common/io/io.h"
|
||||
#include "common/netif/netif.h"
|
||||
#include "util/mallocHelper.h"
|
||||
#include "util/windows/unicode.h"
|
||||
|
||||
#include <ws2tcpip.h>
|
||||
#include <iphlpapi.h>
|
||||
|
||||
const char* ffNetUsageGetIoCounters(FFlist* result)
|
||||
const char* ffNetUsageGetIoCounters(FFlist* result, FFNetUsageOptions* options)
|
||||
{
|
||||
IP_ADAPTER_ADDRESSES* FF_AUTO_FREE adapter_addresses = NULL;
|
||||
|
||||
@@ -37,14 +37,26 @@ const char* ffNetUsageGetIoCounters(FFlist* result)
|
||||
return "GetAdaptersAddresses() failed";
|
||||
}
|
||||
|
||||
uint32_t defaultRouteIfIndex = ffNetifGetDefaultRoute();
|
||||
|
||||
// Iterate through all of the adapters
|
||||
for (IP_ADAPTER_ADDRESSES* adapter = adapter_addresses; adapter; adapter = adapter->Next)
|
||||
{
|
||||
bool isDefaultRoute = adapter->IfIndex == defaultRouteIfIndex;
|
||||
if (options->defaultRouteOnly && !isDefaultRoute)
|
||||
continue;
|
||||
|
||||
char name[128];
|
||||
WideCharToMultiByte(CP_UTF8, 0, adapter->FriendlyName, -1, name, sizeof(name), NULL, NULL);
|
||||
if (options->namePrefix.length && strncmp(name, options->namePrefix.chars, options->namePrefix.length) != 0)
|
||||
continue;
|
||||
|
||||
MIB_IF_ROW2 ifRow = { .InterfaceIndex = adapter->IfIndex };
|
||||
if (GetIfEntry2(&ifRow) == NO_ERROR)
|
||||
{
|
||||
FFNetUsageIoCounters* counters = (FFNetUsageIoCounters*) ffListAdd(result);
|
||||
*counters = (FFNetUsageIoCounters) {
|
||||
.name = ffStrbufCreateS(name),
|
||||
.txBytes = ifRow.OutOctets,
|
||||
.rxBytes = ifRow.InOctets,
|
||||
.txPackets = (ifRow.OutUcastPkts + ifRow.OutNUcastPkts),
|
||||
@@ -53,8 +65,8 @@ const char* ffNetUsageGetIoCounters(FFlist* result)
|
||||
.txErrors = ifRow.OutErrors,
|
||||
.rxDrops = ifRow.InDiscards,
|
||||
.txDrops = ifRow.OutDiscards,
|
||||
.defaultRoute = isDefaultRoute,
|
||||
};
|
||||
ffStrbufSetWS(&counters->name, adapter->FriendlyName);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user