From 51370f0eedb1c8167ab2c340d2a53f0d9f02509c Mon Sep 17 00:00:00 2001 From: Arturo Borrero Gonzalez Date: Thu, 23 May 2013 12:03:04 +0200 Subject: src: add support for XML parsing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch adds capabilities for parsing a XML table/chain/rule. Some comments: * The XML data is case sensitive (so asd != ASD != asd) * All exported functions receive XML and return an object (table|chain|rule). * To compile the lib with XML parsing support, run './configure --with-xml-parsing' * XML parsing is done with libmxml (http://minixml.org). XML parsing depends on this external lib, this dependency is optional at compile time. NOTE: expr/target and expr/match binary data are exported. [ Fixed to compile without --with-xml-parsing --pablo ] Signed-off-by: Arturo Borrero González --- src/table.c | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) (limited to 'src/table.c') diff --git a/src/table.c b/src/table.c index 2e64043..70f482d 100644 --- a/src/table.c +++ b/src/table.c @@ -13,9 +13,11 @@ #include #include #include +#include #include #include #include +#include #include #include @@ -194,6 +196,97 @@ int nft_table_nlmsg_parse(const struct nlmsghdr *nlh, struct nft_table *t) } EXPORT_SYMBOL(nft_table_nlmsg_parse); +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; + char *endptr = NULL; + uint64_t tmp; + + /* NOTE: all XML nodes are mandatory */ + + /* Load the tree */ + tree = mxmlLoadString(NULL, xml, MXML_OPAQUE_CALLBACK); + if (tree == NULL) + return -1; + + /* Get and set the name of the table */ + if (mxmlElementGetAttr(tree, "name") == NULL) { + mxmlDelete(tree); + return -1; + } + + if (t->name) + free(t->name); + + t->name = strdup(mxmlElementGetAttr(tree, "name")); + t->flags |= (1 << NFT_TABLE_ATTR_NAME); + + /* Ignore node */ + node = mxmlFindElement(tree, tree, "properties", NULL, NULL, + MXML_DESCEND_FIRST); + + /* Get the and set node */ + node = mxmlFindElement(tree, tree, "family", NULL, NULL, MXML_DESCEND); + if (node == NULL) { + mxmlDelete(tree); + return -1; + } + + tmp = strtoull(node->child->value.opaque, &endptr, 10); + if (tmp > UINT32_MAX || *endptr || tmp < 0) { + mxmlDelete(tree); + return -1; + } + + t->family = (uint32_t)tmp; + t->flags |= (1 << NFT_TABLE_ATTR_FAMILY); + + /* Get and set */ + node = mxmlFindElement(tree, tree, "table_flags", NULL, NULL, + MXML_DESCEND); + if (node == NULL) { + mxmlDelete(tree); + return -1; + } + + tmp = strtoull(node->child->value.opaque, &endptr, 10); + if (tmp > UINT32_MAX || *endptr || tmp < 0) { + mxmlDelete(tree); + return -1; + } + + t->table_flags = (uint32_t)tmp; + t->flags |= (1 << NFT_TABLE_ATTR_FLAGS); + + mxmlDelete(tree); + return 0; +#else + errno = EOPNOTSUPP; + return -1; +#endif +} + +int nft_table_parse(struct nft_table *t, enum nft_table_parse_type type, + char *data) +{ + int ret; + + switch (type) { + case NFT_TABLE_PARSE_XML: + ret = nft_table_xml_parse(t, data); + break; + default: + ret = -1; + errno = EOPNOTSUPP; + break; + } + + return ret; +} +EXPORT_SYMBOL(nft_table_parse); + static int nft_table_snprintf_xml(char *buf, size_t size, struct nft_table *t) { return snprintf(buf, size, -- cgit v1.2.3