3 Ofr@sdZddlmZddlZddlZddlZddlZyddlZdZ Wne k rXdZ YnXddl Z ddl Z ddlmZmZmZmZmZy ejZWn.ek rddejd >d dZYnXdZdZd Zd Zd ZGd dde jjZGddde jjZGddde jj Z!Gddde jj Z"Gddde jj#Z$Gddde jj Z%Gddde jjZ&Gddde jj#Z'Gddde jj#Z(Gdd d e jj#Z)Gd!d"d"e jj#Z*Gd#d$d$e+Z,Gd%d&d&e,Z-Gd'd(d(e,Z.e/d)Z0d*Z1e-dZ2e-dZ3e2Z4e.ddddZ5e.ddddZ6e.ddddZ7e.ddddZ8e5Z9d+d,Z:d-d.Z;d/d0Ze=gZ?e>dfd4d5Z@e>dfd6d7ZAd8d9ZBdS):zDNS Names. @var root: The DNS root name. @type root: dns.name.Name object @var empty: The empty DNS name. @type empty: dns.name.Name object )BytesIONTF)long binary_type text_typeunichr maybe_decodePc@seZdZdZdS) EmptyLabelzA DNS label is empty.N)__name__ __module__ __qualname____doc__rr/usr/lib/python3.6/name.pyr4src@seZdZdZdS) BadEscapez8An escaped code in a text format of DNS name is invalid.N)rrrrrrrrr9src@seZdZdZdS) BadPointerz=A DNS compression pointer points forward instead of backward.N)rrrrrrrrr>src@seZdZdZdS) BadLabelTypez2The label type in DNS name wire format is unknown.N)rrrrrrrrrCsrc@seZdZdZdS)NeedAbsoluteNameOrOriginzvAn attempt was made to convert a non-absolute name to wire when there was also a non-absolute (or missing) origin.N)rrrrrrrrrHsrc@seZdZdZdS) NameTooLongz A DNS name is > 255 octets long.N)rrrrrrrrrNsrc@seZdZdZdS) LabelTooLongz A DNS label is > 63 octets long.N)rrrrrrrrrSsrc@seZdZdZdS)AbsoluteConcatenationz]An attempt was made to append anything other than the empty name to an absolute DNS name.N)rrrrrrrrrXsrc@seZdZdZdS)NoParentzMAn attempt was made to get the parent of the root name or the empty name.N)rrrrrrrrr^src@seZdZdZdS) NoIDNA2008zLIDNA 2008 processing was requested but the idna module is not available.N)rrrrrrrrrcsrc@seZdZdZedgZdZdS) IDNAExceptionz$IDNA processing raised an exception.idna_exceptionz+IDNA processing exception: {idna_exception}N)rrrrsetZ supp_kwargsZfmtrrrrris rc@s8eZdZdZddZddZddZdd Zd d Zd S) IDNACodecz.Abstract base class for IDNA encoder/decoders.cCsdS)Nr)selfrrr__init__tszIDNACodec.__init__cCs|jjdS)Nsxn--)lower startswith)r"labelrrris_idnawszIDNACodec.is_idnacCs"x|D]}t|dkrdSqWdS)NFT)ord)r"r&crrr is_all_asciizs  zIDNACodec.is_all_asciicCstdS)N)NotImplementedError)r"r&rrrencodeszIDNACodec.encodecCs`|j|rPy|ddjd}WqXtk rL}zt|dWYdd}~XqXXnt|}t|S)Nr Zpunycode)r)r'decode Exceptionrr _escapify)r"r&errrr.s zIDNACodec.decodeN) rrrrr#r'r+r-r.rrrrr!ps r!cs6eZdZdZd fdd ZddZfddZZS) IDNA2003CodeczIDNA 2003 encoder/decoder.Fcstt|j||_dS)aInitialize the IDNA 2003 encoder/decoder. @param strict_decode: If True, then IDNA2003 checking is done when decoding. This can cause failures if the name was encoded with IDNA2008. The default is False. @type strict_decode: bool N)superr2r# strict_decode)r"r4) __class__rrr#szIDNA2003Codec.__init__c Cs6|dkr dSy tjj|Stk r0tYnXdS)N) encodingsidnaZToASCII UnicodeErrorr)r"r&rrrr-s  zIDNA2003Codec.encodecsd|jstt|j|S|dkr"dSyttjj|Stk r^}zt |dWYdd}~XnXdS)Nr7r6)r) r4r3r2r.r0r8r9Z ToUnicoder/r)r"r&r1)r5rrr.szIDNA2003Codec.decode)F)rrrrr#r-r. __classcell__rr)r5rr2s r2cs6eZdZdZd fdd ZddZfddZZS) IDNA2008CodeczIDNA 2008 encoder/decoder.Fcs*tt|j||_||_||_||_dS)aInitialize the IDNA 2008 encoder/decoder. @param uts_46: If True, apply Unicode IDNA compatibility processing as described in Unicode Technical Standard #46 (U{http://unicode.org/reports/tr46/}). This parameter is only meaningful if IDNA 2008 is in use. If False, do not apply the mapping. The default is False @type uts_46: bool @param transitional: If True, use the "transitional" mode described in Unicode Technical Standard #46. This parameter is only meaningful if IDNA 2008 is in use. The default is False. @type transitional: bool @param allow_pure_ascii: If True, then a label which consists of only ASCII characters is allowed. This is less strict than regular IDNA 2008, but is also necessary for mixed names, e.g. a name with starting with "_sip._tcp." and ending in an IDN suffixm which would otherwise be disallowed. The default is False @type allow_pure_ascii: bool @param strict_decode: If True, then IDNA2008 checking is done when decoding. This can cause failures if the name was encoded with IDNA2003. The default is False. @type strict_decode: bool N)r3r<r#uts_46 transitionalallow_pure_asciir4)r"r=r>r?r4)r5rrr#s zIDNA2008Codec.__init__cCs|dkr dS|jr&|j|r&|jdSts.ty |jrFtj|d|j}tj |Stj k r|}zt |dWYdd}~XnXdS)Nr6r7asciiF)r) r?r+r-have_idna_2008rr=r9 uts46_remapr>Zalabel IDNAErrorr)r"r&r1rrrr-s  zIDNA2008Codec.encodecs|jstt|j|S|dkr"dSts*ty"|jr@tj|dd}t tj |Stj k rz}zt |dWYdd}~XnXdS)Nr7r6F)r) r4r3r<r.rArr=r9rBr0ZulabelrCr)r"r&r1)r5rrr.szIDNA2008Codec.decode)FFFF)rrrrr#r-r.r;rr)r5rr<s r<s"().;\@$z"().;\@$cCst|trzd}t|tr |j}xTt|D]H}|tkrH|dt|7}q*|dkrf|dkrf|t|7}q*|d|7}q*W|Sd}xD|D]<}|tkr|d|7}q|dkr|dt|7}q||7}qW|S)zaEscape the characters in label which need it. @returns: the escaped string @rtype: stringr6\ r(z\%03d ) isinstancebytesrr- bytearray_escapedchr _escaped_textr))r&textr*rrrr0s&    r0cCst|}d}d}d}xH|D]@}t|}||d7}|dkr>t|dkrR|dkrR|}|d7}qW|dkrjt|dkr||dkrtdS)a/Check for empty labels in the middle of a label sequence, labels that are too long, and for too many labels. @raises NameTooLong: the name as a whole is too long @raises EmptyLabel: a label is empty (i.e. the root label) and appears in a position other than the end of the label sequencerr?r7N)lenrrr)labelsltotalijr&Zllrrr_validate_labelss    rWcCs(t|tr|St|tr |jStdS)N)rGrrr- ValueError)r&rrr _ensure_bytes/s   rYcsDeZdZdZdgZfddZddZddZd d Zd d Z fd dZ ddZ ddZ ddZ ddZddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,ZdLd.d/ZdMd1d2ZdNd3d4ZdOd5d6Zd7d8Zd9d:Zd;d<Z d=d>Z!d?d@Z"dAdBZ#dCdDZ$dEdFZ%dPdHdIZ&dJdKZ'Z(S)QNamezA DNS name. The dns.name.Name class represents a DNS name as a tuple of labels. Instances of the class are immutable. @ivar labels: The tuple of labels in the name. Each label is a string of up to 63 octets.rRcs2dd|D}tt|jdt|t|jdS)zInitialize a domain name from a list of labels. @param labels: the labels @type labels: any iterable whose values are strings cSsg|] }t|qSr)rY).0xrrr Hsz!Name.__init__..rRN)r3rZ __setattr__tuplerWrR)r"rR)r5rrr#Csz Name.__init__cCs tddS)Nz+object doesn't support attribute assignment) TypeError)r"namevaluerrrr^LszName.__setattr__cCs t|jS)N)rZrR)r"rrr__copy__Osz Name.__copy__cCsttj|j|S)N)rZcopydeepcopyrR)r"memorrr __deepcopy__RszName.__deepcopy__cCs d|jiS)NrR)rR)r"rrr __getstate__UszName.__getstate__cs$tt|jd|dt|jdS)NrR)r3rZr^rWrR)r"state)r5rr __setstate__XszName.__setstate__cCst|jdko|jddkS)zXIs the most significant label of this name the root label? @rtype: bool rrr7rP)rQrR)r"rrr is_absolute\szName.is_absolutecCst|jdko|jddkS)z\Is this name wild? (I.e. Is the least significant label '*'?) @rtype: bool r*)rQrR)r"rrris_wildcsz Name.is_wildcCsJtd}x4|jD]*}x$t|jD]}||d>|7}q"WqWt|tS)zHReturn a case-insensitive hash of the name. @rtype: int rr )rrRrIr$intmaxint)r"hr&r*rrr__hash__js  z Name.__hash__c Cs*|j}|j}||kr0|r&tddfStddfSt|j}t|j}||}|dkrZ|}n|}d}d} t} x|dkr|d8}|d8}|d8}|j|j} |j|j} | | krd}| dkrt} | || fS| | krd}| dkrt} | || fS| d7} qlW|}|dkr t} n|dkrt} nt} | || fS)aCompare two names, returning a 3-tuple (relation, order, nlabels). I{relation} describes the relation ship between the names, and is one of: dns.name.NAMERELN_NONE, dns.name.NAMERELN_SUPERDOMAIN, dns.name.NAMERELN_SUBDOMAIN, dns.name.NAMERELN_EQUAL, or dns.name.NAMERELN_COMMONANCESTOR I{order} is < 0 if self < other, > 0 if self > other, and == 0 if self == other. A relative name is always less than an absolute name. If both names have the same relativity, then the DNSSEC order relation is used to order them. I{nlabels} is the number of significant labels that the two names have in common. rrrPrP) rk NAMERELN_NONErQrRr$NAMERELN_COMMONANCESTORNAMERELN_SUPERDOMAINNAMERELN_SUBDOMAINNAMERELN_EQUAL) r"otherZsabsZoabsl1l2ZldiffrSorderZnlabelsZnamerelnZlabel1Zlabel2rrr fullcompareusN          zName.fullcomparecCs(|j|\}}}|tks |tkr$dSdS)zoIs self a subdomain of other? The notion of subdomain includes equality. @rtype: bool TF)r{rurv)r"rwnronlrrr is_subdomainszName.is_subdomaincCs(|j|\}}}|tks |tkr$dSdS)zqIs self a superdomain of other? The notion of subdomain includes equality. @rtype: bool TF)r{rtrv)r"rwr|r}r~rrris_superdomainszName.is_superdomaincCstdd|jDS)zReturn a name which is equal to the current name, but is in DNSSEC canonical form. @rtype: dns.name.Name object cSsg|] }|jqSr)r$)r[r\rrrr]sz%Name.canonicalize..)rZrR)r"rrr canonicalizeszName.canonicalizecCs$t|tr|j|ddkSdSdS)NrrF)rGrZr{)r"rwrrr__eq__s z Name.__eq__cCs$t|tr|j|ddkSdSdS)NrrT)rGrZr{)r"rwrrr__ne__s z Name.__ne__cCs$t|tr|j|ddkStSdS)Nrr)rGrZr{NotImplemented)r"rwrrr__lt__s z Name.__lt__cCs$t|tr|j|ddkStSdS)Nrr)rGrZr{r)r"rwrrr__le__s z Name.__le__cCs$t|tr|j|ddkStSdS)Nrr)rGrZr{r)r"rwrrr__ge__s z Name.__ge__cCs$t|tr|j|ddkStSdS)Nrr)rGrZr{r)r"rwrrr__gt__s z Name.__gt__cCsd|jdS)Nz )__str__)r"rrr__repr__sz Name.__repr__cCs |jdS)NF)to_text)r"rrrrsz Name.__str__FcCspt|jdkrtdSt|jdkr:|jddkr:tdS|rV|jrV|jdd}n|j}djtt|}|S) zConvert name to text format. @param omit_final_dot: If True, don't emit the final dot (denoting the root label) for absolute names. The default is False. @rtype: string r@rr7.N.rP)rQrRrrkjoinmapr0)r"omit_final_dotrSsrrrrs z Name.to_textNcsxt|jdkrdSt|jdkr2|jddkr2dS|rN|jrN|jdd }n|j}dkr`tdjfdd|DS) aConvert name to Unicode text format. IDN ACE labels are converted to Unicode. @param omit_final_dot: If True, don't emit the final dot (denoting the root label) for absolute names. The default is False. @type omit_final_dot: bool @param idna_codec: IDNA encoder/decoder. If None, the IDNA_2003_Practical encoder/decoder is used. The IDNA_2003_Practical decoder does not impose any policy, it just decodes punycode, so if you don't want checking for compliance, you can use this decoder for IDNA2008 as well. @type idna_codec: dns.name.IDNA @rtype: string r@rr7rNcsg|]}j|qSr)r.)r[r\) idna_codecrrr])sz#Name.to_unicode..rP)rQrRrkIDNA_2003_Practicalr)r"rrrSr)rr to_unicodes zName.to_unicodecCsX|js:|dks|j rtt|j}|jt|jn|j}dd|D}dj|S)aConvert name to a format suitable for digesting in hashes. The name is canonicalized and converted to uncompressed wire format. @param origin: If the name is relative and origin is not None, then origin will be appended to it. @type origin: dns.name.Name object @raises NeedAbsoluteNameOrOrigin: All names in wire format are absolute. If self is a relative name, then an origin must be supplied; if it is missing, then this exception is raised @rtype: string NcSs*g|]"}tjdt|t||jqS)z!B%ds)structpackrQr$)r[r\rrrr]@sz&Name.to_digestable..r7)rkrlistrRextendr)r"originrRZdlabelsrrr to_digestable+s zName.to_digestablec Cs.|dkrt}d}nd}|jsR|dks2|j r6tt|j}|jt|jn|j}d}x|D]}t||d}|d7}|dk r|j|} nd} | dk rd| } tj d| } |j | Pqb|dk rt |dkr|j } | dkr| ||<t |} |j tj d | | dkrb|j |qbW|r*|j SdS) aConvert name to wire format, possibly compressing it. @param file: the file where the name is emitted (typically a BytesIO file). If None, a string containing the wire name will be returned. @type file: file or None @param compress: The compression table. If None (the default) names will not be compressed. @type compress: dict @param origin: If the name is relative and origin is not None, then origin will be appended to it. @type origin: dns.name.Name object @raises NeedAbsoluteNameOrOrigin: All names in wire format are absolute. If self is a relative name, then an origin must be supplied; if it is missing, then this exception is raised NTFrriz!Hi?z!B)rrkrrrRrrZgetrrwriterQtellgetvalue) r"filecompressrZ want_returnrRrUr&nposrbrrSrrrto_wireDs@     z Name.to_wirecCs t|jS)z@The length of the name (in labels). @rtype: int )rQrR)r"rrr__len__|sz Name.__len__cCs |j|S)N)rR)r"indexrrr __getitem__szName.__getitem__cCs |j|S)N) concatenate)r"rwrrr__add__sz Name.__add__cCs |j|S)N) relativize)r"rwrrr__sub__sz Name.__sub__cCsnt|j}|dkr|tjjfS||kr2tjj|fS|dksB||krJtdt|d| t|| dfS)a(Split a name into a prefix and suffix at depth. @param depth: the number of labels in the suffix @type depth: int @raises ValueError: the depth was not >= 0 and <= the length of the name. @returns: the tuple (prefix, suffix) @rtype: tuple rz0depth must be >= 0 and <= the length of the nameN)rQrRdnsraemptyrXrZ)r"depthrSrrrsplits   z Name.splitcCs:|jrt|dkrtt|j}|jt|jt|S)zReturn a new name which is the concatenation of self and other. @rtype: dns.name.Name object @raises AbsoluteConcatenation: self is absolute and other is not the empty name r)rkrQrrrRrrZ)r"rwrRrrrrs  zName.concatenatecCs0|dk r(|j|r(t|dt| S|SdS)zIf self is a subdomain of origin, return a new name which is self relative to origin. Otherwise return self. @rtype: dns.name.Name object N)rrZrQ)r"rrrrrszName.relativizecCs|js|j|S|SdS)zIf self is a relative name, return a new name which is the concatenation of self and origin. Otherwise return self. @rtype: dns.name.Name object N)rkr)r"rrrr derelativizes zName.derelativizeTcCs&|r|r|j|S|j|Sn|SdS)aReturn a name with the relativity desired by the caller. If origin is None, then self is returned. Otherwise, if relativize is true the name is relativized, and if relativize is false the name is derelativized. @rtype: dns.name.Name object N)rr)r"rrrrrchoose_relativitys   zName.choose_relativitycCs&|tks|tkrtt|jddS)zReturn the parent of the name. @rtype: dns.name.Name object @raises NoParent: the name is either the root name or the empty name, and thus has no parent. rN)rootrrrZrR)r"rrrparentsz Name.parent)F)FN)N)NNN)NT))rrrr __slots__r#r^rcrgrhrjrkrmrqr{rrrrrrrrrrrrrrrrrrrrrrrrrr;rr)r5rrZ7sJ   =      8   rZr7c Cst|tstd|dkp"t|ts,tdg}d}d}d}d}|dkrLt}|dkrXd}|rz|dkrptd gSx|D]}|r|dkr|jrt|}|d 7}q||7}d}n@|jst|d 9}|t|7}|d 7}|d krd}|t|7}qv|dkr"t |dkr t |j |j |d}qv|dkr:d}d}d}qv||7}qvW|rPtt |dkrp|j |j |n |j d t |dks|dd kr|dk r|j t|jt|S)aConvert unicode text into a Name object. Labels are encoded in IDN ACE form. @param text: The text to convert into a name. @type text: Unicode string @param origin: The origin to append to non-absolute names. @type origin: dns.name.Name @param idna_codec: IDNA encoder/decoder. If None, the default IDNA 2003 encoder/decoder is used. @type idna_codec: dns.name.IDNA @rtype: dns.name.Name object z0input to from_unicode() must be a unicode stringNzorigin must be a Name or Noner6Frrrr7r r 。.。rDT)rrrrrP)rGrrXrZ IDNA_2003isdigitrnrrrQrappendr-rrrR) rMrrrRr&escapingedigitsrTr*rrr from_unicodesd         &rc Cst|trt|||St|ts(td|dkp8t|tsBtdg}d}d}d}d}|dkrbd}|r|dkrztdgSxt|D]}tjd |} |r|dkr| j rt | }|d 7}n || 7}d}nF| j st |d 9}|t | 7}|d 7}|d kr^d}|tjd |7}q| dkr>t |dkr.t |j|d}q| d krVd}d}d}q|| 7}qW|rlt t |dkr|j|n |jdt |dks|ddkr|dk r|jt|jt|S)a}Convert text into a Name object. @param text: The text to convert into a name. @type text: string @param origin: The origin to append to non-absolute names. @type origin: dns.name.Name @param idna_codec: IDNA encoder/decoder. If None, the default IDNA 2003 encoder/decoder is used. @type idna_codec: dns.name.IDNA @rtype: dns.name.Name object z%input to from_text() must be a stringNzorigin must be a Name or Noner7Frrrz!Brrr \TrP)rGrrrrXrZrIrrrrnrrQrrrrrR) rMrrrRr&rrrTr*Zbyte_rrr from_text*sf              &rcCst|tstdtjj|}g}|}d}||}|d7}d}x|dkr|dkr|j||||j||7}|dkr||7}nJ|dkr|d@d||}|dkr|d7}||krt|}|d7}nt ||}|d7}|dkr@|d7}q@W|jdt ||fS) a^Convert possibly compressed wire format into a Name. @param message: the entire DNS message @type message: string @param current: the offset of the beginning of the name from the start of the message @type current: int @raises dns.name.BadPointer: a compression pointer did not point backwards in the message @raises dns.name.BadLabelType: an invalid label type was encountered. @returns: a tuple consisting of the name that was read and the number of bytes of the wire format message which were consumed reading it @rtype: (dns.name.Name object, int) tuple z*input to from_wire() must be a byte stringrr@rNr6) rGrrXrZwiredataZ maybe_wraprZunwraprrrZ)messageZcurrentrRZbiggest_pointerZhopscountZcusedrrr from_wireps<       r)CriorrsysrdZencodings.idnar8r9rA ImportErrorZ dns.exceptionrZ dns.wiredataZ_compatrrrrrroAttributeErrorcalcsizerrrtrurvrsZ exception SyntaxErrorrrZ FormErrorrrZ DNSExceptionrrrrrrrobjectr!r2r<rIrJrLrZIDNA_2003_StrictrZIDNA_2008_PracticalZIDNA_2008_UTS_46ZIDNA_2008_StrictZIDNA_2008_TransitionalZ IDNA_2008r0rWrYrZrrrrrrrrrst     >* HF