Your IP : 3.21.244.240


Current Path : /usr/lib/python3.6/site-packages/dns/__pycache__/
Upload File :
Current File : //usr/lib/python3.6/site-packages/dns/__pycache__/resolver.cpython-36.pyc

3

>D�W���@s�dZddlZddlZddlZddlZyddlZWnek
rLddlZYnXddl	Z
ddlZ
ddlZ
ddl
Z
ddlZ
ddlZ
ddlZ
ddlZ
ddlZ
ddlZ
ddlZ
ddlZ
ddlmZmZejdkr�yddlZWnek
r�ddlZYnXGdd�de
jj�ZGdd	�d	e
jj�Ze
jj Z Gd
d�de
jj�Z!Gdd
�d
e
jj�Z"Gdd�de
jj�Z#Gdd�de
jj�Z$Gdd�de
jj�Z%Gdd�de&�Z'Gdd�de&�Z(Gdd�de&�Z)Gdd�de&�Z*Gdd�de&�Z+da,dd�Z-d d!�Z.e
j/j0e
j1j2d"dd#dfd$d%�Z3e
j1j2d"dfd&d'�Z4ej5ej6gej7ej8giZ9da:ej;Z<ej=Z>ej?Z@ejAZBejCZDejEZFddejGdddfd(d)�ZHd8d*d+�ZId9d,d-�ZJd.d/�ZKd0d1�ZLd2d3�ZMd:d4d5�ZNd6d7�ZOdS);z{DNS stub resolver.

@var default_resolver: The default resolver object
@type default_resolver: dns.resolver.Resolver object�N�)�xrange�string_types�win32csVeZdZdZeddg�ZdZddd�Z�fdd�Zd	d
�Z	e
e	dd�Z	d
d�Z�ZS)�NXDOMAINz"The DNS query name does not exist.�qnames�	responsesNcCs\t|tttf�std��t|�dkr,td��|dkr:i}nt|t�sLtd��t||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)�
isinstance�list�tuple�set�AttributeError�len�dict)�selfrr�kwargs�r�/usr/lib/python3.6/resolver.py�
_check_kwargs9s
zNXDOMAIN._check_kwargscs^d|jkrtt|�j�S|jd}t|�dkr4d}n|jdd�}djtt|��}d||fS)NrrzNone of DNS query names existz, z%s: %s���)	r�superr�__str__r�__doc__�join�map�str)rr�msg)�	__class__rrrEs

zNXDOMAIN.__str__cCs�d|jkrtd��tjj}tjj}d}xj|jdD]\}|jd|}x4|jD]*}|j|ksL|j	|krfqL|j
djj�}qLW|dk	r2tj
j|�Sq2W|jddS)Nrzparametrized exception requiredrr)r�	TypeError�dns�
rdataclass�IN�	rdatatype�CNAME�answer�rdtype�rdclass�items�target�to_text�name�	from_text)rr!r#�cname�qname�responser$rrr�canonical_namePs
zNXDOMAIN.canonical_namez%Return the unresolved canonical name.)�doccCs|t|jjdg��}t|jjdi��}|jjdi�}x<|jjdg�D]*}||krX|j|�||krB||||<qBWt||d�S)z3Augment by results from another NXDOMAIN exception.rr)rr)r
r�getr�appendr)rZe_nxZqnames0Z
responses0Z
responses1Zqname1rrr�__add__bs
zNXDOMAIN.__add__)N)
�__name__�
__module__�__qualname__rr�supp_kwargs�fmtrrr/�propertyr3�
__classcell__rr)rrr3s
rc@seZdZdZdS)�YXDOMAINz8The DNS query name is too long after DNAME substitution.N)r4r5r6rrrrrr;osr;cs.eZdZdZdZedg�Z�fdd�Z�ZS)�NoAnswerz<The DNS response does not contain an answer to the question.z,The DNS response does not contain an answer zto the question: {query}r.cstt|�j|djd�S)Nr.)�query)rr<�_fmt_kwargs�question)rr)rrrr>�s
zNoAnswer._fmt_kwargszDThe DNS response does not contain an answer to the question: {query})	r4r5r6rr8rr7r>r:rr)rrr<zs

