Skip to contentSkip to conversation

tests: remove unnecessary variable initialization

PATCH Applied Alvaro Neira 2 participants 8 messages Aug 27, 2013 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 tests: remove unnecessary variable initialization

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

---
 tests/nft-parsing-test.c |   20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
tests/nft-parsing-test.c +10 -10
index bf5b519..648ed36 100644
@@ -155,12 +155,12 @@ static int test_json(const char *filename)
 {
 #ifdef JSON_PARSING
 	int ret = -1;
-	struct nft_table *t = NULL;
-	struct nft_chain *c = NULL;
-	struct nft_rule *r = NULL;
+	struct nft_table *t;
+	struct nft_chain *c;
+	struct nft_rule *r;
 	json_t *root;
 	json_error_t error;
-	char *json = NULL;
+	char *json;
 
 	root = json_load_file(filename, 0, &error);
 	if (!root) {
@@ -222,13 +222,13 @@ static int test_xml(const char *filename)
 {
 #ifdef XML_PARSING
 	int ret = -1;
-	struct nft_table *t = NULL;
-	struct nft_chain *c = NULL;
-	struct nft_rule *r = NULL;
-	struct nft_set *s = NULL;
+	struct nft_table *t;
+	struct nft_chain *c;
+	struct nft_rule *r;
+	struct nft_set *s;
 	FILE *fp;
-	mxml_node_t *tree = NULL;;
-	char *xml = NULL;
+	mxml_node_t *tree;
+	char *xml;
 
 	fp = fopen(filename, "r");
 	tree = mxmlLoadFile(NULL, fp, MXML_NO_CALLBACK);

--
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/4 set: Add json parser support

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

Add function for parsing set in format JSON.

---
 include/libnftables/set.h |    1 
 src/internal.h            |    1 
 src/jansson.c             |   37 +++++++++++++++++
 src/set.c                 |   99 +++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 138 insertions(+)
include/libnftables/set.h +1 -0
index c55718c..9526ae1 100644
@@ -55,6 +55,7 @@ void nft_set_list_iter_destroy(struct nft_set_list_iter *iter);
 enum nft_set_parse_type {
 	NFT_SET_PARSE_NONE	= 0,
 	NFT_SET_PARSE_XML,
+	NFT_SET_PARSE_JSON,
 	NFT_SET_PARSE_MAX,
 };
 
src/internal.h +1 -0
index 6698962..2a36543 100644
@@ -58,6 +58,7 @@ struct nft_rule_expr *nft_jansson_expr_parse(json_t *root);
 union nft_data_reg;
 int nft_jansson_data_reg_parse(json_t *root, const char *tag,
 			       union nft_data_reg *data_reg);
+int nft_set_elem_json_parse(struct nft_set_elem *e, json_t *root);
 #endif
 
 const char *nft_family2str(uint32_t family);
src/jansson.c +37 -0
index 682b74b..04146e2 100644
@@ -16,6 +16,7 @@
 #include <errno.h>
 #include <string.h>
 #include "expr_ops.h"
+#include <libnftables/set.h>
 
 #include <libnftables/expr.h>
 #include <linux/netfilter/nf_tables.h>
@@ -210,4 +211,40 @@ int nft_jansson_data_reg_parse(json_t *root, const char *tag,
 		return -1;
 	}
 }
+
+int nft_set_elem_json_parse(struct nft_set_elem *e, json_t *root)
+{
+	uint32_t uval32;
+	int set_elem_data;
+
+	if (nft_jansson_parse_val(root, "flags", NFT_TYPE_U32, &uval32) < 0)
+		return -1;
+
+	nft_set_elem_attr_set_u32(e, NFT_SET_ELEM_ATTR_FLAGS, uval32);
+
+	if (nft_jansson_data_reg_parse(root, "key", &e->key) != DATA_VALUE)
+		return -1;
+
+	e->flags |= (1 << NFT_SET_ELEM_ATTR_KEY);
+
+	if (nft_jansson_node_exist(root, "data")) {
+		set_elem_data = nft_jansson_data_reg_parse(root, "data",
+							   &e->data);
+		switch (set_elem_data) {
+		case DATA_VALUE:
+			e->flags |= (1 << NFT_SET_ELEM_ATTR_DATA);
+			break;
+		case DATA_VERDICT:
+			e->flags |= (1 << NFT_SET_ELEM_ATTR_VERDICT);
+			break;
+		case DATA_CHAIN:
+			e->flags |= (1 << NFT_SET_ELEM_ATTR_CHAIN);
+			break;
+		default:
+			return -1;
+		}
+	}
+
+	return 0;
+}
 #endif
src/set.c +99 -0

111 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/4] set: Add json parser support

also applied, thanks.

#3 lore raw

PATCH 3/4 tests: set: add json parsing support

In reply to Alvaro Neira

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

Test the functions for parsing set in JSON.

---
 tests/jsonfiles/62-set.json |    1 +
 tests/jsonfiles/63-set.json |    1 +
 tests/nft-parsing-test.c    |   11 +++++++++++
 3 files changed, 13 insertions(+)
 create mode 100644 tests/jsonfiles/62-set.json
 create mode 100644 tests/jsonfiles/63-set.json
tests/jsonfiles/62-set.json new file +1 -0
new file mode 100644
index 0000000..c5200eb
@@ -0,0 +1 @@
+{ "set": { "name": "set0","table": "filter","flags": 3,"family": "ip","key_type": 12,"key_len": 2,"set_elem": [{"flags": 0,"key": {"data_reg": { "type" : "value", "len" : 2, "data0" : "0x00001700"}}}, {"flags": 0,"key": {"data_reg": { "type" : "value", "len" : 2, "data0" : "0x00001600"}}}]}}
tests/jsonfiles/63-set.json new file +1 -0
new file mode 100644
index 0000000..be3e564
@@ -0,0 +1 @@
+{ "set": { "name": "map0","table": "filter","flags": 11,"family": "ip","key_type": 12,"key_len": 2,"data_type": 4294967040,"data_len": 16,"set_elem": [{"flags": 0,"key": {"data_reg": { "type" : "value", "len" : 2, "data0" : "0x00001700"}},"data": {"data_reg": { "type" : "chain", "chain" : "forward"}}}, {"flags": 0,"key": {"data_reg": { "type" : "value", "len" : 2, "data0" : "0x00001600"}},"data": {"data_reg": { "type" : "chain", "chain" : "chain1"}}}]}}
tests/nft-parsing-test.c +11 -0
index 648ed36..ecde0e2 100644
@@ -158,6 +158,7 @@ static int test_json(const char *filename)
 	struct nft_table *t;
 	struct nft_chain *c;
 	struct nft_rule *r;
+	struct nft_set *s;
 	json_t *root;
 	json_error_t error;
 	char *json;
@@ -200,6 +201,16 @@ static int test_json(const char *filename)
 
 			nft_rule_free(r);
 		}
