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 | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 17 | #include <base/logging.h> |
Chris Masone | 487b8bf | 2011-05-13 16:27:57 -0700 | [diff] [blame] | 18 | #include <base/memory/ref_counted.h> |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 19 | #include <base/stringprintf.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 20 | #include <chromeos/dbus/service_constants.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 21 | |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 22 | #include "shill/connection.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 23 | #include "shill/control_interface.h" |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 24 | #include "shill/device_dbus_adaptor.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 25 | #include "shill/dhcp_config.h" |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 26 | #include "shill/dhcp_provider.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 27 | #include "shill/error.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 28 | #include "shill/event_dispatcher.h" |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 29 | #include "shill/http_proxy.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 30 | #include "shill/manager.h" |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 31 | #include "shill/metrics.h" |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 32 | #include "shill/property_accessor.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 33 | #include "shill/refptr_types.h" |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 34 | #include "shill/rtnl_handler.h" |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 35 | #include "shill/scope_logger.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 36 | #include "shill/service.h" |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 37 | #include "shill/store_interface.h" |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 38 | #include "shill/technology.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 39 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 40 | using base::Bind; |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 41 | using base::StringPrintf; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 42 | using std::string; |
| 43 | using std::vector; |
| 44 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 45 | namespace shill { |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 46 | |
| 47 | // static |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 48 | const char Device::kIPFlagTemplate[] = "/proc/sys/net/%s/conf/%s/%s"; |
| 49 | // static |
| 50 | const char Device::kIPFlagVersion4[] = "ipv4"; |
| 51 | // static |
| 52 | const char Device::kIPFlagVersion6[] = "ipv6"; |
| 53 | // static |
| 54 | const char Device::kIPFlagDisableIPv6[] = "disable_ipv6"; |
| 55 | // static |
| 56 | const char Device::kIPFlagUseTempAddr[] = "use_tempaddr"; |
| 57 | // static |
| 58 | const char Device::kIPFlagUseTempAddrUsedAndDefault[] = "2"; |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 59 | // static |
| 60 | const char Device::kIPFlagReversePathFilter[] = "rp_filter"; |
| 61 | // static |
| 62 | const char Device::kIPFlagReversePathFilterEnabled[] = "1"; |
| 63 | // static |
| 64 | const char Device::kIPFlagReversePathFilterLooseMode[] = "2"; |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 65 | |
| 66 | // static |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 67 | const char Device::kStoragePowered[] = "Powered"; |
| 68 | |
| 69 | // static |
| 70 | const char Device::kStorageIPConfigs[] = "IPConfigs"; |
| 71 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 72 | Device::Device(ControlInterface *control_interface, |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 73 | EventDispatcher *dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 74 | Metrics *metrics, |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 75 | Manager *manager, |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 76 | const string &link_name, |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 77 | const string &address, |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 78 | int interface_index, |
| 79 | Technology::Identifier technology) |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 80 | : enabled_(false), |
| 81 | enabled_persistent_(true), |
| 82 | enabled_pending_(enabled_), |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 83 | reconnect_(true), |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 84 | hardware_address_(address), |
mukesh agrawal | f60e406 | 2011-05-27 13:13:41 -0700 | [diff] [blame] | 85 | interface_index_(interface_index), |
| 86 | running_(false), |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 87 | link_name_(link_name), |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 88 | unique_id_(link_name), |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 89 | control_interface_(control_interface), |
| 90 | dispatcher_(dispatcher), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 91 | metrics_(metrics), |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 92 | manager_(manager), |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 93 | weak_ptr_factory_(this), |
Darin Petkov | 77cb681 | 2011-08-15 16:19:41 -0700 | [diff] [blame] | 94 | adaptor_(control_interface->CreateDeviceAdaptor(this)), |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 95 | portal_detector_callback_(Bind(&Device::PortalDetectorCallback, |
| 96 | weak_ptr_factory_.GetWeakPtr())), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 97 | technology_(technology), |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 98 | portal_attempts_to_online_(0), |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 99 | dhcp_provider_(DHCPProvider::GetInstance()), |
| 100 | rtnl_handler_(RTNLHandler::GetInstance()) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 101 | store_.RegisterConstString(flimflam::kAddressProperty, &hardware_address_); |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 102 | |
| 103 | // flimflam::kBgscanMethodProperty: Registered in WiFi |
| 104 | // flimflam::kBgscanShortIntervalProperty: Registered in WiFi |
| 105 | // flimflam::kBgscanSignalThresholdProperty: Registered in WiFi |
| 106 | |
| 107 | // flimflam::kCellularAllowRoamingProperty: Registered in Cellular |
| 108 | // flimflam::kCarrierProperty: Registered in Cellular |
| 109 | // flimflam::kEsnProperty: Registered in Cellular |
Darin Petkov | 3335b37 | 2011-08-22 11:05:32 -0700 | [diff] [blame] | 110 | // flimflam::kHomeProviderProperty: Registered in Cellular |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 111 | // flimflam::kImeiProperty: Registered in Cellular |
| 112 | // flimflam::kImsiProperty: Registered in Cellular |
| 113 | // flimflam::kManufacturerProperty: Registered in Cellular |
| 114 | // flimflam::kMdnProperty: Registered in Cellular |
| 115 | // flimflam::kMeidProperty: Registered in Cellular |
| 116 | // flimflam::kMinProperty: Registered in Cellular |
| 117 | // flimflam::kModelIDProperty: Registered in Cellular |
| 118 | // flimflam::kFirmwareRevisionProperty: Registered in Cellular |
| 119 | // flimflam::kHardwareRevisionProperty: Registered in Cellular |
| 120 | // flimflam::kPRLVersionProperty: Registered in Cellular |
| 121 | // flimflam::kSIMLockStatusProperty: Registered in Cellular |
| 122 | // flimflam::kFoundNetworksProperty: Registered in Cellular |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 123 | // flimflam::kDBusConnectionProperty: Registered in Cellular |
| 124 | // flimflam::kDBusObjectProperty: Register in Cellular |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 125 | |
Jason Glasgow | e8334fd | 2012-03-30 16:02:37 -0400 | [diff] [blame] | 126 | store_.RegisterConstString(flimflam::kInterfaceProperty, &link_name_); |
Jason Glasgow | 08afdff | 2012-04-03 10:22:26 -0400 | [diff] [blame] | 127 | HelpRegisterConstDerivedRpcIdentifiers(flimflam::kIPConfigsProperty, |
| 128 | &Device::AvailableIPConfigs); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 129 | store_.RegisterConstString(flimflam::kNameProperty, &link_name_); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 130 | store_.RegisterConstBool(flimflam::kPoweredProperty, &enabled_); |
Jason Glasgow | b579005 | 2012-01-27 01:03:52 -0500 | [diff] [blame] | 131 | HelpRegisterDerivedString(flimflam::kTypeProperty, |
| 132 | &Device::GetTechnologyString, |
| 133 | NULL); |
| 134 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 135 | // TODO(cmasone): Chrome doesn't use this...does anyone? |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 136 | // store_.RegisterConstBool(flimflam::kReconnectProperty, &reconnect_); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 137 | |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 138 | // TODO(cmasone): Figure out what shill concept maps to flimflam's "Network". |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 139 | // known_properties_.push_back(flimflam::kNetworksProperty); |
| 140 | |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 141 | // flimflam::kScanningProperty: Registered in WiFi, Cellular |
| 142 | // flimflam::kScanIntervalProperty: Registered in WiFi, Cellular |
| 143 | |
| 144 | // TODO(pstew): Initialize Interface monitor, so we can detect new interfaces |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 145 | SLOG(Device, 2) << "Device " << link_name_ << " index " << interface_index; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | Device::~Device() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 149 | SLOG(Device, 2) << "Device " << link_name_ << " destroyed."; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 150 | } |
| 151 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 152 | bool Device::TechnologyIs(const Technology::Identifier /*type*/) const { |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 153 | return false; |
| 154 | } |
| 155 | |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 156 | void Device::LinkEvent(unsigned flags, unsigned change) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 157 | SLOG(Device, 2) << "Device " << link_name_ |
| 158 | << std::showbase << std::hex |
| 159 | << " flags " << flags << " changed " << change |
| 160 | << std::dec << std::noshowbase; |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Darin Petkov | c086531 | 2011-09-16 15:31:20 -0700 | [diff] [blame] | 163 | void Device::Scan(Error *error) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 164 | SLOG(Device, 2) << "Device " << link_name_ << " scan requested."; |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 165 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 166 | "Device doesn't support scan."); |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 169 | void Device::RegisterOnNetwork(const std::string &/*network_id*/, Error *error, |
| 170 | const ResultCallback &/*callback*/) { |
| 171 | Error::PopulateAndLog(error, Error::kNotSupported, |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 172 | "Device doesn't support network registration."); |
Darin Petkov | 9ae310f | 2011-08-30 15:41:13 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Darin Petkov | c64fe5e | 2012-01-11 12:46:13 +0100 | [diff] [blame] | 175 | void Device::RequirePIN( |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 176 | const string &/*pin*/, bool /*require*/, |
| 177 | Error *error, const ResultCallback &/*callback*/) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 178 | SLOG(Device, 2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 179 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 180 | "Device doesn't support RequirePIN."); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 183 | void Device::EnterPIN(const string &/*pin*/, |
| 184 | Error *error, const ResultCallback &/*callback*/) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 185 | SLOG(Device, 2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 186 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 187 | "Device doesn't support EnterPIN."); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 188 | } |
| 189 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 190 | void Device::UnblockPIN(const string &/*unblock_code*/, |
| 191 | const string &/*pin*/, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 192 | Error *error, const ResultCallback &/*callback*/) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 193 | SLOG(Device, 2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 194 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 195 | "Device doesn't support UnblockPIN."); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 196 | } |
| 197 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 198 | void Device::ChangePIN(const string &/*old_pin*/, |
| 199 | const string &/*new_pin*/, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 200 | Error *error, const ResultCallback &/*callback*/) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 201 | SLOG(Device, 2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 202 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 203 | "Device doesn't support ChangePIN."); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 206 | void Device::DisableIPv6() { |
| 207 | SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "1"); |
| 208 | } |
| 209 | |
| 210 | void Device::EnableIPv6() { |
| 211 | SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "0"); |
| 212 | } |
| 213 | |
| 214 | void Device::EnableIPv6Privacy() { |
| 215 | SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagUseTempAddr, |
| 216 | kIPFlagUseTempAddrUsedAndDefault); |
| 217 | } |
| 218 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 219 | void Device::DisableReversePathFilter() { |
| 220 | // TODO(pstew): Current kernel doesn't offer reverse-path filtering flag |
| 221 | // for IPv6. crosbug.com/24228 |
| 222 | SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter, |
| 223 | kIPFlagReversePathFilterLooseMode); |
| 224 | } |
| 225 | |
| 226 | void Device::EnableReversePathFilter() { |
| 227 | SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter, |
| 228 | kIPFlagReversePathFilterEnabled); |
| 229 | } |
| 230 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 231 | bool Device::IsConnected() const { |
| 232 | if (selected_service_) |
| 233 | return selected_service_->IsConnected(); |
| 234 | return false; |
| 235 | } |
| 236 | |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 237 | bool Device::IsConnectedToService(const ServiceRefPtr &service) const { |
| 238 | return service == selected_service_ && IsConnected(); |
| 239 | } |
| 240 | |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 241 | string Device::GetRpcIdentifier() { |
| 242 | return adaptor_->GetRpcIdentifier(); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 245 | string Device::GetStorageIdentifier() { |
| 246 | string id = GetRpcIdentifier(); |
| 247 | ControlInterface::RpcIdToStorageId(&id); |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 248 | size_t needle = id.find('_'); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 249 | DLOG_IF(ERROR, needle == string::npos) << "No _ in storage id?!?!"; |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 250 | id.replace(id.begin() + needle + 1, id.end(), hardware_address_); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 251 | return id; |
| 252 | } |
| 253 | |
Jason Glasgow | b579005 | 2012-01-27 01:03:52 -0500 | [diff] [blame] | 254 | string Device::GetTechnologyString(Error */*error*/) { |
| 255 | return Technology::NameFromIdentifier(technology()); |
| 256 | } |
| 257 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 258 | const string& Device::FriendlyName() const { |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 259 | return link_name_; |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 262 | const string& Device::UniqueName() const { |
| 263 | return unique_id_; |
| 264 | } |
| 265 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 266 | bool Device::Load(StoreInterface *storage) { |
| 267 | const string id = GetStorageIdentifier(); |
| 268 | if (!storage->ContainsGroup(id)) { |
| 269 | LOG(WARNING) << "Device is not available in the persistent store: " << id; |
| 270 | return false; |
| 271 | } |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 272 | enabled_persistent_ = true; |
| 273 | storage->GetBool(id, kStoragePowered, &enabled_persistent_); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 274 | // TODO(cmasone): What does it mean to load an IPConfig identifier?? |
| 275 | return true; |
| 276 | } |
| 277 | |
| 278 | bool Device::Save(StoreInterface *storage) { |
| 279 | const string id = GetStorageIdentifier(); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 280 | storage->SetBool(id, kStoragePowered, enabled_persistent_); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 281 | if (ipconfig_.get()) { |
| 282 | // The _0 is an index into the list of IPConfigs that this device might |
| 283 | // have. We only have one IPConfig right now, and I hope to never have |
| 284 | // to support more, as sleffler indicates that associating IPConfigs |
| 285 | // with devices is wrong and due to be changed in flimflam anyhow. |
| 286 | string suffix = hardware_address_ + "_0"; |
| 287 | ipconfig_->Save(storage, suffix); |
| 288 | storage->SetString(id, kStorageIPConfigs, SerializeIPConfigs(suffix)); |
| 289 | } |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 290 | return true; |
| 291 | } |
| 292 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 293 | void Device::DestroyIPConfig() { |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 294 | DisableIPv6(); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 295 | if (ipconfig_.get()) { |
| 296 | ipconfig_->ReleaseIP(); |
| 297 | ipconfig_ = NULL; |
| 298 | } |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 299 | DestroyConnection(); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 302 | bool Device::AcquireIPConfig() { |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 303 | return AcquireIPConfigWithLeaseName(string()); |
| 304 | } |
| 305 | |
| 306 | bool Device::AcquireIPConfigWithLeaseName(const string &lease_name) { |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 307 | DestroyIPConfig(); |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 308 | EnableIPv6(); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 309 | ipconfig_ = dhcp_provider_->CreateConfig(link_name_, |
| 310 | manager_->GetHostName(), |
| 311 | lease_name, |
| 312 | manager_->GetArpGateway()); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 313 | ipconfig_->RegisterUpdateCallback(Bind(&Device::OnIPConfigUpdated, |
| 314 | weak_ptr_factory_.GetWeakPtr())); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 315 | dispatcher_->PostTask(Bind(&Device::ConfigureStaticIPTask, |
| 316 | weak_ptr_factory_.GetWeakPtr())); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 317 | return ipconfig_->RequestIP(); |
| 318 | } |
| 319 | |
Jason Glasgow | b579005 | 2012-01-27 01:03:52 -0500 | [diff] [blame] | 320 | void Device::HelpRegisterDerivedString( |
| 321 | const string &name, |
| 322 | string(Device::*get)(Error *error), |
| 323 | void(Device::*set)(const string &value, Error *error)) { |
| 324 | store_.RegisterDerivedString( |
| 325 | name, |
| 326 | StringAccessor(new CustomAccessor<Device, string>(this, get, set))); |
| 327 | } |
| 328 | |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 329 | void Device::HelpRegisterDerivedStrings( |
| 330 | const string &name, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 331 | Strings(Device::*get)(Error *error), |
| 332 | void(Device::*set)(const Strings &value, Error *error)) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 333 | store_.RegisterDerivedStrings( |
| 334 | name, |
| 335 | StringsAccessor(new CustomAccessor<Device, Strings>(this, get, set))); |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Jason Glasgow | 08afdff | 2012-04-03 10:22:26 -0400 | [diff] [blame] | 338 | void Device::HelpRegisterConstDerivedRpcIdentifiers( |
| 339 | const string &name, |
| 340 | RpcIdentifiers(Device::*get)(Error *)) { |
| 341 | store_.RegisterDerivedRpcIdentifiers( |
| 342 | name, |
| 343 | RpcIdentifiersAccessor( |
| 344 | new CustomAccessor<Device, RpcIdentifiers>(this, get, NULL))); |
| 345 | } |
| 346 | |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 347 | void Device::ConfigureStaticIPTask() { |
| 348 | SLOG(Device, 2) << __func__ << " selected_service " << selected_service_.get() |
| 349 | << " ipconfig " << ipconfig_.get(); |
| 350 | |
| 351 | if (!selected_service_ || !ipconfig_) { |
| 352 | return; |
| 353 | } |
| 354 | |
| 355 | const StaticIPParameters &static_ip_parameters = |
| 356 | selected_service_->static_ip_parameters(); |
| 357 | if (static_ip_parameters.ContainsAddress()) { |
| 358 | SLOG(Device, 2) << __func__ << " " << " configuring static IP parameters."; |
| 359 | // If the parameters contain an IP address, apply them now and bring |
| 360 | // the interface up. When DHCP information arrives, it will supplement |
| 361 | // the static information. |
| 362 | OnIPConfigUpdated(ipconfig_, true); |
| 363 | } else { |
| 364 | SLOG(Device, 2) << __func__ << " " << " no static IP address."; |
| 365 | } |
| 366 | } |
| 367 | |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 368 | void Device::OnIPConfigUpdated(const IPConfigRefPtr &ipconfig, bool success) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 369 | SLOG(Device, 2) << __func__ << " " << " success: " << success; |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 370 | if (success) { |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 371 | CreateConnection(); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 372 | if (selected_service_) { |
| 373 | ipconfig->ApplyStaticIPParameters( |
| 374 | selected_service_->static_ip_parameters()); |
| 375 | } |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 376 | connection_->UpdateFromIPConfig(ipconfig); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 377 | // SetConnection must occur after the UpdateFromIPConfig so the |
| 378 | // service can use the values derived from the connection. |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 379 | if (selected_service_) { |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 380 | selected_service_->SetConnection(connection_); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 381 | } |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 382 | // The service state change needs to happen last, so that at the |
| 383 | // time we report the state change to the manager, the service |
| 384 | // has its connection. |
| 385 | SetServiceState(Service::kStateConnected); |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 386 | portal_attempts_to_online_ = 0; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 387 | // Subtle: Start portal detection after transitioning the service |
| 388 | // to the Connected state because this call may immediately transition |
| 389 | // to the Online state. |
| 390 | StartPortalDetection(); |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 391 | } else { |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 392 | // TODO(pstew): This logic gets yet more complex when multiple |
| 393 | // IPConfig types are run in parallel (e.g. DHCP and DHCP6) |
| 394 | if (selected_service_ && |
| 395 | selected_service_->static_ip_parameters().ContainsAddress()) { |
| 396 | // Consider three cases: |
| 397 | // |
| 398 | // 1. We're here because DHCP failed while starting up. There |
| 399 | // are two subcases: |
| 400 | // a. DHCP has failed, and Static IP config has _not yet_ |
| 401 | // completed. It's fine to do nothing, because we'll |
| 402 | // apply the static config shortly. |
| 403 | // b. DHCP has failed, and Static IP config has _already_ |
| 404 | // completed. It's fine to do nothing, because we can |
| 405 | // continue to use the static config that's already |
| 406 | // been applied. |
| 407 | // |
| 408 | // 2. We're here because a previously valid DHCP configuration |
| 409 | // is no longer valid. There's still a static IP config, |
| 410 | // because the condition in the if clause evaluated to true. |
| 411 | // Furthermore, the static config includes an IP address for |
| 412 | // us to use. |
| 413 | // |
| 414 | // The current configuration may include some DHCP |
| 415 | // parameters, overriden by any static parameters |
| 416 | // provided. We continue to use this configuration, because |
| 417 | // the only configuration element that is leased to us (IP |
| 418 | // address) will be overriden by a static parameter. |
| 419 | return; |
| 420 | } |
| 421 | |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 422 | SetServiceState(Service::kStateDisconnected); |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 423 | DestroyConnection(); |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 424 | } |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 427 | void Device::CreateConnection() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 428 | SLOG(Device, 2) << __func__; |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 429 | if (!connection_.get()) { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 430 | connection_ = new Connection(interface_index_, |
| 431 | link_name_, |
Paul Stewart | e00600e | 2012-03-16 07:08:00 -0700 | [diff] [blame] | 432 | technology_, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 433 | manager_->device_info()); |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | |
| 437 | void Device::DestroyConnection() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 438 | SLOG(Device, 2) << __func__; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 439 | StopPortalDetection(); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 440 | if (selected_service_.get()) { |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 441 | selected_service_->SetConnection(NULL); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 442 | } |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 443 | connection_ = NULL; |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 446 | void Device::SelectService(const ServiceRefPtr &service) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 447 | SLOG(Device, 2) << __func__ << ": " |
| 448 | << (service.get() ? |
| 449 | StringPrintf("%s (%s)", |
| 450 | service->UniqueName().c_str(), |
| 451 | service->friendly_name().c_str()) : |
| 452 | "*reset*"); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 453 | |
| 454 | if (selected_service_.get() == service.get()) { |
| 455 | // No change to |selected_service_|. Return early to avoid |
| 456 | // changing its state. |
| 457 | return; |
| 458 | } |
| 459 | |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 460 | if (selected_service_.get()) { |
| 461 | if (selected_service_->state() != Service::kStateFailure) { |
| 462 | selected_service_->SetState(Service::kStateIdle); |
| 463 | } |
| 464 | // TODO(pstew): We need to revisit the model here: should the Device |
| 465 | // subclass be responsible for calling DestroyIPConfig() (which would |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 466 | // trigger DestroyConnection() and Service::SetConnection(NULL))? |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 467 | // Ethernet does, but WiFi currently does not. crosbug.com/23929 |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 468 | selected_service_->SetConnection(NULL); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 469 | } |
| 470 | selected_service_ = service; |
| 471 | } |
| 472 | |
| 473 | void Device::SetServiceState(Service::ConnectState state) { |
| 474 | if (selected_service_.get()) { |
| 475 | selected_service_->SetState(state); |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | void Device::SetServiceFailure(Service::ConnectFailure failure_state) { |
| 480 | if (selected_service_.get()) { |
| 481 | selected_service_->SetFailure(failure_state); |
| 482 | } |
| 483 | } |
| 484 | |
Eric Shienbrood | cc95c5d | 2012-03-30 15:25:49 -0400 | [diff] [blame] | 485 | void Device::SetServiceFailureSilent(Service::ConnectFailure failure_state) { |
| 486 | if (selected_service_.get()) { |
| 487 | selected_service_->SetFailureSilent(failure_state); |
| 488 | } |
| 489 | } |
| 490 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 491 | string Device::SerializeIPConfigs(const string &suffix) { |
| 492 | return StringPrintf("%s:%s", suffix.c_str(), ipconfig_->type().c_str()); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 493 | } |
| 494 | |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 495 | bool Device::SetIPFlag(IPAddress::Family family, const string &flag, |
| 496 | const string &value) { |
| 497 | string ip_version; |
| 498 | if (family == IPAddress::kFamilyIPv4) { |
| 499 | ip_version = kIPFlagVersion4; |
| 500 | } else if (family == IPAddress::kFamilyIPv6) { |
| 501 | ip_version = kIPFlagVersion6; |
| 502 | } else { |
| 503 | NOTIMPLEMENTED(); |
| 504 | } |
| 505 | FilePath flag_file(StringPrintf(kIPFlagTemplate, ip_version.c_str(), |
| 506 | link_name_.c_str(), flag.c_str())); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 507 | SLOG(Device, 2) << "Writing " << value << " to flag file " |
| 508 | << flag_file.value(); |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 509 | if (file_util::WriteFile(flag_file, value.c_str(), value.length()) != 1) { |
| 510 | LOG(ERROR) << StringPrintf("IP flag write failed: %s to %s", |
| 511 | value.c_str(), flag_file.value().c_str()); |
| 512 | return false; |
| 513 | } |
| 514 | return true; |
| 515 | } |
| 516 | |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 517 | bool Device::RestartPortalDetection() { |
| 518 | StopPortalDetection(); |
| 519 | return StartPortalDetection(); |
| 520 | } |
| 521 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 522 | bool Device::RequestPortalDetection() { |
| 523 | if (!selected_service_) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 524 | SLOG(Device, 2) << FriendlyName() |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 525 | << ": No selected service, so no need for portal check."; |
| 526 | return false; |
| 527 | } |
| 528 | |
| 529 | if (!connection_.get()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 530 | SLOG(Device, 2) << FriendlyName() |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 531 | << ": No connection, so no need for portal check."; |
| 532 | return false; |
| 533 | } |
| 534 | |
| 535 | if (selected_service_->state() != Service::kStatePortal) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 536 | SLOG(Device, 2) << FriendlyName() |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 537 | << ": Service is not in portal state. No need to start check."; |
| 538 | return false; |
| 539 | } |
| 540 | |
| 541 | if (!connection_->is_default()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 542 | SLOG(Device, 2) << FriendlyName() |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 543 | << ": Service is not the default connection. Don't start check."; |
| 544 | return false; |
| 545 | } |
| 546 | |
| 547 | if (portal_detector_.get() && portal_detector_->IsInProgress()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 548 | SLOG(Device, 2) << FriendlyName() |
| 549 | << ": Portal detection is already running."; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 550 | return true; |
| 551 | } |
| 552 | |
| 553 | return StartPortalDetection(); |
| 554 | } |
| 555 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 556 | bool Device::StartPortalDetection() { |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 557 | DCHECK(selected_service_.get()); |
| 558 | if (selected_service_->IsPortalDetectionDisabled()) { |
| 559 | SLOG(Device, 2) << "Service " << selected_service_->friendly_name() |
| 560 | << ": Portal detection is disabled; " |
| 561 | << "marking service online."; |
| 562 | SetServiceConnectedState(Service::kStateOnline); |
| 563 | return false; |
| 564 | } |
| 565 | |
| 566 | if (selected_service_->IsPortalDetectionAuto() && |
| 567 | !manager_->IsPortalDetectionEnabled(technology())) { |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 568 | // If portal detection is disabled for this technology, immediately set |
| 569 | // the service state to "Online". |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 570 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 571 | << ": Portal detection is disabled; " |
| 572 | << "marking service online."; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 573 | SetServiceConnectedState(Service::kStateOnline); |
| 574 | return false; |
| 575 | } |
| 576 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 577 | if (selected_service_->HasProxyConfig()) { |
| 578 | // Services with HTTP proxy configurations should not be checked by the |
| 579 | // connection manager, since we don't have the ability to evaluate |
| 580 | // arbitrary proxy configs and their possible credentials. |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 581 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 582 | << ": Service has proxy config; marking it online."; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 583 | SetServiceConnectedState(Service::kStateOnline); |
| 584 | return false; |
| 585 | } |
| 586 | |
| 587 | portal_detector_.reset(new PortalDetector(connection_, |
| 588 | dispatcher_, |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 589 | portal_detector_callback_)); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 590 | if (!portal_detector_->Start(manager_->GetPortalCheckURL())) { |
| 591 | LOG(ERROR) << "Device " << FriendlyName() |
| 592 | << ": Portal detection failed to start: likely bad URL: " |
| 593 | << manager_->GetPortalCheckURL(); |
| 594 | SetServiceConnectedState(Service::kStateOnline); |
| 595 | return false; |
| 596 | } |
| 597 | |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 598 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 599 | << ": Portal detection has started."; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 600 | return true; |
| 601 | } |
| 602 | |
| 603 | void Device::StopPortalDetection() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 604 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 605 | << ": Portal detection has stopped."; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 606 | portal_detector_.reset(); |
| 607 | } |
| 608 | |
| 609 | void Device::SetServiceConnectedState(Service::ConnectState state) { |
| 610 | DCHECK(selected_service_.get()); |
| 611 | |
| 612 | if (!selected_service_.get()) { |
| 613 | LOG(ERROR) << FriendlyName() << ": " |
| 614 | << "Portal detection completed but no selected service exists!"; |
| 615 | return; |
| 616 | } |
| 617 | |
| 618 | if (!selected_service_->IsConnected()) { |
| 619 | LOG(ERROR) << FriendlyName() << ": " |
| 620 | << "Portal detection completed but selected service " |
| 621 | << selected_service_->UniqueName() |
| 622 | << " is in non-connected state."; |
| 623 | return; |
| 624 | } |
| 625 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 626 | if (state == Service::kStatePortal && connection_->is_default() && |
| 627 | manager_->GetPortalCheckInterval() != 0) { |
| 628 | CHECK(portal_detector_.get()); |
| 629 | if (!portal_detector_->StartAfterDelay( |
| 630 | manager_->GetPortalCheckURL(), |
| 631 | manager_->GetPortalCheckInterval())) { |
| 632 | LOG(ERROR) << "Device " << FriendlyName() |
| 633 | << ": Portal detection failed to restart: likely bad URL: " |
| 634 | << manager_->GetPortalCheckURL(); |
| 635 | SetServiceState(Service::kStateOnline); |
| 636 | portal_detector_.reset(); |
| 637 | return; |
| 638 | } |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 639 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 640 | << ": Portal detection retrying."; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 641 | } else { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 642 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 643 | << ": Portal will not retry."; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 644 | portal_detector_.reset(); |
| 645 | } |
| 646 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 647 | SetServiceState(state); |
| 648 | } |
| 649 | |
| 650 | void Device::PortalDetectorCallback(const PortalDetector::Result &result) { |
| 651 | if (!result.final) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 652 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 653 | << ": Received non-final status: " |
| 654 | << PortalDetector::StatusToString(result.status); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 655 | return; |
| 656 | } |
| 657 | |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 658 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 659 | << ": Received final status: " |
| 660 | << PortalDetector::StatusToString(result.status); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 661 | |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 662 | portal_attempts_to_online_ += result.num_attempts; |
| 663 | |
| 664 | metrics()->SendEnumToUMA( |
| 665 | metrics()->GetFullMetricName(Metrics::kMetricPortalResult, technology()), |
| 666 | Metrics::PortalDetectionResultToEnum(result), |
| 667 | Metrics::kPortalResultMax); |
| 668 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 669 | if (result.status == PortalDetector::kStatusSuccess) { |
| 670 | SetServiceConnectedState(Service::kStateOnline); |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 671 | |
| 672 | metrics()->SendToUMA( |
| 673 | metrics()->GetFullMetricName( |
| 674 | Metrics::kMetricPortalAttemptsToOnline, technology()), |
| 675 | portal_attempts_to_online_, |
| 676 | Metrics::kMetricPortalAttemptsToOnlineMin, |
| 677 | Metrics::kMetricPortalAttemptsToOnlineMax, |
| 678 | Metrics::kMetricPortalAttemptsToOnlineNumBuckets); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 679 | } else { |
| 680 | SetServiceConnectedState(Service::kStatePortal); |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 681 | |
| 682 | metrics()->SendToUMA( |
| 683 | metrics()->GetFullMetricName( |
| 684 | Metrics::kMetricPortalAttempts, technology()), |
| 685 | result.num_attempts, |
| 686 | Metrics::kMetricPortalAttemptsMin, |
| 687 | Metrics::kMetricPortalAttemptsMax, |
| 688 | Metrics::kMetricPortalAttemptsNumBuckets); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 689 | } |
| 690 | } |
| 691 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 692 | vector<string> Device::AvailableIPConfigs(Error */*error*/) { |
Jason Glasgow | 08afdff | 2012-04-03 10:22:26 -0400 | [diff] [blame] | 693 | if (ipconfig_.get()) { |
| 694 | string id = ipconfig_->GetRpcIdentifier(); |
| 695 | return vector<string>(1, id); |
| 696 | } |
| 697 | return vector<string>(); |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | string Device::GetRpcConnectionIdentifier() { |
| 701 | return adaptor_->GetRpcConnectionIdentifier(); |
| 702 | } |
| 703 | |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 704 | bool Device::IsUnderlyingDeviceEnabled() const { |
| 705 | return false; |
| 706 | } |
| 707 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 708 | // callback |
| 709 | void Device::OnEnabledStateChanged(const ResultCallback &callback, |
| 710 | const Error &error) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 711 | SLOG(Device, 2) << __func__ << "(" << enabled_pending_ << ")"; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 712 | if (error.IsSuccess()) { |
| 713 | enabled_ = enabled_pending_; |
| 714 | manager_->UpdateEnabledTechnologies(); |
| 715 | adaptor_->EmitBoolChanged(flimflam::kPoweredProperty, enabled_); |
| 716 | adaptor_->UpdateEnabled(); |
| 717 | } |
Gary Morain | baeefdf | 2012-04-30 14:53:35 -0700 | [diff] [blame] | 718 | enabled_pending_ = enabled_; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 719 | if (!callback.is_null()) |
| 720 | callback.Run(error); |
| 721 | } |
| 722 | |
| 723 | void Device::SetEnabled(bool enable) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 724 | SLOG(Device, 2) << __func__ << "(" << enable << ")"; |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 725 | Error error; |
| 726 | SetEnabledInternal(enable, false, &error, ResultCallback()); |
| 727 | LOG_IF(ERROR, error.IsFailure()) |
| 728 | << "Enabled failed, but no way to report the failure."; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | void Device::SetEnabledPersistent(bool enable, |
| 732 | Error *error, |
| 733 | const ResultCallback &callback) { |
| 734 | SetEnabledInternal(enable, true, error, callback); |
| 735 | } |
| 736 | |
| 737 | void Device::SetEnabledInternal(bool enable, |
| 738 | bool persist, |
| 739 | Error *error, |
| 740 | const ResultCallback &callback) { |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 741 | DCHECK(error); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 742 | SLOG(Device, 2) << "Device " << link_name_ << " " |
| 743 | << (enable ? "starting" : "stopping"); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 744 | if (enable == enabled_) { |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 745 | error->Reset(); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 746 | return; |
| 747 | } |
| 748 | |
| 749 | if (enabled_pending_ == enable) { |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 750 | Error::PopulateAndLog(error, Error::kInProgress, |
| 751 | "Enable operation already in progress"); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 752 | return; |
| 753 | } |
| 754 | |
| 755 | if (persist) { |
| 756 | enabled_persistent_ = enable; |
| 757 | manager_->SaveActiveProfile(); |
| 758 | } |
| 759 | |
| 760 | enabled_pending_ = enable; |
| 761 | EnabledStateChangedCallback enabled_callback = |
| 762 | Bind(&Device::OnEnabledStateChanged, |
| 763 | weak_ptr_factory_.GetWeakPtr(), callback); |
| 764 | if (enable) { |
| 765 | running_ = true; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 766 | Start(error, enabled_callback); |
| 767 | } else { |
| 768 | running_ = false; |
| 769 | DestroyIPConfig(); // breaks a reference cycle |
| 770 | SelectService(NULL); // breaks a reference cycle |
| 771 | rtnl_handler_->SetInterfaceFlags(interface_index(), 0, IFF_UP); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 772 | SLOG(Device, 3) << "Device " << link_name_ << " ipconfig_ " |
| 773 | << (ipconfig_ ? "is set." : "is not set."); |
| 774 | SLOG(Device, 3) << "Device " << link_name_ << " connection_ " |
| 775 | << (connection_ ? "is set." : "is not set."); |
| 776 | SLOG(Device, 3) << "Device " << link_name_ << " selected_service_ " |
| 777 | << (selected_service_ ? "is set." : "is not set."); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 778 | Stop(error, enabled_callback); |
| 779 | } |
| 780 | } |
| 781 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 782 | } // namespace shill |