blob: 4ea147b514079a8921259becd24bfdc2b06c5323 [file] [log] [blame]
Darin Petkov50308cd2011-06-01 18:25:07 -07001// 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 Petkov92c43902011-06-09 20:46:06 -07008#include <base/file_path.h>
Darin Petkovd1b715b2011-06-02 21:21:22 -07009#include <base/memory/scoped_ptr.h>
Darin Petkova7b89492011-07-27 12:48:17 -070010#include <base/task.h>
Darin Petkovaceede32011-07-18 15:32:38 -070011#include <dbus-c++/types.h>
Darin Petkov92c43902011-06-09 20:46:06 -070012#include <glib.h>
Darin Petkove7cb7f82011-06-03 13:21:51 -070013#include <gtest/gtest_prod.h> // for FRIEND_TEST
Darin Petkovd1b715b2011-06-02 21:21:22 -070014
Darin Petkov50308cd2011-06-01 18:25:07 -070015#include "shill/ipconfig.h"
16
17namespace shill {
18
Chris Masone19e30402011-07-19 15:48:47 -070019class ControlInterface;
Darin Petkovd1b715b2011-06-02 21:21:22 -070020class DHCPProvider;
21class DHCPProxyInterface;
Darin Petkova7b89492011-07-27 12:48:17 -070022class EventDispatcher;
Darin Petkov3258a812011-06-23 11:28:45 -070023class GLib;
Darin Petkovab565bb2011-10-06 02:55:51 -070024class ProxyFactory;
Darin Petkovd1b715b2011-06-02 21:21:22 -070025
Darin Petkov50308cd2011-06-01 18:25:07 -070026class DHCPConfig : public IPConfig {
27 public:
Darin Petkove7cb7f82011-06-03 13:21:51 -070028 typedef std::map<std::string, DBus::Variant> Configuration;
29
Chris Masone19e30402011-07-19 15:48:47 -070030 DHCPConfig(ControlInterface *control_interface,
Darin Petkova7b89492011-07-27 12:48:17 -070031 EventDispatcher *dispatcher,
Chris Masone19e30402011-07-19 15:48:47 -070032 DHCPProvider *provider,
Darin Petkovf65e9282011-06-21 14:29:56 -070033 const std::string &device_name,
Paul Stewartd32f4842012-01-11 16:08:13 -080034 const std::string &request_hostname,
Darin Petkov3258a812011-06-23 11:28:45 -070035 GLib *glib);
Darin Petkov50308cd2011-06-01 18:25:07 -070036 virtual ~DHCPConfig();
37
Darin Petkovd1b715b2011-06-02 21:21:22 -070038 // Inherited from IPConfig.
Darin Petkov92c43902011-06-09 20:46:06 -070039 virtual bool RequestIP();
40 virtual bool RenewIP();
41 virtual bool ReleaseIP();
Darin Petkovd1b715b2011-06-02 21:21:22 -070042
43 // If |proxy_| is not initialized already, sets it to a new D-Bus proxy to
44 // |service|.
Darin Petkova7b89492011-07-27 12:48:17 -070045 void InitProxy(const std::string &service);
Darin Petkovd1b715b2011-06-02 21:21:22 -070046
Darin Petkove7cb7f82011-06-03 13:21:51 -070047 // Processes an Event signal from dhcpcd.
48 void ProcessEventSignal(const std::string &reason,
49 const Configuration &configuration);
50
Darin Petkov50308cd2011-06-01 18:25:07 -070051 private:
Darin Petkov98dd6a02011-06-10 15:12:57 -070052 friend class DHCPConfigTest;
Darin Petkove7cb7f82011-06-03 13:21:51 -070053 FRIEND_TEST(DHCPConfigTest, GetIPv4AddressString);
Darin Petkova7b89492011-07-27 12:48:17 -070054 FRIEND_TEST(DHCPConfigTest, InitProxy);
Darin Petkove7cb7f82011-06-03 13:21:51 -070055 FRIEND_TEST(DHCPConfigTest, ParseConfiguration);
Darin Petkovf9b0ca82011-06-20 12:10:23 -070056 FRIEND_TEST(DHCPConfigTest, ProcessEventSignalFail);
57 FRIEND_TEST(DHCPConfigTest, ProcessEventSignalSuccess);
58 FRIEND_TEST(DHCPConfigTest, ProcessEventSignalUnknown);
Darin Petkov98dd6a02011-06-10 15:12:57 -070059 FRIEND_TEST(DHCPConfigTest, ReleaseIP);
60 FRIEND_TEST(DHCPConfigTest, RenewIP);
61 FRIEND_TEST(DHCPConfigTest, RequestIP);
62 FRIEND_TEST(DHCPConfigTest, Restart);
63 FRIEND_TEST(DHCPConfigTest, RestartNoClient);
Darin Petkov92c43902011-06-09 20:46:06 -070064 FRIEND_TEST(DHCPConfigTest, StartFail);
Paul Stewartd32f4842012-01-11 16:08:13 -080065 FRIEND_TEST(DHCPConfigTest, StartWithHostname);
66 FRIEND_TEST(DHCPConfigTest, StartWithoutHostname);
Darin Petkov92c43902011-06-09 20:46:06 -070067 FRIEND_TEST(DHCPConfigTest, StartSuccess);
Darin Petkov98dd6a02011-06-10 15:12:57 -070068 FRIEND_TEST(DHCPConfigTest, Stop);
69 FRIEND_TEST(DHCPProviderTest, CreateConfig);
Darin Petkove7cb7f82011-06-03 13:21:51 -070070
Darin Petkovf9b0ca82011-06-20 12:10:23 -070071 static const char kConfigurationKeyBroadcastAddress[];
72 static const char kConfigurationKeyDNS[];
73 static const char kConfigurationKeyDomainName[];
74 static const char kConfigurationKeyDomainSearch[];
75 static const char kConfigurationKeyIPAddress[];
76 static const char kConfigurationKeyMTU[];
77 static const char kConfigurationKeyRouters[];
78 static const char kConfigurationKeySubnetCIDR[];
79
Darin Petkovd1b715b2011-06-02 21:21:22 -070080 static const char kDHCPCDPath[];
Darin Petkov92c43902011-06-09 20:46:06 -070081 static const char kDHCPCDPathFormatLease[];
82 static const char kDHCPCDPathFormatPID[];
Darin Petkovd1b715b2011-06-02 21:21:22 -070083
Darin Petkovf9b0ca82011-06-20 12:10:23 -070084 static const char kReasonBound[];
85 static const char kReasonFail[];
86 static const char kReasonRebind[];
87 static const char kReasonReboot[];
88 static const char kReasonRenew[];
89
Chris Masone0756f232011-07-21 17:24:00 -070090 static const char kType[];
91
Darin Petkova7b89492011-07-27 12:48:17 -070092 // If |proxy_| is not initialized already, sets it to a new D-Bus proxy to
93 // |service|.
94 void InitProxyTask(const std::string &service);
95
Darin Petkovd1b715b2011-06-02 21:21:22 -070096 // Starts dhcpcd, returns true on success and false otherwise.
97 bool Start();
98
Darin Petkov92c43902011-06-09 20:46:06 -070099 // Stops dhcpcd if running.
100 void Stop();
101
Darin Petkov98dd6a02011-06-10 15:12:57 -0700102 // Stops dhcpcd if already running and then starts it. Returns true on success
103 // and false otherwise.
104 bool Restart();
105
Darin Petkove7cb7f82011-06-03 13:21:51 -0700106 // Parses |configuration| into |properties|. Returns true on success, and
107 // false otherwise.
108 bool ParseConfiguration(const Configuration& configuration,
109 IPConfig::Properties *properties);
110
111 // Returns the string representation of the IP address |address|, or an
112 // empty string on failure.
113 std::string GetIPv4AddressString(unsigned int address);
114
Darin Petkov92c43902011-06-09 20:46:06 -0700115 // Called when the dhcpcd client process exits.
116 static void ChildWatchCallback(GPid pid, gint status, gpointer data);
117
Darin Petkov98dd6a02011-06-10 15:12:57 -0700118 // Cleans up remaining state from a running client, if any, including freeing
119 // its GPid, exit watch callback, and state files.
Darin Petkov92c43902011-06-09 20:46:06 -0700120 void CleanupClientState();
121
Darin Petkovab565bb2011-10-06 02:55:51 -0700122 // Store cached copies of singletons for speed/ease of testing.
123 ProxyFactory *proxy_factory_;
124
Darin Petkovd1b715b2011-06-02 21:21:22 -0700125 DHCPProvider *provider_;
126
Paul Stewartd32f4842012-01-11 16:08:13 -0800127 // Hostname to be used in the request. This will be passed to the DHCP
128 // server in the request.
129 std::string request_hostname_;
130
Darin Petkovd1b715b2011-06-02 21:21:22 -0700131 // The PID of the spawned DHCP client. May be 0 if no client has been spawned
132 // yet or the client has died.
Darin Petkov92c43902011-06-09 20:46:06 -0700133 int pid_;
134
135 // Child exit watch callback source tag.
136 unsigned int child_watch_tag_;
Darin Petkovd1b715b2011-06-02 21:21:22 -0700137
138 // The proxy for communicating with the DHCP client.
139 scoped_ptr<DHCPProxyInterface> proxy_;
140
Darin Petkov92c43902011-06-09 20:46:06 -0700141 // Root file path, used for testing.
142 FilePath root_;
143
Darin Petkova7b89492011-07-27 12:48:17 -0700144 ScopedRunnableMethodFactory<DHCPConfig> task_factory_;
145 EventDispatcher *dispatcher_;
Darin Petkov3258a812011-06-23 11:28:45 -0700146 GLib *glib_;
Darin Petkovf7897bc2011-06-08 17:13:36 -0700147
Darin Petkov50308cd2011-06-01 18:25:07 -0700148 DISALLOW_COPY_AND_ASSIGN(DHCPConfig);
149};
150
151} // namespace shill
152
153#endif // SHILL_DHCP_CONFIG_