3
[_ð]"Ä  ã               @   s¸  d Z ddlZddlZddlZddlZyddlZW n ek
rL   ddlZY nX ddl	Z
ddlZ
ddlZ
ddlZ
ddlZ
ddlZ
ddlZ
ddlZ
ddlZ
ddlZ
ddlZ
ddlZ
ddlmZmZ ejdkròyddlZW n ek
rð   ddlZY nX G dd„ de
jjƒZG dd	„ d	e
jjƒZe
jj Z G d
d„ de
jjƒZ!G dd„ de
jjƒZ"G dd„ de
jjƒZ#G dd„ de
jjƒZ$G dd„ de
jjƒZ%G dd„ de&ƒZ'G dd„ de&ƒZ(G dd„ de&ƒZ)G dd„ de&ƒZ*G dd„ de&ƒZ+da,dd„ Z-d d!„ Z.e
j/j0e
j1j2d"dd#ddfd$d%„Z3e
j1j2d"dfd&d'„Z4ej5ej6gej7ej8giZ9da:ej;Z<ej=Z>ej?Z@ejAZBejCZDejEZFddejGdddfd(d)„ZHd8d*d+„ZId9d,d-„ZJd.d/„ ZKd0d1„ ZLd2d3„ ZMd:d4d5„ZNd6d7„ ZOdS );zDNS stub resolver.é    Né   )ÚxrangeÚstring_typesÚwin32c                   sj   e Zd ZdZddhZdZddd„Z‡ fdd„Zd	d
„ Ze	edd�Zdd„ Z
dd„ Zdd„ Zdd„ Z‡  ZS )ÚNXDOMAINz"The DNS query name does not exist.ÚqnamesÚ	responsesNc             C   s\   t |tttfƒstdƒ‚t|ƒdkr,tdƒ‚|d kr:i }nt |tƒsLtdƒ‚t||d�}|S )Nz#qnames must be a list, tuple or setr   z(qnames must contain at least one elementz(responses must be a dict(qname=response))r   r   )Ú
isinstanceÚlistÚtupleÚsetÚAttributeErrorÚlenÚdict)Úselfr   r   Úkwargs© r   ú1/tmp/pip-build-yz_zf6az/dnspython/dns/resolver.pyÚ_check_kwargs7   s    
zNXDOMAIN._check_kwargsc                sT   d| j krtt| ƒjƒ S | j d }t|ƒdkr4d}nd}djtt|ƒƒ}dj||ƒS )Nr   r   zNone of DNS query names existz!The DNS query name does not existz, z{}: {})	r   Úsuperr   Ú__str__r   ÚjoinÚmapÚstrÚformat)r   r   Úmsg)Ú	__class__r   r   r   C   s    

zNXDOMAIN.__str__c             C   s    d| j krtdƒ‚tjj}tjj}d }xj| j d D ]\}| j d | }x4|jD ]*}|j|ksL|j	|krfqL|j
d jjƒ }qLW |d k	r2tjj|ƒS q2W | j d d S )Nr   zparametrized exception requiredr   r   )r   Ú	TypeErrorÚdnsÚ
rdataclassÚINÚ	rdatatypeÚCNAMEÚanswerÚrdtypeÚrdclassÚitemsÚtargetÚto_textÚnameÚ	from_text)r   r    r"   ÚcnameÚqnameÚresponser#   r   r   r   Úcanonical_nameN   s    
zNXDOMAIN.canonical_namez%Return the unresolved canonical name.)Údocc             C   s|   t | jjdg ƒƒ}t| jjdi ƒƒ}|jjdi ƒ}x<|jjdg ƒD ]*}||krX|j|ƒ ||krB|| ||< qBW t||d�S )z3Augment by results from another NXDOMAIN exception.r   r   )r   r   )r
   r   Úgetr   Úappendr   )r   Ze_nxZqnames0Z
