From c2e8c8c7c03d674231eb427b7857c8ed86d85311 Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Sun, 21 Mar 2021 11:32:25 +0100 Subject: [PATCH] interfaces: use an array of MAC addresses when defining supported protocols 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 | 9 ++++----- src/daemon/lldpd.c | 36 +++++++++++++++++++----------------- src/daemon/lldpd.h | 4 +--- 3 files changed, 24 insertions(+), 25 deletions(-) diff --git a/src/daemon/interfaces.c b/src/daemon/interfaces.c index e39ae415..31ecad8b 100644 --- a/src/daemon/interfaces.c +++ b/src/daemon/interfaces.c @@ -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", diff --git a/src/daemon/lldpd.c b/src/daemon/lldpd.c index 1fc26db8..282cbcd1 100644 --- a/src/daemon/lldpd.c +++ b/src/daemon/lldpd.c @@ -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)) { diff --git a/src/daemon/lldpd.h b/src/daemon/lldpd.h index 7b2ea916..c3384842 100644 --- a/src/daemon/lldpd.h +++ b/src/daemon/lldpd.h @@ -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) -- 2.39.5