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
85
86
87
88
89
90
91
92
93
94
95
96
97
use crate::bytes;
use crate::Parser;
#[derive(Clone, Copy)]
pub struct TelnetIAC {
pub command: u8,
}
impl TelnetIAC {
pub fn new(command: u8) -> Self {
Self { command }
}
pub fn into_bytes(self) -> Vec<u8> {
vec![255, self.command]
}
}
#[derive(Clone, Copy)]
pub struct TelnetNegotiation {
pub command: u8,
pub option: u8,
}
impl TelnetNegotiation {
pub fn new(command: u8, option: u8) -> Self {
Self { command, option }
}
pub fn into_bytes(self) -> Vec<u8> {
vec![255, self.command, self.option]
}
}
#[derive(Clone)]
pub struct TelnetSubnegotiation {
pub option: u8,
pub buffer: Vec<u8>,
}
impl TelnetSubnegotiation {
pub fn new(option: u8, buffer: &[u8]) -> Self {
Self {
option,
buffer: Vec::from(buffer),
}
}
pub fn into_bytes(self) -> Vec<u8> {
bytes::concat(vec![
&[255, 250, self.option],
&Parser::escape_iac(self.buffer),
&[255, 240],
])
}
}
#[derive(Clone)]
pub enum TelnetEvents {
IAC(TelnetIAC),
Negotiation(TelnetNegotiation),
Subnegotiation(TelnetSubnegotiation),
DataReceive(Vec<u8>),
DataSend(Vec<u8>),
}
impl TelnetEvents {
pub fn build_send(buffer: Vec<u8>) -> Self {
TelnetEvents::DataSend(buffer)
}
pub fn build_receive(buffer: Vec<u8>) -> Self {
TelnetEvents::DataReceive(buffer)
}
pub fn build_iac(command: u8) -> TelnetEvents {
TelnetEvents::IAC(TelnetIAC::new(command))
}
pub fn build_negotiation(command: u8, option: u8) -> Self {
TelnetEvents::Negotiation(TelnetNegotiation::new(command, option))
}
pub fn build_subnegotiation(option: u8, buffer: Vec<u8>) -> Self {
TelnetEvents::Subnegotiation(TelnetSubnegotiation::new(option, &buffer))
}
}