r<cs@eZdZdZdZdedd
�Zeddg�Z�fdd	�Z�Z	S)�
NoNameserversz�All 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}Nr�request�errorscsdg}x>|dD]2}|jd|d|dr*dnd|d|df�qWtt|�j|d	jd
j|�d�S)NrBz Server %s %s port %s answered %srrZTCPZUDP��rAz; )r=rB)r2rr@r>r?r)rrZsrv_msgs�err)rrrr>�s&
zNoNameservers._fmt_kwargsr)
r4r5r6rrr8rr7r>r:rr)rrr@�s
r@c@seZdZdZdS)�NotAbsolutezEAn absolute domain name is required but a relative name was provided.N)r4r5r6rrrrrrF�srFc@seZdZdZdS)�	NoRootSOAzBThere is no SOA RR at the DNS root name. This should never happen!N)r4r5r6rrrrrrG�srGc@seZdZdZdS)�
NoMetaqueriesz DNS metaqueries are not allowed.N)r4r5r6rrrrrrH�srHc@sBeZdZdZddd�Zdd�Zdd�Zd	d
�Zdd�Zd
d�Z	dS)�Answera9DNS 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.  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
    node's name might not be the query name.

    @ivar qname: The query name
    @type qname: dns.name.Name object
    @ivar rdtype: The query type
    @type rdtype: int
    @ivar rdclass: The query class
    @type rdclass: int
    @ivar response: The response message
    @type response: dns.message.Message object
    @ivar rrset: The answer
    @type rrset: dns.rrset.RRset object
    @ivar expiration: The time when the answer expires
    @type expiration: float (seconds since the epoch)
    @ivar canonical_name: The canonical name of the query name
    @type canonical_name: dns.name.Name object
    TcCs�||_||_||_||_d}d}x�tdd�D]�}y0|j|j|||�}|dksV|j|kr\|j}PWq,tk
�r|t	j
jkr�yJ|j|j||t	j
j�}	|dks�|	j|kr�|	j}x|	D]}
|
j}Pq�Ww,Wn"tk
r�|r�t
|d��YnX|�r�t
|d��Yq,Xq,W|dk�r |�r t
|d��||_||_|dk�r�x�yR|j|j||t	j
j�}|dk�sf|j|k�rl|j}|dj|k�r�|dj}PWn@tk
�r�y|j�}Wnt	jjk
�r�PYnXYnX�q8Wtj�||_dS)	Nrr�)r.rrrr)r-r%r&r.rZ
find_rrsetr$�ttl�KeyErrorrr"r#r(r<r/�rrsetZ	authority�SOAZminimum�parentr*�NoParent�time�
expiration)rr-r%r&r.�raise_on_no_answerZmin_ttlrM�countZcrrsetZrdZsrrsetrrr�__init__�sf






zAnswer.__init__cCs\|dkr|jjS|dkr |jjS|dkr0|jjS|dkr@|jjS|dkrP|jjSt|��dS)Nr*rK�coversr&r%)rMr*rKrVr&r%r
)r�attrrrr�__getattr__szAnswer.__getattr__cCs|jrt|j�pdS)Nr)rMr)rrrr�__len__szAnswer.__len__cCs|jrt|j�ptt��S)N)rM�iterr)rrrr�__iter__szAnswer.__iter__cCs
|j|S)N)rM)r�irrr�__getitem__szAnswer.__getitem__cCs|j|=dS)N)rM)rr\rrr�__delitem__szAnswer.__delitem__N)T)
r4r5r6rrUrXrYr[r]r^rrrrrI�s
7rIc@s<eZdZdZddd�Zdd�Zdd�Zd	d
�Zddd
�ZdS)�CacheaqSimple DNS answer cache.

    @ivar data: A dictionary of cached data
    @type data: dict
    @ivar cleaning_interval: The number of seconds between cleanings.  The
    default is 300 (5 minutes).
    @type cleaning_interval: float
    @ivar next_cleaning: The time the cache should next be cleaned (in seconds
    since the epoch.)
    @type next_cleaning: float
    ��r@cCs*i|_||_tj�|j|_tj�|_dS)z�Initialize a DNS cache.

        @param cleaning_interval: the number of seconds between periodic
        cleanings.  The default is 300.0
        @type cleaning_interval: float.
        N)�data�cleaning_intervalrQ�
