blob: feb99ebdf08e90a6134bc53d4d4f56cd41236e35 [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/stringprintf.h>
10#include <chromeos/dbus/service_constants.h>
11
12#include "shill/key_value_store.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070013#include "shill/logging.h"
Paul Stewart65512e12012-03-26 18:01:08 -070014#include "shill/manager.h"
Darin Petkov33af05c2012-02-28 10:10:30 +010015#include "shill/technology.h"
16#include "shill/vpn_driver.h"
17
Darin Petkov5eb05422012-05-11 15:45:25 +020018using base::Bind;
Darin Petkov02867712012-03-12 14:25:05 +010019using base::StringPrintf;
Darin Petkov5eb05422012-05-11 15:45:25 +020020using base::Unretained;
Darin Petkov02867712012-03-12 14:25:05 +010021using std::replace_if;
Darin Petkov33af05c2012-02-28 10:10:30 +010022using std::string;
23
24namespace shill {
25
Darin Petkov79d74c92012-03-07 17:20:32 +010026VPNService::VPNService(ControlInterface *control,
Darin Petkov33af05c2012-02-28 10:10:30 +010027 EventDispatcher *dispatcher,
28 Metrics *metrics,
29 Manager *manager,
30 VPNDriver *driver)
Darin Petkov79d74c92012-03-07 17:20:32 +010031 : Service(control, dispatcher, metrics, manager, Technology::kVPN),
Paul Stewart22807992012-04-11 08:48:31 -070032 driver_(driver) {
Darin Petkov2f903b32012-04-18 12:56:43 +020033 set_connectable(true);
Darin Petkovcb715292012-04-25 13:04:37 +020034 set_save_credentials(false);
Paul Stewart22807992012-04-11 08:48:31 -070035 mutable_store()->RegisterString(flimflam::kVPNDomainProperty, &vpn_domain_);
36}
Darin Petkov33af05c2012-02-28 10:10:30 +010037
38VPNService::~VPNService() {}
39
40void VPNService::Connect(Error *error) {
Darin Petkov5eb05422012-05-11 15:45:25 +020041 SLOG(VPN, 2) << __func__ << " @ " << friendly_name();
Darin Petkov2f903b32012-04-18 12:56:43 +020042 if (IsConnected() || IsConnecting()) {
43 Error::PopulateAndLog(
44 error, Error::kAlreadyConnected, "VPN service already connected.");
45 return;
46 }
Darin Petkov6aa21872012-03-09 16:10:19 +010047 Service::Connect(error);
Darin Petkov79d74c92012-03-07 17:20:32 +010048 driver_->Connect(this, error);
Darin Petkov33af05c2012-02-28 10:10:30 +010049}
50
Darin Petkov6aa21872012-03-09 16:10:19 +010051void VPNService::Disconnect(Error *error) {
Darin Petkov5eb05422012-05-11 15:45:25 +020052 SLOG(VPN, 2) << __func__ << " @ " << friendly_name();
Darin Petkov6aa21872012-03-09 16:10:19 +010053 Service::Disconnect(error);
54 driver_->Disconnect();
55}
56
Darin Petkov33af05c2012-02-28 10:10:30 +010057string VPNService::GetStorageIdentifier() const {
Darin Petkov02867712012-03-12 14:25:05 +010058 return storage_id_;
59}
60
61// static
62string VPNService::CreateStorageIdentifier(const KeyValueStore &args,
63 Error *error) {
Darin Petkov7f060332012-03-14 11:46:47 +010064 string host = args.LookupString(flimflam::kProviderHostProperty, "");
Darin Petkov02867712012-03-12 14:25:05 +010065 if (host.empty()) {
66 Error::PopulateAndLog(
67 error, Error::kInvalidProperty, "Missing VPN host.");
68 return "";
69 }
Darin Petkov7f060332012-03-14 11:46:47 +010070 string name = args.LookupString(flimflam::kProviderNameProperty, "");
Darin Petkov02867712012-03-12 14:25:05 +010071 if (name.empty()) {
Paul Stewart451aa7f2012-04-11 19:07:58 -070072 name = args.LookupString(flimflam::kNameProperty, "");
73 if (name.empty()) {
74 Error::PopulateAndLog(
75 error, Error::kNotSupported, "Missing VPN name.");
76 return "";
77 }
Darin Petkov02867712012-03-12 14:25:05 +010078 }
79 string id = StringPrintf("vpn_%s_%s", host.c_str(), name.c_str());
80 replace_if(id.begin(), id.end(), &Service::IllegalChar, '_');
81 return id;
Darin Petkov33af05c2012-02-28 10:10:30 +010082}
83
84string VPNService::GetDeviceRpcId(Error *error) {
Darin Petkov33af05c2012-02-28 10:10:30 +010085 error->Populate(Error::kNotSupported);
86 return "/";
87}
88
Darin Petkovf3c71d72012-03-21 12:32:15 +010089bool VPNService::Load(StoreInterface *storage) {
90 return Service::Load(storage) &&
91 driver_->Load(storage, GetStorageIdentifier());
92}
93
94bool VPNService::Save(StoreInterface *storage) {
95 return Service::Save(storage) &&
Darin Petkovcb715292012-04-25 13:04:37 +020096 driver_->Save(storage, GetStorageIdentifier(), save_credentials());
Darin Petkovf3c71d72012-03-21 12:32:15 +010097}
98
Paul Stewart65512e12012-03-26 18:01:08 -070099bool VPNService::Unload() {
Darin Petkova0e645e2012-04-25 11:38:59 +0200100 // The base method also disconnects the service.
Paul Stewart65512e12012-03-26 18:01:08 -0700101 Service::Unload();
102
Darin Petkovcb715292012-04-25 13:04:37 +0200103 set_save_credentials(false);
104 driver_->UnloadCredentials();
105
Paul Stewart65512e12012-03-26 18:01:08 -0700106 // Ask the VPN provider to remove us from its list.
107 manager()->vpn_provider()->RemoveService(this);
108
109 return true;
110}
111
Paul Stewartebd38562012-03-23 13:06:40 -0700112void VPNService::InitDriverPropertyStore() {
113 driver_->InitPropertyStore(mutable_store());
114}
115
Darin Petkov1d0080a2012-04-30 17:10:36 +0200116void VPNService::MakeFavorite() {
117 // The base MakeFavorite method also sets auto_connect_ to true
118 // which is not desirable for VPN services.
119 set_favorite(true);
120}
121
Darin Petkov5eb05422012-05-11 15:45:25 +0200122void VPNService::SetConnection(const ConnectionRefPtr &connection) {
123 // Construct the connection binder here rather than in the constructor because
124 // this service's |friendly_name_| (which will be used for binder logging) may
125 // be initialized late. Also, there's really no reason to construct a binder
126 // if we never connect to this service. It's safe to use an unretained
127 // callback to driver's method because both the binder and the driver will be
128 // destroyed when this service is destructed.
129 if (!connection_binder_.get()) {
130 connection_binder_.reset(
131 new Connection::Binder(friendly_name(),
132 Bind(&VPNDriver::OnConnectionDisconnected,
133 Unretained(driver_.get()))));
134 }
135 // Note that |connection_| is a reference-counted pointer and is always set
136 // through this method. This means that the connection binder will not be
137 // notified when the connection is destructed (because we will unbind it first
138 // here when it's set to NULL, or because the binder will already be destroyed
139 // by ~VPNService) -- it will be notified only if the connection disconnects
140 // (e.g., because an underlying connection is destructed).
141 connection_binder_->Attach(connection);
142 Service::SetConnection(connection);
143}
144
Darin Petkov33af05c2012-02-28 10:10:30 +0100145} // namespace shill