Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
common_defs.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 <atomic>
8#include <chrono>
9#include <cstdint>
10#include <optional>
11#include <string>
12#include <string_view>
13
14// Use nested namespace definition (C++17)
16{
30 {
38 std::size_t max_pending_bytes{0};
39
47 std::size_t high_water_mark{1024 * 1024};
48
57 std::size_t low_water_mark{256 * 1024};
58 };
59
68 {
69 std::atomic<std::size_t> total_bytes_sent{0};
70 std::atomic<std::size_t> total_bytes_received{0};
71 std::atomic<std::size_t> current_pending_bytes{0};
72 std::atomic<std::size_t> peak_pending_bytes{0};
73 std::atomic<std::size_t> backpressure_events{0};
74 std::atomic<std::size_t> rejected_sends{0};
75 std::atomic<std::size_t> send_count{0};
76 std::atomic<std::size_t> receive_count{0};
77
78 void reset()
79 {
80 total_bytes_sent.store(0);
81 total_bytes_received.store(0);
82 current_pending_bytes.store(0);
83 peak_pending_bytes.store(0);
84 backpressure_events.store(0);
85 rejected_sends.store(0);
86 send_count.store(0);
87 receive_count.store(0);
88 }
89 };
90
100 enum class data_mode : std::uint8_t {
101 packet_mode = 1,
102 file_mode = 2,
103 binary_mode = 3
104 };
105
113 enum class tls_version : std::uint8_t {
114 tls_1_0 = 10,
115 tls_1_1 = 11,
116 tls_1_2 = 12,
117 tls_1_3 = 13
118 };
119
124 enum class certificate_verification : std::uint8_t {
125 none = 0,
126 verify_peer = 1,
128 };
129
163 struct tls_config {
165 bool enabled = false;
166
170
173
176 std::optional<std::string> certificate_file;
177
180 std::optional<std::string> private_key_file;
181
183 std::optional<std::string> private_key_password;
184
187 std::optional<std::string> ca_file;
188
190 std::optional<std::string> ca_path;
191
195 std::optional<std::string> cipher_list;
196
199 std::optional<std::string> sni_hostname;
200
203
205 std::size_t handshake_timeout_ms = 10000;
206
213 [[nodiscard]] auto is_valid() const -> bool {
214 if (!enabled) {
215 return true; // Valid if disabled
216 }
217
218 // If verification is enabled, CA file/path is required
220 if (!ca_file.has_value() && !ca_path.has_value()) {
221 return false;
222 }
223 }
224
225 // Note: Certificate and private key validation depends on whether
226 // this is a server or client configuration, which is context-dependent.
227 // Server-specific validation should be done by the server class.
228
229 return true;
230 }
231
239 [[nodiscard]] static auto insecure_for_testing() -> tls_config {
241 config.enabled = true;
243 return config;
244 }
245
253 [[nodiscard]] static auto secure_defaults() -> tls_config {
255 config.enabled = true;
256 config.min_version = tls_version::tls_1_3;
258 config.enable_session_resumption = true;
259 return config;
260 }
261
269 [[nodiscard]] static auto legacy_compatible() -> tls_config {
271 config.enabled = true;
272 config.min_version = tls_version::tls_1_2;
274 config.enable_session_resumption = true;
275 return config;
276 }
277 };
278
279 // Use inline variables for constants (C++17)
280 inline constexpr std::size_t default_buffer_size = 4096;
281 inline constexpr std::size_t default_timeout_ms = 5000;
282 inline constexpr std::string_view default_client_id = "default_client";
283 inline constexpr std::string_view default_server_id = "default_server";
284
285 // TLS defaults
286 inline constexpr std::string_view default_tls_cipher_list =
287 "ECDHE-RSA-AES256-GCM-SHA384:"
288 "ECDHE-RSA-AES128-GCM-SHA256:"
289 "ECDHE-RSA-CHACHA20-POLY1305";
290
291} // namespace kcenon::network::internal
tracing_config config
Definition exporters.cpp:29
constexpr std::string_view default_server_id
tls_version
TLS protocol versions.
data_mode
Represents a simple enumeration for differentiating data transmission modes.
constexpr std::string_view default_client_id
certificate_verification
Certificate verification modes.
constexpr std::string_view default_tls_cipher_list
constexpr std::size_t default_timeout_ms
constexpr std::size_t default_buffer_size
Configuration for TCP socket backpressure control.
Definition common_defs.h:30
std::size_t high_water_mark
High water mark - trigger backpressure callback.
Definition common_defs.h:47
std::size_t max_pending_bytes
Maximum bytes allowed in pending send buffer.
Definition common_defs.h:38
std::size_t low_water_mark
Low water mark - resume sending.
Definition common_defs.h:57
Runtime metrics for socket monitoring.
Definition common_defs.h:68
std::atomic< std::size_t > backpressure_events
Definition common_defs.h:73
std::atomic< std::size_t > total_bytes_received
Definition common_defs.h:70
std::atomic< std::size_t > peak_pending_bytes
Definition common_defs.h:72
std::atomic< std::size_t > rejected_sends
Definition common_defs.h:74
std::atomic< std::size_t > total_bytes_sent
Definition common_defs.h:69
std::atomic< std::size_t > send_count
Definition common_defs.h:75
std::atomic< std::size_t > receive_count
Definition common_defs.h:76
std::atomic< std::size_t > current_pending_bytes
Definition common_defs.h:71
Configuration for TLS/SSL connections.
std::optional< std::string > certificate_file
Path to server certificate file (PEM format) Required for servers when TLS is enabled.
std::optional< std::string > cipher_list
Cipher suite list (OpenSSL format) Default: Use strong ciphers (TLS 1.2+) Example: "ECDHE-RSA-AES256-...
static auto insecure_for_testing() -> tls_config
Creates a default insecure configuration (testing only)
static auto legacy_compatible() -> tls_config
Creates a backwards-compatible configuration (TLS 1.2+)
std::size_t handshake_timeout_ms
Timeout for TLS handshake in milliseconds.
auto is_valid() const -> bool
Validates the TLS configuration.
bool enabled
Enable TLS/SSL for this connection (default: false)
tls_version min_version
Minimum TLS version to accept (default: TLS 1.3) Note: TLS 1.3 is enforced by default to prevent down...
std::optional< std::string > sni_hostname
Server Name Indication (SNI) hostname for clients Used for virtual hosting and certificate selection.
std::optional< std::string > private_key_file
Path to server private key file (PEM format) Required for servers when TLS is enabled.
std::optional< std::string > ca_path
Path to directory containing CA certificates.
static auto secure_defaults() -> tls_config
Creates a secure default configuration.
std::optional< std::string > private_key_password
Password for encrypted private key (if applicable)
bool enable_session_resumption
Enable session resumption for performance.
std::optional< std::string > ca_file
Path to CA certificate file for verification (PEM format) Required when verify_mode !...
certificate_verification verify_mode
Certificate verification mode (default: verify_peer)