netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
To: netfilter-devel@vger.kernel.org
Cc: pablo@netfilter.org
Subject: [libnftables PATCH 12/21] chain: xml: use string for policy
Date: Wed, 26 Jun 2013 13:37:10 +0200	[thread overview]
Message-ID: <20130626113710.23511.49021.stgit@nfdev.cica.es> (raw)
In-Reply-To: <20130626113509.23511.14359.stgit@nfdev.cica.es>

Now the <policy> node is using "accept" or "drop".

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
---
 src/chain.c               |   52 +++++++++++++++++++++++++++++++--------------
 test/nft-chain-xml-add.sh |    6 +++--
 2 files changed, 39 insertions(+), 19 deletions(-)

diff --git a/src/chain.c b/src/chain.c
index ce9bb1b..a66ffe9 100644
--- a/src/chain.c
+++ b/src/chain.c
@@ -660,13 +660,16 @@ static int nft_chain_xml_parse(struct nft_chain *c, char *xml)
 		mxmlDelete(tree);
 		return -1;
 	}
-	utmp = strtoull(node->child->value.opaque, &endptr, 10);
-	if (utmp > UINT32_MAX || utmp < 0 || *endptr) {
+
+	if (strcmp(node->child->value.opaque, "accept") == 0) {
+		c->policy = NF_ACCEPT;
+	} else if (strcmp(node->child->value.opaque, "drop") == 0) {
+		c->policy = NF_DROP;
+	} else {
 		mxmlDelete(tree);
 		return -1;
 	}
 
-	c->policy = (uint32_t)utmp;
 	c->flags |= (1 << NFT_CHAIN_ATTR_POLICY);
 
 	/* Get and set <family> */
@@ -739,23 +742,40 @@ static int nft_chain_snprintf_json(char *buf, size_t size, struct nft_chain *c)
 
 static int nft_chain_snprintf_xml(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>"
+	int ret, len = size, offset = 0;
+
+	ret = snprintf(buf, size,
+		       "<chain name=\"%s\" handle=\"%lu\""
+		       " bytes=\"%lu\" packets=\"%lu\" version=\"%d\">"
+		       "<properties>"
 				"<type>%s</type>"
 				"<table>%s</table>"
 				"<prio>%d</prio>"
 				"<use>%d</use>"
-				"<hooknum>%s</hooknum>"
-				"<policy>%d</policy>"
-				"<family>%s</family>"
-			"</properties>"
-		"</chain>",
-			c->name, c->handle, c->bytes, c->packets,
-			NFT_CHAIN_XML_VERSION, c->type, c->table,
-			c->prio, c->use, hooknum2str_array[c->hooknum],
-			c->policy, nft_family2str(c->family));
+				"<hooknum>%s</hooknum>",
+		       c->name, c->handle, c->bytes, c->packets,
+		       NFT_CHAIN_XML_VERSION, c->type, c->table,
+		       c->prio, c->use, hooknum2str_array[c->hooknum]);
+	SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+
+	/* The parsing will fail both if there are something different
+	 * than {accept|drop} or if the <policy> node is missing.
+	 */
+
+	if (c->policy == NF_ACCEPT) {
+		ret = snprintf(buf+offset, size, "<policy>accept</policy>");
+		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+	} else if (c->policy == NF_DROP) {
+		ret = snprintf(buf+offset, size, "<policy>drop</policy>");
+		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+	}
+
+	ret = snprintf(buf+offset, size, "<family>%s</family>"
+		       "</properties></chain>",
+		       nft_family2str(c->family));
+	SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+
+	return offset;
 }
 
 static int nft_chain_snprintf_default(char *buf, size_t size,
diff --git a/test/nft-chain-xml-add.sh b/test/nft-chain-xml-add.sh
index ab50e2b..ed39d54 100755
--- a/test/nft-chain-xml-add.sh
+++ b/test/nft-chain-xml-add.sh
@@ -41,7 +41,7 @@ XML="<chain name=\"test1\" handle=\"100\" bytes=\"123\" packets=\"321\" version=
                 <prio>0</prio>
                 <use>0</use>
                 <hooknum>NF_INET_LOCAL_IN</hooknum>
-                <policy>1</policy>
+                <policy>accept</policy>
                 <family>ip</family>
         </properties>
 </chain>"
@@ -62,7 +62,7 @@ XML="<chain name=\"test2\" handle=\"101\" bytes=\"59\" packets=\"1\" version=\"0
 		<prio>1</prio>
 		<use>0</use>
 		<hooknum>NF_INET_POST_ROUTING</hooknum>
-		<policy>1</policy>
+		<policy>accept</policy>
 		<family>ip6</family>
 	</properties>
 </chain>"
@@ -84,7 +84,7 @@ XML="<chain name=\"test3\" handle=\"102\" bytes=\"51231239\" packets=\"112312312
 		<prio>0</prio>
 		<use>0</use>
 		<hooknum>NF_INET_FORWARD</hooknum>
-		<policy>1</policy>
+		<policy>drop</policy>
 		<family>ip</family>
 	</properties>
 </chain>"


  parent reply	other threads:[~2013-06-26 11:37 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-06-26 11:36 [libnftables PATCH 00/21] Small fixes for XML Arturo Borrero Gonzalez
2013-06-26 11:36 ` [libnftables PATCH 01/21] chain: add hooknum2str Arturo Borrero Gonzalez
2013-06-26 11:36 ` [libnftables PATCH 02/21] src: xml: convert family values to string Arturo Borrero Gonzalez
2013-06-26 11:36 ` [libnftables PATCH 03/21] rule: xml: conditional compat info Arturo Borrero Gonzalez
2013-06-26 11:37 ` [libnftables PATCH 04/21] bitwise: xml: mask and xor use same number of data registers Arturo Borrero Gonzalez
2013-06-26 11:37 ` [libnftables PATCH 05/21] expr: xml: validate registers < NFT_REG_MAX Arturo Borrero Gonzalez
2013-06-26 11:37 ` [libnftables PATCH 06/21] nat: xml: change nat types string to dnat/snat Arturo Borrero Gonzalez
2013-06-26 11:37 ` [libnftables PATCH 07/21] nat: xml: change IP range node names Arturo Borrero Gonzalez
2013-06-26 11:37 ` [libnftables PATCH 08/21] byteorder: xml: op as string Arturo Borrero Gonzalez
2013-06-26 11:37 ` [libnftables PATCH 09/21] ct: xml: add extra dir check Arturo Borrero Gonzalez
2013-06-26 11:37 ` [libnftables PATCH 10/21] ct: xml: use key's name string instead of numbers Arturo Borrero Gonzalez
2013-06-26 11:37 ` [libnftables PATCH 11/21] exthdr: xml: fix mandatory elements Arturo Borrero Gonzalez
2013-06-26 11:37 ` Arturo Borrero Gonzalez [this message]
2013-06-26 11:37 ` [libnftables PATCH 13/21] data_reg: xml: len node shows byte length Arturo Borrero Gonzalez
2013-06-26 11:37 ` [libnftables PATCH 14/21] data_reg: xml: fix bytes movements Arturo Borrero Gonzalez
2013-06-26 11:37 ` [libnftables PATCH 15/21] target&match: xml: don't print rev number Arturo Borrero Gonzalez
2013-06-26 11:37 ` [libnftables PATCH 16/21] payload: xml: use string for base attribute Arturo Borrero Gonzalez
2013-06-26 11:37 ` [libnftables PATCH 17/21] exthdr: xml: use string for type node Arturo Borrero Gonzalez
2013-06-26 11:37 ` [libnftables PATCH 18/21] meta: xml: use string to represent key attribute Arturo Borrero Gonzalez
2013-06-26 11:37 ` [libnftables PATCH 19/21] nat: snprintf: fix buffer offset Arturo Borrero Gonzalez
2013-06-26 11:37 ` [libnftables PATCH 20/21] nat: xml: rename node type to nat_type Arturo Borrero Gonzalez
2013-06-26 11:37 ` [libnftables PATCH 21/21] exthdr: xml: rename type node to exthdr_type Arturo Borrero Gonzalez
2013-06-27 17:58 ` [libnftables PATCH 00/21] Small fixes for XML 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=20130626113710.23511.49021.stgit@nfdev.cica.es \
    --to=arturo.borrero.glez@gmail.com \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=pablo@netfilter.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).