Skip to content

Commit ef34152

Browse files
committed
Limit duplicate packet suppression to 1s intervals
- Only suppress duplicate packets that happen within the same second. Legitimate queriers will retry the question if they are suppressed. The limit was reduced to one second to be in line with rfc6762: To protect the network against excessive packet flooding due to software bugs or malicious attack, a Multicast DNS responder MUST NOT (except in the one special case of answering probe queries) multicast a record on a given interface until at least one second has elapsed since the last time that record was multicast on that particular
1 parent 937be52 commit ef34152

3 files changed

Lines changed: 33 additions & 6 deletions

File tree

‎tests/test_core.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,3 +696,21 @@ def test_guard_against_oversized_packets():
696696
)
697697

698698
zc.close()
699+
700+
701+
def test_guard_against_duplicate_packets():
702+
"""Ensure we do not process duplicate packets.
703+
These packets can quickly overwhelm the system.
704+
"""
705+
zc = Zeroconf(interfaces=['127.0.0.1'])
706+
listener = _core.AsyncListener(zc)
707+
assert listener.suppress_duplicate_packet(b"first packet", current_time_millis()) is False
708+
assert listener.suppress_duplicate_packet(b"first packet", current_time_millis()) is True
709+
assert listener.suppress_duplicate_packet(b"first packet", current_time_millis()) is True
710+
assert listener.suppress_duplicate_packet(b"first packet", current_time_millis() + 1000) is False
711+
assert listener.suppress_duplicate_packet(b"first packet", current_time_millis()) is True
712+
assert listener.suppress_duplicate_packet(b"other packet", current_time_millis()) is False
713+
assert listener.suppress_duplicate_packet(b"other packet", current_time_millis()) is True
714+
assert listener.suppress_duplicate_packet(b"other packet", current_time_millis() + 1000) is False
715+
assert listener.suppress_duplicate_packet(b"first packet", current_time_millis()) is False
716+
zc.close()

‎zeroconf/_core.py‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,22 @@ class AsyncListener(asyncio.Protocol, QuietLogger):
186186
def __init__(self, zc: 'Zeroconf') -> None:
187187
self.zc = zc
188188
self.data: Optional[bytes] = None
189+
self.last_time: float = 0
189190
self.transport: Optional[asyncio.DatagramTransport] = None
190191

191192
self._deferred: Dict[str, List[DNSIncoming]] = {}
192193
self._timers: Dict[str, asyncio.TimerHandle] = {}
193194

194195
super().__init__()
195196

197+
def suppress_duplicate_packet(self, data: bytes, now: float) -> bool:
198+
"""Suppress duplicate packet if the last one was the same in the last second."""
199+
if self.data == data and (now - 1000) < self.last_time:
200+
return True
201+
self.data = data
202+
self.last_time = now
203+
return False
204+
196205
def datagram_received(
197206
self, data: bytes, addrs: Union[Tuple[str, int], Tuple[str, int, int, int]]
198207
) -> None:
@@ -210,7 +219,9 @@ def datagram_received(
210219
else:
211220
return
212221

213-
if self.data == data:
222+
now = current_time_millis()
223+
if self.suppress_duplicate_packet(data, now):
224+
# Guard against duplicate packets
214225
log.debug(
215226
'Ignoring duplicate message received from %r:%r (socket %d) (%d bytes) as [%r]',
216227
addr,
@@ -221,8 +232,6 @@ def datagram_received(
221232
)
222233
return
223234

224-
self.data = data
225-
226235
if len(data) > _MAX_MSG_ABSOLUTE:
227236
# Guard against oversized packets to ensure bad implementations cannot overwhelm
228237
# the system.
@@ -234,7 +243,7 @@ def datagram_received(
234243
)
235244
return
236245

237-
msg = DNSIncoming(data, scope)
246+
msg = DNSIncoming(data, scope, now)
238247
if msg.valid:
239248
log.debug(
240249
'Received from %r:%r (socket %d): %r (%d bytes) as [%r]',

‎zeroconf/_protocol.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class DNSIncoming(DNSMessage, QuietLogger):
7979

8080
"""Object representation of an incoming DNS packet"""
8181

82-
def __init__(self, data: bytes, scope_id: Optional[int] = None) -> None:
82+
def __init__(self, data: bytes, scope_id: Optional[int] = None, now: Optional[float] = None) -> None:
8383
"""Constructor from string holding bytes of packet"""
8484
super().__init__(0)
8585
self.offset = 0
@@ -92,7 +92,7 @@ def __init__(self, data: bytes, scope_id: Optional[int] = None) -> None:
9292
self.num_authorities = 0
9393
self.num_additionals = 0
9494
self.valid = False
95-
self.now = current_time_millis()
95+
self.now = now or current_time_millis()
9696
self.scope_id = scope_id
9797

9898
try:

0 commit comments

Comments
 (0)