responses0Z
responses1Zqname1r   r   r   Ú__add__`   s    
zNXDOMAIN.__add__c             C   s
   | j d S )zXAll of the names that were tried.

        Returns a list of ``dns.name.Name``.
        r   )r   )r   r   r   r   r   l   s    zNXDOMAIN.qnamesc             C   s
   | j d S )z˜A map from queried names to their NXDOMAIN responses.

        Returns a dict mapping a ``dns.name.Name`` to a
        ``dns.message.Message``.
        r   )r   )r   r   r   r   r   s   s    zNXDOMAIN.responsesc             C   s   | j d | S )zTThe response for query *qname*.

        Returns a ``dns.message.Message``.
        r   )r   )r   r,   r   r   r   r-   {   s    zNXDOMAIN.response)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úsupp_kwargsÚfmtr   r   r.   Úpropertyr2   r   r   r-   Ú__classcell__r   r   )r   r   r   2   s   
r   c               @   s   e Zd ZdZdS )ÚYXDOMAINz8The DNS query name is too long after DNAME substitution.N)r3   r4   r5   r6   r   r   r   r   r;   ƒ   s   r;   c                   s*   e Zd ZdZdZdhZ‡ fdd„Z‡  ZS )ÚNoAnswerz<The DNS response does not contain an answer to the question.z,The DNS response does not contain an answer zto the question: {query}r-   c                s   t t| ƒj|d jd�S )Nr-   )Úquery)r   r<   Ú_fmt_kwargsÚquestion)r   r   )r   r   r   r>   “   s    
zNoAnswer._fmt_kwargszDThe DNS response does not contain an answer to the question: {query})r3   r4   r5   r6   r8   r7   r>   r:   r   r   )r   r   r<   �   s
    r<   c                   s<   e Zd ZdZdZdedd
…  ZddhZ‡ fdd	„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Úerrorsc                sd   g }x>|d D ]2}|j dj|d |d r,dnd|d |d ƒƒ qW tt| ƒj|d	 jd
j|ƒd�S )NrB   z Server {} {} port {} answered {}r   r   ZTCPZUDPé   é   rA   z; )r=   rB   )r1   r   r   r@   r>   r?   r   )r   r   Zsrv_msgsÚerr)r   r   r   r>   ¥   s    $
zNoNameservers._fmt_kwargséÿÿÿÿ)	r3   r4   r5   r6   r   r8   r7   r>   r:   r   r   )r   r   r@   ˜   s
   r@   c               @   s   e Zd ZdZdS )ÚNotAbsolutezEAn absolute domain name is required but a relative name was provided.N)r3   r4   r5   r6   r   r   r   r   rG   ®   s   rG   c               @   s   e Zd ZdZdS )Ú	NoRootSOAzBThere is no SOA RR at the DNS root name. This should never happen!N)r3   r4   r5   r6   r   r   r   r   rH   ²   s   rH   c               @   s   e Zd ZdZdS )ÚNoMetaqueriesz DNS metaqueries are not allowed.N)r3   r4   r5   r6   r   r   r   r   rI   ¶   s   rI   c               @   sB   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dS )Ú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.
    Tc             C   sä  || _ || _|| _|| _d}d }xätddƒD ]Ö}y0|j|j|||ƒ}|dksV|j|k r\|j}P W q, tk
�r    |t	j
jkrìyJ|j|j||t	j
jƒ}	|dks¨|	j|k r®|	j}x|	D ]}
|
j}P q´W w,W n" tk
rê   |ræt|d�‚Y nX |� rüt|d�‚Y q,X q,W |d k�r |�r t|d�‚|| _|| _|d k�rÒxšyR|j|j||t	j
jƒ}|dk�sf|j|k �rl|j}|d j|k �r†|d j}P W n@ tk
�rÊ   y|jƒ }W n t	jjk
�rÄ   P Y nX Y nX �q8W tjƒ | | _d S )	Nr   r   é   )r-   rF   rF   rF   rF   )r,   r$   r%   r-   r   Z
find_rrsetr#   ÚttlÚKeyErrorr   r!   r"   r'   r<   r.   ÚrrsetÚ	authorityÚSOAZminimumÚparentr)   ÚNoParentÚtimeÚ
expiration)r   r,   r$   r%   r-   Úraise_on_no_answerZmin_ttlrN   ÚcountZcrrsetÚrdZsrrsetr   r   r   Ú__init__Ê   sf    






zAnswer.__init__c             C   s\   |dkr| j jS |dkr | j jS |dkr0| j jS |dkr@| j jS |dkrP| j jS t|ƒ‚d S )Nr)   rL   Úcoversr%   r$   )rN   r)   rL   rY   r%   r$   r   )r   Úattrr   r   r   Ú__getattr__  s    zAnswer.__getattr__c             C   s   | j rt| j ƒpdS )Nr   )rN   r   )r   r   r   r   Ú__len__  s    zAnswer.__len__c             C   s   | j rt| j ƒpttƒ ƒS )N)rN   Úiterr   )r   r   r   r   Ú__iter__  s    zAnswer.__iter__c             C   s   | j d krt‚| j | S )N)rN   Ú
IndexError)r   Úir   r   r   Ú__getitem__  s    
zAnswer.__getitem__c             C   s   | j d krt‚| j |= d S )N)rN   r_   )r   r`   r   r   r   Ú__delitem__  s    
zAnswer.__delitem__N)T)
r3   r4   r5   r6   rX   r[   r\   r^   ra   rb   r   r   r   r   rJ   º   s   
7rJ   c               @   s<   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zddd„ZdS )ÚCachez$Simple thread-safe DNS answer cache.ç     Àr@c             C   s*   i | _ || _tjƒ | j | _tjƒ | _dS )zf*cleaning_interval*, a ``float`` is the number of seconds between
        periodic cleanings.
        N)ÚdataÚcleaning_intervalrS   Únext_cleaningÚ
