
    |;i^H                       d Z ddlmZ ddlZddl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 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 ddlmZ e Z!e"Z#e$Z%e&Z'eZ(eZ) ed      jT                  Z+ ed      jT                  Z, ed      jT                  Z-dZ. e/d  e0d      D              Z1 e/d  e0e.      D              Z2eedfD  ci c]  } |  e-|        c} Z3 G d dejh                        Z5e5jl                  jn                  Z8e5jr                  jn                  Z:ejv                  Z<ejz                  Z> G d d      Z?yc c} w )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
    )annotationsN)Sequence)Struct)TYPE_CHECKING   )
DNSPointerDNSQuestion	DNSRecord)NamePartTooLongException)log)
_CLASS_UNIQUE_DNS_HOST_TTL_DNS_OTHER_TTL_DNS_PACKET_HEADER_LEN_FLAGS_QR_MASK_FLAGS_QR_QUERY_FLAGS_QR_RESPONSE	_FLAGS_TC_MAX_MSG_ABSOLUTE_MAX_MSG_TYPICAL   )DNSIncomingz>Bz>Hz>L   c              #  2   K   | ]  }t        |        y wN)	PACK_BYTE.0is     N/home/ubuntu/myenv/lib/python3.12/site-packages/zeroconf/_protocol/outgoing.py	<genexpr>r!   >   s     4A9Q<4      c              #  2   K   | ]  }t        |        y wr   )
PACK_SHORTr   s     r    r!   r!   ?   s     CqZ]Cr"   c                      e Zd ZdZdZy)Stater   r   N)__name__
__module____qualname__initfinished     r    r'   r'   C   s    DHr.   r'   c                  "   e Zd ZdZdZd#d$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d,dZd-dZd.dZd-dZd/dZd-dZd0dZd1dZd2dZd3dZd4dZd5dZd6dZd7dZd8dZd9dZd:dZd;dZd<dZ d=dZ!	 	 	 	 	 	 	 	 	 	 d>d Z"d?d!Z#y")@DNSOutgoingz+Object representation of an outgoing packet)additionals
allow_longanswersauthoritiesdatar,   flagsid	multicastnamespackets_data	questionssizestatec                    || _         d| _        || _        || _        g | _        i | _        g | _        t        | _        d| _	        t        | _        g | _        g | _        g | _        g | _        y )NFT)r6   r,   r7   r8   r:   r9   r5   r   r<   r2   
STATE_INITr=   r;   r3   r4   r1   )selfr6   r8   id_s       r    __init__zDNSOutgoing.__init__c   si    
")+ &(
!#	/	 $
,.68-/,.r.   c                6    | j                   t        z  t        k(  S )z Returns true if this is a query.)r6   r   r   r@   s    r    is_queryzDNSOutgoing.is_queryw   s    

^+??r.   c                6    | j                   t        z  t        k(  S )z#Returns true if this is a response.)r6   r   r   rD   s    r    is_responsezDNSOutgoing.is_response{   s    

^+0BBBr.   c                D    i | _         g | _        t        | _        d| _        y )NT)r9   r5   r   r<   r2   rD   s    r    _reset_for_next_packetz"DNSOutgoing._reset_for_next_packet   s    
	*	r.   c                    dj                  dj                  d| j                   d| j                   d| j                   d| j
                   d| j                   d| j                   g            S )	Nz<DNSOutgoing:{}>z, z
