diff options
author | Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com> | 2013-05-23 12:03:04 +0200 |
---|---|---|
committer | Pablo Neira Ayuso <pablo@netfilter.org> | 2013-05-23 12:34:08 +0200 |
commit | 51370f0eedb1c8167ab2c340d2a53f0d9f02509c (patch) | |
tree | c3c59e1ecd89cad380204fec6a92cf418cfb5ca6 /src/table.c | |
parent | 3231f64ee7bc4ba56e814611f306320b48e9b91d (diff) |
src: add support for XML parsing
This patch adds capabilities for parsing a XML table/chain/rule.
Some comments:
* The XML data is case sensitive
(so <chain>asd</chain> != <chain>ASD</chain> != <CHAIN>asd</CHAIN>)
* 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 <arturo.borrero.glez@gmail.com>
Diffstat (limited to 'src/table.c')
-rw-r--r-- | src/table.c | 93 |
1 files changed, 93 insertions, 0 deletions
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 <time.h> #include <endian.h> #include <stdint.h> +#include <limits.h> #include <stdlib.h> #include <string.h> #include <netinet/in.h> +#include <errno.h> #include <libmnl/libmnl.h> #include <linux/netfilter/nfnetlink.h> @@ -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 <properties> node */ + node = mxmlFindElement(tree, tree, "properties", NULL, NULL, + MXML_DESCEND_FIRST); + + /* Get the and set <family> 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 <table_flags> */ + 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, |