2626from ipaddress import IPv4Address , IPv6Address , _BaseAddress
2727from typing import TYPE_CHECKING , Dict , List , Optional , Set , Union , cast
2828
29+ from .._cache import DNSCache
2930from .._dns import (
3031 DNSAddress ,
3132 DNSNsec ,
3233 DNSPointer ,
34+ DNSQuestion ,
3335 DNSQuestionType ,
3436 DNSRecord ,
3537 DNSService ,
3638 DNSText ,
3739)
3840from .._exceptions import BadTypeInNameException
41+ from .._history import QuestionHistory
3942from .._logger import log
4043from .._protocol .outgoing import DNSOutgoing
4144from .._record_update import RecordUpdate
8992bytes_ = bytes
9093float_ = float
9194int_ = int
95+ str_ = str
9296
9397DNS_QUESTION_TYPE_QU = DNSQuestionType .QU
9498DNS_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 :
0 commit comments