Skip to content

Commit

Permalink
LocalIP: make link speed formattable
Browse files Browse the repository at this point in the history
Fix #1209
  • Loading branch information
CarterLi committed Aug 24, 2024
1 parent c8d85e0 commit ce7ecd3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/detection/localip/localip_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand Down
12 changes: 11 additions & 1 deletion src/modules/localip/localip.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -111,13 +111,22 @@ 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"},
{FF_FORMAT_ARG_TYPE_STRBUF, &ip->mac, "mac"},
{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;
Expand Down Expand Up @@ -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",
}));
}

Expand Down

0 comments on commit ce7ecd3

Please sign in to comment.