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

RESOLV_CONF =

– TODO check if it’s caching right

'/etc/resolv.conf'
HOSTS =
'/etc/hosts'
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



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cool.io/dns_resolver.rb', line 62

def initialize(hostname, *nameservers)
  if nameservers.empty?
    nameservers = File.read(RESOLV_CONF).scan(/^\s*nameserver\s+([0-9.:]+)/).flatten # TODO could optimize this to just read once
    raise RuntimeError, "no nameservers found in #{RESOLV_CONF}" 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 = HOSTS) ⇒ Object

Query /etc/hosts (or the specified hostfile) for the given host



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cool.io/dns_resolver.rb', line 45

def self.hosts(host, hostfile = HOSTS)
  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

  hosts[host]
end

Instance Method Details

#attach(evloop) ⇒ Object

Attach the DNSResolver to the given event loop



78
79
80
81
82
# File 'lib/cool.io/dns_resolver.rb', line 78

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

#detachObject

Detach the DNSResolver from the given event loop



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

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

#on_failureObject

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



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

def on_failure; end

#on_success(address) ⇒ Object

Called when the name has successfully resolved to an address



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

def on_success(address); end

#on_timeoutObject

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



99
100
101
# File 'lib/cool.io/dns_resolver.rb', line 99

def on_timeout
  on_failure
end