
    g+                         S r SSKrSSKrSSKrSSKrSSKrSSKrSSKrSr	Sr
SrSr\R                  S 5       r " S S5      rg)	z*Help for building DNS wire format messages    N         c              #     #    U R                  SU-  5        U R                  5       nS v   U R                  5       nX2-
  nUS:  aI   U R                  X!-
  5         U R                  UR                  US5      5         U R                  U5        g g ! [         a    [
        R                  R                  ef = f! U R                  U5        f = f7f)N    r   big)writetellseekto_bytesOverflowErrordns	exception	FormError)outputlength_lengthstartendlengths        E/home/matz/Project1/venv/lib/python3.13/site-packages/dns/renderer.pyprefixed_lengthr   #   s     
LL-'(KKME	
++-C[Fz	KK-..V__]EBC KK 
 ! .mm---. KKs0   ACB7 !B <C%B44B7 7C

Cc                   `   \ rS rSrSrSS jrS rS r\R                  S 5       r
\R                  S 5       r\R                  R                  4S	 jrS
 rS rSS jrSS jr\R(                  R*                  4S jr\R(                  R*                  4S jrS rS rS rS\SS4S jrSS jrSrg)Renderer5   a&  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                 >   [         R                  " 5       U l        Uc  [        R                  " SS5      U l        OXl        X l        X0l        X@l        0 U l	        [        U l        / SQU l        U R                  R                  S5        SU l        SU l        SU l        g)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__Renderer.__init__b   sx     jjl:nnQ.DGG
 ",'    c                    U R                   R                  U5        U R                   R                  5         / nU R                  R	                  5        H  u  p4XA:  d  M  UR                  U5        M     U H  nU R                  U	 M     g)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   	_rollbackRenderer._rollbacku   so     	MM'')DAz%%a( *  Aa   r0   c                     U R                   U:w  a1  U R                   U:  a  [        R                  R                  eXl         gg)zSet 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_sectionRenderer._set_section   s6     <<7"||g%mm---"L #r0   c              #      #    U R                   R                  5       nUv   U R                   R                  5       U R                  :  a+  U R                  U5        [        R
                  R                  eg 7fN)r   r
   r$   r9   r   r   TooBig)r-   r   s     r   _track_sizeRenderer._track_size   sV       ";;-NN5!--&&& .s   A3A5c              #      #    U R                   R                  5       n U R                   R                  U5        S v   U R                   R                  U5        g ! U R                   R                  U5        f = f7fr?   )r   r
   r   )r-   r5   currents      r   _temporarily_seek_toRenderer._temporarily_seek_to   sU     ++""$	&KKU#KKW%DKKW%s   A9A A9A66A9c                 z   U R                  [        5        U R                  5          UR                  U R                  U R
                  U R                  5        U R                  R                  [        R                  " SX#5      5        SSS5        U R                  [        ==   S-  ss'   g! , (       d  f       N*= f)zAdd a question to the message.z!HHNr   )r<   r'   rA   to_wirer   r&   r%   r	   structpackr)   )r-   qnamerdtyperdclasss       r   add_questionRenderer.add_question   s{     	(#MM$++t}}dkkBKKfkk%AB   	H"  s   A"B,,
B:c                    U R                  U5        U R                  5          UR                  " U R                  U R                  U R
                  40 UD6nSSS5        U R                  U==   W-  ss'   g! , (       d  f       N&= f)zrAdd the rrset to the specified section.

Any keyword arguments are passed on to the rdataset's to_wire()
routine.
Nr<   rA   rH   r   r&   r%   r)   )r-   r(   rrsetkwns        r   	add_rrsetRenderer.add_rrset   sc     	'"dkk4==$++LLA  G!     4A66
Bc                    U R                  U5        U R                  5          UR                  " X R                  U R                  U R
                  40 UD6nSSS5        U R                  U==   W-  ss'   g! , (       d  f       N&= f)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.
NrQ   )r-   r(   namerdatasetrS   rT   s         r   add_rdatasetRenderer.add_rdataset   sf     	'"  {{DMM4;;URTUA  G!  rW   c                    U(       a  UR                   nUS:  d   eUS   nU R                  R                  5       U-   U-   nXr-  nU(       a  SX(-
  -  nOSn[        UR                  5      n	U	R                  [        R                  R                  [        R                  R                  R                  U5      5        [        R                  R                  R                  XVR                  U	5      nSU l        U R!                  ["        U5        g)a-  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   r0   TN)ttlr   r
   listoptionsr4   r   ednsGenericOption
OptionTypePADDINGmessageMessage	_make_optrM   r,   rU   
ADDITIONAL)
r-   optpadopt_size	tsig_sizer_   	opt_rdatasize_without_padding	remainderra   s
             r   add_optRenderer.add_opt   s     ''Cr>!>AI#';;#3#3#5#@9#L ,2I19,,-GNN38811#((2E2E2M2MsST++%%//5F5FPC"DOz3'r0   c                     US-  nX!S-  -  n[         R                  R                  R                  X#U5      nU R	                  U5        g)z&Add an EDNS OPT record to the message.l   ~    N)r   rf   rg   rh   rq   )r-   rb   	ednsflagspayloadra   rj   s         r   add_ednsRenderer.add_edns   sB     	Z	RZ	kk!!++IHSr0   c	           
         U R                   R                  5       n	[        U[        R                  R
                  5      (       a  Un
O [        R                  R                  XU5      n
[        R                  R                  R                  XSUSXEU5      n[        R                  R                  XUS   [        [        R                  " 5       5      U5      u  pU R                  X5        g)z$Add a TSIG signature to the message.r   r0   Nr   getvalue
isinstancer   tsigKeyrf   rg   
_make_tsigsigninttime_write_tsig)r-   keynamesecretfudger"   
tsig_error
other_datarequest_mac	algorithmskeyr}   _s                r   add_tsigRenderer.add_tsig   s     KK  "fchhll++C((,,w	:C{{""--5#rz
 HHMM!$q'3tyy{3C[Q	'r0   c
           
         U R                   R                  5       n
[        U[        R                  R
                  5      (       a  UnO [        R                  R                  X#U	5      n[        R                  R                  R                  X)SUSXVU5      n[        R                  R                  XUS   [        [        R                  " 5       5      XS5      u  pU R                  X5        U$ )aQ  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   r0   Trz   )r-   ctxr   r   r   r"   r   r   r   r   r   r   r}   s                r   add_multi_tsigRenderer.add_multi_tsig   s    ( KK  "fchhll++C((,,w	:C{{""--5#rz
 hhmmDGS-{
 	'
r0   c           	      v   U R                   (       a  S nOU R                  nU R                  [        5        U R	                  5          UR                  U R                  X0R                  5        U R                  R                  [        R                  " S[        R                  R                  [        R                  R                  S5      5        [!        U R                  S5         UR                  U R                  5        S S S 5        S S S 5        U R"                  [        ==   S-  ss'   U R%                  S5         U R                  R                  [        R                  " SU R"                  [           5      5        S S S 5        g ! , (       d  f       N= f! , (       d  f       N= f! , (       d  f       g = f)Nz!HHIr   r   r   
   z!H)r,   r&   r<   ri   rA   rH   r   r%   r	   rI   rJ   r   	rdatatypeTSIG
rdataclassANYr   r)   rE   )r-   r}   r   r&   s       r   r   Renderer._write_tsig"  s   ??H}}H*%OODKK;;?KKFCMM$6$68J8JAN !a0T[[) 1   	J1$&&r*KKfkk$J0GHI +*	 10   +*s2   BF%FF=AF*
F	F
F'*
F8c                 T   U R                  S5         U R                  R                  [        R                  " SU R
                  U R                  U R                  S   U R                  S   U R                  S   U R                  S   5      5        SSS5        g! , (       d  f       g= f)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)rE   r   r	   rI   rJ   r"   r#   r)   r-   s    r   write_headerRenderer.write_header4  sy     &&q)KKGGJJKKNKKNKKNKKN
 *))s   A>B
B'c                 6    U R                   R                  5       $ )zReturn the wire format message.)r   r{   r   s    r   get_wireRenderer.get_wireI  s     {{##%%r0   sizereturnc                     US:  a  [        S5      eXR                  :  a  [        S5      eU =R                  U-  sl        U =R                  U-  sl        g)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   reserveRenderer.reserveN  sG    !8CDD--HIIr0   c                 P    U =R                   U R                  -  sl         SU l        g)zRelease the reserved bytes.r   N)r$   r+   r   s    r   release_reservedRenderer.release_reservedW  s    &r0   )r&   r)   r#   r"   r*   r$   r%   r   r+   r(   r,   )Nr   r   N)r   r   r   r?   )r   N)__name__
__module____qualname____firstlineno____doc__r.   r9   r<   
contextlibcontextmanagerrA   rE   r   r   INrN   rU   r[   rq   rw   r}   default_algorithmr   r   r   r   r   r   r   r   __static_attributes__ r0   r   r   r   5   s    *X &!# ' ' & & 36..2C2C #
""(.$ ((,,(F ((,,!FJ$*&
C D r0   r   )r   r   r   r    rI   r   dns.exceptionr   dns.tsigr'   ANSWER	AUTHORITYri   r   r   r   r   r0   r   <module>r      sY   $ 1  	     	
	
  "e er0   