multicast=zflags=z
questions=zanswers=zauthorities=zadditionals=)formatjoinr8   r6   r;   r3   r4   r1   rD   s    r    __repr__zDNSOutgoing.__repr__   s    !((II  01TZZL)  01t||n-"4#3#3"45"4#3#3"45	
 	
r.   c                :    | j                   j                  |       y)zAdds a questionN)r;   appendr@   records     r    add_questionzDNSOutgoing.add_question   s    f%r.   c                L    |j                  |      s| j                  |d       yy)zAdds an answerg        N)suppressed_byadd_answer_at_time)r@   inprQ   s      r    
add_answerzDNSOutgoing.add_answer   s%    ##C(##FC0 )r.   c                v    |}|5|dk(  s|j                  |      s| j                  j                  ||f       yyy)z6Adds an answer if it does not expire by a certain timeNr   )
is_expiredr3   rO   )r@   rQ   now
now_doubles       r    rU   zDNSOutgoing.add_answer_at_time   s?    
:?&:K:KJ:WLL. ;Xr.   c                :    | j                   j                  |       y)zAdds an authoritative answerN)r4   rO   rP   s     r    add_authorative_answerz"DNSOutgoing.add_authorative_answer   s    'r.   c                :    | j                   j                  |       y)a  Adds an additional answer

        From: RFC 6763, DNS-Based Service Discovery, February 2013

        12.  DNS Additional Record Generation

           DNS has an efficiency feature whereby a DNS server may place
           additional records in the additional section of the DNS message.
           These additional records are records that the client did not
           explicitly request, but the server has reasonable grounds to expect
           that the client might request them shortly, so including them can
           save the client from having to issue additional queries.

           This section recommends which additional records SHOULD be generated
           to improve network efficiency, for both Unicast and Multicast DNS-SD
           responses.

        12.1.  PTR Records

           When including a DNS-SD Service Instance Enumeration or Selective
           Instance Enumeration (subtype) PTR record in a response packet, the
           server/responder SHOULD include the following additional records:

           o  The SRV record(s) named in the PTR rdata.
           o  The TXT record(s) named in the PTR rdata.
           o  All address records (type "A" and "AAAA") named in the SRV rdata.

        12.2.  SRV Records

           When including an SRV record in a response packet, the
           server/responder SHOULD include the following additional records:

           o  All address records (type "A" and "AAAA") named in the SRV rdata.

        N)r1   rO   rP   s     r    add_additional_answerz!DNSOutgoing.add_additional_answer   s    H 	'r.   c                r    | j                   j                  t        |          | xj                  dz  c_        y)z"Writes a single byte to the packetr   N)r5   rO   
BYTE_TABLEr<   r@   values     r    _write_bytezDNSOutgoing._write_byte   s&    		E*+		Q	r.   c                <    |t         k  r	t        |   S t        |      S )z%Convert an unsigned short to 2 bytes.)SHORT_CACHE_MAXSHORT_LOOKUPr%   rb   s     r    
_get_shortzDNSOutgoing._get_short   s    &+o&=|E"T:eCTTr.   c                Z    | j                   j                  d| j                  |             y)z4Inserts an unsigned short at the start of the packetr   N)r5   insertrh   rb   s     r    _insert_short_at_startz"DNSOutgoing._insert_short_at_start   s    		DOOE23r.   c                @    | j                  |      | j                  |<   y)z>Replaces an unsigned short in a certain position in the packetN)rh   r5   )r@   indexrc   s      r    _replace_shortzDNSOutgoing._replace_short   s    ??51		%r.   c                    | j                   j                  | j                  |             | xj                  dz  c_        y)z&Writes an unsigned short to the packetr   N)r5   rO   rh   r<   rb   s     r    write_shortzDNSOutgoing.write_short   s*    		/0		Q	r.   c                    t        |      }t        j                  |      }|| j                  j	                  |       n$| j                  j	                  t        |             | xj                  dz  c_        y)z(Writes an unsigned integer to the packetN   )intLONG_LOOKUPgetr5   rO   	PACK_LONGr<   )r@   rc   value_as_int
long_bytess       r    
_write_intzDNSOutgoing._write_int   sU    5z __\2
!IIZ(IIY|45		Q	r.   c                    t         rt        |t              sJ | j                  j	                  |       | xj
                  t        |      z  c_        y)zWrites a string to the packetN)r   
isinstancebytesr5   rO   r<   lenrb   s     r    write_stringzDNSOutgoing.write_string   s9    eU+++				SZ	r.   c                    |j                  d      }t        |      }|dkD  rt        | j                  |       | j	                  |       y)z5Writes a UTF-8 string of a given length to the packetutf-8@   N)encoder}   r   rd   r~   )r@   sutfstrlengths       r    
_write_utfzDNSOutgoing._write_utf   sB    '"VB;** &!r.   c                    t         rt        |t              sJ t        |      }|dkD  rt        | j                  |       | j                  |       y )Nr#   )r   r{   r|   r}   r   rd   r~   )r@   rc   r   s      r    write_character_stringz"DNSOutgoing.write_character_string   sG    eU+++UC<** % r.   c                   |r|d   dk(  r|dd }| j                   j                  |d      }|r| j                  |       y| j                  }|j	                  d      }|| j                   |<   | j                  |d          d}t        dt        |            D ]  }dj                  ||d       }| j                   j                  |d      }|r| j                  |        y|dk(  rt        |j                  d            }||z   t        |j                  d            z
  | j                   |<   | j                  ||           | j                  d       y)a  
        Write names to packet

        18.14. Name Compression

        When generating Multicast DNS messages, implementations SHOULD use
        name compression wherever possible to compress the names of resource
        records, by replacing some or all of the resource record name with a
        compact two-byte reference to an appearance of that data somewhere
        earlier in the message [RFC1035].
        .Nr   r   r   )r9   ru   _write_link_to_namer<   splitr   ranger}   rL   r   rd   )r@   namerm   