_threadingÚLockÚlock)r   rf   r   r   r   rX   $  s    zCache.__init__c             C   sp   t j ƒ }| j|krlg }x*| jjƒ D ]\}}|j|kr"|j|ƒ q"W x|D ]}| j|= qHW t j ƒ }|| j | _dS )z&Clean the cache if it's time to do so.N)rS   rg   re   r&   rT   r1   rf   )r   ÚnowZkeys_to_deleteÚkÚvr   r   r   Ú_maybe_clean.  s    


zCache._maybe_cleanc          
   C   sN   z<| j jƒ  | jƒ  | jj|ƒ}|dks6|jtjƒ kr:dS |S | j jƒ  X dS )a!  Get the answer associated with *key*.

        Returns None if no answer is cached for the key.

        *key*, a ``(dns.name.Name, int, int)`` tuple whose values are the
        query name, rdtype, and rdclass respectively.

        Returns a ``dns.resolver.Answer`` or ``None``.
        N)rj   Úacquirern   re   r0   rT   rS   Úrelease)r   Úkeyrm   r   r   r   r0   <  s    
z	Cache.getc          
   C   s2   z | j jƒ  | jƒ  || j|< W d| j jƒ  X dS )zèAssociate key and value in the cache.

        *key*, a ``(dns.name.Name, int, int)`` tuple whose values are the
        query name, rdtype, and rdclass respectively.

        *value*, a ``dns.resolver.Answer``, the answer.
        N)rj   ro   rn   re   rp   )r   rq   Úvaluer   r   r   ÚputQ  s
    	
z	Cache.putNc          
   C   sR   z@| j jƒ  |dk	r(|| jkr>| j|= ni | _tjƒ | j | _W d| j jƒ  X dS )a  Flush the cache.

        If *key* is not ``None``, only that item is flushed.  Otherwise
        the entire cache is flushed.

        *key*, a ``(dns.name.Name, int, int)`` tuple whose values are the
        query name, rdtype, and rdclass respectively.
        N)rj   ro   re   rS   rf   rg   rp   )r   rq   r   r   r   Úflusha  s    



zCache.flush)rd   )N)	r3   r4   r5   r6   rX   rn   r0   rs   rt   r   r   r   r   rc   !  s   

rc   c               @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚLRUCacheNodezLRUCache node.c             C   s   || _ || _| | _| | _d S )N)rq   rr   ÚprevÚnext)r   rq   rr   r   r   r   rX   z  s    zLRUCacheNode.__init__c             C   s    |j | _ || _| |j _| |_ d S )N)rv   rw   )r   Únoder   r   r   Úlink_before€  s    zLRUCacheNode.link_beforec             C   s    || _ |j| _| |j_ | |_d S )N)rv   rw   )r   rx   r   r   r   Ú
link_after†  s    zLRUCacheNode.link_afterc             C   s   | j | j_ | j| j _d S )N)rv   rw   )r   r   r   r   ÚunlinkŒ  s    
zLRUCacheNode.unlinkN)r3   r4   r5   r6   rX   ry   rz   r{   r   r   r   r   ru   w  s
   ru   c               @   s<   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zddd„ZdS )Ú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.
    é † c             C   s*   i | _ | j|ƒ tddƒ| _tjƒ | _dS )zl*max_size*, an ``int``, is the maximum number of nodes to cache;
        it must be greater than 0.
        N)re   Úset_max_sizeru   Úsentinelrh   ri   rj   )r   Úmax_sizer   r   r   rX   ›  s    
zLRUCache.__init__c             C   s   |dk rd}|| _ d S )Nr   )r€   )r   r€   r   r   r   r~   ¥  s    zLRUCache.set_max_sizec          
   C   sl   zZ| j jƒ  | jj|ƒ}|dkr$dS |jƒ  |jjtjƒ krJ| j|j= dS |j	| j
