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