next_cleaning�
_threading�Lock�lock)rrbrrrrU-szCache.__init__cCsptj�}|j|krlg}x*|jj�D]\}}|j|kr"|j|�q"Wx|D]}|j|=qHWtj�}||j|_dS)z&Clean the cache if it's time to do so.N)rQrcrar'rRr2rb)r�nowZkeys_to_delete�k�vrrr�_maybe_clean:s


zCache._maybe_cleanc
CsNz<|jj�|j�|jj|�}|dks6|jtj�kr:dS|S|jj�XdS)a)Get the answer associated with I{key}.  Returns None if
        no answer is cached for the key.
        @param key: the key
        @type key: (dns.name.Name, int, int) tuple whose values are the
        query name, rdtype, and rdclass.
        @rtype: dns.resolver.Answer object or None
        N)rf�acquirerjrar1rRrQ�release)r�keyrirrrr1Hs	
z	Cache.getc
Cs2z |jj�|j�||j|<Wd|jj�XdS)aAssociate key and value in the cache.
        @param key: the key
        @type key: (dns.name.Name, int, int) tuple whose values are the
        query name, rdtype, and rdclass.
        @param value: The answer being cached
        @type value: dns.resolver.Answer object
        N)rfrkrjrarl)rrm�valuerrr�put[s
	
z	Cache.putNc
CsRz@|jj�|dk	r(||jkr>|j|=ni|_tj�|j|_Wd|jj�XdS)z�Flush the cache.

        If I{key} is specified, only that item is flushed.  Otherwise
        the entire cache is flushed.

        @param key: the key to flush
        @type key: (dns.name.Name, int, int) tuple or None
        N)rfrkrarQrbrcrl)rrmrrr�flushks



zCache.flush)r`)N)	r4r5r6rrUrjr1rorprrrrr_s

r_c@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)�LRUCacheNodezLRUCache node.
    cCs||_||_||_||_dS)N)rmrn�prev�next)rrmrnrrrrU�szLRUCacheNode.__init__cCs |j|_||_||j_||_dS)N)rrrs)r�noderrr�link_before�szLRUCacheNode.link_beforecCs ||_|j|_||j_||_dS)N)rrrs)rrtrrr�
link_after�szLRUCacheNode.link_aftercCs|j|j_|j|j_dS)N)rrrs)rrrr�unlink�s
zLRUCacheNode.unlinkN)r4r5r6rrUrurvrwrrrrrq�s
rqc@s<eZdZdZddd�Zdd�Zdd�Zd	d
�Zddd
�ZdS)�LRUCacheaOBounded 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.

    @ivar data: A dictionary of cached data
    @type data: dict
    @ivar sentinel: sentinel node for circular doubly linked list of nodes
    @type sentinel: LRUCacheNode object
    @ivar max_size: The maximum number of nodes
    @type max_size: int
    順cCs*i|_|j|�tdd�|_tj�|_dS)z�Initialize a DNS cache.

        @param max_size: The maximum number of nodes to cache; the default is
        100,000. Must be greater than 1.
        @type max_size: int
        N)ra�set_max_sizerq�sentinelrdrerf)r�max_sizerrrrU�s
zLRUCache.__init__cCs|dkrd}||_dS)Nr)r|)rr|rrrrz�szLRUCache.set_max_sizec
CslzZ|jj�|jj|�}|dkr$dS|j�|jjtj�krJ|j|j=dS|j	|j
�|jS|jj�XdS)a)Get the answer associated with I{key}.  Returns None if
        no answer is cached for the key.
        @param key: the key
        @type key: (dns.name.Name, int, int) tuple whose values are the
        query name, rdtype, and rdclass.
        @rtype: dns.resolver.Answer object or None
        N)rfrkrar1rwrnrRrQrmrvr{rl)rrmrtrrrr1�s

zLRUCache.getc
Cs�z�|jj�|jj|�}|dk	r2|j�|j|j=x.t|j�|jkr`|jj	}|j�|j|j=q4Wt
||�}|j|j�||j|<Wd|jj�XdS)aAssociate key and value in the cache.
        @param key: the key
        @type key: (dns.name.Name, int, int) tuple whose values are the
        query name, rdtype, and rdclass.
        @param value: The answer being cached
        @type value: dns.resolver.Answer object
        N)
