Ë
    R/jÿF  ã                  ó~  — U d Z ddlmZ ddlmZmZ ddlmZmZ ddl	m
Z
mZmZmZmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$ ddl%m&Z&m'Z'm(Z(m)Z) e!e#he£Z*g Z+de,d<   g Z-de,d<   ej\                  Z/e0Z1e2Z3dZ4dZ5dZ6dZ7dZ8erddl9m:Z:  G d„ d«      Z; G d„ d«      Z< G d„ d«      Z=y)a‡  Multicast DNS Service Discovery for Python, v0.14-wmcbrine
Copyright 2003 Paul Scott-Murphy, 2014 William McBrine

This module provides a framework for the use of DNS Service Discovery
using IP multicast.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
USA
é    )Úannotations)ÚTYPE_CHECKINGÚcasté   )ÚDNSCacheÚ_UniqueRecordsType)Ú
DNSAddressÚ
DNSPointerÚDNSQuestionÚ	DNSRecordÚDNSRRSet)ÚDNSIncoming)ÚServiceInfo)Ú_WrappedTransport)Ú	IPVersion)Ú_ADDRESS_RECORD_TYPESÚ	_CLASS_INÚ_DNS_OTHER_TTLÚ
_MDNS_PORTÚ_ONE_SECONDÚ_SERVICE_TYPE_ENUMERATION_NAMEÚ_TYPE_AÚ
_TYPE_AAAAÚ	_TYPE_ANYÚ
_TYPE_NSECÚ	_TYPE_PTRÚ	_TYPE_SRVÚ	_TYPE_TXTé   )ÚQuestionAnswersÚ_AnswerWithAdditionalsTypeÚ$construct_outgoing_multicast_answersÚ"construct_outgoing_unicast_answersúlist[ServiceInfo]Ú_EMPTY_SERVICES_LISTú	list[str]Ú_EMPTY_TYPES_LISTé   é   )ÚZeroconfc                  ó,   — e Zd ZdZ	 	 	 	 	 	 	 	 	 	 dd„Zy)Ú_AnswerStrategy)ÚquestionÚservicesÚstrategy_typeÚtypesc                ó<   — || _         || _        || _        || _        y)zCreate an answer strategy.N)r-   r/   r0   r.   )Úselfr-   r/   r0   r.   s        úR/home/josh/.local/lib/python3.12/site-packages/zeroconf/_handlers/query_handler.pyÚ__init__z_AnswerStrategy.__init__N   s!   € ð !ˆŒØ*ˆÔØˆŒ
Ø ˆ�ó    N)
r-   r   r/   Ú_intr0   r&   r.   r$   ÚreturnÚNone)Ú__name__Ú
__module__Ú__qualname__Ú	__slots__r4   © r5   r3   r,   r,   K   s<   „ ØB€Ið!àð!ð ð!ð ð	!ð
 $ð!ð 
