class Gem::Resolv::DNS

Gem::Resolv::DNS 是一个 DNS 存根解析器。

信息取自以下位置

常量

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]]

示例

Gem::Resolv::DNS.new(:nameserver => ['210.251.121.21'],
                :search => ['ruby-lang.org'],
                :ndots => 1)
# File rubygems/vendor/resolv/lib/resolv.rb, line 335
def initialize(config_info=nil)
  @mutex = Thread::Mutex.new
  @config = Config.new(config_info)
  @initialized = nil
end
open(*args) { |dns| ... } 点击切换源代码

创建一个新的 DNS 解析器。有关参数详细信息,请参见 Gem::Resolv::DNS.new

如果给定块,则将创建的 DNS 解析器传递给该块,否则返回该解析器。

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

公共实例方法

close() 点击切换源代码

关闭 DNS 解析器。

# File rubygems/vendor/resolv/lib/resolv.rb, line 369
def close
  @mutex.synchronize {
    if @initialized
      @initialized = false
    end
  }
end
each_address(name) { |address| ... } 点击切换源代码

迭代从 DNS 解析器检索到的 name 的所有 IP 地址。

name 可以是 Gem::Resolv::DNS::Name 或字符串。检索到的地址将是 Gem::Resolv::IPv4Gem::Resolv::IPv6

# File rubygems/vendor/resolv/lib/resolv.rb, line 407
def each_address(name)
  if use_ipv6?
    each_resource(name, Resource::IN::AAAA) {|resource| yield resource.address}
  end
  each_resource(name, Resource::IN::A) {|resource| yield resource.address}
end
each_name(address) { |name| ... } 点击切换源代码

迭代从 DNS 解析器检索到的 address 的所有主机名。

address 必须是 Gem::Resolv::IPv4Gem::Resolv::IPv6 或字符串。检索到的名称将是 Gem::Resolv::DNS::Name 实例。

# File rubygems/vendor/resolv/lib/resolv.rb, line 461
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 rubygems/vendor/resolv/lib/resolv.rb, line 519
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 rubygems/vendor/resolv/lib/resolv.rb, line 525
def fetch_resource(name, typeclass)
  lazy_initialize
  truncated = {}
  requesters = {}
  udp_requester = begin
    make_udp_requester
  rescue Errno::EACCES
    # fall back to TCP
  end
  senders = {}

  begin
    @config.resolv(name) do |candidate, tout, nameserver, port|
      msg = Message.new
      msg.rd = 1
      msg.add_question(candidate, typeclass)

      requester = requesters.fetch([nameserver, port]) do
        if !truncated[candidate] && udp_requester
          udp_requester
        else
          requesters[[nameserver, port]] = make_tcp_requester(nameserver, port)
        end
      end

      unless sender = senders[[candidate, requester, nameserver, port]]
        sender = requester.sender(msg, candidate, nameserver, port)
        next if !sender
        senders[[candidate, requester, 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
          # Retry via TCP:
          truncated[candidate] = true
          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
    end
  ensure
    udp_requester&.close
    requesters.each_value { |requester| requester&.close }
  end
end
getaddress(name) 点击切换源代码

DNS 解析器获取 name 的 IP 地址。

name 可以是 Gem::Resolv::DNS::Name 或字符串。检索到的地址将是 Gem::Resolv::IPv4Gem::Resolv::IPv6

# File rubygems/vendor/resolv/lib/resolv.rb, line 383
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 可以是 Gem::Resolv::DNS::Name 或字符串。检索到的地址将是 Gem::Resolv::IPv4Gem::Resolv::IPv6

# File rubygems/vendor/resolv/lib/resolv.rb, line 394
def getaddresses(name)
  ret = []
  each_address(name) {|address| ret << address}
  return ret
end
getname(address) 点击切换源代码

DNS 解析器获取 address 的主机名。

address 必须是 Gem::Resolv::IPv4Gem::Resolv::IPv6 或字符串。检索到的名称将是 Gem::Resolv::DNS::Name

# File rubygems/vendor/resolv/lib/resolv.rb, line 437
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 必须是 Gem::Resolv::IPv4Gem::Resolv::IPv6 或字符串。检索到的名称将是 Gem::Resolv::DNS::Name 实例。

# File rubygems/vendor/resolv/lib/resolv.rb, line 448
def getnames(address)
  ret = []
  each_name(address) {|name| ret << name}
  return ret
end
getresource(name, typeclass) 点击切换源代码

查找 nametypeclass DNS 资源。

name 必须是 Gem::Resolv::DNS::Name 或字符串。

typeclass 应为以下之一

返回的资源表示为 Gem::Resolv::DNS::Resource 实例,例如 Gem::Resolv::DNS::Resource::IN::A

# File rubygems/vendor/resolv/lib/resolv.rb, line 500
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 rubygems/vendor/resolv/lib/resolv.rb, line 509
def getresources(name, typeclass)
  ret = []
  each_resource(name, typeclass) {|resource| ret << resource}
  return ret
end
timeouts=(values) 点击切换源代码

设置解析器超时。这可能是一个正数或表示秒数超时的正数数组。如果指定了一个数组,则 DNS 请求将重试,并等待数组中的每个连续间隔,直到收到成功的响应。指定 nil 将恢复为默认超时

5,第二个 = 5 * 2 / 名称服务器计数,2 * 第二个,4 * 第二个

示例

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