From 4e02019fb2b8ddcf374c7e026dbdd7881dc09506 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Neira=20Ayuso?= Date: Thu, 12 Sep 2013 19:16:37 +0200 Subject: src: json: refactor json parsing to allow tree based navigation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch refactors nft_*_json_parse to provide a new intermediate function nft_jansson_parse_chain which will allows us to navigate the entire json tree containing the ruleset. Signed-off-by: Álvaro Neira Ayuso Signed-off-by: Pablo Neira Ayuso --- src/rule.c | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) (limited to 'src/rule.c') diff --git a/src/rule.c b/src/rule.c index a85005f..2f92e7d 100644 --- a/src/rule.c +++ b/src/rule.c @@ -475,22 +475,17 @@ int nft_rule_nlmsg_parse(const struct nlmsghdr *nlh, struct nft_rule *r) } EXPORT_SYMBOL(nft_rule_nlmsg_parse); -static int nft_rule_json_parse(struct nft_rule *r, const char *json) -{ #ifdef JSON_PARSING - json_t *root, *node, *array; - json_error_t error; +static int nft_jansson_parse_rule(struct nft_rule *r, json_t *tree) +{ + json_t *root, *array; struct nft_rule_expr *e; const char *str = NULL; uint64_t uval64; uint32_t uval32; int i, family; - node = nft_jansson_create_root(json, &error); - if (node == NULL) - return -1; - - root = nft_jansson_get_node(node, "rule"); + root = nft_jansson_get_node(tree, "rule"); if (root == NULL) return -1; @@ -557,11 +552,25 @@ static int nft_rule_json_parse(struct nft_rule *r, const char *json) nft_rule_add_expr(r, e); } - nft_jansson_free_root(node); + nft_jansson_free_root(tree); return 0; err: - nft_jansson_free_root(node); + nft_jansson_free_root(tree); return -1; +} +#endif + +static int nft_rule_json_parse(struct nft_rule *r, const char *json) +{ +#ifdef JSON_PARSING + json_t *tree; + json_error_t error; + + tree = nft_jansson_create_root(json, &error); + if (tree == NULL) + return -1; + + return nft_jansson_parse_rule(r, tree); #else errno = EOPNOTSUPP; return -1; -- cgit v1.2.3