blob: b0d4d3ac1d1853a09d7b359729e5a7ba8c566a3f [file] [log] [blame]
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Paul Stewart75897df2011-04-27 09:05:53 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Chris Masoneb2e326b2011-07-12 13:28:51 -07005#include "shill/service.h"
6
Paul Stewart75897df2011-04-27 09:05:53 -07007#include <time.h>
Paul Stewart75897df2011-04-27 09:05:53 -07008#include <stdio.h>
Chris Masoneee929b72011-05-10 10:02:18 -07009
Chris Masone8fe2c7e2011-06-09 15:51:19 -070010#include <map>
Paul Stewart75897df2011-04-27 09:05:53 -070011#include <string>
Chris Masone8fe2c7e2011-06-09 15:51:19 -070012#include <vector>
Paul Stewart75897df2011-04-27 09:05:53 -070013
Chris Masoneee929b72011-05-10 10:02:18 -070014#include <base/logging.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070015#include <base/memory/scoped_ptr.h>
mukesh agrawal51a7e932011-07-27 16:18:26 -070016#include <base/string_number_conversions.h>
Paul Stewart2bf424f2012-04-11 18:59:39 -070017#include <base/stringprintf.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070018#include <chromeos/dbus/service_constants.h>
Chris Masoneee929b72011-05-10 10:02:18 -070019
Paul Stewartbe5f5b32011-12-07 17:11:11 -080020#include "shill/connection.h"
Paul Stewart75897df2011-04-27 09:05:53 -070021#include "shill/control_interface.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070022#include "shill/error.h"
Paul Stewartbe5f5b32011-12-07 17:11:11 -080023#include "shill/http_proxy.h"
Chris Masone6791a432011-07-12 13:23:19 -070024#include "shill/manager.h"
Thieu Le48e6d6d2011-12-06 00:40:27 +000025#include "shill/metrics.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070026#include "shill/profile.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070027#include "shill/property_accessor.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070028#include "shill/refptr_types.h"
Ben Chanfad4a0b2012-04-18 15:49:59 -070029#include "shill/scope_logger.h"
Chris Masoned7732e42011-05-20 11:08:56 -070030#include "shill/service_dbus_adaptor.h"
Darin Petkov5eb05422012-05-11 15:45:25 +020031#include "shill/sockets.h"
Darin Petkovba40dd32011-07-11 20:06:39 -070032#include "shill/store_interface.h"
Paul Stewart75897df2011-04-27 09:05:53 -070033
Chris Masone8fe2c7e2011-06-09 15:51:19 -070034using std::map;
Paul Stewart75897df2011-04-27 09:05:53 -070035using std::string;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070036using std::vector;
Paul Stewart75897df2011-04-27 09:05:53 -070037
38namespace shill {
Darin Petkovba40dd32011-07-11 20:06:39 -070039
mukesh agrawalbf14e942012-03-02 14:36:34 -080040const char Service::kAutoConnConnected[] = "connected";
41const char Service::kAutoConnConnecting[] = "connecting";
42const char Service::kAutoConnExplicitDisconnect[] = "explicitly disconnected";
43const char Service::kAutoConnNotConnectable[] = "not connectable";
44
Paul Stewartbc6e7392012-05-24 07:07:48 -070045const size_t Service::kEAPMaxCertificationElements = 10;
46
Darin Petkovba40dd32011-07-11 20:06:39 -070047const char Service::kCheckPortalAuto[] = "auto";
48const char Service::kCheckPortalFalse[] = "false";
49const char Service::kCheckPortalTrue[] = "true";
50
Paul Stewartac4ac002011-08-26 12:04:26 -070051const int Service::kPriorityNone = 0;
52
Darin Petkov2f903b32012-04-18 12:56:43 +020053const char Service::kServiceSortAutoConnect[] = "AutoConnect";
54const char Service::kServiceSortConnectable[] = "Connectable";
55const char Service::kServiceSortFavorite[] = "Favorite";
mukesh agrawalddc378f2012-02-17 18:26:20 -080056const char Service::kServiceSortIsConnected[] = "IsConnected";
57const char Service::kServiceSortIsConnecting[] = "IsConnecting";
58const char Service::kServiceSortIsFailed[] = "IsFailed";
Darin Petkov2f903b32012-04-18 12:56:43 +020059const char Service::kServiceSortPriority[] = "Priority";
mukesh agrawalddc378f2012-02-17 18:26:20 -080060const char Service::kServiceSortSecurityEtc[] = "SecurityEtc";
Darin Petkov2f903b32012-04-18 12:56:43 +020061const char Service::kServiceSortTechnology[] = "Technology";
mukesh agrawalddc378f2012-02-17 18:26:20 -080062const char Service::kServiceSortUniqueName[] = "UniqueName";
63
Darin Petkovba40dd32011-07-11 20:06:39 -070064const char Service::kStorageAutoConnect[] = "AutoConnect";
65const char Service::kStorageCheckPortal[] = "CheckPortal";
66const char Service::kStorageEapAnonymousIdentity[] = "EAP.AnonymousIdentity";
67const char Service::kStorageEapCACert[] = "EAP.CACert";
68const char Service::kStorageEapCACertID[] = "EAP.CACertID";
Paul Stewartecf4cd12012-04-17 11:08:39 -070069const char Service::kStorageEapCACertNSS[] = "EAP.CACertNSS";
Darin Petkovba40dd32011-07-11 20:06:39 -070070const char Service::kStorageEapCertID[] = "EAP.CertID";
71const char Service::kStorageEapClientCert[] = "EAP.ClientCert";
72const char Service::kStorageEapEap[] = "EAP.EAP";
73const char Service::kStorageEapIdentity[] = "EAP.Identity";
74const char Service::kStorageEapInnerEap[] = "EAP.InnerEAP";
75const char Service::kStorageEapKeyID[] = "EAP.KeyID";
76const char Service::kStorageEapKeyManagement[] = "EAP.KeyMgmt";
77const char Service::kStorageEapPIN[] = "EAP.PIN";
78const char Service::kStorageEapPassword[] = "EAP.Password";
79const char Service::kStorageEapPrivateKey[] = "EAP.PrivateKey";
80const char Service::kStorageEapPrivateKeyPassword[] = "EAP.PrivateKeyPassword";
81const char Service::kStorageEapUseSystemCAs[] = "EAP.UseSystemCAs";
Paul Stewart0756db92012-01-27 08:34:47 -080082const char Service::kStorageError[] = "Error";
Darin Petkovba40dd32011-07-11 20:06:39 -070083const char Service::kStorageFavorite[] = "Favorite";
Paul Stewart0756db92012-01-27 08:34:47 -080084const char Service::kStorageGUID[] = "GUID";
mukesh agrawalcf24a242012-05-21 16:46:11 -070085const char Service::kStorageHasEverConnected[] = "HasEverConnected";
Darin Petkovba40dd32011-07-11 20:06:39 -070086const char Service::kStorageName[] = "Name";
87const char Service::kStoragePriority[] = "Priority";
88const char Service::kStorageProxyConfig[] = "ProxyConfig";
89const char Service::kStorageSaveCredentials[] = "SaveCredentials";
Paul Stewart2706aaf2011-12-14 16:44:04 -080090const char Service::kStorageType[] = "Type";
Paul Stewart987e71e2011-12-05 09:45:06 -080091const char Service::kStorageUIData[] = "UIData";
Darin Petkovba40dd32011-07-11 20:06:39 -070092
mukesh agrawal8f3f7752012-02-17 19:42:09 -080093const uint8 Service::kStrengthMax = 100;
94const uint8 Service::kStrengthMin = 0;
95
mukesh agrawal51a7e932011-07-27 16:18:26 -070096// static
97unsigned int Service::serial_number_ = 0;
98
Paul Stewart75897df2011-04-27 09:05:53 -070099Service::Service(ControlInterface *control_interface,
mukesh agrawalb54601c2011-06-07 17:39:22 -0700100 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -0800101 Metrics *metrics,
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700102 Manager *manager,
Gaurav Shah435de2c2011-11-17 19:01:07 -0800103 Technology::Identifier technology)
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400104 : state_(kStateIdle),
Paul Stewart03dba0b2011-08-22 16:32:45 -0700105 failure_(kFailureUnknown),
106 auto_connect_(false),
Darin Petkovba40dd32011-07-11 20:06:39 -0700107 check_portal_(kCheckPortalAuto),
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700108 connectable_(false),
mukesh agrawaladb68482012-01-17 16:31:51 -0800109 explicitly_disconnected_(false),
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700110 favorite_(false),
Darin Petkovba40dd32011-07-11 20:06:39 -0700111 priority_(kPriorityNone),
Paul Stewart1ca3e852011-11-04 07:50:49 -0700112 security_level_(0),
Paul Stewart22aa71b2011-09-16 12:15:11 -0700113 strength_(0),
Darin Petkovba40dd32011-07-11 20:06:39 -0700114 save_credentials_(true),
Gaurav Shah435de2c2011-11-17 19:01:07 -0800115 technology_(technology),
mukesh agrawal568b5c62012-02-28 14:44:47 -0800116 failed_time_(0),
mukesh agrawalcf24a242012-05-21 16:46:11 -0700117 has_ever_connected_(false),
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700118 dispatcher_(dispatcher),
mukesh agrawald835b202011-10-07 15:26:47 -0700119 unique_name_(base::UintToString(serial_number_++)),
120 friendly_name_(unique_name_),
Chris Masonea82b7112011-05-25 15:16:29 -0700121 available_(false),
122 configured_(false),
Chris Masonea82b7112011-05-25 15:16:29 -0700123 configuration_(NULL),
Chris Masone6791a432011-07-12 13:23:19 -0700124 adaptor_(control_interface->CreateServiceAdaptor(this)),
Thieu Le3426c8f2012-01-11 17:35:11 -0800125 metrics_(metrics),
Darin Petkov5eb05422012-05-11 15:45:25 +0200126 manager_(manager),
127 sockets_(new Sockets()) {
Thieu Le284fe792012-01-31 17:53:19 -0800128 HelpRegisterDerivedBool(flimflam::kAutoConnectProperty,
129 &Service::GetAutoConnect,
130 &Service::SetAutoConnect);
Chris Masone4d42df82011-07-02 17:09:39 -0700131
132 // flimflam::kActivationStateProperty: Registered in CellularService
133 // flimflam::kCellularApnProperty: Registered in CellularService
134 // flimflam::kCellularLastGoodApnProperty: Registered in CellularService
135 // flimflam::kNetworkTechnologyProperty: Registered in CellularService
Darin Petkov3335b372011-08-22 11:05:32 -0700136 // flimflam::kOperatorNameProperty: DEPRECATED
137 // flimflam::kOperatorCodeProperty: DEPRECATED
Chris Masone4d42df82011-07-02 17:09:39 -0700138 // flimflam::kRoamingStateProperty: Registered in CellularService
Darin Petkov3335b372011-08-22 11:05:32 -0700139 // flimflam::kServingOperatorProperty: Registered in CellularService
Chris Masone4d42df82011-07-02 17:09:39 -0700140 // flimflam::kPaymentURLProperty: Registered in CellularService
141
Paul Stewartd215af62012-04-24 23:25:50 -0700142 HelpRegisterDerivedString(flimflam::kCheckPortalProperty,
143 &Service::GetCheckPortal,
144 &Service::SetCheckPortal);
Chris Masone27c4aa52011-07-02 13:10:14 -0700145 store_.RegisterConstBool(flimflam::kConnectableProperty, &connectable_);
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400146 HelpRegisterDerivedRpcIdentifier(flimflam::kDeviceProperty,
147 &Service::GetDeviceRpcId,
148 NULL);
Paul Stewart4c561612012-03-21 12:49:01 -0700149 store_.RegisterString(flimflam::kGuidProperty, &guid_);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700150
Chris Masoneb2e326b2011-07-12 13:28:51 -0700151 store_.RegisterString(flimflam::kEapIdentityProperty, &eap_.identity);
152 store_.RegisterString(flimflam::kEAPEAPProperty, &eap_.eap);
153 store_.RegisterString(flimflam::kEapPhase2AuthProperty, &eap_.inner_eap);
Chris Masone27c4aa52011-07-02 13:10:14 -0700154 store_.RegisterString(flimflam::kEapAnonymousIdentityProperty,
Chris Masoneb2e326b2011-07-12 13:28:51 -0700155 &eap_.anonymous_identity);
156 store_.RegisterString(flimflam::kEAPClientCertProperty, &eap_.client_cert);
157 store_.RegisterString(flimflam::kEAPCertIDProperty, &eap_.cert_id);
158 store_.RegisterString(flimflam::kEapPrivateKeyProperty, &eap_.private_key);
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800159 HelpRegisterWriteOnlyDerivedString(flimflam::kEapPrivateKeyPasswordProperty,
160 &Service::SetEAPPrivateKeyPassword,
161 NULL,
162 &eap_.private_key_password);
Chris Masoneb2e326b2011-07-12 13:28:51 -0700163 store_.RegisterString(flimflam::kEAPKeyIDProperty, &eap_.key_id);
164 store_.RegisterString(flimflam::kEapCaCertProperty, &eap_.ca_cert);
165 store_.RegisterString(flimflam::kEapCaCertIDProperty, &eap_.ca_cert_id);
Paul Stewartecf4cd12012-04-17 11:08:39 -0700166 store_.RegisterString(flimflam::kEapCaCertNssProperty, &eap_.ca_cert_nss);
Chris Masoneb2e326b2011-07-12 13:28:51 -0700167 store_.RegisterString(flimflam::kEAPPINProperty, &eap_.pin);
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800168 HelpRegisterWriteOnlyDerivedString(flimflam::kEapPasswordProperty,
169 &Service::SetEAPPassword,
170 NULL,
171 &eap_.password);
Chris Masoneb2e326b2011-07-12 13:28:51 -0700172 store_.RegisterString(flimflam::kEapKeyMgmtProperty, &eap_.key_management);
173 store_.RegisterBool(flimflam::kEapUseSystemCAsProperty, &eap_.use_system_cas);
Paul Stewartbc6e7392012-05-24 07:07:48 -0700174 store_.RegisterConstStrings(shill::kEapRemoteCertificationProperty,
175 &eap_.remote_certification);
176 store_.RegisterString(shill::kEapSubjectMatchProperty,
177 &eap_.subject_match);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700178
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400179 // TODO(ers): in flimflam clearing Error has the side-effect of
180 // setting the service state to IDLE. Is this important? I could
181 // see an autotest depending on it.
Chris Masone27c4aa52011-07-02 13:10:14 -0700182 store_.RegisterConstString(flimflam::kErrorProperty, &error_);
183 store_.RegisterConstBool(flimflam::kFavoriteProperty, &favorite_);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800184 HelpRegisterDerivedUint16(shill::kHTTPProxyPortProperty,
185 &Service::GetHTTPProxyPort,
186 NULL);
Paul Stewart10241e32012-04-23 18:15:06 -0700187 HelpRegisterDerivedRpcIdentifier(shill::kIPConfigProperty,
188 &Service::GetIPConfigRpcIdentifier,
189 NULL);
Chris Masone27c4aa52011-07-02 13:10:14 -0700190 HelpRegisterDerivedBool(flimflam::kIsActiveProperty,
Chris Masoneb2e326b2011-07-12 13:28:51 -0700191 &Service::IsActive,
192 NULL);
Chris Masone4d42df82011-07-02 17:09:39 -0700193 // flimflam::kModeProperty: Registered in WiFiService
Paul Stewart0c438332012-04-11 07:55:27 -0700194
195 // Although this is a read-only property, some callers want to blindly
196 // set this value to its current value.
197 HelpRegisterDerivedString(flimflam::kNameProperty,
198 &Service::GetNameProperty,
199 &Service::AssertTrivialSetNameProperty);
Chris Masone4d42df82011-07-02 17:09:39 -0700200 // flimflam::kPassphraseProperty: Registered in WiFiService
201 // flimflam::kPassphraseRequiredProperty: Registered in WiFiService
Chris Masone27c4aa52011-07-02 13:10:14 -0700202 store_.RegisterInt32(flimflam::kPriorityProperty, &priority_);
203 HelpRegisterDerivedString(flimflam::kProfileProperty,
204 &Service::GetProfileRpcId,
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800205 &Service::SetProfileRpcId);
Chris Masone4d42df82011-07-02 17:09:39 -0700206 store_.RegisterString(flimflam::kProxyConfigProperty, &proxy_config_);
Chris Masoneb2e326b2011-07-12 13:28:51 -0700207 store_.RegisterBool(flimflam::kSaveCredentialsProperty, &save_credentials_);
Gaurav Shah435de2c2011-11-17 19:01:07 -0800208 HelpRegisterDerivedString(flimflam::kTypeProperty,
209 &Service::GetTechnologyString,
210 NULL);
Chris Masone4d42df82011-07-02 17:09:39 -0700211 // flimflam::kSecurityProperty: Registered in WiFiService
Chris Masone27c4aa52011-07-02 13:10:14 -0700212 HelpRegisterDerivedString(flimflam::kStateProperty,
213 &Service::CalculateState,
214 NULL);
Darin Petkovd78ee7e2012-01-12 11:21:10 +0100215 store_.RegisterConstUint8(flimflam::kSignalStrengthProperty, &strength_);
Paul Stewart987e71e2011-12-05 09:45:06 -0800216 store_.RegisterString(flimflam::kUIDataProperty, &ui_data_);
Chris Masone4d42df82011-07-02 17:09:39 -0700217 // flimflam::kWifiAuthMode: Registered in WiFiService
218 // flimflam::kWifiHiddenSsid: Registered in WiFiService
219 // flimflam::kWifiFrequency: Registered in WiFiService
220 // flimflam::kWifiPhyMode: Registered in WiFiService
221 // flimflam::kWifiHexSsid: Registered in WiFiService
Thieu Le48e6d6d2011-12-06 00:40:27 +0000222
223 metrics_->RegisterService(this);
224
Paul Stewart1062d9d2012-04-27 10:42:27 -0700225 static_ip_parameters_.PlumbPropertyStore(&store_);
226
Paul Stewartcb59fed2012-03-21 21:14:46 -0700227 IgnoreParameterForConfigure(flimflam::kTypeProperty);
Paul Stewart7f61e522012-03-22 11:13:45 -0700228 IgnoreParameterForConfigure(flimflam::kProfileProperty);
Paul Stewartcb59fed2012-03-21 21:14:46 -0700229
Ben Chanfad4a0b2012-04-18 15:49:59 -0700230 SLOG(Service, 2) << "Service initialized.";
Paul Stewart75897df2011-04-27 09:05:53 -0700231}
232
Thieu Le48e6d6d2011-12-06 00:40:27 +0000233Service::~Service() {
234 metrics_->DeregisterService(this);
235}
Paul Stewart75897df2011-04-27 09:05:53 -0700236
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000237void Service::AutoConnect() {
mukesh agrawalbf14e942012-03-02 14:36:34 -0800238 const char *reason;
239 if (this->IsAutoConnectable(&reason)) {
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000240 Error error;
241 Connect(&error);
mukesh agrawal76d13882012-01-12 15:23:11 -0800242 } else {
mukesh agrawalbf14e942012-03-02 14:36:34 -0800243 LOG(INFO) << "Suppressed autoconnect to " << friendly_name() << " "
244 << "(" << reason << ")";
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000245 }
246}
247
mukesh agrawaladb68482012-01-17 16:31:51 -0800248void Service::Connect(Error */*error*/) {
249 explicitly_disconnected_ = false;
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400250 // clear any failure state from a previous connect attempt
251 SetState(kStateIdle);
mukesh agrawaladb68482012-01-17 16:31:51 -0800252}
253
254void Service::Disconnect(Error */*error*/) {
255 explicitly_disconnected_ = true;
256}
257
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500258void Service::ActivateCellularModem(const string &/*carrier*/,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500259 Error *error,
260 const ResultCallback &/*callback*/) {
261 Error::PopulateAndLog(error, Error::kNotSupported,
262 "Service doesn't support cellular modem activation.");
Darin Petkovc408e692011-08-17 13:47:15 -0700263}
264
mukesh agrawal1830fa12011-09-26 14:31:40 -0700265bool Service::TechnologyIs(const Technology::Identifier /*type*/) const {
Paul Stewart22aa71b2011-09-16 12:15:11 -0700266 return false;
267}
268
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800269bool Service::IsActive(Error */*error*/) {
Paul Stewart03dba0b2011-08-22 16:32:45 -0700270 return state_ != kStateUnknown &&
271 state_ != kStateIdle &&
272 state_ != kStateFailure;
273}
274
275void Service::SetState(ConnectState state) {
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800276 LOG(INFO) << "In " << __func__ << "(): Service " << friendly_name_
277 << " state " << ConnectStateToString(state_) << " -> "
278 << ConnectStateToString(state);
mukesh agrawal7ec71312011-11-10 02:08:26 +0000279
Paul Stewart03dba0b2011-08-22 16:32:45 -0700280 if (state == state_) {
281 return;
282 }
283
284 state_ = state;
285 if (state != kStateFailure) {
286 failure_ = kFailureUnknown;
mukesh agrawal568b5c62012-02-28 14:44:47 -0800287 failed_time_ = 0;
Paul Stewart03dba0b2011-08-22 16:32:45 -0700288 }
mukesh agrawalcf24a242012-05-21 16:46:11 -0700289 if (state == kStateConnected) {
290 has_ever_connected_ = true;
291 SaveToProfile();
292 }
Paul Stewart03dba0b2011-08-22 16:32:45 -0700293 manager_->UpdateService(this);
Thieu Le48e6d6d2011-12-06 00:40:27 +0000294 metrics_->NotifyServiceStateChanged(this, state);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800295 Error error;
296 adaptor_->EmitStringChanged(flimflam::kStateProperty, CalculateState(&error));
Paul Stewart03dba0b2011-08-22 16:32:45 -0700297}
298
299void Service::SetFailure(ConnectFailure failure) {
300 failure_ = failure;
mukesh agrawal568b5c62012-02-28 14:44:47 -0800301 failed_time_ = time(NULL);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700302 SetState(kStateFailure);
303}
304
mukesh agrawal568b5c62012-02-28 14:44:47 -0800305void Service::SetFailureSilent(ConnectFailure failure) {
306 // Note that order matters here, since SetState modifies |failure_| and
307 // |failed_time_|.
308 SetState(kStateIdle);
309 failure_ = failure;
310 failed_time_ = time(NULL);
311}
312
Chris Masone6791a432011-07-12 13:23:19 -0700313string Service::GetRpcIdentifier() const {
Chris Masone3c3f6a12011-07-01 10:01:41 -0700314 return adaptor_->GetRpcIdentifier();
315}
316
Paul Stewartbba6a5b2011-11-02 18:45:59 -0700317bool Service::IsLoadableFrom(StoreInterface *storage) const {
318 return storage->ContainsGroup(GetStorageIdentifier());
319}
320
Chris Masone9d779932011-08-25 16:33:41 -0700321bool Service::Load(StoreInterface *storage) {
322 const string id = GetStorageIdentifier();
Chris Masone34af2182011-08-22 11:59:36 -0700323 if (!storage->ContainsGroup(id)) {
324 LOG(WARNING) << "Service is not available in the persistent store: " << id;
325 return false;
326 }
327 storage->GetBool(id, kStorageAutoConnect, &auto_connect_);
328 storage->GetString(id, kStorageCheckPortal, &check_portal_);
329 storage->GetBool(id, kStorageFavorite, &favorite_);
mukesh agrawalcf24a242012-05-21 16:46:11 -0700330 storage->GetString(id, kStorageGUID, &guid_);
331 storage->GetBool(id, kStorageHasEverConnected, &has_ever_connected_);
Chris Masone34af2182011-08-22 11:59:36 -0700332 storage->GetInt(id, kStoragePriority, &priority_);
333 storage->GetString(id, kStorageProxyConfig, &proxy_config_);
334 storage->GetBool(id, kStorageSaveCredentials, &save_credentials_);
Paul Stewart987e71e2011-12-05 09:45:06 -0800335 storage->GetString(id, kStorageUIData, &ui_data_);
Chris Masone34af2182011-08-22 11:59:36 -0700336
337 LoadEapCredentials(storage, id);
Paul Stewart1062d9d2012-04-27 10:42:27 -0700338 static_ip_parameters_.Load(storage, id);
Chris Masone34af2182011-08-22 11:59:36 -0700339 // TODO(petkov): Load these:
340
Chris Masone34af2182011-08-22 11:59:36 -0700341 // "Failure"
342 // "Modified"
343 // "LastAttempt"
Chris Masone34af2182011-08-22 11:59:36 -0700344
mukesh agrawaladb68482012-01-17 16:31:51 -0800345 explicitly_disconnected_ = false;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800346 favorite_ = true;
347
Chris Masone34af2182011-08-22 11:59:36 -0700348 return true;
Darin Petkovba40dd32011-07-11 20:06:39 -0700349}
350
Paul Stewart65512e12012-03-26 18:01:08 -0700351bool Service::Unload() {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800352 auto_connect_ = false;
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800353 check_portal_ = kCheckPortalAuto;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800354 favorite_ = false;
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800355 priority_ = kPriorityNone;
356 proxy_config_ = "";
357 save_credentials_ = true;
358 ui_data_ = "";
359
360 UnloadEapCredentials();
Wade Guthrie9e0c2502012-04-19 15:26:40 -0700361 Error error; // Ignored.
362 Disconnect(&error);
Paul Stewart65512e12012-03-26 18:01:08 -0700363 return false;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800364}
365
Chris Masone9d779932011-08-25 16:33:41 -0700366bool Service::Save(StoreInterface *storage) {
367 const string id = GetStorageIdentifier();
Chris Masone34af2182011-08-22 11:59:36 -0700368
Paul Stewart2706aaf2011-12-14 16:44:04 -0800369 storage->SetString(id, kStorageType, GetTechnologyString(NULL));
370
Chris Masone34af2182011-08-22 11:59:36 -0700371 // TODO(petkov): We could choose to simplify the saving code by removing most
372 // conditionals thus saving even default values.
Wade Guthrie4f28e8b2012-04-11 10:52:07 -0700373 storage->SetBool(id, kStorageAutoConnect, auto_connect_);
Chris Masone34af2182011-08-22 11:59:36 -0700374 if (check_portal_ == kCheckPortalAuto) {
375 storage->DeleteKey(id, kStorageCheckPortal);
376 } else {
377 storage->SetString(id, kStorageCheckPortal, check_portal_);
378 }
379 storage->SetBool(id, kStorageFavorite, favorite_);
mukesh agrawalcf24a242012-05-21 16:46:11 -0700380 SaveString(storage, id, kStorageGUID, guid_, false, true);
381 storage->SetBool(id, kStorageHasEverConnected, has_ever_connected_);
mukesh agrawald835b202011-10-07 15:26:47 -0700382 storage->SetString(id, kStorageName, friendly_name_);
Chris Masone34af2182011-08-22 11:59:36 -0700383 if (priority_ != kPriorityNone) {
384 storage->SetInt(id, kStoragePriority, priority_);
385 } else {
386 storage->DeleteKey(id, kStoragePriority);
387 }
Paul Stewart987e71e2011-12-05 09:45:06 -0800388 SaveString(storage, id, kStorageProxyConfig, proxy_config_, false, true);
Chris Masone34af2182011-08-22 11:59:36 -0700389 if (save_credentials_) {
390 storage->DeleteKey(id, kStorageSaveCredentials);
391 } else {
392 storage->SetBool(id, kStorageSaveCredentials, false);
393 }
Paul Stewart987e71e2011-12-05 09:45:06 -0800394 SaveString(storage, id, kStorageUIData, ui_data_, false, true);
Chris Masone34af2182011-08-22 11:59:36 -0700395
396 SaveEapCredentials(storage, id);
Paul Stewart1062d9d2012-04-27 10:42:27 -0700397 static_ip_parameters_.Save(storage, id);
Chris Masone34af2182011-08-22 11:59:36 -0700398
399 // TODO(petkov): Save these:
400
Chris Masone34af2182011-08-22 11:59:36 -0700401 // "Failure"
402 // "Modified"
403 // "LastAttempt"
Chris Masone34af2182011-08-22 11:59:36 -0700404
405 return true;
406}
407
Gary Moraind93615e2012-04-27 11:50:03 -0700408void Service::SaveToCurrentProfile() {
409 // Some unittests do not specify a manager.
410 if (manager()) {
411 manager()->SaveServiceToProfile(this);
412 }
Thieu Led4e9e552012-02-16 16:26:07 -0800413}
414
Paul Stewartcb59fed2012-03-21 21:14:46 -0700415void Service::Configure(const KeyValueStore &args, Error *error) {
416 map<string, bool>::const_iterator bool_it;
Ben Chanfad4a0b2012-04-18 15:49:59 -0700417 SLOG(Service, 5) << "Configuring bool properties:";
Paul Stewartcb59fed2012-03-21 21:14:46 -0700418 for (bool_it = args.bool_properties().begin();
419 bool_it != args.bool_properties().end();
420 ++bool_it) {
421 if (ContainsKey(parameters_ignored_for_configure_, bool_it->first)) {
422 continue;
423 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700424 SLOG(Service, 5) << " " << bool_it->first;
Paul Stewartcb59fed2012-03-21 21:14:46 -0700425 Error set_error;
426 store_.SetBoolProperty(bool_it->first, bool_it->second, &set_error);
427 if (error->IsSuccess() && set_error.IsFailure()) {
428 error->CopyFrom(set_error);
429 }
430 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700431 SLOG(Service, 5) << "Configuring string properties:";
Paul Stewartcb59fed2012-03-21 21:14:46 -0700432 map<string, string>::const_iterator string_it;
433 for (string_it = args.string_properties().begin();
434 string_it != args.string_properties().end();
435 ++string_it) {
436 if (ContainsKey(parameters_ignored_for_configure_, string_it->first)) {
437 continue;
438 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700439 SLOG(Service, 5) << " " << string_it->first;
Paul Stewartcb59fed2012-03-21 21:14:46 -0700440 Error set_error;
441 store_.SetStringProperty(string_it->first, string_it->second, &set_error);
442 if (error->IsSuccess() && set_error.IsFailure()) {
443 error->CopyFrom(set_error);
444 }
445 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700446 SLOG(Service, 5) << "Configuring uint32 properties:";
Paul Stewartcb59fed2012-03-21 21:14:46 -0700447 map<string, uint32>::const_iterator int_it;
448 for (int_it = args.uint_properties().begin();
449 int_it != args.uint_properties().end();
450 ++int_it) {
451 if (ContainsKey(parameters_ignored_for_configure_, int_it->first)) {
452 continue;
453 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700454 SLOG(Service, 5) << " " << int_it->first;
Paul Stewartcb59fed2012-03-21 21:14:46 -0700455 Error set_error;
456 store_.SetUint32Property(int_it->first, int_it->second, &set_error);
457 if (error->IsSuccess() && set_error.IsFailure()) {
458 error->CopyFrom(set_error);
459 }
460 }
461}
462
Paul Stewart10ccbb32012-04-26 15:59:30 -0700463bool Service::IsRemembered() const {
464 return profile_ && !manager_->IsServiceEphemeral(this);
465}
466
mukesh agrawal00917ce2011-11-22 23:56:55 +0000467void Service::MakeFavorite() {
468 if (favorite_) {
469 // We do not want to clobber the value of auto_connect_ (it may
470 // be user-set). So return early.
471 return;
472 }
473
474 auto_connect_ = true;
475 favorite_ = true;
476}
477
Darin Petkov5eb05422012-05-11 15:45:25 +0200478void Service::SetConnection(const ConnectionRefPtr &connection) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800479 if (connection.get()) {
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800480 http_proxy_.reset(new HTTPProxy(connection));
Darin Petkov5eb05422012-05-11 15:45:25 +0200481 http_proxy_->Start(dispatcher_, sockets_.get());
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800482 } else {
483 http_proxy_.reset();
484 }
485 connection_ = connection;
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800486}
487
Gaurav Shah10109f22011-11-11 20:16:22 -0800488bool Service::Is8021xConnectable() const {
489 // We mirror all the flimflam checks (see service.c:is_connectable()).
490
491 // Identity is required.
492 if (eap_.identity.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700493 SLOG(Service, 2) << "Not connectable: Identity is empty.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800494 return false;
495 }
496
497 if (!eap_.client_cert.empty() || !eap_.cert_id.empty()) {
498 // If a client certificate is being used, we must have a private key.
499 if (eap_.private_key.empty() && eap_.key_id.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700500 SLOG(Service, 2)
501 << "Not connectable. Client certificate but no private key.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800502 return false;
503 }
504 }
505 if (!eap_.cert_id.empty() || !eap_.key_id.empty() ||
506 !eap_.ca_cert_id.empty()) {
507 // If PKCS#11 data is needed, a PIN is required.
508 if (eap_.pin.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700509 SLOG(Service, 2) << "Not connectable. PKCS#11 data but no PIN.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800510 return false;
511 }
512 }
513
514 // For EAP-TLS, a client certificate is required.
515 if (eap_.eap.empty() || eap_.eap == "TLS") {
Paul Stewart81426132012-05-16 10:05:10 -0700516 if ((!eap_.client_cert.empty() || !eap_.cert_id.empty()) &&
517 (!eap_.private_key.empty() || !eap_.key_id.empty())) {
518 SLOG(Service, 2) << "Connectable. EAP-TLS with a client cert and key.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800519 return true;
520 }
521 }
522
523 // For EAP types other than TLS (e.g. EAP-TTLS or EAP-PEAP, password is the
524 // minimum requirement), at least an identity + password is required.
525 if (eap_.eap.empty() || eap_.eap != "TLS") {
526 if (!eap_.password.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700527 SLOG(Service, 2) << "Connectable. !EAP-TLS and has a password.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800528 return true;
529 }
530 }
531
Ben Chanfad4a0b2012-04-18 15:49:59 -0700532 SLOG(Service, 2)
533 << "Not connectable. No suitable EAP configuration was found.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800534 return false;
535}
536
Paul Stewartbc6e7392012-05-24 07:07:48 -0700537bool Service::AddEAPCertification(const string &name, size_t depth) {
538 if (depth >= kEAPMaxCertificationElements) {
539 LOG(WARNING) << "Ignoring certification " << name
540 << " because depth " << depth
541 << " exceeds our maximum of "
542 << kEAPMaxCertificationElements;
543 return false;
544 }
545
546 if (depth >= eap_.remote_certification.size()) {
547 eap_.remote_certification.resize(depth + 1);
548 } else if (name == eap_.remote_certification[depth]) {
549 return true;
550 }
551
552 eap_.remote_certification[depth] = name;
553 LOG(INFO) << "Received certification for "
554 << name
555 << " at depth "
556 << depth;
557 return true;
558}
559
560void Service::ClearEAPCertification() {
561 eap_.remote_certification.clear();
562}
563
Gaurav Shah10109f22011-11-11 20:16:22 -0800564void Service::set_eap(const EapCredentials &eap) {
565 eap_ = eap;
566 // Note: Connectability can only be updated by a subclass of Service
567 // with knowledge of whether the service actually uses 802.1x credentials.
568}
569
mukesh agrawal00917ce2011-11-22 23:56:55 +0000570// static
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800571const char *Service::ConnectFailureToString(const ConnectFailure &state) {
572 switch (state) {
573 case kFailureUnknown:
574 return "Unknown";
575 case kFailureActivationFailure:
576 return "Activation Failure";
577 case kFailureOutOfRange:
578 return "Out of range";
579 case kFailurePinMissing:
580 return "PIN missing";
581 case kFailureConfigurationFailed:
582 return "Configuration Failed";
583 case kFailureBadCredentials:
584 return "Bad Credentials";
585 case kFailureNeedEVDO:
586 return "Need EVDO";
587 case kFailureNeedHomeNetwork:
588 return "Need Home Network";
589 case kFailureOTASPFailure:
590 return "OTASP Failure";
591 case kFailureAAAFailure:
592 return "AAA Failure";
Thieu Le48e6d6d2011-12-06 00:40:27 +0000593 case kFailureMax:
594 return "Max failure error code";
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800595 }
596 return "Invalid";
597}
598
599// static
600const char *Service::ConnectStateToString(const ConnectState &state) {
601 switch (state) {
602 case kStateUnknown:
603 return "Unknown";
604 case kStateIdle:
605 return "Idle";
606 case kStateAssociating:
607 return "Associating";
608 case kStateConfiguring:
609 return "Configuring";
610 case kStateConnected:
611 return "Connected";
612 case kStateDisconnected:
613 return "Disconnected";
Thieu Le48e6d6d2011-12-06 00:40:27 +0000614 case kStatePortal:
615 return "Portal";
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800616 case kStateFailure:
617 return "Failure";
618 case kStateOnline:
619 return "Online";
620 }
621 return "Invalid";
622}
623
Gaurav Shah435de2c2011-11-17 19:01:07 -0800624string Service::GetTechnologyString(Error */*error*/) {
625 return Technology::NameFromIdentifier(technology());
626}
627
Jason Glasgowb5790052012-01-27 01:03:52 -0500628// static
Paul Stewart22aa71b2011-09-16 12:15:11 -0700629bool Service::DecideBetween(int a, int b, bool *decision) {
630 if (a == b)
631 return false;
632 *decision = (a > b);
633 return true;
634}
635
mukesh agrawal00917ce2011-11-22 23:56:55 +0000636// static
Paul Stewart22aa71b2011-09-16 12:15:11 -0700637bool Service::Compare(ServiceRefPtr a,
638 ServiceRefPtr b,
mukesh agrawalddc378f2012-02-17 18:26:20 -0800639 const vector<Technology::Identifier> &tech_order,
640 const char **reason) {
Paul Stewart22aa71b2011-09-16 12:15:11 -0700641 bool ret;
642
643 if (a->state() != b->state()) {
644 if (DecideBetween(a->IsConnected(), b->IsConnected(), &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800645 *reason = kServiceSortIsConnected;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700646 return ret;
647 }
648
649 // TODO(pstew): Services don't know about portal state yet
650
651 if (DecideBetween(a->IsConnecting(), b->IsConnecting(), &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800652 *reason = kServiceSortIsConnecting;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700653 return ret;
654 }
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000655
656 if (DecideBetween(!a->IsFailed(), !b->IsFailed(), &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800657 *reason = kServiceSortIsFailed;
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000658 return ret;
659 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700660 }
661
Darin Petkov2f903b32012-04-18 12:56:43 +0200662 if (DecideBetween(a->connectable(), b->connectable(), &ret)) {
663 *reason = kServiceSortConnectable;
664 return ret;
665 }
666
667 // Ignore the auto-connect property if both services are connected
668 // already. This allows connected non-autoconnectable VPN services to be
669 // sorted higher than other connected services based on technology order.
670 if (!a->IsConnected() &&
671 DecideBetween(a->auto_connect(), b->auto_connect(), &ret)) {
672 *reason = kServiceSortAutoConnect;
673 return ret;
674 }
675
676 if (DecideBetween(a->favorite(), b->favorite(), &ret)) {
677 *reason = kServiceSortFavorite;
678 return ret;
679 }
680
681 if (DecideBetween(a->priority(), b->priority(), &ret)) {
682 *reason = kServiceSortPriority;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700683 return ret;
684 }
685
686 // TODO(pstew): Below this point we are making value judgements on
687 // services that are not related to anything intrinsic or
688 // user-specified. These heuristics should be richer (contain
689 // historical information, for example) and be subject to user
690 // customization.
Paul Stewart22aa71b2011-09-16 12:15:11 -0700691 for (vector<Technology::Identifier>::const_iterator it = tech_order.begin();
692 it != tech_order.end();
693 ++it) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800694 if (DecideBetween(a->TechnologyIs(*it), b->TechnologyIs(*it), &ret)) {
695 *reason = kServiceSortTechnology;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700696 return ret;
mukesh agrawalddc378f2012-02-17 18:26:20 -0800697 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700698 }
699
Paul Stewart1ca3e852011-11-04 07:50:49 -0700700 if (DecideBetween(a->security_level(), b->security_level(), &ret) ||
Paul Stewart22aa71b2011-09-16 12:15:11 -0700701 DecideBetween(a->strength(), b->strength(), &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800702 *reason = kServiceSortSecurityEtc;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700703 return ret;
704 }
705
mukesh agrawalddc378f2012-02-17 18:26:20 -0800706 *reason = kServiceSortUniqueName;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700707 return a->UniqueName() < b->UniqueName();
708}
709
Chris Masone34af2182011-08-22 11:59:36 -0700710const ProfileRefPtr &Service::profile() const { return profile_; }
711
712void Service::set_profile(const ProfileRefPtr &p) { profile_ = p; }
713
Paul Stewartff14b022012-04-24 20:06:23 -0700714void Service::OnPropertyChanged(const string &property) {
Paul Stewart81426132012-05-16 10:05:10 -0700715 if (Is8021x() &&
716 (property == flimflam::kEAPCertIDProperty ||
717 property == flimflam::kEAPClientCertProperty ||
718 property == flimflam::kEAPKeyIDProperty ||
719 property == flimflam::kEAPPINProperty ||
720 property == flimflam::kEapCaCertIDProperty ||
721 property == flimflam::kEapIdentityProperty ||
722 property == flimflam::kEapPasswordProperty ||
723 property == flimflam::kEapPrivateKeyProperty)) {
724 // This notifies subclassess that EAP parameters have been changed.
725 set_eap(eap_);
726 }
mukesh agrawalcf24a242012-05-21 16:46:11 -0700727 SaveToProfile();
Paul Stewartd215af62012-04-24 23:25:50 -0700728 if ((property == flimflam::kCheckPortalProperty ||
729 property == flimflam::kProxyConfigProperty) &&
730 (state_ == kStateConnected ||
731 state_ == kStatePortal ||
732 state_ == kStateOnline)) {
733 manager_->RecheckPortalOnService(this);
734 }
Paul Stewartff14b022012-04-24 20:06:23 -0700735}
736
Paul Stewart10241e32012-04-23 18:15:06 -0700737string Service::GetIPConfigRpcIdentifier(Error *error) {
738 if (!connection_) {
739 error->Populate(Error::kNotFound);
740 return "/";
741 }
742
743 string id = connection_->ipconfig_rpc_identifier();
744
745 if (id.empty()) {
746 // Do not return an empty IPConfig.
747 error->Populate(Error::kNotFound);
748 return "/";
749 }
750
751 return id;
752}
753
mukesh agrawal29c13a12011-11-24 00:09:19 +0000754void Service::set_connectable(bool connectable) {
755 connectable_ = connectable;
756 adaptor_->EmitBoolChanged(flimflam::kConnectableProperty, connectable_);
757}
758
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800759string Service::CalculateState(Error */*error*/) {
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700760 switch (state_) {
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800761 case kStateIdle:
762 return flimflam::kStateIdle;
763 case kStateAssociating:
764 return flimflam::kStateAssociation;
765 case kStateConfiguring:
766 return flimflam::kStateConfiguration;
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700767 case kStateConnected:
Paul Stewart20088d82012-02-16 06:58:55 -0800768 return flimflam::kStateReady;
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800769 case kStateDisconnected:
770 return flimflam::kStateDisconnect;
771 case kStateFailure:
772 return flimflam::kStateFailure;
Paul Stewart20088d82012-02-16 06:58:55 -0800773 case kStatePortal:
774 return flimflam::kStatePortal;
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800775 case kStateOnline:
776 return flimflam::kStateOnline;
777 case kStateUnknown:
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700778 default:
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800779 return "";
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700780 }
781}
782
mukesh agrawalbf14e942012-03-02 14:36:34 -0800783bool Service::IsAutoConnectable(const char **reason) const {
784 if (!connectable()) {
785 *reason = kAutoConnNotConnectable;
786 return false;
787 }
788
789 if (IsConnected()) {
790 *reason = kAutoConnConnected;
791 return false;
792 }
793
794 if (IsConnecting()) {
795 *reason = kAutoConnConnecting;
796 return false;
797 }
798
799 if (explicitly_disconnected_) {
800 *reason = kAutoConnExplicitDisconnect;
801 return false;
802 }
803
804 return true;
mukesh agrawal76d13882012-01-12 15:23:11 -0800805}
806
Paul Stewartd215af62012-04-24 23:25:50 -0700807bool Service::IsPortalDetectionDisabled() const {
808 return check_portal_ == kCheckPortalFalse;
809}
810
811bool Service::IsPortalDetectionAuto() const {
812 return check_portal_ == kCheckPortalAuto;
813}
814
mukesh agrawalffa3d042011-10-06 15:26:10 -0700815void Service::HelpRegisterDerivedBool(
816 const string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800817 bool(Service::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700818 void(Service::*set)(const bool&, Error *)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700819 store_.RegisterDerivedBool(
820 name,
821 BoolAccessor(new CustomAccessor<Service, bool>(this, get, set)));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700822}
823
mukesh agrawalffa3d042011-10-06 15:26:10 -0700824void Service::HelpRegisterDerivedString(
825 const string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800826 string(Service::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700827 void(Service::*set)(const string&, Error *)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700828 store_.RegisterDerivedString(
829 name,
830 StringAccessor(new CustomAccessor<Service, string>(this, get, set)));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700831}
832
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400833void Service::HelpRegisterDerivedRpcIdentifier(
834 const string &name,
835 RpcIdentifier(Service::*get)(Error *),
836 void(Service::*set)(const RpcIdentifier&, Error *)) {
837 store_.RegisterDerivedRpcIdentifier(
838 name,
839 RpcIdentifierAccessor(new CustomAccessor<Service, RpcIdentifier>(
840 this, get, set)));
841}
842
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800843void Service::HelpRegisterDerivedUint16(
844 const string &name,
845 uint16(Service::*get)(Error *),
846 void(Service::*set)(const uint16&, Error *)) {
847 store_.RegisterDerivedUint16(
848 name,
849 Uint16Accessor(new CustomAccessor<Service, uint16>(this, get, set)));
850}
851
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800852void Service::HelpRegisterWriteOnlyDerivedString(
853 const string &name,
854 void(Service::*set)(const string &, Error *),
855 void(Service::*clear)(Error *),
856 const string *default_value) {
857 store_.RegisterDerivedString(
858 name,
859 StringAccessor(
860 new CustomWriteOnlyAccessor<Service, string>(
861 this, set, clear, default_value)));
862}
863
Darin Petkovba40dd32011-07-11 20:06:39 -0700864void Service::SaveString(StoreInterface *storage,
Chris Masone34af2182011-08-22 11:59:36 -0700865 const string &id,
Darin Petkovba40dd32011-07-11 20:06:39 -0700866 const string &key,
867 const string &value,
868 bool crypted,
869 bool save) {
870 if (value.empty() || !save) {
Chris Masone34af2182011-08-22 11:59:36 -0700871 storage->DeleteKey(id, key);
Darin Petkovba40dd32011-07-11 20:06:39 -0700872 return;
873 }
874 if (crypted) {
Chris Masone34af2182011-08-22 11:59:36 -0700875 storage->SetCryptedString(id, key, value);
Darin Petkovba40dd32011-07-11 20:06:39 -0700876 return;
877 }
Chris Masone34af2182011-08-22 11:59:36 -0700878 storage->SetString(id, key, value);
Darin Petkovba40dd32011-07-11 20:06:39 -0700879}
880
Chris Masone34af2182011-08-22 11:59:36 -0700881void Service::LoadEapCredentials(StoreInterface *storage, const string &id) {
Gaurav Shah10109f22011-11-11 20:16:22 -0800882 EapCredentials eap;
883 storage->GetCryptedString(id, kStorageEapIdentity, &eap.identity);
884 storage->GetString(id, kStorageEapEap, &eap.eap);
885 storage->GetString(id, kStorageEapInnerEap, &eap.inner_eap);
Darin Petkovba40dd32011-07-11 20:06:39 -0700886 storage->GetCryptedString(id,
887 kStorageEapAnonymousIdentity,
Gaurav Shah10109f22011-11-11 20:16:22 -0800888 &eap.anonymous_identity);
889 storage->GetString(id, kStorageEapClientCert, &eap.client_cert);
890 storage->GetString(id, kStorageEapCertID, &eap.cert_id);
891 storage->GetString(id, kStorageEapPrivateKey, &eap.private_key);
Darin Petkovba40dd32011-07-11 20:06:39 -0700892 storage->GetCryptedString(id,
893 kStorageEapPrivateKeyPassword,
Gaurav Shah10109f22011-11-11 20:16:22 -0800894 &eap.private_key_password);
895 storage->GetString(id, kStorageEapKeyID, &eap.key_id);
896 storage->GetString(id, kStorageEapCACert, &eap.ca_cert);
897 storage->GetString(id, kStorageEapCACertID, &eap.ca_cert_id);
Paul Stewartecf4cd12012-04-17 11:08:39 -0700898 storage->GetString(id, kStorageEapCACertNSS, &eap.ca_cert_nss);
Gaurav Shah10109f22011-11-11 20:16:22 -0800899 storage->GetBool(id, kStorageEapUseSystemCAs, &eap.use_system_cas);
900 storage->GetString(id, kStorageEapPIN, &eap.pin);
901 storage->GetCryptedString(id, kStorageEapPassword, &eap.password);
902 storage->GetString(id, kStorageEapKeyManagement, &eap.key_management);
903 set_eap(eap);
Darin Petkovba40dd32011-07-11 20:06:39 -0700904}
905
Chris Masone34af2182011-08-22 11:59:36 -0700906void Service::SaveEapCredentials(StoreInterface *storage, const string &id) {
Darin Petkovba40dd32011-07-11 20:06:39 -0700907 bool save = save_credentials_;
Chris Masone34af2182011-08-22 11:59:36 -0700908 SaveString(storage, id, kStorageEapIdentity, eap_.identity, true, save);
909 SaveString(storage, id, kStorageEapEap, eap_.eap, false, true);
910 SaveString(storage, id, kStorageEapInnerEap, eap_.inner_eap, false, true);
Darin Petkovba40dd32011-07-11 20:06:39 -0700911 SaveString(storage,
Chris Masone34af2182011-08-22 11:59:36 -0700912 id,
Darin Petkovba40dd32011-07-11 20:06:39 -0700913 kStorageEapAnonymousIdentity,
914 eap_.anonymous_identity,
915 true,
916 save);
Chris Masone34af2182011-08-22 11:59:36 -0700917 SaveString(storage, id, kStorageEapClientCert, eap_.client_cert, false, save);
918 SaveString(storage, id, kStorageEapCertID, eap_.cert_id, false, save);
919 SaveString(storage, id, kStorageEapPrivateKey, eap_.private_key, false, save);
Darin Petkovba40dd32011-07-11 20:06:39 -0700920 SaveString(storage,
Chris Masone34af2182011-08-22 11:59:36 -0700921 id,
Darin Petkovba40dd32011-07-11 20:06:39 -0700922 kStorageEapPrivateKeyPassword,
923 eap_.private_key_password,
924 true,
925 save);
Chris Masone34af2182011-08-22 11:59:36 -0700926 SaveString(storage, id, kStorageEapKeyID, eap_.key_id, false, save);
927 SaveString(storage, id, kStorageEapCACert, eap_.ca_cert, false, true);
928 SaveString(storage, id, kStorageEapCACertID, eap_.ca_cert_id, false, true);
Paul Stewartecf4cd12012-04-17 11:08:39 -0700929 SaveString(storage, id, kStorageEapCACertNSS, eap_.ca_cert_nss, false, true);
Chris Masone34af2182011-08-22 11:59:36 -0700930 storage->SetBool(id, kStorageEapUseSystemCAs, eap_.use_system_cas);
931 SaveString(storage, id, kStorageEapPIN, eap_.pin, false, save);
932 SaveString(storage, id, kStorageEapPassword, eap_.password, true, save);
Darin Petkovba40dd32011-07-11 20:06:39 -0700933 SaveString(storage,
Chris Masone34af2182011-08-22 11:59:36 -0700934 id,
Darin Petkovba40dd32011-07-11 20:06:39 -0700935 kStorageEapKeyManagement,
936 eap_.key_management,
937 false,
938 true);
939}
940
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800941void Service::UnloadEapCredentials() {
942 eap_.identity = "";
943 eap_.eap = "";
944 eap_.inner_eap = "";
945 eap_.anonymous_identity = "";
946 eap_.client_cert = "";
947 eap_.cert_id = "";
948 eap_.private_key = "";
949 eap_.private_key_password = "";
950 eap_.key_id = "";
951 eap_.ca_cert = "";
952 eap_.ca_cert_id = "";
Paul Stewart20550982012-04-16 12:16:11 -0700953 eap_.use_system_cas = true;
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800954 eap_.pin = "";
955 eap_.password = "";
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800956}
957
Paul Stewartcb59fed2012-03-21 21:14:46 -0700958void Service::IgnoreParameterForConfigure(const string &parameter) {
959 parameters_ignored_for_configure_.insert(parameter);
960}
961
Paul Stewartac4ac002011-08-26 12:04:26 -0700962const string &Service::GetEAPKeyManagement() const {
963 return eap_.key_management;
964}
965
966void Service::SetEAPKeyManagement(const string &key_management) {
967 eap_.key_management = key_management;
968}
969
Thieu Le284fe792012-01-31 17:53:19 -0800970bool Service::GetAutoConnect(Error */*error*/) {
971 return auto_connect();
972}
973
974void Service::SetAutoConnect(const bool &connect, Error *error) {
Wade Guthrie4f28e8b2012-04-11 10:52:07 -0700975 set_auto_connect(connect);
Thieu Le284fe792012-01-31 17:53:19 -0800976}
977
Paul Stewartd215af62012-04-24 23:25:50 -0700978string Service::GetCheckPortal(Error *error) {
979 return check_portal_;
980}
981
982void Service::SetCheckPortal(const string &check_portal, Error *error) {
983 if (check_portal == check_portal_) {
984 return;
985 }
986 if (check_portal != kCheckPortalFalse &&
987 check_portal != kCheckPortalTrue &&
988 check_portal != kCheckPortalAuto) {
989 Error::PopulateAndLog(error, Error::kInvalidArguments,
990 base::StringPrintf(
991 "Invalid Service CheckPortal property value: %s",
992 check_portal.c_str()));
993 return;
994 }
995 check_portal_ = check_portal;
996}
997
Paul Stewart9f32d192012-01-30 20:37:50 -0800998void Service::SetEAPPassword(const string &password, Error */*error*/) {
999 eap_.password = password;
1000}
1001
1002void Service::SetEAPPrivateKeyPassword(const string &password,
1003 Error */*error*/) {
1004 eap_.private_key_password = password;
1005}
1006
Paul Stewart0c438332012-04-11 07:55:27 -07001007string Service::GetNameProperty(Error *error) {
1008 return friendly_name_;
1009}
1010
1011void Service::AssertTrivialSetNameProperty(const string &name, Error *error) {
1012 if (name != friendly_name_) {
1013 Error::PopulateAndLog(error, Error::kInvalidArguments,
Paul Stewart2bf424f2012-04-11 18:59:39 -07001014 base::StringPrintf(
1015 "Service Name property cannot be modified "
1016 "(%s to %s)", friendly_name_.c_str(),
1017 name.c_str()));
Paul Stewart0c438332012-04-11 07:55:27 -07001018 }
1019}
1020
Paul Stewart1b1a7f22012-01-06 16:24:06 -08001021string Service::GetProfileRpcId(Error *error) {
1022 if (!profile_) {
1023 // This happens in some unit tests where profile_ is not set.
1024 error->Populate(Error::kNotFound);
1025 return "";
1026 }
1027 return profile_->GetRpcIdentifier();
1028}
1029
1030void Service::SetProfileRpcId(const string &profile, Error *error) {
1031 manager_->SetProfileForService(this, profile, error);
1032}
1033
Paul Stewartbe5f5b32011-12-07 17:11:11 -08001034uint16 Service::GetHTTPProxyPort(Error */*error*/) {
1035 if (http_proxy_.get()) {
1036 return static_cast<uint16>(http_proxy_->proxy_port());
1037 }
1038 return 0;
1039}
1040
mukesh agrawalcf24a242012-05-21 16:46:11 -07001041void Service::SaveToProfile() {
1042 if (profile_.get() && profile_->GetConstStorage()) {
1043 profile_->UpdateService(this);
1044 }
1045}
1046
Darin Petkovd78ee7e2012-01-12 11:21:10 +01001047void Service::SetStrength(uint8 strength) {
1048 if (strength == strength_) {
1049 return;
1050 }
1051 strength_ = strength;
1052 adaptor_->EmitUint8Changed(flimflam::kSignalStrengthProperty, strength);
1053}
1054
Paul Stewart75897df2011-04-27 09:05:53 -07001055} // namespace shill