]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
json: allow to specify comment on chain
authorPablo Neira Ayuso <pablo@netfilter.org>
Tue, 25 Apr 2023 08:33:22 +0000 (10:33 +0200)
committerPablo Neira Ayuso <pablo@netfilter.org>
Tue, 25 Apr 2023 09:16:32 +0000 (11:16 +0200)
Allow users to add a comment when declaring a chain.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
src/json.c
src/parser_json.c

index ae00055d71f55c1c0030d3b97eb34c89c8f7677d..1b42ebc06dd3698ee98aae4359359dbad27811b6 100644 (file)
@@ -263,6 +263,9 @@ static json_t *chain_print_json(const struct chain *chain)
                         "name", chain->handle.chain.name,
                         "handle", chain->handle.handle.id);
 
+       if (chain->comment)
+               json_object_set_new(root, "comment", json_string(chain->comment));
+
        if (chain->flags & CHAIN_F_BASECHAIN) {
                mpz_export_data(&priority, chain->priority.expr->value,
                                BYTEORDER_HOST_ENDIAN, sizeof(int));
index 95f6bdcd943d1a276b16e50b253f72259a3333e8..50f8d1d93765caae4a1709bc5c0d26b450c8836f 100644 (file)
@@ -2851,17 +2851,19 @@ static struct cmd *json_parse_cmd_add_chain(struct json_ctx *ctx, json_t *root,
        struct handle h = {
                .table.location = *int_loc,
        };
-       const char *family = "", *policy = "", *type, *hookstr, *name;
-       struct chain *chain;
+       const char *family = "", *policy = "", *type, *hookstr, *name, *comment = NULL;
+       struct chain *chain = NULL;
        int prio;
 
        if (json_unpack_err(ctx, root, "{s:s, s:s}",
                            "family", &family,
                            "table", &h.table.name))
                return NULL;
-       if (op != CMD_DELETE &&
-           json_unpack_err(ctx, root, "{s:s}", "name", &h.chain.name)) {
-               return NULL;
+       if (op != CMD_DELETE) {
+               if (json_unpack_err(ctx, root, "{s:s}", "name", &h.chain.name))
+                       return NULL;
+
+               json_unpack(root, "{s:s}", "comment", &comment);
        } else if (op == CMD_DELETE &&
                   json_unpack(root, "{s:s}", "name", &h.chain.name) &&
                   json_unpack(root, "{s:I}", "handle", &h.handle.id)) {
@@ -2876,14 +2878,22 @@ static struct cmd *json_parse_cmd_add_chain(struct json_ctx *ctx, json_t *root,
        if (h.chain.name)
                h.chain.name = xstrdup(h.chain.name);
 
+       if (comment) {
+               chain = chain_alloc(NULL);
+               handle_merge(&chain->handle, &h);
+               chain->comment = xstrdup(comment);
+       }
+
        if (op == CMD_DELETE ||
            op == CMD_LIST ||
            op == CMD_FLUSH ||
            json_unpack(root, "{s:s, s:s, s:i}",
                        "type", &type, "hook", &hookstr, "prio", &prio))
-               return cmd_alloc(op, obj, &h, int_loc, NULL);
+               return cmd_alloc(op, obj, &h, int_loc, chain);
+
+       if (!chain)
+               chain = chain_alloc(NULL);
 
-       chain = chain_alloc(NULL);
        chain->flags |= CHAIN_F_BASECHAIN;
        chain->type.str = xstrdup(type);
        chain->priority.expr = constant_expr_alloc(int_loc, &integer_type,