Class: Resolv::Hosts
- Inherits:
-
Object
- Object
- Resolv::Hosts
- Defined in:
- lib/logstash/patches/resolv_9270.rb
Overview
Resolv::Hosts is a hostname resolver that uses the system hosts file.
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 IP address of
name
from the hosts file. -
#getaddresses(name) ⇒ Object
Gets all IP addresses for
name
from the hosts file. -
#getname(address) ⇒ Object
Gets the 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 Resolv::Hosts, using
filename
for its data source. -
#lazy_initialize ⇒ Object
:nodoc:.
Constructor Details
#initialize(filename = DefaultFileName) ⇒ Hosts
Creates a new Resolv::Hosts, using filename
for its data source.
192 193 194 195 196 |
# File 'lib/logstash/patches/resolv_9270.rb', line 192 def initialize(filename = DefaultFileName) @filename = filename @mutex = Thread::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.
249 250 251 252 253 254 |
# File 'lib/logstash/patches/resolv_9270.rb', line 249 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.
276 277 278 279 |
# File 'lib/logstash/patches/resolv_9270.rb', line 276 def each_name(address, &proc) lazy_initialize @addr2name[address]&.each(&proc) end |
#getaddress(name) ⇒ Object
Gets the IP address of name
from the hosts file.
232 233 234 235 |
# File 'lib/logstash/patches/resolv_9270.rb', line 232 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.
240 241 242 243 244 |
# File 'lib/logstash/patches/resolv_9270.rb', line 240 def getaddresses(name) ret = [] each_address(name) {|address| ret << address} return ret end |
#getname(address) ⇒ Object
Gets the hostname of address
from the hosts file.
259 260 261 262 |
# File 'lib/logstash/patches/resolv_9270.rb', line 259 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.
267 268 269 270 271 |
# File 'lib/logstash/patches/resolv_9270.rb', line 267 def getnames(address) ret = [] each_name(address) {|name| ret << name} return ret end |
#lazy_initialize ⇒ Object
:nodoc:
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/logstash/patches/resolv_9270.rb', line 198 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 |