blob: a0f39142a62602849a383c7ef7522a7e7e9d09da [file] [log] [blame]
Darin Petkov50308cd2011-06-01 18:25:07 -07001// 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 Petkovd1b715b2011-06-02 21:21:22 -07008#include <map>
9
10#include <base/memory/scoped_ptr.h>
Darin Petkov50308cd2011-06-01 18:25:07 -070011#include <base/memory/singleton.h>
Darin Petkovd1b715b2011-06-02 21:21:22 -070012#include <dbus-c++/connection.h>
Darin Petkovf7897bc2011-06-08 17:13:36 -070013#include <gtest/gtest_prod.h> // for FRIEND_TEST
Darin Petkovd1b715b2011-06-02 21:21:22 -070014
15#include "shill/dhcp_config.h"
Darin Petkov50308cd2011-06-01 18:25:07 -070016
17namespace shill {
18
Darin Petkovd1b715b2011-06-02 21:21:22 -070019class DHCPListenerInterface;
Darin Petkovd1b715b2011-06-02 21:21:22 -070020
21// DHCPProvider is a singleton providing the main DHCP configuration
22// entrypoint. Once the provider is initialized through its Init method, DHCP
23// configurations for devices can be obtained through its CreateConfig
24// method. For example, a single DHCP configuration request can be initiated as:
25//
Darin Petkovf65e9282011-06-21 14:29:56 -070026// DHCPProvider::GetInstance()->CreateConfig(device_name)->Request();
Darin Petkov50308cd2011-06-01 18:25:07 -070027class DHCPProvider {
28 public:
29 // This is a singleton -- use DHCPProvider::GetInstance()->Foo()
30 static DHCPProvider *GetInstance();
31
Darin Petkovd1b715b2011-06-02 21:21:22 -070032 // Initializes the provider singleton. This method hooks up a D-Bus signal
33 // listener to |connection| that catches signals from spawned DHCP clients and
34 // dispatches them to the appropriate DHCP configuration instance.
Darin Petkovf7897bc2011-06-08 17:13:36 -070035 void Init(DBus::Connection *connection, GLibInterface *glib);
Darin Petkovd1b715b2011-06-02 21:21:22 -070036
Darin Petkovf65e9282011-06-21 14:29:56 -070037 // Creates a new DHCPConfig for |device_name|. The DHCP configuration for the
Darin Petkovd1b715b2011-06-02 21:21:22 -070038 // device can then be initiated through DHCPConfig::Request and
39 // DHCPConfig::Renew.
Darin Petkovf65e9282011-06-21 14:29:56 -070040 DHCPConfigRefPtr CreateConfig(const std::string &device_name);
Darin Petkovd1b715b2011-06-02 21:21:22 -070041
42 // Returns the DHCP configuration associated with DHCP client |pid|. Return
43 // NULL if |pid| is not bound to a configuration.
Darin Petkov98dd6a02011-06-10 15:12:57 -070044 DHCPConfigRefPtr GetConfig(int pid);
Darin Petkovd1b715b2011-06-02 21:21:22 -070045
46 // Binds a |pid| to a DHCP |config|. When a DHCP config spawns a new DHCP
47 // client, it binds itself to that client's |pid|.
Darin Petkov92c43902011-06-09 20:46:06 -070048 void BindPID(int pid, DHCPConfigRefPtr config);
Darin Petkovd1b715b2011-06-02 21:21:22 -070049
50 // Unbinds a |pid|. This method is used by a DHCP config to signal the
51 // provider that the DHCP client has been terminated. This may result in
52 // destruction of the DHCP config instance if its reference count goes to 0.
Darin Petkov92c43902011-06-09 20:46:06 -070053 void UnbindPID(int pid);
Darin Petkovd1b715b2011-06-02 21:21:22 -070054
Darin Petkov50308cd2011-06-01 18:25:07 -070055 private:
56 friend struct DefaultSingletonTraits<DHCPProvider>;
Darin Petkov98dd6a02011-06-10 15:12:57 -070057 friend class DHCPProviderTest;
58 FRIEND_TEST(DHCPProviderTest, CreateConfig);
Darin Petkovf7897bc2011-06-08 17:13:36 -070059
Darin Petkov92c43902011-06-09 20:46:06 -070060 typedef std::map<int, DHCPConfigRefPtr> PIDConfigMap;
Darin Petkov50308cd2011-06-01 18:25:07 -070061
Darin Petkovd1b715b2011-06-02 21:21:22 -070062 // Private to ensure that this behaves as a singleton.
Darin Petkov50308cd2011-06-01 18:25:07 -070063 DHCPProvider();
64 virtual ~DHCPProvider();
65
Darin Petkovd1b715b2011-06-02 21:21:22 -070066 // A single listener is used to catch signals from all DHCP clients and
67 // dispatch them to the appropriate proxy.
68 scoped_ptr<DHCPListenerInterface> listener_;
69
70 // A map that binds PIDs to DHCP configuration instances.
71 PIDConfigMap configs_;
72
Darin Petkovf7897bc2011-06-08 17:13:36 -070073 GLibInterface *glib_;
74
Darin Petkov50308cd2011-06-01 18:25:07 -070075 DISALLOW_COPY_AND_ASSIGN(DHCPProvider);
76};
77
78} // namespace shill
79
80#endif // SHILL_DHCP_PROVIDER_