lib/reversed.rb in reversed-0.2.1 vs lib/reversed.rb in reversed-0.3.0
- old
+ new
@@ -1,24 +1,36 @@
require "reversed/version"
require "net/dns"
+require "ipaddr"
module Reversed
def self.lookup(ip, timeout: 5, nameservers: nil)
ip = ip.to_s
unless ip.empty?
+ begin
+ # ensure valid ip
+ ip = IPAddr.new(ip)
+ rescue IPAddr::InvalidAddressError
+ raise ArgumentError, "Invalid IP"
+ end
+
options = {
- retry: 3,
+ retry_number: 3,
udp_timeout: timeout
}
options[:nameservers] = nameservers if nameservers
begin
- resolver = Net::DNS::Resolver.new(options).search(ip)
+ resolver = Net::DNS::Resolver.new(options).search(ip.reverse, Net::DNS::PTR)
answer = resolver.answer.first || resolver.authority.first
if answer && !answer.value.empty?
answer.value.split(" ").first[0..-2]
end
rescue Net::DNS::Resolver::NoResponseError
nil
+ rescue Errno::EAFNOSUPPORT
+ options[:use_tcp] = true
+ options[:tcp_timeout] = timeout
+ retry
end
end
end
end