From 0976c03aed6196c4a8e40e8797b0d471d538a785 Mon Sep 17 00:00:00 2001 From: Ana Rey Date: Mon, 16 Jun 2014 11:10:16 +0200 Subject: expr: limit: Do not print unset values in json It changes the parse and the snprint functions to omit unset values. Now, This part of a json file is gotten without unset elements. {"expr":[{"type":"limit","rate":400,"unit":60}]} Signed-off-by: Ana Rey Signed-off-by: Pablo Neira Ayuso --- src/expr/limit.c | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) (limited to 'src/expr/limit.c') diff --git a/src/expr/limit.c b/src/expr/limit.c index 35b0c01..78206fa 100644 --- a/src/expr/limit.c +++ b/src/expr/limit.c @@ -124,15 +124,11 @@ static int nft_rule_expr_limit_json_parse(struct nft_rule_expr *e, json_t *root, #ifdef JSON_PARSING uint64_t uval64; - if (nft_jansson_parse_val(root, "rate", NFT_TYPE_U64, &uval64, err) < 0) - return -1; - - nft_rule_expr_set_u64(e, NFT_EXPR_LIMIT_RATE, uval64); + if (nft_jansson_parse_val(root, "rate", NFT_TYPE_U64, &uval64, err) == 0) + nft_rule_expr_set_u64(e, NFT_EXPR_LIMIT_RATE, uval64); - if (nft_jansson_parse_val(root, "unit", NFT_TYPE_U64, &uval64, err) < 0) - return -1; - - nft_rule_expr_set_u64(e, NFT_EXPR_LIMIT_UNIT, uval64); + if (nft_jansson_parse_val(root, "unit", NFT_TYPE_U64, &uval64, err) == 0) + nft_rule_expr_set_u64(e, NFT_EXPR_LIMIT_UNIT, uval64); return 0; #else @@ -193,10 +189,24 @@ 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; - return snprintf(buf, len, "\"rate\":%"PRIu64"," - "\"unit\":%"PRIu64"", - limit->rate, limit->unit); + 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--; + + return offset; } static int nft_rule_expr_limit_snprintf_default(char *buf, size_t len, -- cgit v1.2.3