blob: a70d634521da099a19a279a63a82c15c1f4bc87f [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;
20
21typedef scoped_refptr<DHCPConfig> DHCPConfigRefPtr;
22
Darin Petkov50308cd2011-06-01 18:25:07 -070023class DHCPConfig : public IPConfig {
24 public:
Darin Petkove7cb7f82011-06-03 13:21:51 -070025 typedef std::map<std::string, DBus::Variant> Configuration;
26
27 static const char kConfigurationKeyBroadcastAddress[];
28 static const char kConfigurationKeyDNS[];
29 static const char kConfigurationKeyDomainName[];
30 static const char kConfigurationKeyDomainSearch[];
31 static const char kConfigurationKeyIPAddress[];
32 static const char kConfigurationKeyMTU[];
33 static const char kConfigurationKeyRouters[];
34 static const char kConfigurationKeySubnetCIDR[];
35
Chris Masonec1e50412011-06-07 13:04:53 -070036 DHCPConfig(DHCPProvider *provider, DeviceConstRefPtr device);
Darin Petkov50308cd2011-06-01 18:25:07 -070037 virtual ~DHCPConfig();
38
Darin Petkovd1b715b2011-06-02 21:21:22 -070039 // Inherited from IPConfig.
40 virtual bool Request();
41 virtual bool Renew();
42
43 // If |proxy_| is not initialized already, sets it to a new D-Bus proxy to
44 // |service|.
45 void InitProxy(DBus::Connection *connection, const char *service);
46
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 Petkove7cb7f82011-06-03 13:21:51 -070052 FRIEND_TEST(DHCPConfigTest, GetIPv4AddressString);
53 FRIEND_TEST(DHCPConfigTest, ParseConfiguration);
54
Darin Petkovd1b715b2011-06-02 21:21:22 -070055 static const char kDHCPCDPath[];
56
57 // Starts dhcpcd, returns true on success and false otherwise.
58 bool Start();
59
Darin Petkove7cb7f82011-06-03 13:21:51 -070060 // Parses |configuration| into |properties|. Returns true on success, and
61 // false otherwise.
62 bool ParseConfiguration(const Configuration& configuration,
63 IPConfig::Properties *properties);
64
65 // Returns the string representation of the IP address |address|, or an
66 // empty string on failure.
67 std::string GetIPv4AddressString(unsigned int address);
68
Darin Petkovd1b715b2011-06-02 21:21:22 -070069 DHCPProvider *provider_;
70
71 // The PID of the spawned DHCP client. May be 0 if no client has been spawned
72 // yet or the client has died.
73 unsigned int pid_;
74
75 // The proxy for communicating with the DHCP client.
76 scoped_ptr<DHCPProxyInterface> proxy_;
77
Darin Petkov50308cd2011-06-01 18:25:07 -070078 DISALLOW_COPY_AND_ASSIGN(DHCPConfig);
79};
80
81} // namespace shill
82
83#endif // SHILL_DHCP_CONFIG_