summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAna Rey <anarey@gmail.com>2014-06-04 13:03:31 +0200
committerPablo Neira Ayuso <pablo@netfilter.org>2014-06-05 15:23:01 +0200
commita596c41426566f54078f854e138e73a88252e7b9 (patch)
tree89bbeb0b5aa7cf1fb59b20de476ba6a1a3cbc923 /src
parentd79329a5e99f4f2fdf293f9e3418cad0af7e7b1d (diff)
expr: log: Do not print unset values in json
It changes the parse and the snprint functions to omit unset values. Also, It fixes an unnecessary comma after key-value pair type. This comma is not necessary if there is not more key-value pairs in this expr. Example: "expr":[{"type":"log"}] If It uses this rule: nft add rule ip test output log It gets this json file: [...] {"expr":[{"type":"log","prefix":"(null)","group":0,"snaplen":0,"qthreshold":0}]} [...] Now, That rule creates this json file without null values: {"expr":[{"type":"log"}]} Signed-off-by: Ana Rey <anarey@gmail.com> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'src')
-rw-r--r--src/expr/log.c56
-rw-r--r--src/rule.c9
2 files changed, 43 insertions, 22 deletions
diff --git a/src/expr/log.c b/src/expr/log.c
index bd9bdc5..5032fa7 100644
--- a/src/expr/log.c
+++ b/src/expr/log.c
@@ -169,28 +169,20 @@ static int nft_rule_expr_log_json_parse(struct nft_rule_expr *e, json_t *root,
uint16_t group, qthreshold;
prefix = nft_jansson_parse_str(root, "prefix", err);
- if (prefix == NULL)
- return -1;
-
- nft_rule_expr_set_str(e, NFT_EXPR_LOG_PREFIX, prefix);
+ if (prefix != NULL)
+ nft_rule_expr_set_str(e, NFT_EXPR_LOG_PREFIX, prefix);
if (nft_jansson_parse_val(root, "group", NFT_TYPE_U16, &group,
- err) < 0)
- return -1;
-
- nft_rule_expr_set_u16(e, NFT_EXPR_LOG_GROUP, group);
+ err) == 0)
+ nft_rule_expr_set_u16(e, NFT_EXPR_LOG_GROUP, group);
if (nft_jansson_parse_val(root, "snaplen", NFT_TYPE_U32, &snaplen,
- err) < 0)
- return -1;
-
- nft_rule_expr_set_u32(e, NFT_EXPR_LOG_SNAPLEN, snaplen);
+ err) == 0)
+ nft_rule_expr_set_u32(e, NFT_EXPR_LOG_SNAPLEN, snaplen);
if (nft_jansson_parse_val(root, "qthreshold", NFT_TYPE_U16,
- &qthreshold, err) < 0)
- return -1;
-
- nft_rule_expr_set_u16(e, NFT_EXPR_LOG_QTHRESHOLD, qthreshold);
+ &qthreshold, err) == 0)
+ nft_rule_expr_set_u16(e, NFT_EXPR_LOG_QTHRESHOLD, qthreshold);
return 0;
#else
@@ -276,14 +268,34 @@ static int nft_rule_expr_log_snprintf_xml(char *buf, size_t size,
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);
- return snprintf(buf, len, "\"prefix\":\"%s\","
- "\"group\":%u,"
- "\"snaplen\":%u,"
- "\"qthreshold\":%u",
- log->prefix, log->group,
- log->snaplen, log->qthreshold);
+ 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);
+ }
+ /* Remove the last comma characther */
+ if (offset > 0)
+ offset--;
+
+ return offset;
}
diff --git a/src/rule.c b/src/rule.c
index ac88abb..88e9f71 100644
--- a/src/rule.c
+++ b/src/rule.c
@@ -816,6 +816,15 @@ static int nft_rule_snprintf_json(char *buf, size_t size, struct nft_rule *r,
ret = expr->ops->snprintf(buf+offset, len, type, flags, expr);
SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+ /*
+ * Remove comma from the first element if there is type
+ * key-value pair only. Example: "expr":[{"type":"log"}]
+ */
+ if (ret == 0) {
+ offset--;
+ len--;
+ }
+
ret = snprintf(buf+offset, len, "},");
SNPRINTF_BUFFER_SIZE(ret, size, len, offset);