-
Notifications
You must be signed in to change notification settings - Fork 273
/
utils.go
270 lines (244 loc) · 6.06 KB
/
utils.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
* Copyright (c) 2024 Yunshan Networks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package datatype
import (
"encoding/binary"
"math"
"net"
"sort"
"strconv"
"strings"
"github.com/deepflowio/deepflow/server/libs/utils"
)
var (
MAX_NETMASK = utils.MaskLenToNetmask(MAX_MASK_LEN)
_ip6Max = net.ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")
)
func ipv4RangeConvert(startIp, endIp net.IP) []net.IPNet {
start := utils.IpToUint32(startIp)
end := utils.IpToUint32(endIp)
var ips []net.IPNet
for start <= end {
maskLen := getFirstMask(start, end)
ip := utils.IpFromUint32(start)
ipMask := net.CIDRMask(int(maskLen), MAX_MASK_LEN)
ips = append(ips, net.IPNet{IP: ip, Mask: ipMask})
lastIp := getLastIp(start, maskLen)
if lastIp == MAX_NETMASK {
break
}
start += 1 << uint32(MAX_MASK_LEN-maskLen)
}
return ips
}
type ip6Field struct {
ip [2]uint64
}
func newIp6Field(ip net.IP) *ip6Field {
field := &ip6Field{}
field.ip[0] = binary.BigEndian.Uint64(ip)
field.ip[1] = binary.BigEndian.Uint64(ip[8:])
return field
}
func (f *ip6Field) lessOrEqual(m *ip6Field) bool {
if f.ip[0] > m.ip[0] {
return false
} else if f.ip[0] < m.ip[0] {
return true
}
if f.ip[1] > m.ip[1] {
return false
}
return true
}
func (f *ip6Field) add(n uint64) bool {
if n == 0 {
return false
}
if diff := math.MaxUint64 - f.ip[1]; diff < n {
if f.ip[0] == math.MaxUint64 {
f.ip[1] = math.MaxUint64
return true
} else {
f.ip[0] += 1
f.ip[1] = n - diff - 1
}
} else {
f.ip[1] += n
}
return false
}
func (f *ip6Field) sub(n uint64) {
if n == 0 {
return
}
if n > f.ip[1] {
f.ip[0] -= 1
f.ip[1] = math.MaxUint64 - n + f.ip[1] + 1
} else {
f.ip[1] -= n
}
}
func (f *ip6Field) addbyBitOffsetAndSub(offset uint64, count uint64) {
overflow := false
if offset < 64 {
overflow = f.add(1 << offset)
} else if offset < 128 {
offset -= 64
f.ip[0] += 1 << offset
} else {
f.ip[1] = math.MaxUint64
f.ip[0] = math.MaxUint64
}
if !overflow {
f.sub(count)
}
}
func (f *ip6Field) getMask() uint64 {
i := 128
for i = 128; i > 64; i-- {
position := 128 - i
if f.ip[1]&(1<<uint64(position)) != 0 {
return uint64(i)
}
}
for i = 64; i > 0; i-- {
position := 64 - i
if f.ip[0]&(1<<uint64(position)) != 0 {
return uint64(i)
}
}
return uint64(0)
}
func (f *ip6Field) makeIp() net.IP {
ip := make(net.IP, 16)
binary.BigEndian.PutUint64(ip, f.ip[0])
binary.BigEndian.PutUint64(ip[8:], f.ip[1])
return ip
}
func ipv6RangeConvert(startIp, endIp net.IP) []net.IPNet {
start := newIp6Field(startIp)
end := newIp6Field(endIp)
ips := make([]net.IPNet, 0, 4)
for start.lessOrEqual(end) && !start.makeIp().Equal(_ip6Max) {
mask := start.getMask()
for ; mask < 128; mask++ {
tmp := *start
tmp.addbyBitOffsetAndSub(128-mask, 1)
if tmp.lessOrEqual(end) {
break
}
}
ipMask := net.CIDRMask(int(mask), 128)
ip := start.makeIp()
ips = append(ips, net.IPNet{IP: ip, Mask: ipMask})
start.addbyBitOffsetAndSub(128-mask, 0)
}
return ips
}
func IpRangeConvert2CIDR(startIp, endIp net.IP) []net.IPNet {
if len(startIp) == net.IPv4len && len(endIp) == net.IPv4len {
return ipv4RangeConvert(startIp, endIp)
} else if len(startIp) == net.IPv6len && len(endIp) == net.IPv6len {
return ipv6RangeConvert(startIp, endIp)
} else {
log.Warningf("ip version are different in %s-%s\n", startIp, endIp)
return nil
}
}
func getFirstMask(start, end uint32) uint8 {
maxLen := MAX_MASK_LEN
for ; maxLen > MIN_MASK_LEN; maxLen-- {
if start&(1<<uint32(MAX_MASK_LEN-maxLen)) != 0 {
// maxLen继续减少将会使得start不是所在网段的第一个IP
break
}
if start+^utils.MaskLenToNetmask(uint32(maxLen)) >= end || start+^utils.MaskLenToNetmask(uint32(maxLen-1)) > end {
// maxLen继续减少将会使得网段包含end之后的IP
break
}
}
return uint8(maxLen)
}
func getLastIp(ip uint32, mask uint8) uint32 {
ip += ^utils.MaskLenToNetmask(uint32(mask))
return ip
}
func SplitGroup2Int(src []int32) []uint32 {
groups := make([]uint32, 0, 8)
for _, group := range src {
groups = append(groups, uint32(group&0xffff))
}
return groups
}
func getPorts(src string) []PortRange {
splitSrcPorts := strings.Split(src, "-")
ports := make([]PortRange, 0, 8)
if len(splitSrcPorts) < 2 {
portInt, err := strconv.Atoi(src)
if err == nil {
ports = append(ports, NewPortRange(uint16(portInt), uint16(portInt)))
}
return ports
}
min, err := strconv.Atoi(splitSrcPorts[0])
if err != nil {
return ports
}
max, err := strconv.Atoi(splitSrcPorts[1])
if err != nil {
return ports
}
ports = append(ports, NewPortRange(uint16(min), uint16(max)))
return ports
}
func SplitPort2Int(src string) []PortRange {
ports := make([]PortRange, 0, 8)
if len(src) == 0 {
ports := append(ports, NewPortRange(0, 65535))
return ports
}
splitSrcPorts := strings.Split(src, ",")
for _, srcPorts := range splitSrcPorts {
ports = append(ports, getPorts(srcPorts)...)
}
// 从小到大排序
sort.Slice(ports, func(i, j int) bool { return ports[i].Min() < ports[j].Min() })
deleteFlags := make([]bool, len(ports))
for i := 0; i < len(ports); i++ {
if i == len(ports)-1 {
continue
}
// 合并连续的端口号
if ports[i].Max()+1 >= ports[i+1].Min() {
max := ports[i+1].Max()
if ports[i].Max() > max {
max = ports[i].Max()
}
ports[i+1] = NewPortRange(ports[i].Min(), max)
deleteFlags[i] = true
}
}
newPorts := make([]PortRange, 0, len(ports))
// 删除无效数据
for i := 0; i < len(ports); i++ {
if deleteFlags[i] {
continue
}
newPorts = append(newPorts, ports[i])
}
return newPorts
}