blob: 7867f9552a018906dedac6e4c11c1684fe2c4a4c [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
Chris Masone8fe2c7e2011-06-09 15:51:19 -070033using std::map;
Paul Stewart75897df2011-04-27 09:05:53 -070034using std::string;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070035using std::vector;
Paul Stewart75897df2011-04-27 09:05:53 -070036
37namespace shill {
Darin Petkovba40dd32011-07-11 20:06:39 -070038
mukesh agrawalbf14e942012-03-02 14:36:34 -080039const char Service::kAutoConnConnected[] = "connected";
40const char Service::kAutoConnConnecting[] = "connecting";
41const char Service::kAutoConnExplicitDisconnect[] = "explicitly disconnected";
42const char Service::kAutoConnNotConnectable[] = "not connectable";
43
Paul Stewartbc6e7392012-05-24 07:07:48 -070044const size_t Service::kEAPMaxCertificationElements = 10;
45
Darin Petkovba40dd32011-07-11 20:06:39 -070046const char Service::kCheckPortalAuto[] = "auto";
47const char Service::kCheckPortalFalse[] = "false";
48const char Service::kCheckPortalTrue[] = "true";
49
Paul Stewartac4ac002011-08-26 12:04:26 -070050const int Service::kPriorityNone = 0;
51
Darin Petkov2f903b32012-04-18 12:56:43 +020052const char Service::kServiceSortAutoConnect[] = "AutoConnect";
53const char Service::kServiceSortConnectable[] = "Connectable";
54const char Service::kServiceSortFavorite[] = "Favorite";
mukesh agrawalddc378f2012-02-17 18:26:20 -080055const char Service::kServiceSortIsConnected[] = "IsConnected";
56const char Service::kServiceSortIsConnecting[] = "IsConnecting";
57const char Service::kServiceSortIsFailed[] = "IsFailed";
Paul Stewarta121c442012-06-09 14:12:58 -070058const char Service::kServiceSortIsPortalled[] = "IsPortal";
Darin Petkov2f903b32012-04-18 12:56:43 +020059const char Service::kServiceSortPriority[] = "Priority";
mukesh agrawalddc378f2012-02-17 18:26:20 -080060const char Service::kServiceSortSecurityEtc[] = "SecurityEtc";
Darin Petkov2f903b32012-04-18 12:56:43 +020061const char Service::kServiceSortTechnology[] = "Technology";
mukesh agrawalddc378f2012-02-17 18:26:20 -080062const char Service::kServiceSortUniqueName[] = "UniqueName";
63
Darin Petkovba40dd32011-07-11 20:06:39 -070064const char Service::kStorageAutoConnect[] = "AutoConnect";
65const char Service::kStorageCheckPortal[] = "CheckPortal";
66const char Service::kStorageEapAnonymousIdentity[] = "EAP.AnonymousIdentity";
67const char Service::kStorageEapCACert[] = "EAP.CACert";
68const char Service::kStorageEapCACertID[] = "EAP.CACertID";
Paul Stewartecf4cd12012-04-17 11:08:39 -070069const char Service::kStorageEapCACertNSS[] = "EAP.CACertNSS";
Darin Petkovba40dd32011-07-11 20:06:39 -070070const char Service::kStorageEapCertID[] = "EAP.CertID";
71const char Service::kStorageEapClientCert[] = "EAP.ClientCert";
72const char Service::kStorageEapEap[] = "EAP.EAP";
73const char Service::kStorageEapIdentity[] = "EAP.Identity";
74const char Service::kStorageEapInnerEap[] = "EAP.InnerEAP";
75const char Service::kStorageEapKeyID[] = "EAP.KeyID";
76const char Service::kStorageEapKeyManagement[] = "EAP.KeyMgmt";
77const char Service::kStorageEapPIN[] = "EAP.PIN";
78const char Service::kStorageEapPassword[] = "EAP.Password";
79const char Service::kStorageEapPrivateKey[] = "EAP.PrivateKey";
80const char Service::kStorageEapPrivateKeyPassword[] = "EAP.PrivateKeyPassword";
81const char Service::kStorageEapUseSystemCAs[] = "EAP.UseSystemCAs";
Paul Stewart0756db92012-01-27 08:34:47 -080082const char Service::kStorageError[] = "Error";
Darin Petkovba40dd32011-07-11 20:06:39 -070083const char Service::kStorageFavorite[] = "Favorite";
Paul Stewart0756db92012-01-27 08:34:47 -080084const char Service::kStorageGUID[] = "GUID";
mukesh agrawalcf24a242012-05-21 16:46:11 -070085const char Service::kStorageHasEverConnected[] = "HasEverConnected";
Darin Petkovba40dd32011-07-11 20:06:39 -070086const char Service::kStorageName[] = "Name";
87const char Service::kStoragePriority[] = "Priority";
88const char Service::kStorageProxyConfig[] = "ProxyConfig";
89const char Service::kStorageSaveCredentials[] = "SaveCredentials";
Paul Stewart2706aaf2011-12-14 16:44:04 -080090const char Service::kStorageType[] = "Type";
Paul Stewart987e71e2011-12-05 09:45:06 -080091const char Service::kStorageUIData[] = "UIData";
Darin Petkovba40dd32011-07-11 20:06:39 -070092
mukesh agrawal8f3f7752012-02-17 19:42:09 -080093const uint8 Service::kStrengthMax = 100;
94const uint8 Service::kStrengthMin = 0;
95
mukesh agrawal51a7e932011-07-27 16:18:26 -070096// static
97unsigned int Service::serial_number_ = 0;
98
Paul Stewart75897df2011-04-27 09:05:53 -070099Service::Service(ControlInterface *control_interface,
mukesh agrawalb54601c2011-06-07 17:39:22 -0700100 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -0800101 Metrics *metrics,
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700102 Manager *manager,
Gaurav Shah435de2c2011-11-17 19:01:07 -0800103 Technology::Identifier technology)
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400104 : state_(kStateIdle),
Paul Stewart03dba0b2011-08-22 16:32:45 -0700105 failure_(kFailureUnknown),
106 auto_connect_(false),
Darin Petkovba40dd32011-07-11 20:06:39 -0700107 check_portal_(kCheckPortalAuto),
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700108 connectable_(false),
Paul Stewartf2d60912012-07-15 08:37:30 -0700109 error_(ConnectFailureToString(failure_)),
mukesh agrawaladb68482012-01-17 16:31:51 -0800110 explicitly_disconnected_(false),
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700111 favorite_(false),
Darin Petkovba40dd32011-07-11 20:06:39 -0700112 priority_(kPriorityNone),
Paul Stewart1ca3e852011-11-04 07:50:49 -0700113 security_level_(0),
Paul Stewart22aa71b2011-09-16 12:15:11 -0700114 strength_(0),
Darin Petkovba40dd32011-07-11 20:06:39 -0700115 save_credentials_(true),
Gaurav Shah435de2c2011-11-17 19:01:07 -0800116 technology_(technology),
mukesh agrawal568b5c62012-02-28 14:44:47 -0800117 failed_time_(0),
mukesh agrawalcf24a242012-05-21 16:46:11 -0700118 has_ever_connected_(false),
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700119 dispatcher_(dispatcher),
mukesh agrawald835b202011-10-07 15:26:47 -0700120 unique_name_(base::UintToString(serial_number_++)),
121 friendly_name_(unique_name_),
Chris Masonea82b7112011-05-25 15:16:29 -0700122 available_(false),
123 configured_(false),
Chris Masonea82b7112011-05-25 15:16:29 -0700124 configuration_(NULL),
Chris Masone6791a432011-07-12 13:23:19 -0700125 adaptor_(control_interface->CreateServiceAdaptor(this)),
Thieu Le3426c8f2012-01-11 17:35:11 -0800126 metrics_(metrics),
Darin Petkov5eb05422012-05-11 15:45:25 +0200127 manager_(manager),
128 sockets_(new Sockets()) {
Thieu Le284fe792012-01-31 17:53:19 -0800129 HelpRegisterDerivedBool(flimflam::kAutoConnectProperty,
130 &Service::GetAutoConnect,
131 &Service::SetAutoConnect);
Chris Masone4d42df82011-07-02 17:09:39 -0700132
133 // flimflam::kActivationStateProperty: Registered in CellularService
134 // flimflam::kCellularApnProperty: Registered in CellularService
135 // flimflam::kCellularLastGoodApnProperty: Registered in CellularService
136 // flimflam::kNetworkTechnologyProperty: Registered in CellularService
Darin Petkov3335b372011-08-22 11:05:32 -0700137 // flimflam::kOperatorNameProperty: DEPRECATED
138 // flimflam::kOperatorCodeProperty: DEPRECATED
Chris Masone4d42df82011-07-02 17:09:39 -0700139 // flimflam::kRoamingStateProperty: Registered in CellularService
Darin Petkov3335b372011-08-22 11:05:32 -0700140 // flimflam::kServingOperatorProperty: Registered in CellularService
Chris Masone4d42df82011-07-02 17:09:39 -0700141 // flimflam::kPaymentURLProperty: Registered in CellularService
142
Paul Stewartd215af62012-04-24 23:25:50 -0700143 HelpRegisterDerivedString(flimflam::kCheckPortalProperty,
144 &Service::GetCheckPortal,
145 &Service::SetCheckPortal);
Chris Masone27c4aa52011-07-02 13:10:14 -0700146 store_.RegisterConstBool(flimflam::kConnectableProperty, &connectable_);
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400147 HelpRegisterDerivedRpcIdentifier(flimflam::kDeviceProperty,
148 &Service::GetDeviceRpcId,
149 NULL);
Paul Stewart4c561612012-03-21 12:49:01 -0700150 store_.RegisterString(flimflam::kGuidProperty, &guid_);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700151
Chris Masoneb2e326b2011-07-12 13:28:51 -0700152 store_.RegisterString(flimflam::kEapIdentityProperty, &eap_.identity);
153 store_.RegisterString(flimflam::kEAPEAPProperty, &eap_.eap);
154 store_.RegisterString(flimflam::kEapPhase2AuthProperty, &eap_.inner_eap);
Chris Masone27c4aa52011-07-02 13:10:14 -0700155 store_.RegisterString(flimflam::kEapAnonymousIdentityProperty,
Chris Masoneb2e326b2011-07-12 13:28:51 -0700156 &eap_.anonymous_identity);
157 store_.RegisterString(flimflam::kEAPClientCertProperty, &eap_.client_cert);
158 store_.RegisterString(flimflam::kEAPCertIDProperty, &eap_.cert_id);
159 store_.RegisterString(flimflam::kEapPrivateKeyProperty, &eap_.private_key);
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800160 HelpRegisterWriteOnlyDerivedString(flimflam::kEapPrivateKeyPasswordProperty,
161 &Service::SetEAPPrivateKeyPassword,
162 NULL,
163 &eap_.private_key_password);
Chris Masoneb2e326b2011-07-12 13:28:51 -0700164 store_.RegisterString(flimflam::kEAPKeyIDProperty, &eap_.key_id);
165 store_.RegisterString(flimflam::kEapCaCertProperty, &eap_.ca_cert);
166 store_.RegisterString(flimflam::kEapCaCertIDProperty, &eap_.ca_cert_id);
Paul Stewartecf4cd12012-04-17 11:08:39 -0700167 store_.RegisterString(flimflam::kEapCaCertNssProperty, &eap_.ca_cert_nss);
Chris Masoneb2e326b2011-07-12 13:28:51 -0700168 store_.RegisterString(flimflam::kEAPPINProperty, &eap_.pin);
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800169 HelpRegisterWriteOnlyDerivedString(flimflam::kEapPasswordProperty,
170 &Service::SetEAPPassword,
171 NULL,
172 &eap_.password);
Chris Masoneb2e326b2011-07-12 13:28:51 -0700173 store_.RegisterString(flimflam::kEapKeyMgmtProperty, &eap_.key_management);
174 store_.RegisterBool(flimflam::kEapUseSystemCAsProperty, &eap_.use_system_cas);
Paul Stewartbc6e7392012-05-24 07:07:48 -0700175 store_.RegisterConstStrings(shill::kEapRemoteCertificationProperty,
176 &eap_.remote_certification);
177 store_.RegisterString(shill::kEapSubjectMatchProperty,
178 &eap_.subject_match);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700179
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400180 // TODO(ers): in flimflam clearing Error has the side-effect of
181 // setting the service state to IDLE. Is this important? I could
182 // see an autotest depending on it.
Chris Masone27c4aa52011-07-02 13:10:14 -0700183 store_.RegisterConstString(flimflam::kErrorProperty, &error_);
184 store_.RegisterConstBool(flimflam::kFavoriteProperty, &favorite_);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800185 HelpRegisterDerivedUint16(shill::kHTTPProxyPortProperty,
186 &Service::GetHTTPProxyPort,
187 NULL);
Paul Stewart10241e32012-04-23 18:15:06 -0700188 HelpRegisterDerivedRpcIdentifier(shill::kIPConfigProperty,
189 &Service::GetIPConfigRpcIdentifier,
190 NULL);
Chris Masone27c4aa52011-07-02 13:10:14 -0700191 HelpRegisterDerivedBool(flimflam::kIsActiveProperty,
Chris Masoneb2e326b2011-07-12 13:28:51 -0700192 &Service::IsActive,
193 NULL);
Chris Masone4d42df82011-07-02 17:09:39 -0700194 // flimflam::kModeProperty: Registered in WiFiService
Paul Stewart0c438332012-04-11 07:55:27 -0700195
196 // Although this is a read-only property, some callers want to blindly
197 // set this value to its current value.
198 HelpRegisterDerivedString(flimflam::kNameProperty,
199 &Service::GetNameProperty,
200 &Service::AssertTrivialSetNameProperty);
Chris Masone4d42df82011-07-02 17:09:39 -0700201 // flimflam::kPassphraseProperty: Registered in WiFiService
202 // flimflam::kPassphraseRequiredProperty: Registered in WiFiService
Chris Masone27c4aa52011-07-02 13:10:14 -0700203 store_.RegisterInt32(flimflam::kPriorityProperty, &priority_);
204 HelpRegisterDerivedString(flimflam::kProfileProperty,
205 &Service::GetProfileRpcId,
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800206 &Service::SetProfileRpcId);
Chris Masone4d42df82011-07-02 17:09:39 -0700207 store_.RegisterString(flimflam::kProxyConfigProperty, &proxy_config_);
Chris Masoneb2e326b2011-07-12 13:28:51 -0700208 store_.RegisterBool(flimflam::kSaveCredentialsProperty, &save_credentials_);
Gaurav Shah435de2c2011-11-17 19:01:07 -0800209 HelpRegisterDerivedString(flimflam::kTypeProperty,
Darin Petkov58f0b6d2012-06-12 12:52:30 +0200210 &Service::CalculateTechnology,
Gaurav Shah435de2c2011-11-17 19:01:07 -0800211 NULL);
Chris Masone4d42df82011-07-02 17:09:39 -0700212 // flimflam::kSecurityProperty: Registered in WiFiService
Chris Masone27c4aa52011-07-02 13:10:14 -0700213 HelpRegisterDerivedString(flimflam::kStateProperty,
214 &Service::CalculateState,
215 NULL);
Darin Petkovd78ee7e2012-01-12 11:21:10 +0100216 store_.RegisterConstUint8(flimflam::kSignalStrengthProperty, &strength_);
Paul Stewart987e71e2011-12-05 09:45:06 -0800217 store_.RegisterString(flimflam::kUIDataProperty, &ui_data_);
Chris Masone4d42df82011-07-02 17:09:39 -0700218 // flimflam::kWifiAuthMode: Registered in WiFiService
219 // flimflam::kWifiHiddenSsid: Registered in WiFiService
220 // flimflam::kWifiFrequency: Registered in WiFiService
221 // flimflam::kWifiPhyMode: Registered in WiFiService
222 // flimflam::kWifiHexSsid: Registered in WiFiService
Thieu Le48e6d6d2011-12-06 00:40:27 +0000223
224 metrics_->RegisterService(this);
225
Paul Stewart1062d9d2012-04-27 10:42:27 -0700226 static_ip_parameters_.PlumbPropertyStore(&store_);
227
Paul Stewartcb59fed2012-03-21 21:14:46 -0700228 IgnoreParameterForConfigure(flimflam::kTypeProperty);
Paul Stewart7f61e522012-03-22 11:13:45 -0700229 IgnoreParameterForConfigure(flimflam::kProfileProperty);
Paul Stewartcb59fed2012-03-21 21:14:46 -0700230
Ben Chanfad4a0b2012-04-18 15:49:59 -0700231 SLOG(Service, 2) << "Service initialized.";
Paul Stewart75897df2011-04-27 09:05:53 -0700232}
233
Thieu Le48e6d6d2011-12-06 00:40:27 +0000234Service::~Service() {
Darin Petkov9cd7ca12012-07-03 11:06:40 +0200235 LOG(INFO) << "Service destroyed: " << friendly_name_;
Thieu Le48e6d6d2011-12-06 00:40:27 +0000236 metrics_->DeregisterService(this);
237}
Paul Stewart75897df2011-04-27 09:05:53 -0700238
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000239void Service::AutoConnect() {
Darin Petkov3abc3be2012-06-27 10:48:23 +0200240 const char *reason = NULL;
241 if (IsAutoConnectable(&reason)) {
Darin Petkov9cd7ca12012-07-03 11:06:40 +0200242 LOG(INFO) << "Auto-connecting to " << friendly_name_;
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000243 Error error;
244 Connect(&error);
mukesh agrawal76d13882012-01-12 15:23:11 -0800245 } else {
Darin Petkov9cd7ca12012-07-03 11:06:40 +0200246 LOG(INFO) << "Suppressed autoconnect to " << friendly_name_ << " "
mukesh agrawalbf14e942012-03-02 14:36:34 -0800247 << "(" << reason << ")";
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000248 }
249}
250
mukesh agrawaladb68482012-01-17 16:31:51 -0800251void Service::Connect(Error */*error*/) {
252 explicitly_disconnected_ = false;
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400253 // clear any failure state from a previous connect attempt
254 SetState(kStateIdle);
mukesh agrawaladb68482012-01-17 16:31:51 -0800255}
256
257void Service::Disconnect(Error */*error*/) {
258 explicitly_disconnected_ = true;
259}
260
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500261void Service::ActivateCellularModem(const string &/*carrier*/,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500262 Error *error,
263 const ResultCallback &/*callback*/) {
264 Error::PopulateAndLog(error, Error::kNotSupported,
265 "Service doesn't support cellular modem activation.");
Darin Petkovc408e692011-08-17 13:47:15 -0700266}
267
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800268bool Service::IsActive(Error */*error*/) {
Paul Stewart03dba0b2011-08-22 16:32:45 -0700269 return state_ != kStateUnknown &&
270 state_ != kStateIdle &&
271 state_ != kStateFailure;
272}
273
274void Service::SetState(ConnectState state) {
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800275 LOG(INFO) << "In " << __func__ << "(): Service " << friendly_name_
276 << " state " << ConnectStateToString(state_) << " -> "
277 << ConnectStateToString(state);
mukesh agrawal7ec71312011-11-10 02:08:26 +0000278
Paul Stewart03dba0b2011-08-22 16:32:45 -0700279 if (state == state_) {
280 return;
281 }
282
283 state_ = state;
284 if (state != kStateFailure) {
285 failure_ = kFailureUnknown;
mukesh agrawal568b5c62012-02-28 14:44:47 -0800286 failed_time_ = 0;
Paul Stewart03dba0b2011-08-22 16:32:45 -0700287 }
mukesh agrawalcf24a242012-05-21 16:46:11 -0700288 if (state == kStateConnected) {
289 has_ever_connected_ = true;
290 SaveToProfile();
291 }
Paul Stewartf2d60912012-07-15 08:37:30 -0700292 UpdateErrorProperty();
Paul Stewart03dba0b2011-08-22 16:32:45 -0700293 manager_->UpdateService(this);
Thieu Le48e6d6d2011-12-06 00:40:27 +0000294 metrics_->NotifyServiceStateChanged(this, state);
Darin Petkov58f0b6d2012-06-12 12:52:30 +0200295 adaptor_->EmitStringChanged(flimflam::kStateProperty, GetStateString());
Paul Stewart03dba0b2011-08-22 16:32:45 -0700296}
297
298void Service::SetFailure(ConnectFailure failure) {
299 failure_ = failure;
mukesh agrawal568b5c62012-02-28 14:44:47 -0800300 failed_time_ = time(NULL);
Paul Stewartf2d60912012-07-15 08:37:30 -0700301 UpdateErrorProperty();
Paul Stewart03dba0b2011-08-22 16:32:45 -0700302 SetState(kStateFailure);
303}
304
mukesh agrawal568b5c62012-02-28 14:44:47 -0800305void Service::SetFailureSilent(ConnectFailure failure) {
306 // Note that order matters here, since SetState modifies |failure_| and
307 // |failed_time_|.
308 SetState(kStateIdle);
309 failure_ = failure;
Paul Stewartf2d60912012-07-15 08:37:30 -0700310 UpdateErrorProperty();
mukesh agrawal568b5c62012-02-28 14:44:47 -0800311 failed_time_ = time(NULL);
312}
313
Chris Masone6791a432011-07-12 13:23:19 -0700314string Service::GetRpcIdentifier() const {
Chris Masone3c3f6a12011-07-01 10:01:41 -0700315 return adaptor_->GetRpcIdentifier();
316}
317
Paul Stewartbba6a5b2011-11-02 18:45:59 -0700318bool Service::IsLoadableFrom(StoreInterface *storage) const {
319 return storage->ContainsGroup(GetStorageIdentifier());
320}
321
Chris Masone9d779932011-08-25 16:33:41 -0700322bool Service::Load(StoreInterface *storage) {
323 const string id = GetStorageIdentifier();
Chris Masone34af2182011-08-22 11:59:36 -0700324 if (!storage->ContainsGroup(id)) {
325 LOG(WARNING) << "Service is not available in the persistent store: " << id;
326 return false;
327 }
328 storage->GetBool(id, kStorageAutoConnect, &auto_connect_);
329 storage->GetString(id, kStorageCheckPortal, &check_portal_);
330 storage->GetBool(id, kStorageFavorite, &favorite_);
mukesh agrawalcf24a242012-05-21 16:46:11 -0700331 storage->GetString(id, kStorageGUID, &guid_);
332 storage->GetBool(id, kStorageHasEverConnected, &has_ever_connected_);
Chris Masone34af2182011-08-22 11:59:36 -0700333 storage->GetInt(id, kStoragePriority, &priority_);
334 storage->GetString(id, kStorageProxyConfig, &proxy_config_);
335 storage->GetBool(id, kStorageSaveCredentials, &save_credentials_);
Paul Stewart987e71e2011-12-05 09:45:06 -0800336 storage->GetString(id, kStorageUIData, &ui_data_);
Chris Masone34af2182011-08-22 11:59:36 -0700337
338 LoadEapCredentials(storage, id);
Paul Stewart1062d9d2012-04-27 10:42:27 -0700339 static_ip_parameters_.Load(storage, id);
Chris Masone34af2182011-08-22 11:59:36 -0700340 // TODO(petkov): Load these:
341
Chris Masone34af2182011-08-22 11:59:36 -0700342 // "Failure"
343 // "Modified"
344 // "LastAttempt"
Chris Masone34af2182011-08-22 11:59:36 -0700345
mukesh agrawaladb68482012-01-17 16:31:51 -0800346 explicitly_disconnected_ = false;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800347 favorite_ = true;
348
Chris Masone34af2182011-08-22 11:59:36 -0700349 return true;
Darin Petkovba40dd32011-07-11 20:06:39 -0700350}
351
Paul Stewart65512e12012-03-26 18:01:08 -0700352bool Service::Unload() {
Paul Stewart7fb09382012-07-18 17:32:27 -0700353 auto_connect_ = IsAutoConnectByDefault();
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800354 check_portal_ = kCheckPortalAuto;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800355 favorite_ = false;
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800356 priority_ = kPriorityNone;
357 proxy_config_ = "";
358 save_credentials_ = true;
359 ui_data_ = "";
360
361 UnloadEapCredentials();
Wade Guthrie9e0c2502012-04-19 15:26:40 -0700362 Error error; // Ignored.
363 Disconnect(&error);
Paul Stewart65512e12012-03-26 18:01:08 -0700364 return false;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800365}
366
Chris Masone9d779932011-08-25 16:33:41 -0700367bool Service::Save(StoreInterface *storage) {
368 const string id = GetStorageIdentifier();
Chris Masone34af2182011-08-22 11:59:36 -0700369
Darin Petkov58f0b6d2012-06-12 12:52:30 +0200370 storage->SetString(id, kStorageType, GetTechnologyString());
Paul Stewart2706aaf2011-12-14 16:44:04 -0800371
Chris Masone34af2182011-08-22 11:59:36 -0700372 // TODO(petkov): We could choose to simplify the saving code by removing most
373 // conditionals thus saving even default values.
Wade Guthrie4f28e8b2012-04-11 10:52:07 -0700374 storage->SetBool(id, kStorageAutoConnect, auto_connect_);
Chris Masone34af2182011-08-22 11:59:36 -0700375 if (check_portal_ == kCheckPortalAuto) {
376 storage->DeleteKey(id, kStorageCheckPortal);
377 } else {
378 storage->SetString(id, kStorageCheckPortal, check_portal_);
379 }
380 storage->SetBool(id, kStorageFavorite, favorite_);
mukesh agrawalcf24a242012-05-21 16:46:11 -0700381 SaveString(storage, id, kStorageGUID, guid_, false, true);
382 storage->SetBool(id, kStorageHasEverConnected, has_ever_connected_);
mukesh agrawald835b202011-10-07 15:26:47 -0700383 storage->SetString(id, kStorageName, friendly_name_);
Chris Masone34af2182011-08-22 11:59:36 -0700384 if (priority_ != kPriorityNone) {
385 storage->SetInt(id, kStoragePriority, priority_);
386 } else {
387 storage->DeleteKey(id, kStoragePriority);
388 }
Paul Stewart987e71e2011-12-05 09:45:06 -0800389 SaveString(storage, id, kStorageProxyConfig, proxy_config_, false, true);
Chris Masone34af2182011-08-22 11:59:36 -0700390 if (save_credentials_) {
391 storage->DeleteKey(id, kStorageSaveCredentials);
392 } else {
393 storage->SetBool(id, kStorageSaveCredentials, false);
394 }
Paul Stewart987e71e2011-12-05 09:45:06 -0800395 SaveString(storage, id, kStorageUIData, ui_data_, false, true);
Chris Masone34af2182011-08-22 11:59:36 -0700396
397 SaveEapCredentials(storage, id);
Paul Stewart1062d9d2012-04-27 10:42:27 -0700398 static_ip_parameters_.Save(storage, id);
Chris Masone34af2182011-08-22 11:59:36 -0700399
400 // TODO(petkov): Save these:
401
Chris Masone34af2182011-08-22 11:59:36 -0700402 // "Failure"
403 // "Modified"
404 // "LastAttempt"
Chris Masone34af2182011-08-22 11:59:36 -0700405
406 return true;
407}
408
Gary Moraind93615e2012-04-27 11:50:03 -0700409void Service::SaveToCurrentProfile() {
410 // Some unittests do not specify a manager.
411 if (manager()) {
412 manager()->SaveServiceToProfile(this);
413 }
Thieu Led4e9e552012-02-16 16:26:07 -0800414}
415
Paul Stewartcb59fed2012-03-21 21:14:46 -0700416void Service::Configure(const KeyValueStore &args, Error *error) {
417 map<string, bool>::const_iterator bool_it;
Ben Chanfad4a0b2012-04-18 15:49:59 -0700418 SLOG(Service, 5) << "Configuring bool properties:";
Paul Stewartcb59fed2012-03-21 21:14:46 -0700419 for (bool_it = args.bool_properties().begin();
420 bool_it != args.bool_properties().end();
421 ++bool_it) {
422 if (ContainsKey(parameters_ignored_for_configure_, bool_it->first)) {
423 continue;
424 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700425 SLOG(Service, 5) << " " << bool_it->first;
Paul Stewartcb59fed2012-03-21 21:14:46 -0700426 Error set_error;
427 store_.SetBoolProperty(bool_it->first, bool_it->second, &set_error);
428 if (error->IsSuccess() && set_error.IsFailure()) {
429 error->CopyFrom(set_error);
430 }
431 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700432 SLOG(Service, 5) << "Configuring string properties:";
Paul Stewartcb59fed2012-03-21 21:14:46 -0700433 map<string, string>::const_iterator string_it;
434 for (string_it = args.string_properties().begin();
435 string_it != args.string_properties().end();
436 ++string_it) {
437 if (ContainsKey(parameters_ignored_for_configure_, string_it->first)) {
438 continue;
439 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700440 SLOG(Service, 5) << " " << string_it->first;
Paul Stewartcb59fed2012-03-21 21:14:46 -0700441 Error set_error;
442 store_.SetStringProperty(string_it->first, string_it->second, &set_error);
443 if (error->IsSuccess() && set_error.IsFailure()) {
444 error->CopyFrom(set_error);
445 }
446 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700447 SLOG(Service, 5) << "Configuring uint32 properties:";
Paul Stewartcb59fed2012-03-21 21:14:46 -0700448 map<string, uint32>::const_iterator int_it;
449 for (int_it = args.uint_properties().begin();
450 int_it != args.uint_properties().end();
451 ++int_it) {
452 if (ContainsKey(parameters_ignored_for_configure_, int_it->first)) {
453 continue;
454 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700455 SLOG(Service, 5) << " " << int_it->first;
Paul Stewartcb59fed2012-03-21 21:14:46 -0700456 Error set_error;
457 store_.SetUint32Property(int_it->first, int_it->second, &set_error);
458 if (error->IsSuccess() && set_error.IsFailure()) {
459 error->CopyFrom(set_error);
460 }
461 }
462}
463
Paul Stewart10ccbb32012-04-26 15:59:30 -0700464bool Service::IsRemembered() const {
465 return profile_ && !manager_->IsServiceEphemeral(this);
466}
467
mukesh agrawal00917ce2011-11-22 23:56:55 +0000468void Service::MakeFavorite() {
469 if (favorite_) {
470 // We do not want to clobber the value of auto_connect_ (it may
471 // be user-set). So return early.
472 return;
473 }
474
475 auto_connect_ = true;
476 favorite_ = true;
477}
478
Darin Petkov5eb05422012-05-11 15:45:25 +0200479void Service::SetConnection(const ConnectionRefPtr &connection) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800480 if (connection.get()) {
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800481 http_proxy_.reset(new HTTPProxy(connection));
Darin Petkov5eb05422012-05-11 15:45:25 +0200482 http_proxy_->Start(dispatcher_, sockets_.get());
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800483 } else {
484 http_proxy_.reset();
Paul Stewartdef189e2012-08-02 20:12:09 -0700485 static_ip_parameters_.ClearSavedParameters();
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800486 }
487 connection_ = connection;
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800488}
489
Gaurav Shah10109f22011-11-11 20:16:22 -0800490bool Service::Is8021xConnectable() const {
491 // We mirror all the flimflam checks (see service.c:is_connectable()).
492
493 // Identity is required.
494 if (eap_.identity.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700495 SLOG(Service, 2) << "Not connectable: Identity is empty.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800496 return false;
497 }
498
499 if (!eap_.client_cert.empty() || !eap_.cert_id.empty()) {
500 // If a client certificate is being used, we must have a private key.
501 if (eap_.private_key.empty() && eap_.key_id.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700502 SLOG(Service, 2)
503 << "Not connectable. Client certificate but no private key.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800504 return false;
505 }
506 }
507 if (!eap_.cert_id.empty() || !eap_.key_id.empty() ||
508 !eap_.ca_cert_id.empty()) {
509 // If PKCS#11 data is needed, a PIN is required.
510 if (eap_.pin.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700511 SLOG(Service, 2) << "Not connectable. PKCS#11 data but no PIN.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800512 return false;
513 }
514 }
515
516 // For EAP-TLS, a client certificate is required.
517 if (eap_.eap.empty() || eap_.eap == "TLS") {
Paul Stewart81426132012-05-16 10:05:10 -0700518 if ((!eap_.client_cert.empty() || !eap_.cert_id.empty()) &&
519 (!eap_.private_key.empty() || !eap_.key_id.empty())) {
520 SLOG(Service, 2) << "Connectable. EAP-TLS with a client cert and key.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800521 return true;
522 }
523 }
524
525 // For EAP types other than TLS (e.g. EAP-TTLS or EAP-PEAP, password is the
526 // minimum requirement), at least an identity + password is required.
527 if (eap_.eap.empty() || eap_.eap != "TLS") {
528 if (!eap_.password.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700529 SLOG(Service, 2) << "Connectable. !EAP-TLS and has a password.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800530 return true;
531 }
532 }
533
Ben Chanfad4a0b2012-04-18 15:49:59 -0700534 SLOG(Service, 2)
535 << "Not connectable. No suitable EAP configuration was found.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800536 return false;
537}
538
Paul Stewartbc6e7392012-05-24 07:07:48 -0700539bool Service::AddEAPCertification(const string &name, size_t depth) {
540 if (depth >= kEAPMaxCertificationElements) {
541 LOG(WARNING) << "Ignoring certification " << name
542 << " because depth " << depth
543 << " exceeds our maximum of "
544 << kEAPMaxCertificationElements;
545 return false;
546 }
547
548 if (depth >= eap_.remote_certification.size()) {
549 eap_.remote_certification.resize(depth + 1);
550 } else if (name == eap_.remote_certification[depth]) {
551 return true;
552 }
553
554 eap_.remote_certification[depth] = name;
555 LOG(INFO) << "Received certification for "
556 << name
557 << " at depth "
558 << depth;
559 return true;
560}
561
562void Service::ClearEAPCertification() {
563 eap_.remote_certification.clear();
564}
565
Gaurav Shah10109f22011-11-11 20:16:22 -0800566void Service::set_eap(const EapCredentials &eap) {
567 eap_ = eap;
568 // Note: Connectability can only be updated by a subclass of Service
569 // with knowledge of whether the service actually uses 802.1x credentials.
570}
571
mukesh agrawal00917ce2011-11-22 23:56:55 +0000572// static
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800573const char *Service::ConnectFailureToString(const ConnectFailure &state) {
574 switch (state) {
575 case kFailureUnknown:
576 return "Unknown";
Paul Stewartf2d60912012-07-15 08:37:30 -0700577 case kFailureAAA:
578 return flimflam::kErrorAaaFailed;
579 case kFailureActivation:
580 return flimflam::kErrorActivationFailed;
581 case kFailureBadPassphrase:
582 return flimflam::kErrorBadPassphrase;
583 case kFailureBadWEPKey:
584 return flimflam::kErrorBadWEPKey;
585 case kFailureConnect:
586 return flimflam::kErrorConnectFailed;
587 case kFailureDNSLookup:
588 return flimflam::kErrorDNSLookupFailed;
589 case kFailureDHCP:
590 return flimflam::kErrorDhcpFailed;
591 case kFailureHTTPGet:
592 return flimflam::kErrorHTTPGetFailed;
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800593 case kFailureNeedEVDO:
Paul Stewartf2d60912012-07-15 08:37:30 -0700594 return flimflam::kErrorNeedEvdo;
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800595 case kFailureNeedHomeNetwork:
Paul Stewartf2d60912012-07-15 08:37:30 -0700596 return flimflam::kErrorNeedHomeNetwork;
597 case kFailureOTASP:
598 return flimflam::kErrorOtaspFailed;
599 case kFailureOutOfRange:
600 return flimflam::kErrorOutOfRange;
601 case kFailurePinMissing:
602 return flimflam::kErrorPinMissing;
603 case kFailurePPPAuth:
604 return flimflam::kErrorPppAuthFailed;
Thieu Le48e6d6d2011-12-06 00:40:27 +0000605 case kFailureMax:
606 return "Max failure error code";
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800607 }
608 return "Invalid";
609}
610
611// static
612const char *Service::ConnectStateToString(const ConnectState &state) {
613 switch (state) {
614 case kStateUnknown:
615 return "Unknown";
616 case kStateIdle:
617 return "Idle";
618 case kStateAssociating:
619 return "Associating";
620 case kStateConfiguring:
621 return "Configuring";
622 case kStateConnected:
623 return "Connected";
624 case kStateDisconnected:
625 return "Disconnected";
Thieu Le48e6d6d2011-12-06 00:40:27 +0000626 case kStatePortal:
627 return "Portal";
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800628 case kStateFailure:
629 return "Failure";
630 case kStateOnline:
631 return "Online";
632 }
633 return "Invalid";
634}
635
Darin Petkov58f0b6d2012-06-12 12:52:30 +0200636string Service::GetTechnologyString() const {
Gaurav Shah435de2c2011-11-17 19:01:07 -0800637 return Technology::NameFromIdentifier(technology());
638}
639
Darin Petkov58f0b6d2012-06-12 12:52:30 +0200640string Service::CalculateTechnology(Error */*error*/) {
641 return GetTechnologyString();
642}
643
Jason Glasgowb5790052012-01-27 01:03:52 -0500644// static
Paul Stewart22aa71b2011-09-16 12:15:11 -0700645bool Service::DecideBetween(int a, int b, bool *decision) {
646 if (a == b)
647 return false;
648 *decision = (a > b);
649 return true;
650}
651
mukesh agrawal00917ce2011-11-22 23:56:55 +0000652// static
Paul Stewart22aa71b2011-09-16 12:15:11 -0700653bool Service::Compare(ServiceRefPtr a,
654 ServiceRefPtr b,
mukesh agrawalddc378f2012-02-17 18:26:20 -0800655 const vector<Technology::Identifier> &tech_order,
656 const char **reason) {
Paul Stewart22aa71b2011-09-16 12:15:11 -0700657 bool ret;
658
659 if (a->state() != b->state()) {
660 if (DecideBetween(a->IsConnected(), b->IsConnected(), &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800661 *reason = kServiceSortIsConnected;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700662 return ret;
663 }
664
Paul Stewarta121c442012-06-09 14:12:58 -0700665 if (DecideBetween(!a->IsPortalled(), !b->IsPortalled(), &ret)) {
666 *reason = kServiceSortIsPortalled;
667 return ret;
668 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700669
670 if (DecideBetween(a->IsConnecting(), b->IsConnecting(), &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800671 *reason = kServiceSortIsConnecting;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700672 return ret;
673 }
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000674
675 if (DecideBetween(!a->IsFailed(), !b->IsFailed(), &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800676 *reason = kServiceSortIsFailed;
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000677 return ret;
678 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700679 }
680
Darin Petkov2f903b32012-04-18 12:56:43 +0200681 if (DecideBetween(a->connectable(), b->connectable(), &ret)) {
682 *reason = kServiceSortConnectable;
683 return ret;
684 }
685
686 // Ignore the auto-connect property if both services are connected
687 // already. This allows connected non-autoconnectable VPN services to be
688 // sorted higher than other connected services based on technology order.
689 if (!a->IsConnected() &&
690 DecideBetween(a->auto_connect(), b->auto_connect(), &ret)) {
691 *reason = kServiceSortAutoConnect;
692 return ret;
693 }
694
695 if (DecideBetween(a->favorite(), b->favorite(), &ret)) {
696 *reason = kServiceSortFavorite;
697 return ret;
698 }
699
700 if (DecideBetween(a->priority(), b->priority(), &ret)) {
701 *reason = kServiceSortPriority;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700702 return ret;
703 }
704
705 // TODO(pstew): Below this point we are making value judgements on
706 // services that are not related to anything intrinsic or
707 // user-specified. These heuristics should be richer (contain
708 // historical information, for example) and be subject to user
709 // customization.
Paul Stewart22aa71b2011-09-16 12:15:11 -0700710 for (vector<Technology::Identifier>::const_iterator it = tech_order.begin();
711 it != tech_order.end();
712 ++it) {
Joshua Kroll053fa822012-06-05 09:50:43 -0700713 if (DecideBetween(a->technology() == *it, b->technology() == *it, &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800714 *reason = kServiceSortTechnology;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700715 return ret;
mukesh agrawalddc378f2012-02-17 18:26:20 -0800716 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700717 }
718
Paul Stewart1ca3e852011-11-04 07:50:49 -0700719 if (DecideBetween(a->security_level(), b->security_level(), &ret) ||
Paul Stewart22aa71b2011-09-16 12:15:11 -0700720 DecideBetween(a->strength(), b->strength(), &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800721 *reason = kServiceSortSecurityEtc;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700722 return ret;
723 }
724
mukesh agrawalddc378f2012-02-17 18:26:20 -0800725 *reason = kServiceSortUniqueName;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700726 return a->UniqueName() < b->UniqueName();
727}
728
Chris Masone34af2182011-08-22 11:59:36 -0700729const ProfileRefPtr &Service::profile() const { return profile_; }
730
731void Service::set_profile(const ProfileRefPtr &p) { profile_ = p; }
732
Paul Stewartff14b022012-04-24 20:06:23 -0700733void Service::OnPropertyChanged(const string &property) {
Paul Stewart81426132012-05-16 10:05:10 -0700734 if (Is8021x() &&
735 (property == flimflam::kEAPCertIDProperty ||
736 property == flimflam::kEAPClientCertProperty ||
737 property == flimflam::kEAPKeyIDProperty ||
738 property == flimflam::kEAPPINProperty ||
739 property == flimflam::kEapCaCertIDProperty ||
740 property == flimflam::kEapIdentityProperty ||
Paul Stewartadf79d82012-07-18 16:09:56 -0700741 property == flimflam::kEapKeyMgmtProperty ||
Paul Stewart81426132012-05-16 10:05:10 -0700742 property == flimflam::kEapPasswordProperty ||
743 property == flimflam::kEapPrivateKeyProperty)) {
744 // This notifies subclassess that EAP parameters have been changed.
745 set_eap(eap_);
746 }
mukesh agrawalcf24a242012-05-21 16:46:11 -0700747 SaveToProfile();
Paul Stewartd215af62012-04-24 23:25:50 -0700748 if ((property == flimflam::kCheckPortalProperty ||
749 property == flimflam::kProxyConfigProperty) &&
750 (state_ == kStateConnected ||
751 state_ == kStatePortal ||
752 state_ == kStateOnline)) {
753 manager_->RecheckPortalOnService(this);
754 }
Paul Stewartff14b022012-04-24 20:06:23 -0700755}
756
Paul Stewart10241e32012-04-23 18:15:06 -0700757string Service::GetIPConfigRpcIdentifier(Error *error) {
758 if (!connection_) {
759 error->Populate(Error::kNotFound);
760 return "/";
761 }
762
763 string id = connection_->ipconfig_rpc_identifier();
764
765 if (id.empty()) {
766 // Do not return an empty IPConfig.
767 error->Populate(Error::kNotFound);
768 return "/";
769 }
770
771 return id;
772}
773
mukesh agrawal29c13a12011-11-24 00:09:19 +0000774void Service::set_connectable(bool connectable) {
775 connectable_ = connectable;
776 adaptor_->EmitBoolChanged(flimflam::kConnectableProperty, connectable_);
777}
778
Darin Petkovb2ba39f2012-06-06 10:33:43 +0200779void Service::SetConnectable(bool connectable) {
780 if (connectable_ == connectable) {
781 return;
782 }
783 connectable_ = connectable;
784 adaptor_->EmitBoolChanged(flimflam::kConnectableProperty, connectable_);
785 if (manager_->HasService(this)) {
786 manager_->UpdateService(this);
787 }
788}
789
Darin Petkov58f0b6d2012-06-12 12:52:30 +0200790string Service::GetStateString() const {
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700791 switch (state_) {
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800792 case kStateIdle:
793 return flimflam::kStateIdle;
794 case kStateAssociating:
795 return flimflam::kStateAssociation;
796 case kStateConfiguring:
797 return flimflam::kStateConfiguration;
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700798 case kStateConnected:
Paul Stewart20088d82012-02-16 06:58:55 -0800799 return flimflam::kStateReady;
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800800 case kStateDisconnected:
801 return flimflam::kStateDisconnect;
802 case kStateFailure:
803 return flimflam::kStateFailure;
Paul Stewart20088d82012-02-16 06:58:55 -0800804 case kStatePortal:
805 return flimflam::kStatePortal;
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800806 case kStateOnline:
807 return flimflam::kStateOnline;
808 case kStateUnknown:
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700809 default:
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800810 return "";
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700811 }
812}
813
Darin Petkov58f0b6d2012-06-12 12:52:30 +0200814string Service::CalculateState(Error */*error*/) {
815 return GetStateString();
816}
817
mukesh agrawalbf14e942012-03-02 14:36:34 -0800818bool Service::IsAutoConnectable(const char **reason) const {
819 if (!connectable()) {
820 *reason = kAutoConnNotConnectable;
821 return false;
822 }
823
824 if (IsConnected()) {
825 *reason = kAutoConnConnected;
826 return false;
827 }
828
829 if (IsConnecting()) {
830 *reason = kAutoConnConnecting;
831 return false;
832 }
833
834 if (explicitly_disconnected_) {
835 *reason = kAutoConnExplicitDisconnect;
836 return false;
837 }
838
839 return true;
mukesh agrawal76d13882012-01-12 15:23:11 -0800840}
841
Paul Stewartd215af62012-04-24 23:25:50 -0700842bool Service::IsPortalDetectionDisabled() const {
843 return check_portal_ == kCheckPortalFalse;
844}
845
846bool Service::IsPortalDetectionAuto() const {
847 return check_portal_ == kCheckPortalAuto;
848}
849
mukesh agrawalffa3d042011-10-06 15:26:10 -0700850void Service::HelpRegisterDerivedBool(
851 const string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800852 bool(Service::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700853 void(Service::*set)(const bool&, Error *)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700854 store_.RegisterDerivedBool(
855 name,
856 BoolAccessor(new CustomAccessor<Service, bool>(this, get, set)));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700857}
858
mukesh agrawalffa3d042011-10-06 15:26:10 -0700859void Service::HelpRegisterDerivedString(
860 const string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800861 string(Service::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700862 void(Service::*set)(const string&, Error *)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700863 store_.RegisterDerivedString(
864 name,
865 StringAccessor(new CustomAccessor<Service, string>(this, get, set)));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700866}
867
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400868void Service::HelpRegisterDerivedRpcIdentifier(
869 const string &name,
870 RpcIdentifier(Service::*get)(Error *),
871 void(Service::*set)(const RpcIdentifier&, Error *)) {
872 store_.RegisterDerivedRpcIdentifier(
873 name,
874 RpcIdentifierAccessor(new CustomAccessor<Service, RpcIdentifier>(
875 this, get, set)));
876}
877
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800878void Service::HelpRegisterDerivedUint16(
879 const string &name,
880 uint16(Service::*get)(Error *),
881 void(Service::*set)(const uint16&, Error *)) {
882 store_.RegisterDerivedUint16(
883 name,
884 Uint16Accessor(new CustomAccessor<Service, uint16>(this, get, set)));
885}
886
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800887void Service::HelpRegisterWriteOnlyDerivedString(
888 const string &name,
889 void(Service::*set)(const string &, Error *),
890 void(Service::*clear)(Error *),
891 const string *default_value) {
892 store_.RegisterDerivedString(
893 name,
894 StringAccessor(
895 new CustomWriteOnlyAccessor<Service, string>(
896 this, set, clear, default_value)));
897}
898
Darin Petkovba40dd32011-07-11 20:06:39 -0700899void Service::SaveString(StoreInterface *storage,
Chris Masone34af2182011-08-22 11:59:36 -0700900 const string &id,
Darin Petkovba40dd32011-07-11 20:06:39 -0700901 const string &key,
902 const string &value,
903 bool crypted,
904 bool save) {
905 if (value.empty() || !save) {
Chris Masone34af2182011-08-22 11:59:36 -0700906 storage->DeleteKey(id, key);
Darin Petkovba40dd32011-07-11 20:06:39 -0700907 return;
908 }
909 if (crypted) {
Chris Masone34af2182011-08-22 11:59:36 -0700910 storage->SetCryptedString(id, key, value);
Darin Petkovba40dd32011-07-11 20:06:39 -0700911 return;
912 }
Chris Masone34af2182011-08-22 11:59:36 -0700913 storage->SetString(id, key, value);
Darin Petkovba40dd32011-07-11 20:06:39 -0700914}
915
Chris Masone34af2182011-08-22 11:59:36 -0700916void Service::LoadEapCredentials(StoreInterface *storage, const string &id) {
Gaurav Shah10109f22011-11-11 20:16:22 -0800917 EapCredentials eap;
918 storage->GetCryptedString(id, kStorageEapIdentity, &eap.identity);
919 storage->GetString(id, kStorageEapEap, &eap.eap);
920 storage->GetString(id, kStorageEapInnerEap, &eap.inner_eap);
Darin Petkovba40dd32011-07-11 20:06:39 -0700921 storage->GetCryptedString(id,
922 kStorageEapAnonymousIdentity,
Gaurav Shah10109f22011-11-11 20:16:22 -0800923 &eap.anonymous_identity);
924 storage->GetString(id, kStorageEapClientCert, &eap.client_cert);
925 storage->GetString(id, kStorageEapCertID, &eap.cert_id);
926 storage->GetString(id, kStorageEapPrivateKey, &eap.private_key);
Darin Petkovba40dd32011-07-11 20:06:39 -0700927 storage->GetCryptedString(id,
928 kStorageEapPrivateKeyPassword,
Gaurav Shah10109f22011-11-11 20:16:22 -0800929 &eap.private_key_password);
930 storage->GetString(id, kStorageEapKeyID, &eap.key_id);
931 storage->GetString(id, kStorageEapCACert, &eap.ca_cert);
932 storage->GetString(id, kStorageEapCACertID, &eap.ca_cert_id);
Paul Stewartecf4cd12012-04-17 11:08:39 -0700933 storage->GetString(id, kStorageEapCACertNSS, &eap.ca_cert_nss);
Gaurav Shah10109f22011-11-11 20:16:22 -0800934 storage->GetBool(id, kStorageEapUseSystemCAs, &eap.use_system_cas);
935 storage->GetString(id, kStorageEapPIN, &eap.pin);
936 storage->GetCryptedString(id, kStorageEapPassword, &eap.password);
937 storage->GetString(id, kStorageEapKeyManagement, &eap.key_management);
938 set_eap(eap);
Darin Petkovba40dd32011-07-11 20:06:39 -0700939}
940
Chris Masone34af2182011-08-22 11:59:36 -0700941void Service::SaveEapCredentials(StoreInterface *storage, const string &id) {
Darin Petkovba40dd32011-07-11 20:06:39 -0700942 bool save = save_credentials_;
Chris Masone34af2182011-08-22 11:59:36 -0700943 SaveString(storage, id, kStorageEapIdentity, eap_.identity, true, save);
944 SaveString(storage, id, kStorageEapEap, eap_.eap, false, true);
945 SaveString(storage, id, kStorageEapInnerEap, eap_.inner_eap, false, true);
Darin Petkovba40dd32011-07-11 20:06:39 -0700946 SaveString(storage,
Chris Masone34af2182011-08-22 11:59:36 -0700947 id,
Darin Petkovba40dd32011-07-11 20:06:39 -0700948 kStorageEapAnonymousIdentity,
949 eap_.anonymous_identity,
950 true,
951 save);
Chris Masone34af2182011-08-22 11:59:36 -0700952 SaveString(storage, id, kStorageEapClientCert, eap_.client_cert, false, save);
953 SaveString(storage, id, kStorageEapCertID, eap_.cert_id, false, save);
954 SaveString(storage, id, kStorageEapPrivateKey, eap_.private_key, false, save);
Darin Petkovba40dd32011-07-11 20:06:39 -0700955 SaveString(storage,
Chris Masone34af2182011-08-22 11:59:36 -0700956 id,
Darin Petkovba40dd32011-07-11 20:06:39 -0700957 kStorageEapPrivateKeyPassword,
958 eap_.private_key_password,
959 true,
960 save);
Chris Masone34af2182011-08-22 11:59:36 -0700961 SaveString(storage, id, kStorageEapKeyID, eap_.key_id, false, save);
962 SaveString(storage, id, kStorageEapCACert, eap_.ca_cert, false, true);
963 SaveString(storage, id, kStorageEapCACertID, eap_.ca_cert_id, false, true);
Paul Stewartecf4cd12012-04-17 11:08:39 -0700964 SaveString(storage, id, kStorageEapCACertNSS, eap_.ca_cert_nss, false, true);
Chris Masone34af2182011-08-22 11:59:36 -0700965 storage->SetBool(id, kStorageEapUseSystemCAs, eap_.use_system_cas);
966 SaveString(storage, id, kStorageEapPIN, eap_.pin, false, save);
967 SaveString(storage, id, kStorageEapPassword, eap_.password, true, save);
Darin Petkovba40dd32011-07-11 20:06:39 -0700968 SaveString(storage,
Chris Masone34af2182011-08-22 11:59:36 -0700969 id,
Darin Petkovba40dd32011-07-11 20:06:39 -0700970 kStorageEapKeyManagement,
971 eap_.key_management,
972 false,
973 true);
974}
975
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800976void Service::UnloadEapCredentials() {
977 eap_.identity = "";
978 eap_.eap = "";
979 eap_.inner_eap = "";
980 eap_.anonymous_identity = "";
981 eap_.client_cert = "";
982 eap_.cert_id = "";
983 eap_.private_key = "";
984 eap_.private_key_password = "";
985 eap_.key_id = "";
986 eap_.ca_cert = "";
987 eap_.ca_cert_id = "";
Paul Stewart20550982012-04-16 12:16:11 -0700988 eap_.use_system_cas = true;
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800989 eap_.pin = "";
990 eap_.password = "";
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800991}
992
Paul Stewartcb59fed2012-03-21 21:14:46 -0700993void Service::IgnoreParameterForConfigure(const string &parameter) {
994 parameters_ignored_for_configure_.insert(parameter);
995}
996
Paul Stewartac4ac002011-08-26 12:04:26 -0700997const string &Service::GetEAPKeyManagement() const {
998 return eap_.key_management;
999}
1000
1001void Service::SetEAPKeyManagement(const string &key_management) {
1002 eap_.key_management = key_management;
1003}
1004
Thieu Le284fe792012-01-31 17:53:19 -08001005bool Service::GetAutoConnect(Error */*error*/) {
1006 return auto_connect();
1007}
1008
1009void Service::SetAutoConnect(const bool &connect, Error *error) {
Wade Guthrie4f28e8b2012-04-11 10:52:07 -07001010 set_auto_connect(connect);
Thieu Le284fe792012-01-31 17:53:19 -08001011}
1012
Paul Stewartd215af62012-04-24 23:25:50 -07001013string Service::GetCheckPortal(Error *error) {
1014 return check_portal_;
1015}
1016
1017void Service::SetCheckPortal(const string &check_portal, Error *error) {
1018 if (check_portal == check_portal_) {
1019 return;
1020 }
1021 if (check_portal != kCheckPortalFalse &&
1022 check_portal != kCheckPortalTrue &&
1023 check_portal != kCheckPortalAuto) {
1024 Error::PopulateAndLog(error, Error::kInvalidArguments,
1025 base::StringPrintf(
1026 "Invalid Service CheckPortal property value: %s",
1027 check_portal.c_str()));
1028 return;
1029 }
1030 check_portal_ = check_portal;
1031}
1032
Paul Stewart9f32d192012-01-30 20:37:50 -08001033void Service::SetEAPPassword(const string &password, Error */*error*/) {
1034 eap_.password = password;
1035}
1036
1037void Service::SetEAPPrivateKeyPassword(const string &password,
1038 Error */*error*/) {
1039 eap_.private_key_password = password;
1040}
1041
Paul Stewart0c438332012-04-11 07:55:27 -07001042string Service::GetNameProperty(Error *error) {
1043 return friendly_name_;
1044}
1045
1046void Service::AssertTrivialSetNameProperty(const string &name, Error *error) {
1047 if (name != friendly_name_) {
1048 Error::PopulateAndLog(error, Error::kInvalidArguments,
Paul Stewart2bf424f2012-04-11 18:59:39 -07001049 base::StringPrintf(
1050 "Service Name property cannot be modified "
1051 "(%s to %s)", friendly_name_.c_str(),
1052 name.c_str()));
Paul Stewart0c438332012-04-11 07:55:27 -07001053 }
1054}
1055
Paul Stewart1b1a7f22012-01-06 16:24:06 -08001056string Service::GetProfileRpcId(Error *error) {
1057 if (!profile_) {
1058 // This happens in some unit tests where profile_ is not set.
1059 error->Populate(Error::kNotFound);
1060 return "";
1061 }
1062 return profile_->GetRpcIdentifier();
1063}
1064
1065void Service::SetProfileRpcId(const string &profile, Error *error) {
1066 manager_->SetProfileForService(this, profile, error);
1067}
1068
Paul Stewartbe5f5b32011-12-07 17:11:11 -08001069uint16 Service::GetHTTPProxyPort(Error */*error*/) {
1070 if (http_proxy_.get()) {
1071 return static_cast<uint16>(http_proxy_->proxy_port());
1072 }
1073 return 0;
1074}
1075
mukesh agrawalcf24a242012-05-21 16:46:11 -07001076void Service::SaveToProfile() {
1077 if (profile_.get() && profile_->GetConstStorage()) {
1078 profile_->UpdateService(this);
1079 }
1080}
1081
Darin Petkovd78ee7e2012-01-12 11:21:10 +01001082void Service::SetStrength(uint8 strength) {
1083 if (strength == strength_) {
1084 return;
1085 }
1086 strength_ = strength;
1087 adaptor_->EmitUint8Changed(flimflam::kSignalStrengthProperty, strength);
1088}
1089
Paul Stewartf2d60912012-07-15 08:37:30 -07001090void Service::UpdateErrorProperty() {
1091 const string error(ConnectFailureToString(failure_));
1092 if (error == error_) {
1093 return;
1094 }
1095 error_ = error;
1096 adaptor_->EmitStringChanged(flimflam::kErrorProperty, error);
1097}
1098
Paul Stewart75897df2011-04-27 09:05:53 -07001099} // namespace shill