blob: fd8302952dc18745b1820692758229fdbe17eac0 [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>
Darin Petkovaceede32011-07-18 15:32:38 -07009#include <string>
Darin Petkovd1b715b2011-06-02 21:21:22 -070010
11#include <base/memory/scoped_ptr.h>
Darin Petkov50308cd2011-06-01 18:25:07 -070012#include <base/memory/singleton.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 Masone2b105542011-06-22 10:58:09 -070015#include "shill/refptr_types.h"
Darin Petkov50308cd2011-06-01 18:25:07 -070016
17namespace shill {
18
Chris Masone19e30402011-07-19 15:48:47 -070019class ControlInterface;
Darin Petkovaceede32011-07-18 15:32:38 -070020class DHCPCDListener;
Darin Petkova7b89492011-07-27 12:48:17 -070021class EventDispatcher;
Chris Masone2b105542011-06-22 10:58:09 -070022class GLib;
Darin Petkovd1b715b2011-06-02 21:21:22 -070023
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 Petkovf65e9282011-06-21 14:29:56 -070029// DHCPProvider::GetInstance()->CreateConfig(device_name)->Request();
Darin Petkov50308cd2011-06-01 18:25:07 -070030class DHCPProvider {
31 public:
32 // This is a singleton -- use DHCPProvider::GetInstance()->Foo()
33 static DHCPProvider *GetInstance();
34
Darin Petkovd1b715b2011-06-02 21:21:22 -070035 // Initializes the provider singleton. This method hooks up a D-Bus signal
Darin Petkovaceede32011-07-18 15:32:38 -070036 // listener that catches signals from spawned DHCP clients and dispatches them
37 // to the appropriate DHCP configuration instance.
Darin Petkova7b89492011-07-27 12:48:17 -070038 void Init(ControlInterface *control_interface,
39 EventDispatcher *dispatcher,
40 GLib *glib);
Darin Petkovd1b715b2011-06-02 21:21:22 -070041
Darin Petkovf65e9282011-06-21 14:29:56 -070042 // Creates a new DHCPConfig for |device_name|. The DHCP configuration for the
Darin Petkovd1b715b2011-06-02 21:21:22 -070043 // device can then be initiated through DHCPConfig::Request and
44 // DHCPConfig::Renew.
Darin Petkovf65e9282011-06-21 14:29:56 -070045 DHCPConfigRefPtr CreateConfig(const std::string &device_name);
Darin Petkovd1b715b2011-06-02 21:21:22 -070046
47 // Returns the DHCP configuration associated with DHCP client |pid|. Return
48 // NULL if |pid| is not bound to a configuration.
Darin Petkov98dd6a02011-06-10 15:12:57 -070049 DHCPConfigRefPtr GetConfig(int pid);
Darin Petkovd1b715b2011-06-02 21:21:22 -070050
51 // Binds a |pid| to a DHCP |config|. When a DHCP config spawns a new DHCP
52 // client, it binds itself to that client's |pid|.
Chris Masone2b105542011-06-22 10:58:09 -070053 void BindPID(int pid, const DHCPConfigRefPtr &config);
Darin Petkovd1b715b2011-06-02 21:21:22 -070054
55 // Unbinds a |pid|. This method is used by a DHCP config to signal the
56 // provider that the DHCP client has been terminated. This may result in
57 // destruction of the DHCP config instance if its reference count goes to 0.
Darin Petkov92c43902011-06-09 20:46:06 -070058 void UnbindPID(int pid);
Darin Petkovd1b715b2011-06-02 21:21:22 -070059
Darin Petkov50308cd2011-06-01 18:25:07 -070060 private:
61 friend struct DefaultSingletonTraits<DHCPProvider>;
Darin Petkov98dd6a02011-06-10 15:12:57 -070062 friend class DHCPProviderTest;
Darin Petkovafa6fc42011-06-21 16:21:08 -070063 friend class DeviceInfoTest;
64 friend class DeviceTest;
Darin Petkov98dd6a02011-06-10 15:12:57 -070065 FRIEND_TEST(DHCPProviderTest, CreateConfig);
Darin Petkovf7897bc2011-06-08 17:13:36 -070066
Darin Petkov92c43902011-06-09 20:46:06 -070067 typedef std::map<int, DHCPConfigRefPtr> PIDConfigMap;
Darin Petkov50308cd2011-06-01 18:25:07 -070068
Darin Petkovd1b715b2011-06-02 21:21:22 -070069 // Private to ensure that this behaves as a singleton.
Darin Petkov50308cd2011-06-01 18:25:07 -070070 DHCPProvider();
71 virtual ~DHCPProvider();
72
Darin Petkovd1b715b2011-06-02 21:21:22 -070073 // A single listener is used to catch signals from all DHCP clients and
Darin Petkovafa6fc42011-06-21 16:21:08 -070074 // dispatch them to the appropriate DHCP configuration instance.
Darin Petkovaceede32011-07-18 15:32:38 -070075 scoped_ptr<DHCPCDListener> listener_;
Darin Petkovd1b715b2011-06-02 21:21:22 -070076
77 // A map that binds PIDs to DHCP configuration instances.
78 PIDConfigMap configs_;
79
Chris Masone19e30402011-07-19 15:48:47 -070080 ControlInterface *control_interface_;
Darin Petkova7b89492011-07-27 12:48:17 -070081 EventDispatcher *dispatcher_;
82 GLib *glib_;
Darin Petkovf7897bc2011-06-08 17:13:36 -070083
Darin Petkov50308cd2011-06-01 18:25:07 -070084 DISALLOW_COPY_AND_ASSIGN(DHCPProvider);
85};
86
87} // namespace shill
88
89#endif // SHILL_DHCP_PROVIDER_