summaryrefslogtreecommitdiffstats
path: root/src/detection/netio/netio_linux.c
diff options
context:
space:
mode:
authorsumuel <samuel@yakubos.org>2026-08-17 20:44:55 +0000
committersumuel <samuel@yakubos.org>2026-08-17 20:44:55 +0000
commit76424950e373d3b04ac3dd13019151bfba3e8423 (patch)
tree4c3cfdbda039e592b9186be3e28f8d7cfd439e8a /src/detection/netio/netio_linux.c
Add the files
Diffstat (limited to 'src/detection/netio/netio_linux.c')
-rw-r--r--src/detection/netio/netio_linux.c91
1 files changed, 91 insertions, 0 deletions
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 <fcntl.h>
+#include <net/if.h>
+
+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;
+}