Class: Resolv::Hosts
Relationships & Source Files | |
Inherits: | Object |
Defined in: | lib/resolv.rb |
Overview
Hosts
is a hostname resolver that uses the system hosts file.
Class Method Summary
-
.new(filename = DefaultFileName) ⇒ Hosts
constructor
Creates a new
Hosts
, usingfilename
for its data source.
Instance Method Summary
-
#each_address(name, &proc)
Iterates over all IP addresses for
name
retrieved from the hosts file. -
#each_name(address, &proc)
Iterates over all hostnames for
address
retrieved from the hosts file. -
#getaddress(name)
Gets the IP address of
name
from the hosts file. -
#getaddresses(name)
Gets all IP addresses for
name
from the hosts file. -
#getname(address)
Gets the hostname of
address
from the hosts file. -
#getnames(address)
Gets all hostnames for
address
from the hosts file. - #lazy_initialize Internal use only
Constructor Details
.new(filename = DefaultFileName) ⇒ Hosts
Creates a new Hosts
, using filename
for its data source.
# File 'lib/resolv.rb', line 180
def initialize(filename = DefaultFileName) @filename = filename @mutex = Thread::Mutex.new @initialized = nil end
Instance Method Details
#each_address(name, &proc)
Iterates over all IP addresses for name
retrieved from the hosts file.
# File 'lib/resolv.rb', line 237
def each_address(name, &proc) lazy_initialize if @name2addr.include?(name) @name2addr[name].each(&proc) end end
#each_name(address, &proc)
Iterates over all hostnames for address
retrieved from the hosts file.
# File 'lib/resolv.rb', line 264
def each_name(address, &proc) lazy_initialize @addr2name[address]&.each(&proc) end
#getaddress(name)
Gets the IP address of name
from the hosts file.
# File 'lib/resolv.rb', line 220
def getaddress(name) each_address(name) {|address| return address} raise ResolvError.new("#{@filename} has no name: #{name}") end
#getaddresses(name)
Gets all IP addresses for name
from the hosts file.
# File 'lib/resolv.rb', line 228
def getaddresses(name) ret = [] each_address(name) {|address| ret << address} return ret end
#getname(address)
Gets the hostname of address
from the hosts file.
# File 'lib/resolv.rb', line 247
def getname(address) each_name(address) {|name| return name} raise ResolvError.new("#{@filename} has no address: #{address}") end
#getnames(address)
Gets all hostnames for address
from the hosts file.
# File 'lib/resolv.rb', line 255
def getnames(address) ret = [] each_name(address) {|name| ret << name} return ret end
#lazy_initialize
# File 'lib/resolv.rb', line 186
def lazy_initialize # :nodoc: @mutex.synchronize { unless @initialized @name2addr = {} @addr2name = {} File.open(@filename, 'rb') {|f| f.each {|line| line.sub!(/#.*/, '') addr, hostname, *aliases = line.split(/\s+/) next unless addr addr.untaint hostname.untaint @addr2name[addr] = [] unless @addr2name.include? addr @addr2name[addr] << hostname @addr2name[addr] += aliases @name2addr[hostname] = [] unless @name2addr.include? hostname @name2addr[hostname] << addr aliases.each {|n| n.untaint @name2addr[n] = [] unless @name2addr.include? n @name2addr[n] << addr } } } @name2addr.each {|name, arr| arr.reverse!} @initialized = true end } self end