ô!r5   r,   c                  óP   — e Zd ZdZdZdd„Zdd„Zdd„Zdd„Z	 	 dd„Z	dd„Z
dd	„Zy
)Ú_QueryResponsez7A pair for unicast and multicast DNSOutgoing responses.)	Ú_additionalsÚ_cacheÚ	_is_probeÚ_mcast_aggregateÚ_mcast_aggregate_last_secondÚ
_mcast_nowÚ_nowÚ
_questionsÚ_ucastc                óÂ   — || _         || _        || _        || _        i | _        t        «       | _        t        «       | _        t        «       | _        t        «       | _	        y)zBuild a query response.N)
rB   rG   rF   rA   r@   ÚsetrH   rE   rC   rD   )r2   ÚcacheÚ	questionsÚis_probeÚnows        r3   r4   z_QueryResponse.__init__k   sL   € à!ˆŒØ#ˆŒØˆŒ	ØˆŒØ8:ˆÔÜ&)£eˆŒÜ*-«%ˆŒÜ03³ˆÔÜ<?»EˆÕ)r5   c                óJ  — |j                  «       D ]�  \  }}|| j                  |<   | j                  r| j                  j	                  |«       | j                  |«      s| j                  j	                  |«       Œi| j                  rŒv| j                  j	                  |«       Œ’ y)z,Generate a response to a multicast QU query.N)Úitemsr@   rB   rH   ÚaddÚ!_has_mcast_within_one_quarter_ttlrE   )r2   ÚanswersÚrecordÚadditionalss       r3   Úadd_qu_question_responsez'_QueryResponse.add_qu_question_responsew   sz   € à#*§=¡=£?ò 	(ÑˆF�KØ(3ˆD×Ñ˜fÑ%Ø�~Š~Ø—‘—‘ Ô'Ø×9Ñ9¸&ÔAØ—‘×#Ñ# FÕ+Ø—^“^Ø—‘—‘ Õ'ñ	(r5   c                óp   — | j                   j                  |«       | j                  j                  |«       y)z'Generate a response to a unicast query.N)r@   ÚupdaterH   )r2   rS   s     r3   Úadd_ucast_question_responsez*_QueryResponse.add_ucast_question_response‚   s(   € à×Ñ× Ñ  Ô)Ø�‰×Ñ˜7Õ#r5   c                óÒ  — | j                   j                  |«       |D ]Ç  }| j                  r| j                  j	                  |«       Œ+| j                  |«      r| j                  j	                  |«       ŒXt        | j                  «      dk(  r=| j                  d   }|j                  t        v r| j                  j	                  |«       Œ­| j                  j	                  |«       ŒÉ y)z)Generate a response to a multicast query.r   r   N)r@   rX   rB   rE   rQ   Ú _has_mcast_record_in_last_secondrD   ÚlenrG   ÚtypeÚ_RESPOND_IMMEDIATE_TYPESrC   )r2   rS   Úanswerr-   s       r3   Úadd_mcast_question_responsez*_QueryResponse.add_mcast_question_response‡   s»   € à×Ñ× Ñ  Ô)Øò 	.ˆFØ�~Š~Ø—‘×#Ñ# FÔ+Øà×4Ñ4°VÔ<Ø×1Ñ1×5Ñ5°fÔ=Øä�4—?‘?Ó# qÒ(ØŸ?™?¨1Ñ-�Ø—=‘=Ô$<Ñ<Ø—O‘O×'Ñ'¨Ô/Øà×!Ñ!×%Ñ% fÕ-ñ	.r5   c                ó~  — | j                   D �ci c]  }|| j                  |   “Œ }}| j                  D �ci c]  }|| j                  |   “Œ }}| j                  D �ci c]  }|| j                  |   “Œ }}| j                  D �ci c]  }|| j                  |   “Œ }}t        ||||«      S c c}w c c}w c c}w c c}w )z'Return answer sets that will be queued.)rH   r@   rE   rC   rD   r    )r2   ÚrÚucastÚ	mcast_nowÚmcast_aggregateÚmcast_aggregate_last_seconds         r3   rS   z_QueryResponse.answers›   sÅ   € ð 37·+±+Ö>¨Q��D×%Ñ% aÑ(Ñ(Ð>ˆÐ>Ø6:·o±oÖF°�Q˜×)Ñ)¨!Ñ,Ñ,ÐFˆ	ÐFØ<@×<QÑ<QÖR°q˜1˜d×/Ñ/°Ñ2Ñ2ÐRˆÐRØHL×HiÑHiÖ&jÀ1 q¨$×*;Ñ*;¸AÑ*>Ñ'>Ð&jÐ#Ð&jÜ˜u i°ÐB]Ó^Ð^ùò	 ?ùÚFùÚRùÚ&js   �B+¶B0ÁB5ÂB:c                ó¸   — t         rt        t        |«      }| j                  j	                  |«      }t        |duxr |j                  | j                  «      «      S )a  Check to see if a record has been mcasted recently.

        https://datatracker.ietf.org/doc/html/rfc6762#section-5.4
        When receiving a question with the unicast-response bit set, a
        responder SHOULD usually respond with a unicast packet directed back
        to the querier.  However, if the responder has not multicast that
        record recently (within one quarter of its TTL), then the responder
        SHOULD instead multicast the response so as to keep all the peer
        caches up to date
        N)r   r   r   rA   Úasync_get_uniqueÚboolÚ	is_recentrF   ©r2   rT   Úmaybe_entrys      r3   rR   z0_QueryResponse._has_mcast_within_one_quarter_ttl¥   sL   € õ ÜÔ,¨fÓ5ˆFØ—k‘k×2Ñ2°6Ó:ˆÜ�K tÐ+ÒP°×0EÑ0EÀdÇiÁiÓ0PÓQÐQr5   c                óÂ   — t         rt        t        |«      }| j                  j	                  |«      }t        |duxr  | j                  |j                  z
  t        k  «      S )z·Check if an answer was seen in the last second.
        Protect the network against excessive packet flooding
        https://datatracker.ietf.org/doc/html/rfc6762#section-14
        N)	r   r   r   rA   rh   ri   rF   Úcreatedr   rk   s      r3   r[   z/_QueryResponse._has_mcast_record_in_last_secondµ   sR   € õ
 ÜÔ,¨fÓ5ˆFØ—k‘k×2Ñ2°6Ó:ˆÜ�K tÐ+Ò]°·	±	¸K×<OÑ<OÑ0OÔR]Ñ0]Ó^Ð^r5   N)
