pub struct BandwidthStats {
pub circs: HashMap<String, BwCircuitStat>,
pub live_guard_conns: HashMap<String, BwGuardStat>,
pub guards: HashMap<String, BwGuardStat>,
pub circs_destroyed_total: u64,
pub no_conns_since: Option<f64>,
pub no_circs_since: Option<f64>,
pub network_down_since: Option<f64>,
pub max_fake_id: i32,
pub disconnected_circs: bool,
pub disconnected_conns: bool,
}Expand description
Main bandwidth monitoring state for attack detection.
Tracks all circuit and guard connection statistics for bandwidth monitoring. This is the primary interface for the bandguards protection system.
§Architecture
┌─────────────────────────────────────────────────────────────────────────┐
│ BandwidthStats │
│ │
│ ┌─────────────────────────────────────────────────────────────────┐ │
│ │ Circuit Tracking (circs: HashMap<String, BwCircuitStat>) │ │
│ │ • Per-circuit bandwidth statistics │ │
│ │ • Dropped cell detection │ │
│ │ • Purpose and state tracking │ │
│ └─────────────────────────────────────────────────────────────────┘ │
│ │
│ ┌─────────────────────────────────────────────────────────────────┐ │
│ │ Guard Tracking (guards: HashMap<String, BwGuardStat>) │ │
│ │ • Connection state per guard │ │
│ │ • Killed connection correlation │ │
│ │ • Close reason tracking │ │
│ └─────────────────────────────────────────────────────────────────┘ │
│ │
│ ┌─────────────────────────────────────────────────────────────────┐ │
│ │ Connectivity Tracking │ │
│ │ • no_conns_since: When all guard connections were lost │ │
│ │ • no_circs_since: When all circuits started failing │ │
│ │ • network_down_since: When network liveness went down │ │
│ └─────────────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────────────┘§Event Handling
This struct processes several Tor event types:
| Event | Method | Purpose |
|---|---|---|
| ORCONN | orconn_event | Guard connection state changes |
| CIRC | circ_event | Circuit state changes |
| CIRC_MINOR | circ_minor_event | Purpose changes |
| CIRC_BW | circbw_event | Bandwidth updates |
| BW | check_connectivity | Periodic connectivity checks |
| NETWORK_LIVENESS | network_liveness_event | Network state changes |
§Example
use vanguards_rs::bandguards::BandwidthStats;
use vanguards_rs::config::BandguardsConfig;
let mut stats = BandwidthStats::new();
let config = BandguardsConfig::default();
// Track a guard connection
stats.orconn_event("1", &"A".repeat(40), "CONNECTED", None, 1000.0);
// Track a circuit
stats.circ_event("123", "LAUNCHED", "GENERAL", None, &[], None, 1000.0);
stats.circ_event("123", "BUILT", "GENERAL", None, &["A".repeat(40)], None, 1001.0);
// Check connectivity
let status = stats.check_connectivity(1002.0, &config);§See Also
BwCircuitStat- Per-circuit statisticsBwGuardStat- Per-guard statisticsCircuitLimitResult- Limit check resultsConnectivityStatus- Connectivity check results
Fields§
§circs: HashMap<String, BwCircuitStat>Circuit statistics by circuit ID.
live_guard_conns: HashMap<String, BwGuardStat>Live guard connections by connection ID.
guards: HashMap<String, BwGuardStat>All guard statistics by fingerprint.
circs_destroyed_total: u64Total circuits destroyed.
no_conns_since: Option<f64>Timestamp when all connections were lost (None if connected).
no_circs_since: Option<f64>Timestamp when circuits started failing (None if working).
network_down_since: Option<f64>Timestamp when network went down (None if up).
max_fake_id: i32Maximum fake ID used for initial orconn-status entries.
disconnected_circs: boolWhether we’re currently disconnected (circuits failing).
disconnected_conns: boolWhether we’re currently disconnected (no connections).
Implementations§
Source§impl BandwidthStats
impl BandwidthStats
Sourcepub fn orconn_event(
&mut self,
conn_id: &str,
guard_fp: &str,
status: &str,
reason: Option<&str>,
arrived_at: f64,
)
pub fn orconn_event( &mut self, conn_id: &str, guard_fp: &str, status: &str, reason: Option<&str>, arrived_at: f64, )
Handles an ORCONN event.
Tracks guard connection state changes. When a connection closes, marks any circuits using that guard as possibly destroyed.
§Arguments
conn_id- Connection IDguard_fp- Guard fingerprintstatus- Connection status (CONNECTED, CLOSED, FAILED)reason- Close reason (for CLOSED status)arrived_at- Event timestamp
Sourcepub fn circ_event(
&mut self,
circ_id: &str,
status: &str,
purpose: &str,
hs_state: Option<&str>,
path: &[String],
remote_reason: Option<&str>,
arrived_at: f64,
) -> Option<bool>
pub fn circ_event( &mut self, circ_id: &str, status: &str, purpose: &str, hs_state: Option<&str>, path: &[String], remote_reason: Option<&str>, arrived_at: f64, ) -> Option<bool>
Handles a CIRC event.
Tracks circuit state changes including creation, building, and closure.
§Arguments
circ_id- Circuit IDstatus- Circuit status (LAUNCHED, BUILT, EXTENDED, FAILED, CLOSED)purpose- Circuit purposehs_state- Hidden service statepath- Circuit path (list of relay fingerprints)remote_reason- Remote close reasonarrived_at- Event timestamp
§Returns
Some(true) if circuit was destroyed due to guard connection closure,
Some(false) if circuit was closed normally, None otherwise.
Sourcepub fn circ_minor_event(
&mut self,
circ_id: &str,
event_type: &str,
purpose: &str,
hs_state: Option<&str>,
old_purpose: Option<&str>,
old_hs_state: Option<&str>,
path: &[String],
)
pub fn circ_minor_event( &mut self, circ_id: &str, event_type: &str, purpose: &str, hs_state: Option<&str>, old_purpose: Option<&str>, old_hs_state: Option<&str>, path: &[String], )
Handles a CIRC_MINOR event (purpose changes).
Tracks circuit purpose changes, particularly from HS_VANGUARDS to actual HS purposes.
§Arguments
circ_id- Circuit IDevent_type- Event type (PURPOSE_CHANGED, etc.)purpose- New circuit purposehs_state- New hidden service stateold_purpose- Previous circuit purposeold_hs_state- Previous hidden service statepath- Circuit path
Sourcepub fn circbw_event(
&mut self,
circ_id: &str,
read: u64,
written: u64,
delivered_read: u64,
delivered_written: u64,
overhead_read: u64,
overhead_written: u64,
_arrived_at: f64,
)
pub fn circbw_event( &mut self, circ_id: &str, read: u64, written: u64, delivered_read: u64, delivered_written: u64, overhead_read: u64, overhead_written: u64, _arrived_at: f64, )
Handles a CIRC_BW event (bandwidth update).
Updates circuit bandwidth statistics and checks limits.
§Arguments
circ_id- Circuit IDread- Bytes readwritten- Bytes writtendelivered_read- Delivered read bytesdelivered_written- Delivered written bytesoverhead_read- Overhead read bytesoverhead_written- Overhead written bytesarrived_at- Event timestamp
Sourcepub fn check_circuit_limits(
&self,
circ_id: &str,
config: &BandguardsConfig,
) -> CircuitLimitResult
pub fn check_circuit_limits( &self, circ_id: &str, config: &BandguardsConfig, ) -> CircuitLimitResult
Checks circuit limits and returns circuits that should be closed.
Checks for:
- Dropped cells (potential attack)
- Maximum bytes exceeded
- Maximum HSDIR bytes exceeded
- Maximum service intro bytes exceeded
§Arguments
circ_id- Circuit ID to checkconfig- Bandguards configuration
§Returns
A CircuitLimitResult indicating whether the circuit should be closed
and why.
Sourcepub fn get_aged_circuits(&self, config: &BandguardsConfig) -> Vec<String>
pub fn get_aged_circuits(&self, config: &BandguardsConfig) -> Vec<String>
Sourcepub fn check_connectivity(
&mut self,
now: f64,
config: &BandguardsConfig,
) -> ConnectivityStatus
pub fn check_connectivity( &mut self, now: f64, config: &BandguardsConfig, ) -> ConnectivityStatus
Checks connectivity status and returns warnings if disconnected.
§Arguments
now- Current timestampconfig- Bandguards configuration
§Returns
A ConnectivityStatus indicating the current connectivity state.
Sourcepub fn network_liveness_event(&mut self, status: &str, arrived_at: f64)
pub fn network_liveness_event(&mut self, status: &str, arrived_at: f64)
Handles a NETWORK_LIVENESS event.
§Arguments
status- Network status (“UP” or “DOWN”)arrived_at- Event timestamp
Sourcepub fn circuit_count(&self) -> usize
pub fn circuit_count(&self) -> usize
Returns the number of tracked circuits.
Sourcepub fn live_connection_count(&self) -> usize
pub fn live_connection_count(&self) -> usize
Returns the number of live guard connections.
Trait Implementations§
Source§impl Clone for BandwidthStats
impl Clone for BandwidthStats
Source§fn clone(&self) -> BandwidthStats
fn clone(&self) -> BandwidthStats
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more