[libnftnl PATCH 3/4] expr: limit: Do not print unset values in json

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

 



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

Now, This part of a json file is gotten without unset elements.

{"expr":[{"type":"limit","rate":400,"unit":60}]}

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

diff --git a/src/expr/limit.c b/src/expr/limit.c
index 6e0a732..6732318 100644
--- a/src/expr/limit.c
+++ b/src/expr/limit.c
@@ -124,15 +124,11 @@ static int nft_rule_expr_limit_json_parse(struct nft_rule_expr *e, json_t *root,
 #ifdef JSON_PARSING
 	uint64_t uval64;
 
-	if (nft_jansson_parse_val(root, "rate", NFT_TYPE_U64, &uval64, err) < 0)
-		return -1;
-
-	nft_rule_expr_set_u64(e, NFT_EXPR_LIMIT_RATE, uval64);
+	if (nft_jansson_parse_val(root, "rate", NFT_TYPE_U64, &uval64, err) == 0)
+		nft_rule_expr_set_u64(e, NFT_EXPR_LIMIT_RATE, uval64);
 
-	if (nft_jansson_parse_val(root, "unit", NFT_TYPE_U64, &uval64, err) < 0)
-		return -1;
-
-	nft_rule_expr_set_u64(e, NFT_EXPR_LIMIT_UNIT, uval64);
+	if (nft_jansson_parse_val(root, "unit", NFT_TYPE_U64, &uval64, err) == 0)
+		nft_rule_expr_set_u64(e, NFT_EXPR_LIMIT_UNIT, uval64);
 
 	return 0;
 #else
@@ -191,10 +187,24 @@ static int nft_rule_expr_limit_snprintf_xml(char *buf, size_t len,
 static int nft_rule_expr_limit_snprintf_json(char *buf, size_t len,
 					    struct nft_rule_expr *e) {
 	struct nft_expr_limit *limit = nft_expr_data(e);
+	int ret, size = len, offset = 0;
 
-	return snprintf(buf, len, "\"rate\":%"PRIu64","
-				  "\"unit\":%"PRIu64"",
-			limit->rate, limit->unit);
+	if (e->flags & (1 << NFT_EXPR_LIMIT_RATE)) {
+		ret = snprintf(buf + offset, len, "\"rate\":%"PRIu64",",
+			       limit->rate);
+		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+	}
+	if (e->flags & (1 << NFT_EXPR_LIMIT_UNIT)) {
+		ret = snprintf(buf + offset, len, "\"unit\":%"PRIu64",",
+			       limit->unit);
+		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+	}
+
+	/* Remove the last comma characther */
+	if (offset > 0)
+		offset--;
+
+	return offset;
 }
 
 static int nft_rule_expr_limit_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