rK   r   rL   zlist[DNSQuestion]rM   ri   rN   Úfloatr7   r8   )rS   r!   r7   r8   )r7   r    )rT   r   r7   ri   )r9   r:   r;   Ú__doc__r<   r4   rV   rY   r`   rS   rR   r[   r=   r5   r3   r?   r?   \   s<   „ ÙAð
€Ió
Bó	(ó$ó
.ð(_à	ó_óRô _r5   r?   c                  óÌ   — e Zd ZdZdZdd„Z	 	 	 	 	 	 	 	 dd„Z	 	 	 	 	 	 	 	 dd„Z	 	 	 	 	 	 	 	 	 	 dd„Z	 	 	 	 	 	 	 	 	 	 	 	 dd„Z		 	 	 	 	 	 dd„Z
	 	 	 	 dd	„Z	 	 	 	 	 	 	 	 	 	 	 	 dd
„Zy)ÚQueryHandlerzQuery the ServiceRegistry.)rK   Úout_delay_queueÚ	out_queueÚquestion_historyÚregistryÚzcc                ó¼   — || _         |j                  | _        |j                  | _        |j                  | _        |j                  | _        |j
                  | _        y)zInit the query handler.N)rw   rv   rK   ru   rt   rs   )r2   rw   s     r3   r4   zQueryHandler.__init__Ì   sF   € àˆŒØŸ™ˆŒØ—X‘XˆŒ
Ø "× 3Ñ 3ˆÔØŸ™ˆŒØ!×1Ñ1ˆÕr5   c           	     ó�   — |D ]A  }t        t        t        t        t        |d«      }|j                  |«      rŒ5t        «       ||<   ŒC y)z€Provide an answer to a service type enumeration query.

        https://datatracker.ietf.org/doc/html/rfc6763#section-9
        g        N)r
   r   r   r   r   Ú
suppressesrJ   )r2   r0   Ú
answer_setÚknown_answersÚstypeÚdns_pointers         r3   Ú+_add_service_type_enumeration_query_answersz8QueryHandler._add_service_type_enumeration_query_answersÕ   sK   € ð ò 
	0ˆEÜ$Ü.ÜÜÜØØóˆKð !×+Ñ+¨KÕ8Ü*-«%�
˜;Ò'ñ
	0r5   c                óÄ   — |D ][  }|j                  d«      }|j                  |«      rŒ&|j                  d«      |j                  d«      h|j	                  d«      £||<   Œ] y)zAnswer PTR/ANY question.N)Ú_dns_pointerrz   Ú_dns_serviceÚ	_dns_textÚ_get_address_and_nsec_records)r2   r.   r{   r|   Úservicer~   s         r3   Ú_add_pointer_answersz!QueryHandler._add_pointer_answersë   ss   € ð  ò 
	ˆGð "×.Ñ.¨tÓ4ˆKØ×'Ñ'¨Ô4Øà×$Ñ$ TÓ*Ø×!Ñ! $Ó'ð'ð ×6Ñ6°tÓ<ð'ˆJ�{Ò#ñ
	r5   c                ó`  — |D �](  }g }t        «       }t        «       }|j                  dt        «      D ]a  }	|j                  |	j                  «       |	j                  |k7  r|j                  |	«       Œ?|j                  |	«      rŒQ|j                  |	«       Œc t        |z
  }
