From ce7ecd3863c10ae59c4ae3608f78e7cc434a9ff3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sat, 24 Aug 2024 14:47:15 +0800 Subject: [PATCH] LocalIP: make link speed formattable Fix #1209 --- src/detection/localip/localip_linux.c | 2 +- src/modules/localip/localip.c | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/detection/localip/localip_linux.c b/src/detection/localip/localip_linux.c index 1dcaf02a5..dda8b1ffd 100644 --- a/src/detection/localip/localip_linux.c +++ b/src/detection/localip/localip_linux.c @@ -185,7 +185,7 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results) #ifdef __linux__ struct ethtool_cmd edata = { .cmd = ETHTOOL_GSET }; - ifr.ifr_data = (__caddr_t) &edata; + ifr.ifr_data = (caddr_t) &edata; if (ioctl(sockfd, SIOCETHTOOL, &ifr) == 0) iface->speed = (int32_t) ethtool_cmd_speed(&edata); #elif __FreeBSD__ || __APPLE__ diff --git a/src/modules/localip/localip.c b/src/modules/localip/localip.c index 318c241d5..2a5db4914 100644 --- a/src/modules/localip/localip.c +++ b/src/modules/localip/localip.c @@ -5,7 +5,7 @@ #include "util/stringUtils.h" #define FF_LOCALIP_DISPLAY_NAME "Local IP" -#define FF_LOCALIP_NUM_FORMAT_ARGS 6 +#define FF_LOCALIP_NUM_FORMAT_ARGS 7 #pragma GCC diagnostic ignored "-Wsign-conversion" static int sortIps(const FFLocalIpResult* left, const FFLocalIpResult* right) @@ -111,6 +111,14 @@ void ffPrintLocalIp(FFLocalIpOptions* options) } else { + FF_STRBUF_AUTO_DESTROY speedStr = ffStrbufCreate(); + if (ip->speed > 0) + { + if (ip->speed >= 1000) + ffStrbufSetF(&speedStr, "%g Gbps", ip->speed / 1000.0); + else + ffStrbufSetF(&speedStr, "%u Mbps", (unsigned) ip->speed); + } FF_PRINT_FORMAT_CHECKED(key.chars, 0, &options->moduleArgs, FF_PRINT_TYPE_NO_CUSTOM_KEY, FF_LOCALIP_NUM_FORMAT_ARGS, ((FFformatarg[]){ {FF_FORMAT_ARG_TYPE_STRBUF, &ip->ipv4, "ipv4"}, {FF_FORMAT_ARG_TYPE_STRBUF, &ip->ipv6, "ipv6"}, @@ -118,6 +126,7 @@ void ffPrintLocalIp(FFLocalIpOptions* options) {FF_FORMAT_ARG_TYPE_STRBUF, &ip->name, "ifname"}, {FF_FORMAT_ARG_TYPE_BOOL, &ip->defaultRoute, "is-default-route"}, {FF_FORMAT_ARG_TYPE_INT, &ip->mtu, "mtu"}, + {FF_FORMAT_ARG_TYPE_STRBUF, &speedStr, "speed"}, })); } ++index; @@ -397,6 +406,7 @@ void ffPrintLocalIpHelpFormat(void) "Interface name - ifname", "Is default route - is-default-route", "MTU size in bytes - mtu", + "Link speed (formatted) - speed", })); }