blob: 60f6fb8b1ed10f134608247ba3dbfc2f9175bc4a [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
Eric Shienbrood3e20a232012-02-16 11:35:56 -050011#include <base/callback.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"
Darin Petkov60596742012-03-05 12:17:17 +010019#include "shill/routing_table_entry.h"
Chris Masone2b105542011-06-22 10:58:09 -070020
Chris Masonec1e50412011-06-07 13:04:53 -070021namespace shill {
Chris Masonec6c6c132011-06-30 11:29:52 -070022class ControlInterface;
23class Error;
24class IPConfigAdaptorInterface;
Paul Stewart1062d9d2012-04-27 10:42:27 -070025class StaticIPParameters;
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 Stewart48100b02012-03-19 07:53:52 -070040 subnet_prefix(0),
Ben Chana0163122012-09-25 15:10:52 -070041 blackhole_ipv6(false),
Paul Stewartc39f1132011-06-22 12:02:28 -070042 mtu(0) {}
43
Paul Stewart1d18e8c2011-07-15 11:00:31 -070044 IPAddress::Family address_family;
Darin Petkove7cb7f82011-06-03 13:21:51 -070045 std::string address;
Paul Stewart48100b02012-03-19 07:53:52 -070046 int32 subnet_prefix;
Darin Petkove7cb7f82011-06-03 13:21:51 -070047 std::string broadcast_address;
Darin Petkove7cb7f82011-06-03 13:21:51 -070048 std::vector<std::string> dns_servers;
49 std::string domain_name;
50 std::vector<std::string> domain_search;
Chris Masone43b48a12011-07-01 13:37:07 -070051 std::string gateway;
52 std::string method;
53 std::string peer_address;
Paul Stewartce4ec192012-03-14 12:53:46 -070054 // Used by OpenVPN to signify a destination that should bypass any default
55 // route installed. This is usually the external IP address of the VPN
56 // server.
57 std::string trusted_ip;
Ben Chana0163122012-09-25 15:10:52 -070058 bool blackhole_ipv6;
Chris Masone43b48a12011-07-01 13:37:07 -070059 int32 mtu;
Darin Petkov60596742012-03-05 12:17:17 +010060 std::vector<Route> routes;
Paul Stewartc3fdba92013-12-02 11:12:38 -080061 // Vendor encapsulated option string gained from DHCP.
62 std::string vendor_encapsulated_options;
Paul Stewarta63f5212013-06-25 15:29:40 -070063 // Web Proxy Auto Discovery (WPAD) URL gained from DHCP.
64 std::string web_proxy_auto_discovery;
Darin Petkove7cb7f82011-06-03 13:21:51 -070065 };
66
Paul Stewart217c61d2013-06-13 15:12:02 -070067 enum ReleaseReason {
68 kReleaseReasonDisconnect,
69 kReleaseReasonStaticIP
70 };
71
Chris Masone19e30402011-07-19 15:48:47 -070072 IPConfig(ControlInterface *control_interface, const std::string &device_name);
Chris Masone0756f232011-07-21 17:24:00 -070073 IPConfig(ControlInterface *control_interface,
74 const std::string &device_name,
75 const std::string &type);
Darin Petkove02b3ca2011-05-31 16:00:44 -070076 virtual ~IPConfig();
77
Darin Petkovf65e9282011-06-21 14:29:56 -070078 const std::string &device_name() const { return device_name_; }
Chris Masone0756f232011-07-21 17:24:00 -070079 const std::string &type() const { return type_; }
80 uint serial() const { return serial_; }
Darin Petkove02b3ca2011-05-31 16:00:44 -070081
Chris Masone4e851612011-07-01 10:46:53 -070082 std::string GetRpcIdentifier();
83
Darin Petkovefb09c32011-06-07 20:24:17 -070084 // Registers a callback that's executed every time the configuration
85 // properties change. Takes ownership of |callback|. Pass NULL to remove a
Darin Petkovf9b0ca82011-06-20 12:10:23 -070086 // callback. The callback's first argument is a pointer to this IP
87 // configuration instance allowing clients to more easily manage multiple IP
88 // configurations. The callback's second argument is set to false if IP
89 // configuration failed.
Chris Masone2b105542011-06-22 10:58:09 -070090 void RegisterUpdateCallback(
Eric Shienbrood3e20a232012-02-16 11:35:56 -050091 const base::Callback<void(const IPConfigRefPtr&, bool)> &callback);
Darin Petkove7cb7f82011-06-03 13:21:51 -070092
Darin Petkov79d74c92012-03-07 17:20:32 +010093 void set_properties(const Properties &props) { properties_ = props; }
Thieu Le03026662013-04-04 10:45:11 -070094 virtual const Properties &properties() const { return properties_; }
Darin Petkove7cb7f82011-06-03 13:21:51 -070095
Darin Petkov92c43902011-06-09 20:46:06 -070096 // Request, renew and release IP configuration. Return true on success, false
Darin Petkove7cb7f82011-06-03 13:21:51 -070097 // otherwise. The default implementation always returns false indicating a
Paul Stewarta02ee492012-05-16 10:04:53 -070098 // failure. ReleaseIP is advisory: if we are no longer connected, it is not
99 // possible to properly vacate the lease on the remote server. Also,
100 // depending on the configuration of the specific IPConfig subclass, we may
101 // end up holding on to the lease so we can resume to the network lease
102 // faster.
Darin Petkov92c43902011-06-09 20:46:06 -0700103 virtual bool RequestIP();
104 virtual bool RenewIP();
Paul Stewart217c61d2013-06-13 15:12:02 -0700105 virtual bool ReleaseIP(ReleaseReason reason);
Darin Petkovd1b715b2011-06-02 21:21:22 -0700106
Paul Stewart4558bda2012-08-03 10:44:10 -0700107 // Refresh IP configuration. Called by the DBus Adaptor "Refresh" call.
108 void Refresh(Error *error);
109
mukesh agrawalde29fa82011-09-16 16:16:36 -0700110 PropertyStore *mutable_store() { return &store_; }
111 const PropertyStore &store() const { return store_; }
Paul Stewartdef189e2012-08-02 20:12:09 -0700112 void ApplyStaticIPParameters(StaticIPParameters *static_ip_parameters);
Chris Masone43b48a12011-07-01 13:37:07 -0700113
Chris Masone34af2182011-08-22 11:59:36 -0700114 // |id_suffix| is used to generate a storage ID that binds this instance
115 // to its associated device.
116 virtual bool Load(StoreInterface *storage, const std::string &id_suffix);
117 virtual bool Save(StoreInterface *storage, const std::string &id_suffix);
Chris Masone8a7b8be2011-07-22 12:43:37 -0700118
Darin Petkovefb09c32011-06-07 20:24:17 -0700119 protected:
mukesh agrawal7aed61c2013-04-22 16:01:24 -0700120 // Inform RPC listeners of changes to our properties. MAY emit
121 // changes even on unchanged properties.
122 virtual void EmitChanges();
123
Darin Petkovefb09c32011-06-07 20:24:17 -0700124 // Updates the IP configuration properties and notifies registered listeners
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700125 // about the event. |success| is set to false if the IP configuration failed.
mukesh agrawalcc0fded2012-05-09 13:40:58 -0700126 virtual void UpdateProperties(const Properties &properties, bool success);
Darin Petkovefb09c32011-06-07 20:24:17 -0700127
Chris Masone34af2182011-08-22 11:59:36 -0700128 // |id_suffix| is appended to the storage id, intended to bind this instance
129 // to its associated device.
130 std::string GetStorageIdentifier(const std::string &id_suffix);
131
Darin Petkove02b3ca2011-05-31 16:00:44 -0700132 private:
Chris Masonec6c6c132011-06-30 11:29:52 -0700133 friend class IPConfigAdaptorInterface;
mukesh agrawal7aed61c2013-04-22 16:01:24 -0700134 friend class IPConfigTest;
Paul Stewartdd60e452011-08-08 11:38:36 -0700135 friend class ConnectionTest;
Chris Masonec6c6c132011-06-30 11:29:52 -0700136
Paul Stewart2bf1d352011-12-06 15:02:55 -0800137 FRIEND_TEST(DeviceTest, AcquireIPConfig);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700138 FRIEND_TEST(DeviceTest, DestroyIPConfig);
Paul Stewartfa11e282013-12-02 22:04:25 -0800139 FRIEND_TEST(DeviceTest, IsConnectedViaTether);
Darin Petkovefb09c32011-06-07 20:24:17 -0700140 FRIEND_TEST(IPConfigTest, UpdateCallback);
141 FRIEND_TEST(IPConfigTest, UpdateProperties);
Paul Stewartb6063942011-08-05 10:17:29 -0700142 FRIEND_TEST(ResolverTest, Empty);
143 FRIEND_TEST(ResolverTest, NonEmpty);
Paul Stewart3f68bb12012-03-15 13:33:10 -0700144 FRIEND_TEST(RoutingTableTest, ConfigureRoutes);
Paul Stewart75e89d22011-08-01 10:00:02 -0700145 FRIEND_TEST(RoutingTableTest, RouteAddDelete);
Thieu Lecaef8932012-02-28 16:06:59 -0800146 FRIEND_TEST(RoutingTableTest, RouteDeleteForeign);
Darin Petkovefb09c32011-06-07 20:24:17 -0700147
Chris Masone8a7b8be2011-07-22 12:43:37 -0700148 static const char kStorageType[];
Chris Masone0756f232011-07-21 17:24:00 -0700149 static const char kType[];
Chris Masone0756f232011-07-21 17:24:00 -0700150
mukesh agrawal7aed61c2013-04-22 16:01:24 -0700151 void Init();
152
153 static uint global_serial_;
Paul Stewartac4ac002011-08-26 12:04:26 -0700154 PropertyStore store_;
Darin Petkovf65e9282011-06-21 14:29:56 -0700155 const std::string device_name_;
Chris Masone0756f232011-07-21 17:24:00 -0700156 const std::string type_;
157 const uint serial_;
Chris Masone19e30402011-07-19 15:48:47 -0700158 scoped_ptr<IPConfigAdaptorInterface> adaptor_;
Darin Petkove7cb7f82011-06-03 13:21:51 -0700159 Properties properties_;
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500160 base::Callback<void(const IPConfigRefPtr&, bool)> update_callback_;
Darin Petkove02b3ca2011-05-31 16:00:44 -0700161
Darin Petkove02b3ca2011-05-31 16:00:44 -0700162 DISALLOW_COPY_AND_ASSIGN(IPConfig);
163};
164
165} // namespace shill
166
167#endif // SHILL_IPCONFIG_