Skip to contentSkip to conversation

limit: Do not print unset value

PATCH In review Ana Rey 2 participants 6 messages Jun 16, 2014 netfilter

lore
Share & tools
lore

Ana Rey

author
#0 lore raw

cover limit: Do not print unset value

This patchset does tasks about does not print unset value in xml and json file and about refactoring code:
"expr: limit: Add nft_rule_expr_limit_snprinf_* functions" and "expr: limit: Use nft_rule_expr_set_* in the xml parsing code"

Ana Rey (4):
expr: limit: Add nft_rule_expr_limit_snprinf_* functions
expr: limit: Use nft_rule_expr_set_* in the xml parsing code
expr: limit: Do not print unset values in json
expr: limit: Do not print unset values in xml

 src/expr/limit.c | 90 ++++++++++++++++++++++++++++++++++++++------------------
 1 file changed, 62 insertions(+), 28 deletions(-)
Signature · 1 line
2.0.0
author
#1 lore raw

PATCH 1/4 expr: limit: Add nft_rule_expr_limit_snprinf_* functions

Code refactoring in nft_rule_expr_limit_snprinf functions. This process adds three new functions:

* nft_rule_expr_limit_snprinf_default
* nft_rule_expr_limit_snprinf_xml
* nft_rule_expr_limit_snprinf_json

---
 src/expr/limit.c | 38 +++++++++++++++++++++++++++++---------
 1 file changed, 29 insertions(+), 9 deletions(-)
src/expr/limit.c +29 -9
index 7d10340..b7cd32d 100644
@@ -181,24 +181,44 @@ static const char *get_unit(uint64_t u)
 	return "error";
 }
 
+static int nft_rule_expr_limit_snprintf_xml(char *buf, size_t len,
+					    struct nft_rule_expr *e) {
+	struct nft_expr_limit *limit = nft_expr_data(e);
+
+	return snprintf(buf, len, "<rate>%"PRIu64"</rate>"
+				  "<unit>%"PRIu64"</unit>",
+			limit->rate, limit->unit);
+}
+
+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);
+
+	return snprintf(buf, len, "\"rate\":%"PRIu64","
+				  "\"unit\":%"PRIu64"",
+			limit->rate, limit->unit);
+}
+
+static int nft_rule_expr_limit_snprintf_default(char *buf, size_t len,
+						struct nft_rule_expr *e) {
+	struct nft_expr_limit *limit = nft_expr_data(e);
+
+	return snprintf(buf, len, "rate %"PRIu64"/%s ",
+			limit->rate, get_unit(limit->unit));
+}
+
 static int
 nft_rule_expr_limit_snprintf(char *buf, size_t len, uint32_t type,
 			    uint32_t flags, struct nft_rule_expr *e)
 {
-	struct nft_expr_limit *limit = nft_expr_data(e);
 
 	switch(type) {
 	case NFT_OUTPUT_DEFAULT:
-		return snprintf(buf, len, "rate %"PRIu64"/%s ",
-				limit->rate, get_unit(limit->unit));
+		return nft_rule_expr_limit_snprintf_default(buf, len, e);
 	case NFT_OUTPUT_XML:
-		return snprintf(buf, len, "<rate>%"PRIu64"</rate>"
-					  "<unit>%"PRIu64"</unit>",
-				limit->rate, limit->unit);
+		return nft_rule_expr_limit_snprintf_xml(buf, len, e);
 	case NFT_OUTPUT_JSON:
-		return snprintf(buf, len, "\"rate\":%"PRIu64","
-					  "\"unit\":%"PRIu64"",
-				limit->rate, limit->unit);
+		return nft_rule_expr_limit_snprintf_json(buf, len, e);
 	default:
 		break;
 	}
Signature · 1 line
2.0.0
author
#2 lore raw

PATCH 2/4 expr: limit: Use nft_rule_expr_set_* in the xml parsing code

In reply to Ana Rey

Code refactoring to use nft_rule_expr_set_* in parse functions.

---
 src/expr/limit.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
src/expr/limit.c +7 -9
index b7cd32d..6e0a732 100644
@@ -146,21 +146,19 @@ static int nft_rule_expr_limit_xml_parse(struct nft_rule_expr *e,
 					 struct nft_parse_err *err)
 {
 #ifdef XML_PARSING
-	struct nft_expr_limit *limit = nft_expr_data(e);
+	uint64_t rate, unit;
 
 	if (nft_mxml_num_parse(tree, "rate", MXML_DESCEND_FIRST, BASE_DEC,
-			       &limit->rate, NFT_TYPE_U64, NFT_XML_MAND,
-			       err) != 0)
+			       &rate, NFT_TYPE_U64, NFT_XML_MAND,
+			       err) < 0)
 		return -1;
