class Resolv

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

另请参阅 resolv-replace.rb 以使用 Resolv 替换 libc 解析器。

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

Bug

  • 不支持 NIS。

  • 不支持 /etc/nsswitch.conf。

常量

AddressRegex

用于匹配 IP 地址的地址正则表达式。

DefaultResolver

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

VERSION

公共类方法

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

迭代 `name` 的所有 IP 地址。

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

迭代 `address` 的所有主机名。

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

查找 `name` 的第一个 IP 地址。

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

查找 `name` 的所有 IP 地址。

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

查找 `address` 的主机名。

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

查找 `address` 的所有主机名。

# File resolv.rb, line 69
def self.getnames(address)
  DefaultResolver.getnames(address)
end
new(resolvers=(arg_not_set = true; nil), use_ipv6: (keyword_not_set = true; nil)) 点击切换源代码

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

如果未给出 `resolvers`、哈希或 `nil`,则使用 Hosts 解析器和 DNS 解析器。如果 `resolvers` 是哈希,则使用该哈希作为 DNS 解析器的配置。

# File resolv.rb, line 87
def initialize(resolvers=(arg_not_set = true; nil), use_ipv6: (keyword_not_set = true; nil))
  if !keyword_not_set && !arg_not_set
    warn "Support for separate use_ipv6 keyword is deprecated, as it is ignored if an argument is provided. Do not provide a positional argument if using the use_ipv6 keyword argument.", uplevel: 1
  end

  @resolvers = case resolvers
  when Hash, nil
    [Hosts.new, DNS.new(DNS::Config.default_config_hash.merge(resolvers || {}))]
  else
    resolvers
  end
end

公共实例方法

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

迭代 `name` 的所有 IP 地址。

# File resolv.rb, line 120
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 resolv.rb, line 155
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 resolv.rb, line 103
def getaddress(name)
  each_address(name) {|address| return address}
  raise ResolvError.new("no address for #{name}")
end
getaddresses(name) 点击切换源代码

查找 `name` 的所有 IP 地址。

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

查找 `address` 的主机名。

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

查找 `address` 的所有主机名。

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