From b58cf8947422ba695df80fe3b012b383fff22f7a Mon Sep 17 00:00:00 2001 From: Arturo Borrero Gonzalez Date: Tue, 6 Aug 2013 10:40:33 +0200 Subject: src: xml: use nodes instead of attributes When working with XML, it's desirable to work with nodes better than attributes. Table/chain/rules had attributes in their XML representation, and this patch transform those to nodes, ie: Before: ip0
After: filterip0
While at it: * There was a lot of redundant code that is now collapsed with the new nft_mxml_family_parse() helper function. * I've added a small fix: additional validation for the name of the current XML object, and also replace raw strtol calls to nft_strtoi. * Also, all XML testfiles are updated to keep passing the parsing tests and mantain the repo in consisten state. Signed-off-by: Arturo Borrero Gonzalez Signed-off-by: Pablo Neira Ayuso --- src/table.c | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) (limited to 'src/table.c') diff --git a/src/table.c b/src/table.c index 6875dd7..bb66717 100644 --- a/src/table.c +++ b/src/table.c @@ -222,7 +222,7 @@ static int nft_table_xml_parse(struct nft_table *t, char *xml) { #ifdef XML_PARSING mxml_node_t *tree = NULL; - mxml_node_t *node = NULL; + const char *name; int family; /* NOTE: all XML nodes are mandatory */ @@ -232,8 +232,13 @@ static int nft_table_xml_parse(struct nft_table *t, char *xml) if (tree == NULL) return -1; - /* Get and set the name of the table */ - if (mxmlElementGetAttr(tree, "name") == NULL) { + if (strcmp(tree->value.opaque, "table") != 0) { + mxmlDelete(tree); + return -1; + } + + name = nft_mxml_str_parse(tree, "name", MXML_DESCEND_FIRST); + if (name == NULL) { mxmlDelete(tree); return -1; } @@ -241,18 +246,10 @@ static int nft_table_xml_parse(struct nft_table *t, char *xml) if (t->name) xfree(t->name); - t->name = strdup(mxmlElementGetAttr(tree, "name")); + t->name = name; t->flags |= (1 << NFT_TABLE_ATTR_NAME); - /* Get the and set node */ - node = mxmlFindElement(tree, tree, "family", NULL, NULL, - MXML_DESCEND_FIRST); - if (node == NULL) { - mxmlDelete(tree); - return -1; - } - - family = nft_str2family(node->child->value.opaque); + family = nft_mxml_family_parse(tree, "family", MXML_DESCEND_FIRST); if (family < 0) { mxmlDelete(tree); return -1; @@ -261,7 +258,6 @@ static int nft_table_xml_parse(struct nft_table *t, char *xml) t->family = family; t->flags |= (1 << NFT_TABLE_ATTR_FAMILY); - /* Get and set */ if (nft_mxml_num_parse(tree, "table_flags", MXML_DESCEND, BASE_DEC, &t->table_flags, NFT_TYPE_U32) != 0) { mxmlDelete(tree); @@ -360,7 +356,7 @@ static int nft_table_snprintf_json(char *buf, size_t size, struct nft_table *t) static int nft_table_snprintf_xml(char *buf, size_t size, struct nft_table *t) { - return snprintf(buf, size, "%s" + return snprintf(buf, size, "
%s%s" "%d
", t->name, nft_family2str(t->family), t->table_flags); } -- cgit v1.2.3