rfrkrar1rwrmrr|r{rrrqrvrl)rrmrnrtrrrro�s


zLRUCache.putNc
Cs�zr|jj�|dk	r<|jj|�}|dk	rp|j�|j|j=n4|jj}x$||jkrh|j}d|_d|_|}qFWi|_Wd|jj	�XdS)z�Flush the cache.

        If I{key} is specified, only that item is flushed.  Otherwise
        the entire cache is flushed.

        @param key: the key to flush
        @type key: (dns.name.Name, int, int) tuple or None
        N)
rfrkrar1rwrmr{rsrrrl)rrmrtrsrrrrp�s	

zLRUCache.flush)ry)N)	r4r5r6rrUrzr1rorprrrrrx�s
rxc@s�eZdZdZd%dd�Zdd�Zdd	�Zd
d�Zdd
�Zdd�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zejjejjddddfdd�Zdejjfdd �Zd!d"�Zd#d$�ZdS)&�Resolvera-DNS stub resolver

    @ivar domain: The domain of this host
    @type domain: dns.name.Name object
    @ivar nameservers: A list of nameservers to query.  Each nameserver is
    a string which contains the IP address of a nameserver.
    @type nameservers: list of strings
    @ivar search: The search list.  If the query name is a relative name,
    the resolver will construct an absolute query name by appending the search
    names one by one to the query name.
    @type search: list of dns.name.Name objects
    @ivar port: The port to which to send queries.  The default is 53.
    @type port: int
    @ivar timeout: The number of seconds to wait for a response from a
    server, before timing out.
    @type timeout: float
    @ivar lifetime: The total number of seconds to spend trying to get an
    answer to the question.  If the lifetime expires, a Timeout exception
    will occur.
    @type lifetime: float
    @ivar keyring: The TSIG keyring to use.  The default is None.
    @type keyring: dict
    @ivar keyname: The TSIG keyname to use.  The default is None.
    @type keyname: dns.name.Name object
    @ivar keyalgorithm: The TSIG key algorithm to use.  The default is
    dns.tsig.default_algorithm.
    @type keyalgorithm: string
    @ivar edns: The EDNS level to use.  The default is -1, no Edns.
    @type edns: int
    @ivar ednsflags: The EDNS flags
    @type ednsflags: int
    @ivar payload: The EDNS payload size.  The default is 0.
    @type payload: int
    @ivar flags: The message flags to use.  The default is None (i.e. not
    overwritten)
    @type flags: int
    @ivar cache: The cache to use.  The default is None.
    @type cache: dns.resolver.Cache object
    @ivar retry_servfail: should we retry a nameserver if it says SERVFAIL?
    The default is 'false'.
    @type retry_servfail: bool
    �/etc/resolv.confTcCs�d|_d|_d|_d|_d|_d|_d|_d|_d|_d|_	d|_
d|_d|_d|_
d|_d|_d|_|j�|r�tjdkr�|j�n|r�|j|�dS)aJInitialize a resolver instance.

        @param filename: The filename of a configuration file in
        standard /etc/resolv.conf format.  This parameter is meaningful
        only when I{configure} is true and the platform is POSIX.
        @type filename: string or file object
        @param configure: If True (the default), the resolver instance
        is configured in the normal fashion for the operating system
        the resolver is running on.  (I.e. a /etc/resolv.conf file on
        POSIX systems and from the registry on Windows systems.)
        @type configure: boolNFr)�domain�nameservers�nameserver_ports�port�search�timeout�lifetime�keyring�keyname�keyalgorithm�edns�	ednsflags�payload�cache�flags�retry_servfail�rotate�reset�sys�platform�
read_registry�read_resolv_conf)r�filenameZ	configurerrrrU9s.


zResolver.__init__cCs�tjjtjjtj��dd��|_t|j�dkr:tjj|_g|_	i|_
d|_g|_d|_
d|_d|_d|_tjj|_d|_d|_d|_d|_d|_d|_d|_dS)	z1Reset all resolver configuration to the defaults.rNr�5g@g>@Fr)rr*�Namer+�socket�gethostnamerr�rootr�r�r�r�r�r�r�r��tsig�default_algorithmr�r�r�r�r�r�r�r�)rrrrr�_s&"

