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
.sessions
.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();
}
@ -533,7 +533,7 @@ where
addr,
Link::Inbound,
self.config.is_persistent(&address),
&self.rng,
self.rng.clone(),
),
);
}
@ -929,7 +929,7 @@ where
.filter(|(_, session)| session.last_active < *now - KEEP_ALIVE_DELTA)
.map(|(_, session)| session);
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 {
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 {
addr,
state: SessionState::default(),
@ -92,7 +92,7 @@ impl Session {
persistent,
last_active: LocalTime::default(),
attempts: 0,
rng: rng.clone(),
rng,
}
}
@ -116,14 +116,15 @@ impl Session {
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 {
let ponglen = rng.u16(0..wire::message::MAX_PAYLOAD_SIZE_BYTES);
let msg = Message::Ping {
let ponglen = self.rng.u16(0..wire::message::MAX_PAYLOAD_SIZE_BYTES);
let msg =
Message::Ping {
ponglen,
zeroes: message::ZeroBytes::new(
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);