blob: 4f4ae2bda0cbfcba0860b34fd59fcd4b48d00862 [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_provider.h"
6
Paul Stewart65512e12012-03-26 18:01:08 -07007#include <algorithm>
8
Darin Petkov33af05c2012-02-28 10:10:30 +01009#include <base/logging.h>
10#include <chromeos/dbus/service_constants.h>
11
12#include "shill/error.h"
Paul Stewartca6abd42012-03-01 15:45:29 -080013#include "shill/manager.h"
Darin Petkov33af05c2012-02-28 10:10:30 +010014#include "shill/openvpn_driver.h"
15#include "shill/vpn_service.h"
16
17using std::string;
Paul Stewartca6abd42012-03-01 15:45:29 -080018using std::vector;
Darin Petkov33af05c2012-02-28 10:10:30 +010019
20namespace shill {
21
22VPNProvider::VPNProvider(ControlInterface *control_interface,
23 EventDispatcher *dispatcher,
24 Metrics *metrics,
25 Manager *manager)
26 : control_interface_(control_interface),
27 dispatcher_(dispatcher),
28 metrics_(metrics),
29 manager_(manager) {}
30
31VPNProvider::~VPNProvider() {}
32
33void VPNProvider::Start() {}
34
35void VPNProvider::Stop() {}
36
37VPNServiceRefPtr VPNProvider::GetService(const KeyValueStore &args,
38 Error *error) {
39 VLOG(2) << __func__;
Darin Petkov7f060332012-03-14 11:46:47 +010040 string type = args.LookupString(flimflam::kProviderTypeProperty, "");
41 if (type.empty()) {
Darin Petkov33af05c2012-02-28 10:10:30 +010042 Error::PopulateAndLog(
43 error, Error::kNotSupported, "Missing VPN type property.");
44 return NULL;
45 }
Paul Stewartca6abd42012-03-01 15:45:29 -080046
Darin Petkov02867712012-03-12 14:25:05 +010047 string storage_id = VPNService::CreateStorageIdentifier(args, error);
48 if (storage_id.empty()) {
49 return NULL;
50 }
51
Paul Stewart39964fa2012-04-04 09:50:25 -070052 // Find a service in the provider list which matches these parameters.
53 for (vector<VPNServiceRefPtr>::const_iterator it = services_.begin();
54 it != services_.end();
55 ++it) {
56 if (type == (*it)->driver()->GetProviderType() &&
57 (*it)->GetStorageIdentifier() == storage_id) {
58 return *it;
59 }
60 }
61
Darin Petkov33af05c2012-02-28 10:10:30 +010062 scoped_ptr<VPNDriver> driver;
63 if (type == flimflam::kProviderOpenVpn) {
Darin Petkovf20994f2012-03-05 16:12:19 +010064 driver.reset(new OpenVPNDriver(
65 control_interface_, dispatcher_, metrics_, manager_,
Darin Petkov36a3ace2012-03-06 17:22:14 +010066 manager_->device_info(), manager_->glib(), args));
Darin Petkov33af05c2012-02-28 10:10:30 +010067 } else {
68 Error::PopulateAndLog(
69 error, Error::kNotSupported, "Unsupported VPN type: " + type);
70 return NULL;
71 }
Paul Stewartca6abd42012-03-01 15:45:29 -080072
Darin Petkov79d74c92012-03-07 17:20:32 +010073 VPNServiceRefPtr service = new VPNService(
74 control_interface_, dispatcher_, metrics_, manager_, driver.release());
Darin Petkov02867712012-03-12 14:25:05 +010075 service->set_storage_id(storage_id);
Paul Stewartebd38562012-03-23 13:06:40 -070076 service->InitDriverPropertyStore();
Darin Petkov7f060332012-03-14 11:46:47 +010077 string name = args.LookupString(flimflam::kProviderNameProperty, "");
78 if (!name.empty()) {
79 service->set_friendly_name(name);
80 }
Darin Petkov79d74c92012-03-07 17:20:32 +010081 services_.push_back(service);
82 manager_->RegisterService(service);
83 return service;
Paul Stewartca6abd42012-03-01 15:45:29 -080084}
85
86bool VPNProvider::OnDeviceInfoAvailable(const string &link_name,
87 int interface_index) {
88 for (vector<VPNServiceRefPtr>::const_iterator it = services_.begin();
89 it != services_.end();
90 ++it) {
91 if ((*it)->driver()->ClaimInterface(link_name, interface_index)) {
92 return true;
93 }
94 }
95
96 return false;
Darin Petkov33af05c2012-02-28 10:10:30 +010097}
98
Paul Stewart65512e12012-03-26 18:01:08 -070099void VPNProvider::RemoveService(VPNServiceRefPtr service) {
100 vector<VPNServiceRefPtr>::iterator it;
101 it = std::find(services_.begin(), services_.end(), service);
102 if (it != services_.end()) {
103 services_.erase(it);
104 }
105}
106
Darin Petkov33af05c2012-02-28 10:10:30 +0100107} // namespace shill