Skip to content

Commit

Permalink
-- prefer IPv4 before IPv6 during connections to other peers
Browse files Browse the repository at this point in the history
  • Loading branch information
VDanielEdwards committed Nov 20, 2023
1 parent 358d613 commit e7a4a38
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions SilKit/source/core/vasio/ConnectPeer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ void ConnectPeer::Shutdown()
}


static auto IsIp4(const std::string& address) -> bool
{
return (address.find('.') != std::string::npos) && (address.find(':') == std::string::npos);
}

static auto IsIp6(const std::string& address) -> bool
{
return (address.find('.') == std::string::npos) && (address.find(':') != std::string::npos);
}


void ConnectPeer::UpdateUris()
{
std::vector<Uri> acceptorUris;
Expand All @@ -89,8 +100,20 @@ void ConnectPeer::UpdateUris()
{
// resolve the host part in tcp:// URIs

for (const auto& address : _ioContext->Resolve(uri.Host()))
for (std::string address : _ioContext->Resolve(uri.Host()))
{
if (IsIp6(address))
{
if (address.front() != '[')
{
address.insert(0, "[");
}
if (address.back() != ']')
{
address.push_back(']');
}
}

auto tcpUri{Uri::MakeTcp(address, uri.Port())};
acceptorUris.emplace_back(std::move(tcpUri));
}
Expand Down Expand Up @@ -118,9 +141,17 @@ void ConnectPeer::UpdateUris()
case Uri::UriType::Local:
return 100;
case Uri::UriType::Tcp:
return 200;
if (IsIp4(uri.Host()))
{
return 200;
}
if (IsIp6(uri.Host()))
{
return 300;
}
return 400;
default:
return 300;
return 500;
}
}};

Expand Down

0 comments on commit e7a4a38

Please sign in to comment.