
    ,h+                         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        Z G d d      Zy)	z*Help for building DNS wire format messages    N         c              #     K   | j                  d|z         | j                         }d  | j                         }||z
  }|dkD  rJ	 | j                  ||z
         	 | j                  |j                  |d             	 | j                  |       y y # t        $ r t
        j                  j                  w xY w# | j                  |       w xY ww)N    r   big)writetellseekto_bytesOverflowErrordns	exception	FormError)outputlength_lengthstartendlengths        N/var/www/html/Resume-Scraper/venv/lib/python3.12/site-packages/dns/renderer.pyprefixed_lengthr   #   s     
LL-'(KKME	
++-C5[Fz	KK-..V__]EBC KK 
 ! .mm---. KKs0   ACB7 !B =C$B44B7 7C

Cc                   H   e Zd ZdZddZd Zd Zej                  d        Z	ej                  d        Z
ej                  j                  fdZd	 Zd
 ZddZddZej&                  j(                  fdZej&                  j(                  fdZd Zd Zd ZdeddfdZddZy)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)
    Nc                 @   t        j                         | _        |t        j                  dd      | _        n|| _        || _        || _        || _        i | _	        t        | _        g d| _        | j                  j                  d       d| _        d| _        d| _        y)zInitialize a new renderer.Nr     )r   r   r   r   s                F)ioBytesIOr   randomrandintidflagsmax_sizeorigincompressQUESTIONsectioncountsr	   macreserved
was_padded)selfr!   r"   r#   r$   s        r   __init__zRenderer.__init__b   s     jjl:nnQ.DGDG
 ",'    c                    | j                   j                  |       | j                   j                          g }| j                  j	                         D ]  \  }}||k\  s|j                  |        |D ]  }| j                  |=  y)zTruncate 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,   wherekeys_to_deletekvs        r   	_rollbackzRenderer._rollbacku   s}     	MM'') 	)DAqEz%%a(	)   	!Aa 	!r.   c                     | j                   |k7  r1| j                   |kD  rt        j                  j                  || _         yy)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'   s     r   _set_sectionzRenderer._set_section   s8     <<7"||g%mm---"DL #r.   c              #      K   | j                   j                         }| | j                   j                         | j                  kD  r+| j                  |       t        j
                  j                  y wN)r   r
   r#   r7   r   r   TooBig)r,   r   s     r   _track_sizezRenderer._track_size   sW       ";;-NN5!--&&& .s   A2A4c              #      K   | j                   j                         }	 | j                   j                  |       d  | j                   j                  |       y # | j                   j                  |       w xY wwr;   )r   r
   r   )r,   r3   currents      r   _temporarily_seek_tozRenderer._temporarily_seek_to   sV     ++""$	&KKU#KKW%DKKW%s   A9A A9A66A9c                 p   | j                  t               | j                         5  |j                  | j                  | j
                  | j                         | j                  j                  t        j                  d||             ddd       | j                  t        xx   dz  cc<   y# 1 sw Y   %xY w)zAdd a question to the message.z!HHNr   )r9   r&   r=   to_wirer   r%   r$   r	   structpackr(   )r,   qnamerdtyperdclasss       r   add_questionzRenderer.add_question   s     	(# 	CMM$++t}}dkkBKKfkk%AB	C 	H"	C 	Cs   A"B,,B5c                    | j                  |       | j                         5   |j                  | j                  | j                  | j
                  fi |}ddd       | j                  |xx   z  cc<   y# 1 sw Y   !xY w)zAdd the rrset to the specified section.

        Any keyword arguments are passed on to the rdataset's to_wire()
        routine.
        Nr9   r=   rB   r   r%   r$   r(   )r,   r'   rrsetkwns        r   	add_rrsetzRenderer.add_rrset   sp     	'" 	Mdkk4==$++LLA	MG!	M 	Ms   4A66A?c                    | j                  |       | j                         5   |j                  || j                  | j                  | j
                  fi |}ddd       | j                  |xx   z  cc<   y# 1 sw Y   !xY w)zAdd 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.
        NrJ   )r,   r'   namerdatasetrL   rM   s         r   add_rdatasetzRenderer.add_rdataset   su     	'" 	V   t{{DMM4;;URTUA	VG!	V 	Vs   5A77B c                    |r|j                   }|dk\  sJ |d   }| j                  j                         |z   |z   }||z  }|r	d||z
  z  }nd}t        |j                        }	|	j                  t        j                  j                  t        j                  j                  j                  |             t        j                  j                  j                  ||j                  |	      }d| _        | j!                  t"        |       y)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   r.   TN)ttlr   r
   listoptionsr2   r   ednsGenericOption
