123456789_123456789_123456789_123456789_123456789_

Class: Resolv::DNS::Config

Do not use. This class is for internal use only.
Relationships & Source Files
Namespace Children
Exceptions:
Inherits: Object
Defined in: lib/resolv.rb

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(config_info = nil) ⇒ Config

[ GitHub ]

  
# File 'lib/resolv.rb', line 931

def initialize(config_info=nil)
  @mutex = Thread::Mutex.new
  @config_info = config_info
  @initialized = nil
  @timeouts = nil
end

Class Method Details

.default_config_hash(filename = "/etc/resolv.conf")

[ GitHub ]

  
# File 'lib/resolv.rb', line 983

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

.parse_resolv_conf(filename)

[ GitHub ]

  
# File 'lib/resolv.rb', line 951

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+/)
      args.each(&:untaint)
      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

Instance Attribute Details

#single?Boolean (readonly)

[ GitHub ]

  
# File 'lib/resolv.rb', line 1068

def single?
  lazy_initialize
  if @nameserver_port.length == 1
    return @nameserver_port[0]
  else
    return nil
  end
end

#timeouts=(values) (writeonly)

[ GitHub ]

  
# File 'lib/resolv.rb', line 938

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

Instance Method Details

#generate_candidates(name)

[ GitHub ]

  
# File 'lib/resolv.rb', line 1081

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

#generate_timeouts

[ GitHub ]

  
# File 'lib/resolv.rb', line 1103

def generate_timeouts
  ts = [InitialTimeout]
  ts << ts[-1] * 2 / @nameserver_port.length
  ts << ts[-1] * 2
  ts << ts[-1] * 2
  return ts
end

#lazy_initialize

[ GitHub ]

  
# File 'lib/resolv.rb', line 998

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

#nameserver_port

[ GitHub ]

  
# File 'lib/resolv.rb', line 1077

def nameserver_port
  @nameserver_port
end

#resolv(name)

[ GitHub ]

  
# File 'lib/resolv.rb', line 1111

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