o
    ¯bÇ  ã                   @   sz   d Z ddlmZ ddlmZmZ ddlmZmZ ddl	m
Z
 ddlmZ ddlmZ dd	„ Zd
d„ ZG dd„ dejƒZdS )z
hosts(5) support.
é    )Údefer)ÚisIPAddressÚisIPv6Address)ÚcommonÚdns)Úfailure)ÚnativeString)ÚFilePathc              	   C   sÊ   g }z|   ¡  ¡ }W n ty   | Y S w | ¡ }|D ]F}| d¡}|dkr-|d|… }|s0q| ¡ }| ¡ dd„ |dd… D ƒv rbzt|d ƒ}W n	 tyT   Y qw t|ƒs]t	|ƒrb| 
|¡ q|S )a±  
    Search the given file, which is in hosts(5) standard format, for addresses
    associated with a given name.

    @param hostsFile: The name of the hosts(5)-format file to search.
    @type hostsFile: L{FilePath}

    @param name: The name to search for.
    @type name: C{bytes}

    @return: L{None} if the name is not found in the file, otherwise a
        C{str} giving the address in the file associated with the name.
    ó   #éÿÿÿÿNc                 S   s   g | ]}|  ¡ ‘qS © )Úlower)Ú.0Úsr   r   ú5/usr/lib/python3/dist-packages/twisted/names/hosts.pyÚ
<listcomp>/   s    z$searchFileForAll.<locals>.<listcomp>é   r   )Ú
getContentÚ
splitlinesÚBaseExceptionr   ÚfindÚsplitr   Ú
ValueErrorr   r   Úappend)Ú	hostsFileÚnameÚresultsÚlinesÚlineÚidxÚpartsÚmaybeIPr   r   r   ÚsearchFileForAll   s0   ÿ
ÿ
€r"   c                 C   s   t t| ƒ|ƒ}|r|d S dS )a²  
    Grep given file, which is in hosts(5) standard format, for an address
    entry with a given name.

    @param file: The name of the hosts(5)-format file to search.
    @type file: C{str} or C{bytes}

    @param name: The name to search for.
    @type name: C{bytes}

    @return: L{None} if the name is not found in the file, otherwise a
        C{str} giving the first address in the file associated with
        the name.
    r   N)r"   r	   )Úfiler   Ú	addressesr   r   r   ÚsearchFileFor9   s   r%   c                   @   sJ   e Zd ZdZddd„Zdd„ Zdd	„ Zd
d„ Zddd„Zddd„Z	eZ
dS )ÚResolverz9
    A resolver that services hosts(5) format files.
    ó
   /etc/hostsé  c                 C   s   t j | ¡ || _|| _d S ©N)r   ÚResolverBaseÚ__init__r#   Úttl)Úselfr#   r,   r   r   r   r+   S   s   
zResolver.__init__c                    ó$   t ‡ ‡fdd„ttˆjƒˆ ƒD ƒƒS )zv
        Return a tuple of L{dns.RRHeader} instances for all of the IPv4
        addresses in the hosts file.
        c                 3   ó:   � | ]}t |ƒrt ˆ tjtjˆjt |ˆj¡¡V  qd S r)   )r   r   ÚRRHeaderÚAÚINr,   ÚRecord_A©r   Úaddr©r   r-   r   r   Ú	<genexpr>]   s   € ý 
ÿz%Resolver._aRecords.<locals>.<genexpr>©Útupler"   r	   r#   ©r-   r   r   r6   r   Ú	_aRecordsX   s   þzResolver._aRecordsc                    r.   )zv
        Return a tuple of L{dns.RRHeader} instances for all of the IPv6
        addresses in the hosts file.
        c                 3   r/   r)   )r   r   r0   ÚAAAAr2   r,   ÚRecord_AAAAr4   r6   r   r   r7   h   s   € ûÿ
ÿz(Resolver._aaaaRecords.<locals>.<genexpr>r8   r:   r   r6   r   Ú_aaaaRecordsc   s   üzResolver._aaaaRecordsc                 C   s*   |r
t  |ddf¡S t  t t |¡¡¡S )aF  
        Generate a response for the given name containing the given result
        records, or a failure if there are no result records.

        @param name: The DNS name the response is for.
        @type name: C{str}

        @param records: A tuple of L{dns.RRHeader} instances giving the results
            that will go into the response.

        @return: A L{Deferred} which will fire with a three-tuple of result
            records, authority records, and additional records, or which will
            fail with L{dns.DomainError} if there are no result records.
        r   )r   ÚsucceedÚfailr   ÚFailurer   ÚDomainError)r-   r   Úrecordsr   r   r   Ú_respondp   s   zResolver._respondNc                 C   ó   t  |¡}|  ||  |¡¡S )zm
        Read any IPv4 addresses from C{self.file} and return them as
        L{Record_A} instances.
        )r   ÚdomainStringrD   r;   ©r-   r   Útimeoutr   r   r   ÚlookupAddressƒ   ó   
zResolver.lookupAddressc                 C   rE   )zp
        Read any IPv6 addresses from C{self.file} and return them as
        L{Record_AAAA} instances.
        )r   rF   rD   r>   rG   r   r   r   ÚlookupIPV6Address‹   rJ   zResolver.lookupIPV6Address)r'   r(   r)   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r+   r;   r>   rD   rI   rK   ÚlookupAllRecordsr   r   r   r   r&   N   s    


r&   N)rO   Útwisted.internetr   Útwisted.internet.abstractr   r   Útwisted.namesr   r   Útwisted.pythonr   Útwisted.python.compatr   Útwisted.python.filepathr	   r"   r%   r*   r&   r   r   r   r   Ú<module>   s   '