class Gem::Resolv

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

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

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

Examples:

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

Gem::Resolv::DNS.open do |dns|
  ress = dns.getresources "www.ruby-lang.org", Gem::Resolv::DNS::Resource::IN::A
  p ress.map(&:address)
  ress = dns.getresources "ruby-lang.org", Gem::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.

Constants

AddressRegex

Address Regexp to use for matching IP addresses.

DefaultResolver

Default resolver to use for Gem::Resolv class methods.

VERSION

Public Class Methods

each_address(name, &block) click to toggle source

Iterates over all IP addresses for name.

# File rubygems/vendor/resolv/lib/resolv.rb, line 59
def self.each_address(name, &block)
  DefaultResolver.each_address(name, &block)
end
each_name(address, &proc) click to toggle source

Iterates over all hostnames for address.

# File rubygems/vendor/resolv/lib/resolv.rb, line 80
def self.each_name(address, &proc)
  DefaultResolver.each_name(address, &proc)
end
getaddress(name) click to toggle source

Looks up the first IP address for name.

# File rubygems/vendor/resolv/lib/resolv.rb, line 45
def self.getaddress(name)
  DefaultResolver.getaddress(name)
end
getaddresses(name) click to toggle source

Looks up all IP address for name.

# File rubygems/vendor/resolv/lib/resolv.rb, line 52
def self.getaddresses(name)
  DefaultResolver.getaddresses(name)
end
getname(address) click to toggle source

Looks up the hostname of address.

# File rubygems/vendor/resolv/lib/resolv.rb, line 66
def self.getname(address)
  DefaultResolver.getname(address)
end
getnames(address) click to toggle source

Looks up all hostnames for address.

# File rubygems/vendor/resolv/lib/resolv.rb, line 73
def self.getnames(address)
  DefaultResolver.getnames(address)
end
new(resolvers=nil, use_ipv6: nil) click to toggle source

Creates a new Gem::Resolv using resolvers.

# File rubygems/vendor/resolv/lib/resolv.rb, line 87
def initialize(resolvers=nil, use_ipv6: nil)
  @resolvers = resolvers || [Hosts.new, DNS.new(DNS::Config.default_config_hash.merge(use_ipv6: use_ipv6))]
end

Public Instance Methods

each_address(name) { |name| ... } click to toggle source

Iterates over all IP addresses for name.

# File rubygems/vendor/resolv/lib/resolv.rb, line 111
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) { |name| ... } click to toggle source

Iterates over all hostnames for address.

# File rubygems/vendor/resolv/lib/resolv.rb, line 146
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) click to toggle source

Looks up the first IP address for name.

# File rubygems/vendor/resolv/lib/resolv.rb, line 94
def getaddress(name)
  each_address(name) {|address| return address}
  raise ResolvError.new("no address for #{name}")
end
getaddresses(name) click to toggle source

Looks up all IP address for name.

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

Looks up the hostname of address.

# File rubygems/vendor/resolv/lib/resolv.rb, line 129
def getname(address)
  each_name(address) {|name| return name}
  raise ResolvError.new("no name for #{address}")
end
getnames(address) click to toggle source

Looks up all hostnames for address.

# File rubygems/vendor/resolv/lib/resolv.rb, line 137
def getnames(address)
  ret = []
  each_name(address) {|name| ret << name}
  return ret
end