]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
src: add nft_ctx_output_{get,set}_json() to nft_ctx_output_{get,set}_flags
authorPablo Neira Ayuso <pablo@netfilter.org>
Sat, 27 Oct 2018 10:02:02 +0000 (12:02 +0200)
committerPablo Neira Ayuso <pablo@netfilter.org>
Mon, 29 Oct 2018 14:07:35 +0000 (15:07 +0100)
Add NFT_CTX_OUTPUT_JSON flag and display output in json format.

Acked-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
doc/libnftables.adoc
include/nftables.h
include/nftables/libnftables.h
src/libnftables.c
src/main.c
src/monitor.c
src/rule.c

index dbd38bdd2134837840dfe47d4961758d2ea833c6..b385567c2f699e9face33083c997ba6099eb563d 100644 (file)
@@ -31,9 +31,6 @@ void nft_ctx_output_set_debug(struct nft_ctx* '\*ctx'*, unsigned int* 'mask'*);
 bool nft_ctx_output_get_echo(struct nft_ctx* '\*ctx'*);
 void nft_ctx_output_set_echo(struct nft_ctx* '\*ctx'*, bool* 'val'*);
 
-bool nft_ctx_output_get_json(struct nft_ctx* '\*ctx'*);
-void nft_ctx_output_set_json(struct nft_ctx* '\*ctx'*, bool* 'val'*);
-
 FILE *nft_ctx_set_output(struct nft_ctx* '\*ctx'*, FILE* '\*fp'*);
 int nft_ctx_buffer_output(struct nft_ctx* '\*ctx'*);
 int nft_ctx_unbuffer_output(struct nft_ctx* '\*ctx'*);
@@ -94,6 +91,7 @@ enum {
         NFT_CTX_OUTPUT_SERVICE     = (1 << 1),
         NFT_CTX_OUTPUT_STATELESS   = (1 << 2),
         NFT_CTX_OUTPUT_HANDLE      = (1 << 3),
+        NFT_CTX_OUTPUT_JSON        = (1 << 4),
 };
 ----
 
@@ -109,6 +107,10 @@ NFT_CTX_OUTPUT_HANDLE::
 For example, when deleting a table or chain, it may be identified either by name or handle.
 Rules on the other hand must be deleted by handle because there is no other way to uniquely identify them.
 This flag makes ruleset listings include handle values.
+NFT_CTX_OUTPUT_JSON::
+       If enabled at compile-time, libnftables accepts input in JSON format and is able to print output in JSON format as well.
+See *libnftables-json*(5) for a description of the supported schema.
+This flag controls JSON output format, input is auto-detected.
 
 The *nft_ctx_output_get_flags*() function returns the output flags setting's value in 'ctx'.
 
@@ -190,16 +192,6 @@ The *nft_ctx_output_get_echo*() function returns the echo output setting's value
 
 The *nft_ctx_output_set_echo*() function sets the echo output setting in 'ctx' to the value of 'val'.
 
-=== nft_ctx_output_get_json() and nft_ctx_output_set_json()
-If enabled at compile-time, libnftables accepts input in JSON format and is able to print output in JSON format as well.
-See *libnftables-json*(5) for a description of the supported schema.
-These functions control JSON output format, input is auto-detected.
-The default setting is *false*.
-
-The *nft_ctx_output_get_json*() function returns the JSON output setting's value in 'ctx'.
-
-The *nft_ctx_output_set_json*() function sets the JSON output setting in 'ctx' to the value of 'val'.
-
 === Controlling library standard and error output
 By default, any output from the library (e.g., after a *list* command) is written to 'stdout' and any error messages are written to 'stderr'.
 To give applications control over them, there are functions to assign custom file pointers as well as having the library buffer what would be written for later retrieval in a static buffer.
