From 50c8809d51d39c51034d610b49f6eddc345fdb7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Tue, 26 Sep 2023 00:12:40 +0800 Subject: [PATCH] LocalIp (Linux): add ioCounter detection --- src/detection/localip/localip_linux.c | 55 +++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/src/detection/localip/localip_linux.c b/src/detection/localip/localip_linux.c index e70234735..a94a83c95 100644 --- a/src/detection/localip/localip_linux.c +++ b/src/detection/localip/localip_linux.c @@ -228,6 +228,61 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results) if (ifAddrStruct) freeifaddrs(ifAddrStruct); + #ifdef __linux__ + { + FF_STRBUF_AUTO_DESTROY path = ffStrbufCreateS("/sys/class/net/"); + uint32_t baseLen = path.length; + + FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate(); + FF_LIST_FOR_EACH(FFLocalIpResult, ip, *results) + { + ffStrbufAppend(&path, &ip->name); + ffStrbufAppendS(&path, "/statistics/"); + uint32_t statLen = path.length; + + ffStrbufAppendS(&path, "rx_bytes"); + if (ffReadFileBuffer(path.chars, &buffer)) + ip->ioCounters.rxBytes = ffStrbufToUInt(&buffer, 0); + ffStrbufSubstrBefore(&path, statLen); + + ffStrbufAppendS(&path, "tx_bytes"); + if (ffReadFileBuffer(path.chars, &buffer)) + ip->ioCounters.txBytes = ffStrbufToUInt(&buffer, 0); + ffStrbufSubstrBefore(&path, statLen); + + ffStrbufAppendS(&path, "rx_packets"); + if (ffReadFileBuffer(path.chars, &buffer)) + ip->ioCounters.rxPackets = ffStrbufToUInt(&buffer, 0); + ffStrbufSubstrBefore(&path, statLen); + + ffStrbufAppendS(&path, "tx_packets"); + if (ffReadFileBuffer(path.chars, &buffer)) + ip->ioCounters.txPackets = ffStrbufToUInt(&buffer, 0); + ffStrbufSubstrBefore(&path, statLen); + + ffStrbufAppendS(&path, "rx_errors"); + if (ffReadFileBuffer(path.chars, &buffer)) + ip->ioCounters.rxErrors = ffStrbufToUInt(&buffer, 0); + ffStrbufSubstrBefore(&path, statLen); + + ffStrbufAppendS(&path, "tx_errors"); + if (ffReadFileBuffer(path.chars, &buffer)) + ip->ioCounters.txErrors = ffStrbufToUInt(&buffer, 0); + ffStrbufSubstrBefore(&path, statLen); + + ffStrbufAppendS(&path, "rx_dropped"); + if (ffReadFileBuffer(path.chars, &buffer)) + ip->ioCounters.rxDrops = ffStrbufToUInt(&buffer, 0); + ffStrbufSubstrBefore(&path, statLen); + + ffStrbufAppendS(&path, "tx_dropped"); + if (ffReadFileBuffer(path.chars, &buffer)) + ip->ioCounters.txDrops = ffStrbufToUInt(&buffer, 0); + ffStrbufSubstrBefore(&path, baseLen); + } + } + #endif + char iface[16 /*IF_NAMESIZE*/ + 1]; if (getDefaultRoute(iface)) {