]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
json: allow to specify comment on table
authorPablo Neira Ayuso <pablo@netfilter.org>
Mon, 24 Apr 2023 21:17:50 +0000 (23:17 +0200)
committerPablo Neira Ayuso <pablo@netfilter.org>
Wed, 22 Jan 2025 23:05:48 +0000 (00:05 +0100)
commit fd595286a68e2c676657d4ac753da517a4d0c3a2 upstream.

Allow users to add a comment when declaring a table:

 # sudo nft add table inet test3 '{comment "this is a comment";}'
 # nft list ruleset
 table inet test3 {
        comment "this is a comment"
 }
 # nft -j list ruleset
 {"nftables": [{"metainfo": {"version": "1.0.7", "release_name": "Old Doc Yak", "json_schema_version": 1}}, {"table": {"family": "inet", "name": "test3", "handle": 3, "comment": "this is a comment"}}]}
 # nft -j list ruleset > test.json
 # nft flush ruleset
 # nft -j -f test.json
 # nft -j list ruleset
 {"nftables": [{"metainfo": {"version": "1.0.7", "release_name": "Old Doc Yak", "json_schema_version": 1}}, {"table": {"family": "inet", "name": "test3", "handle": 4, "comment": "this is a comment"}}]}

Closes: https://bugzilla.netfilter.org/show_bug.cgi?id=1670
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
src/json.c
src/parser_json.c

index f167c88a9c4f62aed6ba653b191f3aa3ddd2ea40..6d8514cd34810b12891ebc4a95f5db83f93e4697 100644 (file)
@@ -505,6 +505,9 @@ static json_t *table_print_json(const struct table *table)
        if (tmp)
                json_object_set_new(root, "flags", tmp);
 
+       if (table->comment)
+               json_object_set_new(root, "comment", json_string(table->comment));
+
        return json_pack("{s:o}", "table", root);
 }
 
index eb73848aa2440e83538acbc69be9af8639a730e1..9569bded3596aaad97ba9480119e9d8ad67c7169 100644 (file)
@@ -2851,17 +2851,21 @@ static struct stmt *json_parse_stmt(struct json_ctx *ctx, json_t *root)
 static struct cmd *json_parse_cmd_add_table(struct json_ctx *ctx, json_t *root,
                                            enum cmd_ops op, enum cmd_obj obj)
 {
+       const char *family = "", *comment = NULL;
        struct handle h = {
                .table.location = *int_loc,
        };
-       const char *family = "";
+       struct table *table = NULL;
 
        if (json_unpack_err(ctx, root, "{s:s}",
                            "family", &family))
                return NULL;
-       if (op != CMD_DELETE &&
-           json_unpack_err(ctx, root, "{s:s}", "name", &h.table.name)) {
-               return NULL;
+
+       if (op != CMD_DELETE) {
+               if (json_unpack_err(ctx, root, "{s:s}", "name", &h.table.name))
+                       return NULL;
+
+               json_unpack(root, "{s:s}", "comment", &comment);
        } else if (op == CMD_DELETE &&
                   json_unpack(root, "{s:s}", "name", &h.table.name) &&
                   json_unpack(root, "{s:I}", "handle", &h.handle.id)) {
@@ -2875,10 +2879,16 @@ static struct cmd *json_parse_cmd_add_table(struct json_ctx *ctx, json_t *root,
        if (h.table.name)
                h.table.name = xstrdup(h.table.name);
 
+       if (comment) {
+               table = table_alloc();
+               handle_merge(&table->handle, &h);
+               table->comment = xstrdup(comment);
+       }
+
        if (op == CMD_ADD)
                json_object_del(root, "handle");
 
-       return cmd_alloc(op, obj, &h, int_loc, NULL);
+       return cmd_alloc(op, obj, &h, int_loc, table);
 }
 
 static struct expr *parse_policy(const char *policy)