-
Notifications
You must be signed in to change notification settings - Fork 442
/
ip.go
173 lines (143 loc) · 4.02 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
package ipam
import (
"math/big"
"strings"
"github.com/kubeovn/kube-ovn/pkg/util"
)
type IP string
type IPRange struct {
Start IP
End IP
}
func (a IP) Equal(b IP) bool {
return a == b
}
func (a IP) LessThan(b IP) bool {
return util.Ip2BigInt(string(a)).Cmp(util.Ip2BigInt(string(b))) == -1
}
func (a IP) GreaterThan(b IP) bool {
return util.Ip2BigInt(string(a)).Cmp(util.Ip2BigInt(string(b))) == 1
}
func (a IP) Add(num int64) IP {
return IP(util.BigInt2Ip(big.NewInt(0).Add(util.Ip2BigInt(string(a)), big.NewInt(num))))
}
func (a IP) Sub(num int64) IP {
return IP(util.BigInt2Ip(big.NewInt(0).Sub(util.Ip2BigInt(string(a)), big.NewInt(num))))
}
func (ipr IPRange) IPExist(ip IP) bool {
return (ipr.Start.LessThan(ip) || ipr.Start.Equal(ip)) &&
(ipr.End.GreaterThan(ip) || ipr.End.Equal(ip))
}
type IPRangeList []*IPRange
func (iprl IPRangeList) Contains(ip IP) bool {
for _, ipr := range iprl {
if ipr.IPExist(ip) {
return true
}
}
return false
}
func splitIPRangeList(iprl IPRangeList, ip IP) (bool, IPRangeList) {
newIPRangeList := []*IPRange{}
split := false
for _, ipr := range iprl {
if split {
newIPRangeList = append(newIPRangeList, ipr)
continue
}
if ipr.Start.Equal(ipr.End) && ipr.Start.Equal(ip) {
split = true
continue
}
if ipr.Start.Equal(ip) {
newIPRangeList = append(newIPRangeList, &IPRange{Start: ip.Add(1), End: ipr.End})
split = true
continue
}
if ipr.End.Equal(ip) {
newIPRangeList = append(newIPRangeList, &IPRange{Start: ipr.Start, End: ip.Add(-1)})
split = true
continue
}
if ipr.IPExist(ip) {
newIpr1 := IPRange{Start: ipr.Start, End: ip.Add(-1)}
newIpr2 := IPRange{Start: ip.Add(1), End: ipr.End}
newIPRangeList = append(newIPRangeList, &newIpr1, &newIpr2)
split = true
continue
}
newIPRangeList = append(newIPRangeList, ipr)
}
return split, newIPRangeList
}
func mergeIPRangeList(iprl IPRangeList, ip IP) (bool, IPRangeList) {
insertIPRangeList := []*IPRange{}
inserted := false
if iprl.Contains(ip) {
return false, nil
}
for _, ipr := range iprl {
if inserted || ipr.Start.LessThan(ip) {
insertIPRangeList = append(insertIPRangeList, ipr)
continue
}
if ipr.Start.GreaterThan(ip) {
insertIPRangeList = append(insertIPRangeList, &IPRange{Start: ip, End: ip}, ipr)
inserted = true
continue
}
}
if !inserted {
newIpr := IPRange{Start: ip, End: ip}
insertIPRangeList = append(insertIPRangeList, &newIpr)
}
mergedIPRangeList := []*IPRange{}
for _, ipr := range insertIPRangeList {
if len(mergedIPRangeList) == 0 {
mergedIPRangeList = append(mergedIPRangeList, ipr)
continue
}
if mergedIPRangeList[len(mergedIPRangeList)-1].End.Add(1).Equal(ipr.Start) {
mergedIPRangeList[len(mergedIPRangeList)-1].End = ipr.End
} else {
mergedIPRangeList = append(mergedIPRangeList, ipr)
}
}
return true, mergedIPRangeList
}
func convertExcludeIps(excludeIps []string) IPRangeList {
newIPRangeList := make([]*IPRange, 0, len(excludeIps))
for _, ex := range excludeIps {
ips := strings.Split(ex, "..")
if len(ips) == 1 {
ipr := IPRange{Start: IP(ips[0]), End: IP(ips[0])}
newIPRangeList = append(newIPRangeList, &ipr)
} else {
ipr := IPRange{Start: IP(ips[0]), End: IP(ips[1])}
newIPRangeList = append(newIPRangeList, &ipr)
}
}
return newIPRangeList
}
func splitRange(a, b *IPRange) IPRangeList {
if b.End.LessThan(a.Start) || b.Start.GreaterThan(a.End) {
return IPRangeList{a}
}
if (a.Start.Equal(b.Start) || a.Start.GreaterThan(b.Start)) &&
(a.End.Equal(b.End) || a.End.LessThan(b.End)) {
return nil
}
if (a.Start.Equal(b.Start) || a.Start.GreaterThan(b.Start)) &&
a.End.GreaterThan(b.End) {
ipr := IPRange{Start: b.End.Add(1), End: a.End}
return IPRangeList{&ipr}
}
if (a.End.Equal(b.End) || a.End.LessThan(b.End)) &&
a.Start.LessThan(b.Start) {
ipr := IPRange{Start: a.Start, End: b.Start.Add(-1)}
return IPRangeList{&ipr}
}
ipr1 := IPRange{Start: a.Start, End: b.Start.Add(-1)}
ipr2 := IPRange{Start: b.End.Add(1), End: a.End}
return IPRangeList{&ipr1, &ipr2}
}