o
    2c                     @   sn  d Z ddlZddlZddlZddlZddlZddlZddlZddl	Zddl
ZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZG dd dejjZG dd dejjZG dd dejjZG d	d
 d
ejjZG dd dejjZG dd dejjZG dd dejjZ G dd dejjZ!G dd dejjZ"G dd dejjZ#G dd dejj$Z%G dd dej&j'Z(dZ)dZ*G dd dZ+G dd  d Z,G d!d" d"e+Z-d#d$ Z.d%d& Z/G d'd( d(Z0		*	*	*d:d+d,Z1G d-d. d.Z2	*	d;d0d1Z3d<d2d3Z4ej5j6dd*dddddfd4d5Z7	6	d=d8d9Z8e(j9Z9e(j:Z:e(j;Z;e(j<Z<dS )>zDNS Messages    Nc                   @      e Zd ZdZdS )ShortHeaderz2The DNS packet passed to from_wire() is too short.N__name__
__module____qualname____doc__ r	   r	   A/var/www/html/gps/gps/lib/python3.10/site-packages/dns/message.pyr   ,       r   c                   @   r   )TrailingJunkzEThe DNS packet passed to from_wire() has extra junk at the end of it.Nr   r	   r	   r	   r
   r   0   r   r   c                   @   r   )UnknownHeaderFieldzVThe header field name was not recognized when converting from text
    into a message.Nr   r	   r	   r	   r
   r   4   r   r   c                   @   r   )BadEDNSzLAn OPT record occurred somewhere other than
    the additional data section.Nr   r	   r	   r	   r
   r   9   r   r   c                   @   r   )BadTSIGzWA TSIG record occurred somewhere other than the end of
    the additional data section.Nr   r	   r	   r	   r
   r   >   r   r   c                   @   r   )UnknownTSIGKeyz(A TSIG with an unknown key was received.Nr   r	   r	   r	   r
   r   C   r   r   c                   @   s   e Zd ZdZdhZdd ZdS )	TruncatedzThe truncated flag is set.messagec                 C   
   | j d S )zbAs much of the message as could be processed.

        Returns a ``dns.message.Message``.
        r   )kwargsselfr	   r	   r
   r   L   s   
zTruncated.messageN)r   r   r   r   supp_kwargsr   r	   r	   r	   r
   r   G   s    r   c                   @   r   )NotQueryResponsez%Message is not a response to a query.Nr   r	   r	   r	   r
   r   T   r   r   c                   @   r   )ChainTooLongzThe CNAME chain is too long.Nr   r	   r	   r	   r
   r   X   r   r   c                   @   r   )AnswerForNXDOMAINz.The rcode is NXDOMAIN but an answer was found.Nr   r	   r	   r	   r
   r   \   r   r   c                   @   r   )NoPreviousNamezNo previous name was known.Nr   r	   r	   r	   r
   r   _   r   r   c                   @   s,   e Zd ZdZdZdZdZdZedd Z	dS )	MessageSectionzMessage sectionsr            c                 C   s   dS Nr   r	   )clsr	   r	   r
   _maximumj      zMessageSection._maximumN)
r   r   r   r   QUESTIONANSWER	AUTHORITY
ADDITIONALclassmethodr"   r	   r	   r	   r
   r   c   s    r   i     c                   @   s  e Zd ZdZeZdWddZedd Zej	dd Zedd	 Z
e
j	d
d	 Z
edd Zej	dd Zedd Zej	dd Zdd Zdd ZdXddZdd Zdd Zdd Zdd Zd d! Zejjdd"d"fd#d$Zejjdd"d"fd%d&ZdYd(d)Zed*d+ Zdd,dd'd-ejjfd.d/Zed0d1 Z ed2d3 Z!ed4d5 Z"ed6d7 Z#ed8d9 Z$ed'e%dfd:d;Z&d'd'e%ddfd<d=Z'ed>d? Z(ed@dA Z)e)j	dBdA Z)edCdD Z*edEdF Z+dZdGdHZ,dIdJ Z-dKdL Z.dMdN Z/dOdP Z0dQdR Z1dSdT Z2dUdV Z3dS )[MessagezA DNS message.Nc                 C   sj   |d u rt j | _n|| _d| _g g g g g| _d | _d| _d | _d | _	d| _
d| _d | _d | _i | _d S )Nr       F)dnsentropy	random_16idflagssectionsoptrequest_payloadkeyringtsigrequest_macxfrorigintsig_ctxindex)r   r/   r	   r	   r
   __init__w   s   
zMessage.__init__c                 C   r   )z The question section.r   r1   r   r	   r	   r
   question      
zMessage.questionc                 C      || j d< d S Nr   r<   r   vr	   r	   r
   r=         c                 C   r   )z The answer section.r   r<   r   r	   r	   r
   answer   r>   zMessage.answerc                 C   r?   )Nr   r<   rA   r	   r	   r
   rD      rC   c                 C   r   )z The authority section.r   r<   r   r	   r	   r
   	authority   r>   zMessage.authorityc                 C   r?   )Nr   r<   rA   r	   r	   r
   rE      rC   c                 C   r   )z The additional data section.r   r<   r   r	   r	   r
   
