diff --git a/source/base/net/tcp_channel.cc b/source/base/net/tcp_channel.cc index a6661e6c6..703e09380 100644 --- a/source/base/net/tcp_channel.cc +++ b/source/base/net/tcp_channel.cc @@ -227,12 +227,21 @@ void TcpChannel::setDecryptor(std::unique_ptr decryptor) //-------------------------------------------------------------------------------------------------- std::u16string TcpChannel::peerAddress() const { - if (!socket_.is_open()) + if (!socket_.is_open() || !isConnected()) return std::u16string(); try { - asio::ip::address address = socket_.remote_endpoint().address(); + std::error_code error_code; + asio::ip::tcp::endpoint endpoint = socket_.remote_endpoint(error_code); + if (error_code) + { + LOG(LS_ERROR) << "Unable to get peer address: " + << base::utf16FromLocal8Bit(error_code.message()); + return std::u16string(); + } + + asio::ip::address address = endpoint.address(); if (address.is_v4()) { asio::ip::address_v4 ipv4_address = address.to_v4(); diff --git a/source/relay/pending_session.cc b/source/relay/pending_session.cc index 8e854b658..037ab1448 100644 --- a/source/relay/pending_session.cc +++ b/source/relay/pending_session.cc @@ -42,7 +42,25 @@ PendingSession::PendingSession(std::shared_ptr task_runner, timer_(base::WaitableTimer::Type::SINGLE_SHOT, std::move(task_runner)), socket_(std::move(socket)) { - address_ = socket_.remote_endpoint().address().to_string(); + try + { + std::error_code error_code; + asio::ip::tcp::endpoint endpoint = socket_.remote_endpoint(error_code); + if (error_code) + { + LOG(LS_ERROR) << "Unable to get endpoint for pending session: " + << base::utf16FromLocal8Bit(error_code.message()); + } + else + { + address_ = endpoint.address().to_string(); + } + } + catch (const std::error_code& error_code) + { + LOG(LS_ERROR) << "Unable to get address for pending session: " + << base::utf16FromLocal8Bit(error_code.message()); + } } //-------------------------------------------------------------------------------------------------- diff --git a/source/relay/session_manager.cc b/source/relay/session_manager.cc index c913f1051..d0c3dd4ad 100644 --- a/source/relay/session_manager.cc +++ b/source/relay/session_manager.cc @@ -94,6 +94,31 @@ std::unique_ptr removeSessionT(std::vector>* session_list, return nullptr; } +std::u16string peerAddress(const asio::ip::tcp::socket& socket) +{ + try + { + std::error_code error_code; + asio::ip::tcp::endpoint endpoint = socket.remote_endpoint(error_code); + if (error_code) + { + LOG(LS_ERROR) << "Unable to get endpoint for accepted connection: " + << base::utf16FromLocal8Bit(error_code.message()); + } + else + { + return base::utf16FromLocal8Bit(endpoint.address().to_string()); + } + } + catch (const std::error_code& error_code) + { + LOG(LS_ERROR) << "Unable to get address for pending session: " + << base::utf16FromLocal8Bit(error_code.message()); + } + + return std::u16string(); +} + } // namespace //-------------------------------------------------------------------------------------------------- @@ -284,8 +309,7 @@ void SessionManager::doAccept(SessionManager* self) { if (!error_code) { - LOG(LS_INFO) << "New accepted connection: " << base::utf16FromLocal8Bit( - socket.remote_endpoint().address().to_string()); + LOG(LS_INFO) << "New accepted connection: " << peerAddress(socket); // A new peer is connected. Create and start the pending session. self->pending_sessions_.emplace_back(std::make_unique(