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/stringprintf.h> |
| 10 | #include <chromeos/dbus/service_constants.h> |
| 11 | |
| 12 | #include "shill/key_value_store.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 13 | #include "shill/logging.h" |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 14 | #include "shill/manager.h" |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 15 | #include "shill/technology.h" |
| 16 | #include "shill/vpn_driver.h" |
| 17 | |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 18 | using base::Bind; |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 19 | using base::StringPrintf; |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 20 | using base::Unretained; |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 21 | using std::replace_if; |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 22 | using std::string; |
| 23 | |
| 24 | namespace shill { |
| 25 | |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 26 | VPNService::VPNService(ControlInterface *control, |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 27 | EventDispatcher *dispatcher, |
| 28 | Metrics *metrics, |
| 29 | Manager *manager, |
| 30 | VPNDriver *driver) |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 31 | : Service(control, dispatcher, metrics, manager, Technology::kVPN), |
Paul Stewart | 2280799 | 2012-04-11 08:48:31 -0700 | [diff] [blame] | 32 | driver_(driver) { |
Darin Petkov | 2f903b3 | 2012-04-18 12:56:43 +0200 | [diff] [blame] | 33 | set_connectable(true); |
Darin Petkov | cb71529 | 2012-04-25 13:04:37 +0200 | [diff] [blame] | 34 | set_save_credentials(false); |
Paul Stewart | 2280799 | 2012-04-11 08:48:31 -0700 | [diff] [blame] | 35 | mutable_store()->RegisterString(flimflam::kVPNDomainProperty, &vpn_domain_); |
| 36 | } |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 37 | |
| 38 | VPNService::~VPNService() {} |
| 39 | |
| 40 | void VPNService::Connect(Error *error) { |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 41 | SLOG(VPN, 2) << __func__ << " @ " << friendly_name(); |
Darin Petkov | 2f903b3 | 2012-04-18 12:56:43 +0200 | [diff] [blame] | 42 | if (IsConnected() || IsConnecting()) { |
| 43 | Error::PopulateAndLog( |
| 44 | error, Error::kAlreadyConnected, "VPN service already connected."); |
| 45 | return; |
| 46 | } |
Darin Petkov | 6aa2187 | 2012-03-09 16:10:19 +0100 | [diff] [blame] | 47 | Service::Connect(error); |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 48 | driver_->Connect(this, error); |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 49 | } |
| 50 | |
Darin Petkov | 6aa2187 | 2012-03-09 16:10:19 +0100 | [diff] [blame] | 51 | void VPNService::Disconnect(Error *error) { |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 52 | SLOG(VPN, 2) << __func__ << " @ " << friendly_name(); |
Darin Petkov | 6aa2187 | 2012-03-09 16:10:19 +0100 | [diff] [blame] | 53 | Service::Disconnect(error); |
| 54 | driver_->Disconnect(); |
| 55 | } |
| 56 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 57 | string VPNService::GetStorageIdentifier() const { |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 58 | return storage_id_; |
| 59 | } |
| 60 | |
| 61 | // static |
| 62 | string VPNService::CreateStorageIdentifier(const KeyValueStore &args, |
| 63 | Error *error) { |
Darin Petkov | 7f06033 | 2012-03-14 11:46:47 +0100 | [diff] [blame] | 64 | string host = args.LookupString(flimflam::kProviderHostProperty, ""); |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 65 | if (host.empty()) { |
| 66 | Error::PopulateAndLog( |
| 67 | error, Error::kInvalidProperty, "Missing VPN host."); |
| 68 | return ""; |
| 69 | } |
Darin Petkov | 7f06033 | 2012-03-14 11:46:47 +0100 | [diff] [blame] | 70 | string name = args.LookupString(flimflam::kProviderNameProperty, ""); |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 71 | if (name.empty()) { |
Paul Stewart | 451aa7f | 2012-04-11 19:07:58 -0700 | [diff] [blame] | 72 | name = args.LookupString(flimflam::kNameProperty, ""); |
| 73 | if (name.empty()) { |
| 74 | Error::PopulateAndLog( |
| 75 | error, Error::kNotSupported, "Missing VPN name."); |
| 76 | return ""; |
| 77 | } |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 78 | } |
| 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 Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | string VPNService::GetDeviceRpcId(Error *error) { |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 85 | error->Populate(Error::kNotSupported); |
| 86 | return "/"; |
| 87 | } |
| 88 | |
Darin Petkov | f3c71d7 | 2012-03-21 12:32:15 +0100 | [diff] [blame] | 89 | bool VPNService::Load(StoreInterface *storage) { |
| 90 | return Service::Load(storage) && |
| 91 | driver_->Load(storage, GetStorageIdentifier()); |
| 92 | } |
| 93 | |
| 94 | bool VPNService::Save(StoreInterface *storage) { |
| 95 | return Service::Save(storage) && |
Darin Petkov | cb71529 | 2012-04-25 13:04:37 +0200 | [diff] [blame] | 96 | driver_->Save(storage, GetStorageIdentifier(), save_credentials()); |
Darin Petkov | f3c71d7 | 2012-03-21 12:32:15 +0100 | [diff] [blame] | 97 | } |
| 98 | |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 99 | bool VPNService::Unload() { |
Darin Petkov | a0e645e | 2012-04-25 11:38:59 +0200 | [diff] [blame] | 100 | // The base method also disconnects the service. |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 101 | Service::Unload(); |
| 102 | |
Darin Petkov | cb71529 | 2012-04-25 13:04:37 +0200 | [diff] [blame] | 103 | set_save_credentials(false); |
| 104 | driver_->UnloadCredentials(); |
| 105 | |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 106 | // Ask the VPN provider to remove us from its list. |
| 107 | manager()->vpn_provider()->RemoveService(this); |
| 108 | |
| 109 | return true; |
| 110 | } |
| 111 | |
Paul Stewart | ebd3856 | 2012-03-23 13:06:40 -0700 | [diff] [blame] | 112 | void VPNService::InitDriverPropertyStore() { |
| 113 | driver_->InitPropertyStore(mutable_store()); |
| 114 | } |
| 115 | |
Darin Petkov | 1d0080a | 2012-04-30 17:10:36 +0200 | [diff] [blame] | 116 | void 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 Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 122 | void 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 Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 145 | } // namespace shill |