summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorFernando Fernandez Mancera <ffmancera@riseup.net>2022-09-04 19:18:26 +0200
committerPablo Neira Ayuso <pablo@netfilter.org>2022-09-05 15:08:56 +0200
commit9a879f675f383ef6d7fcaa7817eb048f90b35b9b (patch)
tree6106cab8886c5750d4f68da0a2fbb0d1e99e8357 /src
parent482fc1f21a40b7f2e11ddfc73e0b82027e68d345 (diff)
json: fix empty statement list output in sets and maps
JSON output of sets and map should not include the statements list if is empty. The statement output should be stateless also. In addition, removes duplicated code. Fixes: 07958ec53830 ("json: add set statement list support") Fixes: e66f3187d891 ("json: add table map statement support") Signed-off-by: Fernando Fernandez Mancera <ffmancera@riseup.net> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'src')
-rw-r--r--src/json.c61
1 files changed, 32 insertions, 29 deletions
diff --git a/src/json.c b/src/json.c
index 1f2889c6..6598863e 100644
--- a/src/json.c
+++ b/src/json.c
@@ -105,6 +105,25 @@ static json_t *stmt_print_json(const struct stmt *stmt, struct output_ctx *octx)
return json_pack("s", buf);
}
+static json_t *set_stmt_list_json(const struct list_head *stmt_list,
+ struct output_ctx *octx)
+{
+ unsigned int flags = octx->flags;
+ json_t *root, *tmp;
+ struct stmt *i;
+
+ root = json_array();
+ octx->flags |= NFT_CTX_OUTPUT_STATELESS;
+
+ list_for_each_entry(i, stmt_list, list) {
+ tmp = stmt_print_json(i, octx);
+ json_array_append_new(root, tmp);
+ }
+ octx->flags = flags;
+
+ return root;
+}
+
static json_t *set_print_json(struct output_ctx *octx, const struct set *set)
{
json_t *root, *tmp;
@@ -181,17 +200,8 @@ static json_t *set_print_json(struct output_ctx *octx, const struct set *set)
}
if (!list_empty(&set->stmt_list)) {
- json_t *array, *tmp;
- struct stmt *stmt;
-
- array = json_array();
-
- list_for_each_entry(stmt, &set->stmt_list, list) {
- tmp = stmt_print_json(stmt, octx);
- json_array_append_new(array, tmp);
- }
-
- json_object_set_new(root, "stmt", array);
+ json_object_set_new(root, "stmt",
+ set_stmt_list_json(&set->stmt_list, octx));
}
return json_pack("{s:o}", type, root);
@@ -1453,29 +1463,22 @@ json_t *counter_stmt_json(const struct stmt *stmt, struct output_ctx *octx)
"bytes", stmt->counter.bytes);
}
-static json_t *set_stmt_list_json(const struct list_head *stmt_list,
- struct output_ctx *octx)
-{
- json_t *root, *tmp;
- struct stmt *i;
-
- root = json_array();
-
- list_for_each_entry(i, stmt_list, list) {
- tmp = stmt_print_json(i, octx);
- json_array_append_new(root, tmp);
- }
-
- return root;
-}
-
json_t *set_stmt_json(const struct stmt *stmt, struct output_ctx *octx)
{
- return json_pack("{s:{s:s, s:o, s:o, s:s+}}", "set",
+ json_t *root;
+
+ root = json_pack("{s:s, s:o, s:s+}",
"op", set_stmt_op_names[stmt->set.op],
"elem", expr_print_json(stmt->set.key, octx),
- "stmt", set_stmt_list_json(&stmt->set.stmt_list, octx),
"set", "@", stmt->set.set->set->handle.set.name);
+
+ if (!list_empty(&stmt->set.stmt_list)) {
+ json_object_set_new(root, "stmt",
+ set_stmt_list_json(&stmt->set.stmt_list,
+ octx));
+ }
+
+ return json_pack("{s:o}", "set", root);
}
json_t *objref_stmt_json(const struct stmt *stmt, struct output_ctx *octx)