diff options
Diffstat (limited to 'src/rule.c')
-rw-r--r-- | src/rule.c | 52 |
1 files changed, 50 insertions, 2 deletions
@@ -668,6 +668,52 @@ int nft_rule_parse(struct nft_rule *r, enum nft_rule_parse_type type, char *data } EXPORT_SYMBOL(nft_rule_parse); +static int nft_rule_snprintf_json(char *buf, size_t size, struct nft_rule *r, + uint32_t type, uint32_t flags) +{ + int ret, len = size, offset = 0; + struct nft_rule_expr *expr; + + ret = snprintf(buf, size, + "{ \"rule\": { \"family\" : \"%s\", \"table\" : \"%s\", " + "\"chain\" : \"%s\", \"handle\" : %llu, \"version\" : %d, ", + nft_family2str(r->family), r->table, r->chain, + (unsigned long long)r->handle, + NFT_RULE_JSON_VERSION); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + + ret = snprintf(buf+offset, len, "\"rule_flags\" : %u, ", + r->rule_flags); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + + if(NFT_RULE_ATTR_COMPAT_PROTO != 0 || NFT_RULE_ATTR_COMPAT_FLAGS != 0){ + ret = snprintf(buf+offset,len,"\"compat_flags\" : %u, " + "\"compat_proto\" : %u, ", + r->compat.flags, r->compat.proto); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + } + + ret = snprintf(buf+offset, len, "\"expr\" : ["); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + + list_for_each_entry(expr, &r->expr_list, head) { + ret = snprintf(buf+offset, len, + " { \"type\" : \"%s\", ", expr->ops->name); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + + ret = expr->ops->snprintf(buf+offset, len, type, flags, expr); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + + ret = snprintf(buf+offset, len, "},"); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + + } + ret = snprintf(buf+offset-1, len, "]}}"); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + + return offset; +} + static int nft_rule_snprintf_xml(char *buf, size_t size, struct nft_rule *r, uint32_t type, uint32_t flags) { @@ -739,10 +785,12 @@ int nft_rule_snprintf(char *buf, size_t size, struct nft_rule *r, uint32_t type, uint32_t flags) { switch(type) { - case NFT_RULE_O_XML: - return nft_rule_snprintf_xml(buf, size, r, type, flags); case NFT_RULE_O_DEFAULT: return nft_rule_snprintf_default(buf, size, r, type, flags); + case NFT_RULE_O_XML: + return nft_rule_snprintf_xml(buf, size, r, type, flags); + case NFT_RULE_O_JSON: + return nft_rule_snprintf_json(buf, size, r, type, flags); default: break; } |