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 |
Darin Petkov | a0a0efe | 2012-06-27 12:50:01 +0200 | [diff] [blame] | 143 | LOG(INFO) << "Device created: " << link_name_ |
| 144 | << " index " << interface_index_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | Device::~Device() { |
Darin Petkov | a0a0efe | 2012-06-27 12:50:01 +0200 | [diff] [blame] | 148 | LOG(INFO) << "Device destructed: " << link_name_ |
| 149 | << " index " << interface_index_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 152 | void Device::LinkEvent(unsigned flags, unsigned change) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 153 | SLOG(Device, 2) << "Device " << link_name_ |
| 154 | << std::showbase << std::hex |
| 155 | << " flags " << flags << " changed " << change |
| 156 | << std::dec << std::noshowbase; |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Darin Petkov | c086531 | 2011-09-16 15:31:20 -0700 | [diff] [blame] | 159 | void Device::Scan(Error *error) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 160 | SLOG(Device, 2) << "Device " << link_name_ << " scan requested."; |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 161 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 162 | "Device doesn't support scan."); |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 165 | void Device::RegisterOnNetwork(const std::string &/*network_id*/, Error *error, |
| 166 | const ResultCallback &/*callback*/) { |
| 167 | Error::PopulateAndLog(error, Error::kNotSupported, |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 168 | "Device doesn't support network registration."); |
Darin Petkov | 9ae310f | 2011-08-30 15:41:13 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Darin Petkov | c64fe5e | 2012-01-11 12:46:13 +0100 | [diff] [blame] | 171 | void Device::RequirePIN( |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 172 | const string &/*pin*/, bool /*require*/, |
| 173 | Error *error, const ResultCallback &/*callback*/) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 174 | SLOG(Device, 2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 175 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 176 | "Device doesn't support RequirePIN."); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 179 | void Device::EnterPIN(const string &/*pin*/, |
| 180 | Error *error, const ResultCallback &/*callback*/) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 181 | SLOG(Device, 2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 182 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 183 | "Device doesn't support EnterPIN."); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 184 | } |
| 185 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 186 | void Device::UnblockPIN(const string &/*unblock_code*/, |
| 187 | const string &/*pin*/, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 188 | Error *error, const ResultCallback &/*callback*/) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 189 | SLOG(Device, 2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 190 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 191 | "Device doesn't support UnblockPIN."); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 192 | } |
| 193 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 194 | void Device::ChangePIN(const string &/*old_pin*/, |
| 195 | const string &/*new_pin*/, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 196 | Error *error, const ResultCallback &/*callback*/) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 197 | SLOG(Device, 2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 198 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 199 | "Device doesn't support ChangePIN."); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 202 | void Device::DisableIPv6() { |
| 203 | SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "1"); |
| 204 | } |
| 205 | |
| 206 | void Device::EnableIPv6() { |
| 207 | SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "0"); |
| 208 | } |
| 209 | |
| 210 | void Device::EnableIPv6Privacy() { |
| 211 | SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagUseTempAddr, |
| 212 | kIPFlagUseTempAddrUsedAndDefault); |
| 213 | } |
| 214 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 215 | void Device::DisableReversePathFilter() { |
| 216 | // TODO(pstew): Current kernel doesn't offer reverse-path filtering flag |
| 217 | // for IPv6. crosbug.com/24228 |
| 218 | SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter, |
| 219 | kIPFlagReversePathFilterLooseMode); |
| 220 | } |
| 221 | |
| 222 | void Device::EnableReversePathFilter() { |
| 223 | SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter, |
| 224 | kIPFlagReversePathFilterEnabled); |
| 225 | } |
| 226 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 227 | bool Device::IsConnected() const { |
| 228 | if (selected_service_) |
| 229 | return selected_service_->IsConnected(); |
| 230 | return false; |
| 231 | } |
| 232 | |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 233 | bool Device::IsConnectedToService(const ServiceRefPtr &service) const { |
| 234 | return service == selected_service_ && IsConnected(); |
| 235 | } |
| 236 | |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 237 | string Device::GetRpcIdentifier() { |
| 238 | return adaptor_->GetRpcIdentifier(); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 241 | string Device::GetStorageIdentifier() { |
| 242 | string id = GetRpcIdentifier(); |
| 243 | ControlInterface::RpcIdToStorageId(&id); |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 244 | size_t needle = id.find('_'); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 245 | DLOG_IF(ERROR, needle == string::npos) << "No _ in storage id?!?!"; |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 246 | id.replace(id.begin() + needle + 1, id.end(), hardware_address_); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 247 | return id; |
| 248 | } |
| 249 | |
Jason Glasgow | b579005 | 2012-01-27 01:03:52 -0500 | [diff] [blame] | 250 | string Device::GetTechnologyString(Error */*error*/) { |
| 251 | return Technology::NameFromIdentifier(technology()); |
| 252 | } |
| 253 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 254 | const string& Device::FriendlyName() const { |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 255 | return link_name_; |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 258 | const string& Device::UniqueName() const { |
| 259 | return unique_id_; |
| 260 | } |
| 261 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 262 | bool Device::Load(StoreInterface *storage) { |
| 263 | const string id = GetStorageIdentifier(); |
| 264 | if (!storage->ContainsGroup(id)) { |
| 265 | LOG(WARNING) << "Device is not available in the persistent store: " << id; |
| 266 | return false; |
| 267 | } |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 268 | enabled_persistent_ = true; |
| 269 | storage->GetBool(id, kStoragePowered, &enabled_persistent_); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 270 | // TODO(cmasone): What does it mean to load an IPConfig identifier?? |
| 271 | return true; |
| 272 | } |
| 273 | |
| 274 | bool Device::Save(StoreInterface *storage) { |
| 275 | const string id = GetStorageIdentifier(); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 276 | storage->SetBool(id, kStoragePowered, enabled_persistent_); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 277 | if (ipconfig_.get()) { |
| 278 | // The _0 is an index into the list of IPConfigs that this device might |
| 279 | // have. We only have one IPConfig right now, and I hope to never have |
| 280 | // to support more, as sleffler indicates that associating IPConfigs |
| 281 | // with devices is wrong and due to be changed in flimflam anyhow. |
| 282 | string suffix = hardware_address_ + "_0"; |
| 283 | ipconfig_->Save(storage, suffix); |
| 284 | storage->SetString(id, kStorageIPConfigs, SerializeIPConfigs(suffix)); |
| 285 | } |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 286 | return true; |
| 287 | } |
| 288 | |
Darin Petkov | 2b8e44e | 2012-06-25 15:13:26 +0200 | [diff] [blame] | 289 | void Device::DropConnection() { |
| 290 | SLOG(Device, 2) << __func__; |
| 291 | DestroyIPConfig(); |
| 292 | SelectService(NULL); |
| 293 | } |
| 294 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 295 | void Device::DestroyIPConfig() { |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 296 | DisableIPv6(); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 297 | if (ipconfig_.get()) { |
| 298 | ipconfig_->ReleaseIP(); |
| 299 | ipconfig_ = NULL; |
| 300 | } |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 301 | DestroyConnection(); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 304 | bool Device::AcquireIPConfig() { |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 305 | return AcquireIPConfigWithLeaseName(string()); |
| 306 | } |
| 307 | |
| 308 | bool Device::AcquireIPConfigWithLeaseName(const string &lease_name) { |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 309 | DestroyIPConfig(); |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 310 | EnableIPv6(); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 311 | ipconfig_ = dhcp_provider_->CreateConfig(link_name_, |
| 312 | manager_->GetHostName(), |
| 313 | lease_name, |
| 314 | manager_->GetArpGateway()); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 315 | ipconfig_->RegisterUpdateCallback(Bind(&Device::OnIPConfigUpdated, |
| 316 | weak_ptr_factory_.GetWeakPtr())); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 317 | dispatcher_->PostTask(Bind(&Device::ConfigureStaticIPTask, |
| 318 | weak_ptr_factory_.GetWeakPtr())); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 319 | return ipconfig_->RequestIP(); |
| 320 | } |
| 321 | |
Jason Glasgow | b579005 | 2012-01-27 01:03:52 -0500 | [diff] [blame] | 322 | void Device::HelpRegisterDerivedString( |
| 323 | const string &name, |
| 324 | string(Device::*get)(Error *error), |
| 325 | void(Device::*set)(const string &value, Error *error)) { |
| 326 | store_.RegisterDerivedString( |
| 327 | name, |
| 328 | StringAccessor(new CustomAccessor<Device, string>(this, get, set))); |
| 329 | } |
| 330 | |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 331 | void Device::HelpRegisterDerivedStrings( |
| 332 | const string &name, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 333 | Strings(Device::*get)(Error *error), |
| 334 | void(Device::*set)(const Strings &value, Error *error)) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 335 | store_.RegisterDerivedStrings( |
| 336 | name, |
| 337 | StringsAccessor(new CustomAccessor<Device, Strings>(this, get, set))); |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Jason Glasgow | 08afdff | 2012-04-03 10:22:26 -0400 | [diff] [blame] | 340 | void Device::HelpRegisterConstDerivedRpcIdentifiers( |
| 341 | const string &name, |
| 342 | RpcIdentifiers(Device::*get)(Error *)) { |
| 343 | store_.RegisterDerivedRpcIdentifiers( |
| 344 | name, |
| 345 | RpcIdentifiersAccessor( |
| 346 | new CustomAccessor<Device, RpcIdentifiers>(this, get, NULL))); |
| 347 | } |
| 348 | |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 349 | void Device::ConfigureStaticIPTask() { |
| 350 | SLOG(Device, 2) << __func__ << " selected_service " << selected_service_.get() |
| 351 | << " ipconfig " << ipconfig_.get(); |
| 352 | |
| 353 | if (!selected_service_ || !ipconfig_) { |
| 354 | return; |
| 355 | } |
| 356 | |
| 357 | const StaticIPParameters &static_ip_parameters = |
| 358 | selected_service_->static_ip_parameters(); |
| 359 | if (static_ip_parameters.ContainsAddress()) { |
| 360 | SLOG(Device, 2) << __func__ << " " << " configuring static IP parameters."; |
| 361 | // If the parameters contain an IP address, apply them now and bring |
| 362 | // the interface up. When DHCP information arrives, it will supplement |
| 363 | // the static information. |
| 364 | OnIPConfigUpdated(ipconfig_, true); |
| 365 | } else { |
| 366 | SLOG(Device, 2) << __func__ << " " << " no static IP address."; |
| 367 | } |
| 368 | } |
| 369 | |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 370 | void Device::OnIPConfigUpdated(const IPConfigRefPtr &ipconfig, bool success) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 371 | SLOG(Device, 2) << __func__ << " " << " success: " << success; |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 372 | if (success) { |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 373 | CreateConnection(); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 374 | if (selected_service_) { |
| 375 | ipconfig->ApplyStaticIPParameters( |
| 376 | selected_service_->static_ip_parameters()); |
| 377 | } |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 378 | connection_->UpdateFromIPConfig(ipconfig); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 379 | // SetConnection must occur after the UpdateFromIPConfig so the |
| 380 | // service can use the values derived from the connection. |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 381 | if (selected_service_) { |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 382 | selected_service_->SetConnection(connection_); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 383 | } |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 384 | // The service state change needs to happen last, so that at the |
| 385 | // time we report the state change to the manager, the service |
| 386 | // has its connection. |
| 387 | SetServiceState(Service::kStateConnected); |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 388 | portal_attempts_to_online_ = 0; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 389 | // Subtle: Start portal detection after transitioning the service |
| 390 | // to the Connected state because this call may immediately transition |
| 391 | // to the Online state. |
| 392 | StartPortalDetection(); |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 393 | } else { |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 394 | // TODO(pstew): This logic gets yet more complex when multiple |
| 395 | // IPConfig types are run in parallel (e.g. DHCP and DHCP6) |
| 396 | if (selected_service_ && |
| 397 | selected_service_->static_ip_parameters().ContainsAddress()) { |
| 398 | // Consider three cases: |
| 399 | // |
| 400 | // 1. We're here because DHCP failed while starting up. There |
| 401 | // are two subcases: |
| 402 | // a. DHCP has failed, and Static IP config has _not yet_ |
| 403 | // completed. It's fine to do nothing, because we'll |
| 404 | // apply the static config shortly. |
| 405 | // b. DHCP has failed, and Static IP config has _already_ |
| 406 | // completed. It's fine to do nothing, because we can |
| 407 | // continue to use the static config that's already |
| 408 | // been applied. |
| 409 | // |
| 410 | // 2. We're here because a previously valid DHCP configuration |
| 411 | // is no longer valid. There's still a static IP config, |
| 412 | // because the condition in the if clause evaluated to true. |
| 413 | // Furthermore, the static config includes an IP address for |
| 414 | // us to use. |
| 415 | // |
| 416 | // The current configuration may include some DHCP |
| 417 | // parameters, overriden by any static parameters |
| 418 | // provided. We continue to use this configuration, because |
| 419 | // the only configuration element that is leased to us (IP |
| 420 | // address) will be overriden by a static parameter. |
| 421 | return; |
| 422 | } |
| 423 | |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 424 | SetServiceState(Service::kStateDisconnected); |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 425 | DestroyConnection(); |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 426 | } |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 429 | void Device::CreateConnection() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 430 | SLOG(Device, 2) << __func__; |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 431 | if (!connection_.get()) { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 432 | connection_ = new Connection(interface_index_, |
| 433 | link_name_, |
Paul Stewart | e00600e | 2012-03-16 07:08:00 -0700 | [diff] [blame] | 434 | technology_, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 435 | manager_->device_info()); |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | |
| 439 | void Device::DestroyConnection() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 440 | SLOG(Device, 2) << __func__; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 441 | StopPortalDetection(); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 442 | if (selected_service_.get()) { |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 443 | selected_service_->SetConnection(NULL); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 444 | } |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 445 | connection_ = NULL; |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 448 | void Device::SelectService(const ServiceRefPtr &service) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 449 | SLOG(Device, 2) << __func__ << ": " |
| 450 | << (service.get() ? |
| 451 | StringPrintf("%s (%s)", |
| 452 | service->UniqueName().c_str(), |
| 453 | service->friendly_name().c_str()) : |
| 454 | "*reset*"); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 455 | |
| 456 | if (selected_service_.get() == service.get()) { |
| 457 | // No change to |selected_service_|. Return early to avoid |
| 458 | // changing its state. |
| 459 | return; |
| 460 | } |
| 461 | |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 462 | if (selected_service_.get()) { |
| 463 | if (selected_service_->state() != Service::kStateFailure) { |
| 464 | selected_service_->SetState(Service::kStateIdle); |
| 465 | } |
Paul Stewart | 20b0a09 | 2012-05-22 20:39:57 -0700 | [diff] [blame] | 466 | // Just in case the Device subclass has not already done so, make |
| 467 | // sure the previously selected service has its connection removed. |
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()); |
Jason Glasgow | 7234ec3 | 2012-05-23 16:01:21 -0400 | [diff] [blame] | 727 | LOG_IF(ERROR, error.IsFailure() && !error.IsOngoing()) |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 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 |