COROIO: NNet::TSslSocket< TSocket > Class Template Reference
COROIO
 
Loading...
Searching...
No Matches
NNet::TSslSocket< TSocket > Class Template Reference

TLS layer over any connected socket, exposing the same ReadSome/WriteSome interface. More...

#include <ssl.hpp>

Public Types

using TPoller = typename TSocket::TPoller
 

Public Member Functions

 TSslSocket (TSocket &&socket, TSslContext &ctx)
 Constructs a TSslSocket, taking ownership of the underlying socket.
 
 TSslSocket (TSslSocket &&other)
 
TSslSocketoperator= (TSslSocket &&other)
 
 TSslSocket (const TSslSocket &)=delete
 
TSslSocketoperator= (const TSslSocket &)=delete
 
 ~TSslSocket ()
 Frees the SSL instance, associated BIOs, and any in-progress handshake coroutine.
 
void SslSetTlsExtHostName (const std::string &host)
 Sets the TLS SNI host name sent in the ClientHello.
 
TFuture< TSslSocket< TSocket > > Accept ()
 Accepts a TCP connection and performs the server-side TLS handshake.
 
TFuture< void > AcceptHandshake ()
 Performs the server-side TLS handshake on an already-accepted TCP socket.
 
TFuture< void > Connect (const TAddress &address, TTime deadline=TTime::max())
 TCP-connects to address and performs the client-side TLS handshake.
 
TFuture< ssize_t > ReadSome (void *data, size_t size)
 Reads up to size decrypted bytes into data.
 
TFuture< ssize_t > WriteSome (const void *data, size_t size)
 Encrypts and sends all size bytes from data.
 
auto Poller ()
 Returns the poller associated with the underlying socket.
 

Detailed Description

template<typename TSocket>
class NNet::TSslSocket< TSocket >

TLS layer over any connected socket, exposing the same ReadSome/WriteSome interface.

Takes ownership of the underlying socket (moved in). References the TSslContext — the context must outlive this object. Move-only.

Client usage:

TSslSocket ssl(std::move(socket), ctx);
ssl.SslSetTlsExtHostName("example.com"); // SNI — call before Connect()
co_await ssl.Connect(addr);
ssize_t n = co_await ssl.ReadSome(buf, size);
TLS layer over any connected socket, exposing the same ReadSome/WriteSome interface.
Definition ssl.hpp:108
Owns an OpenSSL SSL_CTX and optional log callback.
Definition ssl.hpp:39
static TSslContext Client(const std::function< void(const char *)> &logFunc={})
Creates a TLS client context (no certificate required).
Definition ssl.cpp:24

Server usage:

TSslContext ctx = TSslContext::Server("server.crt", "server.key");
TSslSocket listener(std::move(listeningSocket), ctx);
auto client = co_await listener.Accept(); // TCP accept + TLS handshake
static TSslContext Server(const char *certfile, const char *keyfile, const std::function< void(const char *)> &logFunc={})
Creates a TLS server context from PEM files on disk.
Definition ssl.cpp:32
Template Parameters
TSocketUnderlying connected socket type (e.g. TDefaultPoller::TSocket).

Constructor & Destructor Documentation

◆ TSslSocket()

template<typename TSocket >
NNet::TSslSocket< TSocket >::TSslSocket ( TSocket &&  socket,
TSslContext ctx 
)
inline

Constructs a TSslSocket, taking ownership of the underlying socket.

Parameters
socketMoved-in socket (TCP-connected for client; bound+listening for server).
ctxTLS context — must outlive this object.

Member Function Documentation

◆ Accept()

template<typename TSocket >
TFuture< TSslSocket< TSocket > > NNet::TSslSocket< TSocket >::Accept ( )
inline

Accepts a TCP connection and performs the server-side TLS handshake.

Calls Socket.Accept() then AcceptHandshake() on the resulting socket. Use this on a bound+listening TSslSocket in a server accept loop.

Returns
A fully-handshaked TSslSocket ready for ReadSome/WriteSome.

◆ AcceptHandshake()

template<typename TSocket >
TFuture< void > NNet::TSslSocket< TSocket >::AcceptHandshake ( )
inline

Performs the server-side TLS handshake on an already-accepted TCP socket.

Called automatically by Accept(). Call directly only if you accepted the TCP connection separately and want to add TLS on top.

◆ Connect()

template<typename TSocket >
TFuture< void > NNet::TSslSocket< TSocket >::Connect ( const TAddress address,
TTime  deadline = TTime::max() 
)
inline

TCP-connects to address and performs the client-side TLS handshake.

Call SslSetTlsExtHostName before this if the server requires SNI.

Parameters
addressRemote address to connect to.
deadlineOptional connection timeout (defaults to no timeout).
Exceptions
std::system_erroron TCP connect failure or timeout.
std::runtime_erroron TLS handshake failure.

◆ ReadSome()

template<typename TSocket >
TFuture< ssize_t > NNet::TSslSocket< TSocket >::ReadSome ( void *  data,
size_t  size 
)
inline

Reads up to size decrypted bytes into data.

Waits for the handshake to complete if it hasn't yet. Returns bytes read (>0), 0 on clean TLS shutdown, or a negative value on transient error. Throws std::runtime_error on fatal TLS errors.

◆ SslSetTlsExtHostName()

template<typename TSocket >
void NNet::TSslSocket< TSocket >::SslSetTlsExtHostName ( const std::string &  host)
inline

Sets the TLS SNI host name sent in the ClientHello.

Required for servers that host multiple certificates on one IP. Must be called before Connect().

Parameters
hostThe server hostname (e.g. "example.com").

◆ WriteSome()

template<typename TSocket >
TFuture< ssize_t > NNet::TSslSocket< TSocket >::WriteSome ( const void *  data,
size_t  size 
)
inline

Encrypts and sends all size bytes from data.

Waits for the handshake to complete if it hasn't yet. Returns size on success (all bytes are always written). Throws std::runtime_error on TLS error or connection close.


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