Class: Resolv

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

Overview

Resolv is a thread-aware DNS resolver library written in Ruby. Resolv can handle multiple DNS requests concurrently without blocking the entire Ruby interpreter.

See also resolv-replace.rb to replace the libc resolver with Resolv.

Resolv can look up various DNS resources using the DNS module directly.

Examples:

p Resolv.getaddress "www.ruby-lang.org"
p Resolv.getname "210.251.121.214"

Resolv::DNS.open do |dns|
  ress = dns.getresources "www.ruby-lang.org", Resolv::DNS::Resource::IN::A
  p ress.map(&:address)
  ress = dns.getresources "ruby-lang.org", Resolv::DNS::Resource::IN::MX
  p ress.map { |r| [r.exchange.to_s, r.preference] }
end

Bugs

  • NIS is not supported.

  • /etc/nsswitch.conf is not supported.

Defined Under Namespace

Modules: LOC Classes: DNS, Hosts, IPv4, IPv6, MDNS, ResolvError, ResolvTimeout

Constant Summary collapse

DefaultResolver =

Default resolver to use for Resolv class methods.

self.new
AddressRegex =

Address Regexp to use for matching IP addresses.

/(?:#{IPv4::Regex})|(?:#{IPv6::Regex})/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resolvers = [Hosts.new, DNS.new]) ⇒ Resolv

Creates a new Resolv using resolvers.



85
86
87
# File 'lib/resolv.rb', line 85

def initialize(resolvers=[Hosts.new, DNS.new])
  @resolvers = resolvers
end

Class Method Details

.each_address(name, &block) ⇒ Object

Iterates over all IP addresses for name.



57
58
59
# File 'lib/resolv.rb', line 57

def self.each_address(name, &block)
  DefaultResolver.each_address(name, &block)
end

.each_name(address, &proc) ⇒ Object

Iterates over all hostnames for address.



78
79
80
# File 'lib/resolv.rb', line 78

def self.each_name(address, &proc)
  DefaultResolver.each_name(address, &proc)
end

.getaddress(name) ⇒ Object

Looks up the first IP address for name.



43
44
45
# File 'lib/resolv.rb', line 43

def self.getaddress(name)
  DefaultResolver.getaddress(name)
end

.getaddresses(name) ⇒ Object

Looks up all IP address for name.



50
51
52
# File 'lib/resolv.rb', line 50

def self.getaddresses(name)
  DefaultResolver.getaddresses(name)
end

.getname(address) ⇒ Object

Looks up the hostname of address.



64
65
66
# File 'lib/resolv.rb', line 64

def self.getname(address)
  DefaultResolver.getname(address)
end

.getnames(address) ⇒ Object

Looks up all hostnames for address.



71
72
73
# File 'lib/resolv.rb', line 71

def self.getnames(address)
  DefaultResolver.getnames(address)
end

Instance Method Details

#each_address(name) ⇒ Object

Iterates over all IP addresses for name.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/resolv.rb', line 109

def each_address(name)
  if AddressRegex =~ name
    yield name
    return
  end
  yielded = false
  @resolvers.each {|r|
    r.each_address(name) {|address|
      yield address.to_s
      yielded = true
    }
    return if yielded
  }
end

#each_name(address) ⇒ Object

Iterates over all hostnames for address.



144
145
146
147
148
149
150
151
152
153
# File 'lib/resolv.rb', line 144

def each_name(address)
  yielded = false
  @resolvers.each {|r|
    r.each_name(address) {|name|
      yield name.to_s
      yielded = true
    }
    return if yielded
  }
end

#getaddress(name) ⇒ Object

Looks up the first IP address for name.

Raises:



92
93
94
95
# File 'lib/resolv.rb', line 92

def getaddress(name)
  each_address(name) {|address| return address}
  raise ResolvError.new("no address for #{name}")
end

#getaddresses(name) ⇒ Object

Looks up all IP address for name.



100
101
102
103
104
# File 'lib/resolv.rb', line 100

def getaddresses(name)
  ret = []
  each_address(name) {|address| ret << address}
  return ret
end

#getname(address) ⇒ Object

Looks up the hostname of address.

Raises:



127
128
129
130
# File 'lib/resolv.rb', line 127

def getname(address)
  each_name(address) {|name| return name}
  raise ResolvError.new("no name for #{address}")
end

#getnames(address) ⇒ Object

Looks up all hostnames for address.



135
136
137
138
139
# File 'lib/resolv.rb', line 135

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