Line data Source code
1 : #include "source/extensions/transport_sockets/alts/tsi_handshaker.h" 2 : 3 : #include <cstddef> 4 : #include <cstdint> 5 : #include <memory> 6 : #include <utility> 7 : 8 : #include "envoy/buffer/buffer.h" 9 : #include "envoy/event/dispatcher.h" 10 : 11 : #include "source/common/buffer/buffer_impl.h" 12 : #include "source/common/common/assert.h" 13 : #include "source/extensions/transport_sockets/alts/alts_tsi_handshaker.h" 14 : 15 : #include "absl/status/status.h" 16 : 17 : namespace Envoy { 18 : namespace Extensions { 19 : namespace TransportSockets { 20 : namespace Alts { 21 : 22 : void TsiHandshaker::onNextDone(absl::Status status, void* handshaker, 23 : const unsigned char* bytes_to_send, size_t bytes_to_send_size, 24 0 : std::unique_ptr<AltsHandshakeResult> handshake_result) { 25 0 : TsiHandshaker* tsi_handshaker = static_cast<TsiHandshaker*>(handshaker); 26 0 : Buffer::InstancePtr to_send = std::make_unique<Buffer::OwnedImpl>(); 27 0 : if (bytes_to_send_size > 0) { 28 0 : to_send->add(bytes_to_send, bytes_to_send_size); 29 0 : } 30 : 31 0 : auto next_result = new TsiHandshakerCallbacks::NextResult{status, std::move(to_send), 32 0 : std::move(handshake_result)}; 33 : 34 0 : tsi_handshaker->dispatcher_.post([tsi_handshaker, next_result]() { 35 0 : TsiHandshakerCallbacks::NextResultPtr next_result_ptr{next_result}; 36 : 37 0 : ASSERT(tsi_handshaker->calling_); 38 0 : tsi_handshaker->calling_ = false; 39 : 40 0 : if (tsi_handshaker->delete_on_done_) { 41 0 : tsi_handshaker->deferredDelete(); 42 0 : return; 43 0 : } 44 0 : tsi_handshaker->callbacks_->onNextDone(std::move(next_result_ptr)); 45 0 : }); 46 0 : } 47 : 48 : TsiHandshaker::TsiHandshaker(std::unique_ptr<AltsTsiHandshaker> handshaker, 49 : Event::Dispatcher& dispatcher) 50 0 : : handshaker_(std::move(handshaker)), dispatcher_(dispatcher) {} 51 : 52 0 : TsiHandshaker::~TsiHandshaker() { ASSERT(!calling_); } 53 : 54 0 : absl::Status TsiHandshaker::next(Envoy::Buffer::Instance& received) { 55 0 : ASSERT(callbacks_); 56 0 : ASSERT(!calling_); 57 0 : calling_ = true; 58 : 59 0 : uint64_t received_size = received.length(); 60 0 : absl::Status status = handshaker_->next( 61 0 : this, reinterpret_cast<const unsigned char*>(received.linearize(received_size)), 62 0 : received_size, onNextDone); 63 : 64 0 : received.drain(received_size); 65 0 : if (!status.ok()) { 66 0 : onNextDone(status, this, /*bytes_to_send=*/nullptr, 67 0 : /*bytes_to_send_size=*/0, /*handshake_result=*/nullptr); 68 0 : } 69 0 : return status; 70 0 : } 71 : 72 0 : void TsiHandshaker::deferredDelete() { 73 0 : if (calling_) { 74 0 : delete_on_done_ = true; 75 0 : } else { 76 0 : dispatcher_.deferredDelete(Event::DeferredDeletablePtr{this}); 77 0 : } 78 0 : } 79 : 80 : } // namespace Alts 81 : } // namespace TransportSockets 82 : } // namespace Extensions 83 : } // namespace Envoy