TARAXA
dev::p2p::RLPXHandshake Struct Referencefinal

Setup inbound or outbound connection for communication over RLPXFrameCoder. RLPx Spec: https://github.com/ethereum/devp2p/blob/master/rlpx.md#encrypted-handshake. More...

#include <RLPxHandshake.h>

Collaboration diagram for dev::p2p::RLPXHandshake:

Classes

struct  HostContext
 

Public Member Functions

 RLPXHandshake (std::shared_ptr< HostContext const > ctx, std::shared_ptr< RLPXSocket > const &_socket)
 Setup incoming connection. More...
 
 RLPXHandshake (std::shared_ptr< HostContext const > ctx, std::shared_ptr< RLPXSocket > const &_socket, NodeID _remote)
 Setup outbound connection. More...
 
void start ()
 Start handshake. More...
 
void cancel ()
 Aborts the handshake. More...
 
NodeID remote () const
 

Private Types

enum  State {
  Error = -1 , New , AckAuth , AckAuthEIP8 ,
  WriteHello , ReadHello , StartSession
}
 Sequential states of handshake. More...
 

Private Member Functions

void writeAuth ()
 Write Auth message to socket and transitions to AckAuth. More...
 
void readAuth ()
 Reads Auth message from socket and transitions to AckAuth. More...
 
void readAuthEIP8 ()
 
void setAuthValues (Signature const &sig, Public const &remotePubk, h256 const &remoteNonce, uint64_t remoteVersion)
 
void writeAck ()
 Write Ack message to socket and transitions to WriteHello. More...
 
void writeAckEIP8 ()
 Write Ack message in EIP-8 format to socket and transitions to WriteHello. More...
 
void readAck ()
 Reads Auth message from socket and transitions to WriteHello. More...
 
void readAckEIP8 ()
 
void error (boost::system::error_code _ech={})
 Closes connection and ends transitions. More...
 
void transition (boost::system::error_code _ech={})
 Performs transition for m_nextState. More...
 
char const * connectionDirectionString () const
 Get a string indicating if the connection is incoming or outgoing. More...
 
bool remoteSocketConnected () const
 Determine if the remote socket is still connected. More...
 

Private Attributes

State m_nextState = New
 Current or expected state of transition. More...
 
bool m_cancel = false
 Will be set to true if connection was canceled. More...
 
std::shared_ptr< HostContext const > host_ctx_
 
NodeID m_remote
 Node id of remote host for socket. More...
 
bool m_originated = false
 True if connection is outbound. More...
 
bytes m_auth
 Buffers for encoded and decoded handshake phases. More...
 
bytes m_authCipher
 Ciphertext of egress or ingress Auth message. More...
 
bytes m_ack
 Plaintext of egress or ingress Ack message. More...
 
bytes m_ackCipher
 Ciphertext of egress or ingress Ack message. More...
 
bytes m_handshakeOutBuffer
 Frame buffer for egress Hello packet. More...
 
bytes m_handshakeInBuffer
 Frame buffer for ingress Hello packet. More...
 
KeyPair m_ecdheLocal = KeyPair::create()
 Ephemeral ECDH secret and agreement. More...
 
h256 m_nonce
 Nonce generated by this host for handshake. More...
 
Public m_ecdheRemote
 Remote ephemeral public key. More...
 
h256 m_remoteNonce
 Nonce generated by remote host for handshake. More...
 
uint64_t m_remoteVersion
 
std::unique_ptr< RLPXFrameCoderm_io
 
std::shared_ptr< RLPXSocketm_socket
 
ba::steady_timer m_idleTimer
 Timer which enforces c_timeout. Reset for each stage of the handshake. More...
 
HandshakeFailureReason m_failureReason
 
Logger m_logger {createLogger(VerbosityTrace, "rlpx")}
 
Logger m_errorLogger {createLogger(VerbosityError, "rlpx")}
 

Static Private Attributes

