blob: 0c9478796173c839899b68dcca20f4ad43a5085a [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
Ben Chanc45688b2014-07-02 23:50:45 -07005#ifndef SHILL_DHCP_PROVIDER_H_
6#define SHILL_DHCP_PROVIDER_H_
Darin Petkov50308cd2011-06-01 18:25:07 -07007
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
Ben Chana0ddf462014-02-06 11:32:42 -080011#include <base/files/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;
Paul Stewart3bdf1ab2014-07-17 19:22:26 -070024class Metrics;
Darin Petkovab565bb2011-10-06 02:55:51 -070025class ProxyFactory;
Darin Petkovd1b715b2011-06-02 21:21:22 -070026
27// DHCPProvider is a singleton providing the main DHCP configuration
28// entrypoint. Once the provider is initialized through its Init method, DHCP
29// configurations for devices can be obtained through its CreateConfig
30// method. For example, a single DHCP configuration request can be initiated as:
31//
Paul Stewartd408fdf2012-05-07 17:15:57 -070032// DHCPProvider::GetInstance()->CreateConfig(device_name,
33// host_name,
34// lease_file_suffix,
Paul Stewartb1083182014-06-25 03:04:53 -070035// arp_gateway)->Request();
Darin Petkov50308cd2011-06-01 18:25:07 -070036class DHCPProvider {
37 public:
mukesh agrawal88fb16f2014-04-24 10:37:39 -070038 static constexpr char kDHCPCDPathFormatLease[] =
39 "var/lib/dhcpcd/dhcpcd-%s.lease";
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080040
Paul Stewart0d2ada32011-08-09 17:01:57 -070041 virtual ~DHCPProvider();
42
mukesh agrawalf407d592013-07-31 11:37:57 -070043 // This is a singleton -- use DHCPProvider::GetInstance()->Foo().
Darin Petkov50308cd2011-06-01 18:25:07 -070044 static DHCPProvider *GetInstance();
45
Darin Petkovd1b715b2011-06-02 21:21:22 -070046 // Initializes the provider singleton. This method hooks up a D-Bus signal
Darin Petkovaceede32011-07-18 15:32:38 -070047 // listener that catches signals from spawned DHCP clients and dispatches them
48 // to the appropriate DHCP configuration instance.
Thieu Lefb46caf2012-03-08 11:57:15 -080049 virtual void Init(ControlInterface *control_interface,
50 EventDispatcher *dispatcher,
Paul Stewart3bdf1ab2014-07-17 19:22:26 -070051 GLib *glib,
52 Metrics *metrics);
Darin Petkovd1b715b2011-06-02 21:21:22 -070053
Darin Petkovf65e9282011-06-21 14:29:56 -070054 // Creates a new DHCPConfig for |device_name|. The DHCP configuration for the
Darin Petkovd1b715b2011-06-02 21:21:22 -070055 // device can then be initiated through DHCPConfig::Request and
Paul Stewartd408fdf2012-05-07 17:15:57 -070056 // DHCPConfig::Renew. If |host_name| is not-empty, it is placed in the DHCP
Paul Stewartd32f4842012-01-11 16:08:13 -080057 // request to allow the server to map the request to a specific user-named
Paul Stewartd408fdf2012-05-07 17:15:57 -070058 // origin. The DHCP lease file will contain the suffix supplied
59 // in |lease_file_suffix| if non-empty, otherwise |device_name|. If
60 // |arp_gateway| is true, the DHCP client will ARP for the gateway IP
61 // address as an additional safeguard against the issued IP address being
Paul Stewartb1083182014-06-25 03:04:53 -070062 // in-use by another station.
Paul Stewartd32f4842012-01-11 16:08:13 -080063 virtual DHCPConfigRefPtr CreateConfig(const std::string &device_name,
Paul Stewartd408fdf2012-05-07 17:15:57 -070064 const std::string &host_name,
65 const std::string &lease_file_suffix,
Paul Stewartb1083182014-06-25 03:04:53 -070066 bool arp_gateway);
Darin Petkovd1b715b2011-06-02 21:21:22 -070067
68 // Returns the DHCP configuration associated with DHCP client |pid|. Return
69 // NULL if |pid| is not bound to a configuration.
Darin Petkov98dd6a02011-06-10 15:12:57 -070070 DHCPConfigRefPtr GetConfig(int pid);
Darin Petkovd1b715b2011-06-02 21:21:22 -070071
72 // Binds a |pid| to a DHCP |config|. When a DHCP config spawns a new DHCP
73 // client, it binds itself to that client's |pid|.
Chris Masone2b105542011-06-22 10:58:09 -070074 void BindPID(int pid, const DHCPConfigRefPtr &config);
Darin Petkovd1b715b2011-06-02 21:21:22 -070075
76 // Unbinds a |pid|. This method is used by a DHCP config to signal the
77 // provider that the DHCP client has been terminated. This may result in
78 // destruction of the DHCP config instance if its reference count goes to 0.
Darin Petkov92c43902011-06-09 20:46:06 -070079 void UnbindPID(int pid);
Darin Petkovd1b715b2011-06-02 21:21:22 -070080
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080081 // Destroy lease file associated with this |name|.
82 virtual void DestroyLease(const std::string &name);
83
Paul Stewart0d2ada32011-08-09 17:01:57 -070084 protected:
85 DHCPProvider();
86
Darin Petkov50308cd2011-06-01 18:25:07 -070087 private:
Paul Stewart0d2ada32011-08-09 17:01:57 -070088 friend struct base::DefaultLazyInstanceTraits<DHCPProvider>;
Darin Petkov77cb6812011-08-15 16:19:41 -070089 friend class CellularTest;
Darin Petkov98dd6a02011-06-10 15:12:57 -070090 friend class DHCPProviderTest;
Darin Petkovafa6fc42011-06-21 16:21:08 -070091 friend class DeviceInfoTest;
92 friend class DeviceTest;
Darin Petkov98dd6a02011-06-10 15:12:57 -070093 FRIEND_TEST(DHCPProviderTest, CreateConfig);
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080094 FRIEND_TEST(DHCPProviderTest, DestroyLease);
Darin Petkovf7897bc2011-06-08 17:13:36 -070095
Darin Petkov92c43902011-06-09 20:46:06 -070096 typedef std::map<int, DHCPConfigRefPtr> PIDConfigMap;
Darin Petkov50308cd2011-06-01 18:25:07 -070097
Darin Petkovab565bb2011-10-06 02:55:51 -070098 // Store cached copies of singletons for speed/ease of testing.
99 ProxyFactory *proxy_factory_;
100
Darin Petkovd1b715b2011-06-02 21:21:22 -0700101 // A single listener is used to catch signals from all DHCP clients and
Darin Petkovafa6fc42011-06-21 16:21:08 -0700102 // dispatch them to the appropriate DHCP configuration instance.
Darin Petkovaceede32011-07-18 15:32:38 -0700103 scoped_ptr<DHCPCDListener> listener_;
Darin Petkovd1b715b2011-06-02 21:21:22 -0700104
105 // A map that binds PIDs to DHCP configuration instances.
106 PIDConfigMap configs_;
107
Albert Chaulk0e1cdea2013-02-27 15:32:55 -0800108 base::FilePath root_;
Chris Masone19e30402011-07-19 15:48:47 -0700109 ControlInterface *control_interface_;
Darin Petkova7b89492011-07-27 12:48:17 -0700110 EventDispatcher *dispatcher_;
111 GLib *glib_;
Paul Stewart3bdf1ab2014-07-17 19:22:26 -0700112 Metrics *metrics_;
Darin Petkovf7897bc2011-06-08 17:13:36 -0700113
Darin Petkov50308cd2011-06-01 18:25:07 -0700114 DISALLOW_COPY_AND_ASSIGN(DHCPProvider);
115};
116
117} // namespace shill
118
Ben Chanc45688b2014-07-02 23:50:45 -0700119#endif // SHILL_DHCP_PROVIDER_H_