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