-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc.go
72 lines (56 loc) · 2.22 KB
/
doc.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
/*
Package iprange parses IPv4/IPv6 addresses from strings in IP range
format.
The following IP range formats are supported:
172.18.0.1 fd00::1
172.18.0.0/24 fd00::/64
172.18.0.1-10 fd00::1-a
172.18.0.1-172.18.1.10 fd00::1-fd00::1:a
It takes a set of IP range strings, and returns a list of start-end IP
address pairs, which can then be automatically extended and normalized,
for instance:
v4Ranges, err := iprange.Parse("172.18.0.1", "172.18.0.0/24") // √
v6Ranges, err := iprange.Parse("fd00::1", "fd00::/64") // √
invalid, err := iprange.Parse("Invalid IP range string") // ×
dual, err := iprange.Parse("172.18.0.1", "fd00::/64") // ×
When parsing an invalid IP range string, error errInvalidIPRangeFormat
will be returned, and dual-stack IP ranges are not allowed because this
approach is too complex and confusing. Use the following functions to
assert the errors:
func IsInvalidIPRangeFormat(err error) bool
func IsDualStackIPRanges(err error) bool
Use the interval methods of IPRanges to calculate the union, difference or
intersection of two IPRanges. They do not change the original parameters
(rr and rs), just calculate, and return the results.
func (rr *IPRanges) Union(rs *IPRanges) *IPRanges
func (rr *IPRanges) Diff(rs *IPRanges) *IPRanges
func (rr *IPRanges) Intersect(rs *IPRanges) *IPRanges
However, do not attempt to perform calculations on two IPRanges with
different IP versions, it won't work:
res := v4Ranges.Diff(v6Ranges) // res will be equal to v4Ranges.
The IPRanges can be converted into multiple net.IP (i.e. IP addresses)
or *net.IPNet (i.e. subnets) through their own iterators. Continuously
call the method Next() until nil is returned:
ipIter := ranges.IPIterator()
for {
ip := ipIter.Next()
if ip == nil {
break
}
// Do someting.
}
cidrIter := ranges.CIDRIterator()
for {
cidr := cidrIter.Next()
if cidr == nil {
break
}
// Do someting.
}
Finally, the inspiration for writing this package comes from
CNI plugins: https://github.com/containernetworking/plugins
malfunkt/iprange: https://github.com/malfunkt/iprange
netaddr/netaddr: https://github.com/netaddr/netaddr
both of which are great!
*/
package iprange