Class: Resolv::DNS::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/net/dns/resolv.rb,
lib/net/dns/resolvx.rb

Overview

Encapsulates the resolver configuration information.

config_info can be nil, a String or a Hash:

- nil is the default, configuration is read from /etc/resolv.conf, or
  from Win32::Resolv.get_resolv_info on Windows.
- String is used as the name of the config file to parse instead of
  /etc/resolv.conf.
- Hash must map the :nameserver and/or :search symbol keys to a single
  String or an array of String to use as the value of those config options.

Defined Under Namespace

Classes: NXDomain, OtherResolvError

Constant Summary collapse

InitialTimeout =

:nodoc:

5

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_info = nil) ⇒ Config

Returns a new instance of Config.



773
774
775
776
777
# File 'lib/net/dns/resolv.rb', line 773

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

Instance Attribute Details

#nameserversObject (readonly)

The list of nameservers to query, should be dotted IP addresses, not domain names.



55
56
57
# File 'lib/net/dns/resolvx.rb', line 55

def nameservers
  @nameservers
end

#ndotsObject (readonly)

A name that has a number of labels greater than than ndots will be looked up directly. The default value of ndots is 1, so “example” would not be looked up directly, but “example.com” would be (it has 1 dot). Names ending in a dot, like “org.”, will always be looked up directly, regardless of the setting of ndots.



49
50
51
# File 'lib/net/dns/resolvx.rb', line 49

def ndots
  @ndots
end

#searchObject (readonly)

A series of search suffixes to use if the name being looked up does not end in a dot.



52
53
54
# File 'lib/net/dns/resolvx.rb', line 52

def search
  @search
end

Class Method Details

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

:nodoc:



813
814
815
816
817
818
819
820
821
822
823
824
825
# File 'lib/net/dns/resolv.rb', line 813

def Config.default_config_hash(filename="/etc/resolv.conf") # :nodoc:
  if File.exist? filename
    config_hash = Config.parse_resolv_conf(filename)
  else
    if /mswin32|cygwin|mingw|bccwin/ =~ RUBY_PLATFORM
      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) ⇒ Object

:nodoc:



779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
# File 'lib/net/dns/resolv.rb', line 779

def Config.parse_resolv_conf(filename) # :nodoc:
  nameserver = []
  search = nil
  ndots = 1
  open(filename) {|f|
    f.each {|line|
      line.sub!(/[#;].*/, '')
      keyword, *args = line.split(/\s+/)
      args.each { |arg|
        arg.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 Method Details

#generate_candidates(name) ⇒ Object

:nodoc:



894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
# File 'lib/net/dns/resolv.rb', line 894

def generate_candidates(name) # :nodoc:
  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)})
  end
  return candidates
end

#generate_timeoutsObject

:nodoc:



912
913
914
915
916
917
918
# File 'lib/net/dns/resolv.rb', line 912

def generate_timeouts # :nodoc:
  ts = [InitialTimeout]
  ts << ts[-1] * 2 / @nameserver.length
  ts << ts[-1] * 2
  ts << ts[-1] * 2
  return ts
end

#lazy_initializeObject

:nodoc:



827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
# File 'lib/net/dns/resolv.rb', line 827

def lazy_initialize # :nodoc:
  @mutex.synchronize {
    unless @initialized
      @nameserver = []
      @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
      @nameserver = config_hash[:nameserver] if config_hash.include? :nameserver
      @search = config_hash[:search] if config_hash.include? :search
      @ndots = config_hash[:ndots] if config_hash.include? :ndots

      @nameserver = ['0.0.0.0'] if @nameserver.empty?
      if @search
        @search = @search.map {|arg| Label.split(arg) }
      else
        hostname = Socket.gethostname
        if /\./ =~ hostname
          @search = [Label.split($')]
        else
          @search = [[]]
        end
      end

      if !@nameserver.kind_of?(Array) ||
         !@nameserver.all? {|ns| String === ns }
        raise ArgumentError.new("invalid nameserver config: #{@nameserver.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

#resolv(name) ⇒ Object

:nodoc:



920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
# File 'lib/net/dns/resolv.rb', line 920

def resolv(name) # :nodoc:
  candidates = generate_candidates(name)
  timeouts = generate_timeouts
  begin
    candidates.each {|candidate|
      begin
        timeouts.each {|tout|
          @nameserver.each {|nameserver|
            begin
              yield candidate, tout, nameserver
            rescue ResolvTimeout
            end
          }
        }
        raise ResolvError.new("DNS resolv timeout: #{name}")
      rescue NXDomain
      end
    }
  rescue ResolvError
  end
end

#single?Boolean

Returns:

  • (Boolean)


885
886
887
888
889
890
891
892
# File 'lib/net/dns/resolv.rb', line 885

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