U mfP@sUdZddlZddlZddlZddlZddlZddlZddlZddlm Z m Z m Z m Z m Z mZmZmZddlmZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddlZddl Zddl!Zddl"Zddl#Zddl$Zej%dkrddl&ZGdddej'j(Z)Gdd d ej'j(Z*ee e+e,e-ee.e+fe ej/j0fZ1e e1e e+d d d Z2Gd ddej'j3Z4e4Z3Gdddej'j(Z5Gdddej'j(Z6Gdddej'j(Z7Gdddej'j(Z8Gdddej'j(Z9Gdddej'j(Z:GdddZ;Gddde<Z=Gdd d e=Z>Gd!d"d"Z?Gd#d$d$Z@eejAjBejCjDejEjFfZGGd%d&d&e@ZHGd'd(d(ZIGd)d*d*e@ZJGd+d,d,ZKGd-d.d.ZLGd/d0d0eLZMdaNe eMeOd1<eMd2d3d4ZPdd2d5d6ZQejCjRejEjSd7dd8dddfeejAjBe+feejCjDe+feejEjFe+fe,e e+e,e-e eTe e,e;d9 d:d;ZUejCjRejEjSd7dd8ddfeejAjBe+feejCjDe+feejEjFe+fe,e e+e,e-e eTe;d< d=d>ZVe+e e e;d?d@dAZWejXfeejAjBe+fe-e e>dBdCdDZYeejAjBe+fejAjBdEdFdGZZdgeTddIdJdKZ[ejEjSd7ddfeejAjBe+fejEjFe,e eMe eTejAjBdLdMdNZ\dOejXdfeejAjBe+fe-e-e eMeMdPdQdRZ]ejCjRejEjSd7dd8ddddOejXdf eejAjBe+feejAjBe+feejCjDe+feejEjFe+fe,e e+e,e-e eTe e,e-e-e eMe;dSdTdUZ^ej_ej`gejaejbgiZcdadejeZfejgZhejiZjejkZlejmZnejoZpddejXdddfdVdWZqdhdXdYZrdidZd[Zsd\d]Ztd^d_Zud`daZvdje eMddbdcddZwdd2dedfZxdS)kDNS stub resolver.N)AnyDictIteratorListOptionalSequenceTupleUnion)urlparsewin32csteZdZdZddhZdZfddZdddZed fd d Z e d d Z ddZ ddZ ddZddZZS)NXDOMAINz"The DNS query name does not exist.qnames responsesNcstj||dSNsuper__init__selfargskwargs __class__:/opt/hc_python/lib/python3.8/site-packages/dns/resolver.pyr>szNXDOMAIN.__init__cCs\t|tttfstdt|dkr,td|dkr:i}nt|tsLtdt||d}|S)Nz#qnames must be a list, tuple or setrz(qnames must contain at least one elementz(responses must be a dict(qname=response)rr) isinstancelisttuplesetAttributeErrorlendict)rrrrrrr _check_kwargsAs   zNXDOMAIN._check_kwargsreturncsPd|jkrtS|jd}t|dkr0d}nd}dtt|}d||S)NrzNone of DNS query names existz!The DNS query name does not existz, z{}: {})rr__str__r"joinmapstrformat)rrmsgrrrr(Ms    zNXDOMAIN.__str__c Csrd|jkrtd|jdD]F}|jd|}z|}||krJ|WSWqtk r`YqXq|jddS)z%Return the unresolved canonical name.rzparametrized exception requiredrr)r TypeErrorcanonical_name Exception)rqnameresponsecnamerrrr/Xs zNXDOMAIN.canonical_namecCsxt|jdg}t|jdi}|jdi}|jdgD]*}||krV||||kr@||||<q@t||dS)z3Augment by results from another NXDOMAIN exception.rrr)rrgetr#appendr )rZe_nxZqnames0Z responses0Z responses1Zqname1rrr__add__is zNXDOMAIN.__add__cCs |jdS)zXAll of the names that were tried. Returns a list of ``dns.name.Name``. rrrrrrruszNXDOMAIN.qnamescCs |jdS)zA map from queried names to their NXDOMAIN responses. Returns a dict mapping a ``dns.name.Name`` to a ``dns.message.Message``. rr7r8rrrr|szNXDOMAIN.responsescCs|jd|S)zTThe response for query *qname*. Returns a ``dns.message.Message``. rr7)rr1rrrr2szNXDOMAIN.response)N)__name__ __module__ __qualname____doc__ supp_kwargsfmtrr$r+r(propertyr/r6rrr2 __classcell__rrrrr 4s     r c@seZdZdZdS)YXDOMAINz8The DNS query name is too long after DNAME substitution.Nr9r:r;r<rrrrrAsrA)errorsr&cCs,g}|D]}|d|d|dq|S)z3Turn a resolution errors trace into a list of text.zServer {} answered {}r)r5r,)rCZtextserrrrr_errors_to_textsrFcsHeZdZdZdZdeddZddhZfddZfd d ZZ S) LifetimeTimeoutz The resolution lifetime expired.z(%s after {timeout:.3f} seconds: {errors}NtimeoutrCcstj||dSrrrrrrrszLifetimeTimeout.__init__c s&t|d}tj|dd|dS)NrCrI; rIrC)rFr _fmt_kwargsr)rrZsrv_msgsrrrrLs  zLifetimeTimeout._fmt_kwargs r9r:r;r<r-r>r=rrLr@rrrrrGs  rGcs>eZdZdZdZdhZfddZfddZdd ZZ S) NoAnswerzr=rrLr2r@rrrrrOs   rOcsHeZdZdZdZdeddZddhZfdd Zfd d ZZ S) NoNameserverszAll nameservers failed to answer the query. errors: list of servers and respective errors The type of errors is [(server IP address, any object convertible to string)]. Non-empty errors list will add explanatory message () z+All nameservers failed to answer the query.z%s {query}: {errors}NrHrequestrCcstj||dSrrrrrrrszNoNameservers.__init__c s(t|d}tj|djd|dS)NrCrSrJ)rPrC)rFrrLrQr)rMrrrrLs  zNoNameservers._fmt_kwargsrNrrrrrRs  rRc@seZdZdZdS) NotAbsolutezEAn absolute domain name is required but a relative name was provided.NrBrrrrrTsrTc@seZdZdZdS) NoRootSOAzBThere is no SOA RR at the DNS root name. This should never happen!NrBrrrrrUsrUc@seZdZdZdS) NoMetaqueriesz DNS metaqueries are not allowed.NrBrrrrrVsrVc@seZdZdZdS)NoResolverConfigurationzEResolver configuration could not be read or specified no nameservers.NrBrrrrrWsrWc @sreZdZdZdejjejjej j ej j e ee eddddZddZedd d Zd d Zd dZddZdS)Answera&DNS stub resolver answer. Instances of this class bundle up the result of a successful DNS resolution. For convenience, the answer object implements much of the sequence protocol, forwarding to its ``rrset`` attribute. E.g. ``for a in answer`` is equivalent to ``for a in answer.rrset``. ``answer[i]`` is equivalent to ``answer.rrset[i]``, and ``answer[i:j]`` is equivalent to ``answer.rrset[i:j]``. Note that CNAMEs or DNAMEs in the response may mean that answer RRset's name might not be the query name. N)r1rdtyperdclassr2 nameserverportr&cCsX||_||_||_||_||_||_||_|jj|_|jj |_ t |jj |_ dSr)r1rYrZr2r[r\Zresolve_chainingZchaining_resultr/answerrrsettimeZ minimum_ttl expiration)rr1rYrZr2r[r\rrrrs    zAnswer.__init__cCs\|dkr|jjS|dkr |jjS|dkr0|jjS|dkr@|jjS|dkrP|jjSt|dS)NnamettlcoversrZrY)r^rarbrcrZrYr!)rattrrrr __getattr__szAnswer.__getattr__r%cCs|jrt|jpdSNr)r^r"r8rrr__len__)szAnswer.__len__cCs|jrt|jpttSr)r^iterrr8rrr__iter__,szAnswer.__iter__cCs|jdkrt|j|Srr^ IndexErrorrirrr __getitem__/s zAnswer.__getitem__cCs|jdkrt|j|=dSrrjrlrrr __delitem__4s zAnswer.__delitem__)NN)r9r:r;r<dnsraName rdatatype RdataType rdataclass RdataClassmessage QueryMessagerr+intrrergrirnrorrrrrXs" rXc@seZdZdZdS)Answersz5A dict of DNS stub resolver answers, indexed by type.NrBrrrrry:sryc@seZdZdZedeeeeeddddZe j fe e e ee fddd Ze j fe e edd d Zejjd d dZdS) HostAnswerszTA dict of DNS stub resolver answers to a host name lookup, indexed by type. NT)v6v4 add_emptyr&cCsFt}|dk r$|s|jr$||tjj<|dk rB|s6|jrB||tjj<|Sr)rzr^rprrAAAAA)clsr{r|r}answersrrrmakeCs   zHostAnswers.make)familyr&ccs|tjkr2|tjEdH|tjEdHdS|tjkrL|tjj}n(|tjkrf|tjj }nt d||r|D]}|j |fVq|dS)Nunknown address family ) socket AF_UNSPECaddresses_and_familiesAF_INET6AF_INETr4rprrr~rNotImplementedErroraddress)rrr]ZrdatarrrrSs   z"HostAnswers.addresses_and_familiescCsdd||DS)Ncss|]}|dVqdS)rNr).0pairrrr gsz(HostAnswers.addresses..)r)rrrrr addressesfszHostAnswers.addressesr%cCs |tjj|tjj}|jSr)r4rprrr~rr/)rr]rrrr/jszHostAnswers.canonical_name)NNT)r9r:r;r< classmethodrrXboolrrrrxrr r+rrrprarqr/rrrrrz>s$ rzc@s@eZdZdZd eeddddZdddd Zddd d ZdS) CacheStatisticszCache StatisticsrN)hitsmissesr&cCs||_||_dSrrr)rrrrrrrrszCacheStatistics.__init__r%cCsd|_d|_dSrfrr8rrrresetvszCacheStatistics.resetcCst|j|jSr)rrrr8rrrclonezszCacheStatistics.clone)rr)r9r:r;r<rxrrrrrrrrosrc@sReZdZddddZddddZedddZedd d Zedd d Z dS) CacheBaseNr%cCst|_t|_dSr) threadingLocklockr statisticsr8rrrrs zCacheBase.__init__c Cs |j|jW5QRXdS)zReset all statistics to zero.N)rrrr8rrrreset_statisticsszCacheBase.reset_statisticsc Cs&|j|jjW5QRSQRXdS)z How many hits has the cache had?N)rrrr8rrrrszCacheBase.hitsc Cs&|j|jjW5QRSQRXdS)z"How many misses has the cache had?N)rrrr8rrrrszCacheBase.missesc Cs(|j|jW5QRSQRXdS)zReturn a consistent snapshot of all the statistics. If running with multiple threads, it's better to take a snapshot than to call statistics methods such as hits() and misses() individually. N)rrrr8rrrget_statistics_snapshotsz!CacheBase.get_statistics_snapshot) r9r:r;rrrxrrrrrrrrr~s rcsteZdZdZdeddfdd Zdddd Zeee d d d Z ee dd ddZ deedd ddZ Z S)Cachez$Simple thread-safe DNS answer cache.r@N)cleaning_intervalr&cs*ti|_||_t|j|_dS)zf*cleaning_interval*, a ``float`` is the number of seconds between periodic cleanings. N)rrdatarr_ next_cleaning)rrrrrrs zCache.__init__r%cCsht}|j|krdg}|jD]\}}|j|kr ||q |D] }|j|=qBt}||j|_dS)z&Clean the cache if it's time to do so.N)r_rritemsr`r5r)rnowZkeys_to_deletekvrrr _maybe_cleans    zCache._maybe_cleankeyr&c Csz|jj||j|}|dks2|jtkrP|jjd7_W5QRdS|jjd7_|W5QRSQRXdSaKGet the answer associated with *key*. Returns None if no answer is cached for the key. *key*, a ``(dns.name.Name, dns.rdatatype.RdataType, dns.rdataclass.RdataClass)`` tuple whose values are the query name, rdtype, and rdclass respectively. Returns a ``dns.resolver.Answer`` or ``None``. Nr') rrrr4r`r_rrr)rrrrrrr4s  z Cache.getrvaluer&c Cs(|j|||j|<W5QRXdSaAssociate key and value in the cache. *key*, a ``(dns.name.Name, dns.rdatatype.RdataType, dns.rdataclass.RdataClass)`` tuple whose values are the query name, rdtype, and rdclass respectively. *value*, a ``dns.resolver.Answer``, the answer. N)rrrrrrrrrputs z Cache.putc CsH|j8|dk r$||jkr:|j|=ni|_t|j|_W5QRXdSa2Flush the cache. If *key* is not ``None``, only that item is flushed. Otherwise the entire cache is flushed. *key*, a ``(dns.name.Name, dns.rdatatype.RdataType, dns.rdataclass.RdataClass)`` tuple whose values are the query name, rdtype, and rdclass respectively. N)rrr_rr)rrrrrflushs   z Cache.flush)r)N)r9r:r;r<floatrrCacheKeyrrXr4rrr@rrrrrs   rc@s6eZdZdZddZdddddZddd d ZdS) LRUCacheNodezLRUCache node.cCs"||_||_d|_||_||_dSrf)rrrprevnextrrrrrs zLRUCacheNode.__init__N)noder&cCs ||_|j|_||j_||_dSrrr)rrrrr link_afterszLRUCacheNode.link_afterr%cCs|j|j_|j|j_dSrrr8rrrunlinks zLRUCacheNode.unlink)r9r:r;r<rrrrrrrrsrcseZdZdZdeddfdd ZeddddZeee d d d Z eed d d Z ee ddddZ deedd ddZ ZS)LRUCachea`Thread-safe, bounded, least-recently-used DNS answer cache. This cache is better than the simple cache (above) if you're running a web crawler or other process that does a lot of resolutions. The LRUCache has a maximum number of nodes, and when it is full, the least-recently used node is removed to make space for a new one. 順N)max_sizer&cs>ti|_||tdd|_|j|j_|j|j_dS)zl*max_size*, an ``int``, is the maximum number of nodes to cache; it must be greater than 0. N)rrr set_max_sizersentinelrrrrrrrrs     zLRUCache.__init__cCs|dkr d}||_dS)Nr')rrrrrrszLRUCache.set_max_sizerc Cs|j|j|}|dkr:|jjd7_W5QRdS||jjtkrz|j|j =|jjd7_W5QRdS| |j |jj d7_ |j d7_ |jW5QRSQRXdSr) rrr4rrrrr`r_rrrrrrrrrrr4s    z LRUCache.getc CsZ|jJ|j|}|dks,|jjtkr:W5QRdS|jW5QRSW5QRXdS)zBReturn the number of cache hits associated with the specified key.Nr)rrr4rr`r_rrrrrget_hits_for_key;s  zLRUCache.get_hits_for_keyrc Cs|jx|j|}|dk r.||j|j=t|j|jkrZ|jj}||j|j=q.t ||}| |j||j|<W5QRXdSr) rrr4rrr"rrrrr)rrrrgnoderrrrDs      z LRUCache.putc Csr|jb|dk r8|j|}|dk rd||j|j=n,|jj}||jkr^|j}||}q@i|_W5QRXdSr)rrr4rrrr)rrrrrrrrrZs    zLRUCache.flush)r)N)r9r:r;r<rxrrrrrXr4rrrr@rrrrrs   rc @seZdZdZdeejjefeej j efeej j efe e ee ddddZeeejjeefddd Zeejje efdd d Zeejjeeeeee fd d dZdS) _ResolutionaHelper class for dns.resolver.Resolver.resolve(). All of the "business logic" of resolution is encapsulated in this class, allowing us to have multiple resolve() implementations using different I/O schemes without copying all of the complicated logic. This class is a "friend" to dns.resolver.Resolver and manipulates resolver data structures directly. BaseResolverN)resolverr1rYrZtcpraise_on_no_answersearchr&cCst|trtj|d}tjj|}tj|r6t tj j |}tj |rTt ||_ ||||_|jdd|_||_||_||_||_i|_tjj|_g|_g|_g|_d|_d|_d|_d|_d|_dS)NF) rr+rpra from_textrrrsrZ is_metatyperVrtruZ is_metaclassr_get_qnames_to_try qnames_to_tryrrYrZrrnxdomain_responsesemptyr1 nameserverscurrent_nameserversrCr[ tcp_attemptretry_with_tcprSbackoff)rrr1rYrZrrrrrrrs2    z_Resolution.__init__r%cCst|jdkr|jd|_|jjr|jj|j|j|jf}|dk rn|j dkrf|j rft |j dnd|fS|jj|jt jj|jf}|dk r|j t jjkr|j |j|j<qt j|j|j|j}|jjdk r|j|jj|jj|jjd|j|jj|jj|jj|jjd|jjdk r,|jj|_|j|jj |jj!|jj"|_#|jj$r`t%&|j#|j#dd|_'g|_(d|_)d|_*d|_+||_,d|_-|dfSt|j.|jddS) zGet the next request to send, and check the cache. Returns a (request, answer) tuple. At most one of request or answer will not be None. rNr2) algorithm)optionsFg?r)/r"rpopr1rcacher4rYrZr^rrOr2rprrANYrcoder rrvZ make_querykeynameuse_tsigkeyring keyalgorithmuse_ednsedns ednsflagspayload ednsoptionsflags_enrich_nameservers _nameserversnameserver_portsr\rrotaterandomshufflerrCr[rrrSrr)rr]rSrrr next_requests^     z_Resolution.next_requestcCs|jr:|jdk st|jr"td|_d|_|jddfSd}|jst|jdkrbt|j |j d|jdd|_|j }t |j dd|_ |j d|_|jp|j|_|j|j|fS)NTFrr)rSrC)rr[AssertionErrorZis_always_max_sizerrr"rrRrSrCrminrr)rrrrrnext_nameservers  z_Resolution.next_nameserver)r2exr&c Cs |jdk st|r|dkst|jt|j|j|j||ft|tj j snt|t snt|t snt|t r~|j|jn*t|tjjr|jr|j|jnd|_dS|dk stt|tjjst|}|tjjkrz(t|j|j|j||j|j}Wn\tk rd}z<|jt|j|j|j||f|j|jWYdSd}~XYnX|jjr|jj|j|j|jf||jdkr|j rt!|j"d|dfS|tjj#krtzt|jtj$j%tj&j'|}Wn\tk r:}z<|jt|j|j|j||f|j|jWYdSd}~XYnX||j(|j<|jjrp|jj|jtj$j%|jf|dS|tjj)krt)}|jt|j|j|j||f|nV|tjj*ks|jj+s|j|j|jt|j|j|jtj,||fdSdS)NT)NFr)NT)-r[rrCr5r+rZ answer_portrrp exceptionZ FormErrorEOFErrorOSErrorrrremovervZ TruncatedrrwrZNOERRORrXr1rYrZZanswer_nameserverr0rrrr^rrOr2r rrrrtINrrAZSERVFAILretry_servfailto_text)rr2rrr]eZyexrrr query_results            z_Resolution.query_result)r9r:r;r<r rprarqr+rrrsrtrurrrr rvrwrXrr[ NameserverrrMessager0rrrrrrss"  ' K rc@seZdZUdZejjed<ee e fed<e ed<e ejjed<e ed<e ed<e ed<eeed <eeejje fed <eejje fed <e ed <e ed <ee ejjed<e ed<eed<ee ed<e ed<e ed<ee ed<eee ejjfed<d;e e ddddZddddZeddd d!Zddd"d#Zdd|kr.zt|dd|_Wnttfk rYnXq.qVW5QRXt|dkrtd||_d S)aProcess *f* as a file in the /etc/resolv.conf format. If f is a ``str``, it is used as the name of the file to open; otherwise it is treated as the file itself. Interprets the following items: - nameserver - name server IP address - domain - local domain name - search - search list for host-name lookup - options - supported options are rotate, timeout, edns0, and ndots z cannot open r#;rr[r'rrNrrTZedns0rI:rzno nameservers)rr+openrrW contextlib nullcontextr"splitr5rprarrrrrrxrI ValueErrorrkrr)rrrcmltokenssuffixoptrrrrsT    $         zBaseResolver.read_resolv_confcCsNz0tj}|jdk r|j|_|j|_|j|_Wntk rHtYnXdS)z9Extract resolver configuration from the Windows registry.N)rpZ win32utilZ get_dns_inforrrr!r)rinforrrrs   zBaseResolver.read_registry)startrrCr&cCst|dkr|jn|}t}||}|dkr.g}|dkrP|dkrLt||dnd}||krdt||dt|||jS)NrrHrK)rr_rGrrI)rrrrCrdurationrrr_compute_timeouts zBaseResolver._compute_timeout)r1rr&cCs|dkr|j}g}|r&||n|tjj}|rt|jdkrV|jdd}n&|j tjjkrx|j dk rx|j g}ng}|j dkrd}n|j }|D]}|||qt||kr| d|q||n |||S)Nrr') r is_absoluter5Z concatenaterprar r"rrrinsert)rr1rrZ abs_qnameZ search_listrrrrrr6s,      zBaseResolver._get_qnames_to_try)rrrr&cCs||_||_||_dS)zAdd a TSIG signature to each query. The parameters are passed to ``dns.message.Message.use_tsig()``; see its documentation for details. N)rrr)rrrrrrrr_s zBaseResolver.use_tsigr)rrrrr&cCs>|dks|dkrd}n |dkr"d}||_||_||_||_dS)aConfigure 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. *options*, a list of ``dns.edns.Option`` objects or ``None``, the EDNS options. NFrHTr)rrrr)rrrrrrrrroszBaseResolver.use_edns)rr&cCs ||_dS)zkOverrides the default flags with your own. *flags*, an ``int``, the message flags to use. N)r)rrrrr set_flagsszBaseResolver.set_flags)rr default_portr&c Csg}t|tr|D]}t|tjjr*|}nptj|rR|||}tj||}nHzt |j dkrft Wn$t k rt d|dYnXtj|}||qnt dt||S)Nhttpsz nameserver z\ is not a dns.nameserver.Nameserver instance or text form, IP address, nor a valid https URLz.nameservers must be a list or tuple (not a {}))rrrpr[rinet is_addressr4Do53Nameserverr schemerr0rZ DoHNameserverr5r,type)rrrr$Zenriched_nameserversr[Zenriched_nameserverr\rrrrs4     z BaseResolver._enrich_nameserverscCs|jSr)rr8rrrrszBaseResolver.nameservers)rr&cCs|||j|j||_dS)a  *nameservers*, a ``list`` of nameservers, where a nameserver is either a string interpretable as a nameserver, or a ``dns.nameserver.Nameserver`` instance. Raises ``ValueError`` if *nameservers* is not a list of nameservers. N)rrr\r)rrrrrrs )rT)NN))r9r:r;r<rprarq__annotations__rr+rxrrrrrr rOptionrr[rrrrr ErrorTupler rr rrrvZDEFAULT_EDNS_PAYLOADrr#rrr?rsetterrrrrrs      G   ,  !  %rc @sHeZdZdZejjejjddddddfe ej j e fe ejj e fe ejje feee eeeeeeed ddZejjejjdddddfe ej j e fe ejj e fe ejje feee eeeeed d d Ze eeed d dZejfe ej j e feeedddZe ej j e fej j dddZdeddddZdS)ResolverrFNTr r1rYrZrsourcer source_portrrr&c  Cst||||||| } t} | \} } | dk r4| S| dk s@td}|s| \}}}|rdt||| || j}z|j| ||||d}Wn:t k r}z| d|\}}WYqDW5d}~XYnX| |d\} }| dk rD| SqDqdS)aQuery nameservers to find the answer to the question. The *qname*, *rdtype*, and *rdclass* parameters may be objects of the appropriate type, or strings that can be converted into objects of the appropriate type. *qname*, a ``dns.name.Name`` or ``str``, the query name. *rdtype*, an ``int`` or ``str``, the query type. *rdclass*, an ``int`` or ``str``, the query class. *tcp*, a ``bool``. If ``True``, use TCP to make the query. *source*, a ``str`` or ``None``. If not ``None``, bind to this IP address when making queries. *raise_on_no_answer*, a ``bool``. If ``True``, raise ``dns.resolver.NoAnswer`` if there's no answer to the question. *source_port*, an ``int``, the port from which to send the message. *lifetime*, a ``float``, how many seconds a query should run before timing out. *search*, a ``bool`` or ``None``, determines whether the search list configured in the system's resolver configuration are used for relative names, and whether the resolver's domain may be added to relative names. The default is ``None``, which causes the value of the resolver's ``use_search_by_default`` attribute to be used. Raises ``dns.resolver.LifetimeTimeout`` if no answers could be found in the specified lifetime. Raises ``dns.resolver.NXDOMAIN`` if the query name does not exist. Raises ``dns.resolver.YXDOMAIN`` if the query name is too long after DNAME substitution. Raises ``dns.resolver.NoAnswer`` if *raise_on_no_answer* is ``True`` and the query name exists but has no RRset of the desired type and class. Raises ``dns.resolver.NoNameservers`` if no non-broken nameservers are available to answer the question. Returns a ``dns.resolver.Answer`` instance. NF)rIr1r2r) rr_rrrsleepr rCrPr0r)rr1rYrZrr1rr2rr resolutionrrSr]doner[rrIr2r_rrrresolvesD?    zResolver.resolve r1rYrZrr1rr2rr&c Cs*tjdtdd|||||||||d S)a$Query nameservers to find the answer to the question. This method calls resolve() with ``search=True``, and is provided for backwards compatibility with prior versions of dnspython. See the documentation for the resolve() method for further details. z2please use dns.resolver.Resolver.resolve() insteadr stacklevelTwarningswarnDeprecationWarningr7) rr1rYrZrr1rr2rrrrrP=s zResolver.queryipaddrrrr&cOs@i}||tjj|d<tjj|d<|jtj|f||S)aUse a resolver to run a reverse query for PTR records. This utilizes the resolve() method to perform a PTR lookup on the specified IP address. *ipaddr*, a ``str``, the IPv4 or IPv6 address you want to get the PTR record for. All other arguments that can be passed to the resolve() function except for rdtype and rdclass are also supported by this function. rYrZ) updaterprrPTRrtrr7 reversename from_address)rr@rrmodified_kwargsrrrresolve_address`s    zResolver.resolve_addressrarrr&c Ks,i}|||ddtjj|d<|tjkrP|j|tjj f|}t j |dS|tj krz|j|tjj f|}t j |dS|tjkrtd||dd}|d d}t} |j|tjj fd || |d |}|j}|j|tjj fd || |d |}t j ||| d } | s(t|jd | S)aUse a resolver to query for address records. This utilizes the resolve() method to perform A and/or AAAA lookups on the specified name. *qname*, a ``dns.name.Name`` or ``str``, the name to resolve. *family*, an ``int``, the address family. If socket.AF_UNSPEC (the default), both A and AAAA records will be retrieved. All other arguments that can be passed to the resolve() function except for rdtype and rdclass are also supported by this function. rYNrZ)r|)r{rrTrF)rr)r{r|r}r)rArrprtrrrr7rrrrzrrr~rrr_r r1rOr2) rrarrrEr|r{rrrrrrr resolve_namexsL              zResolver.resolve_namerar&c CsJz|j|dd}|j}Wn,tjjk rD}z |j}W5d}~XYnX|S)aDetermine the canonical name of *name*. The canonical name is the name the resolver uses for queries after all CNAME and DNAME renamings have been applied. *name*, a ``dns.name.Name`` or ``str``, the query name. This method can raise any exception that ``resolve()`` can raise, other than ``dns.resolver.NoAnswer`` and ``dns.resolver.NXDOMAIN``. Returns a ``dns.name.Name``. F)rN)r7r/rprr )rrar]r/rrrrr/s  zResolver.canonical_namer rr&cCsjzPt|}|jtjjd|d}tj|}tj||}t|dkrN||_ Wnt k rdYnXdS)aTry to update the resolver's nameservers using Discovery of Designated Resolvers (DDR). If successful, the resolver will subsequently use DNS-over-HTTPS or DNS-over-TLS for future queries. *lifetime*, a float, is the maximum time to spend attempting DDR. The default is 5 seconds. If the SVCB query is successful and results in a non-empty list of nameservers, then the resolver's nameservers are set to the returned servers in priority order. The current implementation does not use any address hints from the SVCB record, nor does it resolve addresses for the SCVB target name, rather it assumes that the bootstrap nameserver will always be one of the addresses and uses it. A future revision to the code may offer fuller support. The code verifies that the bootstrap nameserver is in the Subject Alternative Name field of the TLS certficate. ZSVCBrrN) r_r7rpZ_ddrZ_local_resolver_namerPZ _remainingZ_get_nameservers_syncr"rr0)rrr`r]rIrrrrtry_ddrs    zResolver.try_ddr)r )r9r:r;r<rprrrrtrr rarqr+rsrurrrxrrXr7rPrrFrrrzrHr/rLrrrrr/sb i # C r/default_resolverr%cCstdkrttdk sttS)z7Get the default resolver, initializing it if necessary.N)rMreset_default_resolverrrrrrget_default_resolvers rOcCs tadS)zRe-initialize default resolver. Note that the resolver configuration (i.e. /etc/resolv.conf on UNIX systems) will be re-read immediately. N)r/rMrrrrrNsrNFTr0c Cst||||||||| S)zQuery nameservers to find the answer to the question. This is a convenience function that uses the default resolver object to make the query. See ``dns.resolver.Resolver.resolve`` for more information on the parameters. )rOr7) r1rYrZrr1rr2rrrrrr7 sr7r8c Cs(tjdtddt||||||||d S)aQuery nameservers to find the answer to the question. This method calls resolve() with ``search=True``, and is provided for backwards compatibility with prior versions of dnspython. See the documentation for the resolve() method for further details. z)please use dns.resolver.resolve() insteadrr9Tr;)r1rYrZrr1rr2rrrrrP*s rPr?cOstj|f||S)zUse a resolver to run a reverse query for PTR records. See ``dns.resolver.Resolver.resolve_address`` for more information on the parameters. )rOrF)r@rrrrrrFKsrFrGcKstj||f|S)zUse a resolver to query for address records. See ``dns.resolver.Resolver.resolve_name`` for more information on the parameters. )rOrH)rarrrrrrHUs rHrIcCs t|S)zDetermine the canonical name of *name*. See ``dns.resolver.Resolver.canonical_name`` for more information on the parameters and possible exceptions. )rOr/rarrrr/asr/r rJcCs t|S)aTry to update the default resolver's nameservers using Discovery of Designated Resolvers (DDR). If successful, the resolver will subsequently use DNS-over-HTTPS or DNS-over-TLS for future queries. See :py:func:`dns.resolver.Resolver.try_ddr` for more information. )rOrLrKrrrrLksrL)rarZrrrr&c Cst|trtj|tjj}|dkr*t}|s:t|t }|dk rT||}nd}zb|dk r||t }|dkrd}nd}|j |tj j |||d}|j dk st|j j|kr|WSWntjjtjjfk rr} zt| tjjr| |} n| } | rb| jD]T} | jtj j kr | j|kr | j|\} } } | tjjkr | jWY Sq W5d} ~ XYnXz |}WqXtjjk rtYqXXqXdS)aFind the name of the zone which contains the specified name. *name*, an absolute ``dns.name.Name`` or ``str``, the query name. *rdclass*, an ``int``, the query class. *tcp*, a ``bool``. If ``True``, use TCP to make the query. *resolver*, a ``dns.resolver.Resolver`` or ``None``, the resolver to use. If ``None``, the default, then the default resolver is used. *lifetime*, a ``float``, the total time to allow for the queries needed to determine the zone. If ``None``, the default, then only the individual query limits of the resolver apply. Raises ``dns.resolver.NoRootSOA`` if there is no SOA RR at the DNS root. (This is only likely to happen if you're using non-default root servers in your network and they are misconfigured.) Raises ``dns.resolver.LifetimeTimeout`` if the answer could not be found in the allotted lifetime. Returns a ``dns.name.Name``. NrrK)rr+rprarr rOr!rTr_r7rrZSOAr^rrr rOrr4r2 authorityrYrZZ fullcompareZNAMERELN_SUPERDOMAINparentZNoParentrU)rarZrrrrr`Z rlifetimer]rr2Zrrsnrr6rrr zone_for_nameusP      ( rTr )wherer\rrr&cCs|dkrt}g}t|tr>tj|r>|tj||n*| || D]}|tj||qNtj j dd}||_ |S)aMake a stub resolver using the specified destination as the full resolver. *where*, a ``dns.name.Name`` or ``str`` the domain name or IP address of the full resolver. *port*, an ``int``, the port to use. If not specified, the default is 53. *family*, an ``int``, the address family to use. This parameter is used if *where* is not an address. The default is ``socket.AF_UNSPEC`` in which case the first address returned by ``resolve_name()`` will be used, otherwise the first address of the specified family will be used. *resolver*, a ``dns.resolver.Resolver`` or ``None``, the resolver to use for resolution of hostnames. If not specified, the default resolver will be used. Returns a ``dns.resolver.Resolver`` or raises an exception. NF)r)rOrr+rpr&r'r5r[r(rHrrr/r)rUr\rrrrresrrrmake_resolver_atsrW)rUr1rYrZrr1rr2rrr\rrr&c Cs$t|| | | ||||||||| S)a}Query nameservers to find the answer to the question. This is a convenience function that calls ``dns.resolver.make_resolver_at()`` to make a resolver, and then uses it to resolve the query. See ``dns.resolver.Resolver.resolve`` for more information on the resolution parameters, and ``dns.resolver.make_resolver_at`` for information about the resolver parameters *where*, *port*, *family*, and *resolver*. If making more than one query, it is more efficient to call ``dns.resolver.make_resolver_at()`` and then use that resolver for the queries instead of calling ``resolve_at()`` multiple times. )rWr7) rUr1rYrZrr1rr2rrr\rrrrr resolve_atsrXc Cs^|tj@dkr t||||||S|tjtjB@dkrBttjd|dkr`|dkr`ttjdg}d}|dkrt||||||Sz tj |t||||||WSt k rYnXz&t ||}|}|d}WnJtjjk rttjdYn$t k r(ttjdYnXd} z|dkr@d} nt|} WnLt k r|tj@dkrzt|} Wnt k rYnXYnX| dkrttjdg} |dkrtjtjg} n|g} |tj@dkr|} nd} |D]L\} }| D]<}t|D],}tj | | f|}| |||| |fqqqt| dkrZttjd| S)Nrz*Non-recoverable failure in name resolutionName or service not knownTz$Temporary failure in name resolution)rAI_NUMERICHOST_original_getaddrinfo AI_ADDRCONFIG AI_V4MAPPEDgaierrorEAI_FAIL EAI_NONAMErpr&Zaf_for_addressr0 _resolverrHrr/rrr EAI_AGAINrxAI_NUMERICSERV getservbyname SOCK_DGRAM SOCK_STREAM AI_CANONNAME_protocols_for_socktypeZlow_level_address_tupler5r")hostservicersocktypeprotoraddrsr/rr\tuplesZ socktypesr3addrafZ addr_tuplerrr _getaddrinfo*sl           rrc Csh|d}|d}t|dkr,|d}tj}n d}tj}t|||tjtjd}t|dkrbtd|ddd}|tj@rd}nd}t j |} |tj @dkr z"t | d} | jdjd } WnXt jjt jjfk r|tj@rttjd |} |dk r| d t|7} YnXn|} |dk r>| d t|7} |tj@rTt|} n t||} | | fS) Nrr'rDz'sockaddr resolved to multiple addressesZudprrBTrY%)r"rrrrrrgSOL_TCPerrorNI_DGRAMrprCrDNI_NUMERICHOSTrbr7r^targetrrr rO NI_NAMEREQDr_rar+NI_NUMERICSERV getservbyport) sockaddrrrjr\scoperrorpZpnamer1r]hostnamerkrrr _getnameinfo~s@            rcCs<|dkrt}zt|\}}}Wntk r6YnX|Sr)rr _gethostbyaddrr0)rar6rrr_getfqdnsrcCst|ddS)Nrr)_gethostbyname_exrPrrr_gethostbynamesrcCsTg}g}t|dtjtjtjtj}|dd}|D]}||ddq2|||fS)NrrDrs)rrrrrgrurhr5)raaliasesrro canonicalitemrrrrs rc Csz"tj||dddf}tj}WnVtk rxztj|Wn"tk rdttjdYnX|df}tj }YnXt |tj \}}g}g}t |d|tj tjtj}|dd}tj||} |D]0} | dd} tj|| } | | kr|| q|||fS)NPrrYrDrs)rpZipv6 inet_atonrrr0Zipv4r_rarrrzrrrgrurhr& inet_ptonr5) ipr}rrar6rrrorZbin_iprrpZbin_addrrrrrs<       r)rr&cCs:|dkrt}|att_tt_tt_t t_ t t_ t t_dS)aOverride the system resolver routines in the socket module with versions which use dnspython's resolver. This can be useful in testing situations where you want to control the resolution behavior of python code without having to change the system's resolver settings (e.g. /etc/resolv.conf). The resolver to use may be specified; if it's not, the default resolver will be used. resolver, a ``dns.resolver.Resolver`` or ``None``, the resolver to use. N)rOrbrrr getaddrinfor getnameinforgetfqdnr gethostbynamergethostbyname_exr gethostbyaddr)rrrroverride_system_resolversrcCs,datt_tt_tt_tt_ t t_ t t_ dS)z5Undo the effects of prior override_system_resolver().N)rbr\rr_original_getnameinfor_original_getfqdnr_original_gethostbynamer_original_gethostbyname_exr_original_gethostbyaddrrrrrrrestore_system_resolversr)r )r)N)N)yr<rrrrrr_r<typingrrrrrrr r urllib.parser Zdns._ddrrpZdns.ednsZ dns.exceptionZ dns.flagsZdns.inetZdns.ipv4Zdns.ipv6Z dns.messageZdns.nameZdns.nameserverZ dns.queryZ dns.rcodeZdns.rdataclassZ dns.rdatatypeZdns.rdtypes.svcbbaseZdns.reversenameZdns.tsigrZ dns.win32utilrZ DNSExceptionr rAr+rrxr0rvrr-rFTimeoutrGrOrRrTrUrVrWrXr#ryrzrrrarqrrrsrtrurrrrrrr/rMr+rOrNrrrr7rPrFrrHr/rLrTrWrXrfSOL_UDPrgrurirbrr\rrrrrrrrrrrrrrrrrrrrrrrs~(  X   E1OoU!  # !    W ' / T '