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_provider.h" |
| 6 | |
| 7 | #include <base/logging.h> |
| 8 | #include <chromeos/dbus/service_constants.h> |
| 9 | |
| 10 | #include "shill/error.h" |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 11 | #include "shill/manager.h" |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 12 | #include "shill/openvpn_driver.h" |
| 13 | #include "shill/vpn_service.h" |
| 14 | |
| 15 | using std::string; |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 16 | using std::vector; |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 17 | |
| 18 | namespace shill { |
| 19 | |
| 20 | VPNProvider::VPNProvider(ControlInterface *control_interface, |
| 21 | EventDispatcher *dispatcher, |
| 22 | Metrics *metrics, |
| 23 | Manager *manager) |
| 24 | : control_interface_(control_interface), |
| 25 | dispatcher_(dispatcher), |
| 26 | metrics_(metrics), |
| 27 | manager_(manager) {} |
| 28 | |
| 29 | VPNProvider::~VPNProvider() {} |
| 30 | |
| 31 | void VPNProvider::Start() {} |
| 32 | |
| 33 | void VPNProvider::Stop() {} |
| 34 | |
| 35 | VPNServiceRefPtr VPNProvider::GetService(const KeyValueStore &args, |
| 36 | Error *error) { |
| 37 | VLOG(2) << __func__; |
Darin Petkov | 7f06033 | 2012-03-14 11:46:47 +0100 | [diff] [blame] | 38 | string type = args.LookupString(flimflam::kProviderTypeProperty, ""); |
| 39 | if (type.empty()) { |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 40 | Error::PopulateAndLog( |
| 41 | error, Error::kNotSupported, "Missing VPN type property."); |
| 42 | return NULL; |
| 43 | } |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 44 | |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 45 | string storage_id = VPNService::CreateStorageIdentifier(args, error); |
| 46 | if (storage_id.empty()) { |
| 47 | return NULL; |
| 48 | } |
| 49 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 50 | scoped_ptr<VPNDriver> driver; |
| 51 | if (type == flimflam::kProviderOpenVpn) { |
Darin Petkov | f20994f | 2012-03-05 16:12:19 +0100 | [diff] [blame] | 52 | driver.reset(new OpenVPNDriver( |
| 53 | control_interface_, dispatcher_, metrics_, manager_, |
Darin Petkov | 36a3ace | 2012-03-06 17:22:14 +0100 | [diff] [blame] | 54 | manager_->device_info(), manager_->glib(), args)); |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 55 | } else { |
| 56 | Error::PopulateAndLog( |
| 57 | error, Error::kNotSupported, "Unsupported VPN type: " + type); |
| 58 | return NULL; |
| 59 | } |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 60 | |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 61 | VPNServiceRefPtr service = new VPNService( |
| 62 | control_interface_, dispatcher_, metrics_, manager_, driver.release()); |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 63 | service->set_storage_id(storage_id); |
Darin Petkov | 7f06033 | 2012-03-14 11:46:47 +0100 | [diff] [blame] | 64 | string name = args.LookupString(flimflam::kProviderNameProperty, ""); |
| 65 | if (!name.empty()) { |
| 66 | service->set_friendly_name(name); |
| 67 | } |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 68 | services_.push_back(service); |
| 69 | manager_->RegisterService(service); |
| 70 | return service; |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | bool VPNProvider::OnDeviceInfoAvailable(const string &link_name, |
| 74 | int interface_index) { |
| 75 | for (vector<VPNServiceRefPtr>::const_iterator it = services_.begin(); |
| 76 | it != services_.end(); |
| 77 | ++it) { |
| 78 | if ((*it)->driver()->ClaimInterface(link_name, interface_index)) { |
| 79 | return true; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | return false; |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | } // namespace shill |