Skip to contentSkip to conversation

chain: change policy2str function to nft_verdict2str function

PATCH In review Alvaro Neira 2 participants 8 messages Jul 31, 2013 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH 1/7 chain: change policy2str function to nft_verdict2str function

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

---
 src/chain.c |   17 ++---------------
 1 file changed, 2 insertions(+), 15 deletions(-)
src/chain.c +2 -15
index 14db5f7..621794c 100644
@@ -659,19 +659,6 @@ int nft_chain_parse(struct nft_chain *c, enum nft_chain_parse_type type,
 }
 EXPORT_SYMBOL(nft_chain_parse);
 
-static const char *policy2str(int policy)
-{
-	switch (policy) {
-	case NF_ACCEPT:
-		return "accept";
-	case NF_DROP:
-		return "drop";
-	default:
-		break;
-	}
-	return "unknown";
-}
-
 static int nft_chain_snprintf_json(char *buf, size_t size, struct nft_chain *c)
 {
 	int ret, len = size, offset = 0;
@@ -698,7 +685,7 @@ static int nft_chain_snprintf_json(char *buf, size_t size, struct nft_chain *c)
 				"\"prio\": %d,"
 				"\"policy\": \"%s\"",
 			c->type, hooknum2str_array[c->hooknum], c->prio,
-			policy2str(c->policy));
+			nft_verdict2str(c->policy));
 		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
 	}
 
@@ -762,7 +749,7 @@ static int nft_chain_snprintf_default(char *buf, size_t size,
 			       " type %s hook %s prio %d policy %s use %d "
 			       "packets %"PRIu64" bytes %"PRIu64"",
 			       c->type, hooknum2str_array[c->hooknum], c->prio,
-			       policy2str(c->policy), c->use,
+			       nft_verdict2str(c->policy), c->use,
 			       c->packets, c->bytes);
 		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
 	}

--
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/7 jansson: Add helper function for building the tree

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

Add a helper function for parsing and return the jansson tree

---
 src/internal.h |    1 +
 src/jansson.c  |   19 +++++++++++++++++++
 src/table.c    |   14 +++-----------
 3 files changed, 23 insertions(+), 11 deletions(-)
src/internal.h +1 -0
index a8ae431..d1c7690 100644
@@ -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);
src/jansson.c +19 -0
index cc68ae0..4c778d9 100644
@@ -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
src/table.c +3 -11
index 1f4fe76..526f3e7 100644
@@ -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)

--
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

PATCH 3/7 table: remove the properties node in Json output

In reply to Alvaro Neira

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

I have removed the properties node from table because it's a node without relevant information

---
 src/table.c                   |   12 ++----------
 tests/jsonfiles/01-table.json |    2 +-
 tests/jsonfiles/02-table.json |    2 +-
 3 files changed, 4 insertions(+), 12 deletions(-)
src/table.c +2 -10
index 526f3e7..402e8c1 100644
@@ -300,12 +300,6 @@ static int nft_table_json_parse(struct nft_table *t, char *json)
 
 	nft_table_attr_set_str(t, NFT_TABLE_ATTR_NAME, strdup(str));
 
-	root = json_object_get(root, "properties");
-	if (root == NULL) {
-		errno = EINVAL;
-		goto err;
-	}
-
 	str = nft_jansson_value_parse_str(root, "family");
 	if (str == NULL)
 		goto err;
@@ -360,10 +354,8 @@ static int nft_table_snprintf_json(char *buf, size_t size, struct nft_table *t)
 	return snprintf(buf, size,
 			"{\"table\" : {"
 			"\"name\" : \"%s\","
-			"\"properties\" : {"
-				"\"family\" : \"%s\","
-				"\"table_flags\" : %d"
-				"}"
+			"\"family\" : \"%s\","
+			"\"table_flags\" : %d"
 			"}"
 			"}" ,
 			t->name, nft_family2str(t->family), t->table_flags);
tests/jsonfiles/01-table.json +1 -1
index 9afc014..a5ba38b 100644
@@ -1 +1 @@
-{"table" : {"name" : "filter","properties" : {"family" : "ip","table_flags" : 0}}}
+{"table" : {"name" : "filter","family" : "ip","table_flags" : 0}}
tests/jsonfiles/02-table.json +1 -1
index b66ec52..f1171a1 100644
@@ -1 +1 @@
-{"table" : {"name" : "filter2","properties" : {"family" : "ip6","table_flags" : 0}}}
+{"table" : {"name" : "filter2","family" : "ip6","table_flags" : 0}}

--
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 4/7 chain: remove the properties node in Json output

In reply to Alvaro Neira

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

I have removed the properties node from chain because it's a node without relevant information

---
 src/chain.c |    8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
src/chain.c +3 -5
index 621794c..7e2567b 100644
@@ -669,10 +669,9 @@ static int nft_chain_snprintf_json(char *buf, size_t size, struct nft_chain *c)
 			"\"handle\": %"PRIu64","
 			"\"bytes\": %"PRIu64","
 			"\"packets\": %"PRIu64","
-			"\"properties\": {"
-				"\"family\": \"%s\","
-				"\"table\": \"%s\","
-				"\"use\": %d",
+			"\"family\": \"%s\","
+			"\"table\": \"%s\","
+			"\"use\": %d",
 			c->name, c->handle, c->bytes, c->packets,
 			nft_family2str(c->family),
 			c->table, c->use);
@@ -690,7 +689,6 @@ static int nft_chain_snprintf_json(char *buf, size_t size, struct nft_chain *c)
 	}
 
 	ret = snprintf(buf+offset, size,
-			"}"
 		"}"
 		"}");
 	SNPRINTF_BUFFER_SIZE(ret, size, len, offset);

--
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

#4 lore raw

PATCH 5/7 chain:Add json parser support

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                 |  117 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 118 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 +117 -0

129 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 6/7 test:chain:test json parsing 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..0e71e8f
@@ -0,0 +1 @@
+{ "chain": {"name": "input","handle": 1,"bytes": 1375696,"packets": 4136,"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..e841032
@@ -0,0 +1 @@
+{ "chain": {"name": "forward","handle": 2,"bytes": 0,"packets": 0,"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..9967233
@@ -0,0 +1 @@
+{ "chain": {"name": "output","handle": 3,"bytes": 454786,"packets": 2681,"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..716d022
@@ -0,0 +1 @@
+{ "chain": {"name": "chain1","handle": 4,"bytes": 0,"packets": 0,"family": "ip","table": "filter","use": 0}}
tests/nft-parsing-test.c +9 -0
index 0734f07..e111fd0 100644
@@ -24,6 +24,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;
@@ -47,6 +48,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

#6 lore raw

PATCH 7/7 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 |  118 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 122 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 +118 -0

118 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