lib/net/dns/resolver.rb in net-dns-0.6.0 vs lib/net/dns/resolver.rb in net-dns-0.6.1

- old
+ new

@@ -841,10 +841,12 @@ # Returns a Net::DNS::Packet object. If you need to examine the response packet # whether it contains any answers or not, use the send() method instead. # def search(name,type=Net::DNS::A,cls=Net::DNS::IN) + return query(name,type,cls) if name.class == IPAddr + # If the name contains at least one dot then try it as is first. if name.include? "." @logger.debug "Search(#{name},#{Net::DNS::RR::Types.new(type)},#{Net::DNS::RR::Classes.new(cls)})" ans = query(name,type,cls) return ans if ans.header.anCount > 0 @@ -889,11 +891,13 @@ # Returns a Net::DNS::Packet object. If you need to examine the response # packet whether it contains any answers or not, use the Resolver#send # method instead. # def query(name,type=Net::DNS::A,cls=Net::DNS::IN) - + + return send(name,type,cls) if name.class == IPAddr + # If the name doesn't contain any dots then append the default domain. if name !~ /\./ and name !~ /:/ and @config[:defnames] name += "." + @config[:domain] end @@ -910,42 +914,42 @@ # or a name string plus optional type and class, which if # omitted default to +A+ and +IN+. # # Returns a Net::DNS::Packet object. # - # # Sending a +Packet+ object - # send_packet = Net::DNS::Packet.new("host.example.com",Net::DNS::NS,Net::DNS::HS) + # # Executes the query with a +Packet+ object + # send_packet = Net::DNS::Packet.new("host.example.com", Net::DNS::NS, Net::DNS::HS) # packet = res.send(send_packet) # - # # Performing a query + # # Executes the query with a host, type and cls # packet = res.send("host.example.com") - # packet = res.send("host.example.com",Net::DNS::NS) - # packet = res.send("host.example.com",Net::DNS::NS,Net::DNS::HS) + # packet = res.send("host.example.com", Net::DNS::NS) + # packet = res.send("host.example.com", Net::DNS::NS, Net::DNS::HS) # # If the name is an IP address (Ipv4 or IPv6), in the form of a string # or a IPAddr object, then an appropriate PTR query will be performed: # # ip = IPAddr.new("172.16.100.2") # packet = res.send(ip) - # packet = res.send("192.168.10.254") + # + # packet = res.send("172.16.100.2") # # Use +packet.header.ancount+ or +packet.answer+ to find out if there # were any records in the answer section. # - def send(argument,type=Net::DNS::A,cls=Net::DNS::IN) + def send(argument, type = Net::DNS::A, cls = Net::DNS::IN) if @config[:nameservers].size == 0 raise ResolverError, "No nameservers specified!" end method = :send_udp - - if argument.kind_of? Net::DNS::Packet - packet = argument + packet = if argument.kind_of? Net::DNS::Packet + argument else - packet = make_query_packet(argument,type,cls) + make_query_packet(argument, type, cls) end - + # Store packet_data for performance improvements, # so methods don't keep on calling Packet#data packet_data = packet.data packet_size = packet_data.size @@ -1098,41 +1102,40 @@ end end @config[:nameservers] << arr end - def make_query_packet(string,type,cls) + def make_query_packet(string, type, cls) case string when IPAddr name = string.reverse type = Net::DNS::PTR @logger.warn "PTR query required for address #{string}, changing type to PTR" when /\d/ # Contains a number, try to see if it's an IP or IPv6 address begin - name = IPAddr.new(string).reverse - type = Net::DNS::PTR - rescue ArgumentError + name = IPAddr.new(string.chomp(".")).reverse + type = Net::DNS::PTR + rescue ::ArgumentError name = string if valid? string end else name = string if valid? string end # Create the packet - packet = Net::DNS::Packet.new(name,type,cls) + packet = Net::DNS::Packet.new(name, type, cls) if packet.query? packet.header.recursive = @config[:recursive] ? 1 : 0 end # DNSSEC and TSIG stuff to be inserted here packet - end - def send_tcp(packet,packet_data) + def send_tcp(packet, packet_data) ans = nil length = [packet_data.size].pack("n") @config[:nameservers].each do |ns| @@ -1176,11 +1179,11 @@ socket.close end end end - def send_udp(packet,packet_data) + def send_udp(packet, packet_data) socket = UDPSocket.new socket.bind(@config[:source_address].to_s,@config[:source_port]) ans = nil response = "" @@ -1191,11 +1194,11 @@ socket.send(packet_data,0,ns.to_s,@config[:port]) ans = socket.recvfrom(@config[:packet_size]) end break if ans rescue TimeoutError - @logger.warn "Nameserver #{ns} not responding within UDP timeout, trying next one" + @logger.warn "Nameserver #{ns} not responding within UDP timeout, trying next one" next end end ans end @@ -1205,23 +1208,23 @@ raise ArgumentError, "Invalid domain name #{name}" else true end end - - + + class << self - + # Returns true if running on a Windows platform. # # Note. This method doesn't rely on the RUBY_PLATFORM constant # because the comparison will fail when running on JRuby. # On JRuby RUBY_PLATFORM == 'java'. def platform_windows? !!(Config::CONFIG["host_os"] =~ /msdos|mswin|djgpp|mingw/i) end - + end - + end end -end \ No newline at end of file +end