|
Network System 0.1.1
High-performance modular networking library for scalable client-server applications
|
A wrapper around ASIO UDP socket with OpenSSL DTLS encryption. More...
#include <dtls_socket.h>


Public Types | |
| enum class | handshake_type { client , server } |
| Handshake type enumeration. More... | |
Public Member Functions | |
| ~dtls_socket () | |
| Destructor. Cleans up OpenSSL resources. | |
| dtls_socket (const dtls_socket &)=delete | |
| dtls_socket & | operator= (const dtls_socket &)=delete |
| dtls_socket (dtls_socket &&)=delete | |
| dtls_socket & | operator= (dtls_socket &&)=delete |
| auto | async_handshake (handshake_type type, std::function< void(std::error_code)> handler) -> void |
| Performs asynchronous DTLS handshake. | |
| auto | set_receive_callback (std::function< void(const std::vector< uint8_t > &, const asio::ip::udp::endpoint &)> callback) -> void |
| Sets a callback to receive decrypted inbound datagrams. | |
| auto | set_error_callback (std::function< void(std::error_code)> callback) -> void |
| Sets a callback to handle socket errors. | |
| auto | start_receive () -> void |
| Begins the continuous asynchronous receive loop. | |
| auto | stop_receive () -> void |
| Stops the receive loop. | |
| auto | deliver_encrypted (const std::vector< uint8_t > &data, const asio::ip::udp::endpoint &sender) -> void |
| Injects an already-received encrypted datagram for DTLS processing. | |
| auto | async_send (std::vector< uint8_t > &&data, std::function< void(std::error_code, std::size_t)> handler) -> void |
| Initiates an asynchronous encrypted send. | |
| auto | async_send_to (std::vector< uint8_t > &&data, const asio::ip::udp::endpoint &endpoint, std::function< void(std::error_code, std::size_t)> handler) -> void |
| Initiates an asynchronous encrypted send to a specific endpoint. | |
| auto | set_peer_endpoint (const asio::ip::udp::endpoint &endpoint) -> void |
| Sets the peer endpoint for connected mode. | |
| auto | peer_endpoint () const -> asio::ip::udp::endpoint |
| Returns the peer endpoint. | |
| auto | socket () -> asio::ip::udp::socket & |
| Provides direct access to the underlying UDP socket. | |
| auto | is_handshake_complete () const -> bool |
| Checks if the DTLS handshake is complete. | |
Static Public Member Functions | |
| static Result< std::shared_ptr< dtls_socket > > | create (asio::ip::udp::socket socket, SSL_CTX *ssl_ctx) |
Constructs a dtls_socket with an existing UDP socket. | |
Private Member Functions | |
| dtls_socket (asio::ip::udp::socket socket, SSL *ssl, BIO *rbio, BIO *wbio) | |
| auto | do_receive () -> void |
| Internal function to handle the receive logic. | |
| auto | process_received_data (const std::vector< uint8_t > &data, const asio::ip::udp::endpoint &sender) -> void |
| Processes received encrypted data through DTLS. | |
| auto | flush_bio_output () -> void |
| Flushes pending DTLS output to the network. | |
| auto | continue_handshake () -> void |
| Continues the handshake process. | |
| auto | make_ssl_error () const -> std::error_code |
| Creates an OpenSSL error code from the current error state. | |
Private Attributes | |
| asio::ip::udp::socket | socket_ |
| asio::ip::udp::endpoint | peer_endpoint_ |
| asio::ip::udp::endpoint | sender_endpoint_ |
| SSL_CTX * | ssl_ctx_ |
| SSL * | ssl_ |
| BIO * | rbio_ |
| BIO * | wbio_ |
| std::array< uint8_t, 65536 > | read_buffer_ |
| std::mutex | ssl_mutex_ |
| std::mutex | callback_mutex_ |
| std::mutex | endpoint_mutex_ |
| std::function< void(const std::vector< uint8_t > &, const asio::ip::udp::endpoint &)> | receive_callback_ |
| std::function< void(std::error_code)> | error_callback_ |
| std::function< void(std::error_code)> | handshake_callback_ |
| std::atomic< bool > | is_receiving_ {false} |
| std::atomic< bool > | handshake_complete_ {false} |
| std::atomic< bool > | handshake_in_progress_ {false} |
| handshake_type | handshake_type_ {handshake_type::client} |
A wrapper around ASIO UDP socket with OpenSSL DTLS encryption.
set_receive_callback() to handle decrypted inbound datagrams and set_error_callback() for error handling.start_receive() begins an ongoing loop of receiving encrypted datagrams.async_send() encrypts and sends data to the configured peer.Definition at line 49 of file dtls_socket.h.
|
strong |
Handshake type enumeration.
| Enumerator | |
|---|---|
| client | Client-side handshake |
| server | Server-side handshake |
Definition at line 55 of file dtls_socket.h.
| kcenon::network::internal::dtls_socket::~dtls_socket | ( | ) |
Destructor. Cleans up OpenSSL resources.
Definition at line 89 of file dtls_socket.cpp.
References ssl_, and stop_receive().

