-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathip.go
More file actions
93 lines (75 loc) · 1.63 KB
/
ip.go
File metadata and controls
93 lines (75 loc) · 1.63 KB
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
package iprange
import (
"math/big"
"net/netip"
)
// ip wraps netip.Addr in order to expand the method of it.
type ip struct {
addr netip.Addr
}
// version returns IP version.
func (ip ip) version() family {
if ip.addr.Is4() {
return IPv4
}
return IPv6
}
// next returns the IP following i. If there is none, it returns the zero IP.
func (ip ip) next() ip {
ip.addr = ip.addr.Next()
return ip
}
// nextN returns the next nth IP following i. If there is none, it returns the
// zero IP. buf is an optional big.Int used to avoid memory allocations.
func (ip ip) nextN(n, buf *big.Int) ip {
if n == nil || n.Sign() <= 0 {
return ip
}
if n.IsUint64() && n.Uint64() == 1 {
return ip.next()
}
if buf == nil {
buf = new(big.Int)
}
buf.SetBytes(ip.addr.AsSlice())
buf.Add(buf, n)
ip.addr = intToAddr(ip.version(), buf)
return ip
}
// prev returns the IP before i. If there is none, it returns the zero IP.
func (ip ip) prev() ip {
ip.addr = ip.addr.Prev()
return ip
}
// cmp returns an integer comparing two IPs:
//
// -1: ip < other
// 0: ip == other
// +1: ip > other
func (ip ip) cmp(other ip) int {
return ip.addr.Compare(other.addr)
}
// intToAddr converts big.Int to netip.Addr.
func intToAddr(version family, i *big.Int) netip.Addr {
var b [16]byte
if version == IPv4 {
i.FillBytes(b[0:4])
return netip.AddrFrom4([4]byte(b[0:4]))
}
i.FillBytes(b[:])
return netip.AddrFrom16(b)
}
// maxIP returns the numerically higher IP.
func maxIP(x, y ip) ip {
if x.cmp(y) > 0 {
return x
}
return y
}
// minIP returns the numerically lower IP.
func minIP(x, y ip) ip {
if x.cmp(y) < 0 {
return x
}
return y
}