From: Phil Oester Date: Thu, 20 Jul 2006 17:00:19 +0000 (+0000) Subject: reduce service_to_port duplication (Phil Oester ) X-Git-Tag: v1.3.6~12 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=58179b1d0d1722ea16028aa2ea9d74afc86dd5dc;p=thirdparty%2Fiptables.git reduce service_to_port duplication (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. --- diff --git a/extensions/libip6t_multiport.c b/extensions/libip6t_multiport.c index a8a8e08a..d33db9f2 100644 --- a/extensions/libip6t_multiport.c +++ b/extensions/libip6t_multiport.c @@ -50,17 +50,6 @@ proto_to_name(u_int8_t proto) } } -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) { diff --git a/extensions/libip6t_tcp.c b/extensions/libip6t_tcp.c index a0491280..d89a3462 100644 --- a/extensions/libip6t_tcp.c +++ b/extensions/libip6t_tcp.c @@ -38,24 +38,13 @@ static struct option opts[] = { {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, diff --git a/extensions/libip6t_udp.c b/extensions/libip6t_udp.c index 842581d9..656a79e2 100644 --- a/extensions/libip6t_udp.c +++ b/extensions/libip6t_udp.c @@ -30,24 +30,13 @@ static struct option opts[] = { {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, diff --git a/extensions/libipt_dccp.c b/extensions/libipt_dccp.c index af1d20e2..603be1de 100644 --- a/extensions/libipt_dccp.c +++ b/extensions/libipt_dccp.c @@ -56,17 +56,6 @@ static struct option opts[] = { { .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 @@ parse_dccp_port(const char *port) 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, diff --git a/extensions/libipt_mport.c b/extensions/libipt_mport.c index a387b214..e6975d03 100644 --- a/extensions/libipt_mport.c +++ b/extensions/libipt_mport.c @@ -33,17 +33,6 @@ static struct option opts[] = { {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) { diff --git a/extensions/libipt_multiport.c b/extensions/libipt_multiport.c index 97e14765..f25632f0 100644 --- a/extensions/libipt_multiport.c +++ b/extensions/libipt_multiport.c @@ -68,17 +68,6 @@ proto_to_name(u_int8_t proto) } } -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) { diff --git a/extensions/libipt_sctp.c b/extensions/libipt_sctp.c index 2d060af9..06c48b4e 100644 --- a/extensions/libipt_sctp.c +++ b/extensions/libipt_sctp.c @@ -79,17 +79,6 @@ static struct option opts[] = { { .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 @@ parse_sctp_port(const char *port) 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, diff --git a/extensions/libipt_tcp.c b/extensions/libipt_tcp.c index f8ed249a..7551a0a6 100644 --- a/extensions/libipt_tcp.c +++ b/extensions/libipt_tcp.c @@ -38,24 +38,13 @@ static struct option opts[] = { {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, diff --git a/extensions/libipt_udp.c b/extensions/libipt_udp.c index f45f364c..a49e8f79 100644 --- a/extensions/libipt_udp.c +++ b/extensions/libipt_udp.c @@ -30,24 +30,13 @@ static struct option opts[] = { {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, diff --git a/include/ip6tables.h b/include/ip6tables.h index 96f9798b..e711982c 100644 --- a/include/ip6tables.h +++ b/include/ip6tables.h @@ -133,6 +133,7 @@ extern int line; 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. */ diff --git a/include/iptables.h b/include/iptables.h index 1cd9dc41..4465e59b 100644 --- a/include/iptables.h +++ b/include/iptables.h @@ -151,6 +151,7 @@ extern int line; 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); diff --git a/ip6tables.c b/ip6tables.c index 82e95950..6dc7aa2d 100644 --- a/ip6tables.c +++ b/ip6tables.c @@ -245,6 +245,17 @@ proto_to_name(u_int8_t proto, int nolookup) 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) { diff --git a/iptables.c b/iptables.c index 78149862..56921db7 100644 --- a/iptables.c +++ b/iptables.c @@ -249,6 +249,17 @@ proto_to_name(u_int8_t proto, int nolookup) 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) {