-
Notifications
You must be signed in to change notification settings - Fork 28
/
tree.go
675 lines (582 loc) · 18 KB
/
tree.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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
// Package mmdbwriter provides the tools to create and write MaxMind DB
// files.
package mmdbwriter
import (
"bufio"
"errors"
"fmt"
"io"
"math"
"net"
"time"
"github.com/oschwald/maxminddb-golang"
"go4.org/netipx"
"github.com/maxmind/mmdbwriter/inserter"
"github.com/maxmind/mmdbwriter/mmdbtype"
)
var (
metadataStartMarker = []byte("\xAB\xCD\xEFMaxMind.com")
dataSectionSeparator = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
)
// Options holds configuration parameters for the writer.
type Options struct {
// BuildEpoch is the database build timestamp as a Unix epoch value. It
// defaults to the epoch of when New was called.
BuildEpoch int64
// DatabaseType is a string that indicates the structure of each data record
// associated with an IP address. The actual definition of these structures
// is left up to the database creator.
DatabaseType string
// Description is a map where the key is a language code and the value is
// the description of the database in that language.
Description map[string]string
// DisableIPv4Aliasing will disable the IPv4 aliasing in IPv6 trees. This
// aliasing maps some IPv6 networks to the IPv4 network, e.g.,
// ::ffff:0:0/96.
DisableIPv4Aliasing bool
// IncludeReservedNetworks will allow reserved networks to be added to the
// database.
//
// If this is false, any attempt to insert into these networks will result
// in an error and inserting a network that contains a reserved network will
// result in the reserved portion of the network being excluded. Reserved
// networks that are globally routable to an individual device, such as
// Teredo, may still be added.
IncludeReservedNetworks bool
// IPVersion indicates whether an IPv4 or IPv6 database should be built. An
// IPv6 database supports both IPv4 and IPv6 lookups. The default value is
// "6" for IPv6.
IPVersion int
// Languages is a slice of strings, each of which is a locale code. A given
// record may contain data items that have been localized to some or all of
// these locales. Records should not contain localized data for locales not
// included in this slice.
Languages []string
// RecordSize indicates the number of bits in a record in the search tree.
// The supported values are 24, 28, and 32. A smaller size will result in a
// smaller database, but it will limit the maximum size of the database.
// The default is 28.
RecordSize int
// DisableMetadataPointers prevents the use of pointers in the metadata
// section of the database. This option exists to avoid bugs in reader
// implementations that do not correctly handle metadata pointers. Its
// use should primarily be limited to existing database types.
DisableMetadataPointers bool
// Inserter is the insert function used when calling `Insert`. It defaults
// to `inserter.ReplaceWith`, which replaces any conflicting old value
// entirely with the new.
Inserter inserter.FuncGenerator
// KeyGenerator is used to generate unique keys for the top-level record
// values inserted into the database. This is used to deduplicate data
// in memory as the tree is being created. The KeyGenerator must
// generate a unique key for the value. If two different values have
// the same key, only one will be used.
//
// The default key generator serializes the value and generates a
// SHA-256 hash from it. Although this is relatively safe, it can be
// resource intensive for large data structures.
KeyGenerator KeyGenerator
}
// Tree represents an MaxMind DB search tree.
type Tree struct {
buildEpoch int64
databaseType string
dataMap *dataMap
description map[string]string
disableMetadataPointers bool
ipVersion int
languages []string
recordSize int
root *node
treeDepth int
// This is set when the tree is finalized
nodeCount int
inserterFuncGen inserter.FuncGenerator
}
// New creates a new Tree.
func New(opts Options) (*Tree, error) {
tree := &Tree{
buildEpoch: time.Now().Unix(),
databaseType: opts.DatabaseType,
description: map[string]string{},
disableMetadataPointers: opts.DisableMetadataPointers,
ipVersion: 6,
recordSize: 28,
root: &node{},
inserterFuncGen: inserter.ReplaceWith,
}
if opts.BuildEpoch != 0 {
tree.buildEpoch = opts.BuildEpoch
}
if opts.Description != nil {
tree.description = opts.Description
}
if opts.IPVersion != 0 {
tree.ipVersion = opts.IPVersion
}
if opts.KeyGenerator == nil {
tree.dataMap = newDataMap(newKeyWriter())
} else {
tree.dataMap = newDataMap(opts.KeyGenerator)
}
if opts.Languages != nil {
tree.languages = opts.Languages
}
if opts.RecordSize != 0 {
tree.recordSize = opts.RecordSize
}
if opts.Inserter != nil {
tree.inserterFuncGen = opts.Inserter
}
switch tree.ipVersion {
case 6:
tree.treeDepth = 128
case 4:
tree.treeDepth = 32
default:
return nil, fmt.Errorf("unsupported IPVersion: %d", tree.ipVersion)
}
if tree.ipVersion == 6 && !opts.DisableIPv4Aliasing {
if err := tree.insertIPv4Aliases(); err != nil {
return nil, err
}
}
if !opts.IncludeReservedNetworks {
err := tree.insertReservedNetworks()
if err != nil {
return nil, err
}
}
return tree, nil
}
// Load an existing database into the writer.
func Load(path string, opts Options) (*Tree, error) {
db, err := maxminddb.Open(path)
if err != nil {
return nil, fmt.Errorf("opening %s: %w", path, err)
}
defer db.Close()
metadata := db.Metadata
if opts.DatabaseType == "" {
opts.DatabaseType = metadata.DatabaseType
}
if opts.Description == nil {
opts.Description = metadata.Description
}
if opts.IPVersion == 0 {
//nolint:gosec // no risk. Will be 4 or 6.
opts.IPVersion = int(metadata.IPVersion)
}
if opts.Languages == nil {
opts.Languages = metadata.Languages
}
if opts.RecordSize == 0 {
//nolint:gosec // no risk. Will be 24, 28, or 32.
opts.RecordSize = int(metadata.RecordSize)
}
tree, err := New(opts)
if err != nil {
return nil, err
}
dser := newDeserializer()
var networkOpts []maxminddb.NetworksOption
if opts.IPVersion == 6 && !opts.DisableIPv4Aliasing {
networkOpts = append(networkOpts, maxminddb.SkipAliasedNetworks)
}
networks := db.Networks(networkOpts...)
for networks.Next() {
var network *net.IPNet
dser.clear()
network, err = networks.Network(dser)
if err != nil {
return nil, fmt.Errorf("unmarshaling record for network: %w", err)
}
err = tree.Insert(network, dser.rv)
if err != nil {
return nil, err
}
}
if err := networks.Err(); err != nil {
return nil, fmt.Errorf("iterating over networks: %w", err)
}
return tree, nil
}
// Insert a data value into the tree using the Tree's inserter function
// (defaults to inserter.ReplaceWith).
//
// This is not safe to call from multiple threads.
func (t *Tree) Insert(network *net.IPNet, value mmdbtype.DataType) error {
return t.InsertFunc(network, t.inserterFuncGen(value))
}
// InsertFunc will insert the output of the function passed to it. The argument
// passed to the function is the existing value in the record. The inserter
// function should return the mmdbtype.DataType to be inserted. In both cases,
// a nil value means an empty record.
//
// You must never modify the argument passed to the function as the value may
// be shared with other records. If you want a copy of the mmdbtype.DataType to modify,
// call the Copy method on it, which will make a deep copy. This isn't done
// automatically before calling the function as not all functions will require
// the record to be copied and there is a non-trivial performance impact.
//
// The function will be called multiple times per insert when the network
// has multiple preexisting records associated with it.
//
// This is not safe to call from multiple threads.
func (t *Tree) InsertFunc(
network *net.IPNet,
inserterFunc inserter.Func,
) error {
return t.insert(network, recordTypeData, inserterFunc, nil)
}
func (t *Tree) insert(
network *net.IPNet,
recordType recordType,
inserterFunc inserter.Func,
node *node,
) error {
// We set this to 0 so that the tree must be finalized again.
t.nodeCount = 0
prefixLen, _ := network.Mask.Size()
ip := network.IP
if t.treeDepth == 128 && len(ip) == 4 {
ip = ipV4ToV6(ip)
prefixLen += 96
}
return t.root.insert(
insertRecord{
ip: ip,
prefixLen: prefixLen,
recordType: recordType,
inserter: inserterFunc,
insertedNode: node,
dataMap: t.dataMap,
},
0,
)
}
// InsertRange is the same as Insert, except it will insert all subnets within
// the range of IPs specified by `[start,end]`.
func (t *Tree) InsertRange(
start net.IP,
end net.IP,
value mmdbtype.DataType,
) error {
return t.InsertRangeFunc(start, end, t.inserterFuncGen(value))
}
// InsertRangeFunc is the same as InsertFunc, except it will insert all subnets
// within the range of IPs specified by `[start,end]`.
func (t *Tree) InsertRangeFunc(
start net.IP,
end net.IP,
inserterFunc inserter.Func,
) error {
return t.insertRange(start, end, recordTypeData, inserterFunc, nil)
}
func (t *Tree) insertRange(
start net.IP,
end net.IP,
recordType recordType,
inserterFunc inserter.Func,
node *node,
) error {
startNetIP, ok := netipx.FromStdIP(start)
if !ok {
return errors.New("start IP is invalid")
}
endNetIP, ok := netipx.FromStdIP(end)
if !ok {
return errors.New("end IP is invalid")
}
r := netipx.IPRangeFrom(startNetIP, endNetIP)
if !r.IsValid() {
return errors.New("start & end IPs did not give valid range")
}
subnets := r.Prefixes()
for _, subnet := range subnets {
if err := t.insert(netipx.PrefixIPNet(subnet), recordType, inserterFunc, node); err != nil {
return err
}
}
return nil
}
func (t *Tree) insertStringNetwork(
network string,
recordType recordType,
inserterFunc inserter.Func,
node *node,
) error {
//nolint:forbidigo // code predates netip
_, ipnet, err := net.ParseCIDR(network)
if err != nil {
return fmt.Errorf("parsing network (%s): %w", network, err)
}
return t.insert(ipnet, recordType, inserterFunc, node)
}
var ipv4AliasNetworks = []string{
"::ffff:0:0/96",
"2001::/32",
"2002::/16",
}
func (t *Tree) insertIPv4Aliases() error {
//nolint:forbidigo // code predates netip
_, ipv4Root, err := net.ParseCIDR("::/96")
if err != nil {
return fmt.Errorf("parsing IPv4 root: %w", err)
}
ipv4RootNode := &node{}
// Make ::/96, the IPv4 root, a fixed node.
err = t.insert(ipv4Root, recordTypeFixedNode, nil, ipv4RootNode)
if err != nil {
return err
}
for _, network := range ipv4AliasNetworks {
err := t.insertStringNetwork(network, recordTypeAlias, nil, ipv4RootNode)
if err != nil {
return err
}
}
return nil
}
func (t *Tree) insertReservedNetworks() error {
// the reserved networks are in reserved.go
networks := reservedNetworksIPv4
if t.ipVersion == 6 {
networks = append(networks, reservedNetworksIPv6...)
}
for _, network := range networks {
err := t.insertStringNetwork(network, recordTypeReserved, nil, nil)
if err != nil {
return err
}
}
return nil
}
// Get the value for the given IP address from the tree. If the nil interface
// is returned, that means the tree does not have a value for the IP.
func (t *Tree) Get(ip net.IP) (*net.IPNet, mmdbtype.DataType) {
lookupIP := ip
if t.treeDepth == 128 {
// We use To4() here as Go will parse an IPv4 address to a 16 byte
// IPv6-mapped IPv4 address, e.g.:
//
// len(net.ParseIP("1.1.1.1")) == 16
//
// The parsed address above is equal to ::ffff:1.1.1.1. However,
// the MaxMind DB format has the record for 1.1.1.1 at ::1.1.1.1.
if ipv4 := ip.To4(); ipv4 != nil {
lookupIP = ipV4ToV6(ipv4)
// This simplifies the logic around creating the IPNet. If we didn't
// do this, we would need to specifically adjust the prefix length
// when creating the mask and we would also need to worry about
// what to do if there isn't an IPv4 tree.
ip = ip.To16()
}
}
prefixLen, r := t.root.get(lookupIP, 0)
mask := net.CIDRMask(prefixLen, t.treeDepth)
var value mmdbtype.DataType
if r.recordType == recordTypeData {
value = r.value.data
}
return &net.IPNet{
IP: ip.Mask(mask),
Mask: mask,
}, value
}
// finalize prepares the tree for writing. It is not threadsafe.
func (t *Tree) finalize() {
t.nodeCount = t.root.finalize(0)
}
// WriteTo writes the tree to the provided Writer.
func (t *Tree) WriteTo(w io.Writer) (int64, error) {
if t.nodeCount == 0 {
t.finalize()
}
buf := bufio.NewWriter(w)
//nolint:errcheck // We check the error on flush the only place that matters.
defer buf.Flush()
// We create this here so that we don't have to allocate millions of these. This
// may no longer make sense now that we are using a bufio.Writer anyway, which has
// WriteByte, but we should probably do some testing.
recordBuf := make([]byte, 2*t.recordSize/8)
usePointers := true
dataWriter := newDataWriter(t.dataMap, usePointers)
nodeCount, numBytes, err := t.writeNode(buf, t.root, dataWriter, recordBuf)
if err != nil {
return numBytes, err
}
if nodeCount != t.nodeCount {
// This should only happen if there is a programming bug
// in this library.
return numBytes, fmt.Errorf(
"number of nodes written (%d) doesn't match number expected (%d)",
nodeCount,
t.nodeCount,
)
}
nb, err := buf.Write(dataSectionSeparator)
numBytes += int64(nb)
if err != nil {
return numBytes, fmt.Errorf("writing data section separator: %w", err)
}
nb64, err := dataWriter.WriteTo(buf)
numBytes += nb64
if err != nil {
return numBytes, fmt.Errorf("writing data to buffer: %w", err)
}
nb, err = buf.Write(metadataStartMarker)
numBytes += int64(nb)
if err != nil {
return numBytes, fmt.Errorf("writing metadata start marker: %w", err)
}
metadataWriter := newDataWriter(dataWriter.dataMap, !t.disableMetadataPointers)
_, err = t.writeMetadata(metadataWriter)
if err != nil {
return numBytes, fmt.Errorf("writing metadata: %w", err)
}
nb64, err = metadataWriter.WriteTo(buf)
numBytes += nb64
if err != nil {
return numBytes, fmt.Errorf("writing metadata to buffer: %w", err)
}
err = buf.Flush()
if err != nil {
return numBytes, fmt.Errorf("flushing buffer to writer: %w", err)
}
return numBytes, nil
}
func (t *Tree) writeNode(
w io.Writer,
n *node,
dataWriter *dataWriter,
recordBuf []byte,
) (int, int64, error) {
err := t.copyNode(recordBuf, n, dataWriter)
if err != nil {
return 0, 0, err
}
numBytes := int64(0)
nb, err := w.Write(recordBuf)
numBytes += int64(nb)
nodesWritten := 1
if err != nil {
return nodesWritten, numBytes, fmt.Errorf("writing node: %w", err)
}
for i := 0; i < 2; i++ {
child := n.children[i]
if child.recordType != recordTypeNode && child.recordType != recordTypeFixedNode {
continue
}
addedNodes, addedBytes, err := t.writeNode(
w,
n.children[i].node,
dataWriter,
recordBuf,
)
nodesWritten += addedNodes
numBytes += addedBytes
if err != nil {
return nodesWritten, numBytes, err
}
}
return nodesWritten, numBytes, nil
}
func (t *Tree) recordValue(
r record,
dataWriter *dataWriter,
) (int, error) {
switch r.recordType {
case recordTypeData:
offset, err := dataWriter.maybeWrite(r.value)
return t.nodeCount + len(dataSectionSeparator) + offset, err
case recordTypeEmpty, recordTypeReserved:
return t.nodeCount, nil
default:
return r.node.nodeNum, nil
}
}
func (t *Tree) copyNode(buf []byte, n *node, dataWriter *dataWriter) error {
left, err := t.recordValue(n.children[0], dataWriter)
if err != nil {
return err
}
right, err := t.recordValue(n.children[1], dataWriter)
if err != nil {
return err
}
maxRecord := 1 << t.recordSize
if left >= maxRecord || right >= maxRecord {
return fmt.Errorf(
"exceeded record capacity by attempting to write (%d, %d) to node with %d bit record size; "+
"try increasing RecordSize or reducing the size of the database",
left,
right,
t.recordSize,
)
}
switch t.recordSize {
case 24:
buf[0] = byte((left >> 16) & 0xFF)
buf[1] = byte((left >> 8) & 0xFF)
buf[2] = byte(left & 0xFF)
buf[3] = byte((right >> 16) & 0xFF)
buf[4] = byte((right >> 8) & 0xFF)
buf[5] = byte(right & 0xFF)
case 28:
buf[0] = byte((left >> 16) & 0xFF)
buf[1] = byte((left >> 8) & 0xFF)
buf[2] = byte(left & 0xFF)
buf[3] = byte((((left >> 24) & 0x0F) << 4) | (right >> 24 & 0x0F))
buf[4] = byte((right >> 16) & 0xFF)
buf[5] = byte((right >> 8) & 0xFF)
buf[6] = byte(right & 0xFF)
case 32:
buf[0] = byte((left >> 24) & 0xFF)
buf[1] = byte((left >> 16) & 0xFF)
buf[2] = byte((left >> 8) & 0xFF)
buf[3] = byte(left & 0xFF)
buf[4] = byte((right >> 24) & 0xFF)
buf[5] = byte((right >> 16) & 0xFF)
buf[6] = byte((right >> 8) & 0xFF)
buf[7] = byte(right & 0xFF)
default:
return fmt.Errorf("unsupported record size of %d", t.recordSize)
}
return nil
}
var v4Prefix = net.IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
func ipV4ToV6(ip net.IP) net.IP {
return append(v4Prefix, ip...)
}
func (t *Tree) writeMetadata(dw *dataWriter) (int64, error) {
description := mmdbtype.Map{}
for k, v := range t.description {
description[mmdbtype.String(k)] = mmdbtype.String(v)
}
languages := mmdbtype.Slice{}
for _, v := range t.languages {
languages = append(languages, mmdbtype.String(v))
}
if t.nodeCount > math.MaxUint32 {
return 0, fmt.Errorf("node count of %d exceeds the maximum allowed value", t.nodeCount)
}
metadata := mmdbtype.Map{
"binary_format_major_version": mmdbtype.Uint16(2),
"binary_format_minor_version": mmdbtype.Uint16(0),
// Although it might make sense to change the type on this, there is no use
// case where someone would reasonably pass a negative build epoch.
//nolint:gosec // See above.
"build_epoch": mmdbtype.Uint64(t.buildEpoch),
"database_type": mmdbtype.String(t.databaseType),
"description": description,
//nolint:gosec // no risk. Will be 4 or 6.
"ip_version": mmdbtype.Uint16(t.ipVersion),
"languages": languages,
//nolint:gosec // checked above
"node_count": mmdbtype.Uint32(t.nodeCount),
//nolint:gosec // no risk. Will be 24, 28, or 32.
"record_size": mmdbtype.Uint16(t.recordSize),
}
return metadata.WriteTo(dw)
}