Browse Source

PoC-7: IPv6.

cl-refactor
Gav Wood 10 years ago
parent
commit
784ea95f27
  1. 13
      libp2p/Common.cpp
  2. 2
      libp2p/Common.h
  3. 16
      libp2p/Session.cpp

13
libp2p/Common.cpp

@ -27,8 +27,7 @@ using namespace dev::p2p;
// Helper function to determine if an address falls within one of the reserved ranges
// For V4:
// Class A "10.*", Class B "172.[16->31].*", Class C "192.168.*"
// Not implemented yet for V6
bool p2p::isPrivateAddress(bi::address _addressToCheck)
bool p2p::isPrivateAddress(bi::address const& _addressToCheck)
{
if (_addressToCheck.is_v4())
{
@ -41,6 +40,16 @@ bool p2p::isPrivateAddress(bi::address _addressToCheck)
if (bytesToCheck[0] == 192 && bytesToCheck[1] == 168)
return true;
}
else if (_addressToCheck.is_v6())
{
bi::address_v6 v6Address = _addressToCheck.to_v6();
bi::address_v6::bytes_type bytesToCheck = v6Address.to_bytes();
if (bytesToCheck[0] == 0xfd && bytesToCheck[1] == 0)
return true;
if (!bytesToCheck[0] && !bytesToCheck[1] && !bytesToCheck[2] && !bytesToCheck[3] && !bytesToCheck[4] && !bytesToCheck[5] && !bytesToCheck[6] && !bytesToCheck[7]
&& !bytesToCheck[8] && !bytesToCheck[9] && !bytesToCheck[10] && !bytesToCheck[11] && !bytesToCheck[12] && !bytesToCheck[13] && !bytesToCheck[14] && (bytesToCheck[15] == 0 || bytesToCheck[15] == 1))
return true;
}
return false;
}

2
libp2p/Common.h

@ -44,7 +44,7 @@ class RLPStream;
namespace p2p
{
bool isPrivateAddress(bi::address _addressToCheck);
bool isPrivateAddress(bi::address const& _addressToCheck);
class UPnP;
class Capability;

16
libp2p/Session.cpp

@ -146,7 +146,10 @@ bool Session::interpret(RLP const& _r)
for (auto i: peers)
{
clogS(NetTriviaDetail) << "Sending peer " << i.first.abridged() << i.second;
s.appendList(3) << bytesConstRef(i.second.address().to_v4().to_bytes().data(), 4) << i.second.port() << i.first;
if (i.second.address().is_v4())
s.appendList(3) << bytesConstRef(i.second.address().to_v4().to_bytes().data(), 4) << i.second.port() << i.first;
else// if (i.second.address().is_v6()) - assumed
s.appendList(3) << bytesConstRef(i.second.address().to_v6().to_bytes().data(), 16) << i.second.port() << i.first;
}
sealAndSend(s);
break;
@ -155,7 +158,16 @@ bool Session::interpret(RLP const& _r)
clogS(NetTriviaSummary) << "Peers (" << dec << (_r.itemCount() - 1) << " entries)";
for (unsigned i = 1; i < _r.itemCount(); ++i)
{
bi::address_v4 peerAddress(_r[i][0].toHash<FixedHash<4>>().asArray());
bi::address peerAddress;
if (_r[i][0].size() == 16)
peerAddress = bi::address_v6(_r[i][0].toHash<FixedHash<16>>().asArray());
else if (_r[i][0].size() == 4)
peerAddress = bi::address_v4(_r[i][0].toHash<FixedHash<4>>().asArray());
else
{
disconnect(BadProtocol);
return false;
}
auto ep = bi::tcp::endpoint(peerAddress, _r[i][1].toInt<short>());
h512 id = _r[i][2].toHash<h512>();
clogS(NetAllDetail) << "Checking: " << ep << "(" << id.abridged() << ")" << isPrivateAddress(peerAddress) << m_id.abridged() << isPrivateAddress(endpoint().address()) << m_server->m_incomingPeers.count(id) << (m_server->m_incomingPeers.count(id) ? isPrivateAddress(m_server->m_incomingPeers.at(id).first.address()) : -1);

Loading…
Cancel
Save