From: Ana Rey <anarey@gmail.com>
To: netfilter-devel@vger.kernel.org
Cc: Ana Rey <anarey@gmail.com>
Subject: [libnftnl PATCH 4/4] expr: payload: Do not print unset values in json file
Date: Sun, 22 Jun 2014 20:32:21 +0200 [thread overview]
Message-ID: <1403461941-21605-5-git-send-email-anarey@gmail.com> (raw)
In-Reply-To: <1403461941-21605-1-git-send-email-anarey@gmail.com>
It changes the parse functions to omit unset values.
Signed-off-by: Ana Rey <anarey@gmail.com>
---
src/expr/payload.c | 56 ++++++++++++++++++++++++++++--------------------------
1 file changed, 29 insertions(+), 27 deletions(-)
diff --git a/src/expr/payload.c b/src/expr/payload.c
index 3cf2362..3f89ca3 100644
--- a/src/expr/payload.c
+++ b/src/expr/payload.c
@@ -170,14 +170,25 @@ nft_rule_expr_payload_snprintf_json(char *buf, size_t len, uint32_t flags,
struct nft_expr_payload *payload = nft_expr_data(e);
int size = len, offset = 0, ret;
- ret = snprintf(buf, len, "\"dreg\":%u,"
- "\"offset\":%u,"
- "\"len\":%u,"
- "\"base\":\"%s\"",
- payload->dreg, payload->offset, payload->len,
- base2str(payload->base));
- SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+ if (e->flags & (1 << NFT_EXPR_PAYLOAD_DREG)) {
+ ret = snprintf(buf, len, "\"dreg\":%u,", payload->dreg);
+ SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+ }
+ if (e->flags & (1 << NFT_EXPR_PAYLOAD_OFFSET)) {
+ ret = snprintf(buf + offset, len, "\"offset\":%u,",
+ payload->offset);
+ SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+ }
+ if (e->flags & (1 << NFT_EXPR_PAYLOAD_LEN)) {
+ ret = snprintf(buf + offset, len, "\"len\":%u,", payload->len);
+ SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+ }
+ if (e->flags & (1 << NFT_EXPR_PAYLOAD_BASE)) {
+ ret = snprintf(buf + offset, len, "\"base\":\"%s\"",
+ base2str(payload->base));
+ SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+ }
return offset;
}
@@ -204,31 +215,22 @@ nft_rule_expr_payload_json_parse(struct nft_rule_expr *e, json_t *root,
uint32_t reg, uval32;
int base;
- if (nft_jansson_parse_reg(root, "dreg", NFT_TYPE_U32, ®, err) < 0)
- return -1;
-
- nft_rule_expr_set_u32(e, NFT_EXPR_PAYLOAD_DREG, reg);
+ if (nft_jansson_parse_reg(root, "dreg", NFT_TYPE_U32, ®, err) == 0)
+ nft_rule_expr_set_u32(e, NFT_EXPR_PAYLOAD_DREG, reg);
base_str = nft_jansson_parse_str(root, "base", err);
- if (base_str == NULL)
- return -1;
-
- base = nft_str2base(base_str);
- if (base < 0)
- return -1;
-
- nft_rule_expr_set_u32(e, NFT_EXPR_PAYLOAD_BASE, base);
+ if (base_str != NULL) {
+ base = nft_str2base(base_str);
+ if (base >= 0)
+ nft_rule_expr_set_u32(e, NFT_EXPR_PAYLOAD_BASE, base);
+ }
if (nft_jansson_parse_val(root, "offset", NFT_TYPE_U32, &uval32,
- err) < 0)
- return -1;
-
- nft_rule_expr_set_u32(e, NFT_EXPR_PAYLOAD_OFFSET, uval32);
-
- if (nft_jansson_parse_val(root, "len", NFT_TYPE_U32, &uval32, err) < 0)
- return -1;
+ err) == 0)
+ nft_rule_expr_set_u32(e, NFT_EXPR_PAYLOAD_OFFSET, uval32);
- nft_rule_expr_set_u32(e, NFT_EXPR_PAYLOAD_LEN, uval32);
+ if (nft_jansson_parse_val(root, "len", NFT_TYPE_U32, &uval32, err) == 0)
+ nft_rule_expr_set_u32(e, NFT_EXPR_PAYLOAD_LEN, uval32);
return 0;
#else
--
2.0.0
next prev parent reply other threads:[~2014-06-22 18:32 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-06-22 18:32 [libnftnl PATCH 0/4] expr: payload: Do not print unset values in xml and json Ana Rey
2014-06-22 18:32 ` [libnftnl PATCH 1/4] expr: payload: Use nft_rule_expr_set_* in the xml parsing code Ana Rey
2014-06-22 18:32 ` [libnftnl PATCH 2/4] expr: payload: get a nft_rule_expr type in nft_rule_expr_payload_snprintf_* functions Ana Rey
2014-06-22 18:32 ` [libnftnl PATCH 3/4] expr: payload: Do not print unset values in xml file Ana Rey
2014-06-22 18:32 ` Ana Rey [this message]
2014-06-23 9:15 ` [libnftnl PATCH 0/4] expr: payload: Do not print unset values in xml and json 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=1403461941-21605-5-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).