summaryrefslogtreecommitdiffstats
path: root/src/expr/log.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/log.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/log.c')
-rw-r--r--src/expr/log.c98
1 files changed, 18 insertions, 80 deletions
diff --git a/src/expr/log.c b/src/expr/log.c
index 98481c9..0a324c4 100644
--- a/src/expr/log.c
+++ b/src/expr/log.c
@@ -21,6 +21,7 @@
#include <libnftnl/expr.h>
#include <libnftnl/rule.h>
#include "expr_ops.h"
+#include <buffer.h>
struct nft_expr_log {
uint32_t snaplen;
@@ -288,90 +289,28 @@ static int nft_rule_expr_log_snprintf_default(char *buf, size_t size,
return offset;
}
-static int nft_rule_expr_log_snprintf_xml(char *buf, size_t size,
- struct nft_rule_expr *e)
+static int nft_rule_expr_log_export(char *buf, size_t size,
+ struct nft_rule_expr *e, int type)
{
- int ret, len = size, offset = 0;
struct nft_expr_log *log = nft_expr_data(e);
+ NFT_BUF_INIT(b, buf, size);
- if (e->flags & (1 << NFT_EXPR_LOG_PREFIX)) {
- ret = snprintf(buf + offset, len, "<prefix>%s</prefix>",
- log->prefix);
- SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
- }
- if (e->flags & (1 << NFT_EXPR_LOG_GROUP)) {
- ret = snprintf(buf + offset, len, "<group>%u</group>",
- log->group);
- SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
- }
- if (e->flags & (1 << NFT_EXPR_LOG_SNAPLEN)) {
- ret = snprintf(buf + offset, len, "<snaplen>%u</snaplen>",
- log->snaplen);
- SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
- }
- if (e->flags & (1 << NFT_EXPR_LOG_QTHRESHOLD)) {
- ret = snprintf(buf + offset, len, "<qthreshold>%u</qthreshold>",
- log->qthreshold);
- SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
- }
- if (e->flags & (1 << NFT_EXPR_LOG_LEVEL)) {
- ret = snprintf(buf + offset, len, "<level>%u</level>",
- log->level);
- SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
- }
- if (e->flags & (1 << NFT_EXPR_LOG_FLAGS)) {
- ret = snprintf(buf + offset, len, "<flags>%u</flags>",
- log->flags);
- SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
- }
-
- return offset;
-}
-
-static int nft_rule_expr_log_snprintf_json(char *buf, size_t len,
- struct nft_rule_expr *e)
-{
- int ret, size = len, offset = 0;
- struct nft_expr_log *log = nft_expr_data(e);
-
- if (e->flags & (1 << NFT_EXPR_LOG_PREFIX)) {
- ret = snprintf(buf + offset, len, "\"prefix\":\"%s\",",
- log->prefix);
- SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
- }
- if (e->flags & (1 << NFT_EXPR_LOG_GROUP)) {
- ret = snprintf(buf + offset, len, "\"group\":%u,",
- log->group);
- SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
- }
- if (e->flags & (1 << NFT_EXPR_LOG_SNAPLEN)) {
- ret = snprintf(buf + offset, len, "\"snaplen\":%u,",
- log->snaplen);
- SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
- }
- if (e->flags & (1 << NFT_EXPR_LOG_QTHRESHOLD)) {
- ret = snprintf(buf + offset, len, "\"qthreshold\":%u,",
- log->qthreshold);
- SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
- }
- if (e->flags & (1 << NFT_EXPR_LOG_LEVEL)) {
- ret = snprintf(buf + offset, len, "\"level\":%u,",
- log->level);
- SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
- }
- if (e->flags & (1 << NFT_EXPR_LOG_FLAGS)) {
- ret = snprintf(buf + offset, len, "\"flags\":%u,",
- log->flags);
- SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
- }
- /* Remove the last comma characther */
- if (offset > 0)
- offset--;
+ if (e->flags & (1 << NFT_EXPR_LOG_PREFIX))
+ nft_buf_str(&b, type, log->prefix, PREFIX);
+ if (e->flags & (1 << NFT_EXPR_LOG_GROUP))
+ nft_buf_u32(&b, type, log->group, GROUP);
+ if (e->flags & (1 << NFT_EXPR_LOG_SNAPLEN))
+ nft_buf_u32(&b, type, log->snaplen, SNAPLEN);
+ if (e->flags & (1 << NFT_EXPR_LOG_QTHRESHOLD))
+ nft_buf_u32(&b, type, log->qthreshold, QTHRESH);
+ if (e->flags & (1 << NFT_EXPR_LOG_LEVEL))
+ nft_buf_u32(&b, type, log->level, LEVEL);
+ if (e->flags & (1 << NFT_EXPR_LOG_FLAGS))
+ nft_buf_u32(&b, type, log->level, FLAGS);
- return offset;
+ return nft_buf_done(&b);
}
-
static int
nft_rule_expr_log_snprintf(char *buf, size_t len, uint32_t type,
uint32_t flags, struct nft_rule_expr *e)
@@ -380,9 +319,8 @@ nft_rule_expr_log_snprintf(char *buf, size_t len, uint32_t type,
case NFT_OUTPUT_DEFAULT:
return nft_rule_expr_log_snprintf_default(buf, len, e);
case NFT_OUTPUT_XML:
- return nft_rule_expr_log_snprintf_xml(buf, len, e);
case NFT_OUTPUT_JSON:
- return nft_rule_expr_log_snprintf_json(buf, len, e);
+ return nft_rule_expr_log_export(buf, len, e, type);
default:
break;
}