Skip to content

Commit 668e745

Browse files
committed
fix: don't cache-flush an AAAA received with another scope
The scope is not on the wire, so an AAAA the packet carries under a different scope is the same record (RFC 6762 section 10.2). Without this a host with two interfaces on the link, or a dual-stack socket, expires and re-adds the other scope's variant on every announcement, firing an Updated event each time.
1 parent 73aa8cf commit 668e745

3 files changed

Lines changed: 45 additions & 2 deletions

File tree

‎src/zeroconf/_cache.pxd‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ cdef object heappush
1616
cdef object heapify
1717

1818
cdef object _UNIQUE_RECORD_TYPES
19+
cdef unsigned int _TYPE_AAAA
1920
cdef unsigned int _TYPE_PTR
2021
cdef cython.uint _ONE_SECOND
2122
cdef unsigned int _MIN_SCHEDULED_RECORD_EXPIRATION
@@ -25,6 +26,8 @@ cdef unsigned int _MAX_CACHE_RECORDS
2526
@cython.locals(record_cache=dict)
2627
cdef _remove_key(cython.dict cache, object key, DNSRecord record)
2728

29+
cdef bint _has_scope_variant(DNSRecord record, object answers)
30+
2831

2932
cdef class DNSCache:
3033

‎src/zeroconf/_cache.py‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from collections.abc import Iterable
1111
from heapq import heapify, heappop, heappush
12-
from typing import cast
12+
from typing import TYPE_CHECKING, cast
1313

1414
from ._dns import (
1515
DNSAddress,
@@ -22,13 +22,14 @@
2222
DNSText,
2323
)
2424
from ._utils.time import current_time_millis
25-
from .const import _MAX_CACHE_RECORDS, _ONE_SECOND, _TYPE_PTR
25+
from .const import _MAX_CACHE_RECORDS, _ONE_SECOND, _TYPE_AAAA, _TYPE_PTR
2626

2727
_UNIQUE_RECORD_TYPES = (DNSAddress, DNSHinfo, DNSPointer, DNSText, DNSService)
2828
_UniqueRecordsType = DNSAddress | DNSHinfo | DNSPointer | DNSText | DNSService
2929
_DNSRecordCacheType = dict[str, dict[DNSRecord, DNSRecord]]
3030
_UniqueType = tuple[str, int, int]
3131
_DNSRecord = DNSRecord
32+
_DNSRecordSet = set[DNSRecord]
3233
_str = str
3334
_float = float
3435
_int = int
@@ -50,6 +51,16 @@ def _remove_key(cache: _DNSRecordCacheType, key: _str, record: _DNSRecord) -> No
5051
del cache[key]
5152

5253

54+
def _has_scope_variant(record: DNSRecord, answers: _DNSRecordSet) -> bool:
55+
"""True if answers carry the AAAA record's address under another scope."""
56+
if TYPE_CHECKING:
57+
record = cast(DNSAddress, record)
58+
for answer in answers:
59+
if isinstance(answer, DNSAddress) and answer.key == record.key and answer.address == record.address:
60+
return True
61+
return False
62+
63+
5364
class DNSCache:
5465
"""Store of received DNS records, indexed for fast lookup."""
5566

@@ -156,6 +167,10 @@ def async_mark_unique_records_older_than_1s_to_expire(
156167
for record in self.async_all_by_details(name, type_, class_):
157168
created_double = record.created
158169
if (now - created_double > _ONE_SECOND) and record not in answers_rrset:
170+
# The scope is not on the wire, so the same address
171+
# received with another scope is the same record.
172+
if type_ == _TYPE_AAAA and _has_scope_variant(record, answers_rrset):
173+
continue
159174
# Expire in 1s
160175
self._async_set_created_ttl(record, now, 1)
161176

‎tests/test_cache.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import annotations
44

55
import logging
6+
import socket
67
import unittest.mock
78
from heapq import heapify, heappop
89

@@ -719,3 +720,27 @@ def actual() -> int:
719720
cache.async_add_records([rec])
720721
assert cache._total_records == actual()
721722
assert cache._total_records == const._MAX_CACHE_RECORDS
723+
724+
725+
def test_cache_flush_keeps_aaaa_received_with_another_scope() -> None:
726+
"""A cache-flush answer does not expire the same AAAA cached under another scope."""
727+
cache = r.DNSCache()
728+
now = r.current_time_millis()
729+
old = now - 2000
730+
address = socket.inet_pton(socket.AF_INET6, "fe80::1")
731+
other_address = socket.inet_pton(socket.AF_INET6, "fe80::2")
732+
scoped = r.DNSAddress(
733+
"host.local.", const._TYPE_AAAA, const._CLASS_IN | const._CLASS_UNIQUE, 120, address, 2, old
734+
)
735+
stale = r.DNSAddress(
736+
"host.local.", const._TYPE_AAAA, const._CLASS_IN | const._CLASS_UNIQUE, 120, other_address, 2, old
737+
)
738+
cache.async_add_records([scoped, stale])
739+
unscoped = r.DNSAddress(
740+
"host.local.", const._TYPE_AAAA, const._CLASS_IN | const._CLASS_UNIQUE, 120, address, 0, now
741+
)
742+
cache.async_mark_unique_records_older_than_1s_to_expire(
743+
{("host.local.", const._TYPE_AAAA, const._CLASS_IN)}, [unscoped], now
744+
)
745+
assert scoped.ttl == 120
746+
assert stale.ttl == 1

0 commit comments

Comments
 (0)