Skip to contentSkip to conversation

src: consolidate XMl parsing of data_reg via nft_mxml_data_reg_parse

PATCH Applied Arturo Borrero Gonzalez 2 participants 2 messages Jul 5, 2013 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 src: consolidate XMl parsing of data_reg via nft_mxml_data_reg_parse

Move common code for XML parsing of data_reg to the new nft_mxml_data_reg_parse function.

---
 src/expr/bitwise.c   |   36 ++++++-----------------------------
 src/expr/cmp.c       |   17 +++-------------
 src/expr/immediate.c |   45 ++++++++++++-------------------------------
 src/internal.h       |    2 ++
 src/mxml.c           |   52 ++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 75 insertions(+), 77 deletions(-)
src/expr/bitwise.c +6 -30
index 6b534b4..3836ab4 100644
@@ -202,10 +202,7 @@ nft_rule_expr_bitwise_xml_parse(struct nft_rule_expr *e, mxml_node_t *tree)
 {
 #ifdef XML_PARSING
 	struct nft_expr_bitwise *bitwise = (struct nft_expr_bitwise *)e->data;
-	mxml_node_t *node = NULL;
-	mxml_node_t *save = NULL;
 	int32_t reg;
-	union nft_data_reg data_regtmp;
 
 	reg = nft_mxml_reg_parse(tree, "sreg", MXML_DESCEND_FIRST);
 	if (reg < 0)
@@ -221,39 +218,18 @@ nft_rule_expr_bitwise_xml_parse(struct nft_rule_expr *e, mxml_node_t *tree)
 	bitwise->dreg = reg;
 	e->flags |= (1 << NFT_EXPR_BITWISE_DREG);
 
-	/* Get and set <mask> */
-	node = mxmlFindElement(tree, tree, "mask", NULL, NULL,
-			       MXML_DESCEND);
-	if (node == NULL)
+	if (nft_mxml_data_reg_parse(tree, "mask",
+				    &bitwise->mask) != DATA_VALUE) {
 		return -1;
+	}
 
-	/* hack for mxmSaveAllocString to print just the current node */
-	save = node->next;
-	node->next = NULL;
-	if (nft_data_reg_xml_parse(&data_regtmp,
-			mxmlSaveAllocString(node, MXML_NO_CALLBACK)) < 0)
-		return -1;
-	node->next = save;
-
-	memcpy(&bitwise->mask.val, data_regtmp.val, data_regtmp.len);
-	bitwise->mask.len = data_regtmp.len;
 	e->flags |= (1 << NFT_EXPR_BITWISE_MASK);
 
-	/* Get and set <xor> */
-	node = mxmlFindElement(tree, tree, "xor", NULL, NULL,
-			       MXML_DESCEND);
-	if (node == NULL)
-		return -1;
-
-	/* hack for mxmSaveAllocString to print just the current node */
-	save = node->next;
-	node->next = NULL;
-	if (nft_data_reg_xml_parse(&data_regtmp,
-			mxmlSaveAllocString(node, MXML_NO_CALLBACK)) < 0)
+	if (nft_mxml_data_reg_parse(tree, "xor",
+				    &bitwise->xor) != DATA_VALUE) {
 		return -1;
+	}
 
-	memcpy(&bitwise->xor.val, data_regtmp.val, data_regtmp.len);
-	bitwise->xor.len = data_regtmp.len;
 	e->flags |= (1 << NFT_EXPR_BITWISE_XOR);
 
 	/* Additional validation: mask and xor must use the same number of
src/expr/cmp.c +3 -14
index f3a363a..2e284fe 100644
@@ -173,8 +173,6 @@ static int nft_rule_expr_cmp_xml_parse(struct nft_rule_expr *e, mxml_node_t *tre
 #ifdef XML_PARSING
 	struct nft_expr_cmp *cmp = (struct nft_expr_cmp *)e->data;
 	mxml_node_t *node = NULL;
-	mxml_node_t *save = NULL;
-	union nft_data_reg data_regtmp;
 	int32_t reg;
 
 	reg = nft_mxml_reg_parse(tree, "sreg", MXML_DESCEND_FIRST);
@@ -207,22 +205,13 @@ static int nft_rule_expr_cmp_xml_parse(struct nft_rule_expr *e, mxml_node_t *tre
 	}
 
 	/* Get and set <cmpdata>. Is not mandatory */
-	node = mxmlFindElement(tree, tree, "cmpdata", NULL, NULL,
-			       MXML_DESCEND);
+	node = mxmlFindElement(tree, tree, "cmpdata", NULL, NULL, MXML_DESCEND);
 	if (node != NULL) {
-		/* hack for mxmSaveAllocString to print just the current node */
-		save = node->next;
-		node->next = NULL;
-
-		if (nft_data_reg_xml_parse(&data_regtmp,
-			mxmlSaveAllocString(node, MXML_NO_CALLBACK)) < 0) {
+		if (nft_mxml_data_reg_parse(tree, "cmpdata",
+					    &cmp->data) != DATA_VALUE) {
 			return -1;
 		}
 
-		node->next = save;
-
-		memcpy(&cmp->data.val, data_regtmp.val, data_regtmp.len);
-		cmp->data.len = data_regtmp.len;
 		e->flags |= (1 << NFT_EXPR_CMP_DATA);
 	}
 
src/expr/immediate.c +12 -33

63 lines not on this page · raw message

src/internal.h +2 -0
index ee09661..e2c2882 100644
@@ -17,6 +17,8 @@
 #include <mxml.h>
 struct nft_rule_expr *nft_mxml_expr_parse(mxml_node_t *node);
 int nft_mxml_reg_parse(mxml_node_t *tree, const char *reg_name, uint32_t flags);
+union nft_data_reg;
+int nft_mxml_data_reg_parse(mxml_node_t *tree, const char *node_name, union nft_data_reg *data_reg);
 #endif
 
 #define NFT_TABLE_XML_VERSION 0
src/mxml.c +52 -0

56 lines not on this page · raw message

maintainer Applied
#1 lore raw
Quoted 2 lines · Arturo Borrero Gonzalez

Move common code for XML parsing of data_reg to the new nft_mxml_data_reg_parse function.

Applied, thanks Arturo.