]> git.ipfire.org Git - thirdparty/iptables.git/commitdiff
Use proto_to_name() from xshared in more places
authorPhil Sutter <phil@nwl.cc>
Mon, 16 Nov 2020 23:57:10 +0000 (00:57 +0100)
committerPhil Sutter <phil@nwl.cc>
Mon, 17 May 2021 13:07:22 +0000 (15:07 +0200)
Share the common proto name lookup code. While being at it, make proto
number variable 16bit, values may exceed 256.

This aligns iptables-nft '-p' argument printing with legacy iptables. In
practice, this should make a difference only in corner cases.

Signed-off-by: Phil Sutter <phil@nwl.cc>
include/xtables.h
iptables/ip6tables.c
iptables/iptables.c
iptables/nft-shared.c
iptables/xshared.c
iptables/xshared.h

index df1eaee326643ae2542816ae75e5e428201ad122..1fd5f63ac4b69a3033f262c6713234f0a5e6c990 100644 (file)
@@ -395,7 +395,7 @@ struct xtables_rule_match {
  */
 struct xtables_pprot {
        const char *name;
-       uint8_t num;
+       uint16_t num;
 };
 
 enum xtables_tryload {
index 044d9a33a0266846c4df29f7885115d14ae557db..e967c040fd3c9832977961731be7400bfd890a3f 100644 (file)
@@ -759,28 +759,16 @@ print_iface(char letter, const char *iface, const unsigned char *mask,
        }
 }
 
-/* The ip6tables looks up the /etc/protocols. */
 static void print_proto(uint16_t proto, int invert)
 {
        if (proto) {
-               unsigned int i;
+               const char *pname = proto_to_name(proto, 0);
                const char *invertstr = invert ? " !" : "";
 
-               const struct protoent *pent = getprotobynumber(proto);
-               if (pent) {
-                       printf("%s -p %s",
-                              invertstr, pent->p_name);
-                       return;
-               }
-
-               for (i = 0; xtables_chain_protos[i].name != NULL; ++i)
-                       if (xtables_chain_protos[i].num == proto) {
-                               printf("%s -p %s",
-                                      invertstr, xtables_chain_protos[i].name);
-                               return;
-                       }
-
-               printf("%s -p %u", invertstr, proto);
+               if (pname)
+                       printf("%s -p %s", invertstr, pname);
+               else
+                       printf("%s -p %u", invertstr, proto);
        }
 }
 
index da67dd2e1e9970ccf6508b12434d7d82be1a65ce..b925f0892e0d5fc4d68cfd6d57e07c71a7cef44c 100644 (file)
@@ -727,23 +727,13 @@ list_entries(const xt_chainlabel chain, int rulenum, int verbose, int numeric,
 static void print_proto(uint16_t proto, int invert)
 {
        if (proto) {
-               unsigned int i;
+               const char *pname = proto_to_name(proto, 0);
                const char *invertstr = invert ? " !" : "";
 
-               const struct protoent *pent = getprotobynumber(proto);
-               if (pent) {
-                       printf("%s -p %s", invertstr, pent->p_name);
-                       return;
-               }
-
-               for (i = 0; xtables_chain_protos[i].name != NULL; ++i)
-                       if (xtables_chain_protos[i].num == proto) {
-                               printf("%s -p %s",
-                                      invertstr, xtables_chain_protos[i].name);
-                               return;
-                       }
-
-               printf("%s -p %u", invertstr, proto);
+               if (pname)
+                       printf("%s -p %s", invertstr, pname);
+               else
+                       printf("%s -p %u", invertstr, proto);
        }
 }
 
index c1664b50f938346d720f93e1c00832dbd59d9b46..4253b08196d295309934c487b2c751a8ed601282 100644 (file)
@@ -826,13 +826,13 @@ void save_rule_details(const struct iptables_command_state *cs,
        }
 
        if (proto > 0) {
-               const struct protoent *pent = getprotobynumber(proto);
+               const char *pname = proto_to_name(proto, 0);
 
                if (invflags & XT_INV_PROTO)
                        printf("! ");
 
-               if (pent)
-                       printf("-p %s ", pent->p_name);
+               if (pname)
+                       printf("-p %s ", pname);
                else
                        printf("-p %u ", proto);
        }
index a3f473ea607c864989224baa80339d34b7bacf9d..49a88de518466b33647d65ea24b8bbba2d9330a8 100644 (file)
@@ -48,7 +48,7 @@ void print_extension_helps(const struct xtables_target *t,
 }
 
 const char *
-proto_to_name(uint8_t proto, int nolookup)
+proto_to_name(uint16_t proto, int nolookup)
 {
        unsigned int i;
 
index dace221bbd962f39585a9310ec809ebfb1495e97..823894f94b84150d69ca2dacac09907e56059171 100644 (file)
@@ -162,7 +162,7 @@ enum {
 
 extern void print_extension_helps(const struct xtables_target *,
        const struct xtables_rule_match *);
-extern const char *proto_to_name(uint8_t, int);
+extern const char *proto_to_name(uint16_t, int);
 extern int command_default(struct iptables_command_state *,
        struct xtables_globals *, bool invert);
 extern struct xtables_match *load_proto(struct iptables_command_state *);