BandwidthStats

Struct BandwidthStats 

Source
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:

EventMethodPurpose
ORCONNorconn_eventGuard connection state changes
CIRCcirc_eventCircuit state changes
CIRC_MINORcirc_minor_eventPurpose changes
CIRC_BWcircbw_eventBandwidth updates
BWcheck_connectivityPeriodic connectivity checks
NETWORK_LIVENESSnetwork_liveness_eventNetwork 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

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: u64

Total 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: i32

Maximum fake ID used for initial orconn-status entries.

§disconnected_circs: bool

Whether we’re currently disconnected (circuits failing).

§disconnected_conns: bool

Whether we’re currently disconnected (no connections).

Implementations§

Source§

impl BandwidthStats

Source

pub fn new() -> Self

Creates a new bandwidth stats tracker.

Source

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 ID
  • guard_fp - Guard fingerprint
  • status - Connection status (CONNECTED, CLOSED, FAILED)
  • reason - Close reason (for CLOSED status)
  • arrived_at - Event timestamp
Source

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 ID
  • status - Circuit status (LAUNCHED, BUILT, EXTENDED, FAILED, CLOSED)
  • purpose - Circuit purpose
  • hs_state - Hidden service state
  • path - Circuit path (list of relay fingerprints)
  • remote_reason - Remote close reason
  • arrived_at - Event timestamp
§Returns

Some(true) if circuit was destroyed due to guard connection closure, Some(false) if circuit was closed normally, None otherwise.

Source

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 ID
  • event_type - Event type (PURPOSE_CHANGED, etc.)
  • purpose - New circuit purpose
  • hs_state - New hidden service state
  • old_purpose - Previous circuit purpose
  • old_hs_state - Previous hidden service state
  • path - Circuit path
Source

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 ID
  • read - Bytes read
  • written - Bytes written
  • delivered_read - Delivered read bytes
  • delivered_written - Delivered written bytes
  • overhead_read - Overhead read bytes
  • overhead_written - Overhead written bytes
  • arrived_at - Event timestamp
Source

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 check
  • config - Bandguards configuration
§Returns

A CircuitLimitResult indicating whether the circuit should be closed and why.

Source

pub fn get_aged_circuits(&self, config: &BandguardsConfig) -> Vec<String>

Returns circuits that have exceeded the maximum age.

§Arguments
  • config - Bandguards configuration
§Returns

A list of circuit IDs that should be closed due to age.

Source

pub fn check_connectivity( &mut self, now: f64, config: &BandguardsConfig, ) -> ConnectivityStatus

Checks connectivity status and returns warnings if disconnected.

§Arguments
  • now - Current timestamp
  • config - Bandguards configuration
§Returns

A ConnectivityStatus indicating the current connectivity state.

Source

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
Source

pub fn circuit_count(&self) -> usize

Returns the number of tracked circuits.

Source

pub fn live_connection_count(&self) -> usize

Returns the number of live guard connections.

Trait Implementations§

Source§

impl Clone for BandwidthStats

Source§

fn clone(&self) -> BandwidthStats

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for BandwidthStats

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for BandwidthStats

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more