U
    <æ{fö+  ã                   @   sl   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Z	dZ
dZdZejdd„ ƒZG dd	„ d	ƒZdS )
z*Help for building DNS wire format messagesé    Né   é   é   c              	   c   sŒ   |   d| ¡ |  ¡ }d V  |  ¡ }|| }|dkrˆzF|  || ¡ z|   | |d¡¡ W n tk
rv   tjj‚Y nX W 5 |  |¡ X d S )Nó    r   Úbig)ÚwriteÚtellÚseekÚto_bytesÚOverflowErrorÚdnsÚ	exceptionÚ	FormError)ÚoutputZlength_lengthÚstartÚendÚlength© r   úK/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/dns/renderer.pyÚprefixed_length#   s    r   c                   @   sÐ   e Zd ZdZd)dd„Zdd„ Zd	d
„ Zejdd„ ƒZ	ejdd„ ƒZ
ejjfdd„Zdd„ Zdd„ Zd*dd„Zd+dd„Zejjfdd„Zejjfdd„Zdd„ Zdd „ Zd!d"„ Zedd#œd$d%„Zdd&œd'd(„ZdS ),ÚRendereraš  Helper class for building DNS wire-format messages.

    Most applications can use the higher-level L{dns.message.Message}
    class and its to_wire() method to generate wire-format messages.
    This class is for those applications which need finer control
    over the generation of messages.

    Typical use::

        r = dns.renderer.Renderer(id=1, flags=0x80, max_size=512)
        r.add_question(qname, qtype, qclass)
        r.add_rrset(dns.renderer.ANSWER, rrset_1)
        r.add_rrset(dns.renderer.ANSWER, rrset_2)
        r.add_rrset(dns.renderer.AUTHORITY, ns_rrset)
        r.add_rrset(dns.renderer.ADDITIONAL, ad_rrset_1)
        r.add_rrset(dns.renderer.ADDITIONAL, ad_rrset_2)
        r.add_edns(0, 0, 4096)
        r.write_header()
        r.add_tsig(keyname, secret, 300, 1, 0, '', request_mac)
        wire = r.get_wire()

    If padding is going to be used, then the OPT record MUST be
    written after everything else in the additional section except for
    the TSIG (if any).

    output, an io.BytesIO, where rendering is written

    id: the message id

    flags: the message flags

    max_size: the maximum size of the message

    origin: the origin to use when rendering relative names

    compress: the compression table

    section: an int, the section currently being rendered

    counts: list of the number of RRs in each section

    mac: the MAC of the rendered message (if TSIG was used)
    Nr   éÿÿ  c                 C   sv   t  ¡ | _|dkr"t dd¡| _n|| _|| _|| _|| _i | _	t
| _ddddg| _| j d¡ d| _d| _d| _dS )zInitialize a new renderer.Nr   r   s               Ú F)ÚioÚBytesIOr   ÚrandomÚrandintÚidÚflagsÚmax_sizeÚoriginÚcompressÚQUESTIONÚsectionÚcountsr   ÚmacÚreservedÚ
was_padded)Úselfr   r   r   r    r   r   r   Ú__init__b   s    
zRenderer.__init__c                 C   sV   | j  |¡ | j  ¡  g }| j ¡ D ]\}}||kr$| |¡ q$|D ]}| j|= qDdS )z™Truncate the output buffer at offset *where*, and remove any
        compression table entries that pointed beyond the truncation
        point.
        N)r   r	   Útruncater!   ÚitemsÚappend)r(   ÚwhereZkeys_to_deleteÚkÚvr   r   r   Ú	_rollbacku   s    
zRenderer._rollbackc                 C   s&   | j |kr"| j |krtjj‚|| _ dS )a  Set the renderer's current section.

        Sections must be rendered order: QUESTION, ANSWER, AUTHORITY,
        ADDITIONAL.  Sections may be empty.

        Raises dns.exception.FormError if an attempt was made to set
        a section value less than the current section.
        N)r#   r   r   r   )r(   r#   r   r   r   Ú_set_section„   s    


zRenderer._set_sectionc                 c   s6   | j  ¡ }|V  | j  ¡ | jkr2|  |¡ tjj‚d S ©N)r   r   r   r0   r   r   ZTooBig)r(   r   r   r   r   Ú_track_size“   s
    

zRenderer._track_sizec              	   c   s4   | j  ¡ }z| j  |¡ d V  W 5 | j  |¡ X d S r2   )r   r   r	   )r(   r-   Úcurrentr   r   r   Ú_temporarily_seek_to›   s
    

zRenderer._temporarily_seek_toc              	   C   s^   |   t¡ |  ¡ 0 | | j| j| j¡ | j t 	d||¡¡ W 5 Q R X | j
t  d7  < dS )zAdd a question to the message.z!HHr   N)r1   r"   r3   Úto_wirer   r!   r    r   ÚstructÚpackr$   )r(   ZqnameZrdtypeÚrdclassr   r   r   Úadd_question¤   s
    

 zRenderer.add_questionc              	   K   sL   |   |¡ |  ¡  |j| j| j| jf|Ž}W 5 Q R X | j|  |7  < dS )zŠAdd the rrset to the specified section.

        Any keyword arguments are passed on to the rdataset's to_wire()
        routine.
        N©r1   r3   r6   r   r!   r    r$   )r(   r#   ZrrsetÚkwÚnr   r   r   Ú	add_rrset­   s    

"zRenderer.add_rrsetc              	   K   sN   |   |¡ |  ¡   |j|| j| j| jf|Ž}W 5 Q R X | j|  |7  < dS )zÁAdd the rdataset to the specified section, using the specified
        name as the owner name.

        Any keyword arguments are passed on to the rdataset's to_wire()
        routine.
        Nr;   )r(   r#   ÚnameZrdatasetr<   r=   r   r   r   Úadd_rdataset¹   s    

$zRenderer.add_rdatasetc           
      C   sœ   |rŒ|j }|dkst‚|d }| j ¡ | | }|| }|rJd||  }nd}t|jƒ}	|	 tj 	tjj
