[libnftnl PATCH 3/4] expr: counter: Do not print unset values in xml

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

Signed-off-by: Ana Rey <anarey@xxxxxxxxx>
---
 src/expr/counter.c | 27 ++++++++++++++++-----------
 1 file changed, 16 insertions(+), 11 deletions(-)

diff --git a/src/expr/counter.c b/src/expr/counter.c
index 9d85a96..6f6a7ee 100644
--- a/src/expr/counter.c
+++ b/src/expr/counter.c
@@ -150,16 +150,12 @@ nft_rule_expr_counter_xml_parse(struct nft_rule_expr *e, mxml_node_t *tree,
 	uint64_t pkts, bytes;
 
 	if (nft_mxml_num_parse(tree, "pkts", MXML_DESCEND_FIRST, BASE_DEC,
-			       &pkts, NFT_TYPE_U64, NFT_XML_MAND,
-			       err) != 0)
-		return -1;
-	nft_rule_expr_set_u64(e, NFT_EXPR_CTR_PACKETS, pkts);
+			       &pkts, NFT_TYPE_U64, NFT_XML_MAND,  err) == 0)
+		nft_rule_expr_set_u64(e, NFT_EXPR_CTR_PACKETS, pkts);
 
 	if (nft_mxml_num_parse(tree, "bytes", MXML_DESCEND_FIRST, BASE_DEC,
-			       &bytes, NFT_TYPE_U64, NFT_XML_MAND,
-			       err) != 0)
-		return -1;
-	nft_rule_expr_set_u64(e, NFT_EXPR_CTR_BYTES, bytes);
+			       &bytes, NFT_TYPE_U64, NFT_XML_MAND, err) == 0)
+		nft_rule_expr_set_u64(e, NFT_EXPR_CTR_BYTES, bytes);
 
 	return 0;
 #else
@@ -179,11 +175,20 @@ static int nft_rule_expr_counter_snprintf_json(char *buf, size_t len,
 static int nft_rule_expr_counter_snprintf_xml(char *buf, size_t len,
 					      struct nft_rule_expr *e)
 {
+	int ret, size = len, offset = 0;
 	struct nft_expr_counter *ctr = nft_expr_data(e);
 
-	return snprintf(buf, len, "<pkts>%"PRIu64"</pkts>"
-				  "<bytes>%"PRIu64"</bytes>",
-			ctr->pkts, ctr->bytes);
+	if (e->flags & (1 << NFT_EXPR_CTR_PACKETS)) {
+		ret = snprintf(buf, len, "<pkts>%"PRIu64"</pkts>", ctr->pkts);
+		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+	}
+	if (e->flags & (1 << NFT_EXPR_CTR_BYTES)) {
+		ret = snprintf(buf + offset, len, "<bytes>%"PRIu64"</bytes>",
+			       ctr->bytes);
+		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+	}
+
+	return offset;
 }
 
 static int nft_rule_expr_counter_snprintf_default(char *buf, size_t len,
-- 
2.0.0

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




[Index of Archives]     [Netfitler Users]     [LARTC]     [Bugtraq]     [Yosemite Forum]

  Powered by Linux