This moves getethertype.c into libxtables so that both extensions and
xtables-nft-multi may use the implementations therein. New users are
libebt_arp and libebt_vlan which drop their own duplicated
implementations of getethertypebyname() for the shared one.

This change originated from a covscan report of extensions'
implementations not checking fopen() return value which should be
implicitly fixed by this as well.

Signed-off-by: Phil Sutter <p...@nwl.cc>
---
 extensions/libebt_arp.c                 | 72 +------------------------
 extensions/libebt_vlan.c                | 72 +------------------------
 iptables/Makefile.am                    |  2 +-
 libxtables/Makefile.am                  |  2 +-
 {iptables => libxtables}/getethertype.c |  0
 5 files changed, 4 insertions(+), 144 deletions(-)
 rename {iptables => libxtables}/getethertype.c (100%)

diff --git a/extensions/libebt_arp.c b/extensions/libebt_arp.c
index 45fc8d73e24d2..dc8e306a2879f 100644
--- a/extensions/libebt_arp.c
+++ b/extensions/libebt_arp.c
@@ -209,76 +209,6 @@ static int brarp_get_mac_and_mask(const char *from, 
unsigned char *to, unsigned
        return 0;
 }
 
-static struct ethertypeent *brarp_getethertypeent(FILE *etherf, const char 
*name)
-{
-       static struct ethertypeent et_ent;
-       char *e, *found_name;
-       char line[1024];
-
-       while ((e = fgets(line, sizeof(line), etherf))) {
-               char *endptr, *cp;
-
-               if (*e == '#')
-                       continue;
-
-               cp = strpbrk(e, "#\n");
-               if (cp == NULL)
-                       continue;
-               *cp = '\0';
-               found_name = e;
-
-               cp = strpbrk(e, " \t");
-               if (cp == NULL)
-                       continue;
-
-               *cp++ = '\0';
-               while (*cp == ' ' || *cp == '\t')
-                       cp++;
-               e = strpbrk(cp, " \t");
-               if (e != NULL)
-                       *e++ = '\0';
-
-               et_ent.e_ethertype = strtol(cp, &endptr, 16);
-               if (*endptr != '\0' ||
-                   (et_ent.e_ethertype < ETH_ZLEN || et_ent.e_ethertype > 
0xFFFF))
-                       continue;
-
-               if (strcasecmp(found_name, name) == 0)
-                       return (&et_ent);
-
-               if (e != NULL) {
-                       cp = e;
-                       while (cp && *cp) {
-                               if (*cp == ' ' || *cp == '\t') {
-                                       cp++;
-                                       continue;
-                               }
-                               e = cp;
-                               cp = strpbrk(cp, " \t");
-                               if (cp != NULL)
-                                       *cp++ = '\0';
-                               if (strcasecmp(e, name) == 0)
-                                       return (&et_ent);
-                               e = cp;
-                       }
-               }
-       }
-
-       return NULL;
-}
-
-static struct ethertypeent *brarp_getethertypebyname(const char *name)
-{
-       struct ethertypeent *e;
-       FILE *etherf;
-
-       etherf = fopen(_PATH_ETHERTYPES, "r");
-
-       e = brarp_getethertypeent(etherf, name);
-       fclose(etherf);
-       return (e);
-}
-
 static int
 brarp_parse(int c, char **argv, int invert, unsigned int *flags,
            const void *entry, struct xt_entry_match **match)
