Class: EventMachine::DNS::Resolver

Inherits:
Object
  • Object
show all
Defined in:
lib/em/resolver.rb

Constant Summary collapse

HOSTS_FILE =
windows? ? Win32::Resolv.get_hosts_path : '/etc/hosts'

Class Method Summary collapse

Class Method Details

.hostsObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/em/resolver.rb', line 60

def self.hosts
  return @hosts if @hosts

  @hosts = {}
  IO.readlines(HOSTS_FILE).each do |line|
    next if line =~ /^#/
    addr, host = line.split(/\s+/)

    next unless addr && host
    @hosts[host] ||= []
    @hosts[host] << addr
  end

  @hosts
rescue
  @hosts = {}
end

.nameserverObject



56
57
58
# File 'lib/em/resolver.rb', line 56

def self.nameserver
  nameservers.shuffle.first
end

.nameserversObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/em/resolver.rb', line 36

def self.nameservers
  return @nameservers if @nameservers

  if windows?
    _, ns = Win32::Resolv.get_resolv_info
    return @nameservers = ns || []
  end

  @nameservers = []
  IO.readlines('/etc/resolv.conf').each do |line|
    if line =~ /^nameserver (.+)$/
      @nameservers << $1.split(/\s+/).first
    end
  end

  @nameservers
rescue
  @nameservers = []
end

.nameservers=(ns) ⇒ Object



32
33
34
# File 'lib/em/resolver.rb', line 32

def self.nameservers=(ns)
  @nameservers = ns
end

.resolve(hostname) ⇒ Object



20
21
22
# File 'lib/em/resolver.rb', line 20

def self.resolve(hostname)
  Request.new(socket, hostname)
end

.socketObject



24
25
26
27
28
29
30
# File 'lib/em/resolver.rb', line 24

def self.socket
  if @socket && @socket.error?
    @socket = Socket.open
  else
    @socket ||= Socket.open
  end
end

.windows?Boolean

Returns:

  • (Boolean)


5
6
7
8
9
10
11
12
# File 'lib/em/resolver.rb', line 5

def self.windows?
  if RUBY_PLATFORM =~ /mswin32|cygwin|mingw|bccwin/
    require 'win32/resolv'
    true
  else
    false
  end
end