bitfold_core/config.rs
1use std::{default::Default, time::Duration};
2
3use super::constants::{DEFAULT_MTU, FRAGMENT_SIZE_DEFAULT, MAX_FRAGMENTS_DEFAULT};
4
5/// Compression algorithm to use for packet data.
6#[derive(Clone, Debug, Copy, PartialEq, Eq)]
7pub enum CompressionAlgorithm {
8 /// No compression
9 None,
10 /// Zlib compression (balanced speed/ratio)
11 Zlib,
12 /// LZ4 compression (fast, lower ratio)
13 Lz4,
14}
15
16#[derive(Clone, Debug)]
17/// Configuration options to tune protocol and runtime behavior.
18pub struct Config {
19 /// Make the underlying UDP socket block when true, otherwise non-blocking.
20 pub blocking_mode: bool,
21 /// Max idle time before considering a connection disconnected.
22 pub idle_connection_timeout: Duration,
23 /// Interval for heartbeat packets if no data is sent. None disables heartbeats.
24 pub heartbeat_interval: Option<Duration>,
25 /// Max total packet size in bytes (including fragmentation).
26 pub max_packet_size: usize,
27 /// Max number of fragments per packet (u8).
28 pub max_fragments: u8,
29 /// Max size of each fragment.
30 pub fragment_size: u16,
31 /// Size of the fragment reassembly buffer.
32 pub fragment_reassembly_buffer_size: u16,
33 /// Max receive buffer size in bytes.
34 pub receive_buffer_max_size: usize,
35 /// Smoothing factor (0..1) for RTT measurements.
36 pub rtt_smoothing_factor: f32,
37 /// Max acceptable RTT in milliseconds before considering a problem.
38 pub rtt_max_value: u16,
39 /// Size of the event buffer for socket events.
40 pub socket_event_buffer_size: usize,
41 /// How long to block when polling socket events.
42 pub socket_polling_timeout: Option<Duration>,
43 /// Max reliable packets in flight before dropping a connection.
44 pub max_packets_in_flight: u16,
45 /// Max number of unestablished connections to prevent DoS.
46 pub max_unestablished_connections: u16,
47 /// Number of channels per peer connection (1-255).
48 pub channel_count: u8,
49 /// Incoming bandwidth limit in bytes/sec (0 = unlimited).
50 pub incoming_bandwidth_limit: u32,
51 /// Outgoing bandwidth limit in bytes/sec (0 = unlimited).
52 pub outgoing_bandwidth_limit: u32,
53 /// Enable CRC32 checksums for data integrity verification (default: false).
54 pub use_checksums: bool,
55 /// Compression algorithm to use (default: None).
56 pub compression: CompressionAlgorithm,
57 /// Minimum packet size to compress in bytes (default: 128). Packets smaller than this won't be compressed.
58 pub compression_threshold: usize,
59 /// Use formal 3-way connection handshake for enhanced security (default: false).
60 /// When enabled, uses Connect->VerifyConnect->ACK handshake with session IDs.
61 pub use_connection_handshake: bool,
62 /// Maximum buffered packet data per peer in bytes (0 = unlimited).
63 /// Prevents memory exhaustion from malicious/buggy clients.
64 pub max_waiting_data: usize,
65 /// Enable advanced packet throttling with acceleration/deceleration.
66 /// When enabled, uses dynamic throttle adjustment based on packet loss.
67 pub use_advanced_throttling: bool,
68 /// Throttle scale (maximum throttle value, typically 32).
69 pub throttle_scale: u32,
70 /// Throttle acceleration (rate of improvement when conditions are good).
71 pub throttle_acceleration: u32,
72 /// Throttle deceleration (rate of degradation when packet loss occurs).
73 pub throttle_deceleration: u32,
74 /// Interval for throttle updates in milliseconds.
75 pub throttle_interval: u32,
76 /// Enable dynamic window-based flow control.
77 /// When enabled, uses adaptive window sizing based on network conditions.
78 pub use_window_flow_control: bool,
79 /// Initial window size for flow control (in packets).
80 pub initial_window_size: u32,
81 /// Minimum window size (in packets).
82 pub min_window_size: u32,
83 /// Maximum window size (in packets).
84 pub max_window_size: u32,
85 /// Maximum number of connections allowed from the same IP address (0 = unlimited).
86 /// Useful for NAT scenarios where multiple clients share the same public IP.
87 pub max_duplicate_peers: u16,
88 /// Socket receive buffer size in bytes (None = use system default).
89 /// Corresponds to SO_RCVBUF socket option.
90 pub socket_recv_buffer_size: Option<usize>,
91 /// Socket send buffer size in bytes (None = use system default).
92 /// Corresponds to SO_SNDBUF socket option.
93 pub socket_send_buffer_size: Option<usize>,
94 /// Time-to-live for outgoing packets (None = use system default).
95 /// Corresponds to IP_TTL socket option.
96 pub socket_ttl: Option<u32>,
97 /// Enable broadcast mode (default: false).
98 /// Corresponds to SO_BROADCAST socket option.
99 pub socket_broadcast: bool,
100 /// Enable application-level PMTU discovery (default: false).
101 /// Sends probe commands with increasing sizes and tunes per-peer fragment size.
102 pub use_pmtu_discovery: bool,
103 /// Minimum PMTU probe size (bytes).
104 pub pmtu_min: u16,
105 /// Maximum PMTU probe size (bytes).
106 pub pmtu_max: u16,
107 /// Interval between PMTU probes in milliseconds.
108 pub pmtu_interval_ms: u32,
109 /// Threshold (bytes) at which PMTU search is considered converged.
110 pub pmtu_converge_threshold: u16,
111}
112
113impl Default for Config {
114 fn default() -> Self {
115 Self {
116 blocking_mode: false,
117 idle_connection_timeout: Duration::from_secs(5),
118 heartbeat_interval: None,
119 max_packet_size: (MAX_FRAGMENTS_DEFAULT * FRAGMENT_SIZE_DEFAULT) as usize,
120 max_fragments: MAX_FRAGMENTS_DEFAULT as u8,
121 fragment_size: FRAGMENT_SIZE_DEFAULT,
122 fragment_reassembly_buffer_size: 64,
123 receive_buffer_max_size: DEFAULT_MTU as usize,
124 rtt_smoothing_factor: 0.10,
125 rtt_max_value: 250,
126 socket_event_buffer_size: 1024,
127 socket_polling_timeout: Some(Duration::from_millis(1)),
128 max_packets_in_flight: 512,
129 max_unestablished_connections: 50,
130 channel_count: 1, // Default to single channel like most simple uses
131 incoming_bandwidth_limit: 2_097_152, // 2 MB/s - DoS protection (0 = unlimited)
132 outgoing_bandwidth_limit: 2_097_152, // 2 MB/s - prevents bandwidth abuse (0 = unlimited)
133 use_checksums: true, // Enabled for data integrity protection
134 compression: CompressionAlgorithm::None, // Disabled by default
135 compression_threshold: 128, // Don't compress packets smaller than 128 bytes
136 use_connection_handshake: true, // Enabled for enhanced security with 3-way handshake
137 max_waiting_data: 32 * 1024 * 1024, // 32 MB - prevents memory exhaustion
138 use_advanced_throttling: false, // Disabled by default for backward compatibility
139 throttle_scale: 32, // Default scale
140 throttle_acceleration: 2, // Default acceleration
141 throttle_deceleration: 2, // Default deceleration
142 throttle_interval: 5000, // 5 seconds (default)
143 use_window_flow_control: false, // Disabled by default for backward compatibility
144 initial_window_size: 512, // Start with 512 packets (matches max_packets_in_flight)
145 min_window_size: 64, // Minimum 64 packets
146 max_window_size: 4096, // Maximum 4096 packets
147 max_duplicate_peers: 0, // Unlimited by default
148 socket_recv_buffer_size: None, // Use system default
149 socket_send_buffer_size: None, // Use system default
150 socket_ttl: None, // Use system default
151 socket_broadcast: false, // Disabled by default
152 use_pmtu_discovery: true,
153 pmtu_min: 576,
154 pmtu_max: 1400,
155 pmtu_interval_ms: 5000,
156 pmtu_converge_threshold: 64,
157 }
158 }
159}