zResolver.resetcCsZt|t�r<yt|d�}Wntk
r4dg|_dSXd}nd}z�x�|D]�}t|�dksH|ddksH|ddkrrqH|j�}t|�d	kr�qH|dd
kr�|jj|d�qH|ddkr�tj	j
|d�|_qH|dd
k�rxL|dd�D]}|jjtj	j
|��q�WqH|ddkrHd|dd�krHd|_
qHWWd|�r8|j�Xt|j�dk�rV|jjd�dS)z�Process f as a file in the /etc/resolv.conf format.  If f is
        a string, it is used as the name of the file to open; otherwise it
        is treated as the file itself.�rz	127.0.0.1NTFr�#�;rC�
nameserverrrr�Zoptionsr�)r	r�open�IOErrorr�r�splitr2rr*r+rr�r��close)r�fZ
want_close�l�tokens�suffixrrrr�vs:

$
zResolver.read_resolv_confcCs0|jd�dkrd}n|jd�dkr(d}nd}|S)N� r�,)�find)r�entry�
split_charrrr�_determine_split_char�szResolver._determine_split_charcCsDt|�}|j|�}|j|�}x"|D]}||jkr"|jj|�q"WdS)z&Configure a NameServer registry entry.N)rr�r�r�r2)rr�r�Zns_list�nsrrr�_config_win32_nameservers�s



z"Resolver._config_win32_nameserverscCstjjt|��|_dS)z"Configure a Domain registry entry.N)rr*r+rr)rrrrr�_config_win32_domain�szResolver._config_win32_domaincCsLt|�}|j|�}|j|�}x*|D]"}||jkr"|jjtjj|��q"WdS)z"Configure a Search registry entry.N)rr�r�r�r2rr*r+)rr�r�Zsearch_list�srrr�_config_win32_search�s



zResolver._config_win32_searchc#Cs.ytj|d�\}}Wntk
r,d}YnX|rv|j|�y"tj|d�\}}|r\|j|�Wq�tk
rrYq�Xntytj|d�\}}Wntk
r�d}YnX|r�|j|�y"tj|d�\}}|r�|j|�Wntk
r�YnXytj|d�\}}Wntk
�rd}YnX|�r*|j|�dS)z%Extract DNS info from a registry key.Z
NameServerNZDomainZDhcpNameServerZ
DhcpDomainZ
SearchList)�_winreg�QueryValueEx�WindowsErrorr�r�r�)rrmZserversZrtypeZdomr�rrr�_config_win32_fromkey�s<




zResolver._config_win32_fromkeyc,Cstjdtj�}d}z�ytj|d�}d}Wn tk
rHtj|d�}YnXz|j|�Wd|j�X|r�tj|d�}ztd}xjyNtj||�}|d7}tj||�}|j|||�s�w|z|j|�Wd|j�XWq|tk
r�PYq|Xq|WWd|j�XWd|j�XdS)	z9Extract resolver configuration from the Windows registry.NFz2SYSTEM\CurrentControlSet\Services\Tcpip\ParametersTz+SYSTEM\CurrentControlSet\Services\VxD\MSTCPz=SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfacesrr)	r�ZConnectRegistry�HKEY_LOCAL_MACHINE�OpenKey�EnvironmentErrorr��CloseZEnumKey�_win32_is_nic_enabled)r�lmZ	want_scanZ
tcp_paramsZ
interfacesr\�guidrmrrrr��s@
zResolver.read_registryc
Cs�y�tj|d|�}zftj|d�\}}|tjkr2t�tj|d|�}z(tj|d�\}}|tjkrbt�|d@S|j�XWd|j�XWnFttfk
r�ytj|d�\}	}|	dk	Stk
r�dSXYnXdS)Nz]SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\%s\ConnectionZ
PnpInstanceIDz SYSTEM\CurrentControlSet\Enum\%sZConfigFlagsrZNTEContextListF)	r�r�r�ZREG_SZ�
ValueErrorZ	REG_DWORDr�r�r�)
rr�r�Z
interface_keyZconnection_keyZpnp_idZttypeZ
device_keyr�Znterrrr�s6



