netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alvaro Neira <alvaroneay@gmail.com>
To: netfilter-devel@vger.kernel.org
Cc: eric@regit.org
Subject: [libnftables PATCH 2/2] chain: json: use string to identify policy
Date: Mon, 15 Jul 2013 21:31:00 +0200	[thread overview]
Message-ID: <20130715193100.20241.23433.stgit@Ph0enix> (raw)
In-Reply-To: <20130715193052.20241.52862.stgit@Ph0enix>

From: Álvaro Neira Ayuso <alvaroneay@gmail.com>

* if we don't have hooknum we don't need to print the policy tag
* If we have hooknum, i have used the policy2str function for printing the policy with
"accept" string or "drop" string

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
---
 src/chain.c |   66 ++++++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 40 insertions(+), 26 deletions(-)

diff --git a/src/chain.c b/src/chain.c
index bdbaf60..e2738d5 100644
--- a/src/chain.c
+++ b/src/chain.c
@@ -717,9 +717,24 @@ 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)
 {
-	return snprintf(buf, size,
+	int ret, len = size, offset = 0;
+
+	ret = snprintf(buf, size,
 		"{ \"chain\": {"
 			"\"name\": \"%s\","
 			"\"handle\": %"PRIu64","
@@ -727,20 +742,32 @@ static int nft_chain_snprintf_json(char *buf, size_t size, struct nft_chain *c)
 			"\"packets\": %"PRIu64","
 			"\"version\": %d,"
 			"\"properties\": {"
-				"\"type\" : \"%s\","
-				"\"table\" : \"%s\","
-				"\"prio\" : %d,"
-				"\"use\" : %d,"
-				"\"hooknum\" : \"%s\","
-				"\"policy\" : %d,"
-				"\"family\" : \"%s\""
+				"\"family\": \"%s\","
+				"\"table\": \"%s\","
+				"\"use\": %d",
+			c->name, c->handle, c->bytes, c->packets,
+			NFT_CHAIN_JSON_VERSION, c->table,
+			nft_family2str(c->family), c->use);
+		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+
+	if (c->flags & (1 << NFT_CHAIN_ATTR_HOOKNUM)) {
+		ret =  snprintf(buf+offset, size,
+				",\"type\": \"%s\","
+				"\"hooknum\": \"%s\","
+				"\"prio\": %d,"
+				"\"policy\": \"%s\"",
+			c->type, hooknum2str_array[c->hooknum], c->prio,
+			policy2str(c->policy));
+		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+	}
+
+	ret = snprintf(buf+offset, size,
 			"}"
 		"}"
-		"}",
-			c->name, c->handle, c->bytes, c->packets,
-			NFT_CHAIN_JSON_VERSION, c->type, c->table,
-			c->prio, c->use, hooknum2str_array[c->hooknum],
-			c->policy, nft_family2str(c->family));
+		"}");
+	SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+
+	return offset;
 }
 
 static int nft_chain_snprintf_xml(char *buf, size_t size, struct nft_chain *c)
@@ -780,19 +807,6 @@ static int nft_chain_snprintf_xml(char *buf, size_t size, struct nft_chain *c)
 	return offset;
 }
 
-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_default(char *buf, size_t size,
 				      struct nft_chain *c)
 {

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

  reply	other threads:[~2013-07-15 19:31 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-07-15 19:30 [libnftables PATCH 1/2] set: json: fix incomplete output Alvaro Neira
2013-07-15 19:31 ` Alvaro Neira [this message]
2013-07-15 22:11   ` [libnftables PATCH 2/2] chain: json: use string to identify policy Pablo Neira Ayuso
2013-07-15 22:11 ` [libnftables PATCH 1/2] set: json: fix incomplete output Pablo Neira Ayuso

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20130715193100.20241.23433.stgit@Ph0enix \
    --to=alvaroneay@gmail.com \
    --cc=eric@regit.org \
    --cc=netfilter-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).