o b@szdZddlmZddlmZmZddlmZmZddl m Z ddl m Z ddl mZdd Zd d ZGd d d ejZdS)z hosts(5) support. )defer) isIPAddress isIPv6Address)commondns)failure) nativeString)FilePathc Csg}z|}Wn ty|YSw|}|D]F}|d}|dkr-|d|}|s0q|}|dd|ddDvrbzt|d}Wn tyTYqwt|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. #NcSsg|]}|qS)lower).0sr r 5/usr/lib/python3/dist-packages/twisted/names/hosts.py /sz$searchFileForAll..r) getContent splitlines BaseExceptionr findsplitr ValueErrorrrappend) hostsFilenameresultslineslineidxpartsmaybeIPr r rsearchFileForAlls0     r"cCstt||}|r |dSdS)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. rN)r"r )filer addressesr r r searchFileFor9sr%c@sJeZdZdZdddZddZdd Zd d Zdd dZdddZ eZ d S)Resolverz9 A resolver that services hosts(5) format files. /etc/hostscCstj|||_||_dSN)r ResolverBase__init__r#ttl)selfr#r,r r rr+Ss  zResolver.__init__c$tfddttjDS)zv Return a tuple of L{dns.RRHeader} instances for all of the IPv4 addresses in the hosts file. c 3:|]}t|rttjtjjt|jVqdSr))rrRRHeaderAINr,Record_Araddrrr-r r ]s  z%Resolver._aRecords..tupler"r r#r-rr r6r _aRecordsXszResolver._aRecordscr.)zv Return a tuple of L{dns.RRHeader} instances for all of the IPv6 addresses in the hosts file. c 3r/r))rrr0AAAAr2r, Record_AAAAr4r6r rr7hs z(Resolver._aaaaRecords..r8r:r r6r _aaaaRecordscszResolver._aaaaRecordscCs*|r t|ddfSttt|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 )rsucceedfailrFailurer DomainError)r-rrecordsr r r_respondpszResolver._respondNcCt|}||||S)zm Read any IPv4 addresses from C{self.file} and return them as L{Record_A} instances. )r domainStringrDr;r-rtimeoutr r r lookupAddress zResolver.lookupAddresscCrE)zp Read any IPv6 addresses from C{self.file} and return them as L{Record_AAAA} instances. )rrFrDr>rGr r rlookupIPV6AddressrJzResolver.lookupIPV6Address)r'r(r)) __name__ __module__ __qualname____doc__r+r;r>rDrIrKlookupAllRecordsr r r rr&Ns     r&N)rOtwisted.internetrtwisted.internet.abstractrr twisted.namesrrtwisted.pythonrtwisted.python.compatrtwisted.python.filepathr r"r%r*r&r r r rs    '