static constexpr std::chrono::milliseconds c_timeout {1800}
 

Friends

class RLPXFrameCoder
 

Detailed Description

Setup inbound or outbound connection for communication over RLPXFrameCoder. RLPx Spec: https://github.com/ethereum/devp2p/blob/master/rlpx.md#encrypted-handshake.

Todo:
Implement StartSession transition via lambda which is passed to constructor.

Thread Safety Distinct Objects: Safe. Shared objects: Unsafe.


Class Documentation

◆ dev::p2p::RLPXHandshake::HostContext

struct dev::p2p::RLPXHandshake::HostContext
Collaboration diagram for dev::p2p::RLPXHandshake::HostContext:
Class Members
CapDescs capability_descriptions
string client_version
KeyPair key_pair
function< void(NodeID const &, HandshakeFailureReason)> on_failure
function< void(Public const &, RLP const &, unique_ptr< RLPXFrameCoder >, shared_ptr< RLPXSocket > const &)> on_success
unsigned port

Member Enumeration Documentation

◆ State

Sequential states of handshake.

Enumerator
Error 
New 
AckAuth 
AckAuthEIP8 
WriteHello 
ReadHello 
StartSession 

Constructor & Destructor Documentation

◆ RLPXHandshake() [1/2]

RLPXHandshake::RLPXHandshake ( std::shared_ptr< HostContext const >  ctx,
std::shared_ptr< RLPXSocket > const &  _socket 
)

Setup incoming connection.

◆ RLPXHandshake() [2/2]

RLPXHandshake::RLPXHandshake ( std::shared_ptr< HostContext const >  ctx,
std::shared_ptr< RLPXSocket > const &  _socket,
NodeID  _remote 
)

Setup outbound connection.

Member Function Documentation

◆ cancel()

void RLPXHandshake::cancel ( )

Aborts the handshake.

◆ connectionDirectionString()

char const* dev::p2p::RLPXHandshake::connectionDirectionString ( ) const
inlineprivate

Get a string indicating if the connection is incoming or outgoing.

◆ error()

void RLPXHandshake::error ( boost::system::error_code  _ech = {})
private

Closes connection and ends transitions.

◆ readAck()

void RLPXHandshake::readAck ( )
private

Reads Auth message from socket and transitions to WriteHello.

◆ readAckEIP8()

void RLPXHandshake::readAckEIP8 ( )
private

Continues reading Ack message in EIP-8 format and transitions to WriteHello.

◆ readAuth()

void RLPXHandshake::readAuth ( )
private

Reads Auth message from socket and transitions to AckAuth.

◆ readAuthEIP8()

void RLPXHandshake::readAuthEIP8 ( )
private

Continues reading Auth message in EIP-8 format and transitions to AckAuthEIP8.

◆ remote()

NodeID dev::p2p::RLPXHandshake::remote ( ) const
inline

◆ remoteSocketConnected()

bool RLPXHandshake::remoteSocketConnected ( ) const
private

Determine if the remote socket is still connected.

◆ setAuthValues()

void RLPXHandshake::setAuthValues ( Signature const &  sig,
Public const &  remotePubk,
h256 const &  remoteNonce,
uint64_t  remoteVersion 
)
private

Derives ephemeral secret from signature and sets members after Auth has been decrypted.

◆ start()

void dev::p2p::RLPXHandshake::start ( )
inline

Start handshake.

◆ transition()

void RLPXHandshake::transition ( boost::system::error_code  _ech = {})
private

Performs transition for m_nextState.

This pointer will be freed if there is an error otherwise it will be passed to Host which will take ownership.

authenticate and decrypt header

check frame size

rlp of header has protocol-type, sequence-id[, total-packet-size]

read padded frame and mac

◆ writeAck()

void RLPXHandshake::writeAck ( )
private

Write Ack message to socket and transitions to WriteHello.

◆ writeAckEIP8()

