-
Notifications
You must be signed in to change notification settings - Fork 6
/
NetworkManagerGnomeUtils.cpp
317 lines (291 loc) · 12.9 KB
/
NetworkManagerGnomeUtils.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
#include <stdlib.h>
#include <stdio.h>
#include <glib.h>
#include <thread>
#include <string>
#include <map>
#include <NetworkManager.h>
#include <libnm/NetworkManager.h>
#include "Module.h"
#include "NetworkManagerLogger.h"
#include "NetworkManagerGnomeUtils.h"
#include "NetworkManagerImplementation.h"
#include "INetworkManager.h"
namespace WPEFramework
{
namespace Plugin
{
static const char* ifnameEth = "eth0";
static const char* ifnameWlan = "wlan0";
uint8_t nmUtils::wifiSecurityModeFromAp(guint32 flags, guint32 wpaFlags, guint32 rsnFlags)
{
uint8_t security = Exchange::INetworkManager::WIFI_SECURITY_NONE;
if ((flags == NM_802_11_AP_FLAGS_NONE) && (wpaFlags == NM_802_11_AP_SEC_NONE) && (rsnFlags == NM_802_11_AP_SEC_NONE))
{
security = Exchange::INetworkManager::WIFISecurityMode::WIFI_SECURITY_NONE;
}
else if( (flags & NM_802_11_AP_FLAGS_PRIVACY) && ((wpaFlags & NM_802_11_AP_SEC_PAIR_WEP40) || (rsnFlags & NM_802_11_AP_SEC_PAIR_WEP40)) )
{
security = Exchange::INetworkManager::WIFISecurityMode::WIFI_SECURITY_WEP_64;
}
else if( (flags & NM_802_11_AP_FLAGS_PRIVACY) && ((wpaFlags & NM_802_11_AP_SEC_PAIR_WEP104) || (rsnFlags & NM_802_11_AP_SEC_PAIR_WEP104)) )
{
security = Exchange::INetworkManager::WIFISecurityMode::WIFI_SECURITY_WEP_128;
}
else if((wpaFlags & NM_802_11_AP_SEC_PAIR_TKIP) || (rsnFlags & NM_802_11_AP_SEC_PAIR_TKIP))
{
security = Exchange::INetworkManager::WIFISecurityMode::WIFI_SECURITY_WPA_PSK_TKIP;
}
else if((wpaFlags & NM_802_11_AP_SEC_PAIR_CCMP) || (rsnFlags & NM_802_11_AP_SEC_PAIR_CCMP))
{
security = Exchange::INetworkManager::WIFISecurityMode::WIFI_SECURITY_WPA_PSK_AES;
}
else if ((rsnFlags & NM_802_11_AP_SEC_KEY_MGMT_PSK) && (rsnFlags & NM_802_11_AP_SEC_KEY_MGMT_802_1X))
{
security = Exchange::INetworkManager::WIFISecurityMode::WIFI_SECURITY_WPA_WPA2_ENTERPRISE;
}
else if(rsnFlags & NM_802_11_AP_SEC_KEY_MGMT_PSK)
{
security = Exchange::INetworkManager::WIFISecurityMode::WIFI_SECURITY_WPA_WPA2_PSK;
}
else if((wpaFlags & NM_802_11_AP_SEC_GROUP_CCMP) || (rsnFlags & NM_802_11_AP_SEC_GROUP_CCMP))
{
security = Exchange::INetworkManager::WIFISecurityMode::WIFI_SECURITY_WPA2_PSK_AES;
}
else if((wpaFlags & NM_802_11_AP_SEC_GROUP_TKIP) || (rsnFlags & NM_802_11_AP_SEC_GROUP_TKIP))
{
security = Exchange::INetworkManager::WIFISecurityMode::WIFI_SECURITY_WPA2_PSK_TKIP;
}
else if((rsnFlags & NM_802_11_AP_SEC_KEY_MGMT_OWE) || (rsnFlags & NM_802_11_AP_SEC_KEY_MGMT_OWE_TM))
{
security = Exchange::INetworkManager::WIFISecurityMode::WIFI_SECURITY_WPA3_SAE;
}
else
NMLOG_WARNING("security mode not defined (flag: %d, wpaFlags: %d, rsnFlags: %d)", flags, wpaFlags, rsnFlags);
return security;
}
std::string nmUtils::getSecurityModeString(guint32 flags, guint32 wpaFlags, guint32 rsnFlags)
{
switch(flags)
{
case NM_802_11_AP_FLAGS_NONE:
NMLOG_DEBUG("ap type : point has no special capabilities");
break;
case NM_802_11_AP_FLAGS_PRIVACY:
NMLOG_DEBUG("ap type : access point requires authentication and encryption");
break;
case NM_802_11_AP_FLAGS_WPS:
NMLOG_DEBUG("ap type : access point supports some WPS method");
break;
case NM_802_11_AP_FLAGS_WPS_PBC:
NMLOG_DEBUG("ap type : access point supports push-button WPS");
break;
case NM_802_11_AP_FLAGS_WPS_PIN:
NMLOG_DEBUG("ap type : access point supports PIN-based WPS");
break;
default:
NMLOG_ERROR("ap type : 802.11 flags unknown!");
}
std::string securityStr;
if (!(flags & NM_802_11_AP_FLAGS_PRIVACY) && (wpaFlags != NM_802_11_AP_SEC_NONE) && (rsnFlags != NM_802_11_AP_SEC_NONE))
securityStr += ("Encrypted: ");
if ((flags & NM_802_11_AP_FLAGS_PRIVACY) && (wpaFlags == NM_802_11_AP_SEC_NONE)
&& (rsnFlags == NM_802_11_AP_SEC_NONE))
securityStr += ("WEP ");
if (wpaFlags != NM_802_11_AP_SEC_NONE)
securityStr += ("WPA ");
if ((rsnFlags & NM_802_11_AP_SEC_KEY_MGMT_PSK)
|| (rsnFlags & NM_802_11_AP_SEC_KEY_MGMT_802_1X)) {
securityStr += ("WPA2 ");
}
if (rsnFlags & NM_802_11_AP_SEC_KEY_MGMT_SAE) {
securityStr += ("WPA3 ");
}
if ((rsnFlags & NM_802_11_AP_SEC_KEY_MGMT_OWE)
|| (rsnFlags & NM_802_11_AP_SEC_KEY_MGMT_OWE_TM)) {
securityStr += ("OWE ");
}
if ((wpaFlags & NM_802_11_AP_SEC_KEY_MGMT_802_1X)
|| (rsnFlags & NM_802_11_AP_SEC_KEY_MGMT_802_1X)) {
securityStr += ("802.1X ");
}
if (securityStr.empty())
{
securityStr = "None";
return securityStr;
}
uint32_t flags[2] = { wpaFlags, rsnFlags };
securityStr += "[ WPA Flags: ";
for (int i = 0; i < 2; ++i)
{
if (flags[i] & NM_802_11_AP_SEC_PAIR_WEP40)
securityStr += "pair_wep40 ";
if (flags[i] & NM_802_11_AP_SEC_PAIR_WEP104)
securityStr += "pair_wep104 ";
if (flags[i] & NM_802_11_AP_SEC_PAIR_TKIP)
securityStr += "pair_tkip ";
if (flags[i] & NM_802_11_AP_SEC_PAIR_CCMP)
securityStr += "pair_ccmp ";
if (flags[i] & NM_802_11_AP_SEC_GROUP_WEP40)
securityStr += "group_wep40 ";
if (flags[i] & NM_802_11_AP_SEC_GROUP_WEP104)
securityStr += "group_wep104 ";
if (flags[i] & NM_802_11_AP_SEC_GROUP_TKIP)
securityStr += "group_tkip ";
if (flags[i] & NM_802_11_AP_SEC_GROUP_CCMP)
securityStr += "group_ccmp ";
if (flags[i] & NM_802_11_AP_SEC_KEY_MGMT_PSK)
securityStr += "psk ";
if (flags[i] & NM_802_11_AP_SEC_KEY_MGMT_802_1X)
securityStr += "802.1X ";
if (flags[i] & NM_802_11_AP_SEC_KEY_MGMT_SAE)
securityStr += "sae ";
if (flags[i] & NM_802_11_AP_SEC_KEY_MGMT_OWE)
securityStr += "owe ";
if (flags[i] & NM_802_11_AP_SEC_KEY_MGMT_OWE_TM)
securityStr += "owe_transition_mode ";
if (flags[i] & NM_802_11_AP_SEC_KEY_MGMT_EAP_SUITE_B_192)
securityStr += "wpa-eap-suite-b-192 ";
if (i == 0) {
securityStr += "] [ RSN Flags: ";
}
}
securityStr +="]";
return securityStr;
}
std::string nmUtils::wifiFrequencyFromAp(guint32 apFreq)
{
std:string freq;
if (apFreq >= 2400 && apFreq < 5000)
freq = "2.4";
else if (apFreq >= 5000 && apFreq < 6000)
freq = "5";
else if (apFreq >= 6000)
freq = "6";
else
freq = "Not available";
return freq;
}
JsonObject nmUtils::apToJsonObject(NMAccessPoint *ap)
{
GError *error = NULL;
GBytes *ssid = NULL;
int strength = 0;
std::string freq;
int security;
guint32 flags, wpaFlags, rsnFlags, apFreq;
JsonObject ssidObj;
if(ap == nullptr)
return ssidObj;
ssid = nm_access_point_get_ssid(ap);
if (ssid)
{
char *ssidStr = nullptr;
ssidStr = nm_utils_ssid_to_utf8((const guint8*)g_bytes_get_data(ssid, NULL), g_bytes_get_size(ssid));
string ssidString(ssidStr);
ssidObj["ssid"] = ssidString;
}
else
ssidObj["ssid"] = "---"; // hidden ssid TODO modify
strength = nm_access_point_get_strength(ap);
apFreq = nm_access_point_get_frequency(ap);
flags = nm_access_point_get_flags(ap);
wpaFlags = nm_access_point_get_wpa_flags(ap);
rsnFlags = nm_access_point_get_rsn_flags(ap);
freq = nmUtils::wifiFrequencyFromAp(apFreq);
security = nmUtils::wifiSecurityModeFromAp(flags, wpaFlags, rsnFlags);
ssidObj["security"] = security;
ssidObj["signalStrength"] = strength;
ssidObj["frequency"] = freq;
return ssidObj;
}
void nmUtils::printActiveSSIDsOnly(NMDeviceWifi *wifiDevice)
{
if(!NM_IS_DEVICE_WIFI(wifiDevice))
{
NMLOG_ERROR("Not a wifi object ");
return;
}
const GPtrArray *accessPointsArray = nm_device_wifi_get_access_points(wifiDevice);
for (guint i = 0; i < accessPointsArray->len; i++)
{
NMAccessPoint *ap = NULL;
GBytes *ssidGByte = NULL;
std::string ssid;
ap = (NMAccessPoint*)accessPointsArray->pdata[i];
ssidGByte = nm_access_point_get_ssid(ap);
if(ssidGByte)
{
char* ssidStr = NULL;
gsize len;
const guint8 *ssidData = static_cast<const guint8 *>(g_bytes_get_data(ssidGByte, &len));
ssidStr = nm_utils_ssid_to_utf8(ssidData, len);
if(ssidStr != NULL) {
std::string ssidTmp(ssidStr, len);
ssid = ssidTmp;
}
else
ssid = "---";
}
else
ssid = "---";
NMLOG_INFO("ssid: %s", ssid.c_str());
}
}
bool nmUtils::caseInsensitiveCompare(const std::string& str1, const std::string& str2) {
std::string upperStr1 = str1;
std::string upperStr2 = str2;
// Convert both strings to uppercase
std::transform(upperStr1.begin(), upperStr1.end(), upperStr1.begin(), ::toupper);
std::transform(upperStr2.begin(), upperStr2.end(), upperStr2.begin(), ::toupper);
return upperStr1 == upperStr2;
}
bool nmUtils::GetInterfacesName(std::string &wifiIfname ,std::string ðIfname)
{
std::string line;
static bool fileParsingCompleted = false;
static std::string m_wifiIfname;
static std::string m_ethIfname; // cached interface name
wifiIfname.clear();
ethIfname.clear();
if(fileParsingCompleted)
{
if(!m_wifiIfname.empty())
wifiIfname = m_wifiIfname;
if(!m_ethIfname.empty())
ethIfname = m_ethIfname;
return true;
}
std::ifstream file("/etc/device.properties");
if (!file.is_open()) {
NMLOG_FATAL("/etc/device.properties opening file Error");
return false;
}
while (std::getline(file, line))
{
if (line.empty()) {
continue;
}
if (line.find("ETHERNET_INTERFACE=") != std::string::npos) {
ethIfname = line.substr(line.find('=') + 1);
ethIfname.erase(ethIfname.find_last_not_of("\r\n\t") + 1);
ethIfname.erase(0, ethIfname.find_first_not_of("\r\n\t"));
}
if (line.find("WIFI_INTERFACE=") != std::string::npos) {
wifiIfname = line.substr(line.find('=') + 1);
wifiIfname.erase(wifiIfname.find_last_not_of("\r\n\t") + 1);
wifiIfname.erase(0, wifiIfname.find_first_not_of("\r\n\t"));
}
}
file.close();
if (ethIfname.empty() && wifiIfname.empty()) {
NMLOG_FATAL("Could not find any interface name in /etc/device.properties");
return false;
}
m_wifiIfname = wifiIfname;
m_ethIfname = ethIfname;
fileParsingCompleted = true;
return true;
}
} // Plugin
} // WPEFramework