mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 19:09:54 +00:00
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:
@ -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 {
|
||||
|
||||
@ -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())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user