Fabcoin Core  0.16.2
P2P Digital Currency
RLPXPacket.h
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16  */
22 #pragma once
23 
24 #include <algorithm>
25 #include "Common.h"
26 
27 namespace dev
28 {
29 namespace p2p
30 {
31 
32 struct RLPXInvalidPacket: virtual dev::Exception {};
33 
34 static bytesConstRef nextRLP(bytesConstRef _b) { try { RLP r(_b, RLP::AllowNonCanon); return _b.cropped(0, std::min((size_t)r.actualSize(), _b.size())); } catch(...) {} return bytesConstRef(); }
35 
40 {
41 public:
43  RLPXPacket(uint8_t _capId, RLPStream& _type, RLPStream& _data): m_cap(_capId), m_type(_type.out()), m_data(_data.out()) {}
44 
46  RLPXPacket(uint8_t _capId, bytesConstRef _in): m_cap(_capId), m_type(nextRLP(_in).toBytes()) { if (_in.size() > m_type.size()) { m_data.resize(_in.size() - m_type.size()); _in.cropped(m_type.size()).copyTo(&m_data); } }
47 
48  RLPXPacket(RLPXPacket const& _p) = delete;
49  RLPXPacket(RLPXPacket&& _p): m_cap(_p.m_cap), m_type(_p.m_type), m_data(_p.m_data) {}
50 
51  bytes const& type() const { return m_type; }
52 
53  bytes const& data() const { return m_data; }
54 
55  uint8_t cap() const { return m_cap; }
56 
57  size_t size() const { try { return RLP(m_type).actualSize() + RLP(m_data, RLP::LaissezFaire).actualSize(); } catch(...) { return 0; } }
58 
60  bool append(bytesConstRef _in) { auto offset = m_data.size(); m_data.resize(offset + _in.size()); _in.copyTo(bytesRef(&m_data).cropped(offset)); return isValid(); }
61 
62  virtual bool isValid() const noexcept { try { return !(m_type.empty() && m_data.empty()) && RLP(m_type).actualSize() == m_type.size() && RLP(m_data).actualSize() == m_data.size(); } catch (...) {} return false; }
63 
64 protected:
65  uint8_t m_cap;
68 };
69 
70 }
71 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
size_t size() const
Definition: RLPXPacket.h:57
virtual bool isValid() const noexcept
Definition: RLPXPacket.h:62
RLPXPacket(uint8_t _capId, RLPStream &_type, RLPStream &_data)
Construct packet. RLPStream data is invalidated.
Definition: RLPXPacket.h:43
uint8_t cap() const
Definition: RLPXPacket.h:55
bool append(bytesConstRef _in)
Appends byte data and returns if packet is valid.
Definition: RLPXPacket.h:60
bytes const & data() const
Definition: RLPXPacket.h:53
RLPXPacket(RLPXPacket &&_p)
Definition: RLPXPacket.h:49
vector_ref< _T > cropped(size_t _begin, size_t _count) const
Definition: vector_ref.h:62
ExecStats::duration min
Definition: ExecStats.cpp:35
Base class for all exceptions.
Definition: Exceptions.h:39
vector_ref< byte > bytesRef
Definition: Common.h:76
std::vector< byte > bytes
Definition: Common.h:75
vector_ref< byte const > bytesConstRef
Definition: Common.h:77
size_t size() const
Definition: vector_ref.h:55
RLPX Packet.
Definition: RLPXPacket.h:39
bytes const & type() const
Definition: RLPXPacket.h:51
size_t actualSize() const
Definition: RLP.cpp:108
void copyTo(vector_ref< typename std::remove_const< _T >::type > _t) const
Copies the contents of this vector_ref to the contents of _t, up to the max size of _t...
Definition: vector_ref.h:69
RLPXPacket(uint8_t _capId, bytesConstRef _in)
Construct packet from single bytestream. RLPStream data is invalidated.
Definition: RLPXPacket.h:46
Class for writing to an RLP bytestream.
Definition: RLP.h:383
Class for interpreting Recursive Linear-Prefix Data.
Definition: RLP.h:64