blob: e98dd761dd21faa0bf6f94bf97deb3825a182ffa [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
Ben Chanc45688b2014-07-02 23:50:45 -07005#ifndef SHILL_IPCONFIG_H_
6#define SHILL_IPCONFIG_H_
Darin Petkove02b3ca2011-05-31 16:00:44 -07007
Ben Chancd477322014-10-17 14:19:30 -07008#include <memory>
Darin Petkove02b3ca2011-05-31 16:00:44 -07009#include <string>
Samuel Tan815a6fb2014-10-23 16:53:59 -070010#include <sys/time.h>
Darin Petkove7cb7f82011-06-03 13:21:51 -070011#include <vector>
Darin Petkove02b3ca2011-05-31 16:00:44 -070012
Eric Shienbrood3e20a232012-02-16 11:35:56 -050013#include <base/callback.h>
Darin Petkove02b3ca2011-05-31 16:00:44 -070014#include <base/memory/ref_counted.h>
Darin Petkovefb09c32011-06-07 20:24:17 -070015#include <gtest/gtest_prod.h> // for FRIEND_TEST
Darin Petkove02b3ca2011-05-31 16:00:44 -070016
Peter Qiu8d6b5972014-10-28 15:33:34 -070017#include "shill/net/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;
Paul Stewart1062d9d2012-04-27 10:42:27 -070026class StaticIPParameters;
Samuel Tan815a6fb2014-10-23 16:53:59 -070027class Time;
Darin Petkove02b3ca2011-05-31 16:00:44 -070028
29// IPConfig superclass. Individual IP configuration types will inherit from this
30// class.
Chris Masone27c4aa52011-07-02 13:10:14 -070031class IPConfig : public base::RefCounted<IPConfig> {
Darin Petkove02b3ca2011-05-31 16:00:44 -070032 public:
Darin Petkov60596742012-03-05 12:17:17 +010033 struct Route {
34 std::string host;
35 std::string netmask;
36 std::string gateway;
37 };
38
Paul Stewartc39f1132011-06-22 12:02:28 -070039 struct Properties {
Paul Stewart7355ce12011-09-02 10:47:01 -070040 Properties() : address_family(IPAddress::kFamilyUnknown),
Paul Stewart48100b02012-03-19 07:53:52 -070041 subnet_prefix(0),
Ben Chana0163122012-09-25 15:10:52 -070042 blackhole_ipv6(false),
Paul Stewart1f916e42013-12-23 09:52:54 -080043 mtu(0),
44 lease_duration_seconds(0) {}
Paul Stewartc39f1132011-06-22 12:02:28 -070045
Paul Stewart1d18e8c2011-07-15 11:00:31 -070046 IPAddress::Family address_family;
Darin Petkove7cb7f82011-06-03 13:21:51 -070047 std::string address;
Ben Chan7fab8972014-08-10 17:14:46 -070048 int32_t subnet_prefix;
Darin Petkove7cb7f82011-06-03 13:21:51 -070049 std::string broadcast_address;
Darin Petkove7cb7f82011-06-03 13:21:51 -070050 std::vector<std::string> dns_servers;
51 std::string domain_name;
52 std::vector<std::string> domain_search;
Chris Masone43b48a12011-07-01 13:37:07 -070053 std::string gateway;
54 std::string method;
55 std::string peer_address;
Paul Stewartce4ec192012-03-14 12:53:46 -070056 // Used by OpenVPN to signify a destination that should bypass any default
57 // route installed. This is usually the external IP address of the VPN
58 // server.
59 std::string trusted_ip;
Ben Chana0163122012-09-25 15:10:52 -070060 bool blackhole_ipv6;
Ben Chan7fab8972014-08-10 17:14:46 -070061 int32_t mtu;
Darin Petkov60596742012-03-05 12:17:17 +010062 std::vector<Route> routes;
Paul Stewartc3fdba92013-12-02 11:12:38 -080063 // Vendor encapsulated option string gained from DHCP.
64 std::string vendor_encapsulated_options;
Paul Stewarta63f5212013-06-25 15:29:40 -070065 // Web Proxy Auto Discovery (WPAD) URL gained from DHCP.
66 std::string web_proxy_auto_discovery;
Paul Stewart1f916e42013-12-23 09:52:54 -080067 // Length of time the lease was granted.
Ben Chan7fab8972014-08-10 17:14:46 -070068 uint32_t lease_duration_seconds;
Darin Petkove7cb7f82011-06-03 13:21:51 -070069 };
70
Ben Chan26692bd2014-02-03 16:34:55 -080071 enum Method {
72 kMethodUnknown,
73 kMethodPPP,
74 kMethodStatic,
75 kMethodDHCP
76 };
77
Paul Stewart217c61d2013-06-13 15:12:02 -070078 enum ReleaseReason {
79 kReleaseReasonDisconnect,
80 kReleaseReasonStaticIP
81 };
82
Samuel Tan3c3c36a2014-12-16 16:53:19 -080083 typedef base::Callback<void(const IPConfigRefPtr&, bool)> UpdateCallback;
Paul Stewartc5099532013-12-12 07:53:15 -080084 typedef base::Callback<void(const IPConfigRefPtr&)> Callback;
85
Chris Masone19e30402011-07-19 15:48:47 -070086 IPConfig(ControlInterface *control_interface, const std::string &device_name);
Chris Masone0756f232011-07-21 17:24:00 -070087 IPConfig(ControlInterface *control_interface,
88 const std::string &device_name,
89 const std::string &type);
Darin Petkove02b3ca2011-05-31 16:00:44 -070090 virtual ~IPConfig();
91
Darin Petkovf65e9282011-06-21 14:29:56 -070092 const std::string &device_name() const { return device_name_; }
Chris Masone0756f232011-07-21 17:24:00 -070093 const std::string &type() const { return type_; }
94 uint serial() const { return serial_; }
Darin Petkove02b3ca2011-05-31 16:00:44 -070095
Chris Masone4e851612011-07-01 10:46:53 -070096 std::string GetRpcIdentifier();
97
Darin Petkovefb09c32011-06-07 20:24:17 -070098 // Registers a callback that's executed every time the configuration
Paul Stewartc5099532013-12-12 07:53:15 -080099 // properties are acquired. Takes ownership of |callback|. Pass NULL
Samuel Tan3c3c36a2014-12-16 16:53:19 -0800100 // to remove a callback. The callback's first argument is a pointer to this IP
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700101 // configuration instance allowing clients to more easily manage multiple IP
Samuel Tan3c3c36a2014-12-16 16:53:19 -0800102 // configurations. The callback's second argument is a boolean indicating
103 // whether or not a DHCP lease was acquired from the server.
104 void RegisterUpdateCallback(const UpdateCallback &callback);
Paul Stewartc5099532013-12-12 07:53:15 -0800105
106 // Registers a callback that's executed every time the configuration
107 // properties fail to be acquired. Takes ownership of |callback|. Pass NULL
108 // to remove a callback. The callback's argument is a pointer to this IP
109 // configuration instance allowing clients to more easily manage multiple IP
110 // configurations.
111 void RegisterFailureCallback(const Callback &callback);
Darin Petkove7cb7f82011-06-03 13:21:51 -0700112
Paul Stewart82236532013-12-10 15:33:11 -0800113 // Registers a callback that's executed every time the Refresh method
114 // on the ipconfig is called. Takes ownership of |callback|. Pass NULL
115 // to remove a callback. The callback's argument is a pointer to this IP
116 // configuration instance allowing clients to more easily manage multiple IP
117 // configurations.
Paul Stewartc5099532013-12-12 07:53:15 -0800118 void RegisterRefreshCallback(const Callback &callback);
Paul Stewart82236532013-12-10 15:33:11 -0800119
Paul Stewart1f916e42013-12-23 09:52:54 -0800120 // Registers a callback that's executed every time the the lease exipres
121 // and the IPConfig is about to perform a restart to attempt to regain it.
122 // Takes ownership of |callback|. Pass NULL to remove a callback. The
123 // callback's argument is a pointer to this IP configuration instance
124 // allowing clients to more easily manage multiple IP configurations.
125 void RegisterExpireCallback(const Callback &callback);
126
Darin Petkov79d74c92012-03-07 17:20:32 +0100127 void set_properties(const Properties &props) { properties_ = props; }
Thieu Le03026662013-04-04 10:45:11 -0700128 virtual const Properties &properties() const { return properties_; }
Darin Petkove7cb7f82011-06-03 13:21:51 -0700129
Peter Qiua89154b2014-05-23 15:45:42 -0700130 // Update DNS servers setting for this ipconfig, this allows Chrome
131 // to retrieve the new DNS servers.
132 virtual void UpdateDNSServers(const std::vector<std::string> &dns_servers);
133
Paul Stewartc5099532013-12-12 07:53:15 -0800134 // Reset the IPConfig properties to their default values.
135 virtual void ResetProperties();
136
Darin Petkov92c43902011-06-09 20:46:06 -0700137 // Request, renew and release IP configuration. Return true on success, false
Darin Petkove7cb7f82011-06-03 13:21:51 -0700138 // otherwise. The default implementation always returns false indicating a
Paul Stewarta02ee492012-05-16 10:04:53 -0700139 // failure. ReleaseIP is advisory: if we are no longer connected, it is not
140 // possible to properly vacate the lease on the remote server. Also,
141 // depending on the configuration of the specific IPConfig subclass, we may
142 // end up holding on to the lease so we can resume to the network lease
143 // faster.
Darin Petkov92c43902011-06-09 20:46:06 -0700144 virtual bool RequestIP();
145 virtual bool RenewIP();
Paul Stewart217c61d2013-06-13 15:12:02 -0700146 virtual bool ReleaseIP(ReleaseReason reason);
Darin Petkovd1b715b2011-06-02 21:21:22 -0700147
Paul Stewart4558bda2012-08-03 10:44:10 -0700148 // Refresh IP configuration. Called by the DBus Adaptor "Refresh" call.
149 void Refresh(Error *error);
150
mukesh agrawalde29fa82011-09-16 16:16:36 -0700151 PropertyStore *mutable_store() { return &store_; }
152 const PropertyStore &store() const { return store_; }
Paul Stewartdef189e2012-08-02 20:12:09 -0700153 void ApplyStaticIPParameters(StaticIPParameters *static_ip_parameters);
Chris Masone43b48a12011-07-01 13:37:07 -0700154
Paul Stewart82236532013-12-10 15:33:11 -0800155 // Restore the fields of |properties_| to their original values before
156 // static IP parameters were previously applied.
157 void RestoreSavedIPParameters(StaticIPParameters *static_ip_parameters);
158
Samuel Tan815a6fb2014-10-23 16:53:59 -0700159 // Updates |current_lease_expiration_time_| by adding |new_lease_duration| to
160 // the current time.
161 virtual void UpdateLeaseExpirationTime(uint32_t new_lease_duration);
162
163 // Resets |current_lease_expiration_time_| to its default value.
164 virtual void ResetLeaseExpirationTime();
165
166 // Returns the time left (in seconds) till the current DHCP lease is to be
167 // renewed in |time_left|. Returns false if an error occurs (i.e. current
168 // lease has already expired or no current DHCP lease), true otherwise.
169 bool TimeToLeaseExpiry(uint32_t *time_left);
170
Darin Petkovefb09c32011-06-07 20:24:17 -0700171 protected:
mukesh agrawal7aed61c2013-04-22 16:01:24 -0700172 // Inform RPC listeners of changes to our properties. MAY emit
173 // changes even on unchanged properties.
174 virtual void EmitChanges();
175
Darin Petkovefb09c32011-06-07 20:24:17 -0700176 // Updates the IP configuration properties and notifies registered listeners
Paul Stewartc5099532013-12-12 07:53:15 -0800177 // about the event.
Samuel Tan3c3c36a2014-12-16 16:53:19 -0800178 virtual void UpdateProperties(const Properties &properties,
179 bool new_lease_acquired);
Paul Stewartc5099532013-12-12 07:53:15 -0800180
181 // Notifies registered listeners that the configuration process has failed.
182 virtual void NotifyFailure();
Darin Petkovefb09c32011-06-07 20:24:17 -0700183
Paul Stewart1f916e42013-12-23 09:52:54 -0800184 // Notifies registered listeners that the lease has expired.
185 virtual void NotifyExpiry();
186
Darin Petkove02b3ca2011-05-31 16:00:44 -0700187 private:
Chris Masonec6c6c132011-06-30 11:29:52 -0700188 friend class IPConfigAdaptorInterface;
mukesh agrawal7aed61c2013-04-22 16:01:24 -0700189 friend class IPConfigTest;
Paul Stewartdd60e452011-08-08 11:38:36 -0700190 friend class ConnectionTest;
Chris Masonec6c6c132011-06-30 11:29:52 -0700191
Paul Stewart2bf1d352011-12-06 15:02:55 -0800192 FRIEND_TEST(DeviceTest, AcquireIPConfig);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700193 FRIEND_TEST(DeviceTest, DestroyIPConfig);
Paul Stewartfa11e282013-12-02 22:04:25 -0800194 FRIEND_TEST(DeviceTest, IsConnectedViaTether);
Paul Stewart1f916e42013-12-23 09:52:54 -0800195 FRIEND_TEST(DeviceTest, OnIPConfigExpired);
Darin Petkovefb09c32011-06-07 20:24:17 -0700196 FRIEND_TEST(IPConfigTest, UpdateCallback);
197 FRIEND_TEST(IPConfigTest, UpdateProperties);
Samuel Tan815a6fb2014-10-23 16:53:59 -0700198 FRIEND_TEST(IPConfigTest, UpdateLeaseExpirationTime);
199 FRIEND_TEST(IPConfigTest, TimeToLeaseExpiry_NoDHCPLease);
200 FRIEND_TEST(IPConfigTest, TimeToLeaseExpiry_CurrentLeaseExpired);
201 FRIEND_TEST(IPConfigTest, TimeToLeaseExpiry_Success);
Paul Stewartb6063942011-08-05 10:17:29 -0700202 FRIEND_TEST(ResolverTest, Empty);
203 FRIEND_TEST(ResolverTest, NonEmpty);
Paul Stewart3f68bb12012-03-15 13:33:10 -0700204 FRIEND_TEST(RoutingTableTest, ConfigureRoutes);
Paul Stewart75e89d22011-08-01 10:00:02 -0700205 FRIEND_TEST(RoutingTableTest, RouteAddDelete);
Thieu Lecaef8932012-02-28 16:06:59 -0800206 FRIEND_TEST(RoutingTableTest, RouteDeleteForeign);
Darin Petkovefb09c32011-06-07 20:24:17 -0700207
Chris Masone0756f232011-07-21 17:24:00 -0700208 static const char kType[];
Chris Masone0756f232011-07-21 17:24:00 -0700209
mukesh agrawal7aed61c2013-04-22 16:01:24 -0700210 void Init();
211
212 static uint global_serial_;
Paul Stewartac4ac002011-08-26 12:04:26 -0700213 PropertyStore store_;
Darin Petkovf65e9282011-06-21 14:29:56 -0700214 const std::string device_name_;
Chris Masone0756f232011-07-21 17:24:00 -0700215 const std::string type_;
216 const uint serial_;
Ben Chancd477322014-10-17 14:19:30 -0700217 std::unique_ptr<IPConfigAdaptorInterface> adaptor_;
Darin Petkove7cb7f82011-06-03 13:21:51 -0700218 Properties properties_;
Samuel Tan3c3c36a2014-12-16 16:53:19 -0800219 UpdateCallback update_callback_;
Paul Stewartc5099532013-12-12 07:53:15 -0800220 Callback failure_callback_;
221 Callback refresh_callback_;
Paul Stewart1f916e42013-12-23 09:52:54 -0800222 Callback expire_callback_;
Samuel Tan815a6fb2014-10-23 16:53:59 -0700223 struct timeval current_lease_expiration_time_;
224 Time *time_;
Darin Petkove02b3ca2011-05-31 16:00:44 -0700225
Darin Petkove02b3ca2011-05-31 16:00:44 -0700226 DISALLOW_COPY_AND_ASSIGN(IPConfig);
227};
228
229} // namespace shill
230
Ben Chanc45688b2014-07-02 23:50:45 -0700231#endif // SHILL_IPCONFIG_H_