o
    ¯b�$  ã                   @   sP  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i 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d>dd„Zd>dd„Zd>dd„Zd>dd„Zd>dd„Zd>dd„Zd>dd„Zd>dd„Z d>dd „Z!d>d!d"„Z"d>d#d$„Z#d>d%d&„Z$d>d'd(„Z%d>d)d*„Z&d>d+d,„Z'd>d-d.„Z(d>d/d0„Z)d>d1d2„Z*d>d3d4„Z+d>d5d6„Z,d>d7d8„Z-d?d:d;„Z.d<d=„ Z/dS )@Ú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   ú6/usr/lib/python3/dist-packages/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   ÚresponseCoder   r   r   ÚexceptionForCode<   s   zResolverBase.exceptionForCodec              
   C   sb   z| j |j }W n ty'   | jjd|d� t | j|jjt	j
|j|¡ Y S w t ||jj|¡S )Nz:Query of unknown type {query.type} for {query.name.name!r})Úquery)r   ÚtypeÚKeyErrorÚ_logÚdebugr   Ú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   ÚfailÚNotImplementedError)r   r%   Úclsr   r'   r   r   r   r$   S   s   r)   c                 C   ó   |   t |¡tjtj|¡S r   )r$   r   ÚdomainStringr&   ÚA©r   r%   r'   r   r   r   ÚlookupAddressV   ó   zResolverBase.lookupAddressc                 C   r-   r   )r$   r   r.   r&   ÚAAAAr0   r   r   r   ÚlookupIPV6AddressY   r2   zResolverBase.lookupIPV6Addressc                 C   r-   r   )r$   r   r.   r&   ÚA6r0   r   r   r   ÚlookupAddress6\   r2   zResolverBase.lookupAddress6c                 C   r-   r   )r$   r   r.   r&   ÚMXr0   r   r   r   ÚlookupMailExchange_   r2   zResolverBase.lookupMailExchangec                 C   r-   r   )r$   r   r.   r&   ÚNSr0   r   r   r   ÚlookupNameserversb   r2   zResolverBase.lookupNameserversc                 C   r-   r   )r$   r   r.   r&   ÚCNAMEr0   r   r   r   ÚlookupCanonicalNamee   r2   z ResolverBase.lookupCanonicalNamec                 C   r-   r   )r$   r   r.   r&   ÚMBr0   r   r   r   ÚlookupMailBoxh   r2   zResolverBase.lookupMailBoxc                 C   r-   r   )r$   r   r.   r&   ÚMGr0   r   r   r   ÚlookupMailGroupk   r2   zResolverBase.lookupMailGroupc                 C   r-   r   )r$   r   r.   r&   ÚMRr0   r   r   r   ÚlookupMailRenamen   r2   zResolverBase.lookupMailRenamec                 C   r-   r   )r$   r   r.   r&   ÚPTRr0   r   r   r   ÚlookupPointerq   r2   zResolverBase.lookupPointerc                 C   r-   r   )r$   r   r.   r&   ÚSOAr0   r   r   r   ÚlookupAuthorityt   r2   zResolverBase.lookupAuthorityc                 C   r-   r   )r$   r   r.   r&   ÚNULLr0   r   r   r   Ú
lookupNullw   r2   zResolverBase.lookupNullc                 C   r-   r   )r$   r   r.   r&   ÚWKSr0   r   r   r   ÚlookupWellKnownServicesz   r2   z$ResolverBase.lookupWellKnownServicesc                 C   r-   r   )r$   r   r.   r&   ÚSRVr0   r   r   r   ÚlookupService}   r2   zResolverBase.lookupServicec                 C   r-   r   )r$   r   r.   r&   ÚHINFOr0   r   r   r   ÚlookupHostInfo€   r2   zResolverBase.lookupHostInfoc                 C   r-   r   )r$   r   r.   r&   ÚMINFOr0   r   r   r   ÚlookupMailboxInfoƒ   r2   zResolverBase.lookupMailboxInfoc                 C   r-   r   )r$   r   r.   r&   ÚTXTr0   r   r   r   Ú
lookupText†   r2   zResolverBase.lookupTextc                 C   r-   r   )r$   r   r.   r&   ÚSPFr0   r   r   r   ÚlookupSenderPolicy‰   r2   zResolverBase.lookupSenderPolicyc                 C   r-   r   )r$   r   r.   r&   ÚRPr0   r   r   r   ÚlookupResponsibilityŒ   r2   z!ResolverBase.lookupResponsibilityc                 C   r-   r   )r$   r   r.   r&   ÚAFSDBr0   r   r   r   ÚlookupAFSDatabase�   r2   zResolverBase.lookupAFSDatabasec                 C   r-   r   )r$   r   r.   r&   ÚAXFRr0   r   r   r   Ú
lookupZone’   r2   zResolverBase.lookupZonec                 C   r-   r   )r$   r   r.   r&   ÚNAPTRr0   r   r   r   ÚlookupNamingAuthorityPointer•   r2   z)ResolverBase.lookupNamingAuthorityPointerc                 C   r-   r   )r$   r   r.   r&   ÚALL_RECORDSr0   r   r   r   ÚlookupAllRecords˜   r2   zResolverBase.lookupAllRecordsé
   c                 C   s*   t  |¡}|  ||¡}| | j||¡ |S r   )r   r.   r^   ÚaddCallbackÚ
