]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
interfaces: use an array of MAC addresses when defining supported protocols fix/multiple-mac-handling
authorVincent Bernat <vincent@bernat.ch>
Sun, 21 Mar 2021 10:32:25 +0000 (11:32 +0100)
committerVincent Bernat <vincent@bernat.ch>
Sun, 21 Mar 2021 10:32:25 +0000 (11:32 +0100)
In interfaces.c, we were handling it as a table while in lldpd.c, we
were copy-pasting the same condition three times. This was confusing
for analysis tools.

src/daemon/interfaces.c
src/daemon/lldpd.c
src/daemon/lldpd.h

index e39ae415573346fe60d241fe40348c65ccf3942d..31ecad8bb7d2481eb3071ab9b9991300c85c62b5 100644 (file)
@@ -50,13 +50,12 @@ interfaces_setup_multicast(struct lldpd *cfg, const char *name,
 
        for (i = 0; cfg->g_protocols[i].mode != 0; i++) {
                if (!cfg->g_protocols[i].enabled) continue;
-               for (mac = cfg->g_protocols[i].mac1, j = 0;
-                    j < 3;
-                    mac += ETHER_ADDR_LEN,
+               for (j = 0;
+                    j < sizeof(cfg->g_protocols[0].mac)/sizeof(cfg->g_protocols[0].mac[0]);
                     j++) {
+                       mac = cfg->g_protocols[i].mac[j];
                        if (memcmp(mac, zero, ETHER_ADDR_LEN) == 0) break;
-                       if ((rc = priv_iface_multicast(name,
-                                   mac, !remove)) != 0) {
+                       if ((rc = priv_iface_multicast(name, mac, !remove)) != 0) {
                                errno = rc;
                                if (errno != ENOENT)
                                        log_debug("interfaces",
index 5dd2413adf549222d1aaea6c5b0c4b77078c1598..0e4a64ff923c5ed456b4e7b72105df1dad836e0a 100644 (file)
@@ -52,29 +52,29 @@ static void          usage(void);
 static struct protocol protos[] =
 {
        { LLDPD_MODE_LLDP, 1, "LLDP", 'l', lldp_send, lldp_decode, NULL,
-         LLDP_ADDR_NEAREST_BRIDGE,
-         LLDP_ADDR_NEAREST_NONTPMR_BRIDGE,
-         LLDP_ADDR_NEAREST_CUSTOMER_BRIDGE },
+         LLDP_ADDR_NEAREST_BRIDGE,
+           LLDP_ADDR_NEAREST_NONTPMR_BRIDGE,
+           LLDP_ADDR_NEAREST_CUSTOMER_BRIDGE } },
 #ifdef ENABLE_CDP
        { LLDPD_MODE_CDPV1, 0, "CDPv1", 'c', cdpv1_send, cdp_decode, cdpv1_guess,
-         CDP_MULTICAST_ADDR },
+         { CDP_MULTICAST_ADDR } },
        { LLDPD_MODE_CDPV2, 0, "CDPv2", 'c', cdpv2_send, cdp_decode, cdpv2_guess,
-         CDP_MULTICAST_ADDR },
+         { CDP_MULTICAST_ADDR } },
 #endif
 #ifdef ENABLE_SONMP
        { LLDPD_MODE_SONMP, 0, "SONMP", 's', sonmp_send, sonmp_decode, NULL,
-         SONMP_MULTICAST_ADDR },
+         { SONMP_MULTICAST_ADDR } },
 #endif
 #ifdef ENABLE_EDP
        { LLDPD_MODE_EDP, 0, "EDP", 'e', edp_send, edp_decode, NULL,
-         EDP_MULTICAST_ADDR },
+         { EDP_MULTICAST_ADDR } },
 #endif
 #ifdef ENABLE_FDP
        { LLDPD_MODE_FDP, 0, "FDP", 'f', fdp_send, cdp_decode, NULL,
-         FDP_MULTICAST_ADDR },
+         { FDP_MULTICAST_ADDR } },
 #endif
        { 0, 0, "any", ' ', NULL, NULL, NULL,
-         {0,0,0,0,0,0} }
+         { { 0, 0, 0, 0, 0, 0 } } }
 };
 
 static char            **saved_argv;
@@ -511,19 +511,21 @@ lldpd_move_chassis(struct lldpd_chassis *ochassis,
 static int
 lldpd_guess_type(struct lldpd *cfg, char *frame, int s)
 {
-       int i;
+       size_t i, j;
        if (s < ETHER_ADDR_LEN)
                return -1;
-       for (i=0; cfg->g_protocols[i].mode != 0; i++) {
+       for (i = 0; cfg->g_protocols[i].mode != 0; i++) {
                if (!cfg->g_protocols[i].enabled)
                        continue;
                if (cfg->g_protocols[i].guess == NULL) {
-                       if (memcmp(frame, cfg->g_protocols[i].mac1, ETHER_ADDR_LEN) == 0 ||
-                           memcmp(frame, cfg->g_protocols[i].mac2, ETHER_ADDR_LEN) == 0 ||
-                           memcmp(frame, cfg->g_protocols[i].mac3, ETHER_ADDR_LEN) == 0) {
-                               log_debug("decode", "guessed protocol is %s (from MAC address)",
-                                   cfg->g_protocols[i].name);
-                               return cfg->g_protocols[i].mode;
+                       for (j = 0;
+                            j < sizeof(cfg->g_protocols[0].mac)/sizeof(cfg->g_protocols[0].mac[0]);
+                            j++) {
+                               if (memcmp(frame, cfg->g_protocols[i].mac[j], ETHER_ADDR_LEN) == 0) {
+                                       log_debug("decode", "guessed protocol is %s (from MAC address)",
+                                           cfg->g_protocols[i].name);
+                                       return cfg->g_protocols[i].mode;
+                               }
                        }
                } else {
                        if (cfg->g_protocols[i].guess(frame, s)) {
index 7b2ea9160726fc2c5bca4b92132d4496c154354f..c338484276734024cecfd175067ef5d26ccbd3bc 100644 (file)
@@ -88,9 +88,7 @@ struct protocol {
        int(*send)(PROTO_SEND_SIG);     /* How to send a frame */
        int(*decode)(PROTO_DECODE_SIG); /* How to decode a frame */
        int(*guess)(PROTO_GUESS_SIG);   /* Can be NULL, use MAC address in this case */
-       u_int8_t         mac1[ETHER_ADDR_LEN];  /* Destination MAC address used by this protocol */
-       u_int8_t         mac2[ETHER_ADDR_LEN];  /* Destination MAC address used by this protocol */
-       u_int8_t         mac3[ETHER_ADDR_LEN];  /* Destination MAC address used by this protocol */
+       u_int8_t         mac[3][ETHER_ADDR_LEN];  /* Destination MAC addresses used by this protocol */
 };
 
 #define SMART_HIDDEN(port) (port->p_hidden_in)