-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
295 lines (262 loc) · 5.9 KB
/
cache.go
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
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
284
285
286
287
288
289
290
291
292
293
294
295
// Author: Toke Høiland-Jørgensen (toke@toke.dk)
// Date: 13 Apr 2017
// Copyright (c) 2017, Toke Høiland-Jørgensen
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"bytes"
"sort"
"time"
"github.com/miekg/dns"
)
const (
addRR cacheReqType = iota
checkRR
removeRR
removeName
expireRRs
)
type cacheReqType int
type Cache struct {
entries map[string]CacheSlice /* entries mapped by name */
expiryList CacheSlice /* entries sorted by expiry time */
queue chan CacheRequest
ExpireCallback func(rr []dns.RR) bool
MaxTTL uint32
}
type CacheSlice []*CacheEntry
type CacheEntry struct {
rr dns.RR
expiry time.Time
}
type CacheRequest struct {
reqType cacheReqType
rr dns.RR
name string
reply chan bool
}
func (s CacheSlice) Len() int {
return len(s)
}
func (s CacheSlice) Less(i, j int) bool {
return s[i].expiry.Before(s[j].expiry)
}
func (s CacheSlice) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s CacheSlice) Remove(e *CacheEntry) CacheSlice {
idx := -1
for i, o := range s {
if o == e {
idx = i
break
}
}
if idx > -1 {
copy(s[idx:], s[idx+1:])
s[len(s)-1] = nil
return s[:len(s)-1]
}
return s
}
func same(a, b dns.RR) bool {
if a.Header().Rrtype != b.Header().Rrtype {
return false
}
switch a.Header().Rrtype {
case dns.TypeA:
aa := a.(*dns.A)
bb := b.(*dns.A)
return bytes.Compare(aa.A, bb.A) == 0
case dns.TypeAAAA:
aa := a.(*dns.AAAA)
bb := b.(*dns.AAAA)
return bytes.Compare(aa.AAAA, bb.AAAA) == 0
}
return false
}
func after(seconds uint32) time.Time {
return time.Now().Add(time.Duration(seconds) * time.Second)
}
func (c *Cache) Init() {
c.entries = make(map[string]CacheSlice)
c.expiryList = make(CacheSlice, 0, 10)
c.queue = make(chan CacheRequest)
go c.run()
}
/* return true if we added a new entry */
func (c *Cache) addRR(rr dns.RR) bool {
name := rr.Header().Name
ttl := c.clampTTL(rr)
if ttl == 0 {
return false
}
if nc, ok := c.entries[name]; ok {
for _, e := range nc {
if same(e.rr, rr) {
e.rr.Header().Ttl = ttl
e.expiry = after(ttl)
sort.Sort(c.expiryList)
return false
}
}
/* cache entry exists for name, but not for this RR */
ce := &CacheEntry{rr: rr,
expiry: after(ttl)}
c.entries[name] = append(c.entries[name], ce)
c.expiryList = append(c.expiryList, ce)
sort.Sort(c.expiryList)
return true
} else {
/* no prior cache entires for this name */
nc = make(CacheSlice, 1, 5)
nc[0] = &CacheEntry{rr: rr,
expiry: after(ttl)}
c.entries[name] = nc
c.expiryList = append(c.expiryList, nc[0])
sort.Sort(c.expiryList)
return true
}
}
func (c *Cache) checkRR(rr dns.RR) bool {
name := rr.Header().Name
if nc, ok := c.entries[name]; ok {
for _, e := range nc {
if same(e.rr, rr) {
return true
}
}
}
return false
}
func (c *Cache) removeRR(rr dns.RR) bool {
name := rr.Header().Name
if nc, ok := c.entries[name]; ok {
for _, e := range nc {
if same(e.rr, rr) {
c.entries[name] = nc.Remove(e)
c.expiryList = c.expiryList.Remove(e)
return true
}
}
}
return false
}
func (c *Cache) removeName(name string) bool {
if nc, ok := c.entries[name]; ok {
for _, e := range nc {
c.expiryList = c.expiryList.Remove(e)
}
delete(c.entries, name)
return true
}
return false
}
func (c *Cache) run() {
done := make(chan bool)
go func() {
for {
time.Sleep(time.Second)
select {
case <-done:
return
default:
c.queue <- CacheRequest{reqType: expireRRs}
}
}
}()
for req := range c.queue {
switch req.reqType {
case addRR:
req.reply <- c.addRR(req.rr)
case checkRR:
req.reply <- c.checkRR(req.rr)
case removeRR:
req.reply <- c.removeRR(req.rr)
case removeName:
req.reply <- c.removeName(req.name)
case expireRRs:
rr := make([]dns.RR, 0)
for now := time.Now(); len(c.expiryList) > 0 && c.expiryList[0].expiry.Before(now); {
e := c.expiryList[0]
c.expiryList = c.expiryList[1:]
name := e.rr.Header().Name
c.entries[name] = c.entries[name].Remove(e)
rr = append(rr, e.rr)
}
if len(rr) > 0 {
go c.ExpireCallback(rr)
}
}
}
done <- true
}
func (c *Cache) clampTTL(rr dns.RR) uint32 {
if rr.Header().Ttl > c.MaxTTL {
rr.Header().Ttl = c.MaxTTL
}
return rr.Header().Ttl
}
/**
* Add a RR to the cache. If it is not already in the cache,
* add it and return true, otherwise refresh the expiry time
* and return false.
*/
func (c *Cache) Add(rr dns.RR) bool {
reply := make(chan bool)
req := CacheRequest{reqType: addRR,
rr: rr,
reply: reply,
}
c.queue <- req
return <-reply
}
func (c *Cache) Check(rr dns.RR) bool {
reply := make(chan bool)
req := CacheRequest{reqType: checkRR,
rr: rr,
reply: reply,
}
c.queue <- req
return <-reply
}
func (c *Cache) Remove(rr dns.RR) bool {
reply := make(chan bool)
req := CacheRequest{reqType: removeRR,
rr: rr,
reply: reply,
}
c.queue <- req
return <-reply
}
func (c *Cache) RemoveName(name string) bool {
reply := make(chan bool)
req := CacheRequest{reqType: removeName,
name: name,
reply: reply,
}
c.queue <- req
return <-reply
}
func (c *Cache) Close(runCallback bool) {
close(c.queue)
if runCallback && len(c.expiryList) > 0 {
rr := make([]dns.RR, len(c.expiryList))
for i, e := range c.expiryList {
rr[i] = e.rr
}
c.ExpireCallback(rr)
}
}