blob: 6d4ca2b051dfe34a4871600d6e131d19cbb52802 [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 Petkov92c43902011-06-09 20:46:06 -070010#include <glib.h>
Darin Petkove7cb7f82011-06-03 13:21:51 -070011#include <gtest/gtest_prod.h> // for FRIEND_TEST
Darin Petkovd1b715b2011-06-02 21:21:22 -070012#include <dbus-c++/connection.h>
13
Chris Masonec1e50412011-06-07 13:04:53 -070014#include "shill/device.h"
Darin Petkov50308cd2011-06-01 18:25:07 -070015#include "shill/ipconfig.h"
16
17namespace shill {
18
Darin Petkovd1b715b2011-06-02 21:21:22 -070019class DHCPConfig;
20class DHCPProvider;
21class DHCPProxyInterface;
Darin Petkovf7897bc2011-06-08 17:13:36 -070022class GLibInterface;
Darin Petkovd1b715b2011-06-02 21:21:22 -070023
24typedef scoped_refptr<DHCPConfig> DHCPConfigRefPtr;
25
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
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 Petkovf7897bc2011-06-08 17:13:36 -070039 DHCPConfig(DHCPProvider *provider,
40 DeviceConstRefPtr device,
41 GLibInterface *glib);
Darin Petkov50308cd2011-06-01 18:25:07 -070042 virtual ~DHCPConfig();
43
Darin Petkovd1b715b2011-06-02 21:21:22 -070044 // Inherited from IPConfig.
Darin Petkov92c43902011-06-09 20:46:06 -070045 virtual bool RequestIP();
46 virtual bool RenewIP();
47 virtual bool ReleaseIP();
Darin Petkovd1b715b2011-06-02 21:21:22 -070048
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 Petkove7cb7f82011-06-03 13:21:51 -070053 // Processes an Event signal from dhcpcd.
54 void ProcessEventSignal(const std::string &reason,
55 const Configuration &configuration);
56
Darin Petkov50308cd2011-06-01 18:25:07 -070057 private:
Darin Petkove7cb7f82011-06-03 13:21:51 -070058 FRIEND_TEST(DHCPConfigTest, GetIPv4AddressString);
59 FRIEND_TEST(DHCPConfigTest, ParseConfiguration);
Darin Petkov92c43902011-06-09 20:46:06 -070060 FRIEND_TEST(DHCPConfigTest, StartFail);
61 FRIEND_TEST(DHCPConfigTest, StartSuccess);
Darin Petkove7cb7f82011-06-03 13:21:51 -070062
Darin Petkovd1b715b2011-06-02 21:21:22 -070063 static const char kDHCPCDPath[];
Darin Petkov92c43902011-06-09 20:46:06 -070064 static const char kDHCPCDPathFormatLease[];
65 static const char kDHCPCDPathFormatPID[];
Darin Petkovd1b715b2011-06-02 21:21:22 -070066
67 // Starts dhcpcd, returns true on success and false otherwise.
68 bool Start();
69
Darin Petkov92c43902011-06-09 20:46:06 -070070 // Stops dhcpcd if running.
71 void Stop();
72
Darin Petkove7cb7f82011-06-03 13:21:51 -070073 // Parses |configuration| into |properties|. Returns true on success, and
74 // false otherwise.
75 bool ParseConfiguration(const Configuration& configuration,
76 IPConfig::Properties *properties);
77
78 // Returns the string representation of the IP address |address|, or an
79 // empty string on failure.
80 std::string GetIPv4AddressString(unsigned int address);
81
Darin Petkov92c43902011-06-09 20:46:06 -070082 // Called when the dhcpcd client process exits.
83 static void ChildWatchCallback(GPid pid, gint status, gpointer data);
84
85 void CleanupClientState();
86
Darin Petkovd1b715b2011-06-02 21:21:22 -070087 DHCPProvider *provider_;
88
89 // The PID of the spawned DHCP client. May be 0 if no client has been spawned
90 // yet or the client has died.
Darin Petkov92c43902011-06-09 20:46:06 -070091 int pid_;
92
93 // Child exit watch callback source tag.
94 unsigned int child_watch_tag_;
Darin Petkovd1b715b2011-06-02 21:21:22 -070095
96 // The proxy for communicating with the DHCP client.
97 scoped_ptr<DHCPProxyInterface> proxy_;
98
Darin Petkov92c43902011-06-09 20:46:06 -070099 // Root file path, used for testing.
100 FilePath root_;
101
Darin Petkovf7897bc2011-06-08 17:13:36 -0700102 GLibInterface *glib_;
103
Darin Petkov50308cd2011-06-01 18:25:07 -0700104 DISALLOW_COPY_AND_ASSIGN(DHCPConfig);
105};
106
107} // namespace shill
108
109#endif // SHILL_DHCP_CONFIG_