summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorSteve Kemp <steve@steve.org.uk>2017-08-08 15:59:53 +0300
committerSteve Kemp <steve@steve.org.uk>2017-08-08 15:59:53 +0300
commit3be19cc5ba17e084c10c8f5d0eb114b01e53f733 (patch)
tree1b56b343823ade1425cdb8be9919179586b65747 /lib
parent70b13e92297905631629a05e827d70e98c3f422d (diff)
Use a case-statement for both kinds of IP-matching.
Diffstat (limited to 'lib')
-rw-r--r--lib/custodian/protocoltest/http.rb64
1 files changed, 36 insertions, 28 deletions
diff --git a/lib/custodian/protocoltest/http.rb b/lib/custodian/protocoltest/http.rb
index 32073f8..9ff734c 100644
--- a/lib/custodian/protocoltest/http.rb
+++ b/lib/custodian/protocoltest/http.rb
@@ -61,44 +61,52 @@ module Custodian
#
def ignore_failure?( protocol )
- # IP addresses we found for the host
- ips = []
-
# Get the hostname we're connecting to.
u = URI.parse(@url)
target = u.host
- #
- # Resolve the target to an IP, unless it is already an address.
- #
- if (target =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/) ||
- (target =~ /^([0-9a-f:]+)$/)
- ips.push(target)
+ # IPs for the target
+ ips = []
+
+ case protocol
+ when :ipv4
+ if (target =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/)
+ ips << target
+ end
+ when :ipv6
+ if (target =~ /^([0-9a-f:]+)$/)
+ ips << target
+ end
else
+ raise ArgumentError, "Sanity-checking DNS-failure of unknown type: #{protocol}"
+ end
- #
- # OK if it didn't look like an IP address then attempt to
- # look it up, as both IPv4 and IPv6.
- #
- begin
- timeout(30) do
+ # Early termination?
+ return true unless ips.empty?
+ #
+ # OK if it didn't look like an IP address then attempt to
+ # look it up, as both IPv4 and IPv6.
+ #
+ begin
+ timeout(30) do
+
+ type = case protocol
+ when :ipv4
+ Resolv::DNS::Resource::IN::A
+ when :ipv6
+ Resolv::DNS::Resource::IN::AAAA
+ else
+ raise ArgumentError, "Sanity-checking DNS-failure of unknown type: #{protocol}"
+ end
+
+ begin
Resolv::DNS.open do |dns|
-
- if ( protocol == :ipv4 )
- ress = dns.getresources(target, Resolv::DNS::Resource::IN::A)
- ress.map { |r| ips.push(r.address.to_s) }
- elsif ( protocol == :ipv6 )
-
- ress = dns.getresources(target, Resolv::DNS::Resource::IN::AAAA)
- ress.map { |r| ips.push(r.address.to_s) }
- else
- raise ArgumentError, "Sanity-checking DNS-failure of unknown type"
- end
+ ips = dns.getresources(target, type)
end
+ rescue Timeout::Error => _e
+ # NOP
end
- rescue Timeout::Error => _e
- # NOP
end
end