Class: Dnsruby::Cache

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

Overview

:nodoc: all

Defined Under Namespace

Classes: CacheData, CacheKey, ResolutionCache

Constant Summary collapse

@@max_size =
16 * 1024
@@resolution_generation =
0
@@generation_mutex =
Mutex.new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(preserve_security: false) ⇒ Cache

Returns a new instance of Cache.



38
39
40
41
42
# File 'lib/dnsruby/cache.rb', line 38

def initialize(preserve_security: false)
@preserve_security = preserve_security
@cache = Hash.new
@mutex = Mutex.new
end

Class Method Details

.invalidate_resolutions ⇒ Object



35
36
37
# File 'lib/dnsruby/cache.rb', line 35

def self.invalidate_resolutions
  @@generation_mutex.synchronize { @@resolution_generation += 1 }
end

.negative_soa(message) ⇒ Object

A negative response requires an enclosing SOA. Following aliases here keeps a CNAME-only positive answer distinct from a NODATA response.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/dnsruby/cache.rb', line 67

def self.negative_soa(message)
  q = message.question.first
  return unless q && [RCode::NOERROR, RCode::NXDOMAIN].include?(message.rcode)
  terminal = q.qname
  seen = {}
  loop do
    return if seen[terminal]
    seen[terminal] = true
    return if message.rcode == RCode::NOERROR && message.answer.any? { |rr|
      rr.name == terminal && rr.klass == q.qclass &&
        (rr.type == q.qtype || (q.qtype == Types::ANY && rr.type != Types::RRSIG)) }
    alias_rr = message.answer.find { |rr| rr.name == terminal && rr.klass == q.qclass && rr.type == Types::CNAME }
    if alias_rr
      terminal = alias_rr.cname
    else
      # RFC 6672 permits omission of the synthesized CNAME. Follow the
      # closest enclosing DNAME using complete labels, just as validation
      # does, before deciding whether this response is negative.
      dname = message.answer.select do |rr|
        rr.type == Types::DNAME && rr.klass == q.qclass && terminal.subdomain_of?(rr.name)
      end.max_by { |rr| rr.name.labels.length }
      break unless dname
      terminal = Name.new(terminal.labels.first(terminal.labels.length - dname.name.labels.length) +
        dname.domainname.labels, true)
    end
  end
  message.authority.select { |rr| rr.type == Types::SOA && rr.klass == q.qclass &&
    (terminal == rr.name || terminal.subdomain_of?(rr.name)) }.max_by { |rr| rr.name.labels.length }
rescue ResolvError
  nil
end

.validation_scope ⇒ Object



160
161
162
163
164
165
# File 'lib/dnsruby/cache.rb', line 160

def self.validation_scope
  [@@generation_mutex.synchronize { @@resolution_generation },
    Dnssec.validation_policy, Dnssec.validation_limits,
    [Dnssec.root_verifier, Dnssec.anchor_verifier, Dnssec.dlv_verifier].map(&:validation_cache_identity),
    validation_discovery_scope]
end

Instance Method Details

#add(message, scope: nil, error: nil) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/dnsruby/cache.rb', line 99

def add(message, scope: nil, error: nil)
  return if message.header.tc
  q = message.question.first
  return unless q
  soa = Cache.negative_soa(message)
  return if message.rcode == RCode::NXDOMAIN && !soa
  return if message.rcode == RCode::NOERROR && message.answer.empty? && !soa
  key = CacheKey.new(q.qname, q.qtype, q.qclass).to_s
  # A bare NXDOMAIN applies to all types. With aliases, retain the full
  # original question so a CNAME query never receives a false name error.
  if soa && message.rcode == RCode::NXDOMAIN && message.answer.empty?
    key = CacheKey.new(q.qname, Types::ANY, q.qclass).to_s + ' NXDOMAIN'
  end
  key = [scope, key] if scope
  data = CacheData.new(message, preserve_security: @preserve_security, error: error)
  return if data.expiration <= Time.now.to_i
  @mutex.synchronize do
    @cache.delete(key)
    @cache[key] = data
    @cache.shift while @cache.size > @@max_size
  end
  data
end

#cache ⇒ Object



43
44
45
# File 'lib/dnsruby/cache.rb', line 43

def cache
  @cache
end

#clear ⇒ Object



46
47
48
49
50
# File 'lib/dnsruby/cache.rb', line 46

def clear()
  @mutex.synchronize {
    @cache = Hash.new
  }
end

#delete(qname, qtype, qclass = Classes.IN, scope: nil) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/dnsruby/cache.rb', line 51

def delete(qname, qtype, qclass = Classes.IN, scope: nil)
  qname = Name.create(qname)
  qname.absolute = true
  keys = [CacheKey.new(qname, qtype, qclass).to_s,
    CacheKey.new(qname, Types::ANY, qclass).to_s + ' NXDOMAIN']
  keys.map! { |key| [scope, key] } if scope
  @mutex.synchronize { keys.each { |key| @cache.delete(key) } }
end

#find(qname, qtype, qclass = Classes.IN, scope: nil) ⇒ Object

This method "fixes up" the response, so that the header and ttls are OK The resolver will still need to copy the flags and ID across from the query



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/dnsruby/cache.rb', line 124

def find(qname, qtype, qclass = Classes.IN, scope: nil)
  qn = Name.create(qname)
  qn.absolute = true
  key = CacheKey.new(qn, qtype, qclass).to_s
  name_error_key = CacheKey.new(qn, Types::ANY, qclass).to_s + ' NXDOMAIN'
  key, name_error_key = [scope, key], [scope, name_error_key] if scope
  data = @mutex.synchronize {
    key = name_error_key if !@cache[key] && @cache[name_error_key]
    data = @cache[key]
    if (!data)
      return nil
    end
    if (data.expiration <= Time.now.to_i)
      @cache.delete(key)
      TheLog.debug("CACHE lookup stale\n")
      return nil
    end
    data
  }
  # CacheData owns an immutable wire snapshot, so decoding does not need to
  # serialize other cache lookups, insertions or clears behind this lock.
  m = data.message
  if data.expiration <= Time.now.to_i
    @mutex.synchronize { @cache.delete(key) if @cache[key].equal?(data) }
    return nil
  end
  TheLog.debug("CACHE found\n")
  m.question[0] = Question.new(qname, qtype, qclass)
  m
end

#length ⇒ Object



59
60
61
# File 'lib/dnsruby/cache.rb', line 59

def length
  @mutex.synchronize { @cache.length }
end