# Resolv::DNS (Class)

In: | resolv.rb
Parent: | Object

## Methods

close each_address each_name each_resource extract_resources getaddress getaddresses getname getnames getresource getresources lazy_initialize new open

## Constants

Port | = | 53
| STD0013 (RFC 1035, etc.) ftp.isi.edu/in-notes/iana/assignments/dns-parameters
UDPSize | = | 512
DNSThreadGroup | = | ThreadGroup.new

## Classes and Modules

Module Resolv::DNS::Label
::Class Resolv::DNS::Label::Str
Module Resolv::DNS::OpCode
Module Resolv::DNS::RCode
Class Resolv::DNS::Config
::Class Resolv::DNS::Config::NXDomain
::Class Resolv::DNS::Config::OtherResolvError
Class Resolv::DNS::DecodeError
Class Resolv::DNS::EncodeError
Class Resolv::DNS::Message
::Class Resolv::DNS::Message::MessageDecoder
::Class Resolv::DNS::Message::MessageEncoder
Class Resolv::DNS::Name
Class Resolv::DNS::Query
Class Resolv::DNS::Requester
::Class Resolv::DNS::Requester::ConnectedUDP
:: ::Class Resolv::DNS::Requester::ConnectedUDP::Sender
::Class Resolv::DNS::Requester::RequestError
::Class Resolv::DNS::Requester::Sender
::Class Resolv::DNS::Requester::TCP
:: ::Class Resolv::DNS::Requester::TCP::Sender
::Class Resolv::DNS::Requester::UnconnectedUDP
:: ::Class Resolv::DNS::Requester::UnconnectedUDP::Sender
Class Resolv::DNS::Resource
::Module Resolv::DNS::Resource::IN
:: ::Class Resolv::DNS::Resource::IN::A
:: ::Class Resolv::DNS::Resource::IN::AAAA
:: ::Class Resolv::DNS::Resource::IN::WKS
::Class Resolv::DNS::Resource::ANY
::Class Resolv::DNS::Resource::CNAME
::Class Resolv::DNS::Resource::DomainName
::Class Resolv::DNS::Resource::Generic
::Class Resolv::DNS::Resource::HINFO
::Class Resolv::DNS::Resource::MINFO
::Class Resolv::DNS::Resource::MX
::Class Resolv::DNS::Resource::NS
::Class Resolv::DNS::Resource::PTR
::Class Resolv::DNS::Resource::SOA
::Class Resolv::DNS::Resource::TXT

## Public Class methods

open(*args) {|dns| ...}

[Source]

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

new(config="/etc/resolv.conf")

[Source]

    # File resolv.rb, line 372
        def initialize(config="/etc/resolv.conf")
          @mutex = Mutex.new
          @config = Config.new(config)
          @initialized = nil
        end

## Public Instance methods

lazy_initialize()

[Source]

    # File resolv.rb, line 378
        def lazy_initialize
          @mutex.synchronize {
            unless @initialized
              @config.lazy_initialize

              if nameserver = @config.single?
                @requester = Requester::ConnectedUDP.new(nameserver)
              else
                @requester = Requester::UnconnectedUDP.new
              end

              @initialized = true
            end
          }
        end

close()

[Source]

    # File resolv.rb, line 394
        def close
          @mutex.synchronize {
            if @initialized
              @requester.close if @requester
              @requester = nil
              @initialized = false
            end
          }
        end

getaddress(name)

[Source]

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

getaddresses(name)

[Source]

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

each_address(name) {|resource.address}| ...}

[Source]

    # File resolv.rb, line 415
        def each_address(name)
          each_resource(name, Resource::IN::A) {|resource| yield resource.address}
        end

getname(address)

[Source]

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

getnames(address)

[Source]

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

each_name(address) {|resource.name}| ...}

[Source]

    # File resolv.rb, line 430
        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

getresource(name, typeclass)

[Source]

    # File resolv.rb, line 444
        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)

[Source]

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

each_resource(name, typeclass, &proc)

[Source]

    # File resolv.rb, line 455
        def each_resource(name, typeclass, &proc)
          lazy_initialize
          q = Queue.new
          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, q, nameserver)
              end
              sender.send
              reply = reply_name = nil
              timeout(tout, ResolvTimeout) { reply, reply_name = q.pop }
              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.delete(q)
          end
        end

extract_resources(msg, name, typeclass) {|data if n0 == n| ...}

[Source]

    # File resolv.rb, line 486
        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

[Validate]