
    "                     N   d 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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j2                  dk(  r	 ddlZ G d de
j8                  j:                        Z G d d	e
j8                  j:                        Ze
j8                  j@                  Z  G d
 de
j8                  j:                        Z! G d de
j8                  j:                        Z" G d de
j8                  j:                        Z# G d de
j8                  j:                        Z$ G d de
j8                  j:                        Z% G d de&      Z' G d de&      Z( G d de&      Z) G d de&      Z* G d de&      Z+da,d Z-d Z.e
j^                  j`                  e
jb                  jd                  d dd!ddfd"Z3e
jb                  jd                  d dfd#Z4ejj                  ejl                  gejn                  ejp                  giZ9da:ejv                  Z<ejz                  Z>ej~                  Z@ej                  ZBej                  ZDej                  ZFddej                  dddfd$ZHd,d%ZId-d&ZJd' ZKd( ZLd) ZMd-d*ZNd+ ZOy# e$ r ddlZY {w xY w# e$ r ddlZY ?w xY w).DNS stub resolver.    N   )xrangestring_typeswin32c                   h     e Zd ZdZddhZdZddZ fdZd Z e	ed	      Zd
 Z
d Zd Zd Z xZS )NXDOMAINz"The DNS query name does not exist.qnames	responsesNc                     t        |t        t        t        f      st	        d      t        |      dk(  rt	        d      |i }nt        |t              st	        d      t        ||      }|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   kwargss       lib/third_party/dns/resolver.py_check_kwargszNXDOMAIN._check_kwargs7   si    &4"45 !FGGv;! !KLLIIt, !KLLVy9    c                     d| j                   vrt        t        |          S | j                   d   }t	        |      dkD  rd}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__s      r   r   zNXDOMAIN.__str__C   sh    4;;&4022X&v;?1C5C3sF+,sF++r   c                    d| j                   vrt        d      t        j                  j                  }t        j
                  j                  }d }| j                   d   D ]  }| j                   d   |   }|j                  D ]H  }|j                  |k7  s|j                  |k7  r"|j                  d   j                  j                         }J |ot        j                  j                  |      c S  | 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*   s          r   canonical_namezNXDOMAIN.canonical_nameN   s    4;;&=>>^^##[[*E{{;/6H"//==E)V^^r-AQ..668 *  xx))%00 + {{8$Q''r   z%Return the unresolved canonical name.)docc                 n   t        | j                  j                  dg             }t        | j                  j                  di             }|j                  j                  di       }|j                  j                  dg       D ]$  }||vr|j	                  |       ||v s||   ||<   & t        ||      S )z3Augment by results from another NXDOMAIN exception.r
   r   r   )r   r   getr   appendr	   )r   e_nxqnames0
responses0
responses1qname1s         r   __add__zNXDOMAIN.__add__`   s    t{{x45$++//+r:;
[[__["5
kkooh3FW$v&#%/%7
6"	 4
 w*==r   c                      | j                   d   S )zXAll of the names that were tried.

        Returns a list of ``dns.name.Name``.
        r
   r   r   s    r   r
   zNXDOMAIN.qnamesl   s    
 {{8$$r   c                      | j                   d   S )zA map from queried names to their NXDOMAIN responses.

        Returns a dict mapping a ``dns.name.Name`` to a
        ``dns.message.Message``.
        r   rA   rB   s    r   r   zNXDOMAIN.responsess   s     {{;''r   c                 &    | j                   d   |   S )zTThe response for query *qname*.

        Returns a ``dns.message.Message``.
        r   rA   )r   r3   s     r   r4   zNXDOMAIN.response{   s    
 {{;'..r   N)__name__
__module____qualname____doc__supp_kwargsfmtr   r   r5   propertyr?   r
   r   r4   __classcell__r"   s   @r   r	   r	   2   sI    ,[)K
C
	,( n/2N
>%(/r   r	   c                       e Zd ZdZy)YXDOMAINz8The DNS query name is too long after DNAME substitution.NrF   rG   rH   rI    r   r   rP   rP      s    Br   rP   c                   ,     e Zd ZdZdZdhZ fdZ xZS )NoAnswerz<The DNS response does not contain an answer to the question.zDThe DNS response does not contain an answer to the question: {query}r4   c                 F    t         t        |   |d   j                        S )Nr4   )query)r   rT   _fmt_kwargsquestion)r   r   r"   s     r   rW   zNoAnswer._fmt_kwargs   s*    Xt0$-- 1 / 	/r   )rF   rG   rH   rI   rK   rJ   rW   rM   rN   s   @r   rT   rT      s    F%C,K/ /r   rT   c                   >     e Zd ZdZdZdedd z  ZddhZ fdZ xZ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}Nrequesterrorsc           
          g }|d   D ]5  }|j                  dj                  |d   |d   rdnd|d   |d                7 t        t        |   |d	   j
                  d
j                  |            S )Nr]   z Server {} {} port {} answered {}r   r   TCPUDP      r\   z; )rV   r]   )r9   r    r   rZ   rW   rX   r   )r   r   srv_msgserrr"   s       r   rW   zNoNameservers._fmt_kwargs   s    (#COO>EEc!f%(VEAAH I $ ]D5#,,TYYx5H 6 J 	Jr   )	rF   rG   rH   rI   r!   rK   rJ   rW   rM   rN   s   @r   rZ   rZ      s5     8C
 3s8
