summaryrefslogtreecommitdiffstats
path: root/chromeos/network/network_state.cc
blob: 50fad2359bf427b7ec3c1ab178c4cf1462070dbb (plain)
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
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/network/network_state.h"

#include "base/i18n/icu_encoding_detection.h"
#include "base/i18n/icu_string_conversions.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversion_utils.h"
#include "chromeos/network/network_event_log.h"
#include "chromeos/network/network_ui_data.h"
#include "chromeos/network/onc/onc_utils.h"
#include "third_party/cros_system_api/dbus/service_constants.h"

namespace {

bool ConvertListValueToStringVector(const base::ListValue& string_list,
                                    std::vector<std::string>* result) {
  for (size_t i = 0; i < string_list.GetSize(); ++i) {
    std::string str;
    if (!string_list.GetString(i, &str))
      return false;
    result->push_back(str);
  }
  return true;
}

// Replace non UTF8 characters in |str| with a replacement character.
std::string ValidateUTF8(const std::string& str) {
  std::string result;
  for (int32 index = 0; index < static_cast<int32>(str.size()); ++index) {
    uint32 code_point_out;
    bool is_unicode_char = base::ReadUnicodeCharacter(str.c_str(), str.size(),
                                                      &index, &code_point_out);
    const uint32 kFirstNonControlChar = 0x20;
    if (is_unicode_char && (code_point_out >= kFirstNonControlChar)) {
      base::WriteUnicodeCharacter(code_point_out, &result);
    } else {
      const uint32 kReplacementChar = 0xFFFD;
      // Puts kReplacementChar if character is a control character [0,0x20)
      // or is not readable UTF8.
      base::WriteUnicodeCharacter(kReplacementChar, &result);
    }
  }
  return result;
}

}  // namespace

