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 | |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 7 | #include <chromeos/dbus/service_constants.h> |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 8 | |
Chris Masone | c6c6c13 | 2011-06-30 11:29:52 -0700 | [diff] [blame] | 9 | #include "shill/adaptor_interfaces.h" |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 10 | #include "shill/control_interface.h" |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 11 | #include "shill/error.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 12 | #include "shill/logging.h" |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 13 | #include "shill/static_ip_parameters.h" |
Chris Masone | 8a7b8be | 2011-07-22 12:43:37 -0700 | [diff] [blame] | 14 | #include "shill/store_interface.h" |
Chris Masone | c6c6c13 | 2011-06-30 11:29:52 -0700 | [diff] [blame] | 15 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 16 | using base::Callback; |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 17 | using std::string; |
| 18 | |
| 19 | namespace shill { |
| 20 | |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 21 | // static |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 22 | const char IPConfig::kStorageType[] = "Method"; |
Chris Masone | 8a7b8be | 2011-07-22 12:43:37 -0700 | [diff] [blame] | 23 | // static |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 24 | const char IPConfig::kType[] = "ip"; |
| 25 | // static |
| 26 | uint IPConfig::global_serial_ = 0; |
| 27 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 28 | IPConfig::IPConfig(ControlInterface *control_interface, |
| 29 | const std::string &device_name) |
| 30 | : device_name_(device_name), |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 31 | type_(kType), |
| 32 | serial_(global_serial_++), |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 33 | adaptor_(control_interface->CreateIPConfigAdaptor(this)) { |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 34 | Init(); |
| 35 | } |
| 36 | |
| 37 | IPConfig::IPConfig(ControlInterface *control_interface, |
| 38 | const std::string &device_name, |
| 39 | const std::string &type) |
| 40 | : device_name_(device_name), |
| 41 | type_(type), |
| 42 | serial_(global_serial_++), |
| 43 | adaptor_(control_interface->CreateIPConfigAdaptor(this)) { |
| 44 | Init(); |
| 45 | } |
| 46 | |
| 47 | void IPConfig::Init() { |
Ben Chan | 0295e0f | 2013-09-20 13:47:29 -0700 | [diff] [blame] | 48 | store_.RegisterConstString(kAddressProperty, &properties_.address); |
| 49 | store_.RegisterConstString(kBroadcastProperty, |
Paul Stewart | 10241e3 | 2012-04-23 18:15:06 -0700 | [diff] [blame] | 50 | &properties_.broadcast_address); |
Ben Chan | 0295e0f | 2013-09-20 13:47:29 -0700 | [diff] [blame] | 51 | store_.RegisterConstString(kDomainNameProperty, &properties_.domain_name); |
| 52 | store_.RegisterConstString(kGatewayProperty, &properties_.gateway); |
| 53 | store_.RegisterConstString(kMethodProperty, &properties_.method); |
| 54 | store_.RegisterConstInt32(kMtuProperty, &properties_.mtu); |
| 55 | store_.RegisterConstStrings(kNameServersProperty, &properties_.dns_servers); |
| 56 | store_.RegisterConstString(kPeerAddressProperty, &properties_.peer_address); |
| 57 | store_.RegisterConstInt32(kPrefixlenProperty, &properties_.subnet_prefix); |
Paul Stewart | a63f521 | 2013-06-25 15:29:40 -0700 | [diff] [blame] | 58 | store_.RegisterConstStrings(kSearchDomainsProperty, |
Christopher Wiley | 53b3f47 | 2012-09-17 09:43:39 -0700 | [diff] [blame] | 59 | &properties_.domain_search); |
Paul Stewart | c3fdba9 | 2013-12-02 11:12:38 -0800 | [diff] [blame] | 60 | store_.RegisterConstString(kVendorEncapsulatedOptionsProperty, |
| 61 | &properties_.vendor_encapsulated_options); |
Paul Stewart | a63f521 | 2013-06-25 15:29:40 -0700 | [diff] [blame] | 62 | store_.RegisterConstString(kWebProxyAutoDiscoveryUrlProperty, |
| 63 | &properties_.web_proxy_auto_discovery); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 64 | SLOG(Inet, 2) << __func__ << " device: " << device_name(); |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | IPConfig::~IPConfig() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 68 | SLOG(Inet, 2) << __func__ << " device: " << device_name(); |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 71 | string IPConfig::GetRpcIdentifier() { |
| 72 | return adaptor_->GetRpcIdentifier(); |
| 73 | } |
| 74 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 75 | string IPConfig::GetStorageIdentifier(const string &id_suffix) { |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 76 | string id = GetRpcIdentifier(); |
| 77 | ControlInterface::RpcIdToStorageId(&id); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 78 | size_t needle = id.find('_'); |
| 79 | LOG_IF(ERROR, needle == string::npos) << "No _ in storage id?!?!"; |
| 80 | id.replace(id.begin() + needle + 1, id.end(), id_suffix); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 81 | return id; |
| 82 | } |
| 83 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 84 | bool IPConfig::RequestIP() { |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 85 | return false; |
| 86 | } |
| 87 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 88 | bool IPConfig::RenewIP() { |
| 89 | return false; |
| 90 | } |
| 91 | |
Paul Stewart | 217c61d | 2013-06-13 15:12:02 -0700 | [diff] [blame] | 92 | bool IPConfig::ReleaseIP(ReleaseReason reason) { |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 93 | return false; |
| 94 | } |
| 95 | |
Paul Stewart | 4558bda | 2012-08-03 10:44:10 -0700 | [diff] [blame] | 96 | void IPConfig::Refresh(Error */*error*/) { |
Paul Stewart | 8223653 | 2013-12-10 15:33:11 -0800 | [diff] [blame] | 97 | if (!refresh_callback_.is_null()) { |
| 98 | refresh_callback_.Run(this); |
| 99 | } |
Paul Stewart | 4558bda | 2012-08-03 10:44:10 -0700 | [diff] [blame] | 100 | RenewIP(); |
| 101 | } |
| 102 | |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 103 | void IPConfig::ApplyStaticIPParameters( |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 104 | StaticIPParameters *static_ip_parameters) { |
| 105 | static_ip_parameters->ApplyTo(&properties_); |
mukesh agrawal | 7aed61c | 2013-04-22 16:01:24 -0700 | [diff] [blame] | 106 | EmitChanges(); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Paul Stewart | 8223653 | 2013-12-10 15:33:11 -0800 | [diff] [blame] | 109 | void IPConfig::RestoreSavedIPParameters( |
| 110 | StaticIPParameters *static_ip_parameters) { |
| 111 | static_ip_parameters->RestoreTo(&properties_); |
| 112 | EmitChanges(); |
| 113 | } |
| 114 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 115 | bool IPConfig::Load(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 | if (!storage->ContainsGroup(id)) { |
| 118 | LOG(WARNING) << "IPConfig is not available in the persistent store: " << id; |
| 119 | return false; |
| 120 | } |
| 121 | string local_type; |
| 122 | storage->GetString(id, kStorageType, &local_type); |
| 123 | return local_type == type(); |
| 124 | } |
| 125 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 126 | bool IPConfig::Save(StoreInterface *storage, const string &id_suffix) { |
| 127 | const string id = GetStorageIdentifier(id_suffix); |
Chris Masone | 8a7b8be | 2011-07-22 12:43:37 -0700 | [diff] [blame] | 128 | storage->SetString(id, kStorageType, type()); |
| 129 | return true; |
| 130 | } |
| 131 | |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 132 | void IPConfig::UpdateProperties(const Properties &properties) { |
mukesh agrawal | 1c1dd35 | 2013-05-08 15:58:34 -0700 | [diff] [blame] | 133 | // Take a reference of this instance to make sure we don't get destroyed in |
| 134 | // the middle of this call. (The |update_callback_| may cause a reference |
| 135 | // to be dropped. See, e.g., EthernetService::Disconnect and |
| 136 | // Ethernet::DropConnection.) |
| 137 | IPConfigRefPtr me = this; |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 138 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 139 | properties_ = properties; |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 140 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 141 | if (!update_callback_.is_null()) { |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 142 | update_callback_.Run(this); |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 143 | } |
mukesh agrawal | 7aed61c | 2013-04-22 16:01:24 -0700 | [diff] [blame] | 144 | EmitChanges(); |
Darin Petkov | efb09c3 | 2011-06-07 20:24:17 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 147 | void IPConfig::NotifyFailure() { |
| 148 | // Take a reference of this instance to make sure we don't get destroyed in |
| 149 | // the middle of this call. (The |update_callback_| may cause a reference |
| 150 | // to be dropped. See, e.g., EthernetService::Disconnect and |
| 151 | // Ethernet::DropConnection.) |
| 152 | IPConfigRefPtr me = this; |
| 153 | |
| 154 | if (!failure_callback_.is_null()) { |
| 155 | failure_callback_.Run(this); |
| 156 | } |
| 157 | } |
| 158 | |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame^] | 159 | void IPConfig::NotifyExpiry() { |
| 160 | if (!expire_callback_.is_null()) { |
| 161 | expire_callback_.Run(this); |
| 162 | } |
| 163 | } |
| 164 | |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 165 | void IPConfig::RegisterUpdateCallback(const Callback &callback) { |
| 166 | update_callback_ = callback; |
| 167 | } |
| 168 | |
| 169 | void IPConfig::RegisterFailureCallback(const Callback &callback) { |
| 170 | failure_callback_ = callback; |
| 171 | } |
| 172 | |
| 173 | void IPConfig::RegisterRefreshCallback(const Callback &callback) { |
Paul Stewart | 8223653 | 2013-12-10 15:33:11 -0800 | [diff] [blame] | 174 | refresh_callback_ = callback; |
| 175 | } |
| 176 | |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame^] | 177 | void IPConfig::RegisterExpireCallback(const Callback &callback) { |
| 178 | expire_callback_ = callback; |
| 179 | } |
| 180 | |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 181 | void IPConfig::ResetProperties() { |
| 182 | properties_ = Properties(); |
| 183 | EmitChanges(); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 184 | } |
| 185 | |
mukesh agrawal | 7aed61c | 2013-04-22 16:01:24 -0700 | [diff] [blame] | 186 | void IPConfig::EmitChanges() { |
Ben Chan | 0295e0f | 2013-09-20 13:47:29 -0700 | [diff] [blame] | 187 | adaptor_->EmitStringChanged(kAddressProperty, properties_.address); |
| 188 | adaptor_->EmitStringsChanged(kNameServersProperty, properties_.dns_servers); |
mukesh agrawal | 7aed61c | 2013-04-22 16:01:24 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Darin Petkov | e02b3ca | 2011-05-31 16:00:44 -0700 | [diff] [blame] | 191 | } // namespace shill |