-
Notifications
You must be signed in to change notification settings - Fork 0
/
packed.go
142 lines (126 loc) · 3.08 KB
/
packed.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
package iplist
import (
"encoding/binary"
"fmt"
"io"
"net"
"os"
"github.com/edsrzf/mmap-go"
)
// The packed format is an 8 byte integer of the number of ranges. Then 20
// bytes per range, consisting of 4 byte packed IP being the lower bound IP of
// the range, then 4 bytes of the upper, inclusive bound, 8 bytes for the
// offset of the description from the end of the packed ranges, and 4 bytes
// for the length of the description. After these packed ranges, are the
// concatenated descriptions.
const (
packedRangesOffset = 8
packedRangeLen = 44
)
func (ipl *IPList) WritePacked(w io.Writer) (err error) {
descOffsets := make(map[string]int64, len(ipl.ranges))
descs := make([]string, 0, len(ipl.ranges))
var nextOffset int64
// This is a little monadic, no?
write := func(b []byte, expectedLen int) {
if err != nil {
return
}
var n int
n, err = w.Write(b)
if err != nil {
return
}
if n != expectedLen {
panic(n)
}
}
var b [8]byte
binary.LittleEndian.PutUint64(b[:], uint64(len(ipl.ranges)))
write(b[:], 8)
for _, r := range ipl.ranges {
write(r.First.To16(), 16)
write(r.Last.To16(), 16)
descOff, ok := descOffsets[r.Description]
if !ok {
descOff = nextOffset
descOffsets[r.Description] = descOff
descs = append(descs, r.Description)
nextOffset += int64(len(r.Description))
}
binary.LittleEndian.PutUint64(b[:], uint64(descOff))
write(b[:], 8)
binary.LittleEndian.PutUint32(b[:], uint32(len(r.Description)))
write(b[:4], 4)
}
for _, d := range descs {
write([]byte(d), len(d))
}
return
}
func NewFromPacked(b []byte) PackedIPList {
ret := PackedIPList(b)
minLen := packedRangesOffset + ret.len()*packedRangeLen
if len(b) < minLen {
panic(fmt.Sprintf("packed len %d < %d", len(b), minLen))
}
return ret
}
type PackedIPList []byte
var _ Ranger = PackedIPList{}
func (pil PackedIPList) len() int {
return int(binary.LittleEndian.Uint64(pil[:8]))
}
func (pil PackedIPList) NumRanges() int {
return pil.len()
}
func (pil PackedIPList) getFirst(i int) net.IP {
off := packedRangesOffset + packedRangeLen*i
return net.IP(pil[off : off+16])
}
func (pil PackedIPList) getRange(i int) (ret Range) {
rOff := packedRangesOffset + packedRangeLen*i
last := pil[rOff+16 : rOff+32]
descOff := int(binary.LittleEndian.Uint64(pil[rOff+32:]))
descLen := int(binary.LittleEndian.Uint32(pil[rOff+40:]))
descOff += packedRangesOffset + packedRangeLen*pil.len()
ret = Range{
pil.getFirst(i),
net.IP(last),
string(pil[descOff : descOff+descLen]),
}
return
}
func (pil PackedIPList) Lookup(ip net.IP) (r Range, ok bool) {
ip16 := ip.To16()
if ip16 == nil {
panic(ip)
}
return lookup(pil.getFirst, pil.getRange, pil.len(), ip16)
}
type closerFunc func() error
func (me closerFunc) Close() error {
return me()
}
func MMapPackedFile(filename string) (
ret interface {
Ranger
io.Closer
},
err error,
) {
f, err := os.Open(filename)
if err != nil {
return
}
defer f.Close()
mm, err := mmap.Map(f, mmap.RDONLY, 0)
if err != nil {
return
}
ret = struct {
Ranger
io.Closer
}{NewFromPacked(mm), closerFunc(mm.Unmap)}
return
}