zResolver._win32_is_nic_enabledcCsVtj�}||}|dkr0|dkr,t|d��n|}||jkrDt|d��t|j||j�S)Nrr)r�r)rQ�Timeoutr��minr�)r�startrgZdurationrrr�_compute_timeoutMs

zResolver._compute_timeoutFNrcCst|t�rtjj|d�}t|t�r.tjj|�}tjj|�r>t�t|t�rTtjj|�}tjj	|�rdt�g}|j
�r||j|�nZt|�dkr�|j|j
tjj��|jr�x2|jD]}	|j|j
|	��q�Wn|j|j
|j��d}
i}tj�}d}
�x�|D�]�}
|j�r@|jj|
||f�}|dk	�r@|jdk�r<|�r<t|jd��n|Stjj|
||�}|jdk	�rr|j|j|j|jd�|j|j|j|j �|j!dk	�r�|j!|_!d}|j"dd�}g}|j#�r�t$j%|�d}�x�|dk�r�t|�dk�r�t&||d���x`|dd�D�]N}|j'|�}|j(j||j)�}yx|}|�rDtj*j+||||||d	�}nNtj*j,||||||d	�}|j!tj!j-@�r�d}|j'|�}tj*j+||||||d	�}W�n t.j/tj0j1fk
�r�}z"|j|||||f�d}�w�WYdd}~Xn�tj*j2k
�r"}z"|j|||||f�d}�w�WYdd}~Xn�tj0j3k
�rn}z,|j4|�|j|||||f�d}�w�WYdd}~XnJt5k
�r�}z,|j4|�|j|||||f�d}�w�WYdd}~XnX|j6�}|tj6j7k�r�t7�}|j|||||f�|�|tj6j8k�s|tj6j9k�r
P|tj6j:k�s"|j;�r,|j4|�|j|||tj6j<|�|f�d}�q�W|dk	�r^Pt|�dk�r�|j'|�}t=||�}|d
9}tj>|��q�W|j6�tj6j9k�r�|||
<q�d}
Pq�W|
�r�t9||d��t?|
||||�}|j�r�|jj@|
||f|�|S)
aQuery nameservers to find the answer to the question.

        The I{qname}, I{rdtype}, and I{rdclass} parameters may be objects
        of the appropriate type, or strings that can be converted into objects
        of the appropriate type.  E.g. For I{rdtype} the integer 2 and the
        the string 'NS' both mean to query for records with DNS rdata type NS.

        @param qname: the query name
        @type qname: dns.name.Name object or string
        @param rdtype: the query type
        @type rdtype: int or string
        @param rdclass: the query class
        @type rdclass: int or string
        @param tcp: use TCP to make the query (default is False).
        @type tcp: bool
        @param source: bind to this IP address (defaults to machine default
        IP).
        @type source: IP address in dotted quad notation
        @param raise_on_no_answer: raise NoAnswer if there's no answer
        (defaults is True).
        @type raise_on_no_answer: bool
        @param source_port: The port from which to send the message.
        The default is 0.
        @type source_port: int
        @rtype: dns.resolver.Answer instance
        @raises Timeout: no answers could be found in the specified lifetime
        @raises NXDOMAIN: the query name does not exist
        @raises YXDOMAIN: the query name is too long after DNAME substitution
        @raises NoAnswer: the response did not contain an answer and
        raise_on_no_answer is True.
        @raises NoNameservers: no non-broken nameservers are available to
        answer the question.NrT)r.)�	algorithmg�������?r)rArB)�source�source_portrCF)rr)Ar	rrr*r+r"Zis_metatyperHr Zis_metaclass�is_absoluter2rZconcatenater�r�rrQr�r1rMr<r.�messageZ
make_queryr��use_tsigr�r��use_ednsr�r�r�r�r�r��randomZshuffler@r�r�r�r=�tcp�udpZTCr��error�	exceptionr�ZUnexpectedSourceZ	FormError�remove�EOFError�rcoder;ZNOERRORrZSERVFAILr�r)r�ZsleeprIro)rr-r%r&r�r�rSr�Z
qnames_to_tryr�Zall_nxdomainZnxdomain_responsesr�Z_qnamer$rAr.r�rBZbackoffr�r�r�Ztcp_attemptZexr�Z
sleep_timerrrr=]s�#




















