Thieu Le | caef893 | 2012-02-28 16:06:59 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Ben Chan | c45688b | 2014-07-02 23:50:45 -0700 | [diff] [blame] | 5 | #ifndef SHILL_IPCONFIG_H_ |
| 6 | #define SHILL_IPCONFIG_H_ |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 7 | |
Ben Chan | cd47732 | 2014-10-17 14:19:30 -0700 | [diff] [blame] | 8 | #include <memory> |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 9 | #include <string> |
Samuel Tan | 815a6fb | 2014-10-23 16:53:59 -0700 | [diff] [blame^] | 10 | #include <sys/time.h> |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 11 | #include <vector> |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 12 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 13 | #include <base/callback.h> |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 14 | #include <base/memory/ref_counted.h> |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 15 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 16 | |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 17 | #include "shill/ip_address.h" |
Chris Masone | c6c6c13 | 2011-06-30 11:29:52 -0700 | [diff] [blame] | 18 | #include "shill/property_store.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 19 | #include "shill/refptr_types.h" |
Darin Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 20 | #include "shill/routing_table_entry.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 21 | |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 22 | namespace shill { |
Chris Masone | c6c6c13 | 2011-06-30 11:29:52 -0700 | [diff] [blame] | 23 | class ControlInterface; |
| 24 | class Error; |
| 25 | class IPConfigAdaptorInterface; |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 26 | class StaticIPParameters; |
Samuel Tan | 815a6fb | 2014-10-23 16:53:59 -0700 | [diff] [blame^] | 27 | class Time; |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 28 | |
| 29 | // IPConfig superclass. Individual IP configuration types will inherit from this |
| 30 | // class. |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 31 | class IPConfig : public base::RefCounted<IPConfig> { |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 32 | public: |
Darin Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 33 | struct Route { |
| 34 | std::string host; |
| 35 | std::string netmask; |
| 36 | std::string gateway; |
| 37 | }; |
| 38 | |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 39 | struct Properties { |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 40 | Properties() : address_family(IPAddress::kFamilyUnknown), |
Paul Stewart | 48100b0 | 2012-03-19 07:53:52 -0700 | [diff] [blame] | 41 | subnet_prefix(0), |
Ben Chan | a016312 | 2012-09-25 15:10:52 -0700 | [diff] [blame] | 42 | blackhole_ipv6(false), |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame] | 43 | mtu(0), |
| 44 | lease_duration_seconds(0) {} |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 45 | |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 46 | IPAddress::Family address_family; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 47 | std::string address; |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 48 | int32_t subnet_prefix; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 49 | std::string broadcast_address; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 50 | std::vector<std::string> dns_servers; |
| 51 | std::string domain_name; |
| 52 | std::vector<std::string> domain_search; |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 53 | std::string gateway; |
| 54 | std::string method; |
| 55 | std::string peer_address; |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 56 | // 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 Chan | a016312 | 2012-09-25 15:10:52 -0700 | [diff] [blame] | 60 | bool blackhole_ipv6; |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 61 | int32_t mtu; |
Darin Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 62 | std::vector<Route> routes; |
Paul Stewart | c3fdba9 | 2013-12-02 11:12:38 -0800 | [diff] [blame] | 63 | // Vendor encapsulated option string gained from DHCP. |
| 64 | std::string vendor_encapsulated_options; |
Paul Stewart | a63f521 | 2013-06-25 15:29:40 -0700 | [diff] [blame] | 65 | // Web Proxy Auto Discovery (WPAD) URL gained from DHCP. |
| 66 | std::string web_proxy_auto_discovery; |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame] | 67 | // Length of time the lease was granted. |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 68 | uint32_t lease_duration_seconds; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 69 | }; |
| 70 | |
Ben Chan | 26692bd | 2014-02-03 16:34:55 -0800 | [diff] [blame] | 71 | enum Method { |
| 72 | kMethodUnknown, |
| 73 | kMethodPPP, |
| 74 | kMethodStatic, |
| 75 | kMethodDHCP |
| 76 | }; |
| 77 | |
Paul Stewart | 217c61d | 2013-06-13 15:12:02 -0700 | [diff] [blame] | 78 | enum ReleaseReason { |
| 79 | kReleaseReasonDisconnect, |
| 80 | kReleaseReasonStaticIP |
| 81 | }; |
| 82 | |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 83 | typedef base::Callback<void(const IPConfigRefPtr&)> Callback; |
| 84 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 85 | IPConfig(ControlInterface *control_interface, const std::string &device_name); |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 86 | IPConfig(ControlInterface *control_interface, |
| 87 | const std::string &device_name, |
| 88 | const std::string &type); |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 89 | virtual ~IPConfig(); |
| 90 | |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame] | 91 | const std::string &device_name() const { return device_name_; } |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 92 | const std::string &type() const { return type_; } |
| 93 | uint serial() const { return serial_; } |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 94 | |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 95 | std::string GetRpcIdentifier(); |
| 96 | |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 97 | // Registers a callback that's executed every time the configuration |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 98 | // properties are acquired. Takes ownership of |callback|. Pass NULL |
| 99 | // to remove a callback. The callback's argument is a pointer to this IP |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 100 | // configuration instance allowing clients to more easily manage multiple IP |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 101 | // configurations. |
| 102 | void RegisterUpdateCallback(const Callback &callback); |
| 103 | |
| 104 | // Registers a callback that's executed every time the configuration |
| 105 | // properties fail to be acquired. Takes ownership of |callback|. Pass NULL |
| 106 | // to remove a callback. The callback's argument is a pointer to this IP |
| 107 | // configuration instance allowing clients to more easily manage multiple IP |
| 108 | // configurations. |
| 109 | void RegisterFailureCallback(const Callback &callback); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 110 | |
Paul Stewart | 8223653 | 2013-12-10 15:33:11 -0800 | [diff] [blame] | 111 | // Registers a callback that's executed every time the Refresh method |
| 112 | // on the ipconfig is called. Takes ownership of |callback|. Pass NULL |
| 113 | // to remove a callback. The callback's argument is a pointer to this IP |
| 114 | // configuration instance allowing clients to more easily manage multiple IP |
| 115 | // configurations. |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 116 | void RegisterRefreshCallback(const Callback &callback); |
Paul Stewart | 8223653 | 2013-12-10 15:33:11 -0800 | [diff] [blame] | 117 | |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame] | 118 | // Registers a callback that's executed every time the the lease exipres |
| 119 | // and the IPConfig is about to perform a restart to attempt to regain it. |
| 120 | // Takes ownership of |callback|. Pass NULL to remove a callback. The |
| 121 | // callback's argument is a pointer to this IP configuration instance |
| 122 | // allowing clients to more easily manage multiple IP configurations. |
| 123 | void RegisterExpireCallback(const Callback &callback); |
| 124 | |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 125 | void set_properties(const Properties &props) { properties_ = props; } |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 126 | virtual const Properties &properties() const { return properties_; } |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 127 | |
Peter Qiu | a89154b | 2014-05-23 15:45:42 -0700 | [diff] [blame] | 128 | // Update DNS servers setting for this ipconfig, this allows Chrome |
| 129 | // to retrieve the new DNS servers. |
| 130 | virtual void UpdateDNSServers(const std::vector<std::string> &dns_servers); |
| 131 | |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 132 | // Reset the IPConfig properties to their default values. |
| 133 | virtual void ResetProperties(); |
| 134 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 135 | // Request, renew and release IP configuration. Return true on success, false |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 136 | // otherwise. The default implementation always returns false indicating a |
Paul Stewart | a02ee49 | 2012-05-16 10:04:53 -0700 | [diff] [blame] | 137 | // failure. ReleaseIP is advisory: if we are no longer connected, it is not |
| 138 | // possible to properly vacate the lease on the remote server. Also, |
| 139 | // depending on the configuration of the specific IPConfig subclass, we may |
| 140 | // end up holding on to the lease so we can resume to the network lease |
| 141 | // faster. |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 142 | virtual bool RequestIP(); |
| 143 | virtual bool RenewIP(); |
Paul Stewart | 217c61d | 2013-06-13 15:12:02 -0700 | [diff] [blame] | 144 | virtual bool ReleaseIP(ReleaseReason reason); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 145 | |
Paul Stewart | 4558bda | 2012-08-03 10:44:10 -0700 | [diff] [blame] | 146 | // Refresh IP configuration. Called by the DBus Adaptor "Refresh" call. |
| 147 | void Refresh(Error *error); |
| 148 | |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 149 | PropertyStore *mutable_store() { return &store_; } |
| 150 | const PropertyStore &store() const { return store_; } |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 151 | void ApplyStaticIPParameters(StaticIPParameters *static_ip_parameters); |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 152 | |
Paul Stewart | 8223653 | 2013-12-10 15:33:11 -0800 | [diff] [blame] | 153 | // Restore the fields of |properties_| to their original values before |
| 154 | // static IP parameters were previously applied. |
| 155 | void RestoreSavedIPParameters(StaticIPParameters *static_ip_parameters); |
| 156 | |
Samuel Tan | 815a6fb | 2014-10-23 16:53:59 -0700 | [diff] [blame^] | 157 | // Updates |current_lease_expiration_time_| by adding |new_lease_duration| to |
| 158 | // the current time. |
| 159 | virtual void UpdateLeaseExpirationTime(uint32_t new_lease_duration); |
| 160 | |
| 161 | // Resets |current_lease_expiration_time_| to its default value. |
| 162 | virtual void ResetLeaseExpirationTime(); |
| 163 | |
| 164 | // Returns the time left (in seconds) till the current DHCP lease is to be |
| 165 | // renewed in |time_left|. Returns false if an error occurs (i.e. current |
| 166 | // lease has already expired or no current DHCP lease), true otherwise. |
| 167 | bool TimeToLeaseExpiry(uint32_t *time_left); |
| 168 | |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 169 | protected: |
mukesh agrawal | 7aed61c | 2013-04-22 16:01:24 -0700 | [diff] [blame] | 170 | // Inform RPC listeners of changes to our properties. MAY emit |
| 171 | // changes even on unchanged properties. |
| 172 | virtual void EmitChanges(); |
| 173 | |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 174 | // Updates the IP configuration properties and notifies registered listeners |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 175 | // about the event. |
| 176 | virtual void UpdateProperties(const Properties &properties); |
| 177 | |
| 178 | // Notifies registered listeners that the configuration process has failed. |
| 179 | virtual void NotifyFailure(); |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 180 | |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame] | 181 | // Notifies registered listeners that the lease has expired. |
| 182 | virtual void NotifyExpiry(); |
| 183 | |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 184 | private: |
Chris Masone | c6c6c13 | 2011-06-30 11:29:52 -0700 | [diff] [blame] | 185 | friend class IPConfigAdaptorInterface; |
mukesh agrawal | 7aed61c | 2013-04-22 16:01:24 -0700 | [diff] [blame] | 186 | friend class IPConfigTest; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 187 | friend class ConnectionTest; |
Chris Masone | c6c6c13 | 2011-06-30 11:29:52 -0700 | [diff] [blame] | 188 | |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 189 | FRIEND_TEST(DeviceTest, AcquireIPConfig); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 190 | FRIEND_TEST(DeviceTest, DestroyIPConfig); |
Paul Stewart | fa11e28 | 2013-12-02 22:04:25 -0800 | [diff] [blame] | 191 | FRIEND_TEST(DeviceTest, IsConnectedViaTether); |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame] | 192 | FRIEND_TEST(DeviceTest, OnIPConfigExpired); |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 193 | FRIEND_TEST(IPConfigTest, UpdateCallback); |
| 194 | FRIEND_TEST(IPConfigTest, UpdateProperties); |
Samuel Tan | 815a6fb | 2014-10-23 16:53:59 -0700 | [diff] [blame^] | 195 | FRIEND_TEST(IPConfigTest, UpdateLeaseExpirationTime); |
| 196 | FRIEND_TEST(IPConfigTest, TimeToLeaseExpiry_NoDHCPLease); |
| 197 | FRIEND_TEST(IPConfigTest, TimeToLeaseExpiry_CurrentLeaseExpired); |
| 198 | FRIEND_TEST(IPConfigTest, TimeToLeaseExpiry_Success); |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 199 | FRIEND_TEST(ResolverTest, Empty); |
| 200 | FRIEND_TEST(ResolverTest, NonEmpty); |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 201 | FRIEND_TEST(RoutingTableTest, ConfigureRoutes); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 202 | FRIEND_TEST(RoutingTableTest, RouteAddDelete); |
Thieu Le | caef893 | 2012-02-28 16:06:59 -0800 | [diff] [blame] | 203 | FRIEND_TEST(RoutingTableTest, RouteDeleteForeign); |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 204 | |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 205 | static const char kType[]; |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 206 | |
mukesh agrawal | 7aed61c | 2013-04-22 16:01:24 -0700 | [diff] [blame] | 207 | void Init(); |
| 208 | |
| 209 | static uint global_serial_; |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 210 | PropertyStore store_; |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame] | 211 | const std::string device_name_; |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 212 | const std::string type_; |
| 213 | const uint serial_; |
Ben Chan | cd47732 | 2014-10-17 14:19:30 -0700 | [diff] [blame] | 214 | std::unique_ptr<IPConfigAdaptorInterface> adaptor_; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 215 | Properties properties_; |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 216 | Callback update_callback_; |
| 217 | Callback failure_callback_; |
| 218 | Callback refresh_callback_; |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame] | 219 | Callback expire_callback_; |
Samuel Tan | 815a6fb | 2014-10-23 16:53:59 -0700 | [diff] [blame^] | 220 | struct timeval current_lease_expiration_time_; |
| 221 | Time *time_; |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 222 | |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 223 | DISALLOW_COPY_AND_ASSIGN(IPConfig); |
| 224 | }; |
| 225 | |
| 226 | } // namespace shill |
| 227 | |
Ben Chan | c45688b | 2014-07-02 23:50:45 -0700 | [diff] [blame] | 228 | #endif // SHILL_IPCONFIG_H_ |