Skip to contentSkip to conversation

reduce service_to_port duplication

PATCH Applied Phil Oester 2 participants 2 messages Jul 13, 2006 – Jul 20, 2006 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 reduce service_to_port duplication

The service_to_port function is used in a number of places, and could benefit from some centralization instead of being duplicated everywhere.

Phil

extensions/libip6t_multiport.c +0 -11
diff -ru ipt-orig/extensions/libip6t_multiport.c ipt-new/extensions/libip6t_multiport.c
@@ -50,17 +50,6 @@
 	}
 }
 
-static int
-service_to_port(const char *name, const char *proto)
-{
-	struct servent *service;
-
-	if ((service = getservbyname(name, proto)) != NULL)
-		return ntohs((unsigned short) service->s_port);
-
-		return -1;
-}
-
 static u_int16_t
 parse_port(const char *port, const char *proto)
 {
extensions/libip6t_tcp.c +1 -12
diff -ru ipt-orig/extensions/libip6t_tcp.c ipt-new/extensions/libip6t_tcp.c
@@ -38,24 +38,13 @@
 	{0}
 };
 
-static int
-service_to_port(const char *name)
-{
-	struct servent *service;
-
-	if ((service = getservbyname(name, "tcp")) != NULL)
-		return ntohs((unsigned short) service->s_port);
-
-	return -1;
-}
-
 static u_int16_t
 parse_tcp_port(const char *port)
 {
 	unsigned int portnum;
 
 	if (string_to_number(port, 0, 65535, &portnum) != -1 ||
-	    (portnum = service_to_port(port)) != -1)
+	    (portnum = service_to_port(port, "tcp")) != -1)
 		return (u_int16_t)portnum;
 
 	exit_error(PARAMETER_PROBLEM,
extensions/libip6t_udp.c +1 -12
diff -ru ipt-orig/extensions/libip6t_udp.c ipt-new/extensions/libip6t_udp.c
@@ -30,24 +30,13 @@
 	{0}
 };
 
-static int
-service_to_port(const char *name)
-{
-	struct servent *service;
-
-	if ((service = getservbyname(name, "udp")) != NULL)
-		return ntohs((unsigned short) service->s_port);
-
-		return -1;
-}
-
 static u_int16_t
 parse_udp_port(const char *port)
 {
 	unsigned int portnum;
 
 	if (string_to_number(port, 0, 65535, &portnum) != -1 ||
-	    (portnum = service_to_port(port)) != -1)
+	    (portnum = service_to_port(port, "udp")) != -1)
 		return (u_int16_t)portnum;
 
 		exit_error(PARAMETER_PROBLEM,
extensions/libipt_dccp.c +1 -12
diff -ru ipt-orig/extensions/libipt_dccp.c ipt-new/extensions/libipt_dccp.c
@@ -56,17 +56,6 @@
 	{ .name = 0 }
 };
 
-static int
-service_to_port(const char *name)
-{
-	struct servent *service;
-
-	if ((service = getservbyname(name, "dccp")) != NULL)
-		return ntohs((unsigned short) service->s_port);
-
-	return -1;
-}
-
 static u_int16_t
 parse_dccp_port(const char *port)
 {
@@ -74,7 +63,7 @@
 
 	DEBUGP("%s\n", port);
 	if (string_to_number(port, 0, 65535, &portnum) != -1 ||
-	    (portnum = service_to_port(port)) != -1)
+	    (portnum = service_to_port(port, "dccp")) != -1)
 		return (u_int16_t)portnum;
 
 	exit_error(PARAMETER_PROBLEM,
extensions/libipt_mport.c +0 -11
diff -ru ipt-orig/extensions/libipt_mport.c ipt-new/extensions/libipt_mport.c
@@ -33,17 +33,6 @@
 	{0}
 };
 
-static int
-service_to_port(const char *name, const char *proto)
-{
-	struct servent *service;
-
-	if ((service = getservbyname(name, proto)) != NULL)
-		return ntohs((unsigned short) service->s_port);
-
-		return -1;
-}
-
 static u_int16_t
 parse_port(const char *port, const char *proto)
 {
extensions/libipt_multiport.c +0 -11
diff -ru ipt-orig/extensions/libipt_multiport.c ipt-new/extensions/libipt_multiport.c
@@ -68,17 +68,6 @@
 	}
 }
 
-static int
-service_to_port(const char *name, const char *proto)
-{
-	struct servent *service;
-
-	if ((service = getservbyname(name, proto)) != NULL)
-		return ntohs((unsigned short) service->s_port);
-
-		return -1;
-}
-
 static u_int16_t
 parse_port(const char *port, const char *proto)
 {
extensions/libipt_sctp.c +1 -12
diff -ru ipt-orig/extensions/libipt_sctp.c ipt-new/extensions/libipt_sctp.c
@@ -79,17 +79,6 @@
 	{ .name = 0 }
 };
 