@@ -334,7 +264,7 @@ brarp_parse(int c, char **argv, int invert, unsigned int 
*flags,
                if (i < 0 || i >= (0x1 << 16) || *end !='\0') {
                        struct ethertypeent *ent;
 
-                       ent = brarp_getethertypebyname(argv[optind - 1]);
+                       ent = getethertypebyname(argv[optind - 1]);
                        if (!ent)
                                xtables_error(PARAMETER_PROBLEM, "Problem with 
specified ARP "
                                                                 "protocol 
type");
diff --git a/extensions/libebt_vlan.c b/extensions/libebt_vlan.c
index 4e2ea0fcb7cd1..52cc99fa19c7e 100644
--- a/extensions/libebt_vlan.c
+++ b/extensions/libebt_vlan.c
@@ -50,76 +50,6 @@ static void brvlan_print_help(void)
 "--vlan-encap [!] encap : Encapsulated frame protocol (hexadecimal or 
name)\n");
 }
 
-static struct ethertypeent *vlan_getethertypeent(FILE *etherf, const char 
*name)
-{
-       static struct ethertypeent et_ent;
-       char *e, *found_name;
-       char line[1024];
-
-       while ((e = fgets(line, sizeof(line), etherf))) {
-               char *endptr, *cp;
-
-               if (*e == '#')
-                       continue;
-
-               cp = strpbrk(e, "#\n");
-               if (cp == NULL)
-                       continue;
-               *cp = '\0';
-               found_name = e;
-
-               cp = strpbrk(e, " \t");
-               if (cp == NULL)
-                       continue;
-
-               *cp++ = '\0';
-               while (*cp == ' ' || *cp == '\t')
-                       cp++;
-               e = strpbrk(cp, " \t");
-               if (e != NULL)
-                       *e++ = '\0';
-
-               et_ent.e_ethertype = strtol(cp, &endptr, 16);
-               if (*endptr != '\0' ||
-                   (et_ent.e_ethertype < ETH_ZLEN || et_ent.e_ethertype > 
0xFFFF))
-                       continue; // skip invalid etherproto type entry
-
-               if (strcasecmp(found_name, name) == 0)
-                       return (&et_ent);
-
-               if (e != NULL) {
-                       cp = e;
-                       while (cp && *cp) {
-                               if (*cp == ' ' || *cp == '\t') {
-                                       cp++;
-                                       continue;
-                               }
-                               e = cp;
-                               cp = strpbrk(cp, " \t");
-                               if (cp != NULL)
-                                       *cp++ = '\0';
-                               if (strcasecmp(e, name) == 0)
-                                       return (&et_ent);
-                               e = cp;
-                       }
-               }
-       }
-
-       return NULL;
-}
-
-static struct ethertypeent *brvlan_getethertypebyname(const char *name)
-{
-       struct ethertypeent *e;
-       FILE *etherf;
-
-       etherf = fopen(_PATH_ETHERTYPES, "r");
-
-       e = vlan_getethertypeent(etherf, name);
-       fclose(etherf);
-       return (e);
-}
-
 static int
 brvlan_parse(int c, char **argv, int invert, unsigned int *flags,
               const void *entry, struct xt_entry_match **match)
@@ -156,7 +86,7 @@ brvlan_parse(int c, char **argv, int invert, unsigned int 
*flags,
                        vlaninfo->invflags |= EBT_VLAN_ENCAP;
                local.encap = strtoul(optarg, &end, 16);
                if (*end != '\0') {
-                       ethent = brvlan_getethertypebyname(optarg);
+                       ethent = getethertypebyname(optarg);
                        if (ethent == NULL)
                                xtables_error(PARAMETER_PROBLEM, "Unknown 
--vlan-encap value ('%s')", optarg);
                        local.encap = ethent->e_ethertype;
diff --git a/iptables/Makefile.am b/iptables/Makefile.am
index d0218ddc80487..581dc32ba846b 100644
--- a/iptables/Makefile.am
+++ b/iptables/Makefile.am
@@ -43,7 +43,7 @@ xtables_nft_multi_SOURCES += xtables-save.c xtables-restore.c 
\
                                nft-shared.c nft-ipv4.c nft-ipv6.c nft-arp.c \
                                xtables-monitor.c \
                                xtables-arp-standalone.c xtables-arp.c \
-                               getethertype.c nft-bridge.c \
+                               nft-bridge.c \
                                xtables-eb-standalone.c xtables-eb.c \
                                xtables-eb-translate.c \
                                xtables-translate.c
diff --git a/libxtables/Makefile.am b/libxtables/Makefile.am
index 4267cb5fb7d86..8ff6b0cad2850 100644
--- a/libxtables/Makefile.am
+++ b/libxtables/Makefile.am
@@ -4,7 +4,7 @@ AM_CFLAGS   = ${regular_CFLAGS}
 AM_CPPFLAGS = ${regular_CPPFLAGS} -I${top_builddir}/include 
-I${top_srcdir}/include -I${top_srcdir}/iptables ${kinclude_CPPFLAGS}
 
 lib_LTLIBRARIES       = libxtables.la
-libxtables_la_SOURCES = xtables.c xtoptions.c
+libxtables_la_SOURCES = xtables.c xtoptions.c getethertype.c
 libxtables_la_LDFLAGS = -version-info 
${libxtables_vcurrent}:0:${libxtables_vage}
 libxtables_la_LIBADD  =
 if ENABLE_STATIC
diff --git a/iptables/getethertype.c b/libxtables/getethertype.c
similarity index 100%
rename from iptables/getethertype.c
rename to libxtables/getethertype.c
-- 
2.18.0

Reply via email to