fix: an ARP entry's expiry is no age #22

Merged
christianmanivong merged 1 commits from fix/arp-unknown-age into main 2026-10-30 09:11:51 +00:00
3 changed files with 21 additions and 28 deletions

No files matched your search

+6
View File
@@ -13,6 +13,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- The floor on napalm is 5.1: older releases import `pkg_resources`, which a Python
3.12 venv no longer has.
### Fixed
- `get_arp_table()` reports an unknown age as `-1.0` (napalm-device-types'
`ARP_AGE_UNKNOWN`). It reported the time until the entry *expires* (FreeBSD's
`expires in N seconds`, OpenBSD's `Expire` column), which is no age: an entry
refreshed a second ago looked 20 minutes old (NetOrk/netork#1327).
## [0.5.0] - 2026-10-23
The first release on PyPI.
+7 -18
View File
@@ -128,22 +128,12 @@ def routes(text: str) -> list[dict[str, str]]:
return result
_DURATION = re.compile(r"(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s)?$")
_FREEBSD_ARP = re.compile(r"^\S+ \((?P<ip>[\d.]+)\) at (?P<mac>[0-9a-f:]{17}) on (?P<iface>\S+)")
def duration(text: str) -> float:
"""OpenBSD's ``1h2m3s`` in seconds; -1.0 for anything else (``permanent``)."""
match = _DURATION.match(text)
if not text or not match or not any(match.groups()):
return -1.0
hours, minutes, seconds = (int(g or 0) for g in match.groups())
return float(hours * 3600 + minutes * 60 + seconds)
_FREEBSD_ARP = re.compile(
r"^\S+ \((?P<ip>[\d.]+)\) at (?P<mac>[0-9a-f:]{17}) on (?P<iface>\S+)"
r"(?: expires in (?P<expires>\d+) seconds)?"
)
#: ``arp -an`` shows when an entry *expires* (FreeBSD ``expires in 1176
#: seconds``, OpenBSD's ``Expire`` column), not when the neighbour was last
#: confirmed, so the age is unknown: napalm-device-types' ``ARP_AGE_UNKNOWN``.
_AGE_UNKNOWN = -1.0
def arp(text: str) -> list[dict[str, Any]]:
@@ -152,20 +142,19 @@ def arp(text: str) -> list[dict[str, Any]]:
for line in text.splitlines():
sentence = _FREEBSD_ARP.match(line)
if sentence:
expires = sentence["expires"]
entries.append(
{
"interface": sentence["iface"],
"mac": sentence["mac"],
"ip": sentence["ip"],
"age": float(expires) if expires else -1.0,
"age": _AGE_UNKNOWN,
}
)
continue
words = line.split()
if len(words) >= 4 and re.fullmatch(r"[0-9a-f:]{17}", words[1]):
entries.append(
{"interface": words[2], "mac": words[1], "ip": words[0], "age": duration(words[3])}
{"interface": words[2], "mac": words[1], "ip": words[0], "age": _AGE_UNKNOWN}
)
return entries
+8 -10
View File
@@ -98,15 +98,19 @@ class TestRoutes:
class TestArp:
"""``arp -an`` shows when an entry expires, not when the neighbour was last
confirmed. Reported as the age, an entry refreshed a second ago looked 20
minutes old; the age is unknown, -1.0 (NetOrk/netork#1327)."""
def test_freebsd(self):
entries = parse.arp(fixture("freebsd-host", "arp_an.txt"))
assert entries[0] == {
"interface": "em0",
"mac": "bc:24:11:3c:e2:d0",
"ip": "192.0.2.254",
"age": 1176.0,
"age": -1.0,
}
assert entries[2]["age"] == -1.0 # permanent: its own address
assert {e["age"] for e in entries} == {-1.0}
def test_openbsd_table(self):
entries = parse.arp(fixture("openbsd-vm", "arp_an.txt"))
@@ -114,15 +118,9 @@ class TestArp:
"interface": "vio0",
"mac": "52:55:0a:00:02:02",
"ip": "10.0.2.2",
"age": 18 * 60 + 58.0,
"age": -1.0,
}
assert entries[2]["age"] == -1.0
@pytest.mark.parametrize(
("text", "seconds"), [("18m58s", 1138.0), ("1h2m3s", 3723.0), ("45s", 45.0)]
)
def test_openbsd_durations(self, text, seconds):
assert parse.duration(text) == seconds
assert {e["age"] for e in entries} == {-1.0}
class TestUsers: