blob: 28e07e49fabef5b662c384c4f52824619966b7bf [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
Chris Masonec1e50412011-06-07 13:04:53 -070015#include "shill/device.h"
Darin Petkovd1b715b2011-06-02 21:21:22 -070016#include "shill/dhcp_config.h"
Darin Petkov50308cd2011-06-01 18:25:07 -070017
18namespace shill {
19
Darin Petkovd1b715b2011-06-02 21:21:22 -070020class DHCPListenerInterface;
21class 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 Petkov50308cd2011-06-01 18:25:07 -070029class DHCPProvider {
30 public:
31 // This is a singleton -- use DHCPProvider::GetInstance()->Foo()
32 static DHCPProvider *GetInstance();
33
Darin Petkovd1b715b2011-06-02 21:21:22 -070034 // 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 Petkovf7897bc2011-06-08 17:13:36 -070037 void Init(DBus::Connection *connection, GLibInterface *glib);
Darin Petkovd1b715b2011-06-02 21:21:22 -070038
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 Masonec1e50412011-06-07 13:04:53 -070042 DHCPConfigRefPtr CreateConfig(DeviceConstRefPtr device);
Darin Petkovd1b715b2011-06-02 21:21:22 -070043
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 Petkov50308cd2011-06-01 18:25:07 -070057 private:
58 friend struct DefaultSingletonTraits<DHCPProvider>;
Darin Petkovf7897bc2011-06-08 17:13:36 -070059 FRIEND_TEST(DHCPConfigTest, Start);
60
Darin Petkovd1b715b2011-06-02 21:21:22 -070061 typedef std::map<unsigned int, DHCPConfigRefPtr> PIDConfigMap;
Darin Petkov50308cd2011-06-01 18:25:07 -070062
Darin Petkovd1b715b2011-06-02 21:21:22 -070063 // Private to ensure that this behaves as a singleton.
Darin Petkov50308cd2011-06-01 18:25:07 -070064 DHCPProvider();
65 virtual ~DHCPProvider();
66
Darin Petkovd1b715b2011-06-02 21:21:22 -070067 // 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 Petkovf7897bc2011-06-08 17:13:36 -070074 GLibInterface *glib_;
75
Darin Petkov50308cd2011-06-01 18:25:07 -070076 DISALLOW_COPY_AND_ASSIGN(DHCPProvider);
77};
78
79} // namespace shill
80
81#endif // SHILL_DHCP_PROVIDER_