lib/net/dns/resolver.rb in net-dns-0.9.0 vs lib/net/dns/resolver.rb in net-dns-0.20.0
- old
+ new
@@ -14,11 +14,11 @@
# An optional block can be passed yielding the Net::DNS::Packet object.
#
# Resolver("www.google.com") { |packet| puts packet.size + " bytes" }
# # => 484 bytes
#
-def Resolver(name, type = Net::DNS::A, cls = Net::DNS::IN, &block)
+def Resolver(name, type = Net::DNS::A, cls = Net::DNS::IN)
resolver = Net::DNS::Resolver.start(name, type, cls)
if block_given?
yield resolver
else
resolver
@@ -229,13 +229,13 @@
#
# Part of the above documentation is taken from the one in the
# Net::DNS::Resolver Perl module.
#
def initialize(config = {})
- raise ArgumentError, "Expected `config' to be a Hash" unless config.is_a?(Hash)
+ config.is_a?(Hash) or
+ raise(ArgumentError, "Expected `config' to be a Hash")
- # config.downcase_keys!
@config = Defaults.merge config
@raw = false
# New logger facility
@logger = Logger.new(@config[:log_file])
@@ -400,16 +400,15 @@
# res.port = 10053
#
# The default is port 53.
#
def port=(num)
- if (0..65_535).cover? num
- @config[:port] = num
- @logger.info "Port number changed to #{num}"
- else
- raise ArgumentError, "Wrong port number #{num}"
- end
+ (0..65_535).cover?(num) or
+ raise(ArgumentError, "Wrong port number #{num}")
+
+ @config[:port] = num
+ @logger.info "Port number changed to #{num}"
end
# Get the value of the source port number.
#
# puts "Sending queries using port #{res.source_port}"
@@ -429,19 +428,16 @@
#
# The default is 0, which means that the port will be chosen by the
# underlaying layers.
#
def source_port=(num)
- unless root?
- raise ResolverPermissionError, "Are you root?"
- end
+ root? or
+ raise(ResolverPermissionError, "Are you root?")
+ (0..65_535).cover?(num) or
+ raise(ArgumentError, "Wrong port number #{num}")
- if (0..65_535).cover?(num)
- @config[:source_port] = num
- else
- raise ArgumentError, "Wrong port number #{num}"
- end
+ @config[:source_port] = num
end
alias srcport= source_port=
# Get the local address from which the resolver sends queries
#
@@ -480,17 +476,16 @@
# The class will then generate an exception if you're not root.
#
# The default is 0.0.0.0, meaning any local address (chosen on routing needs).
#
def source_address=(addr)
- unless addr.respond_to? :to_s
- raise ArgumentError, "Wrong address argument #{addr}"
- end
+ addr.respond_to?(:to_s) or
+ raise(ArgumentError, "Wrong address argument #{addr}")
begin
port = rand(1024..65_023)
- @logger.warn "Try to determine state of source address #{addr} with port #{port}"
+ @logger.info "Try to determine state of source address #{addr} with port #{port}"
a = TCPServer.new(addr.to_s, port)
rescue SystemCallError => e
case e.errno
when 98 # Port already in use!
@logger.warn "Port already in use"
@@ -525,16 +520,15 @@
end
alias retrans retry_interval
# Set the retrasmission interval in seconds. Default 5 seconds.
def retry_interval=(num)
- if num > 0
- @config[:retry_interval] = num
- @logger.info "Retransmission interval changed to #{num} seconds"
- else
- raise ArgumentError, "Interval must be positive"
- end
+ num.positive? or
+ raise(ArgumentError, "Interval must be positive")
+
+ @config[:retry_interval] = num
+ @logger.info "Retransmission interval changed to #{num} seconds"
end
alias retrans= retry_interval=
# The number of times the resolver will try a query.
#
@@ -545,16 +539,15 @@
end
# Set the number of times the resolver will try a query.
# Default 4 times.
def retry_number=(num)
- if num.is_a?(Integer) && (num > 0)
- @config[:retry_number] = num
- @logger.info "Retrasmissions number changed to #{num}"
- else
- raise ArgumentError, "Retry value must be a positive integer"
- end
+ (num.is_a?(Integer) && (num > 0)) or
+ raise(ArgumentError, "Retry value must be a positive integer")
+
+ @config[:retry_number] = num
+ @logger.info "Retrasmissions number changed to #{num}"
end
alias_method('retry=', 'retry_number=')
# This method will return true if the resolver is configured to
# perform recursive queries.
@@ -776,11 +769,10 @@
#
# Note that a new logging facility will be create, destroing
# the old one, which will then be impossibile to recover.
#
def log_file=(log)
- @logger.close
@config[:log_file] = log
@logger = Logger.new(@config[:log_file])
@logger.level = $DEBUG ? Logger::DEBUG : Logger::WARN
end
@@ -800,16 +792,14 @@
# res.logger = log
#
# Note that this will destroy the precedent logger.
#
def logger=(logger)
- if logger.is_a? Logger
- @logger.close
- @logger = logger
- else
- raise ArgumentError, "Argument must be an instance of Logger class"
- end
+ logger.is_a?(Logger) or
+ raise(ArgumentError, "Argument must be an instance of Logger class")
+
+ @logger = logger
end
# Set the log level for the built-in logging facility.
#
# The log level can be one of the following:
@@ -946,13 +936,12 @@
#
# Use +packet.header.ancount+ or +packet.answer+ to find out if there
# were any records in the answer section.
#
def query(argument, type = Net::DNS::A, cls = Net::DNS::IN)
- if @config[:nameservers].empty?
- raise Resolver::Error, "No nameservers specified!"
- end
+ !@config[:nameservers].empty? or
+ raise(Resolver::Error, "No nameservers specified!")
method = :query_udp
packet = if argument.is_a? Net::DNS::Packet
argument
else
@@ -985,14 +974,14 @@
end
end
if type == Net::DNS::AXFR
if @raw
- @logger.warn "AXFR query, switching to TCP over RAW socket"
+ @logger.info "AXFR query, switching to TCP over RAW socket"
method = :send_raw_tcp
else
- @logger.warn "AXFR query, switching to TCP"
+ @logger.info "AXFR query, switching to TCP"
method = :query_tcp
end
end
ans = send(method, packet, packet_data)
@@ -1083,18 +1072,18 @@
self.searchlist = ENV['RES_SEARCHLIST'].split(" ")
end
if ENV['LOCALDOMAIN']
self.domain = ENV['LOCALDOMAIN']
end
- if ENV['RES_OPTIONS']
- ENV['RES_OPTIONS'].split(" ").each do |opt|
- name, val = opt.split(":")
- begin
- eval("self.#{name} = #{val}")
- rescue NoMethodError
- raise ArgumentError, "Invalid ENV option #{name}"
- end
+ return unless ENV['RES_OPTIONS']
+
+ ENV['RES_OPTIONS'].split(" ").each do |opt|
+ name, val = opt.split(":")
+ begin
+ eval("self.#{name} = #{val}")
+ rescue NoMethodError
+ raise ArgumentError, "Invalid ENV option #{name}"
end
end
end
def nameservers_from_name(arg)
@@ -1134,16 +1123,15 @@
# DNSSEC and TSIG stuff to be inserted here
packet
end
- def query_tcp(packet, packet_data)
+ def query_tcp(_packet, packet_data)
ans = nil
length = [packet_data.size].pack("n")
@config[:nameservers].each do |ns|
- begin
buffer = ""
socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
socket.bind(Socket.pack_sockaddr_in(@config[:source_port], @config[:source_address].to_s))
sockaddr = Socket.pack_sockaddr_in(@config[:port], ns.to_s)
@@ -1151,16 +1139,16 @@
@config[:tcp_timeout].timeout do
socket.connect(sockaddr)
@logger.info "Contacting nameserver #{ns} port #{@config[:port]}"
socket.write(length + packet_data)
ans = socket.recv(Net::DNS::INT16SZ)
- len = ans.unpack("n")[0]
+ len = ans.unpack1("n")
@logger.info "Receiving #{len} bytes..."
if len == 0
- @logger.warn "Receiving 0 lenght packet from nameserver #{ns}, trying next."
+ @logger.warn "Receiving 0 length packet from nameserver #{ns}, trying next."
next
end
while buffer.size < len
left = len - buffer.size
@@ -1172,29 +1160,28 @@
@logger.warn "Malformed packet from nameserver #{ns}, trying next."
next
end
end
return [buffer, ["", @config[:port], ns.to_s, ns.to_s]]
- rescue TimeoutError
+ rescue Timeout::Error
@logger.warn "Nameserver #{ns} not responding within TCP timeout, trying next one"
next
- ensure
+ ensure
socket.close
- end
end
+ ans
end
- def query_udp(packet, packet_data)
+ def query_udp(_packet, packet_data)
socket4 = UDPSocket.new
socket4.bind(@config[:source_address].to_s, @config[:source_port])
socket6 = UDPSocket.new(Socket::AF_INET6)
socket6.bind(@config[:source_address_inet6].to_s, @config[:source_port])
ans = nil
response = ""
@config[:nameservers].each do |ns|
- begin
@config[:udp_timeout].timeout do
@logger.info "Contacting nameserver #{ns} port #{@config[:port]}"
ans = if ns.ipv6?
socket6.send(packet_data, 0, ns.to_s, @config[:port])
socket6.recvfrom(@config[:packet_size])
@@ -1202,24 +1189,22 @@
socket4.send(packet_data, 0, ns.to_s, @config[:port])
socket4.recvfrom(@config[:packet_size])
end
end
break if ans
- rescue TimeoutError
+ rescue Timeout::Error
@logger.warn "Nameserver #{ns} not responding within UDP timeout, trying next one"
next
- end
end
ans
end
# FIXME: a ? method should never raise.
def valid?(name)
- if name =~ /[^-\w\.]/
- raise ArgumentError, "Invalid domain name #{name}"
- else
- true
- end
+ name !~ /[^-\w\.\*]/ or
+ raise(ArgumentError, "Invalid domain name #{name}")
+
+ true
end
end
end
end