blob: 59af4edd7b1ab36e43b94c9eba3a2889c5101fce [file] [log] [blame]
Darin Petkov33af05c2012-02-28 10:10:30 +01001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "shill/vpn_service.h"
6
Darin Petkov02867712012-03-12 14:25:05 +01007#include <algorithm>
Darin Petkov33af05c2012-02-28 10:10:30 +01008
Darin Petkov02867712012-03-12 14:25:05 +01009#include <base/logging.h>
10#include <base/stringprintf.h>
11#include <chromeos/dbus/service_constants.h>
12
13#include "shill/key_value_store.h"
Paul Stewart65512e12012-03-26 18:01:08 -070014#include "shill/manager.h"
Darin Petkov5eb05422012-05-11 15:45:25 +020015#include "shill/scope_logger.h"
Darin Petkov33af05c2012-02-28 10:10:30 +010016#include "shill/technology.h"
17#include "shill/vpn_driver.h"
18
Darin Petkov5eb05422012-05-11 15:45:25 +020019using base::Bind;
Darin Petkov02867712012-03-12 14:25:05 +010020using base::StringPrintf;
Darin Petkov5eb05422012-05-11 15:45:25 +020021using base::Unretained;
Darin Petkov02867712012-03-12 14:25:05 +010022using std::replace_if;
Darin Petkov33af05c2012-02-28 10:10:30 +010023using std::string;
24
25namespace shill {
26
Darin Petkov79d74c92012-03-07 17:20:32 +010027VPNService::VPNService(ControlInterface *control,
Darin Petkov33af05c2012-02-28 10:10:30 +010028 EventDispatcher *dispatcher,
29 Metrics *metrics,
30 Manager *manager,
31 VPNDriver *driver)
Darin Petkov79d74c92012-03-07 17:20:32 +010032 : Service(control, dispatcher, metrics, manager, Technology::kVPN),
Paul Stewart22807992012-04-11 08:48:31 -070033 driver_(driver) {
Darin Petkov2f903b32012-04-18 12:56:43 +020034 set_connectable(true);
Darin Petkovcb715292012-04-25 13:04:37 +020035 set_save_credentials(false);
Paul Stewart22807992012-04-11 08:48:31 -070036 mutable_store()->RegisterString(flimflam::kVPNDomainProperty, &vpn_domain_);
37}
Darin Petkov33af05c2012-02-28 10:10:30 +010038
39VPNService::~VPNService() {}
40
41void VPNService::Connect(Error *error) {
Darin Petkov5eb05422012-05-11 15:45:25 +020042 SLOG(VPN, 2) << __func__ << " @ " << friendly_name();
Darin Petkov2f903b32012-04-18 12:56:43 +020043 if (IsConnected() || IsConnecting()) {
44 Error::PopulateAndLog(
45 error, Error::kAlreadyConnected, "VPN service already connected.");
46 return;
47 }
Darin Petkov6aa21872012-03-09 16:10:19 +010048 Service::Connect(error);
Darin Petkov79d74c92012-03-07 17:20:32 +010049 driver_->Connect(this, error);
Darin Petkov33af05c2012-02-28 10:10:30 +010050}
51
Darin Petkov6aa21872012-03-09 16:10:19 +010052void VPNService::Disconnect(Error *error) {
Darin Petkov5eb05422012-05-11 15:45:25 +020053 SLOG(VPN, 2) << __func__ << " @ " << friendly_name();
Darin Petkov6aa21872012-03-09 16:10:19 +010054 Service::Disconnect(error);
55 driver_->Disconnect();
56}
57
Darin Petkov33af05c2012-02-28 10:10:30 +010058string VPNService::GetStorageIdentifier() const {
Darin Petkov02867712012-03-12 14:25:05 +010059 return storage_id_;
60}
61
62// static
63string VPNService::CreateStorageIdentifier(const KeyValueStore &args,
64 Error *error) {
Darin Petkov7f060332012-03-14 11:46:47 +010065 string host = args.LookupString(flimflam::kProviderHostProperty, "");
Darin Petkov02867712012-03-12 14:25:05 +010066 if (host.empty()) {
67 Error::PopulateAndLog(
68 error, Error::kInvalidProperty, "Missing VPN host.");
69 return "";
70 }
Darin Petkov7f060332012-03-14 11:46:47 +010071 string name = args.LookupString(flimflam::kProviderNameProperty, "");
Darin Petkov02867712012-03-12 14:25:05 +010072 if (name.empty()) {
Paul Stewart451aa7f2012-04-11 19:07:58 -070073 name = args.LookupString(flimflam::kNameProperty, "");
74 if (name.empty()) {
75 Error::PopulateAndLog(
76 error, Error::kNotSupported, "Missing VPN name.");
77 return "";
78 }
Darin Petkov02867712012-03-12 14:25:05 +010079 }
80 string id = StringPrintf("vpn_%s_%s", host.c_str(), name.c_str());
81 replace_if(id.begin(), id.end(), &Service::IllegalChar, '_');
82 return id;
Darin Petkov33af05c2012-02-28 10:10:30 +010083}
84
85string VPNService::GetDeviceRpcId(Error *error) {
Darin Petkov33af05c2012-02-28 10:10:30 +010086 error->Populate(Error::kNotSupported);
87 return "/";
88}
89
Darin Petkovf3c71d72012-03-21 12:32:15 +010090bool VPNService::Load(StoreInterface *storage) {
91 return Service::Load(storage) &&
92 driver_->Load(storage, GetStorageIdentifier());
93}
94
95bool VPNService::Save(StoreInterface *storage) {
96 return Service::Save(storage) &&
Darin Petkovcb715292012-04-25 13:04:37 +020097 driver_->Save(storage, GetStorageIdentifier(), save_credentials());
Darin Petkovf3c71d72012-03-21 12:32:15 +010098}
99
Paul Stewart65512e12012-03-26 18:01:08 -0700100bool VPNService::Unload() {
Darin Petkova0e645e2012-04-25 11:38:59 +0200101 // The base method also disconnects the service.
Paul Stewart65512e12012-03-26 18:01:08 -0700102 Service::Unload();
103
Darin Petkovcb715292012-04-25 13:04:37 +0200104 set_save_credentials(false);
105 driver_->UnloadCredentials();
106
Paul Stewart65512e12012-03-26 18:01:08 -0700107 // Ask the VPN provider to remove us from its list.
108 manager()->vpn_provider()->RemoveService(this);
109
110 return true;
111}
112
Paul Stewartebd38562012-03-23 13:06:40 -0700113void VPNService::InitDriverPropertyStore() {
114 driver_->InitPropertyStore(mutable_store());
115}
116
Darin Petkov1d0080a2012-04-30 17:10:36 +0200117void VPNService::MakeFavorite() {
118 // The base MakeFavorite method also sets auto_connect_ to true
119 // which is not desirable for VPN services.
120 set_favorite(true);
121}
122
Darin Petkov5eb05422012-05-11 15:45:25 +0200123void VPNService::SetConnection(const ConnectionRefPtr &connection) {
124 // Construct the connection binder here rather than in the constructor because
125 // this service's |friendly_name_| (which will be used for binder logging) may
126 // be initialized late. Also, there's really no reason to construct a binder
127 // if we never connect to this service. It's safe to use an unretained
128 // callback to driver's method because both the binder and the driver will be
129 // destroyed when this service is destructed.
130 if (!connection_binder_.get()) {
131 connection_binder_.reset(
132 new Connection::Binder(friendly_name(),
133 Bind(&VPNDriver::OnConnectionDisconnected,
134 Unretained(driver_.get()))));
135 }
136 // Note that |connection_| is a reference-counted pointer and is always set
137 // through this method. This means that the connection binder will not be
138 // notified when the connection is destructed (because we will unbind it first
139 // here when it's set to NULL, or because the binder will already be destroyed
140 // by ~VPNService) -- it will be notified only if the connection disconnects
141 // (e.g., because an underlying connection is destructed).
142 connection_binder_->Attach(connection);
143 Service::SetConnection(connection);
144}
145
Darin Petkov33af05c2012-02-28 10:10:30 +0100146} // namespace shill