blob: 2afa200b8325ed6f5ad1d15c6c10088ad3170f32 [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 Petkovaceede32011-07-18 15:32:38 -070010#include <dbus-c++/types.h>
Darin Petkov92c43902011-06-09 20:46:06 -070011#include <glib.h>
Darin Petkove7cb7f82011-06-03 13:21:51 -070012#include <gtest/gtest_prod.h> // for FRIEND_TEST
Darin Petkovd1b715b2011-06-02 21:21:22 -070013
Darin Petkov50308cd2011-06-01 18:25:07 -070014#include "shill/ipconfig.h"
15
16namespace shill {
17
Chris Masone19e30402011-07-19 15:48:47 -070018class ControlInterface;
Darin Petkovd1b715b2011-06-02 21:21:22 -070019class DHCPProvider;
20class DHCPProxyInterface;
Darin Petkova7b89492011-07-27 12:48:17 -070021class EventDispatcher;
Darin Petkov3258a812011-06-23 11:28:45 -070022class GLib;
Darin Petkovab565bb2011-10-06 02:55:51 -070023class ProxyFactory;
Darin Petkovd1b715b2011-06-02 21:21:22 -070024
Eric Shienbrood9a245532012-03-07 14:20:39 -050025class DHCPConfig : public IPConfig {
Darin Petkov50308cd2011-06-01 18:25:07 -070026 public:
Darin Petkove7cb7f82011-06-03 13:21:51 -070027 typedef std::map<std::string, DBus::Variant> Configuration;
28
Darin Petkov14c29ec2012-03-02 11:34:19 +010029 static const int kMinMTU;
30
Chris Masone19e30402011-07-19 15:48:47 -070031 DHCPConfig(ControlInterface *control_interface,
Darin Petkova7b89492011-07-27 12:48:17 -070032 EventDispatcher *dispatcher,
Chris Masone19e30402011-07-19 15:48:47 -070033 DHCPProvider *provider,
Darin Petkovf65e9282011-06-21 14:29:56 -070034 const std::string &device_name,
Paul Stewartd32f4842012-01-11 16:08:13 -080035 const std::string &request_hostname,
Darin Petkov3258a812011-06-23 11:28:45 -070036 GLib *glib);
Darin Petkov50308cd2011-06-01 18:25:07 -070037 virtual ~DHCPConfig();
38
Darin Petkovd1b715b2011-06-02 21:21:22 -070039 // Inherited from IPConfig.
Darin Petkov92c43902011-06-09 20:46:06 -070040 virtual bool RequestIP();
41 virtual bool RenewIP();
42 virtual bool ReleaseIP();
Darin Petkovd1b715b2011-06-02 21:21:22 -070043
44 // If |proxy_| is not initialized already, sets it to a new D-Bus proxy to
45 // |service|.
Darin Petkova7b89492011-07-27 12:48:17 -070046 void InitProxy(const std::string &service);
Darin Petkovd1b715b2011-06-02 21:21:22 -070047
Darin Petkove7cb7f82011-06-03 13:21:51 -070048 // Processes an Event signal from dhcpcd.
49 void ProcessEventSignal(const std::string &reason,
50 const Configuration &configuration);
51
Darin Petkov50308cd2011-06-01 18:25:07 -070052 private:
Darin Petkov98dd6a02011-06-10 15:12:57 -070053 friend class DHCPConfigTest;
Darin Petkove7cb7f82011-06-03 13:21:51 -070054 FRIEND_TEST(DHCPConfigTest, GetIPv4AddressString);
Darin Petkova7b89492011-07-27 12:48:17 -070055 FRIEND_TEST(DHCPConfigTest, InitProxy);
Darin Petkove7cb7f82011-06-03 13:21:51 -070056 FRIEND_TEST(DHCPConfigTest, ParseConfiguration);
Darin Petkovf9b0ca82011-06-20 12:10:23 -070057 FRIEND_TEST(DHCPConfigTest, ProcessEventSignalFail);
58 FRIEND_TEST(DHCPConfigTest, ProcessEventSignalSuccess);
59 FRIEND_TEST(DHCPConfigTest, ProcessEventSignalUnknown);
Darin Petkov98dd6a02011-06-10 15:12:57 -070060 FRIEND_TEST(DHCPConfigTest, ReleaseIP);
61 FRIEND_TEST(DHCPConfigTest, RenewIP);
62 FRIEND_TEST(DHCPConfigTest, RequestIP);
63 FRIEND_TEST(DHCPConfigTest, Restart);
64 FRIEND_TEST(DHCPConfigTest, RestartNoClient);
Darin Petkov92c43902011-06-09 20:46:06 -070065 FRIEND_TEST(DHCPConfigTest, StartFail);
Paul Stewartd32f4842012-01-11 16:08:13 -080066 FRIEND_TEST(DHCPConfigTest, StartWithHostname);
67 FRIEND_TEST(DHCPConfigTest, StartWithoutHostname);
Darin Petkov92c43902011-06-09 20:46:06 -070068 FRIEND_TEST(DHCPConfigTest, StartSuccess);
Darin Petkov98dd6a02011-06-10 15:12:57 -070069 FRIEND_TEST(DHCPConfigTest, Stop);
70 FRIEND_TEST(DHCPProviderTest, CreateConfig);
Darin Petkove7cb7f82011-06-03 13:21:51 -070071
Darin Petkovf9b0ca82011-06-20 12:10:23 -070072 static const char kConfigurationKeyBroadcastAddress[];
73 static const char kConfigurationKeyDNS[];
74 static const char kConfigurationKeyDomainName[];
75 static const char kConfigurationKeyDomainSearch[];
76 static const char kConfigurationKeyIPAddress[];
77 static const char kConfigurationKeyMTU[];
78 static const char kConfigurationKeyRouters[];
79 static const char kConfigurationKeySubnetCIDR[];
80
Thieu Le94eed562012-02-21 15:57:29 -080081 static const int kDHCPCDExitPollMilliseconds;
82 static const int kDHCPCDExitWaitMilliseconds;
Darin Petkovd1b715b2011-06-02 21:21:22 -070083 static const char kDHCPCDPath[];
Darin Petkov92c43902011-06-09 20:46:06 -070084 static const char kDHCPCDPathFormatLease[];
85 static const char kDHCPCDPathFormatPID[];
Darin Petkovd1b715b2011-06-02 21:21:22 -070086
Darin Petkovf9b0ca82011-06-20 12:10:23 -070087 static const char kReasonBound[];
88 static const char kReasonFail[];
89 static const char kReasonRebind[];
90 static const char kReasonReboot[];
91 static const char kReasonRenew[];
92
Chris Masone0756f232011-07-21 17:24:00 -070093 static const char kType[];
94
Darin Petkovd1b715b2011-06-02 21:21:22 -070095 // Starts dhcpcd, returns true on success and false otherwise.
96 bool Start();
97
Darin Petkov92c43902011-06-09 20:46:06 -070098 // Stops dhcpcd if running.
99 void Stop();
100
Darin Petkov98dd6a02011-06-10 15:12:57 -0700101 // Stops dhcpcd if already running and then starts it. Returns true on success
102 // and false otherwise.
103 bool Restart();
104
Darin Petkove7cb7f82011-06-03 13:21:51 -0700105 // Parses |configuration| into |properties|. Returns true on success, and
106 // false otherwise.
107 bool ParseConfiguration(const Configuration& configuration,
108 IPConfig::Properties *properties);
109
110 // Returns the string representation of the IP address |address|, or an
111 // empty string on failure.
112 std::string GetIPv4AddressString(unsigned int address);
113
Darin Petkov92c43902011-06-09 20:46:06 -0700114 // Called when the dhcpcd client process exits.
115 static void ChildWatchCallback(GPid pid, gint status, gpointer data);
116
Darin Petkov98dd6a02011-06-10 15:12:57 -0700117 // Cleans up remaining state from a running client, if any, including freeing
118 // its GPid, exit watch callback, and state files.
Darin Petkov92c43902011-06-09 20:46:06 -0700119 void CleanupClientState();
120
Darin Petkovab565bb2011-10-06 02:55:51 -0700121 // Store cached copies of singletons for speed/ease of testing.
122 ProxyFactory *proxy_factory_;
123
Darin Petkovd1b715b2011-06-02 21:21:22 -0700124 DHCPProvider *provider_;
125
Paul Stewartd32f4842012-01-11 16:08:13 -0800126 // Hostname to be used in the request. This will be passed to the DHCP
127 // server in the request.
128 std::string request_hostname_;
129
Darin Petkovd1b715b2011-06-02 21:21:22 -0700130 // The PID of the spawned DHCP client. May be 0 if no client has been spawned
131 // yet or the client has died.
Darin Petkov92c43902011-06-09 20:46:06 -0700132 int pid_;
133
134 // Child exit watch callback source tag.
135 unsigned int child_watch_tag_;
Darin Petkovd1b715b2011-06-02 21:21:22 -0700136
137 // The proxy for communicating with the DHCP client.
138 scoped_ptr<DHCPProxyInterface> proxy_;
139
Darin Petkov92c43902011-06-09 20:46:06 -0700140 // Root file path, used for testing.
141 FilePath root_;
142
Darin Petkova7b89492011-07-27 12:48:17 -0700143 EventDispatcher *dispatcher_;
Darin Petkov3258a812011-06-23 11:28:45 -0700144 GLib *glib_;
Darin Petkovf7897bc2011-06-08 17:13:36 -0700145
Darin Petkov50308cd2011-06-01 18:25:07 -0700146 DISALLOW_COPY_AND_ASSIGN(DHCPConfig);
147};
148
149} // namespace shill
150
151#endif // SHILL_DHCP_CONFIG_