Class: Dnsruby::Hosts
- Inherits:
-
Object
- Object
- Dnsruby::Hosts
- Defined in:
- lib/dnsruby/hosts.rb
Overview
hostnames lookup methods.
Constant Summary collapse
- DefaultFileName =
'/etc/hosts'
Instance Method Summary collapse
-
#each_address(name, &proc) ⇒ Object
Iterates over all IP addresses for
name
retrieved from the hosts file. -
#each_name(address, &proc) ⇒ Object
Iterates over all hostnames for
address
retrieved from the hosts file. -
#getaddress(name) ⇒ Object
Gets the first IP address for
name
from the hosts file. -
#getaddresses(name) ⇒ Object
Gets all IP addresses for
name
from the hosts file. -
#getname(address) ⇒ Object
Gets the first hostname of
address
from the hosts file. -
#getnames(address) ⇒ Object
Gets all hostnames for
address
from the hosts file. -
#initialize(filename = DefaultFileName) ⇒ Hosts
constructor
Creates a new Dnsruby::Hosts using
filename
for its data source. -
#lazy_initialize ⇒ Object
:nodoc:.
Constructor Details
#initialize(filename = DefaultFileName) ⇒ Hosts
Creates a new Dnsruby::Hosts using filename
for its data source
43 44 45 46 47 |
# File 'lib/dnsruby/hosts.rb', line 43 def initialize(filename = DefaultFileName) @filename = filename @mutex = Mutex.new @initialized = nil end |
Instance Method Details
#each_address(name, &proc) ⇒ Object
Iterates over all IP addresses for name
retrieved from the hosts file
102 103 104 105 106 107 |
# File 'lib/dnsruby/hosts.rb', line 102 def each_address(name, &proc) lazy_initialize if @name2addr.include?(name) @name2addr[name].each(&proc) end end |
#each_name(address, &proc) ⇒ Object
Iterates over all hostnames for address
retrieved from the hosts file
123 124 125 126 127 128 |
# File 'lib/dnsruby/hosts.rb', line 123 def each_name(address, &proc) lazy_initialize if @addr2name.include?(address) @addr2name[address].each(&proc) end end |
#getaddress(name) ⇒ Object
Gets the first IP address for name
from the hosts file
89 90 91 92 |
# File 'lib/dnsruby/hosts.rb', line 89 def getaddress(name) each_address(name) {|address| return address} raise ResolvError.new("#{@filename} has no name: #{name}") end |
#getaddresses(name) ⇒ Object
Gets all IP addresses for name
from the hosts file
95 96 97 98 99 |
# File 'lib/dnsruby/hosts.rb', line 95 def getaddresses(name) ret = [] each_address(name) {|address| ret << address} return ret end |
#getname(address) ⇒ Object
Gets the first hostname of address
from the hosts file
110 111 112 113 |
# File 'lib/dnsruby/hosts.rb', line 110 def getname(address) each_name(address) {|name| return name} raise ResolvError.new("#{@filename} has no address: #{address}") end |
#getnames(address) ⇒ Object
Gets all hostnames for address
from the hosts file
116 117 118 119 120 |
# File 'lib/dnsruby/hosts.rb', line 116 def getnames(address) ret = [] each_name(address) {|name| ret << name} return ret end |
#lazy_initialize ⇒ Object
:nodoc:
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/dnsruby/hosts.rb', line 49 def lazy_initialize# :nodoc: @mutex.synchronize { unless @initialized @name2addr = {} @addr2name = {} begin open(@filename) {|f| f.each {|line| line.sub!(/#.*/, '') addr, hostname, *aliases = line.split(/\s+/) next unless addr if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("2.8") addr.untaint hostname.untaint end @addr2name[addr] = [] unless @addr2name.include? addr @addr2name[addr] << hostname @addr2name[addr] += aliases @name2addr[hostname] = [] unless @name2addr.include? hostname @name2addr[hostname] << addr aliases.each {|n| if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("2.8") n.untaint end @name2addr[n] = [] unless @name2addr.include? n @name2addr[n] << addr } } } rescue Exception # Java won't find this file if running on Windows end @name2addr.each {|name, arr| arr.reverse!} @initialized = true end } self end |