diff --git a/CHANGELOG.md b/CHANGELOG.md index 20a6fbf7f..930f7a4fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ Features: * Add option `--size-ndigits` and `--size-max-prefix` (#494) * Add option `--processing-timeout` to the timeout when waiting for child processes. * Public IP module prints the IP location if `--publicip-url` is not set (PublicIP) +* Add option `--localip-default-route-only` (LocalIP) Bugfixes: * Fix possible hanging (TerminalFont, #493) diff --git a/doc/json_schema.json b/doc/json_schema.json index 6391b2b79..025437c64 100644 --- a/doc/json_schema.json +++ b/doc/json_schema.json @@ -859,6 +859,11 @@ "title": "Show IPs with given name prefix only", "type": "string" }, + "showDefaultRouteOnly": { + "title": "Show ips that are used for default routing only", + "type": "boolean", + "default": false + }, "key": { "$ref": "#/$defs/key" }, diff --git a/src/data/help.txt b/src/data/help.txt index fae9abc0a..f7985f03e 100644 --- a/src/data/help.txt +++ b/src/data/help.txt @@ -131,6 +131,7 @@ Module specific options: --localip-show-mac : Show mac addresses in local ip module. Default is false --localip-show-loop : Show loop back addresses (127.0.0.1) in local ip module. Default is false --localip-name-prefix : Show IPs with given name prefix only. Default is empty + --localip-default-route-only : Show ips that are used for default routing only. Default is false --localip-compact : Show all IPs in one line. Default is false --publicip-timeout: Time in milliseconds to wait for the public ip server to respond. Default is disabled (0) --publicip-url: The URL of public IP detection server to be used. diff --git a/src/modules/localip/localip.c b/src/modules/localip/localip.c index 1ae61b4cc..d3aeb3cba 100644 --- a/src/modules/localip/localip.c +++ b/src/modules/localip/localip.c @@ -80,10 +80,17 @@ void ffPrintLocalIp(FFLocalIpOptions* options) { ffPrintLogoAndKey(FF_LOCALIP_DISPLAY_NAME, 0, &options->moduleArgs.key, &options->moduleArgs.keyColor); + bool flag = false; + FF_LIST_FOR_EACH(FFLocalIpResult, ip, results) { - if ((void*) ip != (void*) results.data) + if (options->defaultRouteOnly && !ip->defaultRoute) + continue; + + if (flag) fputs(" - ", stdout); + else + flag = true; printIp(ip, false); } putchar('\n'); @@ -94,11 +101,14 @@ void ffPrintLocalIp(FFLocalIpOptions* options) FF_LIST_FOR_EACH(FFLocalIpResult, ip, results) { + if (options->defaultRouteOnly && !ip->defaultRoute) + continue; + formatKey(options, ip, &key); if(options->moduleArgs.outputFormat.length == 0) { ffPrintLogoAndKey(key.chars, 0, NULL, &options->moduleArgs.keyColor); - printIp(ip, results.length > 1); + printIp(ip, !options->defaultRouteOnly); putchar('\n'); } else @@ -130,6 +140,7 @@ void ffInitLocalIpOptions(FFLocalIpOptions* options) options->showType = FF_LOCALIP_TYPE_IPV4_BIT; ffStrbufInit(&options->namePrefix); + options->defaultRouteOnly = false; } bool ffParseLocalIpCommandOptions(FFLocalIpOptions* options, const char* key, const char* value) @@ -190,6 +201,12 @@ bool ffParseLocalIpCommandOptions(FFLocalIpOptions* options, const char* key, co return true; } + if (ffStrEqualsIgnCase(subKey, "default-route-only")) + { + options->defaultRouteOnly = ffOptionParseBoolean(value); + return true; + } + return false; } @@ -268,6 +285,12 @@ void ffParseLocalIpJsonObject(yyjson_val* module) continue; } + if (ffStrEqualsIgnCase(key, "defaultRouteOnly")) + { + options.defaultRouteOnly = yyjson_get_bool(val); + continue; + } + ffPrintError(FF_LOCALIP_MODULE_NAME, 0, &options.moduleArgs, "Unknown JSON key %s", key); } } diff --git a/src/modules/localip/option.h b/src/modules/localip/option.h index 65dcfc60d..8d58b8235 100644 --- a/src/modules/localip/option.h +++ b/src/modules/localip/option.h @@ -22,4 +22,5 @@ typedef struct FFLocalIpOptions FFLocalIpType showType; FFstrbuf namePrefix; + bool defaultRouteOnly; } FFLocalIpOptions;