class Resolv::Hosts::Dynamic

Public Class Methods

new(hosts = []) click to toggle source
# File lib/resolv-hosts-dynamic.rb, line 7
def initialize(hosts = [])
  @mutex = Mutex.new
  @name2addr = {}
  @addr2name = {}

  hosts = [hosts] if !hosts.is_a? Array

  hosts.each { |host|
    self.add_address(host)
  }
end

Public Instance Methods

add_address(host) click to toggle source

Adds host to the custom resolver.

# File lib/resolv-hosts-dynamic.rb, line 22
def add_address(host)
  @mutex.synchronize {
    addr = host['addr']
    hostname = host['hostname']
    aliases = host['aliases']

    raise "Must specify 'addr' for host" unless addr
    raise "Must specify 'hostname' for host" unless hostname

    addr.untaint
    hostname.untaint

    # So that aliases can be passed a string or an array of strings
    aliases = [aliases] if aliases.is_a? String

    @addr2name[addr] = [] unless @addr2name.include? addr
    @addr2name[addr] << hostname
    @addr2name[addr] += aliases if aliases
    @name2addr[hostname] = [] unless @name2addr.include? hostname
    @name2addr[hostname] << addr
    aliases.each {|n|
      n.untaint
      @name2addr[n] = [] unless @name2addr.include? n
      @name2addr[n] << addr
    } if aliases
  }
end
each_address(name, &proc) click to toggle source

Iterates over all IP addresses for name retrieved from the custom resolver.

# File lib/resolv-hosts-dynamic.rb, line 70
def each_address(name, &proc)
  if @name2addr.include?(name)
    @name2addr[name].each(&proc)
  end
end
each_name(address, &proc) click to toggle source

Iterates over all hostnames for address retrieved from the custom resolver.

# File lib/resolv-hosts-dynamic.rb, line 96
def each_name(address, &proc)
  if @addr2name.include?(address)
    @addr2name[address].each(&proc)
  end
end
getaddress(name) click to toggle source

Gets the IP address of name from the custom resolver.

# File lib/resolv-hosts-dynamic.rb, line 53
def getaddress(name)
  each_address(name) {|address| return address}
  raise ResolvError.new("No dynamic hosts entry for name: #{name}")
end
getaddresses(name) click to toggle source

Gets all IP addresses for name from the custom resolver.

# File lib/resolv-hosts-dynamic.rb, line 61
def getaddresses(name)
  ret = []
  each_address(name) {|address| ret << address}
  return ret
end
getname(address) click to toggle source

Gets the hostname of address from the custom resolver.

# File lib/resolv-hosts-dynamic.rb, line 79
def getname(address)
  each_name(address) {|name| return name}
  raise ResolvError.new("No dynamic hosts entry for address: #{address}")
end
getnames(address) click to toggle source

Gets all hostnames for address from the custom resolver.

# File lib/resolv-hosts-dynamic.rb, line 87
def getnames(address)
  ret = []
  each_name(address) {|name| ret << name}
  return ret
end