mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 1 | // 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 Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 10 | #include <base/stringprintf.h> |
| 11 | #include <base/string_number_conversions.h> |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 12 | #include <base/string_split.h> |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 13 | #include <base/string_util.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 14 | #include <chromeos/dbus/service_constants.h> |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 15 | #include <dbus/dbus.h> |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 16 | #include <glib.h> |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 17 | |
| 18 | #include "shill/control_interface.h" |
| 19 | #include "shill/device.h" |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 20 | #include "shill/error.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 21 | #include "shill/event_dispatcher.h" |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 22 | #include "shill/ieee80211.h" |
Thieu Le | f770945 | 2011-11-15 01:13:19 +0000 | [diff] [blame] | 23 | #include "shill/property_accessor.h" |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 24 | #include "shill/store_interface.h" |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 25 | #include "shill/wifi.h" |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 26 | #include "shill/wifi_endpoint.h" |
| 27 | #include "shill/wpa_supplicant.h" |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 28 | |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame^] | 29 | using std::set; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 30 | using std::string; |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 31 | using std::vector; |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 32 | |
| 33 | namespace shill { |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 34 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 35 | const char WiFiService::kStorageHiddenSSID[] = "WiFi.HiddenSSID"; |
| 36 | |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 37 | WiFiService::WiFiService(ControlInterface *control_interface, |
| 38 | EventDispatcher *dispatcher, |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 39 | Manager *manager, |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 40 | const WiFiRefPtr &device, |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 41 | const vector<uint8_t> &ssid, |
| 42 | const string &mode, |
| 43 | const string &security, |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 44 | bool hidden_ssid) |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 45 | : Service(control_interface, dispatcher, manager, Technology::kWifi), |
Chris Masone | 7561230 | 2011-10-12 16:31:21 -0700 | [diff] [blame] | 46 | need_passphrase_(false), |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 47 | security_(security), |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 48 | mode_(mode), |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 49 | hidden_ssid_(hidden_ssid), |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 50 | task_factory_(this), |
| 51 | wifi_(device), |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 52 | ssid_(ssid) { |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 53 | PropertyStore *store = this->mutable_store(); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 54 | store->RegisterConstString(flimflam::kModeProperty, &mode_); |
Thieu Le | f770945 | 2011-11-15 01:13:19 +0000 | [diff] [blame] | 55 | HelpRegisterDerivedString(store, |
| 56 | flimflam::kPassphraseProperty, |
| 57 | NULL, |
| 58 | &WiFiService::SetPassphrase); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 59 | store->RegisterBool(flimflam::kPassphraseRequiredProperty, &need_passphrase_); |
| 60 | store->RegisterConstString(flimflam::kSecurityProperty, &security_); |
| 61 | store->RegisterConstUint8(flimflam::kSignalStrengthProperty, &strength_); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 62 | |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 63 | 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 agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 67 | |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 68 | 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 Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 77 | |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 78 | // 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 Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 102 | // Until we know better (at Profile load time), use the generic name. |
| 103 | storage_identifier_ = GetGenericStorageIdentifier(); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 104 | UpdateConnectable(); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | WiFiService::~WiFiService() { |
| 108 | LOG(INFO) << __func__; |
| 109 | } |
| 110 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 111 | void WiFiService::Connect(Error */*error*/) { |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 112 | 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 Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 116 | dispatcher()->PostTask( |
mukesh agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 117 | task_factory_.NewRunnableMethod(&WiFiService::ConnectTask)); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 120 | void WiFiService::Disconnect() { |
| 121 | // TODO(quiche) RemoveNetwork from supplicant |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 124 | bool WiFiService::TechnologyIs(const Technology::Identifier type) const { |
| 125 | return wifi_->TechnologyIs(type); |
| 126 | } |
| 127 | |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame^] | 128 | void WiFiService::AddEndpoint(WiFiEndpointConstRefPtr endpoint) { |
| 129 | DCHECK(endpoint->ssid() == ssid()); |
| 130 | endpoints_.insert(endpoint); |
| 131 | } |
| 132 | |
| 133 | void 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 Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 145 | string WiFiService::GetStorageIdentifier() const { |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 146 | return storage_identifier_; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 147 | } |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 148 | const string &WiFiService::mode() const { |
mukesh agrawal | 445e72c | 2011-06-22 11:13:50 -0700 | [diff] [blame] | 149 | return mode_; |
| 150 | } |
| 151 | |
Chris Masone | 092df3e | 2011-08-22 09:41:39 -0700 | [diff] [blame] | 152 | const string &WiFiService::key_management() const { |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 153 | return GetEAPKeyManagement(); |
mukesh agrawal | 445e72c | 2011-06-22 11:13:50 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 156 | const vector<uint8_t> &WiFiService::ssid() const { |
mukesh agrawal | 445e72c | 2011-06-22 11:13:50 -0700 | [diff] [blame] | 157 | return ssid_; |
| 158 | } |
| 159 | |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 160 | void WiFiService::SetPassphrase(const string &passphrase, Error *error) { |
| 161 | if (security_ == flimflam::kSecurityWep) { |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 162 | ValidateWEPPassphrase(passphrase, error); |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 163 | } else if (security_ == flimflam::kSecurityPsk || |
| 164 | security_ == flimflam::kSecurityWpa || |
| 165 | security_ == flimflam::kSecurityRsn) { |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 166 | ValidateWPAPassphrase(passphrase, error); |
| 167 | } else { |
| 168 | error->Populate(Error::kNotSupported); |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 169 | } |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 170 | |
| 171 | if (error->IsSuccess()) |
| 172 | passphrase_ = passphrase; |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 173 | |
| 174 | UpdateConnectable(); |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 177 | bool WiFiService::IsLoadableFrom(StoreInterface *storage) const { |
| 178 | return storage->ContainsGroup(GetGenericStorageIdentifier()) || |
| 179 | storage->ContainsGroup(GetSpecificStorageIdentifier()); |
| 180 | } |
| 181 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 182 | bool WiFiService::IsVisible() const { |
mukesh agrawal | 261daca | 2011-12-02 18:56:56 +0000 | [diff] [blame^] | 183 | const bool is_visible_in_scan = !endpoints_.empty(); |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 184 | |
| 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 Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 191 | bool 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 agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 214 | |
| 215 | // TODO(quiche): Load Passphrase property, ensure that UpdateConnectable |
| 216 | // is called (maybe via SetPassphrase). (crosbug.com/23467) |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 217 | return true; |
| 218 | } |
| 219 | |
| 220 | bool 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 agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 229 | |
| 230 | // TODO(quiche): Save Passphrase property. (crosbug.com/23467) |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 231 | return true; |
| 232 | } |
| 233 | |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 234 | bool WiFiService::IsSecurityMatch(const string &security) const { |
| 235 | return GetSecurityClass(security) == GetSecurityClass(security_); |
| 236 | } |
| 237 | |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 238 | // private methods |
Thieu Le | f770945 | 2011-11-15 01:13:19 +0000 | [diff] [blame] | 239 | void 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 agrawal | dc42bb3 | 2011-07-28 10:40:26 -0700 | [diff] [blame] | 249 | void WiFiService::ConnectTask() { |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 250 | 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 Shah | f8721ee | 2011-11-07 09:12:46 -0800 | [diff] [blame] | 259 | 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 agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 266 | } else if (security_ == flimflam::kSecurityRsn) { |
mukesh agrawal | f2fd745 | 2011-10-03 16:38:47 -0700 | [diff] [blame] | 267 | params[wpa_supplicant::kPropertySecurityProtocol].writer(). |
| 268 | append_string(wpa_supplicant::kSecurityModeRSN); |
| 269 | params[wpa_supplicant::kPropertyPreSharedKey].writer(). |
| 270 | append_string(passphrase_.c_str()); |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 271 | } 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 Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 277 | 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 agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 288 | } else if (security_ == flimflam::kSecurityNone) { |
Gaurav Shah | f8721ee | 2011-11-07 09:12:46 -0800 | [diff] [blame] | 289 | // Nothing special to do here. |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 290 | } else { |
Gaurav Shah | f8721ee | 2011-11-07 09:12:46 -0800 | [diff] [blame] | 291 | LOG(ERROR) << "Can't connect. Unsupported security method " << security_; |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | params[wpa_supplicant::kPropertyKeyManagement].writer(). |
| 295 | append_string(key_management().c_str()); |
Gaurav Shah | 7ad8e53 | 2011-11-11 17:14:49 -0800 | [diff] [blame] | 296 | |
| 297 | // See note in dbus_adaptor.cc on why we need to use a local. |
mukesh agrawal | 6e27777 | 2011-09-29 15:04:23 -0700 | [diff] [blame] | 298 | writer = params[wpa_supplicant::kNetworkPropertySSID].writer(); |
| 299 | writer << ssid_; |
| 300 | |
| 301 | wifi_->ConnectTo(this, params); |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 304 | string WiFiService::GetDeviceRpcId(Error */*error*/) { |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 305 | return wifi_->GetRpcIdentifier(); |
| 306 | } |
| 307 | |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 308 | void 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 agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 323 | // static |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 324 | void WiFiService::ValidateWEPPassphrase(const std::string &passphrase, |
| 325 | Error *error) { |
| 326 | ParseWEPPassphrase(passphrase, NULL, NULL, error); |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | // static |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 330 | void WiFiService::ValidateWPAPassphrase(const std::string &passphrase, |
| 331 | Error *error) { |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 332 | 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 Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 347 | } |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 348 | |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 349 | // static |
| 350 | void 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 agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 409 | if (error->IsSuccess()) { |
Thieu Le | f4cbda9 | 2011-11-10 23:41:24 +0000 | [diff] [blame] | 410 | 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 agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | |
| 423 | // static |
| 424 | bool 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 |
| 435 | bool 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 |
| 448 | bool 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 Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 457 | // static |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 458 | bool 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 Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 475 | // static |
| 476 | string WiFiService::GetSecurityClass(const string &security) { |
| 477 | if (security == flimflam::kSecurityRsn || |
| 478 | security == flimflam::kSecurityWpa) { |
| 479 | return flimflam::kSecurityPsk; |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 480 | } else { |
Paul Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 481 | return security; |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 485 | // static |
| 486 | bool 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 Stewart | 6ab23a9 | 2011-11-09 17:17:47 -0800 | [diff] [blame] | 501 | string WiFiService::GetGenericStorageIdentifier() const { |
| 502 | return GetStorageIdentifierForSecurity(GetSecurityClass(security_)); |
| 503 | } |
| 504 | |
Paul Stewart | d08f443 | 2011-11-04 07:48:20 -0700 | [diff] [blame] | 505 | string WiFiService::GetSpecificStorageIdentifier() const { |
| 506 | return GetStorageIdentifierForSecurity(security_); |
| 507 | } |
| 508 | |
| 509 | string 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 agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 519 | } // namespace shill |