Fabcoin Core  0.16.2
P2P Digital Currency
Session.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 */
23 #pragma once
24 
25 #include <mutex>
26 #include <array>
27 #include <deque>
28 #include <set>
29 #include <memory>
30 #include <utility>
31 
32 #include <libdevcore/Common.h>
33 #include <libdevcore/RLP.h>
34 #include <libdevcore/Guards.h>
35 #include "RLPXFrameCoder.h"
36 #include "RLPXSocket.h"
37 #include "Common.h"
38 #include "RLPXFrameWriter.h"
39 #include "RLPXFrameReader.h"
40 
41 namespace dev
42 {
43 
44 namespace p2p
45 {
46 
47 class Peer;
48 class ReputationManager;
49 
51 {
52 public:
53  virtual ~SessionFace() {}
54 
55  virtual void start() = 0;
56  virtual void disconnect(DisconnectReason _reason) = 0;
57 
58  virtual void ping() = 0;
59 
60  virtual bool isConnected() const = 0;
61 
62  virtual NodeID id() const = 0;
63 
64  virtual void sealAndSend(RLPStream& _s, uint16_t _protocolID) = 0;
65 
66  virtual int rating() const = 0;
67  virtual void addRating(int _r) = 0;
68 
69  virtual void addNote(std::string const& _k, std::string const& _v) = 0;
70 
71  virtual PeerSessionInfo info() const = 0;
73 
74  virtual void registerCapability(CapDesc const& _desc, std::shared_ptr<Capability> _p) = 0;
75  virtual void registerFraming(uint16_t _id) = 0;
76 
77  virtual std::map<CapDesc, std::shared_ptr<Capability>> const& capabilities() const = 0;
78 
79  virtual std::shared_ptr<Peer> peer() const = 0;
80 
82 
83  virtual ReputationManager& repMan() = 0;
84 };
85 
90 class Session: public SessionFace, public std::enable_shared_from_this<SessionFace>
91 {
92 public:
93  static bool isFramingAllowedForVersion(unsigned _version) { return _version > 4; }
94 
95  Session(Host* _server, std::unique_ptr<RLPXFrameCoder>&& _io, std::shared_ptr<RLPXSocket> const& _s, std::shared_ptr<Peer> const& _n, PeerSessionInfo _info);
96  virtual ~Session();
97 
98  void start() override;
99  void disconnect(DisconnectReason _reason) override;
100 
101  void ping() override;
102 
103  bool isConnected() const override { return m_socket->ref().is_open(); }
104 
105  NodeID id() const override;
106 
107  void sealAndSend(RLPStream& _s, uint16_t _protocolID) override;
108 
109  int rating() const override;
110  void addRating(int _r) override;
111 
112  void addNote(std::string const& _k, std::string const& _v) override { Guard l(x_info); m_info.notes[_k] = _v; }
113 
114  PeerSessionInfo info() const override { Guard l(x_info); return m_info; }
115  std::chrono::steady_clock::time_point connectionTime() override { return m_connect; }
116 
117  void registerCapability(CapDesc const& _desc, std::shared_ptr<Capability> _p) override;
118  void registerFraming(uint16_t _id) override;
119 
120  std::map<CapDesc, std::shared_ptr<Capability>> const& capabilities() const override { return m_capabilities; }
121 
122  std::shared_ptr<Peer> peer() const override { return m_peer; }
123 
124  std::chrono::steady_clock::time_point lastReceived() const override { return m_lastReceived; }
125 
126  ReputationManager& repMan() override;
127 
128 private:
129  static RLPStream& prep(RLPStream& _s, PacketType _t, unsigned _args = 0);
130 
131  void send(bytes&& _msg, uint16_t _protocolID);
132 
134  void drop(DisconnectReason _r);
135 
137  void doRead();
138  void doReadFrames();
139 
141  bool checkRead(std::size_t _expected, boost::system::error_code _ec, std::size_t _length);
142 
144  void write();
145  void writeFrames();
146 
148  bool readPacket(uint16_t _capId, PacketType _t, RLP const& _r);
149 
151  bool interpret(PacketType _t, RLP const& _r);
152 
154  static bool checkPacket(bytesConstRef _msg);
155 
157 
158  std::unique_ptr<RLPXFrameCoder> m_io;
159  std::shared_ptr<RLPXSocket> m_socket;
161  std::deque<bytes> m_writeQueue;
162  std::vector<byte> m_data;
164 
165  std::shared_ptr<Peer> m_peer;
166  bool m_dropped = false;
167 
168  mutable Mutex x_info;
170 
174 
175  std::map<CapDesc, std::shared_ptr<Capability>> m_capabilities;
176 
177  // framing-related stuff (protected by x_writeQueue mutex)
178  struct Framing
179  {
180  Framing() = delete;
181  Framing(uint16_t _protocolID): writer(_protocolID), reader(_protocolID) {}
184  };
185 
186  std::map<uint16_t, std::shared_ptr<Framing> > m_framing;
187  std::deque<bytes> m_encFrames;
188 
189  bool isFramingEnabled() const { return isFramingAllowedForVersion(m_info.protocolVersion); }
190  unsigned maxFrameSize() const { return 1024; }
191  std::shared_ptr<Framing> getFraming(uint16_t _protocolID);
192  void multiplexAll();
193 };
194 
195 template <class PeerCap>
196 std::shared_ptr<PeerCap> capabilityFromSession(SessionFace const& _session, u256 const& _version = PeerCap::version())
197 {
198  try
199  {
200  return std::static_pointer_cast<PeerCap>(_session.capabilities().at(std::make_pair(PeerCap::name(), _version)));
201  }
202  catch (...)
203  {
204  return nullptr;
205  }
206 }
207 
208 }
209 }
std::unique_ptr< RLPXFrameCoder > m_io
Transport over which packets are sent.
Definition: Session.h:158
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
unsigned maxFrameSize() const
Definition: Session.h:190
bool isFramingEnabled() const
Definition: Session.h:189
std::map< uint16_t, std::shared_ptr< Framing > > m_framing
Definition: Session.h:186
virtual bool isConnected() const =0
bool isConnected() const override
Definition: Session.h:103
virtual std::chrono::steady_clock::time_point lastReceived() const =0
std::chrono::steady_clock::time_point connectionTime() override
Definition: Session.h:115
Mutex x_framing
Mutex for the write queue.
Definition: Session.h:160
std::pair< std::string, u256 > CapDesc
Definition: Common.h:142
virtual void registerCapability(CapDesc const &_desc, std::shared_ptr< Capability > _p)=0
static bool isFramingAllowedForVersion(unsigned _version)
Definition: Session.h:93
unsigned const protocolVersion
Definition: Common.h:161
PacketType
Definition: Common.h:95
The Host class Capabilities should be registered prior to startNetwork, since m_capabilities is not t...
Definition: Host.h:129
std::shared_ptr< RLPXSocket > m_socket
Socket of peer&#39;s connection.
Definition: Session.h:159
Framing(uint16_t _protocolID)
Definition: Session.h:181
virtual void ping()=0
void version()
Definition: main.cpp:53
std::chrono::steady_clock::time_point m_lastReceived
Time point of last message.
Definition: Session.h:173
std::deque< bytes > m_encFrames
Definition: Session.h:187
DisconnectReason
Definition: Common.h:106
virtual std::chrono::steady_clock::time_point connectionTime()=0
virtual void registerFraming(uint16_t _id)=0
std::lock_guard< std::mutex > Guard
Definition: Guards.h:41
virtual void sealAndSend(RLPStream &_s, uint16_t _protocolID)=0
const char * name
Definition: rest.cpp:36
bytes m_incoming
Read buffer for ingress bytes.
Definition: Session.h:163
virtual int rating() const =0
std::vector< byte > bytes
Definition: Common.h:75
RLPXFrameWriter writer
Definition: Session.h:182
virtual ~SessionFace()
Definition: Session.h:53
std::shared_ptr< PeerCap > capabilityFromSession(SessionFace const &_session, u256 const &_version=PeerCap::version())
Definition: Session.h:196
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
std::chrono::steady_clock::time_point lastReceived() const override
Definition: Session.h:124
virtual void start()=0
virtual std::map< CapDesc, std::shared_ptr< Capability > > const & capabilities() const =0
PeerSessionInfo info() const override
Definition: Session.h:114
std::deque< bytes > m_writeQueue
The write queue.
Definition: Session.h:161
std::map< CapDesc, std::shared_ptr< Capability > > m_capabilities
The peer&#39;s capability set.
Definition: Session.h:175
std::shared_ptr< Peer > peer() const override
Definition: Session.h:122
void addNote(std::string const &_k, std::string const &_v) override
Definition: Session.h:112
std::vector< byte > m_data
Buffer for ingress packet data.
Definition: Session.h:162
std::chrono::steady_clock::time_point m_ping
Time point of last ping.
Definition: Session.h:172
clock::time_point time_point
Definition: bench.h:49
virtual void addNote(std::string const &_k, std::string const &_v)=0
virtual std::shared_ptr< Peer > peer() const =0
std::chrono::steady_clock::time_point m_connect
Time point of connection.
Definition: Session.h:171
virtual ReputationManager & repMan()=0
PeerSessionInfo m_info
Dynamic information about this peer.
Definition: Session.h:169
RLPFrameReader Reads and assembles RLPX frame byte buffers into RLPX packets.
std::mutex Mutex
Definition: Guards.h:37
virtual void disconnect(DisconnectReason _reason)=0
Host * m_server
The host that owns us. Never null.
Definition: Session.h:156
virtual PeerSessionInfo info() const =0
virtual void addRating(int _r)=0
Class for writing to an RLP bytestream.
Definition: RLP.h:383
std::shared_ptr< Peer > m_peer
The Peer object.
Definition: Session.h:165
Class for interpreting Recursive Linear-Prefix Data.
Definition: RLP.h:64
std::map< CapDesc, std::shared_ptr< Capability > > const & capabilities() const override
Definition: Session.h:120
virtual NodeID id() const =0
RLPXFrameReader reader
Definition: Session.h:183
The Session class.
Definition: Session.h:90
Multiplex packets into encrypted RLPX frames.