From 4f02a4fee8ba30fb1bbf131736e010f0562dd0c8 Mon Sep 17 00:00:00 2001 From: subtly Date: Fri, 10 Jul 2015 18:05:02 -0700 Subject: [PATCH] Const frame info. Typo. Const arguments. --- libp2p/RLPXFrameCoder.cpp | 25 ++++++++++++------------- libp2p/RLPXFrameCoder.h | 22 +++++++++++++--------- libp2p/RLPXFrameReader.cpp | 2 +- libp2p/Session.cpp | 18 ++++++++++++------ 4 files changed, 38 insertions(+), 29 deletions(-) diff --git a/libp2p/RLPXFrameCoder.cpp b/libp2p/RLPXFrameCoder.cpp index dd17cbebc..83dc62409 100644 --- a/libp2p/RLPXFrameCoder.cpp +++ b/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(); - multiFrame = itemCount > 1; - sequenceId = multiFrame ? header[1].toInt() : 0; - totalLength = itemCount == 3 ? header[2].toInt() : 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()), + multiFrame(header.itemCount() > 1), + sequenceId(multiFrame ? header[1].toInt() : 0), + totalLength(header.itemCount() == 3 ? header[2].toInt() : 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); diff --git a/libp2p/RLPXFrameCoder.h b/libp2p/RLPXFrameCoder.h index 9266da459..19c648cd5 100644 --- a/libp2p/RLPXFrameCoder.h +++ b/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); diff --git a/libp2p/RLPXFrameReader.cpp b/libp2p/RLPXFrameReader.cpp index b37ffa796..68a329ad6 100644 --- a/libp2p/RLPXFrameReader.cpp +++ b/libp2p/RLPXFrameReader.cpp @@ -28,7 +28,7 @@ using namespace dev::p2p; std::vector RLPXFrameReader::demux(RLPXFrameCoder& _coder, RLPXFrameInfo const& _info, bytesRef _frame) { if (!_coder.authAndDecryptFrame(_frame)) - BOOST_THROW_EXCEPTION(RLPXFrameDecrytFailed()); + BOOST_THROW_EXCEPTION(RLPXFrameDecryptFailed()); std::vector ret; if (_frame.empty()) diff --git a/libp2p/Session.cpp b/libp2p/Session.cpp index 9a981786d..0d90ea6ea 100644 --- a/libp2p/Session.cpp +++ b/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(); 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();