+Ch'KJ Jr   rZ   c                       e Zd ZdZy)NotAbsolutezEAn absolute domain name is required but a relative name was provided.NrQ   rR   r   r   rf   rf      s    Or   rf   c                       e Zd ZdZy)	NoRootSOAzBThere is no SOA RR at the DNS root name. This should never happen!NrQ   rR   r   r   rh   rh      s    Lr   rh   c                       e Zd ZdZy)NoMetaqueriesz DNS metaqueries are not allowed.NrQ   rR   r   r   rj   rj      s    *r   rj   c                   8    e Zd ZdZ	 d	dZd Zd Zd Zd Zd Z	y)
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.
    c                 n   || _         || _        || _        || _        d}d }t	        dd      D ]B  }	 |j                  |j                  |||      }|dk(  s|j                  |k  r|j                  } n ||rt        |      || _        || _        |z	 	 |j                  |j                   ||t        j                  j"                        }|dk(  s|j                  |k  r|j                  }|d   j$                  |k  r|d   j$                  }	 t-        j,                         |z   | _        y # t        $ r |t        j                  j                  k7  r	 |j                  |j                  ||t        j                  j                        }	|dk(  s|	j                  |k  r|	j                  }|	D ]  }
|
j                  } n Y # t        $ r |rt        |      Y nw xY w|rt        |      Y w xY w# t        $ r: 	 |j'                         }n%# t        j(                  j*                  $ r Y Y w xY wY nw xY w)Nr[   r      r4   )r3   r+   r,   r4   r   
find_rrsetr*   ttlKeyErrorr%   r(   r)   r.   rT   r5   rrset	authoritySOAminimumparentr0   NoParenttime
expiration)r   r3   r+   r,   r4   raise_on_no_answermin_ttlrs   countcrrsetrdsrrsets               r   __init__zAnswer.__init__   s/   
 Ar]E6 ++HOOU,3V=b=EII$7#iiG #2 =/H--#
=%001C1CU18#--:K:KMF"}

W(<"(**ay((72"()"3"3 ))+/O  6S]]000>!)!4!4X__5:5<58]]5H5H"J #b=FJJ,@&,jjG"(B$&IIE! #) !# >-"*H"== .> &"H55 &!6D    %88,,   sg   >D)A7G/ )&G,A*F==GG,GG,+G,/	H29H
	H2
H,'H2+H,,H21H2c                 &   |dk(  r| j                   j                  S |dk(  r| j                   j                  S |dk(  r| j                   j                  S |dk(  r| j                   j                  S |dk(  r| j                   j
                  S t        |      )Nr0   rq   coversr,   r+   )rs   r0   rq   r   r,   r+   r   )r   attrs     r   __getattr__zAnswer.__getattr__  s    6>::??"U]::>>!X::$$$Y::%%%X::$$$ &&r   c                 P    | j                   xr t        | j                         xs dS )Nr   )rs   r   rB   s    r   __len__zAnswer.__len__  s    zz-c$**o22r   c                 r    | j                   xr t        | j                         xs t        t                     S rE   )rs   iterr   rB   s    r   __iter__zAnswer.__iter__  s%    zz.d4::.?$uw-?r   c                 D    | j                   t        | j                   |   S rE   rs   
IndexErrorr   is     r   __getitem__zAnswer.__getitem__  s     ::zz!}r   c                 B    | j                   t        | j                   |= y rE   r   r   s     r   __delitem__zAnswer.__delitem__  s    ::JJqMr   N)T)
rF   rG   rH   rI   r   r   r   r   r   r   rR   r   r   rl   rl      s,      %)60p'3@
r   rl   c                   2    e Zd ZdZddZd Zd Zd Zd	dZy)
Cachez$Simple thread-safe DNS answer cache.c                     i | _         || _        t        j                         | j                  z   | _        t	        j
                         | _        y)zf*cleaning_interval*, a ``float`` is the number of seconds between
        periodic cleanings.
        N)datacleaning_intervalry   next_cleaning
