o
    2c*                     @   s"  d Z ddlm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zddl	Z
W n ey3   ddlZ
Y nw 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ejdkrqddlZ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jjZ'G dd dZ(G dd dZ)G dd dZ*G dd de*Z+G dd dZ,G dd  d e*Z-G d!d" d"Z.G d#d$ d$Z/G d%d& d&e/Z0da1d'd( Z2d)d* Z3ej4j5ej6j7d+dd,dddfd-d.Z8ej4j5ej6j7d+dd,ddfd/d0Z9d1d2 Z:d3d4 Z;ej6j7d+dfd5d6Z<ej=ej>gej?ej@giZAdaBejCZDejEZFejGZHejIZJejKZLejMZNddejOdddfd7d8ZPdGd9d:ZQdHd;d<ZRd=d> ZSd?d@ ZTdAdB ZUdHdCdDZVdEdF ZWdS )IDNS stub resolver.    )urlparseNwin32c                       sd   e Zd ZdZddhZdZ	dddZ fddZ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 u ri }n	t |ts&t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   B/var/www/html/gps/gps/lib/python3.10/site-packages/dns/resolver.py_check_kwargs8   s   
zNXDOMAIN._check_kwargsc                    sP   d| j vr
t  S | j d }t|dkrd}nd}dtt|}d||S )Nr      zNone of DNS query names existz!The DNS query name does not existz, z{}: {})r   super__str__r   joinmapstrformat)r   r   msg	__class__r   r   r   E   s   


zNXDOMAIN.__str__c              	   C   sn   d| j vr	td| j d D ]!}| j d | }z| }||kr%|W   S W q ty/   Y qw | j d d S )z%Return the unresolved canonical name.r   zparametrized exception requiredr   r   )r   	TypeErrorcanonical_name	Exception)r   qnameresponsecnamer   r   r   r    P   s   

zNXDOMAIN.canonical_namec                 C   sx   t | jdg }t| jdi }|jdi }|jdg D ]}||vr+|| ||v r5|| ||< q t||dS )z3Augment by results from another NXDOMAIN exception.r   r   r   )r
   r   getr   appendr   )r   e_nxqnames0
responses0
responses1qname1r   r   r   __add__a   s   
zNXDOMAIN.__add__c                 C   
   | j d S )zXAll of the names that were tried.

        Returns a list of ``dns.name.Name``.
        r   r   r   r   r   r   r   m   s   
zNXDOMAIN.qnamesc                 C   r-   )zA 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   t      
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   propertyr    r,   r   r   r#   __classcell__r   r   r   r   r   1   s    

r   c                   @      e Zd ZdZdS )YXDOMAINz8The DNS query name is too long after DNAME substitution.Nr2   r3   r4   r5   r   r   r   r   r;          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.zDThe DNS response does not contain an answer to the question: {query}r#   c                    s   t  j|d jdS )Nr#   )query)r   _fmt_kwargsquestion)r   r   r   r   r   r@         zNoAnswer._fmt_kwargs)r2   r3   r4   r5   r7   r6   r@   r9   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 )
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              
      s\   g }|d D ]}| d|d |d rdnd|d |d  qt j|d	 jd
|dS )NrF   z Server {} {} port {} answered {}r   r   TCPUDP      rE   z; )r?   rF   )r&   r   r   r@   rA   r   )r   r   srv_msgserrr   r   r   r@      s   zNoNameservers._fmt_kwargs)	r2   r3   r4   r5   r   r7   r6   r@   r9   r   r   r   r   rC      s    rC   c                   @   r:   )NotAbsolutezEAn absolute domain name is required but a relative name was provided.Nr<   r   r   r   r   rM      r=   rM   c                   @   r:   )	NoRootSOAzBThere is no SOA RR at the DNS root name. This should never happen!Nr<   r   r   r   r   rN      r=   rN   c                   @   r:   )NoMetaqueriesz DNS metaqueries are not allowed.Nr<   r   r   r   r   rO      r=   rO   c                   @   r:   )NoResolverConfigurationzEResolver configuration could not be read or specified no nameservers.Nr<   r   r   r   r   rP      r=   rP   c                   @   sF   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.
    Nc                 C   sX   || _ || _|| _|| _|| _|| _| | _| jj| _| jj	| _
