From dbac8ad71c3c418fd8a62c08211885a38177b725 Mon Sep 17 00:00:00 2001 From: Phil Oester Date: Thu, 20 Jul 2006 17:01:54 +0000 Subject: reduce parse_*_port duplication (Phil Oester ) The below patch (dependent upon my 'reduce service_to_port duplication' patch) centralizes the parse_*_port functions into parse_port. --- extensions/libip6t_multiport.c | 13 ------------- extensions/libip6t_tcp.c | 19 +++---------------- extensions/libip6t_udp.c | 19 +++---------------- extensions/libipt_dccp.c | 20 +++----------------- extensions/libipt_mport.c | 13 ------------- extensions/libipt_multiport.c | 13 ------------- extensions/libipt_sctp.c | 20 +++----------------- extensions/libipt_tcp.c | 19 +++---------------- extensions/libipt_udp.c | 19 +++---------------- include/ip6tables.h | 1 + include/iptables.h | 1 + ip6tables.c | 13 +++++++++++++ iptables.c | 13 +++++++++++++ 13 files changed, 46 insertions(+), 137 deletions(-) diff --git a/extensions/libip6t_multiport.c b/extensions/libip6t_multiport.c index d33db9f2..ed5fffee 100644 --- a/extensions/libip6t_multiport.c +++ b/extensions/libip6t_multiport.c @@ -50,19 +50,6 @@ proto_to_name(u_int8_t proto) } } -static u_int16_t -parse_port(const char *port, const char *proto) -{ - unsigned int portnum; - - if ((string_to_number(port, 0, 65535, &portnum)) != -1 || - (portnum = service_to_port(port, proto)) != -1) - return (u_int16_t)portnum; - - exit_error(PARAMETER_PROBLEM, - "invalid port/service `%s' specified", port); -} - static unsigned int parse_multi_ports(const char *portstring, u_int16_t *ports, const char *proto) { diff --git a/extensions/libip6t_tcp.c b/extensions/libip6t_tcp.c index d89a3462..734387c4 100644 --- a/extensions/libip6t_tcp.c +++ b/extensions/libip6t_tcp.c @@ -38,19 +38,6 @@ static struct option opts[] = { {0} }; -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, "tcp")) != -1) - return (u_int16_t)portnum; - - exit_error(PARAMETER_PROBLEM, - "invalid TCP port/service `%s' specified", port); -} - static void parse_tcp_ports(const char *portstring, u_int16_t *ports) { @@ -59,13 +46,13 @@ parse_tcp_ports(const char *portstring, u_int16_t *ports) buffer = strdup(portstring); if ((cp = strchr(buffer, ':')) == NULL) - ports[0] = ports[1] = parse_tcp_port(buffer); + ports[0] = ports[1] = parse_port(buffer, "tcp"); else { *cp = '\0'; cp++; - ports[0] = buffer[0] ? parse_tcp_port(buffer) : 0; - ports[1] = cp[0] ? parse_tcp_port(cp) : 0xFFFF; + ports[0] = buffer[0] ? parse_port(buffer, "tcp") : 0; + ports[1] = cp[0] ? parse_port(cp, "tcp") : 0xFFFF; if (ports[0] > ports[1]) exit_error(PARAMETER_PROBLEM, diff --git a/extensions/libip6t_udp.c b/extensions/libip6t_udp.c index 656a79e2..cd3c3d45 100644 --- a/extensions/libip6t_udp.c +++ b/extensions/libip6t_udp.c @@ -30,19 +30,6 @@ static struct option opts[] = { {0} }; -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, "udp")) != -1) - return (u_int16_t)portnum; - - exit_error(PARAMETER_PROBLEM, - "invalid UDP port/service `%s' specified", port); - } - static void parse_udp_ports(const char *portstring, u_int16_t *ports) { @@ -51,13 +38,13 @@ parse_udp_ports(const char *portstring, u_int16_t *ports) buffer = strdup(portstring); if ((cp = strchr(buffer, ':')) == NULL) - ports[0] = ports[1] = parse_udp_port(buffer); + ports[0] = ports[1] = parse_port(buffer, "udp"); else { *cp = '\0'; cp++; - ports[0] = buffer[0] ? parse_udp_port(buffer) : 0; - ports[1] = cp[0] ? parse_udp_port(cp) : 0xFFFF; + ports[0] = buffer[0] ? parse_port(buffer, "udp") : 0; + ports[1] = cp[0] ? parse_port(cp, "udp") : 0xFFFF; if (ports[0] > ports[1]) exit_error(PARAMETER_PROBLEM, diff --git a/extensions/libipt_dccp.c b/extensions/libipt_dccp.c index 603be1de..e5782a85 100644 --- a/extensions/libipt_dccp.c +++ b/extensions/libipt_dccp.c @@ -56,20 +56,6 @@ static struct option opts[] = { { .name = 0 } }; -static u_int16_t -parse_dccp_port(const char *port) -{ - unsigned int portnum; - - DEBUGP("%s\n", port); - if (string_to_number(port, 0, 65535, &portnum) != -1 || - (portnum = service_to_port(port, "dccp")) != -1) - return (u_int16_t)portnum; - - exit_error(PARAMETER_PROBLEM, - "invalid DCCP port/service `%s' specified", port); -} - static void parse_dccp_ports(const char *portstring, u_int16_t *ports) @@ -80,14 +66,14 @@ parse_dccp_ports(const char *portstring, buffer = strdup(portstring); DEBUGP("%s\n", portstring); if ((cp = strchr(buffer, ':')) == NULL) { - ports[0] = ports[1] = parse_dccp_port(buffer); + ports[0] = ports[1] = parse_port(buffer, "dccp"); } else { *cp = '\0'; cp++; - ports[0] = buffer[0] ? parse_dccp_port(buffer) : 0; - ports[1] = cp[0] ? parse_dccp_port(cp) : 0xFFFF; + ports[0] = buffer[0] ? parse_port(buffer, "dccp") : 0; + ports[1] = cp[0] ? parse_port(cp, "dccp") : 0xFFFF; if (ports[0] > ports[1]) exit_error(PARAMETER_PROBLEM, diff --git a/extensions/libipt_mport.c b/extensions/libipt_mport.c index e6975d03..624de134 100644 --- a/extensions/libipt_mport.c +++ b/extensions/libipt_mport.c @@ -33,19 +33,6 @@ static struct option opts[] = { {0} }; -static u_int16_t -parse_port(const char *port, const char *proto) -{ - unsigned int portnum; - - if (string_to_number(port, 0, 65535, &portnum) != -1 || - (portnum = service_to_port(port, proto)) != -1) - return (u_int16_t)portnum; - - exit_error(PARAMETER_PROBLEM, - "invalid port/service `%s' specified", port); -} - static void parse_multi_ports(const char *portstring, struct ipt_mport *minfo, const char *proto) diff --git a/extensions/libipt_multiport.c b/extensions/libipt_multiport.c index f25632f0..2a10abd4 100644 --- a/extensions/libipt_multiport.c +++ b/extensions/libipt_multiport.c @@ -68,19 +68,6 @@ proto_to_name(u_int8_t proto) } } -static u_int16_t -parse_port(const char *port, const char *proto) -{ - unsigned int portnum; - - if (string_to_number(port, 0, 65535, &portnum) != -1 || - (portnum = service_to_port(port, proto)) != -1) - return (u_int16_t)portnum; - - exit_error(PARAMETER_PROBLEM, - "invalid port/service `%s' specified", port); -} - static unsigned int parse_multi_ports(const char *portstring, u_int16_t *ports, const char *proto) { diff --git a/extensions/libipt_sctp.c b/extensions/libipt_sctp.c index 06c48b4e..0354d191 100644 --- a/extensions/libipt_sctp.c +++ b/extensions/libipt_sctp.c @@ -79,20 +79,6 @@ static struct option opts[] = { { .name = 0 } }; -static u_int16_t -parse_sctp_port(const char *port) -{ - unsigned int portnum; - - DEBUGP("%s\n", port); - if (string_to_number(port, 0, 65535, &portnum) != -1 || - (portnum = service_to_port(port, "sctp")) != -1) - return (u_int16_t)portnum; - - exit_error(PARAMETER_PROBLEM, - "invalid SCTP port/service `%s' specified", port); -} - static void parse_sctp_ports(const char *portstring, u_int16_t *ports) @@ -103,14 +89,14 @@ parse_sctp_ports(const char *portstring, buffer = strdup(portstring); DEBUGP("%s\n", portstring); if ((cp = strchr(buffer, ':')) == NULL) { - ports[0] = ports[1] = parse_sctp_port(buffer); + ports[0] = ports[1] = parse_port(buffer, "sctp"); } else { *cp = '\0'; cp++; - ports[0] = buffer[0] ? parse_sctp_port(buffer) : 0; - ports[1] = cp[0] ? parse_sctp_port(cp) : 0xFFFF; + ports[0] = buffer[0] ? parse_port(buffer, "sctp") : 0; + ports[1] = cp[0] ? parse_port(cp, "sctp") : 0xFFFF; if (ports[0] > ports[1]) exit_error(PARAMETER_PROBLEM, diff --git a/extensions/libipt_tcp.c b/extensions/libipt_tcp.c index 7551a0a6..c712b927 100644 --- a/extensions/libipt_tcp.c +++ b/extensions/libipt_tcp.c @@ -38,19 +38,6 @@ static struct option opts[] = { {0} }; -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, "tcp")) != -1) - return (u_int16_t)portnum; - - exit_error(PARAMETER_PROBLEM, - "invalid TCP port/service `%s' specified", port); -} - static void parse_tcp_ports(const char *portstring, u_int16_t *ports) { @@ -59,13 +46,13 @@ parse_tcp_ports(const char *portstring, u_int16_t *ports) buffer = strdup(portstring); if ((cp = strchr(buffer, ':')) == NULL) - ports[0] = ports[1] = parse_tcp_port(buffer); + ports[0] = ports[1] = parse_port(buffer, "tcp"); else { *cp = '\0'; cp++; - ports[0] = buffer[0] ? parse_tcp_port(buffer) : 0; - ports[1] = cp[0] ? parse_tcp_port(cp) : 0xFFFF; + ports[0] = buffer[0] ? parse_port(buffer, "tcp") : 0; + ports[1] = cp[0] ? parse_port(cp, "tcp") : 0xFFFF; if (ports[0] > ports[1]) exit_error(PARAMETER_PROBLEM, diff --git a/extensions/libipt_udp.c b/extensions/libipt_udp.c index a49e8f79..7f461d83 100644 --- a/extensions/libipt_udp.c +++ b/extensions/libipt_udp.c @@ -30,19 +30,6 @@ static struct option opts[] = { {0} }; -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, "udp")) != -1) - return (u_int16_t)portnum; - - exit_error(PARAMETER_PROBLEM, - "invalid UDP port/service `%s' specified", port); - } - static void parse_udp_ports(const char *portstring, u_int16_t *ports) { @@ -51,13 +38,13 @@ parse_udp_ports(const char *portstring, u_int16_t *ports) buffer = strdup(portstring); if ((cp = strchr(buffer, ':')) == NULL) - ports[0] = ports[1] = parse_udp_port(buffer); + ports[0] = ports[1] = parse_port(buffer, "udp"); else { *cp = '\0'; cp++; - ports[0] = buffer[0] ? parse_udp_port(buffer) : 0; - ports[1] = cp[0] ? parse_udp_port(cp) : 0xFFFF; + ports[0] = buffer[0] ? parse_port(buffer, "udp") : 0; + ports[1] = cp[0] ? parse_port(cp, "udp") : 0xFFFF; if (ports[0] > ports[1]) exit_error(PARAMETER_PROBLEM, diff --git a/include/ip6tables.h b/include/ip6tables.h index e711982c..b1140b33 100644 --- a/include/ip6tables.h +++ b/include/ip6tables.h @@ -134,6 +134,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 u_int16_t parse_port(const char *port, 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 4465e59b..ba27cac6 100644 --- a/include/iptables.h +++ b/include/iptables.h @@ -152,6 +152,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 u_int16_t parse_port(const char *port, 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 6dc7aa2d..ef7e3653 100644 --- a/ip6tables.c +++ b/ip6tables.c @@ -256,6 +256,19 @@ service_to_port(const char *name, const char *proto) return -1; } +u_int16_t +parse_port(const char *port, const char *proto) +{ + unsigned int portnum; + + if ((string_to_number(port, 0, 65535, &portnum)) != -1 || + (portnum = service_to_port(port, proto)) != -1) + return (u_int16_t)portnum; + + exit_error(PARAMETER_PROBLEM, + "invalid port/service `%s' specified", port); +} + static void in6addrcpy(struct in6_addr *dst, struct in6_addr *src) { diff --git a/iptables.c b/iptables.c index 56921db7..28917cf1 100644 --- a/iptables.c +++ b/iptables.c @@ -260,6 +260,19 @@ service_to_port(const char *name, const char *proto) return -1; } +u_int16_t +parse_port(const char *port, const char *proto) +{ + unsigned int portnum; + + if ((string_to_number(port, 0, 65535, &portnum)) != -1 || + (portnum = service_to_port(port, proto)) != -1) + return (u_int16_t)portnum; + + exit_error(PARAMETER_PROBLEM, + "invalid port/service `%s' specified", port); +} + struct in_addr * dotted_to_addr(const char *dotted) { -- cgit v1.2.3