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/chain.c | 62 ++++++++++++++++++++++--------------------------------------- 1 file changed, 22 insertions(+), 40 deletions(-) (limited to 'src/chain.c') diff --git a/src/chain.c b/src/chain.c index 0dd3461..3ad52fd 100644 --- a/src/chain.c +++ b/src/chain.c @@ -587,8 +587,7 @@ static int nft_chain_xml_parse(struct nft_chain *c, char *xml) #ifdef XML_PARSING mxml_node_t *tree = NULL; mxml_node_t *node = NULL; - char *endptr = NULL; - uint64_t utmp; + const char *name; const char *hooknum_str; int family, hooknum; @@ -599,54 +598,43 @@ static int nft_chain_xml_parse(struct nft_chain *c, char *xml) if (tree == NULL) return -1; - /* Get and set */ - if (mxmlElementGetAttr(tree, "name") == NULL) { + if (strcmp(tree->value.opaque, "chain") != 0) { mxmlDelete(tree); return -1; } - strncpy(c->name, mxmlElementGetAttr(tree, "name"), - NFT_CHAIN_MAXNAMELEN); - c->flags |= (1 << NFT_CHAIN_ATTR_NAME); - /* Get and set */ - if (mxmlElementGetAttr(tree, "handle") == NULL) { + name = nft_mxml_str_parse(tree, "name", MXML_DESCEND_FIRST); + if (name == NULL) { mxmlDelete(tree); return -1; } - utmp = strtoull(mxmlElementGetAttr(tree, "handle"), &endptr, 10); - if (utmp == UINT64_MAX || utmp < 0 || *endptr) { + strncpy(c->name, name, NFT_CHAIN_MAXNAMELEN); + xfree(name); + c->flags |= (1 << NFT_CHAIN_ATTR_NAME); + + if (nft_mxml_num_parse(tree, "handle", MXML_DESCEND_FIRST, BASE_DEC, + &c->handle, NFT_TYPE_U64) != 0) { mxmlDelete(tree); return -1; } - c->handle = utmp; c->flags |= (1 << NFT_CHAIN_ATTR_HANDLE); - /* Get and set */ - if (mxmlElementGetAttr(tree, "bytes") == NULL) { + if (nft_mxml_num_parse(tree, "bytes", MXML_DESCEND_FIRST, BASE_DEC, + &c->bytes, NFT_TYPE_U64) != 0) { mxmlDelete(tree); return -1; } - utmp = strtoull(mxmlElementGetAttr(tree, "bytes"), &endptr, 10); - if (utmp == UINT64_MAX || utmp < 0 || *endptr) { - mxmlDelete(tree); - return -1; - } - c->bytes = utmp; + c->flags |= (1 << NFT_CHAIN_ATTR_BYTES); - /* Get and set */ - if (mxmlElementGetAttr(tree, "packets") == NULL) { + if (nft_mxml_num_parse(tree, "packets", MXML_DESCEND_FIRST, BASE_DEC, + &c->packets, NFT_TYPE_U64) != 0) { mxmlDelete(tree); return -1; } - utmp = strtoull(mxmlElementGetAttr(tree, "packets"), &endptr, 10); - if (utmp == UINT64_MAX || utmp < 0 || *endptr) { - mxmlDelete(tree); - return -1; - } - c->packets = utmp; + c->flags |= (1 << NFT_CHAIN_ATTR_PACKETS); /* Get and set */ @@ -724,13 +712,7 @@ static int nft_chain_xml_parse(struct nft_chain *c, char *xml) c->flags |= (1 << NFT_CHAIN_ATTR_POLICY); /* Get and set */ - node = mxmlFindElement(tree, tree, "family", NULL, NULL, MXML_DESCEND); - 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; @@ -810,11 +792,11 @@ static int nft_chain_snprintf_xml(char *buf, size_t size, struct nft_chain *c) { int ret, len = size, offset = 0; - ret = snprintf(buf, size, - "" - "%s%s
%d" - "%d%s", + ret = snprintf(buf, size, "%s" + "%"PRIu64"%"PRIu64"" + "%"PRIu64"%s" + "%s
%d%d" + "%s", c->name, c->handle, c->bytes, c->packets, c->type, c->table, c->prio, c->use, hooknum2str_array[c->hooknum]); -- cgit v1.2.3