t | jj | _d S r1   )r"   rdtyperdclassr#   
nameserverportresolve_chainingchaining_resultr    answerrrsettimeminimum_ttl
expiration)r   r"   rR   rS   r#   rT   rU   r   r   r   __init__   s   


zAnswer.__init__c                 C   sX   |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|)NnamettlcoversrS   rR   )rY   r^   r_   r`   rS   rR   r   )r   attrr   r   r   __getattr__   s   zAnswer.__getattr__c                 C   s   | j rt| j p	dS Nr   )rY   r   r/   r   r   r   __len__   rB   zAnswer.__len__c                 C   s   | j rt| j ptt S r1   )rY   iterr   r/   r   r   r   __iter__   s   zAnswer.__iter__c                 C   s   | j d u rt| j | S r1   rY   
IndexErrorr   ir   r   r   __getitem__   s   

zAnswer.__getitem__c                 C   s   | j d u rt| j |= d S r1   rg   ri   r   r   r   __delitem__   s   
zAnswer.__delitem__)NN)
r2   r3   r4   r5   r]   rb   rd   rf   rk   rl   r   r   r   r   rQ      s    
rQ   c                   @   s*   e Zd ZdZd
ddZdd Zdd Zd	S )CacheStatisticszCache Statistics
    r   c                 C   s   || _ || _d S r1   hitsmisses)r   ro   rp   r   r   r   r]         
zCacheStatistics.__init__c                 C   s   d| _ d| _d S rc   rn   r/   r   r   r   reset  rq   zCacheStatistics.resetc                 C   s   t | j| jS r1   )rm   ro   rp   r/   r   r   r   clone  s   zCacheStatistics.cloneN)r   r   )r2   r3   r4   r5   r]   rr   rs   r   r   r   r   rm      s
    
rm   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )	CacheBasec                 C   s   t  | _t | _d S r1   )
_threadingLocklockrm   
statisticsr/   r   r   r   r]     s   
zCacheBase.__init__c                 C   s6   | j  | j  W d   dS 1 sw   Y  dS )zReset all statistics to zero.N)rw   rx   rr   r/   r   r   r   reset_statistics  s   "zCacheBase.reset_statisticsc                 C   2   | j  | jjW  d   S 1 sw   Y  dS )z How many hits has the cache had?N)rw   rx   ro   r/   r   r   r   ro        $zCacheBase.hitsc                 C   rz   )z"How many misses has the cache had?N)rw   rx   rp   r/   r   r   r   rp     r{   zCacheBase.missesc                 C   s4   | j  | j W  d   S 1 sw   Y  dS )zReturn a consistent snapshot of all the statistics.

        If running with multiple threads, it's better to take a
        snapshot than to call statistics methods such as hits() and
        misses() individually.
        N)rw   rx   rs   r/   r   r   r   get_statistics_snapshot  s   $z!CacheBase.get_statistics_snapshotN)r2   r3   r4   r]   ry   ro   rp   r|   r   r   r   r   rt     s    rt   c                       sD   e Zd ZdZd fdd	Zdd Zdd Zd	d
 ZdddZ  Z	S )Cachez$Simple thread-safe DNS answer cache.     r@c                    s*   t    i | _|| _t | j | _dS )zf*cleaning_interval*, a ``float`` is the number of seconds between
        periodic cleanings.
        N)r   r]   datacleaning_intervalrZ   next_cleaning)r   r   r   r   r   r]   -  s   
zCache.__init__c                 C   sl   t   }| j|kr4g }| j D ]\}}|j|kr|| q|D ]}| j|= q!t   }|| j | _dS dS )z&Clean the cache if it's time to do so.N)rZ   r   r   itemsr\   r&   r   )r   nowkeys_to_deletekvr   r   r   _maybe_clean7  s   



zCache._maybe_cleanc                 C   s   | j 8 |   | j|}|du s|jt kr*| j jd7  _	 W d   dS | j jd7  _|W  d   S 1 s>w   Y  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``.
        Nr   )	rw   r   r   r%   r\   rZ   rx   rp   ro   )r   keyr   r   r   r   r%   E  s   $z	Cache.getc                 C   s>   | j  |   || j|< W d   dS 1 sw   Y  dS 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)rw   r   r   r   r   valuer   r   r   putY  s   	"z	Cache.putNc                 C   sn   | j * |dur|| jv r| j|= ni | _t | j | _W d   dS W d   dS 1 s0w   Y  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)rw   r   rZ   r   r   )r   r   r   r   r   flushf  s   

" zCache.flush)r~   r1   )
r2   r3   r4   r5   r]   r   r%   r   r   r9   r   r   r   r   r}   *  s    
r}   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	LRUCacheNodezLRUCache node.c                 C   s"   || _ || _d| _| | _| | _d S rc   )r   r   ro   prevnextr   r   r   r   r]   |  s
   
zLRUCacheNode.__init__c                 C   s    || _ |j| _| |j_ | |_d S r1   r   r   )r   noder   r   r   
link_after  s   
zLRUCacheNode.link_afterc                 C   s   | j | j_ | j| j _d S r1   r   r/   r   r   r   unlink  s   
zLRUCacheNode.unlinkN)r2   r3   r4   r5   r]   r   r   r   r   r   r   r   y  s
    r   c                       sL   e Zd ZdZd fdd	Zdd Zdd Zd	d
 Zdd ZdddZ	  Z
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                    s>   t    i | _| | tdd| _| j| j_| j| j_dS )zl*max_size*, an ``int``, is the maximum number of nodes to cache;
        it must be greater than 0.
        N)r   r]   r   set_max_sizer   sentinelr   r   r   max_sizer   r   r   r]     s   


zLRUCache.__init__c                 C   s   |dk rd}|| _ d S )Nr   )r   r   r   r   r   r     s   
zLRUCache.set_max_sizec                 C   s   | j ] | j|}|du r| j jd7  _	 W d   dS |  |jjt krA| j|j	= | j jd7  _	 W d   dS |
| j | j jd7  _| jd7  _|jW  d   S 1 scw   Y  dS r   )rw   r   r%   rx   rp   r   r   r\   rZ   r   r   r   ro   r   r   r   r   r   r   r%     s"   
$zLRUCache.getc                 C   sf   | j & | j|}|du s|jjt kr	 W d   dS |jW  d   S 1 s,w   Y  dS )zBReturn the number of cache hits associated with the specified key.Nr   )rw   r   r%   r   r\   rZ   ro   r   r   r   r   get_hits_for_key  s   $zLRUCache.get_hits_for_keyc                 C   s   | j I | j|}|dur|  | j|j= t| j| jkr4| jj}|  | j|j= t| j| jkst	||}|
| j || j|< W d   dS 1 sOw   Y  dS r   )rw   r   r%   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r     s   	


"zLRUCache.putNc                 C   s   | j C |dur| j|}|dur|  | j|j= n"| jj}|| jkr3|j}|  |}|| jks%i | _W d   dS W d   dS 1 sIw   Y  dS r   )rw   r   r%   r   r   r   r   )r   r   r   r   r   r   r   r     s"   



" zLRUCache.flush)r   r1   )r2   r3   r4   r5   r]   r   r%   r   r   r   r9   r   r   r   r   r     s    		r   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )_Resolutiona  Helper class for dns.resolver.Resolver.resolve().

    All of the "business logic" of resolution is encapsulated in this
    class, allowing us to have multiple resolve() implementations
    using different I/O schemes without copying all of the
    complicated logic.

    This class is a "friend" to dns.resolver.Resolver and manipulates
    resolver data structures directly.
    c                 C   s   t |trtj|d }tjj|}tj|rt	tj
j|}tj
|r*t	|| _|||| _| jd d  | _|| _|| _|| _|| _i | _tjj| _g | _g | _g | _d | _d| _d| _d| _d | _d| _ d S )Nr   F)!r	   r   dnsr^   	from_text	rdatatype	RdataTypemakeis_metatyperO   
rdataclass
RdataClassis_metaclassresolver_get_qnames_to_tryqnames_to_tryr   rR   rS   tcpraise_on_no_answernxdomain_responsesemptyr"   nameserverscurrent_nameserversrF   rT   rU   tcp_attemptretry_with_tcprE   backoff)r   r   r"   rR   rS   r   r   searchr   r   r   r]     s4   


z_Resolution.__init__c                 C   s  t | jdkr| jd| _| jjrX| jj| j| j| jf}|dur5|j	du r1| j
r1t|jdd|fS | jj| jtjj| jf}|durX|j tjjkrX|j| j| j< q tj| j| j| j}| jjdurw|j| jj| jj| jjd || jj| jj| jj | jjdur| jj|_| jjdd | _| jjrt !| j | jdd | _"g | _#d| _$d| _%d| _&|| _'d| _(|dfS t| j)| jd)zGet the next request to send, and check the cache.

        Returns a (request, answer) tuple.  At most one of request or
        answer will not be None.
        r   Nr#   )	algorithmFg?r   )*r   r   popr"   r   cacher%   rR   rS   rY   r   r>   r#   r   r   ANYrcoder   r   message
make_querykeynameuse_tsigkeyringkeyalgorithmuse_ednsedns	ednsflagspayloadflagsr   rotaterandomshuffler   rF   rT   r   r   rE   r   r   )r   rX   rE   r   r   r   next_request'  sZ   

z_Resolution.next_requestc                 C   s   | j r| jd us
J d| _d| _ | j| jddfS d}| js@t| jdkr,t| j| j	d| jd d  | _| j
}t| j
d d| _
| jd| _| jj| j| jj| _| j| _| j| j| j|fS )NTFr   )rE   rF   rI   )r   rT   r   rU   r   r   r   rC   rE   rF   r   minr   r   nameserver_portsr%   r   )r   r   r   r   r   next_nameserverg  s$   z_Resolution.next_nameserverc                 C   sH  |rM|d u sJ | j | j| j| j||f t|tjjs,t|t	s,t|t
s,t|tr5| j| j dS t|tjjrK| jrH| j| j dS d| _dS |d usSJ | }|tjjkrzt| j| j| j|| j| j}W n ty~   | j| j Y dS w | jjr| jj| j| j| jf| |jd u r| jrt|jd|dfS |tjjkrzt| jtj j!tj"j#|}W n ty   | j| j Y dS w || j$| j< | jjr| jj| jtj j!| jf| dS |tjj%krt% }| j | j| j| j||f ||tjj&ks	| jj's| j| j | j | j| j| jtj(||f dS )NT)NFr   )NT))rF   r&   rT   r   rU   r	   r   	exception	FormErrorEOFErrorOSErrorNotImplementedErrorr   remover   	Truncatedr   r   NOERRORrQ   r"   rR   rS   r!   r   r   r   rY   r   r>   r#   r   r   r   r   INr   r;   SERVFAILretry_servfailto_text)r   r#   exr   rX   yexr   r   r   query_result}  s   z_Resolution.query_resultN)r2   r3   r4   r5   r]   r   r   r   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dd ZdejjfddZddejjfd d!Zd"d# Zed$d% Zejd&d% ZdS ))BaseResolverr   /etc/resolv.confTc                 C   s<   |    |rtjdkr|   dS |r| | dS dS dS )a  *filename*, a ``str`` 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.)
        r   N)rr   sysplatformread_registryread_resolv_conf)r   filename	configurer   r   r   r]     s   
zBaseResolver.__init__c                 C   s   t jt jt dd | _t| jdkrt jj| _g | _	i | _
d| _g | _d| _d| _d| _d| _d| _t jj| _d| _d| _d| _d| _d| _d| _d| _d| _dS )	z1Reset all resolver configuration to the defaults.r   Nr   5   Fg       @g      @rD   )r   r^   Namer   socketgethostnamedomainr   rootr   r   rU   r   use_search_by_defaulttimeoutlifetimer   r   tsigdefault_algorithmr   r   r   r   r   r   r   r   ndotsr/   r   r   r   rr     s,   


zBaseResolver.resetc                 C   s  t  }t|trz	|t|}W n	 ty   tw |D ]}t|dks3|d dks3|d dkr4q|	 }t|dk r?q|d dkrN| j
|d  q|d dkratj|d | _g | _q|d dkr~g | _|dd	 D ]}| jtj| qpq|d d
kr|dd	 D ]K}|dkrd| _q|dkr|   qd|v rzt|	dd | _W q ttfy   Y qw d|v rzt|	dd | _W q ttfy   Y qw qqW d	   n1 sw   Y  t| j
dkrtd	S )a  Process *f* as a file in the /etc/resolv.conf format.  If f is
        a ``str``, it is used as the name of the file to open; otherwise it
        is treated as the file itself.

        Interprets the following items:

        - nameserver - name server IP address

        - domain - local domain name

        - search - search list for host-name lookup

        - options - supported options are rotate, timeout, edns0, and ndots

        r   #;rI   rT   r   r   r   Noptionsr   Tedns0r   :r   )
contextlib	ExitStackr	   r   enter_contextopenr   rP   r   splitr   r&   r   r^   r   r   r   r   r   intr   
ValueErrorrh   r   )r   fstackltokenssuffixoptr   r   r   r     s`   

