lib/net/dns/resolver.rb in net-dns-0.1 vs lib/net/dns/resolver.rb in net-dns-0.2
- old
+ new
@@ -241,74 +241,10 @@
raise ResolverArgumentError, "Option #{key} not valid"
end
end
end
- def Resolver.cname_addr(names,packet)
- addr = []
- oct2 = RegExp.new('(?:2[0-4]\d|25[0-5]|[0-1]?\d\d|\d)')
-
- packet.answer.each do |rr|
- next unless begin
- ret = false
- names.each {|x| ret = true if rr.name.include? x}
- ret
- end
- if rr.type == Net::DNS::CNAME
- names.push(rr.cname)
- elsif rr.type == Net::DNS::A
- next unless rr.address =~ /^(#{oct2}\.#{oct2}\.#{oct2}\.#{oct2})$/
- addr.push($1)
- end
- end
- addr
- end
-
- def _packetsz
- if @udppacketsize > Net::DNS::PACKETSZ
- @udppacketsize
- else
- Net::DNS::PACKETSZ
- end
- end
-
- def send_tcp(packet, packet_data)
- if @nameservers.size == 0
- @errorstring = "no nameservers"
- puts ";; ERROR: send_tcp: no nameservers\n" if $DEBUG
- return nil # ?
- end
-
- self._reset_errorstring
-
- timeout = @tcp_timeout
-
- @nameservers.each do |ns|
- srcport = @srcport
- srcaddr = @srcaddr
- dstport = @port
-
- puts ";; send_tcp(#{ns}:#{dstport}) (src port = #{srcport})" if $DEBUG
-
- sock = TCPSocket.new(ns,dstport)
-
- # TODO !!!
- end
- end
-
- end
- end
-end
-
-
-
-
-
-module Net
- module DNS
- class Resolver
-
# Get the resolver searchlist, returned as an array of entries
#
# res.searchlist
# #=> ["example.com","a.example.com","b.example.com"]
#
@@ -351,11 +287,11 @@
def nameservers
arr = []
@config[:nameservers].each do |x|
arr << x.to_s
end
- arr.inspect
+ arr
end
alias_method :nameserver, :nameservers
# Set the list of resolver nameservers
# +arg+ can be a single ip address or an array of addresses
@@ -816,11 +752,11 @@
#
def log_file=(log)
@logger.close
@config[:log_file] = log
@logger = Logger.new(@config[:log_file])
- @logger.level = $DEBUG ? Logger::Debug : Logger::WARN
+ @logger.level = $DEBUG ? Logger::DEBUG : Logger::WARN
end
# This one permits to have a personal logger facility to handle
# resolver messages, instead of new built-in one, which is set up
# for a +$stdout+ (or +$stderr+) use.
@@ -1017,20 +953,27 @@
private
# Parse a configuration file specified as the argument.
#
def parse_config_file
- IO.foreach(@config[:config_file]) do |line|
- line.gsub!(/\s*[;#].*/,"")
- next unless line =~ /\S/
- case line
- when /^\s*domain\s+(\S+)/
- self.domain = $1
- when /^\s*search\s+(.*)/
- self.searchlist = $1.split(" ")
- when /^\s*nameserver\s+(.*)/
- self.nameservers = $1.split(" ")
+ if RUBY_PLATFORM =~ /win/
+ require 'win32/resolv'
+ arr = Win32::Resolv.get_resolv_info
+ self.domain = arr[0]
+ self.nameservers = arr[1]
+ else
+ IO.foreach(@config[:config_file]) do |line|
+ line.gsub!(/\s*[;#].*/,"")
+ next unless line =~ /\S/
+ case line
+ when /^\s*domain\s+(\S+)/
+ self.domain = $1
+ when /^\s*search\s+(.*)/
+ self.searchlist = $1.split(" ")
+ when /^\s*nameserver\s+(.*)/
+ self.nameservers = $1.split(" ")
+ end
end
end
end
# Parse environment variables
@@ -1140,21 +1083,28 @@
socket = UDPSocket.new
socket.bind(@config[:source_address].to_s,@config[:source_port])
@@udp_socket = socket if @config[:persistent_udp]
end
- ans = ""
+ ans = nil
response = ""
@config[:nameservers].each do |ns|
- @config[:udp_timeout].timeout do
- socket.send(packet_data,0,ns.to_s,@config[:port])
- ans = socket.recvfrom(@config[:packet_size])
- @logger.info "Received #{ans[0].size} bytes from #{ans[1][2]+":"+ans[1][1].to_s}"
+ begin
+ @config[:udp_timeout].timeout do
+ socket.send(packet_data,0,ns.to_s,@config[:port])
+ ans = socket.recvfrom(@config[:packet_size])
+ end
+ if ans
+ @logger.info "Received #{ans[0].size} bytes from #{ans[1][2]+":"+ans[1][1].to_s}"
+ break
+ end
+ rescue TimeoutError
+ @logger.warn "Nameserver #{ns} not responding within UDP timeout, trying next one"
+ next
end
- response = Net::DNS::Packet.parse(ans[0],ans[1])
end
- return response
+ return Net::DNS::Packet.parse(ans[0],ans[1])
end
def valid?(name)
if name =~ /[^-\w\.]/
raise ResolverArgumentError, "Invalid domain name #{name}"
@@ -1168,11 +1118,11 @@
end # module Net
class ResolverArgumentError < ArgumentError # :nodoc:
end
-class Hash # :nodoc:
+module ExtendHash # :nodoc:
# Returns an hash with all the
# keys turned into downcase
#
# hsh = {"Test" => 1, "FooBar" => 2}
# hsh.key_downcase!
@@ -1184,9 +1134,14 @@
hsh[key.downcase] = val
end
self.replace(hsh)
end
end
+
+class Hash # :nodoc:
+ include ExtendHash
+end
+