类 Resolv

Resolv 是一个用 Ruby 编写的线程感知 DNS 解析器库。 Resolv 可以同时处理多个 DNS 请求,而不会阻塞整个 Ruby 解释器。

另请参阅 resolv-replace.rb,以将 libc 解析器替换为 Resolv

Resolv 可以使用 DNS 模块直接查找各种 DNS 资源。

示例

p Resolv.getaddress "www.ruby-lang.org"
p Resolv.getname "210.251.121.214"

Resolv::DNS.open do |dns|
  ress = dns.getresources "www.ruby-lang.org", Resolv::DNS::Resource::IN::A
  p ress.map(&:address)
  ress = dns.getresources "ruby-lang.org", Resolv::DNS::Resource::IN::MX
  p ress.map { |r| [r.exchange.to_s, r.preference] }
end

错误

常量

AddressRegex

用于匹配 IP 地址的 Address Regexp

DefaultResolver

用于 Resolv 类方法的默认解析器。

VERSION

公共类方法

each_address(name, &block) 点击切换源代码

迭代 name 的所有 IP 地址。

# File lib/resolv.rb, line 59
def self.each_address(name, &block)
  DefaultResolver.each_address(name, &block)
end
each_name(address, &proc) 点击切换源代码

迭代 address 的所有主机名。

# File lib/resolv.rb, line 80
def self.each_name(address, &proc)
  DefaultResolver.each_name(address, &proc)
end
getaddress(name) 点击切换源代码

查找 name 的第一个 IP 地址。

# File lib/resolv.rb, line 45
def self.getaddress(name)
  DefaultResolver.getaddress(name)
end
getaddresses(name) 点击切换源代码

查找 name 的所有 IP 地址。

# File lib/resolv.rb, line 52
def self.getaddresses(name)
  DefaultResolver.getaddresses(name)
end
getname(address) 点击切换源代码

查找 address 的主机名。

# File lib/resolv.rb, line 66
def self.getname(address)
  DefaultResolver.getname(address)
end
getnames(address) 点击切换源代码

查找 address 的所有主机名。

# File lib/resolv.rb, line 73
def self.getnames(address)
  DefaultResolver.getnames(address)
end
new(resolvers=nil, use_ipv6: nil) 点击切换源代码

使用 `resolvers` 创建一个新的 Resolv

# File lib/resolv.rb, line 87
def initialize(resolvers=nil, use_ipv6: nil)
  @resolvers = resolvers || [Hosts.new, DNS.new(DNS::Config.default_config_hash.merge(use_ipv6: use_ipv6))]
end

公共实例方法

each_address(name) { |name| ... } 点击切换源代码

迭代 name 的所有 IP 地址。

# File lib/resolv.rb, line 111
def each_address(name)
  if AddressRegex =~ name
    yield name
    return
  end
  yielded = false
  @resolvers.each {|r|
    r.each_address(name) {|address|
      yield address.to_s
      yielded = true
    }
    return if yielded
  }
end
each_name(address) { |name| ... } 点击切换源代码

迭代 address 的所有主机名。

# File lib/resolv.rb, line 146
def each_name(address)
  yielded = false
  @resolvers.each {|r|
    r.each_name(address) {|name|
      yield name.to_s
      yielded = true
    }
    return if yielded
  }
end
getaddress(name) 点击切换源代码

查找 name 的第一个 IP 地址。

# File lib/resolv.rb, line 94
def getaddress(name)
  each_address(name) {|address| return address}
  raise ResolvError.new("no address for #{name}")
end
getaddresses(name) 点击切换源代码

查找 name 的所有 IP 地址。

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

查找 address 的主机名。

# File lib/resolv.rb, line 129
def getname(address)
  each_name(address) {|name| return name}
  raise ResolvError.new("no name for #{address}")
end
getnames(address) 点击切换源代码

查找 address 的所有主机名。

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