blob: 38c0e0b0677f418b517652842bf8435464a4554d [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__;
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000113 // Defer handling, since dbus-c++ does not permit us to send an
114 // outbound request while processing an inbound one.
Paul Stewartac4ac002011-08-26 12:04:26 -0700115 dispatcher()->PostTask(
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700116 task_factory_.NewRunnableMethod(&WiFiService::ConnectTask));
mukesh agrawalb54601c2011-06-07 17:39:22 -0700117}
118
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000119void WiFiService::Disconnect(Error */*error*/) {
120 LOG(INFO) << __func__;
121 // Defer handling, since dbus-c++ does not permit us to send an
122 // outbound request while processing an inbound one.
123 dispatcher()->PostTask(
124 task_factory_.NewRunnableMethod(&WiFiService::DisconnectTask));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700125}
126
Paul Stewart22aa71b2011-09-16 12:15:11 -0700127bool WiFiService::TechnologyIs(const Technology::Identifier type) const {
128 return wifi_->TechnologyIs(type);
129}
130
mukesh agrawal261daca2011-12-02 18:56:56 +0000131void WiFiService::AddEndpoint(WiFiEndpointConstRefPtr endpoint) {
132 DCHECK(endpoint->ssid() == ssid());
133 endpoints_.insert(endpoint);
134}
135
136void WiFiService::RemoveEndpoint(WiFiEndpointConstRefPtr endpoint) {
137 set<WiFiEndpointConstRefPtr>::iterator i = endpoints_.find(endpoint);
138 DCHECK(i != endpoints_.end());
139 if (i == endpoints_.end()) {
140 LOG(WARNING) << "In " << __func__ << "(): "
141 << "ignorning non-existent endpoint "
142 << endpoint->bssid_string();
143 return;
144 }
145 endpoints_.erase(i);
146}
147
Chris Masone6515aab2011-10-12 16:19:09 -0700148string WiFiService::GetStorageIdentifier() const {
Paul Stewartd08f4432011-11-04 07:48:20 -0700149 return storage_identifier_;
Chris Masone34af2182011-08-22 11:59:36 -0700150}
Chris Masone092df3e2011-08-22 09:41:39 -0700151const string &WiFiService::mode() const {
mukesh agrawal445e72c2011-06-22 11:13:50 -0700152 return mode_;
153}
154
Chris Masone092df3e2011-08-22 09:41:39 -0700155const string &WiFiService::key_management() const {
Paul Stewartac4ac002011-08-26 12:04:26 -0700156 return GetEAPKeyManagement();
mukesh agrawal445e72c2011-06-22 11:13:50 -0700157}
158
Paul Stewarta41e38d2011-11-11 07:47:29 -0800159const vector<uint8_t> &WiFiService::ssid() const {
mukesh agrawal445e72c2011-06-22 11:13:50 -0700160 return ssid_;
161}
162
mukesh agrawal1a056262011-10-05 14:36:54 -0700163void WiFiService::SetPassphrase(const string &passphrase, Error *error) {
164 if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000165 ValidateWEPPassphrase(passphrase, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700166 } else if (security_ == flimflam::kSecurityPsk ||
167 security_ == flimflam::kSecurityWpa ||
168 security_ == flimflam::kSecurityRsn) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000169 ValidateWPAPassphrase(passphrase, error);
170 } else {
171 error->Populate(Error::kNotSupported);
mukesh agrawal1a056262011-10-05 14:36:54 -0700172 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000173
174 if (error->IsSuccess())
175 passphrase_ = passphrase;
mukesh agrawal29c13a12011-11-24 00:09:19 +0000176
177 UpdateConnectable();
mukesh agrawal1a056262011-10-05 14:36:54 -0700178}
179
Paul Stewartd08f4432011-11-04 07:48:20 -0700180bool WiFiService::IsLoadableFrom(StoreInterface *storage) const {
181 return storage->ContainsGroup(GetGenericStorageIdentifier()) ||
182 storage->ContainsGroup(GetSpecificStorageIdentifier());
183}
184
Paul Stewarta41e38d2011-11-11 07:47:29 -0800185bool WiFiService::IsVisible() const {
mukesh agrawal261daca2011-12-02 18:56:56 +0000186 const bool is_visible_in_scan = !endpoints_.empty();
Paul Stewarta41e38d2011-11-11 07:47:29 -0800187
188 // WiFi Services should be displayed only if they are in range (have
189 // endpoints that have shown up in a scan) or if the service is actively
190 // being connected.
191 return is_visible_in_scan || IsConnected() || IsConnecting();
192}
193
Paul Stewartd08f4432011-11-04 07:48:20 -0700194bool WiFiService::Load(StoreInterface *storage) {
195 // First find out which storage identifier is available in priority order
196 // of specific, generic.
197 string id = GetSpecificStorageIdentifier();
198 if (!storage->ContainsGroup(id)) {
199 id = GetGenericStorageIdentifier();
200 if (!storage->ContainsGroup(id)) {
201 LOG(WARNING) << "Service is not available in the persistent store: "
202 << id;
203 return false;
204 }
205 }
206
207 // Set our storage identifier to match the storage name in the Profile.
208 storage_identifier_ = id;
209
210 // Load properties common to all Services.
211 if (!Service::Load(storage)) {
212 return false;
213 }
214
215 // Load properties specific to WiFi services.
216 storage->GetBool(id, kStorageHiddenSSID, &hidden_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000217
218 // TODO(quiche): Load Passphrase property, ensure that UpdateConnectable
219 // is called (maybe via SetPassphrase). (crosbug.com/23467)
Paul Stewartd08f4432011-11-04 07:48:20 -0700220 return true;
221}
222
223bool WiFiService::Save(StoreInterface *storage) {
224 // Save properties common to all Services.
225 if (!Service::Save(storage)) {
226 return false;
227 }
228
229 // Save properties specific to WiFi services.
230 const string id = GetStorageIdentifier();
231 storage->SetBool(id, kStorageHiddenSSID, &hidden_ssid_);
mukesh agrawal29c13a12011-11-24 00:09:19 +0000232
233 // TODO(quiche): Save Passphrase property. (crosbug.com/23467)
Paul Stewartd08f4432011-11-04 07:48:20 -0700234 return true;
235}
236
Paul Stewart6ab23a92011-11-09 17:17:47 -0800237bool WiFiService::IsSecurityMatch(const string &security) const {
238 return GetSecurityClass(security) == GetSecurityClass(security_);
239}
240
mukesh agrawal32399322011-09-01 10:53:43 -0700241// private methods
Thieu Lef7709452011-11-15 01:13:19 +0000242void WiFiService::HelpRegisterDerivedString(
243 PropertyStore *store,
244 const std::string &name,
245 std::string(WiFiService::*get)(Error *),
246 void(WiFiService::*set)(const std::string&, Error *)) {
247 store->RegisterDerivedString(
248 name,
249 StringAccessor(new CustomAccessor<WiFiService, string>(this, get, set)));
250}
251
mukesh agrawaldc42bb32011-07-28 10:40:26 -0700252void WiFiService::ConnectTask() {
mukesh agrawal6e277772011-09-29 15:04:23 -0700253 std::map<string, DBus::Variant> params;
254 DBus::MessageIter writer;
255
256 params[wpa_supplicant::kNetworkPropertyMode].writer().
257 append_uint32(WiFiEndpoint::ModeStringToUint(mode_));
258
259 if (security_ == flimflam::kSecurity8021x) {
260 NOTIMPLEMENTED();
261 } else if (security_ == flimflam::kSecurityPsk) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800262 const string psk_proto = StringPrintf("%s %s",
263 wpa_supplicant::kSecurityModeWPA,
264 wpa_supplicant::kSecurityModeRSN);
265 params[wpa_supplicant::kPropertySecurityProtocol].writer().
266 append_string(psk_proto.c_str());
267 params[wpa_supplicant::kPropertyPreSharedKey].writer().
268 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700269 } else if (security_ == flimflam::kSecurityRsn) {
mukesh agrawalf2fd7452011-10-03 16:38:47 -0700270 params[wpa_supplicant::kPropertySecurityProtocol].writer().
271 append_string(wpa_supplicant::kSecurityModeRSN);
272 params[wpa_supplicant::kPropertyPreSharedKey].writer().
273 append_string(passphrase_.c_str());
mukesh agrawal6e277772011-09-29 15:04:23 -0700274 } else if (security_ == flimflam::kSecurityWpa) {
275 params[wpa_supplicant::kPropertySecurityProtocol].writer().
276 append_string(wpa_supplicant::kSecurityModeWPA);
277 params[wpa_supplicant::kPropertyPreSharedKey].writer().
278 append_string(passphrase_.c_str());
279 } else if (security_ == flimflam::kSecurityWep) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000280 params[wpa_supplicant::kPropertyAuthAlg].writer().
281 append_string(wpa_supplicant::kSecurityAuthAlg);
282 Error error;
283 int key_index;
284 std::vector<uint8> password_bytes;
285 ParseWEPPassphrase(passphrase_, &key_index, &password_bytes, &error);
286 writer = params[wpa_supplicant::kPropertyWEPKey +
287 base::IntToString(key_index)].writer();
288 writer << password_bytes;
289 params[wpa_supplicant::kPropertyWEPTxKeyIndex].writer().
290 append_uint32(key_index);
mukesh agrawal6e277772011-09-29 15:04:23 -0700291 } else if (security_ == flimflam::kSecurityNone) {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800292 // Nothing special to do here.
mukesh agrawal6e277772011-09-29 15:04:23 -0700293 } else {
Gaurav Shahf8721ee2011-11-07 09:12:46 -0800294 LOG(ERROR) << "Can't connect. Unsupported security method " << security_;
mukesh agrawal6e277772011-09-29 15:04:23 -0700295 }
296
297 params[wpa_supplicant::kPropertyKeyManagement].writer().
298 append_string(key_management().c_str());
Gaurav Shah7ad8e532011-11-11 17:14:49 -0800299
300 // See note in dbus_adaptor.cc on why we need to use a local.
mukesh agrawal6e277772011-09-29 15:04:23 -0700301 writer = params[wpa_supplicant::kNetworkPropertySSID].writer();
302 writer << ssid_;
303
304 wifi_->ConnectTo(this, params);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700305}
306
mukesh agrawal0ed0f2e2011-12-05 20:36:17 +0000307void WiFiService::DisconnectTask() {
308 wifi_->DisconnectFrom(this);
309}
310
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800311string WiFiService::GetDeviceRpcId(Error */*error*/) {
Chris Masone95207da2011-06-29 16:50:49 -0700312 return wifi_->GetRpcIdentifier();
313}
314
mukesh agrawal29c13a12011-11-24 00:09:19 +0000315void WiFiService::UpdateConnectable() {
316 if (security_ == flimflam::kSecurityNone) {
317 DCHECK(passphrase_.empty());
318 set_connectable(true);
319 } else if (security_ == flimflam::kSecurityWep ||
320 security_ == flimflam::kSecurityWpa ||
321 security_ == flimflam::kSecurityPsk ||
322 security_ == flimflam::kSecurityRsn) {
323 set_connectable(!passphrase_.empty());
324 } else {
325 // TODO(quiche): Handle connectability for 802.1x. (crosbug.com/23466)
326 set_connectable(false);
327 }
328}
329
mukesh agrawal1a056262011-10-05 14:36:54 -0700330// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000331void WiFiService::ValidateWEPPassphrase(const std::string &passphrase,
332 Error *error) {
333 ParseWEPPassphrase(passphrase, NULL, NULL, error);
mukesh agrawal1a056262011-10-05 14:36:54 -0700334}
335
336// static
Thieu Lef4cbda92011-11-10 23:41:24 +0000337void WiFiService::ValidateWPAPassphrase(const std::string &passphrase,
338 Error *error) {
mukesh agrawal1a056262011-10-05 14:36:54 -0700339 unsigned int length = passphrase.length();
340 vector<uint8> passphrase_bytes;
341
342 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
343 if (length != IEEE_80211::kWPAHexLen &&
344 (length < IEEE_80211::kWPAAsciiMinLen ||
345 length > IEEE_80211::kWPAAsciiMaxLen)) {
346 error->Populate(Error::kInvalidPassphrase);
347 }
348 } else {
349 if (length < IEEE_80211::kWPAAsciiMinLen ||
350 length > IEEE_80211::kWPAAsciiMaxLen) {
351 error->Populate(Error::kInvalidPassphrase);
352 }
353 }
Thieu Lef4cbda92011-11-10 23:41:24 +0000354}
mukesh agrawal1a056262011-10-05 14:36:54 -0700355
Thieu Lef4cbda92011-11-10 23:41:24 +0000356// static
357void WiFiService::ParseWEPPassphrase(const string &passphrase,
358 int *key_index,
359 std::vector<uint8> *password_bytes,
360 Error *error) {
361 unsigned int length = passphrase.length();
362 int key_index_local;
363 std::string password_text;
364 bool is_hex = false;
365
366 switch (length) {
367 case IEEE_80211::kWEP40AsciiLen:
368 case IEEE_80211::kWEP104AsciiLen:
369 key_index_local = 0;
370 password_text = passphrase;
371 break;
372 case IEEE_80211::kWEP40AsciiLen + 2:
373 case IEEE_80211::kWEP104AsciiLen + 2:
374 if (CheckWEPKeyIndex(passphrase, error)) {
375 base::StringToInt(passphrase.substr(0,1), &key_index_local);
376 password_text = passphrase.substr(2);
377 }
378 break;
379 case IEEE_80211::kWEP40HexLen:
380 case IEEE_80211::kWEP104HexLen:
381 if (CheckWEPIsHex(passphrase, error)) {
382 key_index_local = 0;
383 password_text = passphrase;
384 is_hex = true;
385 }
386 break;
387 case IEEE_80211::kWEP40HexLen + 2:
388 case IEEE_80211::kWEP104HexLen + 2:
389 if(CheckWEPKeyIndex(passphrase, error) &&
390 CheckWEPIsHex(passphrase.substr(2), error)) {
391 base::StringToInt(passphrase.substr(0,1), &key_index_local);
392 password_text = passphrase.substr(2);
393 is_hex = true;
394 } else if (CheckWEPPrefix(passphrase, error) &&
395 CheckWEPIsHex(passphrase.substr(2), error)) {
396 key_index_local = 0;
397 password_text = passphrase.substr(2);
398 is_hex = true;
399 }
400 break;
401 case IEEE_80211::kWEP40HexLen + 4:
402 case IEEE_80211::kWEP104HexLen + 4:
403 if (CheckWEPKeyIndex(passphrase, error) &&
404 CheckWEPPrefix(passphrase.substr(2), error) &&
405 CheckWEPIsHex(passphrase.substr(4), error)) {
406 base::StringToInt(passphrase.substr(0,1), &key_index_local);
407 password_text = passphrase.substr(4);
408 is_hex = true;
409 }
410 break;
411 default:
412 error->Populate(Error::kInvalidPassphrase);
413 break;
414 }
415
mukesh agrawal1a056262011-10-05 14:36:54 -0700416 if (error->IsSuccess()) {
Thieu Lef4cbda92011-11-10 23:41:24 +0000417 if (key_index)
418 *key_index = key_index_local;
419 if (password_bytes) {
420 if (is_hex)
421 base::HexStringToBytes(password_text, password_bytes);
422 else
423 password_bytes->insert(password_bytes->end(),
424 password_text.begin(),
425 password_text.end());
426 }
mukesh agrawal1a056262011-10-05 14:36:54 -0700427 }
428}
429
430// static
431bool WiFiService::CheckWEPIsHex(const string &passphrase, Error *error) {
432 vector<uint8> passphrase_bytes;
433 if (base::HexStringToBytes(passphrase, &passphrase_bytes)) {
434 return true;
435 } else {
436 error->Populate(Error::kInvalidPassphrase);
437 return false;
438 }
439}
440
441// static
442bool WiFiService::CheckWEPKeyIndex(const string &passphrase, Error *error) {
443 if (StartsWithASCII(passphrase, "0:", false) ||
444 StartsWithASCII(passphrase, "1:", false) ||
445 StartsWithASCII(passphrase, "2:", false) ||
446 StartsWithASCII(passphrase, "3:", false)) {
447 return true;
448 } else {
449 error->Populate(Error::kInvalidPassphrase);
450 return false;
451 }
452}
453
454// static
455bool WiFiService::CheckWEPPrefix(const string &passphrase, Error *error) {
456 if (StartsWithASCII(passphrase, "0x", false)) {
457 return true;
458 } else {
459 error->Populate(Error::kInvalidPassphrase);
460 return false;
461 }
462}
463
Paul Stewart6ab23a92011-11-09 17:17:47 -0800464// static
mukesh agrawald835b202011-10-07 15:26:47 -0700465bool WiFiService::SanitizeSSID(string *ssid) {
466 CHECK(ssid);
467
468 size_t ssid_len = ssid->length();
469 size_t i;
470 bool changed = false;
471
472 for (i=0; i < ssid_len; ++i) {
473 if (!g_ascii_isprint((*ssid)[i])) {
474 (*ssid)[i] = '?';
475 changed = true;
476 }
477 }
478
479 return changed;
480}
481
Paul Stewart6ab23a92011-11-09 17:17:47 -0800482// static
483string WiFiService::GetSecurityClass(const string &security) {
484 if (security == flimflam::kSecurityRsn ||
485 security == flimflam::kSecurityWpa) {
486 return flimflam::kSecurityPsk;
Paul Stewartd08f4432011-11-04 07:48:20 -0700487 } else {
Paul Stewart6ab23a92011-11-09 17:17:47 -0800488 return security;
Paul Stewartd08f4432011-11-04 07:48:20 -0700489 }
490}
491
Paul Stewarta41e38d2011-11-11 07:47:29 -0800492// static
493bool WiFiService::ParseStorageIdentifier(const string &storage_name,
494 string *address,
495 string *mode,
496 string *security) {
497 vector<string> wifi_parts;
498 base::SplitString(storage_name, '_', &wifi_parts);
499 if (wifi_parts.size() != 5 || wifi_parts[0] != flimflam::kTypeWifi) {
500 return false;
501 }
502 *address = wifi_parts[1];
503 *mode = wifi_parts[3];
504 *security = wifi_parts[4];
505 return true;
506}
507
Paul Stewart6ab23a92011-11-09 17:17:47 -0800508string WiFiService::GetGenericStorageIdentifier() const {
509 return GetStorageIdentifierForSecurity(GetSecurityClass(security_));
510}
511
Paul Stewartd08f4432011-11-04 07:48:20 -0700512string WiFiService::GetSpecificStorageIdentifier() const {
513 return GetStorageIdentifierForSecurity(security_);
514}
515
516string WiFiService::GetStorageIdentifierForSecurity(
517 const string &security) const {
518 return StringToLowerASCII(base::StringPrintf("%s_%s_%s_%s_%s",
519 flimflam::kTypeWifi,
520 wifi_->address().c_str(),
521 hex_ssid_.c_str(),
522 mode_.c_str(),
523 security.c_str()));
524}
525
mukesh agrawalb54601c2011-06-07 17:39:22 -0700526} // namespace shill