-
Notifications
You must be signed in to change notification settings - Fork 19
/
dns_v1.go
487 lines (389 loc) · 14.2 KB
/
dns_v1.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
package process
import (
"bytes"
"encoding/binary"
"fmt"
"math"
"github.com/DataDog/mmh3"
)
const bufTooShortStr = "dns buffer is too short"
// DNS data is encoded as a very basic bucketed hash table. There are three blocks, or buffers, of data:
//
// The "name" block is all of the unique DNS names. The length of the name is stored as a varint
// followed by the name itself
//
// The "bucket" block contains all of the hash buckets. The format of each bucket is:
// varint for number of entries in bucket
// For each entry in the bucket:
// varint for length of ip
// ip bytes
// varint for number of names associated with the ip
// Each associated name is encoded as a varint which is the position of the actual name string in the name block
//
// The "position" block is a list of varints, one for each bucket, where each varint is a pointer to the start
// of the bucket in the bucket block
//
// The overall buffer is encoded as:
//
// 1 byte indicating version
// 2 bytes indicating the number of buckets
// varint indicating the length of the name buffer.
// varint indicating the length of the position buffer
// varint indicating the position of the "middle" (bucketCount / 2) bucket in the position block
// We will use this to skip the half of the buckets when searching for the target bucket index
// position block
// bucket block
// name block
//
// Notes:
//
// Using varints saves space at the cost of not having random access to certain sections of data, particularly the
// bucket position mapping. This was a deliberate trade off to reduce the size of the payload and thus memory usage
//
// Varints are also more finicky to deal with in terms of calculating required space ahead of time. This increases
// the implementation complexity, or at least the line count, but we reduce allocations & memory usage by
// pre-sizing the output buffers
//
// This type is not thread safe
type V1DNSEncoder struct {
BucketFactor float64
scratch [binary.MaxVarintLen64]byte // Used for varint encoding
}
type bucketEntry struct {
keys []string
size int
}
// 1 byte for version, 2 byte for bucket count
const dns1Version1PreambleLength = 3
// Used for calculating the number of buckets for a given input map.
// Currently the bucket count is calculated as `len(input) * bucketFactor`
const defaultBucketFactor = 0.75
func NewV1DNSEncoder() DNSEncoderV1 {
return &V1DNSEncoder{
BucketFactor: defaultBucketFactor,
}
}
func (e *V1DNSEncoder) EncodeMapped(dns map[string]*DNSDatabaseEntry, indexToOFfset []int32) ([]byte, error) {
return nil, fmt.Errorf("EncodeMapped not valid in V1")
}
func (e *V1DNSEncoder) EncodeDomainDatabase(names []string) ([]byte, []int32, error) {
return nil, nil, fmt.Errorf("EncodeDomainDatabase not valid in V1")
}
func (e *V1DNSEncoder) Encode(dns map[string]*DNSEntry) ([]byte, error) {
if len(dns) == 0 {
return nil, nil
}
bucketCount := getBucketCount(dns, e.BucketFactor)
buckets := make([]bucketEntry, bucketCount)
nameBufferLength := 0
namePositions := make(map[string]int)
allBucketsEmpty := true
// We do three things here:
// Build up the keys for each bucket
// Calculate the size in bytes for each bucket
// Calculate the size of the names buffer
// The final value of `nameBufferLength` is the size of the name buffer
for ip, entry := range dns {
if len(entry.Names) == 0 {
continue
}
allBucketsEmpty = false
bucket := int(mmh3.Hash32([]byte(ip))) % bucketCount
buckets[bucket].keys = append(buckets[bucket].keys, ip)
buckets[bucket].size += e.varIntSize(len(ip))
buckets[bucket].size += len(ip)
buckets[bucket].size += e.varIntSize(len(entry.Names))
for _, name := range entry.Names {
position, ok := namePositions[name]
if !ok {
position = nameBufferLength // Position is at the current end of the name buffer
namePositions[name] = position
nameBufferLength += e.varIntSize(len(name))
nameBufferLength += len(name)
}
buckets[bucket].size += e.varIntSize(position)
}
}
// Exit early if all the buckets are empty
if allBucketsEmpty {
return nil, nil
}
bucketBufferLength := 0
positionBufferLength := 0
// We encode the position of the "middle" bucket in the position buffer as an optimization for reads that
// lets us skip half of the buckets when scanning for the bucket index
middleBucket := bucketCount / 2
middleBucketPosition := 0
// The size of each bucket also includes the length of the number of keys so add that to each bucket size
// Calculate the size of the position buffer by summing the length of the varints of each bucket position
// Calculate the size of the bucket buffer by summing the sizes of all the buckets
for i := range buckets {
buckets[i].size += e.varIntSize(len(buckets[i].keys))
if i == middleBucket {
middleBucketPosition = positionBufferLength
}
positionBufferLength += e.varIntSize(bucketBufferLength)
bucketBufferLength += buckets[i].size
}
var bucketCountBuf [2]byte
binary.LittleEndian.PutUint16(bucketCountBuf[:], uint16(bucketCount))
sizeOfPositionBufferLength := e.varIntSize(positionBufferLength)
sizeOfNameBufferLength := e.varIntSize(nameBufferLength)
sizeOfMiddleBucketPosition := e.varIntSize(middleBucketPosition)
metaLength := dns1Version1PreambleLength + sizeOfPositionBufferLength + sizeOfNameBufferLength + sizeOfMiddleBucketPosition
bufferSize := metaLength + positionBufferLength + bucketBufferLength + nameBufferLength
buffer := make([]byte, bufferSize)
metaBuffer := buffer[:0]
positionBuffer := buffer[metaLength:][:0]
bucketBuffer := buffer[metaLength+positionBufferLength:][:0]
nameBuffer := buffer[metaLength+positionBufferLength+bucketBufferLength:]
metaBuffer = append(metaBuffer, dnsVersion1)
metaBuffer = append(metaBuffer, bucketCountBuf[:]...)
metaBuffer = e.appendVarInt(metaBuffer, positionBufferLength)
metaBuffer = e.appendVarInt(metaBuffer, nameBufferLength)
metaBuffer = e.appendVarInt(metaBuffer, middleBucketPosition)
for i := range buckets {
bucketBuffer = e.appendVarInt(bucketBuffer, len(buckets[i].keys))
for _, ip := range buckets[i].keys {
entry := dns[ip]
bucketBuffer = e.appendVarInt(bucketBuffer, len(ip))
bucketBuffer = append(bucketBuffer, ip...)
bucketBuffer = e.appendVarInt(bucketBuffer, len(entry.Names))
for _, name := range entry.Names {
position := namePositions[name]
bucketBuffer = e.appendVarInt(bucketBuffer, position)
}
}
}
// The position of each bucket is the cumulative sum of the sizes of the previous buckets
positionCounter := 0
for i := 0; i < bucketCount; i++ {
bucketPosition := 0
if i > 0 {
bucketPosition = buckets[i-1].size
}
positionCounter += bucketPosition
positionBuffer = e.appendVarInt(positionBuffer, positionCounter)
}
for name, position := range namePositions {
bytesWritten := binary.PutUvarint(nameBuffer[position:], uint64(len(name)))
copy(nameBuffer[position+bytesWritten:], name)
}
return buffer, nil
}
func (e *V1DNSEncoder) varIntSize(value int) int {
return binary.PutUvarint(e.scratch[0:], uint64(value))
}
func (e *V1DNSEncoder) appendVarInt(buf []byte, value int) []byte {
bytesWritten := binary.PutUvarint(e.scratch[0:], uint64(value))
return append(buf, e.scratch[0:bytesWritten]...)
}
func getV1(buf []byte, ip string) (string, []string) {
var first string
var names []string
iterateDNSV1(buf, ip, func(i, total int, entry string) bool {
if i == 0 {
first = entry
if total > 1 {
names = make([]string, 0, total-1)
}
} else {
names = append(names, entry)
}
return true
})
return first, names
}
func getDNSNamesV1(buf []byte) []string {
var names []string
// skip the preamble
index := dns1Version1PreambleLength
_, bytesRead := binary.Uvarint(buf[index:])
index += bytesRead
nameBufferLen, bytesRead := binary.Uvarint(buf[index:])
start := len(buf) - int(nameBufferLen)
nameBuffer := buf[start:]
for namePosition := 0; namePosition < len(nameBuffer); {
nameLength, bytesReadForName := binary.Uvarint(nameBuffer[namePosition:])
namePosition += bytesReadForName
name := string(nameBuffer[namePosition : namePosition+int(nameLength)])
names = append(names, name)
namePosition += int(nameLength)
}
return names
}
func iterateDNSV1(buf []byte, ip string, cb func(i, total int, entry string) bool) error {
return unsafeIterateDNSV1(buf, ip, func(i, total int, entry []byte) bool {
return cb(i, total, string(entry))
})
}
func unsafeIterateDNSV1(buf []byte, ip string, cb func(i, total int, entry []byte) bool) error {
bufLen := len(buf)
// Needs 3 bytes so that buf[1:] can convert to uint16
if bufLen <= 2 {
return fmt.Errorf(bufTooShortStr)
}
// Read overview:
// Compute the target bucket for the given ip
// Iterate over all the buckets to find position of the given bucket
// Advance to the position of the bucket
// For each entry in the bucket:
// Compare the key to the given IP and store the comparison result
// Iterate through the name positions associated with the key.
// If the key was a match, load the name value and add it to the result list. Return once all names are processed
// Otherwise iterate through the name positions to reach the next bucket entry
bucketCount := int(binary.LittleEndian.Uint16(buf[1:]))
// skip the preamble
index := dns1Version1PreambleLength
if index > bufLen {
return fmt.Errorf("dns buffer is too short, invalid preamble")
}
positionBufferLen, bytesRead := binary.Uvarint(buf[index:])
index += bytesRead
if index > bufLen {
return fmt.Errorf("dns buffer is too short, invalid position buffer length")
}
nameBufferLen, bytesRead := binary.Uvarint(buf[index:])
nameBuffer := buf[len(buf)-int(nameBufferLen):]
index += bytesRead
if index > bufLen {
return fmt.Errorf("dns buffer is too short, invalid middle bucket position")
}
middleBucketPosition, bytesRead := binary.Uvarint(buf[index:])
index += bytesRead
bucket := int(mmh3.Hash32([]byte(ip))) % bucketCount
// The length of the metadata is the current read index. We will use this to calculate the bucket read index below
metaLength := index
middleBucket := bucketCount / 2
startBucket := 0
endBucket := bucketCount
if bucket >= middleBucket {
startBucket = middleBucket
endBucket = bucketCount
index += int(middleBucketPosition)
}
// Search through the bucket map to find the position of the target bucket
// Due to varints, we don't know how large the bucket index is
// We iterate through all the buckets in order to advance the read pointer to the start of the bucket data
var bucketPosition int
for i := startBucket; i < endBucket; i++ {
if index > bufLen {
return fmt.Errorf("dns buffer is too short, invalid bucket position")
}
value, bytesRead := binary.Uvarint(buf[index:])
index += bytesRead
if bucket == i {
bucketPosition = int(value)
break
}
}
// Move read index to the start of the bucket data. Skip the metadata and the position buffer
index = metaLength + int(positionBufferLen) + bucketPosition
if index > bufLen {
return fmt.Errorf("dns buffer is too short, invalid bucket length")
}
bucketLength, bytesRead := binary.Uvarint(buf[index:])
index += bytesRead
for i := 0; i < int(bucketLength); i++ {
if index > bufLen {
return fmt.Errorf("dns buffer is too short, invalid key length")
}
keyLength, bytesRead := binary.Uvarint(buf[index:])
index += bytesRead
if index > bufLen || (index+int(keyLength)) > bufLen {
return fmt.Errorf("dns buffer is too short, invalid key data`")
}
key := buf[index : index+int(keyLength)]
index += int(keyLength)
matched := bytes.Equal(key, []byte(ip))
if index > bufLen {
return fmt.Errorf("dns buffer is too short, invalid value data`")
}
nameCount, bytesRead := binary.Uvarint(buf[index:])
index += bytesRead
// Advance through all name positions
// We still need to do this even if the current entry didn't match in order to get to the next bucket entry
for j := 0; j < int(nameCount); j++ {
if index > bufLen {
return fmt.Errorf("dns buffer is too short, invalid name data`")
}
namePosition, bytesRead := binary.Uvarint(buf[index:])
index += bytesRead
if !matched {
continue
}
if int(namePosition) > len(nameBuffer) {
return fmt.Errorf("name buffer is too short, invalid name position`")
}
nameLength, bytesReadForName := binary.Uvarint(nameBuffer[int(namePosition):])
start := int(namePosition) + bytesReadForName
if start > len(nameBuffer) || start+int(nameLength) > len(nameBuffer) {
return fmt.Errorf("name buffer is too short, invalid name`")
}
if !cb(j, int(nameCount), nameBuffer[start:start+int(nameLength)]) {
return nil
}
}
if matched {
return nil
}
}
return nil
}
func getBucketCount(dns map[string]*DNSEntry, bucketFactor float64) int {
bucketCount := int(float64(len(dns)) * bucketFactor)
if bucketCount == 0 {
return 1
}
if bucketCount > math.MaxUint16 {
return math.MaxUint16
}
return bucketCount
}
// GetDNS gets the DNS entries for the given IP from the given buffer
func GetDNS(buf []byte, ip string) (string, []string, error) {
if len(buf) == 0 || ip == "" {
return "", nil, nil
}
switch buf[0] {
case dnsVersion1:
first, strings := getV1(buf, ip)
return first, strings, nil
}
return "", nil, fmt.Errorf("Unexpected version %v", buf[0])
}
func getDNSNames(buf []byte) ([]string, error) {
if len(buf) == 0 {
return nil, nil
}
switch buf[0] {
case dnsVersion1:
names := getDNSNamesV1(buf)
return names, nil
}
return nil, fmt.Errorf("Unexpected version %v", buf[0])
}
// IterateDNS invokes the callback function for each DNS entry for the given IP in the given buffer
func IterateDNS(buf []byte, ip string, cb func(i, total int, entry string) bool) error {
if len(buf) == 0 || ip == "" {
return nil
}
switch buf[0] {
case dnsVersion1:
return iterateDNSV1(buf, ip, cb)
}
return fmt.Errorf("Unexpected version %v", buf[0])
}
// UnsafeIterateDNS invokes the callback function for each DNS entry for the given IP in the given buffer.
// Each entry is a the slice from the overall buffer. It should be copied before use
func UnsafeIterateDNS(buf []byte, ip string, cb func(i, total int, entry []byte) bool) error {
if len(buf) == 0 || ip == "" {
return nil
}
switch buf[0] {
case dnsVersion1:
unsafeIterateDNSV1(buf, ip, cb)
return nil
}
return fmt.Errorf("Unexpected version %v", buf[0])
}