forked from client9/ipcat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipset.go
423 lines (366 loc) · 9.89 KB
/
ipset.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
package ipcat
import (
"bytes"
"encoding/csv"
"fmt"
"io"
"math/big"
"net"
"sort"
"strings"
)
// IPParse converts a string IP address to a byte slice, or nil on error.
func IPParse(dots string) []byte {
return net.ParseIP(dots)
}
// IPString converts a byte slice representing an IP address to a string.
func IPString(ip []byte) string {
return net.IP(ip).String()
}
// IPIsAdjacent returns true if the first IP + 1 equals the second.
func IPIsAdjacent(bytes, bytesinc []byte) bool {
if len(bytes) != len(bytesinc) {
return false
}
// Iterate backwards
var carry byte = 1
for i := len(bytes) - 1; i >= 0; i-- {
inc := bytes[i] + carry
if inc != bytesinc[i] {
return false
}
if carry == 1 && bytes[i] == 0xff {
carry = 1
} else {
carry = 0
}
}
return true
}
// CIDR2Range converts a CIDR to a dotted IP address pair, or empty strings and error
//
// Generic.. does not care if ipv4 or ipv6 (for sure this time)
func CIDR2Range(c string) (string, string, error) {
// Parse CIDR notation
addr, network, err := net.ParseCIDR(c)
if err != nil {
return "", "", err
}
// Create new bounds addresses
left := make(net.IP, 16)
right := make(net.IP, 16)
// Pad mask to 16 bytes with 1 bits
mask := make([]byte, 16)
for i := 0; i < 16; i++ {
mask[i] = 0xff
}
copy(mask[16-len(network.Mask):], network.Mask)
// Mask address for left and right bounds
for i := range left {
left[i] = addr[i] & mask[i]
right[i] = addr[i] | ^mask[i]
}
return left.String(), right.String(), nil
}
// Interval is a closed interval [a,b] of an IP range
type Interval struct {
Left [16]byte
Right [16]byte
Name string
URL string
}
// Contains returns true if the IP address is found within the interval.
func (interval *Interval) Contains(ip []byte) bool {
return bytes.Compare(ip, interval.Left[:]) >= 0 && bytes.Compare(ip, interval.Right[:]) <= 0
}
// Size returns the number of IP addresses that fit in the range
func (interval *Interval) Size() *big.Int {
size := big.NewInt(1)
size.Add(size, new(big.Int).SetBytes(interval.Right[:]))
size.Sub(size, new(big.Int).SetBytes(interval.Left[:]))
return size
}
func (interval Interval) String() string {
return IPString(interval.Left[:]) + " to " + IPString(interval.Right[:]) + " (" +
interval.Name + " <" + interval.URL + ">)"
}
type intervallist []Interval
// Len satisfies the sort.Sortable interface
func (ipset intervallist) Len() int {
return len(ipset)
}
// Less satisfies the sort.Sortable interface
func (ipset intervallist) Less(i, j int) bool {
return bytes.Compare(ipset[i].Left[:], ipset[j].Left[:]) < 0
}
// Swap satisfies the sort.Sortable interface
func (ipset intervallist) Swap(i, j int) {
ipset[i], ipset[j] = ipset[j], ipset[i]
}
// IntervalSet is a mapping of an IP range (the closed interval)
// to additional data
type IntervalSet struct {
btree intervallist
sorted bool
}
// NewIntervalSet creates a new set with a capacity
func NewIntervalSet(capacity int) *IntervalSet {
return &IntervalSet{
btree: make([]Interval, 0, capacity),
}
}
// ImportCSV imports data from a CSV file
func (ipset *IntervalSet) ImportCSV(in io.Reader) error {
ipset.btree = nil
ipset.sorted = false
line := 0
r := csv.NewReader(in)
for {
line++
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
return err
}
if len(record) != 4 {
return fmt.Errorf("line %d: expected 4 records but got %d %v", line, len(record), record)
}
if err = ipset.AddRange(record[0], record[1], record[2], record[3]); err != nil {
return err
}
}
return ipset.sort()
}
// ExportCSV export data to a CSV file
func (ipset *IntervalSet) ExportCSV(in io.Writer) error {
if !ipset.sorted {
err := ipset.sort()
if err != nil {
return err
}
}
w := csv.NewWriter(in)
for _, val := range ipset.btree {
rec := []string{IPString(val.Left[:]), IPString(val.Right[:]), val.Name, val.URL}
if err := w.Write(rec); err != nil {
return err
}
}
// Write any buffered data to the underlying writer (standard output).
w.Flush()
if err := w.Error(); err != nil {
return err
}
return nil
}
func (ipset *IntervalSet) sort() error {
if ipset.sorted {
return nil
}
sort.Sort(ipset.btree)
last := Interval{}
// check validity -- probably worth ripping out
for pos, val := range ipset.btree {
if bytes.Compare(val.Left[:], val.Right[:]) > 0 {
return fmt.Errorf("left %s > right %s at pos %d",
IPString(val.Left[:]), IPString(val.Right[:]), pos)
}
if pos > 0 {
if bytes.Compare(val.Left[:], last.Right[:]) <= 0 || bytes.Compare(val.Right[:], last.Right[:]) <= 0 {
return fmt.Errorf("Overlapping regions %s and %s", last, val)
}
}
last = val
}
ipset.sorted = true
// now merge adjacent items
merged := ipset.btree[:0]
for index, interval := range ipset.btree {
if index == 0 {
merged = append(merged, interval)
continue
}
last := &merged[len(merged)-1]
if interval.Name == last.Name && IPIsAdjacent(last.Right[:], interval.Left[:]) {
last.Right = interval.Right
continue
}
merged = append(merged, interval)
}
ipset.btree = merged
return nil
}
// AddCIDR adds an entry based on a CIDR range
func (ipset *IntervalSet) AddCIDR(cidr, name, url string) error {
dotsleft, dotsright, err := CIDR2Range(cidr)
if err != nil {
return err
}
return ipset.AddRange(dotsleft, dotsright, name, url)
}
// AddRange adds an entry based on an IP range
func (ipset *IntervalSet) AddRange(dotsleft, dotsright, name, url string) error {
left := net.ParseIP(dotsleft)
if left == nil {
return fmt.Errorf("Unable to convert %s", dotsleft)
}
right := net.ParseIP(dotsright)
if right == nil {
return fmt.Errorf("Unable to convert %s", dotsright)
}
if bytes.Compare(left, right) > 0 {
return fmt.Errorf("%s > %s", dotsleft, dotsright)
}
ipset.sorted = false
ipset.btree = append(ipset.btree,
Interval{
Name: name,
URL: url,
},
)
index := len(ipset.btree) - 1
copy(ipset.btree[index].Left[:], left)
copy(ipset.btree[index].Right[:], right)
return nil
}
// DeleteByName deletes all entries with the given name
func (ipset *IntervalSet) DeleteByName(name string) {
newlist := ipset.btree[:0]
for _, entry := range ipset.btree {
if entry.Name != name {
newlist = append(newlist, entry)
}
}
ipset.btree = newlist
}
// Len returns the number of elements in the set
func (ipset *IntervalSet) Len() int {
return ipset.btree.Len()
}
// Contains returns the internal record if the IP address is in some
// interval else nil or error. It returns a pointer to the internal
// record, so be careful.
func (ipset *IntervalSet) Contains(dots string) (*Interval, error) {
if err := ipset.sort(); err != nil {
return nil, err
}
ip := net.ParseIP(dots)
if ip == nil {
return nil, fmt.Errorf("Invalid input: %q", dots)
}
len := ipset.Len()
index := sort.Search(len, func(i int) bool {
left := ipset.btree[i].Left[:]
cmp := bytes.Compare(left, ip)
return cmp >= 0
})
if index < len {
// lots of cases in the lookup here.
// if exactly equals, then compare with [i]
interval := &ipset.btree[index]
if interval.Contains(ip) {
return interval, nil
}
}
// ok then it's the record before
if index > 0 {
interval := &ipset.btree[index-1]
if interval.Contains(ip) {
return interval, nil
}
}
return nil, nil
}
// NameSize is a tuple mapping name with a size
type NameSize struct {
Name string
Size *big.Int
}
// NameSizeList is a list of NameSize
type NameSizeList []NameSize
type lessFunc func(p1, p2 *NameSize) bool
// multiSorter implements the Sort interface, sorting the NameSizes within.
// from https://golang.org/pkg/sort/#example__sortMultiKeys
type multiSorter struct {
nameSizes []NameSize
less []lessFunc
}
// Sort sorts the argument slice according to the less functions passed to orderedBy.
func (ms *multiSorter) Sort(nameSizes []NameSize) {
ms.nameSizes = nameSizes
sort.Sort(ms)
}
// orderedBy returns a Sorter that sorts using the less functions, in order.
// Call its Sort method to sort the data.
func orderedBy(less ...lessFunc) *multiSorter {
return &multiSorter{
less: less,
}
}
// Len is part of sort.Interface.
func (ms *multiSorter) Len() int {
return len(ms.nameSizes)
}
// Swap is part of sort.Interface.
func (ms *multiSorter) Swap(i, j int) {
ms.nameSizes[i], ms.nameSizes[j] = ms.nameSizes[j], ms.nameSizes[i]
}
// Less is part of sort.Interface. It is implemented by looping along the
// less functions until it finds a comparison that is either Less or
// !Less. Note that it can call the less functions twice per call. We
// could change the functions to return -1, 0, 1 and reduce the
// number of calls for greater efficiency: an exercise for the reader.
func (ms *multiSorter) Less(i, j int) bool {
p, q := &ms.nameSizes[i], &ms.nameSizes[j]
// Try all but the last comparison.
var k int
for k = 0; k < len(ms.less)-1; k++ {
less := ms.less[k]
switch {
case less(p, q):
// p < q, so we have a decision.
return true
case less(q, p):
// p > q, so we have a decision.
return false
}
// p == q; try the next comparison.
}
// All comparisons to here said "equal", so just return whatever
// the final comparison reports.
return ms.less[k](p, q)
}
// RankBySize returns a list ISP and how many IPs they have
// From this it's easy to compute:
//
// * Lastest providers
//
// * Number of providers
//
// * Total number IP address
//
func (ipset IntervalSet) RankBySize() NameSizeList {
counts := make(map[string]*big.Int, ipset.Len())
for _, val := range ipset.btree {
count, ok := counts[val.Name]
if !ok {
count = big.NewInt(0)
counts[val.Name] = count
}
count.Add(count, val.Size())
}
rank := make(NameSizeList, 0, len(counts))
for k, v := range counts {
rank = append(rank, NameSize{k, v})
}
size := func(left, right *NameSize) bool {
return left.Size.Cmp(right.Size) > 0
}
name := func(l1, l2 *NameSize) bool {
return strings.ToLower(l1.Name) < strings.ToLower(l2.Name)
}
orderedBy(size, name).Sort(rank)
return rank
}