$
.zBaseResolver.read_resolv_confc                 C   s4   | ddkrd}|S | ddkrd}|S d}|S )N r   ,)find)r   entry
split_charr   r   r   _determine_split_char<  s   z"BaseResolver._determine_split_charc                 C   s@   t |}| |}||}|D ]}|| jvr| j| qd S r1   )r   r  r   r   r&   )r   r   r  ns_listnsr   r   r   _config_win32_nameserversK  s   


z&BaseResolver._config_win32_nameserversc                 C   s   t jt|| _d S r1   )r   r^   r   r   r   )r   r   r   r   r   _config_win32_domainT  s   z!BaseResolver._config_win32_domainc                 C   sH   t |}| |}||}|D ]}|| jvr!| jtj| qd S r1   )r   r  r   r   r&   r   r^   r   )r   r   r  search_listsr   r   r   _config_win32_searchX  s   


z!BaseResolver._config_win32_searchc                 C   s*  z
t |d\}}W n ty   d }Y nw |r| | |s!|r<zt |d\}}|r1| | W nA ty;   Y n9w z
t |d\}}W n tyQ   d }Y nw |rt| | zt |d\}}|ri| | W n	 tys   Y nw z
t |d\}}W n ty   d }Y nw |r| | d S d S )N
NameServerDomainDhcpNameServer
DhcpDomain
SearchList)winregQueryValueExWindowsErrorr  r  r  )r   r   always_try_domainservers_domr   r   r   r   _config_win32_fromkeya  sN   