_cbRecords)r   r%   r'   ÚeffortÚdr   r   r   ÚgetHostByNameœ   s   
zResolverBase.getHostByNamec                 C   s8   |\}}}t | t |¡|| | |ƒ}|st |¡‚|S r   )ÚextractRecordr   ÚNamer   ÚDNSLookupError)r   Úrecordsr%   rb   ÚansÚauthÚaddÚresultr   r   r   ra   ¥   s
   

zResolverBase._cbRecordsr   )Nr_   )0Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r!   r   ÚEFORMATr   ÚESERVERr   ÚENAMEr	   ÚENOTIMPr
   ÚEREFUSEDr   r   r   r   r   r   r$   r1   r4   r6   r8   r:   r<   r>   r@   rB   rD   rF   rH   rJ   rL   rN   rP   rR   rT   rV   rX   rZ   r\   r^   rd   ra   r   r   r   r   r   !   sN    	û
	























	r   r_   c                    s~  ˆ sdS t tdƒr?|D ]}|jˆkr#|jtjkr#t tj|jj	¡  S q|D ]}|jˆkr>|jtj
kr>t tj|jj	¡  S q&|D ]}|jˆkrY|jtjkrYt tj|jj	¡  S qA|D ],}|jˆkrˆ|jtjkrˆt| |jj|ˆ d ƒ}|s„| j|jjjˆ d d�  S |  S q\|D ]1}|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é   )rb   r   )ÚclientÚascii)Úserversc                    s$   | \}}}t ˆˆ|| | ˆ d ƒS )Nrw   )re   )rh   ri   rj   rk   ©Úlevelr%   Ú
nsResolverr   r   Ú
queryAgainè   s   
z!extractRecord.<locals>.queryAgain)ÚhasattrÚsocketr%   r   r   r5   rv   ÚAF_INET6ÚpayloadÚaddressr3   r/   ÚAF_INETr;   re   rd   r9   Útwisted.namesrx   ÚResolverÚdecodeÚPORTr1   r`   )Úresolverr%   Úanswersr|   Úrrl   rx   r~   r   r{   r   re   ­   sB   
€€€€ÿÿñre   r1   r4   r6   r:   r<   rF   r>   r@   rB   rH   rJ   rD   rN   rP   r8   rR   rT   rV   rX   rL   r\   rZ   r^   )r_   )3rp   r€   Úzope.interfacer   Útwisted.internetr   r   r   Útwisted.loggerr   r…   r   Útwisted.names.errorr   r	   r
   r   r   r   ÚrangeÚ_ANSÚ_AUTHÚ_ADDÚEMPTY_RESULTÚ	IResolverr   re   r/   r3   r5   r9   r;   rE   r=   r?   rA   rG   rI   rC   rM   rO   r7   rQ   rS   rU   rW   rK   r[   rY   r]   r   r   r   r   r   Ú<module>   sn     
Bÿþýüûúùø	÷
öõôóòñðï
é