U
    ¸~HjŽ$  ã                .   @   s*  d Z ddlZddlmZ ddlmZmZmZ ddlm	Z	 ddl
mZ ddlmZmZmZmZmZmZ edƒ\ZZZd	ZeejƒG d
d„ dƒƒZd&dd„Zejdejdejdejdejdej dej!dej"dej#dej$dej%dej&dej'dej(dej)dej*dej+dej,d ej-d!ej.d"ej/d#ej0d$ej1d%iZ2dS )'z>
Base functionality useful to various parts of Twisted Names.
é    N)Úimplementer)ÚdeferÚerrorÚ
interfaces)ÚLogger)Údns)ÚDNSFormatErrorÚDNSNameErrorÚDNSNotImplementedErrorÚDNSQueryRefusedErrorÚDNSServerErrorÚDNSUnknownErroré   )© r   r   c                
   @   sV  e Zd ZdZeƒ Zejeej	e
ejeejeejeiZdZdd„ Zdd„ Zd>dd„Zd	d
„ Zd?dd„Zd@dd„ZdAdd„ZdBdd„ZdCdd„ZdDdd„ZdEdd„ZdFdd„ZdGdd„ZdHdd„Z dIdd „Z!dJd!d"„Z"dKd#d$„Z#dLd%d&„Z$dMd'd(„Z%dNd)d*„Z&dOd+d,„Z'dPd-d.„Z(dQd/d0„Z)dRd1d2„Z*dSd3d4„Z+dTd5d6„Z,dUd7d8„Z-dVd:d;„Z.d<d=„ Z/dS )WÚResolverBaseaN  
    L{ResolverBase} is a base class for implementations of
    L{interfaces.IResolver} which deals with a lot
    of the boilerplate of implementing all of the lookup methods.

    @cvar _errormap: A C{dict} mapping DNS protocol failure response codes
        to exception classes which will be used to represent those failures.
    Nc                 C   s,   i | _ t  ¡ D ]\}}t| |ƒ| j |< qd S ©N)ÚtypeToMethodÚitemsÚgetattr)ÚselfÚkÚvr   r   ú8/tmp/pip-unpacked-wheel-ewod1tgb/twisted/names/common.pyÚ__init__7   s    zResolverBase.__init__c                 C   s   | j  |t¡S )z¥
        Convert a response code (one of the possible values of
        L{dns.Message.rCode} to an exception instance representing it.

        @since: 10.0
        )Ú	_errormapÚgetr   )r   ZresponseCoder   r   r   ÚexceptionForCode<   s    zResolverBase.exceptionForCodec              
   C   sh   z| j |j }W n@ tk
