blob: d27b4f5ff54ccb82bd2ad846b96445d121549b9c [file] [log] [blame]
Ben Chanfad4a0b2012-04-18 15:49:59 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkove02b3ca2011-05-31 16:00:44 -07002// 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 Masone43b48a12011-07-01 13:37:07 -07007#include <chromeos/dbus/service_constants.h>
Darin Petkove02b3ca2011-05-31 16:00:44 -07008
Chris Masonec6c6c132011-06-30 11:29:52 -07009#include "shill/adaptor_interfaces.h"
Chris Masone19e30402011-07-19 15:48:47 -070010#include "shill/control_interface.h"
Chris Masone43b48a12011-07-01 13:37:07 -070011#include "shill/error.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070012#include "shill/logging.h"
Paul Stewart1062d9d2012-04-27 10:42:27 -070013#include "shill/static_ip_parameters.h"
Chris Masone8a7b8be2011-07-22 12:43:37 -070014#include "shill/store_interface.h"
Chris Masonec6c6c132011-06-30 11:29:52 -070015
Eric Shienbrood3e20a232012-02-16 11:35:56 -050016using base::Callback;
Darin Petkove02b3ca2011-05-31 16:00:44 -070017using std::string;
18
19namespace shill {
20
Chris Masone0756f232011-07-21 17:24:00 -070021// static
Chris Masone5dec5f42011-07-22 14:07:55 -070022const char IPConfig::kStorageType[] = "Method";
Chris Masone8a7b8be2011-07-22 12:43:37 -070023// static
Chris Masone0756f232011-07-21 17:24:00 -070024const char IPConfig::kType[] = "ip";
25// static
26uint IPConfig::global_serial_ = 0;
27
Chris Masone19e30402011-07-19 15:48:47 -070028IPConfig::IPConfig(ControlInterface *control_interface,
29 const std::string &device_name)
30 : device_name_(device_name),
Chris Masone0756f232011-07-21 17:24:00 -070031 type_(kType),
32 serial_(global_serial_++),
Chris Masone19e30402011-07-19 15:48:47 -070033 adaptor_(control_interface->CreateIPConfigAdaptor(this)) {
Chris Masone0756f232011-07-21 17:24:00 -070034 Init();
35}
36
37IPConfig::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
47void IPConfig::Init() {
Ben Chan0295e0f2013-09-20 13:47:29 -070048 store_.RegisterConstString(kAddressProperty, &properties_.address);
49 store_.RegisterConstString(kBroadcastProperty,
Paul Stewart10241e32012-04-23 18:15:06 -070050 &properties_.broadcast_address);
Ben Chan0295e0f2013-09-20 13:47:29 -070051 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 Stewarta63f5212013-06-25 15:29:40 -070058 store_.RegisterConstStrings(kSearchDomainsProperty,
Christopher Wiley53b3f472012-09-17 09:43:39 -070059 &properties_.domain_search);
Paul Stewartc3fdba92013-12-02 11:12:38 -080060 store_.RegisterConstString(kVendorEncapsulatedOptionsProperty,
61 &properties_.vendor_encapsulated_options);
Paul Stewarta63f5212013-06-25 15:29:40 -070062 store_.RegisterConstString(kWebProxyAutoDiscoveryUrlProperty,
63 &properties_.web_proxy_auto_discovery);
Ben Chanfad4a0b2012-04-18 15:49:59 -070064 SLOG(Inet, 2) << __func__ << " device: " << device_name();
Darin Petkove02b3ca2011-05-31 16:00:44 -070065}
66
67IPConfig::~IPConfig() {
Ben Chanfad4a0b2012-04-18 15:49:59 -070068 SLOG(Inet, 2) << __func__ << " device: " << device_name();
Darin Petkove02b3ca2011-05-31 16:00:44 -070069}
70
Chris Masone4e851612011-07-01 10:46:53 -070071string IPConfig::GetRpcIdentifier() {
72 return adaptor_->GetRpcIdentifier();
73}
74
Chris Masone34af2182011-08-22 11:59:36 -070075string IPConfig::GetStorageIdentifier(const string &id_suffix) {
Chris Masone5dec5f42011-07-22 14:07:55 -070076 string id = GetRpcIdentifier();
77 ControlInterface::RpcIdToStorageId(&id);
Chris Masone34af2182011-08-22 11:59:36 -070078 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 Masone5dec5f42011-07-22 14:07:55 -070081 return id;
82}
83
Darin Petkov92c43902011-06-09 20:46:06 -070084bool IPConfig::RequestIP() {
Darin Petkove7cb7f82011-06-03 13:21:51 -070085 return false;
86}
87
Darin Petkov92c43902011-06-09 20:46:06 -070088bool IPConfig::RenewIP() {
89 return false;
90}
91
Paul Stewart217c61d2013-06-13 15:12:02 -070092bool IPConfig::ReleaseIP(ReleaseReason reason) {
Darin Petkove7cb7f82011-06-03 13:21:51 -070093 return false;
94}
95
Paul Stewart4558bda2012-08-03 10:44:10 -070096void IPConfig::Refresh(Error */*error*/) {
Paul Stewart82236532013-12-10 15:33:11 -080097 if (!refresh_callback_.is_null()) {
98 refresh_callback_.Run(this);
99 }
Paul Stewart4558bda2012-08-03 10:44:10 -0700100 RenewIP();
101}
102
Paul Stewart1062d9d2012-04-27 10:42:27 -0700103void IPConfig::ApplyStaticIPParameters(
Paul Stewartdef189e2012-08-02 20:12:09 -0700104 StaticIPParameters *static_ip_parameters) {
105 static_ip_parameters->ApplyTo(&properties_);
mukesh agrawal7aed61c2013-04-22 16:01:24 -0700106 EmitChanges();
Paul Stewart1062d9d2012-04-27 10:42:27 -0700107}
108
Paul Stewart82236532013-12-10 15:33:11 -0800109void IPConfig::RestoreSavedIPParameters(
110 StaticIPParameters *static_ip_parameters) {
111 static_ip_parameters->RestoreTo(&properties_);
112 EmitChanges();
113}
114
Chris Masone34af2182011-08-22 11:59:36 -0700115bool IPConfig::Load(StoreInterface *storage, const string &id_suffix) {
116 const string id = GetStorageIdentifier(id_suffix);
Chris Masone8a7b8be2011-07-22 12:43:37 -0700117 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 Masone34af2182011-08-22 11:59:36 -0700126bool IPConfig::Save(StoreInterface *storage, const string &id_suffix) {
127 const string id = GetStorageIdentifier(id_suffix);
Chris Masone8a7b8be2011-07-22 12:43:37 -0700128 storage->SetString(id, kStorageType, type());
129 return true;
130}
131
Paul Stewartc5099532013-12-12 07:53:15 -0800132void IPConfig::UpdateProperties(const Properties &properties) {
mukesh agrawal1c1dd352013-05-08 15:58:34 -0700133 // 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 Stewartc5099532013-12-12 07:53:15 -0800138
Darin Petkove7cb7f82011-06-03 13:21:51 -0700139 properties_ = properties;
Paul Stewartc5099532013-12-12 07:53:15 -0800140
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500141 if (!update_callback_.is_null()) {
Paul Stewartc5099532013-12-12 07:53:15 -0800142 update_callback_.Run(this);
Darin Petkovefb09c32011-06-07 20:24:17 -0700143 }
mukesh agrawal7aed61c2013-04-22 16:01:24 -0700144 EmitChanges();
Darin Petkovefb09c32011-06-07 20:24:17 -0700145}
146
Paul Stewartc5099532013-12-12 07:53:15 -0800147void 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 Stewart1f916e42013-12-23 09:52:54 -0800159void IPConfig::NotifyExpiry() {
160 if (!expire_callback_.is_null()) {
161 expire_callback_.Run(this);
162 }
163}
164
Paul Stewartc5099532013-12-12 07:53:15 -0800165void IPConfig::RegisterUpdateCallback(const Callback &callback) {
166 update_callback_ = callback;
167}
168
169void IPConfig::RegisterFailureCallback(const Callback &callback) {
170 failure_callback_ = callback;
171}
172
173void IPConfig::RegisterRefreshCallback(const Callback &callback) {
Paul Stewart82236532013-12-10 15:33:11 -0800174 refresh_callback_ = callback;
175}
176
Paul Stewart1f916e42013-12-23 09:52:54 -0800177void IPConfig::RegisterExpireCallback(const Callback &callback) {
178 expire_callback_ = callback;
179}
180
Paul Stewartc5099532013-12-12 07:53:15 -0800181void IPConfig::ResetProperties() {
182 properties_ = Properties();
183 EmitChanges();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700184}
185
mukesh agrawal7aed61c2013-04-22 16:01:24 -0700186void IPConfig::EmitChanges() {
Ben Chan0295e0f2013-09-20 13:47:29 -0700187 adaptor_->EmitStringChanged(kAddressProperty, properties_.address);
188 adaptor_->EmitStringsChanged(kNameServersProperty, properties_.dns_servers);
mukesh agrawal7aed61c2013-04-22 16:01:24 -0700189}
190
Darin Petkove02b3ca2011-05-31 16:00:44 -0700191} // namespace shill