summaryrefslogtreecommitdiffstats
path: root/src/expr/ct.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/ct.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/ct.c')
-rw-r--r--src/expr/ct.c73
1 files changed, 14 insertions, 59 deletions
diff --git a/src/expr/ct.c b/src/expr/ct.c
index d443c1e..12d96d5 100644
--- a/src/expr/ct.c
+++ b/src/expr/ct.c
@@ -21,6 +21,7 @@
#include <libnftnl/expr.h>
#include <libnftnl/rule.h>
#include "expr_ops.h"
+#include <buffer.h>
struct nft_expr_ct {
enum nft_ct_keys key;
@@ -310,66 +311,21 @@ err:
}
static int
-nft_expr_ct_snprintf_json(char *buf, size_t size, struct nft_rule_expr *e)
+nft_expr_ct_export(char *buf, size_t size, struct nft_rule_expr *e, int type)
{
- int ret, len = size, offset = 0;
- struct nft_expr_ct *ct = nft_expr_data(e);
-
- if (e->flags & (1 << NFT_EXPR_CT_DREG)) {
- ret = snprintf(buf+offset, len, "\"dreg\":%u,", ct->dreg);
- SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
- }
-
- if (e->flags & (1 << NFT_EXPR_CT_SREG)) {
- ret = snprintf(buf+offset, len, "\"sreg:\":%u,", ct->sreg);
- SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
- }
-
- if (e->flags & (1 << NFT_EXPR_CT_KEY)) {
- ret = snprintf(buf+offset, len, "\"key\":\"%s\",",
- ctkey2str(ct->key));
- SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
- }
-
- if (nft_rule_expr_is_set(e, NFT_EXPR_CT_DIR)) {
- ret = snprintf(buf+offset, len, "\"dir\":\"%s\",",
- ctdir2str(ct->dir));
- SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
- }
-
- /* Remove the last separator characther */
- if (offset > 0)
- offset--;
-
- return offset;
-}
-
-static int
-nft_expr_ct_snprintf_xml(char *buf, size_t size, struct nft_rule_expr *e)
-{
- int ret, len = size, offset = 0;
struct nft_expr_ct *ct = nft_expr_data(e);
+ NFT_BUF_INIT(b, buf, size);
- if (e->flags & (1 << NFT_EXPR_CT_DREG)) {
- ret = snprintf(buf + offset, len, "<dreg>%u</dreg>", ct->dreg);
- SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
- }
- if (e->flags & (1 << NFT_EXPR_CT_SREG)) {
- ret = snprintf(buf + offset, len, "<sreg>%u</sreg>", ct->sreg);
- SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
- }
- if (e->flags & (1 << NFT_EXPR_CT_KEY)) {
- ret = snprintf(buf + offset, len, "<key>%s</key>",
- ctkey2str(ct->key));
- SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
- }
- if (nft_rule_expr_is_set(e, NFT_EXPR_CT_DIR)) {
- ret = snprintf(buf + offset, len, "<dir>%s</dir>",
- ctdir2str(ct->dir));
- SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
- }
+ if (e->flags & (1 << NFT_EXPR_CT_SREG))
+ nft_buf_u32(&b, type, ct->sreg, SREG);
+ if (e->flags & (1 << NFT_EXPR_CT_DREG))
+ nft_buf_u32(&b, type, ct->dreg, DREG);
+ if (e->flags & (1 << NFT_EXPR_CT_KEY))
+ nft_buf_str(&b, type, ctkey2str(ct->key), KEY);
+ if (e->flags & (1 << NFT_EXPR_CT_DIR))
+ nft_buf_str(&b, type, ctdir2str(ct->dir), DIR);
- return offset;
+ return nft_buf_done(&b);
}
static int
@@ -403,13 +359,12 @@ static int
nft_rule_expr_ct_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_expr_ct_snprintf_default(buf, len, e);
case NFT_OUTPUT_XML:
- return nft_expr_ct_snprintf_xml(buf, len, e);
case NFT_OUTPUT_JSON:
- return nft_expr_ct_snprintf_json(buf, len, e);
+ return nft_expr_ct_export(buf, len, e, type);
default:
break;
}