File: network_util.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (305 lines) | stat: -rw-r--r-- 10,266 bytes parent folder | download | duplicates (8)
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
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/network/network_util.h"

#include <stddef.h>
#include <stdint.h>

#include <utility>

#include "base/strings/string_number_conversions.h"
#include "base/strings/string_tokenizer.h"
#include "base/strings/string_util.h"
#include "base/values.h"
#include "chromeos/ash/components/login/login_state/login_state.h"
#include "chromeos/ash/components/network/device_state.h"
#include "chromeos/ash/components/network/managed_network_configuration_handler.h"
#include "chromeos/ash/components/network/network_state.h"
#include "chromeos/ash/components/network/network_state_handler.h"
#include "chromeos/ash/components/network/network_ui_data.h"
#include "chromeos/ash/components/network/onc/onc_translation_tables.h"
#include "chromeos/ash/components/network/onc/onc_translator.h"
#include "chromeos/components/onc/onc_signature.h"
#include "components/device_event_log/device_event_log.h"
#include "third_party/cros_system_api/dbus/service_constants.h"

namespace ash {

WifiAccessPoint::WifiAccessPoint()
    : signal_strength(0),
      signal_to_noise(0),
      channel(0) {
}

WifiAccessPoint::WifiAccessPoint(const WifiAccessPoint& other) = default;

WifiAccessPoint::~WifiAccessPoint() = default;

CellTower::CellTower() = default;

CellTower::CellTower(const CellTower& other) = default;

CellTower::~CellTower() = default;

CellularScanResult::CellularScanResult() = default;

CellularScanResult::CellularScanResult(const CellularScanResult& other) =
    default;

CellularScanResult::~CellularScanResult() = default;

CellularSIMSlotInfo::CellularSIMSlotInfo() = default;

CellularSIMSlotInfo::CellularSIMSlotInfo(const CellularSIMSlotInfo& other) =
    default;

CellularSIMSlotInfo::~CellularSIMSlotInfo() = default;

namespace network_util {

std::string PrefixLengthToNetmask(int32_t prefix_length) {
  std::string netmask;
  // Return the empty string for invalid inputs.
  if (prefix_length < 0 || prefix_length > 32)
    return netmask;
  for (int i = 0; i < 4; i++) {
    int remainder = 8;
    if (prefix_length >= 8) {
      prefix_length -= 8;
    } else {
      remainder = prefix_length;
      prefix_length = 0;
    }
    if (i > 0)
      netmask += ".";
    int value = remainder == 0 ? 0 :
        ((2L << (remainder - 1)) - 1) << (8 - remainder);
    netmask += base::NumberToString(value);
  }
  return netmask;
}

int32_t NetmaskToPrefixLength(const std::string& netmask) {
  int count = 0;
  int prefix_length = 0;
  base::StringTokenizer t(netmask, ".");
  while (t.GetNext()) {
    // If there are more than 4 numbers, then it's invalid.
    if (count == 4)
      return -1;

    std::string token = t.token();
    // If we already found the last mask and the current one is not
    // "0" then the netmask is invalid. For example, 255.224.255.0
    if (prefix_length / 8 != count) {
      if (token != "0")
        return -1;
    } else if (token == "255") {
      prefix_length += 8;
    } else if (token == "254") {
      prefix_length += 7;
    } else if (token == "252") {
      prefix_length += 6;
    } else if (token == "248") {
      prefix_length += 5;
    } else if (token == "240") {
      prefix_length += 4;
    } else if (token == "224") {
      prefix_length += 3;
    } else if (token == "192") {
      prefix_length += 2;
    } else if (token == "128") {
      prefix_length += 1;
    } else if (token == "0") {
      prefix_length += 0;
    } else {
      // mask is not a valid number.
      return -1;
    }
    count++;
  }
  if (count < 4)
    return -1;
  return prefix_length;
}

std::string FormattedMacAddress(const std::string& shill_mac_address) {
  if (shill_mac_address.size() % 2 != 0)
    return shill_mac_address;
  std::string result;
  for (size_t i = 0; i < shill_mac_address.size(); ++i) {
    if ((i != 0) && (i % 2 == 0))
      result.push_back(':');
    result.push_back(base::ToUpperASCII(shill_mac_address[i]));
  }
  return result;
}

bool ParseCellularScanResults(const base::Value::List& list,
                              std::vector<CellularScanResult>* scan_results) {
  scan_results->clear();
  scan_results->reserve(list.size());
  for (const auto& value : list) {
    const base::Value::Dict* value_dict = value.GetIfDict();
    if (!value_dict) {
      return false;
    }

    CellularScanResult scan_result;
    // If the network id property is not present then this network cannot be
    // connected to so don't include it in the results.
    const std::string* network_id =
        value_dict->FindString(shill::kNetworkIdProperty);
    if (!network_id) {
      continue;
    }
    scan_result.network_id = *network_id;
    const std::string* status = value_dict->FindString(shill::kStatusProperty);
    if (status) {
      scan_result.status = *status;
    }
    const std::string* long_name =
        value_dict->FindString(shill::kLongNameProperty);
    if (long_name) {
      scan_result.long_name = *long_name;
    }
    const std::string* short_name =
        value_dict->FindString(shill::kShortNameProperty);
    if (short_name) {
      scan_result.short_name = *short_name;
    }
    const std::string* technology =
        value_dict->FindString(shill::kTechnologyProperty);
    if (technology) {
      scan_result.technology = *technology;
    }
    scan_results->push_back(scan_result);
  }
  return true;
}

bool ParseCellularSIMSlotInfo(
    const base::Value::List& list,
    std::vector<CellularSIMSlotInfo>* sim_slot_infos) {
  sim_slot_infos->clear();
  sim_slot_infos->reserve(list.size());
  for (size_t i = 0; i < list.size(); i++) {
    const base::Value::Dict* value = list[i].GetIfDict();
    if (!value) {
      return false;
    }

    CellularSIMSlotInfo sim_slot_info;
    // The |slot_id| should start with 1.
    sim_slot_info.slot_id = i + 1;

    const std::string* eid = value->FindString(shill::kSIMSlotInfoEID);
    if (eid) {
      sim_slot_info.eid = *eid;
    }

    const std::string* iccid = value->FindString(shill::kSIMSlotInfoICCID);
    if (iccid) {
      sim_slot_info.iccid = *iccid;
    }

    std::optional<bool> primary = value->FindBool(shill::kSIMSlotInfoPrimary);
    sim_slot_info.primary = primary.has_value() ? *primary : false;

    sim_slot_infos->push_back(sim_slot_info);
  }
  return true;
}

base::Value::Dict TranslateNetworkStateToONC(const NetworkState* network) {
  // Get the properties from the NetworkState.
  base::Value::Dict shill_dictionary;
  network->GetStateProperties(&shill_dictionary);

  // Get any Device properties required to translate state.
  if (NetworkTypePattern::Cellular().MatchesType(network->type())) {
    const DeviceState* device =
        NetworkHandler::Get()->network_state_handler()->GetDeviceState(
            network->device_path());
    if (device) {
      shill_dictionary.Set(
          shill::kDeviceProperty,
          base::Value::Dict()
              // We need to set Device.Cellular.ProviderRequiresRoaming so that
              // Cellular.RoamingState can be set correctly for badging network
              // icons.
              .Set(shill::kProviderRequiresRoamingProperty,
                   device->provider_requires_roaming())
              // Scanning is also used in the UI when displaying a list of
              // networks.
              .Set(shill::kScanningProperty, device->scanning()));
    }
  }

  // NetworkState is always associated with the primary user profile, regardless
  // of what profile is associated with the page that calls this method. We do
  // not expose any sensitive properties in the resulting dictionary, it is
  // only used to show connection state and icons.
  std::string user_id_hash = LoginState::Get()->primary_user_hash();
  ::onc::ONCSource onc_source = ::onc::ONC_SOURCE_NONE;
  NetworkHandler::Get()
      ->managed_network_configuration_handler()
      ->FindPolicyByGUID(user_id_hash, network->guid(), &onc_source);

  base::Value::Dict onc_dictionary = onc::TranslateShillServiceToONCPart(
      shill_dictionary, onc_source, &chromeos::onc::kNetworkWithStateSignature,
      network);

  // Remove IPAddressConfigType/NameServersConfigType as these were
  // historically not provided by TranslateNetworkStateToONC.
  // The source shill properties for those ONC properties are not provided by
  // NetworkState::GetStateProperties, however since CL:2530330 these are
  // assumed to have defaults that are always enforced during ONC translation.
  onc_dictionary.Remove(::onc::network_config::kIPAddressConfigType);
  onc_dictionary.Remove(::onc::network_config::kNameServersConfigType);

  return onc_dictionary;
}

base::Value::List TranslateNetworkListToONC(NetworkTypePattern pattern,
                                            bool configured_only,
                                            bool visible_only,
                                            int limit) {
  NetworkStateHandler::NetworkStateList network_states;
  NetworkHandler::Get()->network_state_handler()->GetNetworkListByType(
      pattern, configured_only, visible_only, limit, &network_states);

  base::Value::List network_properties_list;
  for (const NetworkState* state : network_states) {
    network_properties_list.Append(TranslateNetworkStateToONC(state));
  }
  return network_properties_list;
}

std::string TranslateONCTypeToShill(const std::string& onc_type) {
  if (onc_type == ::onc::network_type::kEthernet)
    return shill::kTypeEthernet;
  std::string shill_type;
  onc::TranslateStringToShill(onc::kNetworkTypeTable, onc_type, &shill_type);
  return shill_type;
}

std::string TranslateONCSecurityToShill(const std::string& onc_security) {
  std::string shill_security;
  onc::TranslateStringToShill(onc::kWiFiSecurityTable, onc_security,
                              &shill_security);
  return shill_security;
}

std::string TranslateShillTypeToONC(const std::string& shill_type) {
  if (shill_type == shill::kTypeEthernet)
    return ::onc::network_type::kEthernet;
  std::string onc_type;
  onc::TranslateStringToONC(onc::kNetworkTypeTable, shill_type, &onc_type);
  return onc_type;
}

}  // namespace network_util
}  // namespace ash