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