table: json: Add Json parser support
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
@@ -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
@@ -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
@@ -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
@@ -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_fconst 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
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
@@ -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
@@ -0,0 +1 @@ +{"table" : {"name" : "filter","version" : 0,"properties" : {"family" : "ip","table_flags" : 0}}}
tests/jsonfiles/02-table.json new file +1 -0
@@ -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
PATCH 3/6 examples: Add nft-table-json-add
In reply to Alvaro NeiraFrom: Á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
@@ -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
PATCH 4/6 chain: json: add function for parsing chain
In reply to Alvaro NeiraFrom: Á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
@@ -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
PATCH 5/6 chain: test: test the chain parser support
In reply to Alvaro NeiraFrom: Á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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
PATCH 6/6 examples: Add nft-chain-json-add
In reply to Alvaro NeiraFrom: Á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
@@ -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
Pablo Neira Ayuso
maintainerQuoted 3 lines · Alvaro Neira
From: Álvaro Neira Ayuso <[email protected]>
Add function for parsing tables in format JSON
Applied, thanks Alvaro.
--
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
Pablo Neira Ayuso
maintainerRe: [libnftables PATCH 2/6] table : tests: test the table json parser support
In reply to Alvaro NeiraQuoted 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
Pablo Neira Ayuso
maintainerRe: [libnftables PATCH 3/6] examples: Add nft-table-json-add
In reply to Alvaro NeiraQuoted 1 line · Alvaro Neira
From: Álvaro Neira Ayuso <[email protected]>
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
Pablo Neira Ayuso
maintainerRe: [libnftables PATCH 4/6] chain: json: add function for parsing chain
In reply to Alvaro NeiraQuoted 30 lines · Alvaro Neira
From: Álvaro Neira Ayuso <[email protected]>
Add function for parsing chains in format JSON
Signed-off-by Alvaro Neira Ayuso <[email protected]>--- include/libnftables/chain.h | 1 src/chain.c | 138 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+)include/libnftables/chain.h +1
-0@@ -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@@ -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