+	} else if (json_object_get(root, "set") != NULL) {
+		s = nft_set_alloc();
+		if (s != NULL) {
+			if (nft_set_parse(s, NFT_SET_PARSE_JSON, json) == 0)
+				ret = compare_test(TEST_JSON_SET, s, filename);
+			else
+				goto failparsing;
+
+			nft_set_free(s);
+			}
 	}
 
 	free(json);

--
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/4 examples: Add nft-set-json-add

In reply to Alvaro Neira

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

---
 examples/Makefile.am        |    4 +
 examples/nft-set-json-add.c |  116 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 120 insertions(+)
 create mode 100644 examples/nft-set-json-add.c
examples/Makefile.am +4 -0
index 639aafb..9965387 100644
@@ -19,6 +19,7 @@ check_PROGRAMS = nft-table-add		\
 		 nft-rule-get		\
 		 nft-events		\
 		 nft-set-add		\
+		 nft-set-json-add	\
 		 nft-set-get		\
 		 nft-set-del		\
 		 nft-set-elem-add	\
@@ -83,6 +84,9 @@ nft_events_LDADD = ../src/libnftables.la ${LIBMNL_LIBS}
 nft_set_add_SOURCES = nft-set-add.c
 nft_set_add_LDADD = ../src/libnftables.la ${LIBMNL_LIBS}
 
+nft_set_json_add_SOURCES = nft-set-json-add.c
+nft_set_json_add_LDADD = ../src/libnftables.la ${LIBMNL_LIBS}
+
 nft_set_del_SOURCES = nft-set-del.c
 nft_set_del_LDADD = ../src/libnftables.la ${LIBMNL_LIBS}
 
examples/nft-set-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

maintainer Applied
#6 lore raw

Re: [libnftables PATCH 4/4] examples: Add nft-set-json-add

Applied, thanks.