-
Notifications
You must be signed in to change notification settings - Fork 463
/
cidr.go
178 lines (142 loc) · 4.47 KB
/
cidr.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
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package cidr
import (
"fmt"
"net"
"k8s.io/apimachinery/pkg/util/validation"
"k8s.io/apimachinery/pkg/util/validation/field"
)
const (
// IPFamilyIPv4 is the IPv4 IP family.
IPFamilyIPv4 string = "IPv4"
// IPFamilyIPv6 is the IPv6 IP family.
IPFamilyIPv6 string = "IPv6"
)
// CIDR contains CIDR and Path information
type CIDR interface {
// GetCIDR returns the provided CIDR
GetCIDR() string
// GetFieldPath returns the fieldpath
GetFieldPath() *field.Path
// GetIPNet optionally returns the IPNet of the CIDR
GetIPNet() *net.IPNet
// Parse checks if CIDR parses
Parse() bool
// ValidateNotOverlap returns errors if subsets overlap with CIDR. This is the inverse operation of ValidateOverlap.
ValidateNotOverlap(subsets ...CIDR) field.ErrorList
// ValidateParse returns errors CIDR can't be parsed.
ValidateParse() field.ErrorList
// ValidateIPFamily returns error if IPFamily does not match CIDR.
ValidateIPFamily(ipFamily string) field.ErrorList
// ValidateSubset returns errors if subsets is not a subset.
ValidateSubset(subsets ...CIDR) field.ErrorList
// LastIPInRange returns the last IP in the CIDR range.
LastIPInRange() net.IP
// ValidateOverlap returns errors if the subnets do not overlap with CIDR.
ValidateOverlap(subsets ...CIDR) field.ErrorList
}
type cidrPath struct {
cidr string
fieldPath *field.Path
net *net.IPNet
ParseError error
}
// NewCIDR creates a new instance of cidrPath
func NewCIDR(c string, f *field.Path) CIDR {
_, ipNet, err := net.ParseCIDR(c)
return &cidrPath{c, f, ipNet, err}
}
func (c *cidrPath) ValidateSubset(subsets ...CIDR) field.ErrorList {
allErrs := field.ErrorList{}
if c.ParseError != nil {
return allErrs
}
for _, subset := range subsets {
if subset == nil || c == subset || !subset.Parse() {
continue
}
if !c.net.Contains(subset.GetIPNet().IP) || !c.net.Contains(subset.LastIPInRange()) {
allErrs = append(allErrs, field.Invalid(subset.GetFieldPath(), subset.GetCIDR(), fmt.Sprintf("must be a subset of %q (%q)", c.fieldPath.String(), c.cidr)))
}
}
return allErrs
}
func (c *cidrPath) ValidateOverlap(subsets ...CIDR) field.ErrorList {
allErrs := field.ErrorList{}
if c.ParseError != nil {
return allErrs
}
for _, subset := range subsets {
if subset == nil || c == subset || !subset.Parse() {
continue
}
// continue if CIDRs overlap.
if c.net.Contains(subset.GetIPNet().IP) || subset.GetIPNet().Contains(c.net.IP) {
continue
}
allErrs = append(allErrs, field.Invalid(subset.GetFieldPath(), subset.GetCIDR(), fmt.Sprintf("must overlap with %q (%q)", c.fieldPath.String(), c.cidr)))
}
return allErrs
}
func (c *cidrPath) ValidateNotOverlap(subsets ...CIDR) field.ErrorList {
allErrs := field.ErrorList{}
if c.ParseError != nil {
return allErrs
}
for _, subset := range subsets {
if subset == nil || c == subset || !subset.Parse() {
continue
}
// continue if CIDRs do not overlap.
if !c.net.Contains(subset.GetIPNet().IP) && !subset.GetIPNet().Contains(c.net.IP) {
continue
}
allErrs = append(allErrs, field.Invalid(subset.GetFieldPath(), subset.GetCIDR(), fmt.Sprintf("must not overlap with %q (%q)", c.fieldPath.String(), c.cidr)))
}
return allErrs
}
func (c *cidrPath) ValidateParse() field.ErrorList {
allErrs := field.ErrorList{}
if c.ParseError != nil {
allErrs = append(allErrs, field.Invalid(c.fieldPath, c.cidr, c.ParseError.Error()))
}
return allErrs
}
// ValidateIPFamily returns error if IPFamily does not match CIDR.
func (c *cidrPath) ValidateIPFamily(ipFamily string) field.ErrorList {
allErrs := field.ErrorList{}
if c.ParseError != nil {
return allErrs
}
switch ipFamily {
case IPFamilyIPv4:
allErrs = append(allErrs, validation.IsValidIPv4Address(c.fieldPath, c.net.IP.String())...)
case IPFamilyIPv6:
allErrs = append(allErrs, validation.IsValidIPv6Address(c.fieldPath, c.net.IP.String())...)
}
return allErrs
}
func (c *cidrPath) Parse() (success bool) {
return c.ParseError == nil
}
func (c *cidrPath) GetIPNet() *net.IPNet {
return c.net
}
func (c *cidrPath) GetFieldPath() *field.Path {
return c.fieldPath
}
func (c *cidrPath) GetCIDR() string {
return c.cidr
}
func (c *cidrPath) LastIPInRange() net.IP {
var buf, res net.IP
for _, b := range c.GetIPNet().Mask {
buf = append(buf, ^b)
}
for i := range c.GetIPNet().IP {
res = append(res, c.GetIPNet().IP[i]|buf[i])
}
return res
}