From f95e8598af7e3a1641166b4e6be31b87d4690326 Mon Sep 17 00:00:00 2001 From: Giuseppe Longo Date: Fri, 5 Jul 2013 10:06:28 +0200 Subject: src: improve default text output This patch improves default plain text output by mimicing the default output of libnl-nft. While at it, several %lu has been translated to use %"PRIu64" for correctness. [ I have added the policy to string translation --pablo ] Signed-off-by: Giuseppe Longo Signed-off-by: Pablo Neira Ayuso --- src/chain.c | 47 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 11 deletions(-) (limited to 'src/chain.c') diff --git a/src/chain.c b/src/chain.c index 68744bc..bdbaf60 100644 --- a/src/chain.c +++ b/src/chain.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -721,9 +722,9 @@ static int nft_chain_snprintf_json(char *buf, size_t size, struct nft_chain *c) return snprintf(buf, size, "{ \"chain\": {" "\"name\": \"%s\"," - "\"handle\": %lu," - "\"bytes\": %lu," - "\"packets\": %lu," + "\"handle\": %"PRIu64"," + "\"bytes\": %"PRIu64"," + "\"packets\": %"PRIu64"," "\"version\": %d," "\"properties\": {" "\"type\" : \"%s\"," @@ -747,8 +748,8 @@ static int nft_chain_snprintf_xml(char *buf, size_t size, struct nft_chain *c) int ret, len = size, offset = 0; ret = snprintf(buf, size, - "" + "" "" "%s" "%s
" @@ -779,15 +780,39 @@ static int nft_chain_snprintf_xml(char *buf, size_t size, struct nft_chain *c) return offset; } +static const char *policy2str(int policy) +{ + switch (policy) { + case NF_ACCEPT: + return "accept"; + case NF_DROP: + return "drop"; + default: + break; + } + return "unknown"; +} + static int nft_chain_snprintf_default(char *buf, size_t size, struct nft_chain *c) { - return snprintf(buf, size, "family=%s table=%s chain=%s type=%s " - "hook=%u prio=%d policy=%d use=%d " - "packets=%lu bytes=%lu", - nft_family2str(c->family), c->table, c->name, c->type, - c->hooknum, c->prio, c->policy, c->use, c->packets, - c->bytes); + int ret, len = size, offset = 0; + + ret = snprintf(buf, size, "%s %s %s", + nft_family2str(c->family), c->table, c->name); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + + if (c->flags & (1 << NFT_CHAIN_ATTR_HOOKNUM)) { + ret = snprintf(buf+offset, size, + " type %s hook %s prio %d policy %s use %d " + "packets %"PRIu64" bytes %"PRIu64"", + c->type, hooknum2str_array[c->hooknum], c->prio, + policy2str(c->policy), c->use, + c->packets, c->bytes); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + } + + return offset; } int nft_chain_snprintf(char *buf, size_t size, struct nft_chain *c, -- cgit v1.2.3