blob: 0707774faac2ffe5681f879ec991a4e30625b4ef [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
Darin Petkov50308cd2011-06-01 18:25:07 -070012#include "shill/ipconfig.h"
13
14namespace shill {
15
Darin Petkovd1b715b2011-06-02 21:21:22 -070016class DHCPConfig;
17class DHCPProvider;
18class DHCPProxyInterface;
19
20typedef scoped_refptr<DHCPConfig> DHCPConfigRefPtr;
21
Darin Petkov50308cd2011-06-01 18:25:07 -070022class DHCPConfig : public IPConfig {
23 public:
Darin Petkove7cb7f82011-06-03 13:21:51 -070024 typedef std::map<std::string, DBus::Variant> Configuration;
25
26 static const char kConfigurationKeyBroadcastAddress[];
27 static const char kConfigurationKeyDNS[];
28 static const char kConfigurationKeyDomainName[];
29 static const char kConfigurationKeyDomainSearch[];
30 static const char kConfigurationKeyIPAddress[];
31 static const char kConfigurationKeyMTU[];
32 static const char kConfigurationKeyRouters[];
33 static const char kConfigurationKeySubnetCIDR[];
34
Darin Petkovd1b715b2011-06-02 21:21:22 -070035 DHCPConfig(DHCPProvider *provider, const Device &device);
Darin Petkov50308cd2011-06-01 18:25:07 -070036 virtual ~DHCPConfig();
37
Darin Petkovd1b715b2011-06-02 21:21:22 -070038 // Inherited from IPConfig.
39 virtual bool Request();
40 virtual bool Renew();
41
42 // If |proxy_| is not initialized already, sets it to a new D-Bus proxy to
43 // |service|.
44 void InitProxy(DBus::Connection *connection, const char *service);
45
Darin Petkove7cb7f82011-06-03 13:21:51 -070046 // Processes an Event signal from dhcpcd.
47 void ProcessEventSignal(const std::string &reason,
48 const Configuration &configuration);
49
Darin Petkov50308cd2011-06-01 18:25:07 -070050 private:
Darin Petkove7cb7f82011-06-03 13:21:51 -070051 FRIEND_TEST(DHCPConfigTest, GetIPv4AddressString);
52 FRIEND_TEST(DHCPConfigTest, ParseConfiguration);
53
Darin Petkovd1b715b2011-06-02 21:21:22 -070054 static const char kDHCPCDPath[];
55
56 // Starts dhcpcd, returns true on success and false otherwise.
57 bool Start();
58
Darin Petkove7cb7f82011-06-03 13:21:51 -070059 // Parses |configuration| into |properties|. Returns true on success, and
60 // false otherwise.
61 bool ParseConfiguration(const Configuration& configuration,
62 IPConfig::Properties *properties);
63
64 // Returns the string representation of the IP address |address|, or an
65 // empty string on failure.
66 std::string GetIPv4AddressString(unsigned int address);
67
Darin Petkovd1b715b2011-06-02 21:21:22 -070068 DHCPProvider *provider_;
69
70 // The PID of the spawned DHCP client. May be 0 if no client has been spawned
71 // yet or the client has died.
72 unsigned int pid_;
73
74 // The proxy for communicating with the DHCP client.
75 scoped_ptr<DHCPProxyInterface> proxy_;
76
Darin Petkov50308cd2011-06-01 18:25:07 -070077 DISALLOW_COPY_AND_ASSIGN(DHCPConfig);
78};
79
80} // namespace shill
81
82#endif // SHILL_DHCP_CONFIG_