1
//! A "receive-only" view of a circuit, as a placeholder for circuits
2
//! that have closed.
3

            
4
use 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)]
11
pub(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

            
17
impl HalfCirc {
18
    /// Create a new HalfCirc that will allow `total_windows` RELAY cells.
19
64
    pub(crate) fn new(total_windows: u16) -> Self {
20
64
        HalfCirc {
21
64
            allow_relay_cells: total_windows,
22
64
        }
23
64
    }
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
208
    pub(crate) fn receive_cell(&mut self) -> Result<()> {
28
208
        if let Some(n) = self.allow_relay_cells.checked_sub(1) {
29
200
            self.allow_relay_cells = n;
30
200
            Ok(())
31
        } else {
32
8
            Err(Error::ChanProto(
33
8
                "Too many cells received on destroyed circuit".into(),
34
8
            ))
35
        }
36
208
    }
37
}