pub struct ExchangeTable<C: Config, const N: usize = 64> { /* private fields */ }Expand description
The fixed-capacity set of open exchanges.
Capacity is C::SESSIONS * C::EXCHANGES_PER_SESSION, which is what
Config says the node was built for. A full table is
ErrorCode::NoSpace — a value the caller answers BUSY with, never an abort.
Implementations§
Source§impl<C: Config, const N: usize> ExchangeTable<C, N>
impl<C: Config, const N: usize> ExchangeTable<C, N>
Sourcepub fn new(first_id: u16) -> Self
pub fn new(first_id: u16) -> Self
An empty table whose first exchange id is first_id.
first_id should come from Rng: starting at zero every
boot makes a node’s exchange ids predictable, and predictable ids make it cheaper
for an off-path attacker to guess one that is live.
Sourcepub fn capacity_remaining(&self) -> usize
pub fn capacity_remaining(&self) -> usize
How many more will fit.
Sourcepub fn open_initiator(
&mut self,
session: SessionId,
protocol: ProtocolId,
params: MrpParams,
peer: Option<Peer>,
now: Instant,
) -> Result<ExchangeKey>
pub fn open_initiator( &mut self, session: SessionId, protocol: ProtocolId, params: MrpParams, peer: Option<Peer>, now: Instant, ) -> Result<ExchangeKey>
Opens an exchange this node initiates, choosing an unused id.
Sourcepub fn open_responder(
&mut self,
session: SessionId,
id: ExchangeId,
protocol: ProtocolId,
params: MrpParams,
now: Instant,
) -> Result<ExchangeKey>
pub fn open_responder( &mut self, session: SessionId, id: ExchangeId, protocol: ProtocolId, params: MrpParams, now: Instant, ) -> Result<ExchangeKey>
Opens an exchange the peer initiated.
Returns ErrorCode::InvalidState if one with that key is already open — a peer
reusing a live id is either confused or hostile, and silently adopting the second
one would let it hijack the first.
Sourcepub fn open_ephemeral(
&mut self,
session: SessionId,
id: ExchangeId,
protocol: ProtocolId,
params: MrpParams,
now: Instant,
) -> Result<ExchangeKey>
pub fn open_ephemeral( &mut self, session: SessionId, id: ExchangeId, protocol: ProtocolId, params: MrpParams, now: Instant, ) -> Result<ExchangeKey>
Opens §4.10.5.2’s ephemeral exchange: one that exists only to carry a standalone acknowledgement and is closed as soon as it has.
Sourcepub fn reap(&mut self, now: Instant, idle: Duration) -> usize
pub fn reap(&mut self, now: Instant, idle: Duration) -> usize
Closes every exchange nothing has used for idle and nothing is pending on.
Returns how many went. Messaging::poll calls
this, so a node that drives its timers at all gets it without asking — which is the
point: an exchange table that is only reclaimed when the application remembers to
reclaim it is one an attacker reclaims on its behalf.
Sourcepub fn reap_deadline(&self, idle: Duration) -> Option<Instant>
pub fn reap_deadline(&self, idle: Duration) -> Option<Instant>
When ExchangeTable::reap would next have something to do.
Sourcepub fn find(&self, key: ExchangeKey) -> Option<&Exchange>
pub fn find(&self, key: ExchangeKey) -> Option<&Exchange>
The exchange with this key, if it is open.
Sourcepub fn find_mut(&mut self, key: ExchangeKey) -> Option<&mut Exchange>
pub fn find_mut(&mut self, key: ExchangeKey) -> Option<&mut Exchange>
The exchange with this key, mutably.
Sourcepub fn find_for_message(
&self,
session: SessionId,
id: ExchangeId,
from_initiator: bool,
) -> Option<&Exchange>
pub fn find_for_message( &self, session: SessionId, id: ExchangeId, from_initiator: bool, ) -> Option<&Exchange>
Finds the exchange an arriving message belongs to.
from_initiator is the message’s I flag. A message from the initiator
belongs to the exchange in which this node is the responder, and the other way
round — getting this inversion wrong is how a node answers its own request.
Sourcepub fn close(&mut self, key: ExchangeKey) -> bool
pub fn close(&mut self, key: ExchangeKey) -> bool
Closes an exchange, returning whether it was open.
Sourcepub fn close_session(&mut self, session: SessionId) -> usize
pub fn close_session(&mut self, session: SessionId) -> usize
Closes every exchange on a session — what tearing the session down does.
Sourcepub fn poll_deadline(&self) -> Option<Instant>
pub fn poll_deadline(&self) -> Option<Instant>
The earliest deadline across every open exchange, which is when the caller’s run
loop should next call ExchangeTable::on_timeout.
Sourcepub fn on_timeout(
&mut self,
now: Instant,
randomness: u32,
on_action: impl FnMut(ExchangeKey, OnTimeout),
) -> usize
pub fn on_timeout( &mut self, now: Instant, randomness: u32, on_action: impl FnMut(ExchangeKey, OnTimeout), ) -> usize
Hands a passed deadline to every exchange that has one, calling on_action for
each thing that falls out.
Returns how many actions were produced.
Trait Implementations§
Source§impl<C: Config, const N: usize> Capacity for ExchangeTable<C, N>
impl<C: Config, const N: usize> Capacity for ExchangeTable<C, N>
Source§const PER_FABRIC: usize = N
const PER_FABRIC: usize = N
The specification sets no per-fabric exchange guarantee, so the whole table is the answer: an exchange is opened and closed inside one interaction, and §4.10.5.3’s reclamation is what keeps a stranger from holding them.