z"BaseResolver._config_win32_fromkeyc                 C   s   t dt j}zlt |d}z| |d W |  n|  w t |d}zAd}	 z-t ||}|d7 }t ||}| |||sDW q)z| |d W |  n|  w W n	 ty`   Y nw q*W |  n|  w W |  dS |  w )z9Extract resolver configuration from the Windows registry.Nz2SYSTEM\CurrentControlSet\Services\Tcpip\ParametersTz=SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfacesr   r   F)	r  ConnectRegistryHKEY_LOCAL_MACHINEOpenKeyr  CloseEnumKey_win32_is_nic_enabledEnvironmentError)r   lm
tcp_params
interfacesrj   guidr   r   r   r   r     s<   zBaseResolver.read_registryc           	      C   s   zJt |d| }z<t |d\}}|t jkrtt |d| }zt |d\}}|t jkr1t|d@  W |  W |  W S |  w |  w  tyT   Y dS w )Nz]SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\%s\ConnectionPnpInstanceIDz SYSTEM\CurrentControlSet\Enum\%sConfigFlagsr   F)r  r  r  REG_SZr   	REG_DWORDr   r!   )	r   r$  r'  r  connection_keypnp_idttype
device_keyr   r   r   r   r"    s<   





z"BaseResolver._win32_is_nic_enabledNc                 C   sb   |d u r| j n|}t }|| }|dk r |dk rt|d|}||kr)t|dt|| | jS )Nr   rD   r   )r   rZ   Timeoutr   r   )r   startr   r   durationr   r   r   _compute_timeout  s   

