blob: 0e25873f6457af162d0a103cb00720b3f7b7fc0e [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 Petkovd1b715b2011-06-02 21:21:22 -07008#include <base/memory/scoped_ptr.h>
Darin Petkove7cb7f82011-06-03 13:21:51 -07009#include <gtest/gtest_prod.h> // for FRIEND_TEST
Darin Petkovd1b715b2011-06-02 21:21:22 -070010#include <dbus-c++/connection.h>
11
Chris Masonec1e50412011-06-07 13:04:53 -070012#include "shill/device.h"
Darin Petkov50308cd2011-06-01 18:25:07 -070013#include "shill/ipconfig.h"
14
15namespace shill {
16
Darin Petkovd1b715b2011-06-02 21:21:22 -070017class DHCPConfig;
18class DHCPProvider;
19class DHCPProxyInterface;
Darin Petkovf7897bc2011-06-08 17:13:36 -070020class GLibInterface;
Darin Petkovd1b715b2011-06-02 21:21:22 -070021
22typedef scoped_refptr<DHCPConfig> DHCPConfigRefPtr;
23
Darin Petkov50308cd2011-06-01 18:25:07 -070024class DHCPConfig : public IPConfig {
25 public:
Darin Petkove7cb7f82011-06-03 13:21:51 -070026 typedef std::map<std::string, DBus::Variant> Configuration;
27
28 static const char kConfigurationKeyBroadcastAddress[];
29 static const char kConfigurationKeyDNS[];
30 static const char kConfigurationKeyDomainName[];
31 static const char kConfigurationKeyDomainSearch[];
32 static const char kConfigurationKeyIPAddress[];
33 static const char kConfigurationKeyMTU[];
34 static const char kConfigurationKeyRouters[];
35 static const char kConfigurationKeySubnetCIDR[];
36
Darin Petkovf7897bc2011-06-08 17:13:36 -070037 DHCPConfig(DHCPProvider *provider,
38 DeviceConstRefPtr device,
39 GLibInterface *glib);
Darin Petkov50308cd2011-06-01 18:25:07 -070040 virtual ~DHCPConfig();
41
Darin Petkovd1b715b2011-06-02 21:21:22 -070042 // Inherited from IPConfig.
43 virtual bool Request();
44 virtual bool Renew();
45
46 // If |proxy_| is not initialized already, sets it to a new D-Bus proxy to
47 // |service|.
48 void InitProxy(DBus::Connection *connection, const char *service);
49
Darin Petkove7cb7f82011-06-03 13:21:51 -070050 // Processes an Event signal from dhcpcd.
51 void ProcessEventSignal(const std::string &reason,
52 const Configuration &configuration);
53
Darin Petkov50308cd2011-06-01 18:25:07 -070054 private:
Darin Petkove7cb7f82011-06-03 13:21:51 -070055 FRIEND_TEST(DHCPConfigTest, GetIPv4AddressString);
56 FRIEND_TEST(DHCPConfigTest, ParseConfiguration);
Darin Petkovf7897bc2011-06-08 17:13:36 -070057 FRIEND_TEST(DHCPConfigTest, Start);
Darin Petkove7cb7f82011-06-03 13:21:51 -070058
Darin Petkovd1b715b2011-06-02 21:21:22 -070059 static const char kDHCPCDPath[];
60
61 // Starts dhcpcd, returns true on success and false otherwise.
62 bool Start();
63
Darin Petkove7cb7f82011-06-03 13:21:51 -070064 // Parses |configuration| into |properties|. Returns true on success, and
65 // false otherwise.
66 bool ParseConfiguration(const Configuration& configuration,
67 IPConfig::Properties *properties);
68
69 // Returns the string representation of the IP address |address|, or an
70 // empty string on failure.
71 std::string GetIPv4AddressString(unsigned int address);
72
Darin Petkovd1b715b2011-06-02 21:21:22 -070073 DHCPProvider *provider_;
74
75 // The PID of the spawned DHCP client. May be 0 if no client has been spawned
76 // yet or the client has died.
77 unsigned int pid_;
78
79 // The proxy for communicating with the DHCP client.
80 scoped_ptr<DHCPProxyInterface> proxy_;
81
Darin Petkovf7897bc2011-06-08 17:13:36 -070082 GLibInterface *glib_;
83
Darin Petkov50308cd2011-06-01 18:25:07 -070084 DISALLOW_COPY_AND_ASSIGN(DHCPConfig);
85};
86
87} // namespace shill
88
89#endif // SHILL_DHCP_CONFIG_