-
Notifications
You must be signed in to change notification settings - Fork 51
/
ipprefix.go
246 lines (199 loc) · 5.19 KB
/
ipprefix.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
243
244
245
246
package ipprefix
import (
"encoding/binary"
"net"
"sync"
)
// FuncOnLpmIP is the type of func which will operate on the value associated with the lpm ip.
type FuncOnLpmIP func(val interface{}) bool
// FuncOnVals is the type of the func which will operate on each value and will return a new value
// for each associated value.
type FuncOnVals func(val interface{}) interface{}
//IPcache is an interface which provides functionality to store ip's and do longest prefix match
type IPcache interface {
// Put takes an argument an ip address, mask and value.
Put(net.IP, int, interface{})
// Get takes an argument the IP address and mask and returns the value that is stored for
// that key.
Get(net.IP, int) (interface{}, bool)
// RunFuncOnLpmIP function takes as an argument an IP address and a function. It finds the
// subnet to which this IP belongs with the longest prefix match. It then calls the
// function supplied by the user on the value stored and if it succeeds then it returns.
RunFuncOnLpmIP(net.IP, FuncOnLpmIP)
// RunFuncOnVals takes an argument a function which is called on all the values stored in
// the cache. This can be used to update the old values with the new values. If the new
// value is nil, it will delete the key.
RunFuncOnVals(FuncOnVals)
}
const (
ipv4MaskSize = 32 + 1
ipv6MaskSize = 128 + 1
)
type ipcacheV4 struct {
ipv4 []map[uint32]interface{}
sync.RWMutex
}
type ipcacheV6 struct {
ipv6 []map[[16]byte]interface{}
sync.RWMutex
}
type ipcache struct {
ipv4 *ipcacheV4
ipv6 *ipcacheV6
}
func (cache *ipcacheV4) Put(ip net.IP, mask int, val interface{}) {
cache.Lock()
defer cache.Unlock()
if cache.ipv4[mask] == nil {
cache.ipv4[mask] = map[uint32]interface{}{}
}
m := cache.ipv4[mask]
// the following expression is ANDing the ip with the mask
key := binary.BigEndian.Uint32(ip) & binary.BigEndian.Uint32(net.CIDRMask(mask, 32))
if val == nil {
delete(m, key)
} else {
m[key] = val
}
}
func (cache *ipcacheV4) Get(ip net.IP, mask int) (interface{}, bool) {
cache.RLock()
defer cache.RUnlock()
m := cache.ipv4[mask]
if m != nil {
val, ok := m[binary.BigEndian.Uint32(ip)&binary.BigEndian.Uint32(net.CIDRMask(mask, 32))]
if ok {
return val, true
}
}
return nil, false
}
func (cache *ipcacheV4) RunFuncOnLpmIP(ip net.IP, f func(val interface{}) bool) {
cache.Lock()
defer cache.Unlock()
for i := len(cache.ipv4) - 1; i >= 0; i-- {
m := cache.ipv4[i]
if m != nil {
val, ok := m[binary.BigEndian.Uint32(ip)&binary.BigEndian.Uint32(net.CIDRMask(i, 32))]
if ok && f(val) {
return
}
}
}
}
func (cache *ipcacheV4) RunFuncOnVals(f func(val interface{}) interface{}) {
cache.Lock()
defer cache.Unlock()
for mask, m := range cache.ipv4 {
if m == nil {
continue
}
for ip, val := range m {
v := f(val)
if v == nil {
delete(m, ip)
continue
}
m[ip] = v
}
if len(m) == 0 {
cache.ipv4[mask] = nil
}
}
}
func (cache *ipcacheV6) Put(ip net.IP, mask int, val interface{}) {
cache.Lock()
defer cache.Unlock()
if cache.ipv6[mask] == nil {
cache.ipv6[mask] = map[[16]byte]interface{}{}
}
m := cache.ipv6[mask]
// the following expression is ANDing the ip with the mask
var maskip [16]byte
copy(maskip[:], ip.Mask(net.CIDRMask(mask, 128)))
if val == nil {
delete(m, maskip)
} else {
m[maskip] = val
}
}
func (cache *ipcacheV6) Get(ip net.IP, mask int) (interface{}, bool) {
cache.RLock()
defer cache.RUnlock()
m := cache.ipv6[mask]
if m != nil {
var maskip [16]byte
copy(maskip[:], ip.Mask(net.CIDRMask(mask, 128)))
val, ok := m[maskip]
if ok {
return val, true
}
}
return nil, false
}
func (cache *ipcacheV6) RunFuncOnLpmIP(ip net.IP, f func(val interface{}) bool) {
cache.Lock()
defer cache.Unlock()
for i := len(cache.ipv6) - 1; i >= 0; i-- {
m := cache.ipv6[i]
if m != nil {
var maskip [16]byte
copy(maskip[:], ip.Mask(net.CIDRMask(i, 128)))
val, ok := m[maskip]
if ok && f(val) {
return
}
}
}
}
func (cache *ipcacheV6) RunFuncOnVals(f func(val interface{}) interface{}) {
cache.Lock()
defer cache.Unlock()
for mask, m := range cache.ipv6 {
if m == nil {
continue
}
for ip, val := range m {
v := f(val)
if v == nil {
delete(m, ip)
continue
}
m[ip] = v
}
if len(m) == 0 {
cache.ipv6[mask] = nil
}
}
}
//NewIPCache creates an object which is implementing the interface IPcache
func NewIPCache() IPcache {
return &ipcache{
ipv4: &ipcacheV4{ipv4: make([]map[uint32]interface{}, ipv4MaskSize)},
ipv6: &ipcacheV6{ipv6: make([]map[[16]byte]interface{}, ipv6MaskSize)},
}
}
func (cache *ipcache) Put(ip net.IP, mask int, val interface{}) {
if ip.To4() != nil {
cache.ipv4.Put(ip.To4(), mask, val)
return
}
cache.ipv6.Put(ip.To16(), mask, val)
}
func (cache *ipcache) Get(ip net.IP, mask int) (interface{}, bool) {
if ip.To4() != nil {
return cache.ipv4.Get(ip.To4(), mask)
}
return cache.ipv6.Get(ip.To16(), mask)
}
func (cache *ipcache) RunFuncOnLpmIP(ip net.IP, f FuncOnLpmIP) {
if ip.To4() != nil {
cache.ipv4.RunFuncOnLpmIP(ip.To4(), f)
return
}
cache.ipv6.RunFuncOnLpmIP(ip.To16(), f)
}
func (cache *ipcache) RunFuncOnVals(f FuncOnVals) {
cache.ipv4.RunFuncOnVals(f)
cache.ipv6.RunFuncOnVals(f)
}