rP   | jjd|d� t | j|jjt	j
|j|¡ Y S X t ||jj|¡S d S )Nz:Query of unknown type {query.type} for {query.name.name!r})Úquery)r   ÚtypeÚKeyErrorÚ_logÚdebugr   ZmaybeDeferredÚ_lookupÚnamer   ÚIN)r   r   ÚtimeoutÚmethodr   r   r   r   E   s    þ    ÿ
zResolverBase.queryc                 C   s   t  tdƒ¡S )NúResolverBase._lookup)r   ZfailÚNotImplementedError)r   r#   Úclsr   r%   r   r   r   r"   S   s    r'   c                 C   s   |   t |¡tjtj|¡S r   )r"   r   ÚdomainStringr$   ÚA©r   r#   r%   r   r   r   ÚlookupAddressV   s    zResolverBase.lookupAddressc                 C   s   |   t |¡tjtj|¡S r   )r"   r   r*   r$   ÚAAAAr,   r   r   r   ÚlookupIPV6AddressY   s    zResolverBase.lookupIPV6Addressc                 C   s   |   t |¡tjtj|¡S r   )r"   r   r*   r$   ÚA6r,   r   r   r   ÚlookupAddress6\   s    zResolverBase.lookupAddress6c                 C   s   |   t |¡tjtj|¡S r   )r"   r   r*   r$   ÚMXr,   r   r   r   ÚlookupMailExchange_   s    zResolverBase.lookupMailExchangec                 C   s   |   t |¡tjtj|¡S r   )r"   r   r*   r$   ÚNSr,   r   r   r   ÚlookupNameserversb   s    zResolverBase.lookupNameserversc                 C   s   |   t |¡tjtj|¡S r   )r"   r   r*   r$   ÚCNAMEr,   r   r   r   ÚlookupCanonicalNamee   s    z ResolverBase.lookupCanonicalNamec                 C   s   |   t |¡tjtj|¡S r   )r"   r   r*   r$   ÚMBr,   r   r   r   ÚlookupMailBoxh   s    zResolverBase.lookupMailBoxc                 C   s   |   t |¡tjtj|¡S r   )r"   r   r*   r$   ÚMGr,   r   r   r   ÚlookupMailGroupk   s    zResolverBase.lookupMailGroupc                 C   s   |   t |¡tjtj|¡S r   )r"   r   r*   r$   ÚMRr,   r   r   r   ÚlookupMailRenamen   s    zResolverBase.lookupMailRenamec                 C   s   |   t |¡tjtj|¡S r   )r"   r   r*   r$   ÚPTRr,   r   r   r   ÚlookupPointerq   s    zResolverBase.lookupPointerc                 C   s   |   t |¡tjtj|¡S r   )r"   r   r*   r$   ÚSOAr,   r   r   r   ÚlookupAuthorityt   s    zResolverBase.lookupAuthorityc                 C   s   |   t |¡tjtj|¡S r   )r"   r   r*   r$   ÚNULLr,   r   r   r   Ú
lookupNullw   s    zResolverBase.lookupNullc                 C   s   |   t |¡tjtj|¡S r   )r"   r   r*   r$   ÚWKSr,   r   r   r   ÚlookupWellKnownServicesz   s    z$ResolverBase.lookupWellKnownServicesc                 C   s   |   t |¡tjtj|¡S r   )r"   r   r*   r$   ÚSRVr,   r   r   r   ÚlookupService}   s    zResolverBase.lookupServicec                 C   s   |   t |¡tjtj|¡S r   )r"   r   r*   r$   ÚHINFOr,   r   r   r   ÚlookupHostInfo€   s    zResolverBase.lookupHostInfoc                 C   s   |   t |¡tjtj|¡S r   )r"   r   r*   r$   ÚMINFOr,   r   r   r   ÚlookupMailboxInfoƒ   s    zResolverBase.lookupMailboxInfoc                 C   s   |   t |¡tjtj|¡S r   )r"   r   r*   r$   ÚTXTr,   r   r   r   Ú
lookupText†   s    zResolverBase.lookupTextc                 C   s   |   t |¡tjtj|¡S r   )r"   r   r*   r$   ÚSPFr,   r   r   r   ÚlookupSenderPolicy‰   s    zResolverBase.lookupSenderPolicyc                 C   s   |   t |¡tjtj|¡S r   )r"   r   r*   r$   ÚRPr,   r   r   r   ÚlookupResponsibilityŒ   s    z!ResolverBase.lookupResponsibilityc                 C   s   |   t |¡tjtj|¡S r   )r"   r   r*   r$   ÚAFSDBr,   r   r   r   ÚlookupAFSDatabase�   s    zResolverBase.lookupAFSDatabasec                 C   s   |   t |¡tjtj|¡S r   )r"   r   r*   r$   ÚAXFRr,   r   r   r   Ú
lookupZone’   s    zResolverBase.lookupZonec                 C   s   |   t |¡tjtj|¡S r   )r"   r   r*   r$   ÚNAPTRr,   r   r   r   ÚlookupNamingAuthorityPointer•   s    z)ResolverBase.lookupNamingAuthorityPointerc                 C   s   |   t |¡tjtj|¡S r   )r"   r   r*   r$   ÚALL_RECORDSr,   r   r   r   ÚlookupAllRecords˜   s    zResolverBase.lookupAllRecordsé
   c                 C   s*   t  |¡}|  ||¡}| | j||¡ |S r   )r   r*   rY   ÚaddCallbackÚ
