Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef SHILL_DHCP_PROVIDER_ |
| 6 | #define SHILL_DHCP_PROVIDER_ |
| 7 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 8 | #include <map> |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 9 | #include <string> |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 10 | |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 11 | #include <base/file_path.h> |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 12 | #include <base/lazy_instance.h> |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 13 | #include <base/memory/scoped_ptr.h> |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 14 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 15 | |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 16 | #include "shill/refptr_types.h" |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 17 | |
| 18 | namespace shill { |
| 19 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 20 | class ControlInterface; |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 21 | class DHCPCDListener; |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 22 | class EventDispatcher; |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 23 | class GLib; |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 24 | class ProxyFactory; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 25 | |
| 26 | // DHCPProvider is a singleton providing the main DHCP configuration |
| 27 | // entrypoint. Once the provider is initialized through its Init method, DHCP |
| 28 | // configurations for devices can be obtained through its CreateConfig |
| 29 | // method. For example, a single DHCP configuration request can be initiated as: |
| 30 | // |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 31 | // DHCPProvider::GetInstance()->CreateConfig(device_name, |
| 32 | // host_name, |
| 33 | // lease_file_suffix, |
| 34 | // arp_gateway)->Request(); |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 35 | class DHCPProvider { |
| 36 | public: |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 37 | static const char kDHCPCDPathFormatLease[]; |
| 38 | |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 39 | virtual ~DHCPProvider(); |
| 40 | |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 41 | // This is a singleton -- use DHCPProvider::GetInstance()->Foo() |
| 42 | static DHCPProvider *GetInstance(); |
| 43 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 44 | // Initializes the provider singleton. This method hooks up a D-Bus signal |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 45 | // listener that catches signals from spawned DHCP clients and dispatches them |
| 46 | // to the appropriate DHCP configuration instance. |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 47 | virtual void Init(ControlInterface *control_interface, |
| 48 | EventDispatcher *dispatcher, |
| 49 | GLib *glib); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 50 | |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame] | 51 | // Creates a new DHCPConfig for |device_name|. The DHCP configuration for the |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 52 | // device can then be initiated through DHCPConfig::Request and |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 53 | // DHCPConfig::Renew. If |host_name| is not-empty, it is placed in the DHCP |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 54 | // request to allow the server to map the request to a specific user-named |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 55 | // origin. The DHCP lease file will contain the suffix supplied |
| 56 | // in |lease_file_suffix| if non-empty, otherwise |device_name|. If |
| 57 | // |arp_gateway| is true, the DHCP client will ARP for the gateway IP |
| 58 | // address as an additional safeguard against the issued IP address being |
| 59 | // in-use by another station. |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 60 | virtual DHCPConfigRefPtr CreateConfig(const std::string &device_name, |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 61 | const std::string &host_name, |
| 62 | const std::string &lease_file_suffix, |
| 63 | bool arp_gateway); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 64 | |
| 65 | // Returns the DHCP configuration associated with DHCP client |pid|. Return |
| 66 | // NULL if |pid| is not bound to a configuration. |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 67 | DHCPConfigRefPtr GetConfig(int pid); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 68 | |
| 69 | // Binds a |pid| to a DHCP |config|. When a DHCP config spawns a new DHCP |
| 70 | // client, it binds itself to that client's |pid|. |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 71 | void BindPID(int pid, const DHCPConfigRefPtr &config); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 72 | |
| 73 | // Unbinds a |pid|. This method is used by a DHCP config to signal the |
| 74 | // provider that the DHCP client has been terminated. This may result in |
| 75 | // destruction of the DHCP config instance if its reference count goes to 0. |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 76 | void UnbindPID(int pid); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 77 | |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 78 | // Destroy lease file associated with this |name|. |
| 79 | virtual void DestroyLease(const std::string &name); |
| 80 | |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 81 | protected: |
| 82 | DHCPProvider(); |
| 83 | |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 84 | private: |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 85 | friend struct base::DefaultLazyInstanceTraits<DHCPProvider>; |
Darin Petkov | 77cb681 | 2011-08-15 16:19:41 -0700 | [diff] [blame] | 86 | friend class CellularTest; |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 87 | friend class DHCPProviderTest; |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 88 | friend class DeviceInfoTest; |
| 89 | friend class DeviceTest; |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 90 | FRIEND_TEST(DHCPProviderTest, CreateConfig); |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 91 | FRIEND_TEST(DHCPProviderTest, DestroyLease); |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 92 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 93 | typedef std::map<int, DHCPConfigRefPtr> PIDConfigMap; |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 94 | |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 95 | // Store cached copies of singletons for speed/ease of testing. |
| 96 | ProxyFactory *proxy_factory_; |
| 97 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 98 | // A single listener is used to catch signals from all DHCP clients and |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 99 | // dispatch them to the appropriate DHCP configuration instance. |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 100 | scoped_ptr<DHCPCDListener> listener_; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 101 | |
| 102 | // A map that binds PIDs to DHCP configuration instances. |
| 103 | PIDConfigMap configs_; |
| 104 | |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 105 | base::FilePath root_; |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 106 | ControlInterface *control_interface_; |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 107 | EventDispatcher *dispatcher_; |
| 108 | GLib *glib_; |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 109 | |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 110 | DISALLOW_COPY_AND_ASSIGN(DHCPProvider); |
| 111 | }; |
| 112 | |
| 113 | } // namespace shill |
| 114 | |
| 115 | #endif // SHILL_DHCP_PROVIDER_ |