-
Notifications
You must be signed in to change notification settings - Fork 2
/
table.go
260 lines (223 loc) · 6.08 KB
/
table.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package worker
import (
"fmt"
"net"
"net/netip"
"sort"
"strings"
"sync"
"time"
)
type IPAddressInfo struct {
Address netip.Addr
Hw net.HardwareAddr
mutex sync.RWMutex
timer *time.Timer
timerExpiration time.Time
timerPreventive1 *time.Timer
timerPreventive2 *time.Timer
timerPreventive3 *time.Timer
stopChannel chan bool
onExpiration func(*IPAddressInfo, int)
}
type IPAddressSet struct {
addresses map[netip.Addr]*IPAddressInfo
addressesMapMutex sync.RWMutex
}
type Table struct {
macs map[string]*IPAddressSet
macsMapMutex sync.RWMutex
}
func (i *IPAddressInfo) IsStillValid() bool {
return i.timerExpiration.After(time.Now())
}
func (i *IPAddressInfo) Extend(ttl time.Duration) {
i.mutex.Lock()
i.timerExpiration = time.Now().Add(ttl)
i.timer.Reset(ttl)
i.timerPreventive1.Reset(ttl / 3 * 2)
i.timerPreventive2.Reset(ttl / 4 * 3)
i.timerPreventive3.Reset(ttl / 5 * 4)
i.mutex.Unlock()
}
func (i *IPAddressInfo) GetExpiration() time.Time {
i.mutex.RLock()
expiration := i.timerExpiration
i.mutex.RUnlock()
return expiration
}
func (i *IPAddressInfo) GetAddress() netip.Addr {
return i.Address
}
func (i *IPAddressInfo) Clear() {
i.mutex.Lock()
i.timerExpiration = time.Now()
i.timer.Stop()
i.timerPreventive1.Stop()
i.timerPreventive2.Stop()
i.timerPreventive3.Stop()
close(i.stopChannel)
i.mutex.Unlock()
}
func NewIPAddressInfo(hw net.HardwareAddr, addr netip.Addr, ttl time.Duration, onExpiration func(*IPAddressInfo, int)) *IPAddressInfo {
info := &IPAddressInfo{
onExpiration: onExpiration,
Address: addr,
Hw: hw,
timer: time.NewTimer(ttl),
timerExpiration: time.Now().Add(ttl),
timerPreventive1: time.NewTimer(ttl / 3 * 2),
timerPreventive2: time.NewTimer(ttl / 4 * 3),
timerPreventive3: time.NewTimer(ttl / 5 * 4),
stopChannel: make(chan bool),
}
go func() {
for {
select {
case <-info.timer.C:
info.onExpiration(info, 0)
return
case <-info.timerPreventive1.C:
info.onExpiration(info, 1)
case <-info.timerPreventive2.C:
info.onExpiration(info, 2)
case <-info.timerPreventive3.C:
info.onExpiration(info, 3)
case <-info.stopChannel:
return
}
}
}()
return info
}
// Add an IPv6 address to the set with a TTL (in seconds).
func (s *IPAddressSet) Add(hw net.HardwareAddr, addr netip.Addr, ttl time.Duration, onExpiration func(*IPAddressInfo, int)) bool {
existing := false
if !s.Contains(addr) {
s.addressesMapMutex.Lock()
s.addresses[addr] = NewIPAddressInfo(hw, addr, ttl, onExpiration)
s.addressesMapMutex.Unlock()
} else {
if (s.addresses[addr]).IsStillValid() {
existing = true
}
(s.addresses[addr]).Extend(ttl)
(s.addresses[addr]).onExpiration = onExpiration
}
return existing
}
// Remove an IPv6 address from the set.
func (s *IPAddressSet) Remove(addr netip.Addr) {
if s.Contains(addr) {
(s.addresses[addr]).Clear()
s.addressesMapMutex.Lock()
delete(s.addresses, addr)
s.addressesMapMutex.Unlock()
}
}
// Check if an IPv6 address exists in the set and is not expired.
func (s *IPAddressSet) Contains(addr netip.Addr) bool {
s.addressesMapMutex.RLock()
info, ok := s.addresses[addr]
s.addressesMapMutex.RUnlock()
if !ok {
return false
}
return time.Now().Before(info.GetExpiration())
}
func (s *IPAddressSet) PrettyPrint(tabSize int) string {
indent := func(level int) string {
return strings.Repeat(" ", level*tabSize)
}
var result strings.Builder
s.addressesMapMutex.RLock()
// Get the keys from the map
keys := make([]netip.Addr, 0, len(s.addresses))
for k := range s.addresses {
keys = append(keys, k)
}
// Sort the keys
sort.Slice(keys, func(i, j int) bool {
return keys[i].Less(keys[j])
})
// Iterate ordered
for _, key := range keys {
ipAddressInfo := s.addresses[key]
fmt.Fprintf(&result, indent(2)+"%s %.0f\n", ipAddressInfo.Address.String(), time.Until(ipAddressInfo.GetExpiration()).Seconds())
}
s.addressesMapMutex.RUnlock()
return result.String()
}
func NewIPAddressSet() *IPAddressSet {
return &IPAddressSet{addresses: make(map[netip.Addr]*IPAddressInfo)}
}
// Add macs address to the set with a TTL (in seconds).
func (t *Table) Add(hw net.HardwareAddr, addr netip.Addr, ttl time.Duration, onExpiration func(*IPAddressInfo, int)) bool {
mac := hw.String()
if !t.Contains(hw) {
t.macsMapMutex.Lock()
t.macs[mac] = NewIPAddressSet()
t.macsMapMutex.Unlock()
}
return (t.macs[mac]).Add(hw, addr, ttl, onExpiration)
}
// Remove an MACs address from the set.
func (t *Table) Remove(hw net.HardwareAddr) {
mac := hw.String()
t.macsMapMutex.Lock()
delete(t.macs, mac)
t.macsMapMutex.Unlock()
}
// Check if an MAC address exists in the set and is not expired.
func (t *Table) Contains(hw net.HardwareAddr) bool {
mac := hw.String()
t.macsMapMutex.RLock()
_, ok := t.macs[mac]
t.macsMapMutex.RUnlock()
return ok
}
func (t *Table) Filter(hws []net.HardwareAddr, prefixes []netip.Prefix) []*IPAddressInfo {
found := []*IPAddressInfo{}
for _, prefix := range prefixes {
for _, hw := range hws {
mac := hw.String()
t.macsMapMutex.RLock()
if t.Contains(hw) {
for _, ipAddressInfo := range t.macs[mac].addresses {
// Remove zone identifier from netip.Addr, zones strip prefixes
test := netip.AddrFrom16(ipAddressInfo.Address.As16())
if prefix.Contains(test) {
found = append(found, ipAddressInfo)
}
}
}
t.macsMapMutex.RUnlock()
}
}
return found
}
func (t *Table) PrettyPrint(tabSize int) string {
indent := func(level int) string {
return strings.Repeat(" ", level*tabSize)
}
var result strings.Builder
result.WriteString("Table:\n")
t.macsMapMutex.RLock()
// Get the keys from the map
keys := make([]string, 0, len(t.macs))
for k := range t.macs {
keys = append(keys, k)
}
// Sort the keys
sort.Strings(keys)
// Iterate ordered
for _, key := range keys {
fmt.Fprintf(&result, indent(1)+"%s:\n", key)
result.WriteString(t.macs[key].PrettyPrint(tabSize))
}
t.macsMapMutex.RUnlock()
return result.String()
}
func NewTable() *Table {
return &Table{macs: make(map[string]*IPAddressSet)}
}