Skip to contentSkip to conversation

expr: match: Do not print unset values in xml and json

PATCH New Ana Rey 1 participant 5 messages Jun 24, 2014 netfilter

lore
Share & tools
lore

Ana Rey

author
#0 lore raw

cover expr: match: Do not print unset values in xml and json

This patchset does tasks about does not print unset value in xml and json file and about refactoring code:

* "expr: match: get a nft_rule_expr type in nft_rule_expr_match_snprintf_* functions"
* "expr: match: Use nft_rule_expr_set_* in the xml parsing code"

Ana Rey (4):
expr: match: get a nft_rule_expr type in
nft_rule_expr_match_snprintf_* functions
expr: match: Use nft_rule_expr_set_* in the xml parsing code
expr: match: Do not print unset values in xml file
expr: match: Do not print unset values in json file

 src/expr/match.c | 39 ++++++++++++++++++---------------------
 1 file changed, 18 insertions(+), 21 deletions(-)
Signature · 1 line
2.0.0
author
#2 lore raw

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

Code refactoring to use nft_rule_expr_set_* in parse functions.

---
 src/expr/match.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
src/expr/match.c +1 -5
index 05f9aff..572d0f9 100644
@@ -194,17 +194,13 @@ static int nft_rule_expr_match_xml_parse(struct nft_rule_expr *e, mxml_node_t *t
 					 struct nft_parse_err *err)
 {
 #ifdef XML_PARSING
-	struct nft_expr_match *mt = nft_expr_data(e);
 	const char *name;
 
 	name = nft_mxml_str_parse(tree, "name", MXML_DESCEND_FIRST,
 				  NFT_XML_MAND, err);
 	if (name == NULL)
 		return -1;
-
-	strncpy(mt->name, name, XT_EXTENSION_MAXNAMELEN);
-	mt->name[XT_EXTENSION_MAXNAMELEN-1] = '\0';
-	e->flags |= (1 << NFT_EXPR_MT_NAME);
+	nft_rule_expr_set_str(e, NFT_EXPR_MT_NAME, name);
 
 	/* mt->info is ignored until other solution is reached */
 
Signature · 1 line
2.0.0
author
#1 lore raw

PATCH 1/4 expr: match: get a nft_rule_expr type in nft_rule_expr_match_snprintf_* functions

In reply to Ana Rey

Code refactoring in nft_rule_expr_match_snprintf_* functions to get a nft_rule_expr type instead of nft_expr_match type.

The nft_rule_expr type is needed to check information into flags variables.

---
 src/expr/match.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
src/expr/match.c +6 -4
index a78bd69..05f9aff 100644
@@ -216,8 +216,9 @@ static int nft_rule_expr_match_xml_parse(struct nft_rule_expr *e, mxml_node_t *t
 }
 
 static int nft_rule_expr_match_snprintf_json(char *buf, size_t len,
-					    struct nft_expr_match *mt)
+					     struct nft_rule_expr *e)
 {
+	struct nft_expr_match *mt = nft_expr_data(e);
 	int ret, size = len, offset = 0;
 
 	ret = snprintf(buf, len, "\"name\":\"%s\"",
@@ -228,8 +229,9 @@ static int nft_rule_expr_match_snprintf_json(char *buf, size_t len,
 }
 
 static int nft_rule_expr_match_snprintf_xml(char *buf, size_t len,
-					    struct nft_expr_match *mt)
+					    struct nft_rule_expr *e)
 {
+	struct nft_expr_match *mt = nft_expr_data(e);
 	int ret, size=len;
 	int offset = 0;
 
@@ -251,9 +253,9 @@ nft_rule_expr_match_snprintf(char *buf, size_t len, uint32_t type,
 		return snprintf(buf, len, "name %s rev %u ",
 				match->name, match->rev);
 	case NFT_OUTPUT_XML:
-		return nft_rule_expr_match_snprintf_xml(buf, len, match);
+		return nft_rule_expr_match_snprintf_xml(buf, len, e);
 	case NFT_OUTPUT_JSON:
-		return nft_rule_expr_match_snprintf_json(buf, len, match);
+		return nft_rule_expr_match_snprintf_json(buf, len, e);
 	default:
 		break;
 	}
Signature · 1 line
2.0.0
author
#3 lore raw

PATCH 3/4 expr: match: Do not print unset values in xml file

In reply to Ana Rey

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

---
 src/expr/match.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
src/expr/match.c +6 -6
index 572d0f9..604a8e6 100644
@@ -198,9 +198,8 @@ static int nft_rule_expr_match_xml_parse(struct nft_rule_expr *e, mxml_node_t *t
 
 	name = nft_mxml_str_parse(tree, "name", MXML_DESCEND_FIRST,
 				  NFT_XML_MAND, err);
-	if (name == NULL)
-		return -1;
-	nft_rule_expr_set_str(e, NFT_EXPR_MT_NAME, name);
+	if (name != NULL)
+		nft_rule_expr_set_str(e, NFT_EXPR_MT_NAME, name);
 
 	/* mt->info is ignored until other solution is reached */
 
@@ -231,13 +230,14 @@ static int nft_rule_expr_match_snprintf_xml(char *buf, size_t len,
 	int ret, size=len;
 	int offset = 0;
 
-	ret = snprintf(buf, len, "<name>%s</name>", mt->name);
-	SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+	if (e->flags & (1 << NFT_EXPR_MT_NAME)) {
+		ret = snprintf(buf, len, "<name>%s</name>", mt->name);
+		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+	}
 
 	return offset;
 }
 
-
 static int
 nft_rule_expr_match_snprintf(char *buf, size_t len, uint32_t type,
 			     uint32_t flags, struct nft_rule_expr *e)
Signature · 1 line
2.0.0
author
#4 lore raw

PATCH 4/4 expr: match: Do not print unset values in json file

In reply to Ana Rey

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

---
 src/expr/match.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
src/expr/match.c +6 -7
index 604a8e6..2c4b3dc 100644
@@ -177,10 +177,8 @@ static int nft_rule_expr_match_json_parse(struct nft_rule_expr *e, json_t *root,
 	const char *name;
 
 	name = nft_jansson_parse_str(root, "name", err);
-	if (name == NULL)
-		return -1;
-
-	nft_rule_expr_set_str(e, NFT_EXPR_MT_NAME, name);
+	if (name != NULL)
+		nft_rule_expr_set_str(e, NFT_EXPR_MT_NAME, name);
 
 	return 0;
 #else
@@ -216,9 +214,10 @@ static int nft_rule_expr_match_snprintf_json(char *buf, size_t len,
 	struct nft_expr_match *mt = nft_expr_data(e);
 	int ret, size = len, offset = 0;
 
-	ret = snprintf(buf, len, "\"name\":\"%s\"",
-				mt->name);
-	SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+	if (e->flags & (1 << NFT_EXPR_MT_NAME)) {
+		ret = snprintf(buf, len, "\"name\":\"%s\"", mt->name);
+		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+	}
 
 	return offset;
 }
Signature · 1 line
2.0.0