類別 Resolv::DNS

Resolv::DNSDNS stub 解析器。

資訊取自下列位置

常數

Port

預設 DNS Port

UDPSize

預設 DNS UDP 封包大小

公開類別方法

new(config_info=nil) 按一下以切換來源

建立新的 DNS 解析器。

config_info 可以是

nil

使用 /etc/resolv.conf。

字串

使用 /etc/resolv.conf 格式的路徑。

雜湊

必須包含 :nameserver、:search 和 :ndots 鍵。

:nameserver_port 可用於指定名稱伺服器位址的埠號。:raise_timeout_errors 可用於將逾時錯誤引發為例外,而不是將其視為 NXDOMAIN 回應。

:nameserver 的值應該是位址字串或位址字串陣列。

  • :nameserver => ‘8.8.8.8’

  • :nameserver => [‘8.8.8.8’, ‘8.8.4.4’]

:nameserver_port 的值應該是名稱伺服器位址和埠號配對的陣列。

  • :nameserver_port => [[‘8.8.8.8’, 53], [‘8.8.4.4’, 53]]

範例

Resolv::DNS.new(:nameserver => ['210.251.121.21'],
                :search => ['ruby-lang.org'],
                :ndots => 1)
# File lib/resolv.rb, line 333
def initialize(config_info=nil)
  @mutex = Thread::Mutex.new
  @config = Config.new(config_info)
  @initialized = nil
end
open(*args) { |dns| ... } 按一下以切換來源

建立新的 DNS 解析器。請參閱 Resolv::DNS.new 以取得引數詳細資料。

如果提供區塊,則將建立的 DNS 解析器傳遞給區塊,否則傳回解析器。

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

公開實例方法

close() 按一下以切換來源

關閉 DNS 解析器。

# File lib/resolv.rb, line 367
def close
  @mutex.synchronize {
    if @initialized
      @initialized = false
    end
  }
end
each_address(name) { |address| ... } 按一下以切換來源

反覆處理從 DNS 解析器擷取的 name 所有 IP 位址。

name 可以是 Resolv::DNS::NameString。擷取的位址會是 Resolv::IPv4Resolv::IPv6

# File lib/resolv.rb, line 405
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
each_name(address) { |name| ... } 按一下以切換來源

針對從 DNS 解析器擷取的 address 逐一反覆運算所有主機名稱。

address 必須是 Resolv::IPv4Resolv::IPv6String。擷取的名稱會是 Resolv::DNS::Name 實例。

# File lib/resolv.rb, line 457
def each_name(address)
  case address
  when Name
    ptr = address
  when IPv4, IPv6
    ptr = address.to_name
  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) 按一下以切換來源

針對 name 的所有 typeclass DNS 資源逐一反覆運算。請參閱 getresource 以取得參數詳細資料。

# File lib/resolv.rb, line 515
def each_resource(name, typeclass, &proc)
  fetch_resource(name, typeclass) {|reply, reply_name|
    extract_resources(reply, reply_name, typeclass, &proc)
  }
end
fetch_resource(name, typeclass) { |reply, reply_name| ... } 按一下以切換來源
# File lib/resolv.rb, line 521
def fetch_resource(name, typeclass)
  lazy_initialize
  begin
    requester = make_udp_requester
  rescue Errno::EACCES
    # fall back to TCP
  end
  senders = {}
  begin
    @config.resolv(name) {|candidate, tout, nameserver, port|
      requester ||= make_tcp_requester(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) 按一下以切換來源

DNS 解析器取得 name 的 IP 位址。

name 可以是 Resolv::DNS::NameString。擷取的位址會是 Resolv::IPv4Resolv::IPv6

# File lib/resolv.rb, line 381
def getaddress(name)
  each_address(name) {|address| return address}
  raise ResolvError.new("DNS result has no information for #{name}")
end
getaddresses(name) 按一下以切換來源

DNS 解析器取得 name 的所有 IP 位址。

name 可以是 Resolv::DNS::NameString。擷取的位址會是 Resolv::IPv4Resolv::IPv6

# File lib/resolv.rb, line 392
def getaddresses(name)
  ret = []
  each_address(name) {|address| ret << address}
  return ret
end
getname(address) 按一下以切換來源

DNS 解析器取得 address 的主機名稱。

address 必須是 Resolv::IPv4Resolv::IPv6String。擷取的名稱會是 Resolv::DNS::Name

# File lib/resolv.rb, line 433
def getname(address)
  each_name(address) {|name| return name}
  raise ResolvError.new("DNS result has no information for #{address}")
end
getnames(address) 按一下以切換來源

DNS 解析器取得 address 的所有主機名稱。

address 必須是 Resolv::IPv4Resolv::IPv6String。擷取的名稱會是 Resolv::DNS::Name 實例。

# File lib/resolv.rb, line 444
def getnames(address)
  ret = []
  each_name(address) {|name| ret << name}
  return ret
end
getresource(name, typeclass) 按一下以切換來源

查詢 nametypeclass DNS 資源。

name 必須是 Resolv::DNS::NameString

typeclass 應為下列其中一個

傳回的資源表示為 Resolv::DNS::Resource 實例,例如 Resolv::DNS::Resource::IN::A

# File lib/resolv.rb, line 496
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) 按一下以切換來源

查詢 name 的所有 typeclass DNS 資源。請參閱 getresource 以取得引數詳細資料。

# File lib/resolv.rb, line 505
def getresources(name, typeclass)
  ret = []
  each_resource(name, typeclass) {|resource| ret << resource}
  return ret
end
timeouts=(values) 按一下以切換來源

設定解析器逾時。這可能是單一正數或表示秒為單位的逾時陣列。如果指定陣列,DNS 要求會重試並等待陣列中每個後續間隔,直到收到成功的回應。指定 nil 會回復為預設逾時

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

範例

dns.timeouts = 3
# File lib/resolv.rb, line 350
def timeouts=(values)
  @config.timeouts = values
end