blob: 72cde1f0b27e573b66f203793206f2987ebe76de [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 Masone3bd3c8c2011-06-13 08:20:26 -070014#include <base/memory/scoped_ptr.h>
mukesh agrawal51a7e932011-07-27 16:18:26 -070015#include <base/string_number_conversions.h>
Paul Stewart2bf424f2012-04-11 18:59:39 -070016#include <base/stringprintf.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070017#include <chromeos/dbus/service_constants.h>
Chris Masoneee929b72011-05-10 10:02:18 -070018
Paul Stewartbe5f5b32011-12-07 17:11:11 -080019#include "shill/connection.h"
Paul Stewart75897df2011-04-27 09:05:53 -070020#include "shill/control_interface.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070021#include "shill/error.h"
Paul Stewartbe5f5b32011-12-07 17:11:11 -080022#include "shill/http_proxy.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070023#include "shill/logging.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"
Chris Masoned7732e42011-05-20 11:08:56 -070029#include "shill/service_dbus_adaptor.h"
Darin Petkov5eb05422012-05-11 15:45:25 +020030#include "shill/sockets.h"
Darin Petkovba40dd32011-07-11 20:06:39 -070031#include "shill/store_interface.h"
Paul Stewart75897df2011-04-27 09:05:53 -070032
Christopher Wiley0801d192012-09-24 11:57:15 -070033using base::Bind;
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";
Christopher Wiley0801d192012-09-24 11:57:15 -070044const char Service::kAutoConnThrottled[] = "throttled";
mukesh agrawalbf14e942012-03-02 14:36:34 -080045
Paul Stewartbc6e7392012-05-24 07:07:48 -070046const size_t Service::kEAPMaxCertificationElements = 10;
47
Darin Petkovba40dd32011-07-11 20:06:39 -070048const char Service::kCheckPortalAuto[] = "auto";
49const char Service::kCheckPortalFalse[] = "false";
50const char Service::kCheckPortalTrue[] = "true";
51
Paul Stewartac4ac002011-08-26 12:04:26 -070052const int Service::kPriorityNone = 0;
53
Darin Petkov2f903b32012-04-18 12:56:43 +020054const char Service::kServiceSortAutoConnect[] = "AutoConnect";
55const char Service::kServiceSortConnectable[] = "Connectable";
Paul Stewartdf3c0a82012-11-09 15:54:33 -080056const char Service::kServiceSortDependency[] = "Dependency";
Darin Petkov2f903b32012-04-18 12:56:43 +020057const char Service::kServiceSortFavorite[] = "Favorite";
mukesh agrawalddc378f2012-02-17 18:26:20 -080058const char Service::kServiceSortIsConnected[] = "IsConnected";
59const char Service::kServiceSortIsConnecting[] = "IsConnecting";
60const char Service::kServiceSortIsFailed[] = "IsFailed";
Paul Stewarta121c442012-06-09 14:12:58 -070061const char Service::kServiceSortIsPortalled[] = "IsPortal";
Darin Petkov2f903b32012-04-18 12:56:43 +020062const char Service::kServiceSortPriority[] = "Priority";
mukesh agrawalddc378f2012-02-17 18:26:20 -080063const char Service::kServiceSortSecurityEtc[] = "SecurityEtc";
Darin Petkov2f903b32012-04-18 12:56:43 +020064const char Service::kServiceSortTechnology[] = "Technology";
mukesh agrawalddc378f2012-02-17 18:26:20 -080065const char Service::kServiceSortUniqueName[] = "UniqueName";
66
Darin Petkovba40dd32011-07-11 20:06:39 -070067const char Service::kStorageAutoConnect[] = "AutoConnect";
68const char Service::kStorageCheckPortal[] = "CheckPortal";
69const char Service::kStorageEapAnonymousIdentity[] = "EAP.AnonymousIdentity";
70const char Service::kStorageEapCACert[] = "EAP.CACert";
71const char Service::kStorageEapCACertID[] = "EAP.CACertID";
Paul Stewartecf4cd12012-04-17 11:08:39 -070072const char Service::kStorageEapCACertNSS[] = "EAP.CACertNSS";
Darin Petkovba40dd32011-07-11 20:06:39 -070073const char Service::kStorageEapCertID[] = "EAP.CertID";
74const char Service::kStorageEapClientCert[] = "EAP.ClientCert";
75const char Service::kStorageEapEap[] = "EAP.EAP";
76const char Service::kStorageEapIdentity[] = "EAP.Identity";
77const char Service::kStorageEapInnerEap[] = "EAP.InnerEAP";
78const char Service::kStorageEapKeyID[] = "EAP.KeyID";
79const char Service::kStorageEapKeyManagement[] = "EAP.KeyMgmt";
80const char Service::kStorageEapPIN[] = "EAP.PIN";
81const char Service::kStorageEapPassword[] = "EAP.Password";
82const char Service::kStorageEapPrivateKey[] = "EAP.PrivateKey";
83const char Service::kStorageEapPrivateKeyPassword[] = "EAP.PrivateKeyPassword";
84const char Service::kStorageEapUseSystemCAs[] = "EAP.UseSystemCAs";
Paul Stewart0756db92012-01-27 08:34:47 -080085const char Service::kStorageError[] = "Error";
Darin Petkovba40dd32011-07-11 20:06:39 -070086const char Service::kStorageFavorite[] = "Favorite";
Paul Stewart0756db92012-01-27 08:34:47 -080087const char Service::kStorageGUID[] = "GUID";
mukesh agrawalcf24a242012-05-21 16:46:11 -070088const char Service::kStorageHasEverConnected[] = "HasEverConnected";
Darin Petkovba40dd32011-07-11 20:06:39 -070089const char Service::kStorageName[] = "Name";
90const char Service::kStoragePriority[] = "Priority";
91const char Service::kStorageProxyConfig[] = "ProxyConfig";
92const char Service::kStorageSaveCredentials[] = "SaveCredentials";
Paul Stewart2706aaf2011-12-14 16:44:04 -080093const char Service::kStorageType[] = "Type";
Paul Stewart987e71e2011-12-05 09:45:06 -080094const char Service::kStorageUIData[] = "UIData";
Darin Petkovba40dd32011-07-11 20:06:39 -070095
mukesh agrawal8f3f7752012-02-17 19:42:09 -080096const uint8 Service::kStrengthMax = 100;
97const uint8 Service::kStrengthMin = 0;
98
Christopher Wiley0801d192012-09-24 11:57:15 -070099const uint64 Service::kMaxAutoConnectCooldownTimeMilliseconds = 30 * 60 * 1000;
100const uint64 Service::kMinAutoConnectCooldownTimeMilliseconds = 1000;
101const uint64 Service::kAutoConnectCooldownBackoffFactor = 2;
102
mukesh agrawal51a7e932011-07-27 16:18:26 -0700103// static
104unsigned int Service::serial_number_ = 0;
105
Paul Stewart75897df2011-04-27 09:05:53 -0700106Service::Service(ControlInterface *control_interface,
mukesh agrawalb54601c2011-06-07 17:39:22 -0700107 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -0800108 Metrics *metrics,
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700109 Manager *manager,
Gaurav Shah435de2c2011-11-17 19:01:07 -0800110 Technology::Identifier technology)
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400111 : state_(kStateIdle),
Paul Stewart03dba0b2011-08-22 16:32:45 -0700112 failure_(kFailureUnknown),
113 auto_connect_(false),
Darin Petkovba40dd32011-07-11 20:06:39 -0700114 check_portal_(kCheckPortalAuto),
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700115 connectable_(false),
Paul Stewartf2d60912012-07-15 08:37:30 -0700116 error_(ConnectFailureToString(failure_)),
mukesh agrawaladb68482012-01-17 16:31:51 -0800117 explicitly_disconnected_(false),
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700118 favorite_(false),
Darin Petkovba40dd32011-07-11 20:06:39 -0700119 priority_(kPriorityNone),
Paul Stewart1ca3e852011-11-04 07:50:49 -0700120 security_level_(0),
Paul Stewart22aa71b2011-09-16 12:15:11 -0700121 strength_(0),
Darin Petkovba40dd32011-07-11 20:06:39 -0700122 save_credentials_(true),
Gaurav Shah435de2c2011-11-17 19:01:07 -0800123 technology_(technology),
mukesh agrawal568b5c62012-02-28 14:44:47 -0800124 failed_time_(0),
mukesh agrawalcf24a242012-05-21 16:46:11 -0700125 has_ever_connected_(false),
Christopher Wiley0801d192012-09-24 11:57:15 -0700126 auto_connect_cooldown_milliseconds_(0),
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700127 dispatcher_(dispatcher),
mukesh agrawald835b202011-10-07 15:26:47 -0700128 unique_name_(base::UintToString(serial_number_++)),
129 friendly_name_(unique_name_),
Chris Masone6791a432011-07-12 13:23:19 -0700130 adaptor_(control_interface->CreateServiceAdaptor(this)),
Thieu Le3426c8f2012-01-11 17:35:11 -0800131 metrics_(metrics),
Darin Petkov5eb05422012-05-11 15:45:25 +0200132 manager_(manager),
Christopher Wiley0801d192012-09-24 11:57:15 -0700133 sockets_(new Sockets()),
134 weak_ptr_factory_(this) {
Thieu Le284fe792012-01-31 17:53:19 -0800135 HelpRegisterDerivedBool(flimflam::kAutoConnectProperty,
136 &Service::GetAutoConnect,
137 &Service::SetAutoConnect);
Chris Masone4d42df82011-07-02 17:09:39 -0700138
139 // flimflam::kActivationStateProperty: Registered in CellularService
140 // flimflam::kCellularApnProperty: Registered in CellularService
141 // flimflam::kCellularLastGoodApnProperty: Registered in CellularService
142 // flimflam::kNetworkTechnologyProperty: Registered in CellularService
Darin Petkov3335b372011-08-22 11:05:32 -0700143 // flimflam::kOperatorNameProperty: DEPRECATED
144 // flimflam::kOperatorCodeProperty: DEPRECATED
Chris Masone4d42df82011-07-02 17:09:39 -0700145 // flimflam::kRoamingStateProperty: Registered in CellularService
Darin Petkov3335b372011-08-22 11:05:32 -0700146 // flimflam::kServingOperatorProperty: Registered in CellularService
Chris Masone4d42df82011-07-02 17:09:39 -0700147 // flimflam::kPaymentURLProperty: Registered in CellularService
148
Paul Stewartd215af62012-04-24 23:25:50 -0700149 HelpRegisterDerivedString(flimflam::kCheckPortalProperty,
150 &Service::GetCheckPortal,
151 &Service::SetCheckPortal);
Chris Masone27c4aa52011-07-02 13:10:14 -0700152 store_.RegisterConstBool(flimflam::kConnectableProperty, &connectable_);
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400153 HelpRegisterDerivedRpcIdentifier(flimflam::kDeviceProperty,
154 &Service::GetDeviceRpcId,
155 NULL);
Paul Stewart4c561612012-03-21 12:49:01 -0700156 store_.RegisterString(flimflam::kGuidProperty, &guid_);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700157
Chris Masoneb2e326b2011-07-12 13:28:51 -0700158 store_.RegisterString(flimflam::kEapIdentityProperty, &eap_.identity);
159 store_.RegisterString(flimflam::kEAPEAPProperty, &eap_.eap);
160 store_.RegisterString(flimflam::kEapPhase2AuthProperty, &eap_.inner_eap);
Chris Masone27c4aa52011-07-02 13:10:14 -0700161 store_.RegisterString(flimflam::kEapAnonymousIdentityProperty,
Chris Masoneb2e326b2011-07-12 13:28:51 -0700162 &eap_.anonymous_identity);
163 store_.RegisterString(flimflam::kEAPClientCertProperty, &eap_.client_cert);
164 store_.RegisterString(flimflam::kEAPCertIDProperty, &eap_.cert_id);
165 store_.RegisterString(flimflam::kEapPrivateKeyProperty, &eap_.private_key);
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800166 HelpRegisterWriteOnlyDerivedString(flimflam::kEapPrivateKeyPasswordProperty,
167 &Service::SetEAPPrivateKeyPassword,
168 NULL,
169 &eap_.private_key_password);
Chris Masoneb2e326b2011-07-12 13:28:51 -0700170 store_.RegisterString(flimflam::kEAPKeyIDProperty, &eap_.key_id);
171 store_.RegisterString(flimflam::kEapCaCertProperty, &eap_.ca_cert);
172 store_.RegisterString(flimflam::kEapCaCertIDProperty, &eap_.ca_cert_id);
Paul Stewartecf4cd12012-04-17 11:08:39 -0700173 store_.RegisterString(flimflam::kEapCaCertNssProperty, &eap_.ca_cert_nss);
Chris Masoneb2e326b2011-07-12 13:28:51 -0700174 store_.RegisterString(flimflam::kEAPPINProperty, &eap_.pin);
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800175 HelpRegisterWriteOnlyDerivedString(flimflam::kEapPasswordProperty,
176 &Service::SetEAPPassword,
177 NULL,
178 &eap_.password);
Chris Masoneb2e326b2011-07-12 13:28:51 -0700179 store_.RegisterString(flimflam::kEapKeyMgmtProperty, &eap_.key_management);
180 store_.RegisterBool(flimflam::kEapUseSystemCAsProperty, &eap_.use_system_cas);
Paul Stewartbc6e7392012-05-24 07:07:48 -0700181 store_.RegisterConstStrings(shill::kEapRemoteCertificationProperty,
182 &eap_.remote_certification);
183 store_.RegisterString(shill::kEapSubjectMatchProperty,
184 &eap_.subject_match);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700185
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400186 // TODO(ers): in flimflam clearing Error has the side-effect of
187 // setting the service state to IDLE. Is this important? I could
188 // see an autotest depending on it.
Chris Masone27c4aa52011-07-02 13:10:14 -0700189 store_.RegisterConstString(flimflam::kErrorProperty, &error_);
190 store_.RegisterConstBool(flimflam::kFavoriteProperty, &favorite_);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800191 HelpRegisterDerivedUint16(shill::kHTTPProxyPortProperty,
192 &Service::GetHTTPProxyPort,
193 NULL);
Paul Stewart10241e32012-04-23 18:15:06 -0700194 HelpRegisterDerivedRpcIdentifier(shill::kIPConfigProperty,
195 &Service::GetIPConfigRpcIdentifier,
196 NULL);
Chris Masone27c4aa52011-07-02 13:10:14 -0700197 HelpRegisterDerivedBool(flimflam::kIsActiveProperty,
Chris Masoneb2e326b2011-07-12 13:28:51 -0700198 &Service::IsActive,
199 NULL);
Chris Masone4d42df82011-07-02 17:09:39 -0700200 // flimflam::kModeProperty: Registered in WiFiService
Paul Stewart0c438332012-04-11 07:55:27 -0700201
202 // Although this is a read-only property, some callers want to blindly
203 // set this value to its current value.
204 HelpRegisterDerivedString(flimflam::kNameProperty,
205 &Service::GetNameProperty,
206 &Service::AssertTrivialSetNameProperty);
Chris Masone4d42df82011-07-02 17:09:39 -0700207 // flimflam::kPassphraseProperty: Registered in WiFiService
208 // flimflam::kPassphraseRequiredProperty: Registered in WiFiService
Chris Masone27c4aa52011-07-02 13:10:14 -0700209 store_.RegisterInt32(flimflam::kPriorityProperty, &priority_);
210 HelpRegisterDerivedString(flimflam::kProfileProperty,
211 &Service::GetProfileRpcId,
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800212 &Service::SetProfileRpcId);
Chris Masone4d42df82011-07-02 17:09:39 -0700213 store_.RegisterString(flimflam::kProxyConfigProperty, &proxy_config_);
Chris Masoneb2e326b2011-07-12 13:28:51 -0700214 store_.RegisterBool(flimflam::kSaveCredentialsProperty, &save_credentials_);
Gaurav Shah435de2c2011-11-17 19:01:07 -0800215 HelpRegisterDerivedString(flimflam::kTypeProperty,
Darin Petkov58f0b6d2012-06-12 12:52:30 +0200216 &Service::CalculateTechnology,
Gaurav Shah435de2c2011-11-17 19:01:07 -0800217 NULL);
Chris Masone4d42df82011-07-02 17:09:39 -0700218 // flimflam::kSecurityProperty: Registered in WiFiService
Chris Masone27c4aa52011-07-02 13:10:14 -0700219 HelpRegisterDerivedString(flimflam::kStateProperty,
220 &Service::CalculateState,
221 NULL);
Darin Petkovd78ee7e2012-01-12 11:21:10 +0100222 store_.RegisterConstUint8(flimflam::kSignalStrengthProperty, &strength_);
Paul Stewart987e71e2011-12-05 09:45:06 -0800223 store_.RegisterString(flimflam::kUIDataProperty, &ui_data_);
Chris Masone4d42df82011-07-02 17:09:39 -0700224 // flimflam::kWifiAuthMode: Registered in WiFiService
225 // flimflam::kWifiHiddenSsid: Registered in WiFiService
226 // flimflam::kWifiFrequency: Registered in WiFiService
227 // flimflam::kWifiPhyMode: Registered in WiFiService
228 // flimflam::kWifiHexSsid: Registered in WiFiService
Thieu Le48e6d6d2011-12-06 00:40:27 +0000229
230 metrics_->RegisterService(this);
231
Paul Stewart1062d9d2012-04-27 10:42:27 -0700232 static_ip_parameters_.PlumbPropertyStore(&store_);
233
Paul Stewartcb59fed2012-03-21 21:14:46 -0700234 IgnoreParameterForConfigure(flimflam::kTypeProperty);
Paul Stewart7f61e522012-03-22 11:13:45 -0700235 IgnoreParameterForConfigure(flimflam::kProfileProperty);
Paul Stewartcb59fed2012-03-21 21:14:46 -0700236
Ben Chanfad4a0b2012-04-18 15:49:59 -0700237 SLOG(Service, 2) << "Service initialized.";
Paul Stewart75897df2011-04-27 09:05:53 -0700238}
239
Thieu Le48e6d6d2011-12-06 00:40:27 +0000240Service::~Service() {
Darin Petkov9cd7ca12012-07-03 11:06:40 +0200241 LOG(INFO) << "Service destroyed: " << friendly_name_;
Thieu Le48e6d6d2011-12-06 00:40:27 +0000242 metrics_->DeregisterService(this);
243}
Paul Stewart75897df2011-04-27 09:05:53 -0700244
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000245void Service::AutoConnect() {
Darin Petkov3abc3be2012-06-27 10:48:23 +0200246 const char *reason = NULL;
247 if (IsAutoConnectable(&reason)) {
Darin Petkov9cd7ca12012-07-03 11:06:40 +0200248 LOG(INFO) << "Auto-connecting to " << friendly_name_;
Christopher Wiley0801d192012-09-24 11:57:15 -0700249 ThrottleFutureAutoConnects();
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000250 Error error;
251 Connect(&error);
mukesh agrawal76d13882012-01-12 15:23:11 -0800252 } else {
Darin Petkov9cd7ca12012-07-03 11:06:40 +0200253 LOG(INFO) << "Suppressed autoconnect to " << friendly_name_ << " "
mukesh agrawalbf14e942012-03-02 14:36:34 -0800254 << "(" << reason << ")";
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000255 }
256}
257
mukesh agrawaladb68482012-01-17 16:31:51 -0800258void Service::Connect(Error */*error*/) {
259 explicitly_disconnected_ = false;
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400260 // clear any failure state from a previous connect attempt
261 SetState(kStateIdle);
mukesh agrawaladb68482012-01-17 16:31:51 -0800262}
263
264void Service::Disconnect(Error */*error*/) {
Christopher Wiley3e7635e2012-08-15 09:46:17 -0700265 MemoryLog::GetInstance()->FlushToDisk();
mukesh agrawaladb68482012-01-17 16:31:51 -0800266}
267
Christopher Wileyabd3b502012-09-26 13:08:52 -0700268void Service::DisconnectWithFailure(ConnectFailure failure, Error *error) {
269 Disconnect(error);
270 SetFailure(failure);
271}
272
273void Service::UserInitiatedDisconnect(Error *error) {
274 Disconnect(error);
275 explicitly_disconnected_ = true;
276}
277
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500278void Service::ActivateCellularModem(const string &/*carrier*/,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500279 Error *error,
280 const ResultCallback &/*callback*/) {
281 Error::PopulateAndLog(error, Error::kNotSupported,
282 "Service doesn't support cellular modem activation.");
Darin Petkovc408e692011-08-17 13:47:15 -0700283}
284
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800285bool Service::IsActive(Error */*error*/) {
Paul Stewartbfb82552012-10-24 16:48:48 -0700286 return state() != kStateUnknown &&
287 state() != kStateIdle &&
288 state() != kStateFailure;
Paul Stewart03dba0b2011-08-22 16:32:45 -0700289}
290
291void Service::SetState(ConnectState state) {
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800292 LOG(INFO) << "In " << __func__ << "(): Service " << friendly_name_
293 << " state " << ConnectStateToString(state_) << " -> "
294 << ConnectStateToString(state);
mukesh agrawal7ec71312011-11-10 02:08:26 +0000295
Paul Stewart03dba0b2011-08-22 16:32:45 -0700296 if (state == state_) {
297 return;
298 }
299
300 state_ = state;
301 if (state != kStateFailure) {
302 failure_ = kFailureUnknown;
303 }
mukesh agrawalcf24a242012-05-21 16:46:11 -0700304 if (state == kStateConnected) {
Christopher Wiley0801d192012-09-24 11:57:15 -0700305 failed_time_ = 0;
mukesh agrawalcf24a242012-05-21 16:46:11 -0700306 has_ever_connected_ = true;
307 SaveToProfile();
Christopher Wiley0801d192012-09-24 11:57:15 -0700308 // When we succeed in connecting, forget that connects failed in the past.
309 // Give services one chance at a fast autoconnect retry by resetting the
310 // cooldown to 0 to indicate that the last connect was successful.
311 auto_connect_cooldown_milliseconds_ = 0;
312 reenable_auto_connect_task_.Cancel();
mukesh agrawalcf24a242012-05-21 16:46:11 -0700313 }
Paul Stewartf2d60912012-07-15 08:37:30 -0700314 UpdateErrorProperty();
Paul Stewart03dba0b2011-08-22 16:32:45 -0700315 manager_->UpdateService(this);
Thieu Le48e6d6d2011-12-06 00:40:27 +0000316 metrics_->NotifyServiceStateChanged(this, state);
Darin Petkov58f0b6d2012-06-12 12:52:30 +0200317 adaptor_->EmitStringChanged(flimflam::kStateProperty, GetStateString());
Paul Stewart03dba0b2011-08-22 16:32:45 -0700318}
319
Christopher Wiley0801d192012-09-24 11:57:15 -0700320void Service::ReEnableAutoConnectTask() {
321 // Kill the thing blocking AutoConnect().
322 reenable_auto_connect_task_.Cancel();
323 // Post to the manager, giving it an opportunity to AutoConnect again.
324 manager_->UpdateService(this);
325}
326
327void Service::ThrottleFutureAutoConnects() {
328 if (auto_connect_cooldown_milliseconds_ > 0) {
329 LOG(INFO) << "Throttling autoconnect to " << friendly_name_ << " for "
330 << auto_connect_cooldown_milliseconds_ << " milliseconds.";
331 reenable_auto_connect_task_.Reset(Bind(&Service::ReEnableAutoConnectTask,
332 weak_ptr_factory_.GetWeakPtr()));
333 dispatcher_->PostDelayedTask(reenable_auto_connect_task_.callback(),
334 auto_connect_cooldown_milliseconds_);
335 }
336 auto_connect_cooldown_milliseconds_ =
337 std::min(kMaxAutoConnectCooldownTimeMilliseconds,
338 std::max(kMinAutoConnectCooldownTimeMilliseconds,
339 auto_connect_cooldown_milliseconds_ *
340 kAutoConnectCooldownBackoffFactor));
341}
342
Paul Stewart03dba0b2011-08-22 16:32:45 -0700343void Service::SetFailure(ConnectFailure failure) {
344 failure_ = failure;
mukesh agrawal568b5c62012-02-28 14:44:47 -0800345 failed_time_ = time(NULL);
Paul Stewartf2d60912012-07-15 08:37:30 -0700346 UpdateErrorProperty();
Paul Stewart03dba0b2011-08-22 16:32:45 -0700347 SetState(kStateFailure);
348}
349
mukesh agrawal568b5c62012-02-28 14:44:47 -0800350void Service::SetFailureSilent(ConnectFailure failure) {
351 // Note that order matters here, since SetState modifies |failure_| and
352 // |failed_time_|.
353 SetState(kStateIdle);
354 failure_ = failure;
Paul Stewartf2d60912012-07-15 08:37:30 -0700355 UpdateErrorProperty();
mukesh agrawal568b5c62012-02-28 14:44:47 -0800356 failed_time_ = time(NULL);
357}
358
Chris Masone6791a432011-07-12 13:23:19 -0700359string Service::GetRpcIdentifier() const {
Chris Masone3c3f6a12011-07-01 10:01:41 -0700360 return adaptor_->GetRpcIdentifier();
361}
362
Paul Stewartbba6a5b2011-11-02 18:45:59 -0700363bool Service::IsLoadableFrom(StoreInterface *storage) const {
364 return storage->ContainsGroup(GetStorageIdentifier());
365}
366
Chris Masone9d779932011-08-25 16:33:41 -0700367bool Service::Load(StoreInterface *storage) {
368 const string id = GetStorageIdentifier();
Chris Masone34af2182011-08-22 11:59:36 -0700369 if (!storage->ContainsGroup(id)) {
370 LOG(WARNING) << "Service is not available in the persistent store: " << id;
371 return false;
372 }
373 storage->GetBool(id, kStorageAutoConnect, &auto_connect_);
374 storage->GetString(id, kStorageCheckPortal, &check_portal_);
375 storage->GetBool(id, kStorageFavorite, &favorite_);
mukesh agrawalcf24a242012-05-21 16:46:11 -0700376 storage->GetString(id, kStorageGUID, &guid_);
377 storage->GetBool(id, kStorageHasEverConnected, &has_ever_connected_);
Chris Masone34af2182011-08-22 11:59:36 -0700378 storage->GetInt(id, kStoragePriority, &priority_);
379 storage->GetString(id, kStorageProxyConfig, &proxy_config_);
380 storage->GetBool(id, kStorageSaveCredentials, &save_credentials_);
Paul Stewart987e71e2011-12-05 09:45:06 -0800381 storage->GetString(id, kStorageUIData, &ui_data_);
Chris Masone34af2182011-08-22 11:59:36 -0700382
383 LoadEapCredentials(storage, id);
Paul Stewart1062d9d2012-04-27 10:42:27 -0700384 static_ip_parameters_.Load(storage, id);
Chris Masone34af2182011-08-22 11:59:36 -0700385 // TODO(petkov): Load these:
386
Chris Masone34af2182011-08-22 11:59:36 -0700387 // "Failure"
388 // "Modified"
389 // "LastAttempt"
Chris Masone34af2182011-08-22 11:59:36 -0700390
mukesh agrawaladb68482012-01-17 16:31:51 -0800391 explicitly_disconnected_ = false;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800392 favorite_ = true;
393
Chris Masone34af2182011-08-22 11:59:36 -0700394 return true;
Darin Petkovba40dd32011-07-11 20:06:39 -0700395}
396
Paul Stewart65512e12012-03-26 18:01:08 -0700397bool Service::Unload() {
Paul Stewart7fb09382012-07-18 17:32:27 -0700398 auto_connect_ = IsAutoConnectByDefault();
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800399 check_portal_ = kCheckPortalAuto;
Christopher Wileyabd3b502012-09-26 13:08:52 -0700400 explicitly_disconnected_ = false;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800401 favorite_ = false;
Paul Stewarte7cce8f2012-09-11 10:56:38 -0700402 guid_ = "";
Paul Stewart88769de2012-09-21 13:14:36 -0700403 has_ever_connected_ = false;
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800404 priority_ = kPriorityNone;
405 proxy_config_ = "";
406 save_credentials_ = true;
407 ui_data_ = "";
408
409 UnloadEapCredentials();
Wade Guthrie9e0c2502012-04-19 15:26:40 -0700410 Error error; // Ignored.
411 Disconnect(&error);
Paul Stewart65512e12012-03-26 18:01:08 -0700412 return false;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800413}
414
Chris Masone9d779932011-08-25 16:33:41 -0700415bool Service::Save(StoreInterface *storage) {
416 const string id = GetStorageIdentifier();
Chris Masone34af2182011-08-22 11:59:36 -0700417
Darin Petkov58f0b6d2012-06-12 12:52:30 +0200418 storage->SetString(id, kStorageType, GetTechnologyString());
Paul Stewart2706aaf2011-12-14 16:44:04 -0800419
Chris Masone34af2182011-08-22 11:59:36 -0700420 // TODO(petkov): We could choose to simplify the saving code by removing most
421 // conditionals thus saving even default values.
Wade Guthrie4f28e8b2012-04-11 10:52:07 -0700422 storage->SetBool(id, kStorageAutoConnect, auto_connect_);
Chris Masone34af2182011-08-22 11:59:36 -0700423 if (check_portal_ == kCheckPortalAuto) {
424 storage->DeleteKey(id, kStorageCheckPortal);
425 } else {
426 storage->SetString(id, kStorageCheckPortal, check_portal_);
427 }
428 storage->SetBool(id, kStorageFavorite, favorite_);
mukesh agrawalcf24a242012-05-21 16:46:11 -0700429 SaveString(storage, id, kStorageGUID, guid_, false, true);
430 storage->SetBool(id, kStorageHasEverConnected, has_ever_connected_);
mukesh agrawald835b202011-10-07 15:26:47 -0700431 storage->SetString(id, kStorageName, friendly_name_);
Chris Masone34af2182011-08-22 11:59:36 -0700432 if (priority_ != kPriorityNone) {
433 storage->SetInt(id, kStoragePriority, priority_);
434 } else {
435 storage->DeleteKey(id, kStoragePriority);
436 }
Paul Stewart987e71e2011-12-05 09:45:06 -0800437 SaveString(storage, id, kStorageProxyConfig, proxy_config_, false, true);
Chris Masone34af2182011-08-22 11:59:36 -0700438 if (save_credentials_) {
439 storage->DeleteKey(id, kStorageSaveCredentials);
440 } else {
441 storage->SetBool(id, kStorageSaveCredentials, false);
442 }
Paul Stewart987e71e2011-12-05 09:45:06 -0800443 SaveString(storage, id, kStorageUIData, ui_data_, false, true);
Chris Masone34af2182011-08-22 11:59:36 -0700444
445 SaveEapCredentials(storage, id);
Paul Stewart1062d9d2012-04-27 10:42:27 -0700446 static_ip_parameters_.Save(storage, id);
Chris Masone34af2182011-08-22 11:59:36 -0700447
448 // TODO(petkov): Save these:
449
Chris Masone34af2182011-08-22 11:59:36 -0700450 // "Failure"
451 // "Modified"
452 // "LastAttempt"
Chris Masone34af2182011-08-22 11:59:36 -0700453
454 return true;
455}
456
Gary Moraind93615e2012-04-27 11:50:03 -0700457void Service::SaveToCurrentProfile() {
458 // Some unittests do not specify a manager.
459 if (manager()) {
460 manager()->SaveServiceToProfile(this);
461 }
Thieu Led4e9e552012-02-16 16:26:07 -0800462}
463
Paul Stewartcb59fed2012-03-21 21:14:46 -0700464void Service::Configure(const KeyValueStore &args, Error *error) {
465 map<string, bool>::const_iterator bool_it;
Ben Chanfad4a0b2012-04-18 15:49:59 -0700466 SLOG(Service, 5) << "Configuring bool properties:";
Paul Stewartcb59fed2012-03-21 21:14:46 -0700467 for (bool_it = args.bool_properties().begin();
468 bool_it != args.bool_properties().end();
469 ++bool_it) {
470 if (ContainsKey(parameters_ignored_for_configure_, bool_it->first)) {
471 continue;
472 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700473 SLOG(Service, 5) << " " << bool_it->first;
Paul Stewartcb59fed2012-03-21 21:14:46 -0700474 Error set_error;
475 store_.SetBoolProperty(bool_it->first, bool_it->second, &set_error);
Christopher Wiley27b47232012-11-02 13:13:00 -0700476 OnPropertyChanged(bool_it->first);
Paul Stewartcb59fed2012-03-21 21:14:46 -0700477 if (error->IsSuccess() && set_error.IsFailure()) {
478 error->CopyFrom(set_error);
479 }
480 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700481 SLOG(Service, 5) << "Configuring string properties:";
Paul Stewartcb59fed2012-03-21 21:14:46 -0700482 map<string, string>::const_iterator string_it;
483 for (string_it = args.string_properties().begin();
484 string_it != args.string_properties().end();
485 ++string_it) {
486 if (ContainsKey(parameters_ignored_for_configure_, string_it->first)) {
487 continue;
488 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700489 SLOG(Service, 5) << " " << string_it->first;
Paul Stewartcb59fed2012-03-21 21:14:46 -0700490 Error set_error;
491 store_.SetStringProperty(string_it->first, string_it->second, &set_error);
Christopher Wiley27b47232012-11-02 13:13:00 -0700492 OnPropertyChanged(string_it->first);
Paul Stewartcb59fed2012-03-21 21:14:46 -0700493 if (error->IsSuccess() && set_error.IsFailure()) {
494 error->CopyFrom(set_error);
495 }
496 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700497 SLOG(Service, 5) << "Configuring uint32 properties:";
Paul Stewartcb59fed2012-03-21 21:14:46 -0700498 map<string, uint32>::const_iterator int_it;
499 for (int_it = args.uint_properties().begin();
500 int_it != args.uint_properties().end();
501 ++int_it) {
502 if (ContainsKey(parameters_ignored_for_configure_, int_it->first)) {
503 continue;
504 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700505 SLOG(Service, 5) << " " << int_it->first;
Paul Stewartcb59fed2012-03-21 21:14:46 -0700506 Error set_error;
507 store_.SetUint32Property(int_it->first, int_it->second, &set_error);
Christopher Wiley27b47232012-11-02 13:13:00 -0700508 OnPropertyChanged(int_it->first);
Paul Stewartcb59fed2012-03-21 21:14:46 -0700509 if (error->IsSuccess() && set_error.IsFailure()) {
510 error->CopyFrom(set_error);
511 }
512 }
513}
514
Paul Stewart10ccbb32012-04-26 15:59:30 -0700515bool Service::IsRemembered() const {
516 return profile_ && !manager_->IsServiceEphemeral(this);
517}
518
Paul Stewartdf3c0a82012-11-09 15:54:33 -0800519bool Service::IsDependentOn(const ServiceRefPtr &b) const {
520 if (!connection_ || !b) {
521 return false;
522 }
523 return connection_->GetLowerConnection() == b->connection();
524}
525
mukesh agrawal00917ce2011-11-22 23:56:55 +0000526void Service::MakeFavorite() {
527 if (favorite_) {
528 // We do not want to clobber the value of auto_connect_ (it may
529 // be user-set). So return early.
530 return;
531 }
532
533 auto_connect_ = true;
534 favorite_ = true;
535}
536
Darin Petkov5eb05422012-05-11 15:45:25 +0200537void Service::SetConnection(const ConnectionRefPtr &connection) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800538 if (connection.get()) {
Paul Stewart1e3bc4962012-09-14 12:20:22 -0700539 // TODO(pstew): Make this function testable by using a factory here.
540 // http://crosbug.com/34528
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800541 http_proxy_.reset(new HTTPProxy(connection));
Darin Petkov5eb05422012-05-11 15:45:25 +0200542 http_proxy_->Start(dispatcher_, sockets_.get());
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800543 } else {
544 http_proxy_.reset();
Paul Stewartdef189e2012-08-02 20:12:09 -0700545 static_ip_parameters_.ClearSavedParameters();
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800546 }
547 connection_ = connection;
Paul Stewart1e3bc4962012-09-14 12:20:22 -0700548 Error error;
549 string ipconfig = GetIPConfigRpcIdentifier(&error);
550 if (error.IsSuccess()) {
551 adaptor_->EmitRpcIdentifierChanged(shill::kIPConfigProperty, ipconfig);
552 }
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800553}
554
Gaurav Shah10109f22011-11-11 20:16:22 -0800555bool Service::Is8021xConnectable() const {
556 // We mirror all the flimflam checks (see service.c:is_connectable()).
557
558 // Identity is required.
559 if (eap_.identity.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700560 SLOG(Service, 2) << "Not connectable: Identity is empty.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800561 return false;
562 }
563
564 if (!eap_.client_cert.empty() || !eap_.cert_id.empty()) {
565 // If a client certificate is being used, we must have a private key.
566 if (eap_.private_key.empty() && eap_.key_id.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700567 SLOG(Service, 2)
568 << "Not connectable. Client certificate but no private key.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800569 return false;
570 }
571 }
572 if (!eap_.cert_id.empty() || !eap_.key_id.empty() ||
573 !eap_.ca_cert_id.empty()) {
574 // If PKCS#11 data is needed, a PIN is required.
575 if (eap_.pin.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700576 SLOG(Service, 2) << "Not connectable. PKCS#11 data but no PIN.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800577 return false;
578 }
579 }
580
581 // For EAP-TLS, a client certificate is required.
582 if (eap_.eap.empty() || eap_.eap == "TLS") {
Paul Stewart81426132012-05-16 10:05:10 -0700583 if ((!eap_.client_cert.empty() || !eap_.cert_id.empty()) &&
584 (!eap_.private_key.empty() || !eap_.key_id.empty())) {
585 SLOG(Service, 2) << "Connectable. EAP-TLS with a client cert and key.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800586 return true;
587 }
588 }
589
590 // For EAP types other than TLS (e.g. EAP-TTLS or EAP-PEAP, password is the
591 // minimum requirement), at least an identity + password is required.
592 if (eap_.eap.empty() || eap_.eap != "TLS") {
593 if (!eap_.password.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700594 SLOG(Service, 2) << "Connectable. !EAP-TLS and has a password.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800595 return true;
596 }
597 }
598
Ben Chanfad4a0b2012-04-18 15:49:59 -0700599 SLOG(Service, 2)
600 << "Not connectable. No suitable EAP configuration was found.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800601 return false;
602}
603
Paul Stewartbc6e7392012-05-24 07:07:48 -0700604bool Service::AddEAPCertification(const string &name, size_t depth) {
605 if (depth >= kEAPMaxCertificationElements) {
606 LOG(WARNING) << "Ignoring certification " << name
607 << " because depth " << depth
608 << " exceeds our maximum of "
609 << kEAPMaxCertificationElements;
610 return false;
611 }
612
613 if (depth >= eap_.remote_certification.size()) {
614 eap_.remote_certification.resize(depth + 1);
615 } else if (name == eap_.remote_certification[depth]) {
616 return true;
617 }
618
619 eap_.remote_certification[depth] = name;
620 LOG(INFO) << "Received certification for "
621 << name
622 << " at depth "
623 << depth;
624 return true;
625}
626
627void Service::ClearEAPCertification() {
628 eap_.remote_certification.clear();
629}
630
Gaurav Shah10109f22011-11-11 20:16:22 -0800631void Service::set_eap(const EapCredentials &eap) {
632 eap_ = eap;
633 // Note: Connectability can only be updated by a subclass of Service
634 // with knowledge of whether the service actually uses 802.1x credentials.
635}
636
mukesh agrawal00917ce2011-11-22 23:56:55 +0000637// static
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800638const char *Service::ConnectFailureToString(const ConnectFailure &state) {
639 switch (state) {
640 case kFailureUnknown:
641 return "Unknown";
Paul Stewartf2d60912012-07-15 08:37:30 -0700642 case kFailureAAA:
643 return flimflam::kErrorAaaFailed;
644 case kFailureActivation:
645 return flimflam::kErrorActivationFailed;
646 case kFailureBadPassphrase:
647 return flimflam::kErrorBadPassphrase;
648 case kFailureBadWEPKey:
649 return flimflam::kErrorBadWEPKey;
650 case kFailureConnect:
651 return flimflam::kErrorConnectFailed;
652 case kFailureDNSLookup:
653 return flimflam::kErrorDNSLookupFailed;
654 case kFailureDHCP:
655 return flimflam::kErrorDhcpFailed;
656 case kFailureHTTPGet:
657 return flimflam::kErrorHTTPGetFailed;
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800658 case kFailureNeedEVDO:
Paul Stewartf2d60912012-07-15 08:37:30 -0700659 return flimflam::kErrorNeedEvdo;
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800660 case kFailureNeedHomeNetwork:
Paul Stewartf2d60912012-07-15 08:37:30 -0700661 return flimflam::kErrorNeedHomeNetwork;
662 case kFailureOTASP:
663 return flimflam::kErrorOtaspFailed;
664 case kFailureOutOfRange:
665 return flimflam::kErrorOutOfRange;
666 case kFailurePinMissing:
667 return flimflam::kErrorPinMissing;
668 case kFailurePPPAuth:
669 return flimflam::kErrorPppAuthFailed;
Thieu Le48e6d6d2011-12-06 00:40:27 +0000670 case kFailureMax:
671 return "Max failure error code";
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800672 }
673 return "Invalid";
674}
675
676// static
677const char *Service::ConnectStateToString(const ConnectState &state) {
678 switch (state) {
679 case kStateUnknown:
680 return "Unknown";
681 case kStateIdle:
682 return "Idle";
683 case kStateAssociating:
684 return "Associating";
685 case kStateConfiguring:
686 return "Configuring";
687 case kStateConnected:
688 return "Connected";
689 case kStateDisconnected:
690 return "Disconnected";
Thieu Le48e6d6d2011-12-06 00:40:27 +0000691 case kStatePortal:
692 return "Portal";
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800693 case kStateFailure:
694 return "Failure";
695 case kStateOnline:
696 return "Online";
697 }
698 return "Invalid";
699}
700
Darin Petkov58f0b6d2012-06-12 12:52:30 +0200701string Service::GetTechnologyString() const {
Gaurav Shah435de2c2011-11-17 19:01:07 -0800702 return Technology::NameFromIdentifier(technology());
703}
704
Darin Petkov58f0b6d2012-06-12 12:52:30 +0200705string Service::CalculateTechnology(Error */*error*/) {
706 return GetTechnologyString();
707}
708
Jason Glasgowb5790052012-01-27 01:03:52 -0500709// static
Paul Stewart22aa71b2011-09-16 12:15:11 -0700710bool Service::DecideBetween(int a, int b, bool *decision) {
711 if (a == b)
712 return false;
713 *decision = (a > b);
714 return true;
715}
716
mukesh agrawal00917ce2011-11-22 23:56:55 +0000717// static
Paul Stewart22aa71b2011-09-16 12:15:11 -0700718bool Service::Compare(ServiceRefPtr a,
719 ServiceRefPtr b,
mukesh agrawalddc378f2012-02-17 18:26:20 -0800720 const vector<Technology::Identifier> &tech_order,
721 const char **reason) {
Paul Stewart22aa71b2011-09-16 12:15:11 -0700722 bool ret;
723
724 if (a->state() != b->state()) {
725 if (DecideBetween(a->IsConnected(), b->IsConnected(), &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800726 *reason = kServiceSortIsConnected;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700727 return ret;
728 }
729
Paul Stewarta121c442012-06-09 14:12:58 -0700730 if (DecideBetween(!a->IsPortalled(), !b->IsPortalled(), &ret)) {
731 *reason = kServiceSortIsPortalled;
732 return ret;
733 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700734
735 if (DecideBetween(a->IsConnecting(), b->IsConnecting(), &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800736 *reason = kServiceSortIsConnecting;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700737 return ret;
738 }
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000739
740 if (DecideBetween(!a->IsFailed(), !b->IsFailed(), &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800741 *reason = kServiceSortIsFailed;
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000742 return ret;
743 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700744 }
745
Darin Petkov2f903b32012-04-18 12:56:43 +0200746 if (DecideBetween(a->connectable(), b->connectable(), &ret)) {
747 *reason = kServiceSortConnectable;
748 return ret;
749 }
750
Paul Stewartdf3c0a82012-11-09 15:54:33 -0800751 if (DecideBetween(a->IsDependentOn(b), b->IsDependentOn(a), &ret)) {
752 *reason = kServiceSortDependency;
753 return ret;
754 }
755
Darin Petkov2f903b32012-04-18 12:56:43 +0200756 // Ignore the auto-connect property if both services are connected
757 // already. This allows connected non-autoconnectable VPN services to be
758 // sorted higher than other connected services based on technology order.
759 if (!a->IsConnected() &&
760 DecideBetween(a->auto_connect(), b->auto_connect(), &ret)) {
761 *reason = kServiceSortAutoConnect;
762 return ret;
763 }
764
765 if (DecideBetween(a->favorite(), b->favorite(), &ret)) {
766 *reason = kServiceSortFavorite;
767 return ret;
768 }
769
770 if (DecideBetween(a->priority(), b->priority(), &ret)) {
771 *reason = kServiceSortPriority;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700772 return ret;
773 }
774
775 // TODO(pstew): Below this point we are making value judgements on
776 // services that are not related to anything intrinsic or
777 // user-specified. These heuristics should be richer (contain
778 // historical information, for example) and be subject to user
779 // customization.
Paul Stewart22aa71b2011-09-16 12:15:11 -0700780 for (vector<Technology::Identifier>::const_iterator it = tech_order.begin();
781 it != tech_order.end();
782 ++it) {
Joshua Kroll053fa822012-06-05 09:50:43 -0700783 if (DecideBetween(a->technology() == *it, b->technology() == *it, &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800784 *reason = kServiceSortTechnology;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700785 return ret;
mukesh agrawalddc378f2012-02-17 18:26:20 -0800786 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700787 }
788
Paul Stewart1ca3e852011-11-04 07:50:49 -0700789 if (DecideBetween(a->security_level(), b->security_level(), &ret) ||
Paul Stewart22aa71b2011-09-16 12:15:11 -0700790 DecideBetween(a->strength(), b->strength(), &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800791 *reason = kServiceSortSecurityEtc;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700792 return ret;
793 }
794
mukesh agrawalddc378f2012-02-17 18:26:20 -0800795 *reason = kServiceSortUniqueName;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700796 return a->UniqueName() < b->UniqueName();
797}
798
Chris Masone34af2182011-08-22 11:59:36 -0700799const ProfileRefPtr &Service::profile() const { return profile_; }
800
801void Service::set_profile(const ProfileRefPtr &p) { profile_ = p; }
802
Paul Stewartff14b022012-04-24 20:06:23 -0700803void Service::OnPropertyChanged(const string &property) {
Paul Stewart81426132012-05-16 10:05:10 -0700804 if (Is8021x() &&
805 (property == flimflam::kEAPCertIDProperty ||
806 property == flimflam::kEAPClientCertProperty ||
807 property == flimflam::kEAPKeyIDProperty ||
808 property == flimflam::kEAPPINProperty ||
809 property == flimflam::kEapCaCertIDProperty ||
810 property == flimflam::kEapIdentityProperty ||
Paul Stewartadf79d82012-07-18 16:09:56 -0700811 property == flimflam::kEapKeyMgmtProperty ||
Paul Stewart81426132012-05-16 10:05:10 -0700812 property == flimflam::kEapPasswordProperty ||
813 property == flimflam::kEapPrivateKeyProperty)) {
814 // This notifies subclassess that EAP parameters have been changed.
815 set_eap(eap_);
816 }
mukesh agrawalcf24a242012-05-21 16:46:11 -0700817 SaveToProfile();
Paul Stewartd215af62012-04-24 23:25:50 -0700818 if ((property == flimflam::kCheckPortalProperty ||
819 property == flimflam::kProxyConfigProperty) &&
820 (state_ == kStateConnected ||
821 state_ == kStatePortal ||
822 state_ == kStateOnline)) {
823 manager_->RecheckPortalOnService(this);
824 }
Paul Stewartff14b022012-04-24 20:06:23 -0700825}
826
Christopher Wiley0801d192012-09-24 11:57:15 -0700827void Service::OnAfterResume() {
828 // Forget old autoconnect failures across suspend/resume.
829 auto_connect_cooldown_milliseconds_ = 0;
830 reenable_auto_connect_task_.Cancel();
Christopher Wileya4c61ae2012-10-01 11:04:30 -0700831 // Forget if the user disconnected us, we might be able to connect now.
832 explicitly_disconnected_ = false;
Christopher Wiley0801d192012-09-24 11:57:15 -0700833}
834
Paul Stewart10241e32012-04-23 18:15:06 -0700835string Service::GetIPConfigRpcIdentifier(Error *error) {
836 if (!connection_) {
837 error->Populate(Error::kNotFound);
838 return "/";
839 }
840
841 string id = connection_->ipconfig_rpc_identifier();
842
843 if (id.empty()) {
844 // Do not return an empty IPConfig.
845 error->Populate(Error::kNotFound);
846 return "/";
847 }
848
849 return id;
850}
851
mukesh agrawal29c13a12011-11-24 00:09:19 +0000852void Service::set_connectable(bool connectable) {
853 connectable_ = connectable;
854 adaptor_->EmitBoolChanged(flimflam::kConnectableProperty, connectable_);
855}
856
Darin Petkovb2ba39f2012-06-06 10:33:43 +0200857void Service::SetConnectable(bool connectable) {
858 if (connectable_ == connectable) {
859 return;
860 }
861 connectable_ = connectable;
862 adaptor_->EmitBoolChanged(flimflam::kConnectableProperty, connectable_);
863 if (manager_->HasService(this)) {
864 manager_->UpdateService(this);
865 }
866}
867
Darin Petkov58f0b6d2012-06-12 12:52:30 +0200868string Service::GetStateString() const {
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700869 switch (state_) {
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800870 case kStateIdle:
871 return flimflam::kStateIdle;
872 case kStateAssociating:
873 return flimflam::kStateAssociation;
874 case kStateConfiguring:
875 return flimflam::kStateConfiguration;
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700876 case kStateConnected:
Paul Stewart20088d82012-02-16 06:58:55 -0800877 return flimflam::kStateReady;
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800878 case kStateDisconnected:
879 return flimflam::kStateDisconnect;
880 case kStateFailure:
881 return flimflam::kStateFailure;
Paul Stewart20088d82012-02-16 06:58:55 -0800882 case kStatePortal:
883 return flimflam::kStatePortal;
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800884 case kStateOnline:
885 return flimflam::kStateOnline;
886 case kStateUnknown:
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700887 default:
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800888 return "";
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700889 }
890}
891
Darin Petkov58f0b6d2012-06-12 12:52:30 +0200892string Service::CalculateState(Error */*error*/) {
893 return GetStateString();
894}
895
mukesh agrawalbf14e942012-03-02 14:36:34 -0800896bool Service::IsAutoConnectable(const char **reason) const {
897 if (!connectable()) {
898 *reason = kAutoConnNotConnectable;
899 return false;
900 }
901
902 if (IsConnected()) {
903 *reason = kAutoConnConnected;
904 return false;
905 }
906
907 if (IsConnecting()) {
908 *reason = kAutoConnConnecting;
909 return false;
910 }
911
912 if (explicitly_disconnected_) {
913 *reason = kAutoConnExplicitDisconnect;
914 return false;
915 }
916
Christopher Wiley0801d192012-09-24 11:57:15 -0700917 if (!reenable_auto_connect_task_.IsCancelled()) {
918 *reason = kAutoConnThrottled;
919 return false;
920 }
921
mukesh agrawalbf14e942012-03-02 14:36:34 -0800922 return true;
mukesh agrawal76d13882012-01-12 15:23:11 -0800923}
924
Paul Stewartd215af62012-04-24 23:25:50 -0700925bool Service::IsPortalDetectionDisabled() const {
926 return check_portal_ == kCheckPortalFalse;
927}
928
929bool Service::IsPortalDetectionAuto() const {
930 return check_portal_ == kCheckPortalAuto;
931}
932
mukesh agrawalffa3d042011-10-06 15:26:10 -0700933void Service::HelpRegisterDerivedBool(
934 const string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800935 bool(Service::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700936 void(Service::*set)(const bool&, Error *)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700937 store_.RegisterDerivedBool(
938 name,
939 BoolAccessor(new CustomAccessor<Service, bool>(this, get, set)));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700940}
941
mukesh agrawalffa3d042011-10-06 15:26:10 -0700942void Service::HelpRegisterDerivedString(
943 const string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800944 string(Service::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700945 void(Service::*set)(const string&, Error *)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700946 store_.RegisterDerivedString(
947 name,
948 StringAccessor(new CustomAccessor<Service, string>(this, get, set)));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700949}
950
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400951void Service::HelpRegisterDerivedRpcIdentifier(
952 const string &name,
953 RpcIdentifier(Service::*get)(Error *),
954 void(Service::*set)(const RpcIdentifier&, Error *)) {
955 store_.RegisterDerivedRpcIdentifier(
956 name,
957 RpcIdentifierAccessor(new CustomAccessor<Service, RpcIdentifier>(
958 this, get, set)));
959}
960
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800961void Service::HelpRegisterDerivedUint16(
962 const string &name,
963 uint16(Service::*get)(Error *),
964 void(Service::*set)(const uint16&, Error *)) {
965 store_.RegisterDerivedUint16(
966 name,
967 Uint16Accessor(new CustomAccessor<Service, uint16>(this, get, set)));
968}
969
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800970void Service::HelpRegisterWriteOnlyDerivedString(
971 const string &name,
972 void(Service::*set)(const string &, Error *),
973 void(Service::*clear)(Error *),
974 const string *default_value) {
975 store_.RegisterDerivedString(
976 name,
977 StringAccessor(
978 new CustomWriteOnlyAccessor<Service, string>(
979 this, set, clear, default_value)));
980}
981
Darin Petkovba40dd32011-07-11 20:06:39 -0700982void Service::SaveString(StoreInterface *storage,
Chris Masone34af2182011-08-22 11:59:36 -0700983 const string &id,
Darin Petkovba40dd32011-07-11 20:06:39 -0700984 const string &key,
985 const string &value,
986 bool crypted,
987 bool save) {
988 if (value.empty() || !save) {
Chris Masone34af2182011-08-22 11:59:36 -0700989 storage->DeleteKey(id, key);
Darin Petkovba40dd32011-07-11 20:06:39 -0700990 return;
991 }
992 if (crypted) {
Chris Masone34af2182011-08-22 11:59:36 -0700993 storage->SetCryptedString(id, key, value);
Darin Petkovba40dd32011-07-11 20:06:39 -0700994 return;
995 }
Chris Masone34af2182011-08-22 11:59:36 -0700996 storage->SetString(id, key, value);
Darin Petkovba40dd32011-07-11 20:06:39 -0700997}
998
Chris Masone34af2182011-08-22 11:59:36 -0700999void Service::LoadEapCredentials(StoreInterface *storage, const string &id) {
Gaurav Shah10109f22011-11-11 20:16:22 -08001000 EapCredentials eap;
1001 storage->GetCryptedString(id, kStorageEapIdentity, &eap.identity);
1002 storage->GetString(id, kStorageEapEap, &eap.eap);
1003 storage->GetString(id, kStorageEapInnerEap, &eap.inner_eap);
Darin Petkovba40dd32011-07-11 20:06:39 -07001004 storage->GetCryptedString(id,
1005 kStorageEapAnonymousIdentity,
Gaurav Shah10109f22011-11-11 20:16:22 -08001006 &eap.anonymous_identity);
1007 storage->GetString(id, kStorageEapClientCert, &eap.client_cert);
1008 storage->GetString(id, kStorageEapCertID, &eap.cert_id);
1009 storage->GetString(id, kStorageEapPrivateKey, &eap.private_key);
Darin Petkovba40dd32011-07-11 20:06:39 -07001010 storage->GetCryptedString(id,
1011 kStorageEapPrivateKeyPassword,
Gaurav Shah10109f22011-11-11 20:16:22 -08001012 &eap.private_key_password);
1013 storage->GetString(id, kStorageEapKeyID, &eap.key_id);
1014 storage->GetString(id, kStorageEapCACert, &eap.ca_cert);
1015 storage->GetString(id, kStorageEapCACertID, &eap.ca_cert_id);
Paul Stewartecf4cd12012-04-17 11:08:39 -07001016 storage->GetString(id, kStorageEapCACertNSS, &eap.ca_cert_nss);
Gaurav Shah10109f22011-11-11 20:16:22 -08001017 storage->GetBool(id, kStorageEapUseSystemCAs, &eap.use_system_cas);
1018 storage->GetString(id, kStorageEapPIN, &eap.pin);
1019 storage->GetCryptedString(id, kStorageEapPassword, &eap.password);
1020 storage->GetString(id, kStorageEapKeyManagement, &eap.key_management);
1021 set_eap(eap);
Darin Petkovba40dd32011-07-11 20:06:39 -07001022}
1023
Chris Masone34af2182011-08-22 11:59:36 -07001024void Service::SaveEapCredentials(StoreInterface *storage, const string &id) {
Darin Petkovba40dd32011-07-11 20:06:39 -07001025 bool save = save_credentials_;
Chris Masone34af2182011-08-22 11:59:36 -07001026 SaveString(storage, id, kStorageEapIdentity, eap_.identity, true, save);
1027 SaveString(storage, id, kStorageEapEap, eap_.eap, false, true);
1028 SaveString(storage, id, kStorageEapInnerEap, eap_.inner_eap, false, true);
Darin Petkovba40dd32011-07-11 20:06:39 -07001029 SaveString(storage,
Chris Masone34af2182011-08-22 11:59:36 -07001030 id,
Darin Petkovba40dd32011-07-11 20:06:39 -07001031 kStorageEapAnonymousIdentity,
1032 eap_.anonymous_identity,
1033 true,
1034 save);
Chris Masone34af2182011-08-22 11:59:36 -07001035 SaveString(storage, id, kStorageEapClientCert, eap_.client_cert, false, save);
1036 SaveString(storage, id, kStorageEapCertID, eap_.cert_id, false, save);
1037 SaveString(storage, id, kStorageEapPrivateKey, eap_.private_key, false, save);
Darin Petkovba40dd32011-07-11 20:06:39 -07001038 SaveString(storage,
Chris Masone34af2182011-08-22 11:59:36 -07001039 id,
Darin Petkovba40dd32011-07-11 20:06:39 -07001040 kStorageEapPrivateKeyPassword,
1041 eap_.private_key_password,
1042 true,
1043 save);
Chris Masone34af2182011-08-22 11:59:36 -07001044 SaveString(storage, id, kStorageEapKeyID, eap_.key_id, false, save);
1045 SaveString(storage, id, kStorageEapCACert, eap_.ca_cert, false, true);
1046 SaveString(storage, id, kStorageEapCACertID, eap_.ca_cert_id, false, true);
Paul Stewartecf4cd12012-04-17 11:08:39 -07001047 SaveString(storage, id, kStorageEapCACertNSS, eap_.ca_cert_nss, false, true);
Chris Masone34af2182011-08-22 11:59:36 -07001048 storage->SetBool(id, kStorageEapUseSystemCAs, eap_.use_system_cas);
1049 SaveString(storage, id, kStorageEapPIN, eap_.pin, false, save);
1050 SaveString(storage, id, kStorageEapPassword, eap_.password, true, save);
Darin Petkovba40dd32011-07-11 20:06:39 -07001051 SaveString(storage,
Chris Masone34af2182011-08-22 11:59:36 -07001052 id,
Darin Petkovba40dd32011-07-11 20:06:39 -07001053 kStorageEapKeyManagement,
1054 eap_.key_management,
1055 false,
1056 true);
1057}
1058
Paul Stewartd8ad3c42012-01-09 12:39:38 -08001059void Service::UnloadEapCredentials() {
1060 eap_.identity = "";
1061 eap_.eap = "";
1062 eap_.inner_eap = "";
1063 eap_.anonymous_identity = "";
1064 eap_.client_cert = "";
1065 eap_.cert_id = "";
1066 eap_.private_key = "";
1067 eap_.private_key_password = "";
1068 eap_.key_id = "";
1069 eap_.ca_cert = "";
1070 eap_.ca_cert_id = "";
Paul Stewart20550982012-04-16 12:16:11 -07001071 eap_.use_system_cas = true;
Paul Stewartd8ad3c42012-01-09 12:39:38 -08001072 eap_.pin = "";
1073 eap_.password = "";
Paul Stewartd8ad3c42012-01-09 12:39:38 -08001074}
1075
Paul Stewartcb59fed2012-03-21 21:14:46 -07001076void Service::IgnoreParameterForConfigure(const string &parameter) {
1077 parameters_ignored_for_configure_.insert(parameter);
1078}
1079
Paul Stewartac4ac002011-08-26 12:04:26 -07001080const string &Service::GetEAPKeyManagement() const {
1081 return eap_.key_management;
1082}
1083
1084void Service::SetEAPKeyManagement(const string &key_management) {
1085 eap_.key_management = key_management;
1086}
1087
Thieu Le284fe792012-01-31 17:53:19 -08001088bool Service::GetAutoConnect(Error */*error*/) {
1089 return auto_connect();
1090}
1091
1092void Service::SetAutoConnect(const bool &connect, Error *error) {
Wade Guthrie4f28e8b2012-04-11 10:52:07 -07001093 set_auto_connect(connect);
Thieu Le284fe792012-01-31 17:53:19 -08001094}
1095
Paul Stewartd215af62012-04-24 23:25:50 -07001096string Service::GetCheckPortal(Error *error) {
1097 return check_portal_;
1098}
1099
1100void Service::SetCheckPortal(const string &check_portal, Error *error) {
1101 if (check_portal == check_portal_) {
1102 return;
1103 }
1104 if (check_portal != kCheckPortalFalse &&
1105 check_portal != kCheckPortalTrue &&
1106 check_portal != kCheckPortalAuto) {
1107 Error::PopulateAndLog(error, Error::kInvalidArguments,
1108 base::StringPrintf(
1109 "Invalid Service CheckPortal property value: %s",
1110 check_portal.c_str()));
1111 return;
1112 }
1113 check_portal_ = check_portal;
1114}
1115
Paul Stewart9f32d192012-01-30 20:37:50 -08001116void Service::SetEAPPassword(const string &password, Error */*error*/) {
1117 eap_.password = password;
1118}
1119
1120void Service::SetEAPPrivateKeyPassword(const string &password,
1121 Error */*error*/) {
1122 eap_.private_key_password = password;
1123}
1124
Paul Stewart0c438332012-04-11 07:55:27 -07001125string Service::GetNameProperty(Error *error) {
1126 return friendly_name_;
1127}
1128
1129void Service::AssertTrivialSetNameProperty(const string &name, Error *error) {
1130 if (name != friendly_name_) {
1131 Error::PopulateAndLog(error, Error::kInvalidArguments,
Paul Stewart2bf424f2012-04-11 18:59:39 -07001132 base::StringPrintf(
1133 "Service Name property cannot be modified "
1134 "(%s to %s)", friendly_name_.c_str(),
1135 name.c_str()));
Paul Stewart0c438332012-04-11 07:55:27 -07001136 }
1137}
1138
Paul Stewart1b1a7f22012-01-06 16:24:06 -08001139string Service::GetProfileRpcId(Error *error) {
1140 if (!profile_) {
1141 // This happens in some unit tests where profile_ is not set.
1142 error->Populate(Error::kNotFound);
1143 return "";
1144 }
1145 return profile_->GetRpcIdentifier();
1146}
1147
1148void Service::SetProfileRpcId(const string &profile, Error *error) {
1149 manager_->SetProfileForService(this, profile, error);
1150}
1151
Paul Stewartbe5f5b32011-12-07 17:11:11 -08001152uint16 Service::GetHTTPProxyPort(Error */*error*/) {
1153 if (http_proxy_.get()) {
1154 return static_cast<uint16>(http_proxy_->proxy_port());
1155 }
1156 return 0;
1157}
1158
mukesh agrawalcf24a242012-05-21 16:46:11 -07001159void Service::SaveToProfile() {
1160 if (profile_.get() && profile_->GetConstStorage()) {
1161 profile_->UpdateService(this);
1162 }
1163}
1164
Darin Petkovd78ee7e2012-01-12 11:21:10 +01001165void Service::SetStrength(uint8 strength) {
1166 if (strength == strength_) {
1167 return;
1168 }
1169 strength_ = strength;
1170 adaptor_->EmitUint8Changed(flimflam::kSignalStrengthProperty, strength);
1171}
1172
Paul Stewartf2d60912012-07-15 08:37:30 -07001173void Service::UpdateErrorProperty() {
1174 const string error(ConnectFailureToString(failure_));
1175 if (error == error_) {
1176 return;
1177 }
1178 error_ = error;
1179 adaptor_->EmitStringChanged(flimflam::kErrorProperty, error);
1180}
1181
Paul Stewart75897df2011-04-27 09:05:53 -07001182} // namespace shill