Skip to content

Commit aba80d4

Browse files
committed
feat: make ServiceInfo aware of question history
fixes #1337
1 parent cf40470 commit aba80d4

5 files changed

Lines changed: 63 additions & 40 deletions

File tree

‎src/zeroconf/_protocol/outgoing.pxd‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11

22
import cython
33

4-
from .._cache cimport DNSCache
54
from .._dns cimport DNSEntry, DNSPointer, DNSQuestion, DNSRecord
65
from .incoming cimport DNSIncoming
76

@@ -127,10 +126,6 @@ cdef class DNSOutgoing:
127126
)
128127
cpdef packets(self)
129128

130-
cpdef add_question_or_all_cache(self, DNSCache cache, double now, str name, object type_, object class_)
131-
132-
cpdef add_question_or_one_cache(self, DNSCache cache, double now, str name, object type_, object class_)
133-
134129
cpdef add_question(self, DNSQuestion question)
135130

136131
cpdef add_answer(self, DNSIncoming inp, DNSRecord record)

‎src/zeroconf/_protocol/outgoing.py‎

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
from struct import Struct
2626
from typing import TYPE_CHECKING, Dict, List, Optional, Sequence, Tuple, Union
2727

28-
from .._cache import DNSCache
2928
from .._dns import DNSPointer, DNSQuestion, DNSRecord
3029
from .._exceptions import NamePartTooLongException
3130
from .._logger import log
@@ -198,29 +197,6 @@ def add_additional_answer(self, record: DNSRecord) -> None:
198197
"""
199198
self.additionals.append(record)
200199

201-
def add_question_or_one_cache(
202-
self, cache: DNSCache, now: float_, name: str_, type_: int_, class_: int_
203-
) -> None:
204-
"""Add a question if it is not already cached."""
205-
cached_entry = cache.get_by_details(name, type_, class_)
206-
if not cached_entry:
207-
self.add_question(DNSQuestion(name, type_, class_))
208-
else:
209-
self.add_answer_at_time(cached_entry, now)
210-
211-
def add_question_or_all_cache(
212-
self, cache: DNSCache, now: float_, name: str_, type_: int_, class_: int_
213-
) -> None:
214-
"""Add a question if it is not already cached.
215-
This is currently only used for IPv6 addresses.
216-
"""
217-
cached_entries = cache.get_all_by_details(name, type_, class_)
218-
if not cached_entries:
219-
self.add_question(DNSQuestion(name, type_, class_))
220-
return
221-
for cached_entry in cached_entries:
222-
self.add_answer_at_time(cached_entry, now)
223-
224200
def _write_byte(self, value: int_) -> None:
225201
"""Writes a single byte to the packet"""
226202
self.data.append(BYTE_TABLE[value])

‎src/zeroconf/_services/info.pxd‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@
22
import cython
33

44
from .._cache cimport DNSCache
5-
from .._dns cimport DNSAddress, DNSNsec, DNSPointer, DNSRecord, DNSService, DNSText
5+
from .._dns cimport (
6+
DNSAddress,
7+
DNSNsec,
8+
DNSPointer,
9+
DNSQuestion,
10+
DNSRecord,
11+
DNSService,
12+
DNSText,
13+
)
14+
from .._history cimport QuestionHistory
615
from .._protocol.outgoing cimport DNSOutgoing
716
from .._record_update cimport RecordUpdate
817
from .._updates cimport RecordUpdateListener
@@ -123,5 +132,18 @@ cdef class ServiceInfo(RecordUpdateListener):
123132

124133
cpdef void async_clear_cache(self)
125134

126-
@cython.locals(cache=DNSCache)
135+
@cython.locals(cache=DNSCache, question_history=QuestionHistory, out=DNSOutgoing)
127136
cdef DNSOutgoing _generate_request_query(self, object zc, double now, object question_type)
137+
138+
@cython.locals(question=DNSQuestion)
139+
cdef void _add_question_with_known_answers(
140+
self,
141+
DNSOutgoing out,
142+
object question_type,
143+
QuestionHistory question_history,
144+
DNSCache cache,
145+
double now,
146+
str name,
147+
object type_,
148+
object class_
149+
)

‎src/zeroconf/_services/info.py‎

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,19 @@
2626
from ipaddress import IPv4Address, IPv6Address, _BaseAddress
2727
from typing import TYPE_CHECKING, Dict, List, Optional, Set, Union, cast
2828

29+
from .._cache import DNSCache
2930
from .._dns import (
3031
DNSAddress,
3132
DNSNsec,
3233
DNSPointer,
34+
DNSQuestion,
3335
DNSQuestionType,
3436
DNSRecord,
3537
DNSService,
3638
DNSText,
3739
)
3840
from .._exceptions import BadTypeInNameException
41+
from .._history import QuestionHistory
3942
from .._logger import log
4043
from .._protocol.outgoing import DNSOutgoing
4144
from .._record_update import RecordUpdate
@@ -89,6 +92,7 @@
8992
bytes_ = bytes
9093
float_ = float
9194
int_ = int
95+
str_ = str
9296

9397
DNS_QUESTION_TYPE_QU = DNSQuestionType.QU
9498
DNS_QUESTION_TYPE_QM = DNSQuestionType.QM
@@ -823,7 +827,6 @@ async def async_request(
823827
return self._load_from_cache(zc, now)
824828
zc.async_send(out, addr, port)
825829
next_ = now + delay
826-
delay *= 2
827830
next_ += random.randint(*_AVOID_SYNC_DELAY_RANDOM_INTERVAL)
828831

829832
await self.async_wait(min(next_, last) - now, zc.loop)
@@ -833,6 +836,27 @@ async def async_request(
833836

834837
return True
835838

839+
def _add_question_with_known_answers(
840+
self,
841+
out: DNSOutgoing,
842+
question_type: DNSQuestionType,
843+
question_history: QuestionHistory,
844+
cache: DNSCache,
845+
now: float_,
846+
name: str_,
847+
type_: int_,
848+
class_: int_,
849+
) -> None:
850+
"""Add a question with known answers if its not suppressed."""
851+
known_answers = cache.get_all_by_details(name, type_, class_)
852+
question = DNSQuestion(name, type_, class_)
853+
if not question_history.suppresses(question, now, set(known_answers)):
854+
if question_type is DNS_QUESTION_TYPE_QU:
855+
question.unicast = True
856+
out.add_question(question)
857+
for answer in known_answers:
858+
out.add_answer_at_time(answer, now)
859+
836860
def _generate_request_query(
837861
self, zc: 'Zeroconf', now: float_, question_type: DNSQuestionType
838862
) -> DNSOutgoing:
@@ -841,13 +865,19 @@ def _generate_request_query(
841865
name = self._name
842866
server_or_name = self.server or name
843867
cache = zc.cache
844-
out.add_question_or_one_cache(cache, now, name, _TYPE_SRV, _CLASS_IN)
845-
out.add_question_or_one_cache(cache, now, name, _TYPE_TXT, _CLASS_IN)
846-
out.add_question_or_all_cache(cache, now, server_or_name, _TYPE_A, _CLASS_IN)
847-
out.add_question_or_all_cache(cache, now, server_or_name, _TYPE_AAAA, _CLASS_IN)
848-
if question_type is DNS_QUESTION_TYPE_QU:
849-
for question in out.questions:
850-
question.unicast = True
868+
question_history = zc.question_history
869+
self._add_question_with_known_answers(
870+
out, question_type, question_history, cache, now, name, _TYPE_SRV, _CLASS_IN
871+
)
872+
self._add_question_with_known_answers(
873+
out, question_type, question_history, cache, now, name, _TYPE_TXT, _CLASS_IN
874+
)
875+
self._add_question_with_known_answers(
876+
out, question_type, question_history, cache, now, server_or_name, _TYPE_A, _CLASS_IN
877+
)
878+
self._add_question_with_known_answers(
879+
out, question_type, question_history, cache, now, server_or_name, _TYPE_AAAA, _CLASS_IN
880+
)
851881
return out
852882

853883
def __repr__(self) -> str:

‎src/zeroconf/const.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
_UNREGISTER_TIME = 125 # ms
2929
_CHECK_TIME = 175 # ms
3030
_REGISTER_TIME = 225 # ms
31-
_LISTENER_TIME = 200 # ms
31+
_LISTENER_TIME = 1000 # ms # must be at least _DUPLICATE_PACKET_SUPPRESSION_INTERVAL
3232
_BROWSER_TIME = 10000 # ms
3333
_DUPLICATE_PACKET_SUPPRESSION_INTERVAL = 1000 # ms
3434
_DUPLICATE_QUESTION_INTERVAL = 999 # ms # Must be 1ms less than _DUPLICATE_PACKET_SUPPRESSION_INTERVAL

0 commit comments

Comments
 (0)