Browse Source

Const frame info. Typo. Const arguments.

cl-refactor
subtly 10 years ago
parent
commit
4f02a4fee8
  1. 25
      libp2p/RLPXFrameCoder.cpp
  2. 22
      libp2p/RLPXFrameCoder.h
  3. 2
      libp2p/RLPXFrameReader.cpp
  4. 18
      libp2p/Session.cpp

25
libp2p/RLPXFrameCoder.cpp

@ -30,29 +30,28 @@ using namespace dev;
using namespace dev::p2p;
using namespace CryptoPP;
RLPXFrameInfo::RLPXFrameInfo(bytesConstRef _header)
{
length = (_header[0] * 256 + _header[1]) * 256 + _header[2];
padding = ((16 - (length % 16)) % 16);
RLP header(_header.cropped(3), RLP::ThrowOnFail | RLP::FailIfTooSmall);
auto itemCount = header.itemCount();
protocolId = header[0].toInt<uint16_t>();
multiFrame = itemCount > 1;
sequenceId = multiFrame ? header[1].toInt<uint16_t>() : 0;
totalLength = itemCount == 3 ? header[2].toInt<uint32_t>() : 0;
}
RLPXFrameInfo::RLPXFrameInfo(bytesConstRef _header):
length((_header[0] * 256 + _header[1]) * 256 + _header[2]),
padding((16 - (length % 16)) % 16),
data(_header.cropped(3).toBytes()),
header(RLP(data, RLP::ThrowOnFail | RLP::FailIfTooSmall)),
protocolId(header[0].toInt<uint16_t>()),
multiFrame(header.itemCount() > 1),
sequenceId(multiFrame ? header[1].toInt<uint16_t>() : 0),
totalLength(header.itemCount() == 3 ? header[2].toInt<uint32_t>() : 0)
{}
RLPXFrameCoder::RLPXFrameCoder(RLPXHandshake const& _init)
{
setup(_init.m_originated, _init.m_remoteEphemeral, _init.m_remoteNonce, _init.m_ecdhe, _init.m_nonce, &_init.m_ackCipher, &_init.m_authCipher);
}
RLPXFrameCoder::RLPXFrameCoder(bool _originated, h512 _remoteEphemeral, h256 _remoteNonce, crypto::ECDHE const& _ecdhe, h256 _nonce, bytesConstRef _ackCipher, bytesConstRef _authCipher)
RLPXFrameCoder::RLPXFrameCoder(bool _originated, h512 const& _remoteEphemeral, h256 const& _remoteNonce, crypto::ECDHE const& _ecdhe, h256 const& _nonce, bytesConstRef _ackCipher, bytesConstRef _authCipher)
{
setup(_originated, _remoteEphemeral, _remoteNonce, _ecdhe, _nonce, _ackCipher, _authCipher);
}
void RLPXFrameCoder::setup(bool _originated, h512 _remoteEphemeral, h256 _remoteNonce, crypto::ECDHE const& _ecdhe, h256 _nonce, bytesConstRef _ackCipher, bytesConstRef _authCipher)
void RLPXFrameCoder::setup(bool _originated, h512 const& _remoteEphemeral, h256 const& _remoteNonce, crypto::ECDHE const& _ecdhe, h256 const& _nonce, bytesConstRef _ackCipher, bytesConstRef _authCipher)
{
bytes keyMaterialBytes(64);
bytesRef keyMaterial(&keyMaterialBytes);

22
libp2p/RLPXFrameCoder.h

@ -33,7 +33,7 @@ namespace dev
namespace p2p
{
struct RLPXFrameDecrytFailed: virtual dev::Exception {};
struct RLPXFrameDecryptFailed: virtual dev::Exception {};
/**
* @brief Encapsulation of Frame
@ -44,13 +44,17 @@ struct RLPXFrameInfo
RLPXFrameInfo() = default;
/// Constructor. frame-size || protocol-type, [sequence-id[, total-packet-size]]
RLPXFrameInfo(bytesConstRef _frameHeader);
uint32_t length = 0; ///< Size of frame (excludes padding). Max: 2**24
uint8_t padding = 0; ///< Length of padding which follows @length.
uint32_t const length; ///< Size of frame (excludes padding). Max: 2**24
uint8_t const padding; ///< Length of padding which follows @length.
bytes const data; ///< Bytes of Header.
RLP const header; ///< Header RLP.
uint16_t protocolId = 0; ///< Protocol ID as negotiated by handshake.
bool multiFrame = false; ///< If this frame is part of a sequence
uint16_t sequenceId = 0; ///< Sequence ID of frame
uint32_t totalLength = 0; ///< Set to total length of packet in first frame of multiframe packet
uint16_t const protocolId; ///< Protocol ID as negotiated by handshake.
bool const multiFrame; ///< If this frame is part of a sequence
uint16_t const sequenceId; ///< Sequence ID of frame
uint32_t const totalLength; ///< Set to total length of packet in first frame of multiframe packet
};
class RLPXHandshake;
@ -75,12 +79,12 @@ public:
RLPXFrameCoder(RLPXHandshake const& _init);
/// Construct with external key material.
RLPXFrameCoder(bool _originated, h512 _remoteEphemeral, h256 _remoteNonce, crypto::ECDHE const& _ephemeral, h256 _nonce, bytesConstRef _ackCipher, bytesConstRef _authCipher);
RLPXFrameCoder(bool _originated, h512 const& _remoteEphemeral, h256 const& _remoteNonce, crypto::ECDHE const& _ephemeral, h256 const& _nonce, bytesConstRef _ackCipher, bytesConstRef _authCipher);
~RLPXFrameCoder() {}
/// Establish shared secrets and setup AES and MAC states.
void setup(bool _originated, h512 _remoteEphemeral, h256 _remoteNonce, crypto::ECDHE const& _ephemeral, h256 _nonce, bytesConstRef _ackCipher, bytesConstRef _authCipher);
void setup(bool _originated, h512 const& _remoteEphemeral, h256 const& _remoteNonce, crypto::ECDHE const& _ephemeral, h256 const& _nonce, bytesConstRef _ackCipher, bytesConstRef _authCipher);
/// Write single-frame payload of packet(s).
void writeFrame(uint16_t _protocolType, bytesConstRef _payload, bytes& o_bytes);

2
libp2p/RLPXFrameReader.cpp

@ -28,7 +28,7 @@ using namespace dev::p2p;
std::vector<RLPXPacket> RLPXFrameReader::demux(RLPXFrameCoder& _coder, RLPXFrameInfo const& _info, bytesRef _frame)
{
if (!_coder.authAndDecryptFrame(_frame))
BOOST_THROW_EXCEPTION(RLPXFrameDecrytFailed());
BOOST_THROW_EXCEPTION(RLPXFrameDecryptFailed());
std::vector<RLPXPacket> ret;
if (_frame.empty())

18
libp2p/Session.cpp

@ -375,10 +375,16 @@ void Session::doRead()
return;
}
RLPXFrameInfo header;
uint16_t hProtocolId;
uint32_t hLength;
uint8_t hPadding;
try
{
header = RLPXFrameInfo(bytesConstRef(m_data.data(), length));
RLPXFrameInfo header(bytesConstRef(m_data.data(), length));
hProtocolId = header.protocolId;
hLength = header.length;
hPadding = header.padding;
}
catch (std::exception const& _e)
{
@ -388,8 +394,8 @@ void Session::doRead()
}
/// read padded frame and mac
auto tlen = header.length + header.padding + h128::size;
ba::async_read(m_socket->ref(), boost::asio::buffer(m_data, tlen), [this, self, header, tlen](boost::system::error_code ec, std::size_t length)
auto tlen = hLength + hPadding + h128::size;
ba::async_read(m_socket->ref(), boost::asio::buffer(m_data, tlen), [this, self, hLength, hProtocolId, tlen](boost::system::error_code ec, std::size_t length)
{
ThreadContext tc(info().id.abridged());
ThreadContext tc2(info().clientVersion);
@ -402,7 +408,7 @@ void Session::doRead()
return;
}
bytesConstRef frame(m_data.data(), header.length);
bytesConstRef frame(m_data.data(), hLength);
if (!checkPacket(frame))
{
cerr << "Received " << frame.size() << ": " << toHex(frame) << endl;
@ -414,7 +420,7 @@ void Session::doRead()
{
auto packetType = (PacketType)RLP(frame.cropped(0, 1)).toInt<unsigned>();
RLP r(frame.cropped(1));
if (!readPacket(header.protocolId, packetType, r))
if (!readPacket(hProtocolId, packetType, r))
clog(NetWarn) << "Couldn't interpret packet." << RLP(r);
}
doRead();

Loading…
Cancel
Save