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> |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 18 | #include <base/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" |
Arman Uguray | f84a424 | 2013-04-09 20:01:07 -0700 | [diff] [blame] | 23 | #include "shill/connection_health_checker.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 24 | #include "shill/control_interface.h" |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 25 | #include "shill/device_dbus_adaptor.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 26 | #include "shill/dhcp_config.h" |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 27 | #include "shill/dhcp_provider.h" |
Arman Uguray | f84a424 | 2013-04-09 20:01:07 -0700 | [diff] [blame] | 28 | #include "shill/dns_client.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 29 | #include "shill/error.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 30 | #include "shill/event_dispatcher.h" |
Gaurav Shah | 6d2c72d | 2012-10-16 16:30:44 -0700 | [diff] [blame] | 31 | #include "shill/geolocation_info.h" |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 32 | #include "shill/http_proxy.h" |
Arman Uguray | f84a424 | 2013-04-09 20:01:07 -0700 | [diff] [blame] | 33 | #include "shill/ip_address.h" |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 34 | #include "shill/link_monitor.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 35 | #include "shill/logging.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 36 | #include "shill/manager.h" |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 37 | #include "shill/metrics.h" |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 38 | #include "shill/property_accessor.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 39 | #include "shill/refptr_types.h" |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 40 | #include "shill/rtnl_handler.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 41 | #include "shill/service.h" |
Arman Uguray | f84a424 | 2013-04-09 20:01:07 -0700 | [diff] [blame] | 42 | #include "shill/socket_info_reader.h" |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 43 | #include "shill/store_interface.h" |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 44 | #include "shill/technology.h" |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 45 | #include "shill/traffic_monitor.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 46 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 47 | using base::Bind; |
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 |
Paul Stewart | 41a071e | 2013-04-26 07:56:04 -0700 | [diff] [blame] | 74 | const char Device::kStorageIPConfigs[] = "IPConfigs"; |
| 75 | // static |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 76 | const char Device::kStoragePowered[] = "Powered"; |
| 77 | // static |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 78 | const char Device::kStorageReceiveByteCount[] = "ReceiveByteCount"; |
| 79 | // static |
| 80 | const char Device::kStorageTransmitByteCount[] = "TransmitByteCount"; |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 81 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 82 | Device::Device(ControlInterface *control_interface, |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 83 | EventDispatcher *dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 84 | Metrics *metrics, |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 85 | Manager *manager, |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 86 | const string &link_name, |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 87 | const string &address, |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 88 | int interface_index, |
| 89 | Technology::Identifier technology) |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 90 | : enabled_(false), |
| 91 | enabled_persistent_(true), |
| 92 | enabled_pending_(enabled_), |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 93 | reconnect_(true), |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 94 | hardware_address_(address), |
mukesh agrawal | f60e406 | 2011-05-27 13:13:41 -0700 | [diff] [blame] | 95 | interface_index_(interface_index), |
| 96 | running_(false), |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 97 | link_name_(link_name), |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 98 | unique_id_(link_name), |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 99 | control_interface_(control_interface), |
| 100 | dispatcher_(dispatcher), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 101 | metrics_(metrics), |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 102 | manager_(manager), |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 103 | weak_ptr_factory_(this), |
Darin Petkov | 77cb681 | 2011-08-15 16:19:41 -0700 | [diff] [blame] | 104 | adaptor_(control_interface->CreateDeviceAdaptor(this)), |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 105 | traffic_monitor_enabled_(false), |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 106 | portal_detector_callback_(Bind(&Device::PortalDetectorCallback, |
| 107 | weak_ptr_factory_.GetWeakPtr())), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 108 | technology_(technology), |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 109 | portal_attempts_to_online_(0), |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 110 | receive_byte_offset_(0), |
| 111 | transmit_byte_offset_(0), |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 112 | dhcp_provider_(DHCPProvider::GetInstance()), |
| 113 | rtnl_handler_(RTNLHandler::GetInstance()) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 114 | store_.RegisterConstString(flimflam::kAddressProperty, &hardware_address_); |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 115 | |
| 116 | // flimflam::kBgscanMethodProperty: Registered in WiFi |
| 117 | // flimflam::kBgscanShortIntervalProperty: Registered in WiFi |
| 118 | // flimflam::kBgscanSignalThresholdProperty: Registered in WiFi |
| 119 | |
| 120 | // flimflam::kCellularAllowRoamingProperty: Registered in Cellular |
| 121 | // flimflam::kCarrierProperty: Registered in Cellular |
| 122 | // flimflam::kEsnProperty: Registered in Cellular |
Darin Petkov | 3335b37 | 2011-08-22 11:05:32 -0700 | [diff] [blame] | 123 | // flimflam::kHomeProviderProperty: Registered in Cellular |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 124 | // flimflam::kImeiProperty: Registered in Cellular |
Ben Chan | a5e2708 | 2012-07-31 14:30:28 -0700 | [diff] [blame] | 125 | // flimflam::kIccidProperty: Registered in Cellular |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 126 | // flimflam::kImsiProperty: Registered in Cellular |
| 127 | // flimflam::kManufacturerProperty: Registered in Cellular |
| 128 | // flimflam::kMdnProperty: Registered in Cellular |
| 129 | // flimflam::kMeidProperty: Registered in Cellular |
| 130 | // flimflam::kMinProperty: Registered in Cellular |
| 131 | // flimflam::kModelIDProperty: Registered in Cellular |
| 132 | // flimflam::kFirmwareRevisionProperty: Registered in Cellular |
| 133 | // flimflam::kHardwareRevisionProperty: Registered in Cellular |
| 134 | // flimflam::kPRLVersionProperty: Registered in Cellular |
| 135 | // flimflam::kSIMLockStatusProperty: Registered in Cellular |
| 136 | // flimflam::kFoundNetworksProperty: Registered in Cellular |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 137 | // flimflam::kDBusConnectionProperty: Registered in Cellular |
| 138 | // flimflam::kDBusObjectProperty: Register in Cellular |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 139 | |
Jason Glasgow | e8334fd | 2012-03-30 16:02:37 -0400 | [diff] [blame] | 140 | store_.RegisterConstString(flimflam::kInterfaceProperty, &link_name_); |
Jason Glasgow | 08afdff | 2012-04-03 10:22:26 -0400 | [diff] [blame] | 141 | HelpRegisterConstDerivedRpcIdentifiers(flimflam::kIPConfigsProperty, |
| 142 | &Device::AvailableIPConfigs); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 143 | store_.RegisterConstString(flimflam::kNameProperty, &link_name_); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 144 | store_.RegisterConstBool(flimflam::kPoweredProperty, &enabled_); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 145 | HelpRegisterConstDerivedString(flimflam::kTypeProperty, |
| 146 | &Device::GetTechnologyString); |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 147 | HelpRegisterConstDerivedUint64(shill::kLinkMonitorResponseTimeProperty, |
| 148 | &Device::GetLinkMonitorResponseTime); |
Jason Glasgow | b579005 | 2012-01-27 01:03:52 -0500 | [diff] [blame] | 149 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 150 | // TODO(cmasone): Chrome doesn't use this...does anyone? |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 151 | // store_.RegisterConstBool(flimflam::kReconnectProperty, &reconnect_); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 152 | |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 153 | // TODO(cmasone): Figure out what shill concept maps to flimflam's "Network". |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 154 | // known_properties_.push_back(flimflam::kNetworksProperty); |
| 155 | |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 156 | // flimflam::kScanningProperty: Registered in WiFi, Cellular |
| 157 | // flimflam::kScanIntervalProperty: Registered in WiFi, Cellular |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 158 | |
| 159 | if (manager_ && manager_->device_info()) { // Unit tests may not have these. |
| 160 | manager_->device_info()->GetByteCounts( |
| 161 | interface_index_, &receive_byte_offset_, &transmit_byte_offset_); |
| 162 | HelpRegisterConstDerivedUint64(shill::kReceiveByteCountProperty, |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 163 | &Device::GetReceiveByteCountProperty); |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 164 | HelpRegisterConstDerivedUint64(shill::kTransmitByteCountProperty, |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 165 | &Device::GetTransmitByteCountProperty); |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Darin Petkov | a0a0efe | 2012-06-27 12:50:01 +0200 | [diff] [blame] | 168 | LOG(INFO) << "Device created: " << link_name_ |
| 169 | << " index " << interface_index_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | Device::~Device() { |
Darin Petkov | a0a0efe | 2012-06-27 12:50:01 +0200 | [diff] [blame] | 173 | LOG(INFO) << "Device destructed: " << link_name_ |
| 174 | << " index " << interface_index_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 177 | void Device::LinkEvent(unsigned flags, unsigned change) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 178 | SLOG(Device, 2) << "Device " << link_name_ |
| 179 | << std::showbase << std::hex |
| 180 | << " flags " << flags << " changed " << change |
| 181 | << std::dec << std::noshowbase; |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Wade Guthrie | 4823f4f | 2013-07-25 10:03:03 -0700 | [diff] [blame] | 184 | void Device::Scan(ScanType scan_type, Error *error, const string &reason) { |
| 185 | SLOG(Device, 2) << __func__ << " [Device] on " << link_name() << " from " |
| 186 | << reason; |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 187 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 188 | "Device doesn't support scan."); |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 191 | void Device::RegisterOnNetwork(const std::string &/*network_id*/, Error *error, |
| 192 | const ResultCallback &/*callback*/) { |
| 193 | Error::PopulateAndLog(error, Error::kNotSupported, |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 194 | "Device doesn't support network registration."); |
Darin Petkov | 9ae310f | 2011-08-30 15:41:13 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Darin Petkov | c64fe5e | 2012-01-11 12:46:13 +0100 | [diff] [blame] | 197 | void Device::RequirePIN( |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 198 | const string &/*pin*/, bool /*require*/, |
| 199 | Error *error, const ResultCallback &/*callback*/) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 200 | SLOG(Device, 2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 201 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 202 | "Device doesn't support RequirePIN."); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 205 | void Device::EnterPIN(const string &/*pin*/, |
| 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 EnterPIN."); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 210 | } |
| 211 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 212 | void Device::UnblockPIN(const string &/*unblock_code*/, |
| 213 | const string &/*pin*/, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 214 | Error *error, const ResultCallback &/*callback*/) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 215 | SLOG(Device, 2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 216 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 217 | "Device doesn't support UnblockPIN."); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 218 | } |
| 219 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 220 | void Device::ChangePIN(const string &/*old_pin*/, |
| 221 | const string &/*new_pin*/, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 222 | Error *error, const ResultCallback &/*callback*/) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 223 | SLOG(Device, 2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 224 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 225 | "Device doesn't support ChangePIN."); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Ben Chan | ad663e1 | 2013-01-08 01:58:47 -0800 | [diff] [blame] | 228 | void Device::Reset(Error *error, const ResultCallback &/*callback*/) { |
| 229 | SLOG(Device, 2) << __func__; |
| 230 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 231 | "Device doesn't support Reset."); |
| 232 | } |
| 233 | |
Darin Petkov | c37a9c4 | 2012-09-06 15:28:22 +0200 | [diff] [blame] | 234 | void Device::SetCarrier(const string &/*carrier*/, |
| 235 | Error *error, const ResultCallback &/*callback*/) { |
| 236 | SLOG(Device, 2) << __func__; |
| 237 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 238 | "Device doesn't support SetCarrier."); |
| 239 | } |
| 240 | |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 241 | void Device::DisableIPv6() { |
| 242 | SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "1"); |
| 243 | } |
| 244 | |
| 245 | void Device::EnableIPv6() { |
| 246 | SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "0"); |
| 247 | } |
| 248 | |
| 249 | void Device::EnableIPv6Privacy() { |
| 250 | SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagUseTempAddr, |
| 251 | kIPFlagUseTempAddrUsedAndDefault); |
| 252 | } |
| 253 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 254 | void Device::DisableReversePathFilter() { |
| 255 | // TODO(pstew): Current kernel doesn't offer reverse-path filtering flag |
Paul Stewart | ee6b3d7 | 2013-07-12 16:07:51 -0700 | [diff] [blame] | 256 | // for IPv6. crbug.com/207193 |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 257 | SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter, |
| 258 | kIPFlagReversePathFilterLooseMode); |
| 259 | } |
| 260 | |
| 261 | void Device::EnableReversePathFilter() { |
| 262 | SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter, |
| 263 | kIPFlagReversePathFilterEnabled); |
| 264 | } |
| 265 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 266 | bool Device::IsConnected() const { |
| 267 | if (selected_service_) |
| 268 | return selected_service_->IsConnected(); |
| 269 | return false; |
| 270 | } |
| 271 | |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 272 | bool Device::IsConnectedToService(const ServiceRefPtr &service) const { |
| 273 | return service == selected_service_ && IsConnected(); |
| 274 | } |
| 275 | |
mukesh agrawal | f6b3209 | 2013-04-10 15:49:55 -0700 | [diff] [blame] | 276 | string Device::GetRpcIdentifier() const { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 277 | return adaptor_->GetRpcIdentifier(); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 280 | string Device::GetStorageIdentifier() { |
| 281 | string id = GetRpcIdentifier(); |
| 282 | ControlInterface::RpcIdToStorageId(&id); |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 283 | size_t needle = id.find('_'); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 284 | DLOG_IF(ERROR, needle == string::npos) << "No _ in storage id?!?!"; |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 285 | id.replace(id.begin() + needle + 1, id.end(), hardware_address_); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 286 | return id; |
| 287 | } |
| 288 | |
Gaurav Shah | 6d2c72d | 2012-10-16 16:30:44 -0700 | [diff] [blame] | 289 | vector<GeolocationInfo> Device::GetGeolocationObjects() const { |
| 290 | return vector<GeolocationInfo>(); |
| 291 | }; |
| 292 | |
Jason Glasgow | b579005 | 2012-01-27 01:03:52 -0500 | [diff] [blame] | 293 | string Device::GetTechnologyString(Error */*error*/) { |
| 294 | return Technology::NameFromIdentifier(technology()); |
| 295 | } |
| 296 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 297 | const string& Device::FriendlyName() const { |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 298 | return link_name_; |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 301 | const string& Device::UniqueName() const { |
| 302 | return unique_id_; |
| 303 | } |
| 304 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 305 | bool Device::Load(StoreInterface *storage) { |
| 306 | const string id = GetStorageIdentifier(); |
| 307 | if (!storage->ContainsGroup(id)) { |
| 308 | LOG(WARNING) << "Device is not available in the persistent store: " << id; |
| 309 | return false; |
| 310 | } |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 311 | enabled_persistent_ = true; |
| 312 | storage->GetBool(id, kStoragePowered, &enabled_persistent_); |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 313 | uint64 rx_byte_count = 0, tx_byte_count = 0; |
| 314 | |
| 315 | manager_->device_info()->GetByteCounts( |
| 316 | interface_index_, &rx_byte_count, &tx_byte_count); |
| 317 | // If there is a byte-count present in the profile, the return value |
| 318 | // of Device::Get*ByteCount() should be the this stored value plus |
| 319 | // whatever additional bytes we receive since time-of-load. We |
| 320 | // accomplish this by the subtractions below, which can validly |
| 321 | // roll over "negative" in the subtractions below and in Get*ByteCount. |
| 322 | uint64 profile_byte_count; |
| 323 | if (storage->GetUint64(id, kStorageReceiveByteCount, &profile_byte_count)) { |
| 324 | receive_byte_offset_ = rx_byte_count - profile_byte_count; |
| 325 | } |
| 326 | if (storage->GetUint64(id, kStorageTransmitByteCount, &profile_byte_count)) { |
| 327 | transmit_byte_offset_ = tx_byte_count - profile_byte_count; |
| 328 | } |
| 329 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 330 | return true; |
| 331 | } |
| 332 | |
| 333 | bool Device::Save(StoreInterface *storage) { |
| 334 | const string id = GetStorageIdentifier(); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 335 | storage->SetBool(id, kStoragePowered, enabled_persistent_); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 336 | if (ipconfig_.get()) { |
| 337 | // The _0 is an index into the list of IPConfigs that this device might |
| 338 | // have. We only have one IPConfig right now, and I hope to never have |
| 339 | // to support more, as sleffler indicates that associating IPConfigs |
| 340 | // with devices is wrong and due to be changed in flimflam anyhow. |
| 341 | string suffix = hardware_address_ + "_0"; |
| 342 | ipconfig_->Save(storage, suffix); |
| 343 | storage->SetString(id, kStorageIPConfigs, SerializeIPConfigs(suffix)); |
| 344 | } |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 345 | storage->SetUint64(id, kStorageReceiveByteCount, GetReceiveByteCount()); |
| 346 | storage->SetUint64(id, kStorageTransmitByteCount, GetTransmitByteCount()); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 347 | return true; |
| 348 | } |
| 349 | |
mukesh agrawal | 784566d | 2012-08-08 18:32:58 -0700 | [diff] [blame] | 350 | void Device::OnBeforeSuspend() { |
| 351 | // Nothing to be done in the general case. |
| 352 | } |
| 353 | |
| 354 | void Device::OnAfterResume() { |
| 355 | if (ipconfig_) { |
| 356 | SLOG(Device, 3) << "Renewing IP address on resume."; |
| 357 | ipconfig_->RenewIP(); |
| 358 | } |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 359 | if (link_monitor_) { |
| 360 | SLOG(Device, 3) << "Informing Link Monitor of resume."; |
| 361 | link_monitor_->OnAfterResume(); |
| 362 | } |
mukesh agrawal | 784566d | 2012-08-08 18:32:58 -0700 | [diff] [blame] | 363 | } |
| 364 | |
Darin Petkov | 2b8e44e | 2012-06-25 15:13:26 +0200 | [diff] [blame] | 365 | void Device::DropConnection() { |
| 366 | SLOG(Device, 2) << __func__; |
| 367 | DestroyIPConfig(); |
| 368 | SelectService(NULL); |
| 369 | } |
| 370 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 371 | void Device::DestroyIPConfig() { |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 372 | DisableIPv6(); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 373 | if (ipconfig_.get()) { |
Paul Stewart | 217c61d | 2013-06-13 15:12:02 -0700 | [diff] [blame] | 374 | ipconfig_->ReleaseIP(IPConfig::kReleaseReasonDisconnect); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 375 | ipconfig_ = NULL; |
| 376 | } |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 377 | DestroyConnection(); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Arman Uguray | ed8e610 | 2012-11-29 14:47:20 -0800 | [diff] [blame] | 380 | bool Device::ShouldUseArpGateway() const { |
| 381 | return false; |
| 382 | } |
| 383 | |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 384 | bool Device::AcquireIPConfig() { |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 385 | return AcquireIPConfigWithLeaseName(string()); |
| 386 | } |
| 387 | |
| 388 | bool Device::AcquireIPConfigWithLeaseName(const string &lease_name) { |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 389 | DestroyIPConfig(); |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 390 | EnableIPv6(); |
Arman Uguray | ed8e610 | 2012-11-29 14:47:20 -0800 | [diff] [blame] | 391 | bool arp_gateway = manager_->GetArpGateway() && ShouldUseArpGateway(); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 392 | ipconfig_ = dhcp_provider_->CreateConfig(link_name_, |
| 393 | manager_->GetHostName(), |
| 394 | lease_name, |
Arman Uguray | ed8e610 | 2012-11-29 14:47:20 -0800 | [diff] [blame] | 395 | arp_gateway); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 396 | ipconfig_->RegisterUpdateCallback(Bind(&Device::OnIPConfigUpdated, |
| 397 | weak_ptr_factory_.GetWeakPtr())); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 398 | dispatcher_->PostTask(Bind(&Device::ConfigureStaticIPTask, |
| 399 | weak_ptr_factory_.GetWeakPtr())); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 400 | return ipconfig_->RequestIP(); |
| 401 | } |
| 402 | |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 403 | void Device::DestroyIPConfigLease(const string &name) { |
| 404 | dhcp_provider_->DestroyLease(name); |
| 405 | } |
| 406 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 407 | void Device::HelpRegisterConstDerivedString( |
Jason Glasgow | b579005 | 2012-01-27 01:03:52 -0500 | [diff] [blame] | 408 | const string &name, |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 409 | string(Device::*get)(Error *error)) { |
Jason Glasgow | b579005 | 2012-01-27 01:03:52 -0500 | [diff] [blame] | 410 | store_.RegisterDerivedString( |
| 411 | name, |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 412 | StringAccessor(new CustomAccessor<Device, string>(this, get, NULL))); |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Jason Glasgow | 08afdff | 2012-04-03 10:22:26 -0400 | [diff] [blame] | 415 | void Device::HelpRegisterConstDerivedRpcIdentifiers( |
| 416 | const string &name, |
| 417 | RpcIdentifiers(Device::*get)(Error *)) { |
| 418 | store_.RegisterDerivedRpcIdentifiers( |
| 419 | name, |
| 420 | RpcIdentifiersAccessor( |
| 421 | new CustomAccessor<Device, RpcIdentifiers>(this, get, NULL))); |
| 422 | } |
| 423 | |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 424 | void Device::HelpRegisterConstDerivedUint64( |
| 425 | const string &name, |
| 426 | uint64(Device::*get)(Error *)) { |
| 427 | store_.RegisterDerivedUint64( |
| 428 | name, |
| 429 | Uint64Accessor( |
| 430 | new CustomAccessor<Device, uint64>(this, get, NULL))); |
| 431 | } |
| 432 | |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 433 | void Device::ConfigureStaticIPTask() { |
| 434 | SLOG(Device, 2) << __func__ << " selected_service " << selected_service_.get() |
| 435 | << " ipconfig " << ipconfig_.get(); |
| 436 | |
| 437 | if (!selected_service_ || !ipconfig_) { |
| 438 | return; |
| 439 | } |
| 440 | |
| 441 | const StaticIPParameters &static_ip_parameters = |
| 442 | selected_service_->static_ip_parameters(); |
| 443 | if (static_ip_parameters.ContainsAddress()) { |
| 444 | SLOG(Device, 2) << __func__ << " " << " configuring static IP parameters."; |
| 445 | // If the parameters contain an IP address, apply them now and bring |
| 446 | // the interface up. When DHCP information arrives, it will supplement |
| 447 | // the static information. |
| 448 | OnIPConfigUpdated(ipconfig_, true); |
| 449 | } else { |
| 450 | SLOG(Device, 2) << __func__ << " " << " no static IP address."; |
| 451 | } |
| 452 | } |
| 453 | |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 454 | void Device::OnIPConfigUpdated(const IPConfigRefPtr &ipconfig, bool success) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 455 | SLOG(Device, 2) << __func__ << " " << " success: " << success; |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 456 | if (success) { |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 457 | CreateConnection(); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 458 | if (selected_service_) { |
| 459 | ipconfig->ApplyStaticIPParameters( |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 460 | selected_service_->mutable_static_ip_parameters()); |
Paul Stewart | 217c61d | 2013-06-13 15:12:02 -0700 | [diff] [blame] | 461 | if (selected_service_->static_ip_parameters().ContainsAddress()) { |
| 462 | // If we are using a statically configured IP address instead |
| 463 | // of a leased IP address, release any acquired lease so it may |
| 464 | // be used by others. This allows us to merge other non-leased |
| 465 | // parameters (like DNS) when they're available from a DHCP server |
| 466 | // and not overridden by static parameters, but at the same time |
| 467 | // we avoid taking up a dynamic IP address the DHCP server could |
| 468 | // assign to someone else who might actually use it. |
| 469 | ipconfig->ReleaseIP(IPConfig::kReleaseReasonStaticIP); |
| 470 | } |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 471 | } |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 472 | connection_->UpdateFromIPConfig(ipconfig); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 473 | // SetConnection must occur after the UpdateFromIPConfig so the |
| 474 | // service can use the values derived from the connection. |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 475 | if (selected_service_) { |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 476 | selected_service_->SetConnection(connection_); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 477 | } |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 478 | // The service state change needs to happen last, so that at the |
| 479 | // time we report the state change to the manager, the service |
| 480 | // has its connection. |
| 481 | SetServiceState(Service::kStateConnected); |
Christopher Wiley | 5519e9e | 2013-01-08 16:55:56 -0800 | [diff] [blame] | 482 | OnConnected(); |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 483 | portal_attempts_to_online_ = 0; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 484 | // Subtle: Start portal detection after transitioning the service |
| 485 | // to the Connected state because this call may immediately transition |
| 486 | // to the Online state. |
Thieu Le | d176092 | 2012-09-11 14:15:35 -0700 | [diff] [blame] | 487 | if (selected_service_) { |
| 488 | StartPortalDetection(); |
| 489 | } |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 490 | StartLinkMonitor(); |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 491 | StartTrafficMonitor(); |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 492 | SetupConnectionHealthChecker(); |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 493 | } else { |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 494 | // TODO(pstew): This logic gets yet more complex when multiple |
| 495 | // IPConfig types are run in parallel (e.g. DHCP and DHCP6) |
| 496 | if (selected_service_ && |
| 497 | selected_service_->static_ip_parameters().ContainsAddress()) { |
| 498 | // Consider three cases: |
| 499 | // |
| 500 | // 1. We're here because DHCP failed while starting up. There |
| 501 | // are two subcases: |
| 502 | // a. DHCP has failed, and Static IP config has _not yet_ |
| 503 | // completed. It's fine to do nothing, because we'll |
| 504 | // apply the static config shortly. |
| 505 | // b. DHCP has failed, and Static IP config has _already_ |
| 506 | // completed. It's fine to do nothing, because we can |
| 507 | // continue to use the static config that's already |
| 508 | // been applied. |
| 509 | // |
| 510 | // 2. We're here because a previously valid DHCP configuration |
| 511 | // is no longer valid. There's still a static IP config, |
| 512 | // because the condition in the if clause evaluated to true. |
| 513 | // Furthermore, the static config includes an IP address for |
| 514 | // us to use. |
| 515 | // |
| 516 | // The current configuration may include some DHCP |
| 517 | // parameters, overriden by any static parameters |
| 518 | // provided. We continue to use this configuration, because |
| 519 | // the only configuration element that is leased to us (IP |
| 520 | // address) will be overriden by a static parameter. |
| 521 | return; |
| 522 | } |
| 523 | |
Paul Stewart | f6f9648 | 2013-07-12 12:49:15 -0700 | [diff] [blame] | 524 | OnIPConfigFailure(); |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 525 | DestroyConnection(); |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 526 | } |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Paul Stewart | f6f9648 | 2013-07-12 12:49:15 -0700 | [diff] [blame] | 529 | void Device::OnIPConfigFailure() { |
| 530 | if (selected_service_) { |
| 531 | Error error; |
| 532 | selected_service_->DisconnectWithFailure(Service::kFailureDHCP, &error); |
| 533 | } |
| 534 | } |
| 535 | |
Christopher Wiley | 5519e9e | 2013-01-08 16:55:56 -0800 | [diff] [blame] | 536 | void Device::OnConnected() {} |
| 537 | |
Paul Stewart | 8596f9f | 2013-03-14 07:58:26 -0700 | [diff] [blame] | 538 | void Device::OnConnectionUpdated() { |
| 539 | if (selected_service_) { |
| 540 | manager_->UpdateService(selected_service_); |
| 541 | } |
| 542 | } |
| 543 | |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 544 | void Device::CreateConnection() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 545 | SLOG(Device, 2) << __func__; |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 546 | if (!connection_.get()) { |
mukesh agrawal | 23ac6b7 | 2013-01-31 18:52:37 -0800 | [diff] [blame] | 547 | connection_ = new Connection(interface_index_, |
| 548 | link_name_, |
| 549 | technology_, |
| 550 | manager_->device_info()); |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 551 | } |
| 552 | } |
| 553 | |
| 554 | void Device::DestroyConnection() { |
mukesh agrawal | 6813e76 | 2013-07-10 19:05:08 -0700 | [diff] [blame] | 555 | SLOG(Device, 2) << __func__ << " on " << link_name_; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 556 | StopPortalDetection(); |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 557 | StopLinkMonitor(); |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 558 | StopTrafficMonitor(); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 559 | if (selected_service_.get()) { |
mukesh agrawal | 6813e76 | 2013-07-10 19:05:08 -0700 | [diff] [blame] | 560 | SLOG(Device, 3) << "Clearing connection of service " |
| 561 | << selected_service_->unique_name(); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 562 | selected_service_->SetConnection(NULL); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 563 | } |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 564 | connection_ = NULL; |
Arman Uguray | f84a424 | 2013-04-09 20:01:07 -0700 | [diff] [blame] | 565 | health_checker_.reset(); |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 566 | } |
| 567 | |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 568 | void Device::SelectService(const ServiceRefPtr &service) { |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 569 | SLOG(Device, 2) << __func__ << ": service " |
mukesh agrawal | 6813e76 | 2013-07-10 19:05:08 -0700 | [diff] [blame] | 570 | << (service ? service->unique_name() : "*reset*") |
| 571 | << " on " << link_name_; |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 572 | |
| 573 | if (selected_service_.get() == service.get()) { |
| 574 | // No change to |selected_service_|. Return early to avoid |
| 575 | // changing its state. |
| 576 | return; |
| 577 | } |
| 578 | |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 579 | if (selected_service_.get()) { |
| 580 | if (selected_service_->state() != Service::kStateFailure) { |
| 581 | selected_service_->SetState(Service::kStateIdle); |
| 582 | } |
Paul Stewart | 20b0a09 | 2012-05-22 20:39:57 -0700 | [diff] [blame] | 583 | // Just in case the Device subclass has not already done so, make |
| 584 | // sure the previously selected service has its connection removed. |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 585 | selected_service_->SetConnection(NULL); |
Paul Stewart | c886061 | 2012-09-28 07:36:21 -0700 | [diff] [blame] | 586 | StopLinkMonitor(); |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 587 | StopTrafficMonitor(); |
Paul Stewart | c886061 | 2012-09-28 07:36:21 -0700 | [diff] [blame] | 588 | StopPortalDetection(); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 589 | } |
| 590 | selected_service_ = service; |
| 591 | } |
| 592 | |
| 593 | void Device::SetServiceState(Service::ConnectState state) { |
| 594 | if (selected_service_.get()) { |
| 595 | selected_service_->SetState(state); |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | void Device::SetServiceFailure(Service::ConnectFailure failure_state) { |
| 600 | if (selected_service_.get()) { |
| 601 | selected_service_->SetFailure(failure_state); |
| 602 | } |
| 603 | } |
| 604 | |
Eric Shienbrood | cc95c5d | 2012-03-30 15:25:49 -0400 | [diff] [blame] | 605 | void Device::SetServiceFailureSilent(Service::ConnectFailure failure_state) { |
| 606 | if (selected_service_.get()) { |
| 607 | selected_service_->SetFailureSilent(failure_state); |
| 608 | } |
| 609 | } |
| 610 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 611 | string Device::SerializeIPConfigs(const string &suffix) { |
| 612 | return StringPrintf("%s:%s", suffix.c_str(), ipconfig_->type().c_str()); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 613 | } |
| 614 | |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 615 | bool Device::SetIPFlag(IPAddress::Family family, const string &flag, |
| 616 | const string &value) { |
| 617 | string ip_version; |
| 618 | if (family == IPAddress::kFamilyIPv4) { |
| 619 | ip_version = kIPFlagVersion4; |
| 620 | } else if (family == IPAddress::kFamilyIPv6) { |
| 621 | ip_version = kIPFlagVersion6; |
| 622 | } else { |
| 623 | NOTIMPLEMENTED(); |
| 624 | } |
| 625 | FilePath flag_file(StringPrintf(kIPFlagTemplate, ip_version.c_str(), |
| 626 | link_name_.c_str(), flag.c_str())); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 627 | SLOG(Device, 2) << "Writing " << value << " to flag file " |
| 628 | << flag_file.value(); |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 629 | if (file_util::WriteFile(flag_file, value.c_str(), value.length()) != 1) { |
| 630 | LOG(ERROR) << StringPrintf("IP flag write failed: %s to %s", |
| 631 | value.c_str(), flag_file.value().c_str()); |
| 632 | return false; |
| 633 | } |
| 634 | return true; |
| 635 | } |
| 636 | |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 637 | void Device::ResetByteCounters() { |
| 638 | manager_->device_info()->GetByteCounts( |
| 639 | interface_index_, &receive_byte_offset_, &transmit_byte_offset_); |
| 640 | manager_->UpdateDevice(this); |
| 641 | } |
| 642 | |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 643 | void Device::SetupConnectionHealthChecker() { |
| 644 | DCHECK(connection_); |
| 645 | if (!health_checker_.get()) { |
| 646 | health_checker_.reset(new ConnectionHealthChecker( |
| 647 | connection_, |
| 648 | dispatcher_, |
| 649 | manager_->health_checker_remote_ips(), |
| 650 | Bind(&Device::OnConnectionHealthCheckerResult, |
| 651 | weak_ptr_factory_.GetWeakPtr()))); |
| 652 | } else { |
| 653 | health_checker_->SetConnection(connection_); |
| 654 | } |
| 655 | // Add URL in either case because a connection reset could have dropped past |
| 656 | // DNS queries. |
| 657 | health_checker_->AddRemoteURL(manager_->GetPortalCheckURL()); |
| 658 | } |
| 659 | |
Arman Uguray | f84a424 | 2013-04-09 20:01:07 -0700 | [diff] [blame] | 660 | void Device::RequestConnectionHealthCheck() { |
| 661 | if (!health_checker_.get()) { |
| 662 | SLOG(Device, 2) << "No health checker exists, cannot request " |
| 663 | << "health check."; |
| 664 | return; |
| 665 | } |
| 666 | if (health_checker_->health_check_in_progress()) { |
| 667 | SLOG(Device, 2) << "Health check already in progress."; |
| 668 | return; |
| 669 | } |
| 670 | health_checker_->Start(); |
| 671 | } |
| 672 | |
| 673 | void Device::OnConnectionHealthCheckerResult( |
| 674 | ConnectionHealthChecker::Result result) { |
Prathmesh Prabhu | 5489b7a | 2013-04-10 13:33:59 -0700 | [diff] [blame] | 675 | SLOG(Device, 2) |
| 676 | << FriendlyName() |
| 677 | << ": ConnectionHealthChecker result: " |
| 678 | << ConnectionHealthChecker::ResultToString(result); |
Arman Uguray | f84a424 | 2013-04-09 20:01:07 -0700 | [diff] [blame] | 679 | } |
| 680 | |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 681 | bool Device::RestartPortalDetection() { |
| 682 | StopPortalDetection(); |
| 683 | return StartPortalDetection(); |
| 684 | } |
| 685 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 686 | bool Device::RequestPortalDetection() { |
| 687 | if (!selected_service_) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 688 | SLOG(Device, 2) << FriendlyName() |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 689 | << ": No selected service, so no need for portal check."; |
| 690 | return false; |
| 691 | } |
| 692 | |
| 693 | if (!connection_.get()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 694 | SLOG(Device, 2) << FriendlyName() |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 695 | << ": No connection, so no need for portal check."; |
| 696 | return false; |
| 697 | } |
| 698 | |
| 699 | if (selected_service_->state() != Service::kStatePortal) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 700 | SLOG(Device, 2) << FriendlyName() |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 701 | << ": Service is not in portal state. No need to start check."; |
| 702 | return false; |
| 703 | } |
| 704 | |
| 705 | if (!connection_->is_default()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 706 | SLOG(Device, 2) << FriendlyName() |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 707 | << ": Service is not the default connection. Don't start check."; |
| 708 | return false; |
| 709 | } |
| 710 | |
| 711 | if (portal_detector_.get() && portal_detector_->IsInProgress()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 712 | SLOG(Device, 2) << FriendlyName() |
| 713 | << ": Portal detection is already running."; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 714 | return true; |
| 715 | } |
| 716 | |
| 717 | return StartPortalDetection(); |
| 718 | } |
| 719 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 720 | bool Device::StartPortalDetection() { |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 721 | DCHECK(selected_service_); |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 722 | if (selected_service_->IsPortalDetectionDisabled()) { |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 723 | SLOG(Device, 2) << "Service " << selected_service_->unique_name() |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 724 | << ": Portal detection is disabled; " |
| 725 | << "marking service online."; |
| 726 | SetServiceConnectedState(Service::kStateOnline); |
| 727 | return false; |
| 728 | } |
| 729 | |
| 730 | if (selected_service_->IsPortalDetectionAuto() && |
| 731 | !manager_->IsPortalDetectionEnabled(technology())) { |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 732 | // If portal detection is disabled for this technology, immediately set |
| 733 | // the service state to "Online". |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 734 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 735 | << ": Portal detection is disabled; " |
| 736 | << "marking service online."; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 737 | SetServiceConnectedState(Service::kStateOnline); |
| 738 | return false; |
| 739 | } |
| 740 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 741 | if (selected_service_->HasProxyConfig()) { |
| 742 | // Services with HTTP proxy configurations should not be checked by the |
| 743 | // connection manager, since we don't have the ability to evaluate |
| 744 | // arbitrary proxy configs and their possible credentials. |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 745 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 746 | << ": Service has proxy config; marking it online."; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 747 | SetServiceConnectedState(Service::kStateOnline); |
| 748 | return false; |
| 749 | } |
| 750 | |
| 751 | portal_detector_.reset(new PortalDetector(connection_, |
| 752 | dispatcher_, |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 753 | portal_detector_callback_)); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 754 | if (!portal_detector_->Start(manager_->GetPortalCheckURL())) { |
| 755 | LOG(ERROR) << "Device " << FriendlyName() |
| 756 | << ": Portal detection failed to start: likely bad URL: " |
| 757 | << manager_->GetPortalCheckURL(); |
| 758 | SetServiceConnectedState(Service::kStateOnline); |
| 759 | return false; |
| 760 | } |
| 761 | |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 762 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 763 | << ": Portal detection has started."; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 764 | return true; |
| 765 | } |
| 766 | |
| 767 | void Device::StopPortalDetection() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 768 | SLOG(Device, 2) << "Device " << FriendlyName() |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 769 | << ": Portal detection stopping."; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 770 | portal_detector_.reset(); |
| 771 | } |
| 772 | |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 773 | void Device::set_link_monitor(LinkMonitor *link_monitor) { |
| 774 | link_monitor_.reset(link_monitor); |
| 775 | } |
| 776 | |
| 777 | bool Device::StartLinkMonitor() { |
| 778 | if (!manager_->IsTechnologyLinkMonitorEnabled(technology())) { |
| 779 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 780 | << ": Link Monitoring is disabled."; |
| 781 | return false; |
| 782 | } |
| 783 | |
| 784 | if (!link_monitor()) { |
| 785 | set_link_monitor( |
| 786 | new LinkMonitor( |
| 787 | connection_, dispatcher_, metrics(), manager_->device_info(), |
| 788 | Bind(&Device::OnLinkMonitorFailure, weak_ptr_factory_.GetWeakPtr()))); |
| 789 | } |
| 790 | |
| 791 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 792 | << ": Link Monitor starting."; |
| 793 | return link_monitor_->Start(); |
| 794 | } |
| 795 | |
| 796 | void Device::StopLinkMonitor() { |
| 797 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 798 | << ": Link Monitor stopping."; |
| 799 | link_monitor_.reset(); |
| 800 | } |
| 801 | |
| 802 | void Device::OnLinkMonitorFailure() { |
| 803 | LOG(ERROR) << "Device " << FriendlyName() |
| 804 | << ": Link Monitor indicates failure."; |
| 805 | } |
| 806 | |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 807 | void Device::set_traffic_monitor(TrafficMonitor *traffic_monitor) { |
| 808 | traffic_monitor_.reset(traffic_monitor); |
| 809 | } |
| 810 | |
| 811 | bool Device::StartTrafficMonitor() { |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 812 | SLOG(Device, 2) << __func__; |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 813 | if (!traffic_monitor_enabled_) { |
| 814 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 815 | << ": Traffic Monitoring is disabled."; |
| 816 | return false; |
| 817 | } |
| 818 | |
| 819 | if (!traffic_monitor_.get()) { |
| 820 | traffic_monitor_.reset(new TrafficMonitor(this, dispatcher_)); |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 821 | traffic_monitor_->set_tcp_out_traffic_not_routed_callback( |
| 822 | Bind(&Device::OnNoNetworkRouting, weak_ptr_factory_.GetWeakPtr())); |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 826 | << ": Traffic Monitor starting."; |
| 827 | traffic_monitor_->Start(); |
| 828 | return true; |
| 829 | } |
| 830 | |
| 831 | void Device::StopTrafficMonitor() { |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 832 | SLOG(Device, 2) << __func__; |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 833 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 834 | << ": Traffic Monitor stopping."; |
| 835 | traffic_monitor_.reset(); |
| 836 | } |
| 837 | |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 838 | void Device::OnNoNetworkRouting() { |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 839 | SLOG(Device, 2) << "Device " << FriendlyName() |
Arman Uguray | f84a424 | 2013-04-09 20:01:07 -0700 | [diff] [blame] | 840 | << ": Traffic Monitor detects network congestion."; |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 841 | } |
| 842 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 843 | void Device::SetServiceConnectedState(Service::ConnectState state) { |
| 844 | DCHECK(selected_service_.get()); |
| 845 | |
| 846 | if (!selected_service_.get()) { |
| 847 | LOG(ERROR) << FriendlyName() << ": " |
| 848 | << "Portal detection completed but no selected service exists!"; |
| 849 | return; |
| 850 | } |
| 851 | |
| 852 | if (!selected_service_->IsConnected()) { |
| 853 | LOG(ERROR) << FriendlyName() << ": " |
| 854 | << "Portal detection completed but selected service " |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 855 | << selected_service_->unique_name() |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 856 | << " is in non-connected state."; |
| 857 | return; |
| 858 | } |
| 859 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 860 | if (state == Service::kStatePortal && connection_->is_default() && |
| 861 | manager_->GetPortalCheckInterval() != 0) { |
| 862 | CHECK(portal_detector_.get()); |
| 863 | if (!portal_detector_->StartAfterDelay( |
| 864 | manager_->GetPortalCheckURL(), |
| 865 | manager_->GetPortalCheckInterval())) { |
| 866 | LOG(ERROR) << "Device " << FriendlyName() |
| 867 | << ": Portal detection failed to restart: likely bad URL: " |
| 868 | << manager_->GetPortalCheckURL(); |
| 869 | SetServiceState(Service::kStateOnline); |
| 870 | portal_detector_.reset(); |
| 871 | return; |
| 872 | } |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 873 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 874 | << ": Portal detection retrying."; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 875 | } else { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 876 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 877 | << ": Portal will not retry."; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 878 | portal_detector_.reset(); |
| 879 | } |
| 880 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 881 | SetServiceState(state); |
| 882 | } |
| 883 | |
| 884 | void Device::PortalDetectorCallback(const PortalDetector::Result &result) { |
| 885 | if (!result.final) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 886 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 887 | << ": Received non-final status: " |
| 888 | << PortalDetector::StatusToString(result.status); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 889 | return; |
| 890 | } |
| 891 | |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 892 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 893 | << ": Received final status: " |
| 894 | << PortalDetector::StatusToString(result.status); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 895 | |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 896 | portal_attempts_to_online_ += result.num_attempts; |
| 897 | |
| 898 | metrics()->SendEnumToUMA( |
| 899 | metrics()->GetFullMetricName(Metrics::kMetricPortalResult, technology()), |
| 900 | Metrics::PortalDetectionResultToEnum(result), |
| 901 | Metrics::kPortalResultMax); |
| 902 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 903 | if (result.status == PortalDetector::kStatusSuccess) { |
| 904 | SetServiceConnectedState(Service::kStateOnline); |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 905 | |
| 906 | metrics()->SendToUMA( |
| 907 | metrics()->GetFullMetricName( |
| 908 | Metrics::kMetricPortalAttemptsToOnline, technology()), |
| 909 | portal_attempts_to_online_, |
| 910 | Metrics::kMetricPortalAttemptsToOnlineMin, |
| 911 | Metrics::kMetricPortalAttemptsToOnlineMax, |
| 912 | Metrics::kMetricPortalAttemptsToOnlineNumBuckets); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 913 | } else { |
| 914 | SetServiceConnectedState(Service::kStatePortal); |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 915 | |
| 916 | metrics()->SendToUMA( |
| 917 | metrics()->GetFullMetricName( |
| 918 | Metrics::kMetricPortalAttempts, technology()), |
| 919 | result.num_attempts, |
| 920 | Metrics::kMetricPortalAttemptsMin, |
| 921 | Metrics::kMetricPortalAttemptsMax, |
| 922 | Metrics::kMetricPortalAttemptsNumBuckets); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 923 | } |
| 924 | } |
| 925 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 926 | vector<string> Device::AvailableIPConfigs(Error */*error*/) { |
Jason Glasgow | 08afdff | 2012-04-03 10:22:26 -0400 | [diff] [blame] | 927 | if (ipconfig_.get()) { |
| 928 | string id = ipconfig_->GetRpcIdentifier(); |
| 929 | return vector<string>(1, id); |
| 930 | } |
| 931 | return vector<string>(); |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | string Device::GetRpcConnectionIdentifier() { |
| 935 | return adaptor_->GetRpcConnectionIdentifier(); |
| 936 | } |
| 937 | |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 938 | uint64 Device::GetLinkMonitorResponseTime(Error *error) { |
| 939 | if (!link_monitor_.get()) { |
| 940 | // It is not strictly an error that the link monitor does not |
| 941 | // exist, but returning an error here allows the GetProperties |
| 942 | // call in our Adaptor to omit this parameter. |
| 943 | error->Populate(Error::kNotFound, "Device is not running LinkMonitor"); |
| 944 | return 0; |
| 945 | } |
| 946 | return link_monitor_->GetResponseTimeMilliseconds(); |
| 947 | } |
| 948 | |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 949 | uint64 Device::GetReceiveByteCount() { |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 950 | uint64 rx_byte_count = 0, tx_byte_count = 0; |
| 951 | manager_->device_info()->GetByteCounts( |
| 952 | interface_index_, &rx_byte_count, &tx_byte_count); |
| 953 | return rx_byte_count - receive_byte_offset_; |
| 954 | } |
| 955 | |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 956 | uint64 Device::GetTransmitByteCount() { |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 957 | uint64 rx_byte_count = 0, tx_byte_count = 0; |
| 958 | manager_->device_info()->GetByteCounts( |
| 959 | interface_index_, &rx_byte_count, &tx_byte_count); |
| 960 | return tx_byte_count - transmit_byte_offset_; |
| 961 | } |
| 962 | |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 963 | uint64 Device::GetReceiveByteCountProperty(Error */*error*/) { |
| 964 | return GetReceiveByteCount(); |
| 965 | } |
| 966 | |
| 967 | uint64 Device::GetTransmitByteCountProperty(Error */*error*/) { |
| 968 | return GetTransmitByteCount(); |
| 969 | } |
| 970 | |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 971 | bool Device::IsUnderlyingDeviceEnabled() const { |
| 972 | return false; |
| 973 | } |
| 974 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 975 | // callback |
| 976 | void Device::OnEnabledStateChanged(const ResultCallback &callback, |
| 977 | const Error &error) { |
mukesh agrawal | 46c27cc | 2013-07-10 16:39:10 -0700 | [diff] [blame] | 978 | SLOG(Device, 2) << __func__ |
| 979 | << " (target: " << enabled_pending_ << "," |
| 980 | << " success: " << error.IsSuccess() << ")" |
| 981 | << " on " << link_name_; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 982 | if (error.IsSuccess()) { |
| 983 | enabled_ = enabled_pending_; |
| 984 | manager_->UpdateEnabledTechnologies(); |
| 985 | adaptor_->EmitBoolChanged(flimflam::kPoweredProperty, enabled_); |
| 986 | adaptor_->UpdateEnabled(); |
| 987 | } |
Gary Morain | baeefdf | 2012-04-30 14:53:35 -0700 | [diff] [blame] | 988 | enabled_pending_ = enabled_; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 989 | if (!callback.is_null()) |
| 990 | callback.Run(error); |
| 991 | } |
| 992 | |
| 993 | void Device::SetEnabled(bool enable) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 994 | SLOG(Device, 2) << __func__ << "(" << enable << ")"; |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 995 | Error error; |
| 996 | SetEnabledInternal(enable, false, &error, ResultCallback()); |
Jason Glasgow | 7234ec3 | 2012-05-23 16:01:21 -0400 | [diff] [blame] | 997 | LOG_IF(ERROR, error.IsFailure() && !error.IsOngoing()) |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 998 | << "Enabled failed, but no way to report the failure."; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | void Device::SetEnabledPersistent(bool enable, |
| 1002 | Error *error, |
| 1003 | const ResultCallback &callback) { |
| 1004 | SetEnabledInternal(enable, true, error, callback); |
| 1005 | } |
| 1006 | |
| 1007 | void Device::SetEnabledInternal(bool enable, |
| 1008 | bool persist, |
| 1009 | Error *error, |
| 1010 | const ResultCallback &callback) { |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 1011 | DCHECK(error); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 1012 | SLOG(Device, 2) << "Device " << link_name_ << " " |
| 1013 | << (enable ? "starting" : "stopping"); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1014 | if (enable == enabled_) { |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 1015 | error->Reset(); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1016 | return; |
| 1017 | } |
| 1018 | |
| 1019 | if (enabled_pending_ == enable) { |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 1020 | Error::PopulateAndLog(error, Error::kInProgress, |
| 1021 | "Enable operation already in progress"); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1022 | return; |
| 1023 | } |
| 1024 | |
| 1025 | if (persist) { |
| 1026 | enabled_persistent_ = enable; |
Darin Petkov | e7c6ad3 | 2012-06-29 10:22:09 +0200 | [diff] [blame] | 1027 | manager_->UpdateDevice(this); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | enabled_pending_ = enable; |
| 1031 | EnabledStateChangedCallback enabled_callback = |
| 1032 | Bind(&Device::OnEnabledStateChanged, |
| 1033 | weak_ptr_factory_.GetWeakPtr(), callback); |
| 1034 | if (enable) { |
| 1035 | running_ = true; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1036 | Start(error, enabled_callback); |
| 1037 | } else { |
| 1038 | running_ = false; |
| 1039 | DestroyIPConfig(); // breaks a reference cycle |
| 1040 | SelectService(NULL); // breaks a reference cycle |
| 1041 | rtnl_handler_->SetInterfaceFlags(interface_index(), 0, IFF_UP); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 1042 | SLOG(Device, 3) << "Device " << link_name_ << " ipconfig_ " |
| 1043 | << (ipconfig_ ? "is set." : "is not set."); |
| 1044 | SLOG(Device, 3) << "Device " << link_name_ << " connection_ " |
| 1045 | << (connection_ ? "is set." : "is not set."); |
| 1046 | SLOG(Device, 3) << "Device " << link_name_ << " selected_service_ " |
| 1047 | << (selected_service_ ? "is set." : "is not set."); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1048 | Stop(error, enabled_callback); |
| 1049 | } |
| 1050 | } |
| 1051 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 1052 | } // namespace shill |