2023-09-27 00:05:37 +08:00
|
|
|
#include "netio.h"
|
2023-09-26 10:32:21 +08:00
|
|
|
|
|
|
|
|
#include "common/io/io.h"
|
2023-09-26 22:34:37 +08:00
|
|
|
#include "common/netif/netif.h"
|
|
|
|
|
#include "util/stringUtils.h"
|
2023-09-26 10:32:21 +08:00
|
|
|
|
2025-04-14 16:30:02 +08:00
|
|
|
#include <fcntl.h>
|
2023-09-26 10:32:21 +08:00
|
|
|
#include <net/if.h>
|
|
|
|
|
|
2025-04-14 16:30:02 +08:00
|
|
|
static void getData(FFstrbuf* buffer, const char* ifName, bool isDefaultRoute, int basefd, FFlist* result)
|
2023-09-27 10:13:59 +08:00
|
|
|
{
|
2025-04-14 16:30:02 +08:00
|
|
|
FF_AUTO_CLOSE_FD int dfd = openat(basefd, ifName, O_RDONLY | O_DIRECTORY);
|
|
|
|
|
if (dfd < 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
char operstate;
|
|
|
|
|
if(!ffReadFileDataRelative(dfd, "operstate", 1, &operstate) || operstate != 'u' /* up or unknown */)
|
2023-09-27 10:13:59 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
FFNetIOResult* counters = (FFNetIOResult*) ffListAdd(result);
|
|
|
|
|
ffStrbufInitS(&counters->name, ifName);
|
|
|
|
|
counters->defaultRoute = isDefaultRoute;
|
|
|
|
|
|
2025-04-14 16:30:02 +08:00
|
|
|
if (ffReadFileBufferRelative(dfd, "statistics/rx_bytes", buffer))
|
2023-09-27 10:13:59 +08:00
|
|
|
counters->rxBytes = ffStrbufToUInt(buffer, 0);
|
|
|
|
|
|
2025-04-14 16:30:02 +08:00
|
|
|
if (ffReadFileBufferRelative(dfd, "statistics/tx_bytes", buffer))
|
2023-09-27 10:13:59 +08:00
|
|
|
counters->txBytes = ffStrbufToUInt(buffer, 0);
|
|
|
|
|
|
2025-04-14 16:30:02 +08:00
|
|
|
if (ffReadFileBufferRelative(dfd, "statistics/rx_packets", buffer))
|
2023-09-27 10:13:59 +08:00
|
|
|
counters->rxPackets = ffStrbufToUInt(buffer, 0);
|
|
|
|
|
|
2025-04-14 16:30:02 +08:00
|
|
|
if (ffReadFileBufferRelative(dfd, "statistics/tx_packets", buffer))
|
2023-09-27 10:13:59 +08:00
|
|
|
counters->txPackets = ffStrbufToUInt(buffer, 0);
|
|
|
|
|
|
2025-04-14 16:30:02 +08:00
|
|
|
if (ffReadFileBufferRelative(dfd, "statistics/rx_errors", buffer))
|
2023-09-27 10:13:59 +08:00
|
|
|
counters->rxErrors = ffStrbufToUInt(buffer, 0);
|
|
|
|
|
|
2025-04-14 16:30:02 +08:00
|
|
|
if (ffReadFileBufferRelative(dfd, "statistics/tx_errors", buffer))
|
2023-09-27 10:13:59 +08:00
|
|
|
counters->txErrors = ffStrbufToUInt(buffer, 0);
|
|
|
|
|
|
2025-04-14 16:30:02 +08:00
|
|
|
if (ffReadFileBufferRelative(dfd, "statistics/rx_dropped", buffer))
|
2023-09-27 10:13:59 +08:00
|
|
|
counters->rxDrops = ffStrbufToUInt(buffer, 0);
|
|
|
|
|
|
2025-04-14 16:30:02 +08:00
|
|
|
if (ffReadFileBufferRelative(dfd, "statistics/tx_dropped", buffer))
|
2023-09-27 10:13:59 +08:00
|
|
|
counters->txDrops = ffStrbufToUInt(buffer, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-27 00:05:37 +08:00
|
|
|
const char* ffNetIOGetIoCounters(FFlist* result, FFNetIOOptions* options)
|
2023-09-26 10:32:21 +08:00
|
|
|
{
|
|
|
|
|
FF_AUTO_CLOSE_DIR DIR* dirp = opendir("/sys/class/net");
|
|
|
|
|
if (!dirp) return "opendir(\"/sys/class/net\") == NULL";
|
|
|
|
|
|
|
|
|
|
FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate();
|
|
|
|
|
|
2025-08-07 16:35:42 +08:00
|
|
|
const char* defaultRouteIfName = ffNetifGetDefaultRouteV4()->ifName;
|
2023-09-26 22:34:37 +08:00
|
|
|
|
2023-09-27 10:13:59 +08:00
|
|
|
if (options->defaultRouteOnly)
|
|
|
|
|
{
|
|
|
|
|
if (options->namePrefix.length && strncmp(defaultRouteIfName, options->namePrefix.chars, options->namePrefix.length) != 0)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
2025-04-14 16:30:02 +08:00
|
|
|
getData(&buffer, defaultRouteIfName, true, dirfd(dirp), result);
|
2023-09-27 10:13:59 +08:00
|
|
|
}
|
|
|
|
|
else
|
2023-09-26 10:32:21 +08:00
|
|
|
{
|
2023-09-27 10:13:59 +08:00
|
|
|
struct dirent* entry;
|
|
|
|
|
while((entry = readdir(dirp)) != NULL)
|
|
|
|
|
{
|
|
|
|
|
const char* ifName = entry->d_name;
|
|
|
|
|
if(ifName[0] == '.')
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (options->namePrefix.length && strncmp(ifName, options->namePrefix.chars, options->namePrefix.length) != 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
2025-04-14 16:30:02 +08:00
|
|
|
getData(&buffer, ifName, ffStrEquals(ifName, defaultRouteIfName), dirfd(dirp), result);
|
2023-09-27 10:13:59 +08:00
|
|
|
}
|
2023-09-26 10:32:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|