libDwmCredence-0.1.32
Dwm::Credence::Peer Class Reference

Encapsulate a network peer. More...

#include <DwmCredencePeer.hh>

Public Member Functions

 Peer ()
 Default constructor.
 
void SetKeyExchangeTimeout (std::chrono::milliseconds ms)
 Sets the time we'll wait for the peer to send its public key.
 
bool Accept (boost::asio::ip::tcp::socket &&s)
 Used by a server to accept a new connection on the given TCP socket s.
 
bool Accept (boost::asio::local::stream_protocol::socket &&s)
 Used by a server to accept a new connection on the given UNIX domain socket s.
 
bool Connect (const std::string &host, uint16_t port, std::chrono::milliseconds timeOut=std::chrono::milliseconds(5000))
 Used by a client to connect to host at the given port, waiting timeOut for success.
 
bool Connect (const std::string &path, std::chrono::milliseconds timeOut=std::chrono::milliseconds(5000))
 Used by a client to connect to a UNIX domain socket at the given path, waiting timeOut for success.
 
void SetIdExchangeTimeout (std::chrono::milliseconds ms)
 Sets the time we'll wait for the peer to send its ID during authentication.
 
bool Authenticate (const KeyStash &keyStash, const KnownKeys &knownKeys)
 Using the given keyStash and knownKeys, authenticate our identity to the peer and verify the peer's identity.
 
const std::string & Id () const
 If Authenticate() was used, returns the peer's identifier.
 
template<typename T >
bool Send (const T &msg)
 Sends the given msg to the peer.
 
template<typename T >
bool Receive (T &msg)
 Receives the given msg from the peer.
 
bool ReceiveWouldBlock (size_t numBytes)
 Returns true if a Receive() of numBytes or greater would block.
 
void Disconnect ()
 Disconnects the peer.
 
std::string EndPointString () const
 Returns a string representation of the peer endpoint, in 'addr:port' form.
 

Detailed Description

Encapsulate a network peer.

Note that once a connection is set up with Accept() or Connect(), all traffic will be encrypted with XChacha20Poly1305. Identity authentication (via the Authenticate() member function) after Accept() or Connect() is optional but highly recommended, as I don't have any production code that doesn't use it.

Member Function Documentation

◆ Accept() [1/2]

bool Dwm::Credence::Peer::Accept ( boost::asio::ip::tcp::socket && s)

Used by a server to accept a new connection on the given TCP socket s.

Returns true on success, false on failure. Note that s is expected to be a socket that was already accepted (via Boost::asio::ip::tcp::acceptor::accept()).

◆ Accept() [2/2]

bool Dwm::Credence::Peer::Accept ( boost::asio::local::stream_protocol::socket && s)

Used by a server to accept a new connection on the given UNIX domain socket s.

Returns true on success, false on failure. Note that s is expected to be a socket that was already accepted (via Boost::asio::local::stream_protocol::acceptor::accept()).

◆ Authenticate()

bool Dwm::Credence::Peer::Authenticate ( const KeyStash & keyStash,
const KnownKeys & knownKeys )

Using the given keyStash and knownKeys, authenticate our identity to the peer and verify the peer's identity.

This is done using randomly generated challenges which must be signed with an Ed25519 key. Since this should be called immediately after Accept() or Connect(), the entire transaction is encrypted with XChaCha20Poly1305. Returns true on success, false on failure.

◆ Connect() [1/2]

bool Dwm::Credence::Peer::Connect ( const std::string & host,
uint16_t port,
std::chrono::milliseconds timeOut = std::chrono::milliseconds(5000) )

Used by a client to connect to host at the given port, waiting timeOut for success.

Returns true on success, false on failure.

◆ Connect() [2/2]

bool Dwm::Credence::Peer::Connect ( const std::string & path,
std::chrono::milliseconds timeOut = std::chrono::milliseconds(5000) )

Used by a client to connect to a UNIX domain socket at the given path, waiting timeOut for success.

Returns true on success, false on failure.

◆ Receive()

template<typename T >
bool Dwm::Credence::Peer::Receive ( T & msg)
inline

Receives the given msg from the peer.

Returns true on success, false on failure. Note that T must be supported directly by a Dwm::StreamIO::Read() function (See DwmStreamIO.hh in libDwm) or meet the requirements of the Dwm::HasStreamRead concept (see DwmStreamIOCapable.hh in libDwm).

Here is the call graph for this function:

◆ Send()

template<typename T >
bool Dwm::Credence::Peer::Send ( const T & msg)
inline

Sends the given msg to the peer.

Returns true on success, false on failure. Note that T must be supported directly by a Dwm::StreamIO::Write() function (see DwmStreamIO.hh in libDwm) or meet the requirements of the Dwm::HasStreamWrite concept (see DwmStreamIOCapable.hh in libDwm).

Here is the call graph for this function:

◆ SetIdExchangeTimeout()

void Dwm::Credence::Peer::SetIdExchangeTimeout ( std::chrono::milliseconds ms)

Sets the time we'll wait for the peer to send its ID during authentication.

If not set, a default of 1000 milliseconds (1 second) will be used.

◆ SetKeyExchangeTimeout()

void Dwm::Credence::Peer::SetKeyExchangeTimeout ( std::chrono::milliseconds ms)

Sets the time we'll wait for the peer to send its public key.

If not set, a default of 1000 milliseconds (1 second) will be used.


The documentation for this class was generated from the following file: