From f63b54623fcd1ab7d2f51928571c164409f00175 Mon Sep 17 00:00:00 2001 From: Phil Sutter Date: Fri, 8 Jun 2018 17:27:18 +0200 Subject: JSON: Support latest enhancements of fwd statement JSON equivalent of fwd statement was too primitive to support the added address and family parameters, so make its value an object and accept the device expression as value of a "dev" property in there. Then add optional "addr" and "family" properties to it. While being at it, add a testcase to make sure the extended syntax works right. Signed-off-by: Phil Sutter Signed-off-by: Pablo Neira Ayuso --- src/json.c | 13 +++++++++++-- src/parser_json.c | 40 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/json.c b/src/json.c index a871c934..c1cd0fbf 100644 --- a/src/json.c +++ b/src/json.c @@ -1008,9 +1008,18 @@ json_t *limit_stmt_json(const struct stmt *stmt, struct output_ctx *octx) json_t *fwd_stmt_json(const struct stmt *stmt, struct output_ctx *octx) { - json_t *root; + json_t *root, *tmp; + + root = json_pack("{s:o}", "dev", expr_print_json(stmt->fwd.dev, octx)); + + if (stmt->fwd.addr) { + tmp = json_string(family2str(stmt->fwd.family)); + json_object_set_new(root, "family", tmp); + + tmp = expr_print_json(stmt->fwd.addr, octx); + json_object_set_new(root, "addr", tmp); + } - root = expr_print_json(stmt->fwd.dev, octx); return json_pack("{s:o}", "fwd", root); } diff --git a/src/parser_json.c b/src/parser_json.c index bc36136f..2fd0ef83 100644 --- a/src/parser_json.c +++ b/src/parser_json.c @@ -1584,11 +1584,47 @@ static struct stmt *json_parse_limit_stmt(struct json_ctx *ctx, static struct stmt *json_parse_fwd_stmt(struct json_ctx *ctx, const char *key, json_t *value) { - struct stmt *stmt = fwd_stmt_alloc(int_loc); + json_t *jaddr, *jdev; + const char *family; + struct stmt *stmt; + int familyval; + + if (json_unpack_err(ctx, value, "{s:o}", "dev", &jdev)) + return NULL; - stmt->fwd.dev = json_parse_expr(ctx, value); + stmt = fwd_stmt_alloc(int_loc); + + stmt->fwd.dev = json_parse_stmt_expr(ctx, jdev); + if (!stmt->fwd.dev) { + json_error(ctx, "Invalid fwd dev value."); + goto out_err; + } + + if (json_unpack(value, "{s:s, s:o}", + "family", &family, "addr", &jaddr)) + return stmt; + + familyval = parse_family(family); + switch (familyval) { + case NFPROTO_IPV4: + case NFPROTO_IPV6: + stmt->fwd.family = familyval; + break; + default: + json_error(ctx, "Invalid fwd family value '%s'.", family); + goto out_err; + } + + stmt->fwd.addr = json_parse_stmt_expr(ctx, jaddr); + if (!stmt->fwd.addr) { + json_error(ctx, "Invalid fwd addr value."); + goto out_err; + } return stmt; +out_err: + stmt_free(stmt); + return NULL; } static struct stmt *json_parse_notrack_stmt(struct json_ctx *ctx, -- cgit v1.2.3