blob: bad79dd21d158d2c54292537c3ecc495baad2dcb [file] [log] [blame]
Thieu Lefb46caf2012-03-08 11:57:15 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkov50308cd2011-06-01 18:25:07 -07002// 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 Petkovab565bb2011-10-06 02:55:51 -070023class ProxyFactory;
Darin Petkovd1b715b2011-06-02 21:21:22 -070024
25// DHCPProvider is a singleton providing the main DHCP configuration
26// entrypoint. Once the provider is initialized through its Init method, DHCP
27// configurations for devices can be obtained through its CreateConfig
28// method. For example, a single DHCP configuration request can be initiated as:
29//
Paul Stewartd408fdf2012-05-07 17:15:57 -070030// DHCPProvider::GetInstance()->CreateConfig(device_name,
31// host_name,
32// lease_file_suffix,
33// arp_gateway)->Request();
Darin Petkov50308cd2011-06-01 18:25:07 -070034class DHCPProvider {
35 public:
Paul Stewart0d2ada32011-08-09 17:01:57 -070036 virtual ~DHCPProvider();
37
Darin Petkov50308cd2011-06-01 18:25:07 -070038 // This is a singleton -- use DHCPProvider::GetInstance()->Foo()
39 static DHCPProvider *GetInstance();
40
Darin Petkovd1b715b2011-06-02 21:21:22 -070041 // Initializes the provider singleton. This method hooks up a D-Bus signal
Darin Petkovaceede32011-07-18 15:32:38 -070042 // listener that catches signals from spawned DHCP clients and dispatches them
43 // to the appropriate DHCP configuration instance.
Thieu Lefb46caf2012-03-08 11:57:15 -080044 virtual void Init(ControlInterface *control_interface,
45 EventDispatcher *dispatcher,
46 GLib *glib);
Darin Petkovd1b715b2011-06-02 21:21:22 -070047
Darin Petkovf65e9282011-06-21 14:29:56 -070048 // Creates a new DHCPConfig for |device_name|. The DHCP configuration for the
Darin Petkovd1b715b2011-06-02 21:21:22 -070049 // device can then be initiated through DHCPConfig::Request and
Paul Stewartd408fdf2012-05-07 17:15:57 -070050 // DHCPConfig::Renew. If |host_name| is not-empty, it is placed in the DHCP
Paul Stewartd32f4842012-01-11 16:08:13 -080051 // request to allow the server to map the request to a specific user-named
Paul Stewartd408fdf2012-05-07 17:15:57 -070052 // origin. The DHCP lease file will contain the suffix supplied
53 // in |lease_file_suffix| if non-empty, otherwise |device_name|. If
54 // |arp_gateway| is true, the DHCP client will ARP for the gateway IP
55 // address as an additional safeguard against the issued IP address being
56 // in-use by another station.
Paul Stewartd32f4842012-01-11 16:08:13 -080057 virtual DHCPConfigRefPtr CreateConfig(const std::string &device_name,
Paul Stewartd408fdf2012-05-07 17:15:57 -070058 const std::string &host_name,
59 const std::string &lease_file_suffix,
60 bool arp_gateway);
Darin Petkovd1b715b2011-06-02 21:21:22 -070061
62 // Returns the DHCP configuration associated with DHCP client |pid|. Return
63 // NULL if |pid| is not bound to a configuration.
Darin Petkov98dd6a02011-06-10 15:12:57 -070064 DHCPConfigRefPtr GetConfig(int pid);
Darin Petkovd1b715b2011-06-02 21:21:22 -070065
66 // Binds a |pid| to a DHCP |config|. When a DHCP config spawns a new DHCP
67 // client, it binds itself to that client's |pid|.
Chris Masone2b105542011-06-22 10:58:09 -070068 void BindPID(int pid, const DHCPConfigRefPtr &config);
Darin Petkovd1b715b2011-06-02 21:21:22 -070069
70 // Unbinds a |pid|. This method is used by a DHCP config to signal the
71 // provider that the DHCP client has been terminated. This may result in
72 // destruction of the DHCP config instance if its reference count goes to 0.
Darin Petkov92c43902011-06-09 20:46:06 -070073 void UnbindPID(int pid);
Darin Petkovd1b715b2011-06-02 21:21:22 -070074
Paul Stewart0d2ada32011-08-09 17:01:57 -070075 protected:
76 DHCPProvider();
77
Darin Petkov50308cd2011-06-01 18:25:07 -070078 private:
Paul Stewart0d2ada32011-08-09 17:01:57 -070079 friend struct base::DefaultLazyInstanceTraits<DHCPProvider>;
Darin Petkov77cb6812011-08-15 16:19:41 -070080 friend class CellularTest;
Darin Petkov98dd6a02011-06-10 15:12:57 -070081 friend class DHCPProviderTest;
Darin Petkovafa6fc42011-06-21 16:21:08 -070082 friend class DeviceInfoTest;
83 friend class DeviceTest;
Darin Petkov98dd6a02011-06-10 15:12:57 -070084 FRIEND_TEST(DHCPProviderTest, CreateConfig);
Darin Petkovf7897bc2011-06-08 17:13:36 -070085
Darin Petkov92c43902011-06-09 20:46:06 -070086 typedef std::map<int, DHCPConfigRefPtr> PIDConfigMap;
Darin Petkov50308cd2011-06-01 18:25:07 -070087
Darin Petkovab565bb2011-10-06 02:55:51 -070088 // Store cached copies of singletons for speed/ease of testing.
89 ProxyFactory *proxy_factory_;
90
Darin Petkovd1b715b2011-06-02 21:21:22 -070091 // A single listener is used to catch signals from all DHCP clients and
Darin Petkovafa6fc42011-06-21 16:21:08 -070092 // dispatch them to the appropriate DHCP configuration instance.
Darin Petkovaceede32011-07-18 15:32:38 -070093 scoped_ptr<DHCPCDListener> listener_;
Darin Petkovd1b715b2011-06-02 21:21:22 -070094
95 // A map that binds PIDs to DHCP configuration instances.
96 PIDConfigMap configs_;
97
Chris Masone19e30402011-07-19 15:48:47 -070098 ControlInterface *control_interface_;
Darin Petkova7b89492011-07-27 12:48:17 -070099 EventDispatcher *dispatcher_;
100 GLib *glib_;
Darin Petkovf7897bc2011-06-08 17:13:36 -0700101
Darin Petkov50308cd2011-06-01 18:25:07 -0700102 DISALLOW_COPY_AND_ASSIGN(DHCPProvider);
103};
104
105} // namespace shill
106
107#endif // SHILL_DHCP_PROVIDER_