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/expr/lookup.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) (limited to 'src/expr/lookup.c') diff --git a/src/expr/lookup.c b/src/expr/lookup.c index 1046615..6d2b9a2 100644 --- a/src/expr/lookup.c +++ b/src/expr/lookup.c @@ -15,6 +15,7 @@ #include #include /* for memcpy */ #include +#include #include #include #include @@ -150,6 +151,82 @@ nft_rule_expr_lookup_parse(struct nft_rule_expr *e, struct nlattr *attr) return ret; } +static int +nft_rule_expr_lookup_xml_parse(struct nft_rule_expr *e, char *xml) +{ +#ifdef XML_PARSING + struct nft_expr_lookup *lookup = (struct nft_expr_lookup *)e->data; + mxml_node_t *tree = NULL; + mxml_node_t *node = NULL; + uint64_t tmp; + char *endptr; + + tree = mxmlLoadString(NULL, xml, MXML_OPAQUE_CALLBACK); + if (tree == NULL) + return -1; + + if (mxmlElementGetAttr(tree, "type") == NULL) { + mxmlDelete(tree); + return -1; + } + + if (strcmp("lookup", mxmlElementGetAttr(tree, "type")) != 0) { + mxmlDelete(tree); + return -1; + } + + /* get and set . Is mandatory */ + node = mxmlFindElement(tree, tree, "set", NULL, NULL, + MXML_DESCEND_FIRST); + if (node == NULL) { + mxmlDelete(tree); + return -1; + } + + memcpy(lookup->set_name, node->child->value.opaque, IFNAMSIZ); + lookup->set_name[IFNAMSIZ-1] = '\0'; + e->flags |= (1 << NFT_EXPR_LOOKUP_SET); + + /* get and set . Is mandatory */ + node = mxmlFindElement(tree, tree, "sreg", NULL, NULL, + MXML_DESCEND); + if (node == NULL) { + mxmlDelete(tree); + return -1; + } + + errno = 0; + + tmp = strtoull(node->child->value.opaque, &endptr, 10); + if (tmp > UINT32_MAX || tmp < 0 || *endptr) { + mxmlDelete(tree); + return -1; + } + + lookup->sreg = (uint32_t)tmp; + e->flags |= (1 << NFT_EXPR_LOOKUP_SREG); + + /* get and set . Isn't mandatory */ + node = mxmlFindElement(tree, tree, "dreg", NULL, NULL, + MXML_DESCEND); + if (node != NULL) { + tmp = strtoull(node->child->value.opaque, &endptr, 10); + if (tmp > UINT32_MAX || tmp < 0 || *endptr) { + mxmlDelete(tree); + return -1; + } + + lookup->dreg = (uint32_t)tmp; + e->flags |= (1 << NFT_EXPR_LOOKUP_DREG); + } + mxmlDelete(tree); + return 0; +#else + errno = EOPNOTSUPP; + return -1; +#endif +} + static int nft_rule_expr_lookup_snprintf_xml(char *buf, size_t size, struct nft_expr_lookup *l) @@ -202,4 +279,5 @@ struct expr_ops expr_ops_lookup = { .parse = nft_rule_expr_lookup_parse, .build = nft_rule_expr_lookup_build, .snprintf = nft_rule_expr_lookup_snprintf, + .xml_parse = nft_rule_expr_lookup_xml_parse, }; -- cgit v1.2.3