tls_client_connection.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2019 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. #include "components/openscreen_platform/tls_client_connection.h"
  5. #include <algorithm>
  6. #include <limits>
  7. #include <sstream>
  8. #include <utility>
  9. #include <vector>
  10. #include "base/bind.h"
  11. #include "base/numerics/safe_conversions.h"
  12. namespace openscreen_platform {
  13. using openscreen::Error;
  14. TlsClientConnection::TlsClientConnection(
  15. openscreen::TaskRunner* task_runner,
  16. openscreen::IPEndpoint local_address,
  17. openscreen::IPEndpoint remote_address,
  18. mojo::ScopedDataPipeConsumerHandle receive_stream,
  19. mojo::ScopedDataPipeProducerHandle send_stream,
  20. mojo::Remote<network::mojom::TCPConnectedSocket> tcp_socket,
  21. mojo::Remote<network::mojom::TLSClientSocket> tls_socket)
  22. : task_runner_(task_runner),
  23. local_address_(std::move(local_address)),
  24. remote_address_(std::move(remote_address)),
  25. receive_stream_(std::move(receive_stream)),
  26. send_stream_(std::move(send_stream)),
  27. tcp_socket_(std::move(tcp_socket)),
  28. tls_socket_(std::move(tls_socket)),
  29. receive_stream_watcher_(FROM_HERE,
  30. mojo::SimpleWatcher::ArmingPolicy::MANUAL) {
  31. DCHECK(task_runner_);
  32. if (receive_stream_.is_valid()) {
  33. receive_stream_watcher_.Watch(
  34. receive_stream_.get(),
  35. MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_PEER_CLOSED |
  36. MOJO_HANDLE_SIGNAL_NEW_DATA_READABLE,
  37. MOJO_TRIGGER_CONDITION_SIGNALS_SATISFIED,
  38. base::BindRepeating(&TlsClientConnection::ReceiveMore,
  39. base::Unretained(this)));
  40. receive_stream_watcher_.ArmOrNotify();
  41. }
  42. }
  43. TlsClientConnection::~TlsClientConnection() = default;
  44. void TlsClientConnection::SetClient(Client* client) {
  45. client_ = client;
  46. }
  47. bool TlsClientConnection::Send(const void* data, size_t len) {
  48. if (!send_stream_.is_valid()) {
  49. if (client_) {
  50. client_->OnError(this, Error(Error::Code::kSocketClosedFailure,
  51. "Send stream was closed."));
  52. }
  53. return false;
  54. }
  55. uint32_t num_bytes = base::checked_cast<uint32_t>(len);
  56. const MojoResult result = send_stream_->WriteData(
  57. data, &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE);
  58. mojo::HandleSignalsState state = send_stream_->QuerySignalsState();
  59. return ProcessMojoResult(result, state.peer_closed()
  60. ? Error::Code::kSocketClosedFailure
  61. : Error::Code::kSocketSendFailure) ==
  62. Error::Code::kNone;
  63. }
  64. openscreen::IPEndpoint TlsClientConnection::GetRemoteEndpoint() const {
  65. return remote_address_;
  66. }
  67. void TlsClientConnection::ReceiveMore(MojoResult result,
  68. const mojo::HandleSignalsState& state) {
  69. if (!receive_stream_.is_valid()) {
  70. if (client_) {
  71. client_->OnError(this, Error(Error::Code::kSocketClosedFailure,
  72. "Receive stream was closed."));
  73. }
  74. return;
  75. }
  76. if (result == MOJO_RESULT_OK) {
  77. uint32_t num_bytes = 0;
  78. result = receive_stream_->ReadData(nullptr, &num_bytes,
  79. MOJO_READ_DATA_FLAG_QUERY);
  80. if (result == MOJO_RESULT_OK) {
  81. num_bytes = std::min(num_bytes, kMaxBytesPerRead);
  82. std::vector<uint8_t> buffer(num_bytes);
  83. result = receive_stream_->ReadData(buffer.data(), &num_bytes,
  84. MOJO_READ_DATA_FLAG_NONE);
  85. if (result == MOJO_RESULT_OK && client_) {
  86. buffer.resize(num_bytes);
  87. client_->OnRead(this, std::move(buffer));
  88. }
  89. }
  90. }
  91. const Error::Code interpretation = ProcessMojoResult(
  92. result, state.peer_closed() ? Error::Code::kSocketClosedFailure
  93. : Error::Code::kSocketReadFailure);
  94. if (interpretation == Error::Code::kNone ||
  95. interpretation == Error::Code::kAgain) {
  96. receive_stream_watcher_.ArmOrNotify();
  97. }
  98. }
  99. Error::Code TlsClientConnection::ProcessMojoResult(
  100. MojoResult result,
  101. Error::Code error_code_if_fatal) {
  102. switch (result) {
  103. case MOJO_RESULT_OK:
  104. return Error::Code::kNone;
  105. case MOJO_RESULT_UNAVAILABLE:
  106. case MOJO_RESULT_OUT_OF_RANGE: // Cannot write all data (all-or-none mode).
  107. case MOJO_RESULT_BUSY:
  108. case MOJO_RESULT_SHOULD_WAIT:
  109. // Transient (i.e., "try again") errors.
  110. return Error::Code::kAgain;
  111. default:
  112. // Fatal errors.
  113. if (client_) {
  114. std::ostringstream error_message;
  115. error_message << "MojoResult: " << result;
  116. client_->OnError(this, Error(error_code_if_fatal, error_message.str()));
  117. }
  118. return error_code_if_fatal;
  119. }
  120. }
  121. // static
  122. constexpr uint32_t TlsClientConnection::kMaxBytesPerRead;
  123. } // namespace openscreen_platform