-
Notifications
You must be signed in to change notification settings - Fork 19
/
answer_sorted.go
338 lines (269 loc) · 8.54 KB
/
answer_sorted.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
Copyright (c) Meta Platforms, Inc. and affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package db
import (
"bytes"
"errors"
"io"
"github.com/facebook/dns/dnsrocks/dnsdata"
"github.com/golang/glog"
"github.com/miekg/dns"
)
func (r *sortedDataReader) FindAnswer(q []byte, packedControlName []byte, qname string, qtype uint16, loc *Location, a *dns.Msg, maxAnswer int) (bool, bool) {
var (
wrs = Wrs{MaxAnswers: maxAnswer}
err error
recordFound = false
wildcard = false
// resource record pointer used during record lookups
rec ResourceRecord
// rr will be used to construct temporary ResourceRecords
rr dns.RR
rrs []dns.RR
)
parseResult := func(result []byte) error {
if errors.Is(err, io.EOF) {
return nil
}
if rec, err = ExtractRRFromRow(result, wildcard); err != nil {
// Not a location match
// nolint: nilerr
return nil
}
recordFound = true
if rec.Qtype == dns.TypeCNAME || rec.Qtype == qtype || qtype == dns.TypeANY {
// When dealing with A/AAAA we may have weighted round-robin records
// Compute the weight and update wrr4/wrr6 with the current winner.
// When we are done looping, we will add the record to the answer.
if rec.Qtype == dns.TypeA || rec.Qtype == dns.TypeAAAA {
if err := wrs.Add(rec, result); err != nil {
glog.Errorf("Failed in adding record to WRS: %v", err)
}
// For other records, we append them to the answer.
} else {
hdr := dns.RR_Header{Name: qname, Rrtype: rec.Qtype, Class: dns.ClassINET, Ttl: rec.TTL, Rdlength: uint16(len(result[rec.Offset:]))}
rr, _, err = dns.UnpackRRWithHeader(hdr, result, rec.Offset)
if err != nil {
glog.Errorf("Failed to convert from tinydns format %v %d, %d", err, hdr.Rdlength, len(result[rec.Offset:]))
return err
}
a.Answer = append(a.Answer, rr)
}
}
return nil
}
var lastLength = len(q)
preIterationCheck := func(q []byte, length int) bool {
// we iterate on labels of initial qName, so length check should be sufficient to determine
// whether we passed zone border
if length < len(packedControlName) {
return false
}
// we can also check if reversed control name is part of new q
// (which can be any key less than what we look for),
// but that will require another allocation to actually store this reversed control name,
// plus `bytes.Equal` call
// i points to the first character of the label
// i-1 is length of the label
i := length
for i < lastLength {
labelLength := int(q[i-1])
label := q[i : i+labelLength]
if !dnsLabelWildsafe(label) {
return false
}
i += labelLength + 1
}
lastLength = length
return true
}
postIterationCheck := func() bool {
// append A/AAAA records with the selected RR record
if rrs, err = wrs.ARecord(qname, dns.ClassINET); err != nil {
glog.Errorf("%v", err)
} else {
a.Answer = append(a.Answer, rrs...)
}
if rrs, err = wrs.AAAARecord(qname, dns.ClassINET); err != nil {
glog.Errorf("%v", err)
} else {
a.Answer = append(a.Answer, rrs...)
}
if recordFound {
return false
}
wildcard = true
return true
}
r.find(q, loc, parseResult, preIterationCheck, postIterationCheck)
return wrs.WeightedAnswer(), recordFound
}
func (r *sortedDataReader) IsAuthoritative(q []byte, loc *Location) (ns bool, auth bool, zoneCut []byte, err error) {
parseResult := func(result []byte) error {
rec, err := ExtractRRFromRow(result, false)
if err != nil {
// nolint: nilerr
return nil
}
switch rec.Qtype {
case dns.TypeSOA:
auth = true
case dns.TypeNS:
ns = true
}
return nil
}
var zoneCutLength int
preIterationCheck := func(_ []byte, qLength int) bool {
zoneCutLength = qLength
return true
}
postIterationCheck := func() bool {
return !ns
}
r.find(q, loc, parseResult, preIterationCheck, postIterationCheck)
zoneCut = q[len(q)-zoneCutLength:]
return
}
func (r *sortedDataReader) find(
q []byte,
loc *Location,
parseResult func(value []byte) error,
preIterationCheck func(qName []byte, currentLength int) bool,
postIterationCheck func() bool,
) {
var err error
reversedQName := reverseZoneName(q)
qLength := len(reversedQName)
key := make([]byte, len(q)+max(len(loc.LocID), len(EmptyLocation.LocID))+len(dnsdata.ResourceRecordsKeyMarker))
copy(key, []byte(dnsdata.ResourceRecordsKeyMarker))
// assumption is that both provided location AND empty location have same length
locationLength := len(loc.LocID)
domainNameStart := len(dnsdata.ResourceRecordsKeyMarker)
copy(key[domainNameStart:], reversedQName)
for {
// intentionally passing untouched reversed QName so client can examine length changes
// for example for wildcard safety
if !preIterationCheck(reversedQName, qLength) {
break
}
locationStart := domainNameStart + qLength
// mark domain name end. we can avoid re-copying as we are cutting labels from the end
key[locationStart-1] = 0x00
copy(key[locationStart:], loc.LocID[:])
key = key[:locationStart+locationLength]
// new key that is equal to what we asked for, or less than it.
// This new key can be very different from what we requested, i.e.
// for com.example.foo (if exact match is not found) previous key will be returned,
// which doesn't guaranteed to even start with com.example, it can be com.examnle.foo for what we know
var k []byte
k, err = r.TryForEach(key, parseResult)
if err != nil {
break
}
// zone cut for different location exists -> check default location
if !loc.IsEmpty() &&
// empty location override might exists as only location part is different in found key
len(key) == len(k) &&
bytes.Equal(
key[:len(key)-locationLength], k[:len(key)-locationLength],
) {
copy(key[locationStart:], EmptyLocation.LocID[:])
k, err = r.TryForEach(key, parseResult)
if err != nil {
break
}
}
if !postIterationCheck() {
break
}
if len(k) < len(dnsdata.ResourceRecordsKeyMarker) ||
!bytes.Equal(k[:len(dnsdata.ResourceRecordsKeyMarker)], []byte(dnsdata.ResourceRecordsKeyMarker)) {
// reached border of data
break
}
// reached root zone
if qLength == 1 {
break
}
// strip out prefix and location
foundLabel := k[domainNameStart : len(k)-locationLength]
// ignore end length as it will be \0 for foundLabel and most probably non \0 for current name
if bytes.Equal(reversedQName[:qLength-1], foundLabel[:len(foundLabel)-1]) {
qLength = getLengthWithoutLastLabel(reversedQName, qLength)
} else {
qLength = findCommonLongestPrefix(reversedQName, foundLabel) + 1 // +1 for terminating \0
}
}
}
// cut out exactly one label from the end
func getLengthWithoutLastLabel(qName []byte, qLength int) int {
var i byte
lastLabelLengthIndex := 0
for i < byte(qLength)-1 {
lastLabelLengthIndex = int(i)
i += qName[i] + 1
}
return lastLabelLengthIndex + 1
}
// TryForEach performs provided operation on each value in case exact key is found
// otherwise closest smaller key (previous) is returned
func (r *sortedDataReader) TryForEach(key []byte, f func(value []byte) error) (foundKey []byte, err error) {
foundKey, err = r.closestKeyFinder.FindClosestKey(key, r.context)
if err != nil {
return nil, err
}
if bytes.Equal(key, foundKey) {
err = r.ForEach(key, f)
}
return foundKey, err
}
// search for common labels at the beginning of 2 packed names
// returns length of the common part
func findCommonLongestPrefix(str1 []byte, str2 []byte) int {
i := 0
for i < len(str1) && i < len(str2) {
if str1[i] != str2[i] {
break
}
match := true
for j := i + 1; j <= i+int(str1[i]); j++ {
if str1[j] != str2[j] {
match = false
break
}
}
if match {
i = i + int(str1[i]) + 1
} else {
break
}
}
return i
}
func reverseZoneName(qName []byte) []byte {
reversedName := make([]byte, len(qName))
reverseZoneNameToBuffer(qName, reversedName)
return reversedName
}
func reverseZoneNameToBuffer(qName []byte, destination []byte) {
i := byte(len(qName) - 1)
destination[i] = 0
for qName[0] > 0 {
i -= qName[0]
copy(destination[i:], qName[1:qName[0]+1])
i--
destination[i] = qName[0]
qName = qName[qName[0]+1:]
}
}