]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
src: add nft_ctx_output_{get,set}_echo() to nft_ctx_output_{get,set}_flags
authorPablo Neira Ayuso <pablo@netfilter.org>
Mon, 29 Oct 2018 11:11:09 +0000 (12:11 +0100)
committerPablo Neira Ayuso <pablo@netfilter.org>
Mon, 29 Oct 2018 14:07:35 +0000 (15:07 +0100)
Add NFT_CTX_OUTPUT_ECHO flag and echo the command that has been send to
the kernel.

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/mnl.c
src/monitor.c
src/parser_json.c
src/rule.c

index b385567c2f699e9face33083c997ba6099eb563d..6b8098fdf2d24b0f7830000a146f0fbbeab8c80f 100644 (file)
@@ -28,9 +28,6 @@ void nft_ctx_output_set_numeric(struct nft_ctx* '\*ctx'*,
 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'*);
-
 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'*);
@@ -92,6 +89,7 @@ enum {
         NFT_CTX_OUTPUT_STATELESS   = (1 << 2),
         NFT_CTX_OUTPUT_HANDLE      = (1 << 3),
         NFT_CTX_OUTPUT_JSON        = (1 << 4),
+        NFT_CTX_OUTPUT_ECHO        = (1 << 5),
 };
 ----
 
@@ -111,6 +109,9 @@ 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.
+NFT_CTX_OUTPUT_ECHO::
+       The echo setting makes libnftables print the changes once they are committed to the kernel, just like a running instance of *nft monitor* would.
+Amongst other things, this allows to retrieve an added rule's handle atomically.
 
 The *nft_ctx_output_get_flags*() function returns the output flags setting's value in 'ctx'.
 
@@ -183,15 +184,6 @@ The *nft_ctx_output_get_debug*() function returns the debug output setting's val
 
 The *nft_ctx_output_set_debug*() function sets the debug output setting in 'ctx' to the value of 'mask'.
 
-=== nft_ctx_output_get_echo() and nft_ctx_output_set_echo()
-The echo setting makes libnftables print the changes once they are committed to the kernel, just like a running instance of *nft monitor* would.
-Amongst other things, this allows to retrieve an added rule's handle atomically.
-The default setting is *false*.
-
-The *nft_ctx_output_get_echo*() function returns the echo output setting's value in 'ctx'.
-
-The *nft_ctx_output_set_echo*() function sets the echo 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 86788a43dd714c05d07147793179f1bdbcecac6f..fa6665a17a7eec9713b971eddd3f1bae19c6c998 100644 (file)
@@ -18,7 +18,6 @@ struct cookie {
 struct output_ctx {
        unsigned int flags;
        unsigned int numeric;
-       unsigned int echo;
        union {
                FILE *output_fp;
                struct cookie output_cookie;
@@ -54,6 +53,11 @@ static inline bool nft_output_json(const struct output_ctx *octx)
        return octx->flags & NFT_CTX_OUTPUT_JSON;
 }
 
+static inline bool nft_output_echo(const struct output_ctx *octx)
+{
+       return octx->flags & NFT_CTX_OUTPUT_ECHO;
+}
+
 struct nft_cache {
        uint16_t                genid;
        struct list_head        list;
index 35374072560e219ac7ecc42bafa37f20b1bea8b3..4777240883f01de4488b437851e87a41abe967bd 100644 (file)
@@ -50,6 +50,7 @@ enum {
        NFT_CTX_OUTPUT_STATELESS        = (1 << 2),
        NFT_CTX_OUTPUT_HANDLE           = (1 << 3),
        NFT_CTX_OUTPUT_JSON             = (1 << 4),
+       NFT_CTX_OUTPUT_ECHO             = (1 << 5),
 };
 
 unsigned int nft_ctx_output_get_flags(struct nft_ctx *ctx);
@@ -59,8 +60,6 @@ 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);
-bool nft_ctx_output_get_echo(struct nft_ctx *ctx);
-void nft_ctx_output_set_echo(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 ff7a53d22ba475d98cbb7ddd3e6f3476c430f162..03c15fbaf7e5af520627e69130b14f7f72f3b9c7 100644 (file)
@@ -342,16 +342,6 @@ void nft_ctx_output_set_debug(struct nft_ctx *ctx, unsigned int mask)
        ctx->debug_mask = mask;
 }
 
-bool nft_ctx_output_get_echo(struct nft_ctx *ctx)
-{
-       return ctx->output.echo;
-}
-
-void nft_ctx_output_set_echo(struct nft_ctx *ctx, bool val)
-{
-       ctx->output.echo = val;
-}
-
 static const struct input_descriptor indesc_cmdline = {
        .type   = INDESC_BUFFER,
        .name   = "<cmdline>",
@@ -431,7 +421,9 @@ err:
        }
        free(nlbuf);
 
-       if (!rc && nft_output_json(&nft->output) && nft->output.echo)
+       if (!rc &&
+           nft_output_json(&nft->output) &&
+           nft_output_echo(&nft->output))
                json_print_echo(nft);
        return rc;
 }
@@ -472,7 +464,9 @@ err:
                nft->scanner = NULL;
        }
 
-       if (!rc && nft_output_json(&nft->output) && nft->output.echo)
+       if (!rc &&
+           nft_output_json(&nft->output) &&
+           nft_output_echo(&nft->output))
                json_print_echo(nft);
        return rc;
 }
