tor_proto/tunnel/circuit/
halfcirc.rs

1//! A "receive-only" view of a circuit, as a placeholder for circuits
2//! that have closed.
3
4use crate::{Error, Result};
5
6/// A HalfCirc represents the receive-only aspects of a circuit, for
7/// use to represent closed circuits and make sure that only
8/// acceptable data is received there.
9// TODO: This should probably have an expiration time too.
10#[derive(Debug, Clone)]
11pub(crate) struct HalfCirc {
12    /// How many RELAY cells will we accept on this circuit before we
13    /// conclude that somebody is violating the protocols?
14    allow_relay_cells: u16,
15}
16
17impl HalfCirc {
18    /// Create a new HalfCirc that will allow `total_windows` RELAY cells.
19    pub(crate) fn new(total_windows: u16) -> Self {
20        HalfCirc {
21            allow_relay_cells: total_windows,
22        }
23    }
24
25    /// Try receiving a relay cell on this circuit. Give an error if there
26    /// have been too many such cells to believe.
27    pub(crate) fn receive_cell(&mut self) -> Result<()> {
28        if let Some(n) = self.allow_relay_cells.checked_sub(1) {
29            self.allow_relay_cells = n;
30            Ok(())
31        } else {
32            Err(Error::ChanProto(
33                "Too many cells received on destroyed circuit".into(),
34            ))
35        }
36    }
37}