From 0864c2d49ee8a1cc537568c05e14943186480125 Mon Sep 17 00:00:00 2001 From: "Jose M. Guisado Gomez" Date: Tue, 11 Aug 2020 16:27:20 +0200 Subject: src: add comment support for set declarations Allow users to add a comment when declaring a named set. Adds set output handling the comment in both nftables and json format. $ nft add table ip x $ nft add set ip x s {type ipv4_addr\; comment "some_addrs"\; elements = {1.1.1.1, 1.2.3.4}} $ nft list ruleset table ip x { set s { type ipv4_addr; comment "some_addrs" elements = { 1.1.1.1, 1.2.3.4 } } } $ nft --json list ruleset { "nftables": [ { "metainfo": { "json_schema_version": 1, "release_name": "Capital Idea #2", "version": "0.9.6" } }, { "table": { "family": "ip", "handle": 4857, "name": "x" } }, { "set": { "comment": "some_addrs", "elem": [ "1.1.1.1", "1.2.3.4" ], "family": "ip", "handle": 1, "name": "s", "table": "x", "type": "ipv4_addr" } } ] } Signed-off-by: Jose M. Guisado Gomez Signed-off-by: Pablo Neira Ayuso --- src/rule.c | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src/rule.c') diff --git a/src/rule.c b/src/rule.c index 8dc1792b..2b5685c2 100644 --- a/src/rule.c +++ b/src/rule.c @@ -367,6 +367,8 @@ void set_free(struct set *set) return; if (set->init != NULL) expr_free(set->init); + if (set->comment) + xfree(set->comment); handle_free(&set->handle); stmt_free(set->stmt); expr_free(set->key); @@ -584,6 +586,13 @@ static void set_print_declaration(const struct set *set, time_print(set->gc_int, octx); nft_print(octx, "%s", opts->stmt_separator); } + + if (set->comment) { + nft_print(octx, "%s%scomment \"%s\"%s", + opts->tab, opts->tab, + set->comment, + opts->stmt_separator); + } } static void do_set_print(const struct set *set, struct print_fmt_options *opts, -- cgit v1.2.3