class Resolv::DNS

Parent:
Object

Resolv::DNS is a DNS stub resolver.

Information taken from the following places:

Constants

Port

Default DNS Port

UDPSize

Default DNS UDP packet size

Public Class Methods

new(config_info=nil) Show source
# File lib/resolv.rb, line 336
def initialize(config_info=nil)
  @mutex = Mutex.new
  @config = Config.new(config_info)
  @initialized = nil
end

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.

:nameserver_port can be used to specify port number of nameserver address.

The value of :nameserver should be an address string or an array of address strings.

  • :nameserver => '8.8.8.8'

  • :nameserver => ['8.8.8.8', '8.8.4.4']

The value of :nameserver_port should be an array of pair of nameserver address and port number.

  • :nameserver_port => [['8.8.8.8', 53], ['8.8.4.4', 53]]

Example:

Resolv::DNS.new(:nameserver => ['210.251.121.21'],
                :search => ['ruby-lang.org'],
                :ndots => 1)
open(*args) { |dns| ... } Show source
# File lib/resolv.rb, line 301
def self.open(*args)
  dns = new(*args)
  return dns unless block_given?
  begin
    yield dns
  ensure
    dns.close
  end
end

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

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

Public Instance Methods

close() Show source
# File lib/resolv.rb, line 370
def close
  @mutex.synchronize {
    if @initialized
      @initialized = false
    end
  }
end

Closes the DNS resolver.

each_address(name) { |address| ... } Show source
# File lib/resolv.rb, line 408
def each_address(name)
  each_resource(name, Resource::IN::A) {|resource| yield resource.address}
  if use_ipv6?
    each_resource(name, Resource::IN::AAAA) {|resource| yield resource.address}
  end
end

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

each_name(address) { |name| ... } Show source
# File lib/resolv.rb, line 455
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

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.

each_resource(name, typeclass, &proc) Show source
# File lib/resolv.rb, line 511
def each_resource(name, typeclass, &proc)
  fetch_resource(name, typeclass) {|reply, reply_name|
    extract_resources(reply, reply_name, typeclass, &proc)
  }
end

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

fetch_resource(name, typeclass) { |reply, reply_name| ... } Show source
# File lib/resolv.rb, line 517
def fetch_resource(name, typeclass)
  lazy_initialize
  requester = make_udp_requester
  senders = {}
  begin
    @config.resolv(name) {|candidate, tout, nameserver, port|
      msg = Message.new
      msg.rd = 1
      msg.add_question(candidate, typeclass)
      unless sender = senders[[candidate, nameserver, port]]
        sender = requester.sender(msg, candidate, nameserver, port)
        next if !sender
        senders[[candidate, nameserver, port]] = sender
      end
      reply, reply_name = requester.request(sender, tout)
      case reply.rcode
      when RCode::NoError
        if reply.tc == 1 and not Requester::TCP === requester
          requester.close
          # Retry via TCP:
          requester = make_tcp_requester(nameserver, port)
          senders = {}
          # This will use TCP for all remaining candidates (assuming the
          # current candidate does not already respond successfully via
          # TCP).  This makes sense because we already know the full
          # response will not fit in an untruncated UDP packet.
          redo
        else
          yield(reply, reply_name)
        end
        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) Show source
# File lib/resolv.rb, line 384
def getaddress(name)
  each_address(name) {|address| return address}
  raise ResolvError.new("DNS result has no information for #{name}")
end

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

getaddresses(name) Show source
# File lib/resolv.rb, line 395
def getaddresses(name)
  ret = []
  each_address(name) {|address| ret << address}
  return ret
end

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

getname(address) Show source
# File lib/resolv.rb, line 431
def getname(address)
  each_name(address) {|name| return name}
  raise ResolvError.new("DNS result has no information for #{address}")
end

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.

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

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.

getresource(name, typeclass) Show source
# File lib/resolv.rb, line 492
def getresource(name, typeclass)
  each_resource(name, typeclass) {|resource| return resource}
  raise ResolvError.new("DNS result has no information for #{name}")
end

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.

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

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

timeouts=(values) Show source
# File lib/resolv.rb, line 353
def timeouts=(values)
  @config.timeouts = values
end

Sets the resolver timeouts. This may be a single positive number or an array of positive numbers representing timeouts in seconds. If an array is specified, a DNS request will retry and wait for each successive interval in the array until a successful response is received. Specifying nil reverts to the default timeouts:

5, second = 5 * 2 / nameserver_count, 2 * second, 4 * second

Example:

dns.timeouts = 3

Ruby Core © 1993–2017 Yukihiro Matsumoto
Licensed under the Ruby License.
Ruby Standard Library © contributors
Licensed under their own licenses.