-
Notifications
You must be signed in to change notification settings - Fork 1
/
netlink_presence.go
210 lines (178 loc) · 5.25 KB
/
netlink_presence.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
package wifioccupancy
import (
"errors"
"net"
"os"
"time"
"github.com/brutella/hc/log"
"github.com/hkwi/nlgo"
"github.com/mdlayher/netlink"
"github.com/mdlayher/netlink/genetlink"
. "github.com/deckarep/golang-set"
)
type NetlinkPresence struct {
watchedSet Set
currentSet Set
}
func NewNetlinkPresence(addresses Set) *NetlinkPresence {
return &NetlinkPresence{
watchedSet: addresses,
}
}
func (p *NetlinkPresence) Watch(monitor chan<- bool) error {
connection, err := genetlink.Dial(nil)
if err != nil {
return err
}
if family, err := connection.Family.Get(nlgo.NL80211_GENL_NAME); err != nil {
if os.IsNotExist(err) {
log.Info.Printf("%q family not available", nlgo.NL80211_GENL_NAME)
}
log.Info.Printf("failed to query for family: %v", err)
return err
} else {
log.Info.Printf("%s: %+v", nlgo.NL80211_GENL_NAME, family)
groups := p.getMulticastGroups(family.Groups)
group, ok := groups["mlme"]
if !ok {
return errors.New("mlme group is not found")
}
err := connection.JoinGroup(group.ID)
if err != nil {
log.Info.Println("Cannot join mlme")
return err
}
addresses, err := p.getAllStations(connection)
if err != nil {
return err
}
macAddresses := make([]interface{}, len(addresses))
for idx, value := range addresses {
macAddresses[idx] = value
}
p.currentSet = NewSet(macAddresses...)
monitor <- p.IsOccupied()
go p.ReceivingNetlinkEvent(connection, monitor)
go p.PollingStation(monitor)
}
return nil
}
func (p *NetlinkPresence) ReceivingNetlinkEvent(connection *genetlink.Conn, monitor chan<- bool) {
for {
messages, _, err := connection.Receive()
if err != nil {
log.Debug.Printf("failed to receive messages: %v", err)
}
for _, message := range messages {
header := message.Header
switch header.Command {
case nlgo.NL80211_CMD_DEL_STATION:
attrs, _ := p.getAttributes(message.Data)
station := net.HardwareAddr(attrs[nlgo.NL80211_ATTR_MAC].Data).String()
p.currentSet.Remove(station)
log.Info.Printf("%v is disconnected", station)
monitor <- p.IsOccupied()
case nlgo.NL80211_CMD_NEW_STATION:
attrs, _ := p.getAttributes(message.Data)
station := net.HardwareAddr(attrs[nlgo.NL80211_ATTR_MAC].Data).String()
p.currentSet.Add(station)
log.Info.Printf("%v is connected", station)
monitor <- p.IsOccupied()
}
}
}
}
func (p *NetlinkPresence) PollingStation(monitor chan<- bool) {
connection, err := genetlink.Dial(nil)
if err != nil {
log.Debug.Printf("Cannot polling because of error: %v", err)
return
}
// For device deauthenticated without disassociated
tickerCh := time.Tick(5 * time.Second)
for range tickerCh {
addresses, err := p.getAllStations(connection)
if err != nil {
continue
}
macAddresses := make([]interface{}, len(addresses))
for idx, value := range addresses {
macAddresses[idx] = value
}
p.currentSet = NewSet(macAddresses...)
monitor <- p.IsOccupied()
}
}
func (p *NetlinkPresence) IsOccupied() bool {
log.Debug.Println("Current: ", p.currentSet)
log.Debug.Println("Watched: ", p.watchedSet)
// TODO: Add option to check all/atleast one.
// Current rule is all
existing := p.currentSet.Intersect(p.watchedSet)
log.Debug.Printf("Existing: %v, %v, %v\n", existing, existing.Cardinality(), p.watchedSet.Cardinality())
isOccupied := existing.Cardinality() == p.watchedSet.Cardinality()
log.Debug.Println("isOccupied: ", isOccupied)
return isOccupied
}
func (p *NetlinkPresence) getMulticastGroups(groups []genetlink.MulticastGroup) map[string]genetlink.MulticastGroup {
groupMap := make(map[string]genetlink.MulticastGroup)
for _, group := range groups {
groupMap[group.Name] = group
}
return groupMap
}
func (p *NetlinkPresence) getAttributes(data []byte) (map[uint16]netlink.Attribute, error) {
if attrs, err := netlink.UnmarshalAttributes(data); err != nil {
return nil, err
} else {
attributes := make(map[uint16]netlink.Attribute)
for _, attr := range attrs {
attributes[attr.Type] = attr
}
return attributes, nil
}
}
func (p *NetlinkPresence) getAllStations(connection *genetlink.Conn) ([]string, error) {
if family, err := connection.Family.Get(nlgo.NL80211_GENL_NAME); err != nil {
if os.IsNotExist(err) {
log.Info.Printf("%q family not available", nlgo.NL80211_GENL_NAME)
}
log.Info.Printf("failed to query for family: %v", err)
return nil, err
} else {
req := genetlink.Message{
Header: genetlink.Header{
Command: nlgo.NL80211_CMD_GET_INTERFACE,
Version: family.Version,
},
}
flags := netlink.HeaderFlagsRequest | netlink.HeaderFlagsDump
msgs, err := connection.Execute(req, family.ID, flags)
if err != nil {
return nil, err
}
var stations []string
for _, msg := range msgs {
req = genetlink.Message{
Header: genetlink.Header{
Command: nlgo.NL80211_CMD_GET_STATION,
Version: family.Version,
},
Data: msg.Data,
}
msgs, err = connection.Execute(req, family.ID, flags)
if err != nil {
return nil, err
}
for _, msg := range msgs {
attrs, err := p.getAttributes(msg.Data)
if err != nil {
log.Info.Printf("failed to unmarshal attributes: %v", err)
return nil, err
}
stations = append(stations, net.HardwareAddr(attrs[nlgo.NL80211_ATTR_MAC].Data).String())
}
}
return stations, nil
}
}