| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "extensions/browser/api/cast_channel/cast_socket.h" | 5 #include "components/cast_channel/cast_socket.h" |
| 6 | 6 |
| 7 #include <stdlib.h> | 7 #include <stdlib.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/callback_helpers.h" | 13 #include "base/callback_helpers.h" |
| 14 #include "base/format_macros.h" | 14 #include "base/format_macros.h" |
| 15 #include "base/lazy_instance.h" | 15 #include "base/lazy_instance.h" |
| 16 #include "base/location.h" | 16 #include "base/location.h" |
| 17 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
| 18 #include "base/numerics/safe_conversions.h" | 18 #include "base/numerics/safe_conversions.h" |
| 19 #include "base/single_thread_task_runner.h" | 19 #include "base/single_thread_task_runner.h" |
| 20 #include "base/strings/string_number_conversions.h" | 20 #include "base/strings/string_number_conversions.h" |
| 21 #include "base/strings/stringprintf.h" | 21 #include "base/strings/stringprintf.h" |
| 22 #include "base/sys_byteorder.h" | 22 #include "base/sys_byteorder.h" |
| 23 #include "base/threading/thread_task_runner_handle.h" | 23 #include "base/threading/thread_task_runner_handle.h" |
| 24 #include "base/time/time.h" | 24 #include "base/time/time.h" |
| 25 #include "extensions/browser/api/cast_channel/cast_auth_util.h" | 25 #include "components/cast_channel/cast_auth_util.h" |
| 26 #include "extensions/browser/api/cast_channel/cast_framer.h" | 26 #include "components/cast_channel/cast_framer.h" |
| 27 #include "extensions/browser/api/cast_channel/cast_message_util.h" | 27 #include "components/cast_channel/cast_message_util.h" |
| 28 #include "extensions/browser/api/cast_channel/cast_transport.h" | 28 #include "components/cast_channel/cast_transport.h" |
| 29 #include "extensions/browser/api/cast_channel/logger.h" | 29 #include "components/cast_channel/logger.h" |
| 30 #include "extensions/common/api/cast_channel/cast_channel.pb.h" | 30 #include "components/cast_channel/proto/cast_channel.pb.h" |
| 31 #include "net/base/address_list.h" | 31 #include "net/base/address_list.h" |
| 32 #include "net/base/host_port_pair.h" | 32 #include "net/base/host_port_pair.h" |
| 33 #include "net/base/net_errors.h" | 33 #include "net/base/net_errors.h" |
| 34 #include "net/cert/cert_verifier.h" | 34 #include "net/cert/cert_verifier.h" |
| 35 #include "net/cert/cert_verify_result.h" | 35 #include "net/cert/cert_verify_result.h" |
| 36 #include "net/cert/ct_policy_enforcer.h" | 36 #include "net/cert/ct_policy_enforcer.h" |
| 37 #include "net/cert/multi_log_ct_verifier.h" | 37 #include "net/cert/multi_log_ct_verifier.h" |
| 38 #include "net/cert/x509_certificate.h" | 38 #include "net/cert/x509_certificate.h" |
| 39 #include "net/http/transport_security_state.h" | 39 #include "net/http/transport_security_state.h" |
| 40 #include "net/log/net_log.h" | 40 #include "net/log/net_log.h" |
| 41 #include "net/log/net_log_source_type.h" | 41 #include "net/log/net_log_source_type.h" |
| 42 #include "net/socket/client_socket_factory.h" | 42 #include "net/socket/client_socket_factory.h" |
| 43 #include "net/socket/client_socket_handle.h" | 43 #include "net/socket/client_socket_handle.h" |
| 44 #include "net/socket/ssl_client_socket.h" | 44 #include "net/socket/ssl_client_socket.h" |
| 45 #include "net/socket/stream_socket.h" | 45 #include "net/socket/stream_socket.h" |
| 46 #include "net/socket/tcp_client_socket.h" | 46 #include "net/socket/tcp_client_socket.h" |
| 47 #include "net/ssl/ssl_config_service.h" | 47 #include "net/ssl/ssl_config_service.h" |
| 48 #include "net/ssl/ssl_info.h" | 48 #include "net/ssl/ssl_info.h" |
| 49 | 49 |
| 50 // Helper for logging data with remote host IP and authentication state. | 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 | 51 // Assumes |ip_endpoint_| of type net::IPEndPoint and |channel_auth_| of enum |
| 52 // type ChannelAuthType are available in the current scope. | 52 // type ChannelAuthType are available in the current scope. |
| 53 #define CONNECTION_INFO() \ | 53 #define CONNECTION_INFO() \ |
| 54 "[" << ip_endpoint_.ToString() \ | 54 "[" << ip_endpoint_.ToString() \ |
| 55 << ", auth=" << ::cast_channel::ChannelAuthTypeToString(channel_auth_) \ | 55 << ", auth=" << ::cast_channel::ChannelAuthTypeToString(channel_auth_) \ |
| 56 << "] " | 56 << "] " |
| 57 #define VLOG_WITH_CONNECTION(level) VLOG(level) << CONNECTION_INFO() | 57 #define VLOG_WITH_CONNECTION(level) VLOG(level) << CONNECTION_INFO() |
| 58 #define LOG_WITH_CONNECTION(level) LOG(level) << CONNECTION_INFO() | 58 #define LOG_WITH_CONNECTION(level) LOG(level) << CONNECTION_INFO() |
| 59 | 59 |
| 60 namespace extensions { | |
| 61 namespace api { | |
| 62 namespace cast_channel { | 60 namespace cast_channel { |
| 63 namespace { | 61 namespace { |
| 64 | 62 |
| 65 bool IsTerminalState(proto::ConnectionState state) { | 63 bool IsTerminalState(proto::ConnectionState state) { |
| 66 return state == proto::CONN_STATE_FINISHED || | 64 return state == proto::CONN_STATE_FINISHED || |
| 67 state == proto::CONN_STATE_ERROR || state == proto::CONN_STATE_TIMEOUT; | 65 state == proto::CONN_STATE_ERROR || state == proto::CONN_STATE_TIMEOUT; |
| 68 } | 66 } |
| 69 | 67 |
| 70 // Cert verifier which blindly accepts all certificates, regardless of validity. | 68 // Cert verifier which blindly accepts all certificates, regardless of validity. |
| 71 class FakeCertVerifier : public net::CertVerifier { | 69 class FakeCertVerifier : public net::CertVerifier { |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 // Note that |context| fields remain owned by CastSocketImpl. | 198 // Note that |context| fields remain owned by CastSocketImpl. |
| 201 net::SSLClientSocketContext context; | 199 net::SSLClientSocketContext context; |
| 202 context.cert_verifier = cert_verifier_.get(); | 200 context.cert_verifier = cert_verifier_.get(); |
| 203 context.transport_security_state = transport_security_state_.get(); | 201 context.transport_security_state = transport_security_state_.get(); |
| 204 context.cert_transparency_verifier = cert_transparency_verifier_.get(); | 202 context.cert_transparency_verifier = cert_transparency_verifier_.get(); |
| 205 context.ct_policy_enforcer = ct_policy_enforcer_.get(); | 203 context.ct_policy_enforcer = ct_policy_enforcer_.get(); |
| 206 | 204 |
| 207 std::unique_ptr<net::ClientSocketHandle> connection( | 205 std::unique_ptr<net::ClientSocketHandle> connection( |
| 208 new net::ClientSocketHandle); | 206 new net::ClientSocketHandle); |
| 209 connection->SetSocket(std::move(socket)); | 207 connection->SetSocket(std::move(socket)); |
| 210 net::HostPortPair host_and_port = net::HostPortPair::FromIPEndPoint( | 208 net::HostPortPair host_and_port = |
| 211 ip_endpoint_); | 209 net::HostPortPair::FromIPEndPoint(ip_endpoint_); |
| 212 | 210 |
| 213 return net::ClientSocketFactory::GetDefaultFactory()->CreateSSLClientSocket( | 211 return net::ClientSocketFactory::GetDefaultFactory()->CreateSSLClientSocket( |
| 214 std::move(connection), host_and_port, ssl_config, context); | 212 std::move(connection), host_and_port, ssl_config, context); |
| 215 } | 213 } |
| 216 | 214 |
| 217 scoped_refptr<net::X509Certificate> CastSocketImpl::ExtractPeerCert() { | 215 scoped_refptr<net::X509Certificate> CastSocketImpl::ExtractPeerCert() { |
| 218 net::SSLInfo ssl_info; | 216 net::SSLInfo ssl_info; |
| 219 if (!socket_->GetSSLInfo(&ssl_info) || !ssl_info.cert.get()) | 217 if (!socket_->GetSSLInfo(&ssl_info) || !ssl_info.cert.get()) |
| 220 return nullptr; | 218 return nullptr; |
| 221 | 219 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 247 return result.success(); | 245 return result.success(); |
| 248 } | 246 } |
| 249 | 247 |
| 250 void CastSocketImpl::SetTransportForTesting( | 248 void CastSocketImpl::SetTransportForTesting( |
| 251 std::unique_ptr<CastTransport> transport) { | 249 std::unique_ptr<CastTransport> transport) { |
| 252 transport_ = std::move(transport); | 250 transport_ = std::move(transport); |
| 253 } | 251 } |
| 254 | 252 |
| 255 void CastSocketImpl::Connect(std::unique_ptr<CastTransport::Delegate> delegate, | 253 void CastSocketImpl::Connect(std::unique_ptr<CastTransport::Delegate> delegate, |
| 256 base::Callback<void(ChannelError)> callback) { | 254 base::Callback<void(ChannelError)> callback) { |
| 257 DCHECK(CalledOnValidThread()); | 255 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 258 VLOG_WITH_CONNECTION(1) << "Connect readyState = " | 256 VLOG_WITH_CONNECTION(1) << "Connect readyState = " |
| 259 << ::cast_channel::ReadyStateToString(ready_state_); | 257 << ::cast_channel::ReadyStateToString(ready_state_); |
| 260 DCHECK_EQ(proto::CONN_STATE_START_CONNECT, connect_state_); | 258 DCHECK_EQ(proto::CONN_STATE_START_CONNECT, connect_state_); |
| 261 | 259 |
| 262 delegate_ = std::move(delegate); | 260 delegate_ = std::move(delegate); |
| 263 | 261 |
| 264 if (ready_state_ != ReadyState::NONE) { | 262 if (ready_state_ != ReadyState::NONE) { |
| 265 callback.Run(ChannelError::CONNECT_ERROR); | 263 callback.Run(ChannelError::CONNECT_ERROR); |
| 266 return; | 264 return; |
| 267 } | 265 } |
| 268 | 266 |
| 269 connect_callback_ = callback; | 267 connect_callback_ = callback; |
| 270 SetReadyState(ReadyState::CONNECTING); | 268 SetReadyState(ReadyState::CONNECTING); |
| 271 SetConnectState(proto::CONN_STATE_TCP_CONNECT); | 269 SetConnectState(proto::CONN_STATE_TCP_CONNECT); |
| 272 | 270 |
| 273 // Set up connection timeout. | 271 // Set up connection timeout. |
| 274 if (connect_timeout_.InMicroseconds() > 0) { | 272 if (connect_timeout_.InMicroseconds() > 0) { |
| 275 DCHECK(connect_timeout_callback_.IsCancelled()); | 273 DCHECK(connect_timeout_callback_.IsCancelled()); |
| 276 connect_timeout_callback_.Reset( | 274 connect_timeout_callback_.Reset( |
| 277 base::Bind(&CastSocketImpl::OnConnectTimeout, base::Unretained(this))); | 275 base::Bind(&CastSocketImpl::OnConnectTimeout, base::Unretained(this))); |
| 278 GetTimer()->Start(FROM_HERE, | 276 GetTimer()->Start(FROM_HERE, connect_timeout_, |
| 279 connect_timeout_, | |
| 280 connect_timeout_callback_.callback()); | 277 connect_timeout_callback_.callback()); |
| 281 } | 278 } |
| 282 | 279 |
| 283 DoConnectLoop(net::OK); | 280 DoConnectLoop(net::OK); |
| 284 } | 281 } |
| 285 | 282 |
| 286 CastTransport* CastSocketImpl::transport() const { | 283 CastTransport* CastSocketImpl::transport() const { |
| 287 return transport_.get(); | 284 return transport_.get(); |
| 288 } | 285 } |
| 289 | 286 |
| 290 void CastSocketImpl::OnConnectTimeout() { | 287 void CastSocketImpl::OnConnectTimeout() { |
| 291 DCHECK(CalledOnValidThread()); | 288 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 292 // Stop all pending connection setup tasks and report back to the client. | 289 // Stop all pending connection setup tasks and report back to the client. |
| 293 is_canceled_ = true; | 290 is_canceled_ = true; |
| 294 VLOG_WITH_CONNECTION(1) << "Timeout while establishing a connection."; | 291 VLOG_WITH_CONNECTION(1) << "Timeout while establishing a connection."; |
| 295 SetErrorState(ChannelError::CONNECT_TIMEOUT); | 292 SetErrorState(ChannelError::CONNECT_TIMEOUT); |
| 296 DoConnectCallback(); | 293 DoConnectCallback(); |
| 297 } | 294 } |
| 298 | 295 |
| 299 void CastSocketImpl::ResetConnectLoopCallback() { | 296 void CastSocketImpl::ResetConnectLoopCallback() { |
| 300 DCHECK(connect_loop_callback_.IsCancelled()); | 297 DCHECK(connect_loop_callback_.IsCancelled()); |
| 301 connect_loop_callback_.Reset( | 298 connect_loop_callback_.Reset( |
| 302 base::Bind(&CastSocketImpl::DoConnectLoop, base::Unretained(this))); | 299 base::Bind(&CastSocketImpl::DoConnectLoop, base::Unretained(this))); |
| 303 } | 300 } |
| 304 | 301 |
| 305 void CastSocketImpl::PostTaskToStartConnectLoop(int result) { | 302 void CastSocketImpl::PostTaskToStartConnectLoop(int result) { |
| 306 DCHECK(CalledOnValidThread()); | 303 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 307 | 304 |
| 308 ResetConnectLoopCallback(); | 305 ResetConnectLoopCallback(); |
| 309 base::ThreadTaskRunnerHandle::Get()->PostTask( | 306 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 310 FROM_HERE, base::Bind(connect_loop_callback_.callback(), result)); | 307 FROM_HERE, base::Bind(connect_loop_callback_.callback(), result)); |
| 311 } | 308 } |
| 312 | 309 |
| 313 // This method performs the state machine transitions for connection flow. | 310 // This method performs the state machine transitions for connection flow. |
| 314 // There are two entry points to this method: | 311 // There are two entry points to this method: |
| 315 // 1. Connect method: this starts the flow | 312 // 1. Connect method: this starts the flow |
| 316 // 2. Callback from network operations that finish asynchronously. | 313 // 2. Callback from network operations that finish asynchronously. |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 501 const CastMessage& message) { | 498 const CastMessage& message) { |
| 502 if (!IsAuthMessage(message)) { | 499 if (!IsAuthMessage(message)) { |
| 503 error_state_ = ChannelError::TRANSPORT_ERROR; | 500 error_state_ = ChannelError::TRANSPORT_ERROR; |
| 504 socket_->PostTaskToStartConnectLoop(net::ERR_INVALID_RESPONSE); | 501 socket_->PostTaskToStartConnectLoop(net::ERR_INVALID_RESPONSE); |
| 505 } else { | 502 } else { |
| 506 socket_->challenge_reply_.reset(new CastMessage(message)); | 503 socket_->challenge_reply_.reset(new CastMessage(message)); |
| 507 socket_->PostTaskToStartConnectLoop(net::OK); | 504 socket_->PostTaskToStartConnectLoop(net::OK); |
| 508 } | 505 } |
| 509 } | 506 } |
| 510 | 507 |
| 511 void CastSocketImpl::AuthTransportDelegate::Start() { | 508 void CastSocketImpl::AuthTransportDelegate::Start() {} |
| 512 } | |
| 513 | 509 |
| 514 int CastSocketImpl::DoAuthChallengeReplyComplete(int result) { | 510 int CastSocketImpl::DoAuthChallengeReplyComplete(int result) { |
| 515 VLOG_WITH_CONNECTION(1) << "DoAuthChallengeReplyComplete: " << result; | 511 VLOG_WITH_CONNECTION(1) << "DoAuthChallengeReplyComplete: " << result; |
| 516 | 512 |
| 517 if (auth_delegate_->error_state() != ChannelError::NONE) { | 513 if (auth_delegate_->error_state() != ChannelError::NONE) { |
| 518 SetErrorState(auth_delegate_->error_state()); | 514 SetErrorState(auth_delegate_->error_state()); |
| 519 SetConnectState(proto::CONN_STATE_ERROR); | 515 SetConnectState(proto::CONN_STATE_ERROR); |
| 520 return net::ERR_CONNECTION_FAILED; | 516 return net::ERR_CONNECTION_FAILED; |
| 521 } | 517 } |
| 522 auth_delegate_ = nullptr; | 518 auth_delegate_ = nullptr; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 549 SetReadyState(ReadyState::OPEN); | 545 SetReadyState(ReadyState::OPEN); |
| 550 transport_->SetReadDelegate(std::move(delegate_)); | 546 transport_->SetReadDelegate(std::move(delegate_)); |
| 551 } else { | 547 } else { |
| 552 CloseInternal(); | 548 CloseInternal(); |
| 553 } | 549 } |
| 554 | 550 |
| 555 base::ResetAndReturn(&connect_callback_).Run(error_state_); | 551 base::ResetAndReturn(&connect_callback_).Run(error_state_); |
| 556 } | 552 } |
| 557 | 553 |
| 558 void CastSocketImpl::Close(const net::CompletionCallback& callback) { | 554 void CastSocketImpl::Close(const net::CompletionCallback& callback) { |
| 559 DCHECK(CalledOnValidThread()); | 555 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 560 CloseInternal(); | 556 CloseInternal(); |
| 561 // Run this callback last. It may delete the socket. | 557 // Run this callback last. It may delete the socket. |
| 562 callback.Run(net::OK); | 558 callback.Run(net::OK); |
| 563 } | 559 } |
| 564 | 560 |
| 565 void CastSocketImpl::CloseInternal() { | 561 void CastSocketImpl::CloseInternal() { |
| 566 // TODO(mfoltz): Enforce this when CastChannelAPITest is rewritten to create | 562 // TODO(mfoltz): Enforce this when CastChannelAPITest is rewritten to create |
| 567 // and free sockets on the same thread. crbug.com/398242 | 563 // and free sockets on the same thread. crbug.com/398242 |
| 568 DCHECK(CalledOnValidThread()); | 564 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 569 if (ready_state_ == ReadyState::CLOSED) { | 565 if (ready_state_ == ReadyState::CLOSED) { |
| 570 return; | 566 return; |
| 571 } | 567 } |
| 572 | 568 |
| 573 VLOG_WITH_CONNECTION(1) << "Close ReadyState = " | 569 VLOG_WITH_CONNECTION(1) << "Close ReadyState = " |
| 574 << ::cast_channel::ReadyStateToString(ready_state_); | 570 << ::cast_channel::ReadyStateToString(ready_state_); |
| 575 transport_.reset(); | 571 transport_.reset(); |
| 576 tcp_socket_.reset(); | 572 tcp_socket_.reset(); |
| 577 socket_.reset(); | 573 socket_.reset(); |
| 578 transport_security_state_.reset(); | 574 transport_security_state_.reset(); |
| 579 if (GetTimer()) { | 575 if (GetTimer()) { |
| 580 GetTimer()->Stop(); | 576 GetTimer()->Stop(); |
| 581 } | 577 } |
| 582 | 578 |
| 583 // Cancel callbacks that we queued ourselves to re-enter the connect or read | 579 // Cancel callbacks that we queued ourselves to re-enter the connect or read |
| 584 // loops. | 580 // loops. |
| 585 connect_loop_callback_.Cancel(); | 581 connect_loop_callback_.Cancel(); |
| 586 connect_timeout_callback_.Cancel(); | 582 connect_timeout_callback_.Cancel(); |
| 587 SetReadyState(ReadyState::CLOSED); | 583 SetReadyState(ReadyState::CLOSED); |
| 588 } | 584 } |
| 589 | 585 |
| 590 bool CastSocketImpl::CalledOnValidThread() const { | |
| 591 return thread_checker_.CalledOnValidThread(); | |
| 592 } | |
| 593 | |
| 594 base::Timer* CastSocketImpl::GetTimer() { | 586 base::Timer* CastSocketImpl::GetTimer() { |
| 595 return connect_timeout_timer_.get(); | 587 return connect_timeout_timer_.get(); |
| 596 } | 588 } |
| 597 | 589 |
| 598 void CastSocketImpl::SetConnectState(proto::ConnectionState connect_state) { | 590 void CastSocketImpl::SetConnectState(proto::ConnectionState connect_state) { |
| 599 if (connect_state_ != connect_state) { | 591 if (connect_state_ != connect_state) { |
| 600 connect_state_ = connect_state; | 592 connect_state_ = connect_state; |
| 601 } | 593 } |
| 602 } | 594 } |
| 603 | 595 |
| 604 void CastSocketImpl::SetReadyState(ReadyState ready_state) { | 596 void CastSocketImpl::SetReadyState(ReadyState ready_state) { |
| 605 if (ready_state_ != ready_state) | 597 if (ready_state_ != ready_state) |
| 606 ready_state_ = ready_state; | 598 ready_state_ = ready_state; |
| 607 } | 599 } |
| 608 | 600 |
| 609 void CastSocketImpl::SetErrorState(ChannelError error_state) { | 601 void CastSocketImpl::SetErrorState(ChannelError error_state) { |
| 610 VLOG_WITH_CONNECTION(1) << "SetErrorState " | 602 VLOG_WITH_CONNECTION(1) << "SetErrorState " |
| 611 << ::cast_channel::ChannelErrorToString(error_state); | 603 << ::cast_channel::ChannelErrorToString(error_state); |
| 612 DCHECK_EQ(ChannelError::NONE, error_state_); | 604 DCHECK_EQ(ChannelError::NONE, error_state_); |
| 613 error_state_ = error_state; | 605 error_state_ = error_state; |
| 614 delegate_->OnError(error_state_); | 606 delegate_->OnError(error_state_); |
| 615 } | 607 } |
| 616 | 608 |
| 617 } // namespace cast_channel | 609 } // namespace cast_channel |
| 618 } // namespace api | |
| 619 } // namespace extensions | |
| 620 #undef VLOG_WITH_CONNECTION | 610 #undef VLOG_WITH_CONNECTION |
| OLD | NEW |