In Files

  • resolv.rb

Resolv::DNS

Resolv::DNS is a DNS stub resolver.

Information taken from the following places:

Constants

DNSThreadGroup

Group of DNS resolver threads (obsolete)

Port

Default DNS Port

RequestID
RequestIDMutex
UDPSize

Default DNS UDP packet size

Public Class Methods

new(config_info=nil) click to toggle source

Creates a new DNS resolver.

config_info can be:

nil

Uses /etc/resolv.conf.

String

Path to a file using /etc/resolv.conf's format.

Hash

Must contain :nameserver, :search and :ndots keys.

Example:

Resolv::DNS.new(:nameserver => ['210.251.121.21'],
                :search => ['ruby-lang.org'],
                :ndots => 1)
 
               # File resolv.rb, line 328
def initialize(config_info=nil)
  @mutex = Mutex.new
  @config = Config.new(config_info)
  @initialized = nil
end
            
open(*args) click to toggle source

Creates a new DNS resolver. See ::new for argument details.

Yields the created DNS resolver to the block, if given, otherwise returns it.

 
               # File resolv.rb, line 303
def self.open(*args)
  dns = new(*args)
  return dns unless block_given?
  begin
    yield dns
  ensure
    dns.close
  end
end
            

Public Instance Methods

close() click to toggle source

Closes the DNS resolver.

 
               # File resolv.rb, line 347
def close
  @mutex.synchronize {
    if @initialized
      @initialized = false
    end
  }
end
            
each_address(name) click to toggle source

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

name can be a Resolv::DNS::Name or a String. Retrieved addresses will be a Resolv::IPv4 or Resolv::IPv6

 
               # File resolv.rb, line 385
def each_address(name)
  each_resource(name, Resource::IN::A) {|resource| yield resource.address}
  each_resource(name, Resource::IN::AAAA) {|resource| yield resource.address}
end
            
each_name(address) click to toggle source

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

address must be a Resolv::IPv4, Resolv::IPv6 or a String. Retrieved names will be Resolv::DNS::Name instances.

 
               # File resolv.rb, line 420
def each_name(address)
  case address
  when Name
    ptr = address
  when IPv4::Regex
    ptr = IPv4.create(address).to_name
  when IPv6::Regex
    ptr = IPv6.create(address).to_name
  else
    raise ResolvError.new("cannot interpret as address: #{address}")
  end
  each_resource(ptr, Resource::IN::PTR) {|resource| yield resource.name}
end
            
each_resource(name, typeclass, &proc) click to toggle source

Iterates over all typeclass DNS resources for name. See getresource for argument details.

 
               # File resolv.rb, line 476
def each_resource(name, typeclass, &proc)
  lazy_initialize
  requester = make_requester
  senders = {}
  begin
    @config.resolv(name) {|candidate, tout, nameserver|
      msg = Message.new
      msg.rd = 1
      msg.add_question(candidate, typeclass)
      unless sender = senders[[candidate, nameserver]]
        sender = senders[[candidate, nameserver]] =
          requester.sender(msg, candidate, nameserver)
      end
      reply, reply_name = requester.request(sender, tout)
      case reply.rcode
      when RCode::NoError
        extract_resources(reply, reply_name, typeclass, &proc)
        return
      when RCode::NXDomain
        raise Config::NXDomain.new(reply_name.to_s)
      else
        raise Config::OtherResolvError.new(reply_name.to_s)
      end
    }
  ensure
    requester.close
  end
end
            
getaddress(name) click to toggle source

Gets the IP address of name from the DNS resolver.

name can be a Resolv::DNS::Name or a String. Retrieved address will be a Resolv::IPv4 or Resolv::IPv6

 
               # File resolv.rb, line 361
def getaddress(name)
  each_address(name) {|address| return address}
  raise ResolvError.new("DNS result has no information for #{name}")
end
            
getaddresses(name) click to toggle source

Gets all IP addresses for name from the DNS resolver.

name can be a Resolv::DNS::Name or a String. Retrieved addresses will be a Resolv::IPv4 or Resolv::IPv6

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

Gets the hostname for address from the DNS resolver.

address must be a Resolv::IPv4, Resolv::IPv6 or a String. Retrieved name will be a Resolv::DNS::Name.

 
               # File resolv.rb, line 396
def getname(address)
  each_name(address) {|name| return name}
  raise ResolvError.new("DNS result has no information for #{address}")
end
            
getnames(address) click to toggle source

Gets all hostnames for address from the DNS resolver.

address must be a Resolv::IPv4, Resolv::IPv6 or a String. Retrieved names will be Resolv::DNS::Name instances.

 
               # File resolv.rb, line 407
def getnames(address)
  ret = []
  each_name(address) {|name| ret << name}
  return ret
end
            
getresource(name, typeclass) click to toggle source

Look up the typeclass DNS resource of name.

name must be a Resolv::DNS::Name or a String.

typeclass should be one of the following:

Returned resource is represented as a Resolv::DNS::Resource instance, i.e. Resolv::DNS::Resource::IN::A.

 
               # File resolv.rb, line 457
def getresource(name, typeclass)
  each_resource(name, typeclass) {|resource| return resource}
  raise ResolvError.new("DNS result has no information for #{name}")
end
            
getresources(name, typeclass) click to toggle source

Looks up all typeclass DNS resources for name. See getresource for argument details.

 
               # File resolv.rb, line 466
def getresources(name, typeclass)
  ret = []
  each_resource(name, typeclass) {|resource| ret << resource}
  return ret
end