blob: 757361363c44bbe0e4c086b6204587d8a44fa5c1 [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);
Paul Stewart22807992012-04-11 08:48:31 -070032 mutable_store()->RegisterString(flimflam::kVPNDomainProperty, &vpn_domain_);
33}
Darin Petkov33af05c2012-02-28 10:10:30 +010034
35VPNService::~VPNService() {}
36
Darin Petkov2f903b32012-04-18 12:56:43 +020037bool VPNService::TechnologyIs(const Technology::Identifier type) const {
38 return type == Technology::kVPN;
39}
40
Darin Petkov33af05c2012-02-28 10:10:30 +010041void VPNService::Connect(Error *error) {
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) {
52 Service::Disconnect(error);
53 driver_->Disconnect();
54}
55
Darin Petkov33af05c2012-02-28 10:10:30 +010056string VPNService::GetStorageIdentifier() const {
Darin Petkov02867712012-03-12 14:25:05 +010057 return storage_id_;
58}
59
60// static
61string VPNService::CreateStorageIdentifier(const KeyValueStore &args,
62 Error *error) {
Darin Petkov7f060332012-03-14 11:46:47 +010063 string host = args.LookupString(flimflam::kProviderHostProperty, "");
Darin Petkov02867712012-03-12 14:25:05 +010064 if (host.empty()) {
65 Error::PopulateAndLog(
66 error, Error::kInvalidProperty, "Missing VPN host.");
67 return "";
68 }
Darin Petkov7f060332012-03-14 11:46:47 +010069 string name = args.LookupString(flimflam::kProviderNameProperty, "");
Darin Petkov02867712012-03-12 14:25:05 +010070 if (name.empty()) {
Paul Stewart451aa7f2012-04-11 19:07:58 -070071 name = args.LookupString(flimflam::kNameProperty, "");
72 if (name.empty()) {
73 Error::PopulateAndLog(
74 error, Error::kNotSupported, "Missing VPN name.");
75 return "";
76 }
Darin Petkov02867712012-03-12 14:25:05 +010077 }
78 string id = StringPrintf("vpn_%s_%s", host.c_str(), name.c_str());
79 replace_if(id.begin(), id.end(), &Service::IllegalChar, '_');
80 return id;
Darin Petkov33af05c2012-02-28 10:10:30 +010081}
82
83string VPNService::GetDeviceRpcId(Error *error) {
Darin Petkov33af05c2012-02-28 10:10:30 +010084 error->Populate(Error::kNotSupported);
85 return "/";
86}
87
Darin Petkovf3c71d72012-03-21 12:32:15 +010088bool VPNService::Load(StoreInterface *storage) {
89 return Service::Load(storage) &&
90 driver_->Load(storage, GetStorageIdentifier());
91}
92
93bool VPNService::Save(StoreInterface *storage) {
94 return Service::Save(storage) &&
95 driver_->Save(storage, GetStorageIdentifier());
96}
97
Paul Stewart65512e12012-03-26 18:01:08 -070098bool VPNService::Unload() {
99 Service::Unload();
100
101 // VPN services which have been removed from the profile should be
102 // disconnected.
103 driver_->Disconnect();
104
105 // Ask the VPN provider to remove us from its list.
106 manager()->vpn_provider()->RemoveService(this);
107
108 return true;
109}
110
Paul Stewartebd38562012-03-23 13:06:40 -0700111void VPNService::InitDriverPropertyStore() {
112 driver_->InitPropertyStore(mutable_store());
113}
114
Darin Petkov33af05c2012-02-28 10:10:30 +0100115} // namespace shill