]> git.ipfire.org Git - thirdparty/libnftnl.git/commitdiff
src: json: refactor json parsing to allow tree based navigation
authorÁlvaro Neira Ayuso <alvaroneay@gmail.com>
Thu, 12 Sep 2013 17:16:37 +0000 (19:16 +0200)
committerPablo Neira Ayuso <pablo@netfilter.org>
Fri, 13 Sep 2013 07:43:59 +0000 (09:43 +0200)
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>
src/chain.c
src/rule.c
src/set.c
src/table.c

index b196cd64c51d1d19e2664c10a257409336144135..94e0c69bc46eda11d977a5c7782d9fe7e9a65f4b 100644 (file)
@@ -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;
index a85005fcc2f4ce306e47d8723943dc0970b7d831..2f92e7d5baaa9442e1536235123d070574cd07fa 100644 (file)
@@ -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;
index fe30e777cd280dc6e6706d2d9bdf59deae01743d..a4b644a5a8572f69972d078bcee6df9372c16126 100644 (file)
--- 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;
index 8d20be5aaf1bf3a9555b1c85fdad494c2badc00e..838c5ee2dcbc59de684ee2e6cb338172b272c29d 100644 (file)
@@ -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;