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 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 8 | #include <base/file_path.h> |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 9 | #include <base/memory/scoped_ptr.h> |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 10 | #include <base/task.h> |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 11 | #include <dbus-c++/types.h> |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 12 | #include <glib.h> |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 13 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 14 | |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 15 | #include "shill/ipconfig.h" |
| 16 | |
| 17 | namespace shill { |
| 18 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 19 | class ControlInterface; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 20 | class DHCPProvider; |
| 21 | class DHCPProxyInterface; |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 22 | class EventDispatcher; |
Darin Petkov | 3258a81 | 2011-06-23 11:28:45 -0700 | [diff] [blame] | 23 | class GLib; |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 24 | class ProxyFactory; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 25 | |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 26 | class DHCPConfig : public IPConfig { |
| 27 | public: |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 28 | typedef std::map<std::string, DBus::Variant> Configuration; |
| 29 | |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame^] | 30 | static const int kMinMTU; |
| 31 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 32 | DHCPConfig(ControlInterface *control_interface, |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 33 | EventDispatcher *dispatcher, |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 34 | DHCPProvider *provider, |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame] | 35 | const std::string &device_name, |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 36 | const std::string &request_hostname, |
Darin Petkov | 3258a81 | 2011-06-23 11:28:45 -0700 | [diff] [blame] | 37 | GLib *glib); |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 38 | virtual ~DHCPConfig(); |
| 39 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 40 | // Inherited from IPConfig. |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 41 | virtual bool RequestIP(); |
| 42 | virtual bool RenewIP(); |
| 43 | virtual bool ReleaseIP(); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 44 | |
| 45 | // If |proxy_| is not initialized already, sets it to a new D-Bus proxy to |
| 46 | // |service|. |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 47 | void InitProxy(const std::string &service); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 48 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 49 | // Processes an Event signal from dhcpcd. |
| 50 | void ProcessEventSignal(const std::string &reason, |
| 51 | const Configuration &configuration); |
| 52 | |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 53 | private: |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 54 | friend class DHCPConfigTest; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 55 | FRIEND_TEST(DHCPConfigTest, GetIPv4AddressString); |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 56 | FRIEND_TEST(DHCPConfigTest, InitProxy); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 57 | FRIEND_TEST(DHCPConfigTest, ParseConfiguration); |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 58 | FRIEND_TEST(DHCPConfigTest, ProcessEventSignalFail); |
| 59 | FRIEND_TEST(DHCPConfigTest, ProcessEventSignalSuccess); |
| 60 | FRIEND_TEST(DHCPConfigTest, ProcessEventSignalUnknown); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 61 | FRIEND_TEST(DHCPConfigTest, ReleaseIP); |
| 62 | FRIEND_TEST(DHCPConfigTest, RenewIP); |
| 63 | FRIEND_TEST(DHCPConfigTest, RequestIP); |
| 64 | FRIEND_TEST(DHCPConfigTest, Restart); |
| 65 | FRIEND_TEST(DHCPConfigTest, RestartNoClient); |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 66 | FRIEND_TEST(DHCPConfigTest, StartFail); |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 67 | FRIEND_TEST(DHCPConfigTest, StartWithHostname); |
| 68 | FRIEND_TEST(DHCPConfigTest, StartWithoutHostname); |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 69 | FRIEND_TEST(DHCPConfigTest, StartSuccess); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 70 | FRIEND_TEST(DHCPConfigTest, Stop); |
| 71 | FRIEND_TEST(DHCPProviderTest, CreateConfig); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 72 | |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 73 | static const char kConfigurationKeyBroadcastAddress[]; |
| 74 | static const char kConfigurationKeyDNS[]; |
| 75 | static const char kConfigurationKeyDomainName[]; |
| 76 | static const char kConfigurationKeyDomainSearch[]; |
| 77 | static const char kConfigurationKeyIPAddress[]; |
| 78 | static const char kConfigurationKeyMTU[]; |
| 79 | static const char kConfigurationKeyRouters[]; |
| 80 | static const char kConfigurationKeySubnetCIDR[]; |
| 81 | |
Thieu Le | 94eed56 | 2012-02-21 15:57:29 -0800 | [diff] [blame] | 82 | static const int kDHCPCDExitPollMilliseconds; |
| 83 | static const int kDHCPCDExitWaitMilliseconds; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 84 | static const char kDHCPCDPath[]; |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 85 | static const char kDHCPCDPathFormatLease[]; |
| 86 | static const char kDHCPCDPathFormatPID[]; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 87 | |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 88 | static const char kReasonBound[]; |
| 89 | static const char kReasonFail[]; |
| 90 | static const char kReasonRebind[]; |
| 91 | static const char kReasonReboot[]; |
| 92 | static const char kReasonRenew[]; |
| 93 | |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 94 | static const char kType[]; |
| 95 | |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 96 | // If |proxy_| is not initialized already, sets it to a new D-Bus proxy to |
| 97 | // |service|. |
| 98 | void InitProxyTask(const std::string &service); |
| 99 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 100 | // Starts dhcpcd, returns true on success and false otherwise. |
| 101 | bool Start(); |
| 102 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 103 | // Stops dhcpcd if running. |
| 104 | void Stop(); |
| 105 | |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 106 | // Stops dhcpcd if already running and then starts it. Returns true on success |
| 107 | // and false otherwise. |
| 108 | bool Restart(); |
| 109 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 110 | // Parses |configuration| into |properties|. Returns true on success, and |
| 111 | // false otherwise. |
| 112 | bool ParseConfiguration(const Configuration& configuration, |
| 113 | IPConfig::Properties *properties); |
| 114 | |
| 115 | // Returns the string representation of the IP address |address|, or an |
| 116 | // empty string on failure. |
| 117 | std::string GetIPv4AddressString(unsigned int address); |
| 118 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 119 | // Called when the dhcpcd client process exits. |
| 120 | static void ChildWatchCallback(GPid pid, gint status, gpointer data); |
| 121 | |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 122 | // Cleans up remaining state from a running client, if any, including freeing |
| 123 | // its GPid, exit watch callback, and state files. |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 124 | void CleanupClientState(); |
| 125 | |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 126 | // Store cached copies of singletons for speed/ease of testing. |
| 127 | ProxyFactory *proxy_factory_; |
| 128 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 129 | DHCPProvider *provider_; |
| 130 | |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 131 | // Hostname to be used in the request. This will be passed to the DHCP |
| 132 | // server in the request. |
| 133 | std::string request_hostname_; |
| 134 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 135 | // The PID of the spawned DHCP client. May be 0 if no client has been spawned |
| 136 | // yet or the client has died. |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 137 | int pid_; |
| 138 | |
| 139 | // Child exit watch callback source tag. |
| 140 | unsigned int child_watch_tag_; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 141 | |
| 142 | // The proxy for communicating with the DHCP client. |
| 143 | scoped_ptr<DHCPProxyInterface> proxy_; |
| 144 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 145 | // Root file path, used for testing. |
| 146 | FilePath root_; |
| 147 | |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 148 | ScopedRunnableMethodFactory<DHCPConfig> task_factory_; |
| 149 | EventDispatcher *dispatcher_; |
Darin Petkov | 3258a81 | 2011-06-23 11:28:45 -0700 | [diff] [blame] | 150 | GLib *glib_; |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 151 | |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 152 | DISALLOW_COPY_AND_ASSIGN(DHCPConfig); |
| 153 | }; |
| 154 | |
| 155 | } // namespace shill |
| 156 | |
| 157 | #endif // SHILL_DHCP_CONFIG_ |