blob: c327a33fd5c264a926780d6bf64d9e8c806167d0 [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";
56const char Service::kServiceSortFavorite[] = "Favorite";
mukesh agrawalddc378f2012-02-17 18:26:20 -080057const char Service::kServiceSortIsConnected[] = "IsConnected";
58const char Service::kServiceSortIsConnecting[] = "IsConnecting";
59const char Service::kServiceSortIsFailed[] = "IsFailed";
Paul Stewarta121c442012-06-09 14:12:58 -070060const char Service::kServiceSortIsPortalled[] = "IsPortal";
Darin Petkov2f903b32012-04-18 12:56:43 +020061const char Service::kServiceSortPriority[] = "Priority";
mukesh agrawalddc378f2012-02-17 18:26:20 -080062const char Service::kServiceSortSecurityEtc[] = "SecurityEtc";
Darin Petkov2f903b32012-04-18 12:56:43 +020063const char Service::kServiceSortTechnology[] = "Technology";
mukesh agrawalddc378f2012-02-17 18:26:20 -080064const char Service::kServiceSortUniqueName[] = "UniqueName";
65
Darin Petkovba40dd32011-07-11 20:06:39 -070066const char Service::kStorageAutoConnect[] = "AutoConnect";
67const char Service::kStorageCheckPortal[] = "CheckPortal";
68const char Service::kStorageEapAnonymousIdentity[] = "EAP.AnonymousIdentity";
69const char Service::kStorageEapCACert[] = "EAP.CACert";
70const char Service::kStorageEapCACertID[] = "EAP.CACertID";
Paul Stewartecf4cd12012-04-17 11:08:39 -070071const char Service::kStorageEapCACertNSS[] = "EAP.CACertNSS";
Darin Petkovba40dd32011-07-11 20:06:39 -070072const char Service::kStorageEapCertID[] = "EAP.CertID";
73const char Service::kStorageEapClientCert[] = "EAP.ClientCert";
74const char Service::kStorageEapEap[] = "EAP.EAP";
75const char Service::kStorageEapIdentity[] = "EAP.Identity";
76const char Service::kStorageEapInnerEap[] = "EAP.InnerEAP";
77const char Service::kStorageEapKeyID[] = "EAP.KeyID";
78const char Service::kStorageEapKeyManagement[] = "EAP.KeyMgmt";
79const char Service::kStorageEapPIN[] = "EAP.PIN";
80const char Service::kStorageEapPassword[] = "EAP.Password";
81const char Service::kStorageEapPrivateKey[] = "EAP.PrivateKey";
82const char Service::kStorageEapPrivateKeyPassword[] = "EAP.PrivateKeyPassword";
83const char Service::kStorageEapUseSystemCAs[] = "EAP.UseSystemCAs";
Paul Stewart0756db92012-01-27 08:34:47 -080084const char Service::kStorageError[] = "Error";
Darin Petkovba40dd32011-07-11 20:06:39 -070085const char Service::kStorageFavorite[] = "Favorite";
Paul Stewart0756db92012-01-27 08:34:47 -080086const char Service::kStorageGUID[] = "GUID";
mukesh agrawalcf24a242012-05-21 16:46:11 -070087const char Service::kStorageHasEverConnected[] = "HasEverConnected";
Darin Petkovba40dd32011-07-11 20:06:39 -070088const char Service::kStorageName[] = "Name";
89const char Service::kStoragePriority[] = "Priority";
90const char Service::kStorageProxyConfig[] = "ProxyConfig";
91const char Service::kStorageSaveCredentials[] = "SaveCredentials";
Paul Stewart2706aaf2011-12-14 16:44:04 -080092const char Service::kStorageType[] = "Type";
Paul Stewart987e71e2011-12-05 09:45:06 -080093const char Service::kStorageUIData[] = "UIData";
Darin Petkovba40dd32011-07-11 20:06:39 -070094
mukesh agrawal8f3f7752012-02-17 19:42:09 -080095const uint8 Service::kStrengthMax = 100;
96const uint8 Service::kStrengthMin = 0;
97
Christopher Wiley0801d192012-09-24 11:57:15 -070098const uint64 Service::kMaxAutoConnectCooldownTimeMilliseconds = 30 * 60 * 1000;
99const uint64 Service::kMinAutoConnectCooldownTimeMilliseconds = 1000;
100const uint64 Service::kAutoConnectCooldownBackoffFactor = 2;
101
mukesh agrawal51a7e932011-07-27 16:18:26 -0700102// static
103unsigned int Service::serial_number_ = 0;
104
Paul Stewart75897df2011-04-27 09:05:53 -0700105Service::Service(ControlInterface *control_interface,
mukesh agrawalb54601c2011-06-07 17:39:22 -0700106 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -0800107 Metrics *metrics,
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700108 Manager *manager,
Gaurav Shah435de2c2011-11-17 19:01:07 -0800109 Technology::Identifier technology)
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400110 : state_(kStateIdle),
Paul Stewart03dba0b2011-08-22 16:32:45 -0700111 failure_(kFailureUnknown),
112 auto_connect_(false),
Darin Petkovba40dd32011-07-11 20:06:39 -0700113 check_portal_(kCheckPortalAuto),
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700114 connectable_(false),
Paul Stewartf2d60912012-07-15 08:37:30 -0700115 error_(ConnectFailureToString(failure_)),
mukesh agrawaladb68482012-01-17 16:31:51 -0800116 explicitly_disconnected_(false),
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700117 favorite_(false),
Darin Petkovba40dd32011-07-11 20:06:39 -0700118 priority_(kPriorityNone),
Paul Stewart1ca3e852011-11-04 07:50:49 -0700119 security_level_(0),
Paul Stewart22aa71b2011-09-16 12:15:11 -0700120 strength_(0),
Darin Petkovba40dd32011-07-11 20:06:39 -0700121 save_credentials_(true),
Gaurav Shah435de2c2011-11-17 19:01:07 -0800122 technology_(technology),
mukesh agrawal568b5c62012-02-28 14:44:47 -0800123 failed_time_(0),
mukesh agrawalcf24a242012-05-21 16:46:11 -0700124 has_ever_connected_(false),
Christopher Wiley0801d192012-09-24 11:57:15 -0700125 auto_connect_cooldown_milliseconds_(0),
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700126 dispatcher_(dispatcher),
mukesh agrawald835b202011-10-07 15:26:47 -0700127 unique_name_(base::UintToString(serial_number_++)),
128 friendly_name_(unique_name_),
Chris Masone6791a432011-07-12 13:23:19 -0700129 adaptor_(control_interface->CreateServiceAdaptor(this)),
Thieu Le3426c8f2012-01-11 17:35:11 -0800130 metrics_(metrics),
Darin Petkov5eb05422012-05-11 15:45:25 +0200131 manager_(manager),
Christopher Wiley0801d192012-09-24 11:57:15 -0700132 sockets_(new Sockets()),
133 weak_ptr_factory_(this) {
Thieu Le284fe792012-01-31 17:53:19 -0800134 HelpRegisterDerivedBool(flimflam::kAutoConnectProperty,
135 &Service::GetAutoConnect,
136 &Service::SetAutoConnect);
Chris Masone4d42df82011-07-02 17:09:39 -0700137
138 // flimflam::kActivationStateProperty: Registered in CellularService
139 // flimflam::kCellularApnProperty: Registered in CellularService
140 // flimflam::kCellularLastGoodApnProperty: Registered in CellularService
141 // flimflam::kNetworkTechnologyProperty: Registered in CellularService
Darin Petkov3335b372011-08-22 11:05:32 -0700142 // flimflam::kOperatorNameProperty: DEPRECATED
143 // flimflam::kOperatorCodeProperty: DEPRECATED
Chris Masone4d42df82011-07-02 17:09:39 -0700144 // flimflam::kRoamingStateProperty: Registered in CellularService
Darin Petkov3335b372011-08-22 11:05:32 -0700145 // flimflam::kServingOperatorProperty: Registered in CellularService
Chris Masone4d42df82011-07-02 17:09:39 -0700146 // flimflam::kPaymentURLProperty: Registered in CellularService
147
Paul Stewartd215af62012-04-24 23:25:50 -0700148 HelpRegisterDerivedString(flimflam::kCheckPortalProperty,
149 &Service::GetCheckPortal,
150 &Service::SetCheckPortal);
Chris Masone27c4aa52011-07-02 13:10:14 -0700151 store_.RegisterConstBool(flimflam::kConnectableProperty, &connectable_);
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400152 HelpRegisterDerivedRpcIdentifier(flimflam::kDeviceProperty,
153 &Service::GetDeviceRpcId,
154 NULL);
Paul Stewart4c561612012-03-21 12:49:01 -0700155 store_.RegisterString(flimflam::kGuidProperty, &guid_);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700156
Chris Masoneb2e326b2011-07-12 13:28:51 -0700157 store_.RegisterString(flimflam::kEapIdentityProperty, &eap_.identity);
158 store_.RegisterString(flimflam::kEAPEAPProperty, &eap_.eap);
159 store_.RegisterString(flimflam::kEapPhase2AuthProperty, &eap_.inner_eap);
Chris Masone27c4aa52011-07-02 13:10:14 -0700160 store_.RegisterString(flimflam::kEapAnonymousIdentityProperty,
Chris Masoneb2e326b2011-07-12 13:28:51 -0700161 &eap_.anonymous_identity);
162 store_.RegisterString(flimflam::kEAPClientCertProperty, &eap_.client_cert);
163 store_.RegisterString(flimflam::kEAPCertIDProperty, &eap_.cert_id);
164 store_.RegisterString(flimflam::kEapPrivateKeyProperty, &eap_.private_key);
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800165 HelpRegisterWriteOnlyDerivedString(flimflam::kEapPrivateKeyPasswordProperty,
166 &Service::SetEAPPrivateKeyPassword,
167 NULL,
168 &eap_.private_key_password);
Chris Masoneb2e326b2011-07-12 13:28:51 -0700169 store_.RegisterString(flimflam::kEAPKeyIDProperty, &eap_.key_id);
170 store_.RegisterString(flimflam::kEapCaCertProperty, &eap_.ca_cert);
171 store_.RegisterString(flimflam::kEapCaCertIDProperty, &eap_.ca_cert_id);
Paul Stewartecf4cd12012-04-17 11:08:39 -0700172 store_.RegisterString(flimflam::kEapCaCertNssProperty, &eap_.ca_cert_nss);
Chris Masoneb2e326b2011-07-12 13:28:51 -0700173 store_.RegisterString(flimflam::kEAPPINProperty, &eap_.pin);
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800174 HelpRegisterWriteOnlyDerivedString(flimflam::kEapPasswordProperty,
175 &Service::SetEAPPassword,
176 NULL,
177 &eap_.password);
Chris Masoneb2e326b2011-07-12 13:28:51 -0700178 store_.RegisterString(flimflam::kEapKeyMgmtProperty, &eap_.key_management);
179 store_.RegisterBool(flimflam::kEapUseSystemCAsProperty, &eap_.use_system_cas);
Paul Stewartbc6e7392012-05-24 07:07:48 -0700180 store_.RegisterConstStrings(shill::kEapRemoteCertificationProperty,
181 &eap_.remote_certification);
182 store_.RegisterString(shill::kEapSubjectMatchProperty,
183 &eap_.subject_match);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700184
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400185 // TODO(ers): in flimflam clearing Error has the side-effect of
186 // setting the service state to IDLE. Is this important? I could
187 // see an autotest depending on it.
Chris Masone27c4aa52011-07-02 13:10:14 -0700188 store_.RegisterConstString(flimflam::kErrorProperty, &error_);
189 store_.RegisterConstBool(flimflam::kFavoriteProperty, &favorite_);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800190 HelpRegisterDerivedUint16(shill::kHTTPProxyPortProperty,
191 &Service::GetHTTPProxyPort,
192 NULL);
Paul Stewart10241e32012-04-23 18:15:06 -0700193 HelpRegisterDerivedRpcIdentifier(shill::kIPConfigProperty,
194 &Service::GetIPConfigRpcIdentifier,
195 NULL);
Chris Masone27c4aa52011-07-02 13:10:14 -0700196 HelpRegisterDerivedBool(flimflam::kIsActiveProperty,
Chris Masoneb2e326b2011-07-12 13:28:51 -0700197 &Service::IsActive,
198 NULL);
Chris Masone4d42df82011-07-02 17:09:39 -0700199 // flimflam::kModeProperty: Registered in WiFiService
Paul Stewart0c438332012-04-11 07:55:27 -0700200
201 // Although this is a read-only property, some callers want to blindly
202 // set this value to its current value.
203 HelpRegisterDerivedString(flimflam::kNameProperty,
204 &Service::GetNameProperty,
205 &Service::AssertTrivialSetNameProperty);
Chris Masone4d42df82011-07-02 17:09:39 -0700206 // flimflam::kPassphraseProperty: Registered in WiFiService
207 // flimflam::kPassphraseRequiredProperty: Registered in WiFiService
Chris Masone27c4aa52011-07-02 13:10:14 -0700208 store_.RegisterInt32(flimflam::kPriorityProperty, &priority_);
209 HelpRegisterDerivedString(flimflam::kProfileProperty,
210 &Service::GetProfileRpcId,
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800211 &Service::SetProfileRpcId);
Chris Masone4d42df82011-07-02 17:09:39 -0700212 store_.RegisterString(flimflam::kProxyConfigProperty, &proxy_config_);
Chris Masoneb2e326b2011-07-12 13:28:51 -0700213 store_.RegisterBool(flimflam::kSaveCredentialsProperty, &save_credentials_);
Gaurav Shah435de2c2011-11-17 19:01:07 -0800214 HelpRegisterDerivedString(flimflam::kTypeProperty,
Darin Petkov58f0b6d2012-06-12 12:52:30 +0200215 &Service::CalculateTechnology,
Gaurav Shah435de2c2011-11-17 19:01:07 -0800216 NULL);
Chris Masone4d42df82011-07-02 17:09:39 -0700217 // flimflam::kSecurityProperty: Registered in WiFiService
Chris Masone27c4aa52011-07-02 13:10:14 -0700218 HelpRegisterDerivedString(flimflam::kStateProperty,
219 &Service::CalculateState,
220 NULL);
Darin Petkovd78ee7e2012-01-12 11:21:10 +0100221 store_.RegisterConstUint8(flimflam::kSignalStrengthProperty, &strength_);
Paul Stewart987e71e2011-12-05 09:45:06 -0800222 store_.RegisterString(flimflam::kUIDataProperty, &ui_data_);
Chris Masone4d42df82011-07-02 17:09:39 -0700223 // flimflam::kWifiAuthMode: Registered in WiFiService
224 // flimflam::kWifiHiddenSsid: Registered in WiFiService
225 // flimflam::kWifiFrequency: Registered in WiFiService
226 // flimflam::kWifiPhyMode: Registered in WiFiService
227 // flimflam::kWifiHexSsid: Registered in WiFiService
Thieu Le48e6d6d2011-12-06 00:40:27 +0000228
229 metrics_->RegisterService(this);
230
Paul Stewart1062d9d2012-04-27 10:42:27 -0700231 static_ip_parameters_.PlumbPropertyStore(&store_);
232
Paul Stewartcb59fed2012-03-21 21:14:46 -0700233 IgnoreParameterForConfigure(flimflam::kTypeProperty);
Paul Stewart7f61e522012-03-22 11:13:45 -0700234 IgnoreParameterForConfigure(flimflam::kProfileProperty);
Paul Stewartcb59fed2012-03-21 21:14:46 -0700235
Ben Chanfad4a0b2012-04-18 15:49:59 -0700236 SLOG(Service, 2) << "Service initialized.";
Paul Stewart75897df2011-04-27 09:05:53 -0700237}
238
Thieu Le48e6d6d2011-12-06 00:40:27 +0000239Service::~Service() {
Darin Petkov9cd7ca12012-07-03 11:06:40 +0200240 LOG(INFO) << "Service destroyed: " << friendly_name_;
Thieu Le48e6d6d2011-12-06 00:40:27 +0000241 metrics_->DeregisterService(this);
242}
Paul Stewart75897df2011-04-27 09:05:53 -0700243
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000244void Service::AutoConnect() {
Darin Petkov3abc3be2012-06-27 10:48:23 +0200245 const char *reason = NULL;
246 if (IsAutoConnectable(&reason)) {
Darin Petkov9cd7ca12012-07-03 11:06:40 +0200247 LOG(INFO) << "Auto-connecting to " << friendly_name_;
Christopher Wiley0801d192012-09-24 11:57:15 -0700248 ThrottleFutureAutoConnects();
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000249 Error error;
250 Connect(&error);
mukesh agrawal76d13882012-01-12 15:23:11 -0800251 } else {
Darin Petkov9cd7ca12012-07-03 11:06:40 +0200252 LOG(INFO) << "Suppressed autoconnect to " << friendly_name_ << " "
mukesh agrawalbf14e942012-03-02 14:36:34 -0800253 << "(" << reason << ")";
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000254 }
255}
256
mukesh agrawaladb68482012-01-17 16:31:51 -0800257void Service::Connect(Error */*error*/) {
258 explicitly_disconnected_ = false;
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400259 // clear any failure state from a previous connect attempt
260 SetState(kStateIdle);
mukesh agrawaladb68482012-01-17 16:31:51 -0800261}
262
263void Service::Disconnect(Error */*error*/) {
Christopher Wiley3e7635e2012-08-15 09:46:17 -0700264 MemoryLog::GetInstance()->FlushToDisk();
mukesh agrawaladb68482012-01-17 16:31:51 -0800265}
266
Christopher Wileyabd3b502012-09-26 13:08:52 -0700267void Service::DisconnectWithFailure(ConnectFailure failure, Error *error) {
268 Disconnect(error);
269 SetFailure(failure);
270}
271
272void Service::UserInitiatedDisconnect(Error *error) {
273 Disconnect(error);
274 explicitly_disconnected_ = true;
275}
276
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500277void Service::ActivateCellularModem(const string &/*carrier*/,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500278 Error *error,
279 const ResultCallback &/*callback*/) {
280 Error::PopulateAndLog(error, Error::kNotSupported,
281 "Service doesn't support cellular modem activation.");
Darin Petkovc408e692011-08-17 13:47:15 -0700282}
283
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800284bool Service::IsActive(Error */*error*/) {
Paul Stewartbfb82552012-10-24 16:48:48 -0700285 return state() != kStateUnknown &&
286 state() != kStateIdle &&
287 state() != kStateFailure;
Paul Stewart03dba0b2011-08-22 16:32:45 -0700288}
289
290void Service::SetState(ConnectState state) {
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800291 LOG(INFO) << "In " << __func__ << "(): Service " << friendly_name_
292 << " state " << ConnectStateToString(state_) << " -> "
293 << ConnectStateToString(state);
mukesh agrawal7ec71312011-11-10 02:08:26 +0000294
Paul Stewart03dba0b2011-08-22 16:32:45 -0700295 if (state == state_) {
296 return;
297 }
298
299 state_ = state;
300 if (state != kStateFailure) {
301 failure_ = kFailureUnknown;
302 }
mukesh agrawalcf24a242012-05-21 16:46:11 -0700303 if (state == kStateConnected) {
Christopher Wiley0801d192012-09-24 11:57:15 -0700304 failed_time_ = 0;
mukesh agrawalcf24a242012-05-21 16:46:11 -0700305 has_ever_connected_ = true;
306 SaveToProfile();
Christopher Wiley0801d192012-09-24 11:57:15 -0700307 // When we succeed in connecting, forget that connects failed in the past.
308 // Give services one chance at a fast autoconnect retry by resetting the
309 // cooldown to 0 to indicate that the last connect was successful.
310 auto_connect_cooldown_milliseconds_ = 0;
311 reenable_auto_connect_task_.Cancel();
mukesh agrawalcf24a242012-05-21 16:46:11 -0700312 }
Paul Stewartf2d60912012-07-15 08:37:30 -0700313 UpdateErrorProperty();
Paul Stewart03dba0b2011-08-22 16:32:45 -0700314 manager_->UpdateService(this);
Thieu Le48e6d6d2011-12-06 00:40:27 +0000315 metrics_->NotifyServiceStateChanged(this, state);
Darin Petkov58f0b6d2012-06-12 12:52:30 +0200316 adaptor_->EmitStringChanged(flimflam::kStateProperty, GetStateString());
Paul Stewart03dba0b2011-08-22 16:32:45 -0700317}
318
Christopher Wiley0801d192012-09-24 11:57:15 -0700319void Service::ReEnableAutoConnectTask() {
320 // Kill the thing blocking AutoConnect().
321 reenable_auto_connect_task_.Cancel();
322 // Post to the manager, giving it an opportunity to AutoConnect again.
323 manager_->UpdateService(this);
324}
325
326void Service::ThrottleFutureAutoConnects() {
327 if (auto_connect_cooldown_milliseconds_ > 0) {
328 LOG(INFO) << "Throttling autoconnect to " << friendly_name_ << " for "
329 << auto_connect_cooldown_milliseconds_ << " milliseconds.";
330 reenable_auto_connect_task_.Reset(Bind(&Service::ReEnableAutoConnectTask,
331 weak_ptr_factory_.GetWeakPtr()));
332 dispatcher_->PostDelayedTask(reenable_auto_connect_task_.callback(),
333 auto_connect_cooldown_milliseconds_);
334 }
335 auto_connect_cooldown_milliseconds_ =
336 std::min(kMaxAutoConnectCooldownTimeMilliseconds,
337 std::max(kMinAutoConnectCooldownTimeMilliseconds,
338 auto_connect_cooldown_milliseconds_ *
339 kAutoConnectCooldownBackoffFactor));
340}
341
Paul Stewart03dba0b2011-08-22 16:32:45 -0700342void Service::SetFailure(ConnectFailure failure) {
343 failure_ = failure;
mukesh agrawal568b5c62012-02-28 14:44:47 -0800344 failed_time_ = time(NULL);
Paul Stewartf2d60912012-07-15 08:37:30 -0700345 UpdateErrorProperty();
Paul Stewart03dba0b2011-08-22 16:32:45 -0700346 SetState(kStateFailure);
347}
348
mukesh agrawal568b5c62012-02-28 14:44:47 -0800349void Service::SetFailureSilent(ConnectFailure failure) {
350 // Note that order matters here, since SetState modifies |failure_| and
351 // |failed_time_|.
352 SetState(kStateIdle);
353 failure_ = failure;
Paul Stewartf2d60912012-07-15 08:37:30 -0700354 UpdateErrorProperty();
mukesh agrawal568b5c62012-02-28 14:44:47 -0800355 failed_time_ = time(NULL);
356}
357
Chris Masone6791a432011-07-12 13:23:19 -0700358string Service::GetRpcIdentifier() const {
Chris Masone3c3f6a12011-07-01 10:01:41 -0700359 return adaptor_->GetRpcIdentifier();
360}
361
Paul Stewartbba6a5b2011-11-02 18:45:59 -0700362bool Service::IsLoadableFrom(StoreInterface *storage) const {
363 return storage->ContainsGroup(GetStorageIdentifier());
364}
365
Chris Masone9d779932011-08-25 16:33:41 -0700366bool Service::Load(StoreInterface *storage) {
367 const string id = GetStorageIdentifier();
Chris Masone34af2182011-08-22 11:59:36 -0700368 if (!storage->ContainsGroup(id)) {
369 LOG(WARNING) << "Service is not available in the persistent store: " << id;
370 return false;
371 }
372 storage->GetBool(id, kStorageAutoConnect, &auto_connect_);
373 storage->GetString(id, kStorageCheckPortal, &check_portal_);
374 storage->GetBool(id, kStorageFavorite, &favorite_);
mukesh agrawalcf24a242012-05-21 16:46:11 -0700375 storage->GetString(id, kStorageGUID, &guid_);
376 storage->GetBool(id, kStorageHasEverConnected, &has_ever_connected_);
Chris Masone34af2182011-08-22 11:59:36 -0700377 storage->GetInt(id, kStoragePriority, &priority_);
378 storage->GetString(id, kStorageProxyConfig, &proxy_config_);
379 storage->GetBool(id, kStorageSaveCredentials, &save_credentials_);
Paul Stewart987e71e2011-12-05 09:45:06 -0800380 storage->GetString(id, kStorageUIData, &ui_data_);
Chris Masone34af2182011-08-22 11:59:36 -0700381
382 LoadEapCredentials(storage, id);
Paul Stewart1062d9d2012-04-27 10:42:27 -0700383 static_ip_parameters_.Load(storage, id);
Chris Masone34af2182011-08-22 11:59:36 -0700384 // TODO(petkov): Load these:
385
Chris Masone34af2182011-08-22 11:59:36 -0700386 // "Failure"
387 // "Modified"
388 // "LastAttempt"
Chris Masone34af2182011-08-22 11:59:36 -0700389
mukesh agrawaladb68482012-01-17 16:31:51 -0800390 explicitly_disconnected_ = false;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800391 favorite_ = true;
392
Chris Masone34af2182011-08-22 11:59:36 -0700393 return true;
Darin Petkovba40dd32011-07-11 20:06:39 -0700394}
395
Paul Stewart65512e12012-03-26 18:01:08 -0700396bool Service::Unload() {
Paul Stewart7fb09382012-07-18 17:32:27 -0700397 auto_connect_ = IsAutoConnectByDefault();
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800398 check_portal_ = kCheckPortalAuto;
Christopher Wileyabd3b502012-09-26 13:08:52 -0700399 explicitly_disconnected_ = false;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800400 favorite_ = false;
Paul Stewarte7cce8f2012-09-11 10:56:38 -0700401 guid_ = "";
Paul Stewart88769de2012-09-21 13:14:36 -0700402 has_ever_connected_ = false;
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800403 priority_ = kPriorityNone;
404 proxy_config_ = "";
405 save_credentials_ = true;
406 ui_data_ = "";
407
408 UnloadEapCredentials();
Wade Guthrie9e0c2502012-04-19 15:26:40 -0700409 Error error; // Ignored.
410 Disconnect(&error);
Paul Stewart65512e12012-03-26 18:01:08 -0700411 return false;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800412}
413
Chris Masone9d779932011-08-25 16:33:41 -0700414bool Service::Save(StoreInterface *storage) {
415 const string id = GetStorageIdentifier();
Chris Masone34af2182011-08-22 11:59:36 -0700416
Darin Petkov58f0b6d2012-06-12 12:52:30 +0200417 storage->SetString(id, kStorageType, GetTechnologyString());
Paul Stewart2706aaf2011-12-14 16:44:04 -0800418
Chris Masone34af2182011-08-22 11:59:36 -0700419 // TODO(petkov): We could choose to simplify the saving code by removing most
420 // conditionals thus saving even default values.
Wade Guthrie4f28e8b2012-04-11 10:52:07 -0700421 storage->SetBool(id, kStorageAutoConnect, auto_connect_);
Chris Masone34af2182011-08-22 11:59:36 -0700422 if (check_portal_ == kCheckPortalAuto) {
423 storage->DeleteKey(id, kStorageCheckPortal);
424 } else {
425 storage->SetString(id, kStorageCheckPortal, check_portal_);
426 }
427 storage->SetBool(id, kStorageFavorite, favorite_);
mukesh agrawalcf24a242012-05-21 16:46:11 -0700428 SaveString(storage, id, kStorageGUID, guid_, false, true);
429 storage->SetBool(id, kStorageHasEverConnected, has_ever_connected_);
mukesh agrawald835b202011-10-07 15:26:47 -0700430 storage->SetString(id, kStorageName, friendly_name_);
Chris Masone34af2182011-08-22 11:59:36 -0700431 if (priority_ != kPriorityNone) {
432 storage->SetInt(id, kStoragePriority, priority_);
433 } else {
434 storage->DeleteKey(id, kStoragePriority);
435 }
Paul Stewart987e71e2011-12-05 09:45:06 -0800436 SaveString(storage, id, kStorageProxyConfig, proxy_config_, false, true);
Chris Masone34af2182011-08-22 11:59:36 -0700437 if (save_credentials_) {
438 storage->DeleteKey(id, kStorageSaveCredentials);
439 } else {
440 storage->SetBool(id, kStorageSaveCredentials, false);
441 }
Paul Stewart987e71e2011-12-05 09:45:06 -0800442 SaveString(storage, id, kStorageUIData, ui_data_, false, true);
Chris Masone34af2182011-08-22 11:59:36 -0700443
444 SaveEapCredentials(storage, id);
Paul Stewart1062d9d2012-04-27 10:42:27 -0700445 static_ip_parameters_.Save(storage, id);
Chris Masone34af2182011-08-22 11:59:36 -0700446
447 // TODO(petkov): Save these:
448
Chris Masone34af2182011-08-22 11:59:36 -0700449 // "Failure"
450 // "Modified"
451 // "LastAttempt"
Chris Masone34af2182011-08-22 11:59:36 -0700452
453 return true;
454}
455
Gary Moraind93615e2012-04-27 11:50:03 -0700456void Service::SaveToCurrentProfile() {
457 // Some unittests do not specify a manager.
458 if (manager()) {
459 manager()->SaveServiceToProfile(this);
460 }
Thieu Led4e9e552012-02-16 16:26:07 -0800461}
462
Paul Stewartcb59fed2012-03-21 21:14:46 -0700463void Service::Configure(const KeyValueStore &args, Error *error) {
464 map<string, bool>::const_iterator bool_it;
Ben Chanfad4a0b2012-04-18 15:49:59 -0700465 SLOG(Service, 5) << "Configuring bool properties:";
Paul Stewartcb59fed2012-03-21 21:14:46 -0700466 for (bool_it = args.bool_properties().begin();
467 bool_it != args.bool_properties().end();
468 ++bool_it) {
469 if (ContainsKey(parameters_ignored_for_configure_, bool_it->first)) {
470 continue;
471 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700472 SLOG(Service, 5) << " " << bool_it->first;
Paul Stewartcb59fed2012-03-21 21:14:46 -0700473 Error set_error;
474 store_.SetBoolProperty(bool_it->first, bool_it->second, &set_error);
475 if (error->IsSuccess() && set_error.IsFailure()) {
476 error->CopyFrom(set_error);
477 }
478 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700479 SLOG(Service, 5) << "Configuring string properties:";
Paul Stewartcb59fed2012-03-21 21:14:46 -0700480 map<string, string>::const_iterator string_it;
481 for (string_it = args.string_properties().begin();
482 string_it != args.string_properties().end();
483 ++string_it) {
484 if (ContainsKey(parameters_ignored_for_configure_, string_it->first)) {
485 continue;
486 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700487 SLOG(Service, 5) << " " << string_it->first;
Paul Stewartcb59fed2012-03-21 21:14:46 -0700488 Error set_error;
489 store_.SetStringProperty(string_it->first, string_it->second, &set_error);
490 if (error->IsSuccess() && set_error.IsFailure()) {
491 error->CopyFrom(set_error);
492 }
493 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700494 SLOG(Service, 5) << "Configuring uint32 properties:";
Paul Stewartcb59fed2012-03-21 21:14:46 -0700495 map<string, uint32>::const_iterator int_it;
496 for (int_it = args.uint_properties().begin();
497 int_it != args.uint_properties().end();
498 ++int_it) {
499 if (ContainsKey(parameters_ignored_for_configure_, int_it->first)) {
500 continue;
501 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700502 SLOG(Service, 5) << " " << int_it->first;
Paul Stewartcb59fed2012-03-21 21:14:46 -0700503 Error set_error;
504 store_.SetUint32Property(int_it->first, int_it->second, &set_error);
505 if (error->IsSuccess() && set_error.IsFailure()) {
506 error->CopyFrom(set_error);
507 }
508 }
509}
510
Paul Stewart10ccbb32012-04-26 15:59:30 -0700511bool Service::IsRemembered() const {
512 return profile_ && !manager_->IsServiceEphemeral(this);
513}
514
mukesh agrawal00917ce2011-11-22 23:56:55 +0000515void Service::MakeFavorite() {
516 if (favorite_) {
517 // We do not want to clobber the value of auto_connect_ (it may
518 // be user-set). So return early.
519 return;
520 }
521
522 auto_connect_ = true;
523 favorite_ = true;
524}
525
Darin Petkov5eb05422012-05-11 15:45:25 +0200526void Service::SetConnection(const ConnectionRefPtr &connection) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800527 if (connection.get()) {
Paul Stewart1e3bc4962012-09-14 12:20:22 -0700528 // TODO(pstew): Make this function testable by using a factory here.
529 // http://crosbug.com/34528
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800530 http_proxy_.reset(new HTTPProxy(connection));
Darin Petkov5eb05422012-05-11 15:45:25 +0200531 http_proxy_->Start(dispatcher_, sockets_.get());
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800532 } else {
533 http_proxy_.reset();
Paul Stewartdef189e2012-08-02 20:12:09 -0700534 static_ip_parameters_.ClearSavedParameters();
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800535 }
536 connection_ = connection;
Paul Stewart1e3bc4962012-09-14 12:20:22 -0700537 Error error;
538 string ipconfig = GetIPConfigRpcIdentifier(&error);
539 if (error.IsSuccess()) {
540 adaptor_->EmitRpcIdentifierChanged(shill::kIPConfigProperty, ipconfig);
541 }
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800542}
543
Gaurav Shah10109f22011-11-11 20:16:22 -0800544bool Service::Is8021xConnectable() const {
545 // We mirror all the flimflam checks (see service.c:is_connectable()).
546
547 // Identity is required.
548 if (eap_.identity.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700549 SLOG(Service, 2) << "Not connectable: Identity is empty.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800550 return false;
551 }
552
553 if (!eap_.client_cert.empty() || !eap_.cert_id.empty()) {
554 // If a client certificate is being used, we must have a private key.
555 if (eap_.private_key.empty() && eap_.key_id.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700556 SLOG(Service, 2)
557 << "Not connectable. Client certificate but no private key.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800558 return false;
559 }
560 }
561 if (!eap_.cert_id.empty() || !eap_.key_id.empty() ||
562 !eap_.ca_cert_id.empty()) {
563 // If PKCS#11 data is needed, a PIN is required.
564 if (eap_.pin.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700565 SLOG(Service, 2) << "Not connectable. PKCS#11 data but no PIN.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800566 return false;
567 }
568 }
569
570 // For EAP-TLS, a client certificate is required.
571 if (eap_.eap.empty() || eap_.eap == "TLS") {
Paul Stewart81426132012-05-16 10:05:10 -0700572 if ((!eap_.client_cert.empty() || !eap_.cert_id.empty()) &&
573 (!eap_.private_key.empty() || !eap_.key_id.empty())) {
574 SLOG(Service, 2) << "Connectable. EAP-TLS with a client cert and key.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800575 return true;
576 }
577 }
578
579 // For EAP types other than TLS (e.g. EAP-TTLS or EAP-PEAP, password is the
580 // minimum requirement), at least an identity + password is required.
581 if (eap_.eap.empty() || eap_.eap != "TLS") {
582 if (!eap_.password.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700583 SLOG(Service, 2) << "Connectable. !EAP-TLS and has a password.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800584 return true;
585 }
586 }
587
Ben Chanfad4a0b2012-04-18 15:49:59 -0700588 SLOG(Service, 2)
589 << "Not connectable. No suitable EAP configuration was found.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800590 return false;
591}
592
Paul Stewartbc6e7392012-05-24 07:07:48 -0700593bool Service::AddEAPCertification(const string &name, size_t depth) {
594 if (depth >= kEAPMaxCertificationElements) {
595 LOG(WARNING) << "Ignoring certification " << name
596 << " because depth " << depth
597 << " exceeds our maximum of "
598 << kEAPMaxCertificationElements;
599 return false;
600 }
601
602 if (depth >= eap_.remote_certification.size()) {
603 eap_.remote_certification.resize(depth + 1);
604 } else if (name == eap_.remote_certification[depth]) {
605 return true;
606 }
607
608 eap_.remote_certification[depth] = name;
609 LOG(INFO) << "Received certification for "
610 << name
611 << " at depth "
612 << depth;
613 return true;
614}
615
616void Service::ClearEAPCertification() {
617 eap_.remote_certification.clear();
618}
619
Gaurav Shah10109f22011-11-11 20:16:22 -0800620void Service::set_eap(const EapCredentials &eap) {
621 eap_ = eap;
622 // Note: Connectability can only be updated by a subclass of Service
623 // with knowledge of whether the service actually uses 802.1x credentials.
624}
625
mukesh agrawal00917ce2011-11-22 23:56:55 +0000626// static
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800627const char *Service::ConnectFailureToString(const ConnectFailure &state) {
628 switch (state) {
629 case kFailureUnknown:
630 return "Unknown";
Paul Stewartf2d60912012-07-15 08:37:30 -0700631 case kFailureAAA:
632 return flimflam::kErrorAaaFailed;
633 case kFailureActivation:
634 return flimflam::kErrorActivationFailed;
635 case kFailureBadPassphrase:
636 return flimflam::kErrorBadPassphrase;
637 case kFailureBadWEPKey:
638 return flimflam::kErrorBadWEPKey;
639 case kFailureConnect:
640 return flimflam::kErrorConnectFailed;
641 case kFailureDNSLookup:
642 return flimflam::kErrorDNSLookupFailed;
643 case kFailureDHCP:
644 return flimflam::kErrorDhcpFailed;
645 case kFailureHTTPGet:
646 return flimflam::kErrorHTTPGetFailed;
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800647 case kFailureNeedEVDO:
Paul Stewartf2d60912012-07-15 08:37:30 -0700648 return flimflam::kErrorNeedEvdo;
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800649 case kFailureNeedHomeNetwork:
Paul Stewartf2d60912012-07-15 08:37:30 -0700650 return flimflam::kErrorNeedHomeNetwork;
651 case kFailureOTASP:
652 return flimflam::kErrorOtaspFailed;
653 case kFailureOutOfRange:
654 return flimflam::kErrorOutOfRange;
655 case kFailurePinMissing:
656 return flimflam::kErrorPinMissing;
657 case kFailurePPPAuth:
658 return flimflam::kErrorPppAuthFailed;
Thieu Le48e6d6d2011-12-06 00:40:27 +0000659 case kFailureMax:
660 return "Max failure error code";
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800661 }
662 return "Invalid";
663}
664
665// static
666const char *Service::ConnectStateToString(const ConnectState &state) {
667 switch (state) {
668 case kStateUnknown:
669 return "Unknown";
670 case kStateIdle:
671 return "Idle";
672 case kStateAssociating:
673 return "Associating";
674 case kStateConfiguring:
675 return "Configuring";
676 case kStateConnected:
677 return "Connected";
678 case kStateDisconnected:
679 return "Disconnected";
Thieu Le48e6d6d2011-12-06 00:40:27 +0000680 case kStatePortal:
681 return "Portal";
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800682 case kStateFailure:
683 return "Failure";
684 case kStateOnline:
685 return "Online";
686 }
687 return "Invalid";
688}
689
Darin Petkov58f0b6d2012-06-12 12:52:30 +0200690string Service::GetTechnologyString() const {
Gaurav Shah435de2c2011-11-17 19:01:07 -0800691 return Technology::NameFromIdentifier(technology());
692}
693
Darin Petkov58f0b6d2012-06-12 12:52:30 +0200694string Service::CalculateTechnology(Error */*error*/) {
695 return GetTechnologyString();
696}
697
Jason Glasgowb5790052012-01-27 01:03:52 -0500698// static
Paul Stewart22aa71b2011-09-16 12:15:11 -0700699bool Service::DecideBetween(int a, int b, bool *decision) {
700 if (a == b)
701 return false;
702 *decision = (a > b);
703 return true;
704}
705
mukesh agrawal00917ce2011-11-22 23:56:55 +0000706// static
Paul Stewart22aa71b2011-09-16 12:15:11 -0700707bool Service::Compare(ServiceRefPtr a,
708 ServiceRefPtr b,
mukesh agrawalddc378f2012-02-17 18:26:20 -0800709 const vector<Technology::Identifier> &tech_order,
710 const char **reason) {
Paul Stewart22aa71b2011-09-16 12:15:11 -0700711 bool ret;
712
713 if (a->state() != b->state()) {
714 if (DecideBetween(a->IsConnected(), b->IsConnected(), &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800715 *reason = kServiceSortIsConnected;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700716 return ret;
717 }
718
Paul Stewarta121c442012-06-09 14:12:58 -0700719 if (DecideBetween(!a->IsPortalled(), !b->IsPortalled(), &ret)) {
720 *reason = kServiceSortIsPortalled;
721 return ret;
722 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700723
724 if (DecideBetween(a->IsConnecting(), b->IsConnecting(), &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800725 *reason = kServiceSortIsConnecting;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700726 return ret;
727 }
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000728
729 if (DecideBetween(!a->IsFailed(), !b->IsFailed(), &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800730 *reason = kServiceSortIsFailed;
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000731 return ret;
732 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700733 }
734
Darin Petkov2f903b32012-04-18 12:56:43 +0200735 if (DecideBetween(a->connectable(), b->connectable(), &ret)) {
736 *reason = kServiceSortConnectable;
737 return ret;
738 }
739
740 // Ignore the auto-connect property if both services are connected
741 // already. This allows connected non-autoconnectable VPN services to be
742 // sorted higher than other connected services based on technology order.
743 if (!a->IsConnected() &&
744 DecideBetween(a->auto_connect(), b->auto_connect(), &ret)) {
745 *reason = kServiceSortAutoConnect;
746 return ret;
747 }
748
749 if (DecideBetween(a->favorite(), b->favorite(), &ret)) {
750 *reason = kServiceSortFavorite;
751 return ret;
752 }
753
754 if (DecideBetween(a->priority(), b->priority(), &ret)) {
755 *reason = kServiceSortPriority;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700756 return ret;
757 }
758
759 // TODO(pstew): Below this point we are making value judgements on
760 // services that are not related to anything intrinsic or
761 // user-specified. These heuristics should be richer (contain
762 // historical information, for example) and be subject to user
763 // customization.
Paul Stewart22aa71b2011-09-16 12:15:11 -0700764 for (vector<Technology::Identifier>::const_iterator it = tech_order.begin();
765 it != tech_order.end();
766 ++it) {
Joshua Kroll053fa822012-06-05 09:50:43 -0700767 if (DecideBetween(a->technology() == *it, b->technology() == *it, &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800768 *reason = kServiceSortTechnology;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700769 return ret;
mukesh agrawalddc378f2012-02-17 18:26:20 -0800770 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700771 }
772
Paul Stewart1ca3e852011-11-04 07:50:49 -0700773 if (DecideBetween(a->security_level(), b->security_level(), &ret) ||
Paul Stewart22aa71b2011-09-16 12:15:11 -0700774 DecideBetween(a->strength(), b->strength(), &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800775 *reason = kServiceSortSecurityEtc;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700776 return ret;
777 }
778
mukesh agrawalddc378f2012-02-17 18:26:20 -0800779 *reason = kServiceSortUniqueName;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700780 return a->UniqueName() < b->UniqueName();
781}
782
Chris Masone34af2182011-08-22 11:59:36 -0700783const ProfileRefPtr &Service::profile() const { return profile_; }
784
785void Service::set_profile(const ProfileRefPtr &p) { profile_ = p; }
786
Paul Stewartff14b022012-04-24 20:06:23 -0700787void Service::OnPropertyChanged(const string &property) {
Paul Stewart81426132012-05-16 10:05:10 -0700788 if (Is8021x() &&
789 (property == flimflam::kEAPCertIDProperty ||
790 property == flimflam::kEAPClientCertProperty ||
791 property == flimflam::kEAPKeyIDProperty ||
792 property == flimflam::kEAPPINProperty ||
793 property == flimflam::kEapCaCertIDProperty ||
794 property == flimflam::kEapIdentityProperty ||
Paul Stewartadf79d82012-07-18 16:09:56 -0700795 property == flimflam::kEapKeyMgmtProperty ||
Paul Stewart81426132012-05-16 10:05:10 -0700796 property == flimflam::kEapPasswordProperty ||
797 property == flimflam::kEapPrivateKeyProperty)) {
798 // This notifies subclassess that EAP parameters have been changed.
799 set_eap(eap_);
800 }
mukesh agrawalcf24a242012-05-21 16:46:11 -0700801 SaveToProfile();
Paul Stewartd215af62012-04-24 23:25:50 -0700802 if ((property == flimflam::kCheckPortalProperty ||
803 property == flimflam::kProxyConfigProperty) &&
804 (state_ == kStateConnected ||
805 state_ == kStatePortal ||
806 state_ == kStateOnline)) {
807 manager_->RecheckPortalOnService(this);
808 }
Paul Stewartff14b022012-04-24 20:06:23 -0700809}
810
Christopher Wiley0801d192012-09-24 11:57:15 -0700811void Service::OnAfterResume() {
812 // Forget old autoconnect failures across suspend/resume.
813 auto_connect_cooldown_milliseconds_ = 0;
814 reenable_auto_connect_task_.Cancel();
Christopher Wileya4c61ae2012-10-01 11:04:30 -0700815 // Forget if the user disconnected us, we might be able to connect now.
816 explicitly_disconnected_ = false;
Christopher Wiley0801d192012-09-24 11:57:15 -0700817}
818
Paul Stewart10241e32012-04-23 18:15:06 -0700819string Service::GetIPConfigRpcIdentifier(Error *error) {
820 if (!connection_) {
821 error->Populate(Error::kNotFound);
822 return "/";
823 }
824
825 string id = connection_->ipconfig_rpc_identifier();
826
827 if (id.empty()) {
828 // Do not return an empty IPConfig.
829 error->Populate(Error::kNotFound);
830 return "/";
831 }
832
833 return id;
834}
835
mukesh agrawal29c13a12011-11-24 00:09:19 +0000836void Service::set_connectable(bool connectable) {
837 connectable_ = connectable;
838 adaptor_->EmitBoolChanged(flimflam::kConnectableProperty, connectable_);
839}
840
Darin Petkovb2ba39f2012-06-06 10:33:43 +0200841void Service::SetConnectable(bool connectable) {
842 if (connectable_ == connectable) {
843 return;
844 }
845 connectable_ = connectable;
846 adaptor_->EmitBoolChanged(flimflam::kConnectableProperty, connectable_);
847 if (manager_->HasService(this)) {
848 manager_->UpdateService(this);
849 }
850}
851
Darin Petkov58f0b6d2012-06-12 12:52:30 +0200852string Service::GetStateString() const {
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700853 switch (state_) {
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800854 case kStateIdle:
855 return flimflam::kStateIdle;
856 case kStateAssociating:
857 return flimflam::kStateAssociation;
858 case kStateConfiguring:
859 return flimflam::kStateConfiguration;
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700860 case kStateConnected:
Paul Stewart20088d82012-02-16 06:58:55 -0800861 return flimflam::kStateReady;
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800862 case kStateDisconnected:
863 return flimflam::kStateDisconnect;
864 case kStateFailure:
865 return flimflam::kStateFailure;
Paul Stewart20088d82012-02-16 06:58:55 -0800866 case kStatePortal:
867 return flimflam::kStatePortal;
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800868 case kStateOnline:
869 return flimflam::kStateOnline;
870 case kStateUnknown:
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700871 default:
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800872 return "";
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700873 }
874}
875
Darin Petkov58f0b6d2012-06-12 12:52:30 +0200876string Service::CalculateState(Error */*error*/) {
877 return GetStateString();
878}
879
mukesh agrawalbf14e942012-03-02 14:36:34 -0800880bool Service::IsAutoConnectable(const char **reason) const {
881 if (!connectable()) {
882 *reason = kAutoConnNotConnectable;
883 return false;
884 }
885
886 if (IsConnected()) {
887 *reason = kAutoConnConnected;
888 return false;
889 }
890
891 if (IsConnecting()) {
892 *reason = kAutoConnConnecting;
893 return false;
894 }
895
896 if (explicitly_disconnected_) {
897 *reason = kAutoConnExplicitDisconnect;
898 return false;
899 }
900
Christopher Wiley0801d192012-09-24 11:57:15 -0700901 if (!reenable_auto_connect_task_.IsCancelled()) {
902 *reason = kAutoConnThrottled;
903 return false;
904 }
905
mukesh agrawalbf14e942012-03-02 14:36:34 -0800906 return true;
mukesh agrawal76d13882012-01-12 15:23:11 -0800907}
908
Paul Stewartd215af62012-04-24 23:25:50 -0700909bool Service::IsPortalDetectionDisabled() const {
910 return check_portal_ == kCheckPortalFalse;
911}
912
913bool Service::IsPortalDetectionAuto() const {
914 return check_portal_ == kCheckPortalAuto;
915}
916
mukesh agrawalffa3d042011-10-06 15:26:10 -0700917void Service::HelpRegisterDerivedBool(
918 const string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800919 bool(Service::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700920 void(Service::*set)(const bool&, Error *)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700921 store_.RegisterDerivedBool(
922 name,
923 BoolAccessor(new CustomAccessor<Service, bool>(this, get, set)));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700924}
925
mukesh agrawalffa3d042011-10-06 15:26:10 -0700926void Service::HelpRegisterDerivedString(
927 const string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800928 string(Service::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700929 void(Service::*set)(const string&, Error *)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700930 store_.RegisterDerivedString(
931 name,
932 StringAccessor(new CustomAccessor<Service, string>(this, get, set)));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700933}
934
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400935void Service::HelpRegisterDerivedRpcIdentifier(
936 const string &name,
937 RpcIdentifier(Service::*get)(Error *),
938 void(Service::*set)(const RpcIdentifier&, Error *)) {
939 store_.RegisterDerivedRpcIdentifier(
940 name,
941 RpcIdentifierAccessor(new CustomAccessor<Service, RpcIdentifier>(
942 this, get, set)));
943}
944
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800945void Service::HelpRegisterDerivedUint16(
946 const string &name,
947 uint16(Service::*get)(Error *),
948 void(Service::*set)(const uint16&, Error *)) {
949 store_.RegisterDerivedUint16(
950 name,
951 Uint16Accessor(new CustomAccessor<Service, uint16>(this, get, set)));
952}
953
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800954void Service::HelpRegisterWriteOnlyDerivedString(
955 const string &name,
956 void(Service::*set)(const string &, Error *),
957 void(Service::*clear)(Error *),
958 const string *default_value) {
959 store_.RegisterDerivedString(
960 name,
961 StringAccessor(
962 new CustomWriteOnlyAccessor<Service, string>(
963 this, set, clear, default_value)));
964}
965
Darin Petkovba40dd32011-07-11 20:06:39 -0700966void Service::SaveString(StoreInterface *storage,
Chris Masone34af2182011-08-22 11:59:36 -0700967 const string &id,
Darin Petkovba40dd32011-07-11 20:06:39 -0700968 const string &key,
969 const string &value,
970 bool crypted,
971 bool save) {
972 if (value.empty() || !save) {
Chris Masone34af2182011-08-22 11:59:36 -0700973 storage->DeleteKey(id, key);
Darin Petkovba40dd32011-07-11 20:06:39 -0700974 return;
975 }
976 if (crypted) {
Chris Masone34af2182011-08-22 11:59:36 -0700977 storage->SetCryptedString(id, key, value);
Darin Petkovba40dd32011-07-11 20:06:39 -0700978 return;
979 }
Chris Masone34af2182011-08-22 11:59:36 -0700980 storage->SetString(id, key, value);
Darin Petkovba40dd32011-07-11 20:06:39 -0700981}
982
Chris Masone34af2182011-08-22 11:59:36 -0700983void Service::LoadEapCredentials(StoreInterface *storage, const string &id) {
Gaurav Shah10109f22011-11-11 20:16:22 -0800984 EapCredentials eap;
985 storage->GetCryptedString(id, kStorageEapIdentity, &eap.identity);
986 storage->GetString(id, kStorageEapEap, &eap.eap);
987 storage->GetString(id, kStorageEapInnerEap, &eap.inner_eap);
Darin Petkovba40dd32011-07-11 20:06:39 -0700988 storage->GetCryptedString(id,
989 kStorageEapAnonymousIdentity,
Gaurav Shah10109f22011-11-11 20:16:22 -0800990 &eap.anonymous_identity);
991 storage->GetString(id, kStorageEapClientCert, &eap.client_cert);
992 storage->GetString(id, kStorageEapCertID, &eap.cert_id);
993 storage->GetString(id, kStorageEapPrivateKey, &eap.private_key);
Darin Petkovba40dd32011-07-11 20:06:39 -0700994 storage->GetCryptedString(id,
995 kStorageEapPrivateKeyPassword,
Gaurav Shah10109f22011-11-11 20:16:22 -0800996 &eap.private_key_password);
997 storage->GetString(id, kStorageEapKeyID, &eap.key_id);
998 storage->GetString(id, kStorageEapCACert, &eap.ca_cert);
999 storage->GetString(id, kStorageEapCACertID, &eap.ca_cert_id);
Paul Stewartecf4cd12012-04-17 11:08:39 -07001000 storage->GetString(id, kStorageEapCACertNSS, &eap.ca_cert_nss);
Gaurav Shah10109f22011-11-11 20:16:22 -08001001 storage->GetBool(id, kStorageEapUseSystemCAs, &eap.use_system_cas);
1002 storage->GetString(id, kStorageEapPIN, &eap.pin);
1003 storage->GetCryptedString(id, kStorageEapPassword, &eap.password);
1004 storage->GetString(id, kStorageEapKeyManagement, &eap.key_management);
1005 set_eap(eap);
Darin Petkovba40dd32011-07-11 20:06:39 -07001006}
1007
Chris Masone34af2182011-08-22 11:59:36 -07001008void Service::SaveEapCredentials(StoreInterface *storage, const string &id) {
Darin Petkovba40dd32011-07-11 20:06:39 -07001009 bool save = save_credentials_;
Chris Masone34af2182011-08-22 11:59:36 -07001010 SaveString(storage, id, kStorageEapIdentity, eap_.identity, true, save);
1011 SaveString(storage, id, kStorageEapEap, eap_.eap, false, true);
1012 SaveString(storage, id, kStorageEapInnerEap, eap_.inner_eap, false, true);
Darin Petkovba40dd32011-07-11 20:06:39 -07001013 SaveString(storage,
Chris Masone34af2182011-08-22 11:59:36 -07001014 id,
Darin Petkovba40dd32011-07-11 20:06:39 -07001015 kStorageEapAnonymousIdentity,
1016 eap_.anonymous_identity,
1017 true,
1018 save);
Chris Masone34af2182011-08-22 11:59:36 -07001019 SaveString(storage, id, kStorageEapClientCert, eap_.client_cert, false, save);
1020 SaveString(storage, id, kStorageEapCertID, eap_.cert_id, false, save);
1021 SaveString(storage, id, kStorageEapPrivateKey, eap_.private_key, false, save);
Darin Petkovba40dd32011-07-11 20:06:39 -07001022 SaveString(storage,
Chris Masone34af2182011-08-22 11:59:36 -07001023 id,
Darin Petkovba40dd32011-07-11 20:06:39 -07001024 kStorageEapPrivateKeyPassword,
1025 eap_.private_key_password,
1026 true,
1027 save);
Chris Masone34af2182011-08-22 11:59:36 -07001028 SaveString(storage, id, kStorageEapKeyID, eap_.key_id, false, save);
1029 SaveString(storage, id, kStorageEapCACert, eap_.ca_cert, false, true);
1030 SaveString(storage, id, kStorageEapCACertID, eap_.ca_cert_id, false, true);
Paul Stewartecf4cd12012-04-17 11:08:39 -07001031 SaveString(storage, id, kStorageEapCACertNSS, eap_.ca_cert_nss, false, true);
Chris Masone34af2182011-08-22 11:59:36 -07001032 storage->SetBool(id, kStorageEapUseSystemCAs, eap_.use_system_cas);
1033 SaveString(storage, id, kStorageEapPIN, eap_.pin, false, save);
1034 SaveString(storage, id, kStorageEapPassword, eap_.password, true, save);
Darin Petkovba40dd32011-07-11 20:06:39 -07001035 SaveString(storage,
Chris Masone34af2182011-08-22 11:59:36 -07001036 id,
Darin Petkovba40dd32011-07-11 20:06:39 -07001037 kStorageEapKeyManagement,
1038 eap_.key_management,
1039 false,
1040 true);
1041}
1042
Paul Stewartd8ad3c42012-01-09 12:39:38 -08001043void Service::UnloadEapCredentials() {
1044 eap_.identity = "";
1045 eap_.eap = "";
1046 eap_.inner_eap = "";
1047 eap_.anonymous_identity = "";
1048 eap_.client_cert = "";
1049 eap_.cert_id = "";
1050 eap_.private_key = "";
1051 eap_.private_key_password = "";
1052 eap_.key_id = "";
1053 eap_.ca_cert = "";
1054 eap_.ca_cert_id = "";
Paul Stewart20550982012-04-16 12:16:11 -07001055 eap_.use_system_cas = true;
Paul Stewartd8ad3c42012-01-09 12:39:38 -08001056 eap_.pin = "";
1057 eap_.password = "";
Paul Stewartd8ad3c42012-01-09 12:39:38 -08001058}
1059
Paul Stewartcb59fed2012-03-21 21:14:46 -07001060void Service::IgnoreParameterForConfigure(const string &parameter) {
1061 parameters_ignored_for_configure_.insert(parameter);
1062}
1063
Paul Stewartac4ac002011-08-26 12:04:26 -07001064const string &Service::GetEAPKeyManagement() const {
1065 return eap_.key_management;
1066}
1067
1068void Service::SetEAPKeyManagement(const string &key_management) {
1069 eap_.key_management = key_management;
1070}
1071
Thieu Le284fe792012-01-31 17:53:19 -08001072bool Service::GetAutoConnect(Error */*error*/) {
1073 return auto_connect();
1074}
1075
1076void Service::SetAutoConnect(const bool &connect, Error *error) {
Wade Guthrie4f28e8b2012-04-11 10:52:07 -07001077 set_auto_connect(connect);
Thieu Le284fe792012-01-31 17:53:19 -08001078}
1079
Paul Stewartd215af62012-04-24 23:25:50 -07001080string Service::GetCheckPortal(Error *error) {
1081 return check_portal_;
1082}
1083
1084void Service::SetCheckPortal(const string &check_portal, Error *error) {
1085 if (check_portal == check_portal_) {
1086 return;
1087 }
1088 if (check_portal != kCheckPortalFalse &&
1089 check_portal != kCheckPortalTrue &&
1090 check_portal != kCheckPortalAuto) {
1091 Error::PopulateAndLog(error, Error::kInvalidArguments,
1092 base::StringPrintf(
1093 "Invalid Service CheckPortal property value: %s",
1094 check_portal.c_str()));
1095 return;
1096 }
1097 check_portal_ = check_portal;
1098}
1099
Paul Stewart9f32d192012-01-30 20:37:50 -08001100void Service::SetEAPPassword(const string &password, Error */*error*/) {
1101 eap_.password = password;
1102}
1103
1104void Service::SetEAPPrivateKeyPassword(const string &password,
1105 Error */*error*/) {
1106 eap_.private_key_password = password;
1107}
1108
Paul Stewart0c438332012-04-11 07:55:27 -07001109string Service::GetNameProperty(Error *error) {
1110 return friendly_name_;
1111}
1112
1113void Service::AssertTrivialSetNameProperty(const string &name, Error *error) {
1114 if (name != friendly_name_) {
1115 Error::PopulateAndLog(error, Error::kInvalidArguments,
Paul Stewart2bf424f2012-04-11 18:59:39 -07001116 base::StringPrintf(
1117 "Service Name property cannot be modified "
1118 "(%s to %s)", friendly_name_.c_str(),
1119 name.c_str()));
Paul Stewart0c438332012-04-11 07:55:27 -07001120 }
1121}
1122
Paul Stewart1b1a7f22012-01-06 16:24:06 -08001123string Service::GetProfileRpcId(Error *error) {
1124 if (!profile_) {
1125 // This happens in some unit tests where profile_ is not set.
1126 error->Populate(Error::kNotFound);
1127 return "";
1128 }
1129 return profile_->GetRpcIdentifier();
1130}
1131
1132void Service::SetProfileRpcId(const string &profile, Error *error) {
1133 manager_->SetProfileForService(this, profile, error);
1134}
1135
Paul Stewartbe5f5b32011-12-07 17:11:11 -08001136uint16 Service::GetHTTPProxyPort(Error */*error*/) {
1137 if (http_proxy_.get()) {
1138 return static_cast<uint16>(http_proxy_->proxy_port());
1139 }
1140 return 0;
1141}
1142
mukesh agrawalcf24a242012-05-21 16:46:11 -07001143void Service::SaveToProfile() {
1144 if (profile_.get() && profile_->GetConstStorage()) {
1145 profile_->UpdateService(this);
1146 }
1147}
1148
Darin Petkovd78ee7e2012-01-12 11:21:10 +01001149void Service::SetStrength(uint8 strength) {
1150 if (strength == strength_) {
1151 return;
1152 }
1153 strength_ = strength;
1154 adaptor_->EmitUint8Changed(flimflam::kSignalStrengthProperty, strength);
1155}
1156
Paul Stewartf2d60912012-07-15 08:37:30 -07001157void Service::UpdateErrorProperty() {
1158 const string error(ConnectFailureToString(failure_));
1159 if (error == error_) {
1160 return;
1161 }
1162 error_ = error;
1163 adaptor_->EmitStringChanged(flimflam::kErrorProperty, error);
1164}
1165
Paul Stewart75897df2011-04-27 09:05:53 -07001166} // namespace shill