Skip to content

Commit

Permalink
use standard api instead of platform dependent literal.
Browse files Browse the repository at this point in the history
  • Loading branch information
taichi committed May 5, 2014
1 parent 1651b27 commit a4c8723
Showing 1 changed file with 3 additions and 8 deletions.
11 changes: 3 additions & 8 deletions lib/cool.io/dns_resolver.rb
Expand Up @@ -17,6 +17,8 @@
#
#++

require 'resolv'

module Coolio
# A non-blocking DNS resolver. It provides interfaces for querying both
# /etc/hosts and nameserves listed in /etc/resolv.conf, or nameservers of
Expand All @@ -31,20 +33,14 @@ module Coolio
# again.
class DNSResolver < IOWatcher
#--
# TODO check if it's caching right
if RUBY_PLATFORM =~ /mingw|win32/
HOSTS = 'c:\WINDOWS\system32\drivers\etc\hosts'
else
HOSTS = '/etc/hosts'
end
DNS_PORT = 53
DATAGRAM_SIZE = 512
TIMEOUT = 3 # Retry timeout for each datagram sent
RETRIES = 4 # Number of retries to attempt
# 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
def self.hosts(host, hostfile = HOSTS)
def self.hosts(host, hostfile = Resolv::Hosts::DefaultFileName)
hosts = {}
File.open(hostfile) do |f|
f.each_line do |host_entry|
Expand All @@ -63,7 +59,6 @@ def self.hosts(host, hostfile = HOSTS)
# use nameservers listed in /etc/resolv.conf
def initialize(hostname, *nameservers)
if nameservers.empty?
require 'resolv'
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
Expand Down

0 comments on commit a4c8723

Please sign in to comment.