|rL|
r=|j                  €J d«       ‚|j                  |j                  t        |
«      d«      «       |D ]  }|||<   Œ	 Œë||
v sŒð|j                  €J d«       ‚t        «       ||j                  t        |
«      d«      <   �Œ+ y)zAnswer A/AAAA/ANY question.Nz+Service server must be set for NSEC record.)rJ   Ú_dns_addressesÚ_IPVersion_ALLrQ   r]   rz   Úappendr   ÚserverÚ	_dns_nsecÚlist)r2   r.   r{   r|   Útype_r…   rS   rU   Ú
seen_typesÚdns_addressÚmissing_typesr_   s               r3   Ú_add_address_answersz!QueryHandler._add_address_answersþ   s%  € ð  ó 	QˆGØ(*ˆGÜ*-«%ˆKÜ#&£5ˆJØ&×5Ñ5°d¼NÓKò 0�Ø—‘˜{×/Ñ/Ô0Ø×#Ñ# uÒ,Ø—O‘O KÕ0Ø&×1Ñ1°+Õ>Ø—N‘N ;Õ/ð0ô '<¸jÑ&HˆMÙÙ Ø"Ÿ>™>Ð5ÐdÐ7dÓdÐ5Ø—O‘O G×$5Ñ$5´d¸=Ó6IÈ4Ó$PÔQØ%ò 5�FØ)4�J˜vÒ&ñ5à˜-Ò'Ø—~‘~Ð1Ð`Ð3`Ó`Ð1ÜKNË5�
˜7×,Ñ,¬T°-Ó-@À$ÓGÓHñ'	Qr5   c                óÚ  — i }|t         k(  r| j                  |||«       |S |t        k(  r| j                  |||«       |S |t        k(  r | j                  ||||j                  «       |S |t        k(  r=|d   }|j                  d«      }|j                  |«      s|j                  d«      ||<   |S |t        k(  r4|d   }|j                  d«      }	|j                  |	«      st        «       ||	<   |S )zAnswer a question.r   N)Ú)_ANSWER_STRATEGY_SERVICE_TYPE_ENUMERATIONr   Ú_ANSWER_STRATEGY_POINTERr†   Ú_ANSWER_STRATEGY_ADDRESSr’   r]   Ú_ANSWER_STRATEGY_SERVICEr‚   rz   r„   Ú_ANSWER_STRATEGY_TEXTrƒ   rJ   )
r2   r-   r/   r0   r.   r|   r{   r…   Údns_serviceÚdns_texts
             r3   Ú_answer_questionzQueryHandler._answer_question  s  € ð 24ˆ
àÔEÒEØ×<Ñ<¸UÀJÐP]Ô^ð$ Ðð# Ô6Ò6Ø×%Ñ% h°
¸MÔJð  Ðð Ô6Ò6Ø×%Ñ% h°
¸MÈ8Ï=É=ÔYð Ðð Ô6Ò6ð ˜q‘kˆGØ!×.Ñ.¨tÓ4ˆKØ ×+Ñ+¨KÔ8Ø*1×*OÑ*OÐPTÓ*U�
˜;Ñ'ð Ðð Ô3Ò3Ø˜q‘kˆGØ×(Ñ(¨Ó.ˆHØ ×+Ñ+¨HÔ5Ü'*£u�
˜8Ñ$àÐr5   c                ó(  — g }|D ]3  }|j                   D ]"  }|j                  | j                  |«      «       Œ$ Œ5 |syd}|d   }|j                   }g }|D ]4  }|j                  «       rd}Œ|j                  |j	                  «       «       Œ6 t        | j                  |||j                  «      }	t        |«      }
d}|j                  }|D ]¸  }|j                  }|j                  }|s/|€|
j                  «       }| j                  j                  |||«       | j                  ||j                  |j                   |j"                  |
«      }|s|r|	j%                  |«       Œ•|r|	j'                  |«       |	j)                  |«       Œº |	j	                  «       S )z¡Deal with incoming query packets. Provides a response if possible.

        This function must be run in the event loop as it is not
        threadsafe.
        NFr   T)rG   ÚextendÚ_get_answer_strategiesrM   rS   r?   rK   rN   r   r-   ÚuniqueÚ
