netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alvaro Neira Ayuso <alvaroneay@gmail.com>
To: netfilter-devel@vger.kernel.org
Subject: [libnftnl PATCH v2 2/2] rule: Changed parser for being more flexible
Date: Thu, 13 Mar 2014 23:12:04 +0100	[thread overview]
Message-ID: <1394748724-7169-1-git-send-email-alvaroneay@gmail.com> (raw)

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

This change allow us to parser the rule and the kernel
bail out if the rule is well-formed.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
---
v2: Fixed some identation errors and used the function
nft_rule_attr_set_xx() for parsing the attributes in xml.

 src/rule.c |   72 ++++++++++++++++++++++++++----------------------------------
 1 file changed, 31 insertions(+), 41 deletions(-)

diff --git a/src/rule.c b/src/rule.c
index 3aaee71..1dce1d5 100644
--- a/src/rule.c
+++ b/src/rule.c
@@ -540,28 +540,36 @@ int nft_jansson_parse_rule(struct nft_rule *r, json_t *tree,
 	if (root == NULL)
 		return -1;
 
-	if (nft_jansson_parse_family(root, &family, err) != 0)
-		goto err;
+	if (nft_jansson_node_exist(root, "family")) {
+		if (nft_jansson_parse_family(root, &family, err) != 0)
+			goto err;
 
-	nft_rule_attr_set_u32(r, NFT_RULE_ATTR_FAMILY, family);
+		nft_rule_attr_set_u32(r, NFT_RULE_ATTR_FAMILY, family);
+	}
 
-	str = nft_jansson_parse_str(root, "table", err);
-	if (str == NULL)
-		goto err;
+	if (nft_jansson_node_exist(root, "table")) {
+		str = nft_jansson_parse_str(root, "table", err);
+		if (str == NULL)
+			goto err;
 
-	nft_rule_attr_set_str(r, NFT_RULE_ATTR_TABLE, str);
+		nft_rule_attr_set_str(r, NFT_RULE_ATTR_TABLE, str);
+	}
 
-	str = nft_jansson_parse_str(root, "chain", err);
-	if (str == NULL)
-		goto err;
+	if (nft_jansson_node_exist(root, "chain")) {
+		str = nft_jansson_parse_str(root, "chain", err);
+		if (str == NULL)
+			goto err;
 
-	nft_rule_attr_set_str(r, NFT_RULE_ATTR_CHAIN, str);
+		nft_rule_attr_set_str(r, NFT_RULE_ATTR_CHAIN, str);
+	}
 
-	if (nft_jansson_parse_val(root, "handle", NFT_TYPE_U64, &uval64,
-				  err) < 0)
-		goto err;
+	if (nft_jansson_node_exist(root, "handle")) {
+		if (nft_jansson_parse_val(root, "handle", NFT_TYPE_U64, &uval64,
+					  err) < 0)
+			goto err;
 
-	nft_rule_attr_set_u64(r, NFT_RULE_ATTR_HANDLE, uval64);
+		nft_rule_attr_set_u64(r, NFT_RULE_ATTR_HANDLE, uval64);
+	}
 
 	if (nft_jansson_node_exist(root, "compat_proto") ||
 	    nft_jansson_node_exist(root, "compat_flags")) {
@@ -640,39 +648,22 @@ int nft_mxml_rule_parse(mxml_node_t *tree, struct nft_rule *r,
 
 	family = nft_mxml_family_parse(tree, "family", MXML_DESCEND_FIRST,
 				       NFT_XML_MAND, err);
-	if (family < 0)
-		return -1;
-
-	r->family = family;
-	r->flags |= (1 << NFT_RULE_ATTR_FAMILY);
+	if (family >= 0)
+		nft_rule_attr_set_u32(r, NFT_RULE_ATTR_FAMILY, family);
 
 	table = nft_mxml_str_parse(tree, "table", MXML_DESCEND_FIRST,
 				   NFT_XML_MAND, err);
-	if (table == NULL)
-		return -1;
-
-	if (r->table)
-		xfree(r->table);
-
-	r->table = strdup(table);
-	r->flags |= (1 << NFT_RULE_ATTR_TABLE);
+	if (table != NULL)
+		nft_rule_attr_set_str(r, NFT_RULE_ATTR_TABLE, table);
 
 	chain = nft_mxml_str_parse(tree, "chain", MXML_DESCEND_FIRST,
 				   NFT_XML_MAND, err);
-	if (chain == NULL)
-		return -1;
-
-	if (r->chain)
-		xfree(r->chain);
-
-	r->chain = strdup(chain);
-	r->flags |= (1 << NFT_RULE_ATTR_CHAIN);
+	if (chain != NULL)
+		nft_rule_attr_set_str(r, NFT_RULE_ATTR_CHAIN, chain);
 
 	if (nft_mxml_num_parse(tree, "handle", MXML_DESCEND_FIRST, BASE_DEC,
-			       &r->handle, NFT_TYPE_U64, NFT_XML_MAND, err) != 0)
-		return -1;
-
-	r->flags |= (1 << NFT_RULE_ATTR_HANDLE);
+			      &r->handle, NFT_TYPE_U64, NFT_XML_MAND, err) >= 0)
+		r->flags |= (1 << NFT_RULE_ATTR_HANDLE);
 
 	if (nft_mxml_num_parse(tree, "compat_proto", MXML_DESCEND_FIRST,
 			       BASE_DEC, &r->compat.proto, NFT_TYPE_U32,
@@ -687,7 +678,6 @@ int nft_mxml_rule_parse(mxml_node_t *tree, struct nft_rule *r,
 	if (nft_rule_attr_is_set(r, NFT_RULE_ATTR_COMPAT_PROTO) !=
 			nft_rule_attr_is_set(r, NFT_RULE_ATTR_COMPAT_FLAGS)) {
 		errno = EINVAL;
-		return -1;
 	}
 
 	if (nft_mxml_num_parse(tree, "position", MXML_DESCEND_FIRST,
-- 
1.7.10.4

--
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:[~2014-03-13 22:12 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-03-13 22:12 Alvaro Neira Ayuso [this message]
2014-03-17 11:53 ` [libnftnl PATCH v2 2/2] rule: Changed parser for being more flexible 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=1394748724-7169-1-git-send-email-alvaroneay@gmail.com \
    --to=alvaroneay@gmail.com \
    --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).