3131from ._dns import DNSQuestion , DNSQuestionType
3232from ._engine import AsyncEngine
3333from ._exceptions import NonUniqueNameException , NotRunningException
34- from ._handlers .answers import (
35- construct_outgoing_multicast_answers ,
36- construct_outgoing_unicast_answers ,
37- )
3834from ._handlers .multicast_outgoing_queue import MulticastOutgoingQueue
3935from ._handlers .query_handler import QueryHandler
4036from ._handlers .record_manager import RecordManager
10298
10399_REGISTER_BROADCASTS = 3
104100
105- _str = str
106- _int = int
107- _bytes = bytes
108-
109101
110102def async_send_with_transport (
111103 log_debug : bool ,
112104 transport : _WrappedTransport ,
113- packet : _bytes ,
114- packet_num : _int ,
105+ packet : bytes ,
106+ packet_num : int ,
115107 out : DNSOutgoing ,
116- addr : Optional [_str ],
117- port : _int ,
118- v6_flow_scope : Union [Tuple [()], Tuple [int , int ]],
108+ addr : Optional [str ],
109+ port : int ,
110+ v6_flow_scope : Union [Tuple [()], Tuple [int , int ]] = () ,
119111) -> None :
120112 ipv6_socket = transport .is_ipv6
121113 if addr is None :
@@ -144,7 +136,7 @@ def async_send_with_transport(
144136 transport .transport .sendto (packet , (real_addr , port or _MDNS_PORT , * v6_flow_scope ))
145137
146138
147- class Zeroconf :
139+ class Zeroconf ( QuietLogger ) :
148140
149141 """Implementation of Zeroconf Multicast DNS Service Discovery
150142
@@ -191,15 +183,15 @@ def __init__(
191183 self .registry = ServiceRegistry ()
192184 self .cache = DNSCache ()
193185 self .question_history = QuestionHistory ()
194- self .query_handler = QueryHandler (self . registry , self . cache , self . question_history )
186+ self .query_handler = QueryHandler (self )
195187 self .record_manager = RecordManager (self )
196188
197189 self ._notify_futures : Set [asyncio .Future ] = set ()
198190 self .loop : Optional [asyncio .AbstractEventLoop ] = None
199191 self ._loop_thread : Optional [threading .Thread ] = None
200192
201- self ._out_queue = MulticastOutgoingQueue (self , 0 , _AGGREGATION_DELAY )
202- self ._out_delay_queue = MulticastOutgoingQueue (self , _ONE_SECOND , _PROTECTED_AGGREGATION_DELAY )
193+ self .out_queue = MulticastOutgoingQueue (self , 0 , _AGGREGATION_DELAY )
194+ self .out_delay_queue = MulticastOutgoingQueue (self , _ONE_SECOND , _PROTECTED_AGGREGATION_DELAY )
203195
204196 self .start ()
205197
@@ -565,45 +557,11 @@ def async_remove_listener(self, listener: RecordUpdateListener) -> None:
565557 """
566558 self .record_manager .async_remove_listener (listener )
567559
568- def handle_assembled_query (
569- self ,
570- packets : List [DNSIncoming ],
571- addr : _str ,
572- port : _int ,
573- transport : _WrappedTransport ,
574- v6_flow_scope : Union [Tuple [()], Tuple [int , int ]],
575- ) -> None :
576- """Respond to a (re)assembled query.
577-
578- If the protocol received packets with the TC bit set, it will
579- wait a bit for the rest of the packets and only call
580- handle_assembled_query once it has a complete set of packets
581- or the timer expires. If the TC bit is not set, a single
582- packet will be in packets.
583- """
584- ucast_source = port != _MDNS_PORT
585- question_answers = self .query_handler .async_response (packets , ucast_source )
586- if not question_answers :
587- return
588- first_packet = packets [0 ]
589- now = first_packet .now
590- if question_answers .ucast :
591- questions = first_packet .questions
592- id_ = first_packet .id
593- out = construct_outgoing_unicast_answers (question_answers .ucast , ucast_source , questions , id_ )
594- # When sending unicast, only send back the reply
595- # via the same socket that it was recieved from
596- # as we know its reachable from that socket
597- self .async_send (out , addr , port , v6_flow_scope , transport )
598- if question_answers .mcast_now :
599- self .async_send (construct_outgoing_multicast_answers (question_answers .mcast_now ))
600- if question_answers .mcast_aggregate :
601- self ._out_queue .async_add (now , question_answers .mcast_aggregate )
602- if question_answers .mcast_aggregate_last_second :
603- # https://datatracker.ietf.org/doc/html/rfc6762#section-14
604- # If we broadcast it in the last second, we have to delay
605- # at least a second before we send it again
606- self ._out_delay_queue .async_add (now , question_answers .mcast_aggregate_last_second )
560+ def handle_response (self , msg : DNSIncoming ) -> None :
561+ """Deal with incoming response packets. All answers
562+ are held in the cache, and listeners are notified."""
563+ self .log_warning_once ("handle_response is deprecated, use record_manager.async_updates_from_response" )
564+ self .record_manager .async_updates_from_response (msg )
607565
608566 def send (
609567 self ,
@@ -617,9 +575,6 @@ def send(
617575 assert self .loop is not None
618576 self .loop .call_soon_threadsafe (self .async_send , out , addr , port , v6_flow_scope , transport )
619577
620- def _debug_enabled (self ) -> bool :
621- return log .isEnabledFor (logging .DEBUG )
622-
623578 def async_send (
624579 self ,
625580 out : DNSOutgoing ,
@@ -635,14 +590,11 @@ def async_send(
635590 # If no transport is specified, we send to all the ones
636591 # with the same address family
637592 transports = [transport ] if transport else self .engine .senders
638- log_debug = self ._debug_enabled ()
639- max_size = _MAX_MSG_ABSOLUTE
593+ log_debug = log .isEnabledFor (logging .DEBUG )
640594
641595 for packet_num , packet in enumerate (out .packets ()):
642- if len (packet ) > max_size :
643- QuietLogger .log_warning_once (
644- "Dropping %r over-sized packet (%d bytes) %r" , out , len (packet ), packet
645- )
596+ if len (packet ) > _MAX_MSG_ABSOLUTE :
597+ self .log_warning_once ("Dropping %r over-sized packet (%d bytes) %r" , out , len (packet ), packet )
646598 return
647599 for send_transport in transports :
648600 async_send_with_transport (
0 commit comments