Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "shill/portal_detector.h" |
| 6 | |
| 7 | #include <string> |
| 8 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 9 | #include <base/bind.h> |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 10 | #include <base/string_number_conversions.h> |
| 11 | #include <base/string_util.h> |
| 12 | #include <base/stringprintf.h> |
| 13 | |
| 14 | #include "shill/async_connection.h" |
| 15 | #include "shill/connection.h" |
| 16 | #include "shill/dns_client.h" |
| 17 | #include "shill/event_dispatcher.h" |
| 18 | #include "shill/http_url.h" |
| 19 | #include "shill/ip_address.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 20 | #include "shill/logging.h" |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 21 | #include "shill/sockets.h" |
| 22 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 23 | using base::Bind; |
| 24 | using base::Callback; |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 25 | using base::StringPrintf; |
| 26 | using std::string; |
| 27 | |
| 28 | namespace shill { |
| 29 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 30 | const int PortalDetector::kDefaultCheckIntervalSeconds = 30; |
Paul Stewart | f555cf8 | 2012-03-15 14:42:43 -0700 | [diff] [blame] | 31 | const char PortalDetector::kDefaultCheckPortalList[] = "ethernet,wifi,cellular"; |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 32 | const char PortalDetector::kDefaultURL[] = |
| 33 | "http://clients3.google.com/generate_204"; |
Paul Stewart | 6051999 | 2012-09-10 17:45:01 -0700 | [diff] [blame] | 34 | const char PortalDetector::kResponseExpected[] = "HTTP/?.? 204"; |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 35 | |
| 36 | const int PortalDetector::kMaxRequestAttempts = 3; |
| 37 | const int PortalDetector::kMinTimeBetweenAttemptsSeconds = 3; |
| 38 | const int PortalDetector::kRequestTimeoutSeconds = 10; |
Christopher Wiley | 6e1dc0f | 2012-10-17 15:38:56 -0700 | [diff] [blame] | 39 | const int PortalDetector::kMaxFailuresInContentPhase = 2; |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 40 | |
| 41 | const char PortalDetector::kPhaseConnectionString[] = "Connection"; |
| 42 | const char PortalDetector::kPhaseDNSString[] = "DNS"; |
| 43 | const char PortalDetector::kPhaseHTTPString[] = "HTTP"; |
| 44 | const char PortalDetector::kPhaseContentString[] = "Content"; |
| 45 | const char PortalDetector::kPhaseUnknownString[] = "Unknown"; |
| 46 | |
| 47 | const char PortalDetector::kStatusFailureString[] = "Failure"; |
| 48 | const char PortalDetector::kStatusSuccessString[] = "Success"; |
| 49 | const char PortalDetector::kStatusTimeoutString[] = "Timeout"; |
| 50 | |
| 51 | PortalDetector::PortalDetector( |
| 52 | ConnectionRefPtr connection, |
| 53 | EventDispatcher *dispatcher, |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 54 | const Callback<void(const Result &)> &callback) |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 55 | : attempt_count_(0), |
Darin Petkov | e636c69 | 2012-05-31 10:22:17 +0200 | [diff] [blame] | 56 | attempt_start_time_((struct timeval){0}), |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 57 | connection_(connection), |
| 58 | dispatcher_(dispatcher), |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 59 | weak_ptr_factory_(this), |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 60 | portal_result_callback_(callback), |
| 61 | request_read_callback_( |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 62 | Bind(&PortalDetector::RequestReadCallback, |
| 63 | weak_ptr_factory_.GetWeakPtr())), |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 64 | request_result_callback_( |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 65 | Bind(&PortalDetector::RequestResultCallback, |
| 66 | weak_ptr_factory_.GetWeakPtr())), |
Christopher Wiley | 6e1dc0f | 2012-10-17 15:38:56 -0700 | [diff] [blame] | 67 | time_(Time::GetInstance()), |
| 68 | failures_in_content_phase_(0) { } |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 69 | |
| 70 | PortalDetector::~PortalDetector() { |
| 71 | Stop(); |
| 72 | } |
| 73 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 74 | bool PortalDetector::Start(const string &url_string) { |
| 75 | return StartAfterDelay(url_string, 0); |
| 76 | } |
| 77 | |
| 78 | bool PortalDetector::StartAfterDelay(const string &url_string, |
| 79 | int delay_seconds) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 80 | SLOG(Portal, 3) << "In " << __func__; |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 81 | |
| 82 | DCHECK(!request_.get()); |
| 83 | |
| 84 | if (!url_.ParseFromString(url_string)) { |
| 85 | LOG(ERROR) << "Failed to parse URL string: " << url_string; |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | request_.reset(new HTTPRequest(connection_, dispatcher_, &sockets_)); |
| 90 | attempt_count_ = 0; |
Christopher Wiley | 6e1dc0f | 2012-10-17 15:38:56 -0700 | [diff] [blame] | 91 | // If we're starting a new set of attempts, discard past failure history. |
| 92 | failures_in_content_phase_ = 0; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 93 | StartAttempt(delay_seconds); |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 94 | return true; |
| 95 | } |
| 96 | |
| 97 | void PortalDetector::Stop() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 98 | SLOG(Portal, 3) << "In " << __func__; |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 99 | |
| 100 | if (!request_.get()) { |
| 101 | return; |
| 102 | } |
| 103 | |
Paul Stewart | f582b50 | 2012-04-04 21:39:22 -0700 | [diff] [blame] | 104 | start_attempt_.Cancel(); |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 105 | StopAttempt(); |
| 106 | attempt_count_ = 0; |
Christopher Wiley | 6e1dc0f | 2012-10-17 15:38:56 -0700 | [diff] [blame] | 107 | failures_in_content_phase_ = 0; |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 108 | request_.reset(); |
| 109 | } |
| 110 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 111 | bool PortalDetector::IsInProgress() { |
| 112 | return attempt_count_ != 0; |
| 113 | } |
| 114 | |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 115 | // static |
| 116 | const string PortalDetector::PhaseToString(Phase phase) { |
| 117 | switch (phase) { |
| 118 | case kPhaseConnection: |
| 119 | return kPhaseConnectionString; |
| 120 | case kPhaseDNS: |
| 121 | return kPhaseDNSString; |
| 122 | case kPhaseHTTP: |
| 123 | return kPhaseHTTPString; |
| 124 | case kPhaseContent: |
| 125 | return kPhaseContentString; |
| 126 | case kPhaseUnknown: |
| 127 | default: |
| 128 | return kPhaseUnknownString; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // static |
| 133 | const string PortalDetector::StatusToString(Status status) { |
| 134 | switch (status) { |
| 135 | case kStatusSuccess: |
| 136 | return kStatusSuccessString; |
| 137 | case kStatusTimeout: |
| 138 | return kStatusTimeoutString; |
| 139 | case kStatusFailure: |
| 140 | default: |
| 141 | return kStatusFailureString; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | void PortalDetector::CompleteAttempt(Result result) { |
| 146 | LOG(INFO) << StringPrintf("Portal detection completed attempt %d with " |
Christopher Wiley | 6e1dc0f | 2012-10-17 15:38:56 -0700 | [diff] [blame] | 147 | "phase==%s, status==%s, failures in content==%d", |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 148 | attempt_count_, |
| 149 | PhaseToString(result.phase).c_str(), |
Christopher Wiley | 6e1dc0f | 2012-10-17 15:38:56 -0700 | [diff] [blame] | 150 | StatusToString(result.status).c_str(), |
| 151 | failures_in_content_phase_); |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 152 | StopAttempt(); |
Christopher Wiley | 6e1dc0f | 2012-10-17 15:38:56 -0700 | [diff] [blame] | 153 | |
| 154 | if (result.status == kStatusFailure && |
| 155 | result.phase == kPhaseContent) { |
| 156 | failures_in_content_phase_++; |
| 157 | } |
| 158 | if (result.status == kStatusSuccess || |
| 159 | attempt_count_ >= kMaxRequestAttempts || |
| 160 | failures_in_content_phase_ >= kMaxFailuresInContentPhase) { |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 161 | result.num_attempts = attempt_count_; |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 162 | result.final = true; |
| 163 | Stop(); |
Christopher Wiley | 6e1dc0f | 2012-10-17 15:38:56 -0700 | [diff] [blame] | 164 | } else { |
| 165 | StartAttempt(0); |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 168 | portal_result_callback_.Run(result); |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | PortalDetector::Result PortalDetector::GetPortalResultForRequestResult( |
| 172 | HTTPRequest::Result result) { |
| 173 | switch (result) { |
| 174 | case HTTPRequest::kResultSuccess: |
| 175 | // The request completed without receiving the expected payload. |
| 176 | return Result(kPhaseContent, kStatusFailure); |
| 177 | case HTTPRequest::kResultDNSFailure: |
| 178 | return Result(kPhaseDNS, kStatusFailure); |
| 179 | case HTTPRequest::kResultDNSTimeout: |
| 180 | return Result(kPhaseDNS, kStatusTimeout); |
| 181 | case HTTPRequest::kResultConnectionFailure: |
| 182 | return Result(kPhaseConnection, kStatusFailure); |
| 183 | case HTTPRequest::kResultConnectionTimeout: |
| 184 | return Result(kPhaseConnection, kStatusTimeout); |
| 185 | case HTTPRequest::kResultRequestFailure: |
| 186 | case HTTPRequest::kResultResponseFailure: |
| 187 | return Result(kPhaseHTTP, kStatusFailure); |
| 188 | case HTTPRequest::kResultRequestTimeout: |
| 189 | case HTTPRequest::kResultResponseTimeout: |
| 190 | return Result(kPhaseHTTP, kStatusTimeout); |
| 191 | case HTTPRequest::kResultUnknown: |
| 192 | default: |
| 193 | return Result(kPhaseUnknown, kStatusFailure); |
| 194 | } |
| 195 | } |
| 196 | |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 197 | void PortalDetector::RequestReadCallback(const ByteString &response_data) { |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 198 | const string response_expected(kResponseExpected); |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 199 | bool expected_length_received = false; |
| 200 | int compare_length = 0; |
| 201 | if (response_data.GetLength() < response_expected.length()) { |
| 202 | // There isn't enough data yet for a final decision, but we can still |
| 203 | // test to see if the partial string matches so far. |
| 204 | expected_length_received = false; |
| 205 | compare_length = response_data.GetLength(); |
| 206 | } else { |
| 207 | expected_length_received = true; |
| 208 | compare_length = response_expected.length(); |
| 209 | } |
| 210 | |
Paul Stewart | 6051999 | 2012-09-10 17:45:01 -0700 | [diff] [blame] | 211 | if (MatchPattern(string(reinterpret_cast<const char *>( |
| 212 | response_data.GetConstData()), |
| 213 | compare_length), |
| 214 | response_expected.substr(0, compare_length))) { |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 215 | if (expected_length_received) { |
| 216 | CompleteAttempt(Result(kPhaseContent, kStatusSuccess)); |
| 217 | } |
| 218 | // Otherwise, we wait for more data from the server. |
| 219 | } else { |
| 220 | CompleteAttempt(Result(kPhaseContent, kStatusFailure)); |
| 221 | } |
| 222 | } |
| 223 | |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 224 | void PortalDetector::RequestResultCallback( |
| 225 | HTTPRequest::Result result, const ByteString &/*response_data*/) { |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 226 | CompleteAttempt(GetPortalResultForRequestResult(result)); |
| 227 | } |
| 228 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 229 | void PortalDetector::StartAttempt(int init_delay_seconds) { |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 230 | int64 next_attempt_delay = 0; |
| 231 | if (attempt_count_ > 0) { |
| 232 | // Ensure that attempts are spaced at least by a minimal interval. |
| 233 | struct timeval now, elapsed_time; |
| 234 | time_->GetTimeMonotonic(&now); |
| 235 | timersub(&now, &attempt_start_time_, &elapsed_time); |
| 236 | |
| 237 | if (elapsed_time.tv_sec < kMinTimeBetweenAttemptsSeconds) { |
| 238 | struct timeval remaining_time = { kMinTimeBetweenAttemptsSeconds, 0 }; |
| 239 | timersub(&remaining_time, &elapsed_time, &remaining_time); |
| 240 | next_attempt_delay = |
| 241 | remaining_time.tv_sec * 1000 + remaining_time.tv_usec / 1000; |
| 242 | } |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 243 | } else { |
| 244 | next_attempt_delay = init_delay_seconds * 1000; |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 245 | } |
Paul Stewart | f582b50 | 2012-04-04 21:39:22 -0700 | [diff] [blame] | 246 | |
| 247 | start_attempt_.Reset(Bind(&PortalDetector::StartAttemptTask, |
| 248 | weak_ptr_factory_.GetWeakPtr())); |
| 249 | dispatcher_->PostDelayedTask(start_attempt_.callback(), next_attempt_delay); |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | void PortalDetector::StartAttemptTask() { |
| 253 | time_->GetTimeMonotonic(&attempt_start_time_); |
| 254 | ++attempt_count_; |
| 255 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 256 | LOG(INFO) << StringPrintf("Portal detection starting attempt %d of %d", |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 257 | attempt_count_, kMaxRequestAttempts); |
| 258 | |
| 259 | HTTPRequest::Result result = |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 260 | request_->Start(url_, request_read_callback_, request_result_callback_); |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 261 | if (result != HTTPRequest::kResultInProgress) { |
| 262 | CompleteAttempt(GetPortalResultForRequestResult(result)); |
| 263 | return; |
| 264 | } |
| 265 | |
Paul Stewart | f582b50 | 2012-04-04 21:39:22 -0700 | [diff] [blame] | 266 | attempt_timeout_.Reset(Bind(&PortalDetector::TimeoutAttemptTask, |
| 267 | weak_ptr_factory_.GetWeakPtr())); |
| 268 | dispatcher_->PostDelayedTask(attempt_timeout_.callback(), |
| 269 | kRequestTimeoutSeconds * 1000); |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | void PortalDetector::StopAttempt() { |
| 273 | request_->Stop(); |
Paul Stewart | f582b50 | 2012-04-04 21:39:22 -0700 | [diff] [blame] | 274 | attempt_timeout_.Cancel(); |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | void PortalDetector::TimeoutAttemptTask() { |
| 278 | LOG(ERROR) << "Request timed out"; |
| 279 | if (request_->response_data().GetLength()) { |
| 280 | CompleteAttempt(Result(kPhaseContent, kStatusTimeout)); |
| 281 | } else { |
| 282 | CompleteAttempt(Result(kPhaseUnknown, kStatusTimeout)); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | } // namespace shill |