* [libnftnl PATCH 0/4] expr: counter: Do not print unset values in xml and json
@ 2014-06-24 7:15 Ana Rey
2014-06-24 7:15 ` [libnftnl PATCH 1/4] expr: counter: Add nft_rule_expr_counter_snprinf_* functions Ana Rey
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: Ana Rey @ 2014-06-24 7:15 UTC (permalink / raw)
To: netfilter-devel; +Cc: Ana Rey
This patchset does tasks about does not print unset values in xml and json file
and about refactoring code:
* "expr: counter: Add nft_rule_expr_counter_snprinf_* functions"
* "expr: counter: Use nft_rule_expr_set_* in the xml parsing code"
Ana Rey (4):
expr: counter: Add nft_rule_expr_counter_snprinf_* functions
expr: counter: Use nft_rule_expr_set_* in the xml parsing code
expr: counter: Do not print unset values in xml
expr: counter: Do not print unset values in json
src/expr/counter.c | 92 +++++++++++++++++++++++++++++++++++++-----------------
1 file changed, 64 insertions(+), 28 deletions(-)
--
2.0.0
^ permalink raw reply [flat|nested] 5+ messages in thread
* [libnftnl PATCH 1/4] expr: counter: Add nft_rule_expr_counter_snprinf_* functions
2014-06-24 7:15 [libnftnl PATCH 0/4] expr: counter: Do not print unset values in xml and json Ana Rey
@ 2014-06-24 7:15 ` Ana Rey
2014-06-24 7:15 ` [libnftnl PATCH 2/4] expr: counter: Use nft_rule_expr_set_* in the xml parsing code Ana Rey
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Ana Rey @ 2014-06-24 7:15 UTC (permalink / raw)
To: netfilter-devel; +Cc: Ana Rey
Code refactoring in nft_rule_expr_counter_snprinf functions.
This patch adds three new functions:
* nft_rule_expr_counter_snprinf_default
* nft_rule_expr_counter_snprinf_xml
* nft_rule_expr_counter_snprinf_json
Signed-off-by: Ana Rey <anarey@gmail.com>
---
src/expr/counter.c | 40 +++++++++++++++++++++++++++++++---------
1 file changed, 31 insertions(+), 9 deletions(-)
diff --git a/src/expr/counter.c b/src/expr/counter.c
index 4bb7f1b..65acee3 100644
--- a/src/expr/counter.c
+++ b/src/expr/counter.c
@@ -169,23 +169,45 @@ nft_rule_expr_counter_xml_parse(struct nft_rule_expr *e, mxml_node_t *tree,
return -1;
#endif
}
+static int nft_rule_expr_counter_snprintf_json(char *buf, size_t len,
+ struct nft_rule_expr *e)
+{
+ struct nft_expr_counter *ctr = nft_expr_data(e);
-static int
-nft_rule_expr_counter_snprintf(char *buf, size_t len, uint32_t type,
- uint32_t flags, struct nft_rule_expr *e)
+ return snprintf(buf, len, "\"pkts\":%"PRIu64",\"bytes\":%"PRIu64"",
+ ctr->pkts, ctr->bytes);
+}
+
+static int nft_rule_expr_counter_snprintf_xml(char *buf, size_t len,
+ struct nft_rule_expr *e)
{
struct nft_expr_counter *ctr = nft_expr_data(e);
+ return snprintf(buf, len, "<pkts>%"PRIu64"</pkts>"
+ "<bytes>%"PRIu64"</bytes>",
+ ctr->pkts, ctr->bytes);
+}
+
+static int nft_rule_expr_counter_snprintf_default(char *buf, size_t len,
+ struct nft_rule_expr *e)
+{
+ struct nft_expr_counter *ctr = nft_expr_data(e);
+
+ return snprintf(buf, len, "pkts %"PRIu64" bytes %"PRIu64" ",
+ ctr->pkts, ctr->bytes);
+}
+
+static int nft_rule_expr_counter_snprintf(char *buf, size_t len, uint32_t type,
+ uint32_t flags,
+ struct nft_rule_expr *e)
+{
switch(type) {
case NFT_OUTPUT_DEFAULT:
- return snprintf(buf, len, "pkts %"PRIu64" bytes %"PRIu64" ",
- ctr->pkts, ctr->bytes);
+ return nft_rule_expr_counter_snprintf_default(buf, len, e);
case NFT_OUTPUT_XML:
- return snprintf(buf, len, "<pkts>%"PRIu64"</pkts><bytes>%"PRIu64"</bytes>",
- ctr->pkts, ctr->bytes);
+ return nft_rule_expr_counter_snprintf_xml(buf, len, e);
case NFT_OUTPUT_JSON:
- return snprintf(buf, len, "\"pkts\":%"PRIu64",\"bytes\":%"PRIu64"",
- ctr->pkts, ctr->bytes);
+ return nft_rule_expr_counter_snprintf_json(buf, len, e);
default:
break;
}
--
2.0.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [libnftnl PATCH 2/4] expr: counter: Use nft_rule_expr_set_* in the xml parsing code
2014-06-24 7:15 [libnftnl PATCH 0/4] expr: counter: Do not print unset values in xml and json Ana Rey
2014-06-24 7:15 ` [libnftnl PATCH 1/4] expr: counter: Add nft_rule_expr_counter_snprinf_* functions Ana Rey
@ 2014-06-24 7:15 ` Ana Rey
2014-06-24 7:15 ` [libnftnl PATCH 3/4] expr: counter: Do not print unset values in xml Ana Rey
2014-06-24 7:15 ` [libnftnl PATCH 4/4] expr: counter: Do not print unset values in json Ana Rey
3 siblings, 0 replies; 5+ messages in thread
From: Ana Rey @ 2014-06-24 7:15 UTC (permalink / raw)
To: netfilter-devel; +Cc: Ana Rey
Code refactoring to use nft_rule_expr_set_* in parse functions.
Signed-off-by: Ana Rey <anarey@gmail.com>
---
src/expr/counter.c | 12 +++++-------
1 file changed, 5 insertions(+), 7 deletions(-)
diff --git a/src/expr/counter.c b/src/expr/counter.c
index 65acee3..9d85a96 100644
--- a/src/expr/counter.c
+++ b/src/expr/counter.c
@@ -147,21 +147,19 @@ nft_rule_expr_counter_xml_parse(struct nft_rule_expr *e, mxml_node_t *tree,
struct nft_parse_err *err)
{
#ifdef XML_PARSING
- struct nft_expr_counter *ctr = nft_expr_data(e);
+ uint64_t pkts, bytes;
if (nft_mxml_num_parse(tree, "pkts", MXML_DESCEND_FIRST, BASE_DEC,
- &ctr->pkts, NFT_TYPE_U64, NFT_XML_MAND,
+ &pkts, NFT_TYPE_U64, NFT_XML_MAND,
err) != 0)
return -1;
-
- e->flags |= (1 << NFT_EXPR_CTR_PACKETS);
+ nft_rule_expr_set_u64(e, NFT_EXPR_CTR_PACKETS, pkts);
if (nft_mxml_num_parse(tree, "bytes", MXML_DESCEND_FIRST, BASE_DEC,
- &ctr->bytes, NFT_TYPE_U64, NFT_XML_MAND,
+ &bytes, NFT_TYPE_U64, NFT_XML_MAND,
err) != 0)
return -1;
-
- e->flags |= (1 << NFT_EXPR_CTR_BYTES);
+ nft_rule_expr_set_u64(e, NFT_EXPR_CTR_BYTES, bytes);
return 0;
#else
--
2.0.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [libnftnl PATCH 3/4] expr: counter: Do not print unset values in xml
2014-06-24 7:15 [libnftnl PATCH 0/4] expr: counter: Do not print unset values in xml and json Ana Rey
2014-06-24 7:15 ` [libnftnl PATCH 1/4] expr: counter: Add nft_rule_expr_counter_snprinf_* functions Ana Rey
2014-06-24 7:15 ` [libnftnl PATCH 2/4] expr: counter: Use nft_rule_expr_set_* in the xml parsing code Ana Rey
@ 2014-06-24 7:15 ` Ana Rey
2014-06-24 7:15 ` [libnftnl PATCH 4/4] expr: counter: Do not print unset values in json Ana Rey
3 siblings, 0 replies; 5+ messages in thread
From: Ana Rey @ 2014-06-24 7:15 UTC (permalink / raw)
To: netfilter-devel; +Cc: Ana Rey
It changes the parse and the snprint functions to omit unset values.
Signed-off-by: Ana Rey <anarey@gmail.com>
---
src/expr/counter.c | 27 ++++++++++++++++-----------
1 file changed, 16 insertions(+), 11 deletions(-)
diff --git a/src/expr/counter.c b/src/expr/counter.c
index 9d85a96..6f6a7ee 100644
--- a/src/expr/counter.c
+++ b/src/expr/counter.c
@@ -150,16 +150,12 @@ nft_rule_expr_counter_xml_parse(struct nft_rule_expr *e, mxml_node_t *tree,
uint64_t pkts, bytes;
if (nft_mxml_num_parse(tree, "pkts", MXML_DESCEND_FIRST, BASE_DEC,
- &pkts, NFT_TYPE_U64, NFT_XML_MAND,
- err) != 0)
- return -1;
- nft_rule_expr_set_u64(e, NFT_EXPR_CTR_PACKETS, pkts);
+ &pkts, NFT_TYPE_U64, NFT_XML_MAND, err) == 0)
+ nft_rule_expr_set_u64(e, NFT_EXPR_CTR_PACKETS, pkts);
if (nft_mxml_num_parse(tree, "bytes", MXML_DESCEND_FIRST, BASE_DEC,
- &bytes, NFT_TYPE_U64, NFT_XML_MAND,
- err) != 0)
- return -1;
- nft_rule_expr_set_u64(e, NFT_EXPR_CTR_BYTES, bytes);
+ &bytes, NFT_TYPE_U64, NFT_XML_MAND, err) == 0)
+ nft_rule_expr_set_u64(e, NFT_EXPR_CTR_BYTES, bytes);
return 0;
#else
@@ -179,11 +175,20 @@ static int nft_rule_expr_counter_snprintf_json(char *buf, size_t len,
static int nft_rule_expr_counter_snprintf_xml(char *buf, size_t len,
struct nft_rule_expr *e)
{
+ int ret, size = len, offset = 0;
struct nft_expr_counter *ctr = nft_expr_data(e);
- return snprintf(buf, len, "<pkts>%"PRIu64"</pkts>"
- "<bytes>%"PRIu64"</bytes>",
- ctr->pkts, ctr->bytes);
+ if (e->flags & (1 << NFT_EXPR_CTR_PACKETS)) {
+ ret = snprintf(buf, len, "<pkts>%"PRIu64"</pkts>", ctr->pkts);
+ SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+ }
+ if (e->flags & (1 << NFT_EXPR_CTR_BYTES)) {
+ ret = snprintf(buf + offset, len, "<bytes>%"PRIu64"</bytes>",
+ ctr->bytes);
+ SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+ }
+
+ return offset;
}
static int nft_rule_expr_counter_snprintf_default(char *buf, size_t len,
--
2.0.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [libnftnl PATCH 4/4] expr: counter: Do not print unset values in json
2014-06-24 7:15 [libnftnl PATCH 0/4] expr: counter: Do not print unset values in xml and json Ana Rey
` (2 preceding siblings ...)
2014-06-24 7:15 ` [libnftnl PATCH 3/4] expr: counter: Do not print unset values in xml Ana Rey
@ 2014-06-24 7:15 ` Ana Rey
3 siblings, 0 replies; 5+ messages in thread
From: Ana Rey @ 2014-06-24 7:15 UTC (permalink / raw)
To: netfilter-devel; +Cc: Ana Rey
It changes the parse and the snprint functions to omit unset values.
Signed-off-by: Ana Rey <anarey@gmail.com>
---
src/expr/counter.c | 31 +++++++++++++++++++++----------
1 file changed, 21 insertions(+), 10 deletions(-)
diff --git a/src/expr/counter.c b/src/expr/counter.c
index 6f6a7ee..5ab9a5a 100644
--- a/src/expr/counter.c
+++ b/src/expr/counter.c
@@ -125,15 +125,13 @@ nft_rule_expr_counter_json_parse(struct nft_rule_expr *e, json_t *root,
#ifdef JSON_PARSING
uint64_t uval64;
- if (nft_jansson_parse_val(root, "pkts", NFT_TYPE_U64, &uval64, err) < 0)
- return -1;
-
- nft_rule_expr_set_u64(e, NFT_EXPR_CTR_PACKETS, uval64);
-
- if (nft_jansson_parse_val(root, "bytes", NFT_TYPE_U64, &uval64, err) < 0)
- return -1;
+ if (nft_jansson_parse_val(root, "pkts", NFT_TYPE_U64, &uval64,
+ err) == 0)
+ nft_rule_expr_set_u64(e, NFT_EXPR_CTR_PACKETS, uval64);
- nft_rule_expr_set_u64(e, NFT_EXPR_CTR_BYTES, uval64);
+ if (nft_jansson_parse_val(root, "bytes", NFT_TYPE_U64, &uval64,
+ err) == 0)
+ nft_rule_expr_set_u64(e, NFT_EXPR_CTR_BYTES, uval64);
return 0;
#else
@@ -166,10 +164,23 @@ nft_rule_expr_counter_xml_parse(struct nft_rule_expr *e, mxml_node_t *tree,
static int nft_rule_expr_counter_snprintf_json(char *buf, size_t len,
struct nft_rule_expr *e)
{
+ int ret, size = len, offset = 0;
struct nft_expr_counter *ctr = nft_expr_data(e);
- return snprintf(buf, len, "\"pkts\":%"PRIu64",\"bytes\":%"PRIu64"",
- ctr->pkts, ctr->bytes);
+ if (e->flags & (1 << NFT_EXPR_CTR_PACKETS)) {
+ ret = snprintf(buf, len,"\"pkts\":%"PRIu64",", ctr->pkts);
+ SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+ }
+ if (e->flags & (1 << NFT_EXPR_CTR_BYTES)) {
+ ret = snprintf(buf + offset, len, "\"bytes\":%"PRIu64",", ctr->bytes);
+ SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+ }
+
+ /* Remove the last comma characther */
+ if (offset > 0)
+ offset--;
+
+ return offset;
}
static int nft_rule_expr_counter_snprintf_xml(char *buf, size_t len,
--
2.0.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
end of thread, other threads:[~2014-06-24 7:16 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-06-24 7:15 [libnftnl PATCH 0/4] expr: counter: Do not print unset values in xml and json Ana Rey
2014-06-24 7:15 ` [libnftnl PATCH 1/4] expr: counter: Add nft_rule_expr_counter_snprinf_* functions Ana Rey
2014-06-24 7:15 ` [libnftnl PATCH 2/4] expr: counter: Use nft_rule_expr_set_* in the xml parsing code Ana Rey
2014-06-24 7:15 ` [libnftnl PATCH 3/4] expr: counter: Do not print unset values in xml Ana Rey
2014-06-24 7:15 ` [libnftnl PATCH 4/4] expr: counter: Do not print unset values in json Ana Rey
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).