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