additional   r>   zMessage.additionalc                 C   r?   r    r<   rA   r	   r	   r
   rF      rC   c                 C   s   dt | j d S )Nz<DNS message, ID >)reprr/   r   r	   r	   r
   __repr__   s   zMessage.__repr__c                 C   s   |   S N)to_textr   r	   r	   r
   __str__   s   zMessage.__str__Tc           	      K   s:  t  }|d| j  |dtj|    |dtj|    |dtj| j  | j	dkrX|d| j	  | j
dkrP|dtj| j
  |d| j  | jD ]}|d	|   q[| jj D ]'\}}|d
| d | |D ]}||j||fi | |d qqm| dd S )zConvert the message to text.

        The *origin*, *relativize*, and any other keyword
        arguments are passed to the RRset ``to_wire()`` method.

        Returns a ``str``.
        zid %d
z
opcode %s
z	rcode %s
z	flags %s
r   zedns %s
z
eflags %s
zpayload %d
z
option %s
;
N)ioStringIOwriter/   r,   opcoderK   rcoder0   edns	ednsflagsedns_to_textpayloadoptions_section_enum__members__itemssection_from_numbergetvalue)	r   r8   
relativizekwsr2   namewhichrrsetr	   r	   r
   rK      s*   	


zMessage.to_textc                 C   s   t |tsdS | j|jkrdS | j|jkrdS t| jD ]#\}}|j| }|D ]
}||vr1  dS q'|D ]
}||vr>  dS q4qdS )zTwo messages are equal if they have the same content in the
        header, question, answer, and authority sections.

        Returns a ``bool``.
        FT)
isinstancer*   r/   r0   	enumerater1   )r   otherisectionother_sectionnr	   r	   r
   __eq__   s$   

zMessage.__eq__c                 C   s   |  | S rJ   )rl   )r   rg   r	   r	   r
   __ne__      zMessage.__ne__c                 C   s   |j tj j@ dks| j|jkstj| j tj|j krdS | tjjtjjtjj	tjj
hv r:t|jdkr:dS tj| j rCdS | jD ]
}||jvrP dS qF|jD ]
}|| jvr^ dS qTdS )zIIs *other* a response this message?

        Returns a ``bool``.
        r   FT)r0   r,   QRr/   rS   
from_flagsrT   FORMERRSERVFAILNOTIMPREFUSEDlenr=   	is_update)r   rg   rk   r	   r	   r
   is_response   s0   




zMessage.is_responsec                 C   s2   t | jD ]\}}||u r| |  S qtd)zReturn the "section number" of the specified section for use
        in indexing.

        *section* is one of the section attributes of this message.

        Raises ``ValueError`` if the section isn't known.

        Returns an ``int``.
        zunknown section)rf   r1   rZ   
ValueError)r   ri   rh   our_sectionr	   r	   r
   section_number  s
   zMessage.section_numberc                 C   s   | j |}| j| S )a  Return the section list associated with the specified section
        number.

        *number* is a section number `int` or the text form of a section
        name.

        Raises ``ValueError`` if the section isn't known.

        Returns a ``list``.
        )rZ   maker1   )r   numberri   r	   r	   r
   r]     s   
zMessage.section_from_numberFc	                 C   s   t |tr|}	| |	}n| |}	|	|||||f}
|s@| jdur.| j|
}|dur-|S n|D ]}||||||r?|  S q0|sDttj	
|||||}|| | jdur]|| j|
< |S )aP  Find the RRset with the given attributes in the specified section.

        *section*, an ``int`` section number, or one of the section
        attributes of this message.  This specifies the
        the section of the message to search.  For example::

            my_message.find_rrset(my_message.answer, name, rdclass, rdtype)
            my_message.find_rrset(dns.message.ANSWER, name, rdclass, rdtype)

        *name*, a ``dns.name.Name``, the name of the RRset.

        *rdclass*, an ``int``, the class of the RRset.

        *rdtype*, an ``int``, the type of the RRset.

        *covers*, an ``int`` or ``None``, the covers value of the RRset.
        The default is ``None``.

        *deleting*, an ``int`` or ``None``, the deleting value of the RRset.
        The default is ``None``.

        *create*, a ``bool``.  If ``True``, create the RRset if it is not found.
        The created RRset is appended to *section*.

        *force_unique*, a ``bool``.  If ``True`` and *create* is also ``True``,
        create a new RRset regardless of whether a matching RRset exists
        already.  The default is ``False``.  This is useful when creating
        DDNS Update messages, as order matters for them.

        Raises ``KeyError`` if the RRset was not found and create was
        ``False``.

        Returns a ``dns.rrset.RRset object``.
        N)re   intr]   rz   r:   get
full_matchKeyErrorr,   rd   RRsetappend)r   ri   rb   rdclassrdtypecoversdeletingcreateforce_uniquerz   keyrd   r	   r	   r
   
find_rrset-  s0   
&




