-
Notifications
You must be signed in to change notification settings - Fork 0
/
iprange.go
414 lines (358 loc) · 9.87 KB
/
iprange.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
// Package iprange is an extension to net/netip.
//
// An additional type IPRange is defined and the most useful methods for it.
//
// For more advanced functionality IPRange implements the interval.Interface for fast lookups.
//
// see also: https://github.com/gaissmai/interval
package iprange
import (
"errors"
"fmt"
"net/netip"
"sort"
"strings"
"github.com/gaissmai/extnetip"
)
// IPRange represents an inclusive range of IP addresses from the same address family.
//
// 10.0.0.3-10.0.17.134 // range
// 2001:db8::1-2001:db8::f6 // range
// 192.168.0.1/24 // Prefix aka CIDR
// ::1/128 // Prefix aka CIDR
//
// Not all IP address ranges in the wild are CIDRs, very often you have to deal
// with ranges not representable as a prefix.
//
// This library handels IP ranges and CIDRs transparently.
type IPRange struct {
first netip.Addr
last netip.Addr
}
var (
zeroValue IPRange
invalidStr = "invalid IPRange"
)
// FromString parses the input string and returns an IPRange.
//
// Returns an error on invalid input.
//
// Valid strings are of the form:
//
// 192.168.2.3-192.168.7.255
// 2001:db8::1-2001:db8::ff00:35
//
// 2001:db8:dead::/38
// 10.0.0.0/8
//
// 4.4.4.4
// ::0
//
// Single IP addresses as input are converted to /32 or /128 ranges.
//
// The hard part is done by netip.ParseAddr and netip.ParsePrefix from the stdlib.
func FromString(s string) (IPRange, error) {
if s == "" {
return zeroValue, errors.New("empty string")
}
// addr/bits
i := strings.IndexByte(s, '/')
if i >= 0 {
p, err := netip.ParsePrefix(s)
if err != nil {
return zeroValue, err
}
return FromPrefix(p)
}
// addr-addr
ip, ip2, found := strings.Cut(s, "-")
if found {
first, err := netip.ParseAddr(ip)
if err != nil {
return zeroValue, err
}
last, err := netip.ParseAddr(ip2)
if err != nil {
return zeroValue, err
}
return FromAddrs(first, last)
}
// an addr, or maybe just rubbish
addr, err := netip.ParseAddr(s)
if err != nil {
return zeroValue, err
}
return FromAddrs(addr, addr)
}
// FromPrefix returns an IPRange from the standard library's netip.Prefix type.
func FromPrefix(p netip.Prefix) (IPRange, error) {
if !p.IsValid() {
return zeroValue, errors.New("netip.Prefix is invalid")
}
first, last := extnetip.Range(p)
return IPRange{first, last}, nil
}
// FromAddrs returns an IPRange from the provided IP addresses.
//
// IP addresses with zones are not allowed.
func FromAddrs(first, last netip.Addr) (IPRange, error) {
if !((first.Is4() && last.Is4()) || (first.Is6() && last.Is6())) {
return zeroValue, errors.New("invalid or different IP versions")
}
if first.Zone() != "" || last.Zone() != "" {
return zeroValue, errors.New("ip address MUST NOT have a zone")
}
if last.Less(first) {
return zeroValue, errors.New("last address is less than first address")
}
return IPRange{first, last}, nil
}
// IsValid reports whether r is a valid IPRange.
func (r IPRange) IsValid() bool {
return r != zeroValue
}
// Addrs returns the first and last IP address of the IPRange.
func (r IPRange) Addrs() (first, last netip.Addr) {
return r.first, r.last
}
// Prefix returns r as a netip.Prefix, if it can be presented exactly as such.
// If r is not valid or is not exactly equal to one prefix, ok is false.
func (r IPRange) Prefix() (prefix netip.Prefix, ok bool) {
return extnetip.Prefix(r.first, r.last)
}
// Prefixes returns the slice of netip.Prefix entries that covers r.
//
// If r is invalid Prefixes returns nil.
//
// Prefixes necessarily allocates. See PrefixesAppend for a version that
// uses memory you provide.
func (r IPRange) Prefixes() []netip.Prefix {
return extnetip.Prefixes(r.first, r.last)
}
// PrefixesAppend is the append version of Prefixes.
//
// It appends to dst the netip.Prefix entries that covers r.
func (r IPRange) PrefixesAppend(dst []netip.Prefix) []netip.Prefix {
return extnetip.PrefixesAppend(dst, r.first, r.last)
}
// String returns the string form of the IPRange.
//
// "127.0.0.1-127.0.0.19"
// "2001:db8::/32"
func (r IPRange) String() string {
if r == zeroValue {
return invalidStr
}
p, ok := r.Prefix()
if !ok {
return fmt.Sprintf("%s-%s", r.first, r.last)
}
return p.String()
}
// #########################################################################################
// more complex functions
// Merge adjacent and overlapping IPRanges.
//
// Skip dups and subsets and invalid ranges, returns the remaining IPRanges sorted.
func Merge(in []IPRange) (out []IPRange) {
if len(in) == 0 {
return nil
}
// copy and sort
rs := make([]IPRange, len(in))
copy(rs, in)
sortRanges(rs)
for _, r := range rs {
if r == zeroValue {
continue
}
// starting point
if out == nil {
out = append(out, r)
continue
}
// take ptr to last out item
topic := &out[len(out)-1]
// compare topic with this range
// case order is VERY important!
switch {
case topic.last.Next() == r.first:
// ranges are adjacent [f...l][f...l]
topic.last = r.last
case topic.isDisjunctLeft(r):
// disjoint [f...l] [f...l]
out = append(out, r)
case topic.covers(r):
// no-op
continue
case topic.last.Less(r.last):
// partial overlap [f......l]
// [f....l]
topic.last = r.last
default:
panic("unreachable")
}
}
return
}
// Remove the slice of IPRanges from r, returns the remaining IPRanges.
func (r IPRange) Remove(in []IPRange) (out []IPRange) {
if r == zeroValue {
return nil
}
// copy, sort, merge
merged := Merge(in)
// fast exit?
if len(merged) == 0 {
return []IPRange{r}
}
// r is disjunct with all merged ranges
if r.isDisjunctLeft(merged[0]) {
return []IPRange{r}
}
if r.isDisjunctRight(merged[len(merged)-1]) {
return []IPRange{r}
}
for _, m := range merged {
// case order is VERY important!
switch {
case m.isDisjunct(r):
// no-op
continue
case m.covers(r):
// m covers r, m masks the rest
return out
case m.first.Compare(r.first) <= 0:
// left overlap, move cursor
r.first = m.last.Next()
case m.first.Compare(r.first) > 0:
// right overlap, save [r.first, m.first-1)
out = append(out, IPRange{r.first, m.first.Prev()})
// new r first
r.first = m.last.Next()
default:
panic("unreachable")
}
// test for overflow from last.Next()
if !r.first.IsValid() {
return out
}
// test if cursor moved behind r.last
if r.last.Less(r.first) {
return out
}
}
// save the rest
out = append(out, r)
return out
}
// Compare returns four integers comparing the four points of the two IP ranges.
// Implements the cmp function in the [package interval] for fast lookups.
//
// [package interval]: https://github.com/gaissmai/interval
func Compare(a, b IPRange) (ll int, rr int, lr int, rl int) {
// l=left a.k.a first point from range a.k.a. interval
// r=right a.k.a last point from range a.k.a. interval
ll = a.first.Compare(b.first)
rr = a.last.Compare(b.last)
lr = a.first.Compare(b.last)
rl = a.last.Compare(b.first)
return
}
// CompareLower
//
// Deprecated: see [Compare]
func (r IPRange) CompareLower(r2 IPRange) int {
return r.first.Compare(r2.first)
}
// CompareUpper
//
// Deprecated: see [Compare]
func (r IPRange) CompareUpper(r2 IPRange) int {
return r.last.Compare(r2.last)
}
// #####################################################################################
// MARSHALING
// MarshalText implements the encoding.TextMarshaler interface,
// The encoding is the same as returned by String, with one exception:
// If r is the zero IPRange, the encoding is the empty string.
func (r IPRange) MarshalText() ([]byte, error) {
if !r.first.IsValid() {
return []byte(""), nil
}
return []byte(r.String()), nil
}
// UnmarshalText implements the encoding.TextUnmarshaler interface.
// The IPRange is expected in a form accepted by FromString.
//
// If text is empty, UnmarshalText sets *r to the zero IPRange and
// returns no error.
func (r *IPRange) UnmarshalText(text []byte) error {
if *r != zeroValue {
return errors.New("refusing to Unmarshal into non-zero IPRange")
}
if len(text) == 0 {
return nil
}
var err error
*r, err = FromString(string(text))
return err
}
// MarshalBinary implements the encoding.BinaryMarshaler interface.
func (r IPRange) MarshalBinary() ([]byte, error) {
return append(r.first.AsSlice(), r.last.AsSlice()...), nil
}
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
// It expects data in the form generated by MarshalBinary.
func (r *IPRange) UnmarshalBinary(data []byte) error {
if *r != zeroValue {
return errors.New("refusing to Unmarshal into non-zero IPRange")
}
n := len(data)
if n == 0 {
return nil
}
// first,last: IPv4: 2x4=8 bytes, IPv6: 2x16=32 bytes
if n != 8 && n != 32 {
return errors.New("unexpected slice size")
}
first, _ := netip.AddrFromSlice(data[:n/2])
last, _ := netip.AddrFromSlice(data[n/2:])
if last.Less(first) {
return errors.New("last address is less than first address")
}
(*r).first = first
(*r).last = last
return nil
}
// ##################################################################
// mothers little helpers
func (a IPRange) isDisjunctLeft(b IPRange) bool {
return a.last.Less(b.first)
}
func (a IPRange) isDisjunctRight(b IPRange) bool {
return b.last.Less(a.first)
}
func (a IPRange) isDisjunct(b IPRange) bool {
return a.last.Less(b.first) || b.last.Less(a.first)
}
func (a IPRange) covers(b IPRange) bool {
return a.first.Compare(b.first) <= 0 && a.last.Compare(b.last) >= 0
}
// cmpRange, by first points, supersets to the left as tiebreaker
func cmpRange(a, b IPRange) int {
if a == b {
return 0
}
// cmp first
if cmp := a.first.Compare(b.first); cmp != 0 {
return cmp
}
// first is equal, sort supersets to the left
return -(a.last.Compare(b.last))
}
// sortRanges in place in default sort order,
// first points ascending, supersets to the left.
func sortRanges(rs []IPRange) {
sort.Slice(rs, func(i, j int) bool { return cmpRange(rs[i], rs[j]) < 0 })
}