zBaseResolver._compute_timeoutc                 C   s   |d u r| j }g }| r|| |S |tjj}|rkt| jdkr,| jd d  }n| j	tjjkr=| j	d ur=| j	g}ng }| j
d u rGd}n| j
}|D ]	}|||  qLt||krd|d| |S || |S || |S )Nr   r   )r   is_absoluter&   concatenater   r^   r   r   r   r   r   insert)r   r"   r   r   	abs_qnamer  r   r  r   r   r   r     s2   




zBaseResolver._get_qnames_to_tryc                 C   s   || _ || _|| _dS )zAdd a TSIG signature to each query.

        The parameters are passed to ``dns.message.Message.use_tsig()``;
        see its documentation for details.
        N)r   r   r   )r   r   r   r   r   r   r   r     s   
zBaseResolver.use_tsigr   c                 C   s8   |du s|du rd}n|du 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.
        NFrD   Tr   )r   r   r   )r   r   r   r   r   r   r   r     s   
zBaseResolver.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_flags3  r0   zBaseResolver.set_flagsc                 C   s   | j S r1   )_nameserversr/   r   r   r   r   ;  s   zBaseResolver.nameserversc                 C   s&   t |tr
|| _dS tdt|)z
        *nameservers*, a ``list`` of nameservers.

        Raises ``ValueError`` if *nameservers* is anything other than a
        ``list``.
        z%nameservers must be a list (not a {})N)r	   r
   r:  r   r   type)r   r   r   r   r   r   ?  s
   


)r   Tr1   )r2   r3   r4   r5   r]   rr   r   r  r  r  r  r  r   r"  r4  r   r   r   r   r   r   DEFAULT_EDNS_PAYLOADr   r9  r8   r   setterr   r   r   r   r     s2    
B		$%
-'


