Class: Rev::DNSResolver

Inherits:
IOWatcher show all
Defined in:
lib/rev/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 Rev::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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rev/dns_resolver.rb', line 54

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



39
40
41
42
43
44
45
46
47
48
# File 'lib/rev/dns_resolver.rb', line 39

def self.hosts(host, hostfile = HOSTS)
  hosts = {}
  File.open(hostfile).each_line do |host_entry|
    entries = host_entry.gsub(/#.*$/, '').gsub(/\s+/, ' ').split(' ')
    addr = entries.shift
    entries.each { |e| hosts[e] ||= addr }
  end

  hosts[host]
end

Instance Method Details

#attach(evloop) ⇒ Object

Attach the DNSResolver to the given event loop



70
71
72
73
74
# File 'lib/rev/dns_resolver.rb', line 70

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

#detachObject

Detach the DNSResolver from the given event loop



77
78
79
80
# File 'lib/rev/dns_resolver.rb', line 77

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

#on_failureObject

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



87
# File 'lib/rev/dns_resolver.rb', line 87

def on_failure; end

#on_success(address) ⇒ Object

Called when the name has successfully resolved to an address



83
# File 'lib/rev/dns_resolver.rb', line 83

def on_success(address); end

#on_timeoutObject

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



91
92
93
# File 'lib/rev/dns_resolver.rb', line 91

def on_timeout
  on_failure
end