chore(net): improve ecies error for unreadable stream (#514)

* chore(net): improve ecies error for unreadable stream

* Update crates/net/ecies/src/error.rs

Co-authored-by: Bjerg <onbjerg@users.noreply.github.com>

Co-authored-by: Bjerg <onbjerg@users.noreply.github.com>
This commit is contained in:
Matthias Seitz
2022-12-19 19:07:19 +01:00
committed by GitHub
parent 8a63ef4025
commit a2849cd81e
2 changed files with 20 additions and 2 deletions

View File

@ -83,6 +83,14 @@ pub enum ECIESErrorImpl {
/// The actual value returned from the peer
msg: Option<IngressECIESValue>,
},
/// Error when the stream was closed by the peer for being unreadable.
///
/// This exact error case happens when the wrapped stream in
/// [`Framed`](tokio_util::codec::Framed) is closed by the peer, See
/// [ConnectionReset](std::io::ErrorKind::ConnectionReset) and the ecies codec fails to decode
/// a message from the (partially filled) buffer.
#[error("Stream closed due to not being readable.")]
UnreadableStream,
}
impl From<ECIESErrorImpl> for ECIESError {

View File

@ -62,13 +62,23 @@ where
transport.send(EgressECIESValue::Auth).await?;
trace!("waiting for ecies ack ...");
let msg = transport.try_next().await?;
// `Framed` returns `None` if the underlying stream is no longer readable, and the codec is
// unable to decode another message from the (partially filled) buffer. This usually happens
// if the remote drops the TcpStream.
let msg = msg.ok_or_else(|| ECIESErrorImpl::UnreadableStream)?;
trace!("parsing ecies ack ...");
if matches!(msg, Some(IngressECIESValue::Ack)) {
if matches!(msg, IngressECIESValue::Ack) {
Ok(Self { stream: transport, remote_id })
} else {
Err(ECIESErrorImpl::InvalidHandshake { expected: IngressECIESValue::Ack, msg }.into())
Err(ECIESErrorImpl::InvalidHandshake {
expected: IngressECIESValue::Ack,
msg: Some(msg),
}
.into())
}
}