-static int
-service_to_port(const char *name)
-{
-	struct servent *service;
-
-	if ((service = getservbyname(name, "sctp")) != NULL)
-		return ntohs((unsigned short) service->s_port);
-
-	return -1;
-}
-
 static u_int16_t
 parse_sctp_port(const char *port)
 {
@@ -97,7 +86,7 @@
 
 	DEBUGP("%s\n", port);
 	if (string_to_number(port, 0, 65535, &portnum) != -1 ||
-	    (portnum = service_to_port(port)) != -1)
+	    (portnum = service_to_port(port, "sctp")) != -1)
 		return (u_int16_t)portnum;
 
 	exit_error(PARAMETER_PROBLEM,
extensions/libipt_tcp.c +1 -12
diff -ru ipt-orig/extensions/libipt_tcp.c ipt-new/extensions/libipt_tcp.c
@@ -38,24 +38,13 @@
 	{0}
 };
 
-static int
-service_to_port(const char *name)
-{
-	struct servent *service;
-
-	if ((service = getservbyname(name, "tcp")) != NULL)
-		return ntohs((unsigned short) service->s_port);
-
-	return -1;
-}
-
 static u_int16_t
 parse_tcp_port(const char *port)
 {
 	unsigned int portnum;
 
 	if (string_to_number(port, 0, 65535, &portnum) != -1 ||
-	    (portnum = service_to_port(port)) != -1)
+	    (portnum = service_to_port(port, "tcp")) != -1)
 		return (u_int16_t)portnum;
 
 	exit_error(PARAMETER_PROBLEM,
extensions/libipt_udp.c +1 -12
diff -ru ipt-orig/extensions/libipt_udp.c ipt-new/extensions/libipt_udp.c
@@ -30,24 +30,13 @@
 	{0}
 };
 
-static int
-service_to_port(const char *name)
-{
-	struct servent *service;
-
-	if ((service = getservbyname(name, "udp")) != NULL)
-		return ntohs((unsigned short) service->s_port);
-
-		return -1;
-}
-
 static u_int16_t
 parse_udp_port(const char *port)
 {
 	unsigned int portnum;
 
 	if (string_to_number(port, 0, 65535, &portnum) != -1 ||
-	    (portnum = service_to_port(port)) != -1)
+	    (portnum = service_to_port(port, "udp")) != -1)
 		return (u_int16_t)portnum;
 
 		exit_error(PARAMETER_PROBLEM,
include/ip6tables.h +1 -0
diff -ru ipt-orig/include/ip6tables.h ipt-new/include/ip6tables.h
@@ -133,6 +133,7 @@
 extern void register_match6(struct ip6tables_match *me);
 extern void register_target6(struct ip6tables_target *me);
 
+extern int service_to_port(const char *name, const char *proto);
 extern int do_command6(int argc, char *argv[], char **table,
 		       ip6tc_handle_t *handle);
 /* Keeping track of external matches and targets: linked lists. */
include/iptables.h +1 -0
diff -ru ipt-orig/include/iptables.h ipt-new/include/iptables.h
@@ -151,6 +151,7 @@
 extern void register_match(struct iptables_match *me);
 extern void register_target(struct iptables_target *me);
 
+extern int service_to_port(const char *name, const char *proto);
 extern struct in_addr *dotted_to_addr(const char *dotted);
 extern char *addr_to_dotted(const struct in_addr *addrp);
 extern char *addr_to_anyname(const struct in_addr *addr);
ip6tables.c +11 -0
diff -ru ipt-orig/ip6tables.c ipt-new/ip6tables.c
@@ -245,6 +245,17 @@
 	return NULL;
 }
 
+int
+service_to_port(const char *name, const char *proto)
+{
+	struct servent *service;
+
+	if ((service = getservbyname(name, proto)) != NULL)
+		return ntohs((unsigned short) service->s_port);
+
+	return -1;
+}
+
 static void
 in6addrcpy(struct in6_addr *dst, struct in6_addr *src)
 {
iptables.c +11 -0
diff -ru ipt-orig/iptables.c ipt-new/iptables.c
@@ -249,6 +249,17 @@
 	return NULL;
 }
 
+int
+service_to_port(const char *name, const char *proto)
+{
+	struct servent *service;
+
+	if ((service = getservbyname(name, proto)) != NULL)
+		return ntohs((unsigned short) service->s_port);
+
+	return -1;
+}
+
 struct in_addr *
 dotted_to_addr(const char *dotted)
 {
#1 lore raw
Quoted 2 lines · Phil Oester

The service_to_port function is used in a number of places, and could benefit from some centralization instead of being duplicated everywhere.

Applied, thanks Phil.