diff options
author | Fernando Fernandez Mancera <ffmancera@riseup.net> | 2022-09-02 12:52:04 +0200 |
---|---|---|
committer | Pablo Neira Ayuso <pablo@netfilter.org> | 2022-09-03 15:54:26 +0200 |
commit | e66f3187d891a7b2f7212f33ab7501d5ee6c3b2f (patch) | |
tree | a69770e7ed81754a439bd623ca5d6b2aef2ae5fe /src/json.c | |
parent | a817ea9655dee1915423a802c0133e3611e02b3a (diff) |
json: add table map statement support
When listing a map with statements with JSON support, the statement list were
ignored.
Output example:
{
"map": {
"family": "ip",
"name": "m",
"table": "t",
"type": "ipv4_addr",
"handle": 1,
"map": "mark",
"stmt": [
{
"counter": {
"packets": 0,
"bytes": 0
}
}
]
}
}
Link: https://bugzilla.netfilter.org/show_bug.cgi?id=1588
Signed-off-by: Fernando Fernandez Mancera <ffmancera@riseup.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'src/json.c')
-rw-r--r-- | src/json.c | 70 |
1 files changed, 42 insertions, 28 deletions
@@ -77,6 +77,34 @@ static json_t *set_dtype_json(const struct expr *key) return root; } +static json_t *stmt_print_json(const struct stmt *stmt, struct output_ctx *octx) +{ + char buf[1024]; + FILE *fp; + + /* XXX: Can't be supported at this point: + * xt_stmt_xlate() ignores output_fp. + */ + if (stmt->ops->type == STMT_XT) + return json_pack("{s:n}", "xt"); + + if (stmt->ops->json) + return stmt->ops->json(stmt, octx); + + fprintf(stderr, "warning: stmt ops %s have no json callback\n", + stmt->ops->name); + + fp = octx->output_fp; + octx->output_fp = fmemopen(buf, 1024, "w"); + + stmt->ops->print(stmt, octx); + + fclose(octx->output_fp); + octx->output_fp = fp; + + return json_pack("s", buf); +} + static json_t *set_print_json(struct output_ctx *octx, const struct set *set) { json_t *root, *tmp; @@ -152,6 +180,20 @@ static json_t *set_print_json(struct output_ctx *octx, const struct set *set) json_object_set_new(root, "elem", array); } + 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); + } + return json_pack("{s:o}", type, root); } @@ -168,34 +210,6 @@ static json_t *element_print_json(struct output_ctx *octx, "elem", root); } -static json_t *stmt_print_json(const struct stmt *stmt, struct output_ctx *octx) -{ - char buf[1024]; - FILE *fp; - - /* XXX: Can't be supported at this point: - * xt_stmt_xlate() ignores output_fp. - */ - if (stmt->ops->type == STMT_XT) - return json_pack("{s:n}", "xt"); - - if (stmt->ops->json) - return stmt->ops->json(stmt, octx); - - fprintf(stderr, "warning: stmt ops %s have no json callback\n", - stmt->ops->name); - - fp = octx->output_fp; - octx->output_fp = fmemopen(buf, 1024, "w"); - - stmt->ops->print(stmt, octx); - - fclose(octx->output_fp); - octx->output_fp = fp; - - return json_pack("s", buf); -} - static json_t *rule_print_json(struct output_ctx *octx, const struct rule *rule) { |