Class: Dnsruby::Hosts

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

Overview

hostnames lookup methods.

Constant Summary collapse

DefaultFileName =
'/etc/hosts'

Instance Method Summary collapse

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



98
99
100
101
102
103
# File 'lib/dnsruby/hosts.rb', line 98

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



119
120
121
122
123
124
# File 'lib/dnsruby/hosts.rb', line 119

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

Raises:



85
86
87
88
# File 'lib/dnsruby/hosts.rb', line 85

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



91
92
93
94
95
# File 'lib/dnsruby/hosts.rb', line 91

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

Raises:



106
107
108
109
# File 'lib/dnsruby/hosts.rb', line 106

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



112
113
114
115
116
# File 'lib/dnsruby/hosts.rb', line 112

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

#lazy_initializeObject

: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
# 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
            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
            }
          }
        }
      rescue Exception
        #  Java won't find this file if running on Windows
      end
      @name2addr.each {|name, arr| arr.reverse!}
      @initialized = true
    end
  }
  self
end