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> |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 12 | #include <base/logging.h> |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 13 | #include <base/memory/ref_counted.h> |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 14 | #include <base/memory/scoped_ptr.h> |
| 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; |
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 |
| 87 | // failure. |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 88 | virtual bool RequestIP(); |
| 89 | virtual bool RenewIP(); |
| 90 | virtual bool ReleaseIP(); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 91 | |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 92 | PropertyStore *mutable_store() { return &store_; } |
| 93 | const PropertyStore &store() const { return store_; } |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 94 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 95 | // |id_suffix| is used to generate a storage ID that binds this instance |
| 96 | // to its associated device. |
| 97 | virtual bool Load(StoreInterface *storage, const std::string &id_suffix); |
| 98 | virtual bool Save(StoreInterface *storage, const std::string &id_suffix); |
Chris Masone | 8a7b8be | 2011-07-22 12:43:37 -0700 | [diff] [blame] | 99 | |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 100 | protected: |
| 101 | // Updates the IP configuration properties and notifies registered listeners |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 102 | // about the event. |success| is set to false if the IP configuration failed. |
| 103 | void UpdateProperties(const Properties &properties, bool success); |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 104 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 105 | // |id_suffix| is appended to the storage id, intended to bind this instance |
| 106 | // to its associated device. |
| 107 | std::string GetStorageIdentifier(const std::string &id_suffix); |
| 108 | |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 109 | private: |
Chris Masone | c6c6c13 | 2011-06-30 11:29:52 -0700 | [diff] [blame] | 110 | friend class IPConfigAdaptorInterface; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 111 | friend class ConnectionTest; |
Chris Masone | c6c6c13 | 2011-06-30 11:29:52 -0700 | [diff] [blame] | 112 | |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 113 | FRIEND_TEST(DeviceTest, AcquireIPConfig); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 114 | FRIEND_TEST(DeviceTest, DestroyIPConfig); |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 115 | FRIEND_TEST(IPConfigTest, UpdateCallback); |
| 116 | FRIEND_TEST(IPConfigTest, UpdateProperties); |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 117 | FRIEND_TEST(ResolverTest, Empty); |
| 118 | FRIEND_TEST(ResolverTest, NonEmpty); |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 119 | FRIEND_TEST(RoutingTableTest, ConfigureRoutes); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 120 | FRIEND_TEST(RoutingTableTest, RouteAddDelete); |
Thieu Le | caef893 | 2012-02-28 16:06:59 -0800 | [diff] [blame] | 121 | FRIEND_TEST(RoutingTableTest, RouteDeleteForeign); |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 122 | |
Chris Masone | 8a7b8be | 2011-07-22 12:43:37 -0700 | [diff] [blame] | 123 | static const char kStorageType[]; |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 124 | static const char kType[]; |
| 125 | static uint global_serial_; |
| 126 | |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 127 | PropertyStore store_; |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame] | 128 | const std::string device_name_; |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 129 | const std::string type_; |
| 130 | const uint serial_; |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 131 | scoped_ptr<IPConfigAdaptorInterface> adaptor_; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 132 | Properties properties_; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 133 | base::Callback<void(const IPConfigRefPtr&, bool)> update_callback_; |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 134 | |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 135 | void Init(); |
| 136 | |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 137 | DISALLOW_COPY_AND_ASSIGN(IPConfig); |
| 138 | }; |
| 139 | |
| 140 | } // namespace shill |
| 141 | |
| 142 | #endif // SHILL_IPCONFIG_ |