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
ash / system / diagnostics / networking_log.cc [blame]
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/system/diagnostics/networking_log.h"
#include <sstream>
#include <utility>
#include "base/check.h"
#include "base/check_is_test.h"
#include "base/containers/contains.h"
#include "base/i18n/time_formatting.h"
#include "base/notreached.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
namespace ash {
namespace diagnostics {
namespace {
const char kNewline[] = "\n";
// NetworkingInfo constants:
const char kNetworkingInfoSectionName[] = "--- Network Info ---";
const char kNetworkEventsSectionName[] = "--- Network Events ---";
const char kNetworkNameTitle[] = "Name: ";
const char kNetworkTypeTitle[] = "Type: ";
const char kNetworkStateTitle[] = "State: ";
const char kActiveTitle[] = "Active: ";
const char kMacAddressTitle[] = "MAC Address: ";
// CellularStateProperties constants:
const char kCellularIccidTitle[] = "ICCID: ";
const char kCellularEidTitle[] = "EID: ";
const char kCellularNetworkTechnologyTitle[] = "Technology: ";
const char kCellularRoamingTitle[] = "Roaming: ";
const char kCellularRoamingStateTitle[] = "Roaming State: ";
const char kCellularSignalStrengthTitle[] = "Signal Strength: ";
const char kCellularSimLockedTitle[] = "SIM Locked: ";
const char kCellularLockTypeTitle[] = "SIM Lock Type: ";
// EthernetStateProperties constants:
const char kEthernetAuthenticationTitle[] = "Authentication: ";
// WiFiStateProperties constants:
const char kWifiSignalStrengthTitle[] = "Signal Strength: ";
const char kWifiFrequencyTitle[] = "Frequency: ";
const char kWifiSsidTitle[] = "SSID: ";
const char kWifiBssidTitle[] = "BSSID: ";
const char kSecurityTitle[] = "Security: ";
// IpConfigProperties constants:
const char kNameServersTitle[] = "Name Servers: ";
const char kGatewayTitle[] = "Gateway: ";
const char kIPAddressTitle[] = "IP Address: ";
const char kSubnetMaskTitle[] = "Subnet Mask: ";
// Event log entries
const char kEventLogFilename[] = "network_events.log";
const char kNetworkAddedEventTemplate[] =
"%s network [%s] started in state %s\n";
const char kNetworkRemovedEventTemplate[] = "%s network [%s] removed\n";
const char kNetworkStateChangedEventTemplate[] =
"%s network [%s] changed state from %s to %s\n";
const char kJoinedWiFiEventTemplate[] =
"%s network [%s] joined SSID '%s' on access point [%s]\n";
const char kLeftWiFiEventTemplate[] = "%s network [%s] left SSID '%s'\n";
const char kAccessPointRoamingEventTemplate[] =
"%s network [%s] on SSID '%s' roamed from access point [%s] to [%s]\n";
std::string GetSubnetMask(int prefix) {
uint32_t mask = (0xFFFFFFFF << (32 - prefix)) & 0xFFFFFFFF;
std::vector<uint32_t> pieces = {mask >> 24, (mask >> 16) & 0xFF,
(mask >> 8) & 0xFF, mask & 0xFF};
std::vector<std::string> vec;
for (const auto& piece : pieces) {
vec.push_back(base::NumberToString(piece));
}
return base::JoinString(vec, ".");
}
std::string GetSecurityType(mojom::SecurityType type) {
switch (type) {
case mojom::SecurityType::kNone:
return "None";
case mojom::SecurityType::kWep8021x:
case mojom::SecurityType::kWpaEap:
return "EAP";
case mojom::SecurityType::kWepPsk:
return "WEP";
case mojom::SecurityType::kWpaPsk:
return "PSK (WPA or RSN)";
}
}
std::string GetSsid(const mojom::NetworkPtr& network) {
DCHECK(network->type == mojom::NetworkType::kWiFi);
return network->type_properties ? network->type_properties->get_wifi()->ssid
: "";
}
std::string GetBssid(const mojom::NetworkPtr& network) {
DCHECK(network->type == mojom::NetworkType::kWiFi);
return network->type_properties ? network->type_properties->get_wifi()->bssid
: "";
}
bool HasJoinedWiFiNetwork(const mojom::NetworkPtr& old_state,
const mojom::NetworkPtr& new_state) {
const std::string old_ssid = GetSsid(old_state);
return old_ssid.empty() && (old_ssid != GetSsid(new_state));
}
bool HasLeftWiFiNetwork(const mojom::NetworkPtr& old_state,
const mojom::NetworkPtr& new_state) {
const std::string new_ssid = GetSsid(new_state);
return new_ssid.empty() && (new_ssid != GetSsid(old_state));
}
bool HasRoamedAccessPoint(const mojom::NetworkPtr& old_state,
const mojom::NetworkPtr& new_state) {
const std::string new_ssid = GetSsid(new_state);
return !new_ssid.empty() && (new_ssid == GetSsid(old_state)) &&
(GetBssid(old_state) != GetBssid(new_state));
}
void AddWifiInfoToLog(const mojom::NetworkTypeProperties& type_props,
std::stringstream& output) {
output << kWifiSignalStrengthTitle << type_props.get_wifi()->signal_strength
<< kNewline << kWifiFrequencyTitle << type_props.get_wifi()->frequency
<< kNewline << kWifiSsidTitle << type_props.get_wifi()->ssid
<< kNewline << kWifiBssidTitle << type_props.get_wifi()->bssid
<< kNewline << kSecurityTitle
<< GetSecurityType(type_props.get_wifi()->security) << kNewline;
}
std::string GetBoolAsString(bool value) {
return value ? "True" : "False";
}
std::string GetCellularRoamingState(mojom::RoamingState state) {
switch (state) {
case mojom::RoamingState::kNone:
return "None";
case mojom::RoamingState::kHome:
return "Home";
case mojom::RoamingState::kRoaming:
return "Roaming";
}
}
std::string GetCellularLockType(mojom::LockType lock_type) {
switch (lock_type) {
case mojom::LockType::kNone:
return "None";
case mojom::LockType::kSimPin:
return "sim-pin";
case mojom::LockType::kSimPuk:
return "sim-puk";
case mojom::LockType::kNetworkPin:
return "network-pin";
}
}
void AddCellularInfoToLog(const mojom::NetworkTypeProperties& type_props,
std::stringstream& output) {
output << kCellularIccidTitle << type_props.get_cellular()->iccid << kNewline
<< kCellularEidTitle << type_props.get_cellular()->eid << kNewline
<< kCellularNetworkTechnologyTitle
<< type_props.get_cellular()->network_technology << kNewline
<< kCellularRoamingTitle
<< GetBoolAsString(type_props.get_cellular()->roaming) << kNewline
<< kCellularRoamingStateTitle
<< GetCellularRoamingState(type_props.get_cellular()->roaming_state)
<< kNewline << kCellularSignalStrengthTitle
<< type_props.get_cellular()->signal_strength << kNewline
<< kCellularSimLockedTitle
<< GetBoolAsString(type_props.get_cellular()->sim_locked) << kNewline
<< kCellularLockTypeTitle
<< GetCellularLockType(type_props.get_cellular()->lock_type)
<< kNewline;
}
std::string GetEthernetAuthenticationType(mojom::AuthenticationType type) {
switch (type) {
case mojom::AuthenticationType::kNone:
return "None";
case mojom::AuthenticationType::k8021x:
return "EAP";
}
}
void AddEthernetInfoToLog(const mojom::NetworkTypeProperties& type_props,
std::stringstream& output) {
output << kEthernetAuthenticationTitle
<< GetEthernetAuthenticationType(
type_props.get_ethernet()->authentication)
<< kNewline;
}
void AddTypePropertiesToLog(const mojom::NetworkTypeProperties& type_props,
mojom::NetworkType type,
std::stringstream& output) {
switch (type) {
case mojom::NetworkType::kWiFi:
AddWifiInfoToLog(type_props, output);
break;
case mojom::NetworkType::kCellular:
AddCellularInfoToLog(type_props, output);
break;
case mojom::NetworkType::kEthernet:
AddEthernetInfoToLog(type_props, output);
break;
case mojom::NetworkType::kUnsupported:
NOTREACHED();
}
}
void AddIPConfigPropertiesToLog(const mojom::IPConfigProperties& ip_config,
std::stringstream& output) {
output << kGatewayTitle << ip_config.gateway.value_or("") << kNewline;
output << kIPAddressTitle << ip_config.ip_address.value_or("") << kNewline;
auto name_servers = base::JoinString(
ip_config.name_servers.value_or(std::vector<std::string>()), ", ");
output << kNameServersTitle << name_servers << kNewline;
// A routing prefix can not be 0, 0 indicates an unset value.
auto subnet_mask = ip_config.routing_prefix != 0
? GetSubnetMask(ip_config.routing_prefix)
: "";
output << kSubnetMaskTitle << subnet_mask << kNewline;
}
std::string GetNetworkStateString(mojom::NetworkState state) {
switch (state) {
case mojom::NetworkState::kOnline:
return "Online";
case mojom::NetworkState::kConnected:
return "Connected";
case mojom::NetworkState::kConnecting:
return "Connecting";
case mojom::NetworkState::kNotConnected:
return "Not Connected";
case mojom::NetworkState::kDisabled:
return "Disabled";
case mojom::NetworkState::kPortal:
return "Portal";
}
}
std::string GetNetworkType(mojom::NetworkType type) {
switch (type) {
case mojom::NetworkType::kWiFi:
return "WiFi";
case mojom::NetworkType::kCellular:
return "Cellular";
case mojom::NetworkType::kEthernet:
return "Ethernet";
case mojom::NetworkType::kUnsupported:
NOTREACHED();
}
}
} // namespace
NetworkingLog::NetworkingLog(const base::FilePath& log_base_path)
: event_log_(log_base_path.Append(kEventLogFilename)) {}
NetworkingLog::~NetworkingLog() = default;
std::string NetworkingLog::GetNetworkInfo() const {
std::stringstream output;
output << kNetworkingInfoSectionName << kNewline;
for (const auto& pair : latest_network_states_) {
const mojom::NetworkPtr& network = pair.second;
output << kNewline << kNetworkNameTitle << network->name << kNewline
<< kNetworkTypeTitle << GetNetworkType(network->type) << kNewline
<< kNetworkStateTitle << GetNetworkStateString(network->state)
<< kNewline << kActiveTitle
<< (network->observer_guid == active_guid_ ? "True" : "False")
<< kNewline << kMacAddressTitle << network->mac_address.value_or("")
<< kNewline;
if (network->type_properties) {
AddTypePropertiesToLog(*network->type_properties, network->type, output);
}
if (network->ip_config) {
AddIPConfigPropertiesToLog(*network->ip_config, output);
}
}
return output.str();
}
std::string NetworkingLog::GetNetworkEvents() const {
return std::string(kNetworkEventsSectionName) + kNewline + kNewline +
event_log_.GetContents();
}
void NetworkingLog::UpdateNetworkList(
const std::vector<std::string>& observer_guids,
std::string active_guid) {
// If a network is no longer valid, remove it from the map.
for (auto iter = latest_network_states_.begin();
iter != latest_network_states_.end();) {
if (!base::Contains(observer_guids, iter->first)) {
LogNetworkRemoved(iter->second);
iter = latest_network_states_.erase(iter);
continue;
}
++iter;
}
active_guid_ = active_guid;
++update_network_list_call_count_for_testing_;
}
void NetworkingLog::UpdateNetworkState(mojom::NetworkPtr network) {
if (network.is_null()) {
LOG(ERROR) << "Network to log update is null";
return;
}
if (!base::Contains(latest_network_states_, network->observer_guid)) {
LogNetworkAdded(network);
latest_network_states_.emplace(network->observer_guid, std::move(network));
return;
}
LogNetworkChanges(network);
latest_network_states_[network->observer_guid] = std::move(network);
}
void NetworkingLog::LogEvent(const std::string& event_string) {
const std::string datetime =
base::UTF16ToUTF8(base::TimeFormatShortDateAndTime(base::Time::Now()));
event_log_.Append(datetime + " - " + event_string);
}
void NetworkingLog::LogNetworkAdded(const mojom::NetworkPtr& network) {
const std::string line = base::StringPrintf(
kNetworkAddedEventTemplate, GetNetworkType(network->type).c_str(),
network->mac_address.value_or("").c_str(),
GetNetworkStateString(network->state).c_str());
LogEvent(line);
}
void NetworkingLog::LogNetworkRemoved(const mojom::NetworkPtr& network) {
if (network.is_null()) {
LOG(ERROR) << "Network to log removal is null";
return;
}
const std::string line = base::StringPrintf(
kNetworkRemovedEventTemplate, GetNetworkType(network->type).c_str(),
network->mac_address.value_or("").c_str());
LogEvent(line);
}
void NetworkingLog::LogNetworkChanges(const mojom::NetworkPtr& new_state) {
DCHECK(base::Contains(latest_network_states_, new_state->observer_guid));
const mojom::NetworkPtr& old_state =
latest_network_states_.at(new_state->observer_guid);
if (new_state->type == mojom::NetworkType::kWiFi) {
if (HasJoinedWiFiNetwork(old_state, new_state)) {
LogJoinedWiFiNetwork(new_state);
} else if (HasLeftWiFiNetwork(old_state, new_state)) {
LogLeftWiFiNetwork(new_state, GetSsid(old_state));
} else if (HasRoamedAccessPoint(old_state, new_state)) {
LogWiFiRoamedAccessPoint(new_state, GetBssid(old_state));
}
}
if (old_state->state != new_state->state) {
LogNetworkStateChanged(old_state, new_state);
}
}
void NetworkingLog::LogNetworkStateChanged(const mojom::NetworkPtr& old_state,
const mojom::NetworkPtr& new_state) {
const std::string line =
base::StringPrintf(kNetworkStateChangedEventTemplate,
GetNetworkType(new_state->type).c_str(),
new_state->mac_address.value_or("").c_str(),
GetNetworkStateString(old_state->state).c_str(),
GetNetworkStateString(new_state->state).c_str());
LogEvent(line);
}
void NetworkingLog::LogJoinedWiFiNetwork(const mojom::NetworkPtr& network) {
const std::string line = base::StringPrintf(
kJoinedWiFiEventTemplate, GetNetworkType(network->type).c_str(),
network->mac_address.value_or("").c_str(), GetSsid(network).c_str(),
GetBssid(network).c_str());
LogEvent(line);
}
void NetworkingLog::LogLeftWiFiNetwork(const mojom::NetworkPtr& network,
const std::string& old_ssid) {
const std::string line = base::StringPrintf(
kLeftWiFiEventTemplate, GetNetworkType(network->type).c_str(),
network->mac_address.value_or("").c_str(), old_ssid.c_str());
LogEvent(line);
}
void NetworkingLog::LogWiFiRoamedAccessPoint(const mojom::NetworkPtr& network,
const std::string& old_bssid) {
const std::string line = base::StringPrintf(
kAccessPointRoamingEventTemplate, GetNetworkType(network->type).c_str(),
network->mac_address.value_or("").c_str(), GetSsid(network).c_str(),
old_bssid.c_str(), GetBssid(network).c_str());
LogEvent(line);
}
size_t NetworkingLog::update_network_list_call_count_for_testing() const {
CHECK_IS_TEST();
return update_network_list_call_count_for_testing_;
}
} // namespace diagnostics
} // namespace ash