zResolver.querycCs4||_|dkr$t|jj��d|_n||_||_dS)a�Add a TSIG signature to the query.

        @param keyring: The TSIG keyring to use; defaults to None.
        @type keyring: dict
        @param keyname: The name of the TSIG key to use; defaults to None.
        The key must be defined in the keyring.  If a keyring is specified
        but a keyname is not, then 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 keyring is used, unless
        they know the keyring contains only one key.
        @param algorithm: The TSIG key algorithm to use.  The default
        is dns.tsig.default_algorithm.
        @type algorithm: stringNr)r�r
�keysr�r�)rr�r�r�rrrr�"s
zResolver.use_tsigcCs"|dkrd}||_||_||_dS)aConfigure Edns.

        @param edns: The EDNS level to use.  The default is -1, no Edns.
        @type edns: int
        @param ednsflags: The EDNS flags
        @type ednsflags: int
        @param payload: The EDNS payload size.  The default is 0.
        @type payload: intNrr)r�r�r�)rr�r�r�rrrr�8s

zResolver.use_ednscCs
||_dS)z�Overrides the default flags with your own

        @param flags: The flags to overwrite the default with
        @type flags: intN)r�)rr�rrr�	set_flagsHszResolver.set_flags)r~T)r4r5r6rrUr�r�r�r�r�r�r�r�r�r�rr"�Ar r!r=r�r�r�r�r�rrrrr}s&+
&)

"-7Er}cCstdkrt�tS)z7Get the default resolver, initializing it if necessary.N)�default_resolver�reset_default_resolverrrrr�get_default_resolverRsr�cCs
t�adS)zSRe-initialize default resolver.

    resolv.conf will be re-read immediatelly.
    N)r}r�rrrrr�Ysr�FTcCst�j|||||||�S)z�Query nameservers to find the answer to the question.

    This is a convenience function that uses the default resolver
    object to make the query.
    @see: L{dns.resolver.Resolver.query} for more information on the
    parameters.)r�r=)r-r%r&r�r�rSr�rrrr=bs	r=cCs�t|t�rtjj|tjj�}|dkr*t�}|j�s:t|��xxy(|j	|tj
j||�}|jj|krb|SWn tj
jtj
jfk
r�YnXy|j�}Wq<tjjk
r�t�Yq<Xq<WdS)a�Find the name of the zone which contains the specified name.

    @param name: the query name
    @type name: absolute dns.name.Name object or string
    @param rdclass: The query class
    @type rdclass: int
    @param tcp: use TCP to make the query (default is False).
    @type tcp: bool
    @param resolver: the resolver to use
    @type resolver: dns.resolver.Resolver object or None
    @rtype: dns.name.NameN)r	rrr*r+r�r�r�rFr=r"rNrM�resolverrr<rOrPrG)r*r&r�r�r$rrr�
zone_for_nameos"
r�c,Cs�|tjtjB@dkrt�|dkr4|dkr4tjtj��g}g}d}y�|dkr�d}|tj@dkrr|jd�|jd�q�|jd�|jd�n>|jd�}	t	|	�dkr�|	d}
n|}
t
jj|
�}|j|�|}W�npt
k
�r:y t
jj|�}|j|�|}W�n8t
k
�r4|tj@dk�r0y�|tjk�s8|tjk�r�tj|t
jjd	d
�}|j}|jjd�}|jdk	�r�x|jD]}
|j|
j��qrW|tjk�s�|tjk�r�tj|t
jjd	d
�}|j}|jjd�}|jdk	�r�x|jD]}
|j|
j��q�WWn<t
jjk
�rtjtj��Yntjtj��YnXYnXYnXd}y|dk�rRd}nt |�}WnLt
k
�r�|tj!@dk�r�ytj"|�}Wnt
k
�r�YnXYnX|dk�r�tjtj��g}|dk�r�tj#tj$g}n|g}|tj%@dk�r�|}nd}|tjk�s|tjk�rfxP|D]H}x@|D]8}x0t&|D]$}|jtj|||||ddff��q2W�q$W�qW|tjk�s~|tjk�r�xL|D]D}x<|D]4}x,t&|D] }|jtj|||||ff��q�W�q�W�q�Wt	|�dk�r�tjtj��|S)
NrZ	localhostz::z0.0.0.0z::1z	127.0.0.1�%rCF)rST�)'r�Z
AI_ADDRCONFIGZAI_V4MAPPED�NotImplementedError�gaierror�
EAI_NONAMEZ
AI_PASSIVEr2r�rr�ipv6�	inet_aton�	ExceptionZipv4ZAI_NUMERICHOST�AF_INET6�	AF_UNSPEC�	_resolverr=r"ZAAAAr-r/r)rMZaddress�AF_INETr�r�rZ
EAI_SYSTEM�intZAI_NUMERICSERVZ
getservbyname�
SOCK_DGRAM�SOCK_STREAM�AI_CANONNAME�_protocols_for_socktype)�host�service�familyZsocktype�protor�Zv6addrsZv4addrsr/�partsZahost�addrZv6ZrdataZv4r��tuplesZ	socktypesr,rrr�_getaddrinfo�s�












