diff --git a/doc/json_schema.json b/doc/json_schema.json index bc436f26c..363497e30 100644 --- a/doc/json_schema.json +++ b/doc/json_schema.json @@ -1438,6 +1438,11 @@ "type": "boolean", "default": false }, + "showPrefixLen": { + "description": "Show network prefix length (/N)", + "type": "boolean", + "default": true + }, "compact": { "description": "Show all IPs in one line", "type": "boolean", diff --git a/src/data/help.json b/src/data/help.json index 621d2d71e..b50980800 100644 --- a/src/data/help.json +++ b/src/data/help.json @@ -1261,6 +1261,15 @@ "default": false } }, + { + "long": "localip-show-prefix-len", + "desc": "Show network prefix length (/N) in local ip module", + "arg": { + "type": "bool", + "optional": true, + "default": true + } + }, { "long": "localip-name-prefix", "desc": "Show interfaces with given interface name prefix only", diff --git a/src/detection/localip/localip_linux.c b/src/detection/localip/localip_linux.c index e5ade13a3..92b803bb0 100644 --- a/src/detection/localip/localip_linux.c +++ b/src/detection/localip/localip_linux.c @@ -85,12 +85,15 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results) char addressBuffer[INET_ADDRSTRLEN + 4]; inet_ntop(AF_INET, &ipv4->sin_addr, addressBuffer, INET_ADDRSTRLEN); - struct sockaddr_in* netmask = (struct sockaddr_in*) ifa->ifa_netmask; - int cidr = __builtin_popcount(netmask->sin_addr.s_addr); - if (cidr != 0) + if (options->showType & FF_LOCALIP_TYPE_PREFIX_LEN_BIT) { - size_t len = strlen(addressBuffer); - snprintf(addressBuffer + len, 4, "/%d", cidr); + struct sockaddr_in* netmask = (struct sockaddr_in*) ifa->ifa_netmask; + int cidr = __builtin_popcount(netmask->sin_addr.s_addr); + if (cidr != 0) + { + size_t len = strlen(addressBuffer); + snprintf(addressBuffer + len, 4, "/%d", cidr); + } } addNewIp(results, ifa->ifa_name, addressBuffer, AF_INET, isDefaultRoute); @@ -104,14 +107,17 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results) char addressBuffer[INET6_ADDRSTRLEN + 4]; inet_ntop(AF_INET6, &ipv6->sin6_addr, addressBuffer, INET6_ADDRSTRLEN); - struct sockaddr_in6* netmask = (struct sockaddr_in6*) ifa->ifa_netmask; - int cidr = 0; - for (uint32_t i = 0; i < sizeof(netmask->sin6_addr.s6_addr32) / sizeof(netmask->sin6_addr.s6_addr32[0]); ++i) - cidr += __builtin_popcount(netmask->sin6_addr.s6_addr32[i]); - if (cidr != 0) + if (options->showType & FF_LOCALIP_TYPE_PREFIX_LEN_BIT) { - size_t len = strlen(addressBuffer); - snprintf(addressBuffer + len, 4, "/%d", cidr); + struct sockaddr_in6* netmask = (struct sockaddr_in6*) ifa->ifa_netmask; + int cidr = 0; + for (uint32_t i = 0; i < sizeof(netmask->sin6_addr.s6_addr32) / sizeof(netmask->sin6_addr.s6_addr32[0]); ++i) + cidr += __builtin_popcount(netmask->sin6_addr.s6_addr32[i]); + if (cidr != 0) + { + size_t len = strlen(addressBuffer); + snprintf(addressBuffer + len, 4, "/%d", cidr); + } } addNewIp(results, ifa->ifa_name, addressBuffer, AF_INET6, isDefaultRoute); diff --git a/src/detection/localip/localip_windows.c b/src/detection/localip/localip_windows.c index f64e8c300..96463c331 100644 --- a/src/detection/localip/localip_windows.c +++ b/src/detection/localip/localip_windows.c @@ -110,7 +110,7 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results) char addressBuffer[INET_ADDRSTRLEN + 4]; inet_ntop(AF_INET, &ipv4->sin_addr, addressBuffer, INET_ADDRSTRLEN); - if (ifa->OnLinkPrefixLength) + if ((options->showType & FF_LOCALIP_TYPE_PREFIX_LEN_BIT) && ifa->OnLinkPrefixLength) { size_t len = strlen(addressBuffer); snprintf(addressBuffer + len, 4, "/%u", (unsigned) ifa->OnLinkPrefixLength); @@ -125,7 +125,7 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results) char addressBuffer[INET6_ADDRSTRLEN + 4]; inet_ntop(AF_INET6, &ipv6->sin6_addr, addressBuffer, INET6_ADDRSTRLEN); - if (ifa->OnLinkPrefixLength) + if ((options->showType & FF_LOCALIP_TYPE_PREFIX_LEN_BIT) && ifa->OnLinkPrefixLength) { size_t len = strlen(addressBuffer); snprintf(addressBuffer + len, 4, "/%u", (unsigned) ifa->OnLinkPrefixLength); diff --git a/src/modules/localip/localip.c b/src/modules/localip/localip.c index 4541154e1..f301c7e9d 100644 --- a/src/modules/localip/localip.c +++ b/src/modules/localip/localip.c @@ -174,6 +174,15 @@ bool ffParseLocalIpCommandOptions(FFLocalIpOptions* options, const char* key, co return true; } + if (ffStrEqualsIgnCase(subKey, "show-prefix-len")) + { + if (ffOptionParseBoolean(value)) + options->showType |= FF_LOCALIP_TYPE_PREFIX_LEN_BIT; + else + options->showType &= ~FF_LOCALIP_TYPE_PREFIX_LEN_BIT; + return true; + } + if(ffStrEqualsIgnCase(subKey, "compact")) { if (ffOptionParseBoolean(value)) @@ -247,6 +256,15 @@ void ffParseLocalIpJsonObject(FFLocalIpOptions* options, yyjson_val* module) continue; } + if (ffStrEqualsIgnCase(key, "showPrefixLen")) + { + if (yyjson_get_bool(val)) + options->showType |= FF_LOCALIP_TYPE_PREFIX_LEN_BIT; + else + options->showType &= ~FF_LOCALIP_TYPE_PREFIX_LEN_BIT; + continue; + } + if (ffStrEqualsIgnCase(key, "compact")) { if (yyjson_get_bool(val)) @@ -293,6 +311,9 @@ void ffGenerateLocalIpJsonConfig(FFLocalIpOptions* options, yyjson_mut_doc* doc, if (options->showType & FF_LOCALIP_TYPE_LOOP_BIT) yyjson_mut_obj_add_bool(doc, module, "showLoop", true); + if (options->showType & FF_LOCALIP_TYPE_PREFIX_LEN_BIT) + yyjson_mut_obj_add_bool(doc, module, "showPrefixLen", true); + if (options->showType & FF_LOCALIP_TYPE_COMPACT_BIT) yyjson_mut_obj_add_bool(doc, module, "compact", true); } @@ -369,7 +390,7 @@ void ffInitLocalIpOptions(FFLocalIpOptions* options) ); ffOptionInitModuleArg(&options->moduleArgs); - options->showType = FF_LOCALIP_TYPE_IPV4_BIT; + options->showType = FF_LOCALIP_TYPE_IPV4_BIT | FF_LOCALIP_TYPE_PREFIX_LEN_BIT; ffStrbufInit(&options->namePrefix); options->defaultRouteOnly = #ifdef __ANDROID__ diff --git a/src/modules/localip/option.h b/src/modules/localip/option.h index e2bdda0e3..56092e98d 100644 --- a/src/modules/localip/option.h +++ b/src/modules/localip/option.h @@ -7,12 +7,13 @@ typedef enum FFLocalIpType { FF_LOCALIP_TYPE_NONE, - FF_LOCALIP_TYPE_LOOP_BIT = 1 << 0, - FF_LOCALIP_TYPE_IPV4_BIT = 1 << 1, - FF_LOCALIP_TYPE_IPV6_BIT = 1 << 2, - FF_LOCALIP_TYPE_MAC_BIT = 1 << 3, + FF_LOCALIP_TYPE_LOOP_BIT = 1 << 0, + FF_LOCALIP_TYPE_IPV4_BIT = 1 << 1, + FF_LOCALIP_TYPE_IPV6_BIT = 1 << 2, + FF_LOCALIP_TYPE_MAC_BIT = 1 << 3, + FF_LOCALIP_TYPE_PREFIX_LEN_BIT = 1 << 4, - FF_LOCALIP_TYPE_COMPACT_BIT = 1 << 10, + FF_LOCALIP_TYPE_COMPACT_BIT = 1 << 10, } FFLocalIpType; typedef struct FFLocalIpOptions