-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipcalc.go
236 lines (216 loc) · 5.43 KB
/
ipcalc.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
// Package ipcalc provides network utilities for performing IP address arithmetic.
//
// Note that next/prev functions are subject to wrapping,
// e.g., NextIP(255.255.255.255) -> 0.0.0.0.
package ipcalc
import (
"net"
"strconv"
"strings"
)
// CopyIP returns a copy of a net.IP address.
func CopyIP(ip net.IP) net.IP {
return append(net.IP(nil), ip...)
}
// IP returns an IP address of the correct byte length.
func IP(ip net.IP) net.IP {
if x := ip.To4(); x != nil {
return CopyIP(x)
}
return CopyIP(ip)
}
// IPVersion returns the IP address version for the given net.IP.
func IPVersion(ip net.IP) int {
if len(IP(ip)) == net.IPv4len {
return 4
}
return 6
}
// IPSize returns the address size in bytes for the given net.IP.
func IPSize(ip net.IP) int {
if IPVersion(ip) == 4 {
return net.IPv4len
}
return net.IPv6len
}
// ParseMask returns a net.IPMask from a string representation.
func ParseMask(mask string) net.IPMask {
return net.IPMask(IP(net.ParseIP(mask)))
}
// ParseIPMask returns a net.IP and net.IPMask from an ip[/mask] string representation.
// The IPMask need not be in CIDR format, if it starts with ~ then it will be inverted.
func ParseIPMask(addr string) (net.IP, net.IPMask, error) {
v := strings.Split(addr, "/")
ip := net.ParseIP(v[0])
if ip == nil {
return nil, nil, &net.ParseError{Type: "IP address", Text: v[0]}
}
var mask net.IPMask
if len(v) > 2 {
return nil, nil, &net.ParseError{Type: "IP/Mask", Text: addr}
} else if len(v) == 2 {
size := IPSize(ip) * 8
wildcard := false
if strings.HasPrefix(v[1], "~") {
wildcard = true
v[1] = v[1][1:]
}
if bits, err := strconv.Atoi(v[1]); err == nil {
mask = net.CIDRMask(bits, size)
} else {
mask = ParseMask(v[1])
}
if wildcard {
mask = Complement(mask)
}
if mask == nil {
return nil, nil, &net.ParseError{Type: "Mask", Text: v[1]}
}
}
return ip, mask, nil
}
// Complement returns the complement of a given net.IPMask, commonly used as a Wildcard Mask.
// e.g., Complement(255.255.254.0) -> 0.0.1.255.
func Complement(mask net.IPMask) net.IPMask {
if mask == nil {
return nil
}
w := make(net.IPMask, len(mask))
for i := 0; i < len(mask); i++ {
w[i] = ^mask[i]
}
return w
}
// NextIP returns the next IP address.
// e.g., NextIP(192.168.0.0) -> 192.168.0.1.
func NextIP(ip net.IP) net.IP {
ip = IP(ip)
for i := IPSize(ip) - 1; i >= 0; i-- {
ip[i]++
if ip[i] != 0x00 {
break
}
}
return ip
}
// PrevIP returns the previous IP address.
// e.g., PrevIP(192.168.0.1) -> 192.168.0.0.
func PrevIP(ip net.IP) net.IP {
ip = IP(ip)
for i := IPSize(ip) - 1; i >= 0; i-- {
ip[i]--
if ip[i] != 0xff {
break
}
}
return ip
}
// Add returns the sum of two net.IP addresses with the given mask.
// e.g., Add(192.168.0.1, 192.168.0.2, 0.0.0.255) -> 192.168.0.3.
func Add(a, b net.IP, mask net.IPMask) net.IP {
a = IP(a)
b = IP(b)
for i := len(mask) - 1; i >= 0; i-- {
prev := a[i]
a[i] += b[i] & mask[i]
if a[i] < prev && i > 0 {
for j := i - 1; j >= 0; j-- {
a[j]++
if a[j] != 0x00 {
break
}
}
}
}
return a
}
// Substract returns the difference of two net.IP addresses with the given mask.
// e.g., Substract(192.168.0.3, 192.168.0.1, 0.0.0.255) -> 192.168.0.2.
func Substract(a, b net.IP, mask net.IPMask) net.IP {
a = IP(a)
b = IP(b)
for i := len(mask) - 1; i >= 0; i-- {
prev := a[i]
a[i] -= b[i] & mask[i]
if a[i] > prev && i > 0 {
for j := i - 1; j >= 0; j-- {
a[j]--
if a[j] != 0xff {
break
}
}
}
}
return a
}
// And returns the bitwise AND of two net.IP addresses.
// e.g., And(192.168.0.255, 192.168.255.128) -> 192.168.0.128.
func And(a, b net.IP) net.IP {
a = IP(a)
b = IP(b)
for i := 0; i < IPSize(b); i++ {
a[i] &= b[i]
}
return a
}
// Or returns the bitwise OR of two net.IP addresses.
// e.g., Or(192.168.0.15, 192.168.10.128) -> 192.168.0.128.
func Or(a, b net.IP) net.IP {
a = IP(a)
b = IP(b)
for i := 0; i < IPSize(b); i++ {
a[i] |= b[i]
}
return a
}
// Xor returns the bitwise XOR of two net.IP addresses.
// e.g., Xor(192.0.2.1, 172.31.128.17) -> 108.31.130.16.
func Xor(a, b net.IP) net.IP {
a = IP(a)
b = IP(b)
for i := 0; i < IPSize(b); i++ {
a[i] ^= b[i]
}
return a
}
// Merge combines two net.IP addresses with the given mask.
// For bit i, if mask[i] is set then b[i] is returned, otherwise a[i] is returned.
// e.g., Merge(192.168.0.1, 172.16.32.100, 0.0.0.255) -> 192.168.0.100.
func Merge(a, b net.IP, mask net.IPMask) net.IP {
a = IP(a)
b = IP(b)
ip := make(net.IP, len(mask))
for i := 0; i < len(mask); i++ {
ip[i] = a[i]&^mask[i] | b[i]&mask[i]
}
return ip
}
// Broadcast returns the broadcast IP address for the given IPNet.
func Broadcast(n net.IPNet) net.IP {
ip := make(net.IP, IPSize(n.IP))
for i := 0; i < IPSize(n.IP); i++ {
ip[i] = n.IP[i] | ^n.Mask[i]
}
return ip
}
// NextSubnet returns the next subnet.
// e.g., NextSubnet(192.168.0.0/24) -> 192.168.1.0/24.
func NextSubnet(n net.IPNet) net.IPNet {
return net.IPNet{
IP: NextIP(Broadcast(n)),
Mask: n.Mask,
}
}
// PrevSubnet returns the previous subnet.
// e.g., PrevSubnet(192.168.1.0/24) -> 192.168.0.0/24.
func PrevSubnet(n net.IPNet) net.IPNet {
return net.IPNet{
IP: PrevIP(n.IP).Mask(n.Mask),
Mask: n.Mask,
}
}
// Contains returns whether the first net.IPNet wholly contains the second one.
// If either net has a non-standard mask then the result is undefined.
func Contains(a, b net.IPNet) bool {
return a.Contains(b.IP) && a.Contains(Broadcast(b))
}