zMessage.find_rrsetc	           
   
   C   s8   z|  ||||||||}	W |	S  ty   d}	Y |	S w )a5  Get the RRset with the given attributes in the specified section.

        If the RRset is not found, None is returned.

        *section*, an ``int`` section number, or one of the section
        attributes of this message.  This specifies the
        the section of the message to search.  For example::

            my_message.get_rrset(my_message.answer, name, rdclass, rdtype)
            my_message.get_rrset(dns.message.ANSWER, name, rdclass, rdtype)

        *name*, a ``dns.name.Name``, the name of the RRset.

        *rdclass*, an ``int``, the class of the RRset.

        *rdtype*, an ``int``, the type of the RRset.

        *covers*, an ``int`` or ``None``, the covers value of the RRset.
        The default is ``None``.

        *deleting*, an ``int`` or ``None``, the deleting value of the RRset.
        The default is ``None``.

        *create*, a ``bool``.  If ``True``, create the RRset if it is not found.
        The created RRset is appended to *section*.

        *force_unique*, a ``bool``.  If ``True`` and *create* is also ``True``,
        create a new RRset regardless of whether a matching RRset exists
        already.  The default is ``False``.  This is useful when creating
        DDNS Update messages, as order matters for them.

        Returns a ``dns.rrset.RRset object`` or ``None``.
        N)r   r   )
r   ri   rb   r   r   r   r   r   r   rd   r	   r	   r
   	get_rrsetk  s   %zMessage.get_rrsetr   c           
   	   K   s  |du r| j dur| j }|dkr| jdkr| j}nd}|dk r"d}n|dkr(d}tj| j| j||}| jD ]}||j	|j
|j q6| jD ]}|jtjj|fi | qF| jD ]}|jtjj|fi | qX| jduru|tjj| j | jD ]}|jtjj|fi | qx|  | jdurtj| | j| jd tt | j||\}}	| j  | j| |tjj| j |  |r|	| _| S )a  Return a string containing the message in DNS compressed wire
        format.

        Additional keyword arguments are passed to the RRset ``to_wire()``
        method.

        *origin*, a ``dns.name.Name`` or ``None``, the origin to be appended
        to any relative names.  If ``None``, and the message has an origin
        attribute that is not ``None``, then it will be used.

        *max_size*, an ``int``, the maximum size of the wire format
        output; default is 0, which means "the message's request
        payload, if nonzero, or 65535".

        *multi*, a ``bool``, should be set to ``True`` if this message is
        part of a multiple message sequence.

        *tsig_ctx*, a ``dns.tsig.HMACTSig`` or ``dns.tsig.GSSTSig`` object, the
        ongoing TSIG context, used when signing zone transfers.

        Raises ``dns.exception.TooBig`` if *max_size* was exceeded.

        Returns a ``bytes``.
        Nr   i  i   )r8   r3   r,   rendererRendererr/   r0   r=   add_questionrb   r   r   rD   	add_rrsetr%   rE   r&   r2   r'   rF   write_headerr5   signget_wirer4   r}   timer6   clearaddr9   )
