Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit fbaaf7b

Browse filesBrowse files
authored
feat: speed up processing incoming data (#1167)
1 parent 1431517 commit fbaaf7b
Copy full SHA for fbaaf7b

File tree

2 files changed

+19
-20
lines changed
Filter options

2 files changed

+19
-20
lines changed

‎src/zeroconf/_protocol/incoming.pxd

Copy file name to clipboardExpand all lines: src/zeroconf/_protocol/incoming.pxd
+1-6Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ cdef class DNSIncoming:
5454
cdef unsigned int _data_len
5555
cdef public cython.dict name_cache
5656
cdef public cython.list questions
57-
cdef object _answers
57+
cdef cython.list _answers
5858
cdef public object id
5959
cdef public cython.uint num_questions
6060
cdef public cython.uint num_answers
@@ -78,8 +78,6 @@ cdef class DNSIncoming:
7878

7979
cdef _initial_parse(self)
8080

81-
cdef _unpack(self, object unpacker, object length)
82-
8381
@cython.locals(
8482
end=cython.uint,
8583
length=cython.uint
@@ -88,9 +86,6 @@ cdef class DNSIncoming:
8886

8987
cdef _read_questions(self)
9088

91-
@cython.locals(
92-
length=cython.uint
93-
)
9489
cdef bytes _read_character_string(self)
9590

9691
cdef _read_string(self, unsigned int length)

‎src/zeroconf/_protocol/incoming.py

Copy file name to clipboardExpand all lines: src/zeroconf/_protocol/incoming.py
+18-14Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
import struct
2424
import sys
25-
from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Union, cast
25+
from typing import Any, Dict, List, Optional, Set, Tuple, Union
2626

2727
from .._dns import (
2828
DNSAddress,
@@ -194,10 +194,6 @@ def __repr__(self) -> str:
194194
]
195195
)
196196

197-
def _unpack(self, unpacker: Callable[[bytes, int], tuple], length: int) -> tuple:
198-
self.offset += length
199-
return unpacker(self.data, self.offset - length)
200-
201197
def _read_header(self) -> None:
202198
"""Reads header portion of packet"""
203199
(
@@ -207,7 +203,8 @@ def _read_header(self) -> None:
207203
self.num_answers,
208204
self.num_authorities,
209205
self.num_additionals,
210-
) = self._unpack(UNPACK_6H, 12)
206+
) = UNPACK_6H(self.data)
207+
self.offset += 12
211208

212209
def _read_questions(self) -> None:
213210
"""Reads questions section of packet"""
@@ -264,18 +261,24 @@ def _read_record(
264261
) -> Optional[DNSRecord]:
265262
"""Read known records types and skip unknown ones."""
266263
if type_ == _TYPE_A:
267-
return DNSAddress(domain, type_, class_, ttl, self._read_string(4), created=self.now)
264+
dns_address = DNSAddress(domain, type_, class_, ttl, self._read_string(4))
265+
dns_address.created = self.now
266+
return dns_address
268267
if type_ in (_TYPE_CNAME, _TYPE_PTR):
269268
return DNSPointer(domain, type_, class_, ttl, self._read_name(), self.now)
270269
if type_ == _TYPE_TXT:
271270
return DNSText(domain, type_, class_, ttl, self._read_string(length), self.now)
272271
if type_ == _TYPE_SRV:
272+
priority, weight, port = UNPACK_3H(self.data, self.offset)
273+
self.offset += 6
273274
return DNSService(
274275
domain,
275276
type_,
276277
class_,
277278
ttl,
278-
*cast(Tuple[int, int, int], self._unpack(UNPACK_3H, 6)),
279+
priority,
280+
weight,
281+
port,
279282
self._read_name(),
280283
self.now,
281284
)
@@ -285,14 +288,15 @@ def _read_record(
285288
type_,
286289
class_,
287290
ttl,
288-
self._read_character_string().decode('utf-8'),
289-
self._read_character_string().decode('utf-8'),
291+
self._read_character_string().decode('utf-8', 'replace'),
292+
self._read_character_string().decode('utf-8', 'replace'),
290293
self.now,
291294
)
292295
if type_ == _TYPE_AAAA:
293-
return DNSAddress(
294-
domain, type_, class_, ttl, self._read_string(16), created=self.now, scope_id=self.scope_id
295-
)
296+
dns_address = DNSAddress(domain, type_, class_, ttl, self._read_string(16))
297+
dns_address.created = self.now
298+
dns_address.scope_id = self.scope_id
299+
return dns_address
296300
if type_ == _TYPE_NSEC:
297301
name_start = self.offset
298302
return DNSNsec(
@@ -384,4 +388,4 @@ def _decode_labels_at_offset(self, off: int, labels: List[str], seen_pointers: S
384388
)
385389
return off + DNS_COMPRESSION_POINTER_LEN
386390

387-
raise IncomingDecodeError("Corrupt packet received while decoding name from {self.source}")
391+
raise IncomingDecodeError(f"Corrupt packet received while decoding name from {self.source}")

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.