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 --- examples/nft-set-get.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'examples/nft-set-get.c') diff --git a/examples/nft-set-get.c b/examples/nft-set-get.c index d4588ba..5ef654c 100644 --- a/examples/nft-set-get.c +++ b/examples/nft-set-get.c @@ -23,6 +23,7 @@ static int set_cb(const struct nlmsghdr *nlh, void *data) { struct nft_set *t; char buf[4096]; + uint32_t *type = data; t = nft_set_alloc(); if (t == NULL) { @@ -35,7 +36,7 @@ static int set_cb(const struct nlmsghdr *nlh, void *data) goto err_free; } - nft_set_snprintf(buf, sizeof(buf), t, 0, 0); + nft_set_snprintf(buf, sizeof(buf), t, *type, 0); printf("%s\n", buf); err_free: @@ -50,11 +51,12 @@ int main(int argc, char *argv[]) char buf[MNL_SOCKET_BUFFER_SIZE]; struct nlmsghdr *nlh; uint32_t portid, seq, family; + uint32_t type = NFT_SET_O_DEFAULT; struct nft_set *t = NULL; int ret; - if (argc != 3) { - fprintf(stderr, "%s \n", argv[0]); + if (argc < 3 || argc > 4) { + fprintf(stderr, "%s
[default|json]\n", argv[0]); return EXIT_FAILURE; } t = nft_set_alloc(); @@ -67,13 +69,16 @@ int main(int argc, char *argv[]) family = AF_INET; else if (strcmp(argv[1], "ip6") == 0) family = AF_INET6; - else if (strcmp(argv[2], "bridge") == 0) + else if (strcmp(argv[1], "bridge") == 0) family = AF_BRIDGE; else { fprintf(stderr, "Unknown family: ip, ip6, bridge\n"); exit(EXIT_FAILURE); } + if (argc == 4 && strcmp(argv[3], "json") == 0) + type = NFT_SET_O_JSON; + nlh = nft_set_nlmsg_build_hdr(buf, NFT_MSG_GETSET, family, NLM_F_DUMP|NLM_F_ACK, seq); nft_set_attr_set(t, NFT_SET_ATTR_TABLE, argv[2]); @@ -99,7 +104,7 @@ int main(int argc, char *argv[]) ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); while (ret > 0) { - ret = mnl_cb_run(buf, ret, seq, portid, set_cb, NULL); + ret = mnl_cb_run(buf, ret, seq, portid, set_cb, &type); if (ret <= 0) break; ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); -- cgit v1.2.3