类 Resolv::DNS
Resolv::DNS
是一个 DNS
桩解析器。
信息来自以下位置
-
STD0013
-
RFC 1035
-
等等。
常量
公共类方法
创建一个新的 DNS
解析器。
config_info
可以是
- nil
-
使用 /etc/resolv.conf。
String
-
使用 /etc/resolv.conf 格式的文件路径。
Hash
-
必须包含 :nameserver、:search 和 :ndots 键。
:nameserver_port 可用于指定 nameserver 地址的端口号。:raise_timeout_errors 可用于将超时错误作为异常抛出,而不是将其视为 NXDOMAIN 响应。
:nameserver 的值应为地址字符串或地址字符串数组。
-
:nameserver => ‘8.8.8.8’
-
:nameserver => [‘8.8.8.8’, ‘8.8.4.4’]
:nameserver_port 的值应为 nameserver 地址和端口号对的数组。
-
: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
创建一个新的 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
公共实例方法
关闭 DNS
解析器。
# File lib/resolv.rb, line 367 def close @mutex.synchronize { if @initialized @initialized = false end } end
迭代从 DNS
解析器检索到的 name
的所有 IP 地址。
name
可以是 Resolv::DNS::Name
或 String
。检索到的地址将是 Resolv::IPv4
或 Resolv::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
迭代从 DNS
解析器检索到的 address
的所有主机名。
address
必须是 Resolv::IPv4
、Resolv::IPv6
或 String
。检索到的名称将是 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
迭代 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
# 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
从 DNS
解析器获取 name
的 IP 地址。
name
可以是 Resolv::DNS::Name
或 String
。检索到的地址将是 Resolv::IPv4
或 Resolv::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
从 DNS
解析器获取 name
的所有 IP 地址。
name
可以是 Resolv::DNS::Name
或 String
。检索到的地址将是 Resolv::IPv4
或 Resolv::IPv6
# File lib/resolv.rb, line 392 def getaddresses(name) ret = [] each_address(name) {|address| ret << address} return ret end
从 DNS
解析器获取 address
的主机名。
address
必须是 Resolv::IPv4
、Resolv::IPv6
或 String
。检索到的名称将是 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
从 DNS
解析器获取 address
的所有主机名。
address
必须是 Resolv::IPv4
、Resolv::IPv6
或 String
。检索到的名称将是 Resolv::DNS::Name
实例。
# File lib/resolv.rb, line 444 def getnames(address) ret = [] each_name(address) {|name| ret << name} return ret end
查找 name
的 typeclass
DNS
资源。
name
必须是 Resolv::DNS::Name
或 String
。
typeclass
应该是以下之一
-
Resolv::DNS::Resource::IN::ANY
-
Resolv::DNS::Resource::IN::CNAME
-
Resolv::DNS::Resource::IN::HINFO
-
Resolv::DNS::Resource::IN::MINFO
-
Resolv::DNS::Resource::IN::MX
-
Resolv::DNS::Resource::IN::NS
-
Resolv::DNS::Resource::IN::PTR
-
Resolv::DNS::Resource::IN::SOA
-
Resolv::DNS::Resource::IN::TXT
返回的资源表示为 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
查找 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
设置解析器超时。这可以是单个正数,也可以是表示以秒为单位的超时的正数数组。如果指定了数组,则 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