ƒ |jS | j jƒ  X dS )a!  Get the answer associated with *key*.

        Returns None if no answer is cached for the key.

        *key*, a ``(dns.name.Name, int, int)`` tuple whose values are the
        query name, rdtype, and rdclass respectively.

        Returns a ``dns.resolver.Answer`` or ``None``.
        N)rj   ro   re   r0   r{   rr   rT   rS   rq   rz   r   rp   )r   rq   rx   r   r   r   r0   ª  s    

zLRUCache.getc          
   C   s–   z„| j jƒ  | jj|ƒ}|dk	r2|jƒ  | j|j= x.t| jƒ| jkr`| jj	}|jƒ  | j|j= q4W t
||ƒ}|j| jƒ || j|< W d| j jƒ  X dS )zèAssociate key and value in the cache.

        *key*, a ``(dns.name.Name, int, int)`` tuple whose values are the
        query name, rdtype, and rdclass respectively.

        *value*, a ``dns.resolver.Answer``, the answer.
        N)rj   ro   re   r0   r{   rq   r   r€   r   rv   ru   rz   rp   )r   rq   rr   rx   r   r   r   rs   Å  s    	


zLRUCache.putNc          
   C   s„   zr| j jƒ  |dk	r<| jj|ƒ}|dk	rp|jƒ  | j|j= n4| jj}x$|| jkrh|j}d|_d|_|}qFW i | _W d| j j	ƒ  X dS )a  Flush the cache.

        If *key* is not ``None``, only that item is flushed.  Otherwise
        the entire cache is flushed.

        *key*, a ``(dns.name.Name, int, int)`` tuple whose values are the
        query name, rdtype, and rdclass respectively.
        N)
rj   ro   re   r0   r{   rq   r   rw   rv   rp   )r   rq   rx   rw   r   r   r   rt   Þ  s    


zLRUCache.flush)r}   )N)	r3   r4   r5   r6   rX   r~   r0   rs   rt   r   r   r   r   r|   ‘  s   

r|   c               @   s®   e Zd ZdZd%dd„Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zd&dd„Zejjejjdddddfdd„Zdejjfdd „Zd!d"„ Zd#d$„ ZdS )'ÚResolverzDNS stub resolver.ú/etc/resolv.confTc             C   s˜   d| _ d| _d| _d| _d| _d| _d| _d| _d| _d| _	d| _
d| _d| _d| _d| _d| _d| _| jƒ  |r”tjdkr†| jƒ  n|r”| j|ƒ dS )aü  *filename*, a ``text`` or file object, specifying a file
        in standard /etc/resolv.conf format.  This parameter is meaningful
        only when *configure* is true and the platform is POSIX.

        *configure*, a ``bool``.  If True (the default), the resolver
        instance is configured in the normal fashion for the operating
        system the resolver is running on.  (I.e. by reading a
        /etc/resolv.conf file on POSIX systems and from the registry
        on Windows systems.)
        NFr   )Ú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   ÚfilenameÚ	configurer   r   r   rX   þ  s.    

zResolver.__init__c             C   s¢   t jjt jjtjƒ ƒdd… ƒ| _t| jƒdkr:t jj| _g | _	i | _
d| _g | _d| _d| _d| _d| _t jj| _d| _d| _d| _d| _d| _d| _d| _dS )	z1Reset all resolver configuration to the defaults.r   Nr   é5   g       @g      >@FrF   )r   r)   ÚNamer*   ÚsocketÚgethostnamerƒ   r   Úrootr„   r…   r†   r‡   rˆ   r‰   rŠ   r‹   ÚtsigÚdefault_algorithmrŒ   r�   rŽ   r�   r�   r‘   r’   r“   )r   r   r   r   r”   #  s&    "

zResolver.resetc             C   sZ  t |tƒr<yt|dƒ}W n tk
r4   dg| _dS X d}nd}zèxâ|D ]Ú}t|ƒdksH|d dksH|d dkrrqH|jƒ }t|ƒd	k rˆqH|d d
kr¦| jj|d ƒ qH|d dkrÆtj	j
|d ƒ| _qH|d dk�r xL|dd… D ]}| jjtj	j
|ƒƒ qâW qH|d dkrHd|dd… krHd| _qHW W d|�r8|jƒ  X t| jƒdk�rV| jjdƒ dS )z²Process *f* as a file in the /etc/resolv.conf format.  If f is
        a ``text``, 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   Ú
nameserverr   rƒ   r‡   Úoptionsr“   )r	   r   ÚopenÚIOErrorr„   r   Úsplitr1   r   r)   r*   rƒ   r‡   r“   Úclose)r   ÚfZ
want_closeÚlÚtokensÚsuffixr   r   r   r˜   ;  s:    

$
zResolver.read_resolv_confc             C   s0   |j dƒdkrd}n|j dƒdkr(d}nd}|S )Nú r   ú,)Úfind)r   ÚentryÚ
split_charr   r   r   Ú_determine_split_chare  s    zResolver._determine_split_charc             C   sD   t |ƒ}| j|ƒ}|j|ƒ}x"|D ]}|| jkr"| jj|ƒ q"W d S )N)r   r´   r©   r„   r1   )r   r„   r³   Zns_listÚnsr   r   r   Ú_config_win32_nameserverst  s    



z"Resolver._config_win32_nameserversc             C   s   t jjt|ƒƒ| _d S )N)r   r)   r*   r   rƒ   )r   rƒ   r   r   r   Ú_config_win32_domain}  s    zResolver._config_win32_domainc             C   sL   t |ƒ}| j|ƒ}|j|ƒ}x*|D ]"}|| jkr"| jjtjj|ƒƒ q"W d S )N)r   r´   r©   r‡   r1   r   r)   r*   )r   r‡   r³   Zsearch_listÚsr   r   r   Ú_config_win32_search�  s    



zResolver._config_win32_searchc          #   C   s6  yt j|dƒ\}}W n tk
r,   d }Y nX |r<| j|ƒ |sD|r~y"t j|dƒ\}}|rd| j|ƒ W qò tk
rz   Y qòX ntyt j|dƒ\}}W n tk
rª   d }Y nX |rò| j|ƒ y"t j|dƒ\}}|rÚ| j|ƒ W n tk
rð   Y nX yt j|dƒ\}}W n tk
�r    d }Y nX |�r2| j|ƒ d S )NZ
NameServerÚDomainZDhcpNameServerZ
DhcpDomainZ
SearchList)Ú_winregÚQueryValueExÚWindowsErrorr¶   r·   r¹   )r   rq   Zalways_try_domainZserversZrtypeÚdomr‡   r   r   r   Ú_config_win32_fromkeyŠ  s>    




zResolver._config_win32_fromkeyc          ,   C   s
  t jdt jƒ}d}zèyt j|dƒ}d}W n  tk
rH   t j|dƒ}Y nX z| j|dƒ W d|jƒ  X |røt j|dƒ}zvd}xlyPt j||ƒ}|d7 }t j||ƒ}| j|||ƒs°w~z| j|dƒ W d|jƒ  X W q~ tk
rä   P Y q~X q~W W d|jƒ  X W d|jƒ  X dS )	z9Extract resolver configuration from the Windows registry.NFz2SYSTEM\CurrentControlSet\Services\Tcpip\ParametersTz+SYSTEM\CurrentControlSet\Services\VxD\MSTCPz=SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfacesr   r   )	r»   ZConnectRegistryÚHKEY_LOCAL_MACHINEÚOpenKeyÚEnvironmentErrorr¿   ÚCloseÚEnumKeyÚ_win32_is_nic_enabled)r   ÚlmZ	want_scanZ
tcp_paramsZ
interfacesr`   Úguidrq   r   r   r   r—   ¬  s@    
zResolver.read_registryc       
      C   sÒ   y†t j|d| ƒ}zft j|dƒ\}}|t jkr2t‚t j|d| ƒ}z(t j|dƒ\}}|t jkrbt‚|d@  S |jƒ  X W d |jƒ  X W nF ttfk
