| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium 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 "components/cast_channel/cast_socket.h" | |
| 6 | |
| 7 #include <stdlib.h> | |
| 8 #include <string.h> | |
| 9 | |
| 10 #include <utility> | |
| 11 | |
| 12 #include "base/bind.h" | |
| 13 #include "base/callback_helpers.h" | |
| 14 #include "base/format_macros.h" | |
| 15 #include "base/lazy_instance.h" | |
| 16 #include "base/location.h" | |
| 17 #include "base/memory/ptr_util.h" | |
| 18 #include "base/numerics/safe_conversions.h" | |
| 19 #include "base/single_thread_task_runner.h" | |
| 20 #include "base/strings/string_number_conversions.h" | |
| 21 #include "base/strings/stringprintf.h" | |
| 22 #include "base/sys_byteorder.h" | |
| 23 #include "base/threading/thread_task_runner_handle.h" | |
| 24 #include "base/time/time.h" | |
| 25 #include "components/cast_channel/cast_auth_util.h" | |
| 26 #include "components/cast_channel/cast_framer.h" | |
| 27 #include "components/cast_channel/cast_message_util.h" | |
| 28 #include "components/cast_channel/cast_transport.h" | |
| 29 #include "components/cast_channel/logger.h" | |
| 30 #include "components/cast_channel/proto/cast_channel.pb.h" | |
| 31 #include "net/base/address_list.h" | |
| 32 #include "net/base/host_port_pair.h" | |
| 33 #include "net/base/net_errors.h" | |
| 34 #include "net/cert/cert_verifier.h" | |
| 35 #include "net/cert/cert_verify_result.h" | |
| 36 #include "net/cert/ct_policy_enforcer.h" | |
| 37 #include "net/cert/multi_log_ct_verifier.h" | |
| 38 #include "net/cert/x509_certificate.h" | |
| 39 #include "net/http/transport_security_state.h" | |
| 40 #include "net/log/net_log.h" | |
| 41 #include "net/log/net_log_source_type.h" | |
| 42 #include "net/socket/client_socket_factory.h" | |
| 43 #include "net/socket/client_socket_handle.h" | |
| 44 #include "net/socket/ssl_client_socket.h" | |
| 45 #include "net/socket/stream_socket.h" | |
| 46 #include "net/socket/tcp_client_socket.h" | |
| 47 #include "net/ssl/ssl_config_service.h" | |
| 48 #include "net/ssl/ssl_info.h" | |
| 49 | |
| 50 // Helper for logging data with remote host IP and authentication state. | |
| 51 // Assumes |ip_endpoint_| of type net::IPEndPoint and |channel_auth_| of enum | |
| 52 // type ChannelAuthType are available in the current scope. | |
| 53 #define CONNECTION_INFO() \ | |
| 54 "[" << ip_endpoint_.ToString() \ | |
| 55 << ", auth=" << ::cast_channel::ChannelAuthTypeToString(channel_auth_) \ | |
| 56 << "] " | |
| 57 #define VLOG_WITH_CONNECTION(level) VLOG(level) << CONNECTION_INFO() | |
| 58 #define LOG_WITH_CONNECTION(level) LOG(level) << CONNECTION_INFO() | |
| 59 | |
| 60 namespace cast_channel { | |
| 61 namespace { | |
| 62 | |
| 63 bool IsTerminalState(proto::ConnectionState state) { | |
| 64 return state == proto::CONN_STATE_FINISHED || | |
| 65 state == proto::CONN_STATE_ERROR || state == proto::CONN_STATE_TIMEOUT; | |
| 66 } | |
| 67 | |
| 68 // Cert verifier which blindly accepts all certificates, regardless of validity. | |
| 69 class FakeCertVerifier : public net::CertVerifier { | |
| 70 public: | |
| 71 FakeCertVerifier() {} | |
| 72 ~FakeCertVerifier() override {} | |
| 73 | |
| 74 int Verify(const RequestParams& params, | |
| 75 net::CRLSet*, | |
| 76 net::CertVerifyResult* verify_result, | |
| 77 const net::CompletionCallback&, | |
| 78 std::unique_ptr<Request>*, | |
| 79 const net::NetLogWithSource&) override { | |
| 80 verify_result->Reset(); | |
| 81 verify_result->verified_cert = params.certificate(); | |
| 82 return net::OK; | |
| 83 } | |
| 84 }; | |
| 85 | |
| 86 } // namespace | |
| 87 | |
| 88 using ChannelError = ::cast_channel::ChannelError; | |
| 89 using ChannelAuthType = ::cast_channel::ChannelAuthType; | |
| 90 using ReadyState = ::cast_channel::ReadyState; | |
| 91 | |
| 92 CastSocketImpl::CastSocketImpl(const std::string& owner_extension_id, | |
| 93 const net::IPEndPoint& ip_endpoint, | |
| 94 ChannelAuthType channel_auth, | |
| 95 net::NetLog* net_log, | |
| 96 const base::TimeDelta& timeout, | |
| 97 bool keep_alive, | |
| 98 const scoped_refptr<Logger>& logger, | |
| 99 uint64_t device_capabilities) | |
| 100 : CastSocketImpl(owner_extension_id, | |
| 101 ip_endpoint, | |
| 102 channel_auth, | |
| 103 net_log, | |
| 104 timeout, | |
| 105 keep_alive, | |
| 106 logger, | |
| 107 device_capabilities, | |
| 108 AuthContext::Create()) {} | |
| 109 | |
| 110 CastSocketImpl::CastSocketImpl(const std::string& owner_extension_id, | |
| 111 const net::IPEndPoint& ip_endpoint, | |
| 112 ChannelAuthType channel_auth, | |
| 113 net::NetLog* net_log, | |
| 114 const base::TimeDelta& timeout, | |
| 115 bool keep_alive, | |
| 116 const scoped_refptr<Logger>& logger, | |
| 117 uint64_t device_capabilities, | |
| 118 const AuthContext& auth_context) | |
| 119 : channel_id_(0), | |
| 120 ip_endpoint_(ip_endpoint), | |
| 121 channel_auth_(channel_auth), | |
| 122 net_log_(net_log), | |
| 123 keep_alive_(keep_alive), | |
| 124 logger_(logger), | |
| 125 auth_context_(auth_context), | |
| 126 connect_timeout_(timeout), | |
| 127 connect_timeout_timer_(new base::OneShotTimer), | |
| 128 is_canceled_(false), | |
| 129 device_capabilities_(device_capabilities), | |
| 130 audio_only_(false), | |
| 131 connect_state_(proto::CONN_STATE_START_CONNECT), | |
| 132 error_state_(ChannelError::NONE), | |
| 133 ready_state_(ReadyState::NONE), | |
| 134 auth_delegate_(nullptr) { | |
| 135 DCHECK(net_log_); | |
| 136 net_log_source_.type = net::NetLogSourceType::SOCKET; | |
| 137 net_log_source_.id = net_log_->NextID(); | |
| 138 } | |
| 139 | |
| 140 CastSocketImpl::~CastSocketImpl() { | |
| 141 // Ensure that resources are freed but do not run pending callbacks that | |
| 142 // would result in re-entrancy. | |
| 143 CloseInternal(); | |
| 144 | |
| 145 if (!connect_callback_.is_null()) | |
| 146 base::ResetAndReturn(&connect_callback_).Run(ChannelError::UNKNOWN); | |
| 147 } | |
| 148 | |
| 149 ReadyState CastSocketImpl::ready_state() const { | |
| 150 return ready_state_; | |
| 151 } | |
| 152 | |
| 153 ChannelError CastSocketImpl::error_state() const { | |
| 154 return error_state_; | |
| 155 } | |
| 156 | |
| 157 const net::IPEndPoint& CastSocketImpl::ip_endpoint() const { | |
| 158 return ip_endpoint_; | |
| 159 } | |
| 160 | |
| 161 int CastSocketImpl::id() const { | |
| 162 return channel_id_; | |
| 163 } | |
| 164 | |
| 165 void CastSocketImpl::set_id(int id) { | |
| 166 channel_id_ = id; | |
| 167 } | |
| 168 | |
| 169 ChannelAuthType CastSocketImpl::channel_auth() const { | |
| 170 return channel_auth_; | |
| 171 } | |
| 172 | |
| 173 bool CastSocketImpl::keep_alive() const { | |
| 174 return keep_alive_; | |
| 175 } | |
| 176 | |
| 177 bool CastSocketImpl::audio_only() const { | |
| 178 return audio_only_; | |
| 179 } | |
| 180 | |
| 181 std::unique_ptr<net::TCPClientSocket> CastSocketImpl::CreateTcpSocket() { | |
| 182 net::AddressList addresses(ip_endpoint_); | |
| 183 return std::unique_ptr<net::TCPClientSocket>( | |
| 184 new net::TCPClientSocket(addresses, nullptr, net_log_, net_log_source_)); | |
| 185 // Options cannot be set on the TCPClientSocket yet, because the | |
| 186 // underlying platform socket will not be created until Bind() | |
| 187 // or Connect() is called. | |
| 188 } | |
| 189 | |
| 190 std::unique_ptr<net::SSLClientSocket> CastSocketImpl::CreateSslSocket( | |
| 191 std::unique_ptr<net::StreamSocket> socket) { | |
| 192 net::SSLConfig ssl_config; | |
| 193 cert_verifier_ = base::WrapUnique(new FakeCertVerifier); | |
| 194 transport_security_state_.reset(new net::TransportSecurityState); | |
| 195 cert_transparency_verifier_.reset(new net::MultiLogCTVerifier()); | |
| 196 ct_policy_enforcer_.reset(new net::CTPolicyEnforcer()); | |
| 197 | |
| 198 // Note that |context| fields remain owned by CastSocketImpl. | |
| 199 net::SSLClientSocketContext context; | |
| 200 context.cert_verifier = cert_verifier_.get(); | |
| 201 context.transport_security_state = transport_security_state_.get(); | |
| 202 context.cert_transparency_verifier = cert_transparency_verifier_.get(); | |
| 203 context.ct_policy_enforcer = ct_policy_enforcer_.get(); | |
| 204 | |
| 205 std::unique_ptr<net::ClientSocketHandle> connection( | |
| 206 new net::ClientSocketHandle); | |
| 207 connection->SetSocket(std::move(socket)); | |
| 208 net::HostPortPair host_and_port = | |
| 209 net::HostPortPair::FromIPEndPoint(ip_endpoint_); | |
| 210 | |
| 211 return net::ClientSocketFactory::GetDefaultFactory()->CreateSSLClientSocket( | |
| 212 std::move(connection), host_and_port, ssl_config, context); | |
| 213 } | |
| 214 | |
| 215 scoped_refptr<net::X509Certificate> CastSocketImpl::ExtractPeerCert() { | |
| 216 net::SSLInfo ssl_info; | |
| 217 if (!socket_->GetSSLInfo(&ssl_info) || !ssl_info.cert.get()) | |
| 218 return nullptr; | |
| 219 | |
| 220 return ssl_info.cert; | |
| 221 } | |
| 222 | |
| 223 bool CastSocketImpl::VerifyChannelPolicy(const AuthResult& result) { | |
| 224 audio_only_ = (result.channel_policies & AuthResult::POLICY_AUDIO_ONLY) != 0; | |
| 225 if (audio_only_ && | |
| 226 (device_capabilities_ & CastDeviceCapability::VIDEO_OUT) != 0) { | |
| 227 LOG_WITH_CONNECTION(ERROR) | |
| 228 << "Audio only channel policy enforced for video out capable device"; | |
| 229 return false; | |
| 230 } | |
| 231 return true; | |
| 232 } | |
| 233 | |
| 234 bool CastSocketImpl::VerifyChallengeReply() { | |
| 235 DCHECK(peer_cert_); | |
| 236 AuthResult result = | |
| 237 AuthenticateChallengeReply(*challenge_reply_, *peer_cert_, auth_context_); | |
| 238 logger_->LogSocketChallengeReplyEvent(channel_id_, result); | |
| 239 if (result.success()) { | |
| 240 VLOG(1) << result.error_message; | |
| 241 if (!VerifyChannelPolicy(result)) { | |
| 242 return false; | |
| 243 } | |
| 244 } | |
| 245 return result.success(); | |
| 246 } | |
| 247 | |
| 248 void CastSocketImpl::SetTransportForTesting( | |
| 249 std::unique_ptr<CastTransport> transport) { | |
| 250 transport_ = std::move(transport); | |
| 251 } | |
| 252 | |
| 253 void CastSocketImpl::Connect(std::unique_ptr<CastTransport::Delegate> delegate, | |
| 254 base::Callback<void(ChannelError)> callback) { | |
| 255 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | |
| 256 VLOG_WITH_CONNECTION(1) << "Connect readyState = " | |
| 257 << ::cast_channel::ReadyStateToString(ready_state_); | |
| 258 DCHECK_EQ(proto::CONN_STATE_START_CONNECT, connect_state_); | |
| 259 | |
| 260 delegate_ = std::move(delegate); | |
| 261 | |
| 262 if (ready_state_ != ReadyState::NONE) { | |
| 263 callback.Run(ChannelError::CONNECT_ERROR); | |
| 264 return; | |
| 265 } | |
| 266 | |
| 267 connect_callback_ = callback; | |
| 268 SetReadyState(ReadyState::CONNECTING); | |
| 269 SetConnectState(proto::CONN_STATE_TCP_CONNECT); | |
| 270 | |
| 271 // Set up connection timeout. | |
| 272 if (connect_timeout_.InMicroseconds() > 0) { | |
| 273 DCHECK(connect_timeout_callback_.IsCancelled()); | |
| 274 connect_timeout_callback_.Reset( | |
| 275 base::Bind(&CastSocketImpl::OnConnectTimeout, base::Unretained(this))); | |
| 276 GetTimer()->Start(FROM_HERE, connect_timeout_, | |
| 277 connect_timeout_callback_.callback()); | |
| 278 } | |
| 279 | |
| 280 DoConnectLoop(net::OK); | |
| 281 } | |
| 282 | |
| 283 CastTransport* CastSocketImpl::transport() const { | |
| 284 return transport_.get(); | |
| 285 } | |
| 286 | |
| 287 void CastSocketImpl::OnConnectTimeout() { | |
| 288 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | |
| 289 // Stop all pending connection setup tasks and report back to the client. | |
| 290 is_canceled_ = true; | |
| 291 VLOG_WITH_CONNECTION(1) << "Timeout while establishing a connection."; | |
| 292 SetErrorState(ChannelError::CONNECT_TIMEOUT); | |
| 293 DoConnectCallback(); | |
| 294 } | |
| 295 | |
| 296 void CastSocketImpl::ResetConnectLoopCallback() { | |
| 297 DCHECK(connect_loop_callback_.IsCancelled()); | |
| 298 connect_loop_callback_.Reset( | |
| 299 base::Bind(&CastSocketImpl::DoConnectLoop, base::Unretained(this))); | |
| 300 } | |
| 301 | |
| 302 void CastSocketImpl::PostTaskToStartConnectLoop(int result) { | |
| 303 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | |
| 304 | |
| 305 ResetConnectLoopCallback(); | |
| 306 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 307 FROM_HERE, base::Bind(connect_loop_callback_.callback(), result)); | |
| 308 } | |
| 309 | |
| 310 // This method performs the state machine transitions for connection flow. | |
| 311 // There are two entry points to this method: | |
| 312 // 1. Connect method: this starts the flow | |
| 313 // 2. Callback from network operations that finish asynchronously. | |
| 314 void CastSocketImpl::DoConnectLoop(int result) { | |
| 315 connect_loop_callback_.Cancel(); | |
| 316 if (is_canceled_) { | |
| 317 LOG_WITH_CONNECTION(ERROR) << "CANCELLED - Aborting DoConnectLoop."; | |
| 318 return; | |
| 319 } | |
| 320 | |
| 321 // Network operations can either finish synchronously or asynchronously. | |
| 322 // This method executes the state machine transitions in a loop so that | |
| 323 // correct state transitions happen even when network operations finish | |
| 324 // synchronously. | |
| 325 int rv = result; | |
| 326 do { | |
| 327 proto::ConnectionState state = connect_state_; | |
| 328 connect_state_ = proto::CONN_STATE_UNKNOWN; | |
| 329 switch (state) { | |
| 330 case proto::CONN_STATE_TCP_CONNECT: | |
| 331 rv = DoTcpConnect(); | |
| 332 break; | |
| 333 case proto::CONN_STATE_TCP_CONNECT_COMPLETE: | |
| 334 rv = DoTcpConnectComplete(rv); | |
| 335 break; | |
| 336 case proto::CONN_STATE_SSL_CONNECT: | |
| 337 DCHECK_EQ(net::OK, rv); | |
| 338 rv = DoSslConnect(); | |
| 339 break; | |
| 340 case proto::CONN_STATE_SSL_CONNECT_COMPLETE: | |
| 341 rv = DoSslConnectComplete(rv); | |
| 342 break; | |
| 343 case proto::CONN_STATE_AUTH_CHALLENGE_SEND: | |
| 344 rv = DoAuthChallengeSend(); | |
| 345 break; | |
| 346 case proto::CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE: | |
| 347 rv = DoAuthChallengeSendComplete(rv); | |
| 348 break; | |
| 349 case proto::CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE: | |
| 350 rv = DoAuthChallengeReplyComplete(rv); | |
| 351 DCHECK(IsTerminalState(connect_state_)); | |
| 352 break; | |
| 353 default: | |
| 354 NOTREACHED() << "Unknown state in connect flow: " << state; | |
| 355 SetConnectState(proto::CONN_STATE_FINISHED); | |
| 356 SetErrorState(ChannelError::UNKNOWN); | |
| 357 DoConnectCallback(); | |
| 358 return; | |
| 359 } | |
| 360 } while (rv != net::ERR_IO_PENDING && !IsTerminalState(connect_state_)); | |
| 361 // Exit the state machine if an asynchronous network operation is pending | |
| 362 // or if the state machine is in the terminal "finished" state. | |
| 363 | |
| 364 if (IsTerminalState(connect_state_)) { | |
| 365 DCHECK_NE(rv, net::ERR_IO_PENDING); | |
| 366 GetTimer()->Stop(); | |
| 367 DoConnectCallback(); | |
| 368 } else { | |
| 369 DCHECK_EQ(rv, net::ERR_IO_PENDING); | |
| 370 } | |
| 371 } | |
| 372 | |
| 373 int CastSocketImpl::DoTcpConnect() { | |
| 374 DCHECK(connect_loop_callback_.IsCancelled()); | |
| 375 VLOG_WITH_CONNECTION(1) << "DoTcpConnect"; | |
| 376 SetConnectState(proto::CONN_STATE_TCP_CONNECT_COMPLETE); | |
| 377 tcp_socket_ = CreateTcpSocket(); | |
| 378 | |
| 379 int rv = tcp_socket_->Connect( | |
| 380 base::Bind(&CastSocketImpl::DoConnectLoop, base::Unretained(this))); | |
| 381 logger_->LogSocketEventWithRv(channel_id_, proto::TCP_SOCKET_CONNECT, rv); | |
| 382 return rv; | |
| 383 } | |
| 384 | |
| 385 int CastSocketImpl::DoTcpConnectComplete(int connect_result) { | |
| 386 VLOG_WITH_CONNECTION(1) << "DoTcpConnectComplete: " << connect_result; | |
| 387 logger_->LogSocketEventWithRv(channel_id_, proto::TCP_SOCKET_CONNECT_COMPLETE, | |
| 388 connect_result); | |
| 389 if (connect_result == net::OK) { | |
| 390 SetConnectState(proto::CONN_STATE_SSL_CONNECT); | |
| 391 } else if (connect_result == net::ERR_CONNECTION_TIMED_OUT) { | |
| 392 SetConnectState(proto::CONN_STATE_FINISHED); | |
| 393 SetErrorState(ChannelError::CONNECT_TIMEOUT); | |
| 394 } else { | |
| 395 SetConnectState(proto::CONN_STATE_FINISHED); | |
| 396 SetErrorState(ChannelError::CONNECT_ERROR); | |
| 397 } | |
| 398 return connect_result; | |
| 399 } | |
| 400 | |
| 401 int CastSocketImpl::DoSslConnect() { | |
| 402 DCHECK(connect_loop_callback_.IsCancelled()); | |
| 403 VLOG_WITH_CONNECTION(1) << "DoSslConnect"; | |
| 404 SetConnectState(proto::CONN_STATE_SSL_CONNECT_COMPLETE); | |
| 405 socket_ = CreateSslSocket(std::move(tcp_socket_)); | |
| 406 | |
| 407 int rv = socket_->Connect( | |
| 408 base::Bind(&CastSocketImpl::DoConnectLoop, base::Unretained(this))); | |
| 409 logger_->LogSocketEventWithRv(channel_id_, proto::SSL_SOCKET_CONNECT, rv); | |
| 410 return rv; | |
| 411 } | |
| 412 | |
| 413 int CastSocketImpl::DoSslConnectComplete(int result) { | |
| 414 logger_->LogSocketEventWithRv(channel_id_, proto::SSL_SOCKET_CONNECT_COMPLETE, | |
| 415 result); | |
| 416 VLOG_WITH_CONNECTION(1) << "DoSslConnectComplete: " << result; | |
| 417 if (result == net::OK) { | |
| 418 peer_cert_ = ExtractPeerCert(); | |
| 419 | |
| 420 if (!peer_cert_) { | |
| 421 LOG_WITH_CONNECTION(WARNING) << "Could not extract peer cert."; | |
| 422 SetConnectState(proto::CONN_STATE_FINISHED); | |
| 423 SetErrorState(ChannelError::AUTHENTICATION_ERROR); | |
| 424 return net::ERR_CERT_INVALID; | |
| 425 } | |
| 426 | |
| 427 // SSL connection succeeded. | |
| 428 if (!transport_.get()) { | |
| 429 // Create a channel transport if one wasn't already set (e.g. by test | |
| 430 // code). | |
| 431 transport_.reset(new CastTransportImpl(this->socket_.get(), channel_id_, | |
| 432 ip_endpoint_, channel_auth_, | |
| 433 logger_)); | |
| 434 } | |
| 435 auth_delegate_ = new AuthTransportDelegate(this); | |
| 436 transport_->SetReadDelegate(base::WrapUnique(auth_delegate_)); | |
| 437 SetConnectState(proto::CONN_STATE_AUTH_CHALLENGE_SEND); | |
| 438 } else if (result == net::ERR_CONNECTION_TIMED_OUT) { | |
| 439 SetConnectState(proto::CONN_STATE_FINISHED); | |
| 440 SetErrorState(ChannelError::CONNECT_TIMEOUT); | |
| 441 } else { | |
| 442 SetConnectState(proto::CONN_STATE_FINISHED); | |
| 443 SetErrorState(ChannelError::AUTHENTICATION_ERROR); | |
| 444 } | |
| 445 return result; | |
| 446 } | |
| 447 | |
| 448 int CastSocketImpl::DoAuthChallengeSend() { | |
| 449 VLOG_WITH_CONNECTION(1) << "DoAuthChallengeSend"; | |
| 450 SetConnectState(proto::CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE); | |
| 451 | |
| 452 CastMessage challenge_message; | |
| 453 CreateAuthChallengeMessage(&challenge_message, auth_context_); | |
| 454 VLOG_WITH_CONNECTION(1) << "Sending challenge: " | |
| 455 << CastMessageToString(challenge_message); | |
| 456 | |
| 457 ResetConnectLoopCallback(); | |
| 458 transport_->SendMessage(challenge_message, connect_loop_callback_.callback()); | |
| 459 | |
| 460 // Always return IO_PENDING since the result is always asynchronous. | |
| 461 return net::ERR_IO_PENDING; | |
| 462 } | |
| 463 | |
| 464 int CastSocketImpl::DoAuthChallengeSendComplete(int result) { | |
| 465 VLOG_WITH_CONNECTION(1) << "DoAuthChallengeSendComplete: " << result; | |
| 466 if (result < 0) { | |
| 467 SetConnectState(proto::CONN_STATE_ERROR); | |
| 468 SetErrorState(ChannelError::CAST_SOCKET_ERROR); | |
| 469 logger_->LogSocketEventWithRv(channel_id_, | |
| 470 proto::SEND_AUTH_CHALLENGE_FAILED, result); | |
| 471 return result; | |
| 472 } | |
| 473 transport_->Start(); | |
| 474 SetConnectState(proto::CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE); | |
| 475 return net::ERR_IO_PENDING; | |
| 476 } | |
| 477 | |
| 478 CastSocketImpl::AuthTransportDelegate::AuthTransportDelegate( | |
| 479 CastSocketImpl* socket) | |
| 480 : socket_(socket), error_state_(ChannelError::NONE) { | |
| 481 DCHECK(socket); | |
| 482 } | |
| 483 | |
| 484 ChannelError CastSocketImpl::AuthTransportDelegate::error_state() const { | |
| 485 return error_state_; | |
| 486 } | |
| 487 | |
| 488 LastErrors CastSocketImpl::AuthTransportDelegate::last_errors() const { | |
| 489 return last_errors_; | |
| 490 } | |
| 491 | |
| 492 void CastSocketImpl::AuthTransportDelegate::OnError(ChannelError error_state) { | |
| 493 error_state_ = error_state; | |
| 494 socket_->PostTaskToStartConnectLoop(net::ERR_CONNECTION_FAILED); | |
| 495 } | |
| 496 | |
| 497 void CastSocketImpl::AuthTransportDelegate::OnMessage( | |
| 498 const CastMessage& message) { | |
| 499 if (!IsAuthMessage(message)) { | |
| 500 error_state_ = ChannelError::TRANSPORT_ERROR; | |
| 501 socket_->PostTaskToStartConnectLoop(net::ERR_INVALID_RESPONSE); | |
| 502 } else { | |
| 503 socket_->challenge_reply_.reset(new CastMessage(message)); | |
| 504 socket_->PostTaskToStartConnectLoop(net::OK); | |
| 505 } | |
| 506 } | |
| 507 | |
| 508 void CastSocketImpl::AuthTransportDelegate::Start() {} | |
| 509 | |
| 510 int CastSocketImpl::DoAuthChallengeReplyComplete(int result) { | |
| 511 VLOG_WITH_CONNECTION(1) << "DoAuthChallengeReplyComplete: " << result; | |
| 512 | |
| 513 if (auth_delegate_->error_state() != ChannelError::NONE) { | |
| 514 SetErrorState(auth_delegate_->error_state()); | |
| 515 SetConnectState(proto::CONN_STATE_ERROR); | |
| 516 return net::ERR_CONNECTION_FAILED; | |
| 517 } | |
| 518 auth_delegate_ = nullptr; | |
| 519 | |
| 520 if (result < 0) { | |
| 521 SetConnectState(proto::CONN_STATE_ERROR); | |
| 522 return result; | |
| 523 } | |
| 524 | |
| 525 if (!VerifyChallengeReply()) { | |
| 526 SetErrorState(ChannelError::AUTHENTICATION_ERROR); | |
| 527 SetConnectState(proto::CONN_STATE_ERROR); | |
| 528 return net::ERR_CONNECTION_FAILED; | |
| 529 } | |
| 530 VLOG_WITH_CONNECTION(1) << "Auth challenge verification succeeded"; | |
| 531 | |
| 532 SetConnectState(proto::CONN_STATE_FINISHED); | |
| 533 return net::OK; | |
| 534 } | |
| 535 | |
| 536 void CastSocketImpl::DoConnectCallback() { | |
| 537 VLOG(1) << "DoConnectCallback (error_state = " | |
| 538 << ::cast_channel::ChannelErrorToString(error_state_) << ")"; | |
| 539 if (connect_callback_.is_null()) { | |
| 540 DLOG(FATAL) << "Connection callback invoked multiple times."; | |
| 541 return; | |
| 542 } | |
| 543 | |
| 544 if (error_state_ == ChannelError::NONE) { | |
| 545 SetReadyState(ReadyState::OPEN); | |
| 546 transport_->SetReadDelegate(std::move(delegate_)); | |
| 547 } else { | |
| 548 CloseInternal(); | |
| 549 } | |
| 550 | |
| 551 base::ResetAndReturn(&connect_callback_).Run(error_state_); | |
| 552 } | |
| 553 | |
| 554 void CastSocketImpl::Close(const net::CompletionCallback& callback) { | |
| 555 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | |
| 556 CloseInternal(); | |
| 557 // Run this callback last. It may delete the socket. | |
| 558 callback.Run(net::OK); | |
| 559 } | |
| 560 | |
| 561 void CastSocketImpl::CloseInternal() { | |
| 562 // TODO(mfoltz): Enforce this when CastChannelAPITest is rewritten to create | |
| 563 // and free sockets on the same thread. crbug.com/398242 | |
| 564 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | |
| 565 if (ready_state_ == ReadyState::CLOSED) { | |
| 566 return; | |
| 567 } | |
| 568 | |
| 569 VLOG_WITH_CONNECTION(1) << "Close ReadyState = " | |
| 570 << ::cast_channel::ReadyStateToString(ready_state_); | |
| 571 transport_.reset(); | |
| 572 tcp_socket_.reset(); | |
| 573 socket_.reset(); | |
| 574 transport_security_state_.reset(); | |
| 575 if (GetTimer()) { | |
| 576 GetTimer()->Stop(); | |
| 577 } | |
| 578 | |
| 579 // Cancel callbacks that we queued ourselves to re-enter the connect or read | |
| 580 // loops. | |
| 581 connect_loop_callback_.Cancel(); | |
| 582 connect_timeout_callback_.Cancel(); | |
| 583 SetReadyState(ReadyState::CLOSED); | |
| 584 } | |
| 585 | |
| 586 base::Timer* CastSocketImpl::GetTimer() { | |
| 587 return connect_timeout_timer_.get(); | |
| 588 } | |
| 589 | |
| 590 void CastSocketImpl::SetConnectState(proto::ConnectionState connect_state) { | |
| 591 if (connect_state_ != connect_state) { | |
| 592 connect_state_ = connect_state; | |
| 593 } | |
| 594 } | |
| 595 | |
| 596 void CastSocketImpl::SetReadyState(ReadyState ready_state) { | |
| 597 if (ready_state_ != ready_state) | |
| 598 ready_state_ = ready_state; | |
| 599 } | |
| 600 | |
| 601 void CastSocketImpl::SetErrorState(ChannelError error_state) { | |
| 602 VLOG_WITH_CONNECTION(1) << "SetErrorState " | |
| 603 << ::cast_channel::ChannelErrorToString(error_state); | |
| 604 DCHECK_EQ(ChannelError::NONE, error_state_); | |
| 605 error_state_ = error_state; | |
| 606 delegate_->OnError(error_state_); | |
| 607 } | |
| 608 | |
| 609 } // namespace cast_channel | |
| 610 #undef VLOG_WITH_CONNECTION | |
| OLD | NEW |