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 | |
| 5 | #include "shill/static_ip_parameters.h" |
| 6 | |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 7 | #include <string.h> |
| 8 | |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 9 | #include <base/string_split.h> |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 10 | #include <base/string_util.h> |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 11 | #include <chromeos/dbus/service_constants.h> |
| 12 | |
| 13 | #include "shill/error.h" |
| 14 | #include "shill/ip_address.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 15 | #include "shill/logging.h" |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 16 | #include "shill/property_accessor.h" |
| 17 | #include "shill/property_store.h" |
| 18 | #include "shill/store_interface.h" |
| 19 | |
| 20 | using std::string; |
| 21 | using std::vector; |
| 22 | |
| 23 | namespace shill { |
| 24 | |
| 25 | // static |
| 26 | const char StaticIPParameters::kConfigKeyPrefix[] = "StaticIP."; |
| 27 | // static |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 28 | const char StaticIPParameters::kSavedConfigKeyPrefix[] = "SavedIP."; |
| 29 | // static |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 30 | const StaticIPParameters::Property StaticIPParameters::kProperties[] = { |
Ben Chan | 0295e0f | 2013-09-20 13:47:29 -0700 | [diff] [blame] | 31 | { kAddressProperty, Property::kTypeString }, |
| 32 | { kGatewayProperty, Property::kTypeString }, |
| 33 | { kMtuProperty, Property::kTypeInt32 }, |
| 34 | { kNameServersProperty, Property::kTypeStrings }, |
| 35 | { kPeerAddressProperty, Property::kTypeString }, |
| 36 | { kPrefixlenProperty, Property::kTypeInt32 } |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | StaticIPParameters::StaticIPParameters() {} |
| 40 | |
| 41 | StaticIPParameters::~StaticIPParameters() {} |
| 42 | |
| 43 | void StaticIPParameters::PlumbPropertyStore(PropertyStore *store) { |
| 44 | for (size_t i = 0; i < arraysize(kProperties); ++i) { |
| 45 | const Property &property = kProperties[i]; |
| 46 | const string name(string(kConfigKeyPrefix) + property.name); |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 47 | const string saved_name(string(kSavedConfigKeyPrefix) + property.name); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 48 | switch (property.type) { |
| 49 | case Property::kTypeInt32: |
| 50 | store->RegisterDerivedInt32( |
| 51 | name, |
| 52 | Int32Accessor( |
| 53 | new CustomMappedAccessor<StaticIPParameters, int32, size_t>( |
| 54 | this, |
| 55 | &StaticIPParameters::ClearMappedProperty, |
| 56 | &StaticIPParameters::GetMappedInt32Property, |
| 57 | &StaticIPParameters::SetMappedInt32Property, |
| 58 | i))); |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 59 | store->RegisterDerivedInt32( |
| 60 | saved_name, |
| 61 | Int32Accessor( |
| 62 | new CustomMappedAccessor<StaticIPParameters, int32, size_t>( |
| 63 | this, |
| 64 | &StaticIPParameters::ClearMappedSavedProperty, |
| 65 | &StaticIPParameters::GetMappedSavedInt32Property, |
| 66 | &StaticIPParameters::SetMappedSavedInt32Property, |
| 67 | i))); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 68 | break; |
| 69 | case Property::kTypeString: |
| 70 | case Property::kTypeStrings: |
| 71 | store->RegisterDerivedString( |
| 72 | name, |
| 73 | StringAccessor( |
| 74 | new CustomMappedAccessor<StaticIPParameters, string, size_t>( |
| 75 | this, |
| 76 | &StaticIPParameters::ClearMappedProperty, |
| 77 | &StaticIPParameters::GetMappedStringProperty, |
| 78 | &StaticIPParameters::SetMappedStringProperty, |
| 79 | i))); |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 80 | store->RegisterDerivedString( |
| 81 | saved_name, |
| 82 | StringAccessor( |
| 83 | new CustomMappedAccessor<StaticIPParameters, string, size_t>( |
| 84 | this, |
| 85 | &StaticIPParameters::ClearMappedSavedProperty, |
| 86 | &StaticIPParameters::GetMappedSavedStringProperty, |
| 87 | &StaticIPParameters::SetMappedSavedStringProperty, |
| 88 | i))); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 89 | break; |
| 90 | default: |
| 91 | NOTIMPLEMENTED(); |
| 92 | break; |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | void StaticIPParameters::Load( |
| 98 | StoreInterface *storage, const string &storage_id) { |
| 99 | for (size_t i = 0; i < arraysize(kProperties); ++i) { |
| 100 | const Property &property = kProperties[i]; |
| 101 | const string name(string(kConfigKeyPrefix) + property.name); |
| 102 | switch (property.type) { |
| 103 | case Property::kTypeInt32: |
| 104 | { |
| 105 | int32 value; |
| 106 | if (storage->GetInt(storage_id, name, &value)) { |
| 107 | args_.SetInt(property.name, value); |
| 108 | } else { |
| 109 | args_.RemoveInt(property.name); |
| 110 | } |
| 111 | } |
| 112 | break; |
| 113 | case Property::kTypeString: |
| 114 | case Property::kTypeStrings: |
| 115 | { |
| 116 | string value; |
| 117 | if (storage->GetString(storage_id, name, &value)) { |
| 118 | args_.SetString(property.name, value); |
| 119 | } else { |
| 120 | args_.RemoveString(property.name); |
| 121 | } |
| 122 | } |
| 123 | break; |
| 124 | default: |
| 125 | NOTIMPLEMENTED(); |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | void StaticIPParameters::Save( |
| 132 | StoreInterface *storage, const string &storage_id) { |
| 133 | for (size_t i = 0; i < arraysize(kProperties); ++i) { |
| 134 | const Property &property = kProperties[i]; |
| 135 | const string name(string(kConfigKeyPrefix) + property.name); |
| 136 | bool property_exists = false; |
| 137 | switch (property.type) { |
| 138 | case Property::kTypeInt32: |
| 139 | if (args_.ContainsInt(property.name)) { |
| 140 | property_exists = true; |
| 141 | storage->SetInt(storage_id, name, args_.GetInt(property.name)); |
| 142 | } |
| 143 | break; |
| 144 | case Property::kTypeString: |
| 145 | case Property::kTypeStrings: |
| 146 | if (args_.ContainsString(property.name)) { |
| 147 | property_exists = true; |
| 148 | storage->SetString(storage_id, name, args_.GetString(property.name)); |
| 149 | } |
| 150 | break; |
| 151 | default: |
| 152 | NOTIMPLEMENTED(); |
| 153 | break; |
| 154 | } |
| 155 | if (!property_exists) { |
| 156 | storage->DeleteKey(storage_id, name); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | void StaticIPParameters::ApplyInt( |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 162 | const string &property, int32 *value_out) { |
| 163 | saved_args_.SetInt(property, *value_out); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 164 | if (args_.ContainsInt(property)) { |
| 165 | *value_out = args_.GetInt(property); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | void StaticIPParameters::ApplyString( |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 170 | const string &property, string *value_out) { |
| 171 | saved_args_.SetString(property, *value_out); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 172 | if (args_.ContainsString(property)) { |
| 173 | *value_out = args_.GetString(property); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | void StaticIPParameters::ApplyStrings( |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 178 | const string &property, vector<string> *value_out) { |
| 179 | saved_args_.SetString(property, JoinString(*value_out, ',')); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 180 | if (args_.ContainsString(property)) { |
| 181 | vector<string> values; |
| 182 | string value(args_.GetString(property)); |
| 183 | if (!value.empty()) { |
| 184 | base::SplitString(value, ',', &values); |
| 185 | } |
| 186 | *value_out = values; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 191 | void StaticIPParameters::ApplyTo(IPConfig::Properties *props) { |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 192 | if (props->address_family == IPAddress::kFamilyUnknown) { |
| 193 | // In situations where no address is supplied (bad or missing DHCP config) |
| 194 | // supply an address family ourselves. |
| 195 | // TODO(pstew): Guess from the address values. |
| 196 | props->address_family = IPAddress::kFamilyIPv4; |
| 197 | } |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 198 | ClearSavedParameters(); |
Ben Chan | 0295e0f | 2013-09-20 13:47:29 -0700 | [diff] [blame] | 199 | ApplyString(kAddressProperty, &props->address); |
| 200 | ApplyString(kGatewayProperty, &props->gateway); |
| 201 | ApplyInt(kMtuProperty, &props->mtu); |
| 202 | ApplyStrings(kNameServersProperty, &props->dns_servers); |
| 203 | ApplyString(kPeerAddressProperty, &props->peer_address); |
| 204 | ApplyInt(kPrefixlenProperty, &props->subnet_prefix); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 207 | void StaticIPParameters::ClearSavedParameters() { |
| 208 | saved_args_.Clear(); |
| 209 | } |
| 210 | |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 211 | bool StaticIPParameters::ContainsAddress() const { |
Ben Chan | 0295e0f | 2013-09-20 13:47:29 -0700 | [diff] [blame] | 212 | return args_.ContainsString(kAddressProperty) && |
| 213 | args_.ContainsInt(kPrefixlenProperty); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | void StaticIPParameters::ClearMappedProperty( |
| 217 | const size_t &index, Error *error) { |
| 218 | CHECK(index < arraysize(kProperties)); |
| 219 | |
| 220 | const Property &property = kProperties[index]; |
| 221 | switch (property.type) { |
| 222 | case Property::kTypeInt32: |
| 223 | if (args_.ContainsInt(property.name)) { |
| 224 | args_.RemoveInt(property.name); |
| 225 | } else { |
| 226 | error->Populate(Error::kNotFound, "Property is not set"); |
| 227 | } |
| 228 | break; |
| 229 | case Property::kTypeString: |
| 230 | case Property::kTypeStrings: |
| 231 | if (args_.ContainsString(property.name)) { |
| 232 | args_.RemoveString(property.name); |
| 233 | } else { |
| 234 | error->Populate(Error::kNotFound, "Property is not set"); |
| 235 | } |
| 236 | break; |
| 237 | default: |
| 238 | NOTIMPLEMENTED(); |
| 239 | break; |
| 240 | } |
| 241 | } |
| 242 | |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 243 | void StaticIPParameters::ClearMappedSavedProperty( |
| 244 | const size_t &index, Error *error) { |
| 245 | error->Populate(Error::kInvalidArguments, "Property is read-only"); |
| 246 | } |
| 247 | |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 248 | int32 StaticIPParameters::GetMappedInt32Property( |
| 249 | const size_t &index, Error *error) { |
| 250 | CHECK(index < arraysize(kProperties)); |
| 251 | |
| 252 | const string &key = kProperties[index].name; |
| 253 | if (!args_.ContainsInt(key)) { |
| 254 | error->Populate(Error::kNotFound, "Property is not set"); |
| 255 | return 0; |
| 256 | } |
| 257 | return args_.GetInt(key); |
| 258 | } |
| 259 | |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 260 | int32 StaticIPParameters::GetMappedSavedInt32Property( |
| 261 | const size_t &index, Error *error) { |
| 262 | CHECK(index < arraysize(kProperties)); |
| 263 | |
| 264 | const string &key = kProperties[index].name; |
| 265 | if (!saved_args_.ContainsInt(key)) { |
| 266 | error->Populate(Error::kNotFound, "Property is not set"); |
| 267 | return 0; |
| 268 | } |
| 269 | return saved_args_.GetInt(key); |
| 270 | } |
| 271 | |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 272 | string StaticIPParameters::GetMappedStringProperty( |
| 273 | const size_t &index, Error *error) { |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 274 | CHECK(index < arraysize(kProperties)); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 275 | |
| 276 | const string &key = kProperties[index].name; |
| 277 | if (!args_.ContainsString(key)) { |
| 278 | error->Populate(Error::kNotFound, "Property is not set"); |
| 279 | return string(); |
| 280 | } |
| 281 | return args_.GetString(key); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 284 | string StaticIPParameters::GetMappedSavedStringProperty( |
| 285 | const size_t &index, Error *error) { |
| 286 | CHECK(index < arraysize(kProperties)); |
| 287 | |
| 288 | const string &key = kProperties[index].name; |
| 289 | if (!saved_args_.ContainsString(key)) { |
| 290 | error->Populate(Error::kNotFound, "Property is not set"); |
| 291 | return string(); |
| 292 | } |
| 293 | return saved_args_.GetString(key); |
| 294 | } |
| 295 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 296 | bool StaticIPParameters::SetMappedInt32Property( |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 297 | const size_t &index, const int32 &value, Error *error) { |
| 298 | CHECK(index < arraysize(kProperties)); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 299 | if (args_.ContainsInt(kProperties[index].name) && |
| 300 | args_.GetInt(kProperties[index].name) == value) { |
| 301 | return false; |
| 302 | } |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 303 | args_.SetInt(kProperties[index].name, value); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 304 | return true; |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 305 | } |
| 306 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 307 | bool StaticIPParameters::SetMappedSavedInt32Property( |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 308 | const size_t &index, const int32 &value, Error *error) { |
| 309 | error->Populate(Error::kInvalidArguments, "Property is read-only"); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 310 | return false; |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 311 | } |
| 312 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 313 | bool StaticIPParameters::SetMappedStringProperty( |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 314 | const size_t &index, const string &value, Error *error) { |
| 315 | CHECK(index < arraysize(kProperties)); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 316 | if (args_.ContainsString(kProperties[index].name) && |
| 317 | args_.GetString(kProperties[index].name) == value) { |
| 318 | return false; |
| 319 | } |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 320 | args_.SetString(kProperties[index].name, value); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 321 | return true; |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 322 | } |
| 323 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 324 | bool StaticIPParameters::SetMappedSavedStringProperty( |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 325 | const size_t &index, const string &value, Error *error) { |
| 326 | error->Populate(Error::kInvalidArguments, "Property is read-only"); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 327 | return false; |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 330 | } // namespace shill |