From 5a2a1e0447bc9dc3defe6dca75e6a54312bbb045 Mon Sep 17 00:00:00 2001 From: Pablo Neira Ayuso Date: Sun, 9 Nov 2014 19:26:48 +0100 Subject: src: consolidate XML/JSON exportation Add new buffer class to consolidate the existing code to export objects in XML/JSON and use it. We save ~700 LOC with this change. The rule and set objects are not yet consolidated. It seems this would require some specific glue code per representation type since lists are arranged differently. This also consolidates the tag names, so we make sure the same are used from XML and JSON by placing them in include/buffer.h. Signed-off-by: Pablo Neira Ayuso --- src/expr/match.c | 35 +++++++++-------------------------- 1 file changed, 9 insertions(+), 26 deletions(-) (limited to 'src/expr/match.c') diff --git a/src/expr/match.c b/src/expr/match.c index dc66585..26a368f 100644 --- a/src/expr/match.c +++ b/src/expr/match.c @@ -25,6 +25,7 @@ #include #include "expr_ops.h" +#include /* From include/linux/netfilter/x_tables.h */ #define XT_EXTENSION_MAXNAMELEN 29 @@ -204,33 +205,16 @@ static int nft_rule_expr_match_xml_parse(struct nft_rule_expr *e, mxml_node_t *t #endif } -static int nft_rule_expr_match_snprintf_json(char *buf, size_t len, - struct nft_rule_expr *e) +static int nft_rule_expr_match_export(char *buf, size_t size, + struct nft_rule_expr *e, int type) { struct nft_expr_match *mt = nft_expr_data(e); - int ret, size = len, offset = 0; + NFT_BUF_INIT(b, buf, size); - if (e->flags & (1 << NFT_EXPR_MT_NAME)) { - ret = snprintf(buf, len, "\"name\":\"%s\"", mt->name); - SNPRINTF_BUFFER_SIZE(ret, size, len, offset); - } - - return offset; -} - -static int nft_rule_expr_match_snprintf_xml(char *buf, size_t len, - struct nft_rule_expr *e) -{ - struct nft_expr_match *mt = nft_expr_data(e); - int ret, size=len; - int offset = 0; - - if (e->flags & (1 << NFT_EXPR_MT_NAME)) { - ret = snprintf(buf, len, "%s", mt->name); - SNPRINTF_BUFFER_SIZE(ret, size, len, offset); - } + if (e->flags & (1 << NFT_EXPR_MT_NAME)) + nft_buf_str(&b, type, mt->name, NAME); - return offset; + return nft_buf_done(&b); } static int @@ -239,14 +223,13 @@ nft_rule_expr_match_snprintf(char *buf, size_t len, uint32_t type, { struct nft_expr_match *match = nft_expr_data(e); - switch(type) { + switch (type) { case NFT_OUTPUT_DEFAULT: return snprintf(buf, len, "name %s rev %u ", match->name, match->rev); case NFT_OUTPUT_XML: - return nft_rule_expr_match_snprintf_xml(buf, len, e); case NFT_OUTPUT_JSON: - return nft_rule_expr_match_snprintf_json(buf, len, e); + return nft_rule_expr_match_export(buf, len, e, type); default: break; } -- cgit v1.2.3