json: allow to specify comment on table
Pablo Neira Ayuso
maintainerPATCH 1/1 json: allow to specify comment on 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"}}]}Signed-off-by Pablo Neira Ayuso <[email protected]>
--- src/json.c | 3 +++ src/parser_json.c | 20 +++++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-)
src/json.c +3 -0
@@ -510,6 +510,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); }
src/parser_json.c +15 -5
@@ -2789,17 +2789,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)) {
@@ -2813,10 +2817,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)
Signature · 1 line
2.30.2