|
delete |
|
delete |
|
private |
Definition at line 80 of file dtls_socket.cpp.
| auto kcenon::network::internal::dtls_socket::async_handshake | ( | handshake_type | type, |
| std::function< void(std::error_code)> | handler ) -> void |
Performs asynchronous DTLS handshake.
| type | Handshake type (client or server) |
| handler | Completion handler with signature void(std::error_code) |
Must be called before start_receive() or async_send(). The handshake involves multiple round-trips over UDP.
Definition at line 100 of file dtls_socket.cpp.
| auto kcenon::network::internal::dtls_socket::async_send | ( | std::vector< uint8_t > && | data, |
| std::function< void(std::error_code, std::size_t)> | handler ) -> void |
Initiates an asynchronous encrypted send.
| data | The plaintext data to encrypt and send (moved for efficiency). |
| handler | A completion handler with signature void(std::error_code, std::size_t) invoked upon completion. |
The data is encrypted using DTLS before transmission.
Definition at line 396 of file dtls_socket.cpp.
| auto kcenon::network::internal::dtls_socket::async_send_to | ( | std::vector< uint8_t > && | data, |
| const asio::ip::udp::endpoint & | endpoint, | ||
| std::function< void(std::error_code, std::size_t)> | handler ) -> void |
Initiates an asynchronous encrypted send to a specific endpoint.
| data | The plaintext data to encrypt and send (moved for efficiency). |
| endpoint | The target endpoint. |
| handler | A completion handler invoked upon completion. |
Useful for server responding to different clients.
Definition at line 409 of file dtls_socket.cpp.
|
private |
Continues the handshake process.
Definition at line 128 of file dtls_socket.cpp.
|
staticnodiscard |
Constructs a dtls_socket with an existing UDP socket.
| socket | An asio::ip::udp::socket that must be open. |
| ssl_ctx | The OpenSSL SSL_CTX configured for DTLS. |
The socket should be connected (for client) or bound (for server) before calling handshake methods.
Definition at line 42 of file dtls_socket.cpp.
References dtls_socket(), kcenon::network::protocols::quic::error, kcenon::network::error_codes::common_errors::internal_error, kcenon::network::ok(), and socket().
Referenced by kcenon::network::core::secure_messaging_udp_server::create_session(), and kcenon::network::core::secure_messaging_udp_client::do_start_impl().