namespace chromeos {

NetworkState::NetworkState(const std::string& path)
    : ManagedState(MANAGED_TYPE_NETWORK, path),
      auto_connect_(false),
      favorite_(false),
      priority_(0),
      onc_source_(onc::ONC_SOURCE_NONE),
      signal_strength_(0),
      connectable_(false),
      passphrase_required_(false),
      activate_over_non_cellular_networks_(false),
      cellular_out_of_credits_(false) {
}

NetworkState::~NetworkState() {
}

bool NetworkState::PropertyChanged(const std::string& key,
                                   const base::Value& value) {
  // Keep care that these properties are the same as in |GetProperties|.
  if (ManagedStatePropertyChanged(key, value))
    return true;
  if (key == flimflam::kSignalStrengthProperty) {
    return GetIntegerValue(key, value, &signal_strength_);
  } else if (key == flimflam::kStateProperty) {
    return GetStringValue(key, value, &connection_state_);
  } else if (key == flimflam::kConnectableProperty) {
    return GetBooleanValue(key, value, &connectable_);
  } else if (key == flimflam::kPassphraseRequiredProperty) {
    return GetBooleanValue(key, value, &passphrase_required_);
  } else if (key == flimflam::kErrorProperty) {
    return GetStringValue(key, value, &error_);
  } else if (key == shill::kErrorDetailsProperty) {
    return GetStringValue(key, value, &error_details_);
  } else if (key == IPConfigProperty(flimflam::kAddressProperty)) {
    return GetStringValue(key, value, &ip_address_);
  } else if (key == IPConfigProperty(flimflam::kNameServersProperty)) {
    dns_servers_.clear();
    const base::ListValue* dns_servers;
    if (value.GetAsList(&dns_servers))
      ConvertListValueToStringVector(*dns_servers, &dns_servers_);
    return true;
  } else if (key == flimflam::kActivationStateProperty) {
    return GetStringValue(key, value, &activation_state_);
  } else if (key == flimflam::kRoamingStateProperty) {
    return GetStringValue(key, value, &roaming_);
  } else if (key == flimflam::kSecurityProperty) {
    return GetStringValue(key, value, &security_);
  } else if (key == flimflam::kAutoConnectProperty) {
    return GetBooleanValue(key, value, &auto_connect_);
  } else if (key == flimflam::kFavoriteProperty) {
    return GetBooleanValue(key, value, &favorite_);
  } else if (key == flimflam::kPriorityProperty) {
    return GetIntegerValue(key, value, &priority_);
  } else if (key == flimflam::kProxyConfigProperty) {
    std::string proxy_config_str;
    if (!value.GetAsString(&proxy_config_str)) {
      LOG(WARNING) << "Failed to parse string value for:" << key;
      return false;
    }

    proxy_config_.Clear();
    if (proxy_config_str.empty())
      return true;

    scoped_ptr<base::DictionaryValue> proxy_config_dict(
        onc::ReadDictionaryFromJson(proxy_config_str));
    if (proxy_config_dict) {
      // Warning: The DictionaryValue returned from
      // ReadDictionaryFromJson/JSONParser is an optimized derived class that
      // doesn't allow releasing ownership of nested values. A Swap in the wrong
      // order leads to memory access errors.
      proxy_config_.MergeDictionary(proxy_config_dict.get());
    } else {
      LOG(WARNING) << "Failed to parse dictionary value for: " << key;
    }
    return true;
  } else if (key == flimflam::kUIDataProperty) {
    std::string ui_data_str;
    if (!value.GetAsString(&ui_data_str)) {
      LOG(WARNING) << "Failed to parse string value for:" << key;
      return false;
    }

    onc_source_ = onc::ONC_SOURCE_NONE;
    if (ui_data_str.empty())
      return true;

    scoped_ptr<base::DictionaryValue> ui_data_dict(
        onc::ReadDictionaryFromJson(ui_data_str));
    if (ui_data_dict)
      onc_source_ = NetworkUIData(*ui_data_dict).onc_source();
    else
      LOG(WARNING) << "Failed to parse dictionary value for: " << key;
    return true;
  } else if (key == flimflam::kNetworkTechnologyProperty) {
    return GetStringValue(key, value, &technology_);
  } else if (key == flimflam::kDeviceProperty) {
    return GetStringValue(key, value, &device_path_);
  } else if (key == flimflam::kGuidProperty) {
    return GetStringValue(key, value, &guid_);
  } else if (key == flimflam::kProfileProperty) {
    return GetStringValue(key, value, &profile_path_);
  } else if (key == shill::kActivateOverNonCellularNetworkProperty) {
    return GetBooleanValue(key, value, &activate_over_non_cellular_networks_);
  } else if (key == shill::kOutOfCreditsProperty) {
    return GetBooleanValue(key, value, &cellular_out_of_credits_);
  } else if (key == flimflam::kWifiHexSsid) {
    return GetStringValue(key, value, &hex_ssid_);
  } else if (key == flimflam::kCountryProperty) {
    // TODO(stevenjb): This is currently experimental. If we find a case where
    // base::DetectEncoding() fails in UpdateName(), where country_code_ is
    // set, figure out whether we can use country_code_ with ConvertToUtf8().
    // crbug.com/233267.
    return GetStringValue(key, value, &country_code_);
  }
  return false;
}

void NetworkState::InitialPropertiesReceived() {
  UpdateName();
}

void NetworkState::GetProperties(base::DictionaryValue* dictionary) const {
  // Keep care that these properties are the same as in |PropertyChanged|.
  dictionary->SetStringWithoutPathExpansion(flimflam::kNameProperty, name());
  dictionary->SetStringWithoutPathExpansion(flimflam::kTypeProperty, type());
  dictionary->SetIntegerWithoutPathExpansion(flimflam::kSignalStrengthProperty,
                                             signal_strength_);
  dictionary->SetStringWithoutPathExpansion(flimflam::kStateProperty,
                                            connection_state_);
  dictionary->SetBooleanWithoutPathExpansion(flimflam::kConnectableProperty,
                                             connectable_);
  dictionary->SetBooleanWithoutPathExpansion(
      flimflam::kPassphraseRequiredProperty, passphrase_required_);
  dictionary->SetStringWithoutPathExpansion(flimflam::kErrorProperty,
                                            error_);
  dictionary->SetStringWithoutPathExpansion(shill::kErrorDetailsProperty,
                                            error_details_);
  base::DictionaryValue* ipconfig_properties = new DictionaryValue;
  ipconfig_properties->SetStringWithoutPathExpansion(flimflam::kAddressProperty,
                                                     ip_address_);
  base::ListValue* name_servers = new ListValue;
  name_servers->AppendStrings(dns_servers_);
  ipconfig_properties->SetWithoutPathExpansion(flimflam::kNameServersProperty,
                                               name_servers);
  dictionary->SetWithoutPathExpansion(shill::kIPConfigProperty,
                                      ipconfig_properties);

  dictionary->SetStringWithoutPathExpansion(flimflam::kActivationStateProperty,
                                            activation_state_);
  dictionary->SetStringWithoutPathExpansion(flimflam::kRoamingStateProperty,
                                            roaming_);
  dictionary->SetStringWithoutPathExpansion(flimflam::kSecurityProperty,
                                            security_);
  dictionary->SetBooleanWithoutPathExpansion(flimflam::kAutoConnectProperty,
                                             auto_connect_);
  dictionary->SetBooleanWithoutPathExpansion(flimflam::kFavoriteProperty,
                                             favorite_);
  dictionary->SetIntegerWithoutPathExpansion(flimflam::kPriorityProperty,
                                             priority_);
  // Proxy config and ONC source are intentionally omitted: These properties are
  // placed in NetworkState to transition ProxyConfigServiceImpl from
  // NetworkLibrary to the new network stack. The networking extension API
  // shouldn't depend on this member. Once ManagedNetworkConfigurationHandler
  // is used instead of NetworkLibrary, we can remove them again.
  dictionary->SetStringWithoutPathExpansion(
      flimflam::kNetworkTechnologyProperty,
      technology_);
  dictionary->SetStringWithoutPathExpansion(flimflam::kDeviceProperty,
                                            device_path_);
  dictionary->SetStringWithoutPathExpansion(flimflam::kGuidProperty, guid_);
  dictionary->SetStringWithoutPathExpansion(flimflam::kProfileProperty,
                                            profile_path_);
  dictionary->SetBooleanWithoutPathExpansion(
      shill::kActivateOverNonCellularNetworkProperty,
      activate_over_non_cellular_networks_);
  dictionary->SetBooleanWithoutPathExpansion(shill::kOutOfCreditsProperty,
                                             cellular_out_of_credits_);
}

bool NetworkState::IsConnectedState() const {
  return StateIsConnected(connection_state_);
}

bool NetworkState::IsConnectingState() const {
  return StateIsConnecting(connection_state_);
}

bool NetworkState::HasAuthenticationError() const {
  return (error_ == flimflam::kErrorBadPassphrase ||
          error_ == flimflam::kErrorBadWEPKey ||
          error_ == flimflam::kErrorPppAuthFailed ||
          error_ == shill::kErrorEapLocalTlsFailed ||
          error_ == shill::kErrorEapRemoteTlsFailed ||
          error_ == shill::kErrorEapAuthenticationFailed);
}

void NetworkState::UpdateName() {
  if (hex_ssid_.empty()) {
    // Validate name for UTF8.
    std::string valid_ssid = ValidateUTF8(name());
    if (valid_ssid != name()) {
      set_name(valid_ssid);
      NET_LOG_DEBUG("UpdateName", base::StringPrintf(
          "%s: UTF8: %s", path().c_str(), name().c_str()));
    }
    return;
  }

  std::string ssid;
  std::vector<uint8> raw_ssid_bytes;
  if (base::HexStringToBytes(hex_ssid_, &raw_ssid_bytes)) {
    ssid = std::string(raw_ssid_bytes.begin(), raw_ssid_bytes.end());
  } else {
    std::string desc = base::StringPrintf("%s: Error processing: %s",
                                          path().c_str(), hex_ssid_.c_str());
    NET_LOG_DEBUG("UpdateName", desc);
    LOG(ERROR) << desc;
    ssid = name();
  }

  if (IsStringUTF8(ssid)) {
    if (ssid != name()) {
      set_name(ssid);
      NET_LOG_DEBUG("UpdateName", base::StringPrintf(
          "%s: UTF8: %s", path().c_str(), name().c_str()));
    }
    return;
  }

  // Detect encoding and convert to UTF-8.
  std::string encoding;
  if (!base::DetectEncoding(ssid, &encoding)) {
    // TODO(stevenjb): Test this. See comment in PropertyChanged() under
    // flimflam::kCountryProperty.
    encoding = country_code_;
  }
  if (!encoding.empty()) {
    std::string utf8_ssid;
    if (base::ConvertToUtf8AndNormalize(ssid, encoding, &utf8_ssid)) {
      set_name(utf8_ssid);
      NET_LOG_DEBUG("UpdateName", base::StringPrintf(
          "%s: Encoding=%s: %s", path().c_str(),
          encoding.c_str(), name().c_str()));
      return;
    }
  }

  // Unrecognized encoding. Only use raw bytes if name_ is empty.
  if (name().empty())
    set_name(ssid);
  NET_LOG_DEBUG("UpdateName", base::StringPrintf(
      "%s: Unrecognized Encoding=%s: %s", path().c_str(),
      encoding.c_str(), name().c_str()));
}

// static
bool NetworkState::StateIsConnected(const std::string& connection_state) {
  return (connection_state == flimflam::kStateReady ||
          connection_state == flimflam::kStateOnline ||
          connection_state == flimflam::kStatePortal);
}

// static
bool NetworkState::StateIsConnecting(const std::string& connection_state) {
  return (connection_state == flimflam::kStateAssociation ||
          connection_state == flimflam::kStateConfiguration ||
          connection_state == flimflam::kStateCarrier);
}

// static
std::string NetworkState::IPConfigProperty(const char* key) {
  return base::StringPrintf("%s.%s", shill::kIPConfigProperty, key);
}

}  // namespace chromeos