index e0e7a113540649f09ed5b2c05d432cbdf47ffd65..86788a43dd714c05d07147793179f1bdbcecac6f 100644 (file)
@@ -19,7 +19,6 @@ struct output_ctx {
        unsigned int flags;
        unsigned int numeric;
        unsigned int echo;
-       unsigned int json;
        union {
                FILE *output_fp;
                struct cookie output_cookie;
@@ -50,6 +49,11 @@ static inline bool nft_output_handle(const struct output_ctx *octx)
        return octx->flags & NFT_CTX_OUTPUT_HANDLE;
 }
 
+static inline bool nft_output_json(const struct output_ctx *octx)
+{
+       return octx->flags & NFT_CTX_OUTPUT_JSON;
+}
+
 struct nft_cache {
        uint16_t                genid;
        struct list_head        list;
index a6ce938305c38aa8819b08d9274e09de897dd16d..35374072560e219ac7ecc42bafa37f20b1bea8b3 100644 (file)
@@ -49,6 +49,7 @@ enum {
        NFT_CTX_OUTPUT_SERVICE          = (1 << 1),
        NFT_CTX_OUTPUT_STATELESS        = (1 << 2),
        NFT_CTX_OUTPUT_HANDLE           = (1 << 3),
+       NFT_CTX_OUTPUT_JSON             = (1 << 4),
 };
 
 unsigned int nft_ctx_output_get_flags(struct nft_ctx *ctx);
@@ -60,8 +61,6 @@ unsigned int nft_ctx_output_get_debug(struct nft_ctx *ctx);
 void nft_ctx_output_set_debug(struct nft_ctx *ctx, unsigned int mask);
 bool nft_ctx_output_get_echo(struct nft_ctx *ctx);
 void nft_ctx_output_set_echo(struct nft_ctx *ctx, bool val);
-bool nft_ctx_output_get_json(struct nft_ctx *ctx);
-void nft_ctx_output_set_json(struct nft_ctx *ctx, bool val);
 
 FILE *nft_ctx_set_output(struct nft_ctx *ctx, FILE *fp);
 int nft_ctx_buffer_output(struct nft_ctx *ctx);
index 6dc1be3d5ef895464404056c4c56ed9094ddaffb..ff7a53d22ba475d98cbb7ddd3e6f3476c430f162 100644 (file)
@@ -352,22 +352,6 @@ void nft_ctx_output_set_echo(struct nft_ctx *ctx, bool val)
        ctx->output.echo = val;
 }
 
-bool nft_ctx_output_get_json(struct nft_ctx *ctx)
-{
-#ifdef HAVE_LIBJANSSON
-       return ctx->output.json;
-#else
-       return false;
-#endif
-}
-
-void nft_ctx_output_set_json(struct nft_ctx *ctx, bool val)
-{
-#ifdef HAVE_LIBJANSSON
-       ctx->output.json = val;
-#endif
-}
-
 static const struct input_descriptor indesc_cmdline = {
        .type   = INDESC_BUFFER,
        .name   = "<cmdline>",
@@ -425,7 +409,7 @@ int nft_run_cmd_from_buffer(struct nft_ctx *nft, const char *buf)
        nlbuf = xzalloc(strlen(buf) + 2);
        sprintf(nlbuf, "%s\n", buf);
 
-       if (nft->output.json)
+       if (nft_output_json(&nft->output))
                rc = nft_parse_json_buffer(nft, nlbuf, &msgs, &cmds);
        if (rc == -EINVAL)
                rc = nft_parse_bison_buffer(nft, nlbuf, &msgs, &cmds);
@@ -447,7 +431,7 @@ err:
        }
        free(nlbuf);
 
-       if (!rc && nft->output.json && nft->output.echo)
+       if (!rc && nft_output_json(&nft->output) && nft->output.echo)
                json_print_echo(nft);
        return rc;
 }
@@ -467,7 +451,7 @@ int nft_run_cmd_from_filename(struct nft_ctx *nft, const char *filename)
                filename = "/dev/stdin";
 
        rc = -EINVAL;
-       if (nft->output.json)
+       if (nft_output_json(&nft->output))
                rc = nft_parse_json_filename(nft, filename, &msgs, &cmds);
        if (rc == -EINVAL)
                rc = nft_parse_bison_filename(nft, filename, &msgs, &cmds);
@@ -488,7 +472,7 @@ err:
                nft->scanner = NULL;
        }
 
-       if (!rc && nft->output.json && nft->output.echo)
+       if (!rc && nft_output_json(&nft->output) && nft->output.echo)
                json_print_echo(nft);
        return rc;
 }
index 7cf3bb6801e7cae5b7b471649cac550c8127addc..33e3bc6e766acadf2cafb0de8a465f71268a2655 100644 (file)
@@ -272,7 +272,9 @@ int main(int argc, char * const *argv)
                        nft_ctx_output_set_echo(nft, true);
                        break;
                case OPT_JSON:
-                       nft_ctx_output_set_json(nft, true);
+#ifdef HAVE_LIBJANSSON
+                       output_flags |= NFT_CTX_OUTPUT_JSON;
+#endif
                        break;
                case OPT_INVALID:
                        exit(EXIT_FAILURE);
index 9e3c43dcac689d83712b26b532c3386aad2d057d..01480cd7d86e7887588a57e0bb35ce5c0079f467 100644 (file)
@@ -908,7 +908,7 @@ int netlink_echo_callback(const struct nlmsghdr *nlh, void *data)
        if (!echo_monh.ctx->nft->output.echo)
                return MNL_CB_OK;
 
-       if (ctx->nft->output.json)
+       if (nft_output_json(&ctx->nft->output))
                return json_events_cb(nlh, &echo_monh);
 
        return netlink_events_cb(nlh, &echo_monh);
index da1bdc44ab69c8681110382fa12bc60cd4f97fb2..86b68cb8c34fa902a65a95876347fffec40f0397 100644 (file)
@@ -2127,7 +2127,7 @@ static int do_command_list(struct netlink_ctx *ctx, struct cmd *cmd)
 {
        struct table *table = NULL;
 
-       if (ctx->nft->output.json)
+       if (nft_output_json(&ctx->nft->output))
                return do_command_list_json(ctx, cmd);
 
        if (cmd->handle.table.name != NULL)