blob: b3afed2e2e9c814218a66e971dc4d465acd3df01 [file] [log] [blame]
Thieu Lecaef8932012-02-28 16:06:59 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkove02b3ca2011-05-31 16:00:44 -07002// 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"
Darin Petkov60596742012-03-05 12:17:17 +010020#include "shill/routing_table_entry.h"
Chris Masone2b105542011-06-22 10:58:09 -070021
Chris Masonec1e50412011-06-07 13:04:53 -070022namespace shill {
Chris Masonec6c6c132011-06-30 11:29:52 -070023class ControlInterface;
24class Error;
25class IPConfigAdaptorInterface;
Chris Masone8a7b8be2011-07-22 12:43:37 -070026class StoreInterface;
Darin Petkove02b3ca2011-05-31 16:00:44 -070027
28// IPConfig superclass. Individual IP configuration types will inherit from this
29// class.
Chris Masone27c4aa52011-07-02 13:10:14 -070030class IPConfig : public base::RefCounted<IPConfig> {
Darin Petkove02b3ca2011-05-31 16:00:44 -070031 public:
Darin Petkov60596742012-03-05 12:17:17 +010032 struct Route {
33 std::string host;
34 std::string netmask;
35 std::string gateway;
36 };
37
Paul Stewartc39f1132011-06-22 12:02:28 -070038 struct Properties {
Paul Stewart7355ce12011-09-02 10:47:01 -070039 Properties() : address_family(IPAddress::kFamilyUnknown),
Paul Stewartc39f1132011-06-22 12:02:28 -070040 subnet_cidr(0),
41 mtu(0) {}
42
Paul Stewart1d18e8c2011-07-15 11:00:31 -070043 IPAddress::Family address_family;
Darin Petkove7cb7f82011-06-03 13:21:51 -070044 std::string address;
Chris Masone43b48a12011-07-01 13:37:07 -070045 int32 subnet_cidr;
Darin Petkove7cb7f82011-06-03 13:21:51 -070046 std::string broadcast_address;
Darin Petkove7cb7f82011-06-03 13:21:51 -070047 std::vector<std::string> dns_servers;
48 std::string domain_name;
49 std::vector<std::string> domain_search;
Chris Masone43b48a12011-07-01 13:37:07 -070050 std::string gateway;
51 std::string method;
52 std::string peer_address;
53 int32 mtu;
Darin Petkov60596742012-03-05 12:17:17 +010054 std::vector<Route> routes;
Darin Petkove7cb7f82011-06-03 13:21:51 -070055 };
56
Chris Masone19e30402011-07-19 15:48:47 -070057 IPConfig(ControlInterface *control_interface, const std::string &device_name);
Chris Masone0756f232011-07-21 17:24:00 -070058 IPConfig(ControlInterface *control_interface,
59 const std::string &device_name,
60 const std::string &type);
Darin Petkove02b3ca2011-05-31 16:00:44 -070061 virtual ~IPConfig();
62
Darin Petkovf65e9282011-06-21 14:29:56 -070063 const std::string &device_name() const { return device_name_; }
Chris Masone0756f232011-07-21 17:24:00 -070064 const std::string &type() const { return type_; }
65 uint serial() const { return serial_; }
Darin Petkove02b3ca2011-05-31 16:00:44 -070066
Chris Masone4e851612011-07-01 10:46:53 -070067 std::string GetRpcIdentifier();
68
Darin Petkovefb09c32011-06-07 20:24:17 -070069 // Registers a callback that's executed every time the configuration
70 // properties change. Takes ownership of |callback|. Pass NULL to remove a
Darin Petkovf9b0ca82011-06-20 12:10:23 -070071 // callback. The callback's first argument is a pointer to this IP
72 // configuration instance allowing clients to more easily manage multiple IP
73 // configurations. The callback's second argument is set to false if IP
74 // configuration failed.
Chris Masone2b105542011-06-22 10:58:09 -070075 void RegisterUpdateCallback(
76 Callback2<const IPConfigRefPtr&, bool>::Type *callback);
Darin Petkove7cb7f82011-06-03 13:21:51 -070077
78 const Properties &properties() const { return properties_; }
79
Darin Petkov92c43902011-06-09 20:46:06 -070080 // Request, renew and release IP configuration. Return true on success, false
Darin Petkove7cb7f82011-06-03 13:21:51 -070081 // otherwise. The default implementation always returns false indicating a
82 // failure.
Darin Petkov92c43902011-06-09 20:46:06 -070083 virtual bool RequestIP();
84 virtual bool RenewIP();
85 virtual bool ReleaseIP();
Darin Petkovd1b715b2011-06-02 21:21:22 -070086
mukesh agrawalde29fa82011-09-16 16:16:36 -070087 PropertyStore *mutable_store() { return &store_; }
88 const PropertyStore &store() const { return store_; }
Chris Masone43b48a12011-07-01 13:37:07 -070089
Chris Masone34af2182011-08-22 11:59:36 -070090 // |id_suffix| is used to generate a storage ID that binds this instance
91 // to its associated device.
92 virtual bool Load(StoreInterface *storage, const std::string &id_suffix);
93 virtual bool Save(StoreInterface *storage, const std::string &id_suffix);
Chris Masone8a7b8be2011-07-22 12:43:37 -070094
Darin Petkovefb09c32011-06-07 20:24:17 -070095 protected:
96 // Updates the IP configuration properties and notifies registered listeners
Darin Petkovf9b0ca82011-06-20 12:10:23 -070097 // about the event. |success| is set to false if the IP configuration failed.
98 void UpdateProperties(const Properties &properties, bool success);
Darin Petkovefb09c32011-06-07 20:24:17 -070099
Chris Masone34af2182011-08-22 11:59:36 -0700100 // |id_suffix| is appended to the storage id, intended to bind this instance
101 // to its associated device.
102 std::string GetStorageIdentifier(const std::string &id_suffix);
103
Darin Petkove02b3ca2011-05-31 16:00:44 -0700104 private:
Chris Masonec6c6c132011-06-30 11:29:52 -0700105 friend class IPConfigAdaptorInterface;
Paul Stewartdd60e452011-08-08 11:38:36 -0700106 friend class ConnectionTest;
Chris Masonec6c6c132011-06-30 11:29:52 -0700107
Paul Stewart2bf1d352011-12-06 15:02:55 -0800108 FRIEND_TEST(DeviceTest, AcquireIPConfig);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700109 FRIEND_TEST(DeviceTest, DestroyIPConfig);
Darin Petkovefb09c32011-06-07 20:24:17 -0700110 FRIEND_TEST(IPConfigTest, UpdateCallback);
111 FRIEND_TEST(IPConfigTest, UpdateProperties);
Paul Stewartb6063942011-08-05 10:17:29 -0700112 FRIEND_TEST(ResolverTest, Empty);
113 FRIEND_TEST(ResolverTest, NonEmpty);
Paul Stewart75e89d22011-08-01 10:00:02 -0700114 FRIEND_TEST(RoutingTableTest, RouteAddDelete);
Thieu Lecaef8932012-02-28 16:06:59 -0800115 FRIEND_TEST(RoutingTableTest, RouteDeleteForeign);
Darin Petkovefb09c32011-06-07 20:24:17 -0700116
Chris Masone8a7b8be2011-07-22 12:43:37 -0700117 static const char kStorageType[];
Chris Masone0756f232011-07-21 17:24:00 -0700118 static const char kType[];
119 static uint global_serial_;
120
Paul Stewartac4ac002011-08-26 12:04:26 -0700121 PropertyStore store_;
Darin Petkovf65e9282011-06-21 14:29:56 -0700122 const std::string device_name_;
Chris Masone0756f232011-07-21 17:24:00 -0700123 const std::string type_;
124 const uint serial_;
Chris Masone19e30402011-07-19 15:48:47 -0700125 scoped_ptr<IPConfigAdaptorInterface> adaptor_;
Darin Petkove7cb7f82011-06-03 13:21:51 -0700126 Properties properties_;
Chris Masone2b105542011-06-22 10:58:09 -0700127 scoped_ptr<Callback2<const IPConfigRefPtr&, bool>::Type> update_callback_;
Darin Petkove02b3ca2011-05-31 16:00:44 -0700128
Chris Masone0756f232011-07-21 17:24:00 -0700129 void Init();
130
Darin Petkove02b3ca2011-05-31 16:00:44 -0700131 DISALLOW_COPY_AND_ASSIGN(IPConfig);
132};
133
134} // namespace shill
135
136#endif // SHILL_IPCONFIG_