This repository has been archived by the owner on Jun 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathip.go
221 lines (188 loc) · 5.31 KB
/
ip.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
package inetdata
import (
"encoding/binary"
"errors"
"fmt"
"math"
"net"
"os"
"regexp"
"strings"
)
// MatchIPv6 is a regular expression for validating IPv6 addresses
var MatchIPv6 = regexp.MustCompile(`^((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?$`)
// MatchIPv4 is a regular expression for validating IPv4 addresses
var MatchIPv4 = regexp.MustCompile(`^(?:(?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2}))$`)
//IPv4Masks is a precalculated lookup table for IPv4 CIDR
var IPv4Masks = map[uint32]uint32{
1: 32,
2: 31,
4: 30,
8: 29,
16: 28,
32: 27,
64: 26,
128: 25,
256: 24,
512: 23,
1024: 22,
2048: 21,
4096: 20,
8192: 19,
16384: 18,
32768: 17,
65536: 16,
131072: 15,
262144: 14,
524288: 13,
1048576: 12,
2097152: 11,
4194304: 10,
8388608: 9,
16777216: 8,
33554432: 7,
67108864: 6,
134217728: 5,
268435456: 4,
536870912: 3,
1073741824: 2,
2147483648: 1,
}
//IPv4MaskSizes is a precalculated lookup table for IPv4 CIDR mask sizes
var IPv4MaskSizes = []uint32{
2147483648,
1073741824,
536870912,
268435456,
134217728,
67108864,
33554432,
16777216,
8388608,
4194304,
2097152,
1048576,
524288,
262144,
131072,
65536,
32768,
16384,
8192,
4096,
2048,
1024,
512,
256,
128,
64,
32,
16,
8,
4,
2,
1,
}
// IPv42UInt converts IPv4 addresses to unsigned integers
func IPv42UInt(ips string) (uint32, error) {
ip := net.ParseIP(ips)
if ip == nil {
return 0, errors.New("Invalid IPv4 address")
}
ip = ip.To4()
return binary.BigEndian.Uint32(ip), nil
}
// UInt2IPv4 converts unsigned integers to IPv4 addresses
func UInt2IPv4(ipi uint32) string {
ipb := make([]byte, 4)
binary.BigEndian.PutUint32(ipb, ipi)
ip := net.IP(ipb)
return ip.String()
}
// IPv4Range2CIDRs converts a start and stop IPv4 range to a list of CIDRs
func IPv4Range2CIDRs(sIP string, eIP string) ([]string, error) {
sI, sE := IPv42UInt(sIP)
if sE != nil {
return []string{}, sE
}
eI, eE := IPv42UInt(eIP)
if eE != nil {
return []string{}, eE
}
if sI > eI {
return []string{}, errors.New("Start address is bigger than end address")
}
return IPv4UIntRange2CIDRs(sI, eI), nil
}
// IPv4UIntRange2CIDRs converts a range of insigned integers into IPv4 CIDRs
func IPv4UIntRange2CIDRs(sI uint32, eI uint32) []string {
cidrs := []string{}
// Ranges are inclusive
size := eI - sI + 1
if size == 0 {
return cidrs
}
for i := range IPv4MaskSizes {
maskSize := IPv4MaskSizes[i]
if maskSize > size {
continue
}
// Exact match of the block size
if maskSize == size {
cidrs = append(cidrs, fmt.Sprintf("%s/%d", UInt2IPv4(sI), IPv4Masks[maskSize]))
break
}
// Chop off the biggest block that fits
cidrs = append(cidrs, fmt.Sprintf("%s/%d", UInt2IPv4(sI), IPv4Masks[maskSize]))
sI = sI + maskSize
// Look for additional blocks
newCidrs := IPv4UIntRange2CIDRs(sI, eI)
// Merge those blocks into out results
for x := range newCidrs {
cidrs = append(cidrs, newCidrs[x])
}
break
}
return cidrs
}
//AddressesFromCIDR parses a CIDR and writes individual IPs to a channel
func AddressesFromCIDR(cidr string, o chan<- string) {
if len(cidr) == 0 {
return
}
// We may receive bare IP addresses, not CIDRs sometimes
if !strings.Contains(cidr, "/") {
if strings.Contains(cidr, ":") {
cidr = cidr + "/128"
} else {
cidr = cidr + "/32"
}
}
// Parse CIDR into base address + mask
ip, net, err := net.ParseCIDR(cidr)
if err != nil {
fmt.Fprintf(os.Stderr, "Invalid CIDR %s: %s\n", cidr, err.Error())
return
}
// Verify IPv4 for now
ip4 := net.IP.To4()
if ip4 == nil {
fmt.Fprintf(os.Stderr, "Invalid IPv4 CIDR %s\n", cidr)
return
}
netBase, err := IPv42UInt(net.IP.String())
if err != nil {
fmt.Fprintf(os.Stderr, "Invalid IPv4 Address %s: %s\n", ip.String(), err.Error())
return
}
maskOnes, maskTotal := net.Mask.Size()
// Does not work for IPv6 due to cast to uint32
netSize := uint32(math.Pow(2, float64(maskTotal-maskOnes)))
curBase := netBase
endBase := netBase + netSize
curAddr := curBase
for curAddr = curBase; curAddr < endBase; curAddr++ {
o <- UInt2IPv4(curAddr)
}
return
}