blob: 8b45703b5945b3492b97438a319957ad80949b7a [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 Masone5dec5f42011-07-22 14:07:55 -070075string IPConfig::GetStorageIdentifier() {
76 string id = GetRpcIdentifier();
77 ControlInterface::RpcIdToStorageId(&id);
78 return id;
79}
80
Darin Petkov92c43902011-06-09 20:46:06 -070081bool IPConfig::RequestIP() {
Darin Petkove7cb7f82011-06-03 13:21:51 -070082 return false;
83}
84
Darin Petkov92c43902011-06-09 20:46:06 -070085bool IPConfig::RenewIP() {
86 return false;
87}
88
89bool IPConfig::ReleaseIP() {
Darin Petkove7cb7f82011-06-03 13:21:51 -070090 return false;
91}
92
Chris Masone8a7b8be2011-07-22 12:43:37 -070093bool IPConfig::Load(StoreInterface *storage) {
94 const string id = GetStorageIdentifier();
95 if (!storage->ContainsGroup(id)) {
96 LOG(WARNING) << "IPConfig is not available in the persistent store: " << id;
97 return false;
98 }
99 string local_type;
100 storage->GetString(id, kStorageType, &local_type);
101 return local_type == type();
102}
103
104bool IPConfig::Save(StoreInterface *storage) {
105 const string id = GetStorageIdentifier();
106 storage->SetString(id, kStorageType, type());
107 return true;
108}
109
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700110void IPConfig::UpdateProperties(const Properties &properties, bool success) {
Darin Petkove7cb7f82011-06-03 13:21:51 -0700111 properties_ = properties;
Darin Petkovefb09c32011-06-07 20:24:17 -0700112 if (update_callback_.get()) {
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700113 update_callback_->Run(this, success);
Darin Petkovefb09c32011-06-07 20:24:17 -0700114 }
115}
116
117void IPConfig::RegisterUpdateCallback(
Chris Masone2b105542011-06-22 10:58:09 -0700118 Callback2<const IPConfigRefPtr&, bool>::Type *callback) {
Darin Petkovefb09c32011-06-07 20:24:17 -0700119 update_callback_.reset(callback);
Darin Petkove7cb7f82011-06-03 13:21:51 -0700120}
121
Darin Petkove02b3ca2011-05-31 16:00:44 -0700122} // namespace shill