Skip to contentSkip to conversation

Series short description

PATCH Applied Alvaro Neira 2 participants 5 messages Jun 8, 2013 netfilter

lore
Share & tools
lore
#0 lore raw

cover Series short description

Add the implementation for exporting chains to JSON Format and for to test the function.

---

Alvaro Neira Ayuso (2):
Add function for exporting chain to JSON Format
Implementation for to test the function for exporting chains to JSON format

 examples/nft-chain-get.c    |    5 ++++-
 include/libnftables/chain.h |    1 +
 src/chain.c                 |   27 +++++++++++++++++++++++++++
 src/internal.h              |    1 +
 4 files changed, 33 insertions(+), 1 deletion(-)
Signature · 5 lines
Álvaro Neira Ayuso
--
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 1/2 Add function for exporting chain to JSON Format

From: Alvaro Neira Ayuso <[email protected]>

---
 include/libnftables/chain.h |    1 +
 src/chain.c                 |   27 +++++++++++++++++++++++++++
 src/internal.h              |    1 +
 3 files changed, 29 insertions(+)
include/libnftables/chain.h +1 -0
index f06f743..3645eaa 100644
@@ -44,6 +44,7 @@ void nft_chain_nlmsg_build_payload(struct nlmsghdr *nlh, const struct nft_chain
 enum {
 	NFT_CHAIN_O_DEFAULT	= 0,
 	NFT_CHAIN_O_XML,
+	NFT_CHAIN_O_JSON,
 };
 
 enum nft_chain_parse_type {
src/chain.c +27 -0
index 7775dfc..1c53daa 100644
@@ -688,6 +688,31 @@ int nft_chain_parse(struct nft_chain *c, enum nft_chain_parse_type type,
 }
 EXPORT_SYMBOL(nft_chain_parse);
 
+static int nft_chain_snprintf_json(char *buf, size_t size, struct nft_chain *c)
+{
+	return snprintf(buf, size,
+		"{ \"chain\": {"
+			"\"name\": \"%s\","
+			"\"handle\": %lu,"
+			"\"bytes\": %lu,"
+			"\"packets\": %lu,"
+			"\"version\": %d,"
+			"\"properties\": {"
+				"\"type\" : \"%s\","
+				"\"table\" : \"%s\","
+				"\"prio\" : %d,"
+				"\"use\" : %d,"
+				"\"hooknum\" : %d,"
+				"\"policy\" : %d,"
+				"\"family\" : %d"
+			"}"
+		"}"
+		"}",
+			c->name, c->handle, c->bytes, c->packets,
+			NFT_CHAIN_JSON_VERSION, c->type, c->table,
+			c->prio, c->use, c->hooknum, c->policy, c->family);
+}
+
 static int nft_chain_snprintf_xml(char *buf, size_t size, struct nft_chain *c)
 {
 	return snprintf(buf, size,
@@ -721,6 +746,8 @@ int nft_chain_snprintf(char *buf, size_t size, struct nft_chain *c,
 		       uint32_t type, uint32_t flags)
 {
 	switch(type) {
+	case NFT_CHAIN_O_JSON:
+		return nft_chain_snprintf_json(buf, size, c);
 	case NFT_CHAIN_O_XML:
 		return nft_chain_snprintf_xml(buf, size, c);
 	case NFT_CHAIN_O_DEFAULT:
src/internal.h +1 -0
index 0c5de21..769926b 100644
@@ -21,6 +21,7 @@
 #define NFT_CHAIN_XML_VERSION 0
 #define NFT_RULE_XML_VERSION 0
 #define NFT_TABLE_JSON_VERSION 0
+#define NFT_CHAIN_JSON_VERSION 0
 
 struct expr_ops;
 
maintainer Applied
#2 lore raw

Re: [libnftables PATCH 1/2] Add function for exporting chain to JSON Format

Applied, thanks.

#3 lore raw

PATCH 2/2 Implementation for to test the function for exporting chains to JSON format

In reply to Alvaro Neira

From: Alvaro Neira Ayuso <[email protected]>

---
 examples/nft-chain-get.c |    5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
examples/nft-chain-get.c +4 -1
index b815523..459b79e 100644
@@ -90,8 +90,11 @@ int main(int argc, char *argv[])
 		exit(EXIT_FAILURE);
 	}
 
-	if (strcmp(argv[argc-1], "xml") == 0)
+	if (strcmp(argv[argc-1], "xml") == 0){
 		type = NFT_CHAIN_O_XML;
+	}else if (strcmp(argv[argc-1], "json") == 0){
+		type = NFT_CHAIN_O_JSON;
+	}
 
 	nl = mnl_socket_open(NETLINK_NETFILTER);
 	if (nl == NULL) {