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