Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [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 | #include "shill/ipconfig.h" |
| 6 | |
| 7 | #include <base/logging.h> |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 8 | #include <chromeos/dbus/service_constants.h> |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 9 | |
Chris Masone | c6c6c13 | 2011-06-30 11:29:52 -0700 | [diff] [blame] | 10 | #include "shill/adaptor_interfaces.h" |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 11 | #include "shill/control_interface.h" |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 12 | #include "shill/error.h" |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 13 | #include "shill/scope_logger.h" |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 14 | #include "shill/static_ip_parameters.h" |
Chris Masone | 8a7b8be | 2011-07-22 12:43:37 -0700 | [diff] [blame] | 15 | #include "shill/store_interface.h" |
Chris Masone | c6c6c13 | 2011-06-30 11:29:52 -0700 | [diff] [blame] | 16 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 17 | using base::Callback; |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 18 | using std::string; |
| 19 | |
| 20 | namespace shill { |
| 21 | |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 22 | // static |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 23 | const char IPConfig::kStorageType[] = "Method"; |
Chris Masone | 8a7b8be | 2011-07-22 12:43:37 -0700 | [diff] [blame] | 24 | // static |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 25 | const char IPConfig::kType[] = "ip"; |
| 26 | // static |
| 27 | uint IPConfig::global_serial_ = 0; |
| 28 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 29 | IPConfig::IPConfig(ControlInterface *control_interface, |
| 30 | const std::string &device_name) |
| 31 | : device_name_(device_name), |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 32 | type_(kType), |
| 33 | serial_(global_serial_++), |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 34 | adaptor_(control_interface->CreateIPConfigAdaptor(this)) { |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 35 | Init(); |
| 36 | } |
| 37 | |
| 38 | IPConfig::IPConfig(ControlInterface *control_interface, |
| 39 | const std::string &device_name, |
| 40 | const std::string &type) |
| 41 | : device_name_(device_name), |
| 42 | type_(type), |
| 43 | serial_(global_serial_++), |
| 44 | adaptor_(control_interface->CreateIPConfigAdaptor(this)) { |
| 45 | Init(); |
| 46 | } |
| 47 | |
| 48 | void IPConfig::Init() { |
Paul Stewart | 10241e3 | 2012-04-23 18:15:06 -0700 | [diff] [blame] | 49 | store_.RegisterConstString(flimflam::kAddressProperty, |
| 50 | &properties_.address); |
| 51 | store_.RegisterConstString(flimflam::kBroadcastProperty, |
| 52 | &properties_.broadcast_address); |
| 53 | store_.RegisterConstString(flimflam::kDomainNameProperty, |
| 54 | &properties_.domain_name); |
| 55 | store_.RegisterConstString(flimflam::kGatewayProperty, &properties_.gateway); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 56 | store_.RegisterConstString(flimflam::kMethodProperty, &properties_.method); |
Paul Stewart | 10241e3 | 2012-04-23 18:15:06 -0700 | [diff] [blame] | 57 | store_.RegisterConstInt32(flimflam::kMtuProperty, &properties_.mtu); |
| 58 | store_.RegisterConstStrings(flimflam::kNameServersProperty, |
| 59 | &properties_.dns_servers); |
| 60 | store_.RegisterConstString(flimflam::kPeerAddressProperty, |
| 61 | &properties_.peer_address); |
| 62 | store_.RegisterConstInt32(flimflam::kPrefixlenProperty, |
| 63 | &properties_.subnet_prefix); |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 64 | // TODO(cmasone): Does anyone use this? |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 65 | // store_.RegisterStrings(flimflam::kSearchDomainsProperty, |
| 66 | // &properties_.domain_search); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 67 | SLOG(Inet, 2) << __func__ << " device: " << device_name(); |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | IPConfig::~IPConfig() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 71 | SLOG(Inet, 2) << __func__ << " device: " << device_name(); |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 74 | string IPConfig::GetRpcIdentifier() { |
| 75 | return adaptor_->GetRpcIdentifier(); |
| 76 | } |
| 77 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 78 | string IPConfig::GetStorageIdentifier(const string &id_suffix) { |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 79 | string id = GetRpcIdentifier(); |
| 80 | ControlInterface::RpcIdToStorageId(&id); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 81 | size_t needle = id.find('_'); |
| 82 | LOG_IF(ERROR, needle == string::npos) << "No _ in storage id?!?!"; |
| 83 | id.replace(id.begin() + needle + 1, id.end(), id_suffix); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 84 | return id; |
| 85 | } |
| 86 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 87 | bool IPConfig::RequestIP() { |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 88 | return false; |
| 89 | } |
| 90 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 91 | bool IPConfig::RenewIP() { |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | bool IPConfig::ReleaseIP() { |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 96 | return false; |
| 97 | } |
| 98 | |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 99 | void IPConfig::ApplyStaticIPParameters( |
| 100 | const StaticIPParameters &static_ip_parameters) { |
| 101 | static_ip_parameters.ApplyTo(&properties_); |
| 102 | } |
| 103 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 104 | bool IPConfig::Load(StoreInterface *storage, const string &id_suffix) { |
| 105 | const string id = GetStorageIdentifier(id_suffix); |
Chris Masone | 8a7b8be | 2011-07-22 12:43:37 -0700 | [diff] [blame] | 106 | if (!storage->ContainsGroup(id)) { |
| 107 | LOG(WARNING) << "IPConfig is not available in the persistent store: " << id; |
| 108 | return false; |
| 109 | } |
| 110 | string local_type; |
| 111 | storage->GetString(id, kStorageType, &local_type); |
| 112 | return local_type == type(); |
| 113 | } |
| 114 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 115 | bool IPConfig::Save(StoreInterface *storage, const string &id_suffix) { |
| 116 | const string id = GetStorageIdentifier(id_suffix); |
Chris Masone | 8a7b8be | 2011-07-22 12:43:37 -0700 | [diff] [blame] | 117 | storage->SetString(id, kStorageType, type()); |
| 118 | return true; |
| 119 | } |
| 120 | |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 121 | void IPConfig::UpdateProperties(const Properties &properties, bool success) { |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 122 | properties_ = properties; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 123 | if (!update_callback_.is_null()) { |
| 124 | update_callback_.Run(this, success); |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
| 128 | void IPConfig::RegisterUpdateCallback( |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 129 | const Callback<void(const IPConfigRefPtr&, bool)> &callback) { |
| 130 | update_callback_ = callback; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 133 | } // namespace shill |