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> |
| 9 | |
| 10 | #include <base/memory/scoped_ptr.h> |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 11 | #include <base/memory/singleton.h> |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 12 | #include <dbus-c++/connection.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 | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 15 | #include "shill/device.h" |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 16 | #include "shill/dhcp_config.h" |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 17 | |
| 18 | namespace shill { |
| 19 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 20 | class DHCPListenerInterface; |
| 21 | class Device; |
| 22 | |
| 23 | // DHCPProvider is a singleton providing the main DHCP configuration |
| 24 | // entrypoint. Once the provider is initialized through its Init method, DHCP |
| 25 | // configurations for devices can be obtained through its CreateConfig |
| 26 | // method. For example, a single DHCP configuration request can be initiated as: |
| 27 | // |
| 28 | // DHCPProvider::GetInstance()->CreateConfig(device)->Request(); |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 29 | class DHCPProvider { |
| 30 | public: |
| 31 | // This is a singleton -- use DHCPProvider::GetInstance()->Foo() |
| 32 | static DHCPProvider *GetInstance(); |
| 33 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 34 | // Initializes the provider singleton. This method hooks up a D-Bus signal |
| 35 | // listener to |connection| that catches signals from spawned DHCP clients and |
| 36 | // dispatches them to the appropriate DHCP configuration instance. |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame^] | 37 | void Init(DBus::Connection *connection, GLibInterface *glib); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 38 | |
| 39 | // Creates a new DHCPConfig for |device|. The DHCP configuration for the |
| 40 | // device can then be initiated through DHCPConfig::Request and |
| 41 | // DHCPConfig::Renew. |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 42 | DHCPConfigRefPtr CreateConfig(DeviceConstRefPtr device); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 43 | |
| 44 | // Returns the DHCP configuration associated with DHCP client |pid|. Return |
| 45 | // NULL if |pid| is not bound to a configuration. |
| 46 | DHCPConfigRefPtr GetConfig(unsigned int pid); |
| 47 | |
| 48 | // Binds a |pid| to a DHCP |config|. When a DHCP config spawns a new DHCP |
| 49 | // client, it binds itself to that client's |pid|. |
| 50 | void BindPID(unsigned int pid, DHCPConfigRefPtr config); |
| 51 | |
| 52 | // Unbinds a |pid|. This method is used by a DHCP config to signal the |
| 53 | // provider that the DHCP client has been terminated. This may result in |
| 54 | // destruction of the DHCP config instance if its reference count goes to 0. |
| 55 | void UnbindPID(unsigned int pid); |
| 56 | |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 57 | private: |
| 58 | friend struct DefaultSingletonTraits<DHCPProvider>; |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame^] | 59 | FRIEND_TEST(DHCPConfigTest, Start); |
| 60 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 61 | typedef std::map<unsigned int, DHCPConfigRefPtr> PIDConfigMap; |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 62 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 63 | // Private to ensure that this behaves as a singleton. |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 64 | DHCPProvider(); |
| 65 | virtual ~DHCPProvider(); |
| 66 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 67 | // A single listener is used to catch signals from all DHCP clients and |
| 68 | // dispatch them to the appropriate proxy. |
| 69 | scoped_ptr<DHCPListenerInterface> listener_; |
| 70 | |
| 71 | // A map that binds PIDs to DHCP configuration instances. |
| 72 | PIDConfigMap configs_; |
| 73 | |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame^] | 74 | GLibInterface *glib_; |
| 75 | |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 76 | DISALLOW_COPY_AND_ASSIGN(DHCPProvider); |
| 77 | }; |
| 78 | |
| 79 | } // namespace shill |
| 80 | |
| 81 | #endif // SHILL_DHCP_PROVIDER_ |