summaryrefslogtreecommitdiffstats
path: root/src/table.c
diff options
context:
space:
mode:
authorÁlvaro Neira Ayuso <alvaroneay@gmail.com>2013-07-25 22:52:15 +0200
committerPablo Neira Ayuso <pablo@netfilter.org>2013-07-25 23:02:42 +0200
commitc8b97360470c25007d3675474c636ca1ce95599a (patch)
treee65d608e6f25f7046847d26a4c4557421a747226 /src/table.c
parent02295f3d01d13adba9fefeb7b9a40ea379aded1b (diff)
table: Add json parser support
Add function for parsing tables in format JSON Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'src/table.c')
-rw-r--r--src/table.c69
1 files changed, 69 insertions, 0 deletions
diff --git a/src/table.c b/src/table.c
index d814668..65797e8 100644
--- a/src/table.c
+++ b/src/table.c
@@ -295,6 +295,72 @@ static int nft_table_xml_parse(struct nft_table *t, char *xml)
#endif
}
+static int nft_table_json_parse(struct nft_table *t, char *json)
+{
+#ifdef JSON_PARSING
+ json_t *root;
+ json_error_t error;
+ uint64_t version;
+ uint32_t table_flag;
+ const char *str = NULL;
+
+ root = json_loadb(json, strlen(json), 0, &error);
+ if (!root) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ root = json_object_get(root, "table");
+ if (root == NULL) {
+ errno = ERANGE;
+ return -1;
+ }
+
+ if (nft_jansson_value_parse_val(root, "version",
+ NFT_TYPE_U64, &version) == -1)
+ goto err;
+
+ if (version != NFT_TABLE_JSON_VERSION || version == -1)
+ goto err;
+
+ str = nft_jansson_value_parse_str(root, "name");
+ if (str == NULL)
+ goto err;
+
+ nft_table_attr_set_str(t, NFT_TABLE_ATTR_NAME, strdup(str));
+
+ root = json_object_get(root, "properties");
+ if (root == NULL)
+ goto err;
+
+ str = nft_jansson_value_parse_str(root, "family");
+ if (str == NULL)
+ goto err;
+
+ if (nft_str2family(str) < 0)
+ goto err;
+
+ nft_table_attr_set_u32(t, NFT_TABLE_ATTR_FAMILY, nft_str2family(str));
+
+ if (nft_jansson_value_parse_val(root, "table_flags",
+ NFT_TYPE_U32, &table_flag) == -1)
+ goto err;
+
+ nft_table_attr_set_u32(t, NFT_TABLE_ATTR_FLAGS, table_flag);
+
+ free(root);
+ return 0;
+err:
+ free(root);
+ errno = ERANGE;
+ return -1;
+
+#else
+ errno = EOPNOTSUPP;
+ return -1;
+#endif
+}
+
int nft_table_parse(struct nft_table *t, enum nft_table_parse_type type,
char *data)
{
@@ -304,6 +370,9 @@ int nft_table_parse(struct nft_table *t, enum nft_table_parse_type type,
case NFT_TABLE_PARSE_XML:
ret = nft_table_xml_parse(t, data);
break;
+ case NFT_TABLE_PARSE_JSON:
+ ret = nft_table_json_parse(t, data);
+ break;
default:
ret = -1;
errno = EOPNOTSUPP;