_cbRecords)r   r#   r%   ÚeffortÚdr   r   r   ÚgetHostByNameœ   s    
zResolverBase.getHostByNamec                 C   s8   |\}}}t | t |¡|| | |ƒ}|s4t |¡‚|S r   )ÚextractRecordr   ÚNamer   ZDNSLookupError)r   Úrecordsr#   r]   ÚansÚauthÚaddÚresultr   r   r   r\   ¥   s
    

zResolverBase._cbRecords)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)N)NrZ   )0Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r    r   ZEFORMATr   ZESERVERr   ZENAMEr	   ZENOTIMPr
   ZEREFUSEDr   r   r   r   r   r   r"   r-   r/   r1   r3   r5   r7   r9   r;   r=   r?   rA   rC   rE   rG   rI   rK   rM   rO   rQ   rS   rU   rW   rY   r_   r\   r   r   r   r   r   !   sV   	     û	
























	r   rZ   c                    s„  ˆ sdS t tdƒr~|D ]0}|jˆkr|jtjkrt tj|jj	¡  S q|D ]0}|jˆkrL|jtj
krLt tj|jj	¡  S qL|D ]0}|jˆkr‚|jtjkr‚t tj|jj	¡  S q‚|D ]Z}|jˆkr¸|jtjkr¸t| |jj|ˆ d ƒ}|�s
| j|jjjˆ d d�  S |  S q¸|D ]f}|jtjk�r.�qddlm} |j|jjj d¡tjfgd�‰‡ ‡‡fd	d
„}ˆ ˆj¡ |¡  S dS )am  
    Resolve a name to an IP address, following I{CNAME} records and I{NS}
    referrals recursively.

    This is an implementation detail of L{ResolverBase.getHostByName}.

    @param resolver: The resolver to use for the next query (unless handling
    an I{NS} referral).
    @type resolver: L{IResolver}

    @param name: The name being looked up.
    @type name: L{dns.Name}

    @param answers: All of the records returned by the previous query (answers,
    authority, and additional concatenated).
    @type answers: L{list} of L{dns.RRHeader}

    @param level: Remaining recursion budget. This is decremented at each
    recursion. The query returns L{None} when it reaches 0.
    @type level: L{int}

    @returns: The first IPv4 or IPv6 address (as a dotted quad or colon
    quibbles), or L{None} when no result is found.
    @rtype: native L{str} or L{None}
    NÚ	inet_ntopé   )r]   r   )ÚclientÚascii)Zserversc                    s$   | \}}}t ˆˆ|| | ˆ d ƒS )Nrl   )r`   )rb   rc   rd   re   ©Úlevelr#   Z
nsResolverr   r   Ú
queryAgainè   s    
z!extractRecord.<locals>.queryAgain)ÚhasattrÚsocketr#   r   r   r0   rk   ÚAF_INET6ÚpayloadÚaddressr.   r+   ÚAF_INETr6   r`   r_   r4   Útwisted.namesrm   ÚResolverÚdecodeZPORTr-   r[   )Úresolverr#   Zanswersrp   Úrrf   rm   rq   r   ro   r   r`   ­   s8    

ÿÿr`   r-   r/   r1   r5   r7   rA   r9   r;   r=   rC   rE   r?   rI   rK   r3   rM   rO   rQ   rS   rG   rW   rU   rY   )rZ   )3rj   rs   Zzope.interfacer   Ztwisted.internetr   r   r   Ztwisted.loggerr   rx   r   Ztwisted.names.errorr   r	   r
   r   r   r   ÚrangeZ_ANSZ_AUTHZ_ADDZEMPTY_RESULTZ	IResolverr   r`   r+   r.   r0   r4   r6   r@   r8   r:   r<   rB   rD   r>   rH   rJ   r2   rL   rN   rP   rR   rF   rV   rT   rX   r   r   r   r   r   Ú<module>   sv     
C                       é