Skip to content

Commit

Permalink
Add v1 only detection
Browse files Browse the repository at this point in the history
  • Loading branch information
nyonson committed Oct 3, 2024
1 parent fdb97db commit 81b56ea
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,11 @@ impl<'a> Handshake<'a> {
#[cfg(feature = "std")]
#[derive(Debug)]
pub enum ProtocolError {
/// Wrap an IO errors.
Io(std::io::Error),
/// Remote party likely only supports V1 protocol.
IncompatibleProtocol,
/// Internal protocol specific errors.
Internal(Error),
}

Expand All @@ -1001,8 +1005,11 @@ impl std::error::Error for ProtocolError {}
impl fmt::Display for ProtocolError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ProtocolError::Io(e) => write!(f, "IO error: {:?}", e),
ProtocolError::Internal(e) => write!(f, "Internal error: {:?}", e),
ProtocolError::Io(e) => write!(f, "IO error: {:?}.", e),
ProtocolError::Internal(e) => write!(f, "Internal error: {:?}.", e),
ProtocolError::IncompatibleProtocol => {
write!(f, "Remote likely only supports V1 protocol.")
}
}
}
}
Expand Down Expand Up @@ -1061,7 +1068,16 @@ where

// Read remote's initial key.
let mut remote_ellswift_buffer = [0u8; 64];
reader.read_exact(&mut remote_ellswift_buffer).await?;
// Try to detect errors which likely mean the remote doesn't understand
// the V2 protocol and the caller should re-attempt on V1 if they want.
if let Err(e) = reader.read_exact(&mut remote_ellswift_buffer).await {
return match e.kind() {
std::io::ErrorKind::UnexpectedEof
| std::io::ErrorKind::ConnectionReset
| std::io::ErrorKind::TimedOut => Err(ProtocolError::IncompatibleProtocol),
_ => Err(ProtocolError::Io(e)),
};
}

let num_version_packet_bytes = PacketWriter::required_packet_allocation(&VERSION_CONTENT);
let num_decoy_packets_bytes: usize = match decoys {
Expand Down

0 comments on commit 81b56ea

Please sign in to comment.