From: Ana Rey <anarey@gmail.com>
To: netfilter-devel@vger.kernel.org
Cc: Ana Rey <anarey@gmail.com>
Subject: [libnftnl PATCH 5/5] expr: byteorder: Do not print unset values in json
Date: Thu, 19 Jun 2014 17:07:10 +0200 [thread overview]
Message-ID: <1403190430-22173-6-git-send-email-anarey@gmail.com> (raw)
In-Reply-To: <1403190430-22173-1-git-send-email-anarey@gmail.com>
It changes the parse and the snprint functions to omit unset values.
Now, This part of a json file is gotten without unset elements.
[FILE EXAMPLE]
Signed-off-by: Ana Rey <anarey@gmail.com>
---
src/expr/byteorder.c | 77 ++++++++++++++++++++++++++++++----------------------
1 file changed, 44 insertions(+), 33 deletions(-)
diff --git a/src/expr/byteorder.c b/src/expr/byteorder.c
index e56af3f..20a89d3 100644
--- a/src/expr/byteorder.c
+++ b/src/expr/byteorder.c
@@ -202,35 +202,24 @@ nft_rule_expr_byteorder_json_parse(struct nft_rule_expr *e, json_t *root,
uint32_t sreg, dreg, len, size;
int ntoh;
- if (nft_jansson_parse_reg(root, "sreg", NFT_TYPE_U32, &sreg, err) < 0)
- return -1;
-
- nft_rule_expr_set_u32(e, NFT_EXPR_BYTEORDER_SREG, sreg);
-
- if (nft_jansson_parse_reg(root, "dreg", NFT_TYPE_U32, &dreg, err) < 0)
- return -1;
+ if (nft_jansson_parse_reg(root, "sreg", NFT_TYPE_U32, &sreg, err) == 0)
+ nft_rule_expr_set_u32(e, NFT_EXPR_BYTEORDER_SREG, sreg);
- nft_rule_expr_set_u32(e, NFT_EXPR_BYTEORDER_DREG, dreg);
+ if (nft_jansson_parse_reg(root, "dreg", NFT_TYPE_U32, &dreg, err) == 0)
+ nft_rule_expr_set_u32(e, NFT_EXPR_BYTEORDER_DREG, dreg);
op = nft_jansson_parse_str(root, "op", err);
- if (op == NULL)
- return -1;
-
- ntoh = nft_str2ntoh(op);
- if (ntoh < 0)
- return -1;
-
- nft_rule_expr_set_u32(e, NFT_EXPR_BYTEORDER_OP, ntoh);
-
- if (nft_jansson_parse_val(root, "len", NFT_TYPE_U32, &len, err) < 0)
- return -1;
-
- nft_rule_expr_set_u32(e, NFT_EXPR_BYTEORDER_LEN, len);
+ if (op != NULL) {
+ ntoh = nft_str2ntoh(op);
+ if (ntoh >= 0)
+ nft_rule_expr_set_u32(e, NFT_EXPR_BYTEORDER_OP, ntoh);
+ }
- if (nft_jansson_parse_val(root, "size", NFT_TYPE_U32, &size, err) < 0)
- return -1;
+ if (nft_jansson_parse_val(root, "len", NFT_TYPE_U32, &len, err) == 0)
+ nft_rule_expr_set_u32(e, NFT_EXPR_BYTEORDER_LEN, len);
- nft_rule_expr_set_u32(e, NFT_EXPR_BYTEORDER_SIZE, size);
+ if (nft_jansson_parse_val(root, "size", NFT_TYPE_U32, &size, err) == 0)
+ nft_rule_expr_set_u32(e, NFT_EXPR_BYTEORDER_SIZE, size);
return 0;
#else
@@ -285,15 +274,37 @@ static int nft_rule_expr_byteorder_snprintf_json(char *buf, size_t size,
struct nft_expr_byteorder *byteorder = nft_expr_data(e);
int len = size, offset = 0, ret;
- ret = snprintf(buf, len, "\"sreg\":%u,"
- "\"dreg\":%u,"
- "\"op\":\"%s\","
- "\"len\":%u,"
- "\"size\":%u",
- byteorder->sreg, byteorder->dreg,
- expr_byteorder_str[byteorder->op],
- byteorder->len, byteorder->size);
- SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+ if (e->flags & (1 << NFT_EXPR_BYTEORDER_SREG)) {
+ ret = snprintf(buf + offset, len, "\"sreg\":%u,",
+ byteorder->sreg);
+ SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+ }
+
+ if (e->flags & (1 << NFT_EXPR_BYTEORDER_DREG)) {
+ ret = snprintf(buf + offset, len, "\"dreg\":%u,",
+ byteorder->dreg);
+ SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+ }
+
+ if (e->flags & (1 << NFT_EXPR_BYTEORDER_OP)) {
+ ret = snprintf(buf + offset, len, "\"op\":\"%s\",",
+ expr_byteorder_str[byteorder->op]);
+ SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+ }
+
+ if (e->flags & (1 << NFT_EXPR_BYTEORDER_LEN)) {
+ ret = snprintf(buf + offset, len, "\"len\":%u,",
+ byteorder->len);
+ SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+ }
+ if (e->flags & (1 << NFT_EXPR_BYTEORDER_SIZE)) {
+ ret = snprintf(buf + offset, len, "\"size\":%u,",
+ byteorder->size);
+ SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+ }
+
+ if (offset > 0)
+ offset--;
return offset;
}
--
2.0.0
next prev parent reply other threads:[~2014-06-19 15:07 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-06-19 15:07 [libnftnl PATCH 0/5] expr: bitwise: Do not print unset values Ana Rey
2014-06-19 15:07 ` [libnftnl PATCH 1/5] expr: byteorder: Get a nft_rule_expr type in nft_rule_expr_byteorder_snprintf_* functions Ana Rey
2014-06-19 15:07 ` [libnftnl PATCH 2/5] expr: byteorder: Use nft_rule_expr_set_* in the xml parsing code Ana Rey
2014-06-19 15:07 ` [libnftnl PATCH 3/5] expr: byteorder: Do not print unset values in xml file Ana Rey
2014-06-20 7:12 ` Arturo Borrero Gonzalez
2014-06-19 15:07 ` [libnftnl PATCH 4/5] expr: byteorder: Rename variables in nft_rule_expr_byteorder_json_parse functions Ana Rey
2014-06-19 15:07 ` Ana Rey [this message]
2014-06-20 7:16 ` [libnftnl PATCH 0/5] expr: bitwise: Do not print unset values Arturo Borrero Gonzalez
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1403190430-22173-6-git-send-email-anarey@gmail.com \
--to=anarey@gmail.com \
--cc=netfilter-devel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).