r   c                   @   sb   e Zd ZdZejjejjddddddfddZ	ejjejjdddddfdd	Z
d
d Zdd ZdS )Resolverr   FNTr   c
              
   C   s2  t | ||||||	}
t }	 |
 \}}|dur|S d}|s|
 \}}}}|r.t| | ||}z9tj|rX|rJtj	j
||||||d}n"tj	j||||||dd}nt|j}|dkrcttj	j|||d}W n ty } z|
d|\}}W Y d}~qd}~ww |
|d\}}|dur|S |rq)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 ``str``, the query name.

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

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

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

        *source*, a ``str`` or ``None``.  If not ``None``, bind to this IP
        address when making queries.

        *raise_on_no_answer*, a ``bool``.  If ``True``, raise
        ``dns.resolver.NoAnswer`` if there's no answer to the question.

        *source_port*, an ``int``, the port from which to send the message.

        *lifetime*, a ``float``, how many seconds a query should run
         before timing out.

        *search*, a ``bool`` or ``None``, determines whether the
        search list configured in the system's resolver configuration
        are used for relative names, and whether the resolver's domain
        may be added to relative names.  The default is ``None``,
        which causes the value of the resolver's
        ``use_search_by_default`` attribute to be used.

        Raises ``dns.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.

        TNF)r   rU   sourcesource_port)r   rU   r?  r@  raise_on_truncationhttpsr0  )r   rZ   r   r   sleepr4  r   inet
is_addressr?   r   udpr   schemer   rB  r!   r   )r   r"   rR   rS   r   r?  r   r@  r   r   
resolutionr2  rE   rX   donerT   rU   r   r   r#   protocolr   r  r   r   r   resolveQ  s`   6



zResolver.resolvec	           	      C   s*   t jdtdd | ||||||||d	S )a#  Query nameservers to find the answer to the question.

        This method calls resolve() with ``search=True``, and is
        provided for backwards compatbility with prior versions of
        dnspython.  See the documentation for the resolve() method for
        further details.
        z2please use dns.resolver.Resolver.resolve() insteadrI   
stacklevelTwarningswarnDeprecationWarningrK  )	r   r"   rR   rS   r   r?  r   r@  r   r   r   r   r?     s   
zResolver.queryc                 O   s.   | j tj|g|R tjjtjjd|S )a  Use a resolver to run a reverse query for PTR records.

        This utilizes the resolve() method to perform a PTR lookup on the
        specified IP address.

        *ipaddr*, a ``str``, the IPv4 or IPv6 address you want to get
        the PTR record for.

        All other arguments that can be passed to the resolve() function
        except for rdtype and rdclass are also supported by this
        function.
        )rR   rS   )rK  r   reversenamefrom_addressr   PTRr   r   )r   ipaddrargsr   r   r   r   resolve_address  s   zResolver.resolve_addressc              
   C   sL   z| j |dd}|j}W |S  tjjy% } z