| auto kcenon::network::internal::dtls_socket::deliver_encrypted | ( | const std::vector< uint8_t > & | data, |
| const asio::ip::udp::endpoint & | sender ) -> void |
Injects an already-received encrypted datagram for DTLS processing.
Intended for a server that owns the underlying UDP socket and demultiplexes datagrams to per-client sessions itself, instead of letting each dtls_socket drive its own receive loop via start_receive(). The bytes are fed through the read BIO exactly as the internal receive loop would: an in-progress handshake is advanced, and once the handshake is complete the record is decrypted and the application payload is delivered to the receive callback.
Safe to call with malformed, truncated, or pre-handshake records: such data is processed best-effort and dropped without invoking the receive callback when it does not yield a complete decrypted record.
| data | The encrypted datagram bytes received from the peer. |
| sender | The endpoint the datagram was received from. |
Definition at line 269 of file dtls_socket.cpp.
|
private |
Internal function to handle the receive logic.
Definition at line 222 of file dtls_socket.cpp.
|
private |
Flushes pending DTLS output to the network.
Definition at line 355 of file dtls_socket.cpp.
|
inline |
Checks if the DTLS handshake is complete.
Definition at line 200 of file dtls_socket.h.
References handshake_complete_.
|
private |
Creates an OpenSSL error code from the current error state.
Definition at line 497 of file dtls_socket.cpp.
|
delete |
|
delete |
| auto kcenon::network::internal::dtls_socket::peer_endpoint | ( | ) | const -> asio::ip::udp::endpoint |
Returns the peer endpoint.
Definition at line 491 of file dtls_socket.cpp.
References endpoint_mutex_, and peer_endpoint_.
|
private |
Processes received encrypted data through DTLS.
| data | The encrypted datagram. |
| sender | The sender's endpoint. |
Definition at line 283 of file dtls_socket.cpp.
| auto kcenon::network::internal::dtls_socket::set_error_callback | ( | std::function< void(std::error_code)> | callback | ) | -> void |
Sets a callback to handle socket errors.
| callback | A function with signature void(std::error_code), invoked when any asynchronous operation fails. |
If no callback is set, errors are not explicitly handled here.
Definition at line 201 of file dtls_socket.cpp.
| auto kcenon::network::internal::dtls_socket::set_peer_endpoint | ( | const asio::ip::udp::endpoint & | endpoint | ) | -> void |
Sets the peer endpoint for connected mode.
| endpoint | The peer's UDP endpoint. |
Definition at line 485 of file dtls_socket.cpp.
| auto kcenon::network::internal::dtls_socket::set_receive_callback | ( | std::function< void(const std::vector< uint8_t > &, const asio::ip::udp::endpoint &)> | callback | ) | -> void |
Sets a callback to receive decrypted inbound datagrams.
| callback | A function with signature void(const std::vector<uint8_t>&, const asio::ip::udp::endpoint&), called whenever a datagram is successfully received and decrypted. |
If no callback is set, received data is effectively discarded.
Definition at line 193 of file dtls_socket.cpp.
|
inline |
Provides direct access to the underlying UDP socket.
asio::ip::udp::socket. Definition at line 194 of file dtls_socket.h.
References socket_.
Referenced by create().

| auto kcenon::network::internal::dtls_socket::start_receive | ( | ) | -> void |
Begins the continuous asynchronous receive loop.
Once called, the class repeatedly receives encrypted datagrams, decrypts them, and invokes the receive callback. If an error occurs, the error callback is triggered.
Definition at line 208 of file dtls_socket.cpp.
| auto kcenon::network::internal::dtls_socket::stop_receive | ( | ) | -> void |
Stops the receive loop.
Definition at line 217 of file dtls_socket.cpp.
Referenced by ~dtls_socket().

|
private |
Protects callback registration.
Definition at line 250 of file dtls_socket.h.
|
private |
Protects endpoint access.
Definition at line 251 of file dtls_socket.h.
Referenced by peer_endpoint().
|
private |
Error callback.
Definition at line 256 of file dtls_socket.h.
|
private |
Handshake completion callback.
Definition at line 258 of file dtls_socket.h.
|
private |
Handshake completed flag.
Definition at line 261 of file dtls_socket.h.
Referenced by is_handshake_complete().
|
private |
|
private |
|
private |
|
private |
Peer endpoint for connected mode.
Definition at line 239 of file dtls_socket.h.
Referenced by peer_endpoint().
|
private |
Read BIO (memory).
Definition at line 244 of file dtls_socket.h.
|
private |
Buffer for receiving datagrams.
Definition at line 247 of file dtls_socket.h.
|
private |
Inbound data callback.
Definition at line 254 of file dtls_socket.h.
|
private |
Sender endpoint for receives.
Definition at line 240 of file dtls_socket.h.
|
private |
|
private |
|
private |
OpenSSL context (not owned).
Definition at line 242 of file dtls_socket.h.
|
private |
Protects SSL operations.
Definition at line 249 of file dtls_socket.h.
|
private |
Write BIO (memory).
Definition at line 245 of file dtls_socket.h.