2016-02-05 13:40:41 +01:00
|
|
|
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-01-15 16:36:08 +01:00
|
|
|
use io::IoError;
|
2016-09-01 14:49:12 +02:00
|
|
|
use rlp::*;
|
2016-08-05 10:32:04 +02:00
|
|
|
use util::UtilError;
|
2016-05-21 00:12:51 +02:00
|
|
|
use std::fmt;
|
2016-08-24 18:35:21 +02:00
|
|
|
use ethkey::Error as KeyError;
|
|
|
|
use crypto::Error as CryptoError;
|
2016-01-15 16:36:08 +01:00
|
|
|
|
2016-02-17 14:07:26 +01:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
2016-01-15 16:36:08 +01:00
|
|
|
pub enum DisconnectReason
|
|
|
|
{
|
|
|
|
DisconnectRequested,
|
2016-02-17 14:07:26 +01:00
|
|
|
TCPError,
|
|
|
|
BadProtocol,
|
2016-01-15 16:36:08 +01:00
|
|
|
UselessPeer,
|
2016-02-17 14:07:26 +01:00
|
|
|
TooManyPeers,
|
|
|
|
DuplicatePeer,
|
|
|
|
IncompatibleProtocol,
|
|
|
|
NullIdentity,
|
|
|
|
ClientQuit,
|
|
|
|
UnexpectedIdentity,
|
|
|
|
LocalIdentity,
|
2016-02-02 20:58:12 +01:00
|
|
|
PingTimeout,
|
2016-02-17 14:07:26 +01:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
2016-01-15 16:36:08 +01:00
|
|
|
}
|
|
|
|
|
2016-05-21 00:12:51 +02:00
|
|
|
impl fmt::Display for DisconnectReason {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
use self::DisconnectReason::*;
|
|
|
|
|
|
|
|
let msg = match *self {
|
|
|
|
DisconnectRequested => "disconnect requested",
|
|
|
|
TCPError => "TCP error",
|
|
|
|
BadProtocol => "bad protocol",
|
|
|
|
UselessPeer => "useless peer",
|
|
|
|
TooManyPeers => "too many peers",
|
|
|
|
DuplicatePeer => "duplicate peer",
|
|
|
|
IncompatibleProtocol => "incompatible protocol",
|
|
|
|
NullIdentity => "null identity",
|
|
|
|
ClientQuit => "client quit",
|
|
|
|
UnexpectedIdentity => "unexpected identity",
|
|
|
|
LocalIdentity => "local identity",
|
|
|
|
PingTimeout => "ping timeout",
|
|
|
|
Unknown => "unknown",
|
|
|
|
};
|
|
|
|
|
|
|
|
f.write_str(msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-15 16:36:08 +01:00
|
|
|
#[derive(Debug)]
|
2016-01-22 00:11:19 +01:00
|
|
|
/// Network error.
|
2016-01-15 16:36:08 +01:00
|
|
|
pub enum NetworkError {
|
2016-01-22 00:11:19 +01:00
|
|
|
/// Authentication error.
|
2016-01-15 16:36:08 +01:00
|
|
|
Auth,
|
2016-01-22 00:11:19 +01:00
|
|
|
/// Unrecognised protocol.
|
2016-01-15 16:36:08 +01:00
|
|
|
BadProtocol,
|
2016-02-13 22:57:39 +01:00
|
|
|
/// Message expired.
|
|
|
|
Expired,
|
2016-01-22 00:11:19 +01:00
|
|
|
/// Peer not found.
|
2016-01-15 16:36:08 +01:00
|
|
|
PeerNotFound,
|
2016-01-22 00:11:19 +01:00
|
|
|
/// Peer is diconnected.
|
2016-01-15 16:36:08 +01:00
|
|
|
Disconnect(DisconnectReason),
|
2016-08-05 10:32:04 +02:00
|
|
|
/// Util error.
|
|
|
|
Util(UtilError),
|
2016-01-22 00:11:19 +01:00
|
|
|
/// Socket IO error.
|
2016-01-15 16:36:08 +01:00
|
|
|
Io(IoError),
|
2016-08-05 10:32:04 +02:00
|
|
|
/// Error concerning the network address parsing subsystem.
|
|
|
|
AddressParse(::std::net::AddrParseError),
|
|
|
|
/// Error concerning the network address resolution subsystem.
|
|
|
|
AddressResolve(Option<::std::io::Error>),
|
|
|
|
/// Error concerning the Rust standard library's IO subsystem.
|
|
|
|
StdIo(::std::io::Error),
|
2016-01-15 16:36:08 +01:00
|
|
|
}
|
|
|
|
|
2016-05-21 00:12:51 +02:00
|
|
|
impl fmt::Display for NetworkError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
use self::NetworkError::*;
|
|
|
|
|
|
|
|
let msg = match *self {
|
|
|
|
Auth => "Authentication failure".into(),
|
|
|
|
BadProtocol => "Bad protocol".into(),
|
|
|
|
Expired => "Expired message".into(),
|
|
|
|
PeerNotFound => "Peer not found".into(),
|
|
|
|
Disconnect(ref reason) => format!("Peer disconnected: {}", reason),
|
|
|
|
Io(ref err) => format!("Socket I/O error: {}", err),
|
2016-08-05 10:32:04 +02:00
|
|
|
AddressParse(ref err) => format!("{}", err),
|
|
|
|
AddressResolve(Some(ref err)) => format!("{}", err),
|
|
|
|
AddressResolve(_) => "Failed to resolve network address.".into(),
|
|
|
|
StdIo(ref err) => format!("{}", err),
|
|
|
|
Util(ref err) => format!("{}", err),
|
2016-05-21 00:12:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
f.write_fmt(format_args!("Network error ({})", msg))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-15 16:36:08 +01:00
|
|
|
impl From<DecoderError> for NetworkError {
|
|
|
|
fn from(_err: DecoderError) -> NetworkError {
|
|
|
|
NetworkError::Auth
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-05 10:32:04 +02:00
|
|
|
impl From<::std::io::Error> for NetworkError {
|
|
|
|
fn from(err: ::std::io::Error) -> NetworkError {
|
|
|
|
NetworkError::StdIo(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-15 16:36:08 +01:00
|
|
|
impl From<IoError> for NetworkError {
|
|
|
|
fn from(err: IoError) -> NetworkError {
|
|
|
|
NetworkError::Io(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-05 10:32:04 +02:00
|
|
|
impl From<UtilError> for NetworkError {
|
|
|
|
fn from(err: UtilError) -> NetworkError {
|
|
|
|
NetworkError::Util(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-24 18:35:21 +02:00
|
|
|
impl From<KeyError> for NetworkError {
|
|
|
|
fn from(_err: KeyError) -> Self {
|
|
|
|
NetworkError::Auth
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-12 09:52:32 +01:00
|
|
|
impl From<CryptoError> for NetworkError {
|
|
|
|
fn from(_err: CryptoError) -> NetworkError {
|
|
|
|
NetworkError::Auth
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-05 10:32:04 +02:00
|
|
|
impl From<::std::net::AddrParseError> for NetworkError {
|
|
|
|
fn from(err: ::std::net::AddrParseError) -> NetworkError {
|
|
|
|
NetworkError::AddressParse(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-17 14:07:26 +01:00
|
|
|
#[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 <NetworkError as From<DecoderError>>::from(DecoderError::RlpIsTooBig) {
|
|
|
|
NetworkError::Auth => {},
|
|
|
|
_ => panic!("Unexpeceted error"),
|
|
|
|
}
|
|
|
|
|
2016-08-24 18:35:21 +02:00
|
|
|
match <NetworkError as From<CryptoError>>::from(CryptoError::InvalidMessage) {
|
2016-02-17 14:07:26 +01:00
|
|
|
NetworkError::Auth => {},
|
|
|
|
_ => panic!("Unexpeceted error"),
|
|
|
|
}
|
|
|
|
}
|