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/payload.c | 69 ++++++++++++------------------------------------------ 1 file changed, 15 insertions(+), 54 deletions(-) (limited to 'src/expr/payload.c') diff --git a/src/expr/payload.c b/src/expr/payload.c index 717cdac..1aa20bd 100644 --- a/src/expr/payload.c +++ b/src/expr/payload.c @@ -25,6 +25,7 @@ #include #include "expr_ops.h" +#include struct nft_expr_payload { enum nft_registers dreg; @@ -161,34 +162,6 @@ static const char *base2str(enum nft_payload_bases base) return base2str_array[base]; } -static int -nft_rule_expr_payload_snprintf_json(char *buf, size_t len, uint32_t flags, - struct nft_rule_expr *e) -{ - struct nft_expr_payload *payload = nft_expr_data(e); - int size = len, offset = 0, ret; - - if (e->flags & (1 << NFT_EXPR_PAYLOAD_DREG)) { - ret = snprintf(buf, len, "\"dreg\":%u,", payload->dreg); - SNPRINTF_BUFFER_SIZE(ret, size, len, offset); - } - if (e->flags & (1 << NFT_EXPR_PAYLOAD_OFFSET)) { - ret = snprintf(buf + offset, len, "\"offset\":%u,", - payload->offset); - SNPRINTF_BUFFER_SIZE(ret, size, len, offset); - } - if (e->flags & (1 << NFT_EXPR_PAYLOAD_LEN)) { - ret = snprintf(buf + offset, len, "\"len\":%u,", payload->len); - SNPRINTF_BUFFER_SIZE(ret, size, len, offset); - } - if (e->flags & (1 << NFT_EXPR_PAYLOAD_BASE)) { - ret = snprintf(buf + offset, len, "\"base\":\"%s\"", - base2str(payload->base)); - SNPRINTF_BUFFER_SIZE(ret, size, len, offset); - } - return offset; -} - static inline int nft_str2base(const char *base) { if (strcmp(base, "link") == 0) @@ -277,33 +250,22 @@ nft_rule_expr_payload_xml_parse(struct nft_rule_expr *e, mxml_node_t *tree, #endif } -static int -nft_rule_expr_payload_snprintf_xml(char *buf, size_t len, uint32_t flags, - struct nft_rule_expr *e) +static int nft_rule_expr_payload_export(char *buf, size_t size, uint32_t flags, + struct nft_rule_expr *e, int type) { struct nft_expr_payload *payload = nft_expr_data(e); - int size = len, offset = 0, ret; + NFT_BUF_INIT(b, buf, size); - if (e->flags & (1 << NFT_EXPR_PAYLOAD_DREG)) { - ret = snprintf(buf, len, "%u", payload->dreg); - SNPRINTF_BUFFER_SIZE(ret, size, len, offset); - } - if (e->flags & (1 << NFT_EXPR_PAYLOAD_OFFSET)) { - ret = snprintf(buf + offset, len, "%u", - payload->offset); - SNPRINTF_BUFFER_SIZE(ret, size, len, offset); - } - if (e->flags & (1 << NFT_EXPR_PAYLOAD_LEN)) { - ret = snprintf(buf + offset, len, "%u", payload->len); - SNPRINTF_BUFFER_SIZE(ret, size, len, offset); - } - if (e->flags & (1 << NFT_EXPR_PAYLOAD_BASE)) { - ret = snprintf(buf + offset, len, "%s", - base2str(payload->base)); - SNPRINTF_BUFFER_SIZE(ret, size, len, offset); - } + if (e->flags & (1 << NFT_EXPR_PAYLOAD_DREG)) + nft_buf_u32(&b, type, payload->dreg, DREG); + if (e->flags & (1 << NFT_EXPR_PAYLOAD_OFFSET)) + nft_buf_u32(&b, type, payload->offset, OFFSET); + if (e->flags & (1 << NFT_EXPR_PAYLOAD_LEN)) + nft_buf_u32(&b, type, payload->len, LEN); + if (e->flags & (1 << NFT_EXPR_PAYLOAD_BASE)) + nft_buf_str(&b, type, base2str(payload->base), BASE); - return offset; + return nft_buf_done(&b); } static int @@ -312,15 +274,14 @@ nft_rule_expr_payload_snprintf(char *buf, size_t len, uint32_t type, { struct nft_expr_payload *payload = nft_expr_data(e); - switch(type) { + switch (type) { case NFT_OUTPUT_DEFAULT: return snprintf(buf, len, "load %ub @ %s header + %u => reg %u ", payload->len, base2str(payload->base), payload->offset, payload->dreg); case NFT_OUTPUT_XML: - return nft_rule_expr_payload_snprintf_xml(buf, len, flags, e); case NFT_OUTPUT_JSON: - return nft_rule_expr_payload_snprintf_json(buf, len, flags, e); + return nft_rule_expr_payload_export(buf, len, flags, e, type); default: break; } -- cgit v1.2.3