U mf@sdZddlZddlZddlZddlmZmZmZmZm Z m Z ddl Z ddl Z ddlZ ddlZ ddlZ ddlZ ddlZ ddlZ ddlZ ddlZ ddlZ ddlZ ddlZ ddlZ ddlZ ddlZ ddlZ ddlZ Gddde jjZ Gddde jjZ!Gdd d e jj"Z#Gd d d e jjZ$Gd d d e jjZ%Gddde jj"Z&Gddde jj"Z'Gddde jj"Z(Gddde jj"Z)Gddde jj"Z*Gddde jj+Z,Gddde j-j.Z/GdddZ0dZ1dZ2e e3e j4j5e j6j7e j8j9ee j8j9ee j6j7fZ:ee:e j;jee j;j<fZ?Gd d!d!Z@Gd"d#d#ZAGd$d%d%e@ZBd&d'ZCd(d)ZDGd*d+d+ZEdBeFeeeeFeGee j4j5ee e jHjIe jHjJfeGeGeGeGeGeGe@d. d/d0ZKGd1d2d2ZLdCe>ee j4jMeGee j4j5eGee j4j5e@d4d5d6ZNdDeee j4jMeGe@d7d8d9ZOe j6jPdd-dddddde jQjRdf e e j4j5e>fe e j8j9e>fe e j6j7e>fee e3eGfeGee3ee3ee3eee jSjTee j4jMee3e3e3eBd:d;d<ZUdEe@eGe3e3e3ee3e@d?d@dAZVe/jWZWe/jXZXe/jYZYe/jZZZdS)Fz DNS MessagesN)AnyDictListOptionalTupleUnionc@seZdZdZdS) ShortHeaderz2The DNS packet passed to from_wire() is too short.N__name__ __module__ __qualname____doc__rr9/opt/hc_python/lib/python3.8/site-packages/dns/message.pyr-src@seZdZdZdS) TrailingJunkzEThe DNS packet passed to from_wire() has extra junk at the end of it.Nr rrrrr1src@seZdZdZdS)UnknownHeaderFieldzVThe header field name was not recognized when converting from text into a message.Nr rrrrr5src@seZdZdZdS)BadEDNSzLAn OPT record occurred somewhere other than the additional data section.Nr rrrrr:src@seZdZdZdS)BadTSIGzWA TSIG record occurred somewhere other than the end of the additional data section.Nr rrrrr?src@seZdZdZdS)UnknownTSIGKeyz(A TSIG with an unknown key was received.Nr rrrrrDsrcs.eZdZdZdhZfddZddZZS) TruncatedzThe truncated flag is set.messagecstj||dSN)super__init__)selfargskwargs __class__rrrOszTruncated.__init__cCs |jdS)zbAs much of the message as could be processed. Returns a ``dns.message.Message``. r)rrrrrrRszTruncated.message)r r r r Z supp_kwargsrr __classcell__rrrrrHs rc@seZdZdZdS)NotQueryResponsez%Message is not a response to a query.Nr rrrrr!Zsr!c@seZdZdZdS) ChainTooLongzThe CNAME chain is too long.Nr rrrrr"^sr"c@seZdZdZdS)AnswerForNXDOMAINz.The rcode is NXDOMAIN but an answer was found.Nr rrrrr#bsr#c@seZdZdZdS)NoPreviousNamezNo previous name was known.Nr rrrrr$fsr$c@s,eZdZdZdZdZdZdZeddZ dS) MessageSectionzMessage sectionsrcCsdSNr(r)clsrrr_maximumrszMessageSection._maximumN) r r r r QUESTIONANSWER AUTHORITY ADDITIONAL classmethodr+rrrrr%jsr%c@seZdZeedddZdS) MessageError exceptionoffsetcCs||_||_dSrr2)rr3r4rrrrxszMessageError.__init__N)r r r Exceptionintrrrrrr1wsr1ic @sHeZdZdZeZdjeedddZe e e j j dddZejd dZe e e j j dd d Zejd d Ze e e j j dd dZejddZe e e j j dddZejddZddZddZdkee jjeeeefedddZddZddZdedd d!Ze e j j ed"d#d$Zee e j j d%d&d'Z e j!j"dd(d(dfe#e jje j$j%e j!j&e j!j&ee j$j%eeee jj'e j j d) d*d+Z(e j!j"dd(d(dfe#e jje j$j%e j!j&e j!j&ee j$j%eeee jj'ee j j d) d,d-Z)e#ed"d.d/Z*edd0d1Z+edd2d3Z,dlee jjeeeeeeeeefe-d5d6d7Z.e/d8d9Z0dd:dd4d;e j1j2feee3e jjefeeeee-e3e jjefdd<d=d>Z4e ee jjdd?d@Z5e ee jjddAdBZ6e ee-ddCdDZ7e eeddEdFZ8e eddGdHZ9e/d4e:dfdIdJZ;d4d4e:ddd4fee3eefeeeeee e je eddNdOZrrrrquestionszMessage.questioncCs||jd<dSNrrMrvrrrrNscCs |jdS)zThe answer section.r&rMrrrranswerszMessage.answercCs||jd<dS)Nr&rMrPrrrrRscCs |jdS)zThe authority section.r'rMrrrr authorityszMessage.authoritycCs||jd<dS)Nr'rMrPrrrrSscCs |jdS)zThe additional data section.r(rMrrrr additionalszMessage.additionalcCs||jd<dSr)rMrPrrrrTscCsdt|jdS)Nz)reprr:rrrr__repr__szMessage.__repr__cCs|Sr)to_textrrrr__str__szMessage.__str__T)rF relativizekwrLc Ks6t}|d|j|dtj||dtj||dtj|j|j dkr|d|j |j dkr|dtj |j |d|j |j D]}|d |q|jjD]J\}}|d |d ||D]$}||j||f||d qq|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``. zid %d z opcode %s z rcode %s z flags %s rzedns %s z eflags %s z payload %d z option %s ; N)ioStringIOwriter:r<opcoderXrcoder=edns ednsflagsZ edns_to_textpayloadoptions _section_enum __members__itemssection_from_numbergetvalue) rrFrZr[sr?namewhichrrsetrrrrXs$   zMessage.to_textcCst|tsdS|j|jkrdS|j|jkr.dSt|jD]F\}}|j|}|D]}||krNdSqN|D]}||krhdSqhq8dS)zTwo messages are equal if they have the same content in the header, question, answer, and authority sections. Returns a ``bool``. FT) isinstancer8r:r= enumerater>)rotherisectionZ other_sectionnrrr__eq__s      zMessage.__eq__cCs || Sr)rw)rrsrrr__ne__ szMessage.__ne__)rsrLcCs|jtjj@dks:|j|jks:tj|jtj|jkr>dS|tjjtjjtjj tjj hkrtt |j dkrtdStj |jrdS|j D]}||j krdSq|j D]}||j krdSqdS)zuIs *other*, also a ``dns.message.Message``, a response to this message? Returns a ``bool``. rFT)r=r<QRr:rb from_flagsrcZFORMERRZSERVFAILZNOTIMPZREFUSEDlenrNZ is_update)rrsrvrrr is_responses0      zMessage.is_response)rurLcCs6t|jD]\}}||kr ||Sq tddS)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``. zunknown sectionN)rrr>rh ValueError)rrurtZ our_sectionrrrsection_number5s zMessage.section_number)numberrLcCs|j|}|j|S)aReturn 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``. )rhmaker>)rrrurrrrkEs zMessage.section_from_numberF) rurnrdclassrdtypecoversdeletingcreate force_unique idna_codecrLc Cs>t|tr|} || }n,t|tr<|j|} || }n ||} t|tr`tjj|| d}tj j |}tj j |}tj j |}|dk rtj j |}| |||||f} |s|jdk r|j| } | dk r| Sn$|D]} | |||||r| Sq|sttj|||||} || |jdk r:| |j| <| S)aFind the RRset with the given attributes in the specified section. *section*, an ``int`` section number, a ``str`` section name, 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) my_message.find_rrset("ANSWER", name, rdclass, rdtype) *name*, a ``dns.name.Name`` or ``str``, the name of the RRset. *rdclass*, an ``int`` or ``str``, the class of the RRset. *rdtype*, an ``int`` or ``str``, the type of the RRset. *covers*, an ``int`` or ``str``, the covers value of the RRset. The default is ``dns.rdatatype.NONE``. *deleting*, an ``int``, ``str``, 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. *idna_codec*, a ``dns.name.IDNACodec``, specifies the IDNA encoder/decoder. If ``None``, the default IDNA 2003 encoder/decoder is used. Raises ``KeyError`` if the RRset was not found and create was ``False``. Returns a ``dns.rrset.RRset object``. rN)rqr6rkstrrh from_textr~r<rn rdatatype RdataTyper rdataclass RdataClassrHgetZ full_matchKeyErrorrpRRsetappend) rrurnrrrrrrrr~keyrprrr find_rrsetTs<4             zMessage.find_rrsetc Cs<z|||||||||| } Wntk r6d} YnX| S)ajGet the RRset with the given attributes in the specified section. If the RRset is not found, None is returned. *section*, an ``int`` section number, a ``str`` section name, 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) my_message.get_rrset("ANSWER", name, rdclass, rdtype) *name*, a ``dns.name.Name`` or ``str``, the name of the RRset. *rdclass*, an ``int`` or ``str``, the class of the RRset. *rdtype*, an ``int`` or ``str``, the type of the RRset. *covers*, an ``int`` or ``str``, the covers value of the RRset. The default is ``dns.rdatatype.NONE``. *deleting*, an ``int``, ``str``, 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. *idna_codec*, a ``dns.name.IDNACodec``, specifies the IDNA encoder/decoder. If ``None``, the default IDNA 2003 encoder/decoder is used. Returns a ``dns.rrset.RRset object`` or ``None``. N)rr) rrurnrrrrrrrrprrr get_rrsets3  zMessage.get_rrsetcCst|tr|}||}n,t|tr<|j|}||}n ||}tdd|D}|tj kr|j dk rt|d7}|j dk r|d7}|S)aReturns the number of records in the specified section. *section*, an ``int`` section number, a ``str`` section name, or one of the section attributes of this message. This specifies the the section of the message to count. For example:: my_message.section_count(my_message.answer) my_message.section_count(dns.message.ANSWER) my_message.section_count("ANSWER") css|]}tdt|VqdS)r&N)maxr{).0Zrrsrrr sz(Message.section_count..Nr&) rqr6rkrrhrr~sumr%r/r?rC)rrur~countrrr section_counts         zMessage.section_countcCsJ|js dSd}|jdjD]}|}|t|d7}q|jrF|d7}|S)z:Compute the size required for the OPT RR, padding excludedr )r?rgto_wirer{rA)rsizeoptionwirerrr_compute_opt_reserveszMessage._compute_opt_reservecCs*|js dSt}|j|t|S)z)Compute the size required for the TSIG RRr)rCr_BytesIOrr{rl)rfrrr_compute_tsig_reserves  zMessage._compute_tsig_reserver)rFmax_sizemultirGprepend_lengthprefer_truncationr[rLc Ks&|dkr|jdk r|j}|dkr6|jdkr2|j}nd}|dkrDd}n |dkrPd}tj|j|j||}|} || | } || z|j D]} | | j | j | jq|jD]} |jtjj| f|q|jD]} |jtjj| f|q|jD]} |jtjj| f|qWnFtjjk rT|rN|jtjjkrP|jtjjO_nYnX||jdk r~||j|j| | ||jdk rtj |!|j"|jdt#t$$|j%||\} } |j&|j'| |tjj|j||r| |_(|!}|r"t)|*dd|}|S)aReturn 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. *prepend_length*, a ``bool``, should be set to ``True`` if the caller wants the message length prepended to the message itself. This is useful for messages sent over TCP, TLS (DoT), or QUIC (DoQ). *prefer_truncation*, a ``bool``, should be set to ``True`` if the caller wants the message to be truncated if it would otherwise exceed the maximum length. If the truncation occurs before the additional section, the TC bit will be set. Raises ``dns.exception.TooBig`` if *max_size* was exceeded. Returns a ``bytes``. Nriir'big)+rFr@r<rendererZRendererr:r=rZreserverrNZ add_questionrnrrrRZ add_rrsetr-rSr.rTr/r3ZTooBigruTCZrelease_reservedr?Zadd_optrAZ write_headerrCsignZget_wirerBr6rJrDclearaddrGr{to_bytes)rrFrrrGrrr[rZ opt_reserveZ tsig_reserverpZnew_tsigctxrrrrr(sj,            zMessage.to_wirec Cs8tjjjtjjtjj||||||| }tj|d|SrO)r<rdtypesANYTSIGrrrp from_rdata) keyname algorithmZ time_signedfudgemac original_iderrorrsrCrrr _make_tsigs  zMessage._make_tsig,r;)rBrrr tsig_error other_datarrLc Cst|tjjr|}|j}n`t|r.|||}nLt|trDtj|}|dkrXtt |}||}t|t rztj|||}||_ |dkr|j }| ||j jd|dtjj|j j||||_dS)aWhen 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 this 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`` or ``str``, the TSIG algorithm to use. This is only used if *keyring* is a ``dict``, and the key entry is a ``bytes``. Nr)rqr<rCKeyrncallablerrnextiterbytesrBr:rrZ mac_sizes) rrBrrrrrrrrrruse_tsigs2/     zMessage.use_tsigcCs|jr|jjSdSdSr)rCrnrrrrrszMessage.keynamecCs|jr|jdjSdSdSrO)rCrrrrr keyalgorithms zMessage.keyalgorithmcCs|jr|jdjSdSdSrO)rCrrrrrrs z Message.maccCs|jr|jdjSdSdSrO)rCrrrrrrs zMessage.tsig_errorcCs t|jSr)boolrCrrrrhad_tsigszMessage.had_tsigcCs4tjjj|tjj|pd}tjtjjt ||S)Nr) r<rrOPTrrprrnrootr6)r=rfrgr?rrr _make_optszMessage._make_opt)rdrerfr@rgrArLcCs|dks|dkrd}n |dkr"d}|dkr8d|_d|_nX|dM}||d>O}|dkrXg}|||||_|dkrt|}||_|dkrtd||_dS) a'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. *pad*, a non-negative ``int``. If 0, the default, do not pad; otherwise add padding bytes to make the message size a multiple of *pad*. Note that if padding is non-zero, an EDNS PADDING option will always be added to the message. NFr^Trl~r7zpad must be non-negative)r?r@rr}rA)rrdrerfr@rgrArrruse_edns s$  zMessage.use_ednscCs|jr|jd@d?SdSdS)Nir7r^)r?rerrrrrdAsz Message.ednscCs|jr|jjSdSdSrO)r?ttlrrrrreHszMessage.ednsflagscCs$|jr||j_n|r |||_dSr)r?rrrPrrrreOs cCs|jr|jdjSdSdSrO)r?rfrrrrrfVs zMessage.payloadcCs|jr|jdjSdSdS)Nrr)r?rgrrrrrg]s zMessage.options)wantedrLcCs:|r|jtjjO_n|jr6|jttjjM_dS)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)rer<r=ZDOr?r6)rrrrr want_dnssecds zMessage.want_dnsseccCstjt|jt|jS)zBReturn the rcode. Returns a ``dns.rcode.Rcode``. )r<rcrzr6r=rerrrrrcrsz Message.rcode)rcrLcCsLtj|\}}|jdM_|j|O_|jdM_|j|O_dS)zUSet the rcode. *rcode*, a ``dns.rcode.Rcode``, is the rcode to set. iiN)r<rcto_flagsr=re)rrcvalueZevaluerrr set_rcodeys zMessage.set_rcodecCstjt|jS)zEReturn the opcode. Returns a ``dns.opcode.Opcode``. )r<rbrzr6r=rrrrrbszMessage.opcode)rbrLcCs(|jdM_|jtj|O_dS)zZSet the opcode. *opcode*, a ``dns.opcode.Opcode``, is the opcode to set. iN)r=r<rbr)rrbrrr set_opcodeszMessage.set_opcodecCs|Srr)rrrrr_get_one_rr_per_rrsetszMessage._get_one_rr_per_rrsetcCs ||ddfS)NFr)rrurnrrrrr_parse_rr_headerszMessage._parse_rr_headercCsl|tjjkr.|tjks(|js(|tjjkr`tn2|tjj kr`|tjks\|tjj ks\||dkr`t ||ddfS)Nr&F) r<rrr%r/r?rnrrrrr)rrurpositionrnrrrrr_parse_special_rr_headers"     z Message._parse_special_rr_header)N)NT)NrFNFF)T)Mr r r r r%rhrr6rpropertyrr<rprrNsetterrRrSrTrWrYrnNamerrrrrXrwrxr|r~rkrNONE SectionTyperrr IDNACodecrrrrrrr staticmethodrrCZdefault_algorithmrrrrrrrDEFAULT_EDNS_PAYLOADrrdOptionrrerfrrgrrcZRcoderrbZOpcoderrrrrrrrr8s,       &&   [   C   d  J 5  r8c@s8eZdZdZejjeejj e e ejj dddZ dS)ChainingResultaThe 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 same 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. canonical_namerR minimum_ttlcnamescCs||_||_||_||_dSrr)rrrRrrrrrrszChainingResult.__init__N) r r r r r<rnrrrprr6rrrrrrrs   rc@s,eZdZedddZejjdddZdS) QueryMessagerKc Cs|jtjj@dkrtt|jdkr,tjj|jd}|j}tj j }d}d}g}|t kr,z,| |j ||j|j}t||j }Wq,WqPtk r(|jtjjkrzT| |j ||jtjj}||t||j }|D]}|j}qq|d7}WYqPWntk rYYq,YnXnYq,YqPXqP|t kr:t|tjjkrZ|dk rZt|dkr|} z6| |j| |jtjj} t|| j | dj}WqWnFtk rz | } Wn"tjjk rYYqYnXYnXqht||||S)aFollow 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. rr&N) r=r<ryr!r{rNr3 FormErrorrnrZMAX_TTL MAX_CHAINrrRrrminrrZCNAMErtargetr"rcZNXDOMAINr#rSSOAminimumparentZNoParentr) rrNqnameZmin_ttlrRrrZcrrsetrdZaunameZsrrsetrrrresolve_chainingsx           zQueryMessage.resolve_chainingcCs |jS)aReturn 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. )rrrrrrrszQueryMessage.canonical_nameN) r r r rrr<rnrrrrrrrsKrcCs ddl}dSrO)Z dns.update)r<rrr_maybe_import_update-srcCs2|tjjkrtS|tjjkr*ttjjStSdSr) r<rbQUERYrZUPDATErupdateZ UpdateMessager8)rbrrr_message_factory_from_opcode6s   rc@s:eZdZdZdddZddZdd Zd d Zd d ZdS) _WireReaderaiWire 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. continue_on_error: try to extract as much information as possible from the message, accumulating MessageErrors in the *errors* attribute instead of raising them. FNc CsHtj||_d|_||_||_||_||_||_ ||_ ||_ g|_ dSr) r<rParserparserrinitialize_message question_onlyone_rr_per_rrsetignore_trailingrBrcontinue_on_errorrI) rrrrrrrBrrrrrrQs z_WireReader.__init__c Cs~|jdk st|jj|}t|D]V}|j|jj}|jd\}}|j||||\}}}}|jj ||||dddq"dS)zhRead the next *qcount* records from the wire data and add them to the question section. Nz!HHTrr) rAssertionErrorr>rangerget_namerF get_structrr)rr~qcountru_rrrrrr _get_questiongs&   z_WireReader._get_questioncCs|jt||jjdSr)rIrr1rcurrent)rerrr _add_errorwsz_WireReader._add_errorc Cs|jdk st|jj|}|j}t|D]}|jj}|j}|jjdk r\| |jj}n|}|j d\} } } } | t j j t j jfkr|j||||| | \} } } }n|j||| | \} } } }|jj}z|r| dkrt jjd}t j j}n8|j| t j| | |j|jj}W5QRX|}|jjrD| t j jkrDd}| t j j krjt j|| ||j_n&| t j jkrR|jdkrtdt|jt r|j!|}t|t"rt j#$|||j%}n"t&|jr||j|}n|j}|dkrtd|||j_t j#'|jj(|||t)t**|jj+||jj,|j- |j_,t j|d||j_#n>|j.||| | || d|}|dk r| dkrd} |/|| Wq(t0k r}z*|j1r|2||j3|| nW5d}~XYq(Xq(dS)zRead the next I{count} records from the wire data and add them to the specified section. section_number: the section of the message to which to add records count: the number of records to read Nz!HHIHrTz"got signed message without keyringzkey '%s' unknowni)4rrr>rrrrrrFrZrr<rrrrrr3rrZ restrict_tordataZfrom_wire_parserrrErrprr?rBrrqdictrrrCrrrvalidaterr6rJrDrGrrrr5rr seek)rr~rrurrtZrr_startZ absolute_namernrrrZrdlenremptyZ rdata_startrrrrprrrr _get_sectionzs                 z_WireReader._get_sectionc Cs>|jdkrt|jd\}}}}}}ttj|}||d|_tj ||j_ | |j|j |j |_ z|tj||jr|jWS|tj||tj||tj||js|jdkrt|jr|jjr|jjs|jj|jjWn:tk r6}z|jr$||nW5d}~XYnX|jS)zNRead a wire format DNS message and build a dns.message.Message object. z!HHHHHHr9rN) r remainingrrrr<rbrzrr=Flagrrrrr%r,rrr-r.r/rrrrGrrrr5rr ) rr:r=rZancountZaucountZadcountfactoryrrrrreads8   z_WireReader.read)FFFNFF) r r r r rrr rrrrrrr@s `rr;F) rrBrDrErFrGrrrrraise_on_truncationrrLc  sdkr dfdd} t|| ||| ||| } z | }WnBtjjk r| jr|| jjtjj@r|| r|t| jdnYnX|jtjj@r| rt|d| r| j |_ |S)aConvert 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`` or ``None``. 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. *continue_on_error*, a ``bool``. If ``True``, try to continue parsing even if errors occur. Erroneous rdata will be ignored. Errors will be accumulated as a list of MessageError objects in the message's ``errors`` attribute. This option is recommended only for DNS analysis tools, or for use in a server as part of an error handling path. The default is ``False``. 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``. Nr;cs|_|_|_|_dSr)rDrErFrGrrFrDrGrErrrFsz%from_wire..initialize_messager) rrr<r3rrr=rrrI)rrBrDrErFrGrrrrrrrreadermrrr from_wires:F   rc@sBeZdZdZdddZddZd d Zd d Zd dZddZ dS) _TextReaderatText 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. FNTcCshd|_tjj||d|_d|_||_||_||_||_ d|_ d|_ d|_ t |_d|_tjj|_d|_dS)Nrr^r)rr< tokenizer Tokenizertok last_namerrFrZ relativize_tor:rdrerrfrcrbrr=)rtextrrrFrZr rrrrxs  z_TextReader.__init__cCs|j}|j}|dkr(|j|_nJ|dkrl|j}|sP|j|qh|jtj |jB|_q0n|dkr|j|_ |j |j d>B|_ n|dkr|j dkrd|_ |j}|s|j|q|j tj |jB|_ qn|dkr|j|_ |j dkrrd|_ nb|dkrJ|j}tj ||_|jtj|jB|_n(|d krn|j}tj ||_nt|jd S) z5Process one line from the text format header section.r:r=rdr7ZeflagsrrfrbrcN)rrrZget_intr: is_identifierungetr=r<rrdreZedns_from_textrfZ get_stringrbrrcrget_eol)rrtokenwhatr!rrr _header_linesD              z_TextReader._header_linecCs |jj|}|jjdd}|s>|j||jj|j|j|_ |j }|dkrPt |j}| sjt j jz,t j|j}|j}| st j jWn:t j jk rt j jYntk rt jj}YnXt j|j}|j||||\}}}}|jj||||ddd|jdS)z7Process one line from the text format question section.TZ want_leadingNr)rr>rr is_whitespaceas_namerFrZr rr$r"r<r3 SyntaxErrorrrrr5INrrrr$)rr~rur%rnrrrrrr_question_linesP      z_TextReader._question_linec Cs|jj|}|jjdd}|s>|j||jj|j|j|_ |j }|dkrPt |j}| sjt j jz*t|jd}|j}| st j jWn6t j jk rt j jYntk rd}YnXz,t j|j}|j}| st j jWn>t j jk rt j jYntk r6t jj}YnXt j|j}|j||||\}}}} |j}| r|st j j| s|rt j j|s|j|t j|||j|jj|j|j} | } n d} t jj} |j||||| |d|j} | dk r| | |dS)zfProcess one line from the text format answer, authority, or additional data sections. Tr(Nr) rr>rrr)r*rFrZr rr$r"r<r3r+r6rr5rrr,rr is_eol_or_eofZ UnexpectedEndr#r rrrrr) rr~rur%rnrrrrrrrrprrr_rr_lines                z_TextReader._rr_linecCs`t|j}||jd}|j|_|jdkr<||j|j|j|jrN| |j|j r\|j |_ |S)Nr9r) rrbr:r=rdrrerfrcrrF)rrrrrr _make_message's    z_TextReader._make_messagecCs|j}d}|jdd}|r"q|r|j}|dkrB|j}|jrP|j}n|}zB|j |}|js||_| |j |_ |t jkr|j}n|j}Wntk rYnX|jq |j|||q |js||_|jS)zNRead a text format DNS message and build a dns.message.Message object.NTHEADER)r'rrr.Z is_commentrupperrr0rhrrrr%r,r-r/r5r$r#)rZ line_methodr~r%urrrrr3s>        z_TextReader.read)FNTN) r r r r rr'r-r/r0rrrrrrks )#K rT)r!rrrFrZr rLcCst||||||}|S)aConvert 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`` )rr)r!rrrFrZr rrrrras*r)rrrrLc CsNt|trt|}n t|}|}t|||W5QRSQRXdsJtdS)aRead 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`` FN)rqropen contextlib nullcontextrr)rrrcmrrr from_files    r8)rrrrrrerfr@rgrr:r=rArLc Cst|trtjj|| d}tjj|}tjj |}t | d} tj | | _ | j | j|||dddi}|dk rz||d<|dk r||d<|dk r||d<|dk r||d <|r|dkrd }||d <| |d <| jf|| || S) aMake 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``. If ``None``, EDNS will be enabled only if other parameters (*ednsflags*, *payload*, *request_payload*, or *options*) are set. 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. *id*, an ``int`` or ``None``, the desired query id. The default is ``None``, which generates a random query id. *flags*, an ``int``, the desired query flags. The default is ``dns.flags.RD``. *pad*, a non-negative ``int``. If 0, the default, do not pad; otherwise add padding bytes to make the message size a multiple of *pad*. Note that if padding is non-zero, an EDNS PADDING option will always be added to the message. Returns a ``dns.message.QueryMessage`` rr9TrNrerfr@rgrrdrA)rqrr<rnrrrrrrrr=rrrNrr)rrrrrrerfr@rgrr:r=rArrrrr make_querys.F     r9 r)queryrecursion_available our_payloadrrrArLc Cs|jtjj@rtjdt|}||jd}tjj|jtjj@B|_|r`|jtjj O_| |t |j |_ |j dkr|dkrd}|jD]}|jtj jjkrd}q|jdd||j|d|jr||j|j|d|d|j|j|_|S)aMake 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. *pad*, a non-negative ``int`` or ``None``. If 0, the default, do not pad; otherwise if not ``None`` add padding bytes to make the message size a multiple of *pad*. Note that if padding is non-zero, an EDNS PADDING option will always be added to the message. If ``None``, add padding following RFC 8467, namely if the request is padded, pad the response to 468 otherwise do not pad. 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 queryr9rNi)rAr;)r=r<ryr3rrrbr:RDZRArlistrNrdrgotypeZ OptionTypeZPADDINGrrfrrrBrrrrD) r;r<r=rrrArresponserrrr make_responses8$       rB) Nr;FNNFFFFFF)NFNTN)NF)Fr:rrN)[r r5r_rJtypingrrrrrrZdns.ednsr<Z dns.entropyZdns.enumZ dns.exceptionZ dns.flagsZdns.nameZ dns.opcodeZ dns.rcodeZ dns.rdataZdns.rdataclassZ dns.rdatatypeZdns.rdtypes.ANY.OPTZdns.rdtypes.ANY.TSIGZ dns.rendererZ dns.rrsetZdns.tsigZdns.ttlZdns.wirer3rrrZ DNSExceptionrrrrrr!r"r#r+r$enumIntEnumr%r1rrr6rnrrrrrZ IndexKeyTyperprZ IndexTyperrr8rrrrrrrrCZHMACTSigZGSSTSigrrrrr8r,r=r>rdrr9rBr,r-r.r/rrrrs8    * ]  @  ny    2  &  d E