3838
3939DEBUG_ENABLED = partial (log .isEnabledFor , logging .DEBUG )
4040
41+ _NOT_SEEN = (- 1e30 , True )
42+
4143
4244class AsyncListener :
4345 """Asyncio datagram protocol that receives mDNS packets and feeds them
@@ -46,14 +48,14 @@ class AsyncListener:
4648 __slots__ = (
4749 "_deferred" ,
4850 "_deferred_deadlines" ,
51+ "_last_scoped" ,
4952 "_query_handler" ,
5053 "_recent_packets" ,
5154 "_record_manager" ,
5255 "_registry" ,
5356 "_timers" ,
5457 "data" ,
5558 "last_message" ,
56- "last_sender_scope" ,
5759 "last_time" ,
5860 "sock_description" ,
5961 "transport" ,
@@ -68,7 +70,7 @@ def __init__(self, zc: Zeroconf) -> None:
6870 self .data : bytes | None = None
6971 self .last_time : float = 0
7072 self .last_message : DNSIncoming | None = None
71- self .last_sender_scope = 0
73+ self ._last_scoped = False
7274 self .transport : _WrappedTransport | None = None
7375 self .sock_description : str | None = None
7476 self ._deferred : dict [str , list [DNSIncoming ]] = {}
@@ -78,13 +80,10 @@ def __init__(self, zc: Zeroconf) -> None:
7880 # flood cannot defeat single-slot dedup; relies on dict insertion
7981 # order so the oldest entry is evicted first. Only payloads
8082 # without a QU question are cached so unicast replies still go
81- # out on every receipt.
82- #
83- # A dual-stack socket receives a responder's IPv4 and IPv6 copies
84- # of the same message (RFC 6762 section 20), and only a copy from
85- # a link-local sender gives its link-local AAAA records a scope,
86- # so a copy from a scoped sender is keyed with its scope.
87- self ._recent_packets : dict [bytes | tuple [bytes , int ], float ] = {}
83+ # out on every receipt. Each entry records whether the copy came
84+ # from a scoped sender: only that copy scopes link-local AAAA
85+ # records, so it is not suppressed by an unscoped one.
86+ self ._recent_packets : dict [bytes , tuple [float , bool ]] = {}
8887 super ().__init__ ()
8988
9089 def cancel_pending_timers (self ) -> None :
@@ -236,11 +235,11 @@ def _process_datagram_at_time(
236235 addrs : tuple [str , int ] | tuple [str , int , int , int ],
237236 ) -> None :
238237 # Only a response's records take the sender's scope (QR is the top
239- # bit of header byte 2), so queries keep scope-independent dedup .
240- sender_scope = addrs [ 3 ] if len (addrs ) == 4 and data_len > 2 and data [2 ] & 0x80 else 0
238+ # bit of header byte 2).
239+ scoped = len (addrs ) == 4 and addrs [ 3 ] != 0 and data_len > 2 and ( data [2 ] & 0x80 ) != 0
241240 if (
242241 self .data == data
243- and self .last_sender_scope == sender_scope
242+ and ( self ._last_scoped or not scoped )
244243 and (now - _DUPLICATE_PACKET_SUPPRESSION_INTERVAL ) < self .last_time
245244 and self .last_message is not None
246245 and not self .last_message .has_qu_question ()
@@ -257,17 +256,17 @@ def _process_datagram_at_time(
257256 )
258257 return
259258
260- # `get(data, -1e30 )` keeps the suppression compare a single C
259+ # `get(data, _NOT_SEEN )` keeps the suppression compare a single C
261260 # double compare; the sentinel is far below any real `now -
262261 # _DUPLICATE_PACKET_SUPPRESSION_INTERVAL` so a cache miss never
263262 # triggers the branch even when `now` is small (time.monotonic
264263 # is allowed to start near zero, leaving `now - INTERVAL`
265264 # negative for the first ~1s after boot). Only non-QU payloads
266265 # are cached, so any hit here is safe to suppress without re-
267266 # checking has_qu_question.
268- key = (data , sender_scope ) if sender_scope else data
269- recent_time = self . _recent_packets . get ( key , - 1e30 )
270- if (now - _DUPLICATE_PACKET_SUPPRESSION_INTERVAL ) < recent_time :
267+ recent = self . _recent_packets . get (data , _NOT_SEEN )
268+ recent_time = recent [ 0 ]
269+ if (now - _DUPLICATE_PACKET_SUPPRESSION_INTERVAL ) < recent_time and ( recent [ 1 ] or not scoped ) :
271270 # No timestamp refresh on hit so the suppression window
272271 # ends at first-observation + interval; one parse-and-
273272 # dispatch fires per payload per interval, capping the
@@ -303,16 +302,16 @@ def _process_datagram_at_time(
303302 self .data = data
304303 self .last_time = now
305304 self .last_message = msg
306- self .last_sender_scope = sender_scope
305+ self ._last_scoped = scoped
307306 if not msg .has_qu_question ():
308307 # Refresh LRU position when an entry exists but the
309308 # suppression window has expired; otherwise evict the oldest
310309 # entry once the window is full.
311- if key in self . _recent_packets :
312- del self ._recent_packets [key ]
310+ if recent is not _NOT_SEEN :
311+ del self ._recent_packets [data ]
313312 elif len (self ._recent_packets ) >= _RECENT_PACKETS_MAX :
314313 del self ._recent_packets [next (iter (self ._recent_packets ))]
315- self ._recent_packets [key ] = now
314+ self ._recent_packets [data ] = ( now , scoped )
316315 if msg .valid is True :
317316 if debug :
318317 log .debug (
0 commit comments