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
13
    pub(crate) fn new(total_windows: u16) -> Self {
20
13
        HalfCirc {
21
13
            allow_relay_cells: total_windows,
22
13
        }
23
13
    }
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
104
        if let Some(n) = self.allow_relay_cells.checked_sub(1) {
29
100
            self.allow_relay_cells = n;
30
100
            Ok(())
31
        } else {
32
4
            Err(Error::ChanProto(
33
4
                "Too many cells received on destroyed circuit".into(),
34
4
            ))
35
        }
36
104
    }
37
}