rÌ   yt j|dƒ\}	}|	d k	S  tk
rÆ   dS X Y nX d S )Nz]SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\%s\ConnectionZPnpInstanceIDz SYSTEM\CurrentControlSet\Enum\%sZConfigFlagsr   ZNTEContextListF)	r»   rÁ   r¼   ÚREG_SZÚ
ValueErrorZ	REG_DWORDrÃ   rÂ   r½   )
r   rÆ   rÇ   Zinterface_keyZconnection_keyZpnp_idZttypeZ
device_keyr‘   Znter   r   r   rÅ   Ú  s6    



zResolver._win32_is_nic_enabledNc             C   sd   |d kr| j n|}tjƒ }|| }|dk rB|dk r>t|d�‚n|}||krTt|d�‚t|| | jƒS )Nr   r   )rˆ   rF   )r‰   rS   ÚTimeoutÚminrˆ   )r   Ústartr‰   rk   Údurationr   r   r   Ú_compute_timeout  s    
zResolver._compute_timeoutFr   c	             C   s  t |tƒrtjj|dƒ}t |tƒr.tjj|ƒ}tjj|ƒr>t‚t |tƒrTtjj|ƒ}tjj	|ƒrdt‚g }	|j
ƒ r||	j|ƒ nZt|ƒdkrœ|	j|jtjjƒƒ | jrÄx2| jD ]}
|	j|j|
ƒƒ qªW n|	j|j| jƒƒ d}i }tjƒ }d}�xÖ|	D �]Ì}| j�r@| jj|||fƒ}|dk	�r@|jdk�r<|�r<t|jd�‚n|S tjj|||ƒ}| jdk	�rr|j| j| j| jd� |j| j| j| j ƒ | j!dk	�rš| j!|_!d}| j"dd… }g }| j#�rÂt$j%|ƒ d}�xÔ|dk�rœt|ƒdk�rît&||d�‚�xd|dd… D �]R}| j'||ƒ}| j(j|| j)ƒ}yz|}|�rFtj*j+||||||d	�}nPtj*j,||||||d	�}|j!tj!j-@ �r–d}| j'||ƒ}tj*j+||||||d	�}W �n  t.j/tj0j1fk
�rä } z"|j|||||fƒ d}�wþW Y dd}~X nØ tj*j2k
�r& } z"|j|||||fƒ d}�wþW Y dd}~X n– tj0j3k
�rr } z,|j4|ƒ |j|||||fƒ d}�wþW Y dd}~X nJ t5k
�rº } z,|j4|ƒ |j|||||fƒ d}�wþW Y dd}~X nX |j6ƒ }|tj6j7k�rðt7ƒ }|j|||||fƒ |‚|tj6j8k�s|tj6j9k�rP |tj6j:k�s&| j; �r0|j4|ƒ |j|||tj6j<|ƒ|fƒ d}�qþW |dk	�rbP t|ƒdk�rÊ| j'||ƒ}t=||ƒ}|d
9 }tj>|ƒ �qÊW |j6ƒ tj6j9k�rº|||< qòd}P qòW |�rÖt9|	|d�‚t?|||||ƒ}| j�r| jj@|||f|ƒ |S )a  Query 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 ``text``, the query name.

        *rdtype*, an ``int`` or ``text``,  the query type.

        *rdclass*, an ``int`` or ``text``,  the query class.

        *tcp*, a ``bool``.  If ``True``, use TCP to make the query.

        *source*, a ``text`` 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 long query should run before timing out.

        Raises ``dns.exception.Timeout`` 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.
        Nr   T)r-   )Ú	algorithmgš™™™™™¹?r   )rA   rB   )ÚsourceÚsource_portrC   F)r   r   )Ar	   r   r   r)   r*   r!   Zis_metatyperI   r   Zis_metaclassÚis_absoluter1   r   ZconcatenaterŸ   r‡   rƒ   rS   r�   r0   rN   r<   r-   ÚmessageZ
