Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 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 | |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 11 | #include <base/callback_old.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" |
| 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; |
Chris Masone | 8a7b8be | 2011-07-22 12:43:37 -0700 | [diff] [blame] | 25 | class StoreInterface; |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 26 | |
| 27 | // IPConfig superclass. Individual IP configuration types will inherit from this |
| 28 | // class. |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 29 | class IPConfig : public base::RefCounted<IPConfig> { |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 30 | public: |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 31 | struct Properties { |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 32 | Properties() : address_family(IPAddress::kFamilyUnknown), |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 33 | subnet_cidr(0), |
| 34 | mtu(0) {} |
| 35 | |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 36 | IPAddress::Family address_family; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 37 | std::string address; |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 38 | int32 subnet_cidr; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 39 | std::string broadcast_address; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 40 | std::vector<std::string> dns_servers; |
| 41 | std::string domain_name; |
| 42 | std::vector<std::string> domain_search; |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 43 | std::string gateway; |
| 44 | std::string method; |
| 45 | std::string peer_address; |
| 46 | int32 mtu; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 47 | }; |
| 48 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 49 | IPConfig(ControlInterface *control_interface, const std::string &device_name); |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 50 | IPConfig(ControlInterface *control_interface, |
| 51 | const std::string &device_name, |
| 52 | const std::string &type); |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 53 | virtual ~IPConfig(); |
| 54 | |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame] | 55 | const std::string &device_name() const { return device_name_; } |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 56 | const std::string &type() const { return type_; } |
| 57 | uint serial() const { return serial_; } |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 58 | |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 59 | std::string GetRpcIdentifier(); |
| 60 | |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 61 | // Registers a callback that's executed every time the configuration |
| 62 | // properties change. Takes ownership of |callback|. Pass NULL to remove a |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 63 | // callback. The callback's first argument is a pointer to this IP |
| 64 | // configuration instance allowing clients to more easily manage multiple IP |
| 65 | // configurations. The callback's second argument is set to false if IP |
| 66 | // configuration failed. |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 67 | void RegisterUpdateCallback( |
| 68 | Callback2<const IPConfigRefPtr&, bool>::Type *callback); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 69 | |
| 70 | const Properties &properties() const { return properties_; } |
| 71 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 72 | // Request, renew and release IP configuration. Return true on success, false |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 73 | // otherwise. The default implementation always returns false indicating a |
| 74 | // failure. |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 75 | virtual bool RequestIP(); |
| 76 | virtual bool RenewIP(); |
| 77 | virtual bool ReleaseIP(); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 78 | |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 79 | PropertyStore *mutable_store() { return &store_; } |
| 80 | const PropertyStore &store() const { return store_; } |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 81 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 82 | // |id_suffix| is used to generate a storage ID that binds this instance |
| 83 | // to its associated device. |
| 84 | virtual bool Load(StoreInterface *storage, const std::string &id_suffix); |
| 85 | virtual bool Save(StoreInterface *storage, const std::string &id_suffix); |
Chris Masone | 8a7b8be | 2011-07-22 12:43:37 -0700 | [diff] [blame] | 86 | |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 87 | protected: |
| 88 | // Updates the IP configuration properties and notifies registered listeners |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 89 | // about the event. |success| is set to false if the IP configuration failed. |
| 90 | void UpdateProperties(const Properties &properties, bool success); |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 91 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 92 | // |id_suffix| is appended to the storage id, intended to bind this instance |
| 93 | // to its associated device. |
| 94 | std::string GetStorageIdentifier(const std::string &id_suffix); |
| 95 | |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 96 | private: |
Chris Masone | c6c6c13 | 2011-06-30 11:29:52 -0700 | [diff] [blame] | 97 | friend class IPConfigAdaptorInterface; |
Paul Stewart | dd60e45 | 2011-08-08 11:38:36 -0700 | [diff] [blame] | 98 | friend class ConnectionTest; |
Chris Masone | c6c6c13 | 2011-06-30 11:29:52 -0700 | [diff] [blame] | 99 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 100 | FRIEND_TEST(DeviceTest, AcquireDHCPConfig); |
| 101 | FRIEND_TEST(DeviceTest, DestroyIPConfig); |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 102 | FRIEND_TEST(IPConfigTest, UpdateCallback); |
| 103 | FRIEND_TEST(IPConfigTest, UpdateProperties); |
Paul Stewart | b606394 | 2011-08-05 10:17:29 -0700 | [diff] [blame] | 104 | FRIEND_TEST(ResolverTest, Empty); |
| 105 | FRIEND_TEST(ResolverTest, NonEmpty); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 106 | FRIEND_TEST(RoutingTableTest, RouteAddDelete); |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 107 | |
Chris Masone | 8a7b8be | 2011-07-22 12:43:37 -0700 | [diff] [blame] | 108 | static const char kStorageType[]; |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 109 | static const char kType[]; |
| 110 | static uint global_serial_; |
| 111 | |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 112 | PropertyStore store_; |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame] | 113 | const std::string device_name_; |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 114 | const std::string type_; |
| 115 | const uint serial_; |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 116 | scoped_ptr<IPConfigAdaptorInterface> adaptor_; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 117 | Properties properties_; |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 118 | scoped_ptr<Callback2<const IPConfigRefPtr&, bool>::Type> update_callback_; |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 119 | |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 120 | void Init(); |
| 121 | |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 122 | DISALLOW_COPY_AND_ASSIGN(IPConfig); |
| 123 | }; |
| 124 | |
| 125 | } // namespace shill |
| 126 | |
| 127 | #endif // SHILL_IPCONFIG_ |