Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: speed up parsing NSEC records #1169

Merged
merged 1 commit into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 19 additions & 1 deletion bench/incoming.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@
import timeit
from typing import List

from zeroconf import DNSAddress, DNSIncoming, DNSOutgoing, DNSService, DNSText, const
from zeroconf import (
DNSAddress,
DNSIncoming,
DNSNsec,
DNSOutgoing,
DNSService,
DNSText,
const,
)


def generate_packets() -> List[bytes]:
Expand Down Expand Up @@ -150,6 +158,16 @@ def generate_packets() -> List[bytes]:
record["address"], # type: ignore
)
)
out.add_additional_answer(
DNSNsec(
record["name"], # type: ignore
const._TYPE_NSEC,
const._CLASS_IN | const._CLASS_UNIQUE,
const._DNS_OTHER_TTL,
record["name"], # type: ignore
[const._TYPE_TXT, const._TYPE_SRV],
)
)

return out.packets()

Expand Down
10 changes: 10 additions & 0 deletions src/zeroconf/_protocol/incoming.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ cdef class DNSIncoming:
)
cdef _read_record(self, object domain, unsigned int type_, object class_, object ttl, unsigned int length)

@cython.locals(
offset=cython.uint,
offset_plus_one=cython.uint,
offset_plus_two=cython.uint,
window=cython.uint,
bit=cython.uint,
byte=cython.uint,
i=cython.uint,
bitmap_length=cython.uint,
)
cdef _read_bitmap(self, unsigned int end)

cdef _read_name(self)
10 changes: 7 additions & 3 deletions src/zeroconf/_protocol/incoming.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,13 @@ def _read_bitmap(self, end: int) -> List[int]:
"""Reads an NSEC bitmap from the packet."""
rdtypes = []
while self.offset < end:
window = self.data[self.offset]
bitmap_length = self.data[self.offset + 1]
for i, byte in enumerate(self.data[self.offset + 2 : self.offset + 2 + bitmap_length]):
offset = self.offset
offset_plus_one = offset + 1
offset_plus_two = offset + 2
window = self.data[offset]
bitmap_length = self.data[offset_plus_one]
bitmap_end = offset_plus_two + bitmap_length
for i, byte in enumerate(self.data[offset_plus_two:bitmap_end]):
for bit in range(0, 8):
if byte & (0x80 >> bit):
rdtypes.append(bit + window * 256 + i * 8)
Expand Down