blob: eb03946c482ba9ebd3cdbacaaa2e080b9140c9fd [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
Paul Stewart0d2ada32011-08-09 17:01:57 -070011#include <base/lazy_instance.h>
Darin Petkovd1b715b2011-06-02 21:21:22 -070012#include <base/memory/scoped_ptr.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:
Paul Stewart0d2ada32011-08-09 17:01:57 -070032 virtual ~DHCPProvider();
33
Darin Petkov50308cd2011-06-01 18:25:07 -070034 // This is a singleton -- use DHCPProvider::GetInstance()->Foo()
35 static DHCPProvider *GetInstance();
36
Darin Petkovd1b715b2011-06-02 21:21:22 -070037 // Initializes the provider singleton. This method hooks up a D-Bus signal
Darin Petkovaceede32011-07-18 15:32:38 -070038 // listener that catches signals from spawned DHCP clients and dispatches them
39 // to the appropriate DHCP configuration instance.
Darin Petkova7b89492011-07-27 12:48:17 -070040 void Init(ControlInterface *control_interface,
41 EventDispatcher *dispatcher,
42 GLib *glib);
Darin Petkovd1b715b2011-06-02 21:21:22 -070043
Darin Petkovf65e9282011-06-21 14:29:56 -070044 // Creates a new DHCPConfig for |device_name|. The DHCP configuration for the
Darin Petkovd1b715b2011-06-02 21:21:22 -070045 // device can then be initiated through DHCPConfig::Request and
46 // DHCPConfig::Renew.
Darin Petkov77cb6812011-08-15 16:19:41 -070047 virtual DHCPConfigRefPtr CreateConfig(const std::string &device_name);
Darin Petkovd1b715b2011-06-02 21:21:22 -070048
49 // Returns the DHCP configuration associated with DHCP client |pid|. Return
50 // NULL if |pid| is not bound to a configuration.
Darin Petkov98dd6a02011-06-10 15:12:57 -070051 DHCPConfigRefPtr GetConfig(int pid);
Darin Petkovd1b715b2011-06-02 21:21:22 -070052
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 Masone2b105542011-06-22 10:58:09 -070055 void BindPID(int pid, const DHCPConfigRefPtr &config);
Darin Petkovd1b715b2011-06-02 21:21:22 -070056
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 Petkov92c43902011-06-09 20:46:06 -070060 void UnbindPID(int pid);
Darin Petkovd1b715b2011-06-02 21:21:22 -070061
Paul Stewart0d2ada32011-08-09 17:01:57 -070062 protected:
63 DHCPProvider();
64
Darin Petkov50308cd2011-06-01 18:25:07 -070065 private:
Paul Stewart0d2ada32011-08-09 17:01:57 -070066 friend struct base::DefaultLazyInstanceTraits<DHCPProvider>;
Darin Petkov77cb6812011-08-15 16:19:41 -070067 friend class CellularTest;
Darin Petkov98dd6a02011-06-10 15:12:57 -070068 friend class DHCPProviderTest;
Darin Petkovafa6fc42011-06-21 16:21:08 -070069 friend class DeviceInfoTest;
70 friend class DeviceTest;
Darin Petkov98dd6a02011-06-10 15:12:57 -070071 FRIEND_TEST(DHCPProviderTest, CreateConfig);
Darin Petkovf7897bc2011-06-08 17:13:36 -070072
Darin Petkov92c43902011-06-09 20:46:06 -070073 typedef std::map<int, DHCPConfigRefPtr> PIDConfigMap;
Darin Petkov50308cd2011-06-01 18:25:07 -070074
Darin Petkovd1b715b2011-06-02 21:21:22 -070075 // A single listener is used to catch signals from all DHCP clients and
Darin Petkovafa6fc42011-06-21 16:21:08 -070076 // dispatch them to the appropriate DHCP configuration instance.
Darin Petkovaceede32011-07-18 15:32:38 -070077 scoped_ptr<DHCPCDListener> listener_;
Darin Petkovd1b715b2011-06-02 21:21:22 -070078
79 // A map that binds PIDs to DHCP configuration instances.
80 PIDConfigMap configs_;
81
Chris Masone19e30402011-07-19 15:48:47 -070082 ControlInterface *control_interface_;
Darin Petkova7b89492011-07-27 12:48:17 -070083 EventDispatcher *dispatcher_;
84 GLib *glib_;
Darin Petkovf7897bc2011-06-08 17:13:36 -070085
Darin Petkov50308cd2011-06-01 18:25:07 -070086 DISALLOW_COPY_AND_ASSIGN(DHCPProvider);
87};
88
89} // namespace shill
90
91#endif // SHILL_DHCP_PROVIDER_