summaryrefslogtreecommitdiffstats
path: root/src/expr/limit.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/limit.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/limit.c')
-rw-r--r--src/expr/limit.c50
1 files changed, 10 insertions, 40 deletions
diff --git a/src/expr/limit.c b/src/expr/limit.c
index 68cfa37..375e6e0 100644
--- a/src/expr/limit.c
+++ b/src/expr/limit.c
@@ -22,6 +22,7 @@
#include <libnftnl/expr.h>
#include <libnftnl/rule.h>
#include "expr_ops.h"
+#include <buffer.h>
struct nft_expr_limit {
uint64_t rate;
@@ -169,48 +170,18 @@ static const char *get_unit(uint64_t u)
return "error";
}
-static int nft_rule_expr_limit_snprintf_xml(char *buf, size_t len,
- struct nft_rule_expr *e)
+static int nft_rule_expr_limit_export(char *buf, size_t size,
+ struct nft_rule_expr *e, int type)
{
struct nft_expr_limit *limit = nft_expr_data(e);
- int ret, size = len, offset = 0;
+ NFT_BUF_INIT(b, buf, size);
- if (e->flags & (1 << NFT_EXPR_LIMIT_RATE)) {
- ret = snprintf(buf + offset, len, "<rate>%"PRIu64"</rate>",
- limit->rate);
- SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
- }
- if (e->flags & (1 << NFT_EXPR_LIMIT_UNIT)) {
- ret = snprintf(buf + offset, len, "<unit>%"PRIu64"</unit>",
- limit->unit);
- SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
- }
-
- return offset;
-}
-
-static int nft_rule_expr_limit_snprintf_json(char *buf, size_t len,
- struct nft_rule_expr *e)
-{
- struct nft_expr_limit *limit = nft_expr_data(e);
- int ret, size = len, offset = 0;
-
- if (e->flags & (1 << NFT_EXPR_LIMIT_RATE)) {
- ret = snprintf(buf + offset, len, "\"rate\":%"PRIu64",",
- limit->rate);
- SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
- }
- if (e->flags & (1 << NFT_EXPR_LIMIT_UNIT)) {
- ret = snprintf(buf + offset, len, "\"unit\":%"PRIu64",",
- limit->unit);
- SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
- }
-
- /* Remove the last comma characther */
- if (offset > 0)
- offset--;
+ if (e->flags & (1 << NFT_EXPR_LIMIT_RATE))
+ nft_buf_u64(&b, type, limit->rate, RATE);
+ if (e->flags & (1 << NFT_EXPR_LIMIT_UNIT))
+ nft_buf_u64(&b, type, limit->unit, UNIT);
- return offset;
+ return nft_buf_done(&b);
}
static int nft_rule_expr_limit_snprintf_default(char *buf, size_t len,
@@ -231,9 +202,8 @@ nft_rule_expr_limit_snprintf(char *buf, size_t len, uint32_t type,
case NFT_OUTPUT_DEFAULT:
return nft_rule_expr_limit_snprintf_default(buf, len, e);
case NFT_OUTPUT_XML:
- return nft_rule_expr_limit_snprintf_xml(buf, len, e);
case NFT_OUTPUT_JSON:
- return nft_rule_expr_limit_snprintf_json(buf, len, e);
+ return nft_rule_expr_limit_export(buf, len, e, type);
default:
break;
}