-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmactable.go
203 lines (184 loc) · 4.83 KB
/
mactable.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
package packet
import (
"bytes"
"fmt"
"net"
"net/netip"
"sync"
"time"
"github.com/irai/packet/fastlog"
)
// MACEntry stores mac details
// Each host has one MACEntry
type MACEntry struct {
MAC net.HardwareAddr // unique mac address
Captured bool // true if mac is in capture mode
IP4 netip.Addr // keep current IP4 to detect ip changes
IP4Offer netip.Addr // keep dhcp4 IP offer
IP6GUA netip.Addr // keep current ip6 global unique address
IP6LLA netip.Addr // keep current ip6 local link address
IP6Offer netip.Addr // keep ip6 GUA offer
Online bool // true is mac is online
IsRouter bool // Set to true if this is a router
HostList []*Host // IPs associated with this mac
Row sync.RWMutex // Row level mutex - must lock/unlock if reading/updating MACEntry and Host entry
Manufacturer string // Ethernet card manufacturer name
DHCP4Name NameEntry
MDNSName NameEntry
SSDPName NameEntry
LLMNRName NameEntry
NBNSName NameEntry
LastSeen time.Time
}
func (e *MACEntry) String() string {
l := Logger.Msg("")
return e.FastLog(l).ToString()
}
func (e *MACEntry) FastLog(l *fastlog.Line) *fastlog.Line {
l.MAC("mac", e.MAC)
if e.Captured {
l.Bool("captured", e.Captured)
}
if e.Online {
l.Bool("online", e.Online)
}
l.IP("ip", e.IP4)
l.IP("ip6", e.IP6GUA)
l.IP("lla", e.IP6LLA)
if e.IP4Offer.IsValid() {
l.IP("ip4offer", e.IP4Offer)
}
l.Int("hosts", len(e.HostList))
l.String("lastSeen", time.Since(e.LastSeen).String())
if e.Manufacturer != "" {
l.String("manufacturer", e.Manufacturer)
}
l.Struct(e.DHCP4Name)
l.Struct(e.MDNSName)
l.Struct(e.SSDPName)
l.Struct(e.LLMNRName)
l.Struct(e.NBNSName)
return l
}
// link appends the host to the macEntry host list
func (e *MACEntry) link(host *Host) {
e.HostList = append(e.HostList, host)
}
// unlink removes the Host from the macEntry
func (e *MACEntry) unlink(host *Host) {
for i := range e.HostList {
if e.HostList[i].Addr.IP == host.Addr.IP {
if i+1 == len(e.HostList) { // last element?
e.HostList = e.HostList[:i]
return
}
copy(e.HostList[i:], e.HostList[i+1:])
e.HostList = e.HostList[:len(e.HostList)-1]
return
}
}
}
// MACTable manages a goroutine safe set for adding and removing mac addresses
type MACTable struct {
Table []*MACEntry
}
func newMACTable() MACTable {
return MACTable{Table: []*MACEntry{}}
}
// PrintTable prints the table to stdout
func (h *Session) printMACTable() {
for _, v := range h.MACTable.Table {
fmt.Printf("mac %s\n", v)
}
}
// findOrCreate adds a mac to set
func (s *MACTable) findOrCreate(mac net.HardwareAddr) *MACEntry {
if e, _ := s.findMAC(mac); e != nil {
return e
}
e := &MACEntry{MAC: CopyMAC(mac), IP4: IPv4zero, IP6GUA: IPv6zero, IP6LLA: IPv6zero, IP4Offer: netip.Addr{}}
s.Table = append(s.Table, e)
return e
}
// del deletes the mac from set
func (s *MACTable) delete(mac net.HardwareAddr) error {
var pos int
if _, pos = s.findMAC(mac); pos == -1 {
return nil
}
if pos+1 == len(s.Table) { // last element?
s.Table = s.Table[:pos]
return nil
}
copy(s.Table[pos:], s.Table[pos+1:])
s.Table = s.Table[:len(s.Table)-1]
return nil
}
func (s *MACTable) findMAC(mac net.HardwareAddr) (*MACEntry, int) {
for pos, v := range s.Table {
if bytes.Equal(v.MAC, mac) {
return v, pos
}
}
return nil, -1
}
// NameEntry holds a name entry
type NameEntry struct {
Type string
Name string
Model string
Manufacturer string
OS string
Expire time.Time
}
func (n NameEntry) FastLog(l *fastlog.Line) *fastlog.Line {
if n.Name != "" {
l.String(n.Type+"name", n.Name)
}
if n.Model != "" {
l.String(n.Type+"model", n.Model)
}
if n.OS != "" {
l.String(n.Type+"OS", n.OS)
}
if n.Manufacturer != "" {
l.String(n.Type+"manufacturer", n.Manufacturer)
}
if n.Expire != (time.Time{}) {
l.Time("expire", n.Expire)
}
return l
}
func (e NameEntry) Merge(nameEntry NameEntry) (newEntry NameEntry, modified bool) {
if nameEntry.Name != "" && e.Name != nameEntry.Name {
e.Name = nameEntry.Name
modified = true
}
if nameEntry.Model != "" && e.Model != nameEntry.Model {
e.Model = nameEntry.Model
modified = true
}
if nameEntry.OS != "" && e.OS != nameEntry.OS {
e.OS = nameEntry.OS
modified = true
}
if nameEntry.Manufacturer != "" && e.Manufacturer != nameEntry.Manufacturer {
e.Manufacturer = nameEntry.Manufacturer
modified = true
}
if modified && nameEntry.Expire != (time.Time{}) {
e.Expire = nameEntry.Expire
}
e.Type = nameEntry.Type
return e, modified
}
// IPNameEntry adds an Address to NameEntry
type IPNameEntry struct {
Addr Addr
NameEntry NameEntry
}
func (n IPNameEntry) FastLog(l *fastlog.Line) *fastlog.Line {
l.Struct(n.Addr)
l.Struct(n.NameEntry)
return l
}