forked from rainmeter/rainmeter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeasureWifiStatus.cpp
440 lines (392 loc) · 11.9 KB
/
MeasureWifiStatus.cpp
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/* Copyright (C) 2020 Rainmeter Project Developers
*
* This Source Code Form is subject to the terms of the GNU General Public
* License; either version 2 of the License, or (at your option) any later
* version. If a copy of the GPL was not distributed with this file, You can
* obtain one at <https://www.gnu.org/licenses/gpl-2.0.html>. */
#include "StdAfx.h"
#include "MeasureWifiStatus.h"
#include "Rainmeter.h"
#include "../Common/StringUtil.h"
UINT MeasureWifiStatus::s_Instances = 0U;
HANDLE MeasureWifiStatus::s_Client = nullptr;
PWLAN_INTERFACE_INFO MeasureWifiStatus::s_Interface = nullptr;
PWLAN_INTERFACE_INFO_LIST MeasureWifiStatus::s_InterfaceList = nullptr;
MeasureWifiStatus::MeasureWifiStatus(Skin* skin, const WCHAR* name) : Measure(skin, name),
m_Type(MeasureType::UNINITIALIZED),
m_ListStyle(0U),
m_ListMax(5U),
m_StatusString()
{
++s_Instances;
if (s_Instances == 1U)
{
// Temporarily load the "wlanapi.dll" library as a data file to test if it exists.
// Note: Freeing the temporary library should be okay since delay loading
// of the library occurs later (when WlanOpenHandle is called).
HMODULE lib = LoadLibraryEx(L"wlanapi.dll", nullptr,
LOAD_LIBRARY_AS_DATAFILE | LOAD_LIBRARY_AS_IMAGE_RESOURCE | LOAD_LIBRARY_SEARCH_SYSTEM32);
if (!lib)
{
LogDebugF(this, L"WifiStatus: Cannot find wlanapi.dll");
return;
}
FreeLibrary(lib);
// Create WINLAN API Handle
if (!s_Client)
{
DWORD dwNegotiatedVersion = 0UL;
DWORD dwErr = WlanOpenHandle(WLAN_API_VERSION, nullptr, &dwNegotiatedVersion, &s_Client);
if (ERROR_SUCCESS != dwErr)
{
FinalizeHandle();
LogDebugF(this,
L"WifiStatus: Unable to open WLAN API Handle. Error code (%u): %s",
dwErr, GetErrorString(dwErr));
return;
}
}
// Query list of WLAN interfaces
if (!s_InterfaceList)
{
DWORD dwErr = WlanEnumInterfaces(s_Client, nullptr, &s_InterfaceList);
if (ERROR_SUCCESS != dwErr)
{
FinalizeHandle();
LogDebugF(this,
L"WifiStatus: Unable to find any WLAN interfaces/adapters. Error code %u", dwErr);
return;
}
else if (s_InterfaceList->dwNumberOfItems == 0UL)
{
FinalizeHandle();
LogDebugF(this,
L"WifiStatus: No WLAN interfaces/adapters available.");
return;
}
}
}
}
MeasureWifiStatus::~MeasureWifiStatus()
{
if (s_Instances > 0U)
{
--s_Instances;
if (s_Instances == 0U)
{
FinalizeHandle();
}
}
}
void MeasureWifiStatus::ReadOptions(ConfigParser& parser, const WCHAR* section)
{
Measure::ReadOptions(parser, section);
if (!s_Client) return;
bool changed = false;
int value = parser.ReadInt(section, L"WifiIntfID", 0);
if (value >= (int)s_InterfaceList->dwNumberOfItems)
{
LogErrorF(this, L"WifiStatus: WifiIntfID=%i is not valid", value);
value = 0;
}
s_Interface = &s_InterfaceList->InterfaceInfo[value];
value = parser.ReadInt(section, L"WifiListStyle", 0);
if (value < 0 || value > 7)
{
LogErrorF(this, L"WifiStatus: WifiListStyle=%i is not valid", value);
value = 0;
}
m_ListStyle = value;
value = parser.ReadInt(section, L"WifiListLimit", 5);
if (value <= 0)
{
LogErrorF(this, L"WifiStatus: WifiListLimit=%i is not valid", value);
value = 5;
}
m_ListMax = value;
MeasureType infoType = MeasureType::UNKNOWN;
LPCWSTR type = parser.ReadString(section, L"WifiInfoType", L"").c_str();
if (_wcsicmp(L"SSID", type) == 0)
{
infoType = MeasureType::SSID;
}
else if (_wcsicmp(L"ENCRYPTION", type) == 0)
{
infoType = MeasureType::ENCRYPTION;
}
else if (_wcsicmp(L"AUTH", type) == 0)
{
infoType = MeasureType::AUTH;
}
else if (_wcsicmp(L"LIST", type) == 0)
{
infoType = MeasureType::LIST;
}
else if (_wcsicmp(L"PHY", type) == 0)
{
infoType = MeasureType::PHY;
}
else if (_wcsicmp(L"QUALITY", type) == 0)
{
infoType = MeasureType::QUALITY;
}
else if (_wcsicmp(L"TXRATE", type) == 0)
{
infoType = MeasureType::TXRATE;
}
else if (_wcsicmp(L"RXRATE", type) == 0)
{
infoType = MeasureType::RXRATE;
}
else
{
LogErrorF(this, L"WifiStatus: WifiInfoType=%s not valid", type);
}
if (infoType != m_Type)
{
changed = true;
}
m_Type = infoType;
if (changed)
{
m_StatusString.clear();
switch (infoType)
{
case MeasureType::SSID:
case MeasureType::ENCRYPTION:
case MeasureType::AUTH:
m_MaxValue = 0.0;
break;
case MeasureType::QUALITY:
m_MaxValue = 100.0;
break;
}
}
}
void MeasureWifiStatus::UpdateValue()
{
if (!s_Interface || m_Type == MeasureType::UNKNOWN)
{
m_Value = 0.0;
return;
}
if (m_Type == MeasureType::LIST)
{
PWLAN_AVAILABLE_NETWORK_LIST pwnl = nullptr;
DWORD dwErr =
WlanGetAvailableNetworkList(s_Client, &s_Interface->InterfaceGuid, 0UL, nullptr, &pwnl);
if (ERROR_SUCCESS != dwErr)
{
m_StatusString = L"Error";
}
else
{
// Size of network name can be up to 64 chars, set to 80 to add room for delimiters
m_StatusString.clear();
m_StatusString.reserve((size_t)m_ListMax * 80ULL);
UINT printed = 0U; // count of how many networks have been printed already
// Check all items in WLAN NETWORK LIST
for (size_t i = 0ULL; i < pwnl->dwNumberOfItems ; ++i)
{
if (printed == m_ListMax) break;
// SSID is in UCHAR, convert to WCHAR
std::wstring ssid = StringUtil::Widen(
(LPCSTR)pwnl->Network[i].dot11Ssid.ucSSID, (int)pwnl->Network[i].dot11Ssid.uSSIDLength);
// Prevent duplicates that result from profiles, check using SSID
if (!ssid.empty() && ssid[0] && wcsstr(m_StatusString.c_str(), ssid.c_str()) == nullptr)
{
++printed;
m_StatusString += ssid;
if (m_ListStyle > 0U)
{
if (m_ListStyle == 1U || m_ListStyle == 3U || m_ListStyle == 5U || m_ListStyle == 7U)
{
// ADD PHY type
m_StatusString += L" @";
m_StatusString += GetPHYString(pwnl->Network[i].dot11PhyTypes[0]);
}
if (m_ListStyle == 2U || m_ListStyle == 3U || m_ListStyle == 6U || m_ListStyle == 7U)
{
// ADD cipher and authentication
m_StatusString += L" (";
m_StatusString += GetCipherAlgorithmString(pwnl->Network[i].dot11DefaultCipherAlgorithm);
m_StatusString += L':';
m_StatusString += GetAuthAlgorithmString(pwnl->Network[i].dot11DefaultAuthAlgorithm);
m_StatusString += L')';
}
if (m_ListStyle == 4U || m_ListStyle == 5U || m_ListStyle == 6U || m_ListStyle == 7U)
{
// ADD signal quality
WCHAR buffer[32] = { 0 };
_snwprintf_s(buffer, _TRUNCATE, L"%lu", (ULONG)pwnl->Network[i].wlanSignalQuality);
m_StatusString += L" [";
m_StatusString += buffer;
m_StatusString += L']';
}
}
m_StatusString += L'\n';
}
}
WlanFreeMemory(pwnl);
}
}
else
{
ULONG outsize = 0UL;
PWLAN_CONNECTION_ATTRIBUTES wlan_cattr = nullptr;
DWORD dwErr = WlanQueryInterface(s_Client,
&s_Interface->InterfaceGuid, wlan_intf_opcode_current_connection, nullptr, &outsize, (PVOID*)&wlan_cattr, nullptr);
if (ERROR_SUCCESS != dwErr)
{
switch (m_Type)
{
case MeasureType::SSID:
case MeasureType::PHY:
case MeasureType::ENCRYPTION:
case MeasureType::AUTH:
m_StatusString = L"-1";
break;
case MeasureType::QUALITY:
case MeasureType::TXRATE:
case MeasureType::RXRATE:
m_Value = 0.0;
break;
}
}
else
{
switch (m_Type)
{
case MeasureType::SSID:
// Need to convert ucSSID to wchar from uchar
m_StatusString = StringUtil::Widen(
(LPCSTR)wlan_cattr->wlanAssociationAttributes.dot11Ssid.ucSSID,
(int)wlan_cattr->wlanAssociationAttributes.dot11Ssid.uSSIDLength);
// If not connected yet add current status
m_StatusString += GetInterfaceStateString(wlan_cattr->isState);
break;
case MeasureType::PHY:
m_StatusString = GetPHYString(wlan_cattr->wlanAssociationAttributes.dot11PhyType);
break;
case MeasureType::ENCRYPTION:
m_StatusString = GetCipherAlgorithmString(wlan_cattr->wlanSecurityAttributes.dot11CipherAlgorithm);
break;
case MeasureType::AUTH:
m_StatusString = GetAuthAlgorithmString(wlan_cattr->wlanSecurityAttributes.dot11AuthAlgorithm);
break;
case MeasureType::QUALITY:
m_Value = (double)wlan_cattr->wlanAssociationAttributes.wlanSignalQuality;
break;
case MeasureType::TXRATE:
m_Value = wlan_cattr->wlanAssociationAttributes.ulRxRate * 1000.0;
break;
case MeasureType::RXRATE:
m_Value = wlan_cattr->wlanAssociationAttributes.ulRxRate * 1000.0;
break;
default: // Invalid type
m_StatusString.clear();
break;
}
WlanFreeMemory(wlan_cattr);
}
}
}
const WCHAR* MeasureWifiStatus::GetStringValue()
{
if (!s_Interface) return nullptr;
switch (m_Type)
{
case MeasureType::LIST:
case MeasureType::SSID:
case MeasureType::PHY:
case MeasureType::ENCRYPTION:
case MeasureType::AUTH:
return !m_StatusString.empty() ? CheckSubstitute(m_StatusString.c_str()) : nullptr;
default:
return nullptr;
}
}
void MeasureWifiStatus::FinalizeHandle()
{
s_Interface = nullptr;
if (s_InterfaceList)
{
WlanFreeMemory(s_InterfaceList);
s_InterfaceList = nullptr;
}
if (s_Client)
{
WlanCloseHandle(s_Client, nullptr);
s_Client = nullptr;
}
}
const WCHAR* MeasureWifiStatus::GetCipherAlgorithmString(DOT11_CIPHER_ALGORITHM value)
{
switch (value)
{
case DOT11_CIPHER_ALGO_NONE: return L"NONE";
case DOT11_CIPHER_ALGO_WEP40: return L"WEP40";
case DOT11_CIPHER_ALGO_TKIP: return L"TKIP";
case DOT11_CIPHER_ALGO_CCMP: return L"AES";
case DOT11_CIPHER_ALGO_WEP104: return L"WEP104";
case DOT11_CIPHER_ALGO_BIP: return L"BIP";
case DOT11_CIPHER_ALGO_GCMP: return L"GCMP";
case DOT11_CIPHER_ALGO_WPA_USE_GROUP: return L"WPA-GROUP";
case DOT11_CIPHER_ALGO_WEP: return L"WEP";
default: return L"???";
}
}
const WCHAR* MeasureWifiStatus::GetAuthAlgorithmString(DOT11_AUTH_ALGORITHM value)
{
switch (value)
{
case DOT11_AUTH_ALGO_80211_OPEN: return L"Open";
case DOT11_AUTH_ALGO_80211_SHARED_KEY: return L"Shared";
case DOT11_AUTH_ALGO_WPA: return L"WPA-Enterprise";
case DOT11_AUTH_ALGO_WPA_PSK: return L"WPA-Personal";
case DOT11_AUTH_ALGO_WPA_NONE: return L"WPA-NONE";
case DOT11_AUTH_ALGO_RSNA: return L"WPA2-Enterprise";
case DOT11_AUTH_ALGO_RSNA_PSK: return L"WPA2-Personal";
case DOT11_AUTH_ALGO_WPA3: return L"WPA3-Enterprise";
case DOT11_AUTH_ALGO_WPA3_SAE: return L"WPA3-Personal";
default: return L"???";
}
}
const WCHAR* MeasureWifiStatus::GetInterfaceStateString(WLAN_INTERFACE_STATE value)
{
switch (value)
{
case wlan_interface_state_connected: return L"";
case wlan_interface_state_authenticating: return L"(authorizing...)";
default: return L"(connecting...)";
}
}
const WCHAR* MeasureWifiStatus::GetPHYString(DOT11_PHY_TYPE value)
{
switch (value)
{
case dot11_phy_type_fhss: return L"FHSS";
case dot11_phy_type_dsss: return L"DSSS";
case dot11_phy_type_irbaseband: return L"IR-Band";
case dot11_phy_type_ofdm: return L"802.11a";
case dot11_phy_type_hrdsss: return L"802.11b";
case dot11_phy_type_erp: return L"802.11g";
case dot11_phy_type_ht: return L"802.11n";
case dot11_phy_type_vht: return L"802.11ac";
case dot11_phy_type_dmg: return L"802.11ad";
case dot11_phy_type_he: return L"802.11ax";
case dot11_phy_type_eht: return L"802.11be";
default: return L"???";
}
}
const WCHAR* MeasureWifiStatus::GetErrorString(DWORD value)
{
switch (value)
{
case ERROR_INVALID_PARAMETER: return L"Invalid parameters";
case ERROR_NOT_ENOUGH_MEMORY: return L"Not enough memory";
case ERROR_REMOTE_SESSION_LIMIT_EXCEEDED: return L"Too many handles already issued";
case ERROR_SERVICE_NOT_ACTIVE: return L"WLAN Auto Config (WLANSVC) service is not active";
default: return L"Unknown error code";
}
}