Class: Resolv::DNS::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/resolv.rb

Overview

:nodoc:

Defined Under Namespace

Classes: NXDomain, OtherResolvError

Constant Summary collapse

InitialTimeout =
5

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_info = nil) ⇒ Config

Returns a new instance of Config.



952
953
954
955
956
957
# File 'lib/resolv.rb', line 952

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") ⇒ Object



1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
# File 'lib/resolv.rb', line 1003

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) ⇒ Object



972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
# File 'lib/resolv.rb', line 972

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.concat(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



1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
# File 'lib/resolv.rb', line 1110

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_timeoutsObject



1132
1133
1134
1135
1136
1137
1138
# File 'lib/resolv.rb', line 1132

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

#lazy_initializeObject



1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
# File 'lib/resolv.rb', line 1018

def lazy_initialize
  @mutex.synchronize {
    unless @initialized
      @nameserver_port = []
      @use_ipv6 = nil
      @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
      if config_hash.include? :use_ipv6
        @use_ipv6 = config_hash[:use_ipv6]
      end
      @search = config_hash[:search] if config_hash.include? :search
      @ndots = config_hash[:ndots] if config_hash.include? :ndots
      @raise_timeout_errors = config_hash[:raise_timeout_errors]

      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_portObject



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

def nameserver_port
  @nameserver_port
end

#resolv(name) ⇒ Object



1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
# File 'lib/resolv.rb', line 1140

def resolv(name)
  candidates = generate_candidates(name)
  timeouts = @timeouts || generate_timeouts
  timeout_error = false
  begin
    candidates.each {|candidate|
      begin
        timeouts.each {|tout|
          @nameserver_port.each {|nameserver, port|
            begin
              yield candidate, tout, nameserver, port
            rescue ResolvTimeout
            end
          }
        }
        timeout_error = true
        raise ResolvError.new("DNS resolv timeout: #{name}")
      rescue NXDomain
      end
    }
  rescue ResolvError
    raise if @raise_timeout_errors && timeout_error
  end
end

#single?Boolean

Returns:

  • (Boolean)


1093
1094
1095
1096
1097
1098
1099
1100
# File 'lib/resolv.rb', line 1093

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

#timeouts=(values) ⇒ Object



959
960
961
962
963
964
965
966
967
968
969
970
# File 'lib/resolv.rb', line 959

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

#use_ipv6?Boolean

Returns:

  • (Boolean)


1106
1107
1108
# File 'lib/resolv.rb', line 1106

def use_ipv6?
  @use_ipv6
end