Class: Coolio::DNSResolver

Inherits:
IOWatcher show all
Defined in:
lib/cool.io/dns_resolver.rb

Overview

A non-blocking DNS resolver. It provides interfaces for querying both /etc/hosts and nameserves listed in /etc/resolv.conf, or nameservers of your choosing.

Presently the client only supports UDP requests against your nameservers and cannot resolve anything with records larger than 512-bytes. Also, IPv6 is not presently supported.

DNSResolver objects are one-shot. Once they resolve a domain name they automatically detach themselves from the event loop and cannot be used again.

Direct Known Subclasses

TCPSocket::TCPConnectResolver

Defined Under Namespace

Classes: Timeout

Constant Summary collapse

DNS_PORT =

53
DATAGRAM_SIZE =
512
TIMEOUT =

Retry timeout for each datagram sent

3
RETRIES =

Number of retries to attempt

4

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from IOWatcher

#disable, #enable, #on_writable

Methods included from Meta

#event_callback, #watcher_delegate

Methods inherited from Watcher

#attached?, #disable, #enable, #enabled?, #evloop

Constructor Details

#initialize(hostname, *nameservers) ⇒ DNSResolver

Create a new Coolio::Watcher descended object to resolve the given hostname. If you so desire you can also specify a list of nameservers to query. By default the resolver will use nameservers listed in /etc/resolv.conf


69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/cool.io/dns_resolver.rb', line 69

def initialize(hostname, *nameservers)
  if nameservers.empty?
    nameservers = Resolv::DNS::Config.default_config_hash[:nameserver]
    raise RuntimeError, "no nameservers found" if nameservers.empty? # TODO just call resolve_failed, not raise [also handle Errno::ENOENT)]
  end

  @nameservers = nameservers
  @question = request_question hostname

  @socket = UDPSocket.new
  @timer = Timeout.new(self)

  super(@socket)
end

Class Method Details

.hosts(host, hostfile = Resolv::Hosts::DefaultFileName) ⇒ Object

so currently total is 12s before it will err due to timeouts if it errs due to inability to reach the DNS server [Errno::EHOSTUNREACH], same Query /etc/hosts (or the specified hostfile) for the given host


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cool.io/dns_resolver.rb', line 43

def self.hosts(host, hostfile = Resolv::Hosts::DefaultFileName)
  hosts = {}
  File.open(hostfile) do |f|
    f.each_line do |host_entry|
      entries = host_entry.gsub(/#.*$/, '').gsub(/\s+/, ' ').split(' ')
      addr = entries.shift
      entries.each { |e| hosts[e] ||= addr }
    end
  end
  if hosts.empty?
    # On Windows, there is a case that hosts file doesn't have entry by default
    # and preferred IPv4/IPv6 behavior may be changed by registry key [1], so
    # "localhost" should be resolved by getaddrinfo.
    # (first[3] means preferred resolved IP address ::1 or 127.0.0.1)
    # [1] https://docs.microsoft.com/en-us/troubleshoot/windows-server/networking/configure-ipv6-in-windows
    require "socket"
    hosts["localhost"] = ::Socket.getaddrinfo("localhost", nil).first[3]
  end

  hosts[host]
end

Instance Method Details

#attach(evloop) ⇒ Object

Attach the DNSResolver to the given event loop


85
86
87
88
89
# File 'lib/cool.io/dns_resolver.rb', line 85

def attach(evloop)
  send_request
  @timer.attach(evloop)
  super
end

#detachObject

Detach the DNSResolver from the given event loop


92
93
94
95
# File 'lib/cool.io/dns_resolver.rb', line 92

def detach
  @timer.detach if @timer.attached?
  super
end

#on_failureObject

Called when we receive a response indicating the name didn't resolve


102
# File 'lib/cool.io/dns_resolver.rb', line 102

def on_failure; end

#on_success(address) ⇒ Object

Called when the name has successfully resolved to an address


98
# File 'lib/cool.io/dns_resolver.rb', line 98

def on_success(address); end

#on_timeoutObject

Called if we don't receive a response, defaults to calling on_failure


106
107
108
# File 'lib/cool.io/dns_resolver.rb', line 106

def on_timeout
  on_failure
end