a Mes @s~UdZddlZddlZddlZddlZddlZddlZddlmZddl m Z m Z m Z m Z mZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZdZdZGdddejjZed fd d Zed fd d Z ed fddZ!dZ"ddZ#ddZ$ej%j&Z'ej%j%GdddZ(ej%j%Gddde(Z)iZ*e e ej+j,ej-j.fe fe/d<dZ0ddZ1d,eej+j,e2feej-j.e2feej3j4e2fe ej5j6e7e ej5j6e ej5j8e(dddZ9d-eej+j,e2feej-j.e2fej:j;e ej5j6e(d d!d"Ze>e ej5j6e(d#d$d%Z?Gd&d'd'ejjZ@d(ej+jAfe e>e2e7ej+j,dd)d*d+ZBdS)/z DNS rdata.N) import_module)AnyDictOptionalTupleUnion Tc@seZdZdZdS)NoRelativeRdataOrderingzAn attempt was made to do an ordered comparison of one or more rdata with relative names. The only reliable way of sorting rdata is to use non-relativized rdata. N)__name__ __module__ __qualname____doc__rr-/usr/lib/python3.9/site-packages/dns/rdata.pyr 3sr  cs6s S|fddtdtDS)zXBreak a binary string into chunks of chunksize characters separated by a space. csg|]}||qSrr).0i chunksizedatarr Cz_wordbreak..r)decodejoinrangelen)rr separatorrrr _wordbreak;s rcKstt|||S)zConvert a binary string into its hex encoding, broken up into chunks of chunksize characters separated by a separator. )rbinasciiZhexlifyrrrkwrrr_hexifyJsr!cKstt|||S)zConvert a binary string into its base64 encoding, broken up into chunks of chunksize characters separated by a separator. )rbase64Z b64encoderrrr _base64ifyRsr#s"\cCszt|tr|}t|ts$t|}d}|D]H}|tvrJ|dt|7}q,|dkrh|dkrh|t|7}q,|d|7}q,|S)z7Escape the characters in a quoted string which need it.\rz\%03d) isinstancestrencode bytearray __escapedchr)Zqstringtextcrrr _escapify_s  r/cCsFtt|dddD]$}||dkr|d|dSq|ddS)zDetermine the index of greatest byte that isn't all zeros, and return the bitmap that contains all the bytes less than that index. r)rr)Zwhatrrrr_truncate_bitmaprs r2c @seZdZdZgdZddZddZddZd d Ze j j d d d Z e d ddZd]ee jjeeeefedddZd^eeee jjee jjeedddZd_eeee jjee jjeedddZd`ee jjddddZdaee jjeddd Zd!d"Zd#d$Zd%d&Zd'd(Z d)d*Z!d+d,Z"d-d.Z#d/d0Z$d1d2Z%d3d4Z&e'dbe j(j)e j j e j*j+ee jjeee jjdd5d6d7Z,e'dce j(j)e j j e j-j.ee jjdd8d9d:Z/edd;dd?Z1e'd@dAZ2e'ddeeee eedBdCdDZ3e'dEdFZ4e'dGdHZ5e'dIdJZ6e'dKdLZ7e'dMdNZ8e'dedOdPZ9e'dQdRZ:e'dSdTZ;e'dUdVZe'd[d\Z?dS)fRdataz#Base class for all DNS rdata types.)rdclassrdtype rdcommentcCs"|||_|||_d|_dS)zInitialize an rdata. *rdclass*, an ``int`` is the rdataclass of the Rdata. *rdtype*, an ``int`` is the rdatatype of the Rdata. N)_as_rdataclassr4 _as_rdatatyper5r6)selfr4r5rrr__init__s  zRdata.__init__cCstjdd|jjDS)Ncss|]}t|dgVqdS) __slots__N)getattr)rclsrrr sz'Rdata._get_all_slots..) itertoolschain from_iterable __class____mro__r9rrr_get_all_slotss zRdata._get_all_slotscCs$i}|D]}t||||<q |SN)rEr<)r9stateslotrrr __getstate__s  zRdata.__getstate__cCs<|D]\}}t|||qt|ds8t|dddS)Nr6)itemsobject __setattr__hasattr)r9rGrHvalrrr __setstate__s zRdata.__setstate__)returncCstjjS)aReturn the type a Rdata covers. DNS SIG/RRSIG rdatas apply to a specific type; this type is returned by the covers() function. If the rdata type is not SIG or RRSIG, dns.rdatatype.NONE is returned. This is useful when creating rdatasets, allowing the rdataset to contain only RRSIGs of a particular type, e.g. RRSIG(NS). Returns a ``dns.rdatatype.RdataType``. )dns rdatatypeNONErDrrrcoverss z Rdata.coverscCs|d>|jBS)zReturn a 32-bit type value, the least significant 16 bits of which are the ordinary DNS type, and the upper 16 bits of which are the "covered" type, if any. Returns an ``int``. )rTr5rDrrrextended_rdatatypeszRdata.extended_rdatatypeNTorigin relativizer rPcKstdS)zEConvert an rdata to text format. Returns a ``str``. NNotImplementedErrorr9rXrYr rrrto_texts z Rdata.to_textF)filecompressrX canonicalizerPcCstdSrFrZr9r^r_rXr`rrr_to_wireszRdata._to_wirecCs8|r|||||St}||||||SdS)zSConvert an rdata to wire format. Returns a ``bytes`` or ``None``. N)rbioBytesIOgetvalue)r9r^r_rXr`frrrto_wires z Rdata.to_wirezdns.rdata.GenericRdata)rXrPcCstj|j|j|j|dS)zrCreates a dns.rdata.GenericRdata equivalent of this rdata. Returns a ``dns.rdata.GenericRdata``. )rX)rQrdata GenericRdatar4r5rgr9rXrrr to_genericszRdata.to_genericcCs|j|ddS)zConvert rdata to a format suitable for digesting in hashes. This is also the DNSSEC canonical form. Returns a ``bytes``. T)rXr`)rgrjrrr to_digestableszRdata.to_digestablecCsf|}|tjjkrd}ndtj|d}dtj|jdtj|j|dt|dS)Nr$()z) rTrQrRrSr] rdataclassr4r5r()r9rTZctextrrr__repr__s(   zRdata.__repr__cCs|SrF)r]rDrrr__str__sz Rdata.__str__cCsz|}d}Wn,tjjy<tr4|tjj}d}Yn0z|}d}Wn,tjjyztrr|tjj}d}Yn0tr||kr|rdSdSn |s|rt||krdS||krdSdSdS)aCompare an rdata with another rdata of the same rdtype and rdclass. For rdata with only absolute names: Return < 0 if self < other in the DNSSEC ordering, 0 if self == other, and > 0 if self > other. For rdata with at least one relative names: The rdata sorts before any rdata with only absolute names. When compared with another relative rdata, all names are made absolute as if they were relative to the root, as the proper origin is not available. While this creates a stable ordering, it is NOT guaranteed to be the DNSSEC ordering. In the future, all ordering comparisons for rdata with relative names will be disallowed. FTr1r0rN)rlrQnameNeedAbsoluteNameOrOrigin_allow_relative_comparisonsrootr )r9otherour our_relativetheirtheir_relativerrr_cmps4  z Rdata._cmpcCst|tsdS|j|jks&|j|jkr*dSd}d}z |}Wn(tjjyf|tjj}d}Yn0z |}Wn(tjjy|tjj}d}Yn0||krdS||kS)NFT) r'r3r4r5rlrQrtrurw)r9rxrzr|ryr{rrr__eq__Fs&     z Rdata.__eq__cCs6t|tsdS|j|jks&|j|jkr*dS|| S)NT)r'r3r4r5r~r9rxrrr__ne__[s  z Rdata.__ne__cCs4t|tr"|j|jks"|j|jkr&tS||dkSNrr'r3r4r5NotImplementedr}rrrr__lt__bs  z Rdata.__lt__cCs4t|tr"|j|jks"|j|jkr&tS||dkSrrrrrr__le__ks  z Rdata.__le__cCs4t|tr"|j|jks"|j|jkr&tS||dkSrrrrrr__ge__ts  z Rdata.__ge__cCs4t|tr"|j|jks"|j|jkr&tS||dkSrrrrrr__gt__}s  z Rdata.__gt__cCst|tjjSrF)hashrlrQrtrwrDrrr__hash__szRdata.__hash__)r4r5tokrXrY relativize_torPcCstdSrFrZ)r=r4r5rrXrYrrrr from_texts zRdata.from_textr4r5parserrXrPcCstdSrFrZr=r4r5rrXrrrfrom_wire_parserszRdata.from_wire_parser)kwargsrPc stjj}D]F}|dkr q||vrrz Rdata.replace..N) inspectZ signaturer: parametersAttributeErrorformatrBr rr6rKrL)r9rrrargsZrdr6rrrreplaces,  z Rdata.replacecCstjj|SrF)rQrq RdataClassmaker=valuerrrr7szRdata._as_rdataclasscCstjj|SrF)rQrR RdataTyperrrrrr8szRdata._as_rdatatype)rr) max_lengthempty_okrPcCs||rt|tr|}n,t|tr,t|}nt|tr<|}ntd|dur`t||kr`td|sxt|dkrxtd|S)Nz not bytesztoo longrzempty bytes not allowed)r'r(r)r*bytes ValueErrorr)r=rr)rrZbvaluerrr _as_bytess    zRdata._as_bytescCs0t|trtj|St|tjjs,td|S)Nz not a name)r'r(rQrtrNamerrrrr_as_names   zRdata._as_namecCs.t|tstd|dks"|dkr*td|S)Nnot an integerrz not a uint8r'intrrrrr _as_uint8s  zRdata._as_uint8cCs.t|tstd|dks"|dkr*td|S)Nrriz not a uint16rrrrr _as_uint16s  zRdata._as_uint16cCs.t|tstd|dks"|dkr*td|S)Nrrlz not a uint32rrrrr _as_uint32 s  zRdata._as_uint32cCs.t|tstd|dks"|dkr*td|S)Nrrlz not a uint48rrrrr _as_uint48s  zRdata._as_uint48cCsFt|tstd|dur*||kr*td|durB||krBtd|S)Nrzvalue too smallzvalue too larger)r=rZlowZhighrrr_as_ints z Rdata._as_intcCs8t|trtj|St|tr,tj|StddS)Nznot an IPv4 address)r'r(rQZipv4r`r inet_ntoarrrrr_as_ipv4_address#s     zRdata._as_ipv4_addresscCs8t|trtj|St|tr,tj|StddS)Nznot an IPv6 address)r'r(rQZipv6r`rrrrrrr_as_ipv6_address,s     zRdata._as_ipv6_addresscCst|tr|StddS)Nz not a boolean)r'boolrrrrr_as_bool5s zRdata._as_boolcCs>t|tr||dtjjSt|tr2tj|StddS)Nrz not a TTL) r'rrrQZttlZMAX_TTLr(rrrrrr_as_ttl<s    z Rdata._as_ttlcs:z |fWSty4tfdd|DYS0dS)Nc3s|]}|VqdSrFr)rvas_valuerrr>Nrz"Rdata._as_tuple..) Exceptiontuple)r=rrrrr _as_tupleEs  zRdata._as_tuplecCst|}t||SrF)listrandomZshuffle)r=iterablerJrrr_processing_orderRs zRdata._processing_order)NT)NNF)NNNF)N)N)NTN)N)FNT)NN)@r r r r r;r:rErIrOrQrRrrTrrVrrtrrrr(rr]Z CompressTyperrbrgrkrlrrrsr}r~rrrrrr classmethodrqr tokenizer TokenizerrwireParserrrr7r8rrrrrrrrrrrrrrrrrr3s              /         1             r3csreZdZdZdgZfddZdeejj e e e e fe ddd Zedd d Zdd dZedddZZS)rizGeneric Rdata Class This class is used for rdata types for which we have no better implementation. It implements the DNS "unknown RRs" scheme. rcst||||_dSrF)superr:r)r9r4r5rrBrrr:cszGenericRdata.__init__NTrWcKs dt|jt|jfi|S)Nz\# %d )rrr!r\rrrr]gszGenericRdata.to_textc Csj|}|r|jdkr&tjd|}|d} t | } t | |kr^tjd|||| S)N\#z$generic rdata does not start with \#Tz'generic rdata hex data has wrong length) r is_identifierrrQ exception SyntaxErrorZget_intZ!concatenate_remaining_identifiersr)rZ unhexlifyr) r=r4r5rrXrYrtokenlengthhexrrrrros    zGenericRdata.from_textFcCs||jdSrF)writerrarrrrb}szGenericRdata._to_wirecCs||||SrF)Z get_remainingrrrrrszGenericRdata.from_wire_parser)NT)NTN)NNF)N)r r r r r;r:rrQrtrrrr(rr]rrrbr __classcell__rrrrriYs"     ri_rdata_classesz dns.rdtypesc Cst||f}|sttjj|f}|stj|}tj|}|dd}z.td t ||g}t ||}|t||f<Wndt yz>td t d|g}t ||}|ttjj|f<|t||f<Wnt yYn0Yn0|st }|t||f<|S)N-_.ANY)rrrQrRrrqr]rrr_module_prefixr< ImportErrorri)r4r5r=Z rdclass_text rdtype_textmodrrrget_rdata_classs2         r)r4r5rrXrYr idna_codecrPc Cs6t|trtjj||d}tjj|}tjj |}t ||}tj tj j d}|tkr|} || | r| jdkrt||||||} t||| jdt| j|}|} | | jkrtj d|dur|||||||}|} | jdurt|d| j|WdS1s(0YdS)a3Build an rdata object from text format. This function attempts to dynamically load a class which implements the specified rdata class and type. If there is no class-and-type-specific implementation, the GenericRdata class is used. Once a class is chosen, its from_text() class method is called with the parameters to this function. If *tok* is a ``str``, then a tokenizer is created and the string is used as its input. *rdclass*, a ``dns.rdataclass.RdataClass`` or ``str``, the rdataclass. *rdtype*, a ``dns.rdatatype.RdataType`` or ``str``, the rdatatype. *tok*, a ``dns.tokenizer.Tokenizer`` or a ``str``. *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. *idna_codec*, a ``dns.name.IDNACodec``, specifies the IDNA encoder/decoder to use if a tokenizer needs to be created. If ``None``, the default IDNA 2003 encoder/decoder is used. If a tokenizer is not created, then the codec associated with the tokenizer is the one that is used. Returns an instance of the chosen Rdata subclass. )rNrrz9compressed data in generic syntax form of known rdatatyper6)r'r(rQrrrqrrrRrrrExceptionWrapperrrirZungetrrr from_wirerrrgZget_eol_as_tokenZcommentrKrL) r4r5rrXrYrrr=rhrZgrdataZrwirerrrrs<-       rrcCsjtjj|}tjj|}t||}tjtjj  | ||||WdS1s\0YdS)aBuild an rdata object from wire format This function attempts to dynamically load a class which implements the specified rdata class and type. If there is no class-and-type-specific implementation, the GenericRdata class is used. Once a class is chosen, its from_wire() class method is called with the parameters to this function. *rdclass*, a ``dns.rdataclass.RdataClass`` or ``str``, the rdataclass. *rdtype*, a ``dns.rdatatype.RdataType`` or ``str``, the rdatatype. *parser*, a ``dns.wire.Parser``, the parser, which should be restricted to the rdata length. *origin*, a ``dns.name.Name`` (or ``None``). If not ``None``, then names will be relativized to this origin. Returns an instance of the chosen Rdata subclass. N) rQrqrrrRrrrrZ FormErrorr)r4r5rrXr=rrrrs  r)r4r5rcurrentrdlenrXrPcCsJtj||}||t||||WdS1s<0YdS)a1Build an rdata object from wire format This function attempts to dynamically load a class which implements the specified rdata class and type. If there is no class-and-type-specific implementation, the GenericRdata class is used. Once a class is chosen, its from_wire() class method is called with the parameters to this function. *rdclass*, an ``int``, the rdataclass. *rdtype*, an ``int``, the rdatatype. *wire*, a ``bytes``, the wire-format message. *current*, an ``int``, the offset in wire of the beginning of the rdata. *rdlen*, an ``int``, the length of the wire-format rdata *origin*, a ``dns.name.Name`` (or ``None``). If not ``None``, then names will be relativized to this origin. Returns an instance of the chosen Rdata subclass. N)rQrrZ restrict_tor)r4r5rrrrXrrrrr%s" rc@seZdZdZddhZdZdS)RdatatypeExistszDNS rdatatype already exists.r4r5zKThe rdata type with class {rdclass:d} and rdtype {rdtype:d} already exists.N)r r r r Z supp_kwargsZfmtrrrrrLsrF)implementationr5r is_singletonr4rPcCsftjj|}t||}|tks,tj|r8t||dt|| ddt ||f<tj |||dS)aDynamically register a module to handle an rdatatype. *implementation*, a module implementing the type in the usual dnspython way. *rdtype*, an ``int``, the rdatatype to register. *rdtype_text*, a ``str``, the textual form of the rdatatype. *is_singleton*, a ``bool``, indicating if the type is a singleton (i.e. RRsets of the type can have only one member.) *rdclass*, the rdataclass of the type, or ``dns.rdataclass.ANY`` if it applies to all classes. rrrN) rQrRrrrriZ is_metatyperr<rr register_type)rr5rrr4Z existing_clsrrrrVs   r)NTNN)N)N)Cr r"rrrcr?r importlibrtypingrrrrrZ dns.exceptionrQZ dns.immutableZdns.ipv4Zdns.ipv6Zdns.nameZdns.rdataclassZ dns.rdatatypeZ dns.tokenizerZdns.ttlZdns.wireZ _chunksizervrZ DNSExceptionr rr!r#r+r/r2Z immutableZconstifyZ _constifyr3rirrqrrRr__annotations__rrr(rrrtrrZ IDNACodecrrrrrrrrINrrrrrs   Z,"     ^  *  '