|j}W Y d}~|S d}~ww )a  Determine the canonical name of *name*.

        The canonical name is the name the resolver uses for queries
        after all CNAME and DNAME renamings have been applied.

        *name*, a ``dns.name.Name`` or ``str``, the query name.

        This method can raise any exception that ``resolve()`` can
        raise, other than ``dns.resolver.NoAnswer`` and
        ``dns.resolver.NXDOMAIN``.

        Returns a ``dns.name.Name``.
        Fr   N)rK  r    r   r   r   )r   r^   rX   r    er   r   r   r      s   zResolver.canonical_name)r2   r3   r4   r5   r   r   Ar   r   rK  r?   rW  r    r   r   r   r   r>  N  s    
i
r>  c                   C   s   t du 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 )zRe-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\    s   
r\  FTc	           	      C   s   t  | ||||||||	S )zQuery nameservers to find the answer to the question.

    This is a convenience function that uses the default resolver
    object to make the query.

    See ``dns.resolver.Resolver.resolve`` for more information on the
    parameters.
    )r]  rK  )	r"   rR   rS   r   r?  r   r@  r   r   r   r   r   rK    s   rK  c              
   C   s(   t jdtdd t| |||||||d	S )a  Query nameservers to find the answer to the question.

    This method calls resolve() with ``search=True``, and is
    provided for backwards compatbility with prior versions of
    dnspython.  See the documentation for the resolve() method for
    further details.
    z)please use dns.resolver.resolve() insteadrI   rL  TrN  )r"   rR   rS   r   r?  r   r@  r   r   r   r   r?     s   
r?   c                 O   s   t  j| g|R i |S )zUse a resolver to run a reverse query for PTR records.

    See ``dns.resolver.Resolver.resolve_address`` for more information on the
    parameters.
    )r]  rW  )rU  rV  r   r   r   r   rW  .  s   rW  c                 C   s   t  | S )zDetermine the canonical name of *name*.

    See ``dns.resolver.Resolver.canonical_name`` for more information on the
    parameters and possible exceptions.
    )r]  r    r^   r   r   r   r    8  s   r    c              	   C   s   t | trtj| tjj} |du rt }|  st| 	 z|	| tj
j||}|jj| kr2| W S W n tjjtjjfyB   Y nw z|  } W n tjjyT   tw q)ab  Find the name of the zone which contains the specified name.

    *name*, an absolute ``dns.name.Name`` or ``str``, the query name.

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

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

    *resolver*, a ``dns.resolver.Resolver`` or ``None``, the resolver to use.
    If ``None``, the default 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]  r5  rM   rK  r   SOArY   r   r   r>   parentNoParentrN   )r^   rS   r   r   rX   r   r   r   zone_for_nameB  s*   
rb  c                 C   sF  |t j@ dkrt| |||||S |t jt jB @ dkr!t t jd| d u r0|d u r0t t jdg }g }d }| d u rCt| |||||S ztj	
|  t| |||||W S  ty\   Y nw z^|t jksh|t jkrtj| tjjdd}	|	j} |	jd}|	jd ur|	jD ]}
||
j q|t jks|t jkrtj| tjjdd}|j} |jd}|jd ur|jD ]}
||
j qW n tjjy   t t jd ty   t t jdw d }z|d u rd}nt|}W n$ ty   |t j@ dkr
zt |}W n
 ty	   Y nw Y nw |d u rt t jdg }|dkr't j t j!g}n|g}|t j"@ dkr5|}nd}|t jksC|t jkrf|D ] }|D ]}t#| D ]}|t j|||||ddff qOqIqE|t jksr|t jkr|D ]}|D ]}t#| D ]}|t j|||||ff q~qxqtt$|dkrt t jd|S )	Nr   z*Non-recoverable failure in name resolutionName or service not knownFrX  Tz$Temporary failure in name resolution )%r   AI_NUMERICHOST_original_getaddrinfoAI_ADDRCONFIGAI_V4MAPPEDgaierrorEAI_FAIL
