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/table.c | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) (limited to 'src/table.c') diff --git a/src/table.c b/src/table.c index 8d20be5..838c5ee 100644 --- a/src/table.c +++ b/src/table.c @@ -266,20 +266,15 @@ err: #endif } -static int nft_table_json_parse(struct nft_table *t, const char *json) -{ #ifdef JSON_PARSING - json_t *root, *node; - json_error_t error; +static int nft_jansson_parse_table(struct nft_table *t, json_t *tree) +{ + json_t *root; uint32_t flags; const char *str; int family; - node = nft_jansson_create_root(json, &error); - if (node == NULL) - return -1; - - root = nft_jansson_get_node(node, "table"); + root = nft_jansson_get_node(tree, "table"); if (root == NULL) return -1; @@ -299,11 +294,25 @@ static int nft_table_json_parse(struct nft_table *t, const char *json) nft_table_attr_set_u32(t, NFT_TABLE_ATTR_FLAGS, flags); - 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_table_json_parse(struct nft_table *t, 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_table(t, tree); #else errno = EOPNOTSUPP; return -1; -- cgit v1.2.3