Skip to content

Commit cb4c3b2

Browse files
authored
feat: speed up incoming data parser (#1161)
1 parent 02e7432 commit cb4c3b2

4 files changed

Lines changed: 17 additions & 11 deletions

File tree

src/zeroconf/_dns.pxd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,4 @@ cdef class DNSRRSet:
105105
cpdef suppresses(self, DNSRecord record)
106106

107107
@cython.locals(lookup=cython.dict)
108-
cdef _get_lookup(self)
108+
cdef cython.dict _get_lookup(self)

src/zeroconf/_dns.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,4 +538,6 @@ def suppresses(self, record: _DNSRecord) -> bool:
538538
information held in this record."""
539539
lookup = self._get_lookup()
540540
other_ttl = lookup.get(record)
541-
return bool(other_ttl and other_ttl > (record.ttl / 2))
541+
if other_ttl is None:
542+
return False
543+
return other_ttl > (record.ttl / 2)

src/zeroconf/_protocol/incoming.pxd

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ cdef class DNSIncoming:
3535

3636
cdef bint _did_read_others
3737
cdef public unsigned int flags
38-
cdef unsigned int offset
38+
cdef object offset
3939
cdef public bytes data
4040
cdef unsigned int _data_len
41-
cdef public object name_cache
41+
cdef public cython.dict name_cache
4242
cdef public object questions
4343
cdef object _answers
4444
cdef public object id
@@ -55,9 +55,10 @@ cdef class DNSIncoming:
5555
off=cython.uint,
5656
label_idx=cython.uint,
5757
length=cython.uint,
58-
link=cython.uint
58+
link=cython.uint,
59+
link_data=cython.uint
5960
)
60-
cdef _decode_labels_at_offset(self, unsigned int off, cython.list labels, object seen_pointers)
61+
cdef _decode_labels_at_offset(self, unsigned int off, cython.list labels, cython.set seen_pointers)
6162

6263
cdef _read_header(self)
6364

src/zeroconf/_protocol/incoming.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,9 @@ def _decode_labels_at_offset(self, off: int, labels: List[str], seen_pointers: S
353353
)
354354

355355
# We have a DNS compression pointer
356-
link = (length & 0x3F) * 256 + self.data[off + 1]
356+
link_data = self.data[off + 1]
357+
link = (length & 0x3F) * 256 + link_data
358+
lint_int = int(link)
357359
if link > self._data_len:
358360
raise IncomingDecodeError(
359361
f"DNS compression pointer at {off} points to {link} beyond packet from {self.source}"
@@ -362,15 +364,16 @@ def _decode_labels_at_offset(self, off: int, labels: List[str], seen_pointers: S
362364
raise IncomingDecodeError(
363365
f"DNS compression pointer at {off} points to itself from {self.source}"
364366
)
365-
if link in seen_pointers:
367+
if lint_int in seen_pointers:
366368
raise IncomingDecodeError(
367369
f"DNS compression pointer at {off} was seen again from {self.source}"
368370
)
369-
linked_labels = self.name_cache.get(link, [])
371+
linked_labels = self.name_cache.get(lint_int)
370372
if not linked_labels:
371-
seen_pointers.add(link)
373+
linked_labels = []
374+
seen_pointers.add(lint_int)
372375
self._decode_labels_at_offset(link, linked_labels, seen_pointers)
373-
self.name_cache[link] = linked_labels
376+
self.name_cache[lint_int] = linked_labels
374377
labels.extend(linked_labels)
375378
if len(labels) > MAX_DNS_LABELS:
376379
raise IncomingDecodeError(

0 commit comments

Comments
 (0)