-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patharp_store.go
83 lines (63 loc) · 1.45 KB
/
arp_store.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
package main
import (
"net"
"sort"
"strings"
"sync"
"time"
)
type ARPData struct {
Interface net.Interface
Operation uint16
SenderMACAddress string
SenderIPAddress string
TargetMACAddress string
TargetIPAddress string
Time time.Time
}
type ARPDatas []*ARPData
func (l ARPDatas) Len() int { return len(l) }
func (l ARPDatas) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
func (l ARPDatas) Less(i, j int) bool { return l[i].Time.Unix() > l[j].Time.Unix() }
type ARPStore struct {
sync.RWMutex
arpData map[string]*ARPData
}
func NewARPStore() *ARPStore {
return &ARPStore{arpData: make(map[string]*ARPData)}
}
func (s *ARPStore) PutARPData(data *ARPData) (*ARPData, bool) {
s.Lock()
defer s.Unlock()
key := strings.Join([]string{data.SenderIPAddress, data.TargetIPAddress}, ":")
if existingData, exists := s.arpData[key]; exists {
return existingData, exists
} else {
s.arpData[key] = data
return nil, false
}
}
func (s *ARPStore) ARPDataMap() map[string]*ARPData {
s.RLock()
defer s.RUnlock()
mapCopy := make(map[string]*ARPData)
for key, data := range s.arpData {
mapCopy[key] = data
}
return mapCopy
}
func (s *ARPStore) ARPDataListSorted() []*ARPData {
s.RLock()
defer s.RUnlock()
list := make(ARPDatas, 0)
for _, data := range s.arpData {
list = append(list, data)
}
sort.Sort(list)
return list
}
func (s *ARPStore) Len() int {
s.RLock()
defer s.RUnlock()
return len(s.arpData)
}