Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 1 | // 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 Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 7 | #include <algorithm> |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 8 | |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 9 | #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 Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 14 | #include "shill/manager.h" |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame^] | 15 | #include "shill/scope_logger.h" |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 16 | #include "shill/technology.h" |
| 17 | #include "shill/vpn_driver.h" |
| 18 | |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame^] | 19 | using base::Bind; |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 20 | using base::StringPrintf; |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame^] | 21 | using base::Unretained; |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 22 | using std::replace_if; |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 23 | using std::string; |
| 24 | |
| 25 | namespace shill { |
| 26 | |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 27 | VPNService::VPNService(ControlInterface *control, |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 28 | EventDispatcher *dispatcher, |
| 29 | Metrics *metrics, |
| 30 | Manager *manager, |
| 31 | VPNDriver *driver) |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 32 | : Service(control, dispatcher, metrics, manager, Technology::kVPN), |
Paul Stewart | 2280799 | 2012-04-11 08:48:31 -0700 | [diff] [blame] | 33 | driver_(driver) { |
Darin Petkov | 2f903b3 | 2012-04-18 12:56:43 +0200 | [diff] [blame] | 34 | set_connectable(true); |
Darin Petkov | cb71529 | 2012-04-25 13:04:37 +0200 | [diff] [blame] | 35 | set_save_credentials(false); |
Paul Stewart | 2280799 | 2012-04-11 08:48:31 -0700 | [diff] [blame] | 36 | mutable_store()->RegisterString(flimflam::kVPNDomainProperty, &vpn_domain_); |
| 37 | } |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 38 | |
| 39 | VPNService::~VPNService() {} |
| 40 | |
Darin Petkov | 2f903b3 | 2012-04-18 12:56:43 +0200 | [diff] [blame] | 41 | bool VPNService::TechnologyIs(const Technology::Identifier type) const { |
| 42 | return type == Technology::kVPN; |
| 43 | } |
| 44 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 45 | void VPNService::Connect(Error *error) { |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame^] | 46 | SLOG(VPN, 2) << __func__ << " @ " << friendly_name(); |
Darin Petkov | 2f903b3 | 2012-04-18 12:56:43 +0200 | [diff] [blame] | 47 | if (IsConnected() || IsConnecting()) { |
| 48 | Error::PopulateAndLog( |
| 49 | error, Error::kAlreadyConnected, "VPN service already connected."); |
| 50 | return; |
| 51 | } |
Darin Petkov | 6aa2187 | 2012-03-09 16:10:19 +0100 | [diff] [blame] | 52 | Service::Connect(error); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 53 | driver_->Connect(this, error); |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 54 | } |
| 55 | |
Darin Petkov | 6aa2187 | 2012-03-09 16:10:19 +0100 | [diff] [blame] | 56 | void VPNService::Disconnect(Error *error) { |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame^] | 57 | SLOG(VPN, 2) << __func__ << " @ " << friendly_name(); |
Darin Petkov | 6aa2187 | 2012-03-09 16:10:19 +0100 | [diff] [blame] | 58 | Service::Disconnect(error); |
| 59 | driver_->Disconnect(); |
| 60 | } |
| 61 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 62 | string VPNService::GetStorageIdentifier() const { |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 63 | return storage_id_; |
| 64 | } |
| 65 | |
| 66 | // static |
| 67 | string VPNService::CreateStorageIdentifier(const KeyValueStore &args, |
| 68 | Error *error) { |
Darin Petkov | 7f06033 | 2012-03-14 11:46:47 +0100 | [diff] [blame] | 69 | string host = args.LookupString(flimflam::kProviderHostProperty, ""); |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 70 | if (host.empty()) { |
| 71 | Error::PopulateAndLog( |
| 72 | error, Error::kInvalidProperty, "Missing VPN host."); |
| 73 | return ""; |
| 74 | } |
Darin Petkov | 7f06033 | 2012-03-14 11:46:47 +0100 | [diff] [blame] | 75 | string name = args.LookupString(flimflam::kProviderNameProperty, ""); |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 76 | if (name.empty()) { |
Paul Stewart | 451aa7f | 2012-04-11 19:07:58 -0700 | [diff] [blame] | 77 | name = args.LookupString(flimflam::kNameProperty, ""); |
| 78 | if (name.empty()) { |
| 79 | Error::PopulateAndLog( |
| 80 | error, Error::kNotSupported, "Missing VPN name."); |
| 81 | return ""; |
| 82 | } |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 83 | } |
| 84 | string id = StringPrintf("vpn_%s_%s", host.c_str(), name.c_str()); |
| 85 | replace_if(id.begin(), id.end(), &Service::IllegalChar, '_'); |
| 86 | return id; |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | string VPNService::GetDeviceRpcId(Error *error) { |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 90 | error->Populate(Error::kNotSupported); |
| 91 | return "/"; |
| 92 | } |
| 93 | |
Darin Petkov | f3c71d7 | 2012-03-21 12:32:15 +0100 | [diff] [blame] | 94 | bool VPNService::Load(StoreInterface *storage) { |
| 95 | return Service::Load(storage) && |
| 96 | driver_->Load(storage, GetStorageIdentifier()); |
| 97 | } |
| 98 | |
| 99 | bool VPNService::Save(StoreInterface *storage) { |
| 100 | return Service::Save(storage) && |
Darin Petkov | cb71529 | 2012-04-25 13:04:37 +0200 | [diff] [blame] | 101 | driver_->Save(storage, GetStorageIdentifier(), save_credentials()); |
Darin Petkov | f3c71d7 | 2012-03-21 12:32:15 +0100 | [diff] [blame] | 102 | } |
| 103 | |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 104 | bool VPNService::Unload() { |
Darin Petkov | a0e645e | 2012-04-25 11:38:59 +0200 | [diff] [blame] | 105 | // The base method also disconnects the service. |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 106 | Service::Unload(); |
| 107 | |
Darin Petkov | cb71529 | 2012-04-25 13:04:37 +0200 | [diff] [blame] | 108 | set_save_credentials(false); |
| 109 | driver_->UnloadCredentials(); |
| 110 | |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 111 | // Ask the VPN provider to remove us from its list. |
| 112 | manager()->vpn_provider()->RemoveService(this); |
| 113 | |
| 114 | return true; |
| 115 | } |
| 116 | |
Paul Stewart | ebd3856 | 2012-03-23 13:06:40 -0700 | [diff] [blame] | 117 | void VPNService::InitDriverPropertyStore() { |
| 118 | driver_->InitPropertyStore(mutable_store()); |
| 119 | } |
| 120 | |
Darin Petkov | 1d0080a | 2012-04-30 17:10:36 +0200 | [diff] [blame] | 121 | void VPNService::MakeFavorite() { |
| 122 | // The base MakeFavorite method also sets auto_connect_ to true |
| 123 | // which is not desirable for VPN services. |
| 124 | set_favorite(true); |
| 125 | } |
| 126 | |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame^] | 127 | void VPNService::SetConnection(const ConnectionRefPtr &connection) { |
| 128 | // Construct the connection binder here rather than in the constructor because |
| 129 | // this service's |friendly_name_| (which will be used for binder logging) may |
| 130 | // be initialized late. Also, there's really no reason to construct a binder |
| 131 | // if we never connect to this service. It's safe to use an unretained |
| 132 | // callback to driver's method because both the binder and the driver will be |
| 133 | // destroyed when this service is destructed. |
| 134 | if (!connection_binder_.get()) { |
| 135 | connection_binder_.reset( |
| 136 | new Connection::Binder(friendly_name(), |
| 137 | Bind(&VPNDriver::OnConnectionDisconnected, |
| 138 | Unretained(driver_.get())))); |
| 139 | } |
| 140 | // Note that |connection_| is a reference-counted pointer and is always set |
| 141 | // through this method. This means that the connection binder will not be |
| 142 | // notified when the connection is destructed (because we will unbind it first |
| 143 | // here when it's set to NULL, or because the binder will already be destroyed |
| 144 | // by ~VPNService) -- it will be notified only if the connection disconnects |
| 145 | // (e.g., because an underlying connection is destructed). |
| 146 | connection_binder_->Attach(connection); |
| 147 | Service::SetConnection(connection); |
| 148 | } |
| 149 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 150 | } // namespace shill |