From 877fab32b43e91b0a55b655a7b7d6ac0844918fd Mon Sep 17 00:00:00 2001 From: "/C=DE/ST=Berlin/L=Berlin/O=Netfilter Project/OU=Development/CN=kaber/emailAddress=kaber@netfilter.org" Date: Thu, 20 Jul 2006 17:00:19 +0000 Subject: [PATCH] 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. --- extensions/libip6t_multiport.c | 11 ----------- extensions/libip6t_tcp.c | 13 +------------ extensions/libip6t_udp.c | 13 +------------ extensions/libipt_dccp.c | 13 +------------ extensions/libipt_mport.c | 11 ----------- extensions/libipt_multiport.c | 11 ----------- extensions/libipt_sctp.c | 13 +------------ extensions/libipt_tcp.c | 13 +------------ extensions/libipt_udp.c | 13 +------------ include/ip6tables.h | 1 + include/iptables.h | 1 + ip6tables.c | 11 +++++++++++ iptables.c | 11 +++++++++++ 13 files changed, 30 insertions(+), 105 deletions(-) diff --git a/extensions/libip6t_multiport.c b/extensions/libip6t_multiport.c index a8a8e08..d33db9f 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 a049128..d89a346 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 842581d..656a79e 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 af1d20e..603be1d 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 a387b21..e6975d0 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 97e1476..f25632f 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 2d060af..06c48b4 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 f8ed249..7551a0a 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 f45f364..a49e8f7 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 96f9798..e711982 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 1cd9dc4..4465e59 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 82e9595..6dc7aa2 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 7814986..56921db 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) { -- cgit v1.2.3