EAI_NONAMEr   rD  af_for_addressr!   AF_INET6	AF_UNSPEC	_resolverrK  r   AAAAr"   r    r   rY   r&   addressAF_INETrZ  r   r   	EAI_AGAINr   AI_NUMERICSERVgetservbyname
SOCK_DGRAMSOCK_STREAMAI_CANONNAME_protocols_for_socktyper   )hostservicefamilysocktypeprotor   v6addrsv4addrsr    v6rdatav4rU   tuples	socktypesr$   addrr   r   r   _getaddrinfo{  s   	









r  c              	   C   s`  | d }| d }t | dkr| d }tj}nd }tj}t|||tjtjd}t |dkr1td|d d d }|tj@ rAd}nd}t	j
|}	|tj@ dkrzt|	d}
|
jd jd	}W n7 t	jjt	jjfy   |tj@ rzttjd
|}|d ur|dt| 7 }Y nw |}|d ur|dt| 7 }|tj@ rt|}||fS t||}||fS )Nr   r      rJ   z'sockaddr resolved to multiple addressesrF  r   rT  Trc  %)r   r   rm  rr  r  rw  SOL_TCPerrorNI_DGRAMr   rR  rS  NI_NUMERICHOSTro  rK  rY   targetr   r   r   r>   NI_NAMEREQDri  rk  r   NI_NUMERICSERVgetservbyport)sockaddrr   rz  rU   scoper|  r  r  pnamer"   rX   hostnamer{  r   r   r   _getnameinfo  sN   



r  c                 C   s:   | d u rt  } z
t| \} }}W | S  ty   Y | S w r1   )r   r   _gethostbyaddrr!   )r^   r  r   r   r   _getfqdn  s   r  c                 C   s   t | d d S )NrI   r   )_gethostbyname_exr^  r   r   r   _gethostbyname  s   r  c                 C   sT   g }g }t | dtjtjtjtj}|d d }|D ]}||d d  q|||fS )Nr   rJ   r  )r  r   rr  rw  r  rx  r&   )r^   aliases	addressesr  	canonicalitemr   r   r   r  !  s   
r  c                 C   s   zt j|  | dddf}tj}W n' ty8   zt j|  W n ty.   ttjdw | df}tj	}Y nw t
|tj\}}g }g }t|d|tjtjtj}|d d }t j|| }	|D ]}
|
d d }t j||}|	|krx|| q`|||fS )NP   r   rc  rJ   r  )r   ipv6	inet_atonr   rm  r!   ipv4ri  rk  rr  r  r  r  rw  r  rx  rD  	inet_ptonr&   )ipr  r|  r^   r  r  r  r  r  bin_ipr  r  bin_addrr   r   r   r  -  s<   



r  c                 C   s:   | du 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]  ro  r  r   getaddrinfor  getnameinfor  getfqdnr  gethostbynamer  gethostbyname_exr  gethostbyaddr)r   r   r   r   override_system_resolverM  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)ro  rf  r   r  _original_getnameinfor  _original_getfqdnr  _original_gethostbynamer  _original_gethostbyname_exr  _original_gethostbyaddrr  r   r   r   r   restore_system_resolverg  s   
r  )r   r1   )Xr5   urllib.parser   r   r   r   rZ   r   rO  	threadingru   ImportErrordummy_threadingdns.exceptionr   	dns.flagsdns.inetdns.ipv4dns.ipv6dns.messagedns.name	dns.query	dns.rcodedns.rdataclassdns.rdatatypedns.reversenamedns.tsigr   r  r   DNSExceptionr   r;   r1  r>   rC   rM   rN   rO   rP   rQ   rm   rt   r}   r   r   r   r   r>  r[  r]  r\  r   rZ  r   r   rK  r?   rW  r    rb  rv  SOL_UDPrw  r  ry  ro  r  rf  r  r  r  r  r  r  r  r  r  r  rn  r  r  r  r  r  r  r  r  r   r   r   r   <module>   s   
S
>On K    +




,



m
)
 