make_queryr‹   Úuse_tsigrŠ   rŒ   Úuse_ednsr�   rŽ   r�   r‘   r„   r“   ÚrandomÚshuffler@   rÎ   r…   r†   r=   ÚtcpÚudpZTCr�   ÚerrorÚ	exceptionrÊ   ZUnexpectedSourceZ	FormErrorÚremoveÚEOFErrorÚrcoder;   ZNOERRORr   ZSERVFAILr’   r(   rË   ÚsleeprJ   rs   )r   r,   r$   r%   rØ   rÐ   rU   rÑ   r‰   Zqnames_to_tryr®   Zall_nxdomainZnxdomain_responsesrÌ   Ú_qnamer#   rA   r-   r„   rB   Úbackoffr¥   rˆ   r†   Ztcp_attemptÚexrÞ   Z
sleep_timer   r   r   r=   "  sô    -

















zResolver.queryc             C   s4   || _ |dkr$t| j jƒ ƒd | _n|| _|| _dS )a   Add a TSIG signature to the query.

        See the documentation of the Message class for a complete
        description of the keyring dictionary.

        *keyring*, a ``dict``, the TSIG keyring to use.  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.

        *keyname*, a ``dns.name.Name`` or ``None``, the name of the TSIG key
        to use; defaults to ``None``. The key must be defined in the keyring.

        *algorithm*, a ``dns.name.Name``, the TSIG algorithm to use.
        Nr   )rŠ   r
   Úkeysr‹   rŒ   )r   rŠ   r‹   rÏ   r   r   r   rÔ   ñ  s
    zResolver.use_tsigc             C   s"   |dkrd}|| _ || _|| _dS )a+  Configure 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.
        Nr   rF   )r�   rŽ   r�   )r   r�   rŽ   r�   r   r   r   rÕ     s
    zResolver.use_ednsc             C   s
   || _ dS )zkOverrides the default flags with your own.

        *flags*, an ``int``, the message flags to use.
        N)r‘   )r   r‘   r   r   r   Ú	set_flags!  s    zResolver.set_flags)r‚   T)N)r3   r4   r5   r6   rX   r”   r˜   r´   r¶   r·   r¹   r¿   r—   rÅ   rÎ   r   r!   ÚAr   r    r=   r    r¡   rÔ   rÕ   rä   r   r   r   r   r�   û  s(   
%*		".7
 Nr�   c               C   s   t dkrtƒ  t S )z7Get the default resolver, initializing it if necessary.N)Údefault_resolverÚreset_default_resolverr   r   r   r   Úget_default_resolver.  s    rè   c               C   s
   t ƒ adS )z—Re-initialize default resolver.

    Note that the resolver configuration (i.e. /etc/resolv.conf on UNIX
    systems) will be re-read immediately.
    N)r�   ræ   r   r   r   r   rç   5  s    rç   FTc          	   C   s   t ƒ 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 ``dns.resolver.Resolver.query`` for more information on the
    parameters.
    )rè   r=   )r,   r$   r%   rØ   rÐ   rU   rÑ   r‰   r   r   r   r=   @  s    r=   c             C   s¸   t | tƒrtjj| tjjƒ} |dkr*tƒ }| jƒ s:t| ƒ‚xxy(|j	| tj
j||ƒ}|jj| krb| S W n  tjjtjjfk
r„   Y nX y| jƒ } W q< tjjk
r®   t‚Y q<X q<W dS )ac  Find the name of the zone which contains the specified name.

    *name*, an absolute ``dns.name.Name`` or ``text``, 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 resolver is used.

    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.)

    Returns a ``dns.name.Name``.
    N)r	   r   r   r)   r*   rŸ   rè   rÒ   rG   r=   r!   rP   rN   Úresolverr   r<   rQ   rR   rH   )r)   r%   rØ   ré   r#   r   r   r   Úzone_for_nameQ  s"    
