fix: an ARP entry's expiry is no age
driver / standard (pull_request) Successful in 17s
driver / lint (pull_request) Successful in 15s
driver / types (pull_request) Successful in 1m9s
driver / test (3.12) (pull_request) Successful in 1m8s
driver / test (3.13) (pull_request) Successful in 58s
driver / lowest (pull_request) Successful in 24s
driver / build (pull_request) Successful in 31s
driver / security (pull_request) Successful in 1m1s
CI / driver (pull_request) Successful in 2m41s
driver / publish (pull_request) Skipped

get_arp_table reported the time until the entry expires (FreeBSD's
"expires in N seconds", OpenBSD's Expire column): an entry refreshed a
second ago looked 20 minutes old. It now reports ARP_AGE_UNKNOWN (-1.0).

NetOrk/netork#1327
This commit is contained in:
Christian Manivong committed 2026-10-30 08:37:08 +01:00
1 parent 21d90185f4
commit 1156da123d
3 files changed
+21 -28

No files matched your search

+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