start_sizelabelsname_lengthcountpartial_names           r    
write_namezDNSOutgoing.write_name  s7    DHO9D

tQ'$$U+YY
C%

4q	"1c&k* 		+E88F56N3LJJNN<3E((/a!$++g"67'1K'?#lFYFYZaFbBc'cDJJ|$OOF5M*		+ 	r.   c                Z    | j                  |dz	  dz         | j                  |dz         y )N         )rd   )r@   rm   s     r    r   zDNSOutgoing._write_link_to_name0  s-     	%1*,-&r.   c                    t        | j                        }| j                  }| j                  |j                         | j                  |j                         | j                  |       | j                  ||      S )zWrites a question to the packet)	r}   r5   r<   r   r   rp   type_write_record_class_check_data_limit_or_rollback)r@   questionstart_data_lengthr   s       r    _write_questionzDNSOutgoing._write_question6  s]    		NYY
&'  *112CZPPr.   c                    |j                   }|j                  du r%| j                  r| j                  |t        z         y| j                  |       y)zAWrite out the record class including the unique/unicast (QU) bit.TN)class_uniquer8   rp   r   )r@   rQ   r   s      r    r   zDNSOutgoing._write_record_class?  s>    ==D T^^Vm34V$r.   c                p    | j                  |dk(  r|j                         y|j                  |             y)zWrite out the record ttl.r   N)ry   ttlget_remaining_ttl)r@   rQ   rZ   s      r    
_write_ttlzDNSOutgoing._write_ttlG  s(    cQh

RF4L4LS4QRr.   c                   t        | j                        }| j                  }| j                  |j                         | j                  |j                         | j                  |       | j                  ||       t        | j                        }| j                  d       |j                  |        d}| j                  |dz   d D ]  }|t        |      z  } | j                  ||       | j                  ||      S )zWrites 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.r   r   N)r}   r5   r<   r   r   rp   r   r   r   writern   r   )r@   rQ   rZ   r   r   rm   r   ds           r    _write_recordzDNSOutgoing._write_recordK  s      		NYY
$%  ($DIIT519;' 	Ac!fF	 	E6*112CZPPr.   c                   | j                   rt        nt        }d| _         | j                  |k  ryt	        t
              r!t        j                  d| j                  |       | j                  |d= || _        |}| j                  j                         D cg c]  \  }}||k\  s| }}}|D ]  }| j                  |=  yc c}}w )z@Check data limit, if we go over, then rollback and return False.FTz8Reached data limit (size=%d) > (limit=%d) - rolling backN)r2   r   r   r<   LOGGING_IS_ENABLED_FORLOGGING_DEBUGr   debugr5   r9   items)r@   r   r   	len_limitstart_size_intr   idxrollback_namess           r    r   z)DNSOutgoing._check_data_limit_or_rollbacka  s    )-%>N	99	!!-0IIJ		
 II'()	#04

0@0@0B\94c^F[$\\" 	!D

4 	! ]s   B?#B?c                d    d}| j                   |d  D ]  }| j                  |      s |S |dz  } |S Nr   r   )r;   r   )r@   questions_offsetquestions_writtenr   s       r    _write_questions_from_offsetz(DNSOutgoing._write_questions_from_offsetx  sP    '7'89 	#H''1   "	# ! r.   c                l    d}| j                   |d  D ]  \  }}| j                  ||      s |S |dz  }! |S r   )r3   r   )r@   answer_offsetanswers_writtenanswertime_s        r    _write_answers_from_offsetz&DNSOutgoing._write_answers_from_offset  sR    !\\-.9 	!MFE%%fe4 q O	! r.   c                R    d}||d  D ]  }| j                  |d      s |S |dz  } |S r   )r   )r@   recordsoffsetrecords_writtenrQ   s        r    _write_records_from_offsetz&DNSOutgoing._write_records_from_offset  sJ    fg& 	!F%%fa0 q O	! r.   c                    |t        | j                        k  xsL |t        | j                        k  xs2 |t        | j                        k  xs |t        | j                        k  S )z\Check if all questions, answers, authority, and additionals have been written to the packet.)r}   r;   r3   r4   r1   )r@   r   r   authority_offsetadditional_offsets        r    _has_more_to_addzDNSOutgoing._has_more_to_add  sb     s4>>22 9s4<<009#d&6&6"779 !3t'7'7#88		
r.   c           	     v   | j                   }| j                  t        k(  r|S d}d}d}d}t        t              du }d}|rn|r~t        j                  d||||       t        j                  dt        | j                        t        | j                        t        | j                        t        | j                               | j                  |      }| j                  |      }	| j                  | j                  |      }
| j                  | j                  |      }t        | j                         }| j#                  |       | j#                  |
       | j#                  |	       | j#                  |       ||z  }||	z  }||
