From: Pablo Neira Ayuso Date: Tue, 25 Apr 2023 08:33:22 +0000 (+0200) Subject: json: allow to specify comment on chain X-Git-Tag: v1.0.6.1~291 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=a004cc29a4373b865518ba072cec4736ecc2cc1a;p=thirdparty%2Fnftables.git json: allow to specify comment on chain commit bd976ab13b4d57ebf5d02459c360905a76af9e58 upstream. Allow users to add a comment when declaring a chain. Signed-off-by: Pablo Neira Ayuso --- diff --git a/src/json.c b/src/json.c index 6d8514cd..705669db 100644 --- a/src/json.c +++ b/src/json.c @@ -256,6 +256,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)); diff --git a/src/parser_json.c b/src/parser_json.c index 9569bded..bb18a67b 100644 --- a/src/parser_json.c +++ b/src/parser_json.c @@ -2947,8 +2947,8 @@ 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; - struct chain *chain; + const char *family = "", *policy = "", *type, *hookstr, *comment = NULL; + struct chain *chain = NULL; json_t *devs = NULL; int prio; @@ -2956,9 +2956,11 @@ static struct cmd *json_parse_cmd_add_chain(struct json_ctx *ctx, json_t *root, "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)) { @@ -2973,14 +2975,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,