j|¡¡ tjj ||j|	¡}d| _|  t|¡ dS )aM  Add *opt* to the additional section, applying padding if desired.  The
        padding will take the specified precomputed OPT size and TSIG size into
        account.

        Note that we don't have reliable way of knowing how big a GSS-TSIG digest
        might be, so we we might not get an even multiple of the pad in that case.é   r   r   ó    TN)ÚttlÚAssertionErrorr   r   ÚlistÚoptionsr,   r   ÚednsZGenericOptionZ
OptionTypeZPADDINGÚmessageÚMessageÚ	_make_optr9   r'   r>   Ú
ADDITIONAL)
r(   ÚoptÚpadZopt_sizeZ	tsig_sizerC   Z	opt_rdataZsize_without_paddingÚ	remainderrF   r   r   r   Úadd_optÆ   s    
zRenderer.add_optc                 C   s4   |dM }||d> O }t jj |||¡}|  |¡ dS )z&Add an EDNS OPT record to the message.l   ÿ~ é   N)r   rH   rI   rJ   rO   )r(   rG   Z	ednsflagsÚpayloadrF   rL   r   r   r   Úadd_ednsÝ   s    zRenderer.add_ednsc	              
   C   s~   | j  ¡ }	t|tjjƒr|}
ntj |||¡}
tjj ||d|d|||¡}tj 	|	|
|d t
t ¡ ƒ|¡\}}|  ||¡ dS )z$Add a TSIG signature to the message.r   rB   N©r   ÚgetvalueÚ
isinstancer   ÚtsigÚKeyrH   rI   Z
_make_tsigÚsignÚintÚtimeÚ_write_tsig)r(   ÚkeynameÚsecretÚfudger   Ú
tsig_errorÚ
other_dataÚrequest_macÚ	algorithmÚsÚkeyrV   Ú_r   r   r   Úadd_tsigæ   s     
       ÿ$zRenderer.add_tsigc
              
   C   s‚   | j  ¡ }
t|tjjƒr|}ntj |||	¡}tjj ||	d|d|||¡}tj 	|
||d t
t ¡ ƒ||d¡\}}|  ||¡ |S )ay  Add a TSIG signature to the message. Unlike add_tsig(), this can be
        used for a series of consecutive DNS envelopes, e.g. for a zone
        transfer over TCP [RFC2845, 4.4].

        For the first message in the sequence, give ctx=None. For each
        subsequent message, give the ctx that was returned from the
        add_multi_tsig() call for the previous message.r   rB   TrS   )r(   Úctxr\   r]   r^   r   r_   r`   ra   rb   rc   rd   rV   r   r   r   Úadd_multi_tsigÿ   s2    
       ÿ   
   ÿzRenderer.add_multi_tsigc              
   C   sÌ   | j rd }n| j}|  t¡ |  ¡ \ | | j|| j¡ | j t	 
dtjjtjjd¡¡ t| jdƒ | | j¡ W 5 Q R X W 5 Q R X | jt  d7  < |  d¡  | j t	 
d| jt ¡¡ W 5 Q R X d S )Nz!HHIr   r   r   é
   z!H)r'   r!   r1   rK   r3   r6   r   r    r   r7   r8   r   Z	rdatatypeZTSIGÚ
rdataclassÚANYr   r$   r5   )r(   rV   r\   r!   r   r   r   r[   "  s    

ÿ zRenderer._write_tsigc                 C   sT   |   d¡@ | j t d| j| j| jd | jd | jd | jd ¡¡ W 5 Q R X dS )z¾Write the DNS message header.

        Writing the DNS message header is done after all sections
        have been rendered, but before the optional TSIG signature
        is added.
        r   z!HHHHHHr   r   r   N)r5   r   r   r7   r8   r   r   r$   ©r(   r   r   r   Úwrite_header4  s    ùÿzRenderer.write_headerc                 C   s
   | j  ¡ S )zReturn the wire format message.)r   rT   rl   r   r   r   Úget_wireI  s    zRenderer.get_wire)ÚsizeÚreturnc                 C   sB   |dk rt dƒ‚|| jkr"t dƒ‚|  j|7  _|  j|8  _dS )zReserve *size* bytes.r   z$reserved amount must be non-negativez)cannot reserve more than the maximum sizeN)Ú
ValueErrorr   r&   )r(   ro   r   r   r   ÚreserveN  s    
zRenderer.reserve)rp   c                 C   s   |  j | j7  _ d| _dS )zRelease the reserved bytes.r   N)r   r&   rl   r   r   r   Úrelease_reservedW  s    zRenderer.release_reserved)Nr   r   N)r   r   r   )N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r)   r0   r1   Ú
contextlibÚcontextmanagerr3   r5   r   rj   ÚINr:   r>   r@   rO   rR   rV   Zdefault_algorithmrf   rh   r[   rm   rn   rY   rr   rs   r   r   r   r   r   5   s,   ,


	

÷
#ö
#	r   )rw   rx   r   r   r7   rZ   Zdns.exceptionr   Zdns.tsigr"   ZANSWERZ	AUTHORITYrK   ry   r   r   r   r   r   r   Ú<module>   s   
