Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 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 | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 10 | #include <dbus-c++/connection.h> |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 11 | #include <glib.h> |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 12 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 13 | |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 14 | #include "shill/ipconfig.h" |
| 15 | |
| 16 | namespace shill { |
| 17 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 18 | class DHCPConfig; |
| 19 | class DHCPProvider; |
| 20 | class DHCPProxyInterface; |
Darin Petkov | 3258a81 | 2011-06-23 11:28:45 -0700 | [diff] [blame^] | 21 | class GLib; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 22 | |
| 23 | typedef scoped_refptr<DHCPConfig> DHCPConfigRefPtr; |
| 24 | |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 25 | class DHCPConfig : public IPConfig { |
| 26 | public: |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 27 | typedef std::map<std::string, DBus::Variant> Configuration; |
| 28 | |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 29 | DHCPConfig(DHCPProvider *provider, |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame] | 30 | const std::string &device_name, |
Darin Petkov | 3258a81 | 2011-06-23 11:28:45 -0700 | [diff] [blame^] | 31 | GLib *glib); |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 32 | virtual ~DHCPConfig(); |
| 33 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 34 | // Inherited from IPConfig. |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 35 | virtual bool RequestIP(); |
| 36 | virtual bool RenewIP(); |
| 37 | virtual bool ReleaseIP(); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 38 | |
| 39 | // If |proxy_| is not initialized already, sets it to a new D-Bus proxy to |
| 40 | // |service|. |
| 41 | void InitProxy(DBus::Connection *connection, const char *service); |
| 42 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 43 | // Processes an Event signal from dhcpcd. |
| 44 | void ProcessEventSignal(const std::string &reason, |
| 45 | const Configuration &configuration); |
| 46 | |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 47 | private: |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 48 | friend class DHCPConfigTest; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 49 | FRIEND_TEST(DHCPConfigTest, GetIPv4AddressString); |
| 50 | FRIEND_TEST(DHCPConfigTest, ParseConfiguration); |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 51 | FRIEND_TEST(DHCPConfigTest, ProcessEventSignalFail); |
| 52 | FRIEND_TEST(DHCPConfigTest, ProcessEventSignalSuccess); |
| 53 | FRIEND_TEST(DHCPConfigTest, ProcessEventSignalUnknown); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 54 | FRIEND_TEST(DHCPConfigTest, ReleaseIP); |
| 55 | FRIEND_TEST(DHCPConfigTest, RenewIP); |
| 56 | FRIEND_TEST(DHCPConfigTest, RequestIP); |
| 57 | FRIEND_TEST(DHCPConfigTest, Restart); |
| 58 | FRIEND_TEST(DHCPConfigTest, RestartNoClient); |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 59 | FRIEND_TEST(DHCPConfigTest, StartFail); |
| 60 | FRIEND_TEST(DHCPConfigTest, StartSuccess); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 61 | FRIEND_TEST(DHCPConfigTest, Stop); |
| 62 | FRIEND_TEST(DHCPProviderTest, CreateConfig); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 63 | |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 64 | static const char kConfigurationKeyBroadcastAddress[]; |
| 65 | static const char kConfigurationKeyDNS[]; |
| 66 | static const char kConfigurationKeyDomainName[]; |
| 67 | static const char kConfigurationKeyDomainSearch[]; |
| 68 | static const char kConfigurationKeyIPAddress[]; |
| 69 | static const char kConfigurationKeyMTU[]; |
| 70 | static const char kConfigurationKeyRouters[]; |
| 71 | static const char kConfigurationKeySubnetCIDR[]; |
| 72 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 73 | static const char kDHCPCDPath[]; |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 74 | static const char kDHCPCDPathFormatLease[]; |
| 75 | static const char kDHCPCDPathFormatPID[]; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 76 | |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 77 | static const char kReasonBound[]; |
| 78 | static const char kReasonFail[]; |
| 79 | static const char kReasonRebind[]; |
| 80 | static const char kReasonReboot[]; |
| 81 | static const char kReasonRenew[]; |
| 82 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 83 | // Starts dhcpcd, returns true on success and false otherwise. |
| 84 | bool Start(); |
| 85 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 86 | // Stops dhcpcd if running. |
| 87 | void Stop(); |
| 88 | |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 89 | // Stops dhcpcd if already running and then starts it. Returns true on success |
| 90 | // and false otherwise. |
| 91 | bool Restart(); |
| 92 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 93 | // Parses |configuration| into |properties|. Returns true on success, and |
| 94 | // false otherwise. |
| 95 | bool ParseConfiguration(const Configuration& configuration, |
| 96 | IPConfig::Properties *properties); |
| 97 | |
| 98 | // Returns the string representation of the IP address |address|, or an |
| 99 | // empty string on failure. |
| 100 | std::string GetIPv4AddressString(unsigned int address); |
| 101 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 102 | // Called when the dhcpcd client process exits. |
| 103 | static void ChildWatchCallback(GPid pid, gint status, gpointer data); |
| 104 | |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 105 | // Cleans up remaining state from a running client, if any, including freeing |
| 106 | // its GPid, exit watch callback, and state files. |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 107 | void CleanupClientState(); |
| 108 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 109 | DHCPProvider *provider_; |
| 110 | |
| 111 | // The PID of the spawned DHCP client. May be 0 if no client has been spawned |
| 112 | // yet or the client has died. |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 113 | int pid_; |
| 114 | |
| 115 | // Child exit watch callback source tag. |
| 116 | unsigned int child_watch_tag_; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 117 | |
| 118 | // The proxy for communicating with the DHCP client. |
| 119 | scoped_ptr<DHCPProxyInterface> proxy_; |
| 120 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 121 | // Root file path, used for testing. |
| 122 | FilePath root_; |
| 123 | |
Darin Petkov | 3258a81 | 2011-06-23 11:28:45 -0700 | [diff] [blame^] | 124 | GLib *glib_; |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 125 | |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 126 | DISALLOW_COPY_AND_ASSIGN(DHCPConfig); |
| 127 | }; |
| 128 | |
| 129 | } // namespace shill |
| 130 | |
| 131 | #endif // SHILL_DHCP_CONFIG_ |