summaryrefslogtreecommitdiffstats
path: root/src/expr/meta.c
diff options
context:
space:
mode:
authorPablo Neira Ayuso <pablo@netfilter.org>2014-11-09 19:26:48 +0100
committerPablo Neira Ayuso <pablo@netfilter.org>2014-11-10 18:42:37 +0100
commit5a2a1e0447bc9dc3defe6dca75e6a54312bbb045 (patch)
tree0b6d6fc52096aef745f8598588fd64826d584315 /src/expr/meta.c
parent88e823f8dcb24b5201f2fe4f4d5ec5d277bd24b2 (diff)
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 <pablo@netfilter.org>
Diffstat (limited to 'src/expr/meta.c')
-rw-r--r--src/expr/meta.c64
1 files changed, 13 insertions, 51 deletions
diff --git a/src/expr/meta.c b/src/expr/meta.c
index 59cb55b..d1a6bbb 100644
--- a/src/expr/meta.c
+++ b/src/expr/meta.c
@@ -21,6 +21,7 @@
#include <libnftnl/expr.h>
#include <libnftnl/rule.h>
#include "expr_ops.h"
+#include <buffer.h>
#ifndef NFT_META_MAX
#define NFT_META_MAX (NFT_META_CGROUP + 1)
@@ -264,71 +265,32 @@ nft_rule_expr_meta_snprintf_default(char *buf, size_t len,
return 0;
}
-static int
-nft_rule_expr_meta_snprintf_xml(char *buf, size_t size,
- struct nft_rule_expr *e)
-{
- int ret, len = size, offset = 0;
- struct nft_expr_meta *meta = nft_expr_data(e);
-
- if (e->flags & (1 << NFT_EXPR_META_DREG)) {
- ret = snprintf(buf, len, "<dreg>%u</dreg>", meta->dreg);
- SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
- }
- if (e->flags & (1 << NFT_EXPR_META_KEY)) {
- ret = snprintf(buf + offset, len, "<key>%s</key>",
- meta_key2str(meta->key));
- SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
- }
- if (e->flags & (1 << NFT_EXPR_META_SREG)) {
- ret = snprintf(buf + offset, len, "<sreg>%u</sreg>",
- meta->sreg);
- SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
- }
-
- return offset;
-}
-
-static int
-nft_rule_expr_meta_snprintf_json(char *buf, size_t size,
- struct nft_rule_expr *e)
+static int nft_rule_expr_meta_export(char *buf, size_t size,
+ struct nft_rule_expr *e, int type)
{
- int ret, len = size, offset = 0;
struct nft_expr_meta *meta = nft_expr_data(e);
+ NFT_BUF_INIT(b, buf, size);
- if (e->flags & (1 << NFT_EXPR_META_DREG)) {
- ret = snprintf(buf + offset, len, "\"dreg\":%u,",
- meta->dreg);
- SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
- }
- if (e->flags & (1 << NFT_EXPR_META_KEY)) {
- ret = snprintf(buf + offset, len, "\"key\":\"%s\",",
- meta_key2str(meta->key));
- SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
- }
- if (e->flags & (1 << NFT_EXPR_META_SREG)) {
- ret = snprintf(buf + offset, len, "\"sreg\":%u,",
- meta->sreg);
- SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
- }
- /* Remove the last comma separator */
- if (offset > 0)
- offset--;
+ if (e->flags & (1 << NFT_EXPR_META_DREG))
+ nft_buf_u32(&b, type, meta->dreg, DREG);
+ if (e->flags & (1 << NFT_EXPR_META_KEY))
+ nft_buf_str(&b, type, meta_key2str(meta->key), KEY);
+ if (e->flags & (1 << NFT_EXPR_META_SREG))
+ nft_buf_u32(&b, type, meta->sreg, SREG);
- return offset;
+ return nft_buf_done(&b);
}
static int
nft_rule_expr_meta_snprintf(char *buf, size_t len, uint32_t type,
uint32_t flags, struct nft_rule_expr *e)
{
- switch(type) {
+ switch (type) {
case NFT_OUTPUT_DEFAULT:
return nft_rule_expr_meta_snprintf_default(buf, len, e);
case NFT_OUTPUT_XML:
- return nft_rule_expr_meta_snprintf_xml(buf, len, e);
case NFT_OUTPUT_JSON:
- return nft_rule_expr_meta_snprintf_json(buf, len, e);
+ return nft_rule_expr_meta_export(buf, len, e, type);
default:
break;
}