blob: 901a858cf58ba3d7e6ae23fad5f9ec8b053ac298 [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
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080011#include <base/file_path.h>
Paul Stewart0d2ada32011-08-09 17:01:57 -070012#include <base/lazy_instance.h>
Darin Petkovd1b715b2011-06-02 21:21:22 -070013#include <base/memory/scoped_ptr.h>
Darin Petkovf7897bc2011-06-08 17:13:36 -070014#include <gtest/gtest_prod.h> // for FRIEND_TEST
Darin Petkovd1b715b2011-06-02 21:21:22 -070015
Chris Masone2b105542011-06-22 10:58:09 -070016#include "shill/refptr_types.h"
Darin Petkov50308cd2011-06-01 18:25:07 -070017
18namespace shill {
19
Chris Masone19e30402011-07-19 15:48:47 -070020class ControlInterface;
Darin Petkovaceede32011-07-18 15:32:38 -070021class DHCPCDListener;
Darin Petkova7b89492011-07-27 12:48:17 -070022class EventDispatcher;
Chris Masone2b105542011-06-22 10:58:09 -070023class GLib;
Darin Petkovab565bb2011-10-06 02:55:51 -070024class ProxyFactory;
Darin Petkovd1b715b2011-06-02 21:21:22 -070025
26// DHCPProvider is a singleton providing the main DHCP configuration
27// entrypoint. Once the provider is initialized through its Init method, DHCP
28// configurations for devices can be obtained through its CreateConfig
29// method. For example, a single DHCP configuration request can be initiated as:
30//
Paul Stewartd408fdf2012-05-07 17:15:57 -070031// DHCPProvider::GetInstance()->CreateConfig(device_name,
32// host_name,
33// lease_file_suffix,
34// arp_gateway)->Request();
Darin Petkov50308cd2011-06-01 18:25:07 -070035class DHCPProvider {
36 public:
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080037 static const char kDHCPCDPathFormatLease[];
38
Paul Stewart0d2ada32011-08-09 17:01:57 -070039 virtual ~DHCPProvider();
40
Darin Petkov50308cd2011-06-01 18:25:07 -070041 // This is a singleton -- use DHCPProvider::GetInstance()->Foo()
42 static DHCPProvider *GetInstance();
43
Darin Petkovd1b715b2011-06-02 21:21:22 -070044 // Initializes the provider singleton. This method hooks up a D-Bus signal
Darin Petkovaceede32011-07-18 15:32:38 -070045 // listener that catches signals from spawned DHCP clients and dispatches them
46 // to the appropriate DHCP configuration instance.
Thieu Lefb46caf2012-03-08 11:57:15 -080047 virtual void Init(ControlInterface *control_interface,
48 EventDispatcher *dispatcher,
49 GLib *glib);
Darin Petkovd1b715b2011-06-02 21:21:22 -070050
Darin Petkovf65e9282011-06-21 14:29:56 -070051 // Creates a new DHCPConfig for |device_name|. The DHCP configuration for the
Darin Petkovd1b715b2011-06-02 21:21:22 -070052 // device can then be initiated through DHCPConfig::Request and
Paul Stewartd408fdf2012-05-07 17:15:57 -070053 // DHCPConfig::Renew. If |host_name| is not-empty, it is placed in the DHCP
Paul Stewartd32f4842012-01-11 16:08:13 -080054 // request to allow the server to map the request to a specific user-named
Paul Stewartd408fdf2012-05-07 17:15:57 -070055 // origin. The DHCP lease file will contain the suffix supplied
56 // in |lease_file_suffix| if non-empty, otherwise |device_name|. If
57 // |arp_gateway| is true, the DHCP client will ARP for the gateway IP
58 // address as an additional safeguard against the issued IP address being
59 // in-use by another station.
Paul Stewartd32f4842012-01-11 16:08:13 -080060 virtual DHCPConfigRefPtr CreateConfig(const std::string &device_name,
Paul Stewartd408fdf2012-05-07 17:15:57 -070061 const std::string &host_name,
62 const std::string &lease_file_suffix,
63 bool arp_gateway);
Darin Petkovd1b715b2011-06-02 21:21:22 -070064
65 // Returns the DHCP configuration associated with DHCP client |pid|. Return
66 // NULL if |pid| is not bound to a configuration.
Darin Petkov98dd6a02011-06-10 15:12:57 -070067 DHCPConfigRefPtr GetConfig(int pid);
Darin Petkovd1b715b2011-06-02 21:21:22 -070068
69 // Binds a |pid| to a DHCP |config|. When a DHCP config spawns a new DHCP
70 // client, it binds itself to that client's |pid|.
Chris Masone2b105542011-06-22 10:58:09 -070071 void BindPID(int pid, const DHCPConfigRefPtr &config);
Darin Petkovd1b715b2011-06-02 21:21:22 -070072
73 // Unbinds a |pid|. This method is used by a DHCP config to signal the
74 // provider that the DHCP client has been terminated. This may result in
75 // destruction of the DHCP config instance if its reference count goes to 0.
Darin Petkov92c43902011-06-09 20:46:06 -070076 void UnbindPID(int pid);
Darin Petkovd1b715b2011-06-02 21:21:22 -070077
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080078 // Destroy lease file associated with this |name|.
79 virtual void DestroyLease(const std::string &name);
80
Paul Stewart0d2ada32011-08-09 17:01:57 -070081 protected:
82 DHCPProvider();
83
Darin Petkov50308cd2011-06-01 18:25:07 -070084 private:
Paul Stewart0d2ada32011-08-09 17:01:57 -070085 friend struct base::DefaultLazyInstanceTraits<DHCPProvider>;
Darin Petkov77cb6812011-08-15 16:19:41 -070086 friend class CellularTest;
Darin Petkov98dd6a02011-06-10 15:12:57 -070087 friend class DHCPProviderTest;
Darin Petkovafa6fc42011-06-21 16:21:08 -070088 friend class DeviceInfoTest;
89 friend class DeviceTest;
Darin Petkov98dd6a02011-06-10 15:12:57 -070090 FRIEND_TEST(DHCPProviderTest, CreateConfig);
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080091 FRIEND_TEST(DHCPProviderTest, DestroyLease);
Darin Petkovf7897bc2011-06-08 17:13:36 -070092
Darin Petkov92c43902011-06-09 20:46:06 -070093 typedef std::map<int, DHCPConfigRefPtr> PIDConfigMap;
Darin Petkov50308cd2011-06-01 18:25:07 -070094
Darin Petkovab565bb2011-10-06 02:55:51 -070095 // Store cached copies of singletons for speed/ease of testing.
96 ProxyFactory *proxy_factory_;
97
Darin Petkovd1b715b2011-06-02 21:21:22 -070098 // A single listener is used to catch signals from all DHCP clients and
Darin Petkovafa6fc42011-06-21 16:21:08 -070099 // dispatch them to the appropriate DHCP configuration instance.
Darin Petkovaceede32011-07-18 15:32:38 -0700100 scoped_ptr<DHCPCDListener> listener_;
Darin Petkovd1b715b2011-06-02 21:21:22 -0700101
102 // A map that binds PIDs to DHCP configuration instances.
103 PIDConfigMap configs_;
104
Albert Chaulk0e1cdea2013-02-27 15:32:55 -0800105 base::FilePath root_;
Chris Masone19e30402011-07-19 15:48:47 -0700106 ControlInterface *control_interface_;
Darin Petkova7b89492011-07-27 12:48:17 -0700107 EventDispatcher *dispatcher_;
108 GLib *glib_;
Darin Petkovf7897bc2011-06-08 17:13:36 -0700109
Darin Petkov50308cd2011-06-01 18:25:07 -0700110 DISALLOW_COPY_AND_ASSIGN(DHCPProvider);
111};
112
113} // namespace shill
114
115#endif // SHILL_DHCP_PROVIDER_