summaryrefslogtreecommitdiffstats
path: root/src/chain.c
diff options
context:
space:
mode:
authorArturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>2013-08-06 10:40:33 +0200
committerPablo Neira Ayuso <pablo@netfilter.org>2013-08-06 11:44:20 +0200
commitb58cf8947422ba695df80fe3b012b383fff22f7a (patch)
treeed5708d68972c225492771b57397c52bfef1fc4d /src/chain.c
parentec75831c439ebd3475e0ba6766188d963538129a (diff)
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: <table name="filter"> <family>ip</family> <table_flags>0</table_flags> </table> After: <table> <name>filter</name> <family>ip</family> <table_flags>0</table_flags> </table> 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 <arturo.borrero.glez@gmail.com> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'src/chain.c')
-rw-r--r--src/chain.c62
1 files changed, 22 insertions, 40 deletions
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 <chain name="xxx" ... >*/
- 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 <chain handle="x" ... >*/
- 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 <chain bytes="x" ... >*/
- 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 <chain packets="x" ... > */
- 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 <type> */
@@ -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 <family> */
- 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,
- "<chain name=\"%s\" handle=\"%"PRIu64"\""
- " bytes=\"%"PRIu64"\" packets=\"%"PRIu64"\">"
- "<type>%s</type><table>%s</table><prio>%d</prio>"
- "<use>%d</use><hooknum>%s</hooknum>",
+ ret = snprintf(buf, size, "<chain><name>%s</name>"
+ "<handle>%"PRIu64"</handle><bytes>%"PRIu64"</bytes>"
+ "<packets>%"PRIu64"</packets><type>%s</type>"
+ "<table>%s</table><prio>%d</prio><use>%d</use>"
+ "<hooknum>%s</hooknum>",
c->name, c->handle, c->bytes, c->packets,
c->type, c->table,
c->prio, c->use, hooknum2str_array[c->hooknum]);