Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def build(setup_kwargs: Any) -> None:
dict(
ext_modules=cythonize(
[
"src/zeroconf/_cache.py",
"src/zeroconf/_dns.py",
"src/zeroconf/_cache.py",
"src/zeroconf/_protocol/incoming.py",
"src/zeroconf/_protocol/outgoing.py",
],
Expand Down
16 changes: 15 additions & 1 deletion src/zeroconf/_protocol/outgoing.pxd
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

import cython

from .._dns cimport DNSEntry, DNSQuestion, DNSRecord
from .incoming cimport DNSIncoming


Expand Down Expand Up @@ -41,7 +42,14 @@ cdef class DNSOutgoing:

cdef _write_int(self, object value)

cdef _write_question(self, object question)
cdef _write_question(self, DNSQuestion question)

@cython.locals(
d=cython.bytes,
data_view=cython.list,
length=cython.uint
)
cdef _write_record(self, DNSRecord record, object now)

cdef _write_record_class(self, object record)

Expand All @@ -55,6 +63,12 @@ cdef class DNSOutgoing:

cdef _has_more_to_add(self, object questions_offset, object answer_offset, object authority_offset, object additional_offset)

cdef _write_ttl(self, DNSRecord record, object now)

cpdef write_name(self, object name)

cpdef write_short(self, object value)

@cython.locals(
questions_offset=cython.uint,
answer_offset=cython.uint,
Expand Down
19 changes: 13 additions & 6 deletions src/zeroconf/_protocol/outgoing.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
)
from .incoming import DNSIncoming

str_ = str
float_ = float
DNSQuestion_ = DNSQuestion
DNSRecord_ = DNSRecord


class State(enum.Enum):
init = 0
Expand Down Expand Up @@ -238,7 +243,7 @@ def write_character_string(self, value: bytes) -> None:
self._write_byte(length)
self.write_string(value)

def write_name(self, name: str) -> None:
def write_name(self, name: str_) -> None:
"""
Write names to packet

Expand Down Expand Up @@ -276,26 +281,26 @@ def write_name(self, name: str) -> None:
# this is the end of a name
self._write_byte(0)

def _write_question(self, question: DNSQuestion) -> bool:
def _write_question(self, question: DNSQuestion_) -> bool:
"""Writes a question to the packet"""
start_data_length, start_size = len(self.data), self.size
self.write_name(question.name)
self.write_short(question.type)
self._write_record_class(question)
return self._check_data_limit_or_rollback(start_data_length, start_size)

def _write_record_class(self, record: Union[DNSQuestion, DNSRecord]) -> None:
def _write_record_class(self, record: Union[DNSQuestion_, DNSRecord_]) -> None:
"""Write out the record class including the unique/unicast (QU) bit."""
if record.unique and self.multicast:
self.write_short(record.class_ | _CLASS_UNIQUE)
else:
self.write_short(record.class_)

def _write_ttl(self, record: DNSRecord, now: float) -> None:
def _write_ttl(self, record: DNSRecord_, now: float_) -> None:
"""Write out the record ttl."""
self._write_int(record.ttl if now == 0 else record.get_remaining_ttl(now))

def _write_record(self, record: DNSRecord, now: float) -> bool:
def _write_record(self, record: DNSRecord_, now: float_) -> bool:
"""Writes a record (answer, authoritative answer, additional) to
the packet. Returns True on success, or False if we did not
because the packet because the record does not fit."""
Expand All @@ -308,7 +313,9 @@ def _write_record(self, record: DNSRecord, now: float) -> bool:
self.write_short(0) # Will get replaced with the actual size
record.write(self)
# Adjust size for the short we will write before this record
length = sum(len(d) for d in self.data[index + 1 :])
length = 0
for d in self.data[index + 1 :]:
length += len(d)
# Here we replace the 0 length short we wrote
# before with the actual length
self._replace_short(index, length)
Expand Down