Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | |
Ben Chan | c45688b | 2014-07-02 23:50:45 -0700 | [diff] [blame] | 5 | #ifndef SHILL_STATIC_IP_PARAMETERS_H_ |
| 6 | #define SHILL_STATIC_IP_PARAMETERS_H_ |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 7 | |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include <base/memory/ref_counted.h> |
Paul Stewart | 8223653 | 2013-12-10 15:33:11 -0800 | [diff] [blame] | 12 | #include <base/logging.h> |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 13 | |
| 14 | #include "shill/ipconfig.h" |
| 15 | #include "shill/key_value_store.h" |
| 16 | #include "shill/property_store.h" |
| 17 | |
| 18 | namespace shill { |
| 19 | class StoreInterface; |
| 20 | |
| 21 | // Holder for static IP parameters. Includes methods for reading and |
| 22 | // displaying values over a control API, methods for loading and |
| 23 | // storing this to a persistent store, as well as applying these |
| 24 | // parameters to an IPConfig object. |
| 25 | class StaticIPParameters { |
| 26 | public: |
| 27 | StaticIPParameters(); |
| 28 | virtual ~StaticIPParameters(); |
| 29 | |
| 30 | // Take a property store and add static IP parameters to them. |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 31 | void PlumbPropertyStore(PropertyStore* store); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 32 | |
| 33 | // Load static IP parameters from a persistent store with id |storage_id|. |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 34 | void Load(StoreInterface* storage, const std::string& storage_id); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 35 | |
| 36 | // Save static IP parameters to a persistent store with id |storage_id|. |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 37 | void Save(StoreInterface* storage, const std::string& storage_id); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 38 | |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 39 | // Apply static IP parameters to an IPConfig properties object, and save |
| 40 | // their original values. |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 41 | void ApplyTo(IPConfig::Properties* props); |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 42 | |
Paul Stewart | 8223653 | 2013-12-10 15:33:11 -0800 | [diff] [blame] | 43 | // Restore IP parameters from |saved_args_| to |props|, then clear |
| 44 | // |saved_args_|. |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 45 | void RestoreTo(IPConfig::Properties* props); |
Paul Stewart | 8223653 | 2013-12-10 15:33:11 -0800 | [diff] [blame] | 46 | |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 47 | // Remove any saved parameters from a previous call to ApplyTo(). |
| 48 | void ClearSavedParameters(); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 49 | |
| 50 | // Return whether configuration parameters contain an address property. |
| 51 | bool ContainsAddress() const; |
| 52 | |
Garret Kelly | d01b5cc | 2015-03-12 16:20:55 -0400 | [diff] [blame] | 53 | // Return whether configuration parameters contain a namerservers property. |
| 54 | bool ContainsNameServers() const; |
| 55 | |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 56 | private: |
| 57 | friend class StaticIPParametersTest; |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 58 | FRIEND_TEST(DeviceTest, IPConfigUpdatedFailureWithStatic); |
Garret Kelly | 84a90ce | 2015-03-25 14:54:19 -0400 | [diff] [blame] | 59 | FRIEND_TEST(DeviceTest, PrependWithStaticConfiguration); |
Paul Stewart | 8223653 | 2013-12-10 15:33:11 -0800 | [diff] [blame] | 60 | FRIEND_TEST(StaticIpParametersTest, SavedParameters); |
Peter Qiu | 08bf508 | 2014-09-08 15:57:12 -0700 | [diff] [blame] | 61 | FRIEND_TEST(StaticIpParametersTest, SavedParametersDict); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 62 | |
| 63 | struct Property { |
| 64 | enum Type { |
| 65 | kTypeInt32, |
| 66 | kTypeString, |
| 67 | // Properties of type "Strings" are stored as a comma-separated list |
| 68 | // in the control interface and in the profile, but are stored as a |
| 69 | // vector of strings in the IPConfig properties. |
| 70 | kTypeStrings |
| 71 | }; |
| 72 | |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 73 | const char* name; |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 74 | Type type; |
| 75 | }; |
| 76 | |
| 77 | static const char kConfigKeyPrefix[]; |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 78 | static const char kSavedConfigKeyPrefix[]; |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 79 | static const Property kProperties[]; |
| 80 | |
| 81 | // These functions try to retrieve the argument |property| out of the |
| 82 | // KeyValueStore in |args_|. If that value exists, overwrite |value_out| |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 83 | // with its contents, and save the previous value into |saved_args_|. |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 84 | void ApplyInt(const std::string& property, int32_t* value_out); |
| 85 | void ApplyString(const std::string& property, std::string* value_out); |
| 86 | void ApplyStrings(const std::string& property, |
| 87 | std::vector<std::string>* value_out); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 88 | |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 89 | void ClearMappedProperty(const size_t& index, Error* error); |
| 90 | void ClearMappedSavedProperty(const size_t& index, Error* error); |
| 91 | int32_t GetMappedInt32Property(const size_t& index, Error* error); |
| 92 | int32_t GetMappedSavedInt32Property(const size_t& index, Error* error); |
| 93 | std::string GetMappedStringProperty(const size_t& index, Error* error); |
| 94 | std::string GetMappedSavedStringProperty(const size_t& index, Error* error); |
| 95 | std::string GetMappedStringsProperty(const size_t& index, Error* error); |
| 96 | std::string GetMappedSavedStringsProperty(const size_t& index, Error* error); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 97 | bool SetMappedInt32Property( |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 98 | const size_t& index, const int32_t& value, Error* error); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 99 | bool SetMappedSavedInt32Property( |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 100 | const size_t& index, const int32_t& value, Error* error); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 101 | bool SetMappedStringProperty( |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 102 | const size_t& index, const std::string& value, Error* error); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 103 | bool SetMappedSavedStringProperty( |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 104 | const size_t& index, const std::string& value, Error* error); |
Peter Qiu | 08bf508 | 2014-09-08 15:57:12 -0700 | [diff] [blame] | 105 | bool SetMappedStringsProperty( |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 106 | const size_t& index, const std::string& value, Error* error); |
Peter Qiu | 08bf508 | 2014-09-08 15:57:12 -0700 | [diff] [blame] | 107 | bool SetMappedSavedStringsProperty( |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 108 | const size_t& index, const std::string& value, Error* error); |
Peter Qiu | 08bf508 | 2014-09-08 15:57:12 -0700 | [diff] [blame] | 109 | |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 110 | KeyValueStore GetSavedIPConfig(Error* error); |
| 111 | KeyValueStore GetStaticIPConfig(Error* error); |
| 112 | bool SetStaticIPConfig(const KeyValueStore& value, Error* error); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 113 | |
| 114 | KeyValueStore args_; |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 115 | KeyValueStore saved_args_; |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 116 | |
| 117 | DISALLOW_COPY_AND_ASSIGN(StaticIPParameters); |
| 118 | }; |
| 119 | |
| 120 | } // namespace shill |
| 121 | |
Ben Chan | c45688b | 2014-07-02 23:50:45 -0700 | [diff] [blame] | 122 | #endif // SHILL_STATIC_IP_PARAMETERS_H_ |