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

Instance Method Summary collapse

Constructor Details

#initialize(config_info = nil) ⇒ Config

Returns a new instance of Config.



1083
1084
1085
1086
1087
1088
# File 'lib/resolv.rb', line 1083

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

Instance Method Details

#generate_candidates(name) ⇒ Object



1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
# File 'lib/resolv.rb', line 1240

def generate_candidates(name)
  name = Name.create(name)
  if name.absolute?
    [name]
  else
    abs = Name.new(name.to_a)
    search = @search.map {|domain| Name.new(name.to_a + domain)}
    if @ndots <= name.length - 1
      [abs, *search].uniq
    else
      [*search, abs].uniq
    end
  end
end

#generate_timeouts ⇒ Object



1257
1258
1259
1260
1261
1262
1263
# File 'lib/resolv.rb', line 1257

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



1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
# File 'lib/resolv.rb', line 1148

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



1232
1233
1234
# File 'lib/resolv.rb', line 1232

def nameserver_port
  @nameserver_port
end

#resolv(name) ⇒ Object



1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
# File 'lib/resolv.rb', line 1265

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)


1223
1224
1225
1226
1227
1228
1229
1230
# File 'lib/resolv.rb', line 1223

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

#timeouts=(values) ⇒ Object



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

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)


1236
1237
1238
# File 'lib/resolv.rb', line 1236

def use_ipv6?
  @use_ipv6
end