r   r8   max_sizemultir9   r`   rrd   new_tsigctxr	   r	   r
   to_wire  sL   








zMessage.to_wirec           	      C   s8   t jjjt jjt jj|||||||	}t j| d|S r@   )r,   rdtypesANYTSIG
rdataclass	rdatatyperd   
from_rdata)	keyname	algorithmtime_signedfudgemacoriginal_iderrorrg   r5   r	   r	   r
   
_make_tsig  s
   zMessage._make_tsig,  r+   c           	   
   C   s   t |tjjr|}|j}n0t|r|| |}n&t |tr"tj|}|du r,tt	|}|| }t |t
r=tj|||}|| _|du rG| j}| || jjd|d|||| _dS )a  When sending, a TSIG signature using the specified key
        should be added.

        *key*, a ``dns.tsig.Key`` is the key to use.  If a key is specified,
        the *keyring* and *algorithm* fields are not used.

        *keyring*, a ``dict``, ``callable`` or ``dns.tsig.Key``, is either
        the TSIG keyring or key to use.

        The format of a keyring dict is a mapping from TSIG key name, as
        ``dns.name.Name`` to ``dns.tsig.Key`` or a TSIG secret, a ``bytes``.
        If a ``dict`` *keyring* is specified but a *keyname* is not, the key
        used will be the first key in the *keyring*.  Note that the order of
        keys in a dictionary is not defined, so applications should supply a
        keyname when a ``dict`` keyring is used, unless they know the keyring
        contains only one key.  If a ``callable`` keyring is specified, the
        callable will be called with the message and the keyname, and is
        expected to return a key.

        *keyname*, a ``dns.name.Name``, ``str`` or ``None``, the name of
        thes TSIG key to use; defaults to ``None``.  If *keyring* is a
        ``dict``, the key must be defined in it.  If *keyring* is a
        ``dns.tsig.Key``, this is ignored.

        *fudge*, an ``int``, the TSIG time fudge.

        *original_id*, an ``int``, the TSIG original id.  If ``None``,
        the message's id is used.

        *tsig_error*, an ``int``, the TSIG error code.

        *other_data*, a ``bytes``, the TSIG other data.

        *algorithm*, a ``dns.name.Name``, the TSIG algorithm to use.  This is
        only used if *keyring* is a ``dict``, and the key entry is a ``bytes``.
        Nr   r+   )re   r,   r5   Keyrb   callablestr	from_textnextiterbytesr4   r/   r   r   )	r   r4   r   r   r   
tsig_error
other_datar   r   r	   r	   r
   use_tsig  s$   (


zMessage.use_tsigc                 C   s   | j r| j jS d S rJ   )r5   rb   r   r	   r	   r
   r        zMessage.keynamec                 C      | j r	| j d jS d S r@   )r5   r   r   r	   r	   r
   keyalgorithm#     zMessage.keyalgorithmc                 C   r   r@   )r5   r   r   r	   r	   r
   r   *  r   zMessage.macc                 C   r   r@   )r5   r   r   r	   r	   r
   r   1  r   zMessage.tsig_errorc                 C   s
   t | jS rJ   )boolr5   r   r	   r	   r
   had_tsig8  s   
zMessage.had_tsigc                 C   s4   t jjj|t jj|pd}t jt jjt	| |S )Nr	   )
r,   r   r   OPTr   rd   r   rb   rootr}   )r0   rX   rY   r2   r	   r	   r
   	_make_opt<  s   zMessage._make_optc                 C   s   |du s|du rd}n|du rd}|dk rd| _ d| _dS |dM }||d> O }|du r-g }| |||| _ |du r;|}|| _dS )a6  Configure EDNS behavior.

        *edns*, an ``int``, is the EDNS level to use.  Specifying
        ``None``, ``False``, or ``-1`` means "do not use EDNS", and in this case
        the other parameters are ignored.  Specifying ``True`` is
        equivalent to specifying 0, i.e. "use EDNS0".

        *ednsflags*, an ``int``, the EDNS flag values.

        *payload*, an ``int``, is the EDNS sender's payload field, which is the
        maximum size of UDP datagram the sender can handle.  I.e. how big
        a response to this message can be.

        *request_payload*, an ``int``, is the EDNS payload size to use when
        sending this message.  If not specified, defaults to the value of
        *payload*.

        *options*, a list of ``dns.edns.Option`` objects or ``None``, the EDNS
        options.
        NFrO   Tr   l   ~ r)   )r2   r3   r   )r   rU   rV   rX   r3   rY   r	   r	   r
   use_ednsB  s   

zMessage.use_ednsc                 C   s   | j r
| jd@ d? S dS )Ni   r)   rO   )r2   rV   r   r	   r	   r
   rU   k  s   zMessage.ednsc                 C   s   | j r| j jS dS r@   )r2   ttlr   r	   r	   r
   rV   r  r   zMessage.ednsflagsc                 C   s*   | j r	|| j _d S |r| || _ d S d S rJ   )r2   r   r   rA   r	   r	   r
   rV   y  s
   c                 C   s   | j r	| j d jS dS r@   )r2   rX   r   r	   r	   r
   rX     r   zMessage.payloadc                 C   s   | j r	| j d jS dS )Nr   r	   )r2   rY   r   r	   r	   r
   rY     r   zMessage.optionsc                 C   s<   |r|  j tjjO  _ dS | jr|  j tjj M  _ dS dS )a!  Enable or disable 'DNSSEC desired' flag in requests.

        *wanted*, a ``bool``.  If ``True``, then DNSSEC data is
        desired in the response, EDNS is enabled if required, and then
        the DO bit is set.  If ``False``, the DO bit is cleared if
        EDNS is enabled.
        N)rV   r,   r0   DOr2   )r   wantedr	   r	   r
   want_dnssec  s
   	zMessage.want_dnssecc                 C   s   t jt| jt| jS )z7Return the rcode.

        Returns an ``int``.
        )r,   rT   rp   r}   r0   rV   r   r	   r	   r
   rT     s   zMessage.rcodec                 C   sL   t j|\}}|  jdM  _|  j|O  _|  jdM  _|  j|O  _dS )zJSet the rcode.

        *rcode*, an ``int``, is the rcode to set.
        i  i N)r,   rT   to_flagsr0   rV   )r   rT   valueevaluer	   r	   r
   	set_rcode  s
   zMessage.set_rcodec                 C   s   t jt| jS )z8Return the opcode.

        Returns an ``int``.
        )r,   rS   rp   r}   r0   r   r	   r	   r
   rS     s   zMessage.opcodec                 C   s(   |  j dM  _ |  j tj|O  _ dS )zMSet the opcode.

        *opcode*, an ``int``, is the opcode to set.
        i  N)r0   r,   rS   r   )r   rS   r	   r	   r
   
set_opcode  s   zMessage.set_opcodec                 C   s   |S rJ   r	   )r   r   r	   r	   r
   _get_one_rr_per_rrset  r#   zMessage._get_one_rr_per_rrsetc                 C   s   ||d dfS NFr	   )r   ri   rb   r   r   r	   r	   r
   _parse_rr_header  rn   zMessage._parse_rr_headerc                 C   sl   |t jjkr|tjks| js|t jjkrtn|t jj	kr0|tjks.|t jj
ks.||d kr0t||d dfS )Nr   F)r,   r   r   r   r'   r2   rb   r   r   r   r   r   )r   ri   countpositionrb   r   r   r	   r	   r
   _parse_special_rr_header  s   



z Message._parse_special_rr_headerrJ   )NT)Nr   FN)T)4r   r   r   r   r   rZ   r;   propertyr=   setterrD   rE   rF   rI   rL   rK   rl   rm   rw   rz   r]   r,   r   NONEr   r   r   staticmethodr   r5   default_algorithmr   r   r   r   r   r   DEFAULT_EDNS_PAYLOADr   r   rU   rV   rX   rY   r   rT   r   rS   r   r   r   r   r	   r	   r	   r
   r*   r   s    









" 


?

,B

;





)





r*   c                   @   s   e Zd ZdZdd ZdS )ChainingResulta  The result of a call to dns.message.QueryMessage.resolve_chaining().

    The ``answer`` attribute is the answer RRSet, or ``None`` if it doesn't
    exist.

    The ``canonical_name`` attribute is the canonical name after all
    chaining has been applied (this is the name as ``rrset.name`` in cases
    where rrset is not ``None``).

    The ``minimum_ttl`` attribute is the minimum TTL, i.e. the TTL to
    use if caching the data.  It is the smallest of all the CNAME TTLs
    and either the answer TTL if it exists or the SOA TTL and SOA
    minimum values for negative answers.

    The ``cnames`` attribute is a list of all the CNAME RRSets followed to
    get to the canonical name.
    c                 C   s   || _ || _|| _|| _d S rJ   )canonical_namerD   minimum_ttlcnames)r   r   rD   r   r   r	   r	   r
   r;        
zChainingResult.__init__N)r   r   r   r   r;   r	   r	   r	   r
   r     s    r   c                   @   s   e Zd Zdd Zdd ZdS )QueryMessagec                 C   s  | j tj j@ dkrtt| jdkrtjj| jd }|j}tj	j
}d}d}g }|tk rz| | j||j|j}t||j	}W nA ty   |jtjjkr~z&| | j||jtjj}|| t||j	}|D ]}|j} |d7 }W Y q( ty}   Y Y nw Y nw |tkrt|  tjjkr|durt|du r|}		 z| | j|	|jtjj}
t||
j	|
d j}W n ty   z|	 }	W n tjjy   Y Y nw Y nw qt||||S )a  Follow the CNAME chain in the response to determine the answer
        RRset.

        Raises ``dns.message.NotQueryResponse`` if the message is not
        a response.

        Raises ``dns.message.ChainTooLong`` if the CNAME chain is too long.

        Raises ``dns.message.AnswerForNXDOMAIN`` if the rcode is NXDOMAIN
        but an answer was found.

        Raises ``dns.exception.FormError`` if the question count is not 1.

        Returns a ChainingResult object.
        r   r   N) r0   r,   ro   r   ru   r=   	exception	FormErrorrb   r   MAX_TTL	MAX_CHAINr   rD   r   r   minr   r   CNAMEr   targetr   rT   NXDOMAINr   rE   SOAminimumparentNoParentr   )r   r=   qnamemin_ttlrD   r   r   crrsetrdaunamesrrsetr	   r	   r
   resolve_chaining  sx   



zQueryMessage.resolve_chainingc                 C   s
   |   jS )a  Return the canonical name of the first name in the question
        section.

        Raises ``dns.message.NotQueryResponse`` if the message is not
        a response.

        Raises ``dns.message.ChainTooLong`` if the CNAME chain is too long.

        Raises ``dns.message.AnswerForNXDOMAIN`` if the rcode is NXDOMAIN
        but an answer was found.

        Raises ``dns.exception.FormError`` if the question count is not 1.
        )r   r   r   r	   r	   r
   r   :  s   
zQueryMessage.canonical_nameN)r   r   r   r   r   r	   r	   r	   r
   r     s    Jr   c                  C   s   dd l } d S r@   )
dns.update)r,   r	   r	   r
   _maybe_import_updateK  s   r   c                 C   s.   | t jjkrtS | t jjkrt  t jjS tS rJ   )	r,   rS   QUERYr   UPDATEr   updateUpdateMessager*   )rS   r	   r	   r
   _message_factory_from_opcodeT  s   r   c                   @   s8   e Zd ZdZ			dddZdd Zdd	 Zd
d ZdS )_WireReadera  Wire format reader.

    parser: the binary parser
    message: The message object being built
    initialize_message: Callback to set message parsing options
    question_only: Are we only reading the question?
    one_rr_per_rrset: Put each RR into its own RRset?
    keyring: TSIG keyring
    ignore_trailing: Ignore trailing junk at end of request?
    multi: Is this message part of a multi-message sequence?
    DNS dynamic updates.
    FNc                 C   s<   t j|| _d | _|| _|| _|| _|| _|| _	|| _
d S rJ   )r,   wireParserparserr   initialize_messagequestion_onlyone_rr_per_rrsetignore_trailingr4   r   )r   r   r   r   r   r   r4   r   r	   r	   r
   r;   m  s   
z_WireReader.__init__c              	   C   sp   | j j| }t|D ]+}| j| j j}| jd\}}| j ||||\}}}}| j j||||ddd q
dS )zhRead the next *qcount* records from the wire data and add them to
        the question section.
        z!HHTr   r   N)	r   r1   ranger   get_namer8   
get_structr   r   )r   rz   qcountri   _r   r   r   r	   r	   r
   _get_questiony  s   
z_WireReader._get_questionc                 C   s~  | j j| }| j}t|D ].}| jj}| j }| j jdur'|| j j}n|}| j	d\}	}
}}|	t
jjt
jjfv rM| j |||||
|	\}
}	}}n| j |||
|	\}
}	}}|rk|dkrdt
jjd}t
jj}n&| j| t
j|
|	| j| j j}W d   n1 sw   Y  | }| j jr|	t
jjkrd}|	t
jjkrt
j|||| j _q|	t
jjkr| jdu rtdt| jtr| j |}t|t!rt
j"#|||j$}nt%| jr| | j |}n| j}|du rtd| || j _t
j"&| jj'|||t(t)) | j j*|| j j+| j,	| j _+t
j|d|| j _"q| j -|||
|	||d|}|dur<|dkr6d}|.|| qdS )zRead the next I{count} records from the wire data and add them to
        the specified section.

        section: the section of the message to which to add records
        count: the number of records to read
        Nz!HHIHr   Tz"got signed message without keyringzkey '%s' unknowni)/r   r1   r   r   r   currentr   r8   r_   r   r,   r   r   r   r   r   r   r   r   restrict_tordatafrom_wire_parserr   r7   r   rd   r   r2   r4   r   re   dictr~   r   r5   r   r   r   validater   r}   r   r6   r9   r   r   r   )r   rz   r   ri   r   rh   rr_startabsolute_namerb   r   r   r   rdlenr   emptyr   r   r   rd   r	   r	   r
   _get_section  s   











z_WireReader._get_sectionc                 C   s   | j  dk r	t| j d\}}}}}}ttj|}||d| _tj	
|| j_	| | j | j| j| _| tj| | jrF| jS | tj| | tj| | tj| | jsg| j  dkrgt| jr{| jjr{| jjs{| jj| j j | jS )zNRead a wire format DNS message and build a dns.message.Message
        object.   z!HHHHHHr/   r   )r   	remainingr   r   r   r,   rS   rp   r   r0   Flagr   r   r   r  r   r$   r   r  r%   r&   r'   r   r   r   r9   r   r   r   )r   r/   r0   r   ancountaucountadcountfactoryr	   r	   r
   read  s*   
z_WireReader.read)FFFNF)r   r   r   r   r;   r  r  r  r	   r	   r	   r
   r   ^  s    
Mr   r+   Fc                    s    fdd}t | ||||	||}z| }W n tjjy6   |jr5|jjtjj@ r5|
r5t|jd w |jtjj@ rE|
rEt|d|S )aT  Convert a DNS wire format message into a message
    object.

    *keyring*, a ``dns.tsig.Key`` or ``dict``, the key or keyring to use
    if the message is signed.

    *request_mac*, a ``bytes``.  If the message is a response to a
    TSIG-signed request, *request_mac* should be set to the MAC of
    that request.

    *xfr*, a ``bool``, should be set to ``True`` if this message is part of
    a zone transfer.

    *origin*, a ``dns.name.Name`` or ``None``.  If the message is part
    of a zone transfer, *origin* should be the origin name of the
    zone.  If not ``None``, names will be relativized to the origin.

    *tsig_ctx*, a ``dns.tsig.HMACTSig`` or ``dns.tsig.GSSTSig`` object, the
    ongoing TSIG context, used when validating zone transfers.

    *multi*, a ``bool``, should be set to ``True`` if this message is
    part of a multiple message sequence.

    *question_only*, a ``bool``.  If ``True``, read only up to
    the end of the question section.

    *one_rr_per_rrset*, a ``bool``.  If ``True``, put each RR into its
    own RRset.

    *ignore_trailing*, a ``bool``.  If ``True``, ignore trailing
    junk at end of the message.

    *raise_on_truncation*, a ``bool``.  If ``True``, raise an exception if
    the TC bit is set.

    Raises ``dns.message.ShortHeader`` if the message is less than 12 octets
    long.

    Raises ``dns.message.TrailingJunk`` if there were octets in the message
    past the end of the proper DNS message, and *ignore_trailing* is ``False``.

    Raises ``dns.message.BadEDNS`` if an OPT record was in the
    wrong section, or occurred more than once.

    Raises ``dns.message.BadTSIG`` if a TSIG record was not the last
    record of the additional data section.

    Raises ``dns.message.Truncated`` if the TC flag is set and
    *raise_on_truncation* is ``True``.

    Returns a ``dns.message.Message``.
    c                    s   | _ | _ | _| _d S rJ   )r6   r7   r8   r9   r   r8   r6   r9   r7   r	   r
   r   )  r   z%from_wire.<locals>.initialize_messager  )	r   r  r,   r   r   r   r0   TCr   )r   r4   r6   r7   r8   r9   r   r   r   r   raise_on_truncationr   readermr	   r  r
   	from_wire  s    9
r  c                   @   sF   e 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S )_TextReaderat  Text format reader.

    tok: the tokenizer.
    message: The message object being built.
    DNS dynamic updates.
    last_name: The most recently read name when building a message object.
    one_rr_per_rrset: Put each RR into its own RRset?
    origin: The origin for relative names
    relativize: relativize names?
    relativize_to: the origin to relativize to.
    FNTc                 C   sh   d | _ tjj||d| _d | _|| _|| _|| _|| _	d | _
d| _d| _t| _d | _tjj| _d| _d S )N
idna_codecrO   r   )r   r,   	tokenizer	Tokenizertok	last_namer   r8   r_   relativize_tor/   rU   rV   r   rX   rT   rS   r   r0   )r   textr   r   r8   r_   r%  r	   r	   r
   r;   O  s   

z_TextReader.__init__c                 C   sx  | j  }|j}|dkr| j  | _n|dkr5	 | j  }| s(| j | n| jtj	|jB | _qn|dkrI| j  | _
| j| j
d> B | _nl|dkrs| j
dk rUd| _
	 | j  }| sf| j | n| jtj|jB | _qVnB|dkr| j  | _| j
dk rd| _
n/|d	kr| j  }tj	|| _| jtj| jB | _n|d
kr| j  }tj	|| _nt| j   dS )z5Process one line from the text format header section.r/   r0   TrU   r)   eflagsr   rX   rS   rT   N)r#  r~   r   get_intr/   is_identifierungetr0   r,   r   rU   rV   edns_from_textrX   
get_stringrS   r   rT   r   get_eol)r   r  tokenwhatr&  r	   r	   r
   _header_line`  sV   






