fix: normalize MAC addresses with leading zeros for subscriber matching

When SNMP returns MAC addresses as strings (e.g., 'A:B:C:D:E:F'), they were
being converted to lowercase without padding hex digits with leading zeros.
This caused subscriber matching to fail because the normalize_mac function
expects properly padded MACs (e.g., '0a:0b:0c:0d:0e:0f').

Now properly normalizes string MACs by:
- Removing all separators
- Ensuring 12 hex characters
- Reformatting with colon separators and proper padding

This fixes subscriber matching for ePMP and some Ubiquiti devices that
return string-formatted MACs from SNMP.
This commit is contained in:
Graham McIntire 2026-03-10 11:05:57 -05:00
parent f19fb958cb
commit bbe7a8c4fa
No known key found for this signature in database

View file

@ -253,9 +253,21 @@ defmodule Towerops.Snmp.WirelessClientDiscovery do
defp format_mac(value) when is_binary(value) do defp format_mac(value) when is_binary(value) do
if String.contains?(value, ":") or String.contains?(value, "-") do if String.contains?(value, ":") or String.contains?(value, "-") do
# String MAC with separators - normalize to ensure proper padding
value value
|> String.downcase() |> String.downcase()
|> String.replace("-", ":") |> String.replace(~r/[^0-9a-f]/, "")
|> case do
<<a::binary-2, b::binary-2, c::binary-2, d::binary-2, e::binary-2, f::binary-2>> ->
"#{a}:#{b}:#{c}:#{d}:#{e}:#{f}"
# Not 12 hex chars - try to parse as byte string
_ ->
value
|> :binary.bin_to_list()
|> Enum.map_join(":", &String.pad_leading(Integer.to_string(&1, 16), 2, "0"))
|> String.downcase()
end
else else
value value
|> :binary.bin_to_list() |> :binary.bin_to_list()