Support ipv6 for peer and RPC comms:

Fixes: RIPD-1574

Alias beast address classes to the asio equivalents. Adjust users of
address classes accordingly. Fix resolver class so that it can support
ipv6 addresses. Make unit tests use ipv6 localhost network. Extend
endpoint peer message to support string endpoint
representations while also supporting the existing fields (both are
optional/repeated types). Expand test for Livecache and Endpoint.
Workaround some false positive ipaddr tests on windows (asio bug?)
Replaced usage of address::from_string(deprecated) with free function
make_address. Identified a remaining use of v4 address type and
replaced with the more appropriate IPEndpoint type (rpc_ip cmdline
option). Add CLI flag for using ipv4 with unit tests.

Release Notes
-------------

The optional rpc_port command line flag is deprecated. The rpc_ip
parameter now works as documented and accepts ip and port combined.
This commit is contained in:
Mike Ellery
2017-11-08 10:10:24 -08:00
parent fd4636b056
commit 08382d866b
41 changed files with 968 additions and 1253 deletions

View File

@@ -1048,11 +1048,9 @@ setup_Overlay (BasicConfig const& config)
set (ip, "public_ip", section);
if (! ip.empty ())
{
bool valid;
std::tie (setup.public_ip, valid) =
beast::IP::Address::from_string (ip);
if (! valid || ! setup.public_ip.is_v4() ||
is_private (setup.public_ip))
boost::system::error_code ec;
setup.public_ip = beast::IP::Address::from_string (ip, ec);
if (ec || beast::IP::is_private (setup.public_ip))
Throw<std::runtime_error> ("Configured public IP is invalid");
}
return setup;

View File

@@ -1019,37 +1019,75 @@ PeerImp::onMessage (std::shared_ptr <protocol::TMEndpoints> const& m)
std::vector <PeerFinder::Endpoint> endpoints;
endpoints.reserve (m->endpoints().size());
for (int i = 0; i < m->endpoints ().size (); ++i)
if (m->endpoints_v2().size())
{
PeerFinder::Endpoint endpoint;
protocol::TMEndpoint const& tm (m->endpoints(i));
// hops
endpoint.hops = tm.hops();
// ipv4
if (endpoint.hops > 0)
endpoints.reserve (m->endpoints_v2().size());
for (auto const& tm : m->endpoints_v2 ())
{
in_addr addr;
addr.s_addr = tm.ipv4().ipv4();
beast::IP::AddressV4 v4 (ntohl (addr.s_addr));
endpoint.address = beast::IP::Endpoint (v4, tm.ipv4().ipv4port ());
}
else
{
// This Endpoint describes the peer we are connected to.
// We will take the remote address seen on the socket and
// store that in the IP::Endpoint. If this is the first time,
// then we'll verify that their listener can receive incoming
// by performing a connectivity test.
//
endpoint.address = remote_address_.at_port (
tm.ipv4().ipv4port ());
}
// these endpoint strings support ipv4 and ipv6
auto result = beast::IP::Endpoint::from_string_checked(tm.endpoint());
if (! result.second)
{
JLOG(p_journal_.error()) <<
"failed to parse incoming endpoint: {" <<
tm.endpoint() << "}";
continue;
}
endpoints.push_back (endpoint);
// If hops == 0, this Endpoint describes the peer we are connected
// to -- in that case, we take the remote address seen on the
// socket and store that in the IP::Endpoint. If this is the first
// time, then we'll verify that their listener can receive incoming
// by performing a connectivity test. if hops > 0, then we just
// take the address/port we were given
endpoints.emplace_back(
tm.hops() > 0 ?
result.first :
remote_address_.at_port(result.first.port()),
tm.hops());
JLOG(p_journal_.trace()) <<
"got v2 EP: " << endpoints.back().address <<
", hops = " << endpoints.back().hops;
}
}
else
{
// this branch can be removed once the entire network is operating with
// endpoint_v2() items (strings)
endpoints.reserve (m->endpoints().size());
for (int i = 0; i < m->endpoints ().size (); ++i)
{
PeerFinder::Endpoint endpoint;
protocol::TMEndpoint const& tm (m->endpoints(i));
// hops
endpoint.hops = tm.hops();
// ipv4
if (endpoint.hops > 0)
{
in_addr addr;
addr.s_addr = tm.ipv4().ipv4();
beast::IP::AddressV4 v4 (ntohl (addr.s_addr));
endpoint.address = beast::IP::Endpoint (v4, tm.ipv4().ipv4port ());
}
else
{
// This Endpoint describes the peer we are connected to.
// We will take the remote address seen on the socket and
// store that in the IP::Endpoint. If this is the first time,
// then we'll verify that their listener can receive incoming
// by performing a connectivity test.
//
endpoint.address = remote_address_.at_port (
tm.ipv4().ipv4port ());
}
endpoints.push_back (endpoint);
JLOG(p_journal_.trace()) <<
"got v1 EP: " << endpoints.back().address <<
", hops = " << endpoints.back().hops;
}
}
if (! endpoints.empty())

View File

@@ -514,16 +514,24 @@ PeerImp::sendEndpoints (FwdIt first, FwdIt last)
for (;first != last; ++first)
{
auto const& ep = *first;
// eventually remove endpoints and just keep endpoints_v2
// (once we are sure the entire network understands endpoints_v2)
protocol::TMEndpoint& tme (*tm.add_endpoints());
if (ep.address.is_v4())
tme.mutable_ipv4()->set_ipv4(
beast::toNetworkByteOrder (ep.address.to_v4().value));
beast::toNetworkByteOrder<std::uint32_t> (
ep.address.to_v4().to_ulong()));
else
tme.mutable_ipv4()->set_ipv4(0);
tme.mutable_ipv4()->set_ipv4port (ep.address.port());
tme.set_hops (ep.hops);
// add v2 endpoints (strings)
auto& tme2 (*tm.add_endpoints_v2());
tme2.set_endpoint(ep.address.to_string());
tme2.set_hops (ep.hops);
}
tm.set_version (1);
tm.set_version (2);
send (std::make_shared <Message> (tm, protocol::mtENDPOINTS));
}

