From 26e06d838d6471f5233c1da3fee012bf113564a5 Mon Sep 17 00:00:00 2001 From: Arturo Borrero Gonzalez Date: Fri, 8 Feb 2013 17:51:56 +0100 Subject: src: add XML output support Signed-off-by: Arturo Borrero Gonzalez Signed-off-by: Pablo Neira Ayuso --- src/expr/nat.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 63 insertions(+), 6 deletions(-) (limited to 'src/expr/nat.c') diff --git a/src/expr/nat.c b/src/expr/nat.c index 17f4459..68217bd 100644 --- a/src/expr/nat.c +++ b/src/expr/nat.c @@ -18,6 +18,7 @@ #include #include #include +#include #include "expr_ops.h" struct nft_expr_nat { @@ -201,8 +202,49 @@ nft_rule_expr_nat_build(struct nlmsghdr *nlh, struct nft_rule_expr *e) } static int -nft_rule_expr_nat_snprintf(char *buf, size_t size, uint32_t type, - uint32_t flags, struct nft_rule_expr *e) +nft_rule_expr_nat_snprintf_xml(char *buf, size_t size, + struct nft_rule_expr *e) +{ + struct nft_expr_nat *nat = (struct nft_expr_nat *)e->data; + int len = size, offset = 0, ret = 0; + + switch (nat->type) { + case NFT_NAT_SNAT: + ret = snprintf(buf, len, + "\t\tNFT_NAT_SNAT "); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + break; + case NFT_NAT_DNAT: + ret = snprintf(buf, len, + "\t\tNFT_NAT_DNAT "); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + break; + } + + ret = snprintf(buf, len, "%s ", + nat->family == AF_INET ? "AF_INET" : "AF_INET6"); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + + if (e->flags & (1 << NFT_EXPR_NAT_REG_ADDR_MIN)) { + ret = snprintf(buf, len, "%u" + " %u ", + nat->sreg_addr_min, nat->sreg_addr_max); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + } + + if (e->flags & (1 << NFT_EXPR_NAT_REG_PROTO_MIN)) { + ret = snprintf(buf, len, "%u" + " %u ", + nat->sreg_proto_min, nat->sreg_proto_max); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); + } + + return offset; +} + +static int +nft_rule_expr_nat_snprintf_default(char *buf, size_t size, + struct nft_rule_expr *e) { struct nft_expr_nat *nat = (struct nft_expr_nat *)e->data; int len = size, offset = 0, ret = 0; @@ -210,12 +252,13 @@ nft_rule_expr_nat_snprintf(char *buf, size_t size, uint32_t type, switch (nat->type) { case NFT_NAT_SNAT: ret = snprintf(buf, len, "type=NFT_NAT_SNAT "); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); break; case NFT_NAT_DNAT: ret = snprintf(buf, len, "type=NFT_NAT_DNAT "); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); break; } - SNPRINTF_BUFFER_SIZE(ret, size, len, offset); ret = snprintf(buf, len, "family=%s ", nat->family == AF_INET ? "AF_INET" : "AF_INET6"); @@ -225,20 +268,34 @@ nft_rule_expr_nat_snprintf(char *buf, size_t size, uint32_t type, ret = snprintf(buf, len, "sreg_addr_min_v4=%u sreg_addr_max_v4=%u ", nat->sreg_addr_min, nat->sreg_addr_max); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); } - SNPRINTF_BUFFER_SIZE(ret, size, len, offset); - if (e->flags & (1 << NFT_EXPR_NAT_REG_PROTO_MIN)) { ret = snprintf(buf, len, "sreg_proto_min=%u sreg_proto_max=%u ", nat->sreg_proto_min, nat->sreg_proto_max); + SNPRINTF_BUFFER_SIZE(ret, size, len, offset); } - SNPRINTF_BUFFER_SIZE(ret, size, len, offset); return offset; } +static int +nft_rule_expr_nat_snprintf(char *buf, size_t size, uint32_t type, + uint32_t flags, struct nft_rule_expr *e) +{ + switch (type) { + case NFT_RULE_O_XML: + return nft_rule_expr_nat_snprintf_xml(buf, size, e); + case NFT_RULE_O_DEFAULT: + return nft_rule_expr_nat_snprintf_default(buf, size, e); + default: + break; + } + return -1; +} + struct expr_ops expr_ops_nat = { .name = "nat", .alloc_len = sizeof(struct nft_expr_nat), -- cgit v1.2.3