f+dZddlZddlZddlZddlZddlZddlZddlZdZ dZ dZ dZ ejdZGddZy) z*Help for building DNS wire format messagesNc#K|jd|z|j}d|j}||z }|dkDrJ |j||z  |j|j|d |j|yy#t$rt j jwxYw#|j|wxYww)Nrbig)writetellseekto_bytes OverflowErrordns exception FormError)output length_lengthstartendlengths U/var/lib/jenkins/workspace/mettalog/venv/lib/python3.12/site-packages/dns/renderer.pyprefixed_lengthr#s LL-'( KKME ++-C 5[F z  KK - . . V__]EBC KK  ! .mm--- . KK s0AC B7!B=C $B44B77C  C cHeZdZdZddZdZdZejdZ ejdZ e jjfdZd Zd Zdd Zdd Ze j&j(fd Ze j&j(fdZdZdZdZdeddfdZddZy)RendereraHelper 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@tj|_|tjdd|_n||_||_||_||_i|_ t|_ gd|_ |jjdd|_d|_d|_y)zInitialize a new renderer.Nr)rrrrs F)ioBytesIOrrandomrandintidflagsmax_sizeorigincompressQUESTIONsectioncountsr macreserved was_padded)selfr!r"r#r$s r__init__zRenderer.__init__bsjjl :nnQ.DGDG      "  ,' c |jj||jjg}|jj 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)rr truncater%itemsappend)r,wherekeys_to_deletekvs r _rollbackzRenderer._rollbackuss  MM'')DAqEz%%a(* A a  r.c|j|k7r1|j|kDrtjj||_yy)aSet 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'rrr)r,r's r _set_sectionzRenderer._set_sections8 <<7 "||g%mm---"DL #r.c#K|jj}||jj|jkDr+|j|tj j ywN)rr r#r7rrTooBig)r,rs r _track_sizezRenderer._track_sizesW   " ;;    - NN5 !--&& & .sA2A4c#K|jj} |jj|d|jj|y#|jj|wxYwwr;)rr r )r,r3currents r_temporarily_seek_tozRenderer._temporarily_seek_tosV++""$ & KK  U #  KK  W %DKK  W %sA9AA9A66A9cp|jt|j5|j|j|j |j |jjtjd||ddd|jtxxdz cc<y#1swY%xYw)zAdd a question to the message.z!HHNr) r9r&r=to_wirerr%r$r structpackr()r,qnamerdtyperdclasss r add_questionzRenderer.add_questions (#     C MM$++t}}dkk B KK  fkk%A B C H" C Cs A"B,,B5c |j||j5|j|j|j|j fi|}ddd|j |xxz cc<y#1swY!xYw)zAdd the rrset to the specified section. Any keyword arguments are passed on to the rdataset's to_wire() routine. Nr9r=rBrr%r$r()r,r'rrsetkwns r add_rrsetzRenderer.add_rrsetsp '"     M dkk4==$++LLA M G! M Ms 4A66A?c |j||j5|j||j|j|j fi|}ddd|j |xxz cc<y#1swY!xYw)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'namerdatasetrLrMs r add_rdatasetzRenderer.add_rdatasetsu '"     V   t{{DMM4;;URTUA V G! V Vs 5A77Bc |r|j}|dk\sJ|d}|jj|z|z}||z}|r d||z z}nd}t|j} | j t jjt jjj|t jjj||j| }d|_|j!t"|y)aMAdd *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. rrr.TN)ttlrr listoptionsr2redns GenericOption OptionTypePADDINGmessageMessage _make_optrGr+rN ADDITIONAL) r,optpadopt_size tsig_sizerU opt_rdatasize_without_padding remainderrWs radd_optzRenderer.add_opts ''Cr> !>AI#';;#3#3#5#@9#L ,s2Iy19,,-G NN38811#((2E2E2M2MsS T++%%//Y5F5FPC"DO z3'r.c|dz}||dzz}tjjj|||}|j |y)z&Add an EDNS OPT record to the message.l~N)rr\r]r^rg)r,rX ednsflagspayloadrWr`s radd_ednszRenderer.add_ednssF Z TRZ kk!!++IwH Sr.c |jj} t|tjj r|} n!tjj |||} tj jj||d|d|||} tjj| | | dttj|\} } |j| |y)z$Add a TSIG signature to the message.rr.N rgetvalue isinstancertsigKeyr\r] _make_tsigsigninttime _write_tsig) r,keynamesecretfudger! tsig_error other_data request_mac algorithmskeyrq_s radd_tsigzRenderer.add_tsigs KK " fchhll +C((,,w :C{{""-- Y5#r:z HHMM!S$q'3tyy{3C[Q q w'r.c |jj} t|tjj r|} n!tjj ||| } tj jj|| d|d|||} tjj| | | dttj||d\} }|j| ||S)ayAdd 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.rr.Trn) r,ctxrxryrzr!r{r|r}r~rrrqs radd_multi_tsigzRenderer.add_multi_tsigs( KK " fchhll +C((,,w :C{{""-- Y5#r:z hhmm sDGS-{C s w' r.c L|jrd}n |j}|jt|j 5|j |j ||j|j jtjdtjjtjjdt!|j d5|j |j dddddd|j"txxdz cc<|j%d5|j jtjd|j"tdddy#1swYxYw#1swYxYw#1swYyxYw)Nz!HHIrrr z!H)r+r%r9r_r=rBrr$r rCrDr rdatatypeTSIG rdataclassANYrr(r@)r,rqrxr%s rrwzRenderer._write_tsig"s4 ??H}}H *%     * OODKK4;; ? KK   FCMM$6$68J8JAN !a0 * T[[) *  * J1$  & &r * J KK  fkk$ J0GH I J J  * *  * * J Js2BF F<F8AFF FFF#c H|jd5|jjtjd|j |j |jd|jd|jd|jddddy#1swYyxYw)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. rz!HHHHHHrrrN)r@rr rCrDr!r"r(r,s r write_headerzRenderer.write_header4s & &q )  KK   GGJJKKNKKNKKNKKN    s A=BB!c6|jjS)zReturn the wire format message.)rrors rget_wirezRenderer.get_wireIs{{##%%r.sizereturnc|dkr td||jkDr td|xj|z c_|xj|zc_y)zReserve *size* bytes.rz$reserved amount must be non-negativez)cannot reserve more than the maximum sizeN) ValueErrorr#r*)r,rs rreservezRenderer.reserveNsI !8CD D $-- HI I    r.cP|xj|jz c_d|_y)zRelease the reserved bytes.rN)r#r*rs rrelease_reservedzRenderer.release_reservedWs &  r.)NrrN)rrrr;)rN)__name__ __module__ __qualname____doc__r-r7r9 contextlibcontextmanagerr=r@rrINrHrNrRrgrlrqdefault_algorithmrrrwrrrurrr.rrr5s*X & ! #''&&36..2C2C# " "(.$((,,(F((,,!FJ$*& CDr.r)rrrrrCrv dns.exceptionrdns.tsigr&ANSWER AUTHORITYr_rrrrr.rrsY$1      "eer.