Two-stage connectivity with handshake

This commit is contained in:
Dr. Maxim Orlovsky 2022-11-21 15:28:39 +01:00 committed by Alexis Sellier
parent 008681a2ca
commit 983b2dc534
No known key found for this signature in database
5 changed files with 31 additions and 18 deletions

View File

@ -501,13 +501,15 @@ where
peer.attempted(); peer.attempted();
} }
// TODO: Split into two functions: `connected` and `negotiated` pub fn connecting(
pub fn connected(
&mut self, &mut self,
addr: std::net::SocketAddr, _addr: net::SocketAddr,
_local_addr: &std::net::SocketAddr, _local_addr: &net::SocketAddr,
link: Link, _link: Link,
) { ) {
}
pub fn connected(&mut self, addr: net::SocketAddr, link: Link) {
let ip = addr.ip(); let ip = addr.ip();
let address = addr.into(); let address = addr.into();

View File

@ -210,7 +210,8 @@ where
let local = net::SocketAddr::new(self.ip, self.rng.u16(..)); let local = net::SocketAddr::new(self.ip, self.rng.u16(..));
self.initialize(); self.initialize();
self.service.connected(remote, &local, Link::Inbound); self.service.connecting(remote, &local, Link::Inbound);
self.service.connected(remote, Link::Inbound);
self.receive( self.receive(
&remote, &remote,
Message::init(peer.node_id(), vec![Address::from(remote)]), Message::init(peer.node_id(), vec![Address::from(remote)]),
@ -237,7 +238,8 @@ where
self.initialize(); self.initialize();
self.service.attempted(&remote); self.service.attempted(&remote);
self.service self.service
.connected(remote, &self.local_addr, Link::Outbound); .connecting(remote, &self.local_addr, Link::Outbound);
self.service.connected(remote, Link::Outbound);
let mut msgs = self.messages(&remote); let mut msgs = self.messages(&remote);
msgs.find(|m| matches!(m, Message::Initialize { .. })) msgs.find(|m| matches!(m, Message::Initialize { .. }))

View File

@ -385,7 +385,8 @@ impl<S: WriteStorage + 'static, G: Signer> Simulation<S, G> {
let attempted = link.is_outbound() && self.attempts.remove(&conn); let attempted = link.is_outbound() && self.attempts.remove(&conn);
if attempted || link.is_inbound() { if attempted || link.is_inbound() {
if self.connections.insert(conn, local_addr.port()).is_none() { if self.connections.insert(conn, local_addr.port()).is_none() {
p.connected(addr, &local_addr, link); p.connecting(addr, &local_addr, link);
p.connected(addr, link);
} }
} }
} }

View File

@ -482,8 +482,8 @@ where
} }
fn connected(&mut self, addr: net::SocketAddr, local_addr: &net::SocketAddr, link: Link) { fn connected(&mut self, addr: net::SocketAddr, local_addr: &net::SocketAddr, link: Link) {
self.handshakes.insert(addr, H::new()); self.handshakes.insert(addr, H::new(link));
self.inner.connected(addr, local_addr, link) self.inner.connecting(addr, local_addr, link)
} }
fn disconnected( fn disconnected(
@ -509,7 +509,7 @@ where
} }
return; return;
} }
HandshakeResult::Complete(transcoder, reply) => { HandshakeResult::Complete(transcoder, reply, link) => {
log::debug!("handshake with peer {} is complete", addr); log::debug!("handshake with peer {} is complete", addr);
if !reply.is_empty() { if !reply.is_empty() {
self.inner_queue self.inner_queue
@ -522,6 +522,7 @@ where
deserializer: Deserializer::new(256), deserializer: Deserializer::new(256),
}, },
); );
self.inner.connected(*addr, link);
} }
HandshakeResult::Error(err) => { HandshakeResult::Error(err) => {
log::error!("invalid handshake input. Details: {}", err); log::error!("invalid handshake input. Details: {}", err);

View File

@ -1,3 +1,4 @@
use nakamoto_net::Link;
use std::convert::Infallible; use std::convert::Infallible;
// TODO: Implement Try trait once stabilized // TODO: Implement Try trait once stabilized
@ -6,7 +7,7 @@ pub enum HandshakeResult<H: Handshake, T: Transcode> {
/// Handshake is not completed; we proceed to the next handshake stage. /// Handshake is not completed; we proceed to the next handshake stage.
Next(H, Vec<u8>), Next(H, Vec<u8>),
/// Handshake is completed; we now can communicate in a secure way. /// Handshake is completed; we now can communicate in a secure way.
Complete(T, Vec<u8>), Complete(T, Vec<u8>, Link),
/// Handshake has failed with some error. /// Handshake has failed with some error.
Error(H::Error), Error(H::Error),
} }
@ -21,25 +22,31 @@ pub trait Handshake: Sized {
type Error: std::error::Error; type Error: std::error::Error;
/// Create a new handshake state-machine. /// Create a new handshake state-machine.
fn new() -> Self; fn new(link: Link) -> Self;
/// Advance the state-machine to the next state. /// Advance the state-machine to the next state.
fn step(self, input: &[u8]) -> HandshakeResult<Self, Self::Transcoder>; fn step(self, input: &[u8]) -> HandshakeResult<Self, Self::Transcoder>;
/// Returns direction of the handshake protocol.
fn link(&self) -> Link;
} }
/// Dumb handshake structure which runs void protocol. /// Dumb handshake structure which runs void protocol.
#[derive(Debug, Default)] #[derive(Debug)]
pub struct NoHandshake; pub struct NoHandshake(Link);
impl Handshake for NoHandshake { impl Handshake for NoHandshake {
type Transcoder = PlainTranscoder; type Transcoder = PlainTranscoder;
type Error = Infallible; type Error = Infallible;
fn new() -> Self { fn new(link: Link) -> Self {
NoHandshake NoHandshake(link)
} }
fn step(self, _input: &[u8]) -> HandshakeResult<Self, Self::Transcoder> { fn step(self, _input: &[u8]) -> HandshakeResult<Self, Self::Transcoder> {
HandshakeResult::Complete(PlainTranscoder, vec![]) HandshakeResult::Complete(PlainTranscoder, vec![], self.0)
}
fn link(&self) -> Link {
self.0
} }
} }