Skip to content

Commit a2d9969

Browse files
committed
LocalIP: add new option localip-show-prefix-len
1 parent 3a2786a commit a2d9969

File tree

6 files changed

+62
-20
lines changed

6 files changed

+62
-20
lines changed

doc/json_schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,6 +1438,11 @@
14381438
"type": "boolean",
14391439
"default": false
14401440
},
1441+
"showPrefixLen": {
1442+
"description": "Show network prefix length (/N)",
1443+
"type": "boolean",
1444+
"default": true
1445+
},
14411446
"compact": {
14421447
"description": "Show all IPs in one line",
14431448
"type": "boolean",

src/data/help.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,6 +1261,15 @@
12611261
"default": false
12621262
}
12631263
},
1264+
{
1265+
"long": "localip-show-prefix-len",
1266+
"desc": "Show network prefix length (/N) in local ip module",
1267+
"arg": {
1268+
"type": "bool",
1269+
"optional": true,
1270+
"default": true
1271+
}
1272+
},
12641273
{
12651274
"long": "localip-name-prefix",
12661275
"desc": "Show interfaces with given interface name prefix only",

src/detection/localip/localip_linux.c

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,15 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
8585
char addressBuffer[INET_ADDRSTRLEN + 4];
8686
inet_ntop(AF_INET, &ipv4->sin_addr, addressBuffer, INET_ADDRSTRLEN);
8787

88-
struct sockaddr_in* netmask = (struct sockaddr_in*) ifa->ifa_netmask;
89-
int cidr = __builtin_popcount(netmask->sin_addr.s_addr);
90-
if (cidr != 0)
88+
if (options->showType & FF_LOCALIP_TYPE_PREFIX_LEN_BIT)
9189
{
92-
size_t len = strlen(addressBuffer);
93-
snprintf(addressBuffer + len, 4, "/%d", cidr);
90+
struct sockaddr_in* netmask = (struct sockaddr_in*) ifa->ifa_netmask;
91+
int cidr = __builtin_popcount(netmask->sin_addr.s_addr);
92+
if (cidr != 0)
93+
{
94+
size_t len = strlen(addressBuffer);
95+
snprintf(addressBuffer + len, 4, "/%d", cidr);
96+
}
9497
}
9598

9699
addNewIp(results, ifa->ifa_name, addressBuffer, AF_INET, isDefaultRoute);
@@ -104,14 +107,17 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
104107
char addressBuffer[INET6_ADDRSTRLEN + 4];
105108
inet_ntop(AF_INET6, &ipv6->sin6_addr, addressBuffer, INET6_ADDRSTRLEN);
106109

107-
struct sockaddr_in6* netmask = (struct sockaddr_in6*) ifa->ifa_netmask;
108-
int cidr = 0;
109-
for (uint32_t i = 0; i < sizeof(netmask->sin6_addr.s6_addr32) / sizeof(netmask->sin6_addr.s6_addr32[0]); ++i)
110-
cidr += __builtin_popcount(netmask->sin6_addr.s6_addr32[i]);
111-
if (cidr != 0)
110+
if (options->showType & FF_LOCALIP_TYPE_PREFIX_LEN_BIT)
112111
{
113-
size_t len = strlen(addressBuffer);
114-
snprintf(addressBuffer + len, 4, "/%d", cidr);
112+
struct sockaddr_in6* netmask = (struct sockaddr_in6*) ifa->ifa_netmask;
113+
int cidr = 0;
114+
for (uint32_t i = 0; i < sizeof(netmask->sin6_addr.s6_addr32) / sizeof(netmask->sin6_addr.s6_addr32[0]); ++i)
115+
cidr += __builtin_popcount(netmask->sin6_addr.s6_addr32[i]);
116+
if (cidr != 0)
117+
{
118+
size_t len = strlen(addressBuffer);
119+
snprintf(addressBuffer + len, 4, "/%d", cidr);
120+
}
115121
}
116122

117123
addNewIp(results, ifa->ifa_name, addressBuffer, AF_INET6, isDefaultRoute);

src/detection/localip/localip_windows.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
110110
char addressBuffer[INET_ADDRSTRLEN + 4];
111111
inet_ntop(AF_INET, &ipv4->sin_addr, addressBuffer, INET_ADDRSTRLEN);
112112

113-
if (ifa->OnLinkPrefixLength)
113+
if ((options->showType & FF_LOCALIP_TYPE_PREFIX_LEN_BIT) && ifa->OnLinkPrefixLength)
114114
{
115115
size_t len = strlen(addressBuffer);
116116
snprintf(addressBuffer + len, 4, "/%u", (unsigned) ifa->OnLinkPrefixLength);
@@ -125,7 +125,7 @@ const char* ffDetectLocalIps(const FFLocalIpOptions* options, FFlist* results)
125125
char addressBuffer[INET6_ADDRSTRLEN + 4];
126126
inet_ntop(AF_INET6, &ipv6->sin6_addr, addressBuffer, INET6_ADDRSTRLEN);
127127

128-
if (ifa->OnLinkPrefixLength)
128+
if ((options->showType & FF_LOCALIP_TYPE_PREFIX_LEN_BIT) && ifa->OnLinkPrefixLength)
129129
{
130130
size_t len = strlen(addressBuffer);
131131
snprintf(addressBuffer + len, 4, "/%u", (unsigned) ifa->OnLinkPrefixLength);

src/modules/localip/localip.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,15 @@ bool ffParseLocalIpCommandOptions(FFLocalIpOptions* options, const char* key, co
174174
return true;
175175
}
176176

177+
if (ffStrEqualsIgnCase(subKey, "show-prefix-len"))
178+
{
179+
if (ffOptionParseBoolean(value))
180+
options->showType |= FF_LOCALIP_TYPE_PREFIX_LEN_BIT;
181+
else
182+
options->showType &= ~FF_LOCALIP_TYPE_PREFIX_LEN_BIT;
183+
return true;
184+
}
185+
177186
if(ffStrEqualsIgnCase(subKey, "compact"))
178187
{
179188
if (ffOptionParseBoolean(value))
@@ -247,6 +256,15 @@ void ffParseLocalIpJsonObject(FFLocalIpOptions* options, yyjson_val* module)
247256
continue;
248257
}
249258

259+
if (ffStrEqualsIgnCase(key, "showPrefixLen"))
260+
{
261+
if (yyjson_get_bool(val))
262+
options->showType |= FF_LOCALIP_TYPE_PREFIX_LEN_BIT;
263+
else
264+
options->showType &= ~FF_LOCALIP_TYPE_PREFIX_LEN_BIT;
265+
continue;
266+
}
267+
250268
if (ffStrEqualsIgnCase(key, "compact"))
251269
{
252270
if (yyjson_get_bool(val))
@@ -293,6 +311,9 @@ void ffGenerateLocalIpJsonConfig(FFLocalIpOptions* options, yyjson_mut_doc* doc,
293311
if (options->showType & FF_LOCALIP_TYPE_LOOP_BIT)
294312
yyjson_mut_obj_add_bool(doc, module, "showLoop", true);
295313

314+
if (options->showType & FF_LOCALIP_TYPE_PREFIX_LEN_BIT)
315+
yyjson_mut_obj_add_bool(doc, module, "showPrefixLen", true);
316+
296317
if (options->showType & FF_LOCALIP_TYPE_COMPACT_BIT)
297318
yyjson_mut_obj_add_bool(doc, module, "compact", true);
298319
}
@@ -369,7 +390,7 @@ void ffInitLocalIpOptions(FFLocalIpOptions* options)
369390
);
370391
ffOptionInitModuleArg(&options->moduleArgs);
371392

372-
options->showType = FF_LOCALIP_TYPE_IPV4_BIT;
393+
options->showType = FF_LOCALIP_TYPE_IPV4_BIT | FF_LOCALIP_TYPE_PREFIX_LEN_BIT;
373394
ffStrbufInit(&options->namePrefix);
374395
options->defaultRouteOnly =
375396
#ifdef __ANDROID__

src/modules/localip/option.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
typedef enum FFLocalIpType
88
{
99
FF_LOCALIP_TYPE_NONE,
10-
FF_LOCALIP_TYPE_LOOP_BIT = 1 << 0,
11-
FF_LOCALIP_TYPE_IPV4_BIT = 1 << 1,
12-
FF_LOCALIP_TYPE_IPV6_BIT = 1 << 2,
13-
FF_LOCALIP_TYPE_MAC_BIT = 1 << 3,
10+
FF_LOCALIP_TYPE_LOOP_BIT = 1 << 0,
11+
FF_LOCALIP_TYPE_IPV4_BIT = 1 << 1,
12+
FF_LOCALIP_TYPE_IPV6_BIT = 1 << 2,
13+
FF_LOCALIP_TYPE_MAC_BIT = 1 << 3,
14+
FF_LOCALIP_TYPE_PREFIX_LEN_BIT = 1 << 4,
1415

15-
FF_LOCALIP_TYPE_COMPACT_BIT = 1 << 10,
16+
FF_LOCALIP_TYPE_COMPACT_BIT = 1 << 10,
1617
} FFLocalIpType;
1718

1819
typedef struct FFLocalIpOptions

0 commit comments

Comments
 (0)