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 | |
| 5 | #ifndef SHILL_IPCONFIG_ |
| 6 | #define SHILL_IPCONFIG_ |
| 7 | |
| 8 | #include <string> |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 9 | #include <vector> |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 10 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 11 | #include <base/callback.h> |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 12 | #include <base/memory/ref_counted.h> |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 13 | #include <base/memory/scoped_ptr.h> |
| 14 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 15 | |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 16 | #include "shill/ip_address.h" |
Chris Masone | c6c6c13 | 2011-06-30 11:29:52 -0700 | [diff] [blame] | 17 | #include "shill/property_store.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 18 | #include "shill/refptr_types.h" |
Darin Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 19 | #include "shill/routing_table_entry.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 20 | |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 21 | namespace shill { |
Chris Masone | c6c6c13 | 2011-06-30 11:29:52 -0700 | [diff] [blame] | 22 | class ControlInterface; |
| 23 | class Error; |
| 24 | class IPConfigAdaptorInterface; |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 25 | class StaticIPParameters; |
Chris Masone | 8a7b8be | 2011-07-22 12:43:37 -0700 | [diff] [blame] | 26 | class StoreInterface; |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 27 | |
| 28 | // IPConfig superclass. Individual IP configuration types will inherit from this |
| 29 | // class. |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 30 | class IPConfig : public base::RefCounted<IPConfig> { |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 31 | public: |
Darin Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 32 | struct Route { |
| 33 | std::string host; |
| 34 | std::string netmask; |
| 35 | std::string gateway; |
| 36 | }; |
| 37 | |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 38 | struct Properties { |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 39 | Properties() : address_family(IPAddress::kFamilyUnknown), |
Paul Stewart | 48100b0 | 2012-03-19 07:53:52 -0700 | [diff] [blame] | 40 | subnet_prefix(0), |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 41 | mtu(0) {} |
| 42 | |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 43 | IPAddress::Family address_family; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 44 | std::string address; |
Paul Stewart | 48100b0 | 2012-03-19 07:53:52 -0700 | [diff] [blame] | 45 | int32 subnet_prefix; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 46 | std::string broadcast_address; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 47 | std::vector<std::string> dns_servers; |
| 48 | std::string domain_name; |
| 49 | std::vector<std::string> domain_search; |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 50 | std::string gateway; |
| 51 | std::string method; |
| 52 | std::string peer_address; |
Paul Stewart | ce4ec19 | 2012-03-14 12:53:46 -0700 | [diff] [blame] | 53 | // Used by OpenVPN to signify a destination that should bypass any default |
| 54 | // route installed. This is usually the external IP address of the VPN |
| 55 | // server. |
| 56 | std::string trusted_ip; |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 57 | int32 mtu; |
Darin Petkov | 6059674 | 2012-03-05 12:17:17 +0100 | [diff] [blame] | 58 | std::vector<Route> routes; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 61 | IPConfig(ControlInterface *control_interface, const std::string &device_name); |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 62 | IPConfig(ControlInterface *control_interface, |
| 63 | const std::string &device_name, |
| 64 | const std::string &type); |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 65 | virtual ~IPConfig(); |
| 66 | |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame] | 67 | const std::string &device_name() const { return device_name_; } |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 68 | const std::string &type() const { return type_; } |
| 69 | uint serial() const { return serial_; } |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 70 | |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 71 | std::string GetRpcIdentifier(); |
| 72 | |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 73 | // Registers a callback that's executed every time the configuration |
| 74 | // properties change. Takes ownership of |callback|. Pass NULL to remove a |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 75 | // callback. The callback's first argument is a pointer to this IP |
| 76 | // configuration instance allowing clients to more easily manage multiple IP |
| 77 | // configurations. The callback's second argument is set to false if IP |
| 78 | // configuration failed. |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 79 | void RegisterUpdateCallback( |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 80 | const base::Callback<void(const IPConfigRefPtr&, bool)> &callback); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 81 | |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 82 | void set_properties(const Properties &props) { properties_ = props; } |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 83 | const Properties &properties() const { return properties_; } |
| 84 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 85 | // Request, renew and release IP configuration. Return true on success, false |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 86 | // otherwise. The default implementation always returns false indicating a |
Paul Stewart | a02ee49 | 2012-05-16 10:04:53 -0700 | [diff] [blame] | 87 | // failure. ReleaseIP is advisory: if we are no longer connected, it is not |
| 88 | // possible to properly vacate the lease on the remote server. Also, |
| 89 | // depending on the configuration of the specific IPConfig subclass, we may |
| 90 | // end up holding on to the lease so we can resume to the network lease |
| 91 | // faster. |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 92 | virtual bool RequestIP(); |
| 93 | virtual bool RenewIP(); |
| 94 | virtual bool ReleaseIP(); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 95 | |
Paul Stewart | 4558bda | 2012-08-03 10:44:10 -0700 | [diff] [blame] | 96 | // Refresh IP configuration. Called by the DBus Adaptor "Refresh" call. |
| 97 | void Refresh(Error *error); |
| 98 | |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 99 | PropertyStore *mutable_store() { return &store_; } |
| 100 | const PropertyStore &store() const { return store_; } |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 101 | void ApplyStaticIPParameters(StaticIPParameters *static_ip_parameters); |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 102 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 103 | // |id_suffix| is used to generate a storage ID that binds this instance |
| 104 | // to its associated device. |
| 105 | virtual bool Load(StoreInterface *storage, const std::string &id_suffix); |
| 106 | virtual bool Save(StoreInterface *storage, const std::string &id_suffix); |
Chris Masone | 8a7b8be | 2011-07-22 12:43:37 -0700 | [diff] [blame] | 107 | |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 108 | protected: |
| 109 | // Updates the IP configuration properties and notifies registered listeners |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 110 | // about the event. |success| is set to false if the IP configuration failed. |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 111 | virtual void UpdateProperties(const Properties &properties, bool success); |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 112 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 113 | // |id_suffix| is appended to the storage id, intended to bind this instance |
| 114 | // to its associated device. |
| 115 | std::string GetStorageIdentifier(const std::string &id_suffix); |
| 116 | |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 117 | private: |
Chris Masone | c6c6c13 | 2011-06-30 11:29:52 -0700 | [diff] [blame] | 118 | friend class IPConfigAdaptorInterface; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 119 | friend class ConnectionTest; |
Chris Masone | c6c6c13 | 2011-06-30 11:29:52 -0700 | [diff] [blame] | 120 | |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 121 | FRIEND_TEST(DeviceTest, AcquireIPConfig); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 122 | FRIEND_TEST(DeviceTest, DestroyIPConfig); |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 123 | FRIEND_TEST(IPConfigTest, UpdateCallback); |
| 124 | FRIEND_TEST(IPConfigTest, UpdateProperties); |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 125 | FRIEND_TEST(ResolverTest, Empty); |
| 126 | FRIEND_TEST(ResolverTest, NonEmpty); |
Paul Stewart | bf66761 | 2012-06-29 14:49:54 -0700 | [diff] [blame] | 127 | FRIEND_TEST(ResolverTest, ShortTimeout); |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 128 | FRIEND_TEST(RoutingTableTest, ConfigureRoutes); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 129 | FRIEND_TEST(RoutingTableTest, RouteAddDelete); |
Thieu Le | caef893 | 2012-02-28 16:06:59 -0800 | [diff] [blame] | 130 | FRIEND_TEST(RoutingTableTest, RouteDeleteForeign); |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 131 | |
Chris Masone | 8a7b8be | 2011-07-22 12:43:37 -0700 | [diff] [blame] | 132 | static const char kStorageType[]; |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 133 | static const char kType[]; |
| 134 | static uint global_serial_; |
| 135 | |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 136 | PropertyStore store_; |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame] | 137 | const std::string device_name_; |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 138 | const std::string type_; |
| 139 | const uint serial_; |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 140 | scoped_ptr<IPConfigAdaptorInterface> adaptor_; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 141 | Properties properties_; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 142 | base::Callback<void(const IPConfigRefPtr&, bool)> update_callback_; |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 143 | |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 144 | void Init(); |
| 145 | |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 146 | DISALLOW_COPY_AND_ASSIGN(IPConfig); |
| 147 | }; |
| 148 | |
| 149 | } // namespace shill |
| 150 | |
| 151 | #endif // SHILL_IPCONFIG_ |