
    \d                         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 Zd	 Z G d
 dej                  ZdS )z
hosts(5) support.
    )defer)isIPAddressisIPv6Address)commondns)failure)nativeString)FilePathc                 ,   g }	 |                                                                  }n# t          $ r |cY S w xY w|                                }|D ]}|                    d          }|dk    r
|d|         }|s*|                                }|                                d |dd         D             v rZ	 t          |d                   }n# t          $ r Y w xY wt          |          st          |          r|
                    |           |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                 6    g | ]}|                                 S  )lower).0ss     3lib/python3.11/site-packages/twisted/names/hosts.py
<listcomp>z$searchFileForAll.<locals>.<listcomp>/   s     999!AGGII999       r   )
getContent
splitlinesBaseExceptionr   findsplitr	   
ValueErrorr   r   append)	hostsFilenameresultslineslineidxpartsmaybeIPs           r   searchFileForAllr&      sE    G$$&&1133    ::<<D ( (iioo"99:D 	

::<<99uQRRy99999&uQx00   7## (}W'='= (w'''Ns   &+ :::C
CCc                 T    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   	addressess      r   searchFileForr*   9   s0     !$66I |4r   c                   @    e Zd ZdZddZd Zd Zd Zdd	Zdd
Z	eZ
dS )Resolverz9
    A resolver that services hosts(5) format files.
    
   /etc/hosts  c                 `    t           j                            |            || _        || _        d S N)r   ResolverBase__init__r(   ttl)selfr(   r3   s      r   r2   zResolver.__init__S   s+    $$T***	r   c                 ~     t           f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      K   | ]\}t          |          t          j        t          j        t          j        j        t          j        |j                            V  ]d S r0   )r   r   RRHeaderAINr3   Record_Ar   addrr   r4   s     r   	<genexpr>z%Resolver._aRecords.<locals>.<genexpr>]   sk       
 
4  
LsucfdhT488T8TUU
 
 
 
 
 
r   tupler&   r
   r(   r4   r   s   ``r   	_aRecordszResolver._aRecordsX   sW    
  
 
 
 
 
($))<)<dCC
 
 
 
 
 	
r   c                 ~     t           fdt          t           j                            D                       S )zv
        Return a tuple of L{dns.RRHeader} instances for all of the IPv6
        addresses in the hosts file.
        c              3      K   | ]\}t          |          t          j        t          j        t          j        j        t          j        |j                            V  ]d S r0   )r   r   r7   AAAAr9   r3   Record_AAAAr;   s     r   r=   z(Resolver._aaaaRecords.<locals>.<genexpr>h   sr       
 
 T""
Lch#/$2Q2Q 
 
 
 
 
 
r   r>   r@   s   ``r   _aaaaRecordszResolver._aaaaRecordsc   sY    
  
 
 
 
 
 )$))<)<dCC	
 
 
 
 
 	
r   c                     |rt          j        |ddf          S t          j        t          j        t          j        |                              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)r4   r   recordss      r   _respondzResolver._respondp   sG      	4='2r!2333z'/#/$*?*?@@AAAr   Nc                 |    t          j        |          }|                     ||                     |                    S )zm
        Read any IPv4 addresses from C{self.file} and return them as
        L{Record_A} instances.
        )r   domainStringrM   rA   r4   r   timeouts      r   lookupAddresszResolver.lookupAddress   s4    
 %%}}T4>>$#7#7888r   c                 |    t          j        |          }|                     ||                     |                    S )zp
        Read any IPv6 addresses from C{self.file} and return them as
        L{Record_AAAA} instances.
        )r   rO   rM   rF   rP   s      r   lookupIPV6AddresszResolver.lookupIPV6Address   s6    
 %%}}T4#4#4T#:#:;;;r   )r-   r.   r0   )__name__
__module____qualname____doc__r2   rA   rF   rM   rR   rT   lookupAllRecordsr   r   r   r,   r,   N   s            
	
 	
 	

 
 
B B B&9 9 9 9< < < < %r   r,   N)rX   twisted.internetr   twisted.internet.abstractr   r   twisted.namesr   r   twisted.pythonr   twisted.python.compatr	   twisted.python.filepathr
   r&   r*   r1   r,   r   r   r   <module>r`      s   
 
 # " " " " " @ @ @ @ @ @ @ @ % % % % % % % % " " " " " " . . . . . . , , , , , ,$ $ $N  *I% I% I% I% I%v" I% I% I% I% I%r   