Use session RNG for ping

Signed-off-by: Alexis Sellier <alexis@radicle.xyz>
This commit is contained in:
Alexis Sellier 2022-10-18 12:33:12 +02:00
parent 86ed10fca9
commit fbd4fd9fea
No known key found for this signature in database
2 changed files with 14 additions and 13 deletions

View File

@ -492,7 +492,7 @@ where
let peer = self let peer = self
.sessions .sessions
.entry(ip) .entry(ip)
.or_insert_with(|| Session::new(*addr, Link::Outbound, persistent, &self.rng)); .or_insert_with(|| Session::new(*addr, Link::Outbound, persistent, self.rng.clone()));
peer.attempted(); peer.attempted();
} }
@ -533,7 +533,7 @@ where
addr, addr,
Link::Inbound, Link::Inbound,
self.config.is_persistent(&address), self.config.is_persistent(&address),
&self.rng, self.rng.clone(),
), ),
); );
} }
@ -929,7 +929,7 @@ where
.filter(|(_, session)| session.last_active < *now - KEEP_ALIVE_DELTA) .filter(|(_, session)| session.last_active < *now - KEEP_ALIVE_DELTA)
.map(|(_, session)| session); .map(|(_, session)| session);
for session in inactive_sessions { for session in inactive_sessions {
session.ping(&mut self.reactor, &self.rng).ok(); session.ping(&mut self.reactor).ok();
} }
} }

View File

@ -83,7 +83,7 @@ pub struct Session {
} }
impl Session { impl Session {
pub fn new(addr: net::SocketAddr, link: Link, persistent: bool, rng: &Rng) -> Self { pub fn new(addr: net::SocketAddr, link: Link, persistent: bool, rng: Rng) -> Self {
Self { Self {
addr, addr,
state: SessionState::default(), state: SessionState::default(),
@ -92,7 +92,7 @@ impl Session {
persistent, persistent,
last_active: LocalTime::default(), last_active: LocalTime::default(),
attempts: 0, attempts: 0,
rng: rng.clone(), rng,
} }
} }
@ -116,15 +116,16 @@ impl Session {
self.attempts = 0; self.attempts = 0;
} }
pub fn ping(&mut self, reactor: &mut Reactor, rng: &Rng) -> Result<(), SessionError> { pub fn ping(&mut self, reactor: &mut Reactor) -> Result<(), SessionError> {
if let SessionState::Negotiated { ping, .. } = &mut self.state { if let SessionState::Negotiated { ping, .. } = &mut self.state {
let ponglen = rng.u16(0..wire::message::MAX_PAYLOAD_SIZE_BYTES); let ponglen = self.rng.u16(0..wire::message::MAX_PAYLOAD_SIZE_BYTES);
let msg = Message::Ping { let msg =
ponglen, Message::Ping {
zeroes: message::ZeroBytes::new( ponglen,
rng.u16(0..(wire::message::MAX_PAYLOAD_SIZE_BYTES - (size_of::<u16>() as u16))), zeroes: message::ZeroBytes::new(self.rng.u16(
), 0..(wire::message::MAX_PAYLOAD_SIZE_BYTES - (size_of::<u16>() as u16)),
}; )),
};
reactor.write(self.addr, msg); reactor.write(self.addr, msg);
*ping = PingState::AwaitingResponse(ponglen); *ping = PingState::AwaitingResponse(ponglen);