index 33e3bc6e766acadf2cafb0de8a465f71268a2655..6e1e4186675aee25b905212e34e776cc58905f45 100644 (file)
@@ -269,7 +269,7 @@ int main(int argc, char * const *argv)
                        output_flags |= NFT_CTX_OUTPUT_HANDLE;
                        break;
                case OPT_ECHO:
-                       nft_ctx_output_set_echo(nft, true);
+                       output_flags |= NFT_CTX_OUTPUT_ECHO;
                        break;
                case OPT_JSON:
 #ifdef HAVE_LIBJANSSON
index 951e510ba78ce4597cfa0d23e01616519ed3aa92..455e2d4729a69bf58ab7565313e8f1e4a5bea70c 100644 (file)
--- a/src/mnl.c
+++ b/src/mnl.c
@@ -350,7 +350,7 @@ int mnl_nft_rule_replace(struct netlink_ctx *ctx, const struct cmd *cmd)
        struct nlmsghdr *nlh;
        int err;
 
-       if (ctx->nft->output.echo) {
+       if (nft_output_echo(&ctx->nft->output)) {
                err = cache_update(ctx->nft, CMD_INVALID, ctx->msgs);
                if (err < 0)
                        return err;
index 01480cd7d86e7887588a57e0bb35ce5c0079f467..b2267e1f63e4442052319cb3a5867d98dd294d21 100644 (file)
@@ -905,7 +905,7 @@ int netlink_echo_callback(const struct nlmsghdr *nlh, void *data)
                .cache_needed = true,
        };
 
-       if (!echo_monh.ctx->nft->output.echo)
+       if (!nft_output_echo(&echo_monh.ctx->nft->output))
                return MNL_CB_OK;
 
        if (nft_output_json(&ctx->nft->output))
index bc682e92493cabe13d64f82f9865bc2938c7c775..412f5cf2d2bf4b4ae6f27516711868e1bf4b0fe4 100644 (file)
@@ -3399,7 +3399,7 @@ int nft_parse_json_buffer(struct nft_ctx *nft, const char *buf,
 
        ret = __json_parse(&ctx);
 
-       if (!nft->output.echo) {
+       if (!nft_output_echo(&nft->output)) {
                json_decref(nft->json_root);
                nft->json_root = NULL;
        }
@@ -3427,7 +3427,7 @@ int nft_parse_json_filename(struct nft_ctx *nft, const char *filename,
 
        ret = __json_parse(&ctx);
 
-       if (!nft->output.echo) {
+       if (!nft_output_echo(&nft->output)) {
                json_decref(nft->json_root);
                nft->json_root = NULL;
        }
index 86b68cb8c34fa902a65a95876347fffec40f0397..33cbf0e2e9bbd7a07198928e6832bf7430662216 100644 (file)
@@ -1388,7 +1388,7 @@ static int do_command_add(struct netlink_ctx *ctx, struct cmd *cmd, bool excl)
 {
        uint32_t flags = excl ? NLM_F_EXCL : 0;
 
-       if (ctx->nft->output.echo) {
+       if (nft_output_echo(&ctx->nft->output)) {
                int ret;
 
                ret = cache_update(ctx->nft, cmd->obj, ctx->msgs);
@@ -1439,7 +1439,7 @@ static int do_command_insert(struct netlink_ctx *ctx, struct cmd *cmd)
 {
        uint32_t flags = 0;
 
-       if (ctx->nft->output.echo) {
+       if (nft_output_echo(&ctx->nft->output)) {
                int ret;
 
                ret = cache_update(ctx->nft, cmd->obj, ctx->msgs);