1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use std::io;
use std::fmt;
use failure::{Backtrace, Fail};
#[derive(Debug)]
pub struct HandshakeError {
pub kind: HandshakeErrorKind,
}
#[derive(Debug, Fail)]
pub enum HandshakeErrorKind {
#[fail(display = "First byte of the handshake did not start with a 3")]
BadVersionId,
#[fail(display = "Packet 1's 2nd time field was expected to be empty, but wasn't")]
NonZeroedTimeInPacket1,
#[fail(display = "Peer did not send the correct time back")]
IncorrectPeerTime,
#[fail(display = "Peer did not send the correct random data back")]
IncorrectRandomData,
#[fail(display = "Attempted to continue handshake process after completing handshake")]
HandshakeAlreadyCompleted,
#[fail(display = "No known message format could be determined from the received packet 1")]
UnknownPacket1Format,
#[fail(display = "Invalid handshake packet 2 received")]
InvalidP2Packet,
#[fail(display = "_0")]
Io(#[cause] io::Error)
}
impl fmt::Display for HandshakeError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.kind, f)
}
}
impl Fail for HandshakeError {
fn cause(&self) -> Option<&Fail> {
self.kind.cause()
}
fn backtrace(&self) -> Option<&Backtrace> {
self.kind.backtrace()
}
}
impl From<HandshakeErrorKind> for HandshakeError {
fn from(kind: HandshakeErrorKind) -> Self {
HandshakeError { kind }
}
}
impl From<io::Error> for HandshakeError {
fn from(error: io::Error) -> Self {
HandshakeError { kind: HandshakeErrorKind::Io(error) }
}
}