blob: c114cb8566f8ef835c2929f5447cec8459af4f2a [file] [log] [blame]
Paul Stewart1062d9d2012-04-27 10:42:27 -07001// 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 Chanc45688b2014-07-02 23:50:45 -07005#ifndef SHILL_STATIC_IP_PARAMETERS_H_
6#define SHILL_STATIC_IP_PARAMETERS_H_
Paul Stewart1062d9d2012-04-27 10:42:27 -07007
8#include <string>
9#include <vector>
10
11#include <base/memory/ref_counted.h>
Paul Stewart82236532013-12-10 15:33:11 -080012#include <base/logging.h>
Paul Stewart1062d9d2012-04-27 10:42:27 -070013
14#include "shill/ipconfig.h"
15#include "shill/key_value_store.h"
16#include "shill/property_store.h"
17
18namespace shill {
19class 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.
25class StaticIPParameters {
26 public:
27 StaticIPParameters();
28 virtual ~StaticIPParameters();
29
30 // Take a property store and add static IP parameters to them.
Paul Stewart1a212a62015-06-16 13:13:10 -070031 void PlumbPropertyStore(PropertyStore* store);
Paul Stewart1062d9d2012-04-27 10:42:27 -070032
33 // Load static IP parameters from a persistent store with id |storage_id|.
Paul Stewart1a212a62015-06-16 13:13:10 -070034 void Load(StoreInterface* storage, const std::string& storage_id);
Paul Stewart1062d9d2012-04-27 10:42:27 -070035
36 // Save static IP parameters to a persistent store with id |storage_id|.
Paul Stewart1a212a62015-06-16 13:13:10 -070037 void Save(StoreInterface* storage, const std::string& storage_id);
Paul Stewart1062d9d2012-04-27 10:42:27 -070038
Paul Stewartdef189e2012-08-02 20:12:09 -070039 // Apply static IP parameters to an IPConfig properties object, and save
40 // their original values.
Paul Stewart1a212a62015-06-16 13:13:10 -070041 void ApplyTo(IPConfig::Properties* props);
Paul Stewartdef189e2012-08-02 20:12:09 -070042
Paul Stewart82236532013-12-10 15:33:11 -080043 // Restore IP parameters from |saved_args_| to |props|, then clear
44 // |saved_args_|.
Paul Stewart1a212a62015-06-16 13:13:10 -070045 void RestoreTo(IPConfig::Properties* props);
Paul Stewart82236532013-12-10 15:33:11 -080046
Paul Stewartdef189e2012-08-02 20:12:09 -070047 // Remove any saved parameters from a previous call to ApplyTo().
48 void ClearSavedParameters();
Paul Stewart1062d9d2012-04-27 10:42:27 -070049
50 // Return whether configuration parameters contain an address property.
51 bool ContainsAddress() const;
52
Garret Kellyd01b5cc2015-03-12 16:20:55 -040053 // Return whether configuration parameters contain a namerservers property.
54 bool ContainsNameServers() const;
55
Paul Stewart1062d9d2012-04-27 10:42:27 -070056 private:
57 friend class StaticIPParametersTest;
mukesh agrawalcc0fded2012-05-09 13:40:58 -070058 FRIEND_TEST(DeviceTest, IPConfigUpdatedFailureWithStatic);
Garret Kelly84a90ce2015-03-25 14:54:19 -040059 FRIEND_TEST(DeviceTest, PrependWithStaticConfiguration);
Paul Stewart82236532013-12-10 15:33:11 -080060 FRIEND_TEST(StaticIpParametersTest, SavedParameters);
Peter Qiu08bf5082014-09-08 15:57:12 -070061 FRIEND_TEST(StaticIpParametersTest, SavedParametersDict);
Paul Stewart1062d9d2012-04-27 10:42:27 -070062
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 Stewart1a212a62015-06-16 13:13:10 -070073 const char* name;
Paul Stewart1062d9d2012-04-27 10:42:27 -070074 Type type;
75 };
76
77 static const char kConfigKeyPrefix[];
Paul Stewartdef189e2012-08-02 20:12:09 -070078 static const char kSavedConfigKeyPrefix[];
Paul Stewart1062d9d2012-04-27 10:42:27 -070079 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 Stewartdef189e2012-08-02 20:12:09 -070083 // with its contents, and save the previous value into |saved_args_|.
Paul Stewart1a212a62015-06-16 13:13:10 -070084 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 Stewart1062d9d2012-04-27 10:42:27 -070088
Paul Stewart1a212a62015-06-16 13:13:10 -070089 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 agrawalbebf1b82013-04-23 15:06:33 -070097 bool SetMappedInt32Property(
Paul Stewart1a212a62015-06-16 13:13:10 -070098 const size_t& index, const int32_t& value, Error* error);
mukesh agrawalbebf1b82013-04-23 15:06:33 -070099 bool SetMappedSavedInt32Property(
Paul Stewart1a212a62015-06-16 13:13:10 -0700100 const size_t& index, const int32_t& value, Error* error);
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700101 bool SetMappedStringProperty(
Paul Stewart1a212a62015-06-16 13:13:10 -0700102 const size_t& index, const std::string& value, Error* error);
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700103 bool SetMappedSavedStringProperty(
Paul Stewart1a212a62015-06-16 13:13:10 -0700104 const size_t& index, const std::string& value, Error* error);
Peter Qiu08bf5082014-09-08 15:57:12 -0700105 bool SetMappedStringsProperty(
Paul Stewart1a212a62015-06-16 13:13:10 -0700106 const size_t& index, const std::string& value, Error* error);
Peter Qiu08bf5082014-09-08 15:57:12 -0700107 bool SetMappedSavedStringsProperty(
Paul Stewart1a212a62015-06-16 13:13:10 -0700108 const size_t& index, const std::string& value, Error* error);
Peter Qiu08bf5082014-09-08 15:57:12 -0700109
Paul Stewart1a212a62015-06-16 13:13:10 -0700110 KeyValueStore GetSavedIPConfig(Error* error);
111 KeyValueStore GetStaticIPConfig(Error* error);
112 bool SetStaticIPConfig(const KeyValueStore& value, Error* error);
Paul Stewart1062d9d2012-04-27 10:42:27 -0700113
114 KeyValueStore args_;
Paul Stewartdef189e2012-08-02 20:12:09 -0700115 KeyValueStore saved_args_;
Paul Stewart1062d9d2012-04-27 10:42:27 -0700116
117 DISALLOW_COPY_AND_ASSIGN(StaticIPParameters);
118};
119
120} // namespace shill
121
Ben Chanc45688b2014-07-02 23:50:45 -0700122#endif // SHILL_STATIC_IP_PARAMETERS_H_