-
Notifications
You must be signed in to change notification settings - Fork 4
/
calc.go
58 lines (52 loc) · 1.21 KB
/
calc.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
package netutil
import (
"math/big"
"net"
)
// IPAdd adds `val` to `ip`.
// If `ip` is IPv4 address, the value returned is IPv4, or nil when over/underflowed.
// If `ip` is IPv6 address, the value returned is IPv6, or nil when over/underflowed.
func IPAdd(ip net.IP, val int64) net.IP {
i := big.NewInt(val)
ipv4 := ip.To4()
if ipv4 != nil {
b := new(big.Int)
b.SetBytes([]byte(ipv4))
if i.Add(i, b).Sign() < 0 {
return nil
}
res := i.Bytes()
if len(res) < 4 {
nres := make([]byte, 4)
copy(nres[4-len(res):], res)
res = nres
}
return net.IP(res).To4()
}
b := new(big.Int)
b.SetBytes([]byte(ip))
if i.Add(i, b).Sign() < 0 {
return nil
}
res := i.Bytes()
if len(res) < 16 {
nres := make([]byte, 16)
copy(nres[16-len(res):], res)
res = nres
}
return net.IP(res).To16()
}
// IPDiff calculates the numeric difference between two IP addresses.
// Intuitively, the calculation is done as `ip2 - ip1`.
// `ip1` and `ip2` must be the same IP version (4 or 6).
func IPDiff(ip1, ip2 net.IP) int64 {
if v4 := ip1.To4(); v4 != nil {
ip1 = v4
ip2 = ip2.To4()
}
b1 := new(big.Int)
b2 := new(big.Int)
b1.SetBytes([]byte(ip1))
b2.SetBytes([]byte(ip2))
return b2.Sub(b2, b1).Int64()
}