o
    tPf2                     @   s6  d Z ddlmZmZmZ ddlZddlZddlmZ ddlm	Z	 ddlm
Z
 ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZmZ ddl m!Z" ddl m#Z# dgZ$G dd de#Z%dS )z!
c-ares based hostname resolver.
    )absolute_importprint_functiondivisionN)gaierror)herror)error)
EAI_NONAME)	text_type)integer_types)Waiter)get_hub)	AF_UNSPEC)AF_INET)AF_INET6)
SOCK_DGRAM)SOCK_STREAM)SOL_TCP)SOL_UDP)config)AresSettingMixin   )channel	InvalidIP)_lookup_port)AbstractResolverResolverc                   @   s   e Zd ZdZe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dZdd Zdd Zdd Zdd Zdd ZdS )!r   a  
    Implementation of the resolver API using the `c-ares`_ library.

    This implementation uses the c-ares library to handle name
    resolution. c-ares is natively asynchronous at the socket level
    and so integrates well into gevent's event loop.

    In comparison to :class:`gevent.resolver_thread.Resolver` (which
    delegates to the native system resolver), the implementation is
    much more complex. In addition, there have been reports of it not
    properly honoring certain system configurations (for example, the
    order in which IPv4 and IPv6 results are returned may not match
    the threaded resolver). However, because it does not use threads,
    it may scale better for applications that make many lookups.

    There are some known differences from the system resolver.

    - ``gethostbyname_ex`` and ``gethostbyaddr`` may return
      different for the ``aliaslist`` tuple member. (Sometimes the
      same, sometimes in a different order, sometimes a different
      alias altogether.)

    - ``gethostbyname_ex`` may return the ``ipaddrlist`` in a
      different order.

    - ``getaddrinfo`` does not return ``SOCK_RAW`` results.

    - ``getaddrinfo`` may return results in a different order.

    - Handling of ``.local`` (mDNS) names may be different, even
      if they are listed in the hosts file.

    - c-ares will not resolve ``broadcasthost``, even if listed in
      the hosts file prior to 2020-04-30.

    - This implementation may raise ``gaierror(4)`` where the
      system implementation would raise ``herror(1)`` or vice versa,
      with different error numbers. However, after 2020-04-30, this should be
      much reduced.

    - The results for ``localhost`` may be different. In
      particular, some system resolvers will return more results
      from ``getaddrinfo`` than c-ares does, such as SOCK_DGRAM
      results, and c-ares may report more ips on a multi-homed
      host.

    - The system implementation may return some names fully qualified, where
      this implementation returns only the host name. This appears to be
      the case only with entries found in ``/etc/hosts``.

    - c-ares supports a limited set of flags for ``getnameinfo`` and
      ``getaddrinfo``; unknown flags are ignored. System-specific flags
      such as ``AI_V4MAPPED_CFG`` are not supported.

    - ``getaddrinfo`` may return canonical names even without the ``AI_CANONNAME``
      being set.

    - ``getaddrinfo`` does not appear to support IPv6 symbolic scope IDs.

    .. caution::

        This module is considered extremely experimental on PyPy, and
        due to its implementation in cython, it may be slower. It may also lead to
        interpreter crashes.

    .. versionchanged:: 1.5.0
       This version of gevent typically embeds c-ares 1.15.0 or newer. In
       that version of c-ares, domains ending in ``.onion`` `are never
       resolved <https://github.com/c-ares/c-ares/issues/196>`_ or even
       sent to the DNS server.

    .. versionchanged:: 20.5.0
       ``getaddrinfo`` is now implemented using the native c-ares function
       from c-ares 1.16 or newer.

    .. versionchanged:: 20.5.0
       Now ``herror`` and ``gaierror`` are raised more consistently with
       the standard library resolver, and have more consistent errno values.

       Handling of localhost and broadcast names is now more consistent.

    .. versionchanged:: 22.10.1
       Now has a ``__del__`` method that warns if the object is destroyed
       without being properly closed.

    .. _c-ares: http://c-ares.haxx.se
    NTc                 K   s   t |  |d u rt }|| _|r-tj D ]}t|tr,|	 }|d ur,|
|j| q| j|jfi || _t | _|| _|jjdd| _| j| j d S )NF)ref)r   __init__r   hubr   settingsvalues
isinstancer   get
setdefault
kwarg_namecares_classloopcaresosgetpidpidparamsforkfork_watcherstart_on_fork)selfr   use_environkwargssettingvalue r5   U/var/www/html/humari/django-venv/lib/python3.10/site-packages/gevent/resolver/ares.pyr      s    


zResolver.__init__c                 C   s   dt | | jf S )Nz/<gevent.resolver_ares.Resolver at 0x%x ares=%r>)idr'   r0   r5   r5   r6   __repr__   s   zResolver.__repr__c                 C   sL   t  }|| jkr$| jj| jj | j| jjfi | j	| _|| _d S d S N)
r(   r)   r*   r   r&   run_callbackr'   destroyr%   r+   )r0   r*   r5   r5   r6   r/      s   

