3 bW:@sdZddlmZddlZddlZddlZddlZddlZddl Zddl Zddl Zddl m Z mZmZdZefddZdZefd d Zed Zd d ZddZGdddeZGdddeZiZdZddZdddZdddZdS)aDNS rdata. @var _rdata_modules: A dictionary mapping a (rdclass, rdtype) tuple to the module which implements that type. @type _rdata_modules: dict @var _module_prefix: The prefix to use when forming modules names. The default is 'dns.rdtypes'. Changing this value will break the library. @type _module_prefix: string @var _hex_chunk: At most this many octets that will be represented in each chunk of hexstring that _hexify() produces before whitespace occurs. @type _hex_chunk: int)BytesION)xrange string_types text_type cs4tj|djfddtdtDjS)aConvert a binary string into its hex encoding, broken up into chunks of I{chunksize} characters separated by a space. @param data: the binary string @type data: string @param chunksize: the chunk size. Default is L{dns.rdata._hex_chunksize} @rtype: string  csg|]}||qSr ).0i) chunksizeliner /usr/lib/python3.6/rdata.py 6sz_hexify..r)binasciiZhexlifyjoinrangelendecode)datar r )r r r_hexify+s rcs4tj|djfddtdtDjS)a$Convert a binary string into its base64 encoding, broken up into chunks of I{chunksize} characters separated by a space. @param data: the binary string @type data: string @param chunksize: the chunk size. Default is L{dns.rdata._base64_chunksize} @rtype: string rcsg|]}||qSr r )r r )r r r rrIsz_base64ify..r)base64Z b64encoderrrr)rr r )r r r _base64ify=s rs"\cCs~t|tr|j}t|ts$t|}d}xP|D]H}|tkrL|dt|7}q.|dkrj|dkrj|t|7}q.|d|7}q.W|S)zEscape the characters in a quoted string which need it. @param qstring: the string @type qstring: string @returns: the escaped string @rtype: string \rz\%03d) isinstancerencode bytearray __escapedchr)Zqstringtextcr r r _escapifyOs   r#cCsFx8tt|dddD] }||dkr|d|dSqW|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. @param what: a string of octets representing a bitmap. @type what: string @rtype: string rrr$)rr)Zwhatr r r r_truncate_bitmaphs  r%c@seZdZdZddgZddZddZdd Zd.d d Zd/ddZ d0ddZ ddZ ddZ ddZ ddZddZddZddZd d!Zd"d#Zd$d%Zd&d'Zed1d(d)Zed2d*d+Zd3d,d-Zd S)4Rdataz(Base class for all DNS rdata types. rdclassrdtypecCs||_||_dS)zInitialize an rdata. @param rdclass: The rdata class @type rdclass: int @param rdtype: The rdata type @type rdtype: int N)r'r()selfr'r(r r r__init__~szRdata.__init__cCstjjS)a_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). @rtype: int )dns rdatatypeNONE)r)r r rcoverss z Rdata.coverscCs|jd>|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. @rtype: int )r.r()r)r r rextended_rdatatypeszRdata.extended_rdatatypeNTcKstdS)z@Convert an rdata to text format. @rtype: string N)NotImplementedError)r)origin relativizekwr r rto_textsz Rdata.to_textcCstdS)z@Convert an rdata to wire format. @rtype: string N)r1)r)filecompressr2r r rto_wiresz Rdata.to_wirecCst}|j|d||jS)zlConvert rdata to a format suitable for digesting in hashes. This is also the DNSSEC canonical form.N)rr8getvalue)r)r2fr r r to_digestableszRdata.to_digestablecCstjj|j|j|jdS)zCheck that the current contents of the rdata's fields are valid. If you change an rdata by assigning to its fields, it is a good idea to call validate() when you are done making changes. N)r+rdata from_textr'r(r5)r)r r rvalidateszRdata.validatecCsf|j}|tjjkrd}ndtjj|d}dtjj|jdtjj|j|dt|dS)Nr()z) r.r+r,r-r5 rdataclassr'r(str)r)r.Zctextr r r__repr__s  zRdata.__repr__cCs|jS)N)r5)r)r r r__str__sz Rdata.__str__cCs8|jtjj}|jtjj}||kr(dS||kr4dSdS)zCompare an rdata with another rdata of the same rdtype and rdclass. Return < 0 if self < other in the DNSSEC ordering, 0 if self == other, and > 0 if self > other. rrr$)r;r+nameroot)r)otherZourZtheirr r r_cmpsz Rdata._cmpcCs8t|tsdS|j|jks&|j|jkr*dS|j|dkS)NFr)rr&r'r(rJ)r)rIr r r__eq__s  z Rdata.__eq__cCs8t|tsdS|j|jks&|j|jkr*dS|j|dkS)NTr)rr&r'r(rJ)r)rIr r r__ne__s  z Rdata.__ne__cCs6t|t s$|j|jks$|j|jkr(tS|j|dkS)Nr)rr&r'r(NotImplementedrJ)r)rIr r r__lt__s z Rdata.__lt__cCs6t|t s$|j|jks$|j|jkr(tS|j|dkS)Nr)rr&r'r(rMrJ)r)rIr r r__le__s z Rdata.__le__cCs6t|t s$|j|jks$|j|jkr(tS|j|dkS)Nr)rr&r'r(rMrJ)r)rIr r r__ge__s z Rdata.__ge__cCs6t|t s$|j|jks$|j|jkr(tS|j|dkS)Nr)rr&r'r(rMrJ)r)rIr r r__gt__s z Rdata.__gt__cCst|jtjjS)N)hashr;r+rGrH)r)r r r__hash__szRdata.__hash__cCstdS)aBuild an rdata object from text format. @param rdclass: The rdata class @type rdclass: int @param rdtype: The rdata type @type rdtype: int @param tok: The tokenizer @type tok: dns.tokenizer.Tokenizer @param origin: The origin to use for relative names @type origin: dns.name.Name @param relativize: should names be relativized? @type relativize: bool @rtype: dns.rdata.Rdata instance N)r1)clsr'r(tokr2r3r r rr=szRdata.from_textcCstdS)a<Build an rdata object from wire format @param rdclass: The rdata class @type rdclass: int @param rdtype: The rdata type @type rdtype: int @param wire: The wire-format message @type wire: string @param current: The offset in wire of the beginning of the rdata. @type current: int @param rdlen: The length of the wire-format rdata @type rdlen: int @param origin: The origin to use for relative names @type origin: dns.name.Name @rtype: dns.rdata.Rdata instance N)r1)rTr'r(wirecurrentrdlenr2r r r from_wireszRdata.from_wirecCsdS)zWConvert any domain names in the rdata to the specified relativization. Nr )r)r2r3r r rchoose_relativity&szRdata.choose_relativity)NT)NN)N)NT)N)NT)__name__ __module__ __qualname____doc__ __slots__r*r.r0r5r8r;r>rErFrJrKrLrNrOrPrQrS classmethodr=rYrZr r r rr&ws0        r&csVeZdZdZdgZfddZdddZedd d Zdd d Z edd dZ Z S) GenericRdatazGenerate Rdata Class This class is used for rdata types for which we have no better implementation. It implements the DNS "unknown RRs" scheme. rcstt|j||||_dS)N)superrar*r)r)r'r(r) __class__r rr*8szGenericRdata.__init__NTcKsdt|jt|jS)Nz\# %d )rrr)r)r2r3r4r r rr5<szGenericRdata.to_textc Cs|j}|j s|jdkr(tjjd|j}g}x&|j}|jrHP|j|jj q6Wdj |} t j | } t | |krtjjd|||| S)Nz\#z$generic rdata does not start with \#z'generic rdata hex data has wrong length)get is_identifiervaluer+Z exception SyntaxErrorZget_intZ is_eol_or_eofappendrrrZ unhexlifyr) rTr'r(rUr2r3tokenZlengthZchunkshexrr r rr=?s"   zGenericRdata.from_textcCs|j|jdS)N)writer)r)r6r7r2r r rr8SszGenericRdata.to_wirecCs|||||||S)Nr )rTr'r(rVrWrXr2r r rrYVszGenericRdata.from_wire)NT)NT)NN)N) r[r\r]r^r_r*r5r`r=r8rY __classcell__r r )rcrra.s    raz dns.rdtypescCsdd}tj||f}tjj|}tjj|}|jdd}|stjtjj|f}|sy$|djt ||g}|t||f<WnVt k ry(|djt d|g}|ttjj|f<Wnt k rd}YnXYnX|rt ||}nt }|S)NcSs6t|}|jd}x|ddD]}t||}q W|S)N.r) __import__splitgetattr)rGmodZ componentscompr r r import_module`s  z&get_rdata_class..import_module-_rnANY) _rdata_modulesrer+rCr5r,replacerwr_module_prefix ImportErrorrqra)r'r(rtrrZ rdclass_textZ rdtype_textrTr r rget_rdata_class^s.      r|TcCst|trtjj|}t||}|tkrx|j}|j||j rx|j dkrxtj |||||}t |||j dt|j |S|j |||||S)adBuild 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 I{tok} is a string, then a tokenizer is created and the string is used as its input. @param rdclass: The rdata class @type rdclass: int @param rdtype: The rdata type @type rdtype: int @param tok: The tokenizer or input text @type tok: dns.tokenizer.Tokenizer or string @param origin: The origin to use for relative names @type origin: dns.name.Name @param relativize: Should names be relativized? @type relativize: bool @rtype: dns.rdata.Rdata instancez\#r)rrr+Z tokenizer Tokenizerr|rareZungetrfrgr=rYrr)r'r(rUr2r3rTrjr<r r rr=s      r=cCs*tjj|}t||}|j||||||S)a>Build 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. @param rdclass: The rdata class @type rdclass: int @param rdtype: The rdata type @type rdtype: int @param wire: The wire-format message @type wire: string @param current: The offset in wire of the beginning of the rdata. @type current: int @param rdlen: The length of the wire-format rdata @type rdlen: int @param origin: The origin to use for relative names @type origin: dns.name.Name @rtype: dns.rdata.Rdata instance)r+ZwiredataZ maybe_wrapr|rY)r'r(rVrWrXr2rTr r rrYs  rY)NT)N) r^iorrrZ dns.exceptionr+Zdns.nameZdns.rdataclassZ dns.rdatatypeZ dns.tokenizerZ dns.wiredataZ_compatrrrZ_hex_chunksizerZ_base64_chunksizerrrr#r%objectr&rarxrzr|r=rYr r r rs2   8," /