Skip to content

Commit

Permalink
Fix address converting to string.
Browse files Browse the repository at this point in the history
  • Loading branch information
dchapyshev committed Nov 11, 2023
1 parent bd8dd3c commit 1a4cd32
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions source/base/net/tcp_channel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,34 @@ std::u16string TcpChannel::peerAddress() const
if (!socket_.is_open())
return std::u16string();

asio::ip::address address = socket_.remote_endpoint().address();
if (address.is_v4())
try
{
asio::ip::address ipv4_address = address.to_v4();
return utf16FromLocal8Bit(ipv4_address.to_string());
asio::ip::address address = socket_.remote_endpoint().address();
if (address.is_v4())
{
asio::ip::address_v4 ipv4_address = address.to_v4();
return utf16FromLocal8Bit(ipv4_address.to_string());
}
else
{
asio::ip::address_v6 ipv6_address = address.to_v6();
if (ipv6_address.is_v4_mapped())
{
asio::ip::address_v4 ipv4_address =
asio::ip::make_address_v4(asio::ip::v4_mapped, ipv6_address);
return utf16FromLocal8Bit(ipv4_address.to_string());
}
else
{
return utf16FromLocal8Bit(ipv6_address.to_string());
}
}
}
else
catch (const std::error_code& error_code)
{
asio::ip::address ipv6_address = address.to_v6();
return utf16FromLocal8Bit(ipv6_address.to_string());
LOG(LS_ERROR) << "Unable to get peer address: "
<< base::utf16FromLocal8Bit(error_code.message());
return std::u16string();
}
}

Expand Down

0 comments on commit 1a4cd32

Please sign in to comment.