Skip to contentSkip to conversation

table: json: Add Json parser support

PATCH In review Alvaro Neira 2 participants 10 messages Jul 25, 2013 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH 1/6 table: json: Add Json parser support

From: Álvaro Neira Ayuso <[email protected]>

Add function for parsing tables in format JSON

---
 configure.ac                |    9 ++++-
 include/libnftables/table.h |    1 +
 src/Makefile.am             |    3 +-
 src/internal.h              |   10 +++++
 src/jansson.c               |   79 +++++++++++++++++++++++++++++++++++++++++++
 src/table.c                 |   69 ++++++++++++++++++++++++++++++++++++++
 src/utils.c                 |   61 ++++++++++++++++++++-------------
 7 files changed, 206 insertions(+), 26 deletions(-)
 create mode 100644 src/jansson.c
configure.ac +8 -1
index c8075e9..834c0a3 100644
@@ -18,7 +18,10 @@ AC_ARG_WITH([xml-parsing], AS_HELP_STRING([--with-xml-parsing], [XML parsing sup
 AS_IF([test "x$with_xml_parsing" = "xyes"], [
 	PKG_CHECK_MODULES([LIBXML], [mxml >= 2.6])
 ])
-
+AC_ARG_WITH([json-parsing], AS_HELP_STRING([--with-json-parsing], [JSON parsing support]))
+AS_IF([test "x$with_json_parsing" = "xyes"], [
+	PKG_CHECK_MODULES([LIBJSON], [jansson >= 2.3])
+])
 AC_PROG_CC
 AM_PROG_CC_C_O
 AC_DISABLE_STATIC
@@ -33,6 +36,10 @@ regular_CPPFLAGS="-D_FILE_OFFSET_BITS=64 -D_REENTRANT"
 AS_IF([test "x$with_xml_parsing" = "xyes"], [
 	regular_CPPFLAGS="$regular_CPPFLAGS -DXML_PARSING"
 ])
+
+AS_IF([test "x$with_json_parsing" = "xyes"], [
+	regular_CPPFLAGS="$regular_CPPFLAGS -DJSON_PARSING"
+])
 regular_CFLAGS="-Wall -Waggregate-return -Wmissing-declarations \
 	-Wmissing-prototypes -Wshadow -Wstrict-prototypes \
 	-Wformat=2 -pipe"
include/libnftables/table.h +1 -0
index f3f3e89..24ca374 100644
@@ -40,6 +40,7 @@ enum {
 enum nft_table_parse_type {
 	NFT_TABLE_PARSE_NONE	= 0,
 	NFT_TABLE_PARSE_XML,
+	NFT_TABLE_PARSE_JSON,
 	NFT_TABLE_PARSE_MAX,
 };
 
src/Makefile.am +2 -1
index 6496511..51b40a2 100644
@@ -1,7 +1,7 @@
 include $(top_srcdir)/Make_global.am
 lib_LTLIBRARIES = libnftables.la
 
-libnftables_la_LIBADD = ${LIBMNL_LIBS} ${LIBXML_LIBS}
+libnftables_la_LIBADD = ${LIBMNL_LIBS} ${LIBXML_LIBS} ${LIBJSON_LIBS}
 libnftables_la_LDFLAGS = -Wl,--version-script=$(srcdir)/libnftables.map \
 			 -version-info $(LIBVERSION)
 libnftables_la_SOURCES = utils.c		\
@@ -11,6 +11,7 @@ libnftables_la_SOURCES = utils.c		\
 			 set.c			\
 			 set_elem.c		\
 			 mxml.c			\
+			 jansson.c		\
 			 expr.c			\
 			 expr_ops.c		\
 			 expr/bitwise.c		\
src/internal.h +10 -0
index b846814..47cd635 100644
@@ -12,6 +12,7 @@
 #include "linux_list.h"
 
 #include <stdint.h>
+#include <stdbool.h>
 
 #define BASE_DEC 10
 #define BASE_HEX 16
@@ -37,6 +38,14 @@ int nft_mxml_num_parse(mxml_node_t *tree, const char *node_name, uint32_t mxml_f
 const char *nft_mxml_str_parse(mxml_node_t *tree, const char *node_name, uint32_t mxml_flags);
 #endif
 
+#ifdef JSON_PARSING
+#include <jansson.h>
+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);
+#endif
+
 #define NFT_TABLE_XML_VERSION 0
 #define NFT_CHAIN_XML_VERSION 0
 #define NFT_RULE_XML_VERSION 0
@@ -51,6 +60,7 @@ int nft_str2family(const char *family);
 int nft_strtoi(const char *string, int base, void *number, enum nft_type type);
 const char *nft_verdict2str(uint32_t verdict);
 int nft_str2verdict(const char *verdict);
+int nft_get_value(enum nft_type type, void *val, void *out);
 
 struct expr_ops;
 
src/jansson.c new file +79 -0

79 lines not on this page · raw message

src/table.c +69 -0

81 lines not on this page · raw message

src/utils.c +37 -24

94 lines not on this page · raw message

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

#1 lore raw

PATCH 2/6 table : tests: test the table json parser support

From: Álvaro Neira Ayuso <[email protected]>

Test the functions for parsing tables in JSON Support

---
 tests/Makefile.am             |    2 +-
 tests/jsonfiles/01-table.json |    1 +
 tests/jsonfiles/02-table.json |    1 +
 tests/nft-parsing-test.c      |   49 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 52 insertions(+), 1 deletion(-)
 create mode 100644 tests/jsonfiles/01-table.json
 create mode 100644 tests/jsonfiles/02-table.json
tests/Makefile.am +1 -1
index 6941c3c..cfa4e8e 100644
@@ -3,4 +3,4 @@ include $(top_srcdir)/Make_global.am
 check_PROGRAMS = nft-parsing-test
 
 nft_parsing_test_SOURCES = nft-parsing-test.c
-nft_parsing_test_LDADD = ../src/libnftables.la ${LIBMNL_LIBS} ${LIBXML_LIBS}
+nft_parsing_test_LDADD = ../src/libnftables.la ${LIBMNL_LIBS} ${LIBXML_LIBS} ${LIBJSON_LIBS}
tests/jsonfiles/01-table.json new file +1 -0
new file mode 100644
index 0000000..ec496b9
@@ -0,0 +1 @@
+{"table" : {"name" : "filter","version" : 0,"properties" : {"family" : "ip","table_flags" : 0}}}
tests/jsonfiles/02-table.json new file +1 -0
new file mode 100644
index 0000000..03f4d5a
@@ -0,0 +1 @@
+{"table" : {"name" : "filter2","version" : 0,"properties" : {"family" : "ip6","table_flags" : 0}}}
tests/nft-parsing-test.c +49 -0

61 lines not on this page · raw message

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

#3 lore raw

PATCH 3/6 examples: Add nft-table-json-add

In reply to Alvaro Neira

From: Álvaro Neira Ayuso <[email protected]>

---
 examples/Makefile.am          |    4 +
 examples/nft-table-json-add.c |  116 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 120 insertions(+)
 create mode 100644 examples/nft-table-json-add.c
examples/Makefile.am +4 -0
index bf28bae..722ce0b 100644
@@ -2,6 +2,7 @@ include $(top_srcdir)/Make_global.am
 
 check_PROGRAMS = nft-table-add		\
 		 nft-table-xml-add	\
+		 nft-table-json-add	\
 		 nft-table-upd		\
 		 nft-table-del		\
 		 nft-table-get		\
@@ -29,6 +30,9 @@ nft_table_add_LDADD = ../src/libnftables.la ${LIBMNL_LIBS}
 nft_table_xml_add_SOURCES = nft-table-xml-add.c
 nft_table_xml_add_LDADD = ../src/libnftables.la ${LIBMNL_LIBS}
 
+nft_table_json_add_SOURCES = nft-table-json-add.c
+nft_table_json_add_LDADD = ../src/libnftables.la ${LIBMNL_LIBS}
+
 nft_table_upd_SOURCES = nft-table-upd.c
 nft_table_upd_LDADD = ../src/libnftables.la ${LIBMNL_LIBS}
 
examples/nft-table-json-add.c new file +116 -0

116 lines not on this page · raw message

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

#5 lore raw

PATCH 4/6 chain: json: add function for parsing chain

In reply to Alvaro Neira

From: Álvaro Neira Ayuso <[email protected]>

Add function for parsing chains in format JSON

---
 include/libnftables/chain.h |    1 
 src/chain.c                 |  138 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 139 insertions(+)
include/libnftables/chain.h +1 -0
index 382947f..53fd407 100644
@@ -52,6 +52,7 @@ enum {
 enum nft_chain_parse_type {
 	NFT_CHAIN_PARSE_NONE	= 0,
 	NFT_CHAIN_PARSE_XML,
+	NFT_CHAIN_PARSE_JSON,
 	NFT_CHAIN_PARSE_MAX
 };
 
src/chain.c +138 -0

150 lines not on this page · raw message

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

#7 lore raw

PATCH 5/6 chain: test: test the chain parser support

In reply to Alvaro Neira

From: Álvaro Neira Ayuso <[email protected]>

Test the functions for parsing chains in JSON Support

---
 tests/jsonfiles/11-chain.json |    1 +
 tests/jsonfiles/12-chain.json |    1 +
 tests/jsonfiles/13-chain.json |    1 +
 tests/jsonfiles/14-chain.json |    1 +
 tests/nft-parsing-test.c      |    9 +++++++++
 5 files changed, 13 insertions(+)
 create mode 100644 tests/jsonfiles/11-chain.json
 create mode 100644 tests/jsonfiles/12-chain.json
 create mode 100644 tests/jsonfiles/13-chain.json
 create mode 100644 tests/jsonfiles/14-chain.json
tests/jsonfiles/11-chain.json new file +1 -0
new file mode 100644
index 0000000..9b716f2
@@ -0,0 +1 @@
+{ "chain": {"name": "input","handle": 1,"bytes": 2238649,"packets": 14177,"version": 0,"properties": {"family": "ip","table": "filter","use": 0,"type": "filter","hooknum": "NF_INET_LOCAL_IN","prio": 0,"policy": "accept"}}}
tests/jsonfiles/12-chain.json new file +1 -0
new file mode 100644
index 0000000..0af8588
@@ -0,0 +1 @@
+{ "chain": {"name": "forward","handle": 2,"bytes": 0,"packets": 0,"version": 0,"properties": {"family": "ip","table": "filter","use": 0,"type": "filter","hooknum": "NF_INET_FORWARD","prio": 0,"policy": "accept"}}}
tests/jsonfiles/13-chain.json new file +1 -0
new file mode 100644
index 0000000..835fc2b
@@ -0,0 +1 @@
+{ "chain": {"name": "output","handle": 3,"bytes": 781933,"packets": 6506,"version": 0,"properties": {"family": "ip","table": "filter","use": 0,"type": "filter","hooknum": "NF_INET_LOCAL_OUT","prio": 0,"policy": "accept"}}}
tests/jsonfiles/14-chain.json new file +1 -0
new file mode 100644
index 0000000..de3bc35
@@ -0,0 +1 @@
+{ "chain": {"name": "chain1","handle": 4,"bytes": 0,"packets": 0,"version": 0,"properties": {"family": "ip","table": "filter","use": 0}}}
tests/nft-parsing-test.c +9 -0
index 83a627c..777e2e0 100644
@@ -23,6 +23,7 @@ static int test_json(const char *filename)
 #ifdef JSON_PARSING
 	int ret = -1;
 	struct nft_table *t = NULL;
+	struct nft_chain *c = NULL;
 	json_t *root;
 	json_error_t error;
 	char *json = NULL;
@@ -46,6 +47,14 @@ static int test_json(const char *filename)
 
 			nft_table_free(t);
 		}
+	}else if (json_object_get(root, "chain") != NULL) {
+		c = nft_chain_alloc();
+		if (c != NULL) {
+			if (nft_chain_parse(c, NFT_CHAIN_PARSE_JSON, json) == 0)
+				ret = 0;
+
+			nft_chain_free(c);
+		}
 	}
 
 	return ret;

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

#8 lore raw

PATCH 6/6 examples: Add nft-chain-json-add

In reply to Alvaro Neira

From: Álvaro Neira Ayuso <[email protected]>

---
 examples/Makefile.am          |    4 +
 examples/nft-chain-json-add.c |  120 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 124 insertions(+)
 create mode 100644 examples/nft-chain-json-add.c
examples/Makefile.am +4 -0
index 722ce0b..8d76f00 100644
@@ -8,6 +8,7 @@ check_PROGRAMS = nft-table-add		\
 		 nft-table-get		\
 		 nft-chain-add		\
 		 nft-chain-xml-add	\
+		 nft-chain-json-add	\
 		 nft-chain-del		\
 		 nft-chain-get		\
 		 nft-rule-add		\
@@ -48,6 +49,9 @@ nft_chain_add_LDADD = ../src/libnftables.la ${LIBMNL_LIBS}
 nft_chain_xml_add_SOURCES = nft-chain-xml-add.c
 nft_chain_xml_add_LDADD = ../src/libnftables.la ${LIBMNL_LIBS}
 
+nft_chain_json_add_SOURCES = nft-chain-json-add.c
+nft_chain_json_add_LDADD = ../src/libnftables.la ${LIBMNL_LIBS} ${LIBXML_LIBS} ${LIBJSON_LIBS}
+
 nft_chain_del_SOURCES = nft-chain-del.c
 nft_chain_del_LDADD = ../src/libnftables.la ${LIBMNL_LIBS}
 
examples/nft-chain-json-add.c new file +120 -0

120 lines not on this page · raw message

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

#2 lore raw

Re: [libnftables PATCH 2/6] table : tests: test the table json parser support

In reply to Alvaro Neira
Quoted 3 lines · Alvaro Neira

From: Álvaro Neira Ayuso <[email protected]>

Test the functions for parsing tables in JSON Support

Also applied, thanks.
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#6 lore raw

Re: [libnftables PATCH 4/6] chain: json: add function for parsing chain

In reply to Alvaro Neira
Quoted 30 lines · Alvaro Neira

From: Álvaro Neira Ayuso <[email protected]>

Add function for parsing chains in format JSON

---
 include/libnftables/chain.h |    1 
 src/chain.c                 |  138 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 139 insertions(+)
include/libnftables/chain.h +1 -0
index 382947f..53fd407 100644
@@ -52,6 +52,7 @@ enum {
 enum nft_chain_parse_type {
 	NFT_CHAIN_PARSE_NONE	= 0,
 	NFT_CHAIN_PARSE_XML,
+	NFT_CHAIN_PARSE_JSON,
 	NFT_CHAIN_PARSE_MAX
 };
 
src/chain.c +0 -0
index 1e07044..d9d41ee 100644
@@ -468,6 +468,141 @@ int nft_chain_nlmsg_parse(const struct nlmsghdr *nlh, struct nft_chain *c)
 }
 EXPORT_SYMBOL(nft_chain_nlmsg_parse);
 

src/chain.c int nft_chain_nlmsg_parse(const struct nlmsghdr *nlh, struct nft_chain *c) · line 480

Quoted 10 lines
src/chain.c +10 -0
+static int str2policy(const char *policy)
+{
+	if (strcmp("accept", policy) == 0) {
+		return NF_ACCEPT;
+	} else if (strcmp("drop", policy) == 0) {
+		return NF_DROP;
+	} else {
+		return -1;
+	}
+}

Arturo just added nft_str2verdict, please use it.

src/chain.c int nft_chain_nlmsg_parse(const struct nlmsghdr *nlh, struct nft_chain *c) · line 493

Quoted 13 lines
src/chain.c +13 -0
+
+static int nft_chain_json_parse(struct nft_chain *c, char *json)
+{
+#ifdef JSON_PARSING
+	json_t *root;
+	json_error_t error;
+	uint64_t val64;
+	uint32_t hooknum;
+	int32_t prio;
+	const char *valstr;
+
+	/* Load the tree */
+	root = json_loadb (json, strlen(json), 0, &error);
                         ^

no need for space there.

src/chain.c int nft_chain_nlmsg_parse(const struct nlmsghdr *nlh, struct nft_chain *c) · line 494

Quoted 1 line
src/chain.c +1 -0
+	if (!root) {

if (root == NULL)

for consistency with other code you sent.

Quoted 10 lines
src/chain.c +10 -0
+		errno = EINVAL;
+		return -1;
+	}
+
+	root = json_object_get(root, "chain");
+	if (root == NULL) {
+		errno = ERANGE;
+		return -1;
+	}
+

src/chain.c int nft_chain_nlmsg_parse(const struct nlmsghdr *nlh, struct nft_chain *c) · line 514

Quoted 10 lines
src/chain.c +10 -0
+	if (nft_jansson_value_parse_val(root, "version", NFT_TYPE_U64,
+					 &val64) == -1)
+		goto err;
+
+	if (val64 != NFT_CHAIN_JSON_VERSION)
+		goto err;
+
+	valstr = nft_jansson_value_parse_str(root, "name");
+	if (valstr == NULL)
+		goto err;

You have to change nft_jansson_value_parse_str to set errno accordingly.

Then you return -1;

Quoted 10 lines
src/chain.c +10 -0
+
+	nft_chain_attr_set_str(c, NFT_CHAIN_ATTR_NAME, strdup(valstr));
+
+	if (nft_jansson_value_parse_val(root, "handle", NFT_TYPE_U64,
+					 &val64) == -1)
+		goto err;
+
+	nft_chain_attr_set_u64(c,NFT_CHAIN_ATTR_HANDLE, val64);
+
+	if (nft_jansson_value_parse_val(root, "bytes", NFT_TYPE_U64,

src/chain.c int nft_chain_nlmsg_parse(const struct nlmsghdr *nlh, struct nft_chain *c) · line 534

Quoted 10 lines
src/chain.c +10 -0
+					 &val64) == -1)
+		goto err;
+
+	nft_chain_attr_set_u64(c, NFT_CHAIN_ATTR_BYTES, val64);
+
+	if (nft_jansson_value_parse_val(root, "packets", NFT_TYPE_U64,
+					 &val64) == -1)
+		goto err;
+
+	if (val64 < 0)

val64 is uint64_t, so it cannot be negative.

src/chain.c int nft_chain_nlmsg_parse(const struct nlmsghdr *nlh, struct nft_chain *c) · line 544

Quoted 10 lines
src/chain.c +10 -0
+		goto err;
+
+	nft_chain_attr_set_u64(c, NFT_CHAIN_ATTR_PACKETS, val64);
+
+	root = json_object_get(root, "properties");
+
+	valstr = nft_jansson_value_parse_str(root, "family");
+
+	if (valstr == NULL)
+		goto err;
                return -1;

instead. nft_jansson_value_parse_str should set errno accordingly.

src/chain.c int nft_chain_nlmsg_parse(const struct nlmsghdr *nlh, struct nft_chain *c) · line 547

Quoted 3 lines
src/chain.c +3 -0
+
+	if (nft_str2family(valstr) == -1)
+		goto err;
                return -1;

nft_str2family already sets errno.

Quoted 41 lines
src/chain.c +41 -0
+
+	nft_chain_attr_set_u32(c, NFT_CHAIN_ATTR_FAMILY, nft_str2family(valstr));
+
+	valstr = nft_jansson_value_parse_str(root, "table");
+
+	if (valstr == NULL)
+		goto err;
+
+	nft_chain_attr_set_str(c, NFT_CHAIN_ATTR_TABLE, strdup(valstr));
+
+	if (nft_jansson_node_exist(root, "hooknum")) {
+		valstr = nft_jansson_value_parse_str(root, "type");
+
+		if (valstr == NULL)
+			goto err;
+
+		nft_chain_attr_set_str(c, NFT_CHAIN_ATTR_TYPE, strdup(valstr));
+
+		if (nft_jansson_value_parse_val(root, "prio", NFT_TYPE_S32,
+						 &prio) == -1)
+			goto err;
+
+		nft_chain_attr_set_s32(c, NFT_CHAIN_ATTR_PRIO, prio);
+
+		valstr = nft_jansson_value_parse_str(root, "hooknum");
+		for (hooknum = 0; hooknum < NF_INET_NUMHOOKS; hooknum++) {
+			if (strcmp(valstr, hooknum2str_array[hooknum]) == 0) {
+				nft_chain_attr_set_u32(c, NFT_CHAIN_ATTR_HOOKNUM,
+						       hooknum);
+				break;
+			}
+		}
+
+		valstr = nft_jansson_value_parse_str(root, "policy");
+
+		if (valstr == NULL)
+			goto err;
+
+		if (str2policy(valstr) == -1)
+			goto err;
+

src/chain.c int nft_chain_nlmsg_parse(const struct nlmsghdr *nlh, struct nft_chain *c) · line 598

Quoted 10 lines
src/chain.c +10 -0
+		nft_chain_attr_set_u32(c, NFT_CHAIN_ATTR_POLICY,
+				       str2policy(valstr));
+	}
+
+	free(root);
+	return 0;
+
+err:
+	free(root);
+	errno = ERANGE;

this should return EINVAL.

Quoted 22 lines
src/chain.c +10 -1
+	return -1;
+#else
+	errno = EOPNOTSUPP;
+	return -1;
+#endif
+}
+
 static int nft_chain_xml_parse(struct nft_chain *c, char *xml)
 {
 #ifdef XML_PARSING
@@ -661,6 +796,9 @@ int nft_chain_parse(struct nft_chain *c, enum nft_chain_parse_type type,
 	case NFT_CHAIN_PARSE_XML:
 		ret = nft_chain_xml_parse(c, data);
 		break;
+	case NFT_CHAIN_PARSE_JSON:
+		ret = nft_chain_json_parse(c, data);
+		break;
 	default:
 		ret = -1;
 		errno = EOPNOTSUPP;
 
--
Quoted 3 lines

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

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