forked from projectcalico/felix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ip_addr.go
242 lines (203 loc) · 5.28 KB
/
ip_addr.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
// Copyright (c) 2016-2017 Tigera, Inc. All rights reserved.
//
// 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.
// The ip package contains yet another IP address (and CIDR) type :-). The
// types differ from the ones in the net package in that they are backed by
// fixed-sized arrays of the appropriate size. The key advantage of
// using a fixed-size array is that it makes the types hashable so they can
// be used as map keys. In addition, they can be converted to net.IP by
// slicing.
package ip
import (
"errors"
"fmt"
"net"
"strings"
log "github.com/sirupsen/logrus"
calinet "github.com/projectcalico/libcalico-go/lib/net"
)
var ErrInvalidIP = errors.New("Failed to parse IP address")
// Addr represents either an IPv4 or IPv6 IP address.
type Addr interface {
// Version returns the IP version; 4 or 6.
Version() uint8
// AsNetIP returns a net.IP, which is backed by/shares storage with
// this object.
AsNetIP() net.IP
AsCalicoNetIP() calinet.IP
AsCIDR() CIDR
String() string
}
type V4Addr [4]byte
func (a V4Addr) Version() uint8 {
return 4
}
func (a V4Addr) AsNetIP() net.IP {
return net.IP(a[0:net.IPv4len])
}
func (a V4Addr) AsCalicoNetIP() calinet.IP {
return calinet.IP{IP: a.AsNetIP()}
}
func (a V4Addr) AsCIDR() CIDR {
return V4CIDR{
addr: a,
prefix: 32,
}
}
func (a V4Addr) String() string {
return a.AsNetIP().String()
}
type V6Addr [16]byte
func (a V6Addr) Version() uint8 {
return 6
}
func (a V6Addr) AsNetIP() net.IP {
return net.IP(a[0:net.IPv6len])
}
func (a V6Addr) AsCalicoNetIP() calinet.IP {
return calinet.IP{IP: a.AsNetIP()}
}
func (a V6Addr) AsCIDR() CIDR {
return V6CIDR{
addr: a,
prefix: 128,
}
}
func (a V6Addr) String() string {
return a.AsNetIP().String()
}
type CIDR interface {
Version() uint8
Addr() Addr
Prefix() uint8
String() string
ToIPNet() net.IPNet
}
type V4CIDR struct {
addr V4Addr
prefix uint8
}
func (c V4CIDR) Version() uint8 {
return 4
}
func (c V4CIDR) Addr() Addr {
return c.addr
}
func (c V4CIDR) Prefix() uint8 {
return c.prefix
}
func (c V4CIDR) ToIPNet() net.IPNet {
return net.IPNet{
IP: c.Addr().AsNetIP(),
Mask: net.CIDRMask(int(c.Prefix()), 32),
}
}
func (c V4CIDR) String() string {
return fmt.Sprintf("%s/%v", c.addr.String(), c.prefix)
}
type V6CIDR struct {
addr V6Addr
prefix uint8
}
func (c V6CIDR) Version() uint8 {
return 6
}
func (c V6CIDR) Addr() Addr {
return c.addr
}
func (c V6CIDR) Prefix() uint8 {
return c.prefix
}
func (c V6CIDR) ToIPNet() net.IPNet {
return net.IPNet{
IP: c.Addr().AsNetIP(),
Mask: net.CIDRMask(int(c.Prefix()), 128),
}
}
func (c V6CIDR) String() string {
return fmt.Sprintf("%s/%v", c.addr.String(), c.prefix)
}
func FromString(s string) Addr {
return FromNetIP(net.ParseIP(s))
}
func FromNetIP(netIP net.IP) Addr {
// Note: we have to use To4() here because the net package often represents an IPv4 address
// using 16 bytes. The only way to distinguish an IPv4 address using that API is To4(),
// which returns nil if the IP is a v6 address or nil.
if v4NetIP := netIP.To4(); v4NetIP != nil {
ip := V4Addr{}
copy(ip[:], v4NetIP)
return ip
}
if v6NetIP := netIP.To16(); v6NetIP != nil {
ip := V6Addr{}
copy(ip[:], v6NetIP)
return ip
}
return nil
}
func CIDRFromString(cidrStr string) (CIDR, error) {
_, cidr, err := net.ParseCIDR(cidrStr)
if err != nil {
return nil, err
}
return CIDRFromIPNet(cidr), nil
}
func CIDRFromCalicoNet(ipNet calinet.IPNet) CIDR {
return CIDRFromIPNet(&ipNet.IPNet)
}
func CIDRFromIPNet(ipNet *net.IPNet) CIDR {
ones, _ := ipNet.Mask.Size()
// Mask the IP before creating the CIDR so that we have it in canonical format.
ip := FromNetIP(ipNet.IP.Mask(ipNet.Mask))
if ip.Version() == 4 {
return V4CIDR{
addr: ip.(V4Addr),
prefix: uint8(ones),
}
} else {
return V6CIDR{
addr: ip.(V6Addr),
prefix: uint8(ones),
}
}
}
// CIDRFromNetIP converts the given IP into our CIDR representation as a /32 or /128.
func CIDRFromNetIP(netIP net.IP) CIDR {
return FromNetIP(netIP).AsCIDR()
}
// MustParseCIDROrIP parses the given IP address or CIDR, treating IP addresses as "full length"
// CIDRs. For example, "10.0.0.1" is treated as "10.0.0.1/32". It panics on failure.
func MustParseCIDROrIP(s string) CIDR {
cidr, err := ParseCIDROrIP(s)
if err != nil {
log.WithError(err).WithField("cidr", s).Panic("Failed to parse CIDR")
}
return cidr
}
// ParseCIDROrIP parses the given IP address or CIDR, treating IP addresses as "full length"
// CIDRs. For example, "10.0.0.1" is treated as "10.0.0.1/32".
func ParseCIDROrIP(s string) (CIDR, error) {
if !strings.Contains(s, "/") {
ip := net.ParseIP(s)
if ip == nil {
return nil, ErrInvalidIP
}
return CIDRFromNetIP(ip), nil
}
_, netCIDR, err := net.ParseCIDR(s)
if err != nil {
return nil, err
}
return CIDRFromIPNet(netCIDR), nil
}