blob: 3874a0691c62be2b597b8bd3493d979e26f621d0 [file] [log] [blame]
mukesh agrawalb54601c2011-06-07 17:39:22 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "shill/wifi_service.h"
6
7#include <string>
8
9#include <base/logging.h>
Chris Masone34af2182011-08-22 11:59:36 -070010#include <base/stringprintf.h>
11#include <base/string_number_conversions.h>
Paul Stewarta41e38d2011-11-11 07:47:29 -080012#include <base/string_split.h>
Chris Masone34af2182011-08-22 11:59:36 -070013#include <base/string_util.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070014#include <chromeos/dbus/service_constants.h>
mukesh agrawal6e277772011-09-29 15:04:23 -070015#include <dbus/dbus.h>
mukesh agrawald835b202011-10-07 15:26:47 -070016#include <glib.h>
mukesh agrawalb54601c2011-06-07 17:39:22 -070017
18#include "shill/control_interface.h"
19#include "shill/device.h"
mukesh agrawal1a056262011-10-05 14:36:54 -070020#include "shill/error.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070021#include "shill/event_dispatcher.h"
mukesh agrawal1a056262011-10-05 14:36:54 -070022#include "shill/ieee80211.h"
Thieu Lef7709452011-11-15 01:13:19 +000023#include "shill/property_accessor.h"
Paul Stewartd08f4432011-11-04 07:48:20 -070024#include "shill/store_interface.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070025#include "shill/wifi.h"
mukesh agrawal6e277772011-09-29 15:04:23 -070026#include "shill/wifi_endpoint.h"
27#include "shill/wpa_supplicant.h"
mukesh agrawalb54601c2011-06-07 17:39:22 -070028
mukesh agrawal261daca2011-12-02 18:56:56 +000029using std::set;
mukesh agrawalb54601c2011-06-07 17:39:22 -070030using std::string;
mukesh agrawal1a056262011-10-05 14:36:54 -070031using std::vector;
mukesh agrawalb54601c2011-06-07 17:39:22 -070032
33namespace shill {
mukesh agrawalb54601c2011-06-07 17:39:22 -070034
Paul Stewartd08f4432011-11-04 07:48:20 -070035const char WiFiService::kStorageHiddenSSID[] = "WiFi.HiddenSSID";
36
mukesh agrawalb54601c2011-06-07 17:39:22 -070037WiFiService::WiFiService(ControlInterface *control_interface,
38 EventDispatcher *dispatcher,
Chris Masone6791a432011-07-12 13:23:19 -070039 Manager *manager,
Chris Masone2b105542011-06-22 10:58:09 -070040 const WiFiRefPtr &device,
Paul Stewarta41e38d2011-11-11 07:47:29 -080041 const vector<uint8_t> &ssid,
42 const string &mode,
43 const string &security,
Paul Stewartced6a0b2011-11-08 15:32:04 -080044 bool hidden_ssid)
Gaurav Shah435de2c2011-11-17 19:01:07 -080045 : Service(control_interface, dispatcher, manager, Technology::kWifi),
Chris Masone75612302011-10-12 16:31:21 -070046 need_passphrase_(false),
mukesh agrawal6e277772011-09-29 15:04:23 -070047 security_(security),
Chris Masone092df3e2011-08-22 09:41:39 -070048 mode_(mode),
Paul Stewartced6a0b2011-11-08 15:32:04 -080049 hidden_ssid_(hidden_ssid),
mukesh agrawalb54601c2011-06-07 17:39:22 -070050 task_factory_(this),
51 wifi_(device),
Chris Masone092df3e2011-08-22 09:41:39 -070052 ssid_(ssid) {
mukesh agrawalde29fa82011-09-16 16:16:36 -070053 PropertyStore *store = this->mutable_store();
Paul Stewartac4ac002011-08-26 12:04:26 -070054 store->RegisterConstString(flimflam::kModeProperty, &mode_);
Thieu Lef7709452011-11-15 01:13:19 +000055 HelpRegisterDerivedString(store,
56 flimflam::kPassphraseProperty,
57 NULL,
58 &WiFiService::SetPassphrase);
Paul Stewartac4ac002011-08-26 12:04:26 -070059 store->RegisterBool(flimflam::kPassphraseRequiredProperty, &need_passphrase_);
60 store->RegisterConstString(flimflam::kSecurityProperty, &security_);
61 store->RegisterConstUint8(flimflam::kSignalStrengthProperty, &strength_);
Chris Masone3bd3c8c2011-06-13 08:20:26 -070062
Paul Stewartac4ac002011-08-26 12:04:26 -070063 store->RegisterConstString(flimflam::kWifiAuthMode, &auth_mode_);
64 store->RegisterConstBool(flimflam::kWifiHiddenSsid, &hidden_ssid_);
65 store->RegisterConstUint16(flimflam::kWifiFrequency, &frequency_);
66 store->RegisterConstUint16(flimflam::kWifiPhyMode, &physical_mode_);
mukesh agrawal32399322011-09-01 10:53:43 -070067
mukesh agrawald835b202011-10-07 15:26:47 -070068 hex_ssid_ = base::HexEncode(ssid_.data(), ssid_.size());
69 string ssid_string(
70 reinterpret_cast<const char *>(ssid_.data()), ssid_.size());
71 if (SanitizeSSID(&ssid_string)) {
72 // WifiHexSsid property should only be present if Name property
73 // has been munged.
74 store->RegisterConstString(flimflam::kWifiHexSsid, &hex_ssid_);
75 }
76 set_friendly_name(ssid_string);
Chris Masone9d779932011-08-25 16:33:41 -070077
mukesh agrawal6e277772011-09-29 15:04:23 -070078 // TODO(quiche): determine if it is okay to set EAP.KeyManagement for
79 // a service that is not 802.1x.
80 if (security_ == flimflam::kSecurity8021x) {
81 NOTIMPLEMENTED();
82 // XXX needs_passpharse_ = false ?
83 } else if (security_ == flimflam::kSecurityPsk) {
84 SetEAPKeyManagement("WPA-PSK");
85 need_passphrase_ = true;
86 } else if (security_ == flimflam::kSecurityRsn) {
87 SetEAPKeyManagement("WPA-PSK");
88 need_passphrase_ = true;
89 } else if (security_ == flimflam::kSecurityWpa) {
90 SetEAPKeyManagement("WPA-PSK");
91 need_passphrase_ = true;
92 } else if (security_ == flimflam::kSecurityWep) {
93 SetEAPKeyManagement("NONE");
94 need_passphrase_ = true;
95 } else if (security_ == flimflam::kSecurityNone) {
96 SetEAPKeyManagement("NONE");
97 need_passphrase_ = false;
98 } else {
99 LOG(ERROR) << "unsupported security method " << security_;
100 }
101
Paul Stewartd08f4432011-11-04 07:48:20 -0700102 // Until we know better (at Profile load time), use the generic name.
103 storage_identifier_ = GetGenericStorageIdentifier();
mukesh agrawal29c13a12011-11-24 00:09:19 +0000104 UpdateConnectable();
mukesh agrawalb54601c2011-06-07 17:39:22 -0700105}
106
107WiFiService::~WiFiService() {
108 LOG(INFO) << __func__;
109}
110
mukesh agrawal1830fa12011-09-26 14:31:40 -0700111void WiFiService::Connect(Error */*error*/) {
mukesh agrawalb54601c2011-06-07 17:39:22 -0700112 LOG(INFO) << __func__;
113
114 // NB(quiche) defer handling, since dbus-c++ does not permit us to
115 // send an outbound request while processing an inbound one.
Paul Stewartac4ac002011-08-26 12:04:26 -0700116 dispatcher()->PostTask(
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700117 task_factory_.NewRunnableMethod(&WiFiService::ConnectTask));
mukesh agrawalb54601c2011-06-07 17:39:22 -0700118}
119
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700120void WiFiService::Disconnect() {
121 // TODO(quiche) RemoveNetwork from supplicant
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700122}
123
Paul Stewart22aa71b2011-09-16 12:15:11 -0700124bool WiFiService::TechnologyIs(const Technology::Identifier type) const {
125 return wifi_->TechnologyIs(type);
126}
127
mukesh agrawal261daca2011-12-02 18:56:56 +0000128void WiFiService::AddEndpoint(WiFiEndpointConstRefPtr endpoint) {
129 DCHECK(endpoint->ssid() == ssid());
130 endpoints_.insert(endpoint);
131}
132
133void WiFiService::RemoveEndpoint(WiFiEndpointConstRefPtr endpoint) {
134 set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.find(endpoint);
135 DCHECK(i != endpoints_.end());
136 if (i == endpoints_.end()) {
137 LOG(WARNING) << "In " << __func__ << "(): "
138 << "ignorning non-existent endpoint "
139 << endpoint->bssid_string();
140 return;
141 }
142 endpoints_.erase(i);
143}
144
Chris Masone6515aab2011-10-12 16:19:09 -0700145string WiFiService::GetStorageIdentifier() const {
Paul Stewartd08f4432011-11-04 07:48:20 -0700146 return storage_identifier_;
Chris Masone34af2182011-08-22 11:59:36 -0700147}
Chris Masone092df3e2011-08-22 09:41:39 -0700148const string &WiFiService::mode() const {
mukesh agrawal445e72c2011-06-22 11:13:50 -0700149 return mode_;
150}
151
Chris Masone092df3e2011-08-22 09:41:39 -0700152const string &WiFiService::key_management() const {
Paul Stewartac4ac002011-08-26 12:04:26 -0700153 return GetEAPKeyManagement();
mukesh agrawal445e72c2011-06-22 11:13:50 -0700154}
155
Paul Stewarta41e38d2011-11-11 07:47:29 -0800156const vector<uint8_t> &WiFiService::ssid() const {
mukesh agrawal445e72c2011-06-22 11:13:50 -0700157 return ssid_;
158}
159
mukesh agrawal1a056262011-10-05 14:36:54 -0700160void WiFiService::SetPassphrase(const string &passphrase, Error *error) {
161 if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000162 ValidateWEPPassphrase(passphrase, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700163 } else if (security_ == flimflam::kSecurityPsk ||
164 security_ == flimflam::kSecurityWpa ||
165 security_ == flimflam::kSecurityRsn) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000166 ValidateWPAPassphrase(passphrase, error);
167 } else {
168 error->Populate(Error::kNotSupported);
mukesh agrawal1a056262011-10-05 14:36:54 -0700169 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000170
171 if (error->IsSuccess())
172 passphrase_ = passphrase;
mukesh agrawal29c13a12011-11-24 00:09:19 +0000173
174 UpdateConnectable();
mukesh agrawal1a056262011-10-05 14:36:54 -0700175}
176
Paul Stewartd08f4432011-11-04 07:48:20 -0700177bool WiFiService::IsLoadableFrom(StoreInterface *storage) const {
178 return storage->ContainsGroup(GetGenericStorageIdentifier()) ||
179 storage->ContainsGroup(GetSpecificStorageIdentifier());
180}
181
Paul Stewarta41e38d2011-11-11 07:47:29 -0800182bool WiFiService::IsVisible() const {
mukesh agrawal261daca2011-12-02 18:56:56 +0000183 const bool is_visible_in_scan = !endpoints_.empty();
Paul Stewarta41e38d2011-11-11 07:47:29 -0800184
185 // WiFi Services should be displayed only if they are in range (have
186 // endpoints that have shown up in a scan) or if the service is actively
187 // being connected.
188 return is_visible_in_scan || IsConnected() || IsConnecting();
189}
190
Paul Stewartd08f4432011-11-04 07:48:20 -0700191bool WiFiService::Load(StoreInterface *storage) {
192 // First find out which storage identifier is available in priority order
193 // of specific, generic.
194 string id = GetSpecificStorageIdentifier();
195 if (!storage->ContainsGroup(id)) {
196 id = GetGenericStorageIdentifier();
197 if (!storage->ContainsGroup(id)) {
198 LOG(WARNING) << "Service is not available in the persistent store: "
199 << id;
200 return false;
201 }
202 }
203
204 // Set our storage identifier to match the storage name in the Profile.
205 storage_identifier_ = id;
206
207 // Load properties common to all Services.
208 if (!Service::Load(storage)) {
209 return false;
210 }
211
212 // Load properties specific to WiFi services.
213 storage->GetBool(id, kStorageHiddenSSID, &hidden_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000214
215 // TODO(quiche): Load Passphrase property, ensure that UpdateConnectable
216 // is called (maybe via SetPassphrase). (crosbug.com/23467)
Paul Stewartd08f4432011-11-04 07:48:20 -0700217 return true;
218}
219
220bool WiFiService::Save(StoreInterface *storage) {
221 // Save properties common to all Services.
222 if (!Service::Save(storage)) {
223 return false;
224 }
225
226 // Save properties specific to WiFi services.
227 const string id = GetStorageIdentifier();
228 storage->SetBool(id, kStorageHiddenSSID, &hidden_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000229
230 // TODO(quiche): Save Passphrase property. (crosbug.com/23467)
Paul Stewartd08f4432011-11-04 07:48:20 -0700231 return true;
232}
233
Paul Stewart6ab23a92011-11-09 17:17:47 -0800234bool WiFiService::IsSecurityMatch(const string &security) const {
235 return GetSecurityClass(security) == GetSecurityClass(security_);
236}
237
mukesh agrawal32399322011-09-01 10:53:43 -0700238// private methods
Thieu Lef7709452011-11-15 01:13:19 +0000239void WiFiService::HelpRegisterDerivedString(
240 PropertyStore *store,
241 const std::string &name,
242 std::string(WiFiService::*get)(Error *),
243 void(WiFiService::*set)(const std::string&, Error *)) {
244 store->RegisterDerivedString(
245 name,
246 StringAccessor(new CustomAccessor<WiFiService, string>(this, get, set)));
247}
248
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700249void WiFiService::ConnectTask() {
mukesh agrawal6e277772011-09-29 15:04:23 -0700250 std::map<string, DBus::Variant> params;
251 DBus::MessageIter writer;
252
253 params[wpa_supplicant::kNetworkPropertyMode].writer().
254 append_uint32(WiFiEndpoint::ModeStringToUint(mode_));
255
256 if (security_ == flimflam::kSecurity8021x) {
257 NOTIMPLEMENTED();
258 } else if (security_ == flimflam::kSecurityPsk) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800259 const string psk_proto = StringPrintf("%s %s",
260 wpa_supplicant::kSecurityModeWPA,
261 wpa_supplicant::kSecurityModeRSN);
262 params[wpa_supplicant::kPropertySecurityProtocol].writer().
263 append_string(psk_proto.c_str());
264 params[wpa_supplicant::kPropertyPreSharedKey].writer().
265 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700266 } else if (security_ == flimflam::kSecurityRsn) {
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700267 params[wpa_supplicant::kPropertySecurityProtocol].writer().
268 append_string(wpa_supplicant::kSecurityModeRSN);
269 params[wpa_supplicant::kPropertyPreSharedKey].writer().
270 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700271 } else if (security_ == flimflam::kSecurityWpa) {
272 params[wpa_supplicant::kPropertySecurityProtocol].writer().
273 append_string(wpa_supplicant::kSecurityModeWPA);
274 params[wpa_supplicant::kPropertyPreSharedKey].writer().
275 append_string(passphrase_.c_str());
276 } else if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000277 params[wpa_supplicant::kPropertyAuthAlg].writer().
278 append_string(wpa_supplicant::kSecurityAuthAlg);
279 Error error;
280 int key_index;
281 std::vector<uint8> password_bytes;
282 ParseWEPPassphrase(passphrase_, &key_index, &password_bytes, &error);
283 writer = params[wpa_supplicant::kPropertyWEPKey +
284 base::IntToString(key_index)].writer();
285 writer << password_bytes;
286 params[wpa_supplicant::kPropertyWEPTxKeyIndex].writer().
287 append_uint32(key_index);
mukesh agrawal6e277772011-09-29 15:04:23 -0700288 } else if (security_ == flimflam::kSecurityNone) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800289 // Nothing special to do here.
mukesh agrawal6e277772011-09-29 15:04:23 -0700290 } else {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800291 LOG(ERROR) << "Can't connect. Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700292 }
293
294 params[wpa_supplicant::kPropertyKeyManagement].writer().
295 append_string(key_management().c_str());
Gaurav Shah7ad8e532011-11-11 17:14:49 -0800296
297 // See note in dbus_adaptor.cc on why we need to use a local.
mukesh agrawal6e277772011-09-29 15:04:23 -0700298 writer = params[wpa_supplicant::kNetworkPropertySSID].writer();
299 writer << ssid_;
300
301 wifi_->ConnectTo(this, params);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700302}
303
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800304string WiFiService::GetDeviceRpcId(Error */*error*/) {
Chris Masone95207da2011-06-29 16:50:49 -0700305 return wifi_->GetRpcIdentifier();
306}
307
mukesh agrawal29c13a12011-11-24 00:09:19 +0000308void WiFiService::UpdateConnectable() {
309 if (security_ == flimflam::kSecurityNone) {
310 DCHECK(passphrase_.empty());
311 set_connectable(true);
312 } else if (security_ == flimflam::kSecurityWep ||
313 security_ == flimflam::kSecurityWpa ||
314 security_ == flimflam::kSecurityPsk ||
315 security_ == flimflam::kSecurityRsn) {
316 set_connectable(!passphrase_.empty());
317 } else {
318 // TODO(quiche): Handle connectability for 802.1x. (crosbug.com/23466)
319 set_connectable(false);
320 }
321}
322
mukesh agrawal1a056262011-10-05 14:36:54 -0700323// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000324void WiFiService::ValidateWEPPassphrase(const std::string &passphrase,
325 Error *error) {
326 ParseWEPPassphrase(passphrase, NULL, NULL, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700327}
328
329// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000330void WiFiService::ValidateWPAPassphrase(const std::string &passphrase,
331 Error *error) {
mukesh agrawal1a056262011-10-05 14:36:54 -0700332 unsigned int length = passphrase.length();
333 vector<uint8> passphrase_bytes;
334
335 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
336 if (length != IEEE_80211::kWPAHexLen &&
337 (length < IEEE_80211::kWPAAsciiMinLen ||
338 length > IEEE_80211::kWPAAsciiMaxLen)) {
339 error->Populate(Error::kInvalidPassphrase);
340 }
341 } else {
342 if (length < IEEE_80211::kWPAAsciiMinLen ||
343 length > IEEE_80211::kWPAAsciiMaxLen) {
344 error->Populate(Error::kInvalidPassphrase);
345 }
346 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000347}
mukesh agrawal1a056262011-10-05 14:36:54 -0700348
Thieu Lef4cbda92011-11-10 23:41:24 +0000349// static
350void WiFiService::ParseWEPPassphrase(const string &passphrase,
351 int *key_index,
352 std::vector<uint8> *password_bytes,
353 Error *error) {
354 unsigned int length = passphrase.length();
355 int key_index_local;
356 std::string password_text;
357 bool is_hex = false;
358
359 switch (length) {
360 case IEEE_80211::kWEP40AsciiLen:
361 case IEEE_80211::kWEP104AsciiLen:
362 key_index_local = 0;
363 password_text = passphrase;
364 break;
365 case IEEE_80211::kWEP40AsciiLen + 2:
366 case IEEE_80211::kWEP104AsciiLen + 2:
367 if (CheckWEPKeyIndex(passphrase, error)) {
368 base::StringToInt(passphrase.substr(0,1), &key_index_local);
369 password_text = passphrase.substr(2);
370 }
371 break;
372 case IEEE_80211::kWEP40HexLen:
373 case IEEE_80211::kWEP104HexLen:
374 if (CheckWEPIsHex(passphrase, error)) {
375 key_index_local = 0;
376 password_text = passphrase;
377 is_hex = true;
378 }
379 break;
380 case IEEE_80211::kWEP40HexLen + 2:
381 case IEEE_80211::kWEP104HexLen + 2:
382 if(CheckWEPKeyIndex(passphrase, error) &&
383 CheckWEPIsHex(passphrase.substr(2), error)) {
384 base::StringToInt(passphrase.substr(0,1), &key_index_local);
385 password_text = passphrase.substr(2);
386 is_hex = true;
387 } else if (CheckWEPPrefix(passphrase, error) &&
388 CheckWEPIsHex(passphrase.substr(2), error)) {
389 key_index_local = 0;
390 password_text = passphrase.substr(2);
391 is_hex = true;
392 }
393 break;
394 case IEEE_80211::kWEP40HexLen + 4:
395 case IEEE_80211::kWEP104HexLen + 4:
396 if (CheckWEPKeyIndex(passphrase, error) &&
397 CheckWEPPrefix(passphrase.substr(2), error) &&
398 CheckWEPIsHex(passphrase.substr(4), error)) {
399 base::StringToInt(passphrase.substr(0,1), &key_index_local);
400 password_text = passphrase.substr(4);
401 is_hex = true;
402 }
403 break;
404 default:
405 error->Populate(Error::kInvalidPassphrase);
406 break;
407 }
408
mukesh agrawal1a056262011-10-05 14:36:54 -0700409 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000410 if (key_index)
411 *key_index = key_index_local;
412 if (password_bytes) {
413 if (is_hex)
414 base::HexStringToBytes(password_text, password_bytes);
415 else
416 password_bytes->insert(password_bytes->end(),
417 password_text.begin(),
418 password_text.end());
419 }
mukesh agrawal1a056262011-10-05 14:36:54 -0700420 }
421}
422
423// static
424bool WiFiService::CheckWEPIsHex(const string &passphrase, Error *error) {
425 vector<uint8> passphrase_bytes;
426 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
427 return true;
428 } else {
429 error->Populate(Error::kInvalidPassphrase);
430 return false;
431 }
432}
433
434// static
435bool WiFiService::CheckWEPKeyIndex(const string &passphrase, Error *error) {
436 if (StartsWithASCII(passphrase, "0:", false) ||
437 StartsWithASCII(passphrase, "1:", false) ||
438 StartsWithASCII(passphrase, "2:", false) ||
439 StartsWithASCII(passphrase, "3:", false)) {
440 return true;
441 } else {
442 error->Populate(Error::kInvalidPassphrase);
443 return false;
444 }
445}
446
447// static
448bool WiFiService::CheckWEPPrefix(const string &passphrase, Error *error) {
449 if (StartsWithASCII(passphrase, "0x", false)) {
450 return true;
451 } else {
452 error->Populate(Error::kInvalidPassphrase);
453 return false;
454 }
455}
456
Paul Stewart6ab23a92011-11-09 17:17:47 -0800457// static
mukesh agrawald835b202011-10-07 15:26:47 -0700458bool WiFiService::SanitizeSSID(string *ssid) {
459 CHECK(ssid);
460
461 size_t ssid_len = ssid->length();
462 size_t i;
463 bool changed = false;
464
465 for (i=0; i < ssid_len; ++i) {
466 if (!g_ascii_isprint((*ssid)[i])) {
467 (*ssid)[i] = '?';
468 changed = true;
469 }
470 }
471
472 return changed;
473}
474
Paul Stewart6ab23a92011-11-09 17:17:47 -0800475// static
476string WiFiService::GetSecurityClass(const string &security) {
477 if (security == flimflam::kSecurityRsn ||
478 security == flimflam::kSecurityWpa) {
479 return flimflam::kSecurityPsk;
Paul Stewartd08f4432011-11-04 07:48:20 -0700480 } else {
Paul Stewart6ab23a92011-11-09 17:17:47 -0800481 return security;
Paul Stewartd08f4432011-11-04 07:48:20 -0700482 }
483}
484
Paul Stewarta41e38d2011-11-11 07:47:29 -0800485// static
486bool WiFiService::ParseStorageIdentifier(const string &storage_name,
487 string *address,
488 string *mode,
489 string *security) {
490 vector<string> wifi_parts;
491 base::SplitString(storage_name, '_', &wifi_parts);
492 if (wifi_parts.size() != 5 || wifi_parts[0] != flimflam::kTypeWifi) {
493 return false;
494 }
495 *address = wifi_parts[1];
496 *mode = wifi_parts[3];
497 *security = wifi_parts[4];
498 return true;
499}
500
Paul Stewart6ab23a92011-11-09 17:17:47 -0800501string WiFiService::GetGenericStorageIdentifier() const {
502 return GetStorageIdentifierForSecurity(GetSecurityClass(security_));
503}
504
Paul Stewartd08f4432011-11-04 07:48:20 -0700505string WiFiService::GetSpecificStorageIdentifier() const {
506 return GetStorageIdentifierForSecurity(security_);
507}
508
509string WiFiService::GetStorageIdentifierForSecurity(
510 const string &security) const {
511 return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s",
512 flimflam::kTypeWifi,
513 wifi_->address().c_str(),
514 hex_ssid_.c_str(),
515 mode_.c_str(),
516 security.c_str()));
517}
518
mukesh agrawalb54601c2011-06-07 17:39:22 -0700519} // namespace shill