blob: 99285c6591091fe61465d42ae34564dcb6382f1e [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
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800265bool Service::IsActive(Error */*error*/) {
Paul Stewart03dba0b2011-08-22 16:32:45 -0700266 return state_ != kStateUnknown &&
267 state_ != kStateIdle &&
268 state_ != kStateFailure;
269}
270
271void Service::SetState(ConnectState state) {
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800272 LOG(INFO) << "In " << __func__ << "(): Service " << friendly_name_
273 << " state " << ConnectStateToString(state_) << " -> "
274 << ConnectStateToString(state);
mukesh agrawal7ec71312011-11-10 02:08:26 +0000275
Paul Stewart03dba0b2011-08-22 16:32:45 -0700276 if (state == state_) {
277 return;
278 }
279
280 state_ = state;
281 if (state != kStateFailure) {
282 failure_ = kFailureUnknown;
mukesh agrawal568b5c62012-02-28 14:44:47 -0800283 failed_time_ = 0;
Paul Stewart03dba0b2011-08-22 16:32:45 -0700284 }
mukesh agrawalcf24a242012-05-21 16:46:11 -0700285 if (state == kStateConnected) {
286 has_ever_connected_ = true;
287 SaveToProfile();
288 }
Paul Stewart03dba0b2011-08-22 16:32:45 -0700289 manager_->UpdateService(this);
Thieu Le48e6d6d2011-12-06 00:40:27 +0000290 metrics_->NotifyServiceStateChanged(this, state);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800291 Error error;
292 adaptor_->EmitStringChanged(flimflam::kStateProperty, CalculateState(&error));
Paul Stewart03dba0b2011-08-22 16:32:45 -0700293}
294
295void Service::SetFailure(ConnectFailure failure) {
296 failure_ = failure;
mukesh agrawal568b5c62012-02-28 14:44:47 -0800297 failed_time_ = time(NULL);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700298 SetState(kStateFailure);
299}
300
mukesh agrawal568b5c62012-02-28 14:44:47 -0800301void Service::SetFailureSilent(ConnectFailure failure) {
302 // Note that order matters here, since SetState modifies |failure_| and
303 // |failed_time_|.
304 SetState(kStateIdle);
305 failure_ = failure;
306 failed_time_ = time(NULL);
307}
308
Chris Masone6791a432011-07-12 13:23:19 -0700309string Service::GetRpcIdentifier() const {
Chris Masone3c3f6a12011-07-01 10:01:41 -0700310 return adaptor_->GetRpcIdentifier();
311}
312
Paul Stewartbba6a5b2011-11-02 18:45:59 -0700313bool Service::IsLoadableFrom(StoreInterface *storage) const {
314 return storage->ContainsGroup(GetStorageIdentifier());
315}
316
Chris Masone9d779932011-08-25 16:33:41 -0700317bool Service::Load(StoreInterface *storage) {
318 const string id = GetStorageIdentifier();
Chris Masone34af2182011-08-22 11:59:36 -0700319 if (!storage->ContainsGroup(id)) {
320 LOG(WARNING) << "Service is not available in the persistent store: " << id;
321 return false;
322 }
323 storage->GetBool(id, kStorageAutoConnect, &auto_connect_);
324 storage->GetString(id, kStorageCheckPortal, &check_portal_);
325 storage->GetBool(id, kStorageFavorite, &favorite_);
mukesh agrawalcf24a242012-05-21 16:46:11 -0700326 storage->GetString(id, kStorageGUID, &guid_);
327 storage->GetBool(id, kStorageHasEverConnected, &has_ever_connected_);
Chris Masone34af2182011-08-22 11:59:36 -0700328 storage->GetInt(id, kStoragePriority, &priority_);
329 storage->GetString(id, kStorageProxyConfig, &proxy_config_);
330 storage->GetBool(id, kStorageSaveCredentials, &save_credentials_);
Paul Stewart987e71e2011-12-05 09:45:06 -0800331 storage->GetString(id, kStorageUIData, &ui_data_);
Chris Masone34af2182011-08-22 11:59:36 -0700332
333 LoadEapCredentials(storage, id);
Paul Stewart1062d9d2012-04-27 10:42:27 -0700334 static_ip_parameters_.Load(storage, id);
Chris Masone34af2182011-08-22 11:59:36 -0700335 // TODO(petkov): Load these:
336
Chris Masone34af2182011-08-22 11:59:36 -0700337 // "Failure"
338 // "Modified"
339 // "LastAttempt"
Chris Masone34af2182011-08-22 11:59:36 -0700340
mukesh agrawaladb68482012-01-17 16:31:51 -0800341 explicitly_disconnected_ = false;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800342 favorite_ = true;
343
Chris Masone34af2182011-08-22 11:59:36 -0700344 return true;
Darin Petkovba40dd32011-07-11 20:06:39 -0700345}
346
Paul Stewart65512e12012-03-26 18:01:08 -0700347bool Service::Unload() {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800348 auto_connect_ = false;
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800349 check_portal_ = kCheckPortalAuto;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800350 favorite_ = false;
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800351 priority_ = kPriorityNone;
352 proxy_config_ = "";
353 save_credentials_ = true;
354 ui_data_ = "";
355
356 UnloadEapCredentials();
Wade Guthrie9e0c2502012-04-19 15:26:40 -0700357 Error error; // Ignored.
358 Disconnect(&error);
Paul Stewart65512e12012-03-26 18:01:08 -0700359 return false;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800360}
361
Chris Masone9d779932011-08-25 16:33:41 -0700362bool Service::Save(StoreInterface *storage) {
363 const string id = GetStorageIdentifier();
Chris Masone34af2182011-08-22 11:59:36 -0700364
Paul Stewart2706aaf2011-12-14 16:44:04 -0800365 storage->SetString(id, kStorageType, GetTechnologyString(NULL));
366
Chris Masone34af2182011-08-22 11:59:36 -0700367 // TODO(petkov): We could choose to simplify the saving code by removing most
368 // conditionals thus saving even default values.
Wade Guthrie4f28e8b2012-04-11 10:52:07 -0700369 storage->SetBool(id, kStorageAutoConnect, auto_connect_);
Chris Masone34af2182011-08-22 11:59:36 -0700370 if (check_portal_ == kCheckPortalAuto) {
371 storage->DeleteKey(id, kStorageCheckPortal);
372 } else {
373 storage->SetString(id, kStorageCheckPortal, check_portal_);
374 }
375 storage->SetBool(id, kStorageFavorite, favorite_);
mukesh agrawalcf24a242012-05-21 16:46:11 -0700376 SaveString(storage, id, kStorageGUID, guid_, false, true);
377 storage->SetBool(id, kStorageHasEverConnected, has_ever_connected_);
mukesh agrawald835b202011-10-07 15:26:47 -0700378 storage->SetString(id, kStorageName, friendly_name_);
Chris Masone34af2182011-08-22 11:59:36 -0700379 if (priority_ != kPriorityNone) {
380 storage->SetInt(id, kStoragePriority, priority_);
381 } else {
382 storage->DeleteKey(id, kStoragePriority);
383 }
Paul Stewart987e71e2011-12-05 09:45:06 -0800384 SaveString(storage, id, kStorageProxyConfig, proxy_config_, false, true);
Chris Masone34af2182011-08-22 11:59:36 -0700385 if (save_credentials_) {
386 storage->DeleteKey(id, kStorageSaveCredentials);
387 } else {
388 storage->SetBool(id, kStorageSaveCredentials, false);
389 }
Paul Stewart987e71e2011-12-05 09:45:06 -0800390 SaveString(storage, id, kStorageUIData, ui_data_, false, true);
Chris Masone34af2182011-08-22 11:59:36 -0700391
392 SaveEapCredentials(storage, id);
Paul Stewart1062d9d2012-04-27 10:42:27 -0700393 static_ip_parameters_.Save(storage, id);
Chris Masone34af2182011-08-22 11:59:36 -0700394
395 // TODO(petkov): Save these:
396
Chris Masone34af2182011-08-22 11:59:36 -0700397 // "Failure"
398 // "Modified"
399 // "LastAttempt"
Chris Masone34af2182011-08-22 11:59:36 -0700400
401 return true;
402}
403
Gary Moraind93615e2012-04-27 11:50:03 -0700404void Service::SaveToCurrentProfile() {
405 // Some unittests do not specify a manager.
406 if (manager()) {
407 manager()->SaveServiceToProfile(this);
408 }
Thieu Led4e9e552012-02-16 16:26:07 -0800409}
410
Paul Stewartcb59fed2012-03-21 21:14:46 -0700411void Service::Configure(const KeyValueStore &args, Error *error) {
412 map<string, bool>::const_iterator bool_it;
Ben Chanfad4a0b2012-04-18 15:49:59 -0700413 SLOG(Service, 5) << "Configuring bool properties:";
Paul Stewartcb59fed2012-03-21 21:14:46 -0700414 for (bool_it = args.bool_properties().begin();
415 bool_it != args.bool_properties().end();
416 ++bool_it) {
417 if (ContainsKey(parameters_ignored_for_configure_, bool_it->first)) {
418 continue;
419 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700420 SLOG(Service, 5) << " " << bool_it->first;
Paul Stewartcb59fed2012-03-21 21:14:46 -0700421 Error set_error;
422 store_.SetBoolProperty(bool_it->first, bool_it->second, &set_error);
423 if (error->IsSuccess() && set_error.IsFailure()) {
424 error->CopyFrom(set_error);
425 }
426 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700427 SLOG(Service, 5) << "Configuring string properties:";
Paul Stewartcb59fed2012-03-21 21:14:46 -0700428 map<string, string>::const_iterator string_it;
429 for (string_it = args.string_properties().begin();
430 string_it != args.string_properties().end();
431 ++string_it) {
432 if (ContainsKey(parameters_ignored_for_configure_, string_it->first)) {
433 continue;
434 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700435 SLOG(Service, 5) << " " << string_it->first;
Paul Stewartcb59fed2012-03-21 21:14:46 -0700436 Error set_error;
437 store_.SetStringProperty(string_it->first, string_it->second, &set_error);
438 if (error->IsSuccess() && set_error.IsFailure()) {
439 error->CopyFrom(set_error);
440 }
441 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700442 SLOG(Service, 5) << "Configuring uint32 properties:";
Paul Stewartcb59fed2012-03-21 21:14:46 -0700443 map<string, uint32>::const_iterator int_it;
444 for (int_it = args.uint_properties().begin();
445 int_it != args.uint_properties().end();
446 ++int_it) {
447 if (ContainsKey(parameters_ignored_for_configure_, int_it->first)) {
448 continue;
449 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700450 SLOG(Service, 5) << " " << int_it->first;
Paul Stewartcb59fed2012-03-21 21:14:46 -0700451 Error set_error;
452 store_.SetUint32Property(int_it->first, int_it->second, &set_error);
453 if (error->IsSuccess() && set_error.IsFailure()) {
454 error->CopyFrom(set_error);
455 }
456 }
457}
458
Paul Stewart10ccbb32012-04-26 15:59:30 -0700459bool Service::IsRemembered() const {
460 return profile_ && !manager_->IsServiceEphemeral(this);
461}
462
mukesh agrawal00917ce2011-11-22 23:56:55 +0000463void Service::MakeFavorite() {
464 if (favorite_) {
465 // We do not want to clobber the value of auto_connect_ (it may
466 // be user-set). So return early.
467 return;
468 }
469
470 auto_connect_ = true;
471 favorite_ = true;
472}
473
Darin Petkov5eb05422012-05-11 15:45:25 +0200474void Service::SetConnection(const ConnectionRefPtr &connection) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800475 if (connection.get()) {
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800476 http_proxy_.reset(new HTTPProxy(connection));
Darin Petkov5eb05422012-05-11 15:45:25 +0200477 http_proxy_->Start(dispatcher_, sockets_.get());
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800478 } else {
479 http_proxy_.reset();
480 }
481 connection_ = connection;
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800482}
483
Gaurav Shah10109f22011-11-11 20:16:22 -0800484bool Service::Is8021xConnectable() const {
485 // We mirror all the flimflam checks (see service.c:is_connectable()).
486
487 // Identity is required.
488 if (eap_.identity.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700489 SLOG(Service, 2) << "Not connectable: Identity is empty.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800490 return false;
491 }
492
493 if (!eap_.client_cert.empty() || !eap_.cert_id.empty()) {
494 // If a client certificate is being used, we must have a private key.
495 if (eap_.private_key.empty() && eap_.key_id.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700496 SLOG(Service, 2)
497 << "Not connectable. Client certificate but no private key.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800498 return false;
499 }
500 }
501 if (!eap_.cert_id.empty() || !eap_.key_id.empty() ||
502 !eap_.ca_cert_id.empty()) {
503 // If PKCS#11 data is needed, a PIN is required.
504 if (eap_.pin.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700505 SLOG(Service, 2) << "Not connectable. PKCS#11 data but no PIN.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800506 return false;
507 }
508 }
509
510 // For EAP-TLS, a client certificate is required.
511 if (eap_.eap.empty() || eap_.eap == "TLS") {
Paul Stewart81426132012-05-16 10:05:10 -0700512 if ((!eap_.client_cert.empty() || !eap_.cert_id.empty()) &&
513 (!eap_.private_key.empty() || !eap_.key_id.empty())) {
514 SLOG(Service, 2) << "Connectable. EAP-TLS with a client cert and key.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800515 return true;
516 }
517 }
518
519 // For EAP types other than TLS (e.g. EAP-TTLS or EAP-PEAP, password is the
520 // minimum requirement), at least an identity + password is required.
521 if (eap_.eap.empty() || eap_.eap != "TLS") {
522 if (!eap_.password.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700523 SLOG(Service, 2) << "Connectable. !EAP-TLS and has a password.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800524 return true;
525 }
526 }
527
Ben Chanfad4a0b2012-04-18 15:49:59 -0700528 SLOG(Service, 2)
529 << "Not connectable. No suitable EAP configuration was found.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800530 return false;
531}
532
Paul Stewartbc6e7392012-05-24 07:07:48 -0700533bool Service::AddEAPCertification(const string &name, size_t depth) {
534 if (depth >= kEAPMaxCertificationElements) {
535 LOG(WARNING) << "Ignoring certification " << name
536 << " because depth " << depth
537 << " exceeds our maximum of "
538 << kEAPMaxCertificationElements;
539 return false;
540 }
541
542 if (depth >= eap_.remote_certification.size()) {
543 eap_.remote_certification.resize(depth + 1);
544 } else if (name == eap_.remote_certification[depth]) {
545 return true;
546 }
547
548 eap_.remote_certification[depth] = name;
549 LOG(INFO) << "Received certification for "
550 << name
551 << " at depth "
552 << depth;
553 return true;
554}
555
556void Service::ClearEAPCertification() {
557 eap_.remote_certification.clear();
558}
559
Gaurav Shah10109f22011-11-11 20:16:22 -0800560void Service::set_eap(const EapCredentials &eap) {
561 eap_ = eap;
562 // Note: Connectability can only be updated by a subclass of Service
563 // with knowledge of whether the service actually uses 802.1x credentials.
564}
565
mukesh agrawal00917ce2011-11-22 23:56:55 +0000566// static
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800567const char *Service::ConnectFailureToString(const ConnectFailure &state) {
568 switch (state) {
569 case kFailureUnknown:
570 return "Unknown";
571 case kFailureActivationFailure:
572 return "Activation Failure";
573 case kFailureOutOfRange:
574 return "Out of range";
575 case kFailurePinMissing:
576 return "PIN missing";
577 case kFailureConfigurationFailed:
578 return "Configuration Failed";
579 case kFailureBadCredentials:
580 return "Bad Credentials";
581 case kFailureNeedEVDO:
582 return "Need EVDO";
583 case kFailureNeedHomeNetwork:
584 return "Need Home Network";
585 case kFailureOTASPFailure:
586 return "OTASP Failure";
587 case kFailureAAAFailure:
588 return "AAA Failure";
Thieu Le48e6d6d2011-12-06 00:40:27 +0000589 case kFailureMax:
590 return "Max failure error code";
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800591 }
592 return "Invalid";
593}
594
595// static
596const char *Service::ConnectStateToString(const ConnectState &state) {
597 switch (state) {
598 case kStateUnknown:
599 return "Unknown";
600 case kStateIdle:
601 return "Idle";
602 case kStateAssociating:
603 return "Associating";
604 case kStateConfiguring:
605 return "Configuring";
606 case kStateConnected:
607 return "Connected";
608 case kStateDisconnected:
609 return "Disconnected";
Thieu Le48e6d6d2011-12-06 00:40:27 +0000610 case kStatePortal:
611 return "Portal";
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800612 case kStateFailure:
613 return "Failure";
614 case kStateOnline:
615 return "Online";
616 }
617 return "Invalid";
618}
619
Gaurav Shah435de2c2011-11-17 19:01:07 -0800620string Service::GetTechnologyString(Error */*error*/) {
621 return Technology::NameFromIdentifier(technology());
622}
623
Jason Glasgowb5790052012-01-27 01:03:52 -0500624// static
Paul Stewart22aa71b2011-09-16 12:15:11 -0700625bool Service::DecideBetween(int a, int b, bool *decision) {
626 if (a == b)
627 return false;
628 *decision = (a > b);
629 return true;
630}
631
mukesh agrawal00917ce2011-11-22 23:56:55 +0000632// static
Paul Stewart22aa71b2011-09-16 12:15:11 -0700633bool Service::Compare(ServiceRefPtr a,
634 ServiceRefPtr b,
mukesh agrawalddc378f2012-02-17 18:26:20 -0800635 const vector<Technology::Identifier> &tech_order,
636 const char **reason) {
Paul Stewart22aa71b2011-09-16 12:15:11 -0700637 bool ret;
638
639 if (a->state() != b->state()) {
640 if (DecideBetween(a->IsConnected(), b->IsConnected(), &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800641 *reason = kServiceSortIsConnected;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700642 return ret;
643 }
644
645 // TODO(pstew): Services don't know about portal state yet
646
647 if (DecideBetween(a->IsConnecting(), b->IsConnecting(), &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800648 *reason = kServiceSortIsConnecting;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700649 return ret;
650 }
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000651
652 if (DecideBetween(!a->IsFailed(), !b->IsFailed(), &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800653 *reason = kServiceSortIsFailed;
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000654 return ret;
655 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700656 }
657
Darin Petkov2f903b32012-04-18 12:56:43 +0200658 if (DecideBetween(a->connectable(), b->connectable(), &ret)) {
659 *reason = kServiceSortConnectable;
660 return ret;
661 }
662
663 // Ignore the auto-connect property if both services are connected
664 // already. This allows connected non-autoconnectable VPN services to be
665 // sorted higher than other connected services based on technology order.
666 if (!a->IsConnected() &&
667 DecideBetween(a->auto_connect(), b->auto_connect(), &ret)) {
668 *reason = kServiceSortAutoConnect;
669 return ret;
670 }
671
672 if (DecideBetween(a->favorite(), b->favorite(), &ret)) {
673 *reason = kServiceSortFavorite;
674 return ret;
675 }
676
677 if (DecideBetween(a->priority(), b->priority(), &ret)) {
678 *reason = kServiceSortPriority;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700679 return ret;
680 }
681
682 // TODO(pstew): Below this point we are making value judgements on
683 // services that are not related to anything intrinsic or
684 // user-specified. These heuristics should be richer (contain
685 // historical information, for example) and be subject to user
686 // customization.
Paul Stewart22aa71b2011-09-16 12:15:11 -0700687 for (vector<Technology::Identifier>::const_iterator it = tech_order.begin();
688 it != tech_order.end();
689 ++it) {
Joshua Kroll053fa822012-06-05 09:50:43 -0700690 if (DecideBetween(a->technology() == *it, b->technology() == *it, &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800691 *reason = kServiceSortTechnology;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700692 return ret;
mukesh agrawalddc378f2012-02-17 18:26:20 -0800693 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700694 }
695
Paul Stewart1ca3e852011-11-04 07:50:49 -0700696 if (DecideBetween(a->security_level(), b->security_level(), &ret) ||
Paul Stewart22aa71b2011-09-16 12:15:11 -0700697 DecideBetween(a->strength(), b->strength(), &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800698 *reason = kServiceSortSecurityEtc;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700699 return ret;
700 }
701
mukesh agrawalddc378f2012-02-17 18:26:20 -0800702 *reason = kServiceSortUniqueName;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700703 return a->UniqueName() < b->UniqueName();
704}
705
Chris Masone34af2182011-08-22 11:59:36 -0700706const ProfileRefPtr &Service::profile() const { return profile_; }
707
708void Service::set_profile(const ProfileRefPtr &p) { profile_ = p; }
709
Paul Stewartff14b022012-04-24 20:06:23 -0700710void Service::OnPropertyChanged(const string &property) {
Paul Stewart81426132012-05-16 10:05:10 -0700711 if (Is8021x() &&
712 (property == flimflam::kEAPCertIDProperty ||
713 property == flimflam::kEAPClientCertProperty ||
714 property == flimflam::kEAPKeyIDProperty ||
715 property == flimflam::kEAPPINProperty ||
716 property == flimflam::kEapCaCertIDProperty ||
717 property == flimflam::kEapIdentityProperty ||
718 property == flimflam::kEapPasswordProperty ||
719 property == flimflam::kEapPrivateKeyProperty)) {
720 // This notifies subclassess that EAP parameters have been changed.
721 set_eap(eap_);
722 }
mukesh agrawalcf24a242012-05-21 16:46:11 -0700723 SaveToProfile();
Paul Stewartd215af62012-04-24 23:25:50 -0700724 if ((property == flimflam::kCheckPortalProperty ||
725 property == flimflam::kProxyConfigProperty) &&
726 (state_ == kStateConnected ||
727 state_ == kStatePortal ||
728 state_ == kStateOnline)) {
729 manager_->RecheckPortalOnService(this);
730 }
Paul Stewartff14b022012-04-24 20:06:23 -0700731}
732
Paul Stewart10241e32012-04-23 18:15:06 -0700733string Service::GetIPConfigRpcIdentifier(Error *error) {
734 if (!connection_) {
735 error->Populate(Error::kNotFound);
736 return "/";
737 }
738
739 string id = connection_->ipconfig_rpc_identifier();
740
741 if (id.empty()) {
742 // Do not return an empty IPConfig.
743 error->Populate(Error::kNotFound);
744 return "/";
745 }
746
747 return id;
748}
749
mukesh agrawal29c13a12011-11-24 00:09:19 +0000750void Service::set_connectable(bool connectable) {
751 connectable_ = connectable;
752 adaptor_->EmitBoolChanged(flimflam::kConnectableProperty, connectable_);
753}
754
Darin Petkovb2ba39f2012-06-06 10:33:43 +0200755void Service::SetConnectable(bool connectable) {
756 if (connectable_ == connectable) {
757 return;
758 }
759 connectable_ = connectable;
760 adaptor_->EmitBoolChanged(flimflam::kConnectableProperty, connectable_);
761 if (manager_->HasService(this)) {
762 manager_->UpdateService(this);
763 }
764}
765
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800766string Service::CalculateState(Error */*error*/) {
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700767 switch (state_) {
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800768 case kStateIdle:
769 return flimflam::kStateIdle;
770 case kStateAssociating:
771 return flimflam::kStateAssociation;
772 case kStateConfiguring:
773 return flimflam::kStateConfiguration;
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700774 case kStateConnected:
Paul Stewart20088d82012-02-16 06:58:55 -0800775 return flimflam::kStateReady;
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800776 case kStateDisconnected:
777 return flimflam::kStateDisconnect;
778 case kStateFailure:
779 return flimflam::kStateFailure;
Paul Stewart20088d82012-02-16 06:58:55 -0800780 case kStatePortal:
781 return flimflam::kStatePortal;
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800782 case kStateOnline:
783 return flimflam::kStateOnline;
784 case kStateUnknown:
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700785 default:
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800786 return "";
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700787 }
788}
789
mukesh agrawalbf14e942012-03-02 14:36:34 -0800790bool Service::IsAutoConnectable(const char **reason) const {
791 if (!connectable()) {
792 *reason = kAutoConnNotConnectable;
793 return false;
794 }
795
796 if (IsConnected()) {
797 *reason = kAutoConnConnected;
798 return false;
799 }
800
801 if (IsConnecting()) {
802 *reason = kAutoConnConnecting;
803 return false;
804 }
805
806 if (explicitly_disconnected_) {
807 *reason = kAutoConnExplicitDisconnect;
808 return false;
809 }
810
811 return true;
mukesh agrawal76d13882012-01-12 15:23:11 -0800812}
813
Paul Stewartd215af62012-04-24 23:25:50 -0700814bool Service::IsPortalDetectionDisabled() const {
815 return check_portal_ == kCheckPortalFalse;
816}
817
818bool Service::IsPortalDetectionAuto() const {
819 return check_portal_ == kCheckPortalAuto;
820}
821
mukesh agrawalffa3d042011-10-06 15:26:10 -0700822void Service::HelpRegisterDerivedBool(
823 const string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800824 bool(Service::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700825 void(Service::*set)(const bool&, Error *)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700826 store_.RegisterDerivedBool(
827 name,
828 BoolAccessor(new CustomAccessor<Service, bool>(this, get, set)));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700829}
830
mukesh agrawalffa3d042011-10-06 15:26:10 -0700831void Service::HelpRegisterDerivedString(
832 const string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800833 string(Service::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700834 void(Service::*set)(const string&, Error *)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700835 store_.RegisterDerivedString(
836 name,
837 StringAccessor(new CustomAccessor<Service, string>(this, get, set)));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700838}
839
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400840void Service::HelpRegisterDerivedRpcIdentifier(
841 const string &name,
842 RpcIdentifier(Service::*get)(Error *),
843 void(Service::*set)(const RpcIdentifier&, Error *)) {
844 store_.RegisterDerivedRpcIdentifier(
845 name,
846 RpcIdentifierAccessor(new CustomAccessor<Service, RpcIdentifier>(
847 this, get, set)));
848}
849
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800850void Service::HelpRegisterDerivedUint16(
851 const string &name,
852 uint16(Service::*get)(Error *),
853 void(Service::*set)(const uint16&, Error *)) {
854 store_.RegisterDerivedUint16(
855 name,
856 Uint16Accessor(new CustomAccessor<Service, uint16>(this, get, set)));
857}
858
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800859void Service::HelpRegisterWriteOnlyDerivedString(
860 const string &name,
861 void(Service::*set)(const string &, Error *),
862 void(Service::*clear)(Error *),
863 const string *default_value) {
864 store_.RegisterDerivedString(
865 name,
866 StringAccessor(
867 new CustomWriteOnlyAccessor<Service, string>(
868 this, set, clear, default_value)));
869}
870
Darin Petkovba40dd32011-07-11 20:06:39 -0700871void Service::SaveString(StoreInterface *storage,
Chris Masone34af2182011-08-22 11:59:36 -0700872 const string &id,
Darin Petkovba40dd32011-07-11 20:06:39 -0700873 const string &key,
874 const string &value,
875 bool crypted,
876 bool save) {
877 if (value.empty() || !save) {
Chris Masone34af2182011-08-22 11:59:36 -0700878 storage->DeleteKey(id, key);
Darin Petkovba40dd32011-07-11 20:06:39 -0700879 return;
880 }
881 if (crypted) {
Chris Masone34af2182011-08-22 11:59:36 -0700882 storage->SetCryptedString(id, key, value);
Darin Petkovba40dd32011-07-11 20:06:39 -0700883 return;
884 }
Chris Masone34af2182011-08-22 11:59:36 -0700885 storage->SetString(id, key, value);
Darin Petkovba40dd32011-07-11 20:06:39 -0700886}
887
Chris Masone34af2182011-08-22 11:59:36 -0700888void Service::LoadEapCredentials(StoreInterface *storage, const string &id) {
Gaurav Shah10109f22011-11-11 20:16:22 -0800889 EapCredentials eap;
890 storage->GetCryptedString(id, kStorageEapIdentity, &eap.identity);
891 storage->GetString(id, kStorageEapEap, &eap.eap);
892 storage->GetString(id, kStorageEapInnerEap, &eap.inner_eap);
Darin Petkovba40dd32011-07-11 20:06:39 -0700893 storage->GetCryptedString(id,
894 kStorageEapAnonymousIdentity,
Gaurav Shah10109f22011-11-11 20:16:22 -0800895 &eap.anonymous_identity);
896 storage->GetString(id, kStorageEapClientCert, &eap.client_cert);
897 storage->GetString(id, kStorageEapCertID, &eap.cert_id);
898 storage->GetString(id, kStorageEapPrivateKey, &eap.private_key);
Darin Petkovba40dd32011-07-11 20:06:39 -0700899 storage->GetCryptedString(id,
900 kStorageEapPrivateKeyPassword,
Gaurav Shah10109f22011-11-11 20:16:22 -0800901 &eap.private_key_password);
902 storage->GetString(id, kStorageEapKeyID, &eap.key_id);
903 storage->GetString(id, kStorageEapCACert, &eap.ca_cert);
904 storage->GetString(id, kStorageEapCACertID, &eap.ca_cert_id);
Paul Stewartecf4cd12012-04-17 11:08:39 -0700905 storage->GetString(id, kStorageEapCACertNSS, &eap.ca_cert_nss);
Gaurav Shah10109f22011-11-11 20:16:22 -0800906 storage->GetBool(id, kStorageEapUseSystemCAs, &eap.use_system_cas);
907 storage->GetString(id, kStorageEapPIN, &eap.pin);
908 storage->GetCryptedString(id, kStorageEapPassword, &eap.password);
909 storage->GetString(id, kStorageEapKeyManagement, &eap.key_management);
910 set_eap(eap);
Darin Petkovba40dd32011-07-11 20:06:39 -0700911}
912
Chris Masone34af2182011-08-22 11:59:36 -0700913void Service::SaveEapCredentials(StoreInterface *storage, const string &id) {
Darin Petkovba40dd32011-07-11 20:06:39 -0700914 bool save = save_credentials_;
Chris Masone34af2182011-08-22 11:59:36 -0700915 SaveString(storage, id, kStorageEapIdentity, eap_.identity, true, save);
916 SaveString(storage, id, kStorageEapEap, eap_.eap, false, true);
917 SaveString(storage, id, kStorageEapInnerEap, eap_.inner_eap, false, true);
Darin Petkovba40dd32011-07-11 20:06:39 -0700918 SaveString(storage,
Chris Masone34af2182011-08-22 11:59:36 -0700919 id,
Darin Petkovba40dd32011-07-11 20:06:39 -0700920 kStorageEapAnonymousIdentity,
921 eap_.anonymous_identity,
922 true,
923 save);
Chris Masone34af2182011-08-22 11:59:36 -0700924 SaveString(storage, id, kStorageEapClientCert, eap_.client_cert, false, save);
925 SaveString(storage, id, kStorageEapCertID, eap_.cert_id, false, save);
926 SaveString(storage, id, kStorageEapPrivateKey, eap_.private_key, false, save);
Darin Petkovba40dd32011-07-11 20:06:39 -0700927 SaveString(storage,
Chris Masone34af2182011-08-22 11:59:36 -0700928 id,
Darin Petkovba40dd32011-07-11 20:06:39 -0700929 kStorageEapPrivateKeyPassword,
930 eap_.private_key_password,
931 true,
932 save);
Chris Masone34af2182011-08-22 11:59:36 -0700933 SaveString(storage, id, kStorageEapKeyID, eap_.key_id, false, save);
934 SaveString(storage, id, kStorageEapCACert, eap_.ca_cert, false, true);
935 SaveString(storage, id, kStorageEapCACertID, eap_.ca_cert_id, false, true);
Paul Stewartecf4cd12012-04-17 11:08:39 -0700936 SaveString(storage, id, kStorageEapCACertNSS, eap_.ca_cert_nss, false, true);
Chris Masone34af2182011-08-22 11:59:36 -0700937 storage->SetBool(id, kStorageEapUseSystemCAs, eap_.use_system_cas);
938 SaveString(storage, id, kStorageEapPIN, eap_.pin, false, save);
939 SaveString(storage, id, kStorageEapPassword, eap_.password, true, save);
Darin Petkovba40dd32011-07-11 20:06:39 -0700940 SaveString(storage,
Chris Masone34af2182011-08-22 11:59:36 -0700941 id,
Darin Petkovba40dd32011-07-11 20:06:39 -0700942 kStorageEapKeyManagement,
943 eap_.key_management,
944 false,
945 true);
946}
947
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800948void Service::UnloadEapCredentials() {
949 eap_.identity = "";
950 eap_.eap = "";
951 eap_.inner_eap = "";
952 eap_.anonymous_identity = "";
953 eap_.client_cert = "";
954 eap_.cert_id = "";
955 eap_.private_key = "";
956 eap_.private_key_password = "";
957 eap_.key_id = "";
958 eap_.ca_cert = "";
959 eap_.ca_cert_id = "";
Paul Stewart20550982012-04-16 12:16:11 -0700960 eap_.use_system_cas = true;
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800961 eap_.pin = "";
962 eap_.password = "";
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800963}
964
Paul Stewartcb59fed2012-03-21 21:14:46 -0700965void Service::IgnoreParameterForConfigure(const string &parameter) {
966 parameters_ignored_for_configure_.insert(parameter);
967}
968
Paul Stewartac4ac002011-08-26 12:04:26 -0700969const string &Service::GetEAPKeyManagement() const {
970 return eap_.key_management;
971}
972
973void Service::SetEAPKeyManagement(const string &key_management) {
974 eap_.key_management = key_management;
975}
976
Thieu Le284fe792012-01-31 17:53:19 -0800977bool Service::GetAutoConnect(Error */*error*/) {
978 return auto_connect();
979}
980
981void Service::SetAutoConnect(const bool &connect, Error *error) {
Wade Guthrie4f28e8b2012-04-11 10:52:07 -0700982 set_auto_connect(connect);
Thieu Le284fe792012-01-31 17:53:19 -0800983}
984
Paul Stewartd215af62012-04-24 23:25:50 -0700985string Service::GetCheckPortal(Error *error) {
986 return check_portal_;
987}
988
989void Service::SetCheckPortal(const string &check_portal, Error *error) {
990 if (check_portal == check_portal_) {
991 return;
992 }
993 if (check_portal != kCheckPortalFalse &&
994 check_portal != kCheckPortalTrue &&
995 check_portal != kCheckPortalAuto) {
996 Error::PopulateAndLog(error, Error::kInvalidArguments,
997 base::StringPrintf(
998 "Invalid Service CheckPortal property value: %s",
999 check_portal.c_str()));
1000 return;
1001 }
1002 check_portal_ = check_portal;
1003}
1004
Paul Stewart9f32d192012-01-30 20:37:50 -08001005void Service::SetEAPPassword(const string &password, Error */*error*/) {
1006 eap_.password = password;
1007}
1008
1009void Service::SetEAPPrivateKeyPassword(const string &password,
1010 Error */*error*/) {
1011 eap_.private_key_password = password;
1012}
1013
Paul Stewart0c438332012-04-11 07:55:27 -07001014string Service::GetNameProperty(Error *error) {
1015 return friendly_name_;
1016}
1017
1018void Service::AssertTrivialSetNameProperty(const string &name, Error *error) {
1019 if (name != friendly_name_) {
1020 Error::PopulateAndLog(error, Error::kInvalidArguments,
Paul Stewart2bf424f2012-04-11 18:59:39 -07001021 base::StringPrintf(
1022 "Service Name property cannot be modified "
1023 "(%s to %s)", friendly_name_.c_str(),
1024 name.c_str()));
Paul Stewart0c438332012-04-11 07:55:27 -07001025 }
1026}
1027
Paul Stewart1b1a7f22012-01-06 16:24:06 -08001028string Service::GetProfileRpcId(Error *error) {
1029 if (!profile_) {
1030 // This happens in some unit tests where profile_ is not set.
1031 error->Populate(Error::kNotFound);
1032 return "";
1033 }
1034 return profile_->GetRpcIdentifier();
1035}
1036
1037void Service::SetProfileRpcId(const string &profile, Error *error) {
1038 manager_->SetProfileForService(this, profile, error);
1039}
1040
Paul Stewartbe5f5b32011-12-07 17:11:11 -08001041uint16 Service::GetHTTPProxyPort(Error */*error*/) {
1042 if (http_proxy_.get()) {
1043 return static_cast<uint16>(http_proxy_->proxy_port());
1044 }
1045 return 0;
1046}
1047
mukesh agrawalcf24a242012-05-21 16:46:11 -07001048void Service::SaveToProfile() {
1049 if (profile_.get() && profile_->GetConstStorage()) {
1050 profile_->UpdateService(this);
1051 }
1052}
1053
Darin Petkovd78ee7e2012-01-12 11:21:10 +01001054void Service::SetStrength(uint8 strength) {
1055 if (strength == strength_) {
1056 return;
1057 }
1058 strength_ = strength;
1059 adaptor_->EmitUint8Changed(flimflam::kSignalStrengthProperty, strength);
1060}
1061
Paul Stewart75897df2011-04-27 09:05:53 -07001062} // namespace shill