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