OptionTypePADDINGmessageMessage	_make_optrG   r+   rN   
ADDITIONAL)
r,   optpadopt_size	tsig_sizerU   	opt_rdatasize_without_padding	remainderrW   s
             r   add_optzRenderer.add_opt   s     ''Cr>!AI#';;#3#3#5#@9#L ,s2Iy19,,-GNN38811#((2E2E2M2MsST++%%//Y5F5FPC"DOz3'r.   c                     |dz  }||dz  z  }t         j                  j                  j                  |||      }| j	                  |       y)z&Add an EDNS OPT record to the message.l   ~    N)r   r\   r]   r^   rg   )r,   rX   	ednsflagspayloadrW   r`   s         r   add_ednszRenderer.add_edns   sF     	Z	TRZ	kk!!++IwHSr.   c	           
         | j                   j                         }	t        |t        j                  j
                        r|}
n!t        j                  j                  |||      }
t        j                  j                  j                  ||d|d|||      }t        j                  j                  |	|
|d   t        t        j                               |      \  }}| j                  ||       y)z$Add a TSIG signature to the message.r   r.   Nr   getvalue
isinstancer   tsigKeyr\   r]   
_make_tsigsigninttime_write_tsig)r,   keynamesecretfudger!   
tsig_error
other_datarequest_mac	algorithmskeyrq   _s                r   add_tsigzRenderer.add_tsig   s     KK  "fchhll+C((,,w	:C{{""--Y5#r:z
 HHMM!S$q'3tyy{3C[Q	qw'r.   c
           
         | j                   j                         }
t        |t        j                  j
                        r|}n!t        j                  j                  |||	      }t        j                  j                  j                  ||	d|d|||      }t        j                  j                  |
||d   t        t        j                               ||d      \  }}| j                  ||       |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   r.   Trn   )r,   ctxrx   ry   rz   r!   r{   r|   r}   r~   r   r   rq   s                r   add_multi_tsigzRenderer.add_multi_tsig   s    ( KK  "fchhll+C((,,w	:C{{""--Y5#r:z
 hhmmsDGS-{C
s 	w'
r.   c           	      L   | j                   rd }n| j                  }| j                  t               | j	                         5  |j                  | j                  || j                         | j                  j                  t        j                  dt        j                  j                  t        j                  j                  d             t!        | j                  d      5  |j                  | j                         d d d        d d d        | j"                  t        xx   dz  cc<   | j%                  d      5  | j                  j                  t        j                  d| j"                  t                        d d d        y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   y xY w)Nz!HHIr   r   r   
   z!H)r+   r%   r9   r_   r=   rB   r   r$   r	   rC   rD   r   	rdatatypeTSIG
rdataclassANYr   r(   r@   )r,   rq   rx   r%   s       r   rw   zRenderer._write_tsig"  s4   ??H}}H*% 	*OODKK4;;?KKFCMM$6$68J8JAN !a0 *T[[)*	* 	J1$&&r* 	JKKfkk$J0GHI	J 	J	* *	* 	*	J 	Js2   BF F<F8AFF	FFF#c                 H   | j                  d      5  | j                  j                  t        j                  d| j
                  | j                  | j                  d   | j                  d   | j                  d   | j                  d                ddd       y# 1 sw Y   yxY w)zWrite 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)r@   r   r	   rC   rD   r!   r"   r(   r,   s    r   write_headerzRenderer.write_header4  s     &&q) 	KKGGJJKKNKKNKKNKKN
	 	 	s   A=BB!c                 6    | j                   j                         S )zReturn the wire format message.)r   ro   r   s    r   get_wirezRenderer.get_wireI  s     {{##%%r.   sizereturnc                     |dk  rt        d      || j                  kD  rt        d      | xj                  |z  c_        | xj                  |z  c_        y)zReserve *size* bytes.r   z$reserved amount must be non-negativez)cannot reserve more than the maximum sizeN)
ValueErrorr#   r*   )r,   r   s     r   reservezRenderer.reserveN  sI    !8CDD$--HIIr.   c                 P    | xj                   | j                  z  c_         d| _        y)zRelease the reserved bytes.r   N)r#   r*   r   s    r   release_reservedzRenderer.release_reservedW  s    &r.   )Nr   r   N)r   r   r   r;   )r   N)__name__
__module____qualname____doc__r-   r7   r9   
contextlibcontextmanagerr=   r@   r   r   INrH   rN   rR   rg   rl   rq   default_algorithmr   r   rw   r   r   ru   r   r    r.   r   r   r   5   s    *X &!# ' ' & & 36..2C2C #
""(.$ ((,,(F ((,,!FJ$*&
C D r.   r   )r   r   r   r   rC   rv   dns.exceptionr   dns.tsigr&   ANSWER	AUTHORITYr_   r   r   r   r   r.   r   <module>r      sY   $ 1  	     	
	
  "e er.   