Răsfoiți Sursa

fix(snmp): emit uppercase MACs from the FDB parser (#960) (#961)

The SNMP FDB parser built MACs with %02x (lowercase) while every other
source (agent FDB probe, wireless, DHCP leases, device_attrib,
device_seen) uses uppercase. Merge maps keyed by the raw MAC string then
held the same device twice, once per casing, and SNMP-learned clients
showed up duplicated in the client list.

The same mismatch also broke the bridge-MAC uplink exclusion (fdbsticky
compares against uppercase router MACs), letting transit MACs on uplink
ports count as clients of the switch.

Single-point fix: macFromParts now emits the canonical uppercase form.
Test expectations updated.
Nacho 2 luni în urmă
părinte
comite
cec7c79364

+ 5 - 1
server-go/internal/snmp/fdbTable.go

@@ -133,6 +133,10 @@ func extractMacFromOid(name, prefix string) string {
 	return macFromParts(parts[len(parts)-6:])
 }
 
+// macFromParts construye la MAC en MAYÚSCULAS (#960): es la capitalización
+// canónica de todo el sistema (FDB del agente, wireless, leases,
+// device_attrib, device_seen). En minúscula, los mapas de merge indexados
+// por MAC duplicaban cada cliente SNMP (una entrada por capitalización).
 func macFromParts(parts []string) string {
 	out := make([]string, 6)
 	for i, p := range parts {
@@ -140,7 +144,7 @@ func macFromParts(parts []string) string {
 		if _, err := fmt.Sscanf(p, "%d", &n); err != nil || n < 0 || n > 255 {
 			return ""
 		}
-		out[i] = fmt.Sprintf("%02x", n)
+		out[i] = fmt.Sprintf("%02X", n)
 	}
 	return strings.Join(out, ":")
 }

+ 4 - 4
server-go/internal/snmp/fdbTable_test.go

@@ -45,13 +45,13 @@ func TestFdbEntriesDot1d(t *testing.T) {
 	if len(got) != 3 {
 		t.Fatalf("esperaba 3 entradas, obtuve %d: %+v", len(got), got)
 	}
-	if got[0].MAC != "8c:16:18:bb:1e:0c" || got[0].BridgePortIndex != 3 || got[0].IfIndex != 103 {
+	if got[0].MAC != "8C:16:18:BB:1E:0C" || got[0].BridgePortIndex != 3 || got[0].IfIndex != 103 {
 		t.Errorf("entrada 0 inesperada: %+v", got[0])
 	}
-	if got[1].MAC != "00:0c:22:38:4e:5a" || got[1].BridgePortIndex != 7 || got[1].IfIndex != 7 {
+	if got[1].MAC != "00:0C:22:38:4E:5A" || got[1].BridgePortIndex != 7 || got[1].IfIndex != 7 {
 		t.Errorf("entrada 1 inesperada: %+v", got[1])
 	}
-	if got[2].MAC != "00:04:4b:e9:b2:1d" || got[2].BridgePortIndex != 49167 || got[2].IfIndex != 49167 {
+	if got[2].MAC != "00:04:4B:E9:B2:1D" || got[2].BridgePortIndex != 49167 || got[2].IfIndex != 49167 {
 		t.Errorf("entrada 2 inesperada: %+v", got[2])
 	}
 }
@@ -68,7 +68,7 @@ func TestFdbEntriesDot1q(t *testing.T) {
 	if len(got) != 1 {
 		t.Fatalf("esperaba 1 entrada, obtuve %d: %+v", len(got), got)
 	}
-	if got[0].MAC != "8c:16:18:bb:1e:0c" || got[0].BridgePortIndex != 5 || got[0].IfIndex != 205 {
+	if got[0].MAC != "8C:16:18:BB:1E:0C" || got[0].BridgePortIndex != 5 || got[0].IfIndex != 205 {
 		t.Errorf("entrada inesperada: %+v", got[0])
 	}
 }

+ 2 - 2
server-go/internal/snmp/snmp_test.go

@@ -34,10 +34,10 @@ func TestExtractMacFromOid(t *testing.T) {
 		want string
 	}{
 		{prefix + ".0.17.34.51.68.85", "00:11:22:33:44:55"},
-		{prefix + ".255.255.255.255.255.255", "ff:ff:ff:ff:ff:ff"},
+		{prefix + ".255.255.255.255.255.255", "FF:FF:FF:FF:FF:FF"},
 		// #950: índice con prefijo VLAN (TP-Link Omada indexa la dot1d por
 		// <vlan>.<mac>); la MAC son los últimos 6 octetos.
-		{prefix + ".1.0.4.75.233.178.29", "00:04:4b:e9:b2:1d"},
+		{prefix + ".1.0.4.75.233.178.29", "00:04:4B:E9:B2:1D"},
 		{prefix + ".1.2.3.4.5.6.7", "02:03:04:05:06:07"},
 		{prefix + ".1.2.3.4.5", ""},
 		{prefix + ".0.17.34.51.68.256", ""},