blob: 63e453dad1be7ead81a0690b3909a97065850540 [file] [log] [blame]
Darin Petkove02b3ca2011-05-31 16:00:44 -07001// 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 Masone43b48a12011-07-01 13:37:07 -07008#include <chromeos/dbus/service_constants.h>
Darin Petkove02b3ca2011-05-31 16:00:44 -07009
Chris Masonec6c6c132011-06-30 11:29:52 -070010#include "shill/adaptor_interfaces.h"
Chris Masone19e30402011-07-19 15:48:47 -070011#include "shill/control_interface.h"
Chris Masone43b48a12011-07-01 13:37:07 -070012#include "shill/error.h"
Chris Masone8a7b8be2011-07-22 12:43:37 -070013#include "shill/store_interface.h"
Chris Masonec6c6c132011-06-30 11:29:52 -070014
Darin Petkove02b3ca2011-05-31 16:00:44 -070015using std::string;
16
17namespace shill {
18
Chris Masone0756f232011-07-21 17:24:00 -070019// static
Chris Masone5dec5f42011-07-22 14:07:55 -070020const char IPConfig::kStorageType[] = "Method";
Chris Masone8a7b8be2011-07-22 12:43:37 -070021// static
Chris Masone0756f232011-07-21 17:24:00 -070022const char IPConfig::kType[] = "ip";
23// static
24uint IPConfig::global_serial_ = 0;
25
Chris Masone19e30402011-07-19 15:48:47 -070026IPConfig::IPConfig(ControlInterface *control_interface,
27 const std::string &device_name)
28 : device_name_(device_name),
Chris Masone0756f232011-07-21 17:24:00 -070029 type_(kType),
30 serial_(global_serial_++),
Chris Masone19e30402011-07-19 15:48:47 -070031 adaptor_(control_interface->CreateIPConfigAdaptor(this)) {
Chris Masone0756f232011-07-21 17:24:00 -070032 Init();
33}
34
35IPConfig::IPConfig(ControlInterface *control_interface,
36 const std::string &device_name,
37 const std::string &type)
38 : device_name_(device_name),
39 type_(type),
40 serial_(global_serial_++),
41 adaptor_(control_interface->CreateIPConfigAdaptor(this)) {
42 Init();
43}
44
45void IPConfig::Init() {
Chris Masone43b48a12011-07-01 13:37:07 -070046 // Address might be R/O or not, depending on the type of IPconfig, so
47 // we'll leave this up to the subclasses.
48 // Register(Const?)String(flimflam::kAddressProperty, &properties_.address);
Chris Masone27c4aa52011-07-02 13:10:14 -070049 store_.RegisterString(flimflam::kBroadcastProperty,
50 &properties_.broadcast_address);
51 store_.RegisterString(flimflam::kDomainNameProperty,
52 &properties_.domain_name);
53 store_.RegisterString(flimflam::kGatewayProperty, &properties_.gateway);
54 store_.RegisterConstString(flimflam::kMethodProperty, &properties_.method);
55 store_.RegisterInt32(flimflam::kMtuProperty, &properties_.mtu);
56 store_.RegisterStrings(flimflam::kNameServersProperty,
57 &properties_.dns_servers);
58 store_.RegisterString(flimflam::kPeerAddressProperty,
59 &properties_.peer_address);
60 store_.RegisterInt32(flimflam::kPrefixlenProperty, &properties_.subnet_cidr);
Chris Masone43b48a12011-07-01 13:37:07 -070061 // TODO(cmasone): Does anyone use this?
Chris Masone27c4aa52011-07-02 13:10:14 -070062 // store_.RegisterStrings(flimflam::kSearchDomainsProperty,
63 // &properties_.domain_search);
Chris Masone0756f232011-07-21 17:24:00 -070064 VLOG(2) << __func__ << " device: " << device_name();
Darin Petkove02b3ca2011-05-31 16:00:44 -070065}
66
67IPConfig::~IPConfig() {
Darin Petkovf65e9282011-06-21 14:29:56 -070068 VLOG(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
92bool IPConfig::ReleaseIP() {
Darin Petkove7cb7f82011-06-03 13:21:51 -070093 return false;
94}
95
Chris Masone34af2182011-08-22 11:59:36 -070096bool IPConfig::Load(StoreInterface *storage, const string &id_suffix) {
97 const string id = GetStorageIdentifier(id_suffix);
Chris Masone8a7b8be2011-07-22 12:43:37 -070098 if (!storage->ContainsGroup(id)) {
99 LOG(WARNING) << "IPConfig is not available in the persistent store: " << id;
100 return false;
101 }
102 string local_type;
103 storage->GetString(id, kStorageType, &local_type);
104 return local_type == type();
105}
106
Chris Masone34af2182011-08-22 11:59:36 -0700107bool IPConfig::Save(StoreInterface *storage, const string &id_suffix) {
108 const string id = GetStorageIdentifier(id_suffix);
Chris Masone8a7b8be2011-07-22 12:43:37 -0700109 storage->SetString(id, kStorageType, type());
110 return true;
111}
112
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700113void IPConfig::UpdateProperties(const Properties &properties, bool success) {
Darin Petkove7cb7f82011-06-03 13:21:51 -0700114 properties_ = properties;
Darin Petkovefb09c32011-06-07 20:24:17 -0700115 if (update_callback_.get()) {
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700116 update_callback_->Run(this, success);
Darin Petkovefb09c32011-06-07 20:24:17 -0700117 }
118}
119
120void IPConfig::RegisterUpdateCallback(
Chris Masone2b105542011-06-22 10:58:09 -0700121 Callback2<const IPConfigRefPtr&, bool>::Type *callback) {
Darin Petkovefb09c32011-06-07 20:24:17 -0700122 update_callback_.reset(callback);
Darin Petkove7cb7f82011-06-03 13:21:51 -0700123}
124
Darin Petkove02b3ca2011-05-31 16:00:44 -0700125} // namespace shill