zResolver._on_forkc                 C   s:   t |  | jd ur| jj| jj d | _| j  d S r:   )	r   closer'   r   r&   r;   r<   r-   stopr8   r5   r5   r6   r=      s
   

zResolver.closec                 C   s&   | j d urtdt |   d S d S )Nz)cares Resolver destroyed while not closed)r'   warningswarnResourceWarningr=   r8   r5   r5   r6   __del__   s   
zResolver.__del__c              
   C   s   	 | j }zt| j}|||| | }|d stt| j|W S  tyF } z|| j u r<|jd dkr;t	t| j W Y d }~nd }~ww q)NTr   r   )
r'   r   r   gethostbynamer"   r   r   EAI_NONAME_MSGargsr   )r0   hostname_bytesfamilyareswaiterresultexr5   r5   r6   _gethostbyname_ex   s$   

zResolver._gethostbyname_exc                 C   s
   t ||S r:   )lookup_port)r0   portsocktyper5   r5   r6   r      s   
zResolver._lookup_portr   c           
   	      s   t |tr
|d}t |tr|d}nt |tr(|dkr!d}nt|d}t| j}| j||||||| |	 }	|	sEt
t| j|rt|rU||tkrPtntfg n|rc|tkr]tnt|fg nttfttfg  fdd|	D }	|	S )z
        Returns a list ``(family, socktype, proto, canonname, sockaddr)``

        :raises gaierror: If no results are found.
        idnaasciir   Nc                    sB   g | ]\}}}}} D ]\}}||s|n||s|n|||fqqS r5   r5   ).0rfamilyrtyperprotorcanonraddr	hard_type
hard_protohard_type_protor5   r6   
<listcomp>  s    

z*Resolver.__getaddrinfo.<locals>.<listcomp>)r!   r	   encoder
   strr   r   r'   getaddrinfor"   r   r   rE   r   r   r   r   )
r0   hostrO   rH   rP   protoflagsfill_in_type_protorJ   rK   r5   r[   r6   __getaddrinfo   sF   





zResolver.__getaddrinfoc                 C   s@   	 | j }z| ||||||W S  ty   || j u r Y nw qr:   )r'   _Resolver__getaddrinfor   )r0   
host_bytesrO   rH   rP   rb   rc   rI   r5   r5   r6   _getaddrinfo  s   
zResolver._getaddrinfoc                 C   s   t | j}z| j|| | W S  tyN   | j|d ttddd}|s& |d d d }t	|t
r8|d}||kr= |  | j|| |  Y S w )Nr   )rH   rP   rb   rc   rC   rR   )r   r   r'   gethostbyaddrr"   r   rh   r   r   r!   r	   r^   clear)r0   
ip_addressrJ   rK   _ip_addressr5   r5   r6   __gethostbyaddr  s(   



zResolver.__gethostbyaddrc                 C   s6   	 | j }z| |W S  ty   || j u r Y nw qr:   )r'   _Resolver__gethostbyaddrr   )r0   ip_address_bytesrI   r5   r5   r6   _gethostbyaddr/  s   
zResolver._gethostbyaddrc              	   C   s   | j ||ttdddd}t|dkrtd|d \}}}}	}
|tkr.t|dkr-tdn|tkr>|
d d |dd   }
t| j}| j	
||
| | \}}|d u r`tt| j}t|_|||pddfS )	Nr   F)rH   rP   rb   rc   rd   r   z'sockaddr resolved to multiple addresses   zIPv4 sockaddr must be 2 tuple0)rf   r   r   lenr   r   r   r   r   r'   getnameinfor"   r   r   rE   errno)r0   hostnamerO   sockaddrrc   rK   rH   	_socktype_proto_nameaddressrJ   nodeserviceerrr5   r5   r6   __getnameinfo8  s.   
zResolver.__getnameinfoc                 C   s<   	 | j }z	| ||||W S  ty   || j u r Y nw qr:   )r'   _Resolver__getnameinfor   )r0   address_bytesrO   rw   rc   rI   r5   r5   r6   _getnameinfoY  s   
zResolver._getnameinfo)NT)r   r   r   r   T)__name__
__module____qualname____doc__r   r%   r   r9   r/   r=   rB   rM   r   rf   rh   rn   rp   r   r   r5   r5   r5   r6   r   '   s$    X

N		!)&r   
__future__r   r   r   r(   r?   _socketr   r   r   r   gevent._compatr	   r
   
gevent.hubr   r   gevent.socketr   r   r   r   r   r   r   gevent._configr   r   r'   r   r    r   rN   r   __all__r   r5   r5   r5   r6   <module>   s4   