rê   c          .   C   sô  |t jt jB @ dkrt‚| d kr4|d kr4t jt jƒ‚g }g }d }yŠ| d krˆd}|t j@ dkrr|jdƒ |jdƒ qÆ|jdƒ |jdƒ n>| jdƒ}	t	|	ƒdkr¨|	d }
n| }
t
jj|
ƒ}|j| ƒ | }W �nz tk
�rD   y t
jj| ƒ}|j| ƒ | }W �nB tk
�r>   |t j@ dk�r:yÔ|t jk�s8|t jk�rˆtj| t
jjd	d
�}|j} |jjdƒ}|jd k	�rˆx|jD ]}|j|jƒ �qrW |t jk�s |t jk�rðtj| t
jjd	d
�}|j} |jjdƒ}|jd k	�rðx|jD ]}|j|jƒ �qÚW W nF t
jjk
�r   t jt jƒ‚Y n" tk
�r8   t jt jƒ‚Y nX Y nX Y nX d }y|d k�r\d}nt |ƒ}W nL tk
�r²   |t j!@ dk�r®yt j"|ƒ}W n tk
�r¬   Y nX Y nX |d k�rÊt jt jƒ‚g }|dk�ræt j#t j$g}n|g}|t j%@ dk�r|}nd}|t jk�s|t jk�rpxP|D ]H}x@|D ]8}x0t&| D ]$}|jt j|||||ddffƒ �q<W �q.W �q$W |t jk�sˆ|t jk�rÖxL|D ]D}x<|D ]4}x,t&| D ] }|jt j|||||ffƒ �q¦W �q˜W �qŽW t	|ƒdk�rðt jt jƒ‚|S )Nr   Ú	localhostz::z0.0.0.0z::1z	127.0.0.1ú%rC   F)rU   TÚ )'r�   ÚAI_ADDRCONFIGÚAI_V4MAPPEDÚNotImplementedErrorÚgaierrorÚ
EAI_NONAMEÚ
AI_PASSIVEr1   r©   r   r   Úipv6Ú	inet_atonÚ	ExceptionZipv4ÚAI_NUMERICHOSTÚAF_INET6Ú	AF_UNSPECÚ	_resolverr=   r!   ZAAAAr,   r.   r(   rN   ÚaddressÚAF_INETrå   ré   r   Ú
EAI_SYSTEMÚintÚAI_NUMERICSERVÚgetservbynameÚ
SOCK_DGRAMÚSOCK_STREAMÚAI_CANONNAMEÚ_protocols_for_socktype)ÚhostÚserviceÚfamilyÚsocktypeÚprotor‘   Zv6addrsZv4addrsr.   ÚpartsZahostÚaddrZv6ZrdataZv4r†   ÚtuplesZ	socktypesr+   r   r   r   Ú_getaddrinfoŠ  sª    












