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 | |
Matthew Wein | 08add48 | 2015-04-20 13:26:48 -0700 | [diff] [blame] | 7 | #include <errno.h> |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 8 | #include <netinet/in.h> |
Alex Vakulenko | a41ab51 | 2014-07-23 14:24:23 -0700 | [diff] [blame] | 9 | #include <linux/if.h> // NOLINT - Needs definitions from netinet/in.h |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 10 | #include <stdio.h> |
Paul Stewart | 208a97e | 2015-05-13 09:11:12 -0700 | [diff] [blame] | 11 | #include <sys/param.h> |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 12 | #include <time.h> |
Matthew Wein | 08add48 | 2015-04-20 13:26:48 -0700 | [diff] [blame] | 13 | #include <unistd.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 14 | |
Samuel Tan | 787a1ce | 2014-11-11 17:17:27 -0800 | [diff] [blame] | 15 | #include <algorithm> |
Garret Kelly | c5f89d1 | 2015-02-18 14:39:36 -0500 | [diff] [blame] | 16 | #include <set> |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 17 | #include <string> |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 18 | #include <vector> |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 19 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 20 | #include <base/bind.h> |
Ben Chan | 11c213f | 2014-09-05 08:21:06 -0700 | [diff] [blame] | 21 | #include <base/files/file_util.h> |
Chris Masone | 487b8bf | 2011-05-13 16:27:57 -0700 | [diff] [blame] | 22 | #include <base/memory/ref_counted.h> |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 23 | #include <base/strings/stringprintf.h> |
Peter Qiu | 62abf31 | 2015-05-05 12:58:05 -0700 | [diff] [blame] | 24 | #include <base/strings/string_number_conversions.h> |
| 25 | #include <base/strings/string_util.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 26 | #include <chromeos/dbus/service_constants.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 27 | |
Arman Uguray | f84a424 | 2013-04-09 20:01:07 -0700 | [diff] [blame] | 28 | #include "shill/async_connection.h" |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 29 | #include "shill/connection.h" |
Rebecca Silberstein | f4365a6 | 2014-09-16 11:40:32 -0700 | [diff] [blame] | 30 | #include "shill/connection_tester.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 31 | #include "shill/control_interface.h" |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 32 | #include "shill/device_dbus_adaptor.h" |
Peter Qiu | 675d0b0 | 2015-06-03 13:08:09 -0700 | [diff] [blame] | 33 | #include "shill/dhcp/dhcp_config.h" |
| 34 | #include "shill/dhcp/dhcp_provider.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 35 | #include "shill/error.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 36 | #include "shill/event_dispatcher.h" |
Gaurav Shah | 6d2c72d | 2012-10-16 16:30:44 -0700 | [diff] [blame] | 37 | #include "shill/geolocation_info.h" |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 38 | #include "shill/http_proxy.h" |
Peter Qiu | 62abf31 | 2015-05-05 12:58:05 -0700 | [diff] [blame] | 39 | #include "shill/icmp.h" |
Samuel Tan | fe73467 | 2014-08-07 15:50:48 -0700 | [diff] [blame] | 40 | #include "shill/ip_address_store.h" |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 41 | #include "shill/link_monitor.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 42 | #include "shill/logging.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 43 | #include "shill/manager.h" |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 44 | #include "shill/metrics.h" |
Peter Qiu | 8d6b597 | 2014-10-28 15:33:34 -0700 | [diff] [blame] | 45 | #include "shill/net/ip_address.h" |
| 46 | #include "shill/net/ndisc.h" |
| 47 | #include "shill/net/rtnl_handler.h" |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 48 | #include "shill/property_accessor.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 49 | #include "shill/refptr_types.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 50 | #include "shill/service.h" |
Arman Uguray | f84a424 | 2013-04-09 20:01:07 -0700 | [diff] [blame] | 51 | #include "shill/socket_info_reader.h" |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 52 | #include "shill/store_interface.h" |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 53 | #include "shill/technology.h" |
Paul Stewart | fa11e28 | 2013-12-02 22:04:25 -0800 | [diff] [blame] | 54 | #include "shill/tethering.h" |
Peter Qiu | dc335f8 | 2014-05-15 10:33:17 -0700 | [diff] [blame] | 55 | #include "shill/traffic_monitor.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 56 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 57 | using base::Bind; |
Peter Qiu | 6f5618b | 2014-06-05 15:19:01 -0700 | [diff] [blame] | 58 | using base::Callback; |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 59 | using base::FilePath; |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 60 | using base::StringPrintf; |
Garret Kelly | c5f89d1 | 2015-02-18 14:39:36 -0500 | [diff] [blame] | 61 | using std::set; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 62 | using std::string; |
| 63 | using std::vector; |
| 64 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 65 | namespace shill { |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 66 | |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 67 | namespace Logging { |
| 68 | static auto kModuleLogScope = ScopeLogger::kDevice; |
| 69 | static string ObjectID(Device *d) { return d->GetRpcIdentifier(); } |
| 70 | } |
| 71 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 72 | // static |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 73 | const char Device::kIPFlagTemplate[] = "/proc/sys/net/%s/conf/%s/%s"; |
| 74 | // static |
| 75 | const char Device::kIPFlagVersion4[] = "ipv4"; |
| 76 | // static |
| 77 | const char Device::kIPFlagVersion6[] = "ipv6"; |
| 78 | // static |
| 79 | const char Device::kIPFlagDisableIPv6[] = "disable_ipv6"; |
| 80 | // static |
| 81 | const char Device::kIPFlagUseTempAddr[] = "use_tempaddr"; |
| 82 | // static |
| 83 | const char Device::kIPFlagUseTempAddrUsedAndDefault[] = "2"; |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 84 | // static |
| 85 | const char Device::kIPFlagReversePathFilter[] = "rp_filter"; |
| 86 | // static |
| 87 | const char Device::kIPFlagReversePathFilterEnabled[] = "1"; |
| 88 | // static |
| 89 | const char Device::kIPFlagReversePathFilterLooseMode[] = "2"; |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 90 | // static |
Paul Stewart | 2cb3fa7 | 2014-11-13 01:43:12 -0800 | [diff] [blame] | 91 | const char Device::kIPFlagArpAnnounce[] = "arp_announce"; |
| 92 | // static |
| 93 | const char Device::kIPFlagArpAnnounceDefault[] = "0"; |
| 94 | // static |
| 95 | const char Device::kIPFlagArpAnnounceBestLocal[] = "2"; |
| 96 | // static |
| 97 | const char Device::kIPFlagArpIgnore[] = "arp_ignore"; |
| 98 | // static |
| 99 | const char Device::kIPFlagArpIgnoreDefault[] = "0"; |
| 100 | // static |
| 101 | const char Device::kIPFlagArpIgnoreLocalOnly[] = "1"; |
| 102 | // static |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 103 | const char Device::kStoragePowered[] = "Powered"; |
| 104 | // static |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 105 | const char Device::kStorageReceiveByteCount[] = "ReceiveByteCount"; |
| 106 | // static |
| 107 | const char Device::kStorageTransmitByteCount[] = "TransmitByteCount"; |
Peter Qiu | b9256f3 | 2014-05-09 15:27:29 -0700 | [diff] [blame] | 108 | // static |
| 109 | const char Device::kFallbackDnsTestHostname[] = "www.gstatic.com"; |
| 110 | // static |
| 111 | const char* Device::kFallbackDnsServers[] = { |
| 112 | "8.8.8.8", |
Peter Qiu | a89154b | 2014-05-23 15:45:42 -0700 | [diff] [blame] | 113 | "8.8.4.4" |
Peter Qiu | b9256f3 | 2014-05-09 15:27:29 -0700 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | // static |
| 117 | const int Device::kDNSTimeoutMilliseconds = 5000; |
Peter Qiu | a388fdb | 2015-04-03 10:31:22 -0700 | [diff] [blame] | 118 | const int Device::kLinkUnreliableThresholdSeconds = 60 * 60; |
Peter Qiu | 62abf31 | 2015-05-05 12:58:05 -0700 | [diff] [blame] | 119 | const size_t Device::kHardwareAddressLength = 6U; |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 120 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 121 | Device::Device(ControlInterface *control_interface, |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 122 | EventDispatcher *dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 123 | Metrics *metrics, |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 124 | Manager *manager, |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 125 | const string &link_name, |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 126 | const string &address, |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 127 | int interface_index, |
| 128 | Technology::Identifier technology) |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 129 | : enabled_(false), |
| 130 | enabled_persistent_(true), |
| 131 | enabled_pending_(enabled_), |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 132 | reconnect_(true), |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 133 | hardware_address_(address), |
mukesh agrawal | f60e406 | 2011-05-27 13:13:41 -0700 | [diff] [blame] | 134 | interface_index_(interface_index), |
| 135 | running_(false), |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 136 | link_name_(link_name), |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 137 | unique_id_(link_name), |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 138 | control_interface_(control_interface), |
| 139 | dispatcher_(dispatcher), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 140 | metrics_(metrics), |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 141 | manager_(manager), |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 142 | weak_ptr_factory_(this), |
Darin Petkov | 77cb681 | 2011-08-15 16:19:41 -0700 | [diff] [blame] | 143 | adaptor_(control_interface->CreateDeviceAdaptor(this)), |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 144 | portal_detector_callback_(Bind(&Device::PortalDetectorCallback, |
| 145 | weak_ptr_factory_.GetWeakPtr())), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 146 | technology_(technology), |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 147 | portal_attempts_to_online_(0), |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 148 | receive_byte_offset_(0), |
| 149 | transmit_byte_offset_(0), |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 150 | dhcp_provider_(DHCPProvider::GetInstance()), |
Peter Qiu | a057203 | 2014-09-26 10:07:37 -0700 | [diff] [blame] | 151 | rtnl_handler_(RTNLHandler::GetInstance()), |
| 152 | time_(Time::GetInstance()), |
Rebecca Silberstein | f4365a6 | 2014-09-16 11:40:32 -0700 | [diff] [blame] | 153 | last_link_monitor_failed_time_(0), |
| 154 | connection_tester_callback_(Bind(&Device::ConnectionTesterCallback, |
Paul Stewart | 2cb3fa7 | 2014-11-13 01:43:12 -0800 | [diff] [blame] | 155 | weak_ptr_factory_.GetWeakPtr())), |
| 156 | is_loose_routing_(false), |
| 157 | is_multi_homed_(false) { |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 158 | store_.RegisterConstString(kAddressProperty, &hardware_address_); |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 159 | |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 160 | // kBgscanMethodProperty: Registered in WiFi |
| 161 | // kBgscanShortIntervalProperty: Registered in WiFi |
| 162 | // kBgscanSignalThresholdProperty: Registered in WiFi |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 163 | |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 164 | // kCellularAllowRoamingProperty: Registered in Cellular |
| 165 | // kCarrierProperty: Registered in Cellular |
| 166 | // kEsnProperty: Registered in Cellular |
| 167 | // kHomeProviderProperty: Registered in Cellular |
| 168 | // kImeiProperty: Registered in Cellular |
| 169 | // kIccidProperty: Registered in Cellular |
| 170 | // kImsiProperty: Registered in Cellular |
| 171 | // kManufacturerProperty: Registered in Cellular |
| 172 | // kMdnProperty: Registered in Cellular |
| 173 | // kMeidProperty: Registered in Cellular |
| 174 | // kMinProperty: Registered in Cellular |
| 175 | // kModelIDProperty: Registered in Cellular |
| 176 | // kFirmwareRevisionProperty: Registered in Cellular |
| 177 | // kHardwareRevisionProperty: Registered in Cellular |
| 178 | // kPRLVersionProperty: Registered in Cellular |
| 179 | // kSIMLockStatusProperty: Registered in Cellular |
| 180 | // kFoundNetworksProperty: Registered in Cellular |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 181 | // kDBusObjectProperty: Register in Cellular |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 182 | |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 183 | store_.RegisterConstString(kInterfaceProperty, &link_name_); |
Christopher Wiley | 674598d | 2014-12-12 10:21:39 -0800 | [diff] [blame] | 184 | HelpRegisterConstDerivedRpcIdentifier( |
| 185 | kSelectedServiceProperty, &Device::GetSelectedServiceRpcIdentifier); |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 186 | HelpRegisterConstDerivedRpcIdentifiers(kIPConfigsProperty, |
Jason Glasgow | 08afdff | 2012-04-03 10:22:26 -0400 | [diff] [blame] | 187 | &Device::AvailableIPConfigs); |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 188 | store_.RegisterConstString(kNameProperty, &link_name_); |
| 189 | store_.RegisterConstBool(kPoweredProperty, &enabled_); |
| 190 | HelpRegisterConstDerivedString(kTypeProperty, |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 191 | &Device::GetTechnologyString); |
Ben Chan | 39a7beb | 2013-09-21 11:28:00 -0700 | [diff] [blame] | 192 | HelpRegisterConstDerivedUint64(kLinkMonitorResponseTimeProperty, |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 193 | &Device::GetLinkMonitorResponseTime); |
Jason Glasgow | b579005 | 2012-01-27 01:03:52 -0500 | [diff] [blame] | 194 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 195 | // TODO(cmasone): Chrome doesn't use this...does anyone? |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 196 | // store_.RegisterConstBool(kReconnectProperty, &reconnect_); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 197 | |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 198 | // TODO(cmasone): Figure out what shill concept maps to flimflam's "Network". |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 199 | // known_properties_.push_back(kNetworksProperty); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 200 | |
Wade Guthrie | 227c774 | 2013-10-10 11:06:33 -0700 | [diff] [blame] | 201 | // kRoamThresholdProperty: Registered in WiFi |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 202 | // kScanningProperty: Registered in WiFi, Cellular |
| 203 | // kScanIntervalProperty: Registered in WiFi, Cellular |
Samuel Tan | 96e35cf | 2014-10-28 22:27:38 -0700 | [diff] [blame] | 204 | // kWakeOnWiFiFeaturesEnabledProperty: Registered in WiFi |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 205 | |
| 206 | if (manager_ && manager_->device_info()) { // Unit tests may not have these. |
| 207 | manager_->device_info()->GetByteCounts( |
| 208 | interface_index_, &receive_byte_offset_, &transmit_byte_offset_); |
Ben Chan | 39a7beb | 2013-09-21 11:28:00 -0700 | [diff] [blame] | 209 | HelpRegisterConstDerivedUint64(kReceiveByteCountProperty, |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 210 | &Device::GetReceiveByteCountProperty); |
Ben Chan | 39a7beb | 2013-09-21 11:28:00 -0700 | [diff] [blame] | 211 | HelpRegisterConstDerivedUint64(kTransmitByteCountProperty, |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 212 | &Device::GetTransmitByteCountProperty); |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Paul Stewart | 2cb3fa7 | 2014-11-13 01:43:12 -0800 | [diff] [blame] | 215 | DisableArpFiltering(); |
| 216 | EnableReversePathFilter(); |
| 217 | |
Darin Petkov | a0a0efe | 2012-06-27 12:50:01 +0200 | [diff] [blame] | 218 | LOG(INFO) << "Device created: " << link_name_ |
| 219 | << " index " << interface_index_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | Device::~Device() { |
Darin Petkov | a0a0efe | 2012-06-27 12:50:01 +0200 | [diff] [blame] | 223 | LOG(INFO) << "Device destructed: " << link_name_ |
| 224 | << " index " << interface_index_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 227 | void Device::LinkEvent(unsigned flags, unsigned change) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 228 | SLOG(this, 2) << "Device " << link_name_ |
| 229 | << std::showbase << std::hex |
| 230 | << " flags " << flags << " changed " << change |
| 231 | << std::dec << std::noshowbase; |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Wade Guthrie | 4823f4f | 2013-07-25 10:03:03 -0700 | [diff] [blame] | 234 | void Device::Scan(ScanType scan_type, Error *error, const string &reason) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 235 | SLOG(this, 2) << __func__ << " [Device] on " << link_name() << " from " |
| 236 | << reason; |
Paul Stewart | 34f424e | 2015-01-16 15:30:20 -0800 | [diff] [blame] | 237 | Error::PopulateAndLog(FROM_HERE, error, Error::kNotSupported, |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 238 | "Device doesn't support scan."); |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Peter Qiu | d51b244 | 2015-02-23 10:41:38 -0800 | [diff] [blame] | 241 | void Device::SetSchedScan(bool enable, Error *error) { |
| 242 | SLOG(this, 2) << __func__ << " [Device] on " << link_name(); |
| 243 | Error::PopulateAndLog(FROM_HERE, error, Error::kNotSupported, |
| 244 | "Device doesn't support scheduled scan."); |
| 245 | } |
| 246 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 247 | void Device::RegisterOnNetwork(const std::string &/*network_id*/, Error *error, |
| 248 | const ResultCallback &/*callback*/) { |
Paul Stewart | 34f424e | 2015-01-16 15:30:20 -0800 | [diff] [blame] | 249 | Error::PopulateAndLog(FROM_HERE, error, Error::kNotSupported, |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 250 | "Device doesn't support network registration."); |
Darin Petkov | 9ae310f | 2011-08-30 15:41:13 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Darin Petkov | c64fe5e | 2012-01-11 12:46:13 +0100 | [diff] [blame] | 253 | void Device::RequirePIN( |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 254 | const string &/*pin*/, bool /*require*/, |
| 255 | Error *error, const ResultCallback &/*callback*/) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 256 | SLOG(this, 2) << __func__; |
Paul Stewart | 34f424e | 2015-01-16 15:30:20 -0800 | [diff] [blame] | 257 | Error::PopulateAndLog(FROM_HERE, error, Error::kNotSupported, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 258 | "Device doesn't support RequirePIN."); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 261 | void Device::EnterPIN(const string &/*pin*/, |
| 262 | Error *error, const ResultCallback &/*callback*/) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 263 | SLOG(this, 2) << __func__; |
Paul Stewart | 34f424e | 2015-01-16 15:30:20 -0800 | [diff] [blame] | 264 | Error::PopulateAndLog(FROM_HERE, error, Error::kNotSupported, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 265 | "Device doesn't support EnterPIN."); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 266 | } |
| 267 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 268 | void Device::UnblockPIN(const string &/*unblock_code*/, |
| 269 | const string &/*pin*/, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 270 | Error *error, const ResultCallback &/*callback*/) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 271 | SLOG(this, 2) << __func__; |
Paul Stewart | 34f424e | 2015-01-16 15:30:20 -0800 | [diff] [blame] | 272 | Error::PopulateAndLog(FROM_HERE, error, Error::kNotSupported, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 273 | "Device doesn't support UnblockPIN."); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 274 | } |
| 275 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 276 | void Device::ChangePIN(const string &/*old_pin*/, |
| 277 | const string &/*new_pin*/, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 278 | Error *error, const ResultCallback &/*callback*/) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 279 | SLOG(this, 2) << __func__; |
Paul Stewart | 34f424e | 2015-01-16 15:30:20 -0800 | [diff] [blame] | 280 | Error::PopulateAndLog(FROM_HERE, error, Error::kNotSupported, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 281 | "Device doesn't support ChangePIN."); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Ben Chan | ad663e1 | 2013-01-08 01:58:47 -0800 | [diff] [blame] | 284 | void Device::Reset(Error *error, const ResultCallback &/*callback*/) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 285 | SLOG(this, 2) << __func__; |
Paul Stewart | 34f424e | 2015-01-16 15:30:20 -0800 | [diff] [blame] | 286 | Error::PopulateAndLog(FROM_HERE, error, Error::kNotSupported, |
Ben Chan | ad663e1 | 2013-01-08 01:58:47 -0800 | [diff] [blame] | 287 | "Device doesn't support Reset."); |
| 288 | } |
| 289 | |
Darin Petkov | c37a9c4 | 2012-09-06 15:28:22 +0200 | [diff] [blame] | 290 | void Device::SetCarrier(const string &/*carrier*/, |
| 291 | Error *error, const ResultCallback &/*callback*/) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 292 | SLOG(this, 2) << __func__; |
Paul Stewart | 34f424e | 2015-01-16 15:30:20 -0800 | [diff] [blame] | 293 | Error::PopulateAndLog(FROM_HERE, error, Error::kNotSupported, |
Darin Petkov | c37a9c4 | 2012-09-06 15:28:22 +0200 | [diff] [blame] | 294 | "Device doesn't support SetCarrier."); |
| 295 | } |
| 296 | |
Ben Chan | bcc6e01 | 2013-11-04 14:28:37 -0800 | [diff] [blame] | 297 | bool Device::IsIPv6Allowed() const { |
| 298 | return true; |
| 299 | } |
| 300 | |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 301 | void Device::DisableIPv6() { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 302 | SLOG(this, 2) << __func__; |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 303 | SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "1"); |
| 304 | } |
| 305 | |
| 306 | void Device::EnableIPv6() { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 307 | SLOG(this, 2) << __func__; |
Ben Chan | bcc6e01 | 2013-11-04 14:28:37 -0800 | [diff] [blame] | 308 | if (!IsIPv6Allowed()) { |
| 309 | LOG(INFO) << "Skip enabling IPv6 on " << link_name_ |
| 310 | << " as it is not allowed."; |
| 311 | return; |
| 312 | } |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 313 | SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "0"); |
| 314 | } |
| 315 | |
| 316 | void Device::EnableIPv6Privacy() { |
| 317 | SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagUseTempAddr, |
| 318 | kIPFlagUseTempAddrUsedAndDefault); |
| 319 | } |
| 320 | |
Paul Stewart | 2cb3fa7 | 2014-11-13 01:43:12 -0800 | [diff] [blame] | 321 | void Device::SetLooseRouting(bool is_loose_routing) { |
| 322 | if (is_loose_routing == is_loose_routing_) { |
| 323 | return; |
| 324 | } |
| 325 | is_loose_routing_ = is_loose_routing; |
| 326 | if (is_multi_homed_) { |
| 327 | // Nothing to do: loose routing is already enabled, and should remain so. |
| 328 | return; |
| 329 | } |
| 330 | if (is_loose_routing) { |
| 331 | DisableReversePathFilter(); |
| 332 | } else { |
| 333 | EnableReversePathFilter(); |
| 334 | } |
| 335 | } |
| 336 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 337 | void Device::DisableReversePathFilter() { |
| 338 | // TODO(pstew): Current kernel doesn't offer reverse-path filtering flag |
Paul Stewart | ee6b3d7 | 2013-07-12 16:07:51 -0700 | [diff] [blame] | 339 | // for IPv6. crbug.com/207193 |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 340 | SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter, |
| 341 | kIPFlagReversePathFilterLooseMode); |
| 342 | } |
| 343 | |
| 344 | void Device::EnableReversePathFilter() { |
| 345 | SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter, |
| 346 | kIPFlagReversePathFilterEnabled); |
| 347 | } |
| 348 | |
Paul Stewart | 2cb3fa7 | 2014-11-13 01:43:12 -0800 | [diff] [blame] | 349 | void Device::SetIsMultiHomed(bool is_multi_homed) { |
| 350 | if (is_multi_homed == is_multi_homed_) { |
| 351 | return; |
| 352 | } |
| 353 | LOG(INFO) << "Device " << FriendlyName() << " multi-home state is now " |
| 354 | << is_multi_homed; |
| 355 | is_multi_homed_ = is_multi_homed; |
| 356 | if (is_multi_homed) { |
| 357 | EnableArpFiltering(); |
| 358 | if (!is_loose_routing_) { |
| 359 | DisableReversePathFilter(); |
| 360 | } |
| 361 | } else { |
| 362 | DisableArpFiltering(); |
| 363 | if (!is_loose_routing_) { |
| 364 | EnableReversePathFilter(); |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | void Device::DisableArpFiltering() { |
| 370 | SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagArpAnnounce, |
| 371 | kIPFlagArpAnnounceDefault); |
| 372 | SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagArpIgnore, kIPFlagArpIgnoreDefault); |
| 373 | } |
| 374 | |
| 375 | void Device::EnableArpFiltering() { |
| 376 | SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagArpAnnounce, |
| 377 | kIPFlagArpAnnounceBestLocal); |
| 378 | SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagArpIgnore, |
| 379 | kIPFlagArpIgnoreLocalOnly); |
| 380 | } |
| 381 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 382 | bool Device::IsConnected() const { |
| 383 | if (selected_service_) |
| 384 | return selected_service_->IsConnected(); |
| 385 | return false; |
| 386 | } |
| 387 | |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 388 | bool Device::IsConnectedToService(const ServiceRefPtr &service) const { |
| 389 | return service == selected_service_ && IsConnected(); |
| 390 | } |
| 391 | |
Paul Stewart | fa11e28 | 2013-12-02 22:04:25 -0800 | [diff] [blame] | 392 | bool Device::IsConnectedViaTether() const { |
| 393 | return |
| 394 | ipconfig_.get() && |
| 395 | ipconfig_->properties().vendor_encapsulated_options == |
| 396 | Tethering::kAndroidVendorEncapsulatedOptions; |
| 397 | } |
| 398 | |
mukesh agrawal | f6b3209 | 2013-04-10 15:49:55 -0700 | [diff] [blame] | 399 | string Device::GetRpcIdentifier() const { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 400 | return adaptor_->GetRpcIdentifier(); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 401 | } |
| 402 | |
mukesh agrawal | 515873d | 2014-07-21 17:01:35 -0700 | [diff] [blame] | 403 | string Device::GetStorageIdentifier() const { |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 404 | string id = GetRpcIdentifier(); |
| 405 | ControlInterface::RpcIdToStorageId(&id); |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 406 | size_t needle = id.find('_'); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 407 | DLOG_IF(ERROR, needle == string::npos) << "No _ in storage id?!?!"; |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 408 | id.replace(id.begin() + needle + 1, id.end(), hardware_address_); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 409 | return id; |
| 410 | } |
| 411 | |
Gaurav Shah | 6d2c72d | 2012-10-16 16:30:44 -0700 | [diff] [blame] | 412 | vector<GeolocationInfo> Device::GetGeolocationObjects() const { |
| 413 | return vector<GeolocationInfo>(); |
Wade Guthrie | 227c774 | 2013-10-10 11:06:33 -0700 | [diff] [blame] | 414 | } |
Gaurav Shah | 6d2c72d | 2012-10-16 16:30:44 -0700 | [diff] [blame] | 415 | |
Jason Glasgow | b579005 | 2012-01-27 01:03:52 -0500 | [diff] [blame] | 416 | string Device::GetTechnologyString(Error */*error*/) { |
| 417 | return Technology::NameFromIdentifier(technology()); |
| 418 | } |
| 419 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 420 | const string& Device::FriendlyName() const { |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 421 | return link_name_; |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 422 | } |
| 423 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 424 | const string& Device::UniqueName() const { |
| 425 | return unique_id_; |
| 426 | } |
| 427 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 428 | bool Device::Load(StoreInterface *storage) { |
| 429 | const string id = GetStorageIdentifier(); |
| 430 | if (!storage->ContainsGroup(id)) { |
| 431 | LOG(WARNING) << "Device is not available in the persistent store: " << id; |
| 432 | return false; |
| 433 | } |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 434 | enabled_persistent_ = true; |
| 435 | storage->GetBool(id, kStoragePowered, &enabled_persistent_); |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 436 | uint64_t rx_byte_count = 0, tx_byte_count = 0; |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 437 | |
| 438 | manager_->device_info()->GetByteCounts( |
| 439 | interface_index_, &rx_byte_count, &tx_byte_count); |
| 440 | // If there is a byte-count present in the profile, the return value |
| 441 | // of Device::Get*ByteCount() should be the this stored value plus |
| 442 | // whatever additional bytes we receive since time-of-load. We |
| 443 | // accomplish this by the subtractions below, which can validly |
| 444 | // roll over "negative" in the subtractions below and in Get*ByteCount. |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 445 | uint64_t profile_byte_count; |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 446 | if (storage->GetUint64(id, kStorageReceiveByteCount, &profile_byte_count)) { |
| 447 | receive_byte_offset_ = rx_byte_count - profile_byte_count; |
| 448 | } |
| 449 | if (storage->GetUint64(id, kStorageTransmitByteCount, &profile_byte_count)) { |
| 450 | transmit_byte_offset_ = tx_byte_count - profile_byte_count; |
| 451 | } |
| 452 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 453 | return true; |
| 454 | } |
| 455 | |
| 456 | bool Device::Save(StoreInterface *storage) { |
| 457 | const string id = GetStorageIdentifier(); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 458 | storage->SetBool(id, kStoragePowered, enabled_persistent_); |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 459 | storage->SetUint64(id, kStorageReceiveByteCount, GetReceiveByteCount()); |
| 460 | storage->SetUint64(id, kStorageTransmitByteCount, GetTransmitByteCount()); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 461 | return true; |
| 462 | } |
| 463 | |
Samuel Tan | fbe8d2b | 2014-09-15 20:23:59 -0700 | [diff] [blame] | 464 | void Device::OnBeforeSuspend(const ResultCallback &callback) { |
| 465 | // Nothing to be done in the general case, so immediately report success. |
| 466 | callback.Run(Error(Error::kSuccess)); |
mukesh agrawal | 784566d | 2012-08-08 18:32:58 -0700 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | void Device::OnAfterResume() { |
Samuel Tan | 787a1ce | 2014-11-11 17:17:27 -0800 | [diff] [blame] | 470 | RenewDHCPLease(); |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 471 | if (link_monitor_) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 472 | SLOG(this, 3) << "Informing Link Monitor of resume."; |
mukesh agrawal | bb2231c | 2013-07-17 16:32:24 -0700 | [diff] [blame] | 473 | link_monitor_->OnAfterResume(); |
| 474 | } |
Peter Qiu | a057203 | 2014-09-26 10:07:37 -0700 | [diff] [blame] | 475 | // Resume from sleep, could be in different location now. |
| 476 | // Ignore previous link monitor failures. |
Peter Qiu | a388fdb | 2015-04-03 10:31:22 -0700 | [diff] [blame] | 477 | if (selected_service_) { |
| 478 | selected_service_->set_unreliable(false); |
| 479 | reliable_link_callback_.Cancel(); |
| 480 | } |
Peter Qiu | a057203 | 2014-09-26 10:07:37 -0700 | [diff] [blame] | 481 | last_link_monitor_failed_time_ = 0; |
mukesh agrawal | 784566d | 2012-08-08 18:32:58 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Samuel Tan | 68b73d2 | 2014-10-28 17:00:56 -0700 | [diff] [blame] | 484 | void Device::OnDarkResume(const ResultCallback &callback) { |
| 485 | // Nothing to be done in the general case, so immediately report success. |
| 486 | callback.Run(Error(Error::kSuccess)); |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Darin Petkov | 2b8e44e | 2012-06-25 15:13:26 +0200 | [diff] [blame] | 489 | void Device::DropConnection() { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 490 | SLOG(this, 2) << __func__; |
Darin Petkov | 2b8e44e | 2012-06-25 15:13:26 +0200 | [diff] [blame] | 491 | DestroyIPConfig(); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 492 | SelectService(nullptr); |
Darin Petkov | 2b8e44e | 2012-06-25 15:13:26 +0200 | [diff] [blame] | 493 | } |
| 494 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 495 | void Device::DestroyIPConfig() { |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 496 | DisableIPv6(); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 497 | if (ipconfig_.get()) { |
Paul Stewart | 217c61d | 2013-06-13 15:12:02 -0700 | [diff] [blame] | 498 | ipconfig_->ReleaseIP(IPConfig::kReleaseReasonDisconnect); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 499 | ipconfig_ = nullptr; |
Paul Stewart | d4f2648 | 2014-04-25 19:12:03 -0700 | [diff] [blame] | 500 | UpdateIPConfigsProperty(); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 501 | } |
Peter Qiu | 25f1be6 | 2014-08-12 10:42:27 -0700 | [diff] [blame] | 502 | if (ip6config_.get()) { |
| 503 | StopIPv6DNSServerTimer(); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 504 | ip6config_ = nullptr; |
Peter Qiu | 25f1be6 | 2014-08-12 10:42:27 -0700 | [diff] [blame] | 505 | UpdateIPConfigsProperty(); |
| 506 | } |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 507 | DestroyConnection(); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Paul Stewart | d4f2648 | 2014-04-25 19:12:03 -0700 | [diff] [blame] | 510 | void Device::OnIPv6AddressChanged() { |
| 511 | IPAddress address(IPAddress::kFamilyIPv6); |
| 512 | if (!manager_->device_info()->GetPrimaryIPv6Address( |
| 513 | interface_index_, &address)) { |
| 514 | if (ip6config_) { |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 515 | ip6config_ = nullptr; |
Paul Stewart | d4f2648 | 2014-04-25 19:12:03 -0700 | [diff] [blame] | 516 | UpdateIPConfigsProperty(); |
| 517 | } |
| 518 | return; |
| 519 | } |
| 520 | |
| 521 | IPConfig::Properties properties; |
| 522 | if (!address.IntoString(&properties.address)) { |
| 523 | LOG(ERROR) << "Unable to convert IPv6 address into a string!"; |
| 524 | return; |
| 525 | } |
| 526 | properties.subnet_prefix = address.prefix(); |
| 527 | |
| 528 | if (!ip6config_) { |
| 529 | ip6config_ = new IPConfig(control_interface_, link_name_); |
| 530 | } else if (properties.address == ip6config_->properties().address && |
| 531 | properties.subnet_prefix == |
| 532 | ip6config_->properties().subnet_prefix) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 533 | SLOG(this, 2) << __func__ << " primary address for " |
| 534 | << link_name_ << " is unchanged."; |
Paul Stewart | d4f2648 | 2014-04-25 19:12:03 -0700 | [diff] [blame] | 535 | return; |
| 536 | } |
| 537 | |
| 538 | properties.address_family = IPAddress::kFamilyIPv6; |
| 539 | properties.method = kTypeIPv6; |
Peter Qiu | 25f1be6 | 2014-08-12 10:42:27 -0700 | [diff] [blame] | 540 | // It is possible for device to receive DNS server notification before IP |
| 541 | // address notification, so preserve the saved DNS server if it exist. |
| 542 | properties.dns_servers = ip6config_->properties().dns_servers; |
Garret Kelly | c5f89d1 | 2015-02-18 14:39:36 -0500 | [diff] [blame] | 543 | PrependDNSServers(IPAddress::kFamilyIPv6, &properties.dns_servers); |
Paul Stewart | d4f2648 | 2014-04-25 19:12:03 -0700 | [diff] [blame] | 544 | ip6config_->set_properties(properties); |
| 545 | UpdateIPConfigsProperty(); |
Peter Qiu | b25083f | 2014-08-25 13:22:31 -0700 | [diff] [blame] | 546 | OnIPv6ConfigUpdated(); |
Paul Stewart | d4f2648 | 2014-04-25 19:12:03 -0700 | [diff] [blame] | 547 | } |
| 548 | |
Peter Qiu | 9855170 | 2014-07-28 13:28:53 -0700 | [diff] [blame] | 549 | void Device::OnIPv6DnsServerAddressesChanged() { |
Peter Qiu | 25f1be6 | 2014-08-12 10:42:27 -0700 | [diff] [blame] | 550 | vector<IPAddress> server_addresses; |
| 551 | uint32 lifetime; |
| 552 | |
| 553 | // Stop any existing timer. |
| 554 | StopIPv6DNSServerTimer(); |
| 555 | |
| 556 | if (!manager_->device_info()->GetIPv6DnsServerAddresses( |
| 557 | interface_index_, &server_addresses, &lifetime) || lifetime == 0) { |
| 558 | IPv6DNSServerExpired(); |
| 559 | return; |
| 560 | } |
| 561 | |
| 562 | vector<string> addresses_str; |
| 563 | for (const auto &ip : server_addresses) { |
| 564 | string address_str; |
| 565 | if (!ip.IntoString(&address_str)) { |
| 566 | LOG(ERROR) << "Unable to convert IPv6 address into a string!"; |
| 567 | IPv6DNSServerExpired(); |
| 568 | return; |
| 569 | } |
| 570 | addresses_str.push_back(address_str); |
| 571 | } |
| 572 | |
Peter Qiu | 25f1be6 | 2014-08-12 10:42:27 -0700 | [diff] [blame] | 573 | if (!ip6config_) { |
| 574 | ip6config_ = new IPConfig(control_interface_, link_name_); |
| 575 | } |
| 576 | |
Samuel Tan | 815a6fb | 2014-10-23 16:53:59 -0700 | [diff] [blame] | 577 | if (lifetime != ND_OPT_LIFETIME_INFINITY) { |
| 578 | // Setup timer to monitor DNS server lifetime if not infinite lifetime. |
| 579 | StartIPv6DNSServerTimer(lifetime); |
| 580 | ip6config_->UpdateLeaseExpirationTime(lifetime); |
| 581 | } else { |
| 582 | ip6config_->ResetLeaseExpirationTime(); |
| 583 | } |
| 584 | |
Garret Kelly | c5f89d1 | 2015-02-18 14:39:36 -0500 | [diff] [blame] | 585 | PrependDNSServers(IPAddress::kFamilyIPv6, &addresses_str); |
| 586 | |
Peter Qiu | 25f1be6 | 2014-08-12 10:42:27 -0700 | [diff] [blame] | 587 | // Done if no change in server addresses. |
| 588 | if (ip6config_->properties().dns_servers == addresses_str) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 589 | SLOG(this, 2) << __func__ << " IPv6 DNS server list for " |
| 590 | << link_name_ << " is unchanged."; |
Peter Qiu | 25f1be6 | 2014-08-12 10:42:27 -0700 | [diff] [blame] | 591 | return; |
| 592 | } |
| 593 | |
| 594 | ip6config_->UpdateDNSServers(addresses_str); |
| 595 | UpdateIPConfigsProperty(); |
Peter Qiu | b25083f | 2014-08-25 13:22:31 -0700 | [diff] [blame] | 596 | OnIPv6ConfigUpdated(); |
Peter Qiu | 25f1be6 | 2014-08-12 10:42:27 -0700 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | void Device::StartIPv6DNSServerTimer(uint32 lifetime_seconds) { |
| 600 | int64 delay = static_cast<int64>(lifetime_seconds) * 1000; |
| 601 | ipv6_dns_server_expired_callback_.Reset( |
| 602 | base::Bind(&Device::IPv6DNSServerExpired, base::Unretained(this))); |
| 603 | dispatcher_->PostDelayedTask(ipv6_dns_server_expired_callback_.callback(), |
| 604 | delay); |
| 605 | } |
| 606 | |
| 607 | void Device::StopIPv6DNSServerTimer() { |
| 608 | ipv6_dns_server_expired_callback_.Cancel(); |
| 609 | } |
| 610 | |
| 611 | void Device::IPv6DNSServerExpired() { |
| 612 | if (!ip6config_) { |
| 613 | return; |
| 614 | } |
| 615 | ip6config_->UpdateDNSServers(vector<string>()); |
| 616 | UpdateIPConfigsProperty(); |
| 617 | } |
| 618 | |
| 619 | void Device::StopAllActivities() { |
| 620 | StopTrafficMonitor(); |
| 621 | StopPortalDetection(); |
Paul Stewart | e8303eb | 2014-10-08 22:51:14 -0700 | [diff] [blame] | 622 | StopConnectivityTest(); |
Peter Qiu | 25f1be6 | 2014-08-12 10:42:27 -0700 | [diff] [blame] | 623 | StopLinkMonitor(); |
| 624 | StopDNSTest(); |
| 625 | StopIPv6DNSServerTimer(); |
Peter Qiu | 9855170 | 2014-07-28 13:28:53 -0700 | [diff] [blame] | 626 | } |
| 627 | |
Samuel Tan | 96bdaec | 2014-11-05 18:27:38 -0800 | [diff] [blame] | 628 | void Device::AddWakeOnPacketConnection(const string &ip_endpoint, |
Samuel Tan | fe73467 | 2014-08-07 15:50:48 -0700 | [diff] [blame] | 629 | Error *error) { |
Samuel Tan | 5412de0 | 2014-08-15 17:56:26 -0700 | [diff] [blame] | 630 | Error::PopulateAndLog( |
Paul Stewart | 34f424e | 2015-01-16 15:30:20 -0800 | [diff] [blame] | 631 | FROM_HERE, error, Error::kNotSupported, |
Samuel Tan | fe73467 | 2014-08-07 15:50:48 -0700 | [diff] [blame] | 632 | "AddWakeOnPacketConnection not implemented for " + link_name_ + "."); |
| 633 | return; |
| 634 | } |
| 635 | |
Samuel Tan | 96bdaec | 2014-11-05 18:27:38 -0800 | [diff] [blame] | 636 | void Device::RemoveWakeOnPacketConnection(const string &ip_endpoint, |
Samuel Tan | fe73467 | 2014-08-07 15:50:48 -0700 | [diff] [blame] | 637 | Error *error) { |
Samuel Tan | 5412de0 | 2014-08-15 17:56:26 -0700 | [diff] [blame] | 638 | Error::PopulateAndLog( |
Paul Stewart | 34f424e | 2015-01-16 15:30:20 -0800 | [diff] [blame] | 639 | FROM_HERE, error, Error::kNotSupported, |
Samuel Tan | fe73467 | 2014-08-07 15:50:48 -0700 | [diff] [blame] | 640 | "RemoveWakeOnPacketConnection not implemented for " + link_name_ + "."); |
| 641 | return; |
| 642 | } |
| 643 | |
| 644 | void Device::RemoveAllWakeOnPacketConnections(Error *error) { |
Samuel Tan | 5412de0 | 2014-08-15 17:56:26 -0700 | [diff] [blame] | 645 | Error::PopulateAndLog( |
Paul Stewart | 34f424e | 2015-01-16 15:30:20 -0800 | [diff] [blame] | 646 | FROM_HERE, error, Error::kNotSupported, |
Samuel Tan | fe73467 | 2014-08-07 15:50:48 -0700 | [diff] [blame] | 647 | "RemoveAllWakeOnPacketConnections not implemented for " + link_name_ + |
| 648 | "."); |
| 649 | return; |
| 650 | } |
| 651 | |
Samuel Tan | 787a1ce | 2014-11-11 17:17:27 -0800 | [diff] [blame] | 652 | void Device::RenewDHCPLease() { |
| 653 | LOG(INFO) << __func__; |
| 654 | |
| 655 | if (ipconfig_) { |
| 656 | SLOG(this, 3) << "Renewing IPv4 Address"; |
| 657 | ipconfig_->RenewIP(); |
| 658 | } |
| 659 | if (ip6config_) { |
| 660 | SLOG(this, 3) << "Waiting for new IPv6 configuration"; |
| 661 | // Invalidate the old IPv6 configuration, will receive notifications |
| 662 | // from kernel for new IPv6 configuration if there is one. |
| 663 | StopIPv6DNSServerTimer(); |
| 664 | ip6config_ = nullptr; |
| 665 | UpdateIPConfigsProperty(); |
| 666 | } |
| 667 | } |
| 668 | |
Arman Uguray | ed8e610 | 2012-11-29 14:47:20 -0800 | [diff] [blame] | 669 | bool Device::ShouldUseArpGateway() const { |
| 670 | return false; |
| 671 | } |
| 672 | |
Paul Stewart | 316acef | 2014-05-29 18:40:48 -0700 | [diff] [blame] | 673 | bool Device::IsUsingStaticIP() const { |
| 674 | if (!selected_service_) { |
| 675 | return false; |
| 676 | } |
| 677 | return selected_service_->HasStaticIPAddress(); |
| 678 | } |
| 679 | |
Garret Kelly | d01b5cc | 2015-03-12 16:20:55 -0400 | [diff] [blame] | 680 | bool Device::IsUsingStaticNameServers() const { |
| 681 | if (!selected_service_) { |
| 682 | return false; |
| 683 | } |
| 684 | return selected_service_->HasStaticNameServers(); |
| 685 | } |
| 686 | |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 687 | bool Device::AcquireIPConfig() { |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 688 | return AcquireIPConfigWithLeaseName(string()); |
| 689 | } |
| 690 | |
| 691 | bool Device::AcquireIPConfigWithLeaseName(const string &lease_name) { |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 692 | DestroyIPConfig(); |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 693 | EnableIPv6(); |
Arman Uguray | ed8e610 | 2012-11-29 14:47:20 -0800 | [diff] [blame] | 694 | bool arp_gateway = manager_->GetArpGateway() && ShouldUseArpGateway(); |
Peter Qiu | 8e0151e | 2015-06-04 09:41:47 -0700 | [diff] [blame^] | 695 | auto dhcp_config = dhcp_provider_->CreateIPv4Config( |
| 696 | link_name_, manager_->GetHostName(), lease_name, arp_gateway); |
Garret Kelly | 782cdce | 2015-04-01 16:39:16 -0400 | [diff] [blame] | 697 | const int minimum_mtu = manager()->GetMinimumMTU(); |
| 698 | if (minimum_mtu != IPConfig::kUndefinedMTU) { |
| 699 | dhcp_config->set_minimum_mtu(minimum_mtu); |
| 700 | } |
| 701 | |
| 702 | ipconfig_ = dhcp_config; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 703 | ipconfig_->RegisterUpdateCallback(Bind(&Device::OnIPConfigUpdated, |
| 704 | weak_ptr_factory_.GetWeakPtr())); |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 705 | ipconfig_->RegisterFailureCallback(Bind(&Device::OnIPConfigFailed, |
| 706 | weak_ptr_factory_.GetWeakPtr())); |
Paul Stewart | 8223653 | 2013-12-10 15:33:11 -0800 | [diff] [blame] | 707 | ipconfig_->RegisterRefreshCallback(Bind(&Device::OnIPConfigRefreshed, |
| 708 | weak_ptr_factory_.GetWeakPtr())); |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame] | 709 | ipconfig_->RegisterExpireCallback(Bind(&Device::OnIPConfigExpired, |
| 710 | weak_ptr_factory_.GetWeakPtr())); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 711 | dispatcher_->PostTask(Bind(&Device::ConfigureStaticIPTask, |
| 712 | weak_ptr_factory_.GetWeakPtr())); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 713 | return ipconfig_->RequestIP(); |
| 714 | } |
| 715 | |
Ben Chan | 539ab02 | 2014-02-03 16:34:57 -0800 | [diff] [blame] | 716 | void Device::AssignIPConfig(const IPConfig::Properties &properties) { |
| 717 | DestroyIPConfig(); |
| 718 | EnableIPv6(); |
| 719 | ipconfig_ = new IPConfig(control_interface_, link_name_); |
| 720 | ipconfig_->set_properties(properties); |
| 721 | dispatcher_->PostTask(Bind(&Device::OnIPConfigUpdated, |
Samuel Tan | 3c3c36a | 2014-12-16 16:53:19 -0800 | [diff] [blame] | 722 | weak_ptr_factory_.GetWeakPtr(), ipconfig_, true)); |
Ben Chan | 539ab02 | 2014-02-03 16:34:57 -0800 | [diff] [blame] | 723 | } |
| 724 | |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 725 | void Device::DestroyIPConfigLease(const string &name) { |
| 726 | dhcp_provider_->DestroyLease(name); |
| 727 | } |
| 728 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 729 | void Device::HelpRegisterConstDerivedString( |
Jason Glasgow | b579005 | 2012-01-27 01:03:52 -0500 | [diff] [blame] | 730 | const string &name, |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 731 | string(Device::*get)(Error *error)) { |
Jason Glasgow | b579005 | 2012-01-27 01:03:52 -0500 | [diff] [blame] | 732 | store_.RegisterDerivedString( |
| 733 | name, |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 734 | StringAccessor(new CustomAccessor<Device, string>(this, get, nullptr))); |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 735 | } |
| 736 | |
Christopher Wiley | 674598d | 2014-12-12 10:21:39 -0800 | [diff] [blame] | 737 | void Device::HelpRegisterConstDerivedRpcIdentifier( |
| 738 | const string &name, |
| 739 | RpcIdentifier(Device::*get)(Error *error)) { |
| 740 | store_.RegisterDerivedRpcIdentifier( |
| 741 | name, |
| 742 | RpcIdentifierAccessor( |
| 743 | new CustomAccessor<Device, RpcIdentifier>(this, get, nullptr))); |
| 744 | } |
| 745 | |
Jason Glasgow | 08afdff | 2012-04-03 10:22:26 -0400 | [diff] [blame] | 746 | void Device::HelpRegisterConstDerivedRpcIdentifiers( |
| 747 | const string &name, |
| 748 | RpcIdentifiers(Device::*get)(Error *)) { |
| 749 | store_.RegisterDerivedRpcIdentifiers( |
| 750 | name, |
| 751 | RpcIdentifiersAccessor( |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 752 | new CustomAccessor<Device, RpcIdentifiers>(this, get, nullptr))); |
Jason Glasgow | 08afdff | 2012-04-03 10:22:26 -0400 | [diff] [blame] | 753 | } |
| 754 | |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 755 | void Device::HelpRegisterConstDerivedUint64( |
| 756 | const string &name, |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 757 | uint64_t(Device::*get)(Error *)) { |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 758 | store_.RegisterDerivedUint64( |
| 759 | name, |
| 760 | Uint64Accessor( |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 761 | new CustomAccessor<Device, uint64_t>(this, get, nullptr))); |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 762 | } |
| 763 | |
Rebecca Silberstein | f4365a6 | 2014-09-16 11:40:32 -0700 | [diff] [blame] | 764 | void Device::ConnectionTesterCallback() { |
| 765 | LOG(INFO) << "Device " << FriendlyName() << ": Completed Connectivity Test"; |
| 766 | return; |
| 767 | } |
| 768 | |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 769 | void Device::ConfigureStaticIPTask() { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 770 | SLOG(this, 2) << __func__ << " selected_service " << selected_service_.get() |
| 771 | << " ipconfig " << ipconfig_.get(); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 772 | |
| 773 | if (!selected_service_ || !ipconfig_) { |
| 774 | return; |
| 775 | } |
| 776 | |
Paul Stewart | 316acef | 2014-05-29 18:40:48 -0700 | [diff] [blame] | 777 | if (IsUsingStaticIP()) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 778 | SLOG(this, 2) << __func__ << " " << " configuring static IP parameters."; |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 779 | // If the parameters contain an IP address, apply them now and bring |
| 780 | // the interface up. When DHCP information arrives, it will supplement |
| 781 | // the static information. |
Samuel Tan | 3c3c36a | 2014-12-16 16:53:19 -0800 | [diff] [blame] | 782 | OnIPConfigUpdated(ipconfig_, true); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 783 | } else { |
Paul Stewart | 8223653 | 2013-12-10 15:33:11 -0800 | [diff] [blame] | 784 | // Either |ipconfig_| has just been created in AcquireIPConfig() or |
| 785 | // we're being called by OnIPConfigRefreshed(). In either case a |
| 786 | // DHCP client has been started, and will take care of calling |
| 787 | // OnIPConfigUpdated() when it completes. |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 788 | SLOG(this, 2) << __func__ << " " << " no static IP address."; |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 789 | } |
| 790 | } |
| 791 | |
Peter Qiu | b25083f | 2014-08-25 13:22:31 -0700 | [diff] [blame] | 792 | bool Device::IPConfigCompleted(const IPConfigRefPtr &ipconfig) { |
| 793 | return ipconfig && !ipconfig->properties().address.empty() && |
| 794 | !ipconfig->properties().dns_servers.empty(); |
| 795 | } |
| 796 | |
| 797 | void Device::OnIPv6ConfigUpdated() { |
| 798 | // Setup connection using IPv6 configuration only if the IPv6 configuration |
| 799 | // is ready for connection (contained both IP address and DNS servers), and |
| 800 | // there is no existing IPv4 connection. We always prefer IPv4 |
| 801 | // configuration over IPv6. |
| 802 | if (IPConfigCompleted(ip6config_) && |
| 803 | (!connection_ || connection_->IsIPv6())) { |
| 804 | SetupConnection(ip6config_); |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | void Device::SetupConnection(const IPConfigRefPtr &ipconfig) { |
| 809 | CreateConnection(); |
| 810 | connection_->UpdateFromIPConfig(ipconfig); |
| 811 | |
Peter Qiu | 300769e | 2014-08-27 11:48:45 -0700 | [diff] [blame] | 812 | // Report connection type. |
| 813 | Metrics::NetworkConnectionIPType ip_type = |
| 814 | connection_->IsIPv6() ? Metrics::kNetworkConnectionIPTypeIPv6 |
| 815 | : Metrics::kNetworkConnectionIPTypeIPv4; |
| 816 | metrics_->NotifyNetworkConnectionIPType(technology_, ip_type); |
| 817 | |
| 818 | // Report if device have IPv6 connectivity |
| 819 | bool ipv6_connectivity = IPConfigCompleted(ip6config_); |
| 820 | metrics_->NotifyIPv6ConnectivityStatus(technology_, ipv6_connectivity); |
| 821 | |
Peter Qiu | b25083f | 2014-08-25 13:22:31 -0700 | [diff] [blame] | 822 | // SetConnection must occur after the UpdateFromIPConfig so the |
| 823 | // service can use the values derived from the connection. |
| 824 | if (selected_service_) { |
| 825 | selected_service_->SetConnection(connection_); |
| 826 | |
| 827 | // The service state change needs to happen last, so that at the |
| 828 | // time we report the state change to the manager, the service |
| 829 | // has its connection. |
| 830 | SetServiceState(Service::kStateConnected); |
| 831 | OnConnected(); |
| 832 | portal_attempts_to_online_ = 0; |
| 833 | |
| 834 | // Subtle: Start portal detection after transitioning the service |
| 835 | // to the Connected state because this call may immediately transition |
| 836 | // to the Online state. |
| 837 | StartPortalDetection(); |
| 838 | } |
| 839 | |
Paul Stewart | 208a97e | 2015-05-13 09:11:12 -0700 | [diff] [blame] | 840 | SetHostname(ipconfig->properties().accepted_hostname); |
Peter Qiu | b25083f | 2014-08-25 13:22:31 -0700 | [diff] [blame] | 841 | StartLinkMonitor(); |
| 842 | StartTrafficMonitor(); |
| 843 | } |
| 844 | |
Paul Stewart | 208a97e | 2015-05-13 09:11:12 -0700 | [diff] [blame] | 845 | bool Device::SetHostname(const std::string &hostname) { |
| 846 | if (hostname.empty() || !manager()->ShouldAcceptHostnameFrom(link_name_)) { |
| 847 | return false; |
| 848 | } |
| 849 | |
| 850 | string fixed_hostname = hostname; |
| 851 | if (fixed_hostname.length() > MAXHOSTNAMELEN) { |
| 852 | auto truncate_length = fixed_hostname.find('.'); |
| 853 | if (truncate_length == string::npos || truncate_length > MAXHOSTNAMELEN) { |
| 854 | truncate_length = MAXHOSTNAMELEN; |
| 855 | } |
| 856 | fixed_hostname.resize(truncate_length); |
| 857 | } |
| 858 | |
| 859 | return manager_->device_info()->SetHostname(fixed_hostname); |
| 860 | } |
| 861 | |
Garret Kelly | c5f89d1 | 2015-02-18 14:39:36 -0500 | [diff] [blame] | 862 | void Device::PrependDNSServersIntoIPConfig(const IPConfigRefPtr &ipconfig) { |
| 863 | const auto &properties = ipconfig->properties(); |
| 864 | |
| 865 | vector<string> servers(properties.dns_servers.begin(), |
| 866 | properties.dns_servers.end()); |
| 867 | PrependDNSServers(properties.address_family, &servers); |
| 868 | if (servers == properties.dns_servers) { |
| 869 | // If the server list is the same after being augmented then there's no need |
| 870 | // to update the config's list of servers. |
| 871 | return; |
| 872 | } |
| 873 | |
| 874 | ipconfig->UpdateDNSServers(servers); |
| 875 | } |
| 876 | |
| 877 | void Device::PrependDNSServers(const IPAddress::Family family, |
| 878 | vector<string> *servers) { |
| 879 | vector<string> suffix(servers->begin(), servers->end()); |
| 880 | manager_->FilterPrependDNSServersByFamily(family, servers); |
| 881 | |
| 882 | set<string> unique(servers->begin(), servers->end()); |
| 883 | for (const auto &server : suffix) { |
| 884 | if (unique.find(server) == unique.end()) { |
| 885 | servers->push_back(server); |
| 886 | unique.insert(server); |
| 887 | } |
| 888 | } |
| 889 | } |
| 890 | |
Samuel Tan | 3c3c36a | 2014-12-16 16:53:19 -0800 | [diff] [blame] | 891 | void Device::OnIPConfigUpdated(const IPConfigRefPtr &ipconfig, |
| 892 | bool /*new_lease_acquired*/) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 893 | SLOG(this, 2) << __func__; |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 894 | if (selected_service_) { |
| 895 | ipconfig->ApplyStaticIPParameters( |
| 896 | selected_service_->mutable_static_ip_parameters()); |
Paul Stewart | 316acef | 2014-05-29 18:40:48 -0700 | [diff] [blame] | 897 | if (IsUsingStaticIP()) { |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 898 | // If we are using a statically configured IP address instead |
| 899 | // of a leased IP address, release any acquired lease so it may |
| 900 | // be used by others. This allows us to merge other non-leased |
| 901 | // parameters (like DNS) when they're available from a DHCP server |
| 902 | // and not overridden by static parameters, but at the same time |
| 903 | // we avoid taking up a dynamic IP address the DHCP server could |
| 904 | // assign to someone else who might actually use it. |
| 905 | ipconfig->ReleaseIP(IPConfig::kReleaseReasonStaticIP); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 906 | } |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 907 | } |
Garret Kelly | d01b5cc | 2015-03-12 16:20:55 -0400 | [diff] [blame] | 908 | if (!IsUsingStaticNameServers()) { |
| 909 | PrependDNSServersIntoIPConfig(ipconfig); |
| 910 | } |
Peter Qiu | b25083f | 2014-08-25 13:22:31 -0700 | [diff] [blame] | 911 | SetupConnection(ipconfig); |
Paul Stewart | d4f2648 | 2014-04-25 19:12:03 -0700 | [diff] [blame] | 912 | UpdateIPConfigsProperty(); |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | void Device::OnIPConfigFailed(const IPConfigRefPtr &ipconfig) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 916 | SLOG(this, 2) << __func__; |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 917 | // TODO(pstew): This logic gets yet more complex when multiple |
| 918 | // IPConfig types are run in parallel (e.g. DHCP and DHCP6) |
| 919 | if (selected_service_) { |
Paul Stewart | 316acef | 2014-05-29 18:40:48 -0700 | [diff] [blame] | 920 | if (IsUsingStaticIP()) { |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 921 | // Consider three cases: |
| 922 | // |
| 923 | // 1. We're here because DHCP failed while starting up. There |
| 924 | // are two subcases: |
| 925 | // a. DHCP has failed, and Static IP config has _not yet_ |
| 926 | // completed. It's fine to do nothing, because we'll |
| 927 | // apply the static config shortly. |
| 928 | // b. DHCP has failed, and Static IP config has _already_ |
| 929 | // completed. It's fine to do nothing, because we can |
| 930 | // continue to use the static config that's already |
| 931 | // been applied. |
| 932 | // |
| 933 | // 2. We're here because a previously valid DHCP configuration |
| 934 | // is no longer valid. There's still a static IP config, |
| 935 | // because the condition in the if clause evaluated to true. |
| 936 | // Furthermore, the static config includes an IP address for |
| 937 | // us to use. |
| 938 | // |
| 939 | // The current configuration may include some DHCP |
| 940 | // parameters, overriden by any static parameters |
| 941 | // provided. We continue to use this configuration, because |
| 942 | // the only configuration element that is leased to us (IP |
| 943 | // address) will be overriden by a static parameter. |
| 944 | return; |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | ipconfig->ResetProperties(); |
Paul Stewart | d4f2648 | 2014-04-25 19:12:03 -0700 | [diff] [blame] | 949 | UpdateIPConfigsProperty(); |
Peter Qiu | b25083f | 2014-08-25 13:22:31 -0700 | [diff] [blame] | 950 | |
| 951 | // Fallback to IPv6 if we have an IPv6 configuration that's ready for |
| 952 | // connection, and we're not currently using an IPv6 connection. |
| 953 | if (IPConfigCompleted(ip6config_) && |
| 954 | (!connection_ || !connection_->IsIPv6())) { |
| 955 | SetupConnection(ip6config_); |
| 956 | return; |
| 957 | } |
| 958 | |
| 959 | OnIPConfigFailure(); |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 960 | DestroyConnection(); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 961 | } |
| 962 | |
Paul Stewart | 8223653 | 2013-12-10 15:33:11 -0800 | [diff] [blame] | 963 | void Device::OnIPConfigRefreshed(const IPConfigRefPtr &ipconfig) { |
| 964 | // Clear the previously applied static IP parameters. |
| 965 | ipconfig->RestoreSavedIPParameters( |
| 966 | selected_service_->mutable_static_ip_parameters()); |
| 967 | |
| 968 | dispatcher_->PostTask(Bind(&Device::ConfigureStaticIPTask, |
| 969 | weak_ptr_factory_.GetWeakPtr())); |
| 970 | } |
| 971 | |
Paul Stewart | f6f9648 | 2013-07-12 12:49:15 -0700 | [diff] [blame] | 972 | void Device::OnIPConfigFailure() { |
| 973 | if (selected_service_) { |
| 974 | Error error; |
Samuel Tan | 0d06119 | 2014-07-07 15:45:15 -0700 | [diff] [blame] | 975 | selected_service_->DisconnectWithFailure(Service::kFailureDHCP, |
| 976 | &error, |
| 977 | __func__); |
Paul Stewart | f6f9648 | 2013-07-12 12:49:15 -0700 | [diff] [blame] | 978 | } |
| 979 | } |
| 980 | |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame] | 981 | void Device::OnIPConfigExpired(const IPConfigRefPtr &ipconfig) { |
| 982 | metrics()->SendToUMA( |
| 983 | metrics()->GetFullMetricName( |
mukesh agrawal | 132e96f | 2014-04-24 11:49:42 -0700 | [diff] [blame] | 984 | Metrics::kMetricExpiredLeaseLengthSecondsSuffix, technology()), |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame] | 985 | ipconfig->properties().lease_duration_seconds, |
| 986 | Metrics::kMetricExpiredLeaseLengthSecondsMin, |
| 987 | Metrics::kMetricExpiredLeaseLengthSecondsMax, |
| 988 | Metrics::kMetricExpiredLeaseLengthSecondsNumBuckets); |
| 989 | } |
| 990 | |
Peter Qiu | a388fdb | 2015-04-03 10:31:22 -0700 | [diff] [blame] | 991 | void Device::OnConnected() { |
| 992 | if (selected_service_->unreliable()) { |
| 993 | // Post a delayed task to reset link back to reliable if no link |
| 994 | // failure is detected in the next 5 minutes. |
| 995 | reliable_link_callback_.Reset( |
| 996 | base::Bind(&Device::OnReliableLink, base::Unretained(this))); |
| 997 | dispatcher_->PostDelayedTask( |
| 998 | reliable_link_callback_.callback(), |
| 999 | kLinkUnreliableThresholdSeconds * 1000); |
| 1000 | } |
| 1001 | } |
Christopher Wiley | 5519e9e | 2013-01-08 16:55:56 -0800 | [diff] [blame] | 1002 | |
Paul Stewart | 8596f9f | 2013-03-14 07:58:26 -0700 | [diff] [blame] | 1003 | void Device::OnConnectionUpdated() { |
| 1004 | if (selected_service_) { |
| 1005 | manager_->UpdateService(selected_service_); |
| 1006 | } |
| 1007 | } |
| 1008 | |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 1009 | void Device::CreateConnection() { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1010 | SLOG(this, 2) << __func__; |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 1011 | if (!connection_.get()) { |
mukesh agrawal | 23ac6b7 | 2013-01-31 18:52:37 -0800 | [diff] [blame] | 1012 | connection_ = new Connection(interface_index_, |
| 1013 | link_name_, |
| 1014 | technology_, |
| 1015 | manager_->device_info()); |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | void Device::DestroyConnection() { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1020 | SLOG(this, 2) << __func__ << " on " << link_name_; |
Peter Qiu | 25f1be6 | 2014-08-12 10:42:27 -0700 | [diff] [blame] | 1021 | StopAllActivities(); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 1022 | if (selected_service_.get()) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1023 | SLOG(this, 3) << "Clearing connection of service " |
| 1024 | << selected_service_->unique_name(); |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 1025 | selected_service_->SetConnection(nullptr); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 1026 | } |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 1027 | connection_ = nullptr; |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 1028 | } |
| 1029 | |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 1030 | void Device::SelectService(const ServiceRefPtr &service) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1031 | SLOG(this, 2) << __func__ << ": service " |
| 1032 | << (service ? service->unique_name() : "*reset*") |
| 1033 | << " on " << link_name_; |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1034 | |
| 1035 | if (selected_service_.get() == service.get()) { |
| 1036 | // No change to |selected_service_|. Return early to avoid |
| 1037 | // changing its state. |
| 1038 | return; |
| 1039 | } |
| 1040 | |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 1041 | if (selected_service_.get()) { |
| 1042 | if (selected_service_->state() != Service::kStateFailure) { |
| 1043 | selected_service_->SetState(Service::kStateIdle); |
| 1044 | } |
Paul Stewart | 20b0a09 | 2012-05-22 20:39:57 -0700 | [diff] [blame] | 1045 | // Just in case the Device subclass has not already done so, make |
| 1046 | // sure the previously selected service has its connection removed. |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 1047 | selected_service_->SetConnection(nullptr); |
Peter Qiu | a388fdb | 2015-04-03 10:31:22 -0700 | [diff] [blame] | 1048 | // Reset link status for the previously selected service. |
| 1049 | selected_service_->set_unreliable(false); |
| 1050 | reliable_link_callback_.Cancel(); |
Peter Qiu | 25f1be6 | 2014-08-12 10:42:27 -0700 | [diff] [blame] | 1051 | StopAllActivities(); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 1052 | } |
Peter Qiu | a057203 | 2014-09-26 10:07:37 -0700 | [diff] [blame] | 1053 | |
| 1054 | // Newly selected service (network), previous failures doesn't apply |
| 1055 | // anymore. |
| 1056 | last_link_monitor_failed_time_ = 0; |
| 1057 | |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 1058 | selected_service_ = service; |
Christopher Wiley | 674598d | 2014-12-12 10:21:39 -0800 | [diff] [blame] | 1059 | adaptor_->EmitRpcIdentifierChanged( |
| 1060 | kSelectedServiceProperty, GetSelectedServiceRpcIdentifier(nullptr)); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | void Device::SetServiceState(Service::ConnectState state) { |
| 1064 | if (selected_service_.get()) { |
| 1065 | selected_service_->SetState(state); |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | void Device::SetServiceFailure(Service::ConnectFailure failure_state) { |
| 1070 | if (selected_service_.get()) { |
| 1071 | selected_service_->SetFailure(failure_state); |
| 1072 | } |
| 1073 | } |
| 1074 | |
Eric Shienbrood | cc95c5d | 2012-03-30 15:25:49 -0400 | [diff] [blame] | 1075 | void Device::SetServiceFailureSilent(Service::ConnectFailure failure_state) { |
| 1076 | if (selected_service_.get()) { |
| 1077 | selected_service_->SetFailureSilent(failure_state); |
| 1078 | } |
| 1079 | } |
| 1080 | |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 1081 | bool Device::SetIPFlag(IPAddress::Family family, const string &flag, |
| 1082 | const string &value) { |
| 1083 | string ip_version; |
| 1084 | if (family == IPAddress::kFamilyIPv4) { |
| 1085 | ip_version = kIPFlagVersion4; |
| 1086 | } else if (family == IPAddress::kFamilyIPv6) { |
| 1087 | ip_version = kIPFlagVersion6; |
| 1088 | } else { |
| 1089 | NOTIMPLEMENTED(); |
| 1090 | } |
| 1091 | FilePath flag_file(StringPrintf(kIPFlagTemplate, ip_version.c_str(), |
| 1092 | link_name_.c_str(), flag.c_str())); |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1093 | SLOG(this, 2) << "Writing " << value << " to flag file " |
| 1094 | << flag_file.value(); |
Ben Chan | 6fbf64f | 2014-05-21 18:07:01 -0700 | [diff] [blame] | 1095 | if (base::WriteFile(flag_file, value.c_str(), value.length()) != 1) { |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 1096 | LOG(ERROR) << StringPrintf("IP flag write failed: %s to %s", |
| 1097 | value.c_str(), flag_file.value().c_str()); |
| 1098 | return false; |
| 1099 | } |
| 1100 | return true; |
| 1101 | } |
| 1102 | |
Paul Stewart | c6fbad9 | 2013-11-13 14:50:52 -0800 | [diff] [blame] | 1103 | string Device::PerformTDLSOperation(const string &/* operation */, |
| 1104 | const string &/* peer */, |
| 1105 | Error */* error */) { |
| 1106 | return ""; |
| 1107 | } |
| 1108 | |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 1109 | void Device::ResetByteCounters() { |
| 1110 | manager_->device_info()->GetByteCounts( |
| 1111 | interface_index_, &receive_byte_offset_, &transmit_byte_offset_); |
| 1112 | manager_->UpdateDevice(this); |
| 1113 | } |
| 1114 | |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 1115 | bool Device::RestartPortalDetection() { |
| 1116 | StopPortalDetection(); |
| 1117 | return StartPortalDetection(); |
| 1118 | } |
| 1119 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 1120 | bool Device::RequestPortalDetection() { |
| 1121 | if (!selected_service_) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1122 | SLOG(this, 2) << FriendlyName() |
| 1123 | << ": No selected service, so no need for portal check."; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 1124 | return false; |
| 1125 | } |
| 1126 | |
| 1127 | if (!connection_.get()) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1128 | SLOG(this, 2) << FriendlyName() |
| 1129 | << ": No connection, so no need for portal check."; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 1130 | return false; |
| 1131 | } |
| 1132 | |
| 1133 | if (selected_service_->state() != Service::kStatePortal) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1134 | SLOG(this, 2) << FriendlyName() |
| 1135 | << ": Service is not in portal state. " |
| 1136 | << "No need to start check."; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 1137 | return false; |
| 1138 | } |
| 1139 | |
| 1140 | if (!connection_->is_default()) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1141 | SLOG(this, 2) << FriendlyName() |
| 1142 | << ": Service is not the default connection. " |
| 1143 | << "Don't start check."; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 1144 | return false; |
| 1145 | } |
| 1146 | |
| 1147 | if (portal_detector_.get() && portal_detector_->IsInProgress()) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1148 | SLOG(this, 2) << FriendlyName() |
| 1149 | << ": Portal detection is already running."; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 1150 | return true; |
| 1151 | } |
| 1152 | |
| 1153 | return StartPortalDetection(); |
| 1154 | } |
| 1155 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1156 | bool Device::StartPortalDetection() { |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 1157 | DCHECK(selected_service_); |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 1158 | if (selected_service_->IsPortalDetectionDisabled()) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1159 | SLOG(this, 2) << "Service " << selected_service_->unique_name() |
| 1160 | << ": Portal detection is disabled; " |
| 1161 | << "marking service online."; |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 1162 | SetServiceConnectedState(Service::kStateOnline); |
| 1163 | return false; |
| 1164 | } |
| 1165 | |
| 1166 | if (selected_service_->IsPortalDetectionAuto() && |
| 1167 | !manager_->IsPortalDetectionEnabled(technology())) { |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1168 | // If portal detection is disabled for this technology, immediately set |
| 1169 | // the service state to "Online". |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1170 | SLOG(this, 2) << "Device " << FriendlyName() |
| 1171 | << ": Portal detection is disabled; " |
| 1172 | << "marking service online."; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1173 | SetServiceConnectedState(Service::kStateOnline); |
| 1174 | return false; |
| 1175 | } |
| 1176 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1177 | if (selected_service_->HasProxyConfig()) { |
| 1178 | // Services with HTTP proxy configurations should not be checked by the |
| 1179 | // connection manager, since we don't have the ability to evaluate |
| 1180 | // arbitrary proxy configs and their possible credentials. |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1181 | SLOG(this, 2) << "Device " << FriendlyName() |
| 1182 | << ": Service has proxy config; marking it online."; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1183 | SetServiceConnectedState(Service::kStateOnline); |
| 1184 | return false; |
| 1185 | } |
| 1186 | |
| 1187 | portal_detector_.reset(new PortalDetector(connection_, |
| 1188 | dispatcher_, |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 1189 | portal_detector_callback_)); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1190 | if (!portal_detector_->Start(manager_->GetPortalCheckURL())) { |
| 1191 | LOG(ERROR) << "Device " << FriendlyName() |
| 1192 | << ": Portal detection failed to start: likely bad URL: " |
| 1193 | << manager_->GetPortalCheckURL(); |
| 1194 | SetServiceConnectedState(Service::kStateOnline); |
| 1195 | return false; |
| 1196 | } |
| 1197 | |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1198 | SLOG(this, 2) << "Device " << FriendlyName() |
| 1199 | << ": Portal detection has started."; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1200 | return true; |
| 1201 | } |
| 1202 | |
| 1203 | void Device::StopPortalDetection() { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1204 | SLOG(this, 2) << "Device " << FriendlyName() |
| 1205 | << ": Portal detection stopping."; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1206 | portal_detector_.reset(); |
| 1207 | } |
| 1208 | |
Rebecca Silberstein | 6862b38 | 2014-09-11 08:24:51 -0700 | [diff] [blame] | 1209 | bool Device::StartConnectivityTest() { |
Rebecca Silberstein | f4365a6 | 2014-09-16 11:40:32 -0700 | [diff] [blame] | 1210 | LOG(INFO) << "Device " << FriendlyName() << " starting connectivity test."; |
Rebecca Silberstein | 6862b38 | 2014-09-11 08:24:51 -0700 | [diff] [blame] | 1211 | |
Rebecca Silberstein | f4365a6 | 2014-09-16 11:40:32 -0700 | [diff] [blame] | 1212 | connection_tester_.reset(new ConnectionTester(connection_, |
| 1213 | dispatcher_, |
| 1214 | connection_tester_callback_)); |
| 1215 | connection_tester_->Start(); |
| 1216 | return true; |
Rebecca Silberstein | 6862b38 | 2014-09-11 08:24:51 -0700 | [diff] [blame] | 1217 | } |
| 1218 | |
Paul Stewart | e8303eb | 2014-10-08 22:51:14 -0700 | [diff] [blame] | 1219 | void Device::StopConnectivityTest() { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1220 | SLOG(this, 2) << "Device " << FriendlyName() |
| 1221 | << ": Connectivity test stopping."; |
Paul Stewart | e8303eb | 2014-10-08 22:51:14 -0700 | [diff] [blame] | 1222 | connection_tester_.reset(); |
| 1223 | } |
| 1224 | |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 1225 | void Device::set_link_monitor(LinkMonitor *link_monitor) { |
| 1226 | link_monitor_.reset(link_monitor); |
| 1227 | } |
| 1228 | |
| 1229 | bool Device::StartLinkMonitor() { |
| 1230 | if (!manager_->IsTechnologyLinkMonitorEnabled(technology())) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1231 | SLOG(this, 2) << "Device " << FriendlyName() |
| 1232 | << ": Link Monitoring is disabled."; |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 1233 | return false; |
| 1234 | } |
| 1235 | |
Peter Qiu | d49760e | 2014-09-19 16:13:42 -0700 | [diff] [blame] | 1236 | if (selected_service_ && selected_service_->link_monitor_disabled()) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1237 | SLOG(this, 2) << "Device " << FriendlyName() |
| 1238 | << ": Link Monitoring is disabled for the selected service"; |
Peter Qiu | d49760e | 2014-09-19 16:13:42 -0700 | [diff] [blame] | 1239 | return false; |
| 1240 | } |
| 1241 | |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 1242 | if (!link_monitor()) { |
| 1243 | set_link_monitor( |
| 1244 | new LinkMonitor( |
| 1245 | connection_, dispatcher_, metrics(), manager_->device_info(), |
Peter Qiu | b5d124f | 2014-04-14 12:05:02 -0700 | [diff] [blame] | 1246 | Bind(&Device::OnLinkMonitorFailure, weak_ptr_factory_.GetWeakPtr()), |
| 1247 | Bind(&Device::OnLinkMonitorGatewayChange, |
| 1248 | weak_ptr_factory_.GetWeakPtr()))); |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 1249 | } |
| 1250 | |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1251 | SLOG(this, 2) << "Device " << FriendlyName() |
| 1252 | << ": Link Monitor starting."; |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 1253 | return link_monitor_->Start(); |
| 1254 | } |
| 1255 | |
| 1256 | void Device::StopLinkMonitor() { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1257 | SLOG(this, 2) << "Device " << FriendlyName() |
| 1258 | << ": Link Monitor stopping."; |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 1259 | link_monitor_.reset(); |
| 1260 | } |
| 1261 | |
Peter Qiu | 8e1ad16 | 2014-10-02 15:58:24 -0700 | [diff] [blame] | 1262 | void Device::OnUnreliableLink() { |
Peter Qiu | a388fdb | 2015-04-03 10:31:22 -0700 | [diff] [blame] | 1263 | SLOG(this, 2) << "Device " << FriendlyName() |
| 1264 | << ": Link is unreliable."; |
| 1265 | selected_service_->set_unreliable(true); |
| 1266 | reliable_link_callback_.Cancel(); |
Peter Qiu | 8e1ad16 | 2014-10-02 15:58:24 -0700 | [diff] [blame] | 1267 | metrics_->NotifyUnreliableLinkSignalStrength( |
| 1268 | technology_, selected_service_->strength()); |
| 1269 | } |
| 1270 | |
Peter Qiu | a388fdb | 2015-04-03 10:31:22 -0700 | [diff] [blame] | 1271 | void Device::OnReliableLink() { |
| 1272 | SLOG(this, 2) << "Device " << FriendlyName() |
| 1273 | << ": Link is reliable."; |
| 1274 | selected_service_->set_unreliable(false); |
| 1275 | // TODO(zqiu): report signal strength to UMA. |
| 1276 | } |
| 1277 | |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 1278 | void Device::OnLinkMonitorFailure() { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1279 | SLOG(this, 2) << "Device " << FriendlyName() |
| 1280 | << ": Link Monitor indicates failure."; |
Peter Qiu | a057203 | 2014-09-26 10:07:37 -0700 | [diff] [blame] | 1281 | if (!selected_service_) { |
| 1282 | return; |
| 1283 | } |
| 1284 | |
| 1285 | time_t now; |
| 1286 | time_->GetSecondsBoottime(&now); |
| 1287 | |
Peter Qiu | a388fdb | 2015-04-03 10:31:22 -0700 | [diff] [blame] | 1288 | if (last_link_monitor_failed_time_ != 0 && |
| 1289 | now - last_link_monitor_failed_time_ < kLinkUnreliableThresholdSeconds) { |
Peter Qiu | 8e1ad16 | 2014-10-02 15:58:24 -0700 | [diff] [blame] | 1290 | OnUnreliableLink(); |
Peter Qiu | a057203 | 2014-09-26 10:07:37 -0700 | [diff] [blame] | 1291 | } |
| 1292 | last_link_monitor_failed_time_ = now; |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 1293 | } |
| 1294 | |
Peter Qiu | 9d58193 | 2014-04-14 16:37:37 -0700 | [diff] [blame] | 1295 | void Device::OnLinkMonitorGatewayChange() { |
| 1296 | string gateway_mac = link_monitor()->gateway_mac_address().HexEncode(); |
| 1297 | int connection_id = manager_->CalcConnectionId( |
| 1298 | ipconfig_->properties().gateway, gateway_mac); |
| 1299 | |
| 1300 | CHECK(selected_service_); |
| 1301 | selected_service_->set_connection_id(connection_id); |
| 1302 | |
| 1303 | manager_->ReportServicesOnSameNetwork(connection_id); |
| 1304 | } |
| 1305 | |
Peter Qiu | 6f5618b | 2014-06-05 15:19:01 -0700 | [diff] [blame] | 1306 | bool Device::StartDNSTest( |
| 1307 | const vector<string> &dns_servers, |
| 1308 | bool retry_until_success, |
| 1309 | const Callback<void(const DNSServerTester::Status)> &callback) { |
| 1310 | if (dns_server_tester_.get()) { |
| 1311 | LOG(ERROR) << FriendlyName() << ": " |
| 1312 | << "Failed to start DNS Test: current test still running"; |
| 1313 | return false; |
Peter Qiu | b9256f3 | 2014-05-09 15:27:29 -0700 | [diff] [blame] | 1314 | } |
| 1315 | |
Peter Qiu | 6f5618b | 2014-06-05 15:19:01 -0700 | [diff] [blame] | 1316 | dns_server_tester_.reset(new DNSServerTester(connection_, |
| 1317 | dispatcher_, |
| 1318 | dns_servers, |
| 1319 | retry_until_success, |
| 1320 | callback)); |
| 1321 | dns_server_tester_->Start(); |
| 1322 | return true; |
| 1323 | } |
| 1324 | |
| 1325 | void Device::StopDNSTest() { |
| 1326 | dns_server_tester_.reset(); |
Peter Qiu | b9256f3 | 2014-05-09 15:27:29 -0700 | [diff] [blame] | 1327 | } |
| 1328 | |
Peter Qiu | d670d03 | 2014-06-03 15:04:43 -0700 | [diff] [blame] | 1329 | void Device::FallbackDNSResultCallback(const DNSServerTester::Status status) { |
Peter Qiu | 6f5618b | 2014-06-05 15:19:01 -0700 | [diff] [blame] | 1330 | StopDNSTest(); |
Peter Qiu | f18e771 | 2014-05-20 09:59:46 -0700 | [diff] [blame] | 1331 | int result = Metrics::kFallbackDNSTestResultFailure; |
Peter Qiu | d670d03 | 2014-06-03 15:04:43 -0700 | [diff] [blame] | 1332 | if (status == DNSServerTester::kStatusSuccess) { |
Peter Qiu | f18e771 | 2014-05-20 09:59:46 -0700 | [diff] [blame] | 1333 | result = Metrics::kFallbackDNSTestResultSuccess; |
Peter Qiu | a89154b | 2014-05-23 15:45:42 -0700 | [diff] [blame] | 1334 | |
| 1335 | // Switch to fallback DNS server if service is configured to allow DNS |
| 1336 | // fallback. |
| 1337 | CHECK(selected_service_); |
| 1338 | if (selected_service_->is_dns_auto_fallback_allowed()) { |
Peter Qiu | 6f5618b | 2014-06-05 15:19:01 -0700 | [diff] [blame] | 1339 | LOG(INFO) << "Device " << FriendlyName() |
| 1340 | << ": Switching to fallback DNS servers."; |
| 1341 | // Save the DNS servers from ipconfig. |
| 1342 | config_dns_servers_ = ipconfig_->properties().dns_servers; |
Peter Qiu | a89154b | 2014-05-23 15:45:42 -0700 | [diff] [blame] | 1343 | SwitchDNSServers(vector<string>(std::begin(kFallbackDnsServers), |
| 1344 | std::end(kFallbackDnsServers))); |
Peter Qiu | 6f5618b | 2014-06-05 15:19:01 -0700 | [diff] [blame] | 1345 | // Start DNS test for configured DNS servers. |
| 1346 | StartDNSTest(config_dns_servers_, |
| 1347 | true, |
| 1348 | Bind(&Device::ConfigDNSResultCallback, |
| 1349 | weak_ptr_factory_.GetWeakPtr())); |
Peter Qiu | a89154b | 2014-05-23 15:45:42 -0700 | [diff] [blame] | 1350 | } |
Peter Qiu | b9256f3 | 2014-05-09 15:27:29 -0700 | [diff] [blame] | 1351 | } |
Peter Qiu | f18e771 | 2014-05-20 09:59:46 -0700 | [diff] [blame] | 1352 | metrics()->NotifyFallbackDNSTestResult(technology_, result); |
Peter Qiu | 6f5618b | 2014-06-05 15:19:01 -0700 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | void Device::ConfigDNSResultCallback(const DNSServerTester::Status status) { |
| 1356 | StopDNSTest(); |
| 1357 | // DNS test failed to start due to internal error. |
| 1358 | if (status == DNSServerTester::kStatusFailure) { |
| 1359 | return; |
| 1360 | } |
| 1361 | |
| 1362 | // Switch back to the configured DNS servers. |
| 1363 | LOG(INFO) << "Device " << FriendlyName() |
| 1364 | << ": Switching back to configured DNS servers."; |
| 1365 | SwitchDNSServers(config_dns_servers_); |
Peter Qiu | b9256f3 | 2014-05-09 15:27:29 -0700 | [diff] [blame] | 1366 | } |
| 1367 | |
Peter Qiu | a89154b | 2014-05-23 15:45:42 -0700 | [diff] [blame] | 1368 | void Device::SwitchDNSServers(const vector<string> &dns_servers) { |
| 1369 | CHECK(ipconfig_); |
| 1370 | CHECK(connection_); |
| 1371 | // Push new DNS servers setting to the IP config object. |
| 1372 | ipconfig_->UpdateDNSServers(dns_servers); |
| 1373 | // Push new DNS servers setting to the current connection, so the resolver |
| 1374 | // will be updated to use the new DNS servers. |
| 1375 | connection_->UpdateDNSServers(dns_servers); |
| 1376 | // Allow the service to notify Chrome of ipconfig changes. |
| 1377 | selected_service_->NotifyIPConfigChanges(); |
Peter Qiu | 6f5618b | 2014-06-05 15:19:01 -0700 | [diff] [blame] | 1378 | // Restart the portal detection with the new DNS setting. |
| 1379 | RestartPortalDetection(); |
Peter Qiu | a89154b | 2014-05-23 15:45:42 -0700 | [diff] [blame] | 1380 | } |
| 1381 | |
Peter Qiu | dc335f8 | 2014-05-15 10:33:17 -0700 | [diff] [blame] | 1382 | void Device::set_traffic_monitor(TrafficMonitor *traffic_monitor) { |
| 1383 | traffic_monitor_.reset(traffic_monitor); |
| 1384 | } |
| 1385 | |
Samuel Tan | 787a1ce | 2014-11-11 17:17:27 -0800 | [diff] [blame] | 1386 | bool Device::TimeToNextDHCPLeaseRenewal(uint32_t *result) { |
| 1387 | if (!ipconfig() && !ip6config()) { |
| 1388 | return false; |
| 1389 | } |
| 1390 | uint32_t time_to_ipv4_lease_expiry = UINT32_MAX; |
| 1391 | uint32_t time_to_ipv6_lease_expiry = UINT32_MAX; |
| 1392 | if (ipconfig()) { |
| 1393 | ipconfig()->TimeToLeaseExpiry(&time_to_ipv4_lease_expiry); |
| 1394 | } |
| 1395 | if (ip6config()) { |
| 1396 | ip6config()->TimeToLeaseExpiry(&time_to_ipv6_lease_expiry); |
| 1397 | } |
| 1398 | *result = std::min(time_to_ipv4_lease_expiry, time_to_ipv6_lease_expiry); |
| 1399 | return true; |
| 1400 | } |
| 1401 | |
Peter Qiu | dc335f8 | 2014-05-15 10:33:17 -0700 | [diff] [blame] | 1402 | bool Device::IsTrafficMonitorEnabled() const { |
| 1403 | return false; |
| 1404 | } |
| 1405 | |
| 1406 | void Device::StartTrafficMonitor() { |
| 1407 | // Return if traffic monitor is not enabled for this device. |
| 1408 | if (!IsTrafficMonitorEnabled()) { |
| 1409 | return; |
| 1410 | } |
| 1411 | |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1412 | SLOG(this, 2) << "Device " << FriendlyName() |
| 1413 | << ": Traffic Monitor starting."; |
Peter Qiu | dc335f8 | 2014-05-15 10:33:17 -0700 | [diff] [blame] | 1414 | if (!traffic_monitor_.get()) { |
| 1415 | traffic_monitor_.reset(new TrafficMonitor(this, dispatcher_)); |
| 1416 | traffic_monitor_->set_network_problem_detected_callback( |
| 1417 | Bind(&Device::OnEncounterNetworkProblem, |
| 1418 | weak_ptr_factory_.GetWeakPtr())); |
| 1419 | } |
| 1420 | traffic_monitor_->Start(); |
| 1421 | } |
| 1422 | |
| 1423 | void Device::StopTrafficMonitor() { |
| 1424 | // Return if traffic monitor is not enabled for this device. |
| 1425 | if (!IsTrafficMonitorEnabled()) { |
| 1426 | return; |
| 1427 | } |
| 1428 | |
| 1429 | if (traffic_monitor_.get()) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1430 | SLOG(this, 2) << "Device " << FriendlyName() |
| 1431 | << ": Traffic Monitor stopping."; |
Peter Qiu | dc335f8 | 2014-05-15 10:33:17 -0700 | [diff] [blame] | 1432 | traffic_monitor_->Stop(); |
| 1433 | } |
| 1434 | traffic_monitor_.reset(); |
| 1435 | } |
| 1436 | |
| 1437 | void Device::OnEncounterNetworkProblem(int reason) { |
| 1438 | int metric_code; |
| 1439 | switch (reason) { |
| 1440 | case TrafficMonitor::kNetworkProblemCongestedTxQueue: |
| 1441 | metric_code = Metrics::kNetworkProblemCongestedTCPTxQueue; |
| 1442 | break; |
| 1443 | case TrafficMonitor::kNetworkProblemDNSFailure: |
| 1444 | metric_code = Metrics::kNetworkProblemDNSFailure; |
| 1445 | break; |
| 1446 | default: |
| 1447 | LOG(ERROR) << "Invalid network problem code: " << reason; |
| 1448 | return; |
| 1449 | } |
| 1450 | |
| 1451 | metrics()->NotifyNetworkProblemDetected(technology_, metric_code); |
| 1452 | // Stop the traffic monitor, only report the first network problem detected |
| 1453 | // on the connection for now. |
| 1454 | StopTrafficMonitor(); |
| 1455 | } |
| 1456 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1457 | void Device::SetServiceConnectedState(Service::ConnectState state) { |
| 1458 | DCHECK(selected_service_.get()); |
| 1459 | |
| 1460 | if (!selected_service_.get()) { |
| 1461 | LOG(ERROR) << FriendlyName() << ": " |
| 1462 | << "Portal detection completed but no selected service exists!"; |
| 1463 | return; |
| 1464 | } |
| 1465 | |
| 1466 | if (!selected_service_->IsConnected()) { |
| 1467 | LOG(ERROR) << FriendlyName() << ": " |
| 1468 | << "Portal detection completed but selected service " |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 1469 | << selected_service_->unique_name() |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1470 | << " is in non-connected state."; |
| 1471 | return; |
| 1472 | } |
| 1473 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 1474 | if (state == Service::kStatePortal && connection_->is_default() && |
| 1475 | manager_->GetPortalCheckInterval() != 0) { |
| 1476 | CHECK(portal_detector_.get()); |
| 1477 | if (!portal_detector_->StartAfterDelay( |
| 1478 | manager_->GetPortalCheckURL(), |
| 1479 | manager_->GetPortalCheckInterval())) { |
| 1480 | LOG(ERROR) << "Device " << FriendlyName() |
| 1481 | << ": Portal detection failed to restart: likely bad URL: " |
| 1482 | << manager_->GetPortalCheckURL(); |
| 1483 | SetServiceState(Service::kStateOnline); |
| 1484 | portal_detector_.reset(); |
| 1485 | return; |
| 1486 | } |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1487 | SLOG(this, 2) << "Device " << FriendlyName() |
| 1488 | << ": Portal detection retrying."; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 1489 | } else { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1490 | SLOG(this, 2) << "Device " << FriendlyName() |
| 1491 | << ": Portal will not retry."; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 1492 | portal_detector_.reset(); |
| 1493 | } |
| 1494 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1495 | SetServiceState(state); |
| 1496 | } |
| 1497 | |
| 1498 | void Device::PortalDetectorCallback(const PortalDetector::Result &result) { |
| 1499 | if (!result.final) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1500 | SLOG(this, 2) << "Device " << FriendlyName() |
| 1501 | << ": Received non-final status: " |
| 1502 | << ConnectivityTrial::StatusToString( |
| 1503 | result.trial_result.status); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1504 | return; |
| 1505 | } |
| 1506 | |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1507 | SLOG(this, 2) << "Device " << FriendlyName() |
| 1508 | << ": Received final status: " |
| 1509 | << ConnectivityTrial::StatusToString( |
| 1510 | result.trial_result.status); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1511 | |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 1512 | portal_attempts_to_online_ += result.num_attempts; |
| 1513 | |
Peter Qiu | b9256f3 | 2014-05-09 15:27:29 -0700 | [diff] [blame] | 1514 | int portal_status = Metrics::PortalDetectionResultToEnum(result); |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 1515 | metrics()->SendEnumToUMA( |
mukesh agrawal | 132e96f | 2014-04-24 11:49:42 -0700 | [diff] [blame] | 1516 | metrics()->GetFullMetricName(Metrics::kMetricPortalResultSuffix, |
| 1517 | technology()), |
Peter Qiu | b9256f3 | 2014-05-09 15:27:29 -0700 | [diff] [blame] | 1518 | portal_status, |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 1519 | Metrics::kPortalResultMax); |
| 1520 | |
Rebecca Silberstein | 3d49ea4 | 2014-08-21 11:20:50 -0700 | [diff] [blame] | 1521 | if (result.trial_result.status == ConnectivityTrial::kStatusSuccess) { |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1522 | SetServiceConnectedState(Service::kStateOnline); |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 1523 | |
| 1524 | metrics()->SendToUMA( |
| 1525 | metrics()->GetFullMetricName( |
mukesh agrawal | 132e96f | 2014-04-24 11:49:42 -0700 | [diff] [blame] | 1526 | Metrics::kMetricPortalAttemptsToOnlineSuffix, technology()), |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 1527 | portal_attempts_to_online_, |
| 1528 | Metrics::kMetricPortalAttemptsToOnlineMin, |
| 1529 | Metrics::kMetricPortalAttemptsToOnlineMax, |
| 1530 | Metrics::kMetricPortalAttemptsToOnlineNumBuckets); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1531 | } else { |
Peter Qiu | 9b83c89 | 2014-08-09 23:06:02 -0700 | [diff] [blame] | 1532 | // Set failure phase and status. |
| 1533 | if (selected_service_.get()) { |
| 1534 | selected_service_->SetPortalDetectionFailure( |
Rebecca Silberstein | 3d49ea4 | 2014-08-21 11:20:50 -0700 | [diff] [blame] | 1535 | ConnectivityTrial::PhaseToString(result.trial_result.phase), |
| 1536 | ConnectivityTrial::StatusToString(result.trial_result.status)); |
Peter Qiu | 9b83c89 | 2014-08-09 23:06:02 -0700 | [diff] [blame] | 1537 | } |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1538 | SetServiceConnectedState(Service::kStatePortal); |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 1539 | |
| 1540 | metrics()->SendToUMA( |
| 1541 | metrics()->GetFullMetricName( |
mukesh agrawal | 132e96f | 2014-04-24 11:49:42 -0700 | [diff] [blame] | 1542 | Metrics::kMetricPortalAttemptsSuffix, technology()), |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 1543 | result.num_attempts, |
| 1544 | Metrics::kMetricPortalAttemptsMin, |
| 1545 | Metrics::kMetricPortalAttemptsMax, |
| 1546 | Metrics::kMetricPortalAttemptsNumBuckets); |
Peter Qiu | b9256f3 | 2014-05-09 15:27:29 -0700 | [diff] [blame] | 1547 | |
Peter Qiu | b25083f | 2014-08-25 13:22:31 -0700 | [diff] [blame] | 1548 | // TODO(zqiu): Only support fallback DNS server for IPv4 for now. |
| 1549 | if (connection_->IsIPv6()) { |
| 1550 | return; |
| 1551 | } |
| 1552 | |
Peter Qiu | b9256f3 | 2014-05-09 15:27:29 -0700 | [diff] [blame] | 1553 | // Perform fallback DNS test if the portal failure is DNS related. |
| 1554 | // The test will send a DNS request to Google's DNS server to determine |
| 1555 | // if the DNS failure is due to bad DNS server settings. |
| 1556 | if ((portal_status == Metrics::kPortalResultDNSFailure) || |
| 1557 | (portal_status == Metrics::kPortalResultDNSTimeout)) { |
Peter Qiu | 6f5618b | 2014-06-05 15:19:01 -0700 | [diff] [blame] | 1558 | StartDNSTest(vector<string>(std::begin(kFallbackDnsServers), |
| 1559 | std::end(kFallbackDnsServers)), |
| 1560 | false, |
| 1561 | Bind(&Device::FallbackDNSResultCallback, |
| 1562 | weak_ptr_factory_.GetWeakPtr())); |
Peter Qiu | b9256f3 | 2014-05-09 15:27:29 -0700 | [diff] [blame] | 1563 | } |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1564 | } |
| 1565 | } |
| 1566 | |
Christopher Wiley | 674598d | 2014-12-12 10:21:39 -0800 | [diff] [blame] | 1567 | string Device::GetSelectedServiceRpcIdentifier(Error */*error*/) { |
| 1568 | if (!selected_service_) { |
| 1569 | return "/"; |
| 1570 | } |
| 1571 | return selected_service_->GetRpcIdentifier(); |
| 1572 | } |
| 1573 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 1574 | vector<string> Device::AvailableIPConfigs(Error */*error*/) { |
Paul Stewart | d4f2648 | 2014-04-25 19:12:03 -0700 | [diff] [blame] | 1575 | vector<string> ipconfigs; |
| 1576 | if (ipconfig_) { |
| 1577 | ipconfigs.push_back(ipconfig_->GetRpcIdentifier()); |
Jason Glasgow | 08afdff | 2012-04-03 10:22:26 -0400 | [diff] [blame] | 1578 | } |
Paul Stewart | d4f2648 | 2014-04-25 19:12:03 -0700 | [diff] [blame] | 1579 | if (ip6config_) { |
| 1580 | ipconfigs.push_back(ip6config_->GetRpcIdentifier()); |
| 1581 | } |
| 1582 | return ipconfigs; |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 1583 | } |
| 1584 | |
| 1585 | string Device::GetRpcConnectionIdentifier() { |
| 1586 | return adaptor_->GetRpcConnectionIdentifier(); |
| 1587 | } |
| 1588 | |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 1589 | uint64_t Device::GetLinkMonitorResponseTime(Error *error) { |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 1590 | if (!link_monitor_.get()) { |
| 1591 | // It is not strictly an error that the link monitor does not |
| 1592 | // exist, but returning an error here allows the GetProperties |
| 1593 | // call in our Adaptor to omit this parameter. |
| 1594 | error->Populate(Error::kNotFound, "Device is not running LinkMonitor"); |
| 1595 | return 0; |
| 1596 | } |
| 1597 | return link_monitor_->GetResponseTimeMilliseconds(); |
| 1598 | } |
| 1599 | |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 1600 | uint64_t Device::GetReceiveByteCount() { |
| 1601 | uint64_t rx_byte_count = 0, tx_byte_count = 0; |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 1602 | manager_->device_info()->GetByteCounts( |
| 1603 | interface_index_, &rx_byte_count, &tx_byte_count); |
| 1604 | return rx_byte_count - receive_byte_offset_; |
| 1605 | } |
| 1606 | |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 1607 | uint64_t Device::GetTransmitByteCount() { |
| 1608 | uint64_t rx_byte_count = 0, tx_byte_count = 0; |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 1609 | manager_->device_info()->GetByteCounts( |
| 1610 | interface_index_, &rx_byte_count, &tx_byte_count); |
| 1611 | return tx_byte_count - transmit_byte_offset_; |
| 1612 | } |
| 1613 | |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 1614 | uint64_t Device::GetReceiveByteCountProperty(Error */*error*/) { |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 1615 | return GetReceiveByteCount(); |
| 1616 | } |
| 1617 | |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 1618 | uint64_t Device::GetTransmitByteCountProperty(Error */*error*/) { |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 1619 | return GetTransmitByteCount(); |
| 1620 | } |
| 1621 | |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 1622 | bool Device::IsUnderlyingDeviceEnabled() const { |
| 1623 | return false; |
| 1624 | } |
| 1625 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1626 | // callback |
| 1627 | void Device::OnEnabledStateChanged(const ResultCallback &callback, |
| 1628 | const Error &error) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1629 | SLOG(this, 2) << __func__ |
| 1630 | << " (target: " << enabled_pending_ << "," |
| 1631 | << " success: " << error.IsSuccess() << ")" |
| 1632 | << " on " << link_name_; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1633 | if (error.IsSuccess()) { |
| 1634 | enabled_ = enabled_pending_; |
| 1635 | manager_->UpdateEnabledTechnologies(); |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 1636 | adaptor_->EmitBoolChanged(kPoweredProperty, enabled_); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1637 | } |
Gary Morain | baeefdf | 2012-04-30 14:53:35 -0700 | [diff] [blame] | 1638 | enabled_pending_ = enabled_; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1639 | if (!callback.is_null()) |
| 1640 | callback.Run(error); |
| 1641 | } |
| 1642 | |
| 1643 | void Device::SetEnabled(bool enable) { |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1644 | SLOG(this, 2) << __func__ << "(" << enable << ")"; |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 1645 | Error error; |
mukesh agrawal | 2818551 | 2013-10-18 16:57:09 -0700 | [diff] [blame] | 1646 | SetEnabledChecked(enable, false, &error, ResultCallback()); |
Arman Uguray | 8bf6a5c | 2013-08-12 14:18:03 -0700 | [diff] [blame] | 1647 | |
| 1648 | // SetEnabledInternal might fail here if there is an unfinished enable or |
| 1649 | // disable operation. Don't log error in this case, as this method is only |
| 1650 | // called when the underlying device is already in the target state and the |
| 1651 | // pending operation should eventually bring the device to the expected |
| 1652 | // state. |
| 1653 | LOG_IF(ERROR, |
| 1654 | error.IsFailure() && |
| 1655 | !error.IsOngoing() && |
| 1656 | error.type() != Error::kInProgress) |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 1657 | << "Enabled failed, but no way to report the failure."; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1658 | } |
| 1659 | |
Ben Chan | 9f3dcf8 | 2013-09-25 18:04:58 -0700 | [diff] [blame] | 1660 | void Device::SetEnabledNonPersistent(bool enable, |
| 1661 | Error *error, |
| 1662 | const ResultCallback &callback) { |
mukesh agrawal | 2818551 | 2013-10-18 16:57:09 -0700 | [diff] [blame] | 1663 | SetEnabledChecked(enable, false, error, callback); |
Ben Chan | 9f3dcf8 | 2013-09-25 18:04:58 -0700 | [diff] [blame] | 1664 | } |
| 1665 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1666 | void Device::SetEnabledPersistent(bool enable, |
| 1667 | Error *error, |
| 1668 | const ResultCallback &callback) { |
mukesh agrawal | 2818551 | 2013-10-18 16:57:09 -0700 | [diff] [blame] | 1669 | SetEnabledChecked(enable, true, error, callback); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1670 | } |
| 1671 | |
mukesh agrawal | 2818551 | 2013-10-18 16:57:09 -0700 | [diff] [blame] | 1672 | void Device::SetEnabledChecked(bool enable, |
| 1673 | bool persist, |
| 1674 | Error *error, |
| 1675 | const ResultCallback &callback) { |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 1676 | DCHECK(error); |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1677 | SLOG(this, 2) << "Device " << link_name_ << " " |
| 1678 | << (enable ? "starting" : "stopping"); |
Paul Stewart | ff6be29 | 2014-11-24 17:05:03 -0800 | [diff] [blame] | 1679 | if (enable && manager_->IsTechnologyProhibited(technology())) { |
| 1680 | error->Populate(Error::kPermissionDenied, "The " + |
| 1681 | Technology::NameFromIdentifier(technology()) + |
| 1682 | " technology is prohibited"); |
| 1683 | return; |
| 1684 | } |
| 1685 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1686 | if (enable == enabled_) { |
Arman Uguray | 2f352e6 | 2013-08-28 19:12:53 -0700 | [diff] [blame] | 1687 | if (enable != enabled_pending_ && persist) { |
| 1688 | // Return an error, as there is an ongoing operation to achieve the |
| 1689 | // opposite. |
| 1690 | Error::PopulateAndLog( |
Paul Stewart | 34f424e | 2015-01-16 15:30:20 -0800 | [diff] [blame] | 1691 | FROM_HERE, error, Error::kOperationFailed, |
Arman Uguray | 2f352e6 | 2013-08-28 19:12:53 -0700 | [diff] [blame] | 1692 | enable ? "Cannot enable while the device is disabling." : |
| 1693 | "Cannot disable while the device is enabling."); |
| 1694 | return; |
| 1695 | } |
mukesh agrawal | 2818551 | 2013-10-18 16:57:09 -0700 | [diff] [blame] | 1696 | LOG(INFO) << "Already in desired enable state."; |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 1697 | error->Reset(); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1698 | return; |
| 1699 | } |
| 1700 | |
| 1701 | if (enabled_pending_ == enable) { |
Paul Stewart | 34f424e | 2015-01-16 15:30:20 -0800 | [diff] [blame] | 1702 | Error::PopulateAndLog(FROM_HERE, error, Error::kInProgress, |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 1703 | "Enable operation already in progress"); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1704 | return; |
| 1705 | } |
| 1706 | |
| 1707 | if (persist) { |
| 1708 | enabled_persistent_ = enable; |
Darin Petkov | e7c6ad3 | 2012-06-29 10:22:09 +0200 | [diff] [blame] | 1709 | manager_->UpdateDevice(this); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1710 | } |
| 1711 | |
mukesh agrawal | 2818551 | 2013-10-18 16:57:09 -0700 | [diff] [blame] | 1712 | SetEnabledUnchecked(enable, error, callback); |
| 1713 | } |
| 1714 | |
| 1715 | void Device::SetEnabledUnchecked(bool enable, Error *error, |
| 1716 | const ResultCallback &on_enable_complete) { |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1717 | enabled_pending_ = enable; |
mukesh agrawal | 2818551 | 2013-10-18 16:57:09 -0700 | [diff] [blame] | 1718 | EnabledStateChangedCallback chained_callback = |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1719 | Bind(&Device::OnEnabledStateChanged, |
mukesh agrawal | 2818551 | 2013-10-18 16:57:09 -0700 | [diff] [blame] | 1720 | weak_ptr_factory_.GetWeakPtr(), on_enable_complete); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1721 | if (enable) { |
| 1722 | running_ = true; |
mukesh agrawal | 2818551 | 2013-10-18 16:57:09 -0700 | [diff] [blame] | 1723 | Start(error, chained_callback); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1724 | } else { |
| 1725 | running_ = false; |
| 1726 | DestroyIPConfig(); // breaks a reference cycle |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 1727 | SelectService(nullptr); // breaks a reference cycle |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1728 | rtnl_handler_->SetInterfaceFlags(interface_index(), 0, IFF_UP); |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 1729 | SLOG(this, 3) << "Device " << link_name_ << " ipconfig_ " |
| 1730 | << (ipconfig_ ? "is set." : "is not set."); |
| 1731 | SLOG(this, 3) << "Device " << link_name_ << " ip6config_ " |
| 1732 | << (ip6config_ ? "is set." : "is not set."); |
| 1733 | SLOG(this, 3) << "Device " << link_name_ << " connection_ " |
| 1734 | << (connection_ ? "is set." : "is not set."); |
| 1735 | SLOG(this, 3) << "Device " << link_name_ << " selected_service_ " |
| 1736 | << (selected_service_ ? "is set." : "is not set."); |
mukesh agrawal | 2818551 | 2013-10-18 16:57:09 -0700 | [diff] [blame] | 1737 | Stop(error, chained_callback); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1738 | } |
| 1739 | } |
| 1740 | |
Paul Stewart | d4f2648 | 2014-04-25 19:12:03 -0700 | [diff] [blame] | 1741 | void Device::UpdateIPConfigsProperty() { |
| 1742 | adaptor_->EmitRpcIdentifierArrayChanged( |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 1743 | kIPConfigsProperty, AvailableIPConfigs(nullptr)); |
Paul Stewart | d4f2648 | 2014-04-25 19:12:03 -0700 | [diff] [blame] | 1744 | } |
| 1745 | |
Peter Qiu | 62abf31 | 2015-05-05 12:58:05 -0700 | [diff] [blame] | 1746 | bool Device::ResolvePeerMacAddress(const string &input, |
| 1747 | string *output, |
| 1748 | Error *error) { |
| 1749 | if (!MakeHardwareAddressFromString(input).empty()) { |
| 1750 | // Input is already a MAC address. |
| 1751 | *output = input; |
| 1752 | return true; |
| 1753 | } |
| 1754 | |
| 1755 | IPAddress ip_address(IPAddress::kFamilyIPv4); |
| 1756 | if (!ip_address.SetAddressFromString(input)) { |
| 1757 | Error::PopulateAndLog(FROM_HERE, error, Error::kInvalidArguments, |
| 1758 | "Peer is neither an IP Address nor a MAC address"); |
| 1759 | return false; |
| 1760 | } |
| 1761 | |
| 1762 | // Peer address was specified as an IP address which we need to resolve. |
| 1763 | const DeviceInfo *device_info = manager()->device_info(); |
| 1764 | if (!device_info->HasDirectConnectivityTo(interface_index_, ip_address)) { |
| 1765 | Error::PopulateAndLog(FROM_HERE, error, Error::kInvalidArguments, |
| 1766 | "IP address is not local to this interface"); |
| 1767 | return false; |
| 1768 | } |
| 1769 | |
| 1770 | ByteString mac_address; |
| 1771 | if (device_info->GetMACAddressOfPeer(interface_index_, |
| 1772 | ip_address, |
| 1773 | &mac_address)) { |
| 1774 | *output = MakeStringFromHardwareAddress( |
| 1775 | vector<uint8_t>(mac_address.GetConstData(), |
| 1776 | mac_address.GetConstData() + |
| 1777 | mac_address.GetLength())); |
| 1778 | SLOG(this, 2) << "ARP cache lookup returned peer: " << *output; |
| 1779 | return true; |
| 1780 | } |
| 1781 | |
| 1782 | if (!Icmp().TransmitEchoRequest(ip_address)) { |
| 1783 | Error::PopulateAndLog(FROM_HERE, error, Error::kOperationFailed, |
| 1784 | "Failed to send ICMP request to peer to setup ARP"); |
| 1785 | } else { |
| 1786 | // ARP request was transmitted successfully, address resolution is still |
| 1787 | // pending. |
| 1788 | error->Populate(Error::kInProgress, |
| 1789 | "Peer MAC address was not found in the ARP cache, " |
| 1790 | "but an ARP request was sent to find it. " |
| 1791 | "Please try again."); |
| 1792 | } |
| 1793 | return false; |
| 1794 | } |
| 1795 | |
| 1796 | // static |
| 1797 | vector<uint8_t> Device::MakeHardwareAddressFromString( |
| 1798 | const string &address_string) { |
| 1799 | string address_nosep; |
| 1800 | base::RemoveChars(address_string, ":", &address_nosep); |
| 1801 | vector<uint8_t> address_bytes; |
| 1802 | base::HexStringToBytes(address_nosep, &address_bytes); |
| 1803 | if (address_bytes.size() != kHardwareAddressLength) { |
| 1804 | return vector<uint8_t>(); |
| 1805 | } |
| 1806 | return address_bytes; |
| 1807 | } |
| 1808 | |
| 1809 | // static |
| 1810 | string Device::MakeStringFromHardwareAddress( |
| 1811 | const vector<uint8_t> &address_bytes) { |
| 1812 | CHECK_EQ(kHardwareAddressLength, address_bytes.size()); |
| 1813 | return StringPrintf("%02x:%02x:%02x:%02x:%02x:%02x", |
| 1814 | address_bytes[0], address_bytes[1], address_bytes[2], |
| 1815 | address_bytes[3], address_bytes[4], address_bytes[5]); |
| 1816 | } |
| 1817 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 1818 | } // namespace shill |