]> git.ipfire.org Git - thirdparty/libnftnl.git/commitdiff
jansson: Add helper function for building the tree and use it
authorÁlvaro Neira Ayuso <alvaroneay@gmail.com>
Wed, 31 Jul 2013 13:20:59 +0000 (15:20 +0200)
committerPablo Neira Ayuso <pablo@netfilter.org>
Wed, 31 Jul 2013 14:42:53 +0000 (16:42 +0200)
Add a helper function that parses and returns the jansson
tree, use it in the table parser.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
src/internal.h
src/jansson.c
src/table.c

index a8ae431e1ed06f7af9a7d79bc0ae15c4582eb78a..d1c7690601f8cb3d5c41fc8ac6e6be4beab5f7de 100644 (file)
@@ -45,6 +45,7 @@ int nft_jansson_value_parse_val(json_t *root, const char *tag,
                                  int type, void *out);
 const char *nft_jansson_value_parse_str(json_t *root, const char *tag);
 bool nft_jansson_node_exist(json_t *root, const char *tag);
+json_t *nft_jansson_get_root(char *json, const char *tag, json_error_t *err);
 #endif
 
 const char *nft_family2str(uint32_t family);
index cc68ae0e69ad82e7bef5e2f803e9a59cbd29baad..4c778d9469fe5e0f9c9d3aed3100a3963aa298f1 100644 (file)
@@ -71,4 +71,23 @@ bool nft_jansson_node_exist(json_t *root, const char *tag)
 {
        return json_object_get(root, tag) != NULL;
 }
+
+json_t *nft_jansson_get_root(char *json, const char *tag, json_error_t *err)
+{
+       json_t *root;
+
+       root = json_loadb(json, strlen(json), 0, err);
+       if (root == NULL) {
+               errno = EINVAL;
+               return NULL;
+       }
+
+       root = json_object_get(root, tag);
+       if (root == NULL) {
+               errno = EINVAL;
+               return NULL;
+       }
+
+       return root;
+}
 #endif
index 1f4fe764c57830fac4cc653b8557238fdbcc732b..526f3e7acb7f20aae52add7b907592fd3668481e 100644 (file)
@@ -290,17 +290,9 @@ static int nft_table_json_parse(struct nft_table *t, char *json)
        const char *str;
        int family;
 
-       root = json_loadb(json, strlen(json), 0, &error);
-       if (!root) {
-               errno = EINVAL;
-               goto err;
-       }
-
-       root = json_object_get(root, "table");
-       if (root == NULL) {
-               errno = EINVAL;
-               goto err;
-       }
+       root = nft_jansson_get_root(json, "table", &error);
+       if (root == NULL)
+               return -1;
 
        str = nft_jansson_value_parse_str(root, "name");
        if (str == NULL)