forked from projectcalico/felix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ip_addr.go
190 lines (157 loc) · 4 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
// 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 (
"fmt"
"net"
log "github.com/Sirupsen/logrus"
calinet "github.com/projectcalico/libcalico-go/lib/net"
)
// 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
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) 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) 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 CIDRFromCalicoNet(ipNet calinet.IPNet) CIDR {
return CIDRFromIPNet(&ipNet.IPNet)
}
func CIDRFromIPNet(ipNet *net.IPNet) CIDR {
ones, _ := ipNet.Mask.Size()
ip := FromNetIP(ipNet.IP)
if ip.Version() == 4 {
return V4CIDR{
addr: ip.(V4Addr),
prefix: uint8(ones),
}
} else {
return V6CIDR{
addr: ip.(V6Addr),
prefix: uint8(ones),
}
}
}
func MustParseCIDR(s string) CIDR {
_, ipNet, err := net.ParseCIDR(s)
if err != nil {
log.WithError(err).WithField("cidr", s).Panic("Failed to parse CIDR")
}
return CIDRFromIPNet(ipNet)
}