In Files

  • resolv.rb

Resolv::DNS

Public Class Methods

new(config_info=nil) click to toggle source
 
               # File resolv.rb, line 386
def initialize(config_info=nil)
  @mutex = Mutex.new
  @config = Config.new(config_info)
  @initialized = nil
end
            
open(*args) click to toggle source
 
               # File resolv.rb, line 376
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
 
               # File resolv.rb, line 402
def close
  @mutex.synchronize {
    if @initialized
      @initialized = false
    end
  }
end
            
each_address(name) click to toggle source
 
               # File resolv.rb, line 421
def each_address(name)
  each_resource(name, Resource::IN::A) {|resource| yield resource.address}
end
            
each_name(address) click to toggle source
 
               # File resolv.rb, line 436
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
 
               # File resolv.rb, line 461
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
            
extract_resources(msg, name, typeclass) click to toggle source
 
               # File resolv.rb, line 498
def extract_resources(msg, name, typeclass)
  if typeclass < Resource::ANY
    n0 = Name.create(name)
    msg.each_answer {|n, ttl, data|
      yield data if n0 == n
    }
  end
  yielded = false
  n0 = Name.create(name)
  msg.each_answer {|n, ttl, data|
    if n0 == n
      case data
      when typeclass
        yield data
        yielded = true
      when Resource::CNAME
        n0 = data.name
      end
    end
  }
  return if yielded
  msg.each_answer {|n, ttl, data|
    if n0 == n
      case data
      when typeclass
        yield data
      end
    end
  }
end
            
getaddress(name) click to toggle source
 
               # File resolv.rb, line 410
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
 
               # File resolv.rb, line 415
def getaddresses(name)
  ret = []
  each_address(name) {|address| ret << address}
  return ret
end
            
getname(address) click to toggle source
 
               # File resolv.rb, line 425
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
 
               # File resolv.rb, line 430
def getnames(address)
  ret = []
  each_name(address) {|name| ret << name}
  return ret
end
            
getresource(name, typeclass) click to toggle source
 
               # File resolv.rb, line 450
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
 
               # File resolv.rb, line 455
def getresources(name, typeclass)
  ret = []
  each_resource(name, typeclass) {|resource| ret << resource}
  return ret
end
            
lazy_initialize() click to toggle source
 
               # File resolv.rb, line 392
def lazy_initialize
  @mutex.synchronize {
    unless @initialized
      @config.lazy_initialize
      @initialized = true
    end
  }
  self
end
            

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.

blog comments powered by Disqus