]> git.ipfire.org Git - thirdparty/iptables.git/commitdiff
libxtables: collapse double protocol parsing
authorJan Engelhardt <jengelh@medozas.de>
Thu, 12 May 2011 12:03:36 +0000 (14:03 +0200)
committerJan Engelhardt <jengelh@medozas.de>
Thu, 12 May 2011 12:03:37 +0000 (14:03 +0200)
Un-dent xtables_parse_protocol, and make xtopt_parse_protocol make use
of it.

Signed-off-by: Jan Engelhardt <jengelh@medozas.de>
xtables.c
xtoptions.c

index 9038f892cd3c5ef18992060e52aed29cf387a11e..f10cdb7075076c3e5f552ce17dc7e403b50d5ddf 100644 (file)
--- a/xtables.c
+++ b/xtables.c
@@ -1797,37 +1797,30 @@ const struct xtables_pprot xtables_chain_protos[] = {
 uint16_t
 xtables_parse_protocol(const char *s)
 {
-       unsigned int proto;
+       const struct protoent *pent;
+       unsigned int proto, i;
 
-       if (!xtables_strtoui(s, NULL, &proto, 0, UINT8_MAX)) {
-               struct protoent *pent;
+       if (xtables_strtoui(s, NULL, &proto, 0, UINT8_MAX))
+               return proto;
 
-               /* first deal with the special case of 'all' to prevent
-                * people from being able to redefine 'all' in nsswitch
-                * and/or provoke expensive [not working] ldap/nis/...
-                * lookups */
-               if (!strcmp(s, "all"))
-                       return 0;
+       /* first deal with the special case of 'all' to prevent
+        * people from being able to redefine 'all' in nsswitch
+        * and/or provoke expensive [not working] ldap/nis/...
+        * lookups */
+       if (strcmp(s, "all") == 0)
+               return 0;
 
-               if ((pent = getprotobyname(s)))
-                       proto = pent->p_proto;
-               else {
-                       unsigned int i;
-                       for (i = 0; i < ARRAY_SIZE(xtables_chain_protos); ++i) {
-                               if (xtables_chain_protos[i].name == NULL)
-                                       continue;
+       pent = getprotobyname(s);
+       if (pent != NULL)
+               return pent->p_proto;
 
-                               if (strcmp(s, xtables_chain_protos[i].name) == 0) {
-                                       proto = xtables_chain_protos[i].num;
-                                       break;
-                               }
-                       }
-                       if (i == ARRAY_SIZE(xtables_chain_protos))
-                               xt_params->exit_err(PARAMETER_PROBLEM,
-                                          "unknown protocol `%s' specified",
-                                          s);
-               }
+       for (i = 0; i < ARRAY_SIZE(xtables_chain_protos); ++i) {
+               if (xtables_chain_protos[i].name == NULL)
+                       continue;
+               if (strcmp(s, xtables_chain_protos[i].name) == 0)
+                       return xtables_chain_protos[i].num;
        }
-
-       return proto;
+       xt_params->exit_err(PARAMETER_PROBLEM,
+               "unknown protocol \"%s\" specified", s);
+       return -1;
 }
index 9e19250b624d9364e0df4723936fb6418492490c..eb9e4e6bbec2a40d62c30a26ef3f61fd3cc7bfd9 100644 (file)
@@ -498,19 +498,7 @@ static int xtables_getportbyname(const char *name)
  */
 static void xtopt_parse_protocol(struct xt_option_call *cb)
 {
-       const struct protoent *entry;
-       unsigned int value = -1;
-
-       if (xtables_strtoui(cb->arg, NULL, &value, 0, UINT8_MAX)) {
-               cb->val.protocol = value;
-               return;
-       }
-       entry = getprotobyname(cb->arg);
-       if (entry == NULL)
-               xt_params->exit_err(PARAMETER_PROBLEM,
-                       "Protocol \"%s\" does not resolve to anything.\n",
-                       cb->arg);
-       cb->val.protocol = entry->p_proto;
+       cb->val.protocol = xtables_parse_protocol(cb->arg);
        if (cb->entry->flags & XTOPT_PUT)
                *(uint8_t *)XTOPT_MKPTR(cb) = cb->val.protocol;
 }