summaryrefslogtreecommitdiffstats
path: root/src/rule.c
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/rule.c
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/rule.c')
-rw-r--r--src/rule.c9
1 files changed, 9 insertions, 0 deletions
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);