-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprefix.go
More file actions
242 lines (205 loc) · 4.24 KB
/
prefix.go
File metadata and controls
242 lines (205 loc) · 4.24 KB
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 2016, 2017 CoreSwitch
//
// 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 netutil
import (
//"encoding/json"
"net"
"strconv"
"strings"
)
var MaskBits = []byte{0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff}
var MaskReverseBits = []byte{0xff, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01, 0x00}
const (
AFI_IP = iota
AFI_IP6
AFI_MAX
)
type Prefix struct {
net.IP
Length int
}
func CopyIP(ip net.IP) net.IP {
dup := make(net.IP, len(ip))
copy(dup, ip)
return dup
}
func (p *Prefix) AFI() int {
if len(p.IP) == net.IPv4len {
return AFI_IP
}
if len(p.IP) == net.IPv6len {
return AFI_IP6
}
return AFI_MAX
}
func (p *Prefix) ByteLength() int {
return (p.Length + 7) / 8
}
func NewPrefixAFI(afi int) *Prefix {
switch afi {
case AFI_IP:
return &Prefix{IP: make(net.IP, net.IPv4len), Length: 0}
case AFI_IP6:
return &Prefix{IP: make(net.IP, net.IPv6len), Length: 0}
default:
return nil
}
}
func ParsePrefix(s string) (*Prefix, error) {
i := strings.IndexByte(s, '/')
if i < 0 {
return nil, &net.ParseError{Type: "Prefix address", Text: s}
}
addr, mask := s[:i], s[i+1:]
ip := net.ParseIP(addr)
if ip == nil {
return nil, &net.ParseError{Type: "Prefix address", Text: s}
}
ip4 := ip.To4()
if ip4 != nil {
ip = ip4
}
length, err := strconv.Atoi(mask)
if err != nil {
return nil, err
}
return &Prefix{IP: ip, Length: length}, nil
}
func (p *Prefix) String() string {
return p.IP.String() + "/" + strconv.Itoa(p.Length)
}
func (p *Prefix) MarshalJSON() ([]byte, error) {
return []byte(`"` + p.String() + `"`), nil
}
func (p *Prefix) ApplyMask() *Prefix {
i := p.Length / 8
if i >= len(p.IP) {
return p
}
offset := p.Length % 8
p.IP[i] &= MaskBits[offset]
i++
for i < len(p.IP) {
p.IP[i] = 0
i++
}
return p
}
func (p *Prefix) ApplyReverseMask() *Prefix {
i := p.Length / 8
if i >= len(p.IP) {
return p
}
offset := p.Length % 8
p.IP[i] |= MaskReverseBits[offset]
i++
for i < len(p.IP) {
p.IP[i] = 0
i++
}
return p
}
func (p *Prefix) Copy() *Prefix {
return &Prefix{IP: CopyIP(p.IP), Length: p.Length}
}
func (p *Prefix) Equal(x *Prefix) bool {
if p.IP.Equal(x.IP) && p.Length == x.Length {
return true
} else {
return false
}
}
func PrefixFromIPNet(net net.IPNet) *Prefix {
ip := net.IP.To4()
if ip == nil {
ip = net.IP
}
len, _ := net.Mask.Size()
return &Prefix{IP: CopyIP(ip), Length: len}
}
func PrefixFromNode(n *PtreeNode) *Prefix {
ip := make([]byte, len(n.Key()))
copy(ip, n.Key())
p := &Prefix{
IP: ip,
Length: n.KeyLength(),
}
return p
}
func IPNetFromPrefix(p *Prefix) net.IPNet {
if len(p.IP) == net.IPv4len {
return net.IPNet{IP: p.IP, Mask: net.CIDRMask(p.Length, 32)}
} else {
return net.IPNet{IP: p.IP, Mask: net.CIDRMask(p.Length, 128)}
}
}
func PrefixFromIPPrefixlen(ip net.IP, len int) *Prefix {
return &Prefix{IP: CopyIP(ip), Length: len}
}
func ParseIPv4(s string) net.IP {
ip := net.ParseIP(s)
ip4 := ip.To4()
if ip4 != nil {
return ip4
}
return ip
}
func SameIp(a net.IP, b net.IP) bool {
if a == nil && b == nil {
return true
}
if a == nil && b != nil {
return false
}
if a != nil && b == nil {
return false
}
return a.Equal(b)
}
func (p *Prefix) IsDefault() bool {
if p.Length != 0 {
return false
}
for _, v := range p.IP {
if v != 0 {
return false
}
}
return true
}
func (p *Prefix) Match(q *Prefix) bool {
if p.Length > q.Length {
return false
}
if len(p.IP) != len(q.IP) {
return false
}
if len(p.IP) != 4 && len(p.IP) != 16 {
return false
}
offset := p.Length / 8
shift := p.Length % 8
if shift != 0 {
if (MaskBits[shift] & (p.IP[offset] ^ q.IP[offset])) != 0 {
return false
}
}
for offset != 0 {
offset--
if p.IP[offset] != q.IP[offset] {
return false
}
}
return true
}