netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [libnftnl PATCH] expr: nat: add helper function to translate nat types to string
@ 2014-06-20  8:55 Arturo Borrero Gonzalez
  2014-06-23 18:07 ` Pablo Neira Ayuso
  0 siblings, 1 reply; 2+ messages in thread
From: Arturo Borrero Gonzalez @ 2014-06-20  8:55 UTC (permalink / raw)
  To: netfilter-devel; +Cc: anarey, pablo

This patch adds a helper functon to translate nat types to string.

Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
---
 src/expr/nat.c |   40 +++++++++++++++++-----------------------
 1 file changed, 17 insertions(+), 23 deletions(-)

diff --git a/src/expr/nat.c b/src/expr/nat.c
index 0136fca..c719b6c 100644
--- a/src/expr/nat.c
+++ b/src/expr/nat.c
@@ -184,6 +184,18 @@ nft_rule_expr_nat_build(struct nlmsghdr *nlh, struct nft_rule_expr *e)
 				 htonl(nat->sreg_proto_max));
 }
 
+static inline const char *nft_nat2str(uint16_t nat)
+{
+	switch (nat) {
+	case NFT_NAT_SNAT:
+		return "snat";
+	case NFT_NAT_DNAT:
+		return "dnat";
+	default:
+		return "unknown";
+	}
+}
+
 static inline int nft_str2nat(const char *nat)
 {
 	if (strcmp(nat, "snat") == 0)
@@ -304,11 +316,8 @@ nft_rule_expr_nat_snprintf_json(char *buf, size_t size,
 	struct nft_expr_nat *nat = nft_expr_data(e);
 	int len = size, offset = 0, ret = 0;
 
-	if (nat->type == NFT_NAT_SNAT)
-		ret = snprintf(buf, len, "\"nat_type\":\"snat\",");
-	else if (nat->type == NFT_NAT_DNAT)
-		ret = snprintf(buf, len, "\"nat_type\":\"dnat\",");
-
+	ret = snprintf(buf, len, "\"nat_type\":\"%s\",",
+		       nft_nat2str(nat->type));
 	SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
 
 	ret = snprintf(buf+offset, len, "\"family\":\"%s\",",
@@ -340,14 +349,7 @@ nft_rule_expr_nat_snprintf_xml(char *buf, size_t size,
 	struct nft_expr_nat *nat = nft_expr_data(e);
 	int len = size, offset = 0, ret = 0;
 
-	/* Is a mandatory element. Provide a default, even empty */
-	if (nat->type == NFT_NAT_SNAT)
-		ret = snprintf(buf, len, "<type>snat</type>");
-	else if (nat->type == NFT_NAT_DNAT)
-		ret = snprintf(buf, len, "<type>dnat</type>");
-	else
-		ret = snprintf(buf, len, "<type>unknown</type>");
-
+	ret = snprintf(buf, len, "<type>%s</type>", nft_nat2str(nat->type));
 	SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
 
 	ret = snprintf(buf+offset, len, "<family>%s</family>",
@@ -380,16 +382,8 @@ nft_rule_expr_nat_snprintf_default(char *buf, size_t size,
 	struct nft_expr_nat *nat = nft_expr_data(e);
 	int len = size, offset = 0, ret = 0;
 
-	switch (nat->type) {
-	case NFT_NAT_SNAT:
-		ret = snprintf(buf, len, "snat ");
-		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
-		break;
-	case NFT_NAT_DNAT:
-		ret = snprintf(buf, len, "dnat ");
-		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
-		break;
-	}
+	ret = snprintf(buf, len, "%s ", nft_nat2str(nat->type));
+	SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
 
 	ret = snprintf(buf+offset, len, "%s ", nft_family2str(nat->family));
 	SNPRINTF_BUFFER_SIZE(ret, size, len, offset);


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [libnftnl PATCH] expr: nat: add helper function to translate nat types to string
  2014-06-20  8:55 [libnftnl PATCH] expr: nat: add helper function to translate nat types to string Arturo Borrero Gonzalez
@ 2014-06-23 18:07 ` Pablo Neira Ayuso
  0 siblings, 0 replies; 2+ messages in thread
From: Pablo Neira Ayuso @ 2014-06-23 18:07 UTC (permalink / raw)
  To: Arturo Borrero Gonzalez; +Cc: netfilter-devel, anarey

On Fri, Jun 20, 2014 at 10:55:08AM +0200, Arturo Borrero Gonzalez wrote:
> This patch adds a helper functon to translate nat types to string.

Applied, thanks.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2014-06-23 18:07 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-06-20  8:55 [libnftnl PATCH] expr: nat: add helper function to translate nat types to string Arturo Borrero Gonzalez
2014-06-23 18:07 ` Pablo Neira Ayuso

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).