$

 r�c
Csf|d}|d}t|�dkr,|d}tj}n
d}tj}t|||tjtjd�}t|�dkrbtjd��|ddd}|tj@r�d}nd}t	j
j|�}	|tj@dk�ry"t
j|	d�}
|
jdjjd	�}WnVt	jjt	jjfk
�r|tj@r�tjtj��|}|dk	�r|d
t|�7}YnXn|}|dk	�r<|d
t|�7}|tj@�rRt|�}ntj||�}||fS)Nrr�rDz'sockaddr resolved to multiple addressesr�r�ZPTRTr�)rr�r�r�r�r��SOL_TCPr�ZNI_DGRAMrZreversenameZfrom_addressZNI_NUMERICHOSTr�r=rMr(r)r�rr<�NI_NAMEREQDr�r�rZNI_NUMERICSERVZ
getservbyport)
�sockaddrr�r�r�Zscoper�r�r�Zpnamer-r$Zhostnamer�rrr�_getnameinfosB






r�cCsD|dkrtj�}ytt|d�dd�dStk
r>|SXdS)N�Prr�)r�r�r�r�r�)r*rrr�_getfqdn.sr�cCst|�ddS)NrCr)�_gethostbyname_ex)r*rrr�_gethostbyname7sr�cCsXg}g}t|dtjtjtjtj�}|dd}x|D]}|j|dd�q4W|||fS)NrrDr�)r�r�r�r�r�r�r2)r*�aliases�	addressesr��	canonical�itemrrrr�;s
r�c
Cs�y"tjj|�|dddf}tj}Wn"tk
rD|df}tj}YnXt|tj�\}}g}g}t	|d|tj
tjtj�}|dd}x|D]}	|j
|	dd�q�W|||fS)Nr�rrDr�)rr�r�r�r�r�r�r�r�r�r�r�r�r2)
Zipr�r�r*r�r�r�r�r�rrrr�_gethostbyaddrGs 

rcCs:|dkrt�}|att_tt_tt_t	t_
tt_t
t_dS)a�Override 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.

    @param resolver: the resolver to use
    @type resolver: dns.resolver.Resolver object or None
    N)r�r�r�r��getaddrinfor��getnameinfor��getfqdnr��
gethostbynamer��gethostbyname_exr�
gethostbyaddr)r�rrr�override_system_resolver[srcCs,datt_tt_tt_tt_	t
t_tt_
dS)z4Undo the effects of override_system_resolver().
    N)r��_original_getaddrinfor�r�_original_getnameinfor�_original_getfqdnr�_original_gethostbynamer�_original_gethostbyname_exr�_original_gethostbyaddrrrrrr�restore_system_resolverusr)r)N)N)Prr�r�rQr�Z	threadingrd�ImportErrorZdummy_threadingZ
dns.exceptionrZ	dns.flagsZdns.ipv4Zdns.ipv6Zdns.messageZdns.nameZ	dns.queryZ	dns.rcodeZdns.rdataclassZ
dns.rdatatypeZdns.reversenameZdns.tsigZ_compatrrr��winregr�r�ZDNSExceptionrr;r�r<r@rFrGrH�objectrIr_rqrxr}r�r�r�r"r�r r!r=r�r�ZSOL_UDPr�r�r�r�rr	rr
rrrrrr
rrr�r�r�r�r�r�rrrrrrr�<module>s�
<sboG	&

c
(
	


?>