Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
dtls_socket.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
7#include <array>
8#include <atomic>
9#include <functional>
10#include <memory>
11#include <mutex>
12#include <system_error>
13#include <vector>
14
15#include <asio.hpp>
16
19
21{
48 class dtls_socket : public std::enable_shared_from_this<dtls_socket>
49 {
50 public:
54 enum class handshake_type
55 {
56 client,
57 server
58 };
59
68 dtls_socket(asio::ip::udp::socket socket, SSL_CTX* ssl_ctx);
69
74
75 // Non-copyable, non-movable
76 dtls_socket(const dtls_socket&) = delete;
80
89 auto async_handshake(
90 handshake_type type,
91 std::function<void(std::error_code)> handler) -> void;
92
102 std::function<void(const std::vector<uint8_t>&,
103 const asio::ip::udp::endpoint&)> callback) -> void;
104
112 auto set_error_callback(std::function<void(std::error_code)> callback)
113 -> void;
114
122 auto start_receive() -> void;
123
127 auto stop_receive() -> void;
128
138 auto async_send(
139 std::vector<uint8_t>&& data,
140 std::function<void(std::error_code, std::size_t)> handler) -> void;
141
150 auto async_send_to(
151 std::vector<uint8_t>&& data,
152 const asio::ip::udp::endpoint& endpoint,
153 std::function<void(std::error_code, std::size_t)> handler) -> void;
154
159 auto set_peer_endpoint(const asio::ip::udp::endpoint& endpoint) -> void;
160
165 auto peer_endpoint() const -> asio::ip::udp::endpoint;
166
171 auto socket() -> asio::ip::udp::socket& { return socket_; }
172
177 auto is_handshake_complete() const -> bool
178 {
179 return handshake_complete_.load();
180 }
181
182 private:
186 auto do_receive() -> void;
187
193 auto process_received_data(const std::vector<uint8_t>& data,
194 const asio::ip::udp::endpoint& sender) -> void;
195
199 auto flush_bio_output() -> void;
200
204 auto continue_handshake() -> void;
205
210 auto make_ssl_error() const -> std::error_code;
211
212 private:
213 asio::ip::udp::socket socket_;
214 asio::ip::udp::endpoint peer_endpoint_;
215 asio::ip::udp::endpoint sender_endpoint_;
219 BIO* rbio_;
220 BIO* wbio_;
222 std::array<uint8_t, 65536> read_buffer_;
228 std::function<void(const std::vector<uint8_t>&, const asio::ip::udp::endpoint&)>
230 std::function<void(std::error_code)>
232 std::function<void(std::error_code)>
235 std::atomic<bool> is_receiving_{false};
236 std::atomic<bool> handshake_complete_{false};
237 std::atomic<bool> handshake_in_progress_{false};
239 };
240
241} // namespace kcenon::network::internal
A wrapper around ASIO UDP socket with OpenSSL DTLS encryption.
Definition dtls_socket.h:49
std::atomic< bool > handshake_in_progress_
std::array< uint8_t, 65536 > read_buffer_
auto async_send(std::vector< uint8_t > &&data, std::function< void(std::error_code, std::size_t)> handler) -> void
Initiates an asynchronous encrypted send.
dtls_socket(const 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.
asio::ip::udp::endpoint peer_endpoint_
auto make_ssl_error() const -> std::error_code
Creates an OpenSSL error code from the current error state.
handshake_type
Handshake type enumeration.
Definition dtls_socket.h:55
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.
std::function< void(const std::vector< uint8_t > &, const asio::ip::udp::endpoint &)> receive_callback_
auto set_error_callback(std::function< void(std::error_code)> callback) -> void
Sets a callback to handle socket errors.
auto stop_receive() -> void
Stops the receive loop.
dtls_socket & operator=(const dtls_socket &)=delete
auto set_peer_endpoint(const asio::ip::udp::endpoint &endpoint) -> void
Sets the peer endpoint for connected mode.
auto is_handshake_complete() const -> bool
Checks if the DTLS handshake is complete.
std::function< void(std::error_code)> handshake_callback_
asio::ip::udp::endpoint sender_endpoint_
auto flush_bio_output() -> void
Flushes pending DTLS output to the network.
auto start_receive() -> void
Begins the continuous asynchronous receive loop.
auto process_received_data(const std::vector< uint8_t > &data, const asio::ip::udp::endpoint &sender) -> void
Processes received encrypted data through DTLS.
auto socket() -> asio::ip::udp::socket &
Provides direct access to the underlying UDP socket.
std::function< void(std::error_code)> error_callback_
dtls_socket(asio::ip::udp::socket socket, SSL_CTX *ssl_ctx)
Constructs a dtls_socket with an existing UDP socket.
auto peer_endpoint() const -> asio::ip::udp::endpoint
Returns the peer endpoint.
auto continue_handshake() -> void
Continues the handshake process.
auto do_receive() -> void
Internal function to handle the receive logic.
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.
dtls_socket(dtls_socket &&)=delete
~dtls_socket()
Destructor. Cleans up OpenSSL resources.
struct ssl_ctx_st SSL_CTX
Definition crypto.h:20
struct ssl_st SSL
Definition crypto.h:21
std::mutex mutex
OpenSSL utilities and version definitions.