]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
src: get rid of nft_ctx_output_{get,set}_numeric()
authorPablo Neira Ayuso <pablo@netfilter.org>
Mon, 29 Oct 2018 15:03:32 +0000 (16:03 +0100)
committerPablo Neira Ayuso <pablo@netfilter.org>
Tue, 30 Oct 2018 09:29:44 +0000 (10:29 +0100)
This patch adds NFT_CTX_OUTPUT_NUMERIC_SYMBOL, which replaces the last
client of the numeric level approach.

This patch updates `-n' option semantics to display all output
numerically.

Note that monitor code was still using the -n option to skip printing
the process name, this patch updates that path too to print it
inconditionally to simplify things.

Given the numeric levels have no more clients after this patch, remove
that code.

Update several tests/shell not to use -nn.

This patch adds NFT_CTX_OUTPUT_NUMERIC_ALL which enables all flags to
provide a fully numerical output.

Acked-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
12 files changed:
doc/libnftables.adoc
doc/nft.txt
include/nftables.h
include/nftables/libnftables.h
src/datatype.c
src/json.c
src/libnftables.c
src/main.c
src/monitor.c
tests/shell/testcases/netns/0001nft-f_0
tests/shell/testcases/netns/0002loosecommands_0
tests/shell/testcases/netns/0003many_0

index 788194396db1e9b2f8bb2b47fd7074a24e1ad8cf..007506784600fe73d92bcbd317392af7032e74a8 100644 (file)
@@ -21,10 +21,6 @@ void nft_ctx_set_dry_run(struct nft_ctx* '\*ctx'*, bool* 'dry'*);
 unsigned int nft_ctx_output_get_flags(struct nft_ctx* '\*ctx'*);
 void nft_ctx_output_set_flags(struct nft_ctx* '\*ctx'*, unsigned int* 'flags'*);
 
-enum nft_numeric_level nft_ctx_output_get_numeric(struct nft_ctx* '\*ctx'*);
-void nft_ctx_output_set_numeric(struct nft_ctx* '\*ctx'*,
-                               enum nft_numeric_level* 'level'*);
-
 unsigned int nft_ctx_output_get_debug(struct nft_ctx* '\*ctx'*);
 void nft_ctx_output_set_debug(struct nft_ctx* '\*ctx'*, unsigned int* 'mask'*);
 
@@ -125,37 +121,10 @@ NFT_CTX_OUTPUT_NUMERIC_PROTO::
        Display layer 4 protocol numerically.
 NFT_CTX_OUTPUT_NUMERIC_PRIO::
        Display base chain priority numerically.
-
-=== nft_ctx_output_get_numeric() and nft_ctx_output_set_numeric()
-These functions allow control over value representation in library output.
-For instance, port numbers by default are printed by their name (as listed in '/etc/services' file), if known.
-In libnftables, numeric output is leveled, defined as such:
-
-----
-enum nft_numeric_level {
-        NFT_NUMERIC_NONE,
-        NFT_NUMERIC_ADDR,
-        NFT_NUMERIC_PORT,
-        NFT_NUMERIC_ALL,
-};
-----
-
-Each numeric level includes all previous ones:
-
-NFT_NUMERIC_NONE::
-       No conversion into numeric format happens, this is the default.
-NFT_NUMERIC_ADDR::
-       Network addresses are always converted into numeric format.
-NFT_NUMERIC_PORT::
-       Network services are always converted into numeric format.
-NFT_NUMERIC_ALL::
-       Everything is converted into numeric format.
-
-The default numeric level is *NFT_NUMERIC_NONE*.
-
-The *nft_ctx_output_get_numeric*() function returns the numeric output setting's value contained in 'ctx'.
-
-The *nft_ctx_output_set_numeric*() function sets the numeric output setting in 'ctx' to the value of 'level'.
+NFT_CTX_OUTPUT_NUMERIC_SYMBOL::
+       Display expression datatype as numeric value.
+NFT_CTX_OUTPUT_NUMERIC_ALL::
+       Display all numerically.
 
 === nft_ctx_output_get_debug() and nft_ctx_output_set_debug()
 Libnftables supports separate debugging of different parts of its internals.
index 99ac0e333a938aa75c393c3e10214ed302e42bb8..8e18d9085a0d8a7d02a628849a716cd1741f7cb5 100644 (file)
@@ -34,10 +34,7 @@ For a full summary of options, run *nft --help*.
 
 *-n*::
 *--numeric*::
-       Show data numerically. When used once (the default behaviour), skip
-       lookup of addresses to symbolic names. Use twice to also show Internet
-       services (port numbers) numerically. Use three times to also show
-       protocols, UIDs/GIDs and priorities numerically.
+       Print fully numerical output.
 
 *-s*::
 *--stateless*::
index a4d01e0cddea3df18d8644fcada12024b07eb349..5c0292615b3f4be3370ae6c547e103e0b259fac2 100644 (file)
@@ -17,7 +17,6 @@ struct cookie {
 
 struct output_ctx {
        unsigned int flags;
-       unsigned int numeric;
        union {
                FILE *output_fp;
                struct cookie output_cookie;
@@ -73,6 +72,11 @@ static inline bool nft_output_numeric_prio(const struct output_ctx *octx)
        return octx->flags & NFT_CTX_OUTPUT_NUMERIC_PRIO;
 }
 
+static inline bool nft_output_numeric_symbol(const struct output_ctx *octx)
+{
+       return octx->flags & NFT_CTX_OUTPUT_NUMERIC_SYMBOL;
+}
+
 struct nft_cache {
        uint16_t                genid;
        struct list_head        list;
index fb81edc0df076f1e845a8008a8d0aec146795e56..70e9d238843a702dfdf96354063aba3e8a498ef2 100644 (file)
@@ -26,13 +26,6 @@ enum nft_debug_level {
        NFT_DEBUG_SEGTREE               = 0x40,
 };
 
-enum nft_numeric_level {
-       NFT_NUMERIC_NONE,
-       NFT_NUMERIC_ADDR,
-       NFT_NUMERIC_PORT,
-       NFT_NUMERIC_ALL,
-};
-
 /**
  * Possible flags to pass to nft_ctx_new()
  */
@@ -54,13 +47,15 @@ enum {
        NFT_CTX_OUTPUT_GUID             = (1 << 6),
        NFT_CTX_OUTPUT_NUMERIC_PROTO    = (1 << 7),
        NFT_CTX_OUTPUT_NUMERIC_PRIO     = (1 << 8),
+       NFT_CTX_OUTPUT_NUMERIC_SYMBOL   = (1 << 9),
+       NFT_CTX_OUTPUT_NUMERIC_ALL      = (NFT_CTX_OUTPUT_NUMERIC_PROTO |
+                                          NFT_CTX_OUTPUT_NUMERIC_PRIO |
+                                          NFT_CTX_OUTPUT_NUMERIC_SYMBOL),
 };
 
 unsigned int nft_ctx_output_get_flags(struct nft_ctx *ctx);
 void nft_ctx_output_set_flags(struct nft_ctx *ctx, unsigned int flags);
 
-enum nft_numeric_level nft_ctx_output_get_numeric(struct nft_ctx *ctx);
-void nft_ctx_output_set_numeric(struct nft_ctx *ctx, enum nft_numeric_level level);
 unsigned int nft_ctx_output_get_debug(struct nft_ctx *ctx);
 void nft_ctx_output_set_debug(struct nft_ctx *ctx, unsigned int mask);
 
index bfb70a6ebb76b753125704396e351dae7efb4a5a..6af1c84350d1cad60b3824742e58d599bc5534c0 100644 (file)
@@ -196,7 +196,7 @@ void symbolic_constant_print(const struct symbol_table *tbl,
        if (quotes)
                nft_print(octx, "\"");
 
-       if (octx->numeric > NFT_NUMERIC_ALL)
+       if (nft_output_numeric_symbol(octx))
                nft_print(octx, "%" PRIu64 "", val);
        else
                nft_print(octx, "%s", s->identifier);
index 8a2bcd658bd8fc770f94130ab8963aa8f4b4dedc..fc92d4641fcf51358893d7d0575caac84cff344c 100644 (file)
@@ -812,7 +812,7 @@ static json_t *symbolic_constant_json(const struct symbol_table *tbl,
        if (!s->identifier)
                return expr_basetype(expr)->json(expr, octx);
 
-       if (octx->numeric > NFT_NUMERIC_ALL)
+       if (nft_output_numeric_symbol(octx))
                return json_integer(val);
        else
                return json_string(s->identifier);
index 03c15fbaf7e5af520627e69130b14f7f72f3b9c7..bd79cd6091d259c5cfeff4424587c308b5964bfa 100644 (file)
@@ -312,17 +312,6 @@ void nft_ctx_set_dry_run(struct nft_ctx *ctx, bool dry)
        ctx->check = dry;
 }
 
-enum nft_numeric_level nft_ctx_output_get_numeric(struct nft_ctx *ctx)
-{
-       return ctx->output.numeric;
-}
-
-void nft_ctx_output_set_numeric(struct nft_ctx *ctx,
-                               enum nft_numeric_level level)
-{
-       ctx->output.numeric = level;
-}
-
 unsigned int nft_ctx_output_get_flags(struct nft_ctx *ctx)
 {
        return ctx->output.flags;
index 883261fc9d8b8e596a50e377a75388a9ee618a10..1f01a6c0b528c6b34fb2250df8737638547a2734 100644 (file)
@@ -132,9 +132,7 @@ static void show_help(const char *name)
 "  -i, --interactive           Read input from interactive CLI\n"
 "\n"
 "  -j, --json                  Format output in JSON\n"
-"  -n, --numeric                       When specified once, show network addresses numerically (default behaviour).\n"
-"                              Specify twice to also show Internet services (port numbers) numerically.\n"
-"                              Specify three times to also show protocols, user IDs, and group IDs numerically.\n"
+"  -n, --numeric                       Print fully numerical output.\n"
 "  -s, --stateless             Omit stateful information of ruleset.\n"
 "  -u, --guid                  Print UID/GID as defined in /etc/passwd and /etc/group.\n"
 "  -N                          Translate IP addresses to names.\n"
@@ -189,7 +187,6 @@ static const struct {
 int main(int argc, char * const *argv)
 {
        char *buf = NULL, *filename = NULL;
-       enum nft_numeric_level numeric;
        unsigned int output_flags = 0;
        bool interactive = false;
        unsigned int debug_mask;
@@ -229,14 +226,7 @@ int main(int argc, char * const *argv)
                        }
                        break;
                case OPT_NUMERIC:
-                       numeric = nft_ctx_output_get_numeric(nft);
-                       if (numeric == NFT_NUMERIC_ALL) {
-                               fprintf(stderr, "Too many numeric options "
-                                               "used, max. %u\n",
-                                       NFT_NUMERIC_ALL);
-                               exit(EXIT_FAILURE);
-                       }
-                       nft_ctx_output_set_numeric(nft, numeric + 1);
+                       output_flags |= NFT_CTX_OUTPUT_NUMERIC_ALL;
                        break;
                case OPT_STATELESS:
                        output_flags |= NFT_CTX_OUTPUT_STATELESS;
index b2267e1f63e4442052319cb3a5867d98dd294d21..0e735ed5b1aa7d3348407aa347418556f6c97c87 100644 (file)
@@ -835,11 +835,9 @@ static int netlink_events_newgen_cb(const struct nlmsghdr *nlh, int type,
        }
        if (genid >= 0) {
                nft_mon_print(monh, "# new generation %d", genid);
-               if (pid >= 0) {
-                       nft_mon_print(monh, " by process %d", pid);
-                       if (!monh->ctx->nft->output.numeric)
-                               nft_mon_print(monh, " (%s)", name);
-               }
+               if (pid >= 0)
+                       nft_mon_print(monh, " by process %d (%s)", pid, name);
+
                nft_mon_print(monh, "\n");
        }
 
index 642498260e00c2a28eff61d0831cefa96b89edc4..8344808760b7ba233e4c4c4351f7a367a77ee8d9 100755 (executable)
@@ -90,7 +90,7 @@ if [ $? -ne 0 ] ; then
        exit 1
 fi
 
-KERNEL_RULESET="$($IP netns exec $NETNS_NAME $NFT list ruleset -nn)"
+KERNEL_RULESET="$($IP netns exec $NETNS_NAME $NFT list ruleset)"
 $IP netns del $NETNS_NAME
 if [ "$RULESET" != "$KERNEL_RULESET" ] ; then
         DIFF="$(which diff)"
index 3910446a5565f929e32e8eff4faaf1e111e4db01..e62782804da4c88f169cafaa64052a50f7474170 100755 (executable)
@@ -53,7 +53,7 @@ RULESET="table ip t {
        }
 }"
 
-KERNEL_RULESET="$($IP netns exec $NETNS_NAME $NFT list ruleset -nn)"
+KERNEL_RULESET="$($IP netns exec $NETNS_NAME $NFT list ruleset)"
 $IP netns del $NETNS_NAME
 if [ "$RULESET" != "$KERNEL_RULESET" ] ; then
         DIFF="$(which diff)"
index 5ec4b2e4358ffb27f62ab6691237705aac132f37..61ad37bddadb64160d5085af45ee75f0710ac702 100755 (executable)
@@ -94,7 +94,7 @@ function test_netns()
                exit 1
        fi
 
-       KERNEL_RULESET="$($IP netns exec $NETNS_NAME $NFT list ruleset -nn)"
+       KERNEL_RULESET="$($IP netns exec $NETNS_NAME $NFT list ruleset)"
        if [ "$RULESET" != "$KERNEL_RULESET" ] ; then
                echo "E: ruleset in netns $NETNS_NAME differs from the loaded" >&2
                DIFF="$(which diff)"