From acf8d66df40e29dea1f8fcd652248556a85459f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Neira=20Ayuso?= Date: Thu, 13 Mar 2014 22:31:51 +0100 Subject: rule: don't print unset attributes We print some attribute that maybe the user hasn't defined for printing. We can't assume that the user want to print some attribute that we have put mandatory in the rules. Example: If we have defined family, the output is like that: {"rule":{"family":"ip","handle":4... ip4... And this if we unset the family. {"rule":{"handle":4... 4... Signed-off-by: Alvaro Neira Ayuso Signed-off-by: Pablo Neira Ayuso --- src/rule.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 79 insertions(+), 13 deletions(-) (limited to 'src/rule.c') diff --git a/src/rule.c b/src/rule.c index 162a0a1..3aaee71 100644 --- a/src/rule.c +++ b/src/rule.c @@ -775,12 +775,32 @@ static int nft_rule_snprintf_json(char *buf, size_t size, struct nft_rule *r, int ret, len = size, offset = 0; struct nft_rule_expr *expr; - ret = snprintf(buf, len, "{\"rule\":{\"family\":\"%s\",\"table\":\"%s\"," - "\"chain\":\"%s\",\"handle\":%llu,", - nft_family2str(r->family), r->table, r->chain, - (unsigned long long)r->handle); + ret = snprintf(buf, len, "{\"rule\":{"); SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + if (r->flags & (1 << NFT_RULE_ATTR_FAMILY)) { + ret = snprintf(buf+offset, len, "\"family\":\"%s\",", + nft_family2str(r->family)); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + } + + if (r->flags & (1 << NFT_RULE_ATTR_TABLE)) { + ret = snprintf(buf+offset, len, "\"table\":\"%s\",", + r->table); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + } + + if (r->flags & (1 << NFT_RULE_ATTR_CHAIN)) { + ret = snprintf(buf+offset, len, "\"chain\":\"%s\",", + r->chain); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + } + if (r->flags & (1 << NFT_RULE_ATTR_HANDLE)) { + ret = snprintf(buf+offset, len, "\"handle\":%llu,", + (unsigned long long)r->handle); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + } + if (r->flags & (1 << NFT_RULE_ATTR_COMPAT_PROTO) || r->flags & (1 << NFT_RULE_ATTR_COMPAT_FLAGS)) { ret = snprintf(buf+offset, len, "\"compat_flags\":%u," @@ -824,13 +844,32 @@ static int nft_rule_snprintf_xml(char *buf, size_t size, struct nft_rule *r, int ret, len = size, offset = 0; struct nft_rule_expr *expr; - ret = snprintf(buf, len, "%s" - "%s
%s" - "%llu", - nft_family2str(r->family), r->table, r->chain, - (unsigned long long)r->handle); + ret = snprintf(buf, len, ""); SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + if (r->flags & (1 << NFT_RULE_ATTR_FAMILY)) { + ret = snprintf(buf+offset, len, "%s", + nft_family2str(r->family)); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + } + + if (r->flags & (1 << NFT_RULE_ATTR_TABLE)) { + ret = snprintf(buf+offset, len, "%s
", + r->table); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + } + + if (r->flags & (1 << NFT_RULE_ATTR_CHAIN)) { + ret = snprintf(buf+offset, len, "%s", + r->chain); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + } + if (r->flags & (1 << NFT_RULE_ATTR_HANDLE)) { + ret = snprintf(buf+offset, len, "%llu", + (unsigned long long)r->handle); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + } + if (r->compat.flags != 0 || r->compat.proto != 0) { ret = snprintf(buf+offset, len, "%u" @@ -865,15 +904,42 @@ static int nft_rule_snprintf_xml(char *buf, size_t size, struct nft_rule *r, return offset; } -static int nft_rule_snprintf_default(char *buf, size_t size, struct nft_rule *r, +static int nft_rule_snprintf_default(char *buf, size_t size, struct nft_rule *r, uint32_t type, uint32_t flags) { struct nft_rule_expr *expr; int ret, len = size, offset = 0, i; - ret = snprintf(buf, len, "%s %s %s %"PRIu64" %"PRIu64"\n", - nft_family2str(r->family), r->table, r->chain, - r->handle, r->position); + if (r->flags & (1 << NFT_RULE_ATTR_FAMILY)) { + ret = snprintf(buf+offset, len, "%s ", + nft_family2str(r->family)); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + } + + if (r->flags & (1 << NFT_RULE_ATTR_TABLE)) { + ret = snprintf(buf+offset, len, "%s ", + r->table); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + } + + if (r->flags & (1 << NFT_RULE_ATTR_CHAIN)) { + ret = snprintf(buf+offset, len, "%s ", + r->chain); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + } + if (r->flags & (1 << NFT_RULE_ATTR_HANDLE)) { + ret = snprintf(buf+offset, len, "%llu ", + (unsigned long long)r->handle); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + } + + if (r->flags & (1 << NFT_RULE_ATTR_POSITION)) { + ret = snprintf(buf+offset, len, "%llu ", + (unsigned long long)r->position); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + } + + ret = snprintf(buf+offset, len, "\n"); SNPRINTF_BUFFER_SIZE(ret, size, len, offset); list_for_each_entry(expr, &r->expr_list, head) { -- cgit v1.2.3