blob: b2a09d6e664b41f3e60f07aba38f9b64a4f60009 [file] [log] [blame]
Darin Petkove02b3ca2011-05-31 16:00:44 -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_IPCONFIG_
6#define SHILL_IPCONFIG_
7
8#include <string>
Darin Petkove7cb7f82011-06-03 13:21:51 -07009#include <vector>
Darin Petkove02b3ca2011-05-31 16:00:44 -070010
Darin Petkovefb09c32011-06-07 20:24:17 -070011#include <base/callback_old.h>
Darin Petkove02b3ca2011-05-31 16:00:44 -070012#include <base/memory/ref_counted.h>
Darin Petkovefb09c32011-06-07 20:24:17 -070013#include <base/memory/scoped_ptr.h>
14#include <gtest/gtest_prod.h> // for FRIEND_TEST
Darin Petkove02b3ca2011-05-31 16:00:44 -070015
Paul Stewart1d18e8c2011-07-15 11:00:31 -070016#include "shill/ip_address.h"
Chris Masonec6c6c132011-06-30 11:29:52 -070017#include "shill/property_store.h"
Chris Masone2b105542011-06-22 10:58:09 -070018#include "shill/refptr_types.h"
19
Chris Masonec1e50412011-06-07 13:04:53 -070020namespace shill {
Chris Masonec6c6c132011-06-30 11:29:52 -070021class ControlInterface;
22class Error;
23class IPConfigAdaptorInterface;
Darin Petkove02b3ca2011-05-31 16:00:44 -070024
25// IPConfig superclass. Individual IP configuration types will inherit from this
26// class.
Chris Masone27c4aa52011-07-02 13:10:14 -070027class IPConfig : public base::RefCounted<IPConfig> {
Darin Petkove02b3ca2011-05-31 16:00:44 -070028 public:
Darin Petkove7cb7f82011-06-03 13:21:51 -070029
Paul Stewartc39f1132011-06-22 12:02:28 -070030 struct Properties {
Paul Stewart1d18e8c2011-07-15 11:00:31 -070031 Properties() : address_family(IPAddress::kAddressFamilyUnknown),
Paul Stewartc39f1132011-06-22 12:02:28 -070032 subnet_cidr(0),
33 mtu(0) {}
34
Paul Stewart1d18e8c2011-07-15 11:00:31 -070035 IPAddress::Family address_family;
Darin Petkove7cb7f82011-06-03 13:21:51 -070036 std::string address;
Chris Masone43b48a12011-07-01 13:37:07 -070037 int32 subnet_cidr;
Darin Petkove7cb7f82011-06-03 13:21:51 -070038 std::string broadcast_address;
Darin Petkove7cb7f82011-06-03 13:21:51 -070039 std::vector<std::string> dns_servers;
40 std::string domain_name;
41 std::vector<std::string> domain_search;
Chris Masone43b48a12011-07-01 13:37:07 -070042 std::string gateway;
43 std::string method;
44 std::string peer_address;
45 int32 mtu;
Darin Petkove7cb7f82011-06-03 13:21:51 -070046 };
47
Chris Masone19e30402011-07-19 15:48:47 -070048 IPConfig(ControlInterface *control_interface, const std::string &device_name);
Darin Petkove02b3ca2011-05-31 16:00:44 -070049 virtual ~IPConfig();
50
Darin Petkovf65e9282011-06-21 14:29:56 -070051 const std::string &device_name() const { return device_name_; }
Darin Petkove02b3ca2011-05-31 16:00:44 -070052
Chris Masone4e851612011-07-01 10:46:53 -070053 std::string GetRpcIdentifier();
54
Darin Petkovefb09c32011-06-07 20:24:17 -070055 // Registers a callback that's executed every time the configuration
56 // properties change. Takes ownership of |callback|. Pass NULL to remove a
Darin Petkovf9b0ca82011-06-20 12:10:23 -070057 // callback. The callback's first argument is a pointer to this IP
58 // configuration instance allowing clients to more easily manage multiple IP
59 // configurations. The callback's second argument is set to false if IP
60 // configuration failed.
Chris Masone2b105542011-06-22 10:58:09 -070061 void RegisterUpdateCallback(
62 Callback2<const IPConfigRefPtr&, bool>::Type *callback);
Darin Petkove7cb7f82011-06-03 13:21:51 -070063
64 const Properties &properties() const { return properties_; }
65
Darin Petkov92c43902011-06-09 20:46:06 -070066 // Request, renew and release IP configuration. Return true on success, false
Darin Petkove7cb7f82011-06-03 13:21:51 -070067 // otherwise. The default implementation always returns false indicating a
68 // failure.
Darin Petkov92c43902011-06-09 20:46:06 -070069 virtual bool RequestIP();
70 virtual bool RenewIP();
71 virtual bool ReleaseIP();
Darin Petkovd1b715b2011-06-02 21:21:22 -070072
Chris Masone27c4aa52011-07-02 13:10:14 -070073 PropertyStore *store() { return &store_; }
Chris Masone43b48a12011-07-01 13:37:07 -070074
Darin Petkovefb09c32011-06-07 20:24:17 -070075 protected:
76 // Updates the IP configuration properties and notifies registered listeners
Darin Petkovf9b0ca82011-06-20 12:10:23 -070077 // about the event. |success| is set to false if the IP configuration failed.
78 void UpdateProperties(const Properties &properties, bool success);
Darin Petkovefb09c32011-06-07 20:24:17 -070079
Chris Masone27c4aa52011-07-02 13:10:14 -070080 PropertyStore store_;
81
Darin Petkove02b3ca2011-05-31 16:00:44 -070082 private:
Chris Masonec6c6c132011-06-30 11:29:52 -070083 friend class IPConfigAdaptorInterface;
84
Darin Petkovafa6fc42011-06-21 16:21:08 -070085 FRIEND_TEST(DeviceTest, AcquireDHCPConfig);
86 FRIEND_TEST(DeviceTest, DestroyIPConfig);
Darin Petkovefb09c32011-06-07 20:24:17 -070087 FRIEND_TEST(IPConfigTest, UpdateCallback);
88 FRIEND_TEST(IPConfigTest, UpdateProperties);
89
Darin Petkovf65e9282011-06-21 14:29:56 -070090 const std::string device_name_;
Chris Masone19e30402011-07-19 15:48:47 -070091 scoped_ptr<IPConfigAdaptorInterface> adaptor_;
Darin Petkove7cb7f82011-06-03 13:21:51 -070092 Properties properties_;
Chris Masone2b105542011-06-22 10:58:09 -070093 scoped_ptr<Callback2<const IPConfigRefPtr&, bool>::Type> update_callback_;
Darin Petkove02b3ca2011-05-31 16:00:44 -070094
95 DISALLOW_COPY_AND_ASSIGN(IPConfig);
96};
97
98} // namespace shill
99
100#endif // SHILL_IPCONFIG_