From 089ee38d1773fb761c5b39b31b15d60a0f226cba Mon Sep 17 00:00:00 2001 From: Patrick J Cherry Date: Thu, 11 Dec 2014 13:34:33 +0000 Subject: Resolve DNS server name to an IP before connecting, so ruby knows which protocol to use. --- lib/custodian/protocoltest/dns.rb | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) (limited to 'lib/custodian/protocoltest') diff --git a/lib/custodian/protocoltest/dns.rb b/lib/custodian/protocoltest/dns.rb index b64f128..f96325b 100644 --- a/lib/custodian/protocoltest/dns.rb +++ b/lib/custodian/protocoltest/dns.rb @@ -1,4 +1,5 @@ require 'custodian/settings' +require 'custodian/util/dns' require 'resolv' # @@ -58,6 +59,8 @@ module Custodian @resolve_expected = $3.dup.downcase.split(/[\s,]+/) end + @server_ip = nil + # # Ensure we had all the data. # @@ -112,7 +115,7 @@ module Custodian # if ( !(results - @resolve_expected).empty? or !(@resolve_expected - results).empty? ) - @error = "DNS server *#{@host}* returned the wrong records for `#{resolve_name} IN #{resolve_type}`.\n\nWe expected:\n * #{resolve_expected.join("\n * ")}\n\nWe got:\n * #{results.join("\n * ")}\n" + @error = "DNS server *#{@host}* (#{@server_ip}) returned the wrong records for @#{resolve_name} IN #{resolve_type}@.\n\nWe expected:\n * #{resolve_expected.join("\n * ")}\n\nWe got:\n * #{results.join("\n * ")}\n" end return @error.nil? @@ -131,20 +134,30 @@ module Custodian timeout( period ) do begin - Resolv::DNS.open(:nameserver=>[server]) do |dns| + # + # Lookup the server IP address first, and record it in an instance variable so we can use it later. + # + @server_ip = Custodian::Util::DNS.hostname_to_ip(server) + if @server_ip.nil? + @error = "Could not resolve DNS server #{server}" + return nil + end + + Resolv::DNS.open(:nameserver=>[server_ip]) do |dns| case ltype when /^A$/ then - dns.getresources(name, Resolv::DNS::Resource::IN::A).map{ |r| results.push( r.address.to_s().downcase ) } + dns.getresources(name, Resolv::DNS::Resource::IN::A).map{ |r| results.push( r.address.to_s() ) } when /^AAAA$/ then - dns.getresources(name, Resolv::DNS::Resource::IN::AAAA).map{ |r| results.push( r.address.to_s().downcase ) } + dns.getresources(name, Resolv::DNS::Resource::IN::AAAA).map{ |r| results.push( r.address.to_s() ) } when /^NS$/ then - dns.getresources(name, Resolv::DNS::Resource::IN::NS).map{ |r| results.push( Resolv.getaddresses( r.name.to_s().downcase ) ) } + dns.getresources(name, Resolv::DNS::Resource::IN::NS).map{ |r| results.push( Resolv.getaddresses( r.name.to_s() ) ) } when /^MX$/ then - dns.getresources(name, Resolv::DNS::Resource::IN::MX).map{ |r| results.push( Resolv.getaddresses( r.exchange.to_s().downcase ) ) } + dns.getresources(name, Resolv::DNS::Resource::IN::MX).map{ |r| results.push( Resolv.getaddresses( r.exchange.to_s() ) ) } + else @error = "Unknown record type to resolve: '#{ltype}'" return nil @@ -152,20 +165,20 @@ module Custodian end rescue StandardError => x - @error = "Exception was received when resolving : #{x}" + @error = "Exception was received when resolving: #{x}" return nil end end rescue Timeout::Error => e - @error = "Timed-out performing DNS lookups #{e}" + @error = "Timed-out performing DNS lookups: #{e}" return nil end # # Flatten, sort, uniq # - results.flatten.sort.uniq + results.flatten.map{|r| r.to_s.downcase }.sort.uniq end -- cgit v1.2.1