blob: e3400801cc7476778a6be87893fa89989cffbdad [file] [log] [blame]
Thieu Le94eed562012-02-21 15:57:29 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkov50308cd2011-06-01 18:25:07 -07002// 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
Darin Petkov14c29ec2012-03-02 11:34:19 +010030 static const int kMinMTU;
31
Chris Masone19e30402011-07-19 15:48:47 -070032 DHCPConfig(ControlInterface *control_interface,
Darin Petkova7b89492011-07-27 12:48:17 -070033 EventDispatcher *dispatcher,
Chris Masone19e30402011-07-19 15:48:47 -070034 DHCPProvider *provider,
Darin Petkovf65e9282011-06-21 14:29:56 -070035 const std::string &device_name,
Paul Stewartd32f4842012-01-11 16:08:13 -080036 const std::string &request_hostname,
Darin Petkov3258a812011-06-23 11:28:45 -070037 GLib *glib);
Darin Petkov50308cd2011-06-01 18:25:07 -070038 virtual ~DHCPConfig();
39
Darin Petkovd1b715b2011-06-02 21:21:22 -070040 // Inherited from IPConfig.
Darin Petkov92c43902011-06-09 20:46:06 -070041 virtual bool RequestIP();
42 virtual bool RenewIP();
43 virtual bool ReleaseIP();
Darin Petkovd1b715b2011-06-02 21:21:22 -070044
45 // If |proxy_| is not initialized already, sets it to a new D-Bus proxy to
46 // |service|.
Darin Petkova7b89492011-07-27 12:48:17 -070047 void InitProxy(const std::string &service);
Darin Petkovd1b715b2011-06-02 21:21:22 -070048
Darin Petkove7cb7f82011-06-03 13:21:51 -070049 // Processes an Event signal from dhcpcd.
50 void ProcessEventSignal(const std::string &reason,
51 const Configuration &configuration);
52
Darin Petkov50308cd2011-06-01 18:25:07 -070053 private:
Darin Petkov98dd6a02011-06-10 15:12:57 -070054 friend class DHCPConfigTest;
Darin Petkove7cb7f82011-06-03 13:21:51 -070055 FRIEND_TEST(DHCPConfigTest, GetIPv4AddressString);
Darin Petkova7b89492011-07-27 12:48:17 -070056 FRIEND_TEST(DHCPConfigTest, InitProxy);
Darin Petkove7cb7f82011-06-03 13:21:51 -070057 FRIEND_TEST(DHCPConfigTest, ParseConfiguration);
Darin Petkovf9b0ca82011-06-20 12:10:23 -070058 FRIEND_TEST(DHCPConfigTest, ProcessEventSignalFail);
59 FRIEND_TEST(DHCPConfigTest, ProcessEventSignalSuccess);
60 FRIEND_TEST(DHCPConfigTest, ProcessEventSignalUnknown);
Darin Petkov98dd6a02011-06-10 15:12:57 -070061 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 Petkov92c43902011-06-09 20:46:06 -070066 FRIEND_TEST(DHCPConfigTest, StartFail);
Paul Stewartd32f4842012-01-11 16:08:13 -080067 FRIEND_TEST(DHCPConfigTest, StartWithHostname);
68 FRIEND_TEST(DHCPConfigTest, StartWithoutHostname);
Darin Petkov92c43902011-06-09 20:46:06 -070069 FRIEND_TEST(DHCPConfigTest, StartSuccess);
Darin Petkov98dd6a02011-06-10 15:12:57 -070070 FRIEND_TEST(DHCPConfigTest, Stop);
71 FRIEND_TEST(DHCPProviderTest, CreateConfig);
Darin Petkove7cb7f82011-06-03 13:21:51 -070072
Darin Petkovf9b0ca82011-06-20 12:10:23 -070073 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 Le94eed562012-02-21 15:57:29 -080082 static const int kDHCPCDExitPollMilliseconds;
83 static const int kDHCPCDExitWaitMilliseconds;
Darin Petkovd1b715b2011-06-02 21:21:22 -070084 static const char kDHCPCDPath[];
Darin Petkov92c43902011-06-09 20:46:06 -070085 static const char kDHCPCDPathFormatLease[];
86 static const char kDHCPCDPathFormatPID[];
Darin Petkovd1b715b2011-06-02 21:21:22 -070087
Darin Petkovf9b0ca82011-06-20 12:10:23 -070088 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 Masone0756f232011-07-21 17:24:00 -070094 static const char kType[];
95
Darin Petkova7b89492011-07-27 12:48:17 -070096 // 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 Petkovd1b715b2011-06-02 21:21:22 -0700100 // Starts dhcpcd, returns true on success and false otherwise.
101 bool Start();
102
Darin Petkov92c43902011-06-09 20:46:06 -0700103 // Stops dhcpcd if running.
104 void Stop();
105
Darin Petkov98dd6a02011-06-10 15:12:57 -0700106 // Stops dhcpcd if already running and then starts it. Returns true on success
107 // and false otherwise.
108 bool Restart();
109
Darin Petkove7cb7f82011-06-03 13:21:51 -0700110 // 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 Petkov92c43902011-06-09 20:46:06 -0700119 // Called when the dhcpcd client process exits.
120 static void ChildWatchCallback(GPid pid, gint status, gpointer data);
121
Darin Petkov98dd6a02011-06-10 15:12:57 -0700122 // Cleans up remaining state from a running client, if any, including freeing
123 // its GPid, exit watch callback, and state files.
Darin Petkov92c43902011-06-09 20:46:06 -0700124 void CleanupClientState();
125
Darin Petkovab565bb2011-10-06 02:55:51 -0700126 // Store cached copies of singletons for speed/ease of testing.
127 ProxyFactory *proxy_factory_;
128
Darin Petkovd1b715b2011-06-02 21:21:22 -0700129 DHCPProvider *provider_;
130
Paul Stewartd32f4842012-01-11 16:08:13 -0800131 // 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 Petkovd1b715b2011-06-02 21:21:22 -0700135 // The PID of the spawned DHCP client. May be 0 if no client has been spawned
136 // yet or the client has died.
Darin Petkov92c43902011-06-09 20:46:06 -0700137 int pid_;
138
139 // Child exit watch callback source tag.
140 unsigned int child_watch_tag_;
Darin Petkovd1b715b2011-06-02 21:21:22 -0700141
142 // The proxy for communicating with the DHCP client.
143 scoped_ptr<DHCPProxyInterface> proxy_;
144
Darin Petkov92c43902011-06-09 20:46:06 -0700145 // Root file path, used for testing.
146 FilePath root_;
147
Darin Petkova7b89492011-07-27 12:48:17 -0700148 ScopedRunnableMethodFactory<DHCPConfig> task_factory_;
149 EventDispatcher *dispatcher_;
Darin Petkov3258a812011-06-23 11:28:45 -0700150 GLib *glib_;
Darin Petkovf7897bc2011-06-08 17:13:36 -0700151
Darin Petkov50308cd2011-06-01 18:25:07 -0700152 DISALLOW_COPY_AND_ASSIGN(DHCPConfig);
153};
154
155} // namespace shill
156
157#endif // SHILL_DHCP_CONFIG_