Class: Dnsruby::Cache::ResolutionCache

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

Overview

A resolver's completed negative answers and resolution failures. Failure retention is deliberately short (RFC 9520 requires at least one second). The caller supplies a scope captured before the resolution begins.

Constant Summary collapse

FAILURE_LIFETIME =

:nodoc:

1
FAILURE_LIMIT =
1024

Instance Method Summary collapse

Constructor Details

#initialize ⇒ ResolutionCache

Returns a new instance of ResolutionCache.



214
215
216
217
218
# File 'lib/dnsruby/cache.rb', line 214

def initialize
  @negative = Cache.new(preserve_security: true)
  @failures = {}
  @mutex = Mutex.new
end

Instance Method Details

#add(query, scope, response, error, control: nil, no_response: false) ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/dnsruby/cache.rb', line 243

def add(query, scope, response, error, control: nil, no_response: false)
  return unless scope
  begin
    control&.check!
  rescue QueryControl::Stopped
    return
  end
  return if response && (response.tsig || response.signed? || response.header.tc)
  @mutex.synchronize do
    question = query.question.first
    key = [scope, CacheKey.new(question.qname, question.qtype, question.qclass).to_s]
    if response && (!error || error.is_a?(NXDomain)) &&
        [RCode::NOERROR, RCode::NXDOMAIN].include?(response.rcode) &&
        response.security_level != Message::SecurityLevel::BOGUS
      # A later successful completion supersedes earlier failures and
      # contradictory negatives, even when the success itself has TTL 0.
      @failures.delete(key)
      @negative.delete(question.qname, question.qtype, question.qclass, scope: scope)
    end
    if response && (!error || error.is_a?(NXDomain)) && Cache.negative_soa(response)
      if query.do_validation && response.security_level == Message::SecurityLevel::INSECURE
        proof_expiration = response.instance_variable_get(:@dnsruby_authenticated_expiration)
        return unless proof_expiration && proof_expiration > Time.now.to_i
      end
      return if response.security_level == Message::SecurityLevel::BOGUS
      @negative.add(response, scope: scope, error: error)
    # Nested lookups may inherit an already-consumed validation budget;
    # its exhaustion does not establish that this question itself fails.
    elsif no_response || (error && !error.is_a?(ValidationCancelled) &&
        !(control && error.is_a?(ValidationLimitError)) &&
        [ResolvTimeout, ServFail, Refused, FormErr, VerifyError, SocketEofResolvError,
          IOError, SystemCallError].any? { |klass| error.is_a?(klass) })
      @negative.delete(question.qname, question.qtype, question.qclass, scope: scope)
      snapshot = response && CacheData.new(response, lifetime: FAILURE_LIFETIME, preserve_security: true)
      @failures.delete(key)
      @failures[key] = [TransportClock.now + FAILURE_LIFETIME,
        snapshot, error&.class, error&.message&.dup]
      @failures.shift while @failures.length > FAILURE_LIMIT
    end
  end
end

#find(query, scope) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/dnsruby/cache.rb', line 220

def find(query, scope)
  return unless scope
  question = query.question.first
  @mutex.synchronize do
    response = @negative.find(question.qname, question.qtype, question.qclass, scope: scope)
    if response
      response.header.id = query.header.id
      failure = response.instance_variable_get(:@dnsruby_completion_error)
      return [response, failure && failure[0].new(failure[1])]
    end
    key = [scope, CacheKey.new(question.qname, question.qtype, question.qclass).to_s]
    entry = @failures[key]
    return unless entry
    if entry[0] <= TransportClock.now
      @failures.delete(key)
      return
    end
    response = entry[1]&.message
    response.header.id = query.header.id if response
    [response, entry[2] && entry[2].new(entry[3])]
  end
end