[libnftnl PATCH 4/4] expr: meta: Do not print unset values in json file

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

 



It changes the parse and snprintf functions to omit unset values.
Moreover, It adds some whitespaces arount '+' caracter, as the CodingStyle
recommends.

Signed-off-by: Ana Rey <anarey@xxxxxxxxx>
---
 src/expr/meta.c | 33 +++++++++++++--------------------
 1 file changed, 13 insertions(+), 20 deletions(-)

diff --git a/src/expr/meta.c b/src/expr/meta.c
index 95d538c..248686b 100644
--- a/src/expr/meta.c
+++ b/src/expr/meta.c
@@ -188,29 +188,22 @@ static int nft_rule_expr_meta_json_parse(struct nft_rule_expr *e, json_t *root,
 	int key;
 
 	key_str = nft_jansson_parse_str(root, "key", err);
-	if (key_str == NULL)
-		return -1;
-
-	key = str2meta_key(key_str);
-	if (key < 0)
-		return -1;
-
-	nft_rule_expr_set_u32(e, NFT_EXPR_META_KEY, key);
+	if (key_str != NULL) {
+		key = str2meta_key(key_str);
+		if (key >= 0)
+			nft_rule_expr_set_u32(e, NFT_EXPR_META_KEY, key);
+	}
 
 	if (nft_jansson_node_exist(root, "dreg")) {
 		if (nft_jansson_parse_reg(root, "dreg", NFT_TYPE_U32, &reg,
-					  err) < 0)
-			return -1;
-
-		nft_rule_expr_set_u32(e, NFT_EXPR_META_DREG, reg);
+					  err) == 0)
+			nft_rule_expr_set_u32(e, NFT_EXPR_META_DREG, reg);
 	}
 
 	if (nft_jansson_node_exist(root, "sreg")) {
 		if (nft_jansson_parse_reg(root, "sreg", NFT_TYPE_U32, &reg,
-					  err) < 0)
-			return -1;
-
-		nft_rule_expr_set_u32(e, NFT_EXPR_META_SREG, reg);
+					  err) == 0)
+			nft_rule_expr_set_u32(e, NFT_EXPR_META_SREG, reg);
 	}
 
 	return 0;
@@ -304,19 +297,19 @@ nft_rule_expr_meta_snprintf_json(char *buf, size_t size,
 	struct nft_expr_meta *meta = nft_expr_data(e);
 
 	if (e->flags & (1 << NFT_EXPR_META_DREG)) {
-		ret = snprintf(buf+offset, len, "\"dreg\":%u,",
+		ret = snprintf(buf + offset, len, "\"dreg\":%u,",
 			       meta->dreg);
 		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
 	}
 
 	if (e->flags & (1 << NFT_EXPR_META_KEY)) {
-		ret = snprintf(buf+offset, len, "\"key\":\"%s\",",
-						meta_key2str(meta->key));
+		ret = snprintf(buf + offset, len, "\"key\":\"%s\",",
+			       meta_key2str(meta->key));
 		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
 	}
 
 	if (e->flags & (1 << NFT_EXPR_META_SREG)) {
-		ret = snprintf(buf+offset, len, "\"sreg\":%u,",
+		ret = snprintf(buf + offset, len, "\"sreg\":%u,",
 			       meta->sreg);
 		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
 	}
-- 
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