lookup_setru   Úadd_question_at_timer›   r/   r0   r.   rV   rY   r`   )r2   ÚmsgsÚucast_sourceÚ
strategiesÚmsgr-   rM   rL   rS   Ú	query_resr|   Úknown_answers_setrN   ÚstrategyÚ
is_unicastr{   s                   r3   Úasync_responsezQueryHandler.async_response;  sŸ  € ð -/ˆ
Øò 	IˆCØŸN™Nò I�Ø×!Ñ! $×"=Ñ"=¸hÓ"GÕHñIð	Iñ ð àˆØ�1‰gˆØ—N‘Nˆ	ð $&ˆØò 	.ˆCØ�|‰|Œ~Ø‘à—‘˜sŸ{™{›}Õ-ð		.ô # 4§:¡:¨y¸(ÀCÇGÁGÓLˆ	Ü  Ó)ˆØ37ÐØ�g‰gˆØ"ò 	>ˆHØ×(Ñ(ˆHØ!Ÿ™ˆJÙØ$Ð,Ø(5×(@Ñ(@Ó(BÐ%Ø×%Ñ%×:Ñ:¸8ÀSÐJ[Ô\Ø×.Ñ.ØØ×&Ñ&Ø—‘Ø×!Ñ!ØóˆJñ  ¡JØ×2Ñ2°:Ô>ØÙØ×5Ñ5°jÔAð ×1Ñ1°*Õ=ð+	>ð. × Ñ Ó"Ð"r5   c                ó~  — |j                   }|j                  «       }|j                  }g }|t        k(  rL|t        k(  rC| j
                  j                  «       }|r%|j                  t        |t        |t        «      «       |S |t        t        fv rB| j
                  j                  |«      }|r%|j                  t        |t        t        |«      «       |t        t         t        fv rB| j
                  j#                  |«      }|r%|j                  t        |t$        t        |«      «       |t&        t(        t        fv r…| j
                  j+                  |«      }|�h|t&        t        fv r&|j                  t        |t,        t        |g«      «       |t(        t        fv r&|j                  t        |t.        t        |g«      «       |S )z(Collect strategies to answer a question.)ÚnameÚlowerr]   r   r   rv   Úasync_get_typesrŠ   r,   r”   r%   r   Úasync_get_infos_typer•   r'   r   r   Úasync_get_infos_serverr–   r   r   Úasync_get_info_namer—   r˜   )	r2   r-   r¬   Úquestion_lower_namerŽ   r¤   r0   r.   r…   s	            r3   rž   z#QueryHandler._get_answer_strategiesw  s›  € ð
 �}‰}ˆØ"Ÿj™j›lÐØ—‘ˆØ,.ˆ
à”IÒÐ"5Ô9WÒ"WØ—M‘M×1Ñ1Ó3ˆEÙØ×!Ñ!Ü#Ø ÜAØÜ,ó	ôð Ðà”Y¤	Ð*Ñ*Ø—}‘}×9Ñ9Ð:MÓNˆHÙØ×!Ñ!Ü# HÔ.FÔHYÐ[cÓdôð ”Wœj¬)Ð4Ñ4Ø—}‘}×;Ñ;Ð<OÓPˆHÙØ×!Ñ!Ü# HÔ.FÔHYÐ[cÓdôð ”Y¤	¬9Ð5Ñ5Ø—m‘m×7Ñ7Ð8KÓLˆGØÐ"ØœY¬	Ð2Ñ2Ø×%Ñ%Ü'Ø$Ü4Ü-Ø$˜Ió	ôð œY¬	Ð2Ñ2Ø×%Ñ%Ü'Ø$Ü1Ü-Ø$˜Ió	ôð Ðr5   c                óf  — |d   }|t         k7  }| j                  ||«      }|€y|j                  rO|j                  }	|j                  }
t        |j                  ||	|
«      }| j                  j                  |||||«       |j                  r.| j                  j                  t        |j                  «      «       |j                  r0| j                  j                  |j                  |j                  «       |j                  r1| j                  j                  |j                  |j                  «       yy)aW  Respond to a (re)assembled query.

        If the protocol received packets with the TC bit set, it will
        wait a bit for the rest of the packets and only call
        handle_assembled_query once it has a complete set of packets
        or the timer expires. If the TC bit is not set, a single
        packet will be in packets.
        r   N)r   rª   rc   rG   Úidr#   rw   Ú
async_sendrd   r"   re   rt   Ú	async_addrN   rf   rs   )r2   ÚpacketsÚaddrÚportÚ	transportÚv6_flow_scopeÚfirst_packetr£   Úquestion_answersrL   Úid_Úouts               r3   Úhandle_assembled_queryz#QueryHandler.handle_assembled_query´  s
  € ð  ˜q‘zˆØœzÑ)ˆØ×.Ñ.¨w¸ÓEÐØÐ#ØØ×!Ò!Ø$×/Ñ/ˆIØ—/‘/ˆCÜ4Ð5E×5KÑ5KÈ\Ð[dÐfiÓjˆCð �G‰G×Ñ˜s D¨$°¸yÔIØ×%Ò%Ø�G‰G×ÑÔCÐDT×D^ÑD^Ó_Ô`Ø×+Ò+Ø�N‰N×$Ñ$ \×%5Ñ%5Ð7G×7WÑ7WÔXØ×7Ò7ð × Ñ ×*Ñ*¨<×+;Ñ+;Ð=M×=iÑ=iÕjð	 8r5   N)rw   r*   r7   r8   )r0   r&   r{   r!   r|   r   r7   r8   )r.   r$   r{   r!   r|   r   r7   r8   )
