]> git.ipfire.org Git - thirdparty/iptables.git/commitdiff
reduce parse_*_port duplication (Phil Oester <kernel@linuxace.com>)
authorPhil Oester <kernel@linuxace.com>
Thu, 20 Jul 2006 17:01:54 +0000 (17:01 +0000)
committerPatrick McHardy <kaber@trash.net>
Thu, 20 Jul 2006 17:01:54 +0000 (17:01 +0000)
The below patch (dependent upon my 'reduce service_to_port duplication' patch)
centralizes the parse_*_port functions into parse_port.

13 files changed:
extensions/libip6t_multiport.c
extensions/libip6t_tcp.c
extensions/libip6t_udp.c
extensions/libipt_dccp.c
extensions/libipt_mport.c
extensions/libipt_multiport.c
extensions/libipt_sctp.c
extensions/libipt_tcp.c
extensions/libipt_udp.c
include/ip6tables.h
include/iptables.h
ip6tables.c
iptables.c

index d33db9f2f4924f991e7f53718298998d829e0efa..ed5fffeee31da366db7a43e1cb2364392888f2e9 100644 (file)
@@ -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)
 {
index d89a346227c78f203506e020efaf1d45a5fe3bd9..734387c4387807ac4528d627e061a9696079d9af 100644 (file)
@@ -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, 
index 656a79e272a4a759a154e3f917bf98f76c9fb898..cd3c3d4568d01670cf9ca6a75ba89310eff99c13 100644 (file)
@@ -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,
index 603be1de1dda4695668988af827800a262e25eec..e5782a85d049ca91a411fc3dbf827a442bdd674c 100644 (file)
@@ -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,
index e6975d03864d476c0de4787d0c850a3f357a1cbf..624de13421c98fd6c1d944707601aeed0e1a044d 100644 (file)
@@ -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)
index f25632f0ed12d1b54dea43b71dcc7e612be1283c..2a10abd4f3a2f35048600fb856012b3d8e7d4bc2 100644 (file)
@@ -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)
 {
index 06c48b4eda57e25806453e841297903456c74c88..0354d191f6caf15d52999884093e3bdc4719ee64 100644 (file)
@@ -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,
index 7551a0a65a9af3838dc894bee2392b50102adfa6..c712b9279d8c1e3ac3a5f3c8b8b5e8dd5270f718 100644 (file)
@@ -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,
index a49e8f79ddb2aedac4e76254e51304e9284b112c..7f461d830cdd814a26945ee98840c8d3ac1281be 100644 (file)
@@ -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,
index e711982ceaac60b510043e1110361edbef29a005..b1140b33d07483f0e8f9b16c6e10eae7613a5c62 100644 (file)
@@ -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. */
index 4465e59b3fcc7c3c2254ed873da10d41f1403b5d..ba27cac607831eeb1b53e440910768de83575666 100644 (file)
@@ -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);
index 6dc7aa2da3d80e56ff3157a9578f826299046e9f..ef7e36536475502d3f6875aea2c19f9c9808d289 100644 (file)
@@ -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)
 {
index 56921db7af552e80c850322ff5b73f420e1b77e9..28917cf1b669161d7001f1e0e75057b105838cbd 100644 (file)
@@ -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)
 {