[RFC 3/6] net: protocols -- Add lookup_proto helper

Cyrill Gorcunov <[email protected]>
Newsgroups org.kernel.vger.trinity
Message-ID <[email protected]>
We will use it for parsing disabled sockets families as well.

Signed-off-by: Cyrill Gorcunov <[email protected]>
---
 net/protocols.c | 48 ++++++++++++++++++++++++------------------------
 1 file changed, 24 insertions(+), 24 deletions(-)

diff --git a/net/protocols.c b/net/protocols.c
index c4a4ae2..4331351 100644
--- a/net/protocols.c
+++ b/net/protocols.c
@@ -58,6 +58,19 @@ static const struct protocol protocols[] = {
 	{ "PF_VSOCK",        40 },
 };
 
+static const struct protocol *lookup_proto(const char *name, unsigned int proto)
+{
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(protocols); i++) {
+		if ((name && strcmp(name, protocols[i].name) == 0) ||
+		    (proto != -1u && protocols[i].proto == proto))
+			return &protocols[i];
+	}
+
+	return NULL;
+}
+
 const char * get_proto_name(unsigned int proto)
 {
 	unsigned int i;
@@ -70,33 +83,20 @@ const char * get_proto_name(unsigned int proto)
 
 void find_specific_proto(const char *protoarg)
 {
+	const struct protocol *p;
 	unsigned int i;
 
-	if (specific_proto == 0) {
-		/* we were passed a string */
-		for (i = 0; i < ARRAY_SIZE(protocols); i++) {
-			if (strcmp(protoarg, protocols[i].name) == 0) {
-				specific_proto = protocols[i].proto;
-				output(2, "Proto %s = %d\n", protoarg, specific_proto);
-				break;
-			}
-		}
-	} else {
-		/* we were passed a numeric arg. */
-		for (i = 0; i < ARRAY_SIZE(protocols); i++) {
-			if (specific_proto == protocols[i].proto)
-				break;
-		}
+	p = lookup_proto(protoarg, specific_proto ? : -1u);
+	if (p) {
+		specific_proto = p->proto;
+		output(2, "Using protocol %s (%u) for all sockets\n", p->name, p->proto);
+		return;
 	}
 
-	if (i > ARRAY_SIZE(protocols)) {
-		outputerr("Protocol unknown. Pass a numeric value [0-%d] or one of ", TRINITY_PF_MAX);
-		for (i = 0; i < ARRAY_SIZE(protocols); i++)
-			outputerr("%s ", protocols[i].name);
-		outputerr("\n");
-
-		exit(EXIT_FAILURE);
-	}
+	outputerr("Protocol unknown. Pass a numeric value [0-%d] or one of ", TRINITY_PF_MAX);
+	for (i = 0; i < ARRAY_SIZE(protocols); i++)
+		outputerr("%s ", protocols[i].name);
+	outputerr("\n");
 
-	output(2, "Using protocol %s (%u) for all sockets\n", protocols[i].name, protocols[i].proto);
+	exit(EXIT_FAILURE);
 }
-- 
1.8.3.1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.