From f927190cb24f70fd7c825c6e12151fcc0daf3973 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 1 May 2023 08:17:21 -0500 Subject: [PATCH] feat: speed up decoding dns questions when processing incoming data (#1168) --- src/zeroconf/_protocol/incoming.pxd | 5 +++-- src/zeroconf/_protocol/incoming.py | 9 ++++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/zeroconf/_protocol/incoming.pxd b/src/zeroconf/_protocol/incoming.pxd index 71d70536..d5620692 100644 --- a/src/zeroconf/_protocol/incoming.pxd +++ b/src/zeroconf/_protocol/incoming.pxd @@ -38,6 +38,7 @@ from .._dns cimport ( DNSHinfo, DNSNsec, DNSPointer, + DNSQuestion, DNSRecord, DNSService, DNSText, @@ -48,11 +49,11 @@ cdef class DNSIncoming: cdef bint _did_read_others cdef public unsigned int flags - cdef object offset + cdef cython.uint offset cdef public bytes data cdef unsigned int _data_len cdef public cython.dict name_cache - cdef public object questions + cdef public cython.list questions cdef object _answers cdef public object id cdef public cython.uint num_questions diff --git a/src/zeroconf/_protocol/incoming.py b/src/zeroconf/_protocol/incoming.py index 9c0c39a2..9505f466 100644 --- a/src/zeroconf/_protocol/incoming.py +++ b/src/zeroconf/_protocol/incoming.py @@ -211,9 +211,12 @@ def _read_header(self) -> None: def _read_questions(self) -> None: """Reads questions section of packet""" - self.questions = [ - DNSQuestion(self._read_name(), *self._unpack(UNPACK_HH, 4)) for _ in range(self.num_questions) - ] + for _ in range(self.num_questions): + name = self._read_name() + type_, class_ = UNPACK_HH(self.data, self.offset) + self.offset += 4 + question = DNSQuestion(name, type_, class_) + self.questions.append(question) def _read_character_string(self) -> bytes: """Reads a character string from the packet"""