void RLPXHandshake::writeAckEIP8 ( )
private

Write Ack message in EIP-8 format to socket and transitions to WriteHello.

◆ writeAuth()

void RLPXHandshake::writeAuth ( )
private

Write Auth message to socket and transitions to AckAuth.

Friends And Related Function Documentation

◆ RLPXFrameCoder

friend class RLPXFrameCoder
friend

Member Data Documentation

◆ c_timeout

constexpr std::chrono::milliseconds RLPXHandshake::c_timeout {1800}
staticconstexprprivate

Timeout for a stage in the handshake to complete (the remote to respond to transition events). Enforced by m_idleTimer and refreshed by transition().

◆ host_ctx_

std::shared_ptr<HostContext const> dev::p2p::RLPXHandshake::host_ctx_
private

◆ m_ack

bytes dev::p2p::RLPXHandshake::m_ack
private

Plaintext of egress or ingress Ack message.

◆ m_ackCipher

bytes dev::p2p::RLPXHandshake::m_ackCipher
private

Ciphertext of egress or ingress Ack message.

◆ m_auth

bytes dev::p2p::RLPXHandshake::m_auth
private

Buffers for encoded and decoded handshake phases.

Plaintext of egress or ingress Auth message.

◆ m_authCipher

bytes dev::p2p::RLPXHandshake::m_authCipher
private

Ciphertext of egress or ingress Auth message.

◆ m_cancel

bool dev::p2p::RLPXHandshake::m_cancel = false
private

Will be set to true if connection was canceled.

◆ m_ecdheLocal

KeyPair dev::p2p::RLPXHandshake::m_ecdheLocal = KeyPair::create()
private

Ephemeral ECDH secret and agreement.

◆ m_ecdheRemote

Public dev::p2p::RLPXHandshake::m_ecdheRemote
private

Remote ephemeral public key.

◆ m_errorLogger

Logger dev::p2p::RLPXHandshake::m_errorLogger {createLogger(VerbosityError, "rlpx")}
private

◆ m_failureReason

HandshakeFailureReason dev::p2p::RLPXHandshake::m_failureReason
private

◆ m_handshakeInBuffer

bytes dev::p2p::RLPXHandshake::m_handshakeInBuffer
private

Frame buffer for ingress Hello packet.

◆ m_handshakeOutBuffer

bytes dev::p2p::RLPXHandshake::m_handshakeOutBuffer
private

Frame buffer for egress Hello packet.

◆ m_idleTimer

ba::steady_timer dev::p2p::RLPXHandshake::m_idleTimer
private

Timer which enforces c_timeout. Reset for each stage of the handshake.

◆ m_io

std::unique_ptr<RLPXFrameCoder> dev::p2p::RLPXHandshake::m_io
private

Used to read and write RLPx encrypted frames for last step of handshake authentication. Passed onto Host which will take ownership.

◆ m_logger

Logger dev::p2p::RLPXHandshake::m_logger {createLogger(VerbosityTrace, "rlpx")}
private

◆ m_nextState

State dev::p2p::RLPXHandshake::m_nextState = New
private

Current or expected state of transition.

◆ m_nonce

h256 dev::p2p::RLPXHandshake::m_nonce
private

Nonce generated by this host for handshake.

◆ m_originated

bool dev::p2p::RLPXHandshake::m_originated = false
private

True if connection is outbound.

◆ m_remote

NodeID dev::p2p::RLPXHandshake::m_remote
private

Node id of remote host for socket.

Public address of remote host.

◆ m_remoteNonce

h256 dev::p2p::RLPXHandshake::m_remoteNonce
private

Nonce generated by remote host for handshake.

◆ m_remoteVersion

uint64_t dev::p2p::RLPXHandshake::m_remoteVersion
private

◆ m_socket

std::shared_ptr<RLPXSocket> dev::p2p::RLPXHandshake::m_socket
private

The documentation for this struct was generated from the following files: