Class: Resolv::Hosts

Inherits:
Object
  • Object
show all
Defined in:
lib/resolv.rb

Overview

Resolv::Hosts is a hostname resolver that uses the system hosts file.

Instance Method Summary collapse

Constructor Details

#initialize(filename = DefaultFileName) ⇒ Hosts

Creates a new Resolv::Hosts, using filename for its data source.


179
180
181
182
183
# File 'lib/resolv.rb', line 179

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.


236
237
238
239
240
241
# File 'lib/resolv.rb', line 236

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.


263
264
265
266
267
268
# File 'lib/resolv.rb', line 263

def each_name(address, &proc)
  lazy_initialize
  if @addr2name.include?(address)
    @addr2name[address].each(&proc)
  end
end

#getaddress(name) ⇒ Object

Gets the IP address of name from the hosts file.

Raises:


219
220
221
222
# File 'lib/resolv.rb', line 219

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.


227
228
229
230
231
# File 'lib/resolv.rb', line 227

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.

Raises:


246
247
248
249
# File 'lib/resolv.rb', line 246

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.


254
255
256
257
258
# File 'lib/resolv.rb', line 254

def getnames(address)
  ret = []
  each_name(address) {|name| ret << name}
  return ret
end

#lazy_initializeObject

:nodoc:


185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/resolv.rb', line 185

def lazy_initialize # :nodoc:
  @mutex.synchronize {
    unless @initialized
      @name2addr = {}
      @addr2name = {}
      open(@filename) {|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