z_TextReader._header_linec                 C   s  | j j| }| jjdd}| s| j|| j j| j| j| _	| j	}|du r(t
| j }| s5tjjztj|j}| j }| sJtjjW n tjjyX   tjj tyd   tjj}Y nw tj|j}| j ||||\}}}}| j j||||ddd | j  dS )z7Process one line from the text format question section.Twant_leadingNr   )r   r1   r#  r~   is_whitespaceas_namer8   r_   r%  r$  r   r)  r,   r   SyntaxErrorr   r   r   	ExceptionINr   r   r   r-  )r   rz   ri   r.  rb   r   r   r  r	   r	   r
   _question_line  s>   


z_TextReader._question_linec              
   C   s   | j j| }| jjdd}| s| j|| j j| j| j| _	| j	}|du r(t
| j }| s5tjjzt|jd}| j }| sItjjW n tjjyW   tjj tya   d}Y nw ztj|j}| j }| swtjjW n tjjy   tjj ty   tjj}Y nw tj|j}| j ||||\}}}}	| j }|	r| stjj|	s| rtjj| s| j| tj||| j| j j| j| j}
|
 }nd}
tjj}| j ||||||d| j}|
dur||
| dS dS )zfProcess one line from the text format answer, authority, or
        additional data sections.
        Tr1  Nr   ) r   r1   r#  r~   r3  r4  r8   r_   r%  r$  r   r)  r,   r   r5  r}   r   r6  r   r   r7  r   r   is_eol_or_eofUnexpectedEndr*  r  r   r   r   r   r   )r   rz   ri   r.  rb   r   r   r   r   r  r   r   rd   r	   r	   r
   _rr_line  sv   