r.   r$   r{   r!   r|   r   rŽ   r6   r7   r8   )r-   r   r/   r6   r0   r&   r.   r$   r|   r   r7   r!   )r¢   úlist[DNSIncoming]r£   ri   r7   zQuestionAnswers | None)r-   r   r7   zlist[_AnswerStrategy])r·   rÁ   r¸   Ú_strr¹   r6   rº   r   r»   ztuple[()] | tuple[int, int]r7   r8   )r9   r:   r;   rp   r<   r4   r   r†   r’   r›   rª   rž   rÀ   r=   r5   r3   rr   rr   À   s^  „ Ù$ð€Ió2ð0àð0ð /ð0ð  ð	0ð
 
ó0ð,à#ðð /ðð  ð	ð
 
óð&Qà#ðQð /ðQð  ð	Qð
 ðQð 
óQð:àðð ðð ð	ð
 $ðð  ðð 
$óð@:#Ø%ð:#Ø59ð:#à	ó:#ðx;àð;ð 
ó;ðz%kà"ð%kð ð%kð ð	%kð
 %ð%kð 3ð%kð 
ô%kr5   rr   N)>rp   Ú
__future__r   Útypingr   r   rA   r   r   Ú_dnsr	   r
   r   r   r   Ú_protocol.incomingr   Ú_services.infor   Ú
_transportr   Ú
_utils.netr   Úconstr   r   r   r   r   r   r   r   r   r   r   r   r   rS   r    r!   r"   r#   r^   r%   Ú__annotations__r'   ÚAllr‰   Úintr6   ÚstrrÂ   r”   r•   r–   r—   r˜   Ú_corer*   r,   r?   rr   r=   r5   r3   ú<module>rÐ      sÍ   ðòõ, #ç &ç 1ß KÕ KÝ ,Ý (Ý *Ý "÷÷ ÷ õ ÷ó ð '¨	ÐJÐ4IÐJÐ à*,Ð Ð'Ó ,Ø!Ð �9Ó !à—‘€à
€Ø
€à,-Ð )ØÐ ØÐ ØÐ ØÐ áÝ ÷!ñ !÷"a_ñ a_÷HYkò Ykr5   