From b5dd28d6c6bb39d5c86bb5928039394756d3d9f0 Mon Sep 17 00:00:00 2001 From: Simone Margaritelli Date: Fri, 8 Jan 2021 22:31:59 +0100 Subject: [PATCH] Revert "WiFi Network Locking Optimizations" --- network/wifi.go | 40 +++++++++++++++++---------------------- network/wifi_ap.go | 34 ++++++++++++++++----------------- network/wifi_handshake.go | 18 +++++++++--------- 3 files changed, 43 insertions(+), 49 deletions(-) diff --git a/network/wifi.go b/network/wifi.go index 21e3b634..0fc57d35 100644 --- a/network/wifi.go +++ b/network/wifi.go @@ -43,7 +43,7 @@ type APNewCallback func(ap *AccessPoint) type APLostCallback func(ap *AccessPoint) type WiFi struct { - sync.RWMutex + sync.Mutex aliases *data.UnsortedKV aps map[string]*AccessPoint @@ -67,12 +67,8 @@ func NewWiFi(iface *Endpoint, aliases *data.UnsortedKV, newcb APNewCallback, los } func (w *WiFi) MarshalJSON() ([]byte, error) { - w.RLock() - defer w.RUnlock() - doc := wifiJSON{ - // we know the length so preallocate to reduce memory allocations - AccessPoints: make([]*AccessPoint, 0, len(w.aps)), + AccessPoints: make([]*AccessPoint, 0), } for _, ap := range w.aps { @@ -92,11 +88,10 @@ func (w *WiFi) EachAccessPoint(cb func(mac string, ap *AccessPoint)) { } func (w *WiFi) Stations() (list []*Station) { - w.RLock() - defer w.RUnlock() - - list = make([]*Station, 0, len(w.aps)) + w.Lock() + defer w.Unlock() + list = make([]*Station, 0) for _, ap := range w.aps { list = append(list, ap.Station) } @@ -104,11 +99,10 @@ func (w *WiFi) Stations() (list []*Station) { } func (w *WiFi) List() (list []*AccessPoint) { - w.RLock() - defer w.RUnlock() - - list = make([]*AccessPoint, 0, len(w.aps)) + w.Lock() + defer w.Unlock() + list = make([]*AccessPoint, 0) for _, ap := range w.aps { list = append(list, ap) } @@ -173,8 +167,8 @@ func (w *WiFi) AddIfNew(ssid, mac string, frequency int, rssi int8) (*AccessPoin } func (w *WiFi) Get(mac string) (*AccessPoint, bool) { - w.RLock() - defer w.RUnlock() + w.Lock() + defer w.Unlock() mac = NormalizeMac(mac) ap, found := w.aps[mac] @@ -182,8 +176,8 @@ func (w *WiFi) Get(mac string) (*AccessPoint, bool) { } func (w *WiFi) GetClient(mac string) (*Station, bool) { - w.RLock() - defer w.RUnlock() + w.Lock() + defer w.Unlock() mac = NormalizeMac(mac) for _, ap := range w.aps { @@ -202,8 +196,8 @@ func (w *WiFi) Clear() { } func (w *WiFi) NumHandshakes() int { - w.RLock() - defer w.RUnlock() + w.Lock() + defer w.Unlock() sum := 0 for _, ap := range w.aps { @@ -218,6 +212,9 @@ func (w *WiFi) NumHandshakes() int { } func (w *WiFi) SaveHandshakesTo(fileName string, linkType layers.LinkType) error { + w.Lock() + defer w.Unlock() + // check if folder exists first dirName := filepath.Dir(fileName) if _, err := os.Stat(dirName); err != nil { @@ -241,9 +238,6 @@ func (w *WiFi) SaveHandshakesTo(fileName string, linkType layers.LinkType) error } } - w.RLock() - defer w.RUnlock() - for _, ap := range w.aps { for _, station := range ap.Clients() { // if half (which includes also complete) or has pmkid diff --git a/network/wifi_ap.go b/network/wifi_ap.go index ed17f681..58143cc0 100644 --- a/network/wifi_ap.go +++ b/network/wifi_ap.go @@ -10,7 +10,7 @@ import ( type AccessPoint struct { *Station - sync.RWMutex + sync.Mutex aliases *data.UnsortedKV clients map[string]*Station @@ -32,12 +32,12 @@ func NewAccessPoint(essid, bssid string, frequency int, rssi int8, aliases *data } func (ap *AccessPoint) MarshalJSON() ([]byte, error) { - ap.RLock() - defer ap.RUnlock() + ap.Lock() + defer ap.Unlock() doc := apJSON{ Station: ap.Station, - Clients: make([]*Station, 0, len(ap.clients)), + Clients: make([]*Station, 0), Handshake: ap.withKeyMaterial, } @@ -49,8 +49,8 @@ func (ap *AccessPoint) MarshalJSON() ([]byte, error) { } func (ap *AccessPoint) Get(bssid string) (*Station, bool) { - ap.RLock() - defer ap.RUnlock() + ap.Lock() + defer ap.Unlock() bssid = NormalizeMac(bssid) if s, found := ap.clients[bssid]; found { @@ -97,16 +97,16 @@ func (ap *AccessPoint) AddClientIfNew(bssid string, frequency int, rssi int8) (* } func (ap *AccessPoint) NumClients() int { - ap.RLock() - defer ap.RUnlock() + ap.Lock() + defer ap.Unlock() return len(ap.clients) } func (ap *AccessPoint) Clients() (list []*Station) { - ap.RLock() - defer ap.RUnlock() + ap.Lock() + defer ap.Unlock() - list = make([]*Station, 0, len(ap.clients)) + list = make([]*Station, 0) for _, c := range ap.clients { list = append(list, c) } @@ -130,15 +130,15 @@ func (ap *AccessPoint) WithKeyMaterial(state bool) { } func (ap *AccessPoint) HasKeyMaterial() bool { - ap.RLock() - defer ap.RUnlock() + ap.Lock() + defer ap.Unlock() return ap.withKeyMaterial } func (ap *AccessPoint) NumHandshakes() int { - ap.RLock() - defer ap.RUnlock() + ap.Lock() + defer ap.Unlock() sum := 0 @@ -156,8 +156,8 @@ func (ap *AccessPoint) HasHandshakes() bool { } func (ap *AccessPoint) HasPMKID() bool { - ap.RLock() - defer ap.RUnlock() + ap.Lock() + defer ap.Unlock() for _, c := range ap.clients { if c.Handshake.HasPMKID() { diff --git a/network/wifi_handshake.go b/network/wifi_handshake.go index 7181baec..341a4f9e 100644 --- a/network/wifi_handshake.go +++ b/network/wifi_handshake.go @@ -8,7 +8,7 @@ import ( ) type Handshake struct { - sync.RWMutex + sync.Mutex Beacon gopacket.Packet Challenges []gopacket.Packet @@ -80,8 +80,8 @@ func (h *Handshake) AddFrame(n int, pkt gopacket.Packet) { } func (h *Handshake) Complete() bool { - h.RLock() - defer h.RUnlock() + h.Lock() + defer h.Unlock() nChal := len(h.Challenges) nResp := len(h.Responses) @@ -91,8 +91,8 @@ func (h *Handshake) Complete() bool { } func (h *Handshake) Half() bool { - h.RLock() - defer h.RUnlock() + h.Lock() + defer h.Unlock() /* * You can use every combination of the handshake to crack the net: @@ -110,14 +110,14 @@ func (h *Handshake) Half() bool { } func (h *Handshake) HasPMKID() bool { - h.RLock() - defer h.RUnlock() + h.Lock() + defer h.Unlock() return h.hasPMKID } func (h *Handshake) NumUnsaved() int { - h.RLock() - defer h.RUnlock() + h.Lock() + defer h.Unlock() return len(h.unsaved) }