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