$

 r  c             C   sf  | d }| d }t | ƒdkr,| d }tj}n
d }tj}t|||tjtjdƒ}t |ƒdkrbtjdƒ‚|d d d }|tj@ r‚d}nd}t	j
j|ƒ}	|tj@ dk�ry"tj|	dƒ}
|
jd jjd	ƒ}W nV t	jjt	jjfk
�r   |tj@ røtjtjƒ‚|}|d k	�r|d
t|ƒ 7 }Y nX n|}|d k	�r<|d
t|ƒ 7 }|tj@ �rRt|ƒ}ntj||ƒ}||fS )Nr   r   é   rD   z'sockaddr resolved to multiple addressesrÙ   rØ   ZPTRTrì   )r   r�   rø   rü   r  r  ÚSOL_TCPrÚ   ÚNI_DGRAMr   ZreversenameÚfrom_addressÚNI_NUMERICHOSTrú   r=   rN   r'   r(   ré   r   r<   ÚNI_NAMEREQDrñ   rò   r   ÚNI_NUMERICSERVÚgetservbyport)Úsockaddrr‘   r  r†   Zscoper  r  r  Zpnamer,   r#   Úhostnamer  r   r   r   Ú_getnameinfoî  sB    






r  c             C   sD   | d krt jƒ } ytt| dƒd d ƒd S  tk
r>   | S X d S )NéP   r   r  )r�   rž   r  r  rö   )r)   r   r   r   Ú_getfqdn  s    r  c             C   s   t | ƒd d S )NrC   r   )Ú_gethostbyname_ex)r)   r   r   r   Ú_gethostbyname  s    r  c             C   sX   g }g }t | dtjtjtjtjƒ}|d d }x|D ]}|j|d d ƒ q4W |||fS )Nr   rD   r  )r  r�   rü   r  r  r  r1   )r)   ÚaliasesÚ	addressesr  Ú	canonicalÚitemr   r   r   r  #  s    
r  c       
      C   s¬   y"t jj| ƒ | dddf}tj}W n" tk
rD   | df}tj}Y nX t|tjƒ\}}g }g }t	|d|tj
tjtjƒ}|d d }x|D ]}	|j|	d d ƒ qˆW |||fS )Nr  r   rD   r  )r   rô   rõ   r�   rø   rö   rü   r  r  r  r  r  r  r1   )
Úipr  r  r)   r†   r  r  r  r  r   r   r   r   Ú_gethostbyaddr/  s     

r"  c             C   s:   | dkrt ƒ } | att_tt_tt_t	t_
tt_tt_dS )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.

    resolver, a ``dns.resolver.Resolver`` or ``None``, the resolver to use.
    N)rè   rú   r  r�   Úgetaddrinfor  Úgetnameinfor  Úgetfqdnr  Úgethostbynamer  Úgethostbyname_exr"  Úgethostbyaddr)ré   r   r   r   Úoverride_system_resolverC  s    r)  c               C   s,   da tt_tt_tt_tt_	t
t_tt_dS )z5Undo the effects of prior override_system_resolver().N)rú   Ú_original_getaddrinfor�   r#  Ú_original_getnameinfor$  Ú_original_getfqdnr%  Ú_original_gethostbynamer&  Ú_original_gethostbyname_exr'  Ú_original_gethostbyaddrr(  r   r   r   r   Úrestore_system_resolver]  s    r0  )r   )N)N)Pr6   r�   r•   rS   rÖ   Ú	threadingrh   ÚImportErrorÚdummy_threadingZdns.exceptionr   Z	dns.flagsZdns.ipv4Zdns.ipv6Zdns.messageZdns.nameZ	dns.queryZ	dns.rcodeZdns.rdataclassZdns.rdatatypeZdns.reversenameZdns.tsigÚ_compatr   r   r–   Úwinregr»   rÛ   ZDNSExceptionr   r;   rÊ   r<   r@   rG   rH   rI   ÚobjectrJ   rc   ru   r|   r�   ræ   rè   rç   r!   rå   r   r    r=   rê   r  ÚSOL_UDPr  r  r  rú   r#  r*  r$  r+  r%  r,  r&  r-  r'  r.  r(  r/  rù   r  r  r  r  r  r"  r)  r0  r   r   r   r   Ú<module>   sˆ   
QgVj    4,

c
(
	
