-
Notifications
You must be signed in to change notification settings - Fork 1
/
macs.go
65 lines (55 loc) · 1.3 KB
/
macs.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
// Copyright 2022 Kirill Scherba <kirill@scherba.ru>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Teotun package. Mac address manage module.
package teotun
import "sync"
// macaddr is tunnels connected mac address structure and methods receiver
type macaddr struct {
*sync.RWMutex
m map[string]string
}
// newMacaddr crete new macaddr object
func (t *Teotun) newMacaddr() (p *macaddr) {
p = new(macaddr)
p.RWMutex = new(sync.RWMutex)
p.m = make(map[string]string)
return
}
// add mac address
func (p *macaddr) add(mac, address string) {
p.Lock()
defer p.Unlock()
p.m[mac] = address
}
// del mac address
func (p *macaddr) del(mac string) {
p.Lock()
defer p.Unlock()
delete(p.m, mac)
}
// delByAddr delete mac by peer address
func (p *macaddr) delByAddr(address string) {
p.Lock()
defer p.Unlock()
for mac, addr := range p.m {
if addr == address {
delete(p.m, mac)
}
}
}
// get mac address
func (p *macaddr) get(mac string) (address string, ok bool) {
p.RLock()
defer p.RUnlock()
address, ok = p.m[mac]
return
}
// forEach calls function f for each added mac address
func (p *macaddr) forEach(f func(mac, address string)) {
p.RLock()
defer p.RUnlock()
for mac, address := range p.m {
f(mac, address)
}
}