blob: 0963039122a2d894c85b3d58da3ba09e2cc64a9a [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
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"
Ben Chanfad4a0b2012-04-18 15:49:59 -070013#include "shill/scope_logger.h"
Paul Stewart1062d9d2012-04-27 10:42:27 -070014#include "shill/static_ip_parameters.h"
Chris Masone8a7b8be2011-07-22 12:43:37 -070015#include "shill/store_interface.h"
Chris Masonec6c6c132011-06-30 11:29:52 -070016
Eric Shienbrood3e20a232012-02-16 11:35:56 -050017using base::Callback;
Darin Petkove02b3ca2011-05-31 16:00:44 -070018using std::string;
19
20namespace shill {
21
Chris Masone0756f232011-07-21 17:24:00 -070022// static
Chris Masone5dec5f42011-07-22 14:07:55 -070023const char IPConfig::kStorageType[] = "Method";
Chris Masone8a7b8be2011-07-22 12:43:37 -070024// static
Chris Masone0756f232011-07-21 17:24:00 -070025const char IPConfig::kType[] = "ip";
26// static
27uint IPConfig::global_serial_ = 0;
28
Chris Masone19e30402011-07-19 15:48:47 -070029IPConfig::IPConfig(ControlInterface *control_interface,
30 const std::string &device_name)
31 : device_name_(device_name),
Chris Masone0756f232011-07-21 17:24:00 -070032 type_(kType),
33 serial_(global_serial_++),
Chris Masone19e30402011-07-19 15:48:47 -070034 adaptor_(control_interface->CreateIPConfigAdaptor(this)) {
Chris Masone0756f232011-07-21 17:24:00 -070035 Init();
36}
37
38IPConfig::IPConfig(ControlInterface *control_interface,
39 const std::string &device_name,
40 const std::string &type)
41 : device_name_(device_name),
42 type_(type),
43 serial_(global_serial_++),
44 adaptor_(control_interface->CreateIPConfigAdaptor(this)) {
45 Init();
46}
47
48void IPConfig::Init() {
Paul Stewart10241e32012-04-23 18:15:06 -070049 store_.RegisterConstString(flimflam::kAddressProperty,
50 &properties_.address);
51 store_.RegisterConstString(flimflam::kBroadcastProperty,
52 &properties_.broadcast_address);
53 store_.RegisterConstString(flimflam::kDomainNameProperty,
54 &properties_.domain_name);
55 store_.RegisterConstString(flimflam::kGatewayProperty, &properties_.gateway);
Chris Masone27c4aa52011-07-02 13:10:14 -070056 store_.RegisterConstString(flimflam::kMethodProperty, &properties_.method);
Paul Stewart10241e32012-04-23 18:15:06 -070057 store_.RegisterConstInt32(flimflam::kMtuProperty, &properties_.mtu);
58 store_.RegisterConstStrings(flimflam::kNameServersProperty,
59 &properties_.dns_servers);
60 store_.RegisterConstString(flimflam::kPeerAddressProperty,
61 &properties_.peer_address);
62 store_.RegisterConstInt32(flimflam::kPrefixlenProperty,
63 &properties_.subnet_prefix);
Chris Masone43b48a12011-07-01 13:37:07 -070064 // TODO(cmasone): Does anyone use this?
Chris Masone27c4aa52011-07-02 13:10:14 -070065 // store_.RegisterStrings(flimflam::kSearchDomainsProperty,
66 // &properties_.domain_search);
Ben Chanfad4a0b2012-04-18 15:49:59 -070067 SLOG(Inet, 2) << __func__ << " device: " << device_name();
Darin Petkove02b3ca2011-05-31 16:00:44 -070068}
69
70IPConfig::~IPConfig() {
Ben Chanfad4a0b2012-04-18 15:49:59 -070071 SLOG(Inet, 2) << __func__ << " device: " << device_name();
Darin Petkove02b3ca2011-05-31 16:00:44 -070072}
73
Chris Masone4e851612011-07-01 10:46:53 -070074string IPConfig::GetRpcIdentifier() {
75 return adaptor_->GetRpcIdentifier();
76}
77
Chris Masone34af2182011-08-22 11:59:36 -070078string IPConfig::GetStorageIdentifier(const string &id_suffix) {
Chris Masone5dec5f42011-07-22 14:07:55 -070079 string id = GetRpcIdentifier();
80 ControlInterface::RpcIdToStorageId(&id);
Chris Masone34af2182011-08-22 11:59:36 -070081 size_t needle = id.find('_');
82 LOG_IF(ERROR, needle == string::npos) << "No _ in storage id?!?!";
83 id.replace(id.begin() + needle + 1, id.end(), id_suffix);
Chris Masone5dec5f42011-07-22 14:07:55 -070084 return id;
85}
86
Darin Petkov92c43902011-06-09 20:46:06 -070087bool IPConfig::RequestIP() {
Darin Petkove7cb7f82011-06-03 13:21:51 -070088 return false;
89}
90
Darin Petkov92c43902011-06-09 20:46:06 -070091bool IPConfig::RenewIP() {
92 return false;
93}
94
95bool IPConfig::ReleaseIP() {
Darin Petkove7cb7f82011-06-03 13:21:51 -070096 return false;
97}
98
Paul Stewart1062d9d2012-04-27 10:42:27 -070099void IPConfig::ApplyStaticIPParameters(
100 const StaticIPParameters &static_ip_parameters) {
101 static_ip_parameters.ApplyTo(&properties_);
102}
103
Chris Masone34af2182011-08-22 11:59:36 -0700104bool IPConfig::Load(StoreInterface *storage, const string &id_suffix) {
105 const string id = GetStorageIdentifier(id_suffix);
Chris Masone8a7b8be2011-07-22 12:43:37 -0700106 if (!storage->ContainsGroup(id)) {
107 LOG(WARNING) << "IPConfig is not available in the persistent store: " << id;
108 return false;
109 }
110 string local_type;
111 storage->GetString(id, kStorageType, &local_type);
112 return local_type == type();
113}
114
Chris Masone34af2182011-08-22 11:59:36 -0700115bool IPConfig::Save(StoreInterface *storage, const string &id_suffix) {
116 const string id = GetStorageIdentifier(id_suffix);
Chris Masone8a7b8be2011-07-22 12:43:37 -0700117 storage->SetString(id, kStorageType, type());
118 return true;
119}
120
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700121void IPConfig::UpdateProperties(const Properties &properties, bool success) {
Darin Petkove7cb7f82011-06-03 13:21:51 -0700122 properties_ = properties;
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500123 if (!update_callback_.is_null()) {
124 update_callback_.Run(this, success);
Darin Petkovefb09c32011-06-07 20:24:17 -0700125 }
126}
127
128void IPConfig::RegisterUpdateCallback(
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500129 const Callback<void(const IPConfigRefPtr&, bool)> &callback) {
130 update_callback_ = callback;
Darin Petkove7cb7f82011-06-03 13:21:51 -0700131}
132
Darin Petkove02b3ca2011-05-31 16:00:44 -0700133} // namespace shill