blob: 2cf2e3f7f1248e177b290a7e54b029db73d964aa [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 Masone6791a432011-07-12 13:23:19 -0700124 adaptor_(control_interface->CreateServiceAdaptor(this)),
Thieu Le3426c8f2012-01-11 17:35:11 -0800125 metrics_(metrics),
Darin Petkov5eb05422012-05-11 15:45:25 +0200126 manager_(manager),
127 sockets_(new Sockets()) {
Thieu Le284fe792012-01-31 17:53:19 -0800128 HelpRegisterDerivedBool(flimflam::kAutoConnectProperty,
129 &Service::GetAutoConnect,
130 &Service::SetAutoConnect);
Chris Masone4d42df82011-07-02 17:09:39 -0700131
132 // flimflam::kActivationStateProperty: Registered in CellularService
133 // flimflam::kCellularApnProperty: Registered in CellularService
134 // flimflam::kCellularLastGoodApnProperty: Registered in CellularService
135 // flimflam::kNetworkTechnologyProperty: Registered in CellularService
Darin Petkov3335b372011-08-22 11:05:32 -0700136 // flimflam::kOperatorNameProperty: DEPRECATED
137 // flimflam::kOperatorCodeProperty: DEPRECATED
Chris Masone4d42df82011-07-02 17:09:39 -0700138 // flimflam::kRoamingStateProperty: Registered in CellularService
Darin Petkov3335b372011-08-22 11:05:32 -0700139 // flimflam::kServingOperatorProperty: Registered in CellularService
Chris Masone4d42df82011-07-02 17:09:39 -0700140 // flimflam::kPaymentURLProperty: Registered in CellularService
141
Paul Stewartd215af62012-04-24 23:25:50 -0700142 HelpRegisterDerivedString(flimflam::kCheckPortalProperty,
143 &Service::GetCheckPortal,
144 &Service::SetCheckPortal);
Chris Masone27c4aa52011-07-02 13:10:14 -0700145 store_.RegisterConstBool(flimflam::kConnectableProperty, &connectable_);
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400146 HelpRegisterDerivedRpcIdentifier(flimflam::kDeviceProperty,
147 &Service::GetDeviceRpcId,
148 NULL);
Paul Stewart4c561612012-03-21 12:49:01 -0700149 store_.RegisterString(flimflam::kGuidProperty, &guid_);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700150
Chris Masoneb2e326b2011-07-12 13:28:51 -0700151 store_.RegisterString(flimflam::kEapIdentityProperty, &eap_.identity);
152 store_.RegisterString(flimflam::kEAPEAPProperty, &eap_.eap);
153 store_.RegisterString(flimflam::kEapPhase2AuthProperty, &eap_.inner_eap);
Chris Masone27c4aa52011-07-02 13:10:14 -0700154 store_.RegisterString(flimflam::kEapAnonymousIdentityProperty,
Chris Masoneb2e326b2011-07-12 13:28:51 -0700155 &eap_.anonymous_identity);
156 store_.RegisterString(flimflam::kEAPClientCertProperty, &eap_.client_cert);
157 store_.RegisterString(flimflam::kEAPCertIDProperty, &eap_.cert_id);
158 store_.RegisterString(flimflam::kEapPrivateKeyProperty, &eap_.private_key);
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800159 HelpRegisterWriteOnlyDerivedString(flimflam::kEapPrivateKeyPasswordProperty,
160 &Service::SetEAPPrivateKeyPassword,
161 NULL,
162 &eap_.private_key_password);
Chris Masoneb2e326b2011-07-12 13:28:51 -0700163 store_.RegisterString(flimflam::kEAPKeyIDProperty, &eap_.key_id);
164 store_.RegisterString(flimflam::kEapCaCertProperty, &eap_.ca_cert);
165 store_.RegisterString(flimflam::kEapCaCertIDProperty, &eap_.ca_cert_id);
Paul Stewartecf4cd12012-04-17 11:08:39 -0700166 store_.RegisterString(flimflam::kEapCaCertNssProperty, &eap_.ca_cert_nss);
Chris Masoneb2e326b2011-07-12 13:28:51 -0700167 store_.RegisterString(flimflam::kEAPPINProperty, &eap_.pin);
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800168 HelpRegisterWriteOnlyDerivedString(flimflam::kEapPasswordProperty,
169 &Service::SetEAPPassword,
170 NULL,
171 &eap_.password);
Chris Masoneb2e326b2011-07-12 13:28:51 -0700172 store_.RegisterString(flimflam::kEapKeyMgmtProperty, &eap_.key_management);
173 store_.RegisterBool(flimflam::kEapUseSystemCAsProperty, &eap_.use_system_cas);
Paul Stewartbc6e7392012-05-24 07:07:48 -0700174 store_.RegisterConstStrings(shill::kEapRemoteCertificationProperty,
175 &eap_.remote_certification);
176 store_.RegisterString(shill::kEapSubjectMatchProperty,
177 &eap_.subject_match);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700178
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400179 // TODO(ers): in flimflam clearing Error has the side-effect of
180 // setting the service state to IDLE. Is this important? I could
181 // see an autotest depending on it.
Chris Masone27c4aa52011-07-02 13:10:14 -0700182 store_.RegisterConstString(flimflam::kErrorProperty, &error_);
183 store_.RegisterConstBool(flimflam::kFavoriteProperty, &favorite_);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800184 HelpRegisterDerivedUint16(shill::kHTTPProxyPortProperty,
185 &Service::GetHTTPProxyPort,
186 NULL);
Paul Stewart10241e32012-04-23 18:15:06 -0700187 HelpRegisterDerivedRpcIdentifier(shill::kIPConfigProperty,
188 &Service::GetIPConfigRpcIdentifier,
189 NULL);
Chris Masone27c4aa52011-07-02 13:10:14 -0700190 HelpRegisterDerivedBool(flimflam::kIsActiveProperty,
Chris Masoneb2e326b2011-07-12 13:28:51 -0700191 &Service::IsActive,
192 NULL);
Chris Masone4d42df82011-07-02 17:09:39 -0700193 // flimflam::kModeProperty: Registered in WiFiService
Paul Stewart0c438332012-04-11 07:55:27 -0700194
195 // Although this is a read-only property, some callers want to blindly
196 // set this value to its current value.
197 HelpRegisterDerivedString(flimflam::kNameProperty,
198 &Service::GetNameProperty,
199 &Service::AssertTrivialSetNameProperty);
Chris Masone4d42df82011-07-02 17:09:39 -0700200 // flimflam::kPassphraseProperty: Registered in WiFiService
201 // flimflam::kPassphraseRequiredProperty: Registered in WiFiService
Chris Masone27c4aa52011-07-02 13:10:14 -0700202 store_.RegisterInt32(flimflam::kPriorityProperty, &priority_);
203 HelpRegisterDerivedString(flimflam::kProfileProperty,
204 &Service::GetProfileRpcId,
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800205 &Service::SetProfileRpcId);
Chris Masone4d42df82011-07-02 17:09:39 -0700206 store_.RegisterString(flimflam::kProxyConfigProperty, &proxy_config_);
Chris Masoneb2e326b2011-07-12 13:28:51 -0700207 store_.RegisterBool(flimflam::kSaveCredentialsProperty, &save_credentials_);
Gaurav Shah435de2c2011-11-17 19:01:07 -0800208 HelpRegisterDerivedString(flimflam::kTypeProperty,
Darin Petkov58f0b6d2012-06-12 12:52:30 +0200209 &Service::CalculateTechnology,
Gaurav Shah435de2c2011-11-17 19:01:07 -0800210 NULL);
Chris Masone4d42df82011-07-02 17:09:39 -0700211 // flimflam::kSecurityProperty: Registered in WiFiService
Chris Masone27c4aa52011-07-02 13:10:14 -0700212 HelpRegisterDerivedString(flimflam::kStateProperty,
213 &Service::CalculateState,
214 NULL);
Darin Petkovd78ee7e2012-01-12 11:21:10 +0100215 store_.RegisterConstUint8(flimflam::kSignalStrengthProperty, &strength_);
Paul Stewart987e71e2011-12-05 09:45:06 -0800216 store_.RegisterString(flimflam::kUIDataProperty, &ui_data_);
Chris Masone4d42df82011-07-02 17:09:39 -0700217 // flimflam::kWifiAuthMode: Registered in WiFiService
218 // flimflam::kWifiHiddenSsid: Registered in WiFiService
219 // flimflam::kWifiFrequency: Registered in WiFiService
220 // flimflam::kWifiPhyMode: Registered in WiFiService
221 // flimflam::kWifiHexSsid: Registered in WiFiService
Thieu Le48e6d6d2011-12-06 00:40:27 +0000222
223 metrics_->RegisterService(this);
224
Paul Stewart1062d9d2012-04-27 10:42:27 -0700225 static_ip_parameters_.PlumbPropertyStore(&store_);
226
Paul Stewartcb59fed2012-03-21 21:14:46 -0700227 IgnoreParameterForConfigure(flimflam::kTypeProperty);
Paul Stewart7f61e522012-03-22 11:13:45 -0700228 IgnoreParameterForConfigure(flimflam::kProfileProperty);
Paul Stewartcb59fed2012-03-21 21:14:46 -0700229
Ben Chanfad4a0b2012-04-18 15:49:59 -0700230 SLOG(Service, 2) << "Service initialized.";
Paul Stewart75897df2011-04-27 09:05:53 -0700231}
232
Thieu Le48e6d6d2011-12-06 00:40:27 +0000233Service::~Service() {
Darin Petkov9cd7ca12012-07-03 11:06:40 +0200234 LOG(INFO) << "Service destroyed: " << friendly_name_;
Thieu Le48e6d6d2011-12-06 00:40:27 +0000235 metrics_->DeregisterService(this);
236}
Paul Stewart75897df2011-04-27 09:05:53 -0700237
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000238void Service::AutoConnect() {
Darin Petkov3abc3be2012-06-27 10:48:23 +0200239 const char *reason = NULL;
240 if (IsAutoConnectable(&reason)) {
Darin Petkov9cd7ca12012-07-03 11:06:40 +0200241 LOG(INFO) << "Auto-connecting to " << friendly_name_;
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000242 Error error;
243 Connect(&error);
mukesh agrawal76d13882012-01-12 15:23:11 -0800244 } else {
Darin Petkov9cd7ca12012-07-03 11:06:40 +0200245 LOG(INFO) << "Suppressed autoconnect to " << friendly_name_ << " "
mukesh agrawalbf14e942012-03-02 14:36:34 -0800246 << "(" << reason << ")";
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000247 }
248}
249
mukesh agrawaladb68482012-01-17 16:31:51 -0800250void Service::Connect(Error */*error*/) {
251 explicitly_disconnected_ = false;
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400252 // clear any failure state from a previous connect attempt
253 SetState(kStateIdle);
mukesh agrawaladb68482012-01-17 16:31:51 -0800254}
255
256void Service::Disconnect(Error */*error*/) {
257 explicitly_disconnected_ = true;
Christopher Wiley3e7635e2012-08-15 09:46:17 -0700258 MemoryLog::GetInstance()->FlushToDisk();
mukesh agrawaladb68482012-01-17 16:31:51 -0800259}
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 Stewarte7cce8f2012-09-11 10:56:38 -0700356 guid_ = "";
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800357 priority_ = kPriorityNone;
358 proxy_config_ = "";
359 save_credentials_ = true;
360 ui_data_ = "";
361
362 UnloadEapCredentials();
Wade Guthrie9e0c2502012-04-19 15:26:40 -0700363 Error error; // Ignored.
364 Disconnect(&error);
Philipp Neubeckf883a7b2012-09-14 19:52:44 +0200365 explicitly_disconnected_ = false;
Paul Stewart65512e12012-03-26 18:01:08 -0700366 return false;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800367}
368
Chris Masone9d779932011-08-25 16:33:41 -0700369bool Service::Save(StoreInterface *storage) {
370 const string id = GetStorageIdentifier();
Chris Masone34af2182011-08-22 11:59:36 -0700371
Darin Petkov58f0b6d2012-06-12 12:52:30 +0200372 storage->SetString(id, kStorageType, GetTechnologyString());
Paul Stewart2706aaf2011-12-14 16:44:04 -0800373
Chris Masone34af2182011-08-22 11:59:36 -0700374 // TODO(petkov): We could choose to simplify the saving code by removing most
375 // conditionals thus saving even default values.
Wade Guthrie4f28e8b2012-04-11 10:52:07 -0700376 storage->SetBool(id, kStorageAutoConnect, auto_connect_);
Chris Masone34af2182011-08-22 11:59:36 -0700377 if (check_portal_ == kCheckPortalAuto) {
378 storage->DeleteKey(id, kStorageCheckPortal);
379 } else {
380 storage->SetString(id, kStorageCheckPortal, check_portal_);
381 }
382 storage->SetBool(id, kStorageFavorite, favorite_);
mukesh agrawalcf24a242012-05-21 16:46:11 -0700383 SaveString(storage, id, kStorageGUID, guid_, false, true);
384 storage->SetBool(id, kStorageHasEverConnected, has_ever_connected_);
mukesh agrawald835b202011-10-07 15:26:47 -0700385 storage->SetString(id, kStorageName, friendly_name_);
Chris Masone34af2182011-08-22 11:59:36 -0700386 if (priority_ != kPriorityNone) {
387 storage->SetInt(id, kStoragePriority, priority_);
388 } else {
389 storage->DeleteKey(id, kStoragePriority);
390 }
Paul Stewart987e71e2011-12-05 09:45:06 -0800391 SaveString(storage, id, kStorageProxyConfig, proxy_config_, false, true);
Chris Masone34af2182011-08-22 11:59:36 -0700392 if (save_credentials_) {
393 storage->DeleteKey(id, kStorageSaveCredentials);
394 } else {
395 storage->SetBool(id, kStorageSaveCredentials, false);
396 }
Paul Stewart987e71e2011-12-05 09:45:06 -0800397 SaveString(storage, id, kStorageUIData, ui_data_, false, true);
Chris Masone34af2182011-08-22 11:59:36 -0700398
399 SaveEapCredentials(storage, id);
Paul Stewart1062d9d2012-04-27 10:42:27 -0700400 static_ip_parameters_.Save(storage, id);
Chris Masone34af2182011-08-22 11:59:36 -0700401
402 // TODO(petkov): Save these:
403
Chris Masone34af2182011-08-22 11:59:36 -0700404 // "Failure"
405 // "Modified"
406 // "LastAttempt"
Chris Masone34af2182011-08-22 11:59:36 -0700407
408 return true;
409}
410
Gary Moraind93615e2012-04-27 11:50:03 -0700411void Service::SaveToCurrentProfile() {
412 // Some unittests do not specify a manager.
413 if (manager()) {
414 manager()->SaveServiceToProfile(this);
415 }
Thieu Led4e9e552012-02-16 16:26:07 -0800416}
417
Paul Stewartcb59fed2012-03-21 21:14:46 -0700418void Service::Configure(const KeyValueStore &args, Error *error) {
419 map<string, bool>::const_iterator bool_it;
Ben Chanfad4a0b2012-04-18 15:49:59 -0700420 SLOG(Service, 5) << "Configuring bool properties:";
Paul Stewartcb59fed2012-03-21 21:14:46 -0700421 for (bool_it = args.bool_properties().begin();
422 bool_it != args.bool_properties().end();
423 ++bool_it) {
424 if (ContainsKey(parameters_ignored_for_configure_, bool_it->first)) {
425 continue;
426 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700427 SLOG(Service, 5) << " " << bool_it->first;
Paul Stewartcb59fed2012-03-21 21:14:46 -0700428 Error set_error;
429 store_.SetBoolProperty(bool_it->first, bool_it->second, &set_error);
430 if (error->IsSuccess() && set_error.IsFailure()) {
431 error->CopyFrom(set_error);
432 }
433 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700434 SLOG(Service, 5) << "Configuring string properties:";
Paul Stewartcb59fed2012-03-21 21:14:46 -0700435 map<string, string>::const_iterator string_it;
436 for (string_it = args.string_properties().begin();
437 string_it != args.string_properties().end();
438 ++string_it) {
439 if (ContainsKey(parameters_ignored_for_configure_, string_it->first)) {
440 continue;
441 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700442 SLOG(Service, 5) << " " << string_it->first;
Paul Stewartcb59fed2012-03-21 21:14:46 -0700443 Error set_error;
444 store_.SetStringProperty(string_it->first, string_it->second, &set_error);
445 if (error->IsSuccess() && set_error.IsFailure()) {
446 error->CopyFrom(set_error);
447 }
448 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700449 SLOG(Service, 5) << "Configuring uint32 properties:";
Paul Stewartcb59fed2012-03-21 21:14:46 -0700450 map<string, uint32>::const_iterator int_it;
451 for (int_it = args.uint_properties().begin();
452 int_it != args.uint_properties().end();
453 ++int_it) {
454 if (ContainsKey(parameters_ignored_for_configure_, int_it->first)) {
455 continue;
456 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700457 SLOG(Service, 5) << " " << int_it->first;
Paul Stewartcb59fed2012-03-21 21:14:46 -0700458 Error set_error;
459 store_.SetUint32Property(int_it->first, int_it->second, &set_error);
460 if (error->IsSuccess() && set_error.IsFailure()) {
461 error->CopyFrom(set_error);
462 }
463 }
464}
465
Paul Stewart10ccbb32012-04-26 15:59:30 -0700466bool Service::IsRemembered() const {
467 return profile_ && !manager_->IsServiceEphemeral(this);
468}
469
mukesh agrawal00917ce2011-11-22 23:56:55 +0000470void Service::MakeFavorite() {
471 if (favorite_) {
472 // We do not want to clobber the value of auto_connect_ (it may
473 // be user-set). So return early.
474 return;
475 }
476
477 auto_connect_ = true;
478 favorite_ = true;
479}
480
Darin Petkov5eb05422012-05-11 15:45:25 +0200481void Service::SetConnection(const ConnectionRefPtr &connection) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800482 if (connection.get()) {
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800483 http_proxy_.reset(new HTTPProxy(connection));
Darin Petkov5eb05422012-05-11 15:45:25 +0200484 http_proxy_->Start(dispatcher_, sockets_.get());
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800485 } else {
486 http_proxy_.reset();
Paul Stewartdef189e2012-08-02 20:12:09 -0700487 static_ip_parameters_.ClearSavedParameters();
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800488 }
489 connection_ = connection;
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800490}
491
Gaurav Shah10109f22011-11-11 20:16:22 -0800492bool Service::Is8021xConnectable() const {
493 // We mirror all the flimflam checks (see service.c:is_connectable()).
494
495 // Identity is required.
496 if (eap_.identity.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700497 SLOG(Service, 2) << "Not connectable: Identity is empty.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800498 return false;
499 }
500
501 if (!eap_.client_cert.empty() || !eap_.cert_id.empty()) {
502 // If a client certificate is being used, we must have a private key.
503 if (eap_.private_key.empty() && eap_.key_id.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700504 SLOG(Service, 2)
505 << "Not connectable. Client certificate but no private key.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800506 return false;
507 }
508 }
509 if (!eap_.cert_id.empty() || !eap_.key_id.empty() ||
510 !eap_.ca_cert_id.empty()) {
511 // If PKCS#11 data is needed, a PIN is required.
512 if (eap_.pin.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700513 SLOG(Service, 2) << "Not connectable. PKCS#11 data but no PIN.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800514 return false;
515 }
516 }
517
518 // For EAP-TLS, a client certificate is required.
519 if (eap_.eap.empty() || eap_.eap == "TLS") {
Paul Stewart81426132012-05-16 10:05:10 -0700520 if ((!eap_.client_cert.empty() || !eap_.cert_id.empty()) &&
521 (!eap_.private_key.empty() || !eap_.key_id.empty())) {
522 SLOG(Service, 2) << "Connectable. EAP-TLS with a client cert and key.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800523 return true;
524 }
525 }
526
527 // For EAP types other than TLS (e.g. EAP-TTLS or EAP-PEAP, password is the
528 // minimum requirement), at least an identity + password is required.
529 if (eap_.eap.empty() || eap_.eap != "TLS") {
530 if (!eap_.password.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700531 SLOG(Service, 2) << "Connectable. !EAP-TLS and has a password.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800532 return true;
533 }
534 }
535
Ben Chanfad4a0b2012-04-18 15:49:59 -0700536 SLOG(Service, 2)
537 << "Not connectable. No suitable EAP configuration was found.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800538 return false;
539}
540
Paul Stewartbc6e7392012-05-24 07:07:48 -0700541bool Service::AddEAPCertification(const string &name, size_t depth) {
542 if (depth >= kEAPMaxCertificationElements) {
543 LOG(WARNING) << "Ignoring certification " << name
544 << " because depth " << depth
545 << " exceeds our maximum of "
546 << kEAPMaxCertificationElements;
547 return false;
548 }
549
550 if (depth >= eap_.remote_certification.size()) {
551 eap_.remote_certification.resize(depth + 1);
552 } else if (name == eap_.remote_certification[depth]) {
553 return true;
554 }
555
556 eap_.remote_certification[depth] = name;
557 LOG(INFO) << "Received certification for "
558 << name
559 << " at depth "
560 << depth;
561 return true;
562}
563
564void Service::ClearEAPCertification() {
565 eap_.remote_certification.clear();
566}
567
Gaurav Shah10109f22011-11-11 20:16:22 -0800568void Service::set_eap(const EapCredentials &eap) {
569 eap_ = eap;
570 // Note: Connectability can only be updated by a subclass of Service
571 // with knowledge of whether the service actually uses 802.1x credentials.
572}
573
mukesh agrawal00917ce2011-11-22 23:56:55 +0000574// static
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800575const char *Service::ConnectFailureToString(const ConnectFailure &state) {
576 switch (state) {
577 case kFailureUnknown:
578 return "Unknown";
Paul Stewartf2d60912012-07-15 08:37:30 -0700579 case kFailureAAA:
580 return flimflam::kErrorAaaFailed;
581 case kFailureActivation:
582 return flimflam::kErrorActivationFailed;
583 case kFailureBadPassphrase:
584 return flimflam::kErrorBadPassphrase;
585 case kFailureBadWEPKey:
586 return flimflam::kErrorBadWEPKey;
587 case kFailureConnect:
588 return flimflam::kErrorConnectFailed;
589 case kFailureDNSLookup:
590 return flimflam::kErrorDNSLookupFailed;
591 case kFailureDHCP:
592 return flimflam::kErrorDhcpFailed;
593 case kFailureHTTPGet:
594 return flimflam::kErrorHTTPGetFailed;
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800595 case kFailureNeedEVDO:
Paul Stewartf2d60912012-07-15 08:37:30 -0700596 return flimflam::kErrorNeedEvdo;
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800597 case kFailureNeedHomeNetwork:
Paul Stewartf2d60912012-07-15 08:37:30 -0700598 return flimflam::kErrorNeedHomeNetwork;
599 case kFailureOTASP:
600 return flimflam::kErrorOtaspFailed;
601 case kFailureOutOfRange:
602 return flimflam::kErrorOutOfRange;
603 case kFailurePinMissing:
604 return flimflam::kErrorPinMissing;
605 case kFailurePPPAuth:
606 return flimflam::kErrorPppAuthFailed;
Thieu Le48e6d6d2011-12-06 00:40:27 +0000607 case kFailureMax:
608 return "Max failure error code";
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800609 }
610 return "Invalid";
611}
612
613// static
614const char *Service::ConnectStateToString(const ConnectState &state) {
615 switch (state) {
616 case kStateUnknown:
617 return "Unknown";
618 case kStateIdle:
619 return "Idle";
620 case kStateAssociating:
621 return "Associating";
622 case kStateConfiguring:
623 return "Configuring";
624 case kStateConnected:
625 return "Connected";
626 case kStateDisconnected:
627 return "Disconnected";
Thieu Le48e6d6d2011-12-06 00:40:27 +0000628 case kStatePortal:
629 return "Portal";
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800630 case kStateFailure:
631 return "Failure";
632 case kStateOnline:
633 return "Online";
634 }
635 return "Invalid";
636}
637
Darin Petkov58f0b6d2012-06-12 12:52:30 +0200638string Service::GetTechnologyString() const {
Gaurav Shah435de2c2011-11-17 19:01:07 -0800639 return Technology::NameFromIdentifier(technology());
640}
641
Darin Petkov58f0b6d2012-06-12 12:52:30 +0200642string Service::CalculateTechnology(Error */*error*/) {
643 return GetTechnologyString();
644}
645
Jason Glasgowb5790052012-01-27 01:03:52 -0500646// static
Paul Stewart22aa71b2011-09-16 12:15:11 -0700647bool Service::DecideBetween(int a, int b, bool *decision) {
648 if (a == b)
649 return false;
650 *decision = (a > b);
651 return true;
652}
653
mukesh agrawal00917ce2011-11-22 23:56:55 +0000654// static
Paul Stewart22aa71b2011-09-16 12:15:11 -0700655bool Service::Compare(ServiceRefPtr a,
656 ServiceRefPtr b,
mukesh agrawalddc378f2012-02-17 18:26:20 -0800657 const vector<Technology::Identifier> &tech_order,
658 const char **reason) {
Paul Stewart22aa71b2011-09-16 12:15:11 -0700659 bool ret;
660
661 if (a->state() != b->state()) {
662 if (DecideBetween(a->IsConnected(), b->IsConnected(), &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800663 *reason = kServiceSortIsConnected;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700664 return ret;
665 }
666
Paul Stewarta121c442012-06-09 14:12:58 -0700667 if (DecideBetween(!a->IsPortalled(), !b->IsPortalled(), &ret)) {
668 *reason = kServiceSortIsPortalled;
669 return ret;
670 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700671
672 if (DecideBetween(a->IsConnecting(), b->IsConnecting(), &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800673 *reason = kServiceSortIsConnecting;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700674 return ret;
675 }
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000676
677 if (DecideBetween(!a->IsFailed(), !b->IsFailed(), &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800678 *reason = kServiceSortIsFailed;
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000679 return ret;
680 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700681 }
682
Darin Petkov2f903b32012-04-18 12:56:43 +0200683 if (DecideBetween(a->connectable(), b->connectable(), &ret)) {
684 *reason = kServiceSortConnectable;
685 return ret;
686 }
687
688 // Ignore the auto-connect property if both services are connected
689 // already. This allows connected non-autoconnectable VPN services to be
690 // sorted higher than other connected services based on technology order.
691 if (!a->IsConnected() &&
692 DecideBetween(a->auto_connect(), b->auto_connect(), &ret)) {
693 *reason = kServiceSortAutoConnect;
694 return ret;
695 }
696
697 if (DecideBetween(a->favorite(), b->favorite(), &ret)) {
698 *reason = kServiceSortFavorite;
699 return ret;
700 }
701
702 if (DecideBetween(a->priority(), b->priority(), &ret)) {
703 *reason = kServiceSortPriority;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700704 return ret;
705 }
706
707 // TODO(pstew): Below this point we are making value judgements on
708 // services that are not related to anything intrinsic or
709 // user-specified. These heuristics should be richer (contain
710 // historical information, for example) and be subject to user
711 // customization.
Paul Stewart22aa71b2011-09-16 12:15:11 -0700712 for (vector<Technology::Identifier>::const_iterator it = tech_order.begin();
713 it != tech_order.end();
714 ++it) {
Joshua Kroll053fa822012-06-05 09:50:43 -0700715 if (DecideBetween(a->technology() == *it, b->technology() == *it, &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800716 *reason = kServiceSortTechnology;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700717 return ret;
mukesh agrawalddc378f2012-02-17 18:26:20 -0800718 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700719 }
720
Paul Stewart1ca3e852011-11-04 07:50:49 -0700721 if (DecideBetween(a->security_level(), b->security_level(), &ret) ||
Paul Stewart22aa71b2011-09-16 12:15:11 -0700722 DecideBetween(a->strength(), b->strength(), &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800723 *reason = kServiceSortSecurityEtc;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700724 return ret;
725 }
726
mukesh agrawalddc378f2012-02-17 18:26:20 -0800727 *reason = kServiceSortUniqueName;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700728 return a->UniqueName() < b->UniqueName();
729}
730
Chris Masone34af2182011-08-22 11:59:36 -0700731const ProfileRefPtr &Service::profile() const { return profile_; }
732
733void Service::set_profile(const ProfileRefPtr &p) { profile_ = p; }
734
Paul Stewartff14b022012-04-24 20:06:23 -0700735void Service::OnPropertyChanged(const string &property) {
Paul Stewart81426132012-05-16 10:05:10 -0700736 if (Is8021x() &&
737 (property == flimflam::kEAPCertIDProperty ||
738 property == flimflam::kEAPClientCertProperty ||
739 property == flimflam::kEAPKeyIDProperty ||
740 property == flimflam::kEAPPINProperty ||
741 property == flimflam::kEapCaCertIDProperty ||
742 property == flimflam::kEapIdentityProperty ||
Paul Stewartadf79d82012-07-18 16:09:56 -0700743 property == flimflam::kEapKeyMgmtProperty ||
Paul Stewart81426132012-05-16 10:05:10 -0700744 property == flimflam::kEapPasswordProperty ||
745 property == flimflam::kEapPrivateKeyProperty)) {
746 // This notifies subclassess that EAP parameters have been changed.
747 set_eap(eap_);
748 }
mukesh agrawalcf24a242012-05-21 16:46:11 -0700749 SaveToProfile();
Paul Stewartd215af62012-04-24 23:25:50 -0700750 if ((property == flimflam::kCheckPortalProperty ||
751 property == flimflam::kProxyConfigProperty) &&
752 (state_ == kStateConnected ||
753 state_ == kStatePortal ||
754 state_ == kStateOnline)) {
755 manager_->RecheckPortalOnService(this);
756 }
Paul Stewartff14b022012-04-24 20:06:23 -0700757}
758
Paul Stewart10241e32012-04-23 18:15:06 -0700759string Service::GetIPConfigRpcIdentifier(Error *error) {
760 if (!connection_) {
761 error->Populate(Error::kNotFound);
762 return "/";
763 }
764
765 string id = connection_->ipconfig_rpc_identifier();
766
767 if (id.empty()) {
768 // Do not return an empty IPConfig.
769 error->Populate(Error::kNotFound);
770 return "/";
771 }
772
773 return id;
774}
775
mukesh agrawal29c13a12011-11-24 00:09:19 +0000776void Service::set_connectable(bool connectable) {
777 connectable_ = connectable;
778 adaptor_->EmitBoolChanged(flimflam::kConnectableProperty, connectable_);
779}
780
Darin Petkovb2ba39f2012-06-06 10:33:43 +0200781void Service::SetConnectable(bool connectable) {
782 if (connectable_ == connectable) {
783 return;
784 }
785 connectable_ = connectable;
786 adaptor_->EmitBoolChanged(flimflam::kConnectableProperty, connectable_);
787 if (manager_->HasService(this)) {
788 manager_->UpdateService(this);
789 }
790}
791
Darin Petkov58f0b6d2012-06-12 12:52:30 +0200792string Service::GetStateString() const {
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700793 switch (state_) {
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800794 case kStateIdle:
795 return flimflam::kStateIdle;
796 case kStateAssociating:
797 return flimflam::kStateAssociation;
798 case kStateConfiguring:
799 return flimflam::kStateConfiguration;
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700800 case kStateConnected:
Paul Stewart20088d82012-02-16 06:58:55 -0800801 return flimflam::kStateReady;
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800802 case kStateDisconnected:
803 return flimflam::kStateDisconnect;
804 case kStateFailure:
805 return flimflam::kStateFailure;
Paul Stewart20088d82012-02-16 06:58:55 -0800806 case kStatePortal:
807 return flimflam::kStatePortal;
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800808 case kStateOnline:
809 return flimflam::kStateOnline;
810 case kStateUnknown:
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700811 default:
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800812 return "";
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700813 }
814}
815
Darin Petkov58f0b6d2012-06-12 12:52:30 +0200816string Service::CalculateState(Error */*error*/) {
817 return GetStateString();
818}
819
mukesh agrawalbf14e942012-03-02 14:36:34 -0800820bool Service::IsAutoConnectable(const char **reason) const {
821 if (!connectable()) {
822 *reason = kAutoConnNotConnectable;
823 return false;
824 }
825
826 if (IsConnected()) {
827 *reason = kAutoConnConnected;
828 return false;
829 }
830
831 if (IsConnecting()) {
832 *reason = kAutoConnConnecting;
833 return false;
834 }
835
836 if (explicitly_disconnected_) {
837 *reason = kAutoConnExplicitDisconnect;
838 return false;
839 }
840
841 return true;
mukesh agrawal76d13882012-01-12 15:23:11 -0800842}
843
Paul Stewartd215af62012-04-24 23:25:50 -0700844bool Service::IsPortalDetectionDisabled() const {
845 return check_portal_ == kCheckPortalFalse;
846}
847
848bool Service::IsPortalDetectionAuto() const {
849 return check_portal_ == kCheckPortalAuto;
850}
851
mukesh agrawalffa3d042011-10-06 15:26:10 -0700852void Service::HelpRegisterDerivedBool(
853 const string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800854 bool(Service::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700855 void(Service::*set)(const bool&, Error *)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700856 store_.RegisterDerivedBool(
857 name,
858 BoolAccessor(new CustomAccessor<Service, bool>(this, get, set)));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700859}
860
mukesh agrawalffa3d042011-10-06 15:26:10 -0700861void Service::HelpRegisterDerivedString(
862 const string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800863 string(Service::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700864 void(Service::*set)(const string&, Error *)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700865 store_.RegisterDerivedString(
866 name,
867 StringAccessor(new CustomAccessor<Service, string>(this, get, set)));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700868}
869
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400870void Service::HelpRegisterDerivedRpcIdentifier(
871 const string &name,
872 RpcIdentifier(Service::*get)(Error *),
873 void(Service::*set)(const RpcIdentifier&, Error *)) {
874 store_.RegisterDerivedRpcIdentifier(
875 name,
876 RpcIdentifierAccessor(new CustomAccessor<Service, RpcIdentifier>(
877 this, get, set)));
878}
879
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800880void Service::HelpRegisterDerivedUint16(
881 const string &name,
882 uint16(Service::*get)(Error *),
883 void(Service::*set)(const uint16&, Error *)) {
884 store_.RegisterDerivedUint16(
885 name,
886 Uint16Accessor(new CustomAccessor<Service, uint16>(this, get, set)));
887}
888
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800889void Service::HelpRegisterWriteOnlyDerivedString(
890 const string &name,
891 void(Service::*set)(const string &, Error *),
892 void(Service::*clear)(Error *),
893 const string *default_value) {
894 store_.RegisterDerivedString(
895 name,
896 StringAccessor(
897 new CustomWriteOnlyAccessor<Service, string>(
898 this, set, clear, default_value)));
899}
900
Darin Petkovba40dd32011-07-11 20:06:39 -0700901void Service::SaveString(StoreInterface *storage,
Chris Masone34af2182011-08-22 11:59:36 -0700902 const string &id,
Darin Petkovba40dd32011-07-11 20:06:39 -0700903 const string &key,
904 const string &value,
905 bool crypted,
906 bool save) {
907 if (value.empty() || !save) {
Chris Masone34af2182011-08-22 11:59:36 -0700908 storage->DeleteKey(id, key);
Darin Petkovba40dd32011-07-11 20:06:39 -0700909 return;
910 }
911 if (crypted) {
Chris Masone34af2182011-08-22 11:59:36 -0700912 storage->SetCryptedString(id, key, value);
Darin Petkovba40dd32011-07-11 20:06:39 -0700913 return;
914 }
Chris Masone34af2182011-08-22 11:59:36 -0700915 storage->SetString(id, key, value);
Darin Petkovba40dd32011-07-11 20:06:39 -0700916}
917
Chris Masone34af2182011-08-22 11:59:36 -0700918void Service::LoadEapCredentials(StoreInterface *storage, const string &id) {
Gaurav Shah10109f22011-11-11 20:16:22 -0800919 EapCredentials eap;
920 storage->GetCryptedString(id, kStorageEapIdentity, &eap.identity);
921 storage->GetString(id, kStorageEapEap, &eap.eap);
922 storage->GetString(id, kStorageEapInnerEap, &eap.inner_eap);
Darin Petkovba40dd32011-07-11 20:06:39 -0700923 storage->GetCryptedString(id,
924 kStorageEapAnonymousIdentity,
Gaurav Shah10109f22011-11-11 20:16:22 -0800925 &eap.anonymous_identity);
926 storage->GetString(id, kStorageEapClientCert, &eap.client_cert);
927 storage->GetString(id, kStorageEapCertID, &eap.cert_id);
928 storage->GetString(id, kStorageEapPrivateKey, &eap.private_key);
Darin Petkovba40dd32011-07-11 20:06:39 -0700929 storage->GetCryptedString(id,
930 kStorageEapPrivateKeyPassword,
Gaurav Shah10109f22011-11-11 20:16:22 -0800931 &eap.private_key_password);
932 storage->GetString(id, kStorageEapKeyID, &eap.key_id);
933 storage->GetString(id, kStorageEapCACert, &eap.ca_cert);
934 storage->GetString(id, kStorageEapCACertID, &eap.ca_cert_id);
Paul Stewartecf4cd12012-04-17 11:08:39 -0700935 storage->GetString(id, kStorageEapCACertNSS, &eap.ca_cert_nss);
Gaurav Shah10109f22011-11-11 20:16:22 -0800936 storage->GetBool(id, kStorageEapUseSystemCAs, &eap.use_system_cas);
937 storage->GetString(id, kStorageEapPIN, &eap.pin);
938 storage->GetCryptedString(id, kStorageEapPassword, &eap.password);
939 storage->GetString(id, kStorageEapKeyManagement, &eap.key_management);
940 set_eap(eap);
Darin Petkovba40dd32011-07-11 20:06:39 -0700941}
942
Chris Masone34af2182011-08-22 11:59:36 -0700943void Service::SaveEapCredentials(StoreInterface *storage, const string &id) {
Darin Petkovba40dd32011-07-11 20:06:39 -0700944 bool save = save_credentials_;
Chris Masone34af2182011-08-22 11:59:36 -0700945 SaveString(storage, id, kStorageEapIdentity, eap_.identity, true, save);
946 SaveString(storage, id, kStorageEapEap, eap_.eap, false, true);
947 SaveString(storage, id, kStorageEapInnerEap, eap_.inner_eap, false, true);
Darin Petkovba40dd32011-07-11 20:06:39 -0700948 SaveString(storage,
Chris Masone34af2182011-08-22 11:59:36 -0700949 id,
Darin Petkovba40dd32011-07-11 20:06:39 -0700950 kStorageEapAnonymousIdentity,
951 eap_.anonymous_identity,
952 true,
953 save);
Chris Masone34af2182011-08-22 11:59:36 -0700954 SaveString(storage, id, kStorageEapClientCert, eap_.client_cert, false, save);
955 SaveString(storage, id, kStorageEapCertID, eap_.cert_id, false, save);
956 SaveString(storage, id, kStorageEapPrivateKey, eap_.private_key, false, save);
Darin Petkovba40dd32011-07-11 20:06:39 -0700957 SaveString(storage,
Chris Masone34af2182011-08-22 11:59:36 -0700958 id,
Darin Petkovba40dd32011-07-11 20:06:39 -0700959 kStorageEapPrivateKeyPassword,
960 eap_.private_key_password,
961 true,
962 save);
Chris Masone34af2182011-08-22 11:59:36 -0700963 SaveString(storage, id, kStorageEapKeyID, eap_.key_id, false, save);
964 SaveString(storage, id, kStorageEapCACert, eap_.ca_cert, false, true);
965 SaveString(storage, id, kStorageEapCACertID, eap_.ca_cert_id, false, true);
Paul Stewartecf4cd12012-04-17 11:08:39 -0700966 SaveString(storage, id, kStorageEapCACertNSS, eap_.ca_cert_nss, false, true);
Chris Masone34af2182011-08-22 11:59:36 -0700967 storage->SetBool(id, kStorageEapUseSystemCAs, eap_.use_system_cas);
968 SaveString(storage, id, kStorageEapPIN, eap_.pin, false, save);
969 SaveString(storage, id, kStorageEapPassword, eap_.password, true, save);
Darin Petkovba40dd32011-07-11 20:06:39 -0700970 SaveString(storage,
Chris Masone34af2182011-08-22 11:59:36 -0700971 id,
Darin Petkovba40dd32011-07-11 20:06:39 -0700972 kStorageEapKeyManagement,
973 eap_.key_management,
974 false,
975 true);
976}
977
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800978void Service::UnloadEapCredentials() {
979 eap_.identity = "";
980 eap_.eap = "";
981 eap_.inner_eap = "";
982 eap_.anonymous_identity = "";
983 eap_.client_cert = "";
984 eap_.cert_id = "";
985 eap_.private_key = "";
986 eap_.private_key_password = "";
987 eap_.key_id = "";
988 eap_.ca_cert = "";
989 eap_.ca_cert_id = "";
Paul Stewart20550982012-04-16 12:16:11 -0700990 eap_.use_system_cas = true;
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800991 eap_.pin = "";
992 eap_.password = "";
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800993}
994
Paul Stewartcb59fed2012-03-21 21:14:46 -0700995void Service::IgnoreParameterForConfigure(const string &parameter) {
996 parameters_ignored_for_configure_.insert(parameter);
997}
998
Paul Stewartac4ac002011-08-26 12:04:26 -0700999const string &Service::GetEAPKeyManagement() const {
1000 return eap_.key_management;
1001}
1002
1003void Service::SetEAPKeyManagement(const string &key_management) {
1004 eap_.key_management = key_management;
1005}
1006
Thieu Le284fe792012-01-31 17:53:19 -08001007bool Service::GetAutoConnect(Error */*error*/) {
1008 return auto_connect();
1009}
1010
1011void Service::SetAutoConnect(const bool &connect, Error *error) {
Wade Guthrie4f28e8b2012-04-11 10:52:07 -07001012 set_auto_connect(connect);
Thieu Le284fe792012-01-31 17:53:19 -08001013}
1014
Paul Stewartd215af62012-04-24 23:25:50 -07001015string Service::GetCheckPortal(Error *error) {
1016 return check_portal_;
1017}
1018
1019void Service::SetCheckPortal(const string &check_portal, Error *error) {
1020 if (check_portal == check_portal_) {
1021 return;
1022 }
1023 if (check_portal != kCheckPortalFalse &&
1024 check_portal != kCheckPortalTrue &&
1025 check_portal != kCheckPortalAuto) {
1026 Error::PopulateAndLog(error, Error::kInvalidArguments,
1027 base::StringPrintf(
1028 "Invalid Service CheckPortal property value: %s",
1029 check_portal.c_str()));
1030 return;
1031 }
1032 check_portal_ = check_portal;
1033}
1034
Paul Stewart9f32d192012-01-30 20:37:50 -08001035void Service::SetEAPPassword(const string &password, Error */*error*/) {
1036 eap_.password = password;
1037}
1038
1039void Service::SetEAPPrivateKeyPassword(const string &password,
1040 Error */*error*/) {
1041 eap_.private_key_password = password;
1042}
1043
Paul Stewart0c438332012-04-11 07:55:27 -07001044string Service::GetNameProperty(Error *error) {
1045 return friendly_name_;
1046}
1047
1048void Service::AssertTrivialSetNameProperty(const string &name, Error *error) {
1049 if (name != friendly_name_) {
1050 Error::PopulateAndLog(error, Error::kInvalidArguments,
Paul Stewart2bf424f2012-04-11 18:59:39 -07001051 base::StringPrintf(
1052 "Service Name property cannot be modified "
1053 "(%s to %s)", friendly_name_.c_str(),
1054 name.c_str()));
Paul Stewart0c438332012-04-11 07:55:27 -07001055 }
1056}
1057
Paul Stewart1b1a7f22012-01-06 16:24:06 -08001058string Service::GetProfileRpcId(Error *error) {
1059 if (!profile_) {
1060 // This happens in some unit tests where profile_ is not set.
1061 error->Populate(Error::kNotFound);
1062 return "";
1063 }
1064 return profile_->GetRpcIdentifier();
1065}
1066
1067void Service::SetProfileRpcId(const string &profile, Error *error) {
1068 manager_->SetProfileForService(this, profile, error);
1069}
1070
Paul Stewartbe5f5b32011-12-07 17:11:11 -08001071uint16 Service::GetHTTPProxyPort(Error */*error*/) {
1072 if (http_proxy_.get()) {
1073 return static_cast<uint16>(http_proxy_->proxy_port());
1074 }
1075 return 0;
1076}
1077
mukesh agrawalcf24a242012-05-21 16:46:11 -07001078void Service::SaveToProfile() {
1079 if (profile_.get() && profile_->GetConstStorage()) {
1080 profile_->UpdateService(this);
1081 }
1082}
1083
Darin Petkovd78ee7e2012-01-12 11:21:10 +01001084void Service::SetStrength(uint8 strength) {
1085 if (strength == strength_) {
1086 return;
1087 }
1088 strength_ = strength;
1089 adaptor_->EmitUint8Changed(flimflam::kSignalStrengthProperty, strength);
1090}
1091
Paul Stewartf2d60912012-07-15 08:37:30 -07001092void Service::UpdateErrorProperty() {
1093 const string error(ConnectFailureToString(failure_));
1094 if (error == error_) {
1095 return;
1096 }
1097 error_ = error;
1098 adaptor_->EmitStringChanged(flimflam::kErrorProperty, error);
1099}
1100
Paul Stewart75897df2011-04-27 09:05:53 -07001101} // namespace shill