blob: 1601ce390088c6e0b53942fedc8fd6b5c822d90d [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 Petkov33af05c2012-02-28 10:10:30 +010015#include "shill/technology.h"
16#include "shill/vpn_driver.h"
17
Darin Petkov02867712012-03-12 14:25:05 +010018using base::StringPrintf;
19using std::replace_if;
Darin Petkov33af05c2012-02-28 10:10:30 +010020using std::string;
21
22namespace shill {
23
Darin Petkov79d74c92012-03-07 17:20:32 +010024VPNService::VPNService(ControlInterface *control,
Darin Petkov33af05c2012-02-28 10:10:30 +010025 EventDispatcher *dispatcher,
26 Metrics *metrics,
27 Manager *manager,
28 VPNDriver *driver)
Darin Petkov79d74c92012-03-07 17:20:32 +010029 : Service(control, dispatcher, metrics, manager, Technology::kVPN),
Paul Stewart22807992012-04-11 08:48:31 -070030 driver_(driver) {
Darin Petkov2f903b32012-04-18 12:56:43 +020031 set_connectable(true);
Darin Petkovcb715292012-04-25 13:04:37 +020032 set_save_credentials(false);
Paul Stewart22807992012-04-11 08:48:31 -070033 mutable_store()->RegisterString(flimflam::kVPNDomainProperty, &vpn_domain_);
34}
Darin Petkov33af05c2012-02-28 10:10:30 +010035
36VPNService::~VPNService() {}
37
Darin Petkov2f903b32012-04-18 12:56:43 +020038bool VPNService::TechnologyIs(const Technology::Identifier type) const {
39 return type == Technology::kVPN;
40}
41
Darin Petkov33af05c2012-02-28 10:10:30 +010042void VPNService::Connect(Error *error) {
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) {
53 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 Petkov33af05c2012-02-28 10:10:30 +0100116} // namespace shill