Class: Coolio::DNSResolver
- 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
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
-
.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.
Instance Method Summary collapse
-
#attach(evloop) ⇒ Object
Attach the DNSResolver to the given event loop.
-
#detach ⇒ Object
Detach the DNSResolver from the given event loop.
-
#initialize(hostname, *nameservers) ⇒ DNSResolver
constructor
Create a new Coolio::Watcher descended object to resolve the given hostname.
-
#on_failure ⇒ Object
Called when we receive a response indicating the name didn’t resolve.
-
#on_success(address) ⇒ Object
Called when the name has successfully resolved to an address.
-
#on_timeout ⇒ Object
Called if we don’t receive a response, defaults to calling on_failure.
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
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/cool.io/dns_resolver.rb', line 60 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 |
# 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 hosts[host] end |
Instance Method Details
#attach(evloop) ⇒ Object
Attach the DNSResolver to the given event loop
76 77 78 79 80 |
# File 'lib/cool.io/dns_resolver.rb', line 76 def attach(evloop) send_request @timer.attach(evloop) super end |
#detach ⇒ Object
Detach the DNSResolver from the given event loop
83 84 85 86 |
# File 'lib/cool.io/dns_resolver.rb', line 83 def detach @timer.detach if @timer.attached? super end |
#on_failure ⇒ Object
Called when we receive a response indicating the name didn’t resolve
93 |
# File 'lib/cool.io/dns_resolver.rb', line 93 def on_failure; end |
#on_success(address) ⇒ Object
Called when the name has successfully resolved to an address
89 |
# File 'lib/cool.io/dns_resolver.rb', line 89 def on_success(address); end |
#on_timeout ⇒ Object
Called if we don’t receive a response, defaults to calling on_failure
97 98 99 |
# File 'lib/cool.io/dns_resolver.rb', line 97 def on_timeout on_failure end |