-
-	e->flags |= (1 << NFT_EXPR_LIMIT_RATE);
+	nft_rule_expr_set_u64(e, NFT_EXPR_LIMIT_RATE, rate);
 
 	if (nft_mxml_num_parse(tree, "unit", MXML_DESCEND_FIRST, BASE_DEC,
-			       &limit->unit, NFT_TYPE_U64, NFT_XML_MAND,
-			       err) != 0)
+			       &unit, NFT_TYPE_U64, NFT_XML_MAND,
+			       err) < 0)
 		return -1;
-
-	e->flags |= (1 << NFT_EXPR_LIMIT_UNIT);
+	nft_rule_expr_set_u64(e, NFT_EXPR_LIMIT_UNIT, unit);
 
 	return 0;
 #else
Signature · 1 line
2.0.0
author
#3 lore raw

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

In reply to Ana Rey

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}]}

---
 src/expr/limit.c | 32 +++++++++++++++++++++-----------
 1 file changed, 21 insertions(+), 11 deletions(-)
src/expr/limit.c +21 -11
index 6e0a732..6732318 100644
@@ -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,
Signature · 1 line
2.0.0
author
#4 lore raw

PATCH 4/4 expr: limit: Do not print unset values in xml

In reply to Ana Rey

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

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

<expr type="limit">

	<rate>400</rate>
	<unit>60</unit>

</expr>

---
 src/expr/limit.c | 28 +++++++++++++++++-----------
 1 file changed, 17 insertions(+), 11 deletions(-)
src/expr/limit.c +17 -11
index 6732318..3410597 100644
@@ -145,16 +145,12 @@ static int nft_rule_expr_limit_xml_parse(struct nft_rule_expr *e,
 	uint64_t rate, unit;
 
 	if (nft_mxml_num_parse(tree, "rate", MXML_DESCEND_FIRST, BASE_DEC,
-			       &rate, NFT_TYPE_U64, NFT_XML_MAND,
-			       err) < 0)
-		return -1;
-	nft_rule_expr_set_u64(e, NFT_EXPR_LIMIT_RATE, rate);
+			       &rate, NFT_TYPE_U64, NFT_XML_MAND, err) == 0)
+		nft_rule_expr_set_u64(e, NFT_EXPR_LIMIT_RATE, rate);
 
 	if (nft_mxml_num_parse(tree, "unit", MXML_DESCEND_FIRST, BASE_DEC,
-			       &unit, NFT_TYPE_U64, NFT_XML_MAND,
-			       err) < 0)
-		return -1;
-	nft_rule_expr_set_u64(e, NFT_EXPR_LIMIT_UNIT, unit);
+			       &unit, NFT_TYPE_U64, NFT_XML_MAND, err) == 0)
+		nft_rule_expr_set_u64(e, NFT_EXPR_LIMIT_UNIT, unit);
 
 	return 0;
 #else
@@ -178,10 +174,20 @@ static const char *get_unit(uint64_t u)
 static int nft_rule_expr_limit_snprintf_xml(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"</rate>"
-				  "<unit>%"PRIu64"</unit>",
-			limit->rate, limit->unit);
+	if (e->flags & (1 << NFT_EXPR_LIMIT_RATE)) {
+		ret = snprintf(buf + offset, len, "<rate>%"PRIu64"</rate>",
+			       limit->rate);
+		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+	}
+	if (e->flags & (1 << NFT_EXPR_LIMIT_UNIT)) {
+		ret = snprintf(buf + offset, len, "<unit>%"PRIu64"</unit>",
+			       limit->unit);
+		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+	}
+
+	return offset;
 }
 
 static int nft_rule_expr_limit_snprintf_json(char *buf, size_t len,
Signature · 1 line
2.0.0
#5 lore raw
In reply to Ana Rey
Quoted 10 lines · Ana Rey

This patchset does tasks about does not print unset value in xml and json file and about refactoring code:
"expr: limit: Add nft_rule_expr_limit_snprinf_* functions" and "expr: limit: Use nft_rule_expr_set_* in the xml parsing code"

Ana Rey (4):
expr: limit: Add nft_rule_expr_limit_snprinf_* functions
expr: limit: Use nft_rule_expr_set_* in the xml parsing code
expr: limit: Do not print unset values in json
expr: limit: Do not print unset values in xml

Series applied, thanks Ana.