diff --git a/util/src/network/error.rs b/util/src/network/error.rs index 74babb110..31e1d785b 100644 --- a/util/src/network/error.rs +++ b/util/src/network/error.rs @@ -18,21 +18,42 @@ use io::IoError; use crypto::CryptoError; use rlp::*; -#[derive(Debug, Copy, Clone)] +#[derive(Debug, Copy, Clone, PartialEq, Eq)] pub enum DisconnectReason { DisconnectRequested, - _TCPError, - _BadProtocol, + TCPError, + BadProtocol, UselessPeer, - _TooManyPeers, - _DuplicatePeer, - _IncompatibleProtocol, - _NullIdentity, - _ClientQuit, - _UnexpectedIdentity, - _LocalIdentity, + TooManyPeers, + DuplicatePeer, + IncompatibleProtocol, + NullIdentity, + ClientQuit, + UnexpectedIdentity, + LocalIdentity, PingTimeout, + Unknown, +} + +impl DisconnectReason { + pub fn from_u8(n: u8) -> DisconnectReason { + match n { + 0 => DisconnectReason::DisconnectRequested, + 1 => DisconnectReason::TCPError, + 2 => DisconnectReason::BadProtocol, + 3 => DisconnectReason::UselessPeer, + 4 => DisconnectReason::TooManyPeers, + 5 => DisconnectReason::DuplicatePeer, + 6 => DisconnectReason::IncompatibleProtocol, + 7 => DisconnectReason::NullIdentity, + 8 => DisconnectReason::ClientQuit, + 9 => DisconnectReason::UnexpectedIdentity, + 10 => DisconnectReason::LocalIdentity, + 11 => DisconnectReason::PingTimeout, + _ => DisconnectReason::Unknown, + } + } } #[derive(Debug)] @@ -70,3 +91,22 @@ impl From for NetworkError { } } +#[test] +fn test_errors() { + assert_eq!(DisconnectReason::ClientQuit, DisconnectReason::from_u8(8)); + let mut r = DisconnectReason::DisconnectRequested; + for i in 0 .. 20 { + r = DisconnectReason::from_u8(i); + } + assert_eq!(DisconnectReason::Unknown, r); + + match >::from(DecoderError::RlpIsTooBig) { + NetworkError::Auth => {}, + _ => panic!("Unexpeceted error"), + } + + match >::from(CryptoError::InvalidSecret) { + NetworkError::Auth => {}, + _ => panic!("Unexpeceted error"), + } +} diff --git a/util/src/network/session.rs b/util/src/network/session.rs index 572b91a18..b0db5f7ef 100644 --- a/util/src/network/session.rs +++ b/util/src/network/session.rs @@ -231,7 +231,11 @@ impl Session { try!(self.read_hello(&rlp, host)); Ok(SessionData::Ready) }, - PACKET_DISCONNECT => Err(From::from(NetworkError::Disconnect(DisconnectReason::DisconnectRequested))), + PACKET_DISCONNECT => { + let rlp = UntrustedRlp::new(&packet.data[1..]); + let reason: u8 = try!(rlp.val_at(0)); + Err(From::from(NetworkError::Disconnect(DisconnectReason::from_u8(reason)))) + } PACKET_PING => { try!(self.send_pong()); Ok(SessionData::None)