mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 5 | #include "shill/device.h" |
| 6 | |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 7 | #include <netinet/in.h> |
| 8 | #include <linux/if.h> // Needs definitions from netinet/in.h |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 9 | #include <stdio.h> |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 10 | #include <time.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 11 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 12 | #include <string> |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 13 | #include <vector> |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 14 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 15 | #include <base/bind.h> |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 16 | #include <base/file_util.h> |
Chris Masone | 487b8bf | 2011-05-13 16:27:57 -0700 | [diff] [blame] | 17 | #include <base/memory/ref_counted.h> |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 18 | #include <base/strings/stringprintf.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 19 | #include <chromeos/dbus/service_constants.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 20 | |
Arman Uguray | f84a424 | 2013-04-09 20:01:07 -0700 | [diff] [blame] | 21 | #include "shill/async_connection.h" |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 22 | #include "shill/connection.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 23 | #include "shill/control_interface.h" |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 24 | #include "shill/device_dbus_adaptor.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 25 | #include "shill/dhcp_config.h" |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 26 | #include "shill/dhcp_provider.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 27 | #include "shill/error.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 28 | #include "shill/event_dispatcher.h" |
Gaurav Shah | 6d2c72d | 2012-10-16 16:30:44 -0700 | [diff] [blame] | 29 | #include "shill/geolocation_info.h" |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 30 | #include "shill/http_proxy.h" |
Arman Uguray | f84a424 | 2013-04-09 20:01:07 -0700 | [diff] [blame] | 31 | #include "shill/ip_address.h" |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 32 | #include "shill/link_monitor.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 33 | #include "shill/logging.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 34 | #include "shill/manager.h" |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 35 | #include "shill/metrics.h" |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 36 | #include "shill/property_accessor.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 37 | #include "shill/refptr_types.h" |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 38 | #include "shill/rtnl_handler.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 39 | #include "shill/service.h" |
Arman Uguray | f84a424 | 2013-04-09 20:01:07 -0700 | [diff] [blame] | 40 | #include "shill/socket_info_reader.h" |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 41 | #include "shill/store_interface.h" |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 42 | #include "shill/technology.h" |
Paul Stewart | fa11e28 | 2013-12-02 22:04:25 -0800 | [diff] [blame] | 43 | #include "shill/tethering.h" |
Peter Qiu | dc335f8 | 2014-05-15 10:33:17 -0700 | [diff] [blame] | 44 | #include "shill/traffic_monitor.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 45 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 46 | using base::Bind; |
Peter Qiu | 6f5618b | 2014-06-05 15:19:01 -0700 | [diff] [blame] | 47 | using base::Callback; |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 48 | using base::FilePath; |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 49 | using base::StringPrintf; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 50 | using std::string; |
| 51 | using std::vector; |
| 52 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 53 | namespace shill { |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 54 | |
| 55 | // static |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 56 | const char Device::kIPFlagTemplate[] = "/proc/sys/net/%s/conf/%s/%s"; |
| 57 | // static |
| 58 | const char Device::kIPFlagVersion4[] = "ipv4"; |
| 59 | // static |
| 60 | const char Device::kIPFlagVersion6[] = "ipv6"; |
| 61 | // static |
| 62 | const char Device::kIPFlagDisableIPv6[] = "disable_ipv6"; |
| 63 | // static |
| 64 | const char Device::kIPFlagUseTempAddr[] = "use_tempaddr"; |
| 65 | // static |
| 66 | const char Device::kIPFlagUseTempAddrUsedAndDefault[] = "2"; |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 67 | // static |
| 68 | const char Device::kIPFlagReversePathFilter[] = "rp_filter"; |
| 69 | // static |
| 70 | const char Device::kIPFlagReversePathFilterEnabled[] = "1"; |
| 71 | // static |
| 72 | const char Device::kIPFlagReversePathFilterLooseMode[] = "2"; |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 73 | // static |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 74 | const char Device::kStoragePowered[] = "Powered"; |
| 75 | // static |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 76 | const char Device::kStorageReceiveByteCount[] = "ReceiveByteCount"; |
| 77 | // static |
| 78 | const char Device::kStorageTransmitByteCount[] = "TransmitByteCount"; |
Peter Qiu | b9256f3 | 2014-05-09 15:27:29 -0700 | [diff] [blame] | 79 | // static |
| 80 | const char Device::kFallbackDnsTestHostname[] = "www.gstatic.com"; |
| 81 | // static |
| 82 | const char* Device::kFallbackDnsServers[] = { |
| 83 | "8.8.8.8", |
Peter Qiu | a89154b | 2014-05-23 15:45:42 -0700 | [diff] [blame] | 84 | "8.8.4.4" |
Peter Qiu | b9256f3 | 2014-05-09 15:27:29 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | // static |
| 88 | const int Device::kDNSTimeoutMilliseconds = 5000; |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 89 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 90 | Device::Device(ControlInterface *control_interface, |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 91 | EventDispatcher *dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 92 | Metrics *metrics, |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 93 | Manager *manager, |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 94 | const string &link_name, |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 95 | const string &address, |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 96 | int interface_index, |
| 97 | Technology::Identifier technology) |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 98 | : enabled_(false), |
| 99 | enabled_persistent_(true), |
| 100 | enabled_pending_(enabled_), |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 101 | reconnect_(true), |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 102 | hardware_address_(address), |
mukesh agrawal | f60e406 | 2011-05-27 13:13:41 -0700 | [diff] [blame] | 103 | interface_index_(interface_index), |
| 104 | running_(false), |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 105 | link_name_(link_name), |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 106 | unique_id_(link_name), |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 107 | control_interface_(control_interface), |
| 108 | dispatcher_(dispatcher), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 109 | metrics_(metrics), |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 110 | manager_(manager), |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 111 | weak_ptr_factory_(this), |
Darin Petkov | 77cb681 | 2011-08-15 16:19:41 -0700 | [diff] [blame] | 112 | adaptor_(control_interface->CreateDeviceAdaptor(this)), |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 113 | portal_detector_callback_(Bind(&Device::PortalDetectorCallback, |
| 114 | weak_ptr_factory_.GetWeakPtr())), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 115 | technology_(technology), |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 116 | portal_attempts_to_online_(0), |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 117 | receive_byte_offset_(0), |
| 118 | transmit_byte_offset_(0), |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 119 | dhcp_provider_(DHCPProvider::GetInstance()), |
| 120 | rtnl_handler_(RTNLHandler::GetInstance()) { |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 121 | store_.RegisterConstString(kAddressProperty, &hardware_address_); |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 122 | |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 123 | // kBgscanMethodProperty: Registered in WiFi |
| 124 | // kBgscanShortIntervalProperty: Registered in WiFi |
| 125 | // kBgscanSignalThresholdProperty: Registered in WiFi |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 126 | |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 127 | // kCellularAllowRoamingProperty: Registered in Cellular |
| 128 | // kCarrierProperty: Registered in Cellular |
| 129 | // kEsnProperty: Registered in Cellular |
| 130 | // kHomeProviderProperty: Registered in Cellular |
| 131 | // kImeiProperty: Registered in Cellular |
| 132 | // kIccidProperty: Registered in Cellular |
| 133 | // kImsiProperty: Registered in Cellular |
| 134 | // kManufacturerProperty: Registered in Cellular |
| 135 | // kMdnProperty: Registered in Cellular |
| 136 | // kMeidProperty: Registered in Cellular |
| 137 | // kMinProperty: Registered in Cellular |
| 138 | // kModelIDProperty: Registered in Cellular |
| 139 | // kFirmwareRevisionProperty: Registered in Cellular |
| 140 | // kHardwareRevisionProperty: Registered in Cellular |
| 141 | // kPRLVersionProperty: Registered in Cellular |
| 142 | // kSIMLockStatusProperty: Registered in Cellular |
| 143 | // kFoundNetworksProperty: Registered in Cellular |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 144 | // kDBusObjectProperty: Register in Cellular |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 145 | |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 146 | store_.RegisterConstString(kInterfaceProperty, &link_name_); |
| 147 | HelpRegisterConstDerivedRpcIdentifiers(kIPConfigsProperty, |
Jason Glasgow | 08afdff | 2012-04-03 10:22:26 -0400 | [diff] [blame] | 148 | &Device::AvailableIPConfigs); |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 149 | store_.RegisterConstString(kNameProperty, &link_name_); |
| 150 | store_.RegisterConstBool(kPoweredProperty, &enabled_); |
| 151 | HelpRegisterConstDerivedString(kTypeProperty, |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 152 | &Device::GetTechnologyString); |
Ben Chan | 39a7beb | 2013-09-21 11:28:00 -0700 | [diff] [blame] | 153 | HelpRegisterConstDerivedUint64(kLinkMonitorResponseTimeProperty, |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 154 | &Device::GetLinkMonitorResponseTime); |
Jason Glasgow | b579005 | 2012-01-27 01:03:52 -0500 | [diff] [blame] | 155 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 156 | // TODO(cmasone): Chrome doesn't use this...does anyone? |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 157 | // store_.RegisterConstBool(kReconnectProperty, &reconnect_); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 158 | |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 159 | // TODO(cmasone): Figure out what shill concept maps to flimflam's "Network". |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 160 | // known_properties_.push_back(kNetworksProperty); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 161 | |
Wade Guthrie | 227c774 | 2013-10-10 11:06:33 -0700 | [diff] [blame] | 162 | // kRoamThresholdProperty: Registered in WiFi |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 163 | // kScanningProperty: Registered in WiFi, Cellular |
| 164 | // kScanIntervalProperty: Registered in WiFi, Cellular |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 165 | |
| 166 | if (manager_ && manager_->device_info()) { // Unit tests may not have these. |
| 167 | manager_->device_info()->GetByteCounts( |
| 168 | interface_index_, &receive_byte_offset_, &transmit_byte_offset_); |
Ben Chan | 39a7beb | 2013-09-21 11:28:00 -0700 | [diff] [blame] | 169 | HelpRegisterConstDerivedUint64(kReceiveByteCountProperty, |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 170 | &Device::GetReceiveByteCountProperty); |
Ben Chan | 39a7beb | 2013-09-21 11:28:00 -0700 | [diff] [blame] | 171 | HelpRegisterConstDerivedUint64(kTransmitByteCountProperty, |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 172 | &Device::GetTransmitByteCountProperty); |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Darin Petkov | a0a0efe | 2012-06-27 12:50:01 +0200 | [diff] [blame] | 175 | LOG(INFO) << "Device created: " << link_name_ |
| 176 | << " index " << interface_index_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | Device::~Device() { |
Darin Petkov | a0a0efe | 2012-06-27 12:50:01 +0200 | [diff] [blame] | 180 | LOG(INFO) << "Device destructed: " << link_name_ |
| 181 | << " index " << interface_index_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 184 | void Device::LinkEvent(unsigned flags, unsigned change) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 185 | SLOG(Device, 2) << "Device " << link_name_ |
| 186 | << std::showbase << std::hex |
| 187 | << " flags " << flags << " changed " << change |
| 188 | << std::dec << std::noshowbase; |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Wade Guthrie | 4823f4f | 2013-07-25 10:03:03 -0700 | [diff] [blame] | 191 | void Device::Scan(ScanType scan_type, Error *error, const string &reason) { |
| 192 | SLOG(Device, 2) << __func__ << " [Device] on " << link_name() << " from " |
| 193 | << reason; |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 194 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 195 | "Device doesn't support scan."); |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 198 | void Device::RegisterOnNetwork(const std::string &/*network_id*/, Error *error, |
| 199 | const ResultCallback &/*callback*/) { |
| 200 | Error::PopulateAndLog(error, Error::kNotSupported, |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 201 | "Device doesn't support network registration."); |
Darin Petkov | 9ae310f | 2011-08-30 15:41:13 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Darin Petkov | c64fe5e | 2012-01-11 12:46:13 +0100 | [diff] [blame] | 204 | void Device::RequirePIN( |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 205 | const string &/*pin*/, bool /*require*/, |
| 206 | Error *error, const ResultCallback &/*callback*/) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 207 | SLOG(Device, 2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 208 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 209 | "Device doesn't support RequirePIN."); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 212 | void Device::EnterPIN(const string &/*pin*/, |
| 213 | Error *error, const ResultCallback &/*callback*/) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 214 | SLOG(Device, 2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 215 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 216 | "Device doesn't support EnterPIN."); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 217 | } |
| 218 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 219 | void Device::UnblockPIN(const string &/*unblock_code*/, |
| 220 | const string &/*pin*/, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 221 | Error *error, const ResultCallback &/*callback*/) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 222 | SLOG(Device, 2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 223 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 224 | "Device doesn't support UnblockPIN."); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 225 | } |
| 226 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 227 | void Device::ChangePIN(const string &/*old_pin*/, |
| 228 | const string &/*new_pin*/, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 229 | Error *error, const ResultCallback &/*callback*/) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 230 | SLOG(Device, 2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 231 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 232 | "Device doesn't support ChangePIN."); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Ben Chan | ad663e1 | 2013-01-08 01:58:47 -0800 | [diff] [blame] | 235 | void Device::Reset(Error *error, const ResultCallback &/*callback*/) { |
| 236 | SLOG(Device, 2) << __func__; |
| 237 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 238 | "Device doesn't support Reset."); |
| 239 | } |
| 240 | |
Darin Petkov | c37a9c4 | 2012-09-06 15:28:22 +0200 | [diff] [blame] | 241 | void Device::SetCarrier(const string &/*carrier*/, |
| 242 | Error *error, const ResultCallback &/*callback*/) { |
| 243 | SLOG(Device, 2) << __func__; |
| 244 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 245 | "Device doesn't support SetCarrier."); |
| 246 | } |
| 247 | |
Ben Chan | bcc6e01 | 2013-11-04 14:28:37 -0800 | [diff] [blame] | 248 | bool Device::IsIPv6Allowed() const { |
| 249 | return true; |
| 250 | } |
| 251 | |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 252 | void Device::DisableIPv6() { |
Ben Chan | bcc6e01 | 2013-11-04 14:28:37 -0800 | [diff] [blame] | 253 | SLOG(Device, 2) << __func__; |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 254 | SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "1"); |
| 255 | } |
| 256 | |
| 257 | void Device::EnableIPv6() { |
Ben Chan | bcc6e01 | 2013-11-04 14:28:37 -0800 | [diff] [blame] | 258 | SLOG(Device, 2) << __func__; |
| 259 | if (!IsIPv6Allowed()) { |
| 260 | LOG(INFO) << "Skip enabling IPv6 on " << link_name_ |
| 261 | << " as it is not allowed."; |
| 262 | return; |
| 263 | } |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 264 | SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "0"); |
| 265 | } |
| 266 | |
| 267 | void Device::EnableIPv6Privacy() { |
| 268 | SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagUseTempAddr, |
| 269 | kIPFlagUseTempAddrUsedAndDefault); |
| 270 | } |
| 271 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 272 | void Device::DisableReversePathFilter() { |
| 273 | // TODO(pstew): Current kernel doesn't offer reverse-path filtering flag |
Paul Stewart | ee6b3d7 | 2013-07-12 16:07:51 -0700 | [diff] [blame] | 274 | // for IPv6. crbug.com/207193 |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 275 | SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter, |
| 276 | kIPFlagReversePathFilterLooseMode); |
| 277 | } |
| 278 | |
| 279 | void Device::EnableReversePathFilter() { |
| 280 | SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter, |
| 281 | kIPFlagReversePathFilterEnabled); |
| 282 | } |
| 283 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 284 | bool Device::IsConnected() const { |
| 285 | if (selected_service_) |
| 286 | return selected_service_->IsConnected(); |
| 287 | return false; |
| 288 | } |
| 289 | |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 290 | bool Device::IsConnectedToService(const ServiceRefPtr &service) const { |
| 291 | return service == selected_service_ && IsConnected(); |
| 292 | } |
| 293 | |
Paul Stewart | fa11e28 | 2013-12-02 22:04:25 -0800 | [diff] [blame] | 294 | bool Device::IsConnectedViaTether() const { |
| 295 | return |
| 296 | ipconfig_.get() && |
| 297 | ipconfig_->properties().vendor_encapsulated_options == |
| 298 | Tethering::kAndroidVendorEncapsulatedOptions; |
| 299 | } |
| 300 | |
mukesh agrawal | f6b3209 | 2013-04-10 15:49:55 -0700 | [diff] [blame] | 301 | string Device::GetRpcIdentifier() const { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 302 | return adaptor_->GetRpcIdentifier(); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 305 | string Device::GetStorageIdentifier() { |
| 306 | string id = GetRpcIdentifier(); |
| 307 | ControlInterface::RpcIdToStorageId(&id); |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 308 | size_t needle = id.find('_'); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 309 | DLOG_IF(ERROR, needle == string::npos) << "No _ in storage id?!?!"; |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 310 | id.replace(id.begin() + needle + 1, id.end(), hardware_address_); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 311 | return id; |
| 312 | } |
| 313 | |
Gaurav Shah | 6d2c72d | 2012-10-16 16:30:44 -0700 | [diff] [blame] | 314 | vector<GeolocationInfo> Device::GetGeolocationObjects() const { |
| 315 | return vector<GeolocationInfo>(); |
Wade Guthrie | 227c774 | 2013-10-10 11:06:33 -0700 | [diff] [blame] | 316 | } |
Gaurav Shah | 6d2c72d | 2012-10-16 16:30:44 -0700 | [diff] [blame] | 317 | |
Jason Glasgow | b579005 | 2012-01-27 01:03:52 -0500 | [diff] [blame] | 318 | string Device::GetTechnologyString(Error */*error*/) { |
| 319 | return Technology::NameFromIdentifier(technology()); |
| 320 | } |
| 321 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 322 | const string& Device::FriendlyName() const { |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 323 | return link_name_; |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 326 | const string& Device::UniqueName() const { |
| 327 | return unique_id_; |
| 328 | } |
| 329 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 330 | bool Device::Load(StoreInterface *storage) { |
| 331 | const string id = GetStorageIdentifier(); |
| 332 | if (!storage->ContainsGroup(id)) { |
| 333 | LOG(WARNING) << "Device is not available in the persistent store: " << id; |
| 334 | return false; |
| 335 | } |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 336 | enabled_persistent_ = true; |
| 337 | storage->GetBool(id, kStoragePowered, &enabled_persistent_); |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 338 | uint64 rx_byte_count = 0, tx_byte_count = 0; |
| 339 | |
| 340 | manager_->device_info()->GetByteCounts( |
| 341 | interface_index_, &rx_byte_count, &tx_byte_count); |
| 342 | // If there is a byte-count present in the profile, the return value |
| 343 | // of Device::Get*ByteCount() should be the this stored value plus |
| 344 | // whatever additional bytes we receive since time-of-load. We |
| 345 | // accomplish this by the subtractions below, which can validly |
| 346 | // roll over "negative" in the subtractions below and in Get*ByteCount. |
| 347 | uint64 profile_byte_count; |
| 348 | if (storage->GetUint64(id, kStorageReceiveByteCount, &profile_byte_count)) { |
| 349 | receive_byte_offset_ = rx_byte_count - profile_byte_count; |
| 350 | } |
| 351 | if (storage->GetUint64(id, kStorageTransmitByteCount, &profile_byte_count)) { |
| 352 | transmit_byte_offset_ = tx_byte_count - profile_byte_count; |
| 353 | } |
| 354 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 355 | return true; |
| 356 | } |
| 357 | |
| 358 | bool Device::Save(StoreInterface *storage) { |
| 359 | const string id = GetStorageIdentifier(); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 360 | storage->SetBool(id, kStoragePowered, enabled_persistent_); |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 361 | storage->SetUint64(id, kStorageReceiveByteCount, GetReceiveByteCount()); |
| 362 | storage->SetUint64(id, kStorageTransmitByteCount, GetTransmitByteCount()); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 363 | return true; |
| 364 | } |
| 365 | |
mukesh agrawal | 784566d | 2012-08-08 18:32:58 -0700 | [diff] [blame] | 366 | void Device::OnBeforeSuspend() { |
| 367 | // Nothing to be done in the general case. |
| 368 | } |
| 369 | |
| 370 | void Device::OnAfterResume() { |
| 371 | if (ipconfig_) { |
| 372 | SLOG(Device, 3) << "Renewing IP address on resume."; |
| 373 | ipconfig_->RenewIP(); |
| 374 | } |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 375 | if (link_monitor_) { |
| 376 | SLOG(Device, 3) << "Informing Link Monitor of resume."; |
| 377 | link_monitor_->OnAfterResume(); |
| 378 | } |
mukesh agrawal | 784566d | 2012-08-08 18:32:58 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Darin Petkov | 2b8e44e | 2012-06-25 15:13:26 +0200 | [diff] [blame] | 381 | void Device::DropConnection() { |
| 382 | SLOG(Device, 2) << __func__; |
| 383 | DestroyIPConfig(); |
| 384 | SelectService(NULL); |
| 385 | } |
| 386 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 387 | void Device::DestroyIPConfig() { |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 388 | DisableIPv6(); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 389 | if (ipconfig_.get()) { |
Paul Stewart | 217c61d | 2013-06-13 15:12:02 -0700 | [diff] [blame] | 390 | ipconfig_->ReleaseIP(IPConfig::kReleaseReasonDisconnect); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 391 | ipconfig_ = NULL; |
Paul Stewart | d4f2648 | 2014-04-25 19:12:03 -0700 | [diff] [blame] | 392 | UpdateIPConfigsProperty(); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 393 | } |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 394 | DestroyConnection(); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Paul Stewart | d4f2648 | 2014-04-25 19:12:03 -0700 | [diff] [blame] | 397 | void Device::OnIPv6AddressChanged() { |
| 398 | IPAddress address(IPAddress::kFamilyIPv6); |
| 399 | if (!manager_->device_info()->GetPrimaryIPv6Address( |
| 400 | interface_index_, &address)) { |
| 401 | if (ip6config_) { |
| 402 | ip6config_ = NULL; |
| 403 | UpdateIPConfigsProperty(); |
| 404 | } |
| 405 | return; |
| 406 | } |
| 407 | |
| 408 | IPConfig::Properties properties; |
| 409 | if (!address.IntoString(&properties.address)) { |
| 410 | LOG(ERROR) << "Unable to convert IPv6 address into a string!"; |
| 411 | return; |
| 412 | } |
| 413 | properties.subnet_prefix = address.prefix(); |
| 414 | |
| 415 | if (!ip6config_) { |
| 416 | ip6config_ = new IPConfig(control_interface_, link_name_); |
| 417 | } else if (properties.address == ip6config_->properties().address && |
| 418 | properties.subnet_prefix == |
| 419 | ip6config_->properties().subnet_prefix) { |
| 420 | SLOG(Device, 2) << __func__ << " primary address for " |
| 421 | << link_name_ << " is unchanged."; |
| 422 | return; |
| 423 | } |
| 424 | |
| 425 | properties.address_family = IPAddress::kFamilyIPv6; |
| 426 | properties.method = kTypeIPv6; |
| 427 | ip6config_->set_properties(properties); |
| 428 | UpdateIPConfigsProperty(); |
| 429 | } |
| 430 | |
Arman Uguray | ed8e610 | 2012-11-29 14:47:20 -0800 | [diff] [blame] | 431 | bool Device::ShouldUseArpGateway() const { |
| 432 | return false; |
| 433 | } |
| 434 | |
Paul Stewart | 316acef | 2014-05-29 18:40:48 -0700 | [diff] [blame] | 435 | bool Device::IsUsingStaticIP() const { |
| 436 | if (!selected_service_) { |
| 437 | return false; |
| 438 | } |
| 439 | return selected_service_->HasStaticIPAddress(); |
| 440 | } |
| 441 | |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 442 | bool Device::AcquireIPConfig() { |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 443 | return AcquireIPConfigWithLeaseName(string()); |
| 444 | } |
| 445 | |
| 446 | bool Device::AcquireIPConfigWithLeaseName(const string &lease_name) { |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 447 | DestroyIPConfig(); |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 448 | EnableIPv6(); |
Arman Uguray | ed8e610 | 2012-11-29 14:47:20 -0800 | [diff] [blame] | 449 | bool arp_gateway = manager_->GetArpGateway() && ShouldUseArpGateway(); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 450 | ipconfig_ = dhcp_provider_->CreateConfig(link_name_, |
| 451 | manager_->GetHostName(), |
| 452 | lease_name, |
Paul Stewart | b108318 | 2014-06-25 03:04:53 -0700 | [diff] [blame] | 453 | arp_gateway); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 454 | ipconfig_->RegisterUpdateCallback(Bind(&Device::OnIPConfigUpdated, |
| 455 | weak_ptr_factory_.GetWeakPtr())); |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 456 | ipconfig_->RegisterFailureCallback(Bind(&Device::OnIPConfigFailed, |
| 457 | weak_ptr_factory_.GetWeakPtr())); |
Paul Stewart | 8223653 | 2013-12-10 15:33:11 -0800 | [diff] [blame] | 458 | ipconfig_->RegisterRefreshCallback(Bind(&Device::OnIPConfigRefreshed, |
| 459 | weak_ptr_factory_.GetWeakPtr())); |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame] | 460 | ipconfig_->RegisterExpireCallback(Bind(&Device::OnIPConfigExpired, |
| 461 | weak_ptr_factory_.GetWeakPtr())); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 462 | dispatcher_->PostTask(Bind(&Device::ConfigureStaticIPTask, |
| 463 | weak_ptr_factory_.GetWeakPtr())); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 464 | return ipconfig_->RequestIP(); |
| 465 | } |
| 466 | |
Ben Chan | 539ab02 | 2014-02-03 16:34:57 -0800 | [diff] [blame] | 467 | void Device::AssignIPConfig(const IPConfig::Properties &properties) { |
| 468 | DestroyIPConfig(); |
| 469 | EnableIPv6(); |
| 470 | ipconfig_ = new IPConfig(control_interface_, link_name_); |
| 471 | ipconfig_->set_properties(properties); |
| 472 | dispatcher_->PostTask(Bind(&Device::OnIPConfigUpdated, |
| 473 | weak_ptr_factory_.GetWeakPtr(), ipconfig_)); |
| 474 | } |
| 475 | |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 476 | void Device::DestroyIPConfigLease(const string &name) { |
| 477 | dhcp_provider_->DestroyLease(name); |
| 478 | } |
| 479 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 480 | void Device::HelpRegisterConstDerivedString( |
Jason Glasgow | b579005 | 2012-01-27 01:03:52 -0500 | [diff] [blame] | 481 | const string &name, |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 482 | string(Device::*get)(Error *error)) { |
Jason Glasgow | b579005 | 2012-01-27 01:03:52 -0500 | [diff] [blame] | 483 | store_.RegisterDerivedString( |
| 484 | name, |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 485 | StringAccessor(new CustomAccessor<Device, string>(this, get, NULL))); |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Jason Glasgow | 08afdff | 2012-04-03 10:22:26 -0400 | [diff] [blame] | 488 | void Device::HelpRegisterConstDerivedRpcIdentifiers( |
| 489 | const string &name, |
| 490 | RpcIdentifiers(Device::*get)(Error *)) { |
| 491 | store_.RegisterDerivedRpcIdentifiers( |
| 492 | name, |
| 493 | RpcIdentifiersAccessor( |
| 494 | new CustomAccessor<Device, RpcIdentifiers>(this, get, NULL))); |
| 495 | } |
| 496 | |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 497 | void Device::HelpRegisterConstDerivedUint64( |
| 498 | const string &name, |
| 499 | uint64(Device::*get)(Error *)) { |
| 500 | store_.RegisterDerivedUint64( |
| 501 | name, |
| 502 | Uint64Accessor( |
| 503 | new CustomAccessor<Device, uint64>(this, get, NULL))); |
| 504 | } |
| 505 | |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 506 | void Device::ConfigureStaticIPTask() { |
| 507 | SLOG(Device, 2) << __func__ << " selected_service " << selected_service_.get() |
| 508 | << " ipconfig " << ipconfig_.get(); |
| 509 | |
| 510 | if (!selected_service_ || !ipconfig_) { |
| 511 | return; |
| 512 | } |
| 513 | |
Paul Stewart | 316acef | 2014-05-29 18:40:48 -0700 | [diff] [blame] | 514 | if (IsUsingStaticIP()) { |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 515 | SLOG(Device, 2) << __func__ << " " << " configuring static IP parameters."; |
| 516 | // If the parameters contain an IP address, apply them now and bring |
| 517 | // the interface up. When DHCP information arrives, it will supplement |
| 518 | // the static information. |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 519 | OnIPConfigUpdated(ipconfig_); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 520 | } else { |
Paul Stewart | 8223653 | 2013-12-10 15:33:11 -0800 | [diff] [blame] | 521 | // Either |ipconfig_| has just been created in AcquireIPConfig() or |
| 522 | // we're being called by OnIPConfigRefreshed(). In either case a |
| 523 | // DHCP client has been started, and will take care of calling |
| 524 | // OnIPConfigUpdated() when it completes. |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 525 | SLOG(Device, 2) << __func__ << " " << " no static IP address."; |
| 526 | } |
| 527 | } |
| 528 | |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 529 | void Device::OnIPConfigUpdated(const IPConfigRefPtr &ipconfig) { |
| 530 | SLOG(Device, 2) << __func__; |
| 531 | CreateConnection(); |
| 532 | if (selected_service_) { |
| 533 | ipconfig->ApplyStaticIPParameters( |
| 534 | selected_service_->mutable_static_ip_parameters()); |
Paul Stewart | 316acef | 2014-05-29 18:40:48 -0700 | [diff] [blame] | 535 | if (IsUsingStaticIP()) { |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 536 | // If we are using a statically configured IP address instead |
| 537 | // of a leased IP address, release any acquired lease so it may |
| 538 | // be used by others. This allows us to merge other non-leased |
| 539 | // parameters (like DNS) when they're available from a DHCP server |
| 540 | // and not overridden by static parameters, but at the same time |
| 541 | // we avoid taking up a dynamic IP address the DHCP server could |
| 542 | // assign to someone else who might actually use it. |
| 543 | ipconfig->ReleaseIP(IPConfig::kReleaseReasonStaticIP); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 544 | } |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 545 | } |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 546 | connection_->UpdateFromIPConfig(ipconfig); |
| 547 | // SetConnection must occur after the UpdateFromIPConfig so the |
| 548 | // service can use the values derived from the connection. |
| 549 | if (selected_service_) { |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 550 | selected_service_->SetConnection(connection_); |
| 551 | } |
| 552 | // The service state change needs to happen last, so that at the |
| 553 | // time we report the state change to the manager, the service |
| 554 | // has its connection. |
| 555 | SetServiceState(Service::kStateConnected); |
| 556 | OnConnected(); |
| 557 | portal_attempts_to_online_ = 0; |
| 558 | // Subtle: Start portal detection after transitioning the service |
| 559 | // to the Connected state because this call may immediately transition |
| 560 | // to the Online state. |
| 561 | if (selected_service_) { |
| 562 | StartPortalDetection(); |
| 563 | } |
| 564 | StartLinkMonitor(); |
Peter Qiu | dc335f8 | 2014-05-15 10:33:17 -0700 | [diff] [blame] | 565 | StartTrafficMonitor(); |
Paul Stewart | d4f2648 | 2014-04-25 19:12:03 -0700 | [diff] [blame] | 566 | UpdateIPConfigsProperty(); |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | void Device::OnIPConfigFailed(const IPConfigRefPtr &ipconfig) { |
| 570 | SLOG(Device, 2) << __func__; |
| 571 | // TODO(pstew): This logic gets yet more complex when multiple |
| 572 | // IPConfig types are run in parallel (e.g. DHCP and DHCP6) |
| 573 | if (selected_service_) { |
Paul Stewart | 316acef | 2014-05-29 18:40:48 -0700 | [diff] [blame] | 574 | if (IsUsingStaticIP()) { |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 575 | // Consider three cases: |
| 576 | // |
| 577 | // 1. We're here because DHCP failed while starting up. There |
| 578 | // are two subcases: |
| 579 | // a. DHCP has failed, and Static IP config has _not yet_ |
| 580 | // completed. It's fine to do nothing, because we'll |
| 581 | // apply the static config shortly. |
| 582 | // b. DHCP has failed, and Static IP config has _already_ |
| 583 | // completed. It's fine to do nothing, because we can |
| 584 | // continue to use the static config that's already |
| 585 | // been applied. |
| 586 | // |
| 587 | // 2. We're here because a previously valid DHCP configuration |
| 588 | // is no longer valid. There's still a static IP config, |
| 589 | // because the condition in the if clause evaluated to true. |
| 590 | // Furthermore, the static config includes an IP address for |
| 591 | // us to use. |
| 592 | // |
| 593 | // The current configuration may include some DHCP |
| 594 | // parameters, overriden by any static parameters |
| 595 | // provided. We continue to use this configuration, because |
| 596 | // the only configuration element that is leased to us (IP |
| 597 | // address) will be overriden by a static parameter. |
| 598 | return; |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | ipconfig->ResetProperties(); |
| 603 | OnIPConfigFailure(); |
Paul Stewart | d4f2648 | 2014-04-25 19:12:03 -0700 | [diff] [blame] | 604 | UpdateIPConfigsProperty(); |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 605 | DestroyConnection(); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 606 | } |
| 607 | |
Paul Stewart | 8223653 | 2013-12-10 15:33:11 -0800 | [diff] [blame] | 608 | void Device::OnIPConfigRefreshed(const IPConfigRefPtr &ipconfig) { |
| 609 | // Clear the previously applied static IP parameters. |
| 610 | ipconfig->RestoreSavedIPParameters( |
| 611 | selected_service_->mutable_static_ip_parameters()); |
| 612 | |
| 613 | dispatcher_->PostTask(Bind(&Device::ConfigureStaticIPTask, |
| 614 | weak_ptr_factory_.GetWeakPtr())); |
| 615 | } |
| 616 | |
Paul Stewart | f6f9648 | 2013-07-12 12:49:15 -0700 | [diff] [blame] | 617 | void Device::OnIPConfigFailure() { |
| 618 | if (selected_service_) { |
| 619 | Error error; |
Samuel Tan | 0d06119 | 2014-07-07 15:45:15 -0700 | [diff] [blame] | 620 | selected_service_->DisconnectWithFailure(Service::kFailureDHCP, |
| 621 | &error, |
| 622 | __func__); |
Paul Stewart | f6f9648 | 2013-07-12 12:49:15 -0700 | [diff] [blame] | 623 | } |
| 624 | } |
| 625 | |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame] | 626 | void Device::OnIPConfigExpired(const IPConfigRefPtr &ipconfig) { |
| 627 | metrics()->SendToUMA( |
| 628 | metrics()->GetFullMetricName( |
mukesh agrawal | 132e96f | 2014-04-24 11:49:42 -0700 | [diff] [blame] | 629 | Metrics::kMetricExpiredLeaseLengthSecondsSuffix, technology()), |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame] | 630 | ipconfig->properties().lease_duration_seconds, |
| 631 | Metrics::kMetricExpiredLeaseLengthSecondsMin, |
| 632 | Metrics::kMetricExpiredLeaseLengthSecondsMax, |
| 633 | Metrics::kMetricExpiredLeaseLengthSecondsNumBuckets); |
| 634 | } |
| 635 | |
Christopher Wiley | 5519e9e | 2013-01-08 16:55:56 -0800 | [diff] [blame] | 636 | void Device::OnConnected() {} |
| 637 | |
Paul Stewart | 8596f9f | 2013-03-14 07:58:26 -0700 | [diff] [blame] | 638 | void Device::OnConnectionUpdated() { |
| 639 | if (selected_service_) { |
| 640 | manager_->UpdateService(selected_service_); |
| 641 | } |
| 642 | } |
| 643 | |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 644 | void Device::CreateConnection() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 645 | SLOG(Device, 2) << __func__; |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 646 | if (!connection_.get()) { |
mukesh agrawal | 23ac6b7 | 2013-01-31 18:52:37 -0800 | [diff] [blame] | 647 | connection_ = new Connection(interface_index_, |
| 648 | link_name_, |
| 649 | technology_, |
| 650 | manager_->device_info()); |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 651 | } |
| 652 | } |
| 653 | |
| 654 | void Device::DestroyConnection() { |
mukesh agrawal | 6813e76 | 2013-07-10 19:05:08 -0700 | [diff] [blame] | 655 | SLOG(Device, 2) << __func__ << " on " << link_name_; |
Peter Qiu | dc335f8 | 2014-05-15 10:33:17 -0700 | [diff] [blame] | 656 | StopTrafficMonitor(); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 657 | StopPortalDetection(); |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 658 | StopLinkMonitor(); |
Peter Qiu | 6f5618b | 2014-06-05 15:19:01 -0700 | [diff] [blame] | 659 | StopDNSTest(); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 660 | if (selected_service_.get()) { |
mukesh agrawal | 6813e76 | 2013-07-10 19:05:08 -0700 | [diff] [blame] | 661 | SLOG(Device, 3) << "Clearing connection of service " |
| 662 | << selected_service_->unique_name(); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 663 | selected_service_->SetConnection(NULL); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 664 | } |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 665 | connection_ = NULL; |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 666 | } |
| 667 | |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 668 | void Device::SelectService(const ServiceRefPtr &service) { |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 669 | SLOG(Device, 2) << __func__ << ": service " |
mukesh agrawal | 6813e76 | 2013-07-10 19:05:08 -0700 | [diff] [blame] | 670 | << (service ? service->unique_name() : "*reset*") |
| 671 | << " on " << link_name_; |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 672 | |
| 673 | if (selected_service_.get() == service.get()) { |
| 674 | // No change to |selected_service_|. Return early to avoid |
| 675 | // changing its state. |
| 676 | return; |
| 677 | } |
| 678 | |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 679 | if (selected_service_.get()) { |
| 680 | if (selected_service_->state() != Service::kStateFailure) { |
| 681 | selected_service_->SetState(Service::kStateIdle); |
| 682 | } |
Paul Stewart | 20b0a09 | 2012-05-22 20:39:57 -0700 | [diff] [blame] | 683 | // Just in case the Device subclass has not already done so, make |
| 684 | // sure the previously selected service has its connection removed. |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 685 | selected_service_->SetConnection(NULL); |
Peter Qiu | dc335f8 | 2014-05-15 10:33:17 -0700 | [diff] [blame] | 686 | StopTrafficMonitor(); |
Paul Stewart | c886061 | 2012-09-28 07:36:21 -0700 | [diff] [blame] | 687 | StopLinkMonitor(); |
| 688 | StopPortalDetection(); |
Peter Qiu | 6f5618b | 2014-06-05 15:19:01 -0700 | [diff] [blame] | 689 | StopDNSTest(); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 690 | } |
| 691 | selected_service_ = service; |
| 692 | } |
| 693 | |
| 694 | void Device::SetServiceState(Service::ConnectState state) { |
| 695 | if (selected_service_.get()) { |
| 696 | selected_service_->SetState(state); |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | void Device::SetServiceFailure(Service::ConnectFailure failure_state) { |
| 701 | if (selected_service_.get()) { |
| 702 | selected_service_->SetFailure(failure_state); |
| 703 | } |
| 704 | } |
| 705 | |
Eric Shienbrood | cc95c5d | 2012-03-30 15:25:49 -0400 | [diff] [blame] | 706 | void Device::SetServiceFailureSilent(Service::ConnectFailure failure_state) { |
| 707 | if (selected_service_.get()) { |
| 708 | selected_service_->SetFailureSilent(failure_state); |
| 709 | } |
| 710 | } |
| 711 | |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 712 | bool Device::SetIPFlag(IPAddress::Family family, const string &flag, |
| 713 | const string &value) { |
| 714 | string ip_version; |
| 715 | if (family == IPAddress::kFamilyIPv4) { |
| 716 | ip_version = kIPFlagVersion4; |
| 717 | } else if (family == IPAddress::kFamilyIPv6) { |
| 718 | ip_version = kIPFlagVersion6; |
| 719 | } else { |
| 720 | NOTIMPLEMENTED(); |
| 721 | } |
| 722 | FilePath flag_file(StringPrintf(kIPFlagTemplate, ip_version.c_str(), |
| 723 | link_name_.c_str(), flag.c_str())); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 724 | SLOG(Device, 2) << "Writing " << value << " to flag file " |
| 725 | << flag_file.value(); |
Ben Chan | 6fbf64f | 2014-05-21 18:07:01 -0700 | [diff] [blame] | 726 | if (base::WriteFile(flag_file, value.c_str(), value.length()) != 1) { |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 727 | LOG(ERROR) << StringPrintf("IP flag write failed: %s to %s", |
| 728 | value.c_str(), flag_file.value().c_str()); |
| 729 | return false; |
| 730 | } |
| 731 | return true; |
| 732 | } |
| 733 | |
Paul Stewart | c6fbad9 | 2013-11-13 14:50:52 -0800 | [diff] [blame] | 734 | string Device::PerformTDLSOperation(const string &/* operation */, |
| 735 | const string &/* peer */, |
| 736 | Error */* error */) { |
| 737 | return ""; |
| 738 | } |
| 739 | |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 740 | void Device::ResetByteCounters() { |
| 741 | manager_->device_info()->GetByteCounts( |
| 742 | interface_index_, &receive_byte_offset_, &transmit_byte_offset_); |
| 743 | manager_->UpdateDevice(this); |
| 744 | } |
| 745 | |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 746 | bool Device::RestartPortalDetection() { |
| 747 | StopPortalDetection(); |
| 748 | return StartPortalDetection(); |
| 749 | } |
| 750 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 751 | bool Device::RequestPortalDetection() { |
| 752 | if (!selected_service_) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 753 | SLOG(Device, 2) << FriendlyName() |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 754 | << ": No selected service, so no need for portal check."; |
| 755 | return false; |
| 756 | } |
| 757 | |
| 758 | if (!connection_.get()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 759 | SLOG(Device, 2) << FriendlyName() |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 760 | << ": No connection, so no need for portal check."; |
| 761 | return false; |
| 762 | } |
| 763 | |
| 764 | if (selected_service_->state() != Service::kStatePortal) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 765 | SLOG(Device, 2) << FriendlyName() |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 766 | << ": Service is not in portal state. No need to start check."; |
| 767 | return false; |
| 768 | } |
| 769 | |
| 770 | if (!connection_->is_default()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 771 | SLOG(Device, 2) << FriendlyName() |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 772 | << ": Service is not the default connection. Don't start check."; |
| 773 | return false; |
| 774 | } |
| 775 | |
| 776 | if (portal_detector_.get() && portal_detector_->IsInProgress()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 777 | SLOG(Device, 2) << FriendlyName() |
| 778 | << ": Portal detection is already running."; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 779 | return true; |
| 780 | } |
| 781 | |
| 782 | return StartPortalDetection(); |
| 783 | } |
| 784 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 785 | bool Device::StartPortalDetection() { |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 786 | DCHECK(selected_service_); |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 787 | if (selected_service_->IsPortalDetectionDisabled()) { |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 788 | SLOG(Device, 2) << "Service " << selected_service_->unique_name() |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 789 | << ": Portal detection is disabled; " |
| 790 | << "marking service online."; |
| 791 | SetServiceConnectedState(Service::kStateOnline); |
| 792 | return false; |
| 793 | } |
| 794 | |
| 795 | if (selected_service_->IsPortalDetectionAuto() && |
| 796 | !manager_->IsPortalDetectionEnabled(technology())) { |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 797 | // If portal detection is disabled for this technology, immediately set |
| 798 | // the service state to "Online". |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 799 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 800 | << ": Portal detection is disabled; " |
| 801 | << "marking service online."; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 802 | SetServiceConnectedState(Service::kStateOnline); |
| 803 | return false; |
| 804 | } |
| 805 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 806 | if (selected_service_->HasProxyConfig()) { |
| 807 | // Services with HTTP proxy configurations should not be checked by the |
| 808 | // connection manager, since we don't have the ability to evaluate |
| 809 | // arbitrary proxy configs and their possible credentials. |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 810 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 811 | << ": Service has proxy config; marking it online."; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 812 | SetServiceConnectedState(Service::kStateOnline); |
| 813 | return false; |
| 814 | } |
| 815 | |
| 816 | portal_detector_.reset(new PortalDetector(connection_, |
| 817 | dispatcher_, |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 818 | portal_detector_callback_)); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 819 | if (!portal_detector_->Start(manager_->GetPortalCheckURL())) { |
| 820 | LOG(ERROR) << "Device " << FriendlyName() |
| 821 | << ": Portal detection failed to start: likely bad URL: " |
| 822 | << manager_->GetPortalCheckURL(); |
| 823 | SetServiceConnectedState(Service::kStateOnline); |
| 824 | return false; |
| 825 | } |
| 826 | |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 827 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 828 | << ": Portal detection has started."; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 829 | return true; |
| 830 | } |
| 831 | |
| 832 | void Device::StopPortalDetection() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 833 | SLOG(Device, 2) << "Device " << FriendlyName() |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 834 | << ": Portal detection stopping."; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 835 | portal_detector_.reset(); |
| 836 | } |
| 837 | |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 838 | void Device::set_link_monitor(LinkMonitor *link_monitor) { |
| 839 | link_monitor_.reset(link_monitor); |
| 840 | } |
| 841 | |
| 842 | bool Device::StartLinkMonitor() { |
| 843 | if (!manager_->IsTechnologyLinkMonitorEnabled(technology())) { |
| 844 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 845 | << ": Link Monitoring is disabled."; |
| 846 | return false; |
| 847 | } |
| 848 | |
| 849 | if (!link_monitor()) { |
| 850 | set_link_monitor( |
| 851 | new LinkMonitor( |
| 852 | connection_, dispatcher_, metrics(), manager_->device_info(), |
Peter Qiu | b5d124f | 2014-04-14 12:05:02 -0700 | [diff] [blame] | 853 | Bind(&Device::OnLinkMonitorFailure, weak_ptr_factory_.GetWeakPtr()), |
| 854 | Bind(&Device::OnLinkMonitorGatewayChange, |
| 855 | weak_ptr_factory_.GetWeakPtr()))); |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 859 | << ": Link Monitor starting."; |
| 860 | return link_monitor_->Start(); |
| 861 | } |
| 862 | |
| 863 | void Device::StopLinkMonitor() { |
| 864 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 865 | << ": Link Monitor stopping."; |
| 866 | link_monitor_.reset(); |
| 867 | } |
| 868 | |
| 869 | void Device::OnLinkMonitorFailure() { |
| 870 | LOG(ERROR) << "Device " << FriendlyName() |
| 871 | << ": Link Monitor indicates failure."; |
| 872 | } |
| 873 | |
Peter Qiu | 9d58193 | 2014-04-14 16:37:37 -0700 | [diff] [blame] | 874 | void Device::OnLinkMonitorGatewayChange() { |
| 875 | string gateway_mac = link_monitor()->gateway_mac_address().HexEncode(); |
| 876 | int connection_id = manager_->CalcConnectionId( |
| 877 | ipconfig_->properties().gateway, gateway_mac); |
| 878 | |
| 879 | CHECK(selected_service_); |
| 880 | selected_service_->set_connection_id(connection_id); |
| 881 | |
| 882 | manager_->ReportServicesOnSameNetwork(connection_id); |
| 883 | } |
| 884 | |
Peter Qiu | 6f5618b | 2014-06-05 15:19:01 -0700 | [diff] [blame] | 885 | bool Device::StartDNSTest( |
| 886 | const vector<string> &dns_servers, |
| 887 | bool retry_until_success, |
| 888 | const Callback<void(const DNSServerTester::Status)> &callback) { |
| 889 | if (dns_server_tester_.get()) { |
| 890 | LOG(ERROR) << FriendlyName() << ": " |
| 891 | << "Failed to start DNS Test: current test still running"; |
| 892 | return false; |
Peter Qiu | b9256f3 | 2014-05-09 15:27:29 -0700 | [diff] [blame] | 893 | } |
| 894 | |
Peter Qiu | 6f5618b | 2014-06-05 15:19:01 -0700 | [diff] [blame] | 895 | dns_server_tester_.reset(new DNSServerTester(connection_, |
| 896 | dispatcher_, |
| 897 | dns_servers, |
| 898 | retry_until_success, |
| 899 | callback)); |
| 900 | dns_server_tester_->Start(); |
| 901 | return true; |
| 902 | } |
| 903 | |
| 904 | void Device::StopDNSTest() { |
| 905 | dns_server_tester_.reset(); |
Peter Qiu | b9256f3 | 2014-05-09 15:27:29 -0700 | [diff] [blame] | 906 | } |
| 907 | |
Peter Qiu | d670d03 | 2014-06-03 15:04:43 -0700 | [diff] [blame] | 908 | void Device::FallbackDNSResultCallback(const DNSServerTester::Status status) { |
Peter Qiu | 6f5618b | 2014-06-05 15:19:01 -0700 | [diff] [blame] | 909 | StopDNSTest(); |
Peter Qiu | f18e771 | 2014-05-20 09:59:46 -0700 | [diff] [blame] | 910 | int result = Metrics::kFallbackDNSTestResultFailure; |
Peter Qiu | d670d03 | 2014-06-03 15:04:43 -0700 | [diff] [blame] | 911 | if (status == DNSServerTester::kStatusSuccess) { |
Peter Qiu | f18e771 | 2014-05-20 09:59:46 -0700 | [diff] [blame] | 912 | result = Metrics::kFallbackDNSTestResultSuccess; |
Peter Qiu | a89154b | 2014-05-23 15:45:42 -0700 | [diff] [blame] | 913 | |
| 914 | // Switch to fallback DNS server if service is configured to allow DNS |
| 915 | // fallback. |
| 916 | CHECK(selected_service_); |
| 917 | if (selected_service_->is_dns_auto_fallback_allowed()) { |
Peter Qiu | 6f5618b | 2014-06-05 15:19:01 -0700 | [diff] [blame] | 918 | LOG(INFO) << "Device " << FriendlyName() |
| 919 | << ": Switching to fallback DNS servers."; |
| 920 | // Save the DNS servers from ipconfig. |
| 921 | config_dns_servers_ = ipconfig_->properties().dns_servers; |
Peter Qiu | a89154b | 2014-05-23 15:45:42 -0700 | [diff] [blame] | 922 | SwitchDNSServers(vector<string>(std::begin(kFallbackDnsServers), |
| 923 | std::end(kFallbackDnsServers))); |
Peter Qiu | 6f5618b | 2014-06-05 15:19:01 -0700 | [diff] [blame] | 924 | // Start DNS test for configured DNS servers. |
| 925 | StartDNSTest(config_dns_servers_, |
| 926 | true, |
| 927 | Bind(&Device::ConfigDNSResultCallback, |
| 928 | weak_ptr_factory_.GetWeakPtr())); |
Peter Qiu | a89154b | 2014-05-23 15:45:42 -0700 | [diff] [blame] | 929 | } |
Peter Qiu | b9256f3 | 2014-05-09 15:27:29 -0700 | [diff] [blame] | 930 | } |
Peter Qiu | f18e771 | 2014-05-20 09:59:46 -0700 | [diff] [blame] | 931 | metrics()->NotifyFallbackDNSTestResult(technology_, result); |
Peter Qiu | 6f5618b | 2014-06-05 15:19:01 -0700 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | void Device::ConfigDNSResultCallback(const DNSServerTester::Status status) { |
| 935 | StopDNSTest(); |
| 936 | // DNS test failed to start due to internal error. |
| 937 | if (status == DNSServerTester::kStatusFailure) { |
| 938 | return; |
| 939 | } |
| 940 | |
| 941 | // Switch back to the configured DNS servers. |
| 942 | LOG(INFO) << "Device " << FriendlyName() |
| 943 | << ": Switching back to configured DNS servers."; |
| 944 | SwitchDNSServers(config_dns_servers_); |
Peter Qiu | b9256f3 | 2014-05-09 15:27:29 -0700 | [diff] [blame] | 945 | } |
| 946 | |
Peter Qiu | a89154b | 2014-05-23 15:45:42 -0700 | [diff] [blame] | 947 | void Device::SwitchDNSServers(const vector<string> &dns_servers) { |
| 948 | CHECK(ipconfig_); |
| 949 | CHECK(connection_); |
| 950 | // Push new DNS servers setting to the IP config object. |
| 951 | ipconfig_->UpdateDNSServers(dns_servers); |
| 952 | // Push new DNS servers setting to the current connection, so the resolver |
| 953 | // will be updated to use the new DNS servers. |
| 954 | connection_->UpdateDNSServers(dns_servers); |
| 955 | // Allow the service to notify Chrome of ipconfig changes. |
| 956 | selected_service_->NotifyIPConfigChanges(); |
Peter Qiu | 6f5618b | 2014-06-05 15:19:01 -0700 | [diff] [blame] | 957 | // Restart the portal detection with the new DNS setting. |
| 958 | RestartPortalDetection(); |
Peter Qiu | a89154b | 2014-05-23 15:45:42 -0700 | [diff] [blame] | 959 | } |
| 960 | |
Peter Qiu | dc335f8 | 2014-05-15 10:33:17 -0700 | [diff] [blame] | 961 | void Device::set_traffic_monitor(TrafficMonitor *traffic_monitor) { |
| 962 | traffic_monitor_.reset(traffic_monitor); |
| 963 | } |
| 964 | |
| 965 | bool Device::IsTrafficMonitorEnabled() const { |
| 966 | return false; |
| 967 | } |
| 968 | |
| 969 | void Device::StartTrafficMonitor() { |
| 970 | // Return if traffic monitor is not enabled for this device. |
| 971 | if (!IsTrafficMonitorEnabled()) { |
| 972 | return; |
| 973 | } |
| 974 | |
| 975 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 976 | << ": Traffic Monitor starting."; |
| 977 | if (!traffic_monitor_.get()) { |
| 978 | traffic_monitor_.reset(new TrafficMonitor(this, dispatcher_)); |
| 979 | traffic_monitor_->set_network_problem_detected_callback( |
| 980 | Bind(&Device::OnEncounterNetworkProblem, |
| 981 | weak_ptr_factory_.GetWeakPtr())); |
| 982 | } |
| 983 | traffic_monitor_->Start(); |
| 984 | } |
| 985 | |
| 986 | void Device::StopTrafficMonitor() { |
| 987 | // Return if traffic monitor is not enabled for this device. |
| 988 | if (!IsTrafficMonitorEnabled()) { |
| 989 | return; |
| 990 | } |
| 991 | |
| 992 | if (traffic_monitor_.get()) { |
| 993 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 994 | << ": Traffic Monitor stopping."; |
| 995 | traffic_monitor_->Stop(); |
| 996 | } |
| 997 | traffic_monitor_.reset(); |
| 998 | } |
| 999 | |
| 1000 | void Device::OnEncounterNetworkProblem(int reason) { |
| 1001 | int metric_code; |
| 1002 | switch (reason) { |
| 1003 | case TrafficMonitor::kNetworkProblemCongestedTxQueue: |
| 1004 | metric_code = Metrics::kNetworkProblemCongestedTCPTxQueue; |
| 1005 | break; |
| 1006 | case TrafficMonitor::kNetworkProblemDNSFailure: |
| 1007 | metric_code = Metrics::kNetworkProblemDNSFailure; |
| 1008 | break; |
| 1009 | default: |
| 1010 | LOG(ERROR) << "Invalid network problem code: " << reason; |
| 1011 | return; |
| 1012 | } |
| 1013 | |
| 1014 | metrics()->NotifyNetworkProblemDetected(technology_, metric_code); |
| 1015 | // Stop the traffic monitor, only report the first network problem detected |
| 1016 | // on the connection for now. |
| 1017 | StopTrafficMonitor(); |
| 1018 | } |
| 1019 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1020 | void Device::SetServiceConnectedState(Service::ConnectState state) { |
| 1021 | DCHECK(selected_service_.get()); |
| 1022 | |
| 1023 | if (!selected_service_.get()) { |
| 1024 | LOG(ERROR) << FriendlyName() << ": " |
| 1025 | << "Portal detection completed but no selected service exists!"; |
| 1026 | return; |
| 1027 | } |
| 1028 | |
| 1029 | if (!selected_service_->IsConnected()) { |
| 1030 | LOG(ERROR) << FriendlyName() << ": " |
| 1031 | << "Portal detection completed but selected service " |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 1032 | << selected_service_->unique_name() |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1033 | << " is in non-connected state."; |
| 1034 | return; |
| 1035 | } |
| 1036 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 1037 | if (state == Service::kStatePortal && connection_->is_default() && |
| 1038 | manager_->GetPortalCheckInterval() != 0) { |
| 1039 | CHECK(portal_detector_.get()); |
| 1040 | if (!portal_detector_->StartAfterDelay( |
| 1041 | manager_->GetPortalCheckURL(), |
| 1042 | manager_->GetPortalCheckInterval())) { |
| 1043 | LOG(ERROR) << "Device " << FriendlyName() |
| 1044 | << ": Portal detection failed to restart: likely bad URL: " |
| 1045 | << manager_->GetPortalCheckURL(); |
| 1046 | SetServiceState(Service::kStateOnline); |
| 1047 | portal_detector_.reset(); |
| 1048 | return; |
| 1049 | } |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 1050 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 1051 | << ": Portal detection retrying."; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 1052 | } else { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 1053 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 1054 | << ": Portal will not retry."; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 1055 | portal_detector_.reset(); |
| 1056 | } |
| 1057 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1058 | SetServiceState(state); |
| 1059 | } |
| 1060 | |
| 1061 | void Device::PortalDetectorCallback(const PortalDetector::Result &result) { |
| 1062 | if (!result.final) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 1063 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 1064 | << ": Received non-final status: " |
| 1065 | << PortalDetector::StatusToString(result.status); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1066 | return; |
| 1067 | } |
| 1068 | |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 1069 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 1070 | << ": Received final status: " |
| 1071 | << PortalDetector::StatusToString(result.status); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1072 | |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 1073 | portal_attempts_to_online_ += result.num_attempts; |
| 1074 | |
Peter Qiu | b9256f3 | 2014-05-09 15:27:29 -0700 | [diff] [blame] | 1075 | int portal_status = Metrics::PortalDetectionResultToEnum(result); |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 1076 | metrics()->SendEnumToUMA( |
mukesh agrawal | 132e96f | 2014-04-24 11:49:42 -0700 | [diff] [blame] | 1077 | metrics()->GetFullMetricName(Metrics::kMetricPortalResultSuffix, |
| 1078 | technology()), |
Peter Qiu | b9256f3 | 2014-05-09 15:27:29 -0700 | [diff] [blame] | 1079 | portal_status, |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 1080 | Metrics::kPortalResultMax); |
| 1081 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1082 | if (result.status == PortalDetector::kStatusSuccess) { |
| 1083 | SetServiceConnectedState(Service::kStateOnline); |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 1084 | |
| 1085 | metrics()->SendToUMA( |
| 1086 | metrics()->GetFullMetricName( |
mukesh agrawal | 132e96f | 2014-04-24 11:49:42 -0700 | [diff] [blame] | 1087 | Metrics::kMetricPortalAttemptsToOnlineSuffix, technology()), |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 1088 | portal_attempts_to_online_, |
| 1089 | Metrics::kMetricPortalAttemptsToOnlineMin, |
| 1090 | Metrics::kMetricPortalAttemptsToOnlineMax, |
| 1091 | Metrics::kMetricPortalAttemptsToOnlineNumBuckets); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1092 | } else { |
| 1093 | SetServiceConnectedState(Service::kStatePortal); |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 1094 | |
| 1095 | metrics()->SendToUMA( |
| 1096 | metrics()->GetFullMetricName( |
mukesh agrawal | 132e96f | 2014-04-24 11:49:42 -0700 | [diff] [blame] | 1097 | Metrics::kMetricPortalAttemptsSuffix, technology()), |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 1098 | result.num_attempts, |
| 1099 | Metrics::kMetricPortalAttemptsMin, |
| 1100 | Metrics::kMetricPortalAttemptsMax, |
| 1101 | Metrics::kMetricPortalAttemptsNumBuckets); |
Peter Qiu | b9256f3 | 2014-05-09 15:27:29 -0700 | [diff] [blame] | 1102 | |
| 1103 | // Perform fallback DNS test if the portal failure is DNS related. |
| 1104 | // The test will send a DNS request to Google's DNS server to determine |
| 1105 | // if the DNS failure is due to bad DNS server settings. |
| 1106 | if ((portal_status == Metrics::kPortalResultDNSFailure) || |
| 1107 | (portal_status == Metrics::kPortalResultDNSTimeout)) { |
Peter Qiu | 6f5618b | 2014-06-05 15:19:01 -0700 | [diff] [blame] | 1108 | StartDNSTest(vector<string>(std::begin(kFallbackDnsServers), |
| 1109 | std::end(kFallbackDnsServers)), |
| 1110 | false, |
| 1111 | Bind(&Device::FallbackDNSResultCallback, |
| 1112 | weak_ptr_factory_.GetWeakPtr())); |
Peter Qiu | b9256f3 | 2014-05-09 15:27:29 -0700 | [diff] [blame] | 1113 | } |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1114 | } |
| 1115 | } |
| 1116 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 1117 | vector<string> Device::AvailableIPConfigs(Error */*error*/) { |
Paul Stewart | d4f2648 | 2014-04-25 19:12:03 -0700 | [diff] [blame] | 1118 | vector<string> ipconfigs; |
| 1119 | if (ipconfig_) { |
| 1120 | ipconfigs.push_back(ipconfig_->GetRpcIdentifier()); |
Jason Glasgow | 08afdff | 2012-04-03 10:22:26 -0400 | [diff] [blame] | 1121 | } |
Paul Stewart | d4f2648 | 2014-04-25 19:12:03 -0700 | [diff] [blame] | 1122 | if (ip6config_) { |
| 1123 | ipconfigs.push_back(ip6config_->GetRpcIdentifier()); |
| 1124 | } |
| 1125 | return ipconfigs; |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | string Device::GetRpcConnectionIdentifier() { |
| 1129 | return adaptor_->GetRpcConnectionIdentifier(); |
| 1130 | } |
| 1131 | |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 1132 | uint64 Device::GetLinkMonitorResponseTime(Error *error) { |
| 1133 | if (!link_monitor_.get()) { |
| 1134 | // It is not strictly an error that the link monitor does not |
| 1135 | // exist, but returning an error here allows the GetProperties |
| 1136 | // call in our Adaptor to omit this parameter. |
| 1137 | error->Populate(Error::kNotFound, "Device is not running LinkMonitor"); |
| 1138 | return 0; |
| 1139 | } |
| 1140 | return link_monitor_->GetResponseTimeMilliseconds(); |
| 1141 | } |
| 1142 | |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 1143 | uint64 Device::GetReceiveByteCount() { |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 1144 | uint64 rx_byte_count = 0, tx_byte_count = 0; |
| 1145 | manager_->device_info()->GetByteCounts( |
| 1146 | interface_index_, &rx_byte_count, &tx_byte_count); |
| 1147 | return rx_byte_count - receive_byte_offset_; |
| 1148 | } |
| 1149 | |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 1150 | uint64 Device::GetTransmitByteCount() { |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 1151 | uint64 rx_byte_count = 0, tx_byte_count = 0; |
| 1152 | manager_->device_info()->GetByteCounts( |
| 1153 | interface_index_, &rx_byte_count, &tx_byte_count); |
| 1154 | return tx_byte_count - transmit_byte_offset_; |
| 1155 | } |
| 1156 | |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 1157 | uint64 Device::GetReceiveByteCountProperty(Error */*error*/) { |
| 1158 | return GetReceiveByteCount(); |
| 1159 | } |
| 1160 | |
| 1161 | uint64 Device::GetTransmitByteCountProperty(Error */*error*/) { |
| 1162 | return GetTransmitByteCount(); |
| 1163 | } |
| 1164 | |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 1165 | bool Device::IsUnderlyingDeviceEnabled() const { |
| 1166 | return false; |
| 1167 | } |
| 1168 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1169 | // callback |
| 1170 | void Device::OnEnabledStateChanged(const ResultCallback &callback, |
| 1171 | const Error &error) { |
mukesh agrawal | 46c27cc | 2013-07-10 16:39:10 -0700 | [diff] [blame] | 1172 | SLOG(Device, 2) << __func__ |
| 1173 | << " (target: " << enabled_pending_ << "," |
| 1174 | << " success: " << error.IsSuccess() << ")" |
| 1175 | << " on " << link_name_; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1176 | if (error.IsSuccess()) { |
| 1177 | enabled_ = enabled_pending_; |
| 1178 | manager_->UpdateEnabledTechnologies(); |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 1179 | adaptor_->EmitBoolChanged(kPoweredProperty, enabled_); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1180 | } |
Gary Morain | baeefdf | 2012-04-30 14:53:35 -0700 | [diff] [blame] | 1181 | enabled_pending_ = enabled_; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1182 | if (!callback.is_null()) |
| 1183 | callback.Run(error); |
| 1184 | } |
| 1185 | |
| 1186 | void Device::SetEnabled(bool enable) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 1187 | SLOG(Device, 2) << __func__ << "(" << enable << ")"; |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 1188 | Error error; |
mukesh agrawal | 2818551 | 2013-10-18 16:57:09 -0700 | [diff] [blame] | 1189 | SetEnabledChecked(enable, false, &error, ResultCallback()); |
Arman Uguray | 8bf6a5c | 2013-08-12 14:18:03 -0700 | [diff] [blame] | 1190 | |
| 1191 | // SetEnabledInternal might fail here if there is an unfinished enable or |
| 1192 | // disable operation. Don't log error in this case, as this method is only |
| 1193 | // called when the underlying device is already in the target state and the |
| 1194 | // pending operation should eventually bring the device to the expected |
| 1195 | // state. |
| 1196 | LOG_IF(ERROR, |
| 1197 | error.IsFailure() && |
| 1198 | !error.IsOngoing() && |
| 1199 | error.type() != Error::kInProgress) |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 1200 | << "Enabled failed, but no way to report the failure."; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1201 | } |
| 1202 | |
Ben Chan | 9f3dcf8 | 2013-09-25 18:04:58 -0700 | [diff] [blame] | 1203 | void Device::SetEnabledNonPersistent(bool enable, |
| 1204 | Error *error, |
| 1205 | const ResultCallback &callback) { |
mukesh agrawal | 2818551 | 2013-10-18 16:57:09 -0700 | [diff] [blame] | 1206 | SetEnabledChecked(enable, false, error, callback); |
Ben Chan | 9f3dcf8 | 2013-09-25 18:04:58 -0700 | [diff] [blame] | 1207 | } |
| 1208 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1209 | void Device::SetEnabledPersistent(bool enable, |
| 1210 | Error *error, |
| 1211 | const ResultCallback &callback) { |
mukesh agrawal | 2818551 | 2013-10-18 16:57:09 -0700 | [diff] [blame] | 1212 | SetEnabledChecked(enable, true, error, callback); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1213 | } |
| 1214 | |
mukesh agrawal | 2818551 | 2013-10-18 16:57:09 -0700 | [diff] [blame] | 1215 | void Device::SetEnabledChecked(bool enable, |
| 1216 | bool persist, |
| 1217 | Error *error, |
| 1218 | const ResultCallback &callback) { |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 1219 | DCHECK(error); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 1220 | SLOG(Device, 2) << "Device " << link_name_ << " " |
| 1221 | << (enable ? "starting" : "stopping"); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1222 | if (enable == enabled_) { |
Arman Uguray | 2f352e6 | 2013-08-28 19:12:53 -0700 | [diff] [blame] | 1223 | if (enable != enabled_pending_ && persist) { |
| 1224 | // Return an error, as there is an ongoing operation to achieve the |
| 1225 | // opposite. |
| 1226 | Error::PopulateAndLog( |
| 1227 | error, Error::kOperationFailed, |
| 1228 | enable ? "Cannot enable while the device is disabling." : |
| 1229 | "Cannot disable while the device is enabling."); |
| 1230 | return; |
| 1231 | } |
mukesh agrawal | 2818551 | 2013-10-18 16:57:09 -0700 | [diff] [blame] | 1232 | LOG(INFO) << "Already in desired enable state."; |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 1233 | error->Reset(); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1234 | return; |
| 1235 | } |
| 1236 | |
| 1237 | if (enabled_pending_ == enable) { |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 1238 | Error::PopulateAndLog(error, Error::kInProgress, |
| 1239 | "Enable operation already in progress"); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1240 | return; |
| 1241 | } |
| 1242 | |
| 1243 | if (persist) { |
| 1244 | enabled_persistent_ = enable; |
Darin Petkov | e7c6ad3 | 2012-06-29 10:22:09 +0200 | [diff] [blame] | 1245 | manager_->UpdateDevice(this); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1246 | } |
| 1247 | |
mukesh agrawal | 2818551 | 2013-10-18 16:57:09 -0700 | [diff] [blame] | 1248 | SetEnabledUnchecked(enable, error, callback); |
| 1249 | } |
| 1250 | |
| 1251 | void Device::SetEnabledUnchecked(bool enable, Error *error, |
| 1252 | const ResultCallback &on_enable_complete) { |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1253 | enabled_pending_ = enable; |
mukesh agrawal | 2818551 | 2013-10-18 16:57:09 -0700 | [diff] [blame] | 1254 | EnabledStateChangedCallback chained_callback = |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1255 | Bind(&Device::OnEnabledStateChanged, |
mukesh agrawal | 2818551 | 2013-10-18 16:57:09 -0700 | [diff] [blame] | 1256 | weak_ptr_factory_.GetWeakPtr(), on_enable_complete); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1257 | if (enable) { |
| 1258 | running_ = true; |
mukesh agrawal | 2818551 | 2013-10-18 16:57:09 -0700 | [diff] [blame] | 1259 | Start(error, chained_callback); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1260 | } else { |
| 1261 | running_ = false; |
| 1262 | DestroyIPConfig(); // breaks a reference cycle |
| 1263 | SelectService(NULL); // breaks a reference cycle |
| 1264 | rtnl_handler_->SetInterfaceFlags(interface_index(), 0, IFF_UP); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 1265 | SLOG(Device, 3) << "Device " << link_name_ << " ipconfig_ " |
| 1266 | << (ipconfig_ ? "is set." : "is not set."); |
Paul Stewart | d4f2648 | 2014-04-25 19:12:03 -0700 | [diff] [blame] | 1267 | SLOG(Device, 3) << "Device " << link_name_ << " ip6config_ " |
| 1268 | << (ip6config_ ? "is set." : "is not set."); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 1269 | SLOG(Device, 3) << "Device " << link_name_ << " connection_ " |
| 1270 | << (connection_ ? "is set." : "is not set."); |
| 1271 | SLOG(Device, 3) << "Device " << link_name_ << " selected_service_ " |
| 1272 | << (selected_service_ ? "is set." : "is not set."); |
mukesh agrawal | 2818551 | 2013-10-18 16:57:09 -0700 | [diff] [blame] | 1273 | Stop(error, chained_callback); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1274 | } |
| 1275 | } |
| 1276 | |
Paul Stewart | d4f2648 | 2014-04-25 19:12:03 -0700 | [diff] [blame] | 1277 | void Device::UpdateIPConfigsProperty() { |
| 1278 | adaptor_->EmitRpcIdentifierArrayChanged( |
| 1279 | kIPConfigsProperty, AvailableIPConfigs(NULL)); |
| 1280 | } |
| 1281 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 1282 | } // namespace shill |