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   '