Fabcoin Core  0.16.2
P2P Digital Currency
RLPxHandshake.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 <memory>
26 #include <libdevcrypto/Common.h>
27 #include <libdevcrypto/ECDHE.h>
28 #include "RLPXSocket.h"
29 #include "RLPXFrameCoder.h"
30 #include "Common.h"
31 namespace ba = boost::asio;
32 namespace bi = boost::asio::ip;
33 
34 namespace dev
35 {
36 namespace p2p
37 {
38 
39 static const unsigned c_rlpxVersion = 4;
40 
51 class RLPXHandshake: public std::enable_shared_from_this<RLPXHandshake>
52 {
53  friend class RLPXFrameCoder;
54 
55 public:
57  RLPXHandshake(Host* _host, std::shared_ptr<RLPXSocket> const& _socket): m_host(_host), m_originated(false), m_socket(_socket), m_idleTimer(m_socket->ref().get_io_service()) { crypto::Nonce::get().ref().copyTo(m_nonce.ref()); }
58 
60  RLPXHandshake(Host* _host, std::shared_ptr<RLPXSocket> const& _socket, NodeID _remote): m_host(_host), m_remote(_remote), m_originated(true), m_socket(_socket), m_idleTimer(m_socket->ref().get_io_service()) { crypto::Nonce::get().ref().copyTo(m_nonce.ref()); }
61 
63 
65  void start() { transition(); }
66 
68  void cancel();
69 
70 protected:
72  enum State
73  {
74  Error = -1,
75  New,
81  };
82 
84  void writeAuth();
85 
87  void readAuth();
88 
90  void readAuthEIP8();
91 
93  void setAuthValues(Signature const& sig, Public const& remotePubk, h256 const& remoteNonce, uint64_t remoteVersion);
94 
96  void writeAck();
97 
99  void writeAckEIP8();
100 
102  void readAck();
103 
105  void readAckEIP8();
106 
108  void error();
109 
111  virtual void transition(boost::system::error_code _ech = boost::system::error_code());
112 
114  boost::posix_time::milliseconds const c_timeout = boost::posix_time::milliseconds(1800);
115 
117  bool m_cancel = false;
118 
120 
123  bool m_originated = false;
124 
132 
135 
138  uint64_t m_remoteVersion;
139 
142  std::unique_ptr<RLPXFrameCoder> m_io;
143 
144  std::shared_ptr<RLPXSocket> m_socket;
145  boost::asio::deadline_timer m_idleTimer;
146 };
147 
148 }
149 }
bytes m_ackCipher
Ciphertext of egress or ingress Ack message.
std::shared_ptr< RLPXSocket > m_socket
Socket.
bytes m_ack
Plaintext of egress or ingress Ack message.
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
Host * m_host
Host which provides m_alias, protocolVersion(), m_clientVersion, caps(), and TCP listenPort().
void cancel()
Aborts the handshake.
void readAuthEIP8()
Continues reading Auth message in EIP-8 format and transitions to AckAuthEIP8.
RLPXHandshake(Host *_host, std::shared_ptr< RLPXSocket > const &_socket, NodeID _remote)
Setup outbound connection.
Definition: RLPxHandshake.h:60
vector_ref< _T const > ref(_T const &_t)
Definition: vector_ref.h:115
bool m_cancel
Will be set to true if connection was canceled.
h256 m_nonce
Nonce generated by this host for handshake.
NodeID m_remote
Node id of remote host for socket.
The Host class Capabilities should be registered prior to startNetwork, since m_capabilities is not t...
Definition: Host.h:129
boost::asio::deadline_timer m_idleTimer
Timer which enforces c_timeout.
static Secret get()
Returns the next nonce (might be read from a file).
Definition: Common.h:205
bytes m_authCipher
Ciphertext of egress or ingress Auth message.
crypto::ECDHE m_ecdhe
Ephemeral ECDH secret and agreement.
bytesConstRef ref() const
Definition: FixedHash.h:292
virtual void transition(boost::system::error_code _ech=boost::system::error_code())
Performs transition for m_nextState.
bytesRef ref()
Definition: FixedHash.h:133
void readAck()
Reads Auth message from socket and transitions to WriteHello.
std::vector< byte > bytes
Definition: Common.h:75
Public m_remoteEphemeral
Remote ephemeral public key.
void setAuthValues(Signature const &sig, Public const &remotePubk, h256 const &remoteNonce, uint64_t remoteVersion)
Derives ephemeral secret from signature and sets members after Auth has been decrypted.
bytes m_handshakeInBuffer
Frame buffer for ingress Hello packet.
void writeAuth()
Write Auth message to socket and transitions to AckAuth.
boost::posix_time::milliseconds const c_timeout
Timeout for remote to respond to transition events. Enforced by m_idleTimer and refreshed by transiti...
void error()
Closes connection and ends transitions.
void writeAck()
Write Ack message to socket and transitions to WriteHello.
bool m_originated
True if connection is outbound.
State m_nextState
Current or expected state of transition.
RLPXHandshake(Host *_host, std::shared_ptr< RLPXSocket > const &_socket)
Setup incoming connection.
Definition: RLPxHandshake.h:57
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
void readAckEIP8()
Continues reading Ack message in EIP-8 format and transitions to WriteHello.
void writeAckEIP8()
Write Ack message in EIP-8 format to socket and transitions to WriteHello.
h256 m_remoteNonce
Nonce generated by remote host for handshake.
State
Sequential states of handshake.
Definition: RLPxHandshake.h:72
std::unique_ptr< RLPXFrameCoder > m_io
Used to read and write RLPx encrypted frames for last step of handshake authentication.
void readAuth()
Reads Auth message from socket and transitions to AckAuth.
bytes m_auth
Buffers for encoded and decoded handshake phases.
void start()
Start handshake.
Definition: RLPxHandshake.h:65
bytes m_handshakeOutBuffer
Frame buffer for egress Hello packet.
Derive DH shared secret from EC keypairs.
Definition: ECDHE.h:37
Encoder/decoder transport for RLPx connection established by RLPXHandshake.
Setup inbound or outbound connection for communication over RLPXFrameCoder.
Definition: RLPxHandshake.h:51