config.rs
1 // Copyright (c) 2025-2026 ACDC Network 2 // This file is part of the alphaos library. 3 // 4 // Alpha Chain | Delta Chain Protocol 5 // International Monetary Graphite. 6 // 7 // Derived from Aleo (https://aleo.org) and ProvableHQ (https://provable.com). 8 // They built world-class ZK infrastructure. We installed the EASY button. 9 // Their cryptography: elegant. Our modifications: bureaucracy-compatible. 10 // Original brilliance: theirs. Robert's Rules: ours. Bugs: definitely ours. 11 // 12 // Original Aleo/ProvableHQ code subject to Apache 2.0 https://www.apache.org/licenses/LICENSE-2.0 13 // All modifications and new work: CC0 1.0 Universal Public Domain Dedication. 14 // No rights reserved. No permission required. No warranty. No refunds. 15 // 16 // https://creativecommons.org/publicdomain/zero/1.0/ 17 // SPDX-License-Identifier: CC0-1.0 18 19 use std::{ 20 io::{self, ErrorKind::*}, 21 net::{IpAddr, Ipv4Addr, SocketAddr}, 22 }; 23 24 #[cfg(doc)] 25 use crate::protocols::{self, Handshake, Reading, Writing}; 26 27 /// The Tcp's configuration. See the source of [`Config::default`] for the defaults. 28 #[derive(Debug, Clone)] 29 pub struct Config { 30 /// A user-friendly identifier of the Tcp. It is visible in the logs, where it allows Tcp instances to be 31 /// distinguished more easily if multiple are run at the same time. 32 /// 33 /// note: If set to `None` when the configuration is initially created, it will be automatically assigned 34 /// (the string representation of) a sequential, zero-based numeric identifier. So this is essentially never 35 /// `None`, in a running node. 36 pub name: Option<String>, 37 /// The IP address the Tcp's connection listener should bind to. 38 /// 39 /// note: If set to `None`, the Tcp will not listen for inbound connections at all. 40 pub listener_ip: Option<IpAddr>, 41 /// The desired listening port of the Tcp. If [`Config::allow_random_port`] is set to `true`, the Tcp 42 /// will attempt to bind its listener to a different port if the desired one is not available. 43 /// 44 /// note: [`Config::listener_ip`] must not be `None` in order for it to have any effect. 45 pub desired_listening_port: Option<u16>, 46 /// Allow listening on a different port if [`Config::desired_listening_port`] is unavailable. 47 /// 48 /// note: [`Config::listener_ip`] must not be `None` in order for it to have any effect. 49 pub allow_random_port: bool, 50 /// The list of IO errors considered fatal and causing the connection to be dropped. 51 /// 52 /// note: Tcp needs to implement the [`Reading`] and/or [`Writing`] protocol in order for it to have any effect. 53 pub fatal_io_errors: Vec<io::ErrorKind>, 54 /// The maximum number of active connections Tcp can maintain at any given time. 55 /// 56 /// note: This number can very briefly be breached by 1 in case of inbound connection attempts. It can never be 57 /// breached by outbound connection attempts, though. 58 pub max_connections: u16, 59 /// The maximum time (in milliseconds) allowed to establish a raw (before the [`Handshake`] protocol) TCP connection. 60 pub connection_timeout_ms: u16, 61 } 62 63 impl Config { 64 /// Initializes a new Tcp configuration with a listener address, 65 /// a maximum number of connections, and the default values. 66 pub fn new(listener_address: SocketAddr, max_connections: u16) -> Self { 67 Self { 68 listener_ip: Some(listener_address.ip()), 69 desired_listening_port: Some(listener_address.port()), 70 max_connections, 71 ..Default::default() 72 } 73 } 74 } 75 76 impl Default for Config { 77 /// Initializes a new Tcp configuration with the default values. 78 fn default() -> Self { 79 fn default_ip() -> Option<IpAddr> { 80 Some(IpAddr::V4(Ipv4Addr::UNSPECIFIED)) 81 } 82 83 Self { 84 name: None, 85 listener_ip: default_ip(), 86 desired_listening_port: None, 87 allow_random_port: true, 88 fatal_io_errors: vec![ConnectionReset, ConnectionAborted, BrokenPipe, InvalidData, UnexpectedEof], 89 max_connections: 100, 90 connection_timeout_ms: 1_000, 91 } 92 } 93 }