aboutsummaryrefslogtreecommitdiffstats
path: root/cache/cache.go
blob: 9a5d72a6f4add1d5c06b95eaf22345f60e69b744 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package cache

import (
	"encoding/binary"
	"fmt"
	"hash/fnv"
	"net"
	"sync"
	"time"

	"github.com/miekg/dns"
)

// Cache represents a cache of DNS entries. Use New to initialize a new cache.
type Cache struct {
	capacity   int
	now        func() time.Time
	maintainer *maintainer
	mu         sync.RWMutex
	wg         sync.WaitGroup
	entries    map[uint32]*Value
	keys       []uint32
}

type maintainer struct {
	interval time.Duration
	done     chan bool
}

func maintain(cache *Cache, interval time.Duration) {
	m := &maintainer{
		interval: interval,
		done:     make(chan bool),
	}
	cache.maintainer = m
	cache.wg.Add(1)
	go m.run(cache)
}

func (m *maintainer) run(cache *Cache) {
	defer cache.wg.Done()
	ticker := time.NewTicker(m.interval)
	for {
		select {
		case <-ticker.C:
			cache.deleteExpired()
		case <-m.done:
			ticker.Stop()
			return
		}
	}
}

// Value represents a value stored in the cache.
type Value struct {
	Question  string
	Qtype     uint16
	Answer    net.IP
	CreatedAt time.Time
	msg       *dns.Msg
}

// TTL returns the TTL of this cache value.
func (v *Value) TTL() time.Duration { return minTTL(v.msg) }

// New creates a new cache of given capacity. Stale cache entries are removed at expiryInterval.
func New(capacity int, expiryInterval time.Duration) (*Cache, error) {
	if capacity < 0 {
		return nil, fmt.Errorf("invalid capacity: %d", capacity)
	}
	if expiryInterval == 0 {
		expiryInterval = 10 * time.Minute
	}
	cache := &Cache{
		now:      time.Now,
		capacity: capacity,
		entries:  make(map[uint32]*Value, capacity),
	}
	maintain(cache, expiryInterval)
	return cache, nil
}

// NewKey creates a new cache key for the DNS name, qtype and qclass
func NewKey(name string, qtype, qclass uint16) uint32 {
	h := fnv.New32a()
	h.Write([]byte(name))
	binary.Write(h, binary.LittleEndian, qtype)
	binary.Write(h, binary.LittleEndian, qclass)
	return h.Sum32()
}

// Close closes the cache.
func (c *Cache) Close() error {
	c.maintainer.done <- true
	c.wg.Wait()
	return nil
}

// Get returns the DNS message associated with key k. Get will return nil if any TTL in the answer section of the
// message is exceeded according to time t.
func (c *Cache) Get(k uint32) (*dns.Msg, bool) {
	v, ok := c.getValue(k)
	if !ok {
		return nil, false
	}
	return v.msg, true
}

func (c *Cache) getValue(k uint32) (*Value, bool) {
	c.mu.RLock()
	v, ok := c.entries[k]
	c.mu.RUnlock()
	if !ok || c.isExpired(v) {
		return nil, false
	}
	return v, true
}

// List returns the n most recent cache values.
func (c *Cache) List(n int) []*Value {
	values := make([]*Value, 0, n)
	c.mu.RLock()
	for i := len(c.keys) - 1; i >= 0; i-- {
		if len(values) == n {
			break
		}
		v, _ := c.getValue(c.keys[i])
		values = append(values, v)
	}
	c.mu.RUnlock()
	return values
}

// Set associated key k with the DNS message v. Message msg will expire from the cache according to its TTL. Setting a
// new key in a cache that has reached its capacity will remove the first key.
func (c *Cache) Set(k uint32, msg *dns.Msg) {
	if c.capacity == 0 {
		return
	}
	if !isCacheable(msg) {
		return
	}
	now := c.now()
	c.mu.Lock()
	if len(c.entries) == c.capacity && c.capacity > 0 {
		delete(c.entries, c.keys[0])
		c.keys = c.keys[1:]
	}
	c.entries[k] = &Value{
		Question:  question(msg),
		Answer:    answer(msg),
		Qtype:     qtype(msg),
		CreatedAt: now,
		msg:       msg,
	}
	c.keys = append(c.keys, k)
	c.mu.Unlock()
}

func qtype(m *dns.Msg) uint16 { return m.Question[0].Qtype }

func question(m *dns.Msg) string { return m.Question[0].Name }

func answer(m *dns.Msg) net.IP {
	rr := m.Answer[0]
	switch v := rr.(type) {
	case *dns.A:
		return v.A
	case *dns.AAAA:
		return v.AAAA
	}
	return net.IPv4zero
}

func (c *Cache) deleteExpired() {
	c.mu.Lock()
	for k, v := range c.entries {
		if c.isExpired(v) {
			delete(c.entries, k)
		}
	}
	c.mu.Unlock()
}

func (c *Cache) isExpired(v *Value) bool {
	now := c.now()
	for _, answer := range v.msg.Answer {
		if now.After(v.CreatedAt.Add(ttl(answer))) {
			return true
		}
	}
	return false
}

func min(x, y uint32) uint32 {
	if x < y {
		return x
	}
	return y
}

func minTTL(m *dns.Msg) time.Duration {
	var ttl uint32 = 1<<32 - 1 //  avoids importing math.MaxUint32
	for _, answer := range m.Answer {
		ttl = min(answer.Header().Ttl, ttl)
	}
	for _, ns := range m.Ns {
		ttl = min(ns.Header().Ttl, ttl)
	}
	return time.Duration(ttl) * time.Second
}

func isCacheable(m *dns.Msg) bool {
	if minTTL(m) == 0 {
		return false
	}
	return m.Rcode == dns.RcodeSuccess || m.Rcode == dns.RcodeNameError
}

func ttl(rr dns.RR) time.Duration {
	ttlSecs := rr.Header().Ttl
	return time.Duration(ttlSecs) * time.Second
}