From 76424950e373d3b04ac3dd13019151bfba3e8423 Mon Sep 17 00:00:00 2001 From: sumuel Date: Mon, 17 Aug 2026 20:44:55 +0000 Subject: Add the files --- src/detection/netio/netio_linux.c | 91 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 src/detection/netio/netio_linux.c (limited to 'src/detection/netio/netio_linux.c') diff --git a/src/detection/netio/netio_linux.c b/src/detection/netio/netio_linux.c new file mode 100644 index 0000000..5870d3f --- /dev/null +++ b/src/detection/netio/netio_linux.c @@ -0,0 +1,91 @@ +#include "netio.h" + +#include "common/io.h" +#include "common/netif.h" +#include "common/strutil.h" + +#include +#include + +static void getData(FFstrbuf* buffer, const char* ifName, bool isDefaultRoute, int basefd, FFlist* result) { + 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 */) { + return; + } + + FFNetIOResult* counters = FF_LIST_ADD(FFNetIOResult, *result); + ffStrbufInitS(&counters->name, ifName); + counters->defaultRoute = isDefaultRoute; + + if (ffReadFileBufferRelative(dfd, "statistics/rx_bytes", buffer)) { + counters->rxBytes = ffStrbufToUInt(buffer, 0); + } + + if (ffReadFileBufferRelative(dfd, "statistics/tx_bytes", buffer)) { + counters->txBytes = ffStrbufToUInt(buffer, 0); + } + + if (ffReadFileBufferRelative(dfd, "statistics/rx_packets", buffer)) { + counters->rxPackets = ffStrbufToUInt(buffer, 0); + } + + if (ffReadFileBufferRelative(dfd, "statistics/tx_packets", buffer)) { + counters->txPackets = ffStrbufToUInt(buffer, 0); + } + + if (ffReadFileBufferRelative(dfd, "statistics/rx_errors", buffer)) { + counters->rxErrors = ffStrbufToUInt(buffer, 0); + } + + if (ffReadFileBufferRelative(dfd, "statistics/tx_errors", buffer)) { + counters->txErrors = ffStrbufToUInt(buffer, 0); + } + + if (ffReadFileBufferRelative(dfd, "statistics/rx_dropped", buffer)) { + counters->rxDrops = ffStrbufToUInt(buffer, 0); + } + + if (ffReadFileBufferRelative(dfd, "statistics/tx_dropped", buffer)) { + counters->txDrops = ffStrbufToUInt(buffer, 0); + } +} + +const char* ffNetIOGetIoCounters(FFlist* result, FFNetIOOptions* options) { + FF_AUTO_CLOSE_DIR DIR* dirp = opendir("/sys/class/net"); + if (!dirp) { + return "opendir(\"/sys/class/net\") == NULL"; + } + + FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate(); + + const char* defaultRouteIfName = ffNetifGetDefaultRouteV4()->ifName; + + if (options->defaultRouteOnly) { + if (options->namePrefix.length && strncmp(defaultRouteIfName, options->namePrefix.chars, options->namePrefix.length) != 0) { + return NULL; + } + + getData(&buffer, defaultRouteIfName, true, dirfd(dirp), result); + } else { + 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; + } + + getData(&buffer, ifName, ffStrEquals(ifName, defaultRouteIfName), dirfd(dirp), result); + } + } + + return NULL; +} -- cgit v1.2.3