Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 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 | #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 | |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 11 | #include <base/lazy_instance.h> |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 12 | #include <base/memory/scoped_ptr.h> |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 13 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 14 | |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 15 | #include "shill/refptr_types.h" |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 16 | |
| 17 | namespace shill { |
| 18 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 19 | class ControlInterface; |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 20 | class DHCPCDListener; |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 21 | class EventDispatcher; |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 22 | class GLib; |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 23 | class ProxyFactory; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 24 | |
| 25 | // DHCPProvider is a singleton providing the main DHCP configuration |
| 26 | // entrypoint. Once the provider is initialized through its Init method, DHCP |
| 27 | // configurations for devices can be obtained through its CreateConfig |
| 28 | // method. For example, a single DHCP configuration request can be initiated as: |
| 29 | // |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame^] | 30 | // DHCPProvider::GetInstance()->CreateConfig(device_name, host_name)->Request(); |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 31 | class DHCPProvider { |
| 32 | public: |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 33 | virtual ~DHCPProvider(); |
| 34 | |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 35 | // This is a singleton -- use DHCPProvider::GetInstance()->Foo() |
| 36 | static DHCPProvider *GetInstance(); |
| 37 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 38 | // Initializes the provider singleton. This method hooks up a D-Bus signal |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 39 | // listener that catches signals from spawned DHCP clients and dispatches them |
| 40 | // to the appropriate DHCP configuration instance. |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 41 | void Init(ControlInterface *control_interface, |
| 42 | EventDispatcher *dispatcher, |
| 43 | GLib *glib); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 44 | |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame] | 45 | // Creates a new DHCPConfig for |device_name|. The DHCP configuration for the |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 46 | // device can then be initiated through DHCPConfig::Request and |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame^] | 47 | // DHCPConfig::Renew. If |hostname| is not-empty, it is placed in the DHCP |
| 48 | // request to allow the server to map the request to a specific user-named |
| 49 | // origin. |
| 50 | virtual DHCPConfigRefPtr CreateConfig(const std::string &device_name, |
| 51 | const std::string &host_name); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 52 | |
| 53 | // Returns the DHCP configuration associated with DHCP client |pid|. Return |
| 54 | // NULL if |pid| is not bound to a configuration. |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 55 | DHCPConfigRefPtr GetConfig(int pid); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 56 | |
| 57 | // Binds a |pid| to a DHCP |config|. When a DHCP config spawns a new DHCP |
| 58 | // client, it binds itself to that client's |pid|. |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 59 | void BindPID(int pid, const DHCPConfigRefPtr &config); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 60 | |
| 61 | // Unbinds a |pid|. This method is used by a DHCP config to signal the |
| 62 | // provider that the DHCP client has been terminated. This may result in |
| 63 | // 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] | 64 | void UnbindPID(int pid); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 65 | |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 66 | protected: |
| 67 | DHCPProvider(); |
| 68 | |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 69 | private: |
Paul Stewart | 0d2ada3 | 2011-08-09 17:01:57 -0700 | [diff] [blame] | 70 | friend struct base::DefaultLazyInstanceTraits<DHCPProvider>; |
Darin Petkov | 77cb681 | 2011-08-15 16:19:41 -0700 | [diff] [blame] | 71 | friend class CellularTest; |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 72 | friend class DHCPProviderTest; |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 73 | friend class DeviceInfoTest; |
| 74 | friend class DeviceTest; |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 75 | FRIEND_TEST(DHCPProviderTest, CreateConfig); |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 76 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 77 | typedef std::map<int, DHCPConfigRefPtr> PIDConfigMap; |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 78 | |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 79 | // Store cached copies of singletons for speed/ease of testing. |
| 80 | ProxyFactory *proxy_factory_; |
| 81 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 82 | // 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] | 83 | // dispatch them to the appropriate DHCP configuration instance. |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 84 | scoped_ptr<DHCPCDListener> listener_; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 85 | |
| 86 | // A map that binds PIDs to DHCP configuration instances. |
| 87 | PIDConfigMap configs_; |
| 88 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 89 | ControlInterface *control_interface_; |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 90 | EventDispatcher *dispatcher_; |
| 91 | GLib *glib_; |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 92 | |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 93 | DISALLOW_COPY_AND_ASSIGN(DHCPProvider); |
| 94 | }; |
| 95 | |
| 96 | } // namespace shill |
| 97 | |
| 98 | #endif // SHILL_DHCP_PROVIDER_ |