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/table.c | 86 ++++++++++++------------------------------------------------- 1 file changed, 17 insertions(+), 69 deletions(-) (limited to 'src/table.c') diff --git a/src/table.c b/src/table.c index 53f6a4d..c93e6fb 100644 --- a/src/table.c +++ b/src/table.c @@ -24,6 +24,7 @@ #include #include +#include struct nft_table { struct list_head head; @@ -391,75 +392,24 @@ int nft_table_parse_file(struct nft_table *t, enum nft_parse_type type, } EXPORT_SYMBOL(nft_table_parse_file); -static int nft_table_snprintf_json(char *buf, size_t size, struct nft_table *t) +static int nft_table_export(char *buf, size_t size, struct nft_table *t, + int type) { - int ret, len = size, offset = 0; - - ret = snprintf(buf, size, "{\"table\":{"); - SNPRINTF_BUFFER_SIZE(ret, size, len, offset); - ret = 0; - - if (t->flags & (1 << NFT_TABLE_ATTR_NAME)) { - ret = snprintf(buf + offset, size, "\"name\":\"%s\",", t->name); - SNPRINTF_BUFFER_SIZE(ret, size, len, offset); - } - if (t->flags & (1 << NFT_TABLE_ATTR_FAMILY)) { - ret = snprintf(buf + offset, size, "\"family\":\"%s\",", - nft_family2str(t->family)); - SNPRINTF_BUFFER_SIZE(ret, size, len, offset); - } - if (t->flags & (1 << NFT_TABLE_ATTR_FLAGS)) { - ret = snprintf(buf + offset, size, "\"flags\":%u,", - t->table_flags); - SNPRINTF_BUFFER_SIZE(ret, size, len, offset); - } - if (t->flags & (1 << NFT_TABLE_ATTR_USE)) { - ret = snprintf(buf + offset, size, "\"use\":%u,", t->use); - SNPRINTF_BUFFER_SIZE(ret, size, len, offset); - } - - /* If some values is set, ret is not 0. So, It's needed to remove the - * last comma characther - */ - if (ret > 0) - offset--; - - ret = snprintf(buf + offset, size, "}}"); - SNPRINTF_BUFFER_SIZE(ret, size, len, offset); - - return offset; -} - -static int nft_table_snprintf_xml(char *buf, size_t size, struct nft_table *t) -{ - int ret, len = size, offset = 0; - - ret = snprintf(buf, size, ""); - SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + NFT_BUF_INIT(b, buf, size); - if (t->flags & (1 << NFT_TABLE_ATTR_NAME)) { - ret = snprintf(buf + offset, size, "%s", t->name); - SNPRINTF_BUFFER_SIZE(ret, size, len, offset); - } - if (t->flags & (1 << NFT_TABLE_ATTR_FAMILY)) { - ret = snprintf(buf + offset, size, "%s", - nft_family2str(t->family)); - SNPRINTF_BUFFER_SIZE(ret, size, len, offset); - } - if (t->flags & (1 << NFT_TABLE_ATTR_FLAGS)) { - ret = snprintf(buf + offset, size, "%u", - t->table_flags); - SNPRINTF_BUFFER_SIZE(ret, size, len, offset); - } - if (t->flags & (1 << NFT_TABLE_ATTR_USE)) { - ret = snprintf(buf + offset, size, "%u", t->use); - SNPRINTF_BUFFER_SIZE(ret, size, len, offset); - } + nft_buf_open(&b, type, TABLE); + if (t->flags & (1 << NFT_TABLE_ATTR_NAME)) + nft_buf_str(&b, type, t->name, NAME); + if (t->flags & (1 << NFT_TABLE_ATTR_FAMILY)) + nft_buf_str(&b, type, nft_family2str(t->family), FAMILY); + if (t->flags & (1 << NFT_TABLE_ATTR_FLAGS)) + nft_buf_u32(&b, type, t->table_flags, FLAGS); + if (t->flags & (1 << NFT_TABLE_ATTR_USE)) + nft_buf_u32(&b, type, t->use, USE); - ret = snprintf(buf + offset, size, "
"); - SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + nft_buf_close(&b, type, TABLE); - return offset; + return nft_buf_done(&b); } static int nft_table_snprintf_default(char *buf, size_t size, struct nft_table *t) @@ -477,15 +427,13 @@ int nft_table_snprintf(char *buf, size_t size, struct nft_table *t, ret = nft_event_header_snprintf(buf+offset, len, type, flags); SNPRINTF_BUFFER_SIZE(ret, size, len, offset); - switch(type) { + switch (type) { case NFT_OUTPUT_DEFAULT: ret = nft_table_snprintf_default(buf+offset, len, t); break; case NFT_OUTPUT_XML: - ret = nft_table_snprintf_xml(buf+offset, len, t); - break; case NFT_OUTPUT_JSON: - ret = nft_table_snprintf_json(buf+offset, len, t); + ret = nft_table_export(buf+offset, len, t, type); break; default: return -1; -- cgit v1.2.3