Class: Resolv::DNS
Relationships & Source Files | |
Namespace Children | |
Modules:
| |
Classes:
| |
Exceptions:
| |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
|
|
Inherits: | Object |
Defined in: | lib/resolv.rb |
Overview
DNS
is a DNS
stub resolver.
Information taken from the following places:
-
STD0013
-
RFC 1035
-
etc.
Constant Summary
-
Port =
Default DNS
Port
53
-
RequestID =
Internal use only
# File 'lib/resolv.rb', line 632{}
-
RequestIDMutex =
Internal use only
# File 'lib/resolv.rb', line 633Thread::Mutex.new
-
UDPSize =
Default DNS UDP packet size
512
Class Method Summary
-
.new(config_info = nil) ⇒ DNS
constructor
Creates a new
DNS
resolver. -
.open(*args)
Creates a new
DNS
resolver. - .allocate_request_id(host, port) Internal use only
- .bind_random_port(udpsock, bind_host = "0.0.0.0") Internal use only
- .free_request_id(host, port, id) Internal use only
- .random(arg) Internal use only
Instance Attribute Summary
-
#timeouts=(values)
writeonly
Sets the resolver timeouts.
- #use_ipv6? ⇒ Boolean readonly private Internal use only
Instance Method Summary
-
#close
Closes the
DNS
resolver. -
#each_address(name)
Iterates over all IP addresses for
name
retrieved from theDNS
resolver. -
#each_name(address)
Iterates over all hostnames for
address
retrieved from theDNS
resolver. -
#each_resource(name, typeclass, &proc)
Iterates over all
typeclass
DNS
resources forname
. - #fetch_resource(name, typeclass)
-
#getaddress(name)
Gets the IP address of
name
from theDNS
resolver. -
#getaddresses(name)
Gets all IP addresses for
name
from theDNS
resolver. -
#getname(address)
Gets the hostname for
address
from theDNS
resolver. -
#getnames(address)
Gets all hostnames for
address
from theDNS
resolver. -
#getresource(name, typeclass)
Look up the
typeclass
DNS
resource ofname
. -
#getresources(name, typeclass)
Looks up all
typeclass
DNS
resources forname
. - #extract_resources(msg, name, typeclass) Internal use only
- #lazy_initialize Internal use only
- #make_tcp_requester(host, port) Internal use only
- #make_udp_requester Internal use only
Constructor Details
.new(config_info = nil) ⇒ DNS
Creates a new DNS
resolver.
config_info
can be:
- nil
-
Uses /etc/resolv.conf.
- String
-
Path to a file using /etc/resolv.conf’s format.
- Hash
-
Must contain
:nameserver
,:search
and:ndots
keys.
:nameserver_port
can be used to specify port number of nameserver address. :raise_timeout_errors
can be used to raise timeout errors as exceptions instead of treating the same as an NXDOMAIN response.
The value of :nameserver
should be an address string or an array of address strings.
-
:nameserver
=> ‘8.8.8.8’ -
:nameserver
=> [‘8.8.8.8’, ‘8.8.4.4’]
The value of :nameserver_port
should be an array of pair of nameserver address and port number.
-
:nameserver_port
=> [[‘8.8.8.8’, 53], [‘8.8.4.4’, 53]]
Example:
Resolv::DNS.new(:nameserver => ['210.251.121.21'],
:search => ['ruby-lang.org'],
:ndots => 1)
Class Method Details
.allocate_request_id(host, port)
# File 'lib/resolv.rb', line 635
def self.allocate_request_id(host, port) # :nodoc: id = nil RequestIDMutex.synchronize { h = (RequestID[[host, port]] ||= {}) begin id = random(0x0000..0xffff) end while h[id] h[id] = true } id end
.bind_random_port(udpsock, bind_host = "0.0.0.0")
# File 'lib/resolv.rb', line 659
def self.bind_random_port(udpsock, bind_host="0.0.0.0") # :nodoc: begin port = random(1024..65535) udpsock.bind(bind_host, port) rescue Errno::EADDRINUSE, # POSIX Errno::EACCES, # SunOS: See PRIV_SYS_NFS in privileges(5) Errno::EPERM # FreeBSD: security.mac.portacl.port_high is configurable. See mac_portacl(4). retry end end
.free_request_id(host, port, id)
# File 'lib/resolv.rb', line 647
def self.free_request_id(host, port, id) # :nodoc: RequestIDMutex.synchronize { key = [host, port] if h = RequestID[key] h.delete id if h.empty? RequestID.delete key end end } end
.open(*args)
Creates a new DNS
resolver. See .new for argument details.
Yields the created DNS
resolver to the block, if given, otherwise returns it.
.random(arg)
# File 'lib/resolv.rb', line 619
def self.random(arg) # :nodoc: begin SecureRandom.random_number(arg) rescue NotImplementedError rand(arg) end end
Instance Attribute Details
#timeouts=(values) (writeonly)
Sets the resolver timeouts. This may be a single positive number or an array of positive numbers representing timeouts in seconds. If an array is specified, a DNS
request will retry and wait for each successive interval in the array until a successful response is received. Specifying nil
reverts to the default timeouts:
- 5, second = 5 * 2 / nameserver_count, 2 * second, 4 * second
-
Example:
dns.timeouts = 3
# File 'lib/resolv.rb', line 343
def timeouts=(values) @config.timeouts = values end
#use_ipv6? ⇒ Boolean
(readonly, private)
# File 'lib/resolv.rb', line 405
def use_ipv6? # :nodoc: @config.lazy_initialize unless @config.instance_variable_get(:@initialized) use_ipv6 = @config.use_ipv6? unless use_ipv6.nil? return use_ipv6 end begin list = Socket.ip_address_list rescue NotImplementedError return true end list.any? {|a| a.ipv6? && !a.ipv6_loopback? && !a.ipv6_linklocal? } end
Instance Method Details
#close
Closes the DNS
resolver.
# File 'lib/resolv.rb', line 360
def close @mutex.synchronize { if @initialized @initialized = false end } end
#each_address(name)
# File 'lib/resolv.rb', line 398
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)
# File 'lib/resolv.rb', line 452
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)
Iterates over all typeclass
DNS
resources for name
. See #getresource for argument details.
# File 'lib/resolv.rb', line 510
def each_resource(name, typeclass, &proc) fetch_resource(name, typeclass) {|reply, reply_name| extract_resources(reply, reply_name, typeclass, &proc) } end
#extract_resources(msg, name, typeclass)
# File 'lib/resolv.rb', line 587
def extract_resources(msg, name, typeclass) # :nodoc: if typeclass < Resource::ANY n0 = Name.create(name) msg.each_resource {|n, ttl, data| yield data if n0 == n } end yielded = false n0 = Name.create(name) msg.each_resource {|n, ttl, data| if n0 == n case data when typeclass yield data yielded = true when Resource::CNAME n0 = data.name end end } return if yielded msg.each_resource {|n, ttl, data| if n0 == n case data when typeclass yield data end end } end
#fetch_resource(name, typeclass)
[ GitHub ]# File 'lib/resolv.rb', line 516
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)
# File 'lib/resolv.rb', line 374
def getaddress(name) each_address(name) {|address| return address} raise ResolvError.new("DNS result has no information for #{name}") end
#getaddresses(name)
# File 'lib/resolv.rb', line 385
def getaddresses(name) ret = [] each_address(name) {|address| ret << address} return ret end
#getname(address)
# File 'lib/resolv.rb', line 428
def getname(address) each_name(address) {|name| return name} raise ResolvError.new("DNS result has no information for #{address}") end
#getnames(address)
# File 'lib/resolv.rb', line 439
def getnames(address) ret = [] each_name(address) {|name| ret << name} return ret end
#getresource(name, typeclass)
Look up the typeclass
DNS
resource of name
.
name
must be a DNS::Name
or a String.
typeclass
should be one of the following:
-
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
Returned resource is represented as a DNS::Resource
instance, i.e. DNS::Resource::IN::A
.
# File 'lib/resolv.rb', line 491
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)
Looks up all typeclass
DNS
resources for name
. See #getresource for argument details.
# File 'lib/resolv.rb', line 500
def getresources(name, typeclass) ret = [] each_resource(name, typeclass) {|resource| ret << resource} return ret end
#lazy_initialize
# File 'lib/resolv.rb', line 347
def lazy_initialize # :nodoc: @mutex.synchronize { unless @initialized @config.lazy_initialize @initialized = true end } self end
#make_tcp_requester(host, port)
# File 'lib/resolv.rb', line 578
def make_tcp_requester(host, port) # :nodoc: return Requester::TCP.new(host, port) rescue Errno::ECONNREFUSED # Treat a refused TCP connection attempt to a nameserver like a timeout, # as Resolv::DNS::Config#resolv considers ResolvTimeout exceptions as a # hint to try the next nameserver: raise ResolvTimeout end
#make_udp_requester
# File 'lib/resolv.rb', line 569
def make_udp_requester # :nodoc: nameserver_port = @config.nameserver_port if nameserver_port.length == 1 Requester::ConnectedUDP.new(*nameserver_port[0]) else Requester::UnconnectedUDP.new(*nameserver_port) end end