netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jan Engelhardt <jengelh@medozas.de>
To: kaber@trash.net
Cc: netfilter-devel@vger.kernel.org
Subject: [PATCH 06/12] libxtables: collapse double protocol parsing
Date: Fri, 13 May 2011 02:47:30 +0200	[thread overview]
Message-ID: <1305247657-29158-7-git-send-email-jengelh@medozas.de> (raw)
In-Reply-To: <1305247657-29158-1-git-send-email-jengelh@medozas.de>

Un-dent xtables_parse_protocol, and make xtopt_parse_protocol make use
of it.

Signed-off-by: Jan Engelhardt <jengelh@medozas.de>
---
 xtables.c   |   49 +++++++++++++++++++++----------------------------
 xtoptions.c |   14 +-------------
 2 files changed, 22 insertions(+), 41 deletions(-)

diff --git a/xtables.c b/xtables.c
index 9038f89..f10cdb7 100644
--- a/xtables.c
+++ b/xtables.c
@@ -1797,37 +1797,30 @@ const struct xtables_pprot xtables_chain_protos[] = {
 uint16_t
 xtables_parse_protocol(const char *s)
 {
-	unsigned int proto;
+	const struct protoent *pent;
+	unsigned int proto, i;
 
-	if (!xtables_strtoui(s, NULL, &proto, 0, UINT8_MAX)) {
-		struct protoent *pent;
+	if (xtables_strtoui(s, NULL, &proto, 0, UINT8_MAX))
+		return proto;
 
-		/* first deal with the special case of 'all' to prevent
-		 * people from being able to redefine 'all' in nsswitch
-		 * and/or provoke expensive [not working] ldap/nis/...
-		 * lookups */
-		if (!strcmp(s, "all"))
-			return 0;
+	/* first deal with the special case of 'all' to prevent
+	 * people from being able to redefine 'all' in nsswitch
+	 * and/or provoke expensive [not working] ldap/nis/...
+	 * lookups */
+	if (strcmp(s, "all") == 0)
+		return 0;
 
-		if ((pent = getprotobyname(s)))
-			proto = pent->p_proto;
-		else {
-			unsigned int i;
-			for (i = 0; i < ARRAY_SIZE(xtables_chain_protos); ++i) {
-				if (xtables_chain_protos[i].name == NULL)
-					continue;
+	pent = getprotobyname(s);
+	if (pent != NULL)
+		return pent->p_proto;
 
-				if (strcmp(s, xtables_chain_protos[i].name) == 0) {
-					proto = xtables_chain_protos[i].num;
-					break;
-				}
-			}
-			if (i == ARRAY_SIZE(xtables_chain_protos))
-				xt_params->exit_err(PARAMETER_PROBLEM,
-					   "unknown protocol `%s' specified",
-					   s);
-		}
+	for (i = 0; i < ARRAY_SIZE(xtables_chain_protos); ++i) {
+		if (xtables_chain_protos[i].name == NULL)
+			continue;
+		if (strcmp(s, xtables_chain_protos[i].name) == 0)
+			return xtables_chain_protos[i].num;
 	}
-
-	return proto;
+	xt_params->exit_err(PARAMETER_PROBLEM,
+		"unknown protocol \"%s\" specified", s);
+	return -1;
 }
diff --git a/xtoptions.c b/xtoptions.c
index 9e19250..eb9e4e6 100644
--- a/xtoptions.c
+++ b/xtoptions.c
@@ -498,19 +498,7 @@ static int xtables_getportbyname(const char *name)
  */
 static void xtopt_parse_protocol(struct xt_option_call *cb)
 {
-	const struct protoent *entry;
-	unsigned int value = -1;
-
-	if (xtables_strtoui(cb->arg, NULL, &value, 0, UINT8_MAX)) {
-		cb->val.protocol = value;
-		return;
-	}
-	entry = getprotobyname(cb->arg);
-	if (entry == NULL)
-		xt_params->exit_err(PARAMETER_PROBLEM,
-			"Protocol \"%s\" does not resolve to anything.\n",
-			cb->arg);
-	cb->val.protocol = entry->p_proto;
+	cb->val.protocol = xtables_parse_protocol(cb->arg);
 	if (cb->entry->flags & XTOPT_PUT)
 		*(uint8_t *)XTOPT_MKPTR(cb) = cb->val.protocol;
 }
-- 
1.7.1


  parent reply	other threads:[~2011-05-13  0:49 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-13  0:47 Guided option parser run 8 Jan Engelhardt
2011-05-13  0:47 ` [PATCH 01/12] src: replace old IP*T_ALIGN macros Jan Engelhardt
2011-05-13  0:47 ` [PATCH 02/12] src: combine default_command functions Jan Engelhardt
2011-05-13  0:47 ` [PATCH 03/12] libxt_policy: option table fixes, improved error tracking Jan Engelhardt
2011-05-13  0:47 ` [PATCH 04/12] libxtables: avoid running into .also checks when option not used Jan Engelhardt
2011-05-13  0:47 ` [PATCH 05/12] libxt_policy: use XTTYPE_PROTOCOL type Jan Engelhardt
2011-05-13  0:47 ` Jan Engelhardt [this message]
2011-05-13  0:47 ` [PATCH 07/12] libipt_[SD]NAT: flag up module name on error Jan Engelhardt
2011-05-13  0:47 ` [PATCH 08/12] libipt_[SD]NAT: avoid false error about multiple destinations specified Jan Engelhardt
2011-05-13  0:47 ` [PATCH 09/12] libxt_conntrack: correct printed module name Jan Engelhardt
2011-05-13  0:47 ` [PATCH 10/12] libxt_conntrack: fix assignment to wrong member Jan Engelhardt
2011-05-13  0:47 ` [PATCH 11/12] libxt_conntrack: resolve erroneous rev-2 port range message Jan Engelhardt
2011-05-13  0:47 ` [PATCH 12/12] libip6t_rt: rt-0-not-strict should take no arg Jan Engelhardt
2011-05-23 13:23 ` Guided option parser run 8 Patrick McHardy

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=1305247657-29158-7-git-send-email-jengelh@medozas.de \
    --to=jengelh@medozas.de \
    --cc=kaber@trash.net \
    --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).