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/chain.c | 31 ++++++++++++++++++++----------- src/rule.c | 31 ++++++++++++++++++++----------- src/set.c | 32 +++++++++++++++++++++----------- src/table.c | 31 ++++++++++++++++++++----------- 4 files changed, 81 insertions(+), 44 deletions(-) diff --git a/src/chain.c b/src/chain.c index b196cd6..94e0c69 100644 --- a/src/chain.c +++ b/src/chain.c @@ -505,21 +505,16 @@ static inline int nft_str2hooknum(int family, const char *hook) return -1; } -static int nft_chain_json_parse(struct nft_chain *c, const char *json) -{ #ifdef JSON_PARSING - json_t *root, *node; - json_error_t error; +static int nft_jansson_parse_chain(struct nft_chain *c, json_t *tree) +{ + json_t *root; uint64_t uval64; uint32_t policy; int32_t val32; const char *valstr; - node = nft_jansson_create_root(json, &error); - if (node == NULL) - return -1; - - root = nft_jansson_get_node(node, "chain"); + root = nft_jansson_get_node(tree, "chain"); if (root == NULL) return -1; @@ -591,12 +586,26 @@ static int nft_chain_json_parse(struct nft_chain *c, const char *json) nft_chain_attr_set_u32(c, NFT_CHAIN_ATTR_POLICY, policy); } - 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_chain_json_parse(struct nft_chain *c, 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_chain(c, tree); #else errno = EOPNOTSUPP; return -1; 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; diff --git a/src/set.c b/src/set.c index fe30e77..a4b644a 100644 --- a/src/set.c +++ b/src/set.c @@ -303,21 +303,16 @@ int nft_set_nlmsg_parse(const struct nlmsghdr *nlh, struct nft_set *s) } EXPORT_SYMBOL(nft_set_nlmsg_parse); -static int nft_set_json_parse(struct nft_set *s, const char *json) -{ #ifdef JSON_PARSING - json_t *root, *node, *array, *json_elem; - json_error_t error; +static int nft_jansson_parse_set(struct nft_set *s, json_t *tree) +{ + json_t *root, *array, *json_elem; uint32_t uval32; int family, i; const char *valstr; struct nft_set_elem *elem; - node = nft_jansson_create_root(json, &error); - if (node == NULL) - return -1; - - root = nft_jansson_get_node(node, "set"); + root = nft_jansson_get_node(tree, "set"); if (root == NULL) return -1; @@ -388,11 +383,26 @@ static int nft_set_json_parse(struct nft_set *s, const char *json) } - 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_set_json_parse(struct nft_set *s, 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_set(s, tree); #else errno = EOPNOTSUPP; return -1; 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