blob: ca922a3f33ab83327f33f4389a57edb1428edb68 [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>
Chris Masone0756f232011-07-21 17:24:00 -070012#include <base/logging.h>
Darin Petkove02b3ca2011-05-31 16:00:44 -070013#include <base/memory/ref_counted.h>
Darin Petkovefb09c32011-06-07 20:24:17 -070014#include <base/memory/scoped_ptr.h>
15#include <gtest/gtest_prod.h> // for FRIEND_TEST
Darin Petkove02b3ca2011-05-31 16:00:44 -070016
Paul Stewart1d18e8c2011-07-15 11:00:31 -070017#include "shill/ip_address.h"
Chris Masonec6c6c132011-06-30 11:29:52 -070018#include "shill/property_store.h"
Chris Masone2b105542011-06-22 10:58:09 -070019#include "shill/refptr_types.h"
20
Chris Masonec1e50412011-06-07 13:04:53 -070021namespace shill {
Chris Masonec6c6c132011-06-30 11:29:52 -070022class ControlInterface;
23class Error;
24class IPConfigAdaptorInterface;
Chris Masone8a7b8be2011-07-22 12:43:37 -070025class StoreInterface;
Darin Petkove02b3ca2011-05-31 16:00:44 -070026
27// IPConfig superclass. Individual IP configuration types will inherit from this
28// class.
Chris Masone27c4aa52011-07-02 13:10:14 -070029class IPConfig : public base::RefCounted<IPConfig> {
Darin Petkove02b3ca2011-05-31 16:00:44 -070030 public:
Paul Stewartc39f1132011-06-22 12:02:28 -070031 struct Properties {
Paul Stewart1d18e8c2011-07-15 11:00:31 -070032 Properties() : address_family(IPAddress::kAddressFamilyUnknown),
Paul Stewartc39f1132011-06-22 12:02:28 -070033 subnet_cidr(0),
34 mtu(0) {}
35
Paul Stewart1d18e8c2011-07-15 11:00:31 -070036 IPAddress::Family address_family;
Darin Petkove7cb7f82011-06-03 13:21:51 -070037 std::string address;
Chris Masone43b48a12011-07-01 13:37:07 -070038 int32 subnet_cidr;
Darin Petkove7cb7f82011-06-03 13:21:51 -070039 std::string broadcast_address;
Darin Petkove7cb7f82011-06-03 13:21:51 -070040 std::vector<std::string> dns_servers;
41 std::string domain_name;
42 std::vector<std::string> domain_search;
Chris Masone43b48a12011-07-01 13:37:07 -070043 std::string gateway;
44 std::string method;
45 std::string peer_address;
46 int32 mtu;
Darin Petkove7cb7f82011-06-03 13:21:51 -070047 };
48
Chris Masone19e30402011-07-19 15:48:47 -070049 IPConfig(ControlInterface *control_interface, const std::string &device_name);
Chris Masone0756f232011-07-21 17:24:00 -070050 IPConfig(ControlInterface *control_interface,
51 const std::string &device_name,
52 const std::string &type);
Darin Petkove02b3ca2011-05-31 16:00:44 -070053 virtual ~IPConfig();
54
Darin Petkovf65e9282011-06-21 14:29:56 -070055 const std::string &device_name() const { return device_name_; }
Chris Masone0756f232011-07-21 17:24:00 -070056 const std::string &type() const { return type_; }
57 uint serial() const { return serial_; }
Darin Petkove02b3ca2011-05-31 16:00:44 -070058
Chris Masone4e851612011-07-01 10:46:53 -070059 std::string GetRpcIdentifier();
Chris Masone5dec5f42011-07-22 14:07:55 -070060 std::string GetStorageIdentifier();
Chris Masone4e851612011-07-01 10:46:53 -070061
Darin Petkovefb09c32011-06-07 20:24:17 -070062 // Registers a callback that's executed every time the configuration
63 // properties change. Takes ownership of |callback|. Pass NULL to remove a
Darin Petkovf9b0ca82011-06-20 12:10:23 -070064 // callback. The callback's first argument is a pointer to this IP
65 // configuration instance allowing clients to more easily manage multiple IP
66 // configurations. The callback's second argument is set to false if IP
67 // configuration failed.
Chris Masone2b105542011-06-22 10:58:09 -070068 void RegisterUpdateCallback(
69 Callback2<const IPConfigRefPtr&, bool>::Type *callback);
Darin Petkove7cb7f82011-06-03 13:21:51 -070070
71 const Properties &properties() const { return properties_; }
72
Darin Petkov92c43902011-06-09 20:46:06 -070073 // Request, renew and release IP configuration. Return true on success, false
Darin Petkove7cb7f82011-06-03 13:21:51 -070074 // otherwise. The default implementation always returns false indicating a
75 // failure.
Darin Petkov92c43902011-06-09 20:46:06 -070076 virtual bool RequestIP();
77 virtual bool RenewIP();
78 virtual bool ReleaseIP();
Darin Petkovd1b715b2011-06-02 21:21:22 -070079
Chris Masone27c4aa52011-07-02 13:10:14 -070080 PropertyStore *store() { return &store_; }
Chris Masone43b48a12011-07-01 13:37:07 -070081
Chris Masone8a7b8be2011-07-22 12:43:37 -070082 bool Load(StoreInterface *storage);
83 bool Save(StoreInterface *storage);
84
Darin Petkovefb09c32011-06-07 20:24:17 -070085 protected:
86 // Updates the IP configuration properties and notifies registered listeners
Darin Petkovf9b0ca82011-06-20 12:10:23 -070087 // about the event. |success| is set to false if the IP configuration failed.
88 void UpdateProperties(const Properties &properties, bool success);
Darin Petkovefb09c32011-06-07 20:24:17 -070089
Chris Masone27c4aa52011-07-02 13:10:14 -070090 PropertyStore store_;
91
Darin Petkove02b3ca2011-05-31 16:00:44 -070092 private:
Chris Masonec6c6c132011-06-30 11:29:52 -070093 friend class IPConfigAdaptorInterface;
94
Darin Petkovafa6fc42011-06-21 16:21:08 -070095 FRIEND_TEST(DeviceTest, AcquireDHCPConfig);
96 FRIEND_TEST(DeviceTest, DestroyIPConfig);
Darin Petkovefb09c32011-06-07 20:24:17 -070097 FRIEND_TEST(IPConfigTest, UpdateCallback);
98 FRIEND_TEST(IPConfigTest, UpdateProperties);
Paul Stewartb6063942011-08-05 10:17:29 -070099 FRIEND_TEST(ResolverTest, Empty);
100 FRIEND_TEST(ResolverTest, NonEmpty);
Paul Stewart75e89d22011-08-01 10:00:02 -0700101 FRIEND_TEST(RoutingTableTest, RouteAddDelete);
Darin Petkovefb09c32011-06-07 20:24:17 -0700102
Chris Masone8a7b8be2011-07-22 12:43:37 -0700103 static const char kStorageType[];
Chris Masone0756f232011-07-21 17:24:00 -0700104 static const char kType[];
105 static uint global_serial_;
106
Darin Petkovf65e9282011-06-21 14:29:56 -0700107 const std::string device_name_;
Chris Masone0756f232011-07-21 17:24:00 -0700108 const std::string type_;
109 const uint serial_;
Chris Masone19e30402011-07-19 15:48:47 -0700110 scoped_ptr<IPConfigAdaptorInterface> adaptor_;
Darin Petkove7cb7f82011-06-03 13:21:51 -0700111 Properties properties_;
Chris Masone2b105542011-06-22 10:58:09 -0700112 scoped_ptr<Callback2<const IPConfigRefPtr&, bool>::Type> update_callback_;
Darin Petkove02b3ca2011-05-31 16:00:44 -0700113
Chris Masone0756f232011-07-21 17:24:00 -0700114 void Init();
115
Darin Petkove02b3ca2011-05-31 16:00:44 -0700116 DISALLOW_COPY_AND_ASSIGN(IPConfig);
117};
118
119} // namespace shill
120
121#endif // SHILL_IPCONFIG_