Patchwork [libnftables,2/7] jansson: Add helper function for building the tree

login
register
mail settings
Submitter Alvaro Neira
Date July 31, 2013, 1:20 p.m.
Message ID <20130731132058.29730.38608.stgit@Ph0enix>
Download mbox | patch
Permalink /patch/263696/
State Accepted
Headers show

Comments

Alvaro Neira - July 31, 2013, 1:20 p.m.
From: Álvaro Neira Ayuso <alvaroneay@gmail.com>

Add a helper function for parsing and return the jansson tree

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
---
 src/internal.h |    1 +
 src/jansson.c  |   19 +++++++++++++++++++
 src/table.c    |   14 +++-----------
 3 files changed, 23 insertions(+), 11 deletions(-)


--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Patch

diff --git a/src/internal.h b/src/internal.h
index a8ae431..d1c7690 100644
--- a/src/internal.h
+++ b/src/internal.h
@@ -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);
diff --git a/src/jansson.c b/src/jansson.c
index cc68ae0..4c778d9 100644
--- a/src/jansson.c
+++ b/src/jansson.c
@@ -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
diff --git a/src/table.c b/src/table.c
index 1f4fe76..526f3e7 100644
--- a/src/table.c
+++ b/src/table.c
@@ -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)