From 158392c6c719f156b96f5871d922f6f272012726 Mon Sep 17 00:00:00 2001 From: Arturo Borrero Gonzalez Date: Mon, 11 Feb 2013 12:56:38 +0000 Subject: examples: add XML output for table/chain/rule To show an instance of this patch: (shell)$ ./nft-table-get xml
Signed-off-by: Arturo Borrero Gonzalez --- examples/nft-chain-get.c | 16 ++++++++++------ examples/nft-rule-get.c | 10 +++++++--- examples/nft-table-get.c | 14 ++++++++++++-- 3 files changed, 29 insertions(+), 11 deletions(-) (limited to 'examples') diff --git a/examples/nft-chain-get.c b/examples/nft-chain-get.c index 2756c90..b32fc0c 100644 --- a/examples/nft-chain-get.c +++ b/examples/nft-chain-get.c @@ -23,6 +23,7 @@ static int table_cb(const struct nlmsghdr *nlh, void *data) { struct nft_chain *t; char buf[4096]; + uint32_t *type = data; t = nft_chain_alloc(); if (t == NULL) { @@ -35,7 +36,7 @@ static int table_cb(const struct nlmsghdr *nlh, void *data) goto err_free; } - nft_chain_snprintf(buf, sizeof(buf), t, NFT_CHAIN_O_DEFAULT, 0); + nft_chain_snprintf(buf, sizeof(buf), t, *type, 0); printf("%s", buf); err_free: @@ -49,16 +50,16 @@ int main(int argc, char *argv[]) struct mnl_socket *nl; char buf[MNL_SOCKET_BUFFER_SIZE]; struct nlmsghdr *nlh; - uint32_t portid, seq; + uint32_t portid, seq, type = NFT_CHAIN_O_DEFAULT; struct nft_chain *t = NULL; int ret; seq = time(NULL); - if (argc == 1) { + if (argc >= 1 && argc <= 2) { nlh = nft_chain_nlmsg_build_hdr(buf, NFT_MSG_GETCHAIN, AF_INET, NLM_F_DUMP, seq); - } else if (argc == 4) { + } else if (argc >= 4 && argc <= 5) { int family; if (strcmp(argv[1], "ip") == 0) @@ -84,11 +85,14 @@ int main(int argc, char *argv[]) nft_chain_nlmsg_build_payload(nlh, t); nft_chain_free(t); } else { - fprintf(stderr, "Usage: %s [ ]\n", + fprintf(stderr, "Usage: %s [
] [xml]\n", argv[0]); exit(EXIT_FAILURE); } + if (strcmp(argv[argc-1], "xml") == 0) + type = NFT_CHAIN_O_XML; + nl = mnl_socket_open(NETLINK_NETFILTER); if (nl == NULL) { perror("mnl_socket_open"); @@ -108,7 +112,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, table_cb, NULL); + ret = mnl_cb_run(buf, ret, seq, portid, table_cb, &type); if (ret <= 0) break; ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); diff --git a/examples/nft-rule-get.c b/examples/nft-rule-get.c index 75043d7..2821afe 100644 --- a/examples/nft-rule-get.c +++ b/examples/nft-rule-get.c @@ -23,6 +23,7 @@ static int table_cb(const struct nlmsghdr *nlh, void *data) { struct nft_rule *t; char buf[4096]; + uint32_t *type = data; t = nft_rule_alloc(); if (t == NULL) { @@ -35,7 +36,7 @@ static int table_cb(const struct nlmsghdr *nlh, void *data) goto err_free; } - nft_rule_snprintf(buf, sizeof(buf), t, NFT_RULE_O_DEFAULT, 0); + nft_rule_snprintf(buf, sizeof(buf), t, *type, 0); printf("%s", buf); err_free: @@ -49,10 +50,13 @@ int main(int argc, char *argv[]) struct mnl_socket *nl; char buf[MNL_SOCKET_BUFFER_SIZE]; struct nlmsghdr *nlh; - uint32_t portid, seq; + uint32_t portid, seq, type = NFT_RULE_O_DEFAULT; struct nft_rule *t = NULL; int ret; + if (argc == 2 && strcmp(argv[1], "xml") == 0 ) + type = NFT_RULE_O_XML; + /* XXX requires table, chain and handle attributes for selective get */ t = nft_rule_alloc(); @@ -84,7 +88,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, table_cb, NULL); + ret = mnl_cb_run(buf, ret, seq, portid, table_cb, &type); if (ret <= 0) break; ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); diff --git a/examples/nft-table-get.c b/examples/nft-table-get.c index 219c188..518e176 100644 --- a/examples/nft-table-get.c +++ b/examples/nft-table-get.c @@ -23,6 +23,7 @@ static int table_cb(const struct nlmsghdr *nlh, void *data) { struct nft_table *t; char buf[4096]; + uint32_t *type = data; t = nft_table_alloc(); if (t == NULL) { @@ -35,7 +36,7 @@ static int table_cb(const struct nlmsghdr *nlh, void *data) goto err_free; } - nft_table_snprintf(buf, sizeof(buf), t, NFT_TABLE_O_DEFAULT, 0); + nft_table_snprintf(buf, sizeof(buf), t, *type, 0); printf("%s", buf); err_free: @@ -52,6 +53,15 @@ int main(int argc, char *argv[]) uint32_t portid, seq; struct nft_table *t = NULL; int ret; + uint32_t type = NFT_TABLE_O_DEFAULT; + + if (strcmp(argv[argc-1], "xml") == 0) { + type = NFT_TABLE_O_XML; + argv[argc-1] = NULL; + argc--; + } else if (strcmp(argv[argc - 1], "default") == 0) { + argc--; + } if (argc == 2) { t = nft_table_alloc(); @@ -92,7 +102,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, table_cb, NULL); + ret = mnl_cb_run(buf, ret, seq, portid, table_cb, &type); if (ret <= 0) break; ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); -- cgit v1.2.3