netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ana Rey <anarey@gmail.com>
To: netfilter-devel@vger.kernel.org
Cc: Ana Rey <anarey@gmail.com>
Subject: [libnftnl PATCH 3/4] expr: payload: Do not print unset values in xml file
Date: Sun, 22 Jun 2014 20:32:20 +0200	[thread overview]
Message-ID: <1403461941-21605-4-git-send-email-anarey@gmail.com> (raw)
In-Reply-To: <1403461941-21605-1-git-send-email-anarey@gmail.com>

It changes the parse and snprintf functions to omit unset values.

Signed-off-by: Ana Rey <anarey@gmail.com>
---
 src/expr/payload.c | 54 +++++++++++++++++++++++++++++-------------------------
 1 file changed, 29 insertions(+), 25 deletions(-)

diff --git a/src/expr/payload.c b/src/expr/payload.c
index 6c08e48..3cf2362 100644
--- a/src/expr/payload.c
+++ b/src/expr/payload.c
@@ -247,32 +247,25 @@ nft_rule_expr_payload_xml_parse(struct nft_rule_expr *e, mxml_node_t *tree,
 	uint32_t dreg, offset, len;
 
 	if (nft_mxml_reg_parse(tree, "dreg", &dreg, MXML_DESCEND_FIRST,
-			       NFT_XML_MAND, err) != 0)
-		return -1;
-
-	nft_rule_expr_set_u32(e, NFT_EXPR_PAYLOAD_DREG, dreg);
+			       NFT_XML_MAND, err) == 0)
+		nft_rule_expr_set_u32(e, NFT_EXPR_PAYLOAD_DREG, dreg);
 
 	base_str = nft_mxml_str_parse(tree, "base", MXML_DESCEND_FIRST,
 				      NFT_XML_MAND, err);
-	if (base_str == NULL)
-		return -1;
-
-	base = nft_str2base(base_str);
-	if (base < 0)
-		return -1;
-
-	nft_rule_expr_set_u32(e, NFT_EXPR_PAYLOAD_BASE, base);
+	if (base_str != NULL) {
+		base = nft_str2base(base_str);
+		if (base >= 0)
+			nft_rule_expr_set_u32(e, NFT_EXPR_PAYLOAD_BASE, base);
+	}
 
 	if (nft_mxml_num_parse(tree, "offset", MXML_DESCEND_FIRST, BASE_DEC,
-			       &offset, NFT_TYPE_U32, NFT_XML_MAND, err) != 0)
-		return -1;
-	nft_rule_expr_set_u32(e, NFT_EXPR_PAYLOAD_OFFSET, offset);
+			       &offset, NFT_TYPE_U32, NFT_XML_MAND, err) == 0)
+		nft_rule_expr_set_u32(e, NFT_EXPR_PAYLOAD_OFFSET, offset);
 
 
 	if (nft_mxml_num_parse(tree, "len", MXML_DESCEND_FIRST, BASE_DEC,
-			       &len, NFT_TYPE_U32, NFT_XML_MAND, err) != 0)
-		return -1;
-	nft_rule_expr_set_u32(e, NFT_EXPR_PAYLOAD_LEN, len);
+			       &len, NFT_TYPE_U32, NFT_XML_MAND, err) == 0)
+		nft_rule_expr_set_u32(e, NFT_EXPR_PAYLOAD_LEN, len);
 
 	return 0;
 #else
@@ -288,13 +281,24 @@ nft_rule_expr_payload_snprintf_xml(char *buf, size_t len, uint32_t flags,
 	struct nft_expr_payload *payload = nft_expr_data(e);
 	int size = len, offset = 0, ret;
 
-	ret = snprintf(buf, len, "<dreg>%u</dreg>"
-				 "<offset>%u</offset>"
-				 "<len>%u</len>"
-				 "<base>%s</base>",
-		       payload->dreg, payload->offset, payload->len,
-		       base2str(payload->base));
-	SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+	if (e->flags & (1 << NFT_EXPR_PAYLOAD_DREG)) {
+		ret = snprintf(buf, len, "<dreg>%u</dreg>", payload->dreg);
+		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+	}
+	if (e->flags & (1 << NFT_EXPR_PAYLOAD_OFFSET)) {
+		ret = snprintf(buf + offset, len, "<offset>%u</offset>",
+			       payload->offset);
+		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+	}
+	if (e->flags & (1 << NFT_EXPR_PAYLOAD_LEN)) {
+		ret = snprintf(buf + offset, len, "<len>%u</len>", payload->len);
+		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+	}
+	if (e->flags & (1 << NFT_EXPR_PAYLOAD_BASE)) {
+		ret = snprintf(buf + offset, len, "<base>%s</base>",
+			       base2str(payload->base));
+		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+	}
 
 	return offset;
 }
-- 
2.0.0


  parent reply	other threads:[~2014-06-22 18:32 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-22 18:32 [libnftnl PATCH 0/4] expr: payload: Do not print unset values in xml and json Ana Rey
2014-06-22 18:32 ` [libnftnl PATCH 1/4] expr: payload: Use nft_rule_expr_set_* in the xml parsing code Ana Rey
2014-06-22 18:32 ` [libnftnl PATCH 2/4] expr: payload: get a nft_rule_expr type in nft_rule_expr_payload_snprintf_* functions Ana Rey
2014-06-22 18:32 ` Ana Rey [this message]
2014-06-22 18:32 ` [libnftnl PATCH 4/4] expr: payload: Do not print unset values in json file Ana Rey
2014-06-23  9:15 ` [libnftnl PATCH 0/4] expr: payload: Do not print unset values in xml and json Arturo Borrero Gonzalez

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=1403461941-21605-4-git-send-email-anarey@gmail.com \
    --to=anarey@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).