* [PATCH nft] json: add stateful object comment support
@ 2022-09-10 22:11 Fernando Fernandez Mancera
2022-09-16 7:39 ` Pablo Neira Ayuso
0 siblings, 1 reply; 2+ messages in thread
From: Fernando Fernandez Mancera @ 2022-09-10 22:11 UTC (permalink / raw)
To: netfilter-devel; +Cc: Fernando Fernandez Mancera
When listing a stateful object with JSON support, the comment was ignored.
Output example:
{
"counter": {
"family": "inet",
"name": "mycounter",
"table": "t",
"handle": 1,
"comment": "my comment in counter",
"packets": 0,
"bytes": 0
}
}
Link: https://bugzilla.netfilter.org/show_bug.cgi?id=1611
Signed-off-by: Fernando Fernandez Mancera <ffmancera@riseup.net>
---
src/json.c | 6 ++++++
src/parser_json.c | 3 +++
| 9 +++++++++
| 6 ++++++
4 files changed, 24 insertions(+)
create mode 100755 tests/shell/testcases/json/0006obj_comment_0
create mode 100644 tests/shell/testcases/json/dumps/0006obj_comment_0.nft
diff --git a/src/json.c b/src/json.c
index 6598863e..6662f808 100644
--- a/src/json.c
+++ b/src/json.c
@@ -329,6 +329,12 @@ static json_t *obj_print_json(const struct obj *obj)
"table", obj->handle.table.name,
"handle", obj->handle.handle.id);
+ if (obj->comment) {
+ tmp = json_pack("{s:s}", "comment", obj->comment);
+ json_object_update(root, tmp);
+ json_decref(tmp);
+ }
+
switch (obj->type) {
case NFT_OBJECT_COUNTER:
tmp = json_pack("{s:I, s:I}",
diff --git a/src/parser_json.c b/src/parser_json.c
index 46dca9fd..b8c30b75 100644
--- a/src/parser_json.c
+++ b/src/parser_json.c
@@ -3339,6 +3339,9 @@ static struct cmd *json_parse_cmd_add_object(struct json_ctx *ctx,
obj = obj_alloc(int_loc);
+ if (!json_unpack(root, "{s:s}", "comment", &obj->comment))
+ obj->comment = xstrdup(obj->comment);
+
switch (cmd_obj) {
case CMD_OBJ_COUNTER:
obj->type = NFT_OBJECT_COUNTER;
--git a/tests/shell/testcases/json/0006obj_comment_0 b/tests/shell/testcases/json/0006obj_comment_0
new file mode 100755
index 00000000..76d8fe16
--- /dev/null
+++ b/tests/shell/testcases/json/0006obj_comment_0
@@ -0,0 +1,9 @@
+#!/bin/bash
+
+set -e
+
+$NFT flush ruleset
+
+RULESET='{"nftables": [{"metainfo": {"version": "1.0.5", "release_name": "Lester Gooch #4", "json_schema_version": 1}}, {"table": {"family": "inet", "name": "t", "handle": 9}}, {"counter": {"family": "inet", "name": "mycounter", "table": "t", "handle": 1, "comment": "my comment in counter", "packets": 0, "bytes": 0}}]}'
+
+$NFT -j -f - <<< $RULESET
--git a/tests/shell/testcases/json/dumps/0006obj_comment_0.nft b/tests/shell/testcases/json/dumps/0006obj_comment_0.nft
new file mode 100644
index 00000000..e52b21b4
--- /dev/null
+++ b/tests/shell/testcases/json/dumps/0006obj_comment_0.nft
@@ -0,0 +1,6 @@
+table inet t {
+ counter mycounter {
+ comment "my comment in counter"
+ packets 0 bytes 0
+ }
+}
--
2.30.2
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH nft] json: add stateful object comment support
2022-09-10 22:11 [PATCH nft] json: add stateful object comment support Fernando Fernandez Mancera
@ 2022-09-16 7:39 ` Pablo Neira Ayuso
0 siblings, 0 replies; 2+ messages in thread
From: Pablo Neira Ayuso @ 2022-09-16 7:39 UTC (permalink / raw)
To: Fernando Fernandez Mancera; +Cc: netfilter-devel
On Sun, Sep 11, 2022 at 12:11:14AM +0200, Fernando Fernandez Mancera wrote:
> When listing a stateful object with JSON support, the comment was ignored.
>
> Output example:
>
> {
> "counter": {
> "family": "inet",
> "name": "mycounter",
> "table": "t",
> "handle": 1,
> "comment": "my comment in counter",
> "packets": 0,
> "bytes": 0
> }
> }
Applied, thanks
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2022-09-16 7:39 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-10 22:11 [PATCH nft] json: add stateful object comment support Fernando Fernandez Mancera
2022-09-16 7:39 ` Pablo Neira Ayuso
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).