summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorÁlvaro Neira Ayuso <alvaroneay@gmail.com>2013-09-12 19:16:37 +0200
committerPablo Neira Ayuso <pablo@netfilter.org>2013-09-13 09:43:59 +0200
commit4e02019fb2b8ddcf374c7e026dbdd7881dc09506 (patch)
tree5b249436293dc94d9d480de9ae5a264e134e6929
parent04db2744603bd506470b4e1baf9deee16d43baca (diff)
src: json: refactor json parsing to allow tree based navigation
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 <alvaroneay@gmail.com> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
-rw-r--r--src/chain.c31
-rw-r--r--src/rule.c31
-rw-r--r--src/set.c32
-rw-r--r--src/table.c31
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;