From fc423ae81487e906141f700c7b3515328ff62b7c Mon Sep 17 00:00:00 2001 From: Arturo Borrero Date: Thu, 9 Jan 2014 12:19:06 +0100 Subject: src: rework and generalize the build/parse system The intention behind this patch is to prepare the introduction of the new API that will allow us to parse files that contain the rule-sets expressed in XML/JSON format. This adds the NFT_PARSE_BUFFER that indicates that the input is provided in a buffer, which is what we currently support. Signed-off-by: Arturo Borrero Gonzalez Signed-off-by: Pablo Neira Ayuso --- src/chain.c | 29 +++++++++++++++++++---------- src/internal.h | 12 ++++++++---- src/jansson.c | 19 ++++++++++++++----- src/mxml.c | 13 ++++++++++--- src/rule.c | 28 ++++++++++++++++++---------- src/ruleset.c | 28 ++++++++++++++++++---------- src/set.c | 26 ++++++++++++++++---------- src/set_elem.c | 29 +++++++++++++++++++---------- src/table.c | 29 +++++++++++++++++++---------- 9 files changed, 141 insertions(+), 72 deletions(-) (limited to 'src') diff --git a/src/chain.c b/src/chain.c index a4ddb06..8f40ede 100644 --- a/src/chain.c +++ b/src/chain.c @@ -591,14 +591,15 @@ err: } #endif -static int nft_chain_json_parse(struct nft_chain *c, const char *json, - struct nft_parse_err *err) +static int nft_chain_json_parse(struct nft_chain *c, const void *json, + struct nft_parse_err *err, + enum nft_parse_input input) { #ifdef JSON_PARSING json_t *tree; json_error_t error; - tree = nft_jansson_create_root(json, &error, err); + tree = nft_jansson_create_root(json, &error, err, input); if (tree == NULL) return -1; @@ -708,12 +709,13 @@ int nft_mxml_chain_parse(mxml_node_t *tree, struct nft_chain *c, } #endif -static int nft_chain_xml_parse(struct nft_chain *c, const char *xml, - struct nft_parse_err *err) +static int nft_chain_xml_parse(struct nft_chain *c, const void *xml, + struct nft_parse_err *err, + enum nft_parse_input input) { #ifdef XML_PARSING int ret; - mxml_node_t *tree = nft_mxml_build_tree(xml, "chain", err); + mxml_node_t *tree = nft_mxml_build_tree(xml, "chain", err, input); if (tree == NULL) return -1; @@ -726,18 +728,19 @@ static int nft_chain_xml_parse(struct nft_chain *c, const char *xml, #endif } -int nft_chain_parse(struct nft_chain *c, enum nft_parse_type type, - const char *data, struct nft_parse_err *err) +static int nft_chain_do_parse(struct nft_chain *c, enum nft_parse_type type, + const void *data, struct nft_parse_err *err, + enum nft_parse_input input) { int ret; struct nft_parse_err perr; switch (type) { case NFT_PARSE_XML: - ret = nft_chain_xml_parse(c, data, &perr); + ret = nft_chain_xml_parse(c, data, &perr, input); break; case NFT_PARSE_JSON: - ret = nft_chain_json_parse(c, data, &perr); + ret = nft_chain_json_parse(c, data, &perr, input); break; default: ret = -1; @@ -750,6 +753,12 @@ int nft_chain_parse(struct nft_chain *c, enum nft_parse_type type, return ret; } + +int nft_chain_parse(struct nft_chain *c, enum nft_parse_type type, + const char *data, struct nft_parse_err *err) +{ + return nft_chain_do_parse(c, type, data, err, NFT_PARSE_BUFFER); +} EXPORT_SYMBOL(nft_chain_parse); static int nft_chain_snprintf_json(char *buf, size_t size, struct nft_chain *c) diff --git a/src/internal.h b/src/internal.h index 5fef6d6..fa092cf 100644 --- a/src/internal.h +++ b/src/internal.h @@ -38,12 +38,16 @@ struct nft_parse_err { const char *node_name; }; +enum nft_parse_input { + NFT_PARSE_BUFFER, +}; + #ifdef XML_PARSING #include #define NFT_XML_MAND 0 #define NFT_XML_OPT (1 << 0) -mxml_node_t *nft_mxml_build_tree(const char *xml, const char *treename, - struct nft_parse_err *err); +mxml_node_t *nft_mxml_build_tree(const void *data, const char *treename, + struct nft_parse_err *err, enum nft_parse_input input); struct nft_rule_expr *nft_mxml_expr_parse(mxml_node_t *node, struct nft_parse_err *err); int nft_mxml_reg_parse(mxml_node_t *tree, const char *reg_name, uint32_t flags, @@ -88,8 +92,8 @@ int nft_jansson_parse_val(json_t *root, const char *node_name, int type, const char *nft_jansson_parse_str(json_t *root, const char *node_name, struct nft_parse_err *err); bool nft_jansson_node_exist(json_t *root, const char *node_name); -json_t *nft_jansson_create_root(const char *json, json_error_t *error, - struct nft_parse_err *err); +json_t *nft_jansson_create_root(const void *json, json_error_t *error, + struct nft_parse_err *err, enum nft_parse_input input); json_t *nft_jansson_get_node(json_t *root, const char *node_name, struct nft_parse_err *err); void nft_jansson_free_root(json_t *root); diff --git a/src/jansson.c b/src/jansson.c index e62116b..3428f2f 100644 --- a/src/jansson.c +++ b/src/jansson.c @@ -89,22 +89,31 @@ bool nft_jansson_node_exist(json_t *root, const char *node_name) return json_object_get(root, node_name) != NULL; } -json_t *nft_jansson_create_root(const char *json, json_error_t *error, - struct nft_parse_err *err) +json_t *nft_jansson_create_root(const void *json, json_error_t *error, + struct nft_parse_err *err, enum nft_parse_input input) { json_t *root; - root = json_loadb(json, strlen(json), 0, error); + switch (input) { + case NFT_PARSE_BUFFER: + root = json_loadb(json, strlen(json), 0, error); + break; + default: + goto err; + } + if (root == NULL) { err->error = NFT_PARSE_EBADINPUT; err->line = error->line; err->column = error->column; err->node_name = error->source; - errno = EINVAL; - return NULL; + goto err; } return root; +err: + errno = EINVAL; + return NULL; } json_t *nft_jansson_get_node(json_t *root, const char *node_name, diff --git a/src/mxml.c b/src/mxml.c index bc0f084..575383c 100644 --- a/src/mxml.c +++ b/src/mxml.c @@ -22,12 +22,19 @@ #include #ifdef XML_PARSING -mxml_node_t *nft_mxml_build_tree(const char *xml, const char *treename, - struct nft_parse_err *err) +mxml_node_t *nft_mxml_build_tree(const void *data, const char *treename, + struct nft_parse_err *err, enum nft_parse_input input) { mxml_node_t *tree; - tree = mxmlLoadString(NULL, xml, MXML_OPAQUE_CALLBACK); + switch (input) { + case NFT_PARSE_BUFFER: + tree = mxmlLoadString(NULL, data, MXML_OPAQUE_CALLBACK); + break; + default: + goto err; + } + if (tree == NULL) { err->error = NFT_PARSE_EBADINPUT; goto err; diff --git a/src/rule.c b/src/rule.c index 2e35aba..081686c 100644 --- a/src/rule.c +++ b/src/rule.c @@ -526,14 +526,15 @@ err: } #endif -static int nft_rule_json_parse(struct nft_rule *r, const char *json, - struct nft_parse_err *err) +static int nft_rule_json_parse(struct nft_rule *r, const void *json, + struct nft_parse_err *err, + enum nft_parse_input input) { #ifdef JSON_PARSING json_t *tree; json_error_t error; - tree = nft_jansson_create_root(json, &error, err); + tree = nft_jansson_create_root(json, &error, err, input); if (tree == NULL) return -1; @@ -627,12 +628,13 @@ int nft_mxml_rule_parse(mxml_node_t *tree, struct nft_rule *r, } #endif -static int nft_rule_xml_parse(struct nft_rule *r, const char *xml, - struct nft_parse_err *err) +static int nft_rule_xml_parse(struct nft_rule *r, const void *xml, + struct nft_parse_err *err, + enum nft_parse_input input) { #ifdef XML_PARSING int ret; - mxml_node_t *tree = nft_mxml_build_tree(xml, "rule", err); + mxml_node_t *tree = nft_mxml_build_tree(xml, "rule", err, input); if (tree == NULL) return -1; @@ -645,18 +647,19 @@ static int nft_rule_xml_parse(struct nft_rule *r, const char *xml, #endif } -int nft_rule_parse(struct nft_rule *r, enum nft_parse_type type, - const char *data, struct nft_parse_err *err) +static int nft_rule_do_parse(struct nft_rule *r, enum nft_parse_type type, + const void *data, struct nft_parse_err *err, + enum nft_parse_input input) { int ret; struct nft_parse_err perr; switch (type) { case NFT_PARSE_XML: - ret = nft_rule_xml_parse(r, data, &perr); + ret = nft_rule_xml_parse(r, data, &perr, input); break; case NFT_PARSE_JSON: - ret = nft_rule_json_parse(r, data, &perr); + ret = nft_rule_json_parse(r, data, &perr, input); break; default: ret = -1; @@ -668,6 +671,11 @@ int nft_rule_parse(struct nft_rule *r, enum nft_parse_type type, return ret; } +int nft_rule_parse(struct nft_rule *r, enum nft_parse_type type, + const char *data, struct nft_parse_err *err) +{ + return nft_rule_do_parse(r, type, data, err, NFT_PARSE_BUFFER); +} EXPORT_SYMBOL(nft_rule_parse); static int nft_rule_snprintf_json(char *buf, size_t size, struct nft_rule *r, diff --git a/src/ruleset.c b/src/ruleset.c index a12efa9..c6bcc4b 100644 --- a/src/ruleset.c +++ b/src/ruleset.c @@ -330,14 +330,14 @@ err: #endif -static int nft_ruleset_json_parse(struct nft_ruleset *rs, const char *json, - struct nft_parse_err *err) +static int nft_ruleset_json_parse(struct nft_ruleset *rs, const void *json, + struct nft_parse_err *err, enum nft_parse_input input) { #ifdef JSON_PARSING json_t *root, *array; json_error_t error; - root = nft_jansson_create_root(json, &error, err); + root = nft_jansson_create_root(json, &error, err, input); if (root == NULL) return -1; @@ -534,13 +534,13 @@ err_free: } #endif -static int nft_ruleset_xml_parse(struct nft_ruleset *rs, const char *xml, - struct nft_parse_err *err) +static int nft_ruleset_xml_parse(struct nft_ruleset *rs, const void *xml, + struct nft_parse_err *err, enum nft_parse_input input) { #ifdef XML_PARSING mxml_node_t *tree; - tree = nft_mxml_build_tree(xml, "nftables", err); + tree = nft_mxml_build_tree(xml, "nftables", err, input); if (tree == NULL) return -1; @@ -567,17 +567,19 @@ err: #endif } -int nft_ruleset_parse(struct nft_ruleset *r, enum nft_parse_type type, - const char *data, struct nft_parse_err *err) +static int +nft_ruleset_do_parse(struct nft_ruleset *r, enum nft_parse_type type, + const void *data, struct nft_parse_err *err, + enum nft_parse_input input) { int ret; switch (type) { case NFT_PARSE_XML: - ret = nft_ruleset_xml_parse(r, data, err); + ret = nft_ruleset_xml_parse(r, data, err, input); break; case NFT_PARSE_JSON: - ret = nft_ruleset_json_parse(r, data, err); + ret = nft_ruleset_json_parse(r, data, err, input); break; default: ret = -1; @@ -587,6 +589,12 @@ int nft_ruleset_parse(struct nft_ruleset *r, enum nft_parse_type type, return ret; } + +int nft_ruleset_parse(struct nft_ruleset *r, enum nft_parse_type type, + const char *data, struct nft_parse_err *err) +{ + return nft_ruleset_do_parse(r, type, data, err, NFT_PARSE_BUFFER); +} EXPORT_SYMBOL(nft_ruleset_parse); static const char *nft_ruleset_o_opentag(uint32_t type) diff --git a/src/set.c b/src/set.c index 9317b9c..1c9caf8 100644 --- a/src/set.c +++ b/src/set.c @@ -375,14 +375,14 @@ err: } #endif -static int nft_set_json_parse(struct nft_set *s, const char *json, - struct nft_parse_err *err) +static int nft_set_json_parse(struct nft_set *s, const void *json, + struct nft_parse_err *err, enum nft_parse_input input) { #ifdef JSON_PARSING json_t *tree; json_error_t error; - tree = nft_jansson_create_root(json, &error, err); + tree = nft_jansson_create_root(json, &error, err, input); if (tree == NULL) return -1; @@ -483,12 +483,12 @@ int nft_mxml_set_parse(mxml_node_t *tree, struct nft_set *s, } #endif -static int nft_set_xml_parse(struct nft_set *s, const char *xml, - struct nft_parse_err *err) +static int nft_set_xml_parse(struct nft_set *s, const void *xml, + struct nft_parse_err *err, enum nft_parse_input input) { #ifdef XML_PARSING int ret; - mxml_node_t *tree = nft_mxml_build_tree(xml, "set", err); + mxml_node_t *tree = nft_mxml_build_tree(xml, "set", err, input); if (tree == NULL) return -1; @@ -501,18 +501,19 @@ static int nft_set_xml_parse(struct nft_set *s, const char *xml, #endif } -int nft_set_parse(struct nft_set *s, enum nft_parse_type type, - const char *data, struct nft_parse_err *err) +static int nft_set_do_parse(struct nft_set *s, enum nft_parse_type type, + const void *data, struct nft_parse_err *err, + enum nft_parse_input input) { int ret; struct nft_parse_err perr; switch (type) { case NFT_PARSE_XML: - ret = nft_set_xml_parse(s, data, &perr); + ret = nft_set_xml_parse(s, data, &perr, input); break; case NFT_PARSE_JSON: - ret = nft_set_json_parse(s, data, &perr); + ret = nft_set_json_parse(s, data, &perr, input); break; default: ret = -1; @@ -525,6 +526,11 @@ int nft_set_parse(struct nft_set *s, enum nft_parse_type type, return ret; } +int nft_set_parse(struct nft_set *s, enum nft_parse_type type, + const char *data, struct nft_parse_err *err) +{ + return nft_set_do_parse(s, type, data, err, NFT_PARSE_BUFFER); +} EXPORT_SYMBOL(nft_set_parse); static int nft_set_snprintf_json(char *buf, size_t size, struct nft_set *s, diff --git a/src/set_elem.c b/src/set_elem.c index 14bf6f4..93e8291 100644 --- a/src/set_elem.c +++ b/src/set_elem.c @@ -394,14 +394,15 @@ int nft_mxml_set_elem_parse(mxml_node_t *tree, struct nft_set_elem *e, } #endif -static int nft_set_elem_xml_parse(struct nft_set_elem *e, const char *xml, - struct nft_parse_err *err) +static int nft_set_elem_xml_parse(struct nft_set_elem *e, const void *xml, + struct nft_parse_err *err, + enum nft_parse_input input) { #ifdef XML_PARSING mxml_node_t *tree; int ret; - tree = nft_mxml_build_tree(xml, "set_elem", err); + tree = nft_mxml_build_tree(xml, "set_elem", err, input); if (tree == NULL) return -1; @@ -415,13 +416,14 @@ static int nft_set_elem_xml_parse(struct nft_set_elem *e, const char *xml, } static int nft_set_elem_json_parse(struct nft_set_elem *e, const void *json, - struct nft_parse_err *err) + struct nft_parse_err *err, + enum nft_parse_input input) { #ifdef JSON_PARSING json_t *tree; json_error_t error; - tree = nft_jansson_create_root(json, &error, err); + tree = nft_jansson_create_root(json, &error, err, input); if (tree == NULL) return -1; @@ -432,17 +434,19 @@ static int nft_set_elem_json_parse(struct nft_set_elem *e, const void *json, #endif } -int nft_set_elem_parse(struct nft_set_elem *e, - enum nft_parse_type type, const char *data, - struct nft_parse_err *err) { +static int +nft_set_elem_do_parse(struct nft_set_elem *e, enum nft_parse_type type, + const void *data, struct nft_parse_err *err, + enum nft_parse_input input) +{ int ret; switch (type) { case NFT_PARSE_XML: - ret = nft_set_elem_xml_parse(e, data, err); + ret = nft_set_elem_xml_parse(e, data, err, input); break; case NFT_PARSE_JSON: - ret = nft_set_elem_json_parse(e, data, err); + ret = nft_set_elem_json_parse(e, data, err, input); break; default: errno = EOPNOTSUPP; @@ -452,6 +456,11 @@ int nft_set_elem_parse(struct nft_set_elem *e, return ret; } +int nft_set_elem_parse(struct nft_set_elem *e, enum nft_parse_type type, + const char *data, struct nft_parse_err *err) +{ + return nft_set_elem_do_parse(e, type, data, err, NFT_PARSE_BUFFER); +} EXPORT_SYMBOL(nft_set_elem_parse); static int nft_set_elem_snprintf_json(char *buf, size_t size, diff --git a/src/table.c b/src/table.c index 9b5f5c9..4f2d5d2 100644 --- a/src/table.c +++ b/src/table.c @@ -247,12 +247,13 @@ int nft_mxml_table_parse(mxml_node_t *tree, struct nft_table *t, } #endif -static int nft_table_xml_parse(struct nft_table *t, const char *xml, - struct nft_parse_err *err) +static int nft_table_xml_parse(struct nft_table *t, const void *data, + struct nft_parse_err *err, + enum nft_parse_input input) { #ifdef XML_PARSING int ret; - mxml_node_t *tree = nft_mxml_build_tree(xml, "table", err); + mxml_node_t *tree = nft_mxml_build_tree(data, "table", err, input); if (tree == NULL) return -1; @@ -302,14 +303,15 @@ err: } #endif -static int nft_table_json_parse(struct nft_table *t, const char *json, - struct nft_parse_err *err) +static int nft_table_json_parse(struct nft_table *t, const void *json, + struct nft_parse_err *err, + enum nft_parse_input input) { #ifdef JSON_PARSING json_t *tree; json_error_t error; - tree = nft_jansson_create_root(json, &error, err); + tree = nft_jansson_create_root(json, &error, err, input); if (tree == NULL) return -1; @@ -320,18 +322,19 @@ static int nft_table_json_parse(struct nft_table *t, const char *json, #endif } -int nft_table_parse(struct nft_table *t, enum nft_parse_type type, - const char *data, struct nft_parse_err *err) +static int nft_table_do_parse(struct nft_table *t, enum nft_parse_type type, + const void *data, struct nft_parse_err *err, + enum nft_parse_input input) { int ret; struct nft_parse_err perr; switch (type) { case NFT_PARSE_XML: - ret = nft_table_xml_parse(t, data, &perr); + ret = nft_table_xml_parse(t, data, &perr, input); break; case NFT_PARSE_JSON: - ret = nft_table_json_parse(t, data, &perr); + ret = nft_table_json_parse(t, data, &perr, input); break; default: ret = -1; @@ -344,6 +347,12 @@ int nft_table_parse(struct nft_table *t, enum nft_parse_type type, return ret; } + +int nft_table_parse(struct nft_table *t, enum nft_parse_type type, + const char *data, struct nft_parse_err *err) +{ + return nft_table_do_parse(t, type, data, err, NFT_PARSE_BUFFER); +} EXPORT_SYMBOL(nft_table_parse); static int nft_table_snprintf_json(char *buf, size_t size, struct nft_table *t) -- cgit v1.2.3