summaryrefslogtreecommitdiffstats
path: root/src/set.c
diff options
context:
space:
mode:
authorÁlvaro Neira Ayuso <alvaroneay@gmail.com>2013-07-05 14:41:28 +0200
committerPablo Neira Ayuso <pablo@netfilter.org>2013-07-06 00:00:45 +0200
commitbf39c53f4c5499e359aa86ccca16c996456bdc9e (patch)
treedca0500e0117491dfd40957a8bd6cb5992deda78 /src/set.c
parent485d38ca136e2411cd25b542f63c17445a8deeea (diff)
set: add json output
This patch allows you to dump set and their content in json format. Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'src/set.c')
-rw-r--r--src/set.c56
1 files changed, 54 insertions, 2 deletions
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)