blob: ce8c9c1375de913a13d451aa7816e9c68488d295 [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() {
Paul Stewart10241e32012-04-23 18:15:06 -070048 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 Masone27c4aa52011-07-02 13:10:14 -070055 store_.RegisterConstString(flimflam::kMethodProperty, &properties_.method);
Paul Stewart10241e32012-04-23 18:15:06 -070056 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 Wiley53b3f472012-09-17 09:43:39 -070063 store_.RegisterConstStrings(shill::kSearchDomainsProperty,
64 &properties_.domain_search);
Ben Chanfad4a0b2012-04-18 15:49:59 -070065 SLOG(Inet, 2) << __func__ << " device: " << device_name();
Darin Petkove02b3ca2011-05-31 16:00:44 -070066}
67
68IPConfig::~IPConfig() {
Ben Chanfad4a0b2012-04-18 15:49:59 -070069 SLOG(Inet, 2) << __func__ << " device: " << device_name();
Darin Petkove02b3ca2011-05-31 16:00:44 -070070}
71
Chris Masone4e851612011-07-01 10:46:53 -070072string IPConfig::GetRpcIdentifier() {
73 return adaptor_->GetRpcIdentifier();
74}
75
Chris Masone34af2182011-08-22 11:59:36 -070076string IPConfig::GetStorageIdentifier(const string &id_suffix) {
Chris Masone5dec5f42011-07-22 14:07:55 -070077 string id = GetRpcIdentifier();
78 ControlInterface::RpcIdToStorageId(&id);
Chris Masone34af2182011-08-22 11:59:36 -070079 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 Masone5dec5f42011-07-22 14:07:55 -070082 return id;
83}
84
Darin Petkov92c43902011-06-09 20:46:06 -070085bool IPConfig::RequestIP() {
Darin Petkove7cb7f82011-06-03 13:21:51 -070086 return false;
87}
88
Darin Petkov92c43902011-06-09 20:46:06 -070089bool IPConfig::RenewIP() {
90 return false;
91}
92
93bool IPConfig::ReleaseIP() {
Darin Petkove7cb7f82011-06-03 13:21:51 -070094 return false;
95}
96
Paul Stewart4558bda2012-08-03 10:44:10 -070097void IPConfig::Refresh(Error */*error*/) {
98 RenewIP();
99}
100
Paul Stewart1062d9d2012-04-27 10:42:27 -0700101void IPConfig::ApplyStaticIPParameters(
Paul Stewartdef189e2012-08-02 20:12:09 -0700102 StaticIPParameters *static_ip_parameters) {
103 static_ip_parameters->ApplyTo(&properties_);
mukesh agrawal7aed61c2013-04-22 16:01:24 -0700104 EmitChanges();
Paul Stewart1062d9d2012-04-27 10:42:27 -0700105}
106
Chris Masone34af2182011-08-22 11:59:36 -0700107bool IPConfig::Load(StoreInterface *storage, const string &id_suffix) {
108 const string id = GetStorageIdentifier(id_suffix);
Chris Masone8a7b8be2011-07-22 12:43:37 -0700109 if (!storage->ContainsGroup(id)) {
110 LOG(WARNING) << "IPConfig is not available in the persistent store: " << id;
111 return false;
112 }
113 string local_type;
114 storage->GetString(id, kStorageType, &local_type);
115 return local_type == type();
116}
117
Chris Masone34af2182011-08-22 11:59:36 -0700118bool IPConfig::Save(StoreInterface *storage, const string &id_suffix) {
119 const string id = GetStorageIdentifier(id_suffix);
Chris Masone8a7b8be2011-07-22 12:43:37 -0700120 storage->SetString(id, kStorageType, type());
121 return true;
122}
123
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700124void IPConfig::UpdateProperties(const Properties &properties, bool success) {
mukesh agrawal1c1dd352013-05-08 15:58:34 -0700125 // Take a reference of this instance to make sure we don't get destroyed in
126 // the middle of this call. (The |update_callback_| may cause a reference
127 // to be dropped. See, e.g., EthernetService::Disconnect and
128 // Ethernet::DropConnection.)
129 IPConfigRefPtr me = this;
Darin Petkove7cb7f82011-06-03 13:21:51 -0700130 properties_ = properties;
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500131 if (!update_callback_.is_null()) {
132 update_callback_.Run(this, success);
Darin Petkovefb09c32011-06-07 20:24:17 -0700133 }
mukesh agrawal7aed61c2013-04-22 16:01:24 -0700134 EmitChanges();
Darin Petkovefb09c32011-06-07 20:24:17 -0700135}
136
137void IPConfig::RegisterUpdateCallback(
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500138 const Callback<void(const IPConfigRefPtr&, bool)> &callback) {
139 update_callback_ = callback;
Darin Petkove7cb7f82011-06-03 13:21:51 -0700140}
141
mukesh agrawal7aed61c2013-04-22 16:01:24 -0700142void IPConfig::EmitChanges() {
143 adaptor_->EmitStringChanged(flimflam::kAddressProperty,
144 properties_.address);
145 adaptor_->EmitStringsChanged(flimflam::kNameServersProperty,
146 properties_.dns_servers);
147}
148
Darin Petkove02b3ca2011-05-31 16:00:44 -0700149} // namespace shill