123456789_123456789_123456789_123456789_123456789_

Class: Resolv

Relationships & Source Files
Namespace Children
Modules:
LOC
Classes:
Exceptions:
Inherits: Object
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 { |r| r.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.

Constant Summary

Class Method Summary

Instance Method Summary

Constructor Details

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

Creates a new Resolv using resolvers.

[ GitHub ]

  
# File 'lib/resolv.rb', line 83

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

Class Method Details

.each_address(name, &block)

Iterates over all IP addresses for name.

[ GitHub ]

  
# File 'lib/resolv.rb', line 55

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

.each_name(address, &proc)

Iterates over all hostnames for address.

[ GitHub ]

  
# File 'lib/resolv.rb', line 76

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

.getaddress(name)

Looks up the first IP address for name.

[ GitHub ]

  
# File 'lib/resolv.rb', line 41

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

.getaddresses(name)

Looks up all IP address for name.

[ GitHub ]

  
# File 'lib/resolv.rb', line 48

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

.getname(address)

Looks up the hostname of address.

[ GitHub ]

  
# File 'lib/resolv.rb', line 62

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

.getnames(address)

Looks up all hostnames for address.

[ GitHub ]

  
# File 'lib/resolv.rb', line 69

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

Instance Method Details

#each_address(name)

Iterates over all IP addresses for name.

[ GitHub ]

  
# File 'lib/resolv.rb', line 107

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)

Iterates over all hostnames for address.

[ GitHub ]

  
# File 'lib/resolv.rb', line 142

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)

Looks up the first IP address for name.

Raises:

  • (ResolvError)
[ GitHub ]

  
# File 'lib/resolv.rb', line 90

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

#getaddresses(name)

Looks up all IP address for name.

[ GitHub ]

  
# File 'lib/resolv.rb', line 98

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

#getname(address)

Looks up the hostname of address.

Raises:

  • (ResolvError)
[ GitHub ]

  
# File 'lib/resolv.rb', line 125

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

#getnames(address)

Looks up all hostnames for address.

[ GitHub ]

  
# File 'lib/resolv.rb', line 133

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