-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
ip.go
203 lines (166 loc) · 4.16 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
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
// Copyright 2016-2017 Authors of Cilium
//
// 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 addressing
import (
"encoding/json"
"fmt"
"net"
)
type CiliumIP interface {
IPNet(ones int) *net.IPNet
EndpointPrefix() *net.IPNet
IP() net.IP
String() string
IsIPv6() bool
GetFamilyString() string
IsSet() bool
}
type CiliumIPv6 []byte
// NewCiliumIPv6 returns a IPv6 if the given `address` is an IPv6 address.
func NewCiliumIPv6(address string) (CiliumIPv6, error) {
ip, _, err := net.ParseCIDR(address)
if err != nil {
ip = net.ParseIP(address)
if ip == nil {
return nil, fmt.Errorf("Invalid IPv6 address: %s", address)
}
}
// As result of ParseIP, ip is either a valid IPv6 or IPv4 address. net.IP
// represents both versions on 16 bytes, so a more reliable way to tell
// IPv4 and IPv6 apart is to see if it fits 4 bytes
ip4 := ip.To4()
if ip4 != nil {
return nil, fmt.Errorf("Not an IPv6 address: %s", address)
}
return DeriveCiliumIPv6(ip.To16()), nil
}
func DeriveCiliumIPv6(src net.IP) CiliumIPv6 {
ip := make(CiliumIPv6, 16)
copy(ip, src.To16())
return ip
}
// IsSet returns true if the IP is set
func (ip CiliumIPv6) IsSet() bool {
return ip.String() != ""
}
func (ip CiliumIPv6) IsIPv6() bool {
return true
}
func (ip CiliumIPv6) IPNet(ones int) *net.IPNet {
return &net.IPNet{
IP: ip.IP(),
Mask: net.CIDRMask(ones, 128),
}
}
func (ip CiliumIPv6) EndpointPrefix() *net.IPNet {
return ip.IPNet(128)
}
func (ip CiliumIPv6) IP() net.IP {
return net.IP(ip)
}
func (ip CiliumIPv6) String() string {
if ip == nil {
return ""
}
return net.IP(ip).String()
}
func (ip CiliumIPv6) MarshalJSON() ([]byte, error) {
return json.Marshal(net.IP(ip))
}
func (ip *CiliumIPv6) UnmarshalJSON(b []byte) error {
if len(b) < len(`""`) {
return fmt.Errorf("Invalid CiliumIPv6 '%s'", string(b))
}
str := string(b[1 : len(b)-1])
if str == "" {
return nil
}
c, err := NewCiliumIPv6(str)
if err != nil {
return fmt.Errorf("Invalid CiliumIPv6 '%s': %s", str, err)
}
*ip = c
return nil
}
type CiliumIPv4 []byte
func NewCiliumIPv4(address string) (CiliumIPv4, error) {
ip, _, err := net.ParseCIDR(address)
if err != nil {
ip = net.ParseIP(address)
if ip == nil {
return nil, fmt.Errorf("Invalid IPv4 address: %s", address)
}
}
ip4 := ip.To4()
if ip4 == nil {
return nil, fmt.Errorf("Not an IPv4 address")
}
return DeriveCiliumIPv4(ip4), nil
}
func DeriveCiliumIPv4(src net.IP) CiliumIPv4 {
ip := make(CiliumIPv4, 4)
copy(ip, src.To4())
return ip
}
// IsSet returns true if the IP is set
func (ip CiliumIPv4) IsSet() bool {
return ip.String() != ""
}
func (ip CiliumIPv4) IsIPv6() bool {
return false
}
func (ip CiliumIPv4) IPNet(ones int) *net.IPNet {
return &net.IPNet{
IP: net.IP(ip),
Mask: net.CIDRMask(ones, 32),
}
}
func (ip CiliumIPv4) EndpointPrefix() *net.IPNet {
return ip.IPNet(32)
}
func (ip CiliumIPv4) IP() net.IP {
return net.IP(ip)
}
func (ip CiliumIPv4) String() string {
if ip == nil {
return ""
}
return net.IP(ip).String()
}
func (ip CiliumIPv4) MarshalJSON() ([]byte, error) {
return json.Marshal(net.IP(ip))
}
func (ip *CiliumIPv4) UnmarshalJSON(b []byte) error {
if len(b) < len(`""`) {
return fmt.Errorf("Invalid CiliumIPv4 '%s'", string(b))
}
str := string(b[1 : len(b)-1])
if str == "" {
return nil
}
c, err := NewCiliumIPv4(str)
if err != nil {
return fmt.Errorf("Invalid CiliumIPv4 '%s': %s", str, err)
}
*ip = c
return nil
}
// GetFamilyString returns the address family of ip as a string.
func (ip CiliumIPv4) GetFamilyString() string {
return "IPv4"
}
// GetFamilyString returns the address family of ip as a string.
func (ip CiliumIPv6) GetFamilyString() string {
return "IPv6"
}