z_TextReader._rr_linec                 C   s`   t | j}|| jd}| j|_| jdkr|| j| j| j | jr'|	| j | j
r.| j
|_
|S )Nr  r   )r   rS   r/   r0   rU   r   rV   rX   rT   r   r8   )r   r  r   r	   r	   r
   _make_message  s   

z_TextReader._make_messagec                 C   s   | j }d}	 | jdd}| rnW| r^|j }|dkr"| j }| jr)| j}n|  }z!|j	
|}| jsA|| _|| j| _|tjkrJ| j}n| j}W n	 tyW   Y nw | j  q| j| || q| jsq|  | _| jS )zNRead a text format DNS message and build a dns.message.Message
        object.Nr   THEADER)r0  r#  r~   r9  
is_commentr   upperr   r<  rZ   r   r   r   r   r$   r8  r;  r6  r-  r*  )r   line_methodrz   r.  ur   r	   r	   r
   r    sD   



"
z_TextReader.read)FNTN)
r   r   r   r   r;   r0  r8  r;  r<  r  r	   r	   r	   r
   r  A  s    
*!>r  Tc                 C   s   t | |||||}| S )a  Convert the text format message into a message object.

    The reader stops after reading the first blank line in the input to
    facilitate reading multiple messages from a single file with
    ``dns.message.from_file()``.

    *text*, a ``str``, the text format message.

    *idna_codec*, a ``dns.name.IDNACodec``, specifies the IDNA
    encoder/decoder.  If ``None``, the default IDNA 2003 encoder/decoder
    is used.

    *one_rr_per_rrset*, a ``bool``.  If ``True``, then each RR is put
    into its own rrset.  The default is ``False``.

    *origin*, a ``dns.name.Name`` (or ``None``), the
    origin to use for relative names.

    *relativize*, a ``bool``.  If true, name will be relativized.

    *relativize_to*, a ``dns.name.Name`` (or ``None``), the origin to use
    when relativizing names.  If not set, the *origin* value will be used.

    Raises ``dns.message.UnknownHeaderField`` if a header is unknown.

    Raises ``dns.exception.SyntaxError`` if the text is badly formed.

    Returns a ``dns.message.Message object``
    )r  r  )r&  r   r   r8   r_   r%  r  r	   r	   r
   r   "  s   
$r   c                 C   sP   t  }t| tr|t| } t| ||W  d   S 1 s!w   Y  dS )a  Read the next text format message from the specified file.

    Message blocks are separated by a single blank line.

    *f*, a ``file`` or ``str``.  If *f* is text, it is treated as the
    pathname of a file to open.

    *idna_codec*, a ``dns.name.IDNACodec``, specifies the IDNA
    encoder/decoder.  If ``None``, the default IDNA 2003 encoder/decoder
    is used.

    *one_rr_per_rrset*, a ``bool``.  If ``True``, then each RR is put
    into its own rrset.  The default is ``False``.

    Raises ``dns.message.UnknownHeaderField`` if a header is unknown.

    Raises ``dns.exception.SyntaxError`` if the text is badly formed.

    Returns a ``dns.message.Message object``
    N)
contextlib	ExitStackre   r   enter_contextopenr   )fr   r   stackr	   r	   r
   	from_fileK  s
   


$rH  c
                 C   s   t | trtjj| |	d} tjj|}tjj	|}t
 }
|
 jtjjO  _|
j|
j| ||ddd i }|dur=||d< |durE||d< |durM||d< |durU||d< |r]|du r]d	}||d
< |
jdi | |
| |
S )a  Make a query message.

    The query name, type, and class may all be specified either
    as objects of the appropriate type, or as strings.

    The query will have a randomly chosen query id, and its DNS flags
    will be set to dns.flags.RD.

    qname, a ``dns.name.Name`` or ``str``, the query name.

    *rdtype*, an ``int`` or ``str``, the desired rdata type.

    *rdclass*, an ``int`` or ``str``,  the desired rdata class; the default
    is class IN.

    *use_edns*, an ``int``, ``bool`` or ``None``.  The EDNS level to use; the
    default is None (no EDNS).
    See the description of dns.message.Message.use_edns() for the possible
    values for use_edns and their meanings.

    *want_dnssec*, a ``bool``.  If ``True``, DNSSEC data is desired.

    *ednsflags*, an ``int``, the EDNS flag values.

    *payload*, an ``int``, is the EDNS sender's payload field, which is the
    maximum size of UDP datagram the sender can handle.  I.e. how big
    a response to this message can be.

    *request_payload*, an ``int``, is the EDNS payload size to use when
    sending this message.  If not specified, defaults to the value of
    *payload*.

    *options*, a list of ``dns.edns.Option`` objects or ``None``, the EDNS
    options.

    *idna_codec*, a ``dns.name.IDNACodec``, specifies the IDNA
    encoder/decoder.  If ``None``, the default IDNA 2003 encoder/decoder
    is used.

    Returns a ``dns.message.QueryMessage``
    r  Tr   NrV   rX   r3   rY   r   rU   r	   )re   r   r,   rb   r   r   	RdataTyper{   r   
RdataClassr   r0   RDr   r=   r   r   )r   r   r   r   r   rV   rX   r3   rY   r   r  r   r	   r	   r
   
make_queryg  s0   
-
rL      r   c              	   C   s   | j tj j@ rtjdt|  }|| jd}tj j| j tj j@ B |_ |r0| j tj j	O  _ |
|   t| j|_| jdkrK|dd|| j | jr`|| j| j|d|d| j | j|_|S )a`  Make a message which is a response for the specified query.
    The message returned is really a response skeleton; it has all
    of the infrastructure required of a response, but none of the
    content.

    The response's question section is a shallow copy of the query's
    question section, so the query's question RRsets should not be
    changed.

    *query*, a ``dns.message.Message``, the query to respond to.

    *recursion_available*, a ``bool``, should RA be set in the response?

    *our_payload*, an ``int``, the payload size to advertise in EDNS
    responses.

    *fudge*, an ``int``, the TSIG time fudge.

    *tsig_error*, an ``int``, the TSIG error.

    Returns a ``dns.message.Message`` object whose specific class is
    appropriate for the query.  For example, if query is a
    ``dns.update.UpdateMessage``, response will be too.
    z&specified query message is not a queryr  r   Nr+   )r0   r,   ro   r   r   r   rS   r/   rK  RAr   listr=   rU   r   rX   r   r   r4   r   r   r   r6   )queryrecursion_availableour_payloadr   r   r  responser	   r	   r
   make_response  s"   
rT  )
Nr+   FNNFFFFF)NFNTNr   )FrM  r   r   )=r   rB  rP   r   dns.wirer,   dns.ednsdns.enumdns.exception	dns.flagsdns.name
dns.opcodedns.entropy	dns.rcode	dns.rdatadns.rdataclassdns.rdatatype	dns.rrsetdns.rendererdns.ttldns.tsigdns.rdtypes.ANY.OPTdns.rdtypes.ANY.TSIGr   r   r   r   DNSExceptionr   r   r   r   r   r   r   r   r5  r   enumIntEnumr   r   r   r*   r   r   r   r   r   r  r  r   rH  r   r7  rL  rT  r$   r%   r&   r'   r	   r	   r	   r
   <module>   s       h\	
 
Q b

)
I
.
