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