Class: Dnsruby::Cache::CacheData

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

Overview

:nodoc: all

Instance Method Summary collapse

Constructor Details

#initialize(message, lifetime: nil, preserve_security: false, error: nil) ⇒ CacheData

Returns a new instance of CacheData.



377
378
379
380
381
382
383
384
385
# File 'lib/dnsruby/cache.rb', line 377

def initialize(message, lifetime: nil, preserve_security: false, error: nil)
  @lifetime = lifetime
  @completion_error = error && [error.class, error.message.dup.freeze].freeze
  if preserve_security
    @security_level = message.security_level
    @security_error = message.security_error
  end
  self.message = message
end

Instance Method Details

#expiration ⇒ Object



312
313
314
# File 'lib/dnsruby/cache.rb', line 312

def expiration
  @snapshot.expiration
end

#get_expiration(m) ⇒ Object



363
364
365
366
367
368
369
370
371
372
373
374
375
376
# File 'lib/dnsruby/cache.rb', line 363

def get_expiration(m)
  now = Time.now.to_i
  expirations = []
  soa = Cache.negative_soa(m)
  m.each_resource do |rr|
    next if rr.type == Types::OPT
    received_at = rr.instance_variable_get(:@dnsruby_received_at) || now
    ttl = rr.equal?(soa) ? [rr.ttl, soa.minimum].min : rr.ttl
    expirations << [received_at + ttl,
      rr.instance_variable_get(:@dnsruby_authenticated_expiration),
      m.instance_variable_get(:@dnsruby_authenticated_expiration)].compact.min
  end
  expirations.min || 0
end

#message ⇒ Object



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/dnsruby/cache.rb', line 336

def message
  snapshot = @snapshot
  m = Message.decode(snapshot.wire)
  m.cached = true
  m.instance_variable_set(:@dnsruby_authenticated_expiration, @authenticated_expiration) if @authenticated_expiration
  m.instance_variable_set(:@dnsruby_completion_error, @completion_error) if @completion_error
  m.security_level = @security_level if @security_level
  m.security_error = if @security_error.is_a?(Exception)
    @security_error.exception(@security_error.message)
  elsif @security_error
    @security_error.dup
  end
  #  @TODO@ What do we do about answerfrom, answersize, etc.?
  m.header.aa = false # Anything else to do here?
  #  Fix up TTLs!!
  now = Time.now.to_i
  index = -1
  m.each_resource {|rr|
    index += 1
    next if rr.type == Types::OPT
    rr.ttl = [[snapshot.record_expirations[index] - now, rr.ttl].min, 0].max
    rr.instance_variable_set(:@dnsruby_received_at, now)
    authenticated = snapshot.authenticated_expirations[index]
    rr.instance_variable_set(:@dnsruby_authenticated_expiration, authenticated) if authenticated
  }
  return m
end

#message=(m) ⇒ Object



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/dnsruby/cache.rb', line 316

def message=(m)
  now = Time.now.to_i
  record_expirations = []
  authenticated_expirations = []
  @authenticated_expiration = m.instance_variable_get(:@dnsruby_authenticated_expiration)
  soa = Cache.negative_soa(m)
  m.each_resource do |rr|
    authenticated = [rr.instance_variable_get(:@dnsruby_authenticated_expiration),
      @authenticated_expiration].compact.min
    authenticated_expirations << authenticated
    record_expirations << if rr.type != Types::OPT
      received_at = rr.instance_variable_get(:@dnsruby_received_at) || now
      ttl = rr.equal?(soa) ? [rr.ttl, soa.minimum].min : rr.ttl
      [received_at + ttl, authenticated].compact.min
    end
  end
  @snapshot = Snapshot.new(m.encode(true).freeze,
    @lifetime ? now + @lifetime : (record_expirations.compact.min || 0), record_expirations.freeze,
    authenticated_expirations.freeze).freeze
end

#to_s ⇒ Object



386
387
388
# File 'lib/dnsruby/cache.rb', line 386

def to_s
  return "#{self.message}"
end