blob: f7ac4f4010e9cad8a7153880bb69246a26bb8885 [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>
13
14#include "shill/dhcp_config.h"
Darin Petkov50308cd2011-06-01 18:25:07 -070015
16namespace shill {
17
Darin Petkovd1b715b2011-06-02 21:21:22 -070018class DHCPListenerInterface;
19class Device;
20
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//
26// DHCPProvider::GetInstance()->CreateConfig(device)->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.
35 void Init(DBus::Connection *connection);
36
37 // Creates a new DHCPConfig for |device|. The DHCP configuration for the
38 // device can then be initiated through DHCPConfig::Request and
39 // DHCPConfig::Renew.
40 DHCPConfigRefPtr CreateConfig(const Device &device);
41
42 // Returns the DHCP configuration associated with DHCP client |pid|. Return
43 // NULL if |pid| is not bound to a configuration.
44 DHCPConfigRefPtr GetConfig(unsigned int pid);
45
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|.
48 void BindPID(unsigned int pid, DHCPConfigRefPtr config);
49
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.
53 void UnbindPID(unsigned int pid);
54
Darin Petkov50308cd2011-06-01 18:25:07 -070055 private:
56 friend struct DefaultSingletonTraits<DHCPProvider>;
Darin Petkovd1b715b2011-06-02 21:21:22 -070057 typedef std::map<unsigned int, DHCPConfigRefPtr> PIDConfigMap;
Darin Petkov50308cd2011-06-01 18:25:07 -070058
Darin Petkovd1b715b2011-06-02 21:21:22 -070059 // Private to ensure that this behaves as a singleton.
Darin Petkov50308cd2011-06-01 18:25:07 -070060 DHCPProvider();
61 virtual ~DHCPProvider();
62
Darin Petkovd1b715b2011-06-02 21:21:22 -070063 // A single listener is used to catch signals from all DHCP clients and
64 // dispatch them to the appropriate proxy.
65 scoped_ptr<DHCPListenerInterface> listener_;
66
67 // A map that binds PIDs to DHCP configuration instances.
68 PIDConfigMap configs_;
69
Darin Petkov50308cd2011-06-01 18:25:07 -070070 DISALLOW_COPY_AND_ASSIGN(DHCPProvider);
71};
72
73} // namespace shill
74
75#endif // SHILL_DHCP_PROVIDER_