/lib/resolv.rb
Ruby | 2910 lines | 1948 code | 464 blank | 498 comment | 137 complexity | ed2b47764cec1f71db28cb80823bc778 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, 0BSD, Unlicense, GPL-2.0, BSD-3-Clause
Large files files are truncated, but you can click here to view the full file
- # frozen_string_literal: true
- require 'socket'
- require 'timeout'
- require 'io/wait'
- begin
- require 'securerandom'
- rescue LoadError
- end
- # Resolv is a thread-aware DNS resolver library written in Ruby. Resolv can
- # handle multiple DNS requests concurrently without blocking the entire Ruby
- # interpreter.
- #
- # See also resolv-replace.rb to replace the libc resolver with Resolv.
- #
- # Resolv can look up various DNS resources using the DNS module directly.
- #
- # Examples:
- #
- # p Resolv.getaddress "www.ruby-lang.org"
- # p Resolv.getname "210.251.121.214"
- #
- # Resolv::DNS.open do |dns|
- # ress = dns.getresources "www.ruby-lang.org", Resolv::DNS::Resource::IN::A
- # p ress.map(&:address)
- # ress = dns.getresources "ruby-lang.org", Resolv::DNS::Resource::IN::MX
- # p ress.map { |r| [r.exchange.to_s, r.preference] }
- # end
- #
- #
- # == Bugs
- #
- # * NIS is not supported.
- # * /etc/nsswitch.conf is not supported.
- class Resolv
- ##
- # Looks up the first IP address for +name+.
- def self.getaddress(name)
- DefaultResolver.getaddress(name)
- end
- ##
- # Looks up all IP address for +name+.
- def self.getaddresses(name)
- DefaultResolver.getaddresses(name)
- end
- ##
- # Iterates over all IP addresses for +name+.
- def self.each_address(name, &block)
- DefaultResolver.each_address(name, &block)
- end
- ##
- # Looks up the hostname of +address+.
- def self.getname(address)
- DefaultResolver.getname(address)
- end
- ##
- # Looks up all hostnames for +address+.
- def self.getnames(address)
- DefaultResolver.getnames(address)
- end
- ##
- # Iterates over all hostnames for +address+.
- def self.each_name(address, &proc)
- DefaultResolver.each_name(address, &proc)
- end
- ##
- # Creates a new Resolv using +resolvers+.
- def initialize(resolvers=[Hosts.new, DNS.new])
- @resolvers = resolvers
- end
- ##
- # Looks up the first IP address for +name+.
- def getaddress(name)
- each_address(name) {|address| return address}
- raise ResolvError.new("no address for #{name}")
- end
- ##
- # Looks up all IP address for +name+.
- def getaddresses(name)
- ret = []
- each_address(name) {|address| ret << address}
- return ret
- end
- ##
- # Iterates over all IP addresses for +name+.
- def each_address(name)
- if AddressRegex =~ name
- yield name
- return
- end
- yielded = false
- @resolvers.each {|r|
- r.each_address(name) {|address|
- yield address.to_s
- yielded = true
- }
- return if yielded
- }
- end
- ##
- # Looks up the hostname of +address+.
- def getname(address)
- each_name(address) {|name| return name}
- raise ResolvError.new("no name for #{address}")
- end
- ##
- # Looks up all hostnames for +address+.
- def getnames(address)
- ret = []
- each_name(address) {|name| ret << name}
- return ret
- end
- ##
- # Iterates over all hostnames for +address+.
- def each_name(address)
- yielded = false
- @resolvers.each {|r|
- r.each_name(address) {|name|
- yield name.to_s
- yielded = true
- }
- return if yielded
- }
- end
- ##
- # Indicates a failure to resolve a name or address.
- class ResolvError < StandardError; end
- ##
- # Indicates a timeout resolving a name or address.
- class ResolvTimeout < Timeout::Error; end
- ##
- # Resolv::Hosts is a hostname resolver that uses the system hosts file.
- class Hosts
- if /mswin|mingw|cygwin/ =~ RUBY_PLATFORM and
- begin
- require 'win32/resolv'
- DefaultFileName = Win32::Resolv.get_hosts_path || IO::NULL
- rescue LoadError
- end
- end
- DefaultFileName ||= '/etc/hosts'
- ##
- # Creates a new Resolv::Hosts, using +filename+ for its data source.
- def initialize(filename = DefaultFileName)
- @filename = filename
- @mutex = Thread::Mutex.new
- @initialized = nil
- end
- def lazy_initialize # :nodoc:
- @mutex.synchronize {
- unless @initialized
- @name2addr = {}
- @addr2name = {}
- File.open(@filename, 'rb') {|f|
- f.each {|line|
- line.sub!(/#.*/, '')
- addr, hostname, *aliases = line.split(/\s+/)
- next unless addr
- @addr2name[addr] = [] unless @addr2name.include? addr
- @addr2name[addr] << hostname
- @addr2name[addr] += aliases
- @name2addr[hostname] = [] unless @name2addr.include? hostname
- @name2addr[hostname] << addr
- aliases.each {|n|
- @name2addr[n] = [] unless @name2addr.include? n
- @name2addr[n] << addr
- }
- }
- }
- @name2addr.each {|name, arr| arr.reverse!}
- @initialized = true
- end
- }
- self
- end
- ##
- # Gets the IP address of +name+ from the hosts file.
- def getaddress(name)
- each_address(name) {|address| return address}
- raise ResolvError.new("#{@filename} has no name: #{name}")
- end
- ##
- # Gets all IP addresses for +name+ from the hosts file.
- def getaddresses(name)
- ret = []
- each_address(name) {|address| ret << address}
- return ret
- end
- ##
- # Iterates over all IP addresses for +name+ retrieved from the hosts file.
- def each_address(name, &proc)
- lazy_initialize
- @name2addr[name]&.each(&proc)
- end
- ##
- # Gets the hostname of +address+ from the hosts file.
- def getname(address)
- each_name(address) {|name| return name}
- raise ResolvError.new("#{@filename} has no address: #{address}")
- end
- ##
- # Gets all hostnames for +address+ from the hosts file.
- def getnames(address)
- ret = []
- each_name(address) {|name| ret << name}
- return ret
- end
- ##
- # Iterates over all hostnames for +address+ retrieved from the hosts file.
- def each_name(address, &proc)
- lazy_initialize
- @addr2name[address]&.each(&proc)
- end
- end
- ##
- # Resolv::DNS is a DNS stub resolver.
- #
- # Information taken from the following places:
- #
- # * STD0013
- # * RFC 1035
- # * ftp://ftp.isi.edu/in-notes/iana/assignments/dns-parameters
- # * etc.
- class DNS
- ##
- # Default DNS Port
- Port = 53
- ##
- # Default DNS UDP packet size
- UDPSize = 512
- ##
- # Creates a new DNS resolver. See Resolv::DNS.new for argument details.
- #
- # Yields the created DNS resolver to the block, if given, otherwise
- # returns it.
- def self.open(*args)
- dns = new(*args)
- return dns unless block_given?
- begin
- yield dns
- ensure
- dns.close
- end
- end
- ##
- # Creates a new DNS resolver.
- #
- # +config_info+ can be:
- #
- # nil:: Uses /etc/resolv.conf.
- # String:: Path to a file using /etc/resolv.conf's format.
- # Hash:: Must contain :nameserver, :search and :ndots keys.
- # :nameserver_port can be used to specify port number of nameserver address.
- #
- # The value of :nameserver should be an address string or
- # an array of address strings.
- # - :nameserver => '8.8.8.8'
- # - :nameserver => ['8.8.8.8', '8.8.4.4']
- #
- # The value of :nameserver_port should be an array of
- # pair of nameserver address and port number.
- # - :nameserver_port => [['8.8.8.8', 53], ['8.8.4.4', 53]]
- #
- # Example:
- #
- # Resolv::DNS.new(:nameserver => ['210.251.121.21'],
- # :search => ['ruby-lang.org'],
- # :ndots => 1)
- def initialize(config_info=nil)
- @mutex = Thread::Mutex.new
- @config = Config.new(config_info)
- @initialized = nil
- end
- # Sets the resolver timeouts. This may be a single positive number
- # or an array of positive numbers representing timeouts in seconds.
- # If an array is specified, a DNS request will retry and wait for
- # each successive interval in the array until a successful response
- # is received. Specifying +nil+ reverts to the default timeouts:
- # [ 5, second = 5 * 2 / nameserver_count, 2 * second, 4 * second ]
- #
- # Example:
- #
- # dns.timeouts = 3
- #
- def timeouts=(values)
- @config.timeouts = values
- end
- def lazy_initialize # :nodoc:
- @mutex.synchronize {
- unless @initialized
- @config.lazy_initialize
- @initialized = true
- end
- }
- self
- end
- ##
- # Closes the DNS resolver.
- def close
- @mutex.synchronize {
- if @initialized
- @initialized = false
- end
- }
- end
- ##
- # Gets the IP address of +name+ from the DNS resolver.
- #
- # +name+ can be a Resolv::DNS::Name or a String. Retrieved address will
- # be a Resolv::IPv4 or Resolv::IPv6
- def getaddress(name)
- each_address(name) {|address| return address}
- raise ResolvError.new("DNS result has no information for #{name}")
- end
- ##
- # Gets all IP addresses for +name+ from the DNS resolver.
- #
- # +name+ can be a Resolv::DNS::Name or a String. Retrieved addresses will
- # be a Resolv::IPv4 or Resolv::IPv6
- def getaddresses(name)
- ret = []
- each_address(name) {|address| ret << address}
- return ret
- end
- ##
- # Iterates over all IP addresses for +name+ retrieved from the DNS
- # resolver.
- #
- # +name+ can be a Resolv::DNS::Name or a String. Retrieved addresses will
- # be a Resolv::IPv4 or Resolv::IPv6
- def each_address(name)
- each_resource(name, Resource::IN::A) {|resource| yield resource.address}
- if use_ipv6?
- each_resource(name, Resource::IN::AAAA) {|resource| yield resource.address}
- end
- end
- def use_ipv6? # :nodoc:
- begin
- list = Socket.ip_address_list
- rescue NotImplementedError
- return true
- end
- list.any? {|a| a.ipv6? && !a.ipv6_loopback? && !a.ipv6_linklocal? }
- end
- private :use_ipv6?
- ##
- # Gets the hostname for +address+ from the DNS resolver.
- #
- # +address+ must be a Resolv::IPv4, Resolv::IPv6 or a String. Retrieved
- # name will be a Resolv::DNS::Name.
- def getname(address)
- each_name(address) {|name| return name}
- raise ResolvError.new("DNS result has no information for #{address}")
- end
- ##
- # Gets all hostnames for +address+ from the DNS resolver.
- #
- # +address+ must be a Resolv::IPv4, Resolv::IPv6 or a String. Retrieved
- # names will be Resolv::DNS::Name instances.
- def getnames(address)
- ret = []
- each_name(address) {|name| ret << name}
- return ret
- end
- ##
- # Iterates over all hostnames for +address+ retrieved from the DNS
- # resolver.
- #
- # +address+ must be a Resolv::IPv4, Resolv::IPv6 or a String. Retrieved
- # names will be Resolv::DNS::Name instances.
- def each_name(address)
- case address
- when Name
- ptr = address
- when IPv4, IPv6
- ptr = address.to_name
- when IPv4::Regex
- ptr = IPv4.create(address).to_name
- when IPv6::Regex
- ptr = IPv6.create(address).to_name
- else
- raise ResolvError.new("cannot interpret as address: #{address}")
- end
- each_resource(ptr, Resource::IN::PTR) {|resource| yield resource.name}
- end
- ##
- # Look up the +typeclass+ DNS resource of +name+.
- #
- # +name+ must be a Resolv::DNS::Name or a String.
- #
- # +typeclass+ should be one of the following:
- #
- # * Resolv::DNS::Resource::IN::A
- # * Resolv::DNS::Resource::IN::AAAA
- # * Resolv::DNS::Resource::IN::ANY
- # * Resolv::DNS::Resource::IN::CNAME
- # * Resolv::DNS::Resource::IN::HINFO
- # * Resolv::DNS::Resource::IN::MINFO
- # * Resolv::DNS::Resource::IN::MX
- # * Resolv::DNS::Resource::IN::NS
- # * Resolv::DNS::Resource::IN::PTR
- # * Resolv::DNS::Resource::IN::SOA
- # * Resolv::DNS::Resource::IN::TXT
- # * Resolv::DNS::Resource::IN::WKS
- #
- # Returned resource is represented as a Resolv::DNS::Resource instance,
- # i.e. Resolv::DNS::Resource::IN::A.
- def getresource(name, typeclass)
- each_resource(name, typeclass) {|resource| return resource}
- raise ResolvError.new("DNS result has no information for #{name}")
- end
- ##
- # Looks up all +typeclass+ DNS resources for +name+. See #getresource for
- # argument details.
- def getresources(name, typeclass)
- ret = []
- each_resource(name, typeclass) {|resource| ret << resource}
- return ret
- end
- ##
- # Iterates over all +typeclass+ DNS resources for +name+. See
- # #getresource for argument details.
- def each_resource(name, typeclass, &proc)
- fetch_resource(name, typeclass) {|reply, reply_name|
- extract_resources(reply, reply_name, typeclass, &proc)
- }
- end
- def fetch_resource(name, typeclass)
- lazy_initialize
- begin
- requester = make_udp_requester
- rescue Errno::EACCES
- # fall back to TCP
- end
- senders = {}
- begin
- @config.resolv(name) {|candidate, tout, nameserver, port|
- requester ||= make_tcp_requester(nameserver, port)
- msg = Message.new
- msg.rd = 1
- msg.add_question(candidate, typeclass)
- unless sender = senders[[candidate, nameserver, port]]
- sender = requester.sender(msg, candidate, nameserver, port)
- next if !sender
- senders[[candidate, nameserver, port]] = sender
- end
- reply, reply_name = requester.request(sender, tout)
- case reply.rcode
- when RCode::NoError
- if reply.tc == 1 and not Requester::TCP === requester
- requester.close
- # Retry via TCP:
- requester = make_tcp_requester(nameserver, port)
- senders = {}
- # This will use TCP for all remaining candidates (assuming the
- # current candidate does not already respond successfully via
- # TCP). This makes sense because we already know the full
- # response will not fit in an untruncated UDP packet.
- redo
- else
- yield(reply, reply_name)
- end
- return
- when RCode::NXDomain
- raise Config::NXDomain.new(reply_name.to_s)
- else
- raise Config::OtherResolvError.new(reply_name.to_s)
- end
- }
- ensure
- requester&.close
- end
- end
- def make_udp_requester # :nodoc:
- nameserver_port = @config.nameserver_port
- if nameserver_port.length == 1
- Requester::ConnectedUDP.new(*nameserver_port[0])
- else
- Requester::UnconnectedUDP.new(*nameserver_port)
- end
- end
- def make_tcp_requester(host, port) # :nodoc:
- return Requester::TCP.new(host, port)
- end
- def extract_resources(msg, name, typeclass) # :nodoc:
- if typeclass < Resource::ANY
- n0 = Name.create(name)
- msg.each_resource {|n, ttl, data|
- yield data if n0 == n
- }
- end
- yielded = false
- n0 = Name.create(name)
- msg.each_resource {|n, ttl, data|
- if n0 == n
- case data
- when typeclass
- yield data
- yielded = true
- when Resource::CNAME
- n0 = data.name
- end
- end
- }
- return if yielded
- msg.each_resource {|n, ttl, data|
- if n0 == n
- case data
- when typeclass
- yield data
- end
- end
- }
- end
- if defined? SecureRandom
- def self.random(arg) # :nodoc:
- begin
- SecureRandom.random_number(arg)
- rescue NotImplementedError
- rand(arg)
- end
- end
- else
- def self.random(arg) # :nodoc:
- rand(arg)
- end
- end
- RequestID = {} # :nodoc:
- RequestIDMutex = Thread::Mutex.new # :nodoc:
- def self.allocate_request_id(host, port) # :nodoc:
- id = nil
- RequestIDMutex.synchronize {
- h = (RequestID[[host, port]] ||= {})
- begin
- id = random(0x0000..0xffff)
- end while h[id]
- h[id] = true
- }
- id
- end
- def self.free_request_id(host, port, id) # :nodoc:
- RequestIDMutex.synchronize {
- key = [host, port]
- if h = RequestID[key]
- h.delete id
- if h.empty?
- RequestID.delete key
- end
- end
- }
- end
- def self.bind_random_port(udpsock, bind_host="0.0.0.0") # :nodoc:
- begin
- port = random(1024..65535)
- udpsock.bind(bind_host, port)
- rescue Errno::EADDRINUSE, # POSIX
- Errno::EACCES, # SunOS: See PRIV_SYS_NFS in privileges(5)
- Errno::EPERM # FreeBSD: security.mac.portacl.port_high is configurable. See mac_portacl(4).
- retry
- end
- end
- class Requester # :nodoc:
- def initialize
- @senders = {}
- @socks = nil
- end
- def request(sender, tout)
- start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
- timelimit = start + tout
- begin
- sender.send
- rescue Errno::EHOSTUNREACH, # multi-homed IPv6 may generate this
- Errno::ENETUNREACH
- raise ResolvTimeout
- end
- while true
- before_select = Process.clock_gettime(Process::CLOCK_MONOTONIC)
- timeout = timelimit - before_select
- if timeout <= 0
- raise ResolvTimeout
- end
- if @socks.size == 1
- select_result = @socks[0].wait_readable(timeout) ? [ @socks ] : nil
- else
- select_result = IO.select(@socks, nil, nil, timeout)
- end
- if !select_result
- after_select = Process.clock_gettime(Process::CLOCK_MONOTONIC)
- next if after_select < timelimit
- raise ResolvTimeout
- end
- begin
- reply, from = recv_reply(select_result[0])
- rescue Errno::ECONNREFUSED, # GNU/Linux, FreeBSD
- Errno::ECONNRESET # Windows
- # No name server running on the server?
- # Don't wait anymore.
- raise ResolvTimeout
- end
- begin
- msg = Message.decode(reply)
- rescue DecodeError
- next # broken DNS message ignored
- end
- if sender == sender_for(from, msg)
- break
- else
- # unexpected DNS message ignored
- end
- end
- return msg, sender.data
- end
- def sender_for(addr, msg)
- @senders[[addr,msg.id]]
- end
- def close
- socks = @socks
- @socks = nil
- socks&.each(&:close)
- end
- class Sender # :nodoc:
- def initialize(msg, data, sock)
- @msg = msg
- @data = data
- @sock = sock
- end
- end
- class UnconnectedUDP < Requester # :nodoc:
- def initialize(*nameserver_port)
- super()
- @nameserver_port = nameserver_port
- @initialized = false
- @mutex = Thread::Mutex.new
- end
- def lazy_initialize
- @mutex.synchronize {
- next if @initialized
- @initialized = true
- @socks_hash = {}
- @socks = []
- @nameserver_port.each {|host, port|
- if host.index(':')
- bind_host = "::"
- af = Socket::AF_INET6
- else
- bind_host = "0.0.0.0"
- af = Socket::AF_INET
- end
- next if @socks_hash[bind_host]
- begin
- sock = UDPSocket.new(af)
- rescue Errno::EAFNOSUPPORT
- next # The kernel doesn't support the address family.
- end
- @socks << sock
- @socks_hash[bind_host] = sock
- sock.do_not_reverse_lookup = true
- DNS.bind_random_port(sock, bind_host)
- }
- }
- self
- end
- def recv_reply(readable_socks)
- lazy_initialize
- reply, from = readable_socks[0].recvfrom(UDPSize)
- return reply, [from[3],from[1]]
- end
- def sender(msg, data, host, port=Port)
- host = Addrinfo.ip(host).ip_address
- lazy_initialize
- sock = @socks_hash[host.index(':') ? "::" : "0.0.0.0"]
- return nil if !sock
- service = [host, port]
- id = DNS.allocate_request_id(host, port)
- request = msg.encode
- request[0,2] = [id].pack('n')
- return @senders[[service, id]] =
- Sender.new(request, data, sock, host, port)
- end
- def close
- @mutex.synchronize {
- if @initialized
- super
- @senders.each_key {|service, id|
- DNS.free_request_id(service[0], service[1], id)
- }
- @initialized = false
- end
- }
- end
- class Sender < Requester::Sender # :nodoc:
- def initialize(msg, data, sock, host, port)
- super(msg, data, sock)
- @host = host
- @port = port
- end
- attr_reader :data
- def send
- raise "@sock is nil." if @sock.nil?
- @sock.send(@msg, 0, @host, @port)
- end
- end
- end
- class ConnectedUDP < Requester # :nodoc:
- def initialize(host, port=Port)
- super()
- @host = host
- @port = port
- @mutex = Thread::Mutex.new
- @initialized = false
- end
- def lazy_initialize
- @mutex.synchronize {
- next if @initialized
- @initialized = true
- is_ipv6 = @host.index(':')
- sock = UDPSocket.new(is_ipv6 ? Socket::AF_INET6 : Socket::AF_INET)
- @socks = [sock]
- sock.do_not_reverse_lookup = true
- DNS.bind_random_port(sock, is_ipv6 ? "::" : "0.0.0.0")
- sock.connect(@host, @port)
- }
- self
- end
- def recv_reply(readable_socks)
- lazy_initialize
- reply = readable_socks[0].recv(UDPSize)
- return reply, nil
- end
- def sender(msg, data, host=@host, port=@port)
- lazy_initialize
- unless host == @host && port == @port
- raise RequestError.new("host/port don't match: #{host}:#{port}")
- end
- id = DNS.allocate_request_id(@host, @port)
- request = msg.encode
- request[0,2] = [id].pack('n')
- return @senders[[nil,id]] = Sender.new(request, data, @socks[0])
- end
- def close
- @mutex.synchronize do
- if @initialized
- super
- @senders.each_key {|from, id|
- DNS.free_request_id(@host, @port, id)
- }
- @initialized = false
- end
- end
- end
- class Sender < Requester::Sender # :nodoc:
- def send
- raise "@sock is nil." if @sock.nil?
- @sock.send(@msg, 0)
- end
- attr_reader :data
- end
- end
- class MDNSOneShot < UnconnectedUDP # :nodoc:
- def sender(msg, data, host, port=Port)
- lazy_initialize
- id = DNS.allocate_request_id(host, port)
- request = msg.encode
- request[0,2] = [id].pack('n')
- sock = @socks_hash[host.index(':') ? "::" : "0.0.0.0"]
- return @senders[id] =
- UnconnectedUDP::Sender.new(request, data, sock, host, port)
- end
- def sender_for(addr, msg)
- lazy_initialize
- @senders[msg.id]
- end
- end
- class TCP < Requester # :nodoc:
- def initialize(host, port=Port)
- super()
- @host = host
- @port = port
- sock = TCPSocket.new(@host, @port)
- @socks = [sock]
- @senders = {}
- end
- def recv_reply(readable_socks)
- len = readable_socks[0].read(2).unpack('n')[0]
- reply = @socks[0].read(len)
- return reply, nil
- end
- def sender(msg, data, host=@host, port=@port)
- unless host == @host && port == @port
- raise RequestError.new("host/port don't match: #{host}:#{port}")
- end
- id = DNS.allocate_request_id(@host, @port)
- request = msg.encode
- request[0,2] = [request.length, id].pack('nn')
- return @senders[[nil,id]] = Sender.new(request, data, @socks[0])
- end
- class Sender < Requester::Sender # :nodoc:
- def send
- @sock.print(@msg)
- @sock.flush
- end
- attr_reader :data
- end
- def close
- super
- @senders.each_key {|from,id|
- DNS.free_request_id(@host, @port, id)
- }
- end
- end
- ##
- # Indicates a problem with the DNS request.
- class RequestError < StandardError
- end
- end
- class Config # :nodoc:
- def initialize(config_info=nil)
- @mutex = Thread::Mutex.new
- @config_info = config_info
- @initialized = nil
- @timeouts = nil
- end
- def timeouts=(values)
- if values
- values = Array(values)
- values.each do |t|
- Numeric === t or raise ArgumentError, "#{t.inspect} is not numeric"
- t > 0.0 or raise ArgumentError, "timeout=#{t} must be positive"
- end
- @timeouts = values
- else
- @timeouts = nil
- end
- end
- def Config.parse_resolv_conf(filename)
- nameserver = []
- search = nil
- ndots = 1
- File.open(filename, 'rb') {|f|
- f.each {|line|
- line.sub!(/[#;].*/, '')
- keyword, *args = line.split(/\s+/)
- next unless keyword
- case keyword
- when 'nameserver'
- nameserver += args
- when 'domain'
- next if args.empty?
- search = [args[0]]
- when 'search'
- next if args.empty?
- search = args
- when 'options'
- args.each {|arg|
- case arg
- when /\Andots:(\d+)\z/
- ndots = $1.to_i
- end
- }
- end
- }
- }
- return { :nameserver => nameserver, :search => search, :ndots => ndots }
- end
- def Config.default_config_hash(filename="/etc/resolv.conf")
- if File.exist? filename
- config_hash = Config.parse_resolv_conf(filename)
- else
- if /mswin|cygwin|mingw|bccwin/ =~ RUBY_PLATFORM
- require 'win32/resolv'
- search, nameserver = Win32::Resolv.get_resolv_info
- config_hash = {}
- config_hash[:nameserver] = nameserver if nameserver
- config_hash[:search] = [search].flatten if search
- end
- end
- config_hash || {}
- end
- def lazy_initialize
- @mutex.synchronize {
- unless @initialized
- @nameserver_port = []
- @search = nil
- @ndots = 1
- case @config_info
- when nil
- config_hash = Config.default_config_hash
- when String
- config_hash = Config.parse_resolv_conf(@config_info)
- when Hash
- config_hash = @config_info.dup
- if String === config_hash[:nameserver]
- config_hash[:nameserver] = [config_hash[:nameserver]]
- end
- if String === config_hash[:search]
- config_hash[:search] = [config_hash[:search]]
- end
- else
- raise ArgumentError.new("invalid resolv configuration: #{@config_info.inspect}")
- end
- if config_hash.include? :nameserver
- @nameserver_port = config_hash[:nameserver].map {|ns| [ns, Port] }
- end
- if config_hash.include? :nameserver_port
- @nameserver_port = config_hash[:nameserver_port].map {|ns, port| [ns, (port || Port)] }
- end
- @search = config_hash[:search] if config_hash.include? :search
- @ndots = config_hash[:ndots] if config_hash.include? :ndots
- if @nameserver_port.empty?
- @nameserver_port << ['0.0.0.0', Port]
- end
- if @search
- @search = @search.map {|arg| Label.split(arg) }
- else
- hostname = Socket.gethostname
- if /\./ =~ hostname
- @search = [Label.split($')]
- else
- @search = [[]]
- end
- end
- if !@nameserver_port.kind_of?(Array) ||
- @nameserver_port.any? {|ns_port|
- !(Array === ns_port) ||
- ns_port.length != 2
- !(String === ns_port[0]) ||
- !(Integer === ns_port[1])
- }
- raise ArgumentError.new("invalid nameserver config: #{@nameserver_port.inspect}")
- end
- if !@search.kind_of?(Array) ||
- !@search.all? {|ls| ls.all? {|l| Label::Str === l } }
- raise ArgumentError.new("invalid search config: #{@search.inspect}")
- end
- if !@ndots.kind_of?(Integer)
- raise ArgumentError.new("invalid ndots config: #{@ndots.inspect}")
- end
- @initialized = true
- end
- }
- self
- end
- def single?
- lazy_initialize
- if @nameserver_port.length == 1
- return @nameserver_port[0]
- else
- return nil
- end
- end
- def nameserver_port
- @nameserver_port
- end
- def generate_candidates(name)
- candidates = nil
- name = Name.create(name)
- if name.absolute?
- candidates = [name]
- else
- if @ndots <= name.length - 1
- candidates = [Name.new(name.to_a)]
- else
- candidates = []
- end
- candidates.concat(@search.map {|domain| Name.new(name.to_a + domain)})
- fname = Name.create("#{name}.")
- if !candidates.include?(fname)
- candidates << fname
- end
- end
- return candidates
- end
- InitialTimeout = 5
- def generate_timeouts
- ts = [InitialTimeout]
- ts << ts[-1] * 2 / @nameserver_port.length
- ts << ts[-1] * 2
- ts << ts[-1] * 2
- return ts
- end
- def resolv(name)
- candidates = generate_candidates(name)
- timeouts = @timeouts || generate_timeouts
- begin
- candidates.each {|candidate|
- begin
- timeouts.each {|tout|
- @nameserver_port.each {|nameserver, port|
- begin
- yield candidate, tout, nameserver, port
- rescue ResolvTimeout
- end
- }
- }
- raise ResolvError.new("DNS resolv timeout: #{name}")
- rescue NXDomain
- end
- }
- rescue ResolvError
- end
- end
- ##
- # Indicates no such domain was found.
- class NXDomain < ResolvError
- end
- ##
- # Indicates some other unhandled resolver error was encountered.
- class OtherResolvError < ResolvError
- end
- end
- module OpCode # :nodoc:
- Query = 0
- IQuery = 1
- Status = 2
- Notify = 4
- Update = 5
- end
- module RCode # :nodoc:
- NoError = 0
- FormErr = 1
- ServFail = 2
- NXDomain = 3
- NotImp = 4
- Refused = 5
- YXDomain = 6
- YXRRSet = 7
- NXRRSet = 8
- NotAuth = 9
- NotZone = 10
- BADVERS = 16
- BADSIG = 16
- BADKEY = 17
- BADTIME = 18
- BADMODE = 19
- BADNAME = 20
- BADALG = 21
- end
- ##
- # Indicates that the DNS response was unable to be decoded.
- class DecodeError < StandardError
- end
- ##
- # Indicates that the DNS request was unable to be encoded.
- class EncodeError < StandardError
- end
- module Label # :nodoc:
- def self.split(arg)
- labels = []
- arg.scan(/[^\.]+/) {labels << Str.new($&)}
- return labels
- end
- class Str # :nodoc:
- def initialize(string)
- @string = string
- # case insensivity of DNS labels doesn't apply non-ASCII characters. [RFC 4343]
- # This assumes @string is given in ASCII compatible encoding.
- @downcase = string.b.downcase
- end
- attr_reader :string, :downcase
- def to_s
- return @string
- end
- def inspect
- return "#<#{self.class} #{self}>"
- end
- def ==(other)
- return self.class == other.class && @downcase == other.downcase
- end
- def eql?(other)
- return self == other
- end
- def hash
- return @downcase.hash
- end
- end
- end
- ##
- # A representation of a DNS name.
- class Name
- ##
- # Creates a new DNS name from +arg+. +arg+ can be:
- #
- # Name:: returns +arg+.
- # String:: Creates a new Name.
- def self.create(arg)
- case arg
- when Name
- return arg
- when String
- return Name.new(Label.split(arg), /\.\z/ =~ arg ? true : false)
- else
- raise ArgumentError.new("cannot interpret as DNS name: #{arg.inspect}")
- end
- end
- def initialize(labels, absolute=true) # :nodoc:
- labels = labels.map {|label|
- case label
- when String then Label::Str.new(label)
- when Label::Str then label
- else
- raise ArgumentError, "unexpected label: #{label.inspect}"
- end
- }
- @labels = labels
- @absolute = absolute
- end
- def inspect # :nodoc:
- "#<#{self.class}: #{self}#{@absolute ? '.' : ''}>"
- end
- ##
- # True if this name is absolute.
- def absolute?
- return @absolute
- end
- def ==(other) # :nodoc:
- return false unless Name === other
- return false unless @absolute == other.absolute?
- return @labels == other.to_a
- end
- alias eql? == # :nodoc:
- ##
- # Returns true if +other+ is a subdomain.
- #
- # Example:
- #
- # domain = Resolv::DNS::Name.create("y.z")
- # p Resolv::DNS::Name.create("w.x.y.z").subdomain_of?(domain) #=> true
- # p Resolv::DNS::Name.create("x.y.z").subdomain_of?(domain) #=> true
- # p Resolv::DNS::Name.create("y.z").subdomain_of?(domain) #=> false
- # p Resolv::DNS::Name.create("z").subdomain_of?(domain) #=> false
- # p Resolv::DNS::Name.create("x.y.z.").subdomain_of?(domain) #=> false
- # p Resolv::DNS::Name.create("w.z").subdomain_of?(domain) #=> false
- #
- def subdomain_of?(other)
- raise ArgumentError, "not a domain name: #{other.inspect}" unless Name === other
- return false if @absolute != other.absolute?
- other_len = other.length
- return false if @labels.length <= other_len
- return @labels[-other_len, other_len] == other.to_a
- end
- def hash # :nodoc:
- return @labels.hash ^ @absolute.hash
- end
- def to_a # :nodoc:
- return @labels
- end
- def length # :nodoc:
- return @labels.length
- end
- def [](i) # :nodoc:
- return @labels[i]
- end
- ##
- # returns the domain name as a string.
- #
- # The domain name doesn't have a trailing dot even if the name object is
- # absolute.
- #
- # Example:
- #
- # p Resolv::DNS::Name.create("x.y.z.").to_s #=> "x.y.z"
- # p Resolv::DNS::Name.create("x.y.z").to_s #=> "x.y.z"
- def to_s
- return @labels.join('.')
- end
- end
- class Message # :nodoc:
- @@identifier = -1
- def initialize(id = (@@identifier += 1) & 0xffff)
- @id = id
- @qr = 0
- @opcode = 0
- @aa = 0
- @tc = 0
- @rd = 0 # recursion desired
- @ra = 0 # recursion available
- @rcode = 0
- @question = []
- @answer = []
- @authority = []
- @additional = []
- end
- attr_accessor :id, :qr, :opcode, :aa, :tc, :rd, :ra, :rcode
- attr_reader :question, :answer, :authority, :additional
- def ==(other)
- return @id == other.id &&
- @qr == other.qr &&
- @opcode == other.opcode &&
- @aa == other.aa &&
- @tc == other.tc &&
- @rd == other.rd &&
- @ra == other.ra &&
- @rcode == other.rcode &&
- @question == other.question &&
- @answer == other.answer &&
- @authority == other.authority &&
- @additional == other.additional
- end
- def add_question(name, typeclass)
- @question << [Name.create(name), typeclass]
- end
- def each_question
- @question.each {|name, typeclass|
- yield name, typeclass
- }
- end
- def add_answer(name, ttl, data)
- @answer << [Name.create(name), ttl, data]
- end
- def each_answer
- @answer.each {|name, ttl, data|
- yield name, ttl, data
- }
- end
- def add_authority(name, ttl, data)
- @authority << [Name.create(name), ttl, data]
- end
- def each_authority
- @authority.each {|name, ttl, data|
- yield name, ttl, data
- }
- end
- def add_additional(name, ttl, data)
- @additional << [Name.create(name), ttl, data]
- end
- def each_additional
- @additional.each {|name, ttl, data|
- yield name, ttl, data
- }
- end
- def each_resource
- each_answer {|name, ttl, data| yield name, ttl, data}
- each_authority {|name, ttl, data| yield name, ttl, data}
- each_additional {|name, ttl, data| yield name, ttl, data}
- end
- def encode
- return MessageEncoder.new {|msg|
- msg.put_pack('nnnnnn',
- @id,
- (@qr & 1) << 15 |
- (@opcode & 15) << 11 |
- (@aa & 1) << 10 |
- (@tc & 1) << 9 |
- (@rd & 1) << 8 |
- (@ra & 1) << 7 |
- (@rcode & 15),
- @question.length,
- @answer.length,
- @authority.length,
- @additional.length)
- @question.each {|q|
- name, typeclass = q
- msg.put_name(name)
- msg.put_pack('nn', typeclass::TypeValue, typeclass::ClassValue)
- }
- [@answer, @authority, @additional].each {|rr|
- rr.each {|r|
- name, ttl, data = r
- msg.put_name(name)
- msg.put_pack('nnN', data.class::TypeValue, data.class::ClassValue, ttl)
- msg.put_length16 {data.encode_rdata(msg)}
- }
- }
- }.to_s
- end
- class MessageEncoder # :nodoc:
- def initialize
- @data = ''.dup
- @names = {}
- yield self
- end
- def to_s
- return @data
- end
- def put_bytes(d)
- @data << d
- end
- def put_pack(template, *d)
- @data << d.pack(template)
- end
- def put_length16
- length_index = @data.length
- @data << "\0\0"
- data_start = @data.length
- yield
- data_end = @data.length
- @data[length_index, 2] = [data_end - data_start].pack("n")
- end
- def put_string(d)
- self.put_pack("C", d.length)
- @data << d
- end
- def put_string_list(ds)
- ds.each {|d|
- self.put_string(d)
- }
- end
- def put_name(d)
- put_labels(d.to_a)
- end
- def put_labels(d)
- d.each_index {|i|
- domain = d[i..-1]
- if idx = @names[domain]
- self.put_pack("n", 0xc000 | idx)
- return
- else
- if @data.length < 0x4000
- @names[domain] = @data.length
- end
- self.put_label(d[i])
- end
- }
- @data << "\0"
- end
- def put_label(d)
- self.put_string(d.to_s)
- end
- end
- def Message.decode(m)
- o = Message.new(0)
- MessageDecoder.new(m) {|msg|
- id, flag, qdcount, ancount, nscount, arcount =
- msg.get_unpack('nnnnnn')
- o.id = id
- o.qr = (flag >> 15) & 1
- o.opcode = (flag >> 11) & 15
- o.aa = (flag >> 10) & 1
- o.tc = (flag >> 9) & 1
- o.rd = (flag >> 8) & 1
- o.ra = (flag >> 7) & 1
- o.rcode = flag & 15
- (1..qdcount).each {
- name, typeclass = msg.get_question
- o.add_question(name, typeclass)
- }
- (1..ancount).each {
- name, ttl, data = msg.get_rr
- o.add_answer(name, ttl, data)
- }
- (1..nscount).each {
- name, ttl, data = msg.get_rr
- o.add_authority(name, ttl, data)
- }
- (1..arcount).each {
- name, ttl, data = msg.get_rr
- o.add_additional(name, ttl, data)
- }
- }
- return o
- end
- class MessageDecoder # :nodoc:
- def initialize(data)
- @data = data
- @index = 0
- @limit = data.bytesize
- yield self
- end
- def inspect
- "\#<#{self.class}: #{@data.byteslice(0, @index).inspect} #{@data.byteslice(@index..-1).inspect}>"
- end
- def get_length16
- len, = self.get_unpack('n')
- save_limit = @limit
- @limit = @index + len
- d = yield(len)
- if @index < @limit
- raise DecodeError.new("junk exists")
- elsif @limit < @index
- raise DecodeError.new("limit exceeded")
- end
- @limit = save_limit
- return d
- end
- def get_bytes(len = @limit - @index)
- raise DecodeError.new("limit exceeded") if @limit < @index + len
- d = @data.byteslice(@index, len)
- @index += len
- return d
- end
- def get_unpack(template)
- len = 0
- template.each_byte {|byte|
- byte = "%c" % byte
- case byte
- when ?c, ?C
- len += 1
- when ?n
- len += 2
- when ?N
- len += 4
- else
- raise StandardError.new("unsupported template: '#{byte.chr}' in '#{template}'")
- end
- }
- raise DecodeError.new("limit exceeded") if @limit < @index + len
- arr = @data.unpack("@#{@index}#{template}")
- @index += len
- return arr
- end
- def get_string
- raise DecodeError.new("limit exceeded") if @limit <= @index
- len = @data.getbyte(@index)
- raise DecodeError.new("limit exceeded") if @limit < @index + 1 + len
- d = @data.byteslice(@index + 1, len)
- @index += 1 + len
- return d
- end
- def get_string_list
- strings = []
- while @index < @limit
- strings << self.get_string
- end
- strings
- end
- def get_name
- return Name.new(self.get_labels)
- end
- def get_labels
- prev_index = @index
- save_index = nil
- d = []
- while true
- raise DecodeError.new("limit exceeded") if @limit <= @index
- case @data.getbyte(@index)
- when 0
- @index += 1
- if save_index
- @index = save_index
- end
- return d
- when 192..255
- idx = self.get_unpack('n')[0] & 0x3fff
- if prev_index <= idx
- raise DecodeError.new("non-backward name pointer")
- end
- prev_index = idx
- if !save_index
- save_index = @index
- end
- @index = idx
- else
- d << self.get_label
- end
- end
- end
- def get_label
- return Label::Str.new(self.get_string)
- end
- def get_question
- name = self.get_name
- type, klass = self.get_unpack("nn")
- return name, Resource.get_class(type, klass)
- end
- def get_rr
- name = self.get_name
- type, klass, ttl = self.get_unpack('nnN')
- typeclass = Resource.get_class(type, klass)
- res = self.get_length16 do
- begin
- typeclass.decode_rdata self
- rescue => e
- raise DecodeError, e.message, e.backtrace
- end
- end
- res.instance_variable_set :@ttl, ttl
- return name, ttl, res
- end
- end
- end
- ##
- # A DNS query abstract class.
- class Query
- def encode_rdata(msg) # :nodoc:
- raise EncodeError.new("#{self.class} is query.")
- end
- def self.decode_rdata(msg) # :nodoc:
- raise DecodeError.new("#{self.class} is query.")
- end
- end
- ##
- # A DNS resource abstract class.
- class Resource < Query
- ##
- # Remaining Time To Live for this Resource.
- attr_reader :ttl
- ClassHash = {} # :nodoc:
- def encode_rdata(msg) # :nodoc:
- raise NotImplementedError.new
- end
- def self.decode_rdata(msg) # :nodoc:
- raise NotImplementedError.new
- end
- def ==(other) # :nodoc:
- return false unless self.class == other.class
- s_ivars = self.instance_variables
- s_ivars.sort!
- s_ivars.delete :@ttl
- o_ivars = other.instance_variables
- o_ivars.sort!
- o_ivars.delete :@ttl
- return s_ivars == o_ivars &&
- s_ivars.collect {|name| self.instance_variable_get name} ==
- o_ivars.collect {|name| other.instance_variable_get name}
- end
- def eql?(other) # :nodoc:
- return self == other
- end
- def hash # :nodoc:
- h = 0
- vars = self.instance_variables
- vars.delete :@ttl
- vars.each {|name|
- h ^= self.instance_variable_get(name).hash
- }
- return h
- end
- def self.get_class(type_value, class_value) # :nodoc:
- return ClassHash[[type_value, class_value]] ||
- Generic.create(type_value, class_value)
- end
- ##
- # A generic resource abstract class.
- class Generic < Resource
- ##
- # Creates a new generic resource.
- def initialize(data)
- @data = data
- end
- ##
- # Data for this generic resource.
- attr_reader :data
- def encode_rdata(msg) # :nodoc:
- msg.put_bytes(data)
- end
- def self.decode_rdata(msg) # :nodoc:
- return self.new(msg.get_bytes)
- end
- def self.create(type_value, class_value) # :nodoc:
- c = Class.new(Generic)
- c.const_set(:TypeValue, type_value)
- c.const_set(:ClassValue, class_value)
- Generic.const_set("Type#{type_value}_Class#{class_value}", c)
- ClassHash[[type_value, class_value]] = c
- return c
- end
- end
- ##
- # Domain Name resource abstract class.
- class DomainName < Resource
- ##
- # Creates a new DomainName from +name+.
- def initialize(name)
- @name = name
- end
- ##
- # The name of this DomainName.
- attr_reader :name
- def encode_rdata(msg) # :nodoc:
- msg.put_name(@name)
- end
- def self.decode_rdata(msg) # :nodoc:
- return self.new(msg.get_name)
- end
- end
- # Standard (class generic) RRs
- ClassValue = nil # :nodoc:
- ##
- # An authoritative name server.
- class NS < DomainName
- TypeValue = 2 # :nodoc:
- end
- ##
- # The canonical name for an alias.
- class CNAME < DomainName
- TypeValue = 5 # :nodoc:
- end
- ##
- # Start Of Authority resource.
- class SOA < Resource
- TypeValue = 6 # :nodoc:
- ##
- # Creates a new SOA record. See the attr documentation for the
- # details of each argument.
- def initialize(mname, rname, serial, refresh, retry_, expire, minimum)
- @mname = mname
- @rname = rname
- @serial = serial
- @refresh = refresh
- @retry = retry_
- @expire = expire
- @minimum = minimum
- end
- ##
- # Name of the host where the master zone file for this zone resides.
- attr_reader :mname
- ##
- # The person responsible for this domain name.
- attr_reader :rname
- ##
- # The version number of the zone file.
- attr_reader :serial
- ##
- # How often, in seconds, a secondary name server is to check for
- # updates from the primary name server.
- attr_reader :refresh
- ##
- # How often, in seconds, a secondary name server is to retry after a
- # failure to check for a refresh.
- attr_reader :retry
- ##
- # Time in seconds that a secondary name server is to use the data
- # before refreshing from the primary name server.
- attr_reader :expire
- ##
- # The minimum number of seconds to be used for TTL values in RRs.
- attr_reader :minimum
- def encode_rdata(msg) # :nodoc:
- msg.put_name(@mname)
- msg.put_name(@rname)
- msg.put_pack('NNNNN', @serial, @refresh, @retry, @expire, @minimum)
- end
- def self.decode_rdata(msg) # :nodoc:
- mname = msg.get_name
- rname = msg.get_name
- serial, refresh, retry_, expire, minimum = msg.get_unpack('NNNNN')
- return self.new(
- mname, rname, serial, refresh, retry_, expire, minimum)
- end
- …
Large files files are truncated, but you can click here to view the full file