Module: Net::DNS

Defined in:
lib/net/dns.rb,
lib/net/dns/a_record.rb,
lib/net/dns/ptr_record.rb

Defined Under Namespace

Classes: ARecord, PTRRecord, Record

Class Method Summary collapse

Class Method Details

.lookup(query, proxy, resolver = Resolv::DNS.new) ⇒ Object

Looks up the IP or MAC address. Handles the conversion of a DNS miss exception into nil [query]: IP or hostname Returns: a new DNS record object, A or PTR accordingly We query DNS directly, as its faster then to query our own proxy.


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/net/dns.rb', line 14

def self.lookup query, proxy, resolver = Resolv::DNS.new
  Timeout::timeout(3) do
    if (query =~ Validations::IP_REGEXP)
      n = resolver.getname(query).to_s
      i = query
      t = "PTR"
    else
      i = resolver.getaddress(query).to_s
      n = query
      t = "A"
    end
    attrs = { :hostname => n, :ip => i, :resolver => resolver, :proxy => proxy }
    case t
      when "A"
        ARecord.new attrs
      when "PTR"
        PTRRecord.new attrs
    end
  end
rescue Resolv::ResolvError, SocketError
  nil
rescue Timeout::Error => e
  raise Net::Error, e
end