blob: 95de3c7c3a5bf8f39e9a2e9b7b50d4de0ab67594 [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
Samuel Tan815a6fb2014-10-23 16:53:59 -07007#include <sys/time.h>
8
Chris Masone43b48a12011-07-01 13:37:07 -07009#include <chromeos/dbus/service_constants.h>
Darin Petkove02b3ca2011-05-31 16:00:44 -070010
Chris Masonec6c6c132011-06-30 11:29:52 -070011#include "shill/adaptor_interfaces.h"
Chris Masone19e30402011-07-19 15:48:47 -070012#include "shill/control_interface.h"
Chris Masone43b48a12011-07-01 13:37:07 -070013#include "shill/error.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070014#include "shill/logging.h"
Peter Qiu8d6b5972014-10-28 15:33:34 -070015#include "shill/net/shill_time.h"
Paul Stewart1062d9d2012-04-27 10:42:27 -070016#include "shill/static_ip_parameters.h"
Chris Masonec6c6c132011-06-30 11:29:52 -070017
Eric Shienbrood3e20a232012-02-16 11:35:56 -050018using base::Callback;
Darin Petkove02b3ca2011-05-31 16:00:44 -070019using std::string;
20
21namespace shill {
22
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070023namespace Logging {
24static auto kModuleLogScope = ScopeLogger::kInet;
25static string ObjectID(IPConfig *i) { return i->GetRpcIdentifier(); }
26}
27
Samuel Tan815a6fb2014-10-23 16:53:59 -070028namespace {
29
30const time_t kDefaultLeaseExpirationTime = LONG_MAX;
31
32} // namespace
33
Chris Masone0756f232011-07-21 17:24:00 -070034// static
35const char IPConfig::kType[] = "ip";
Samuel Tan815a6fb2014-10-23 16:53:59 -070036
Chris Masone0756f232011-07-21 17:24:00 -070037// static
38uint IPConfig::global_serial_ = 0;
39
Chris Masone19e30402011-07-19 15:48:47 -070040IPConfig::IPConfig(ControlInterface *control_interface,
41 const std::string &device_name)
42 : device_name_(device_name),
Chris Masone0756f232011-07-21 17:24:00 -070043 type_(kType),
44 serial_(global_serial_++),
Chris Masone19e30402011-07-19 15:48:47 -070045 adaptor_(control_interface->CreateIPConfigAdaptor(this)) {
Chris Masone0756f232011-07-21 17:24:00 -070046 Init();
47}
48
49IPConfig::IPConfig(ControlInterface *control_interface,
50 const std::string &device_name,
51 const std::string &type)
52 : device_name_(device_name),
53 type_(type),
54 serial_(global_serial_++),
55 adaptor_(control_interface->CreateIPConfigAdaptor(this)) {
56 Init();
57}
58
59void IPConfig::Init() {
Ben Chan0295e0f2013-09-20 13:47:29 -070060 store_.RegisterConstString(kAddressProperty, &properties_.address);
61 store_.RegisterConstString(kBroadcastProperty,
Paul Stewart10241e32012-04-23 18:15:06 -070062 &properties_.broadcast_address);
Ben Chan0295e0f2013-09-20 13:47:29 -070063 store_.RegisterConstString(kDomainNameProperty, &properties_.domain_name);
64 store_.RegisterConstString(kGatewayProperty, &properties_.gateway);
65 store_.RegisterConstString(kMethodProperty, &properties_.method);
66 store_.RegisterConstInt32(kMtuProperty, &properties_.mtu);
67 store_.RegisterConstStrings(kNameServersProperty, &properties_.dns_servers);
68 store_.RegisterConstString(kPeerAddressProperty, &properties_.peer_address);
69 store_.RegisterConstInt32(kPrefixlenProperty, &properties_.subnet_prefix);
Paul Stewarta63f5212013-06-25 15:29:40 -070070 store_.RegisterConstStrings(kSearchDomainsProperty,
Christopher Wiley53b3f472012-09-17 09:43:39 -070071 &properties_.domain_search);
Paul Stewartc3fdba92013-12-02 11:12:38 -080072 store_.RegisterConstString(kVendorEncapsulatedOptionsProperty,
73 &properties_.vendor_encapsulated_options);
Paul Stewarta63f5212013-06-25 15:29:40 -070074 store_.RegisterConstString(kWebProxyAutoDiscoveryUrlProperty,
75 &properties_.web_proxy_auto_discovery);
Samuel Tan815a6fb2014-10-23 16:53:59 -070076 time_ = Time::GetInstance();
77 current_lease_expiration_time_ = {kDefaultLeaseExpirationTime, 0};
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070078 SLOG(this, 2) << __func__ << " device: " << device_name();
Darin Petkove02b3ca2011-05-31 16:00:44 -070079}
80
81IPConfig::~IPConfig() {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070082 SLOG(this, 2) << __func__ << " device: " << device_name();
Darin Petkove02b3ca2011-05-31 16:00:44 -070083}
84
Chris Masone4e851612011-07-01 10:46:53 -070085string IPConfig::GetRpcIdentifier() {
86 return adaptor_->GetRpcIdentifier();
87}
88
Darin Petkov92c43902011-06-09 20:46:06 -070089bool IPConfig::RequestIP() {
Darin Petkove7cb7f82011-06-03 13:21:51 -070090 return false;
91}
92
Darin Petkov92c43902011-06-09 20:46:06 -070093bool IPConfig::RenewIP() {
94 return false;
95}
96
Paul Stewart217c61d2013-06-13 15:12:02 -070097bool IPConfig::ReleaseIP(ReleaseReason reason) {
Darin Petkove7cb7f82011-06-03 13:21:51 -070098 return false;
99}
100
Paul Stewart4558bda2012-08-03 10:44:10 -0700101void IPConfig::Refresh(Error */*error*/) {
Paul Stewart82236532013-12-10 15:33:11 -0800102 if (!refresh_callback_.is_null()) {
103 refresh_callback_.Run(this);
104 }
Paul Stewart4558bda2012-08-03 10:44:10 -0700105 RenewIP();
106}
107
Paul Stewart1062d9d2012-04-27 10:42:27 -0700108void IPConfig::ApplyStaticIPParameters(
Paul Stewartdef189e2012-08-02 20:12:09 -0700109 StaticIPParameters *static_ip_parameters) {
110 static_ip_parameters->ApplyTo(&properties_);
mukesh agrawal7aed61c2013-04-22 16:01:24 -0700111 EmitChanges();
Paul Stewart1062d9d2012-04-27 10:42:27 -0700112}
113
Paul Stewart82236532013-12-10 15:33:11 -0800114void IPConfig::RestoreSavedIPParameters(
115 StaticIPParameters *static_ip_parameters) {
116 static_ip_parameters->RestoreTo(&properties_);
117 EmitChanges();
118}
119
Samuel Tan815a6fb2014-10-23 16:53:59 -0700120void IPConfig::UpdateLeaseExpirationTime(uint32_t new_lease_duration) {
121 struct timeval new_expiration_time;
122 time_->GetTimeBoottime(&new_expiration_time);
123 new_expiration_time.tv_sec += new_lease_duration;
124 current_lease_expiration_time_ = new_expiration_time;
125}
126
127void IPConfig::ResetLeaseExpirationTime() {
128 current_lease_expiration_time_ = {kDefaultLeaseExpirationTime, 0};
129}
130
131bool IPConfig::TimeToLeaseExpiry(uint32_t *time_left) {
132 if (current_lease_expiration_time_.tv_sec == kDefaultLeaseExpirationTime) {
133 LOG(ERROR) << __func__ << ": "
134 << "No current DHCP lease";
135 return false;
136 }
137 struct timeval now;
138 time_->GetTimeBoottime(&now);
139 if (now.tv_sec > current_lease_expiration_time_.tv_sec) {
140 LOG(ERROR) << __func__ << ": "
141 << "Current DHCP lease has already expired";
142 return false;
143 }
144 *time_left = current_lease_expiration_time_.tv_sec - now.tv_sec;
145 return true;
146}
147
Samuel Tan3c3c36a2014-12-16 16:53:19 -0800148void IPConfig::UpdateProperties(const Properties &properties,
149 bool new_lease_acquired) {
mukesh agrawal1c1dd352013-05-08 15:58:34 -0700150 // Take a reference of this instance to make sure we don't get destroyed in
151 // the middle of this call. (The |update_callback_| may cause a reference
152 // to be dropped. See, e.g., EthernetService::Disconnect and
153 // Ethernet::DropConnection.)
154 IPConfigRefPtr me = this;
Paul Stewartc5099532013-12-12 07:53:15 -0800155
Darin Petkove7cb7f82011-06-03 13:21:51 -0700156 properties_ = properties;
Paul Stewartc5099532013-12-12 07:53:15 -0800157
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500158 if (!update_callback_.is_null()) {
Samuel Tan3c3c36a2014-12-16 16:53:19 -0800159 update_callback_.Run(this, new_lease_acquired);
Darin Petkovefb09c32011-06-07 20:24:17 -0700160 }
mukesh agrawal7aed61c2013-04-22 16:01:24 -0700161 EmitChanges();
Darin Petkovefb09c32011-06-07 20:24:17 -0700162}
163
Peter Qiua89154b2014-05-23 15:45:42 -0700164void IPConfig::UpdateDNSServers(const std::vector<std::string> &dns_servers) {
165 properties_.dns_servers = dns_servers;
166 EmitChanges();
167}
168
Paul Stewartc5099532013-12-12 07:53:15 -0800169void IPConfig::NotifyFailure() {
170 // Take a reference of this instance to make sure we don't get destroyed in
171 // the middle of this call. (The |update_callback_| may cause a reference
172 // to be dropped. See, e.g., EthernetService::Disconnect and
173 // Ethernet::DropConnection.)
174 IPConfigRefPtr me = this;
175
176 if (!failure_callback_.is_null()) {
177 failure_callback_.Run(this);
178 }
179}
180
Paul Stewart1f916e42013-12-23 09:52:54 -0800181void IPConfig::NotifyExpiry() {
182 if (!expire_callback_.is_null()) {
183 expire_callback_.Run(this);
184 }
185}
186
Samuel Tan3c3c36a2014-12-16 16:53:19 -0800187void IPConfig::RegisterUpdateCallback(const UpdateCallback &callback) {
Paul Stewartc5099532013-12-12 07:53:15 -0800188 update_callback_ = callback;
189}
190
191void IPConfig::RegisterFailureCallback(const Callback &callback) {
192 failure_callback_ = callback;
193}
194
195void IPConfig::RegisterRefreshCallback(const Callback &callback) {
Paul Stewart82236532013-12-10 15:33:11 -0800196 refresh_callback_ = callback;
197}
198
Paul Stewart1f916e42013-12-23 09:52:54 -0800199void IPConfig::RegisterExpireCallback(const Callback &callback) {
200 expire_callback_ = callback;
201}
202
Paul Stewartc5099532013-12-12 07:53:15 -0800203void IPConfig::ResetProperties() {
204 properties_ = Properties();
205 EmitChanges();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700206}
207
mukesh agrawal7aed61c2013-04-22 16:01:24 -0700208void IPConfig::EmitChanges() {
Ben Chan0295e0f2013-09-20 13:47:29 -0700209 adaptor_->EmitStringChanged(kAddressProperty, properties_.address);
210 adaptor_->EmitStringsChanged(kNameServersProperty, properties_.dns_servers);
mukesh agrawal7aed61c2013-04-22 16:01:24 -0700211}
212
Darin Petkove02b3ca2011-05-31 16:00:44 -0700213} // namespace shill