z  }||z  }|rt        j                  d||||       | j%                  ||||      }|rJ| j'                         r:|rt        j                  d       | j#                  | j(                  t*        z         n| j#                  | j(                         | j,                  r| j#                  d       n| j#                  | j.                         |j1                  dj3                  | j                                |st        j4                  d       n|r| j7                          |rnt        | _        |S )	a  Returns a list of bytestrings containing the packets' bytes

        No further parts should be added to the packet once this
        is done.  The packets are each restricted to _MAX_MSG_TYPICAL
        or less in length, except for the case of a single answer which
        will be written out to a single oversized packet no more than
        _MAX_MSG_ABSOLUTE in length (and hence will be subject to IP
        fragmentation potentially).r   TzBoffsets = questions=%d, answers=%d, authorities=%d, additionals=%dzBlengths = questions=%d, answers=%d, authorities=%d, additionals=%dzFnow offsets = questions=%d, answers=%d, authorities=%d, additionals=%dzSetting TC flagr.   z4packets() made no progress adding records; returning)r:   r=   STATE_FINISHEDr   r   r   r   r}   r;   r3   r4   r1   r   r   r   boolr5   rk   r   rE   r6   r   r8   r7   rO   rL   warningrI   )r@   r:   r   r   r   r   debug_enablehas_more_to_addr   r   authorities_writtenadditionals_writtenmade_progresss                r    packetszDNSOutgoing.packets  s}    ((::'-m<D		X$!$% 		X'%(()(() !% A ABR S"==mLO"&"A"A$BRBRTd"e"&"A"A$BRBRTe"f OM''(;<''(;<''8''(9: 11_,M 33!44		\$!$% #33 -1ACTO 4==?II/0++DJJ,BC++DJJ7~~++A.++DGG4 34 
 RS++-M P $
r.   N)Tr   )r6   rs   r8   r   rA   rs   returnNone)r   r   )r   r   )r   str)rQ   r	   r   r   )rV   r   rQ   r
   r   r   )rQ   zDNSRecord | NonerZ   float_r   r   )rQ   r   r   r   )rQ   r
   r   r   )rc   int_r   r   )rc   r   r   r|   )rm   r   rc   r   r   r   )rc   zfloat | intr   r   )rc   bytes_r   r   )r   str_r   r   )rc   r|   r   r   )r   r   r   r   )rm   r   r   r   )r   DNSQuestion_r   r   )rQ   zDNSQuestion_ | DNSRecord_r   r   )rQ   
DNSRecord_rZ   r   r   r   )rQ   r   rZ   r   r   r   )r   r   r   r   r   r   )r   r   r   rs   )r   r   r   rs   )r   zSequence[DNSRecord]r   r   r   rs   )
r   r   r   r   r   r   r   r   r   r   )r   zlist[bytes])$r(   r)   r*   __doc__	__slots__rB   rE   rG   rI   rM   rR   rW   rU   r]   r_   rd   rh   rk   rn   rp   ry   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r-   r.   r    r0   r0   O   s    5I"/(@C
&1
/($(L
U42
 "!)V'Q%SQ,.!

 
 	

  
 

_r.   r0   )@r   
__future__r   enumloggingcollections.abcr   structr   typingr   _dnsr   r	   r
   _exceptionsr   _loggerr   constr   r   r   r   r   r   r   r   r   r   incomingr   r   r   floatr   rs   r   r|   r   r   r   packr   r%   rv   rf   tupler   ra   rg   rt   Enumr'   r+   rc   r?   r,   r   isEnabledForr   DEBUGr   r0   )r   s   0r    <module>r      s/  , #   $    5 5 2    "
	
	
 4L	D\
4L	4s44
CE/,BCC)7(JK1q)A,KDII 
 ZZ
%%)) o o Ls   E