summaryrefslogtreecommitdiffstats
path: root/src/detection/netio/netio_bsd.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_bsd.c
Add the files
Diffstat (limited to 'src/detection/netio/netio_bsd.c')
-rw-r--r--src/detection/netio/netio_bsd.c62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/detection/netio/netio_bsd.c b/src/detection/netio/netio_bsd.c
new file mode 100644
index 0000000..83c1b6b
--- /dev/null
+++ b/src/detection/netio/netio_bsd.c
@@ -0,0 +1,62 @@
+#include "netio.h"
+
+#include "common/netif.h"
+#include "common/mallocHelper.h"
+
+#include <net/if.h>
+#include <net/if_dl.h>
+#include <net/if_types.h>
+#include <net/route.h>
+#include <sys/sysctl.h>
+#include <sys/socket.h>
+
+const char* ffNetIOGetIoCounters(FFlist* result, FFNetIOOptions* options) {
+ uint32_t defaultRouteIfIndex = ffNetifGetDefaultRouteV4()->ifIndex;
+
+ size_t bufSize = 0;
+ if (sysctl((int[]) { CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST, (options->defaultRouteOnly ? (int) defaultRouteIfIndex : 0) }, 6, NULL, &bufSize, 0, 0) < 0) {
+ return "sysctl({ CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST, ifIndex }, 6, NULL, &bufSize, 0, 0) failed";
+ }
+
+ FF_AUTO_FREE struct if_msghdr* buf = (struct if_msghdr*) malloc(bufSize);
+ if (sysctl((int[]) { CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST, (options->defaultRouteOnly ? (int) defaultRouteIfIndex : 0) }, 6, buf, &bufSize, 0, 0) < 0) {
+ return "sysctl({ CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST, ifIndex }, 6, buf, &bufSize, 0, 0) failed";
+ }
+
+ for (struct if_msghdr* ifm = buf;
+ ifm < (struct if_msghdr*) ((uint8_t*) buf + bufSize);
+ ifm = (struct if_msghdr*) ((uint8_t*) ifm + ifm->ifm_msglen)) {
+ if (ifm->ifm_type != RTM_IFINFO || !(ifm->ifm_flags & IFF_RUNNING) || (ifm->ifm_flags & IFF_NOARP)) {
+ continue;
+ }
+
+ struct sockaddr_dl* sdl = (struct sockaddr_dl*) (ifm + 1);
+ assert(sdl->sdl_family == AF_LINK);
+
+ sdl->sdl_data[sdl->sdl_nlen] = 0;
+
+ if (options->namePrefix.length && strncmp(sdl->sdl_data, options->namePrefix.chars, options->namePrefix.length) != 0) {
+ continue;
+ }
+
+ FFNetIOResult* counters = FF_LIST_ADD(FFNetIOResult, *result);
+ *counters = (FFNetIOResult) {
+ .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,
+ .rxPackets = ifm->ifm_data.ifi_ipackets,
+ .txErrors = ifm->ifm_data.ifi_oerrors,
+ .rxErrors = ifm->ifm_data.ifi_ierrors,
+#ifdef FF_HAVE_IFI_OQDROPS
+ .txDrops = ifm->ifm_data.ifi_oqdrops,
+#else
+ .txDrops = 0, // unsupported
+#endif
+ .rxDrops = ifm->ifm_data.ifi_iqdrops,
+ .defaultRoute = sdl->sdl_index == defaultRouteIfIndex,
+ };
+ }
+
+ return NULL;
+}