View File

@@ -114,19 +114,14 @@ buildHello (
TokenType::NodePublic,
app.nodeIdentity().first));
h.set_nodeproof (sig.data(), sig.size());
// h.set_ipv4port (portNumber); // ignored now
h.set_testnet (false);
if (remote.is_v4())
if (beast::IP::is_public (remote))
{
auto addr = remote.to_v4 ();
if (is_public (addr))
{
// Connection is to a public IP
h.set_remote_ip (addr.value);
if (public_ip != beast::IP::Address())
h.set_local_ip (public_ip.to_v4().value);
}
// Connection is to a public IP
h.set_remote_ip_str (remote.to_string());
if (! public_ip.is_unspecified())
h.set_local_ip_str (public_ip.to_string());
}
// We always advertise ourselves as private in the HELLO message. This
@@ -174,13 +169,11 @@ appendHello (boost::beast::http::fields& h,
h.insert ("Previous-Ledger", boost::beast::detail::base64_encode (
hello.ledgerprevious()));
if (hello.has_local_ip())
h.insert ("Local-IP", beast::IP::to_string (
beast::IP::AddressV4(hello.local_ip())));
if (hello.has_local_ip_str())
h.insert ("Local-IP", hello.local_ip_str());
if (hello.has_remote_ip())
h.insert ("Remote-IP", beast::IP::to_string (
beast::IP::AddressV4(hello.remote_ip())));
h.insert ("Remote-IP", hello.remote_ip_str());
}
std::vector<ProtocolVersion>
@@ -306,14 +299,16 @@ parseHello (bool request, boost::beast::http::fields const& h, beast::Journal jo
auto const iter = h.find ("Local-IP");
if (iter != h.end())
{
bool valid;
beast::IP::Address address;
std::tie (address, valid) =
beast::IP::Address::from_string (iter->value().to_string());
if (!valid)
boost::system::error_code ec;
auto address =
beast::IP::Address::from_string (iter->value().to_string(), ec);
if (ec)
{
JLOG(journal.warn()) << "invalid Local-IP: "
<< iter->value().to_string();
return boost::none;
if (address.is_v4())
hello.set_local_ip(address.to_v4().value);
}
hello.set_local_ip_str(address.to_string());
}
}
@@ -321,14 +316,16 @@ parseHello (bool request, boost::beast::http::fields const& h, beast::Journal jo
auto const iter = h.find ("Remote-IP");
if (iter != h.end())
{
bool valid;
beast::IP::Address address;
std::tie (address, valid) =
beast::IP::Address::from_string (iter->value().to_string());
if (!valid)
boost::system::error_code ec;
auto address =
beast::IP::Address::from_string (iter->value().to_string(), ec);
if (ec)
{
JLOG(journal.warn()) << "invalid Remote-IP: "
<< iter->value().to_string();
return boost::none;
if (address.is_v4())
hello.set_remote_ip(address.to_v4().value);
}
hello.set_remote_ip_str(address.to_string());
}
}
@@ -412,33 +409,50 @@ verifyHello (protocol::TMHello const& h,
return boost::none;
}
if (h.has_local_ip () &&
is_public (remote) &&
remote.is_v4 () &&
(remote.to_v4().value != h.local_ip ()))
if (h.has_local_ip_str () &&
is_public (remote))
{
// Remote asked us to confirm connection is from
// correct IP
JLOG(journal.info()) <<
"Hello: Disconnect: Peer IP is " <<
beast::IP::to_string (remote.to_v4())
<< " not " <<
beast::IP::to_string (beast::IP::AddressV4 (h.local_ip()));
return boost::none;
boost::system::error_code ec;
auto local_ip =
beast::IP::Address::from_string (h.local_ip_str(), ec);
if (ec)
{
JLOG(journal.warn()) << "invalid local-ip: " << h.local_ip_str();
return boost::none;
}
if (remote.address() != local_ip)
{
// Remote asked us to confirm connection is from correct IP
JLOG(journal.info()) <<
"Hello: Disconnect: Peer IP is " << remote.address().to_string()
<< " not " << local_ip.to_string();
return boost::none;
}
}
if (h.has_remote_ip() && is_public (remote) &&
(public_ip != beast::IP::Address()) &&
(h.remote_ip() != public_ip.to_v4().value))
if (h.has_remote_ip_str () &&
is_public (remote) &&
(! beast::IP::is_unspecified(public_ip)))
{
// We know our public IP and peer reports connection
// from some other IP
JLOG(journal.info()) <<
"Hello: Disconnect: Our IP is " <<
beast::IP::to_string (public_ip.to_v4())
<< " not " <<
beast::IP::to_string (beast::IP::AddressV4 (h.remote_ip()));
return boost::none;
boost::system::error_code ec;
auto remote_ip =
beast::IP::Address::from_string (h.remote_ip_str(), ec);
if (ec)
{
JLOG(journal.warn()) << "invalid remote-ip: " << h.remote_ip_str();
return boost::none;
}
if (remote_ip != public_ip)
{
// We know our public IP and peer reports connection from some
// other IP
JLOG(journal.info()) <<
"Hello: Disconnect: Our IP is " << public_ip.to_string()
<< " not " << remote_ip.to_string();
return boost::none;
}
}
return publicKey;