netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alvaro Neira <alvaroneay@gmail.com>
To: netfilter-devel@vger.kernel.org
Cc: eric@regit.org
Subject: [libnftables PATCH 1/2] src: expr: use the function base2str in payload.
Date: Sat, 10 Aug 2013 21:40:39 +0200	[thread overview]
Message-ID: <20130810194039.18629.89549.stgit@Ph0enix> (raw)

From: Álvaro Neira Ayuso <alvaroneay@gmail.com>

Use base2str to refactor code in the function snprintf in payload.

Signed-off-by: Alvaro Neira Ayuso <alvaroneay@gmail.com>
---
 src/expr/payload.c |   80 ++++++++++++++++------------------------------------
 1 file changed, 24 insertions(+), 56 deletions(-)

diff --git a/src/expr/payload.c b/src/expr/payload.c
index d00988a..340a0d8 100644
--- a/src/expr/payload.c
+++ b/src/expr/payload.c
@@ -149,31 +149,31 @@ nft_rule_expr_payload_parse(struct nft_rule_expr *e, struct nlattr *attr)
 	return 0;
 }
 
+static char *base2str_array[NFT_PAYLOAD_TRANSPORT_HEADER+1] = {
+	[NFT_PAYLOAD_LL_HEADER]		= "link",
+	[NFT_PAYLOAD_NETWORK_HEADER] 	= "network",
+	[NFT_PAYLOAD_TRANSPORT_HEADER]	= "transport",
+};
+
+static const char *base2str(enum nft_payload_bases base)
+{
+	if (base > NFT_PAYLOAD_TRANSPORT_HEADER)
+		return "unknown";
+
+	return base2str_array[base];
+}
+
 static int
 nft_rule_expr_payload_snprintf_json(char *buf, size_t len, uint32_t flags,
 				   struct nft_expr_payload *p)
 {
 	int size = len, offset = 0, ret;
 
-	ret = snprintf(buf, len, "\"dreg\" : %u, \"offset\" : %u, \"len\" : %u, ",
-					p->dreg, p->offset, p->len);
-	SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
-
-	switch (p->base) {
-	case NFT_PAYLOAD_LL_HEADER:
-		ret = snprintf(buf+offset, len, "\"base\" : \"link\"");
-		break;
-	case NFT_PAYLOAD_NETWORK_HEADER:
-		ret = snprintf(buf+offset, len, "\"base\" : \"network\"");
-		break;
-	case NFT_PAYLOAD_TRANSPORT_HEADER:
-		ret = snprintf(buf+offset, len, "\"base\" : \"transport\"");
-		break;
-	default:
-		ret = snprintf(buf+offset, len, "\"base\" : \"unknown\"");
-		break;
-	}
-
+	ret = snprintf(buf, len, "\"dreg\" : %u, "
+				 "\"offset\" : %u, "
+				 "\"len\" : %u, "
+				 "\"base\" : \"%s\"",
+		       p->dreg, p->offset, p->len, base2str(p->base));
 	SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
 
 	return offset;
@@ -243,48 +243,16 @@ nft_rule_expr_payload_snprintf_xml(char *buf, size_t len, uint32_t flags,
 {
 	int size = len, offset = 0, ret;
 
-	ret = snprintf(buf, len, "<dreg>%u</dreg><offset>%u</offset>"
-		       "<len>%u</len>", p->dreg, p->offset, p->len);
-	SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
-
-	/* A default option is not provided.
-	 * The <base> node will be missing; Is not mandatory.
-	 */
-
-	switch (p->base) {
-	case NFT_PAYLOAD_LL_HEADER:
-		ret = snprintf(buf+offset, len, "<base>link</base>");
-		break;
-	case NFT_PAYLOAD_NETWORK_HEADER:
-		ret = snprintf(buf+offset, len, "<base>network</base>");
-		break;
-	case NFT_PAYLOAD_TRANSPORT_HEADER:
-		ret = snprintf(buf+offset, len, "<base>transport</base>");
-		break;
-	default:
-		ret = snprintf(buf+offset, len, "<base>unknown</base>");
-		break;
-	}
-
+	ret = snprintf(buf, len, "<dreg>%u</dreg>"
+				 "<offset>%u</offset>"
+				 "<len>%u</len>"
+				 "<base>%s</base>",
+		       p->dreg, p->offset, p->len, base2str(p->base));
 	SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
 
 	return offset;
 }
 
-static char *base2str_array[NFT_PAYLOAD_TRANSPORT_HEADER+1] = {
-	[NFT_PAYLOAD_LL_HEADER]		= "link",
-	[NFT_PAYLOAD_NETWORK_HEADER] 	= "network",
-	[NFT_PAYLOAD_TRANSPORT_HEADER]	= "transport",
-};
-
-static const char *base2str(enum nft_payload_bases base)
-{
-	if (base > NFT_PAYLOAD_TRANSPORT_HEADER)
-		return "unknown";
-
-	return base2str_array[base];
-}
-
 static int
 nft_rule_expr_payload_snprintf(char *buf, size_t len, uint32_t type,
 			       uint32_t flags, struct nft_rule_expr *e)

--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

             reply	other threads:[~2013-08-10 19:40 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-08-10 19:40 Alvaro Neira [this message]
2013-08-10 19:40 ` [libnftables PATCH 2/2] test: compare content parsing with original file content Alvaro Neira
2013-08-11  7:47   ` Pablo Neira Ayuso

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20130810194039.18629.89549.stgit@Ph0enix \
    --to=alvaroneay@gmail.com \
    --cc=eric@regit.org \
    --cc=netfilter-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).