_threadingLocklock)r   r   s     r   r   zCache.__init__$  s:    
 	!2!YY[4+A+AAOO%	r   c                 N   t        j                          }| j                  |k  rg }| j                  j                         D ]&  \  }}|j                  |k  s|j                  |       ( |D ]  }| j                  |=  t        j                          }|| j                  z   | _        yy)z&Clean the cache if it's time to do so.N)ry   r   r   r-   rz   r9   r   )r   nowkeys_to_deletekvs        r   _maybe_cleanzCache._maybe_clean.  s     iik$N))//+A<<3&"))!, , $IIaL $))+C!$t'='=!=D %r   c                    	 | j                   j                          | j                          | j                  j	                  |      }|!|j
                  t        j                         k  r	 | j                   j                          y|| j                   j                          S # | j                   j                          w xY w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)r   acquirer   r   r8   rz   ry   release)r   keyr   s      r   r8   z	Cache.get<  s    	 II		c"AyALLDIIK7 II IIDIIs   A(B" B" "B>c                     	 | j                   j                          | j                          || j                  |<   | j                   j	                          y# | j                   j	                          w xY wzAssociate 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)r   r   r   r   r   r   r   values      r   putz	Cache.putQ  sQ    	 II"DIIcNIIDIIs   9A A2Nc                 B   	 | j                   j                          ||| j                  v r;| j                  |= n-i | _        t        j                         | j                  z   | _        | j                   j                          y# | j                   j                          w xY w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)r   r   r   ry   r   r   r   )r   r   s     r   flushzCache.flusha  st    		 II$))#		#	%)YY[43I3I%I"IIDIIs   A%B B)g     r@rE   )	rF   rG   rH   rI   r   r   r8   r   r   rR   r   r   r   r   !  s    .&> *   r   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)LRUCacheNodezLRUCache node.c                 <    || _         || _        | | _        | | _        y rE   )r   r   prevnextr   s      r   r   zLRUCacheNode.__init__z  s    
		r   c                 d    |j                   | _         || _        | |j                   _        | |_         y rE   r   r   r   nodes     r   link_beforezLRUCacheNode.link_before  s&    II					r   c                 d    || _         |j                  | _        | |j                  _         | |_        y rE   r   r   s     r   
link_afterzLRUCacheNode.link_after  s&    	II				r   c                 p    | j                   | j                  _         | j                  | j                   _        y rE   r   rB   s    r   unlinkzLRUCacheNode.unlink  s     				r   N)rF   rG   rH   rI   r   r   r   r   rR   r   r   r   r   w  s    #r   r   c                   2    e Zd ZdZddZd Zd Zd Zd	dZy)
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                     i | _         | j                  |       t        dd      | _        t	        j
                         | _        y)zl*max_size*, an ``int``, is the maximum number of nodes to cache;
        it must be greater than 0.
        N)r   set_max_sizer   sentinelr   r   r   r   max_sizes     r   r   zLRUCache.__init__  s5    
 	(#$T40OO%	r   c                      |dk  rd}|| _         y )Nr   )r   r   s     r   r   zLRUCache.set_max_size  s    a<H r   c                 F   	 | j                   j                          | j                  j                  |      }|	 | j                   j	                          y|j                          |j                  j                  t        j                         k  r3| j                  |j                  = 	 | j                   j	                          y|j                  | j                         |j                  | j                   j	                          S # | j                   j	                          w xY wr   )r   r   r   r8   r   r   r   rz   ry   r   r   r   )r   r   r   s      r   r8   zLRUCache.get  s    	 II99==%D| II KKMzz$$		3IIdhh' II OODMM*::IIDIIs   7D AD &D D c                    	 | j                   j                          | j                  j                  |      }|'|j	                          | j                  |j
                  = t        | j                        | j                  k\  r`| j                  j                  }|j	                          | j                  |j
                  = t        | j                        | j                  k\  r`t        ||      }|j                  | j                         || j                  |<   | j                   j                          y# | j                   j                          w xY wr   )r   r   r   r8   r   r   r   r   r   r   r   r   r   )r   r   r   r   s       r   r   zLRUCache.put  s    	 II99==%DIIdhh'dii.DMM1}}))IIdhh' dii.DMM1  U+DOODMM*!DIIcNIIDIIs   CD3 "6D3 3ENc                    	 | j                   j                          |E| j                  j                  |      }||j	                          | j                  |j
                  = nX| j                  j                  }|| j                  k7  r,|j                  }d|_        d|_        |}|| j                  k7  r,i | _        | j                   j                          y# | j                   j                          w xY wr   )
r   r   r   r8   r   r   r   r   r   r   )r   r   r   r   s       r   r   zLRUCache.flush  s    	 IIyy}}S)#KKM		$((+}}))dmm+99D $DI $DID	 dmm+
 	IIDIIs   B1C 4C C2)i rE   )	rF   rG   rH   rI   r   r   r8   r   r   rR   r   r   r   r     s     &!
 6 2 r   r   c                       e Zd ZdZddZd Zd Zd Zd Zd Z	d	 Z
d
 Zd Zd ZddZej                   j"                  ej$                  j&                  dddddfdZdej*                  j,                  fdZd Zd Zy)Resolverr   Tc                    d| _         d| _        d| _        d| _        d| _        d| _        d| _        d| _        d| _        d| _	        d| _
        d| _        d| _        d| _        d| _        d| _        d| _        | j#                          |r9t$        j&                  dk(  r| j)                          y|r| j+                  |       yyy)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	configures      r   r   zResolver.__init__  s      $	 	

#

||w&""$%%h/  r   c                 .   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| _        y)	z1Reset all resolver configuration to the defaults.r   Nr   5   g       @g      >@r[   F)r%   r0   Namer1   socketgethostnamer   r   rootr   r   r   r   r   r   r   r   tsigdefault_algorithmr   r   r   r   r   r   r   r   rB   s    r   r   zResolver.reset#  s     HHMM#((,,V-?-?-AB12FG 	t{{q ((--DK "	HH66	

#r   c                 6   t        |t              r	 t        |d      }d}nd}	 |D ]  }t        |      dk(  s|d   dk(  s|d   dk(  r"|j                         }t        |      d	k  rA|d   d
k(  r| j                  j                  |d          h|d   dk(  r(t        j                  j                  |d         | _        |d   dk(  rC|dd D ]:  }| j                  j                  t        j                  j                  |             < |d   dk(  sd|dd v sd| _         	 |r|j                          	 t        | j                        dk(  r| j                  j                  d       yy# t        $ r dg| _        Y yw xY w# |r|j                          w w xY w)zProcess *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.r	127.0.0.1NTFr   #;ra   
nameserverr   r   r   optionsr   )r   r   openIOErrorr   r   splitr9   r%   r0   r1   r   r   r   close)r   f
want_closeltokenssuffixs         r   r   zResolver.read_resolv_conf;  s   
 a&CL JJ	q6Q;!A$#+1 v;?!9,$$++F1I6AY(*"%(("4"4VAY"?DKAY(*"(***388+=+=f+EF #-AY)+6!":-&*% ( 	t A%##K0 &?   %0= 	: 	 s)   E, C-F F 	F ,F ?F Fc                 j    |j                  d      dk\  rd}|S |j                  d      dk\  rd}|S d}|S )N r   ,)find)r   entry
split_chars      r   _determine_split_charzResolver._determine_split_chare  sM     ::c?aJ  ZZ_!J  Jr   c                     t        |      }| j                  |      }|j                  |      }|D ],  }|| j                  vs| j                  j	                  |       . y rE   )r   r  r   r   r9   )r   r   r   ns_listnss        r   _config_win32_nameserversz"Resolver._config_win32_nameserverst  sX    +&//<
##J/B)))  ''+ r   c                 ^    t         j                  j                  t        |            | _        y rE   )r%   r0   r1   r   r   )r   r   s     r   _config_win32_domainzResolver._config_win32_domain}  s    hh((V5r   c                     t        |      }| j                  |      }|j                  |      }|D ]I  }|| j                  vs| j                  j	                  t
        j                  j                  |             K y rE   )r   r  r   r   r9   r%   r0   r1   )r   r   r   search_listss        r   _config_win32_searchzResolver._config_win32_search  s`    V//7
ll:.A#""388#5#5a#89 r   c                 t   	 t        j                  |d      \  }}|r| j                  |       |s|r.	 t        j                  |d      \  }}|r| j	                  |       nZ	 t        j                  |d      \  }}|r>| j                  |       	 t        j                  |d      \  }}|r| j	                  |       	 t        j                  |d      \  }}|r| j                  |       y y # t        $ r d }Y w xY w# t        $ r Y Kw xY w# t        $ r d }Y w xY w# t        $ r Y kw xY w# t        $ r d }Y bw xY w)N
NameServerDomainDhcpNameServer
DhcpDomain
SearchList)_winregQueryValueExWindowsErrorr  r  r  )r   r   always_try_domainserversrtypedomr   s          r   _config_win32_fromkeyzResolver._config_win32_fromkey  sW   	$11#|DNGU **73'$11#x@
U--c2!(!5!5c;K!L ..w7!(!5!5c<!HJC11#6	#00lCMFE %%f- 9  	G	   
    $   	F	sX   C) ,C: !D	 ,D ;D) )C76C7:	DD	DD	D&%D&)D76D7c                 ,   t        j                  dt         j                        }d}	 	 t        j                  |d      }d}	 | j                  |d       |j                          |rt        j                  |d      }	 d}	 	 t        j                  ||      }|dz  }t        j                  ||      }| j                  |||      sG	 | j                  |d       |j                          	 k|j                          y# t        $ r t        j                  |d      }Y w xY w# |j                          w xY w# |j                          w xY w# t        $ r Y nw xY w	 |j                          # |j                          w xY w# |j                          w xY w)	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  ConnectRegistryHKEY_LOCAL_MACHINEOpenKeyEnvironmentErrorr  CloseEnumKey_win32_is_nic_enabled)r   lm	want_scan
tcp_params
interfacesr   guidr   s           r   r   zResolver.read_registry  s~    $$T7+E+EF	'	
E$__R.KL
 !	#**:t<  "$__R.<=
'A"#*??:q#ADFA")//*d"CC#'#=#=b$#L (, $ : :3 F #		   HHJA $ E$__R.DE
E   "$ !$		/ "!"!$$&J$$&HHJs   C< D! (F =E, AE E, D6 E )E, <DF DF !D33F 6EE 	EE, EE, F ,E>>F Fc                 Z   	 t        j                  |d|z        }	 t        j                  |d      \  }}|t         j                  k7  rt        t        j                  |d|z        }	 t        j                  |d      \  }}|t         j
                  k7  rt        |dz   |j                          |j                          S # |j                          w xY w# |j                          w xY w# t        t        f$ r1 	 t        j                  |d      \  }	}|	d ucY S # t        $ r Y Y yw xY ww xY w)Nz]SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\%s\ConnectionPnpInstanceIDz SYSTEM\CurrentControlSet\Enum\%sConfigFlagsr   NTEContextListF)	r  r  r  REG_SZ
ValueError	REG_DWORDr  r  r  )
r   r"  r&  interface_keyconnection_keypnp_idttype
device_keyr   ntes
             r   r!  zResolver._win32_is_nic_enabled  s7   /	 %__"$()*N'")"6"6"O#5 GNN*$$$__;fDF
'%,%9%9"M&3NUE  1 11((  %s{? $$&$$& $$&$$& *- 	&33M4D Fe$& 	s_   C* AC (7C  C /C*  CC C''C* *D*:DD*	D&"D*%D&&D*Nc                     || j                   n|}t        j                         }||z
  }|dk  r|dk  rt        |      |}||k\  rt        |      t        ||z
  | j                        S )Nr   r[   )r   )r   ry   Timeoutminr   )r   startr   r   durations        r   _compute_timeoutzResolver._compute_timeout  so    $,$44==(iik;a<"}h//
 x(++8h&55r   Fr   c	           
         t        |t              r t        j                  j	                  |d      }t        |t              rt        j
                  j	                  |      }t        j
                  j                  |      rt        t        |t              rt        j                  j	                  |      }t        j                  j                  |      rt        g }	|j                         r|	j                  |       nt        |      dkD  r8|	j                  |j                  t        j                  j                               | j                  r2| j                  D ]"  }
|	j                  |j                  |
             $ n*|	j                  |j                  | j                                d}i }t#        j"                         }d}|	D ]  }| j$                  rH| j$                  j'                  |||f      }|(|j(                  |rt+        |j,                        |c S t        j.                  j1                  |||      }| j2                  2|j5                  | j6                  | j2                  | j8                         |j;                  | j<                  | j>                  | j@                         | jB                  | jB                  |_!        d}| jD                  dd }g }| jF                  rtI        jJ                  |       d}|Vt        |      dk(  rtM        ||      |dd D ]  }| jO                  ||      }| jP                  j'                  || jR                        }	 |}|r&t        jT                  jW                  ||||||	      }nt        jT                  jY                  ||||||	      }|jB                  t        jB                  jZ                  z  r9d}| jO                  ||      }t        jT                  jW                  ||||||	      }|jm                         }|t        jl                  jn                  k(  r"to               }|j                  |||||f       ||t        jl                  jp                  k(  s|t        jl                  jr                  k(  r nr|t        jl                  jt                  k7  s| jv                  s|ji                  |       |j                  |||t        jl                  jy                  |      |f       d} |nJt        |      dkD  r8| jO                  ||      }t{        ||      }|d
z  }t#        j|                  |       |V|jm                         t        jl                  jr                  k(  r|||<   d} n |rts        |	|      t        ||||      }| j$                  r| j$                  j                  |||f|       |S # t\        j^                  t        j`                  jb                  f$ r#}|j                  ||||f       d}Y d}~d}~wt        jT                  jd                  $ r#}|j                  ||||f       d}Y d}~Ad}~wt        j`                  jf                  $ r4}|ji                  |       |j                  ||||f       d}Y d}~d}~wtj        $ r4}|ji                  |       |j                  ||||f       d}Y d}~d}~ww xY w)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   Tro   )	algorithmg?r   )r\   r]   )sourcesource_portra   Fr   )Ar   r   r%   r0   r1   r(   is_metatyperj   r&   is_metaclassis_absoluter9   r   concatenater   r   r   ry   r   r8   rs   rT   r4   message
make_queryr   use_tsigr   r   use_ednsr   r   r   r   r   r   randomshufflerZ   r9  r   r   rV   tcpudpTCr   error	exceptionr5  UnexpectedSource	FormErrorremoveEOFErrorrcoderP   NOERRORr	   SERVFAILr   r/   r6  sleeprl   r   )r   r3   r+   r,   rH  r<  r{   r=  r   qnames_to_tryr   all_nxdomainnxdomain_responsesr7  _qnamer*   r\   r4   r   r]   backoffr   r   r   tcp_attemptexrQ  
sleep_times                               r   rV   zResolver.query"  s   Z e\*HH&&ud3Efl+]],,V4F==$$V,g|,nn..w7G>>&&w/  '5zA~$$U%6%6sxx}}%EF{{"kkF!(():):6)BC * $$U%6%6t{{%CD		#Fzz(AB%||+0B&@@%kk,,VVWEG||'  t||+/+<+< ! >TYYEzz% $

H **1-KF{{{+G"{#q('GG"-a.J"33E8DG0044ZKD<!&)'*yy}}Wj5<d<BAL (5 (NH
 (+yy}}Wj5<d<BAL (5 (NH  (~~		<.2*.*?*?x*P$'IIMM':2949?>I %2 %K !)Z %NN,E		 2 22%Zz;b'/'1 2 		 1 11!SYY%7%77 		 2 22$:M:M#**:6MM:{D#&99#4#4U#;X#G H#He #1f ' {#a'
 #33E8DG!$Wg!6JqLGJJz*G "H ~~399#5#55-5"6* LA $B -;MNN*,::JJNNFFG4f=i #LL#--*?*?@ !
 z;b'/'1 2#' 9955 ! z;b'/'1 2#' ==22 
! $**:6z;b'/'1 2#' # ! $**:6z;b'/'1 2#' !s=   "B/W-[5
X(( [5Y&& [5)Z55[5)[00[5c                     || _         |3t        | j                   j                               d   | _        || _        y|| _        || _        y)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;  s       r   rD  zResolver.use_tsig  sI    ( ? 1 1 34Q7DL & #DL%r   c                 6    |d}|| _         || _        || _        y)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[   )r   r   r   )r   r   r   r   s       r   rE  zResolver.use_edns  s#     <D	"r   c                     || _         y)zkOverrides the default flags with your own.

        *flags*, an ``int``, the message flags to use.
        N)r   )r   r   s     r   	set_flagszResolver.set_flags!  s     
r   )z/etc/resolv.confTrE   )rF   rG   rH   rI   r   r   r   r  r  r  r  r  r   r!  r9  r%   r(   Ar&   r'   rV   r   r   rD  rE  ra  rR   r   r   r   r     s    #0J0(1T,6: .D,\5n6" #&--//3>>;L;L1M^ )-8855&6*r   r   c                  .    t         
t                t         S )z7Get the default resolver, initializing it if necessary.)default_resolverreset_default_resolverrR   r   r   get_default_resolverrf  .  s     r   c                      t               ay)zRe-initialize default resolver.

    Note that the resolver configuration (i.e. /etc/resolv.conf on UNIX
    systems) will be re-read immediately.
    N)r   rd  rR   r   r   re  re  5  s      zr   FTc           
      B    t               j                  | |||||||      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.query`` for more information on the
    parameters.
    )rf  rV   )r3   r+   r,   rH  r<  r{   r=  r   s           r   rV   rV   @  s,      !''vwV(:K(02 2r   c                 `   t        | t              r8t        j                  j	                  | t        j                  j
                        } |
t               }| j                         st        |       	 	 |j                  | t        j                  j                  ||      }|j                  j                  | k(  r| S 	 	 | j!                         } [# t        j                  j                  t        j                  j                  f$ r Y Kw xY w# t        j                  j"                  $ r t$        w xY w)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``.
    )r   r   r%   r0   r1   r   rf  r@  rf   rV   r(   ru   rs   resolverr	   rT   rw   rx   rh   )r0   r,   rH  rj  r*   s        r   zone_for_namerk  Q  s    & $%xx!!$6')$
	^^D#--*;*;WcJF||  D( )
	;;=D  %%s||'<'<= 		 xx   	O	s   2AC ;D	 7DD	$D-c                 X
   |t         j                  t         j                  z  z  dk7  rt        | %|#t        j                  t         j
                        g }g }d }	 | ^d}|t         j                  z  dk7  r#|j                  d       |j                  d       n||j                  d       |j                  d       nY| j                  d      }	t        |	      dk(  r|	d   }
n| }
t        j                  j                  |
      }|j                  |        | }d }	 |d}ntA        |      }|#t        j                  t         j
                        g }|dk(  r!t         jF                  t         jH                  g}n|g}|t         jJ                  z  dk7  r|}nd}|t         j                   k(  s|t         j"                  k(  rD|D ]?  }|D ]8  }tL        |   D ]*  }|j                  t         j                   |||||ddff       , : A |t         j6                  k(  s|t         j"                  k(  rB|D ]=  }|D ]6  }tL        |   D ](  }|j                  t         j6                  |||||ff       * 8 ? t        |      dk(  r#t        j                  t         j
                        |S # t        $ r; 	 t        j                  j                  |       }|j                  |        | }n# t        $ r |t         j                  z  dk(  r	 |t         j                   k(  s|t         j"                  k(  rt$        j'                  | t        j(                  j*                  d	
      }|j,                  } |j.                  j1                  d      }|j2                  ,|j2                  D ]  }|j                  |j4                          |t         j6                  k(  s|t         j"                  k(  rt$        j'                  | t        j(                  j8                  d	
      }|j,                  } |j.                  j1                  d      }|j2                  ,|j2                  D ]  }|j                  |j4                          no# t        j:                  j<                  $ r$ t        j                  t         j
                        t        $ r$ t        j                  t         j>                        w xY wY nw xY wY w xY w# t        $ r@ |t         jB                  z  dk(  r&	 t        jD                  |      }n# t        $ r Y nw xY wY  w xY w)Nr   	localhostz::z0.0.0.0z::1r   %ra   F)r{   T )'r   AI_ADDRCONFIGAI_V4MAPPEDNotImplementedErrorgaierror
EAI_NONAME
AI_PASSIVEr9   r   r   r%   ipv6	inet_aton	Exceptionipv4AI_NUMERICHOSTAF_INET6	AF_UNSPEC	_resolverrV   r(   AAAAr3   r5   r/   rs   addressAF_INETrb  rj  r	   
EAI_SYSTEMintAI_NUMERICSERVgetservbyname
SOCK_DGRAMSOCK_STREAMAI_CANONNAME_protocols_for_socktype)hostservicefamilysocktypeprotor   v6addrsv4addrsr5   partsahostaddrv6rdatav4r   tuples	socktypesr2   s                      r   _getaddrinfor    sd   $$v'9'99:a?!!|oof//00GGN1=<(Nv(((A-t$y)u%{+JJsOE5zQa88%%e,DNN4 !N@ D?Dw<D |oof//00F1}&&(:(:;	J	v"""a' Ff.>.>$>D%4X>EMM6??He#(4q!*<#> ? ? & 
 6V-=-=#=D%4X>EMM6>>8U#(4,#8 9 ? & 
 6{aoof//00MK  =	=88%%d+DNN4 !N 	=v,,,1=0Ff>N>N4N&__T3==3E3E@E - G  "xx)+):):)B)B4)H88/)+ 'u}} = *2/6V=M=M3M&__T3==??@E - G!xx)+):):)B)B4)H88/)+ 'u}} = *2||,, = //&*;*;<<  = //&*;*;<<=/	==L  6(((A-++G4 	s   B9J S  
S#2KS!S9E*Q$#S$A,SSSSSS T) TT)	T"T)!T""T)(T)c                    | d   }| d   }t        |       dk(  r| d   }t        j                  }nd }t        j                  }t	        |||t        j
                  t        j                  d      }t        |      dkD  rt        j                  d      |d   d   d   }|t        j                  z  rd}nd}t        j                  j                  |      }	|t        j                  z  dk(  r@	 t        j                  |	d      }
|
j                  d   j                   j#                  d	      }n|}||d
t1        |      z   z  }|t        j2                  z  rt1        |      }||fS t        j4                  ||      }||fS # t        j$                  j&                  t        j$                  j(                  f$ rN |t        j*                  z  r#t        j,                  t        j.                        |}||d
t1        |      z   z  }Y w xY w)Nr   r      rb   z'sockaddr resolved to multiple addressesrI  rH  PTRTrn  )r   r   r{  r  r  r  SOL_TCPrK  NI_DGRAMr%   reversenamefrom_addressNI_NUMERICHOSTr}  rV   rs   r.   r/   rj  r	   rT   NI_NAMEREQDrs  rt  r   NI_NUMERICSERVgetservbyport)sockaddrr   r  r   scoper  r  r  pnamer3   r*   hostnamer  s                r   _getnameinfor    s   A;DA;D
8}$ff.@.@ ..!-F
6{QllDEE!9Q<?DvOO((.Ev$$$)	-__UE2F||A--55d;H c%j((Hv$$$d) g &&tU3g %%s||'<'<= 	-v)))oof&7&788H C#e*,,	-s   )>E9 9BG>=G>c                     | t        j                         } 	 t        t        | d      d   d         d   S # t        $ r | cY S w xY w)NP   r   r  )r   r   r  r  rx  r0   s    r   _getfqdnr    sO    |!!#Lr215a89!<< s   6 AAc                 $    t        |       d   d   S )Nra   r   )_gethostbyname_exr  s    r   _gethostbynamer    s    T"1%a((r   c                     g }g }t        | dt        j                  t        j                  t        j                  t        j
                        }|d   d   }|D ]  }|j                  |d   d           |||fS )Nr   rb   r  )r  r   r  r  r  r  r9   )r0   aliases	addressesr  	canonicalitems         r   r  r  #  ss    GI$6>>63E3E ..&*=*=?Fq	!Ia$  w	**r   c                    	 t         j                  j                  |        | dddf}t        j                  }t        |t        j                        \  }}g }g }t        |d|t        j                  t        j                  t        j                        }|d   d   }|D ]  }	|j                  |	d   d           |||fS # t
        $ r | df}t        j                  }Y w xY w)Nr  r   rb   r  )r%   rv  rw  r   r{  rx  r  r  r  r  r  r  r  r9   )
ipr  r  r0   r   r  r  r  r  r  s
             r   _gethostbyaddrr  /  s     2Aq>  &*<*<=LT4GI$66+=+=v~~ --/Fq	!Ia$  w	**   8 s   5B= =CCc                     | 
t               } | at        t        _        t
        t        _        t        t        _        t        t        _
        t        t        _        t        t        _        y)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)rf  r}  r  r   getaddrinfor  getnameinfor  getfqdnr  gethostbynamer  gethostbyname_exr  gethostbyaddr)rj  s    r   override_system_resolverr  C  sF     ')I%F%FFN)F/F)Fr   c                      da t        t        _        t        t        _        t        t        _        t        t        _	        t        t        _        t        t        _        y)z5Undo the effects of prior override_system_resolver().N)r}  _original_getaddrinfor   r  _original_getnameinfor  _original_getfqdnr  _original_gethostbynamer  _original_gethostbyname_exr  _original_gethostbyaddrr  rR   r   r   restore_system_resolverr  ]  s8     I.F.F&FN2F8F2Fr   )r   rE   )PrI   r   r   ry   rF  	threadingr   ImportErrordummy_threadingdns.exceptionr%   	dns.flagsdns.ipv4dns.ipv6dns.messagedns.name	dns.query	dns.rcodedns.rdataclassdns.rdatatypedns.reversenamedns.tsig_compatr   r   r   winregr  rL  DNSExceptionr	   rP   r5  rT   rZ   rf   rh   rj   objectrl   r   r   r   r   rd  rf  re  r(   rb  r&   r'   rV   rk  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  r  rR   r   r   <module>r     s  $   
  )"             )<<7 N/s}})) N/bCs}})) C --

/s}})) /JCMM.. J,P#--,, PM** M+CMM.. +dV dNS F S l#6 #4g v g Tlv l`  " 1B1BDT$2" !$ 1 1ut $X '
( 
 	** ** NN  .. #44  ..  D1A1AAaH%P)	++(*4
3E*  )()(  s#   J  J 	JJ	J$#J$