Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore non-verack and non-version messages in handshake #3522

Merged
merged 4 commits into from
Feb 16, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 36 additions & 8 deletions zebra-network/src/peer/handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,13 +588,29 @@ where
debug!(?our_version, "sending initial version message");
peer_conn.send(our_version).await?;

let remote_msg = peer_conn
let mut remote_msg = peer_conn
.next()
.await
.ok_or(HandshakeError::ConnectionClosed)??;

// Check that we got a Version and destructure its fields into the local scope.
debug!(?remote_msg, "got message from remote peer");
// Wait for next message if the one we got is not Version
loop {
match remote_msg {
Message::Version { .. } => {
debug!(?remote_msg, "got version message from remote peer");
break;
}
_ => {
remote_msg = peer_conn
.next()
.await
.ok_or(HandshakeError::ConnectionClosed)??;
debug!(?remote_msg, "ignoring non-version message from remote peer");
}
}
}

// If we got a Version message, destructure its fields into the local scope.
let (remote_nonce, remote_services, remote_version, remote_canonical_addr, user_agent) =
if let Message::Version {
version,
Expand Down Expand Up @@ -700,14 +716,26 @@ where

peer_conn.send(Message::Verack).await?;

let remote_msg = peer_conn
let mut remote_msg = peer_conn
.next()
.await
.ok_or(HandshakeError::ConnectionClosed)??;
if let Message::Verack = remote_msg {
debug!("got verack from remote peer");
} else {
Err(HandshakeError::UnexpectedMessage(Box::new(remote_msg)))?;

// Wait for next message if the one we got is not Verack
loop {
match remote_msg {
Message::Verack => {
debug!(?remote_msg, "got verack message from remote peer");
break;
}
_ => {
remote_msg = peer_conn
.next()
.await
.ok_or(HandshakeError::ConnectionClosed)??;
debug!(?remote_msg, "ignoring non-verack message from remote peer");
}
}
}

Ok((remote_version, remote_services, remote_canonical_addr))
Expand Down