Thieu Le | 94eed56 | 2012-02-21 15:57:29 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 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_CONFIG_ |
| 6 | #define SHILL_DHCP_CONFIG_ |
| 7 | |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 8 | #include <base/cancelable_callback.h> |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 9 | #include <base/file_path.h> |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 10 | #include <base/memory/scoped_ptr.h> |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 11 | #include <base/memory/weak_ptr.h> |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 12 | #include <dbus-c++/types.h> |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 13 | #include <glib.h> |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 14 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 15 | |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 16 | #include "shill/ipconfig.h" |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 17 | #include "shill/minijail.h" |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 18 | |
| 19 | namespace shill { |
| 20 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 21 | class ControlInterface; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 22 | class DHCPProvider; |
| 23 | class DHCPProxyInterface; |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 24 | class EventDispatcher; |
Darin Petkov | 3258a81 | 2011-06-23 11:28:45 -0700 | [diff] [blame] | 25 | class GLib; |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 26 | class ProxyFactory; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 27 | |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 28 | // This class provides a DHCP client instance for the device |device_name|. |
| 29 | // If |request_hostname| is non-empty, it asks the DHCP server to register |
| 30 | // this hostname on our behalf, for purposes of administration or creating |
| 31 | // a dynamic DNS entry. |
| 32 | // |
| 33 | // The DHPCConfig instance asks the DHCP client to create a lease file |
| 34 | // containing the name |lease_file_suffix|. If this suffix is the same as |
| 35 | // |device_name|, the lease is considered to be ephemeral, and the lease |
| 36 | // file is removed whenever this DHCPConfig instance is no longer needed. |
| 37 | // Otherwise, the lease file persists and will be re-used in future attempts. |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 38 | class DHCPConfig : public IPConfig { |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 39 | public: |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 40 | typedef std::map<std::string, DBus::Variant> Configuration; |
| 41 | |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 42 | static const int kMinMTU; |
| 43 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 44 | DHCPConfig(ControlInterface *control_interface, |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 45 | EventDispatcher *dispatcher, |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 46 | DHCPProvider *provider, |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame] | 47 | const std::string &device_name, |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 48 | const std::string &request_hostname, |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 49 | const std::string &lease_file_suffix, |
| 50 | bool arp_gateway, |
Darin Petkov | 3258a81 | 2011-06-23 11:28:45 -0700 | [diff] [blame] | 51 | GLib *glib); |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 52 | virtual ~DHCPConfig(); |
| 53 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 54 | // Inherited from IPConfig. |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 55 | virtual bool RequestIP(); |
| 56 | virtual bool RenewIP(); |
| 57 | virtual bool ReleaseIP(); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 58 | |
| 59 | // If |proxy_| is not initialized already, sets it to a new D-Bus proxy to |
| 60 | // |service|. |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 61 | void InitProxy(const std::string &service); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 62 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 63 | // Processes an Event signal from dhcpcd. |
| 64 | void ProcessEventSignal(const std::string &reason, |
| 65 | const Configuration &configuration); |
| 66 | |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 67 | protected: |
| 68 | // Overrides base clase implementation. |
| 69 | virtual void UpdateProperties(const Properties &properties, bool success); |
| 70 | |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 71 | private: |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 72 | friend class DHCPConfigTest; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 73 | FRIEND_TEST(DHCPConfigTest, GetIPv4AddressString); |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 74 | FRIEND_TEST(DHCPConfigTest, InitProxy); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 75 | FRIEND_TEST(DHCPConfigTest, ParseConfiguration); |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 76 | FRIEND_TEST(DHCPConfigTest, ProcessEventSignalFail); |
| 77 | FRIEND_TEST(DHCPConfigTest, ProcessEventSignalSuccess); |
| 78 | FRIEND_TEST(DHCPConfigTest, ProcessEventSignalUnknown); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 79 | FRIEND_TEST(DHCPConfigTest, ReleaseIP); |
Paul Stewart | a02ee49 | 2012-05-16 10:04:53 -0700 | [diff] [blame] | 80 | FRIEND_TEST(DHCPConfigTest, ReleaseIPArpGW); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 81 | FRIEND_TEST(DHCPConfigTest, RenewIP); |
| 82 | FRIEND_TEST(DHCPConfigTest, RequestIP); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 83 | FRIEND_TEST(DHCPConfigTest, RequestIPTimeout); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 84 | FRIEND_TEST(DHCPConfigTest, Restart); |
| 85 | FRIEND_TEST(DHCPConfigTest, RestartNoClient); |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 86 | FRIEND_TEST(DHCPConfigTest, StartFail); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 87 | FRIEND_TEST(DHCPConfigTest, StartTimeout); |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 88 | FRIEND_TEST(DHCPConfigTest, StartWithHostname); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 89 | FRIEND_TEST(DHCPConfigTest, StartWithoutArpGateway); |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 90 | FRIEND_TEST(DHCPConfigTest, StartWithoutHostname); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 91 | FRIEND_TEST(DHCPConfigTest, StartWithoutLeaseSuffix); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 92 | FRIEND_TEST(DHCPConfigTest, Stop); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 93 | FRIEND_TEST(DHCPConfigTest, StopDuringRequestIP); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 94 | FRIEND_TEST(DHCPProviderTest, CreateConfig); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 95 | |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 96 | static const char kConfigurationKeyBroadcastAddress[]; |
| 97 | static const char kConfigurationKeyDNS[]; |
| 98 | static const char kConfigurationKeyDomainName[]; |
| 99 | static const char kConfigurationKeyDomainSearch[]; |
| 100 | static const char kConfigurationKeyIPAddress[]; |
| 101 | static const char kConfigurationKeyMTU[]; |
| 102 | static const char kConfigurationKeyRouters[]; |
| 103 | static const char kConfigurationKeySubnetCIDR[]; |
| 104 | |
Thieu Le | 94eed56 | 2012-02-21 15:57:29 -0800 | [diff] [blame] | 105 | static const int kDHCPCDExitPollMilliseconds; |
| 106 | static const int kDHCPCDExitWaitMilliseconds; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 107 | static const char kDHCPCDPath[]; |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 108 | static const char kDHCPCDPathFormatLease[]; |
| 109 | static const char kDHCPCDPathFormatPID[]; |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 110 | static const int kDHCPTimeoutSeconds; |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 111 | static const char kDHCPCDUser[]; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 112 | |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 113 | static const char kReasonBound[]; |
Paul Stewart | a02ee49 | 2012-05-16 10:04:53 -0700 | [diff] [blame] | 114 | static const char kReasonGatewayArp[]; |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 115 | static const char kReasonFail[]; |
| 116 | static const char kReasonRebind[]; |
| 117 | static const char kReasonReboot[]; |
| 118 | static const char kReasonRenew[]; |
| 119 | |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 120 | static const char kType[]; |
| 121 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 122 | // Starts dhcpcd, returns true on success and false otherwise. |
| 123 | bool Start(); |
| 124 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 125 | // Stops dhcpcd if running. |
| 126 | void Stop(); |
| 127 | |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 128 | // Stops dhcpcd if already running and then starts it. Returns true on success |
| 129 | // and false otherwise. |
| 130 | bool Restart(); |
| 131 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 132 | // Parses |configuration| into |properties|. Returns true on success, and |
| 133 | // false otherwise. |
| 134 | bool ParseConfiguration(const Configuration& configuration, |
| 135 | IPConfig::Properties *properties); |
| 136 | |
| 137 | // Returns the string representation of the IP address |address|, or an |
| 138 | // empty string on failure. |
| 139 | std::string GetIPv4AddressString(unsigned int address); |
| 140 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 141 | // Called when the dhcpcd client process exits. |
| 142 | static void ChildWatchCallback(GPid pid, gint status, gpointer data); |
| 143 | |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 144 | // Cleans up remaining state from a running client, if any, including freeing |
| 145 | // its GPid, exit watch callback, and state files. |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 146 | void CleanupClientState(); |
| 147 | |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 148 | // Initialize a callback that will invoke ProcessDHCPTimeout if we |
| 149 | // do not get a lease in a reasonable amount of time. |
| 150 | void StartDHCPTimeout(); |
| 151 | // Cancel callback created by StartDHCPTimeout. One-liner included |
| 152 | // for symmetry. |
| 153 | void StopDHCPTimeout(); |
| 154 | // Called if we do not get a DHCP lease in a reasonable amount of time. |
| 155 | // Informs upper layers of the failure. |
| 156 | void ProcessDHCPTimeout(); |
| 157 | |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 158 | // Store cached copies of singletons for speed/ease of testing. |
| 159 | ProxyFactory *proxy_factory_; |
| 160 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 161 | DHCPProvider *provider_; |
| 162 | |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 163 | // Hostname to be used in the request. This will be passed to the DHCP |
| 164 | // server in the request. |
| 165 | std::string request_hostname_; |
| 166 | |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 167 | // DHCP lease file suffix, used to differentiate the lease of one interface |
| 168 | // or network from another. |
| 169 | std::string lease_file_suffix_; |
| 170 | |
| 171 | // Specifies whether to supply an argument to the DHCP client to validate |
| 172 | // the acquired IP address using an ARP request to the gateway IP address. |
| 173 | bool arp_gateway_; |
| 174 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 175 | // The PID of the spawned DHCP client. May be 0 if no client has been spawned |
| 176 | // yet or the client has died. |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 177 | int pid_; |
| 178 | |
| 179 | // Child exit watch callback source tag. |
| 180 | unsigned int child_watch_tag_; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 181 | |
| 182 | // The proxy for communicating with the DHCP client. |
| 183 | scoped_ptr<DHCPProxyInterface> proxy_; |
| 184 | |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 185 | // Called if we fail to get a DHCP lease in a timely manner. |
| 186 | base::CancelableClosure lease_acquisition_timeout_callback_; |
| 187 | |
| 188 | // Time to wait for a DHCP lease. Represented as field so that it |
| 189 | // can be overriden in tests. |
| 190 | unsigned int lease_acquisition_timeout_seconds_; |
| 191 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 192 | // Root file path, used for testing. |
| 193 | FilePath root_; |
| 194 | |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 195 | base::WeakPtrFactory<DHCPConfig> weak_ptr_factory_; |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 196 | EventDispatcher *dispatcher_; |
Darin Petkov | 3258a81 | 2011-06-23 11:28:45 -0700 | [diff] [blame] | 197 | GLib *glib_; |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 198 | |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 199 | Minijail *minijail_; |
| 200 | |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 201 | DISALLOW_COPY_AND_ASSIGN(DHCPConfig); |
| 202 | }; |
| 203 | |
| 204 | } // namespace shill |
| 205 | |
| 206 | #endif // SHILL_DHCP_CONFIG_ |