From bf39c53f4c5499e359aa86ccca16c996456bdc9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Neira=20Ayuso?= Date: Fri, 5 Jul 2013 14:41:28 +0200 Subject: set: add json output This patch allows you to dump set and their content in json format. Signed-off-by: Alvaro Neira Ayuso Signed-off-by: Pablo Neira Ayuso --- src/set.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- src/set_elem.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 103 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/set.c b/src/set.c index 69f25ac..4f2e8a5 100644 --- a/src/set.c +++ b/src/set.c @@ -316,8 +316,46 @@ int nft_set_nlmsg_parse(const struct nlmsghdr *nlh, struct nft_set *s) } EXPORT_SYMBOL(nft_set_nlmsg_parse); -int nft_set_snprintf(char *buf, size_t size, struct nft_set *s, - uint32_t type, uint32_t flags) +static int nft_set_snprintf_json(char *buf, size_t size, struct nft_set *s, + uint32_t type, uint32_t flags) +{ + int ret; + int len = size, offset = 0; + struct nft_set_elem *elem; + + ret = snprintf(buf, size, "{ \"set\" : { \"name\" : \"%s\", \"table\" : \"%s\", \"flags\" : %u", + s->name, s->table, s->set_flags); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + + /* Empty set? Skip printinf of elements */ + if (list_empty(&s->element_list)){ + ret = snprintf(buf+offset, size, "}}"); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + return offset; + } + + ret = snprintf(buf+offset, size, ", \"set_elem\" : ["); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + + list_for_each_entry(elem, &s->element_list, head) { + ret = snprintf(buf+offset, size, "{"); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + + ret = nft_set_elem_snprintf(buf+offset, size, elem, type, flags); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + + ret = snprintf(buf+offset, size, "}, "); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + } + + ret = snprintf(buf+offset-2, size, "]}}"); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + + return offset; +} + +static int nft_set_snprintf_default(char *buf, size_t size, struct nft_set *s, + uint32_t type, uint32_t flags) { int ret; int len = size, offset = 0; @@ -344,6 +382,20 @@ int nft_set_snprintf(char *buf, size_t size, struct nft_set *s, return offset; } + +int nft_set_snprintf(char *buf, size_t size, struct nft_set *s, + uint32_t type, uint32_t flags) +{ + switch(type) { + case NFT_SET_O_DEFAULT: + return nft_set_snprintf_default(buf, size, s, type, flags); + case NFT_SET_O_JSON: + return nft_set_snprintf_json(buf, size, s, type, flags); + default: + break; + } + return -1; +} EXPORT_SYMBOL(nft_set_snprintf); void nft_set_elem_add(struct nft_set *s, struct nft_set_elem *elem) diff --git a/src/set_elem.c b/src/set_elem.c index 9691895..c5c3de4 100644 --- a/src/set_elem.c +++ b/src/set_elem.c @@ -384,8 +384,41 @@ int nft_set_elems_nlmsg_parse(const struct nlmsghdr *nlh, struct nft_set *s) } EXPORT_SYMBOL(nft_set_elems_nlmsg_parse); -int nft_set_elem_snprintf(char *buf, size_t size, struct nft_set_elem *e, - uint32_t type, uint32_t flags) +static int nft_set_elem_snprintf_json(char *buf, size_t size, struct nft_set_elem *e) +{ + int ret, len = size, offset = 0, i, numregs; + + ret = snprintf(buf, size, "\"flags\" : %u", e->set_elem_flags); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + + numregs = div_round_up(e->key.len, sizeof(uint32_t)); + if (numregs != 0) { + ret = snprintf(buf+offset, len, ", \"key\" : \"0x"); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + for (i = 0; i < numregs; i++) { + ret = snprintf(buf+offset, len, "%.8x", e->key.val[i]); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + } + ret = snprintf(buf+offset, len, "\""); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + } + + numregs = div_round_up(e->data.len, sizeof(uint32_t)); + if (numregs != 0) { + ret = snprintf(buf+offset, size, " ,\"data\" : \"0x"); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + for (i = 0; i < numregs; i++) { + ret = snprintf(buf+offset, len, "%.8x", e->data.val[i]); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + } + ret = snprintf(buf+offset, len, "\""); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + } + + return offset; +} + +static int nft_set_elem_snprintf_default(char *buf, size_t size, struct nft_set_elem *e) { int ret, len = size, offset = 0, i; @@ -410,6 +443,20 @@ int nft_set_elem_snprintf(char *buf, size_t size, struct nft_set_elem *e, return offset; } + +int nft_set_elem_snprintf(char *buf, size_t size, struct nft_set_elem *e, + uint32_t type, uint32_t flags) +{ + switch(type) { + case NFT_SET_O_DEFAULT: + return nft_set_elem_snprintf_default(buf, size, e); + case NFT_SET_O_JSON: + return nft_set_elem_snprintf_json(buf, size, e); + default: + break; + } + return -1; +} EXPORT_SYMBOL(nft_set_elem_snprintf); int nft_set_elem_foreach(struct nft_set *s, -- cgit v1.2.3