Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BugFix: fix protocol check error of bad magic number due to ssl write dirty data to reused socket fd #96

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions clean.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#! /bin/bash

bazel clean --expunge

# Delete xx.log or xx.bin on root directory of trpc-cpp project.
find ./ -maxdepth 1 -name "*.log" -exec rm {} \;
find ./ -maxdepth 1 -name "*.bin" -exec rm {} \;
3 changes: 3 additions & 0 deletions trpc/runtime/iomodel/reactor/common/io_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ class IoHandler {

/// @brief write data to the connection
virtual int Writev(const iovec* iov, int iovcnt) = 0;

/// @brief Destroy IO handler.
virtual void Destroy() {}
};

} // namespace trpc
4 changes: 4 additions & 0 deletions trpc/runtime/iomodel/reactor/default/tcp_connection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ bool TcpConnection::DoConnect() {
<< ", failed. error no: " << errno
<< ", msg: " << strerror(errno));

GetIoHandler()->Destroy();
socket_.Close();
HandleClose(false);

Expand Down Expand Up @@ -353,6 +354,9 @@ void TcpConnection::HandleClose(bool destroy) {
}
io_msgs_.clear();

// Requirements: destroy IO-handler before close socket.
GetIoHandler()->Destroy();

DisableReadWrite();

handshake_status_ = IoHandler::HandshakeStatus::kFailed;
Expand Down
7 changes: 6 additions & 1 deletion trpc/runtime/iomodel/reactor/fiber/fiber_tcp_connection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ FiberTcpConnection::FiberTcpConnection(Reactor* reactor, const Socket& socket)
}

FiberTcpConnection::~FiberTcpConnection() {
// Requirements: destroy IO-handler before close socket.
GetIoHandler()->Destroy();
socket_.Close();

TRPC_LOG_DEBUG("~FiberTcpConnection fd:" << socket_.GetFd() << ", conn_id:" << this->GetConnId());
TRPC_ASSERT(!socket_.IsValid());
}
Expand Down Expand Up @@ -77,6 +81,7 @@ bool FiberTcpConnection::DoConnect() {
<< ", is_client:" << IsClient()
<< ", conn_id: " << this->GetConnId() << ", failed.");

GetIoHandler()->Destroy();
socket_.Close();

return false;
Expand Down Expand Up @@ -361,7 +366,7 @@ void FiberTcpConnection::OnCleanup(CleanupReason reason) {

writing_buffers_.Stop();

socket_.Close();
// For multi-threads-safety, move "socket_.Close()" to ~FiberTcpConnection();
}

IoHandler::HandshakeStatus FiberTcpConnection::DoHandshake(bool from_on_readable) {
Expand Down
1 change: 1 addition & 0 deletions trpc/transport/common/ssl/ssl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,7 @@ Ssl::~Ssl() {
if (ssl_) {
SSL_shutdown(ssl_);
SSL_free(ssl_);
ssl_ = nullptr;
}
}

Expand Down
8 changes: 8 additions & 0 deletions trpc/transport/common/ssl_io_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ int SslIoHandler::Writev(const struct iovec* iov, int iovcnt) {
return n;
}

void SslIoHandler::Destroy() {
if (ssl_) {
ssl_->Shutdown();
ssl_ = nullptr;
handshaked_ = false;
}
}

} // namespace trpc::ssl

#endif
2 changes: 2 additions & 0 deletions trpc/transport/common/ssl_io_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class SslIoHandler : public IoHandler {

int Writev(const struct iovec* iov, int iovcnt) override;

void Destroy() override;

private:
Connection* conn_{nullptr};
SslPtr ssl_{nullptr};
Expand Down