lib/net/dns/resolver.rb in net-dns2-0.8.2 vs lib/net/dns/resolver.rb in net-dns2-0.8.3

- old
+ new

@@ -343,42 +343,12 @@ # #=> ["192.168.0.1","192.168.0.2","192.168.0.3"] # # The default is 127.0.0.1 (localhost) # def nameservers=(arg) - case arg - when String - begin - @config[:nameservers] = [IPAddr.new(arg)] - @logger.info "Nameservers list changed to value #{@config[:nameservers].inspect}" - rescue ArgumentError # arg is in the name form, not IP - nameservers_from_name(arg) - end - when IPAddr - @config[:nameservers] = [arg] - @logger.info "Nameservers list changed to value #{@config[:nameservers].inspect}" - when Array - @config[:nameservers] = [] - arg.each do |x| - @config[:nameservers] << case x - when String - begin - IPAddr.new(x) - rescue ArgumentError - nameservers_from_name(arg) - return - end - when IPAddr - x - else - raise ArgumentError, "Wrong argument format" - end - end - @logger.info "Nameservers list changed to value #{@config[:nameservers].inspect}" - else - raise ArgumentError, "Wrong argument format, neither String, Array nor IPAddr" - end + @config[:nameservers] = convert_nameservers_arg_to_ips(arg) + @logger.info "Nameservers list changed to value #{@config[:nameservers].inspect}" end alias_method("nameserver=","nameservers=") # Return a string with the default domain. def domain @@ -393,10 +363,19 @@ # Return the defined size of the packet. def packet_size @config[:packet_size] end + def packet_size=(arg) + if arg.respond_to? :to_i + @config[:packet_size] = arg.to_i + @logger.info "Packet size changed to value #{@config[:packet_size].inspect}" + else + @logger.error "Packet size not set, #{arg.class} does not respond to to_i" + end + end + # Get the port number to which the resolver sends queries. # # puts "Sending queries to port #{res.port}" # def port @@ -506,12 +485,16 @@ case e.errno when 98 # Port already in use! @logger.warn "Port already in use" retry when 99 # Address is not valid: raw socket - @raw = true - @logger.warn "Using raw sockets" + if Process.uid == 0 + @raw = true + @logger.warn "Using raw sockets" + else + raise RuntimeError, "Raw sockets requested but not running as root." + end else raise SystemCallError, e end else a.close @@ -1078,11 +1061,11 @@ # Parses a configuration file specified as the argument. def parse_config_file if self.class.platform_windows? require 'win32/resolv' arr = Win32::Resolv.get_resolv_info - self.domain = arr[0].to_s + self.domain = arr[0].first.to_s self.nameservers = arr[1] else nameservers = [] IO.foreach(@config[:config_file]) do |line| line.gsub!(/\s*[;#].*/,"") @@ -1121,37 +1104,50 @@ end end end end + def convert_nameservers_arg_to_ips(arg) + if arg.kind_of? IPAddr + [arg] + elsif arg.respond_to? :map + arg.map{|x| convert_nameservers_arg_to_ips(x) }.flatten + elsif arg.respond_to? :to_a + arg.to_a.map{|x| convert_nameservers_arg_to_ips(x) }.flatten + elsif arg.respond_to? :to_s + begin + [IPAddr.new(arg.to_s)] + rescue ArgumentError # arg is in the name form, not IP + nameservers_from_name(arg) + end + else + raise ArgumentError, "Wrong nameservers argument format, cannot convert to array of IPAddrs" + end + end + def nameservers_from_name(arg) arr = [] arg.split(" ").each do |name| Resolver.new.search(name).each_address do |ip| arr << ip end end - @config[:nameservers] << arr + arr end def make_query_packet(string, type, cls) - case string - when IPAddr - name = string.reverse + begin + name = IPAddr.new(string.chomp(".")).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.chomp(".")).reverse - type = Net::DNS::PTR - rescue ArgumentError - name = string if valid? string - end - else + rescue ArgumentError name = string if valid? string end + if name.nil? + raise ArgumentError, "Bad query string" + end + # Create the packet packet = Net::DNS::Packet.new(name, type, cls) if packet.query? packet.header.recursive = @config[:recursive] ? 1 : 0 @@ -1206,16 +1202,19 @@ next ensure socket.close end end + ans end 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]) + if @config[:nameservers].any? { |ns| ns.ipv6? } + socket6 = UDPSocket.new(Socket::AF_INET6) + socket6.bind(@config[:source_address_inet6].to_s,@config[:source_port]) + end ans = nil response = "" @config[:nameservers].each do |ns| begin