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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use crate::Parser;
use bytes::{BufMut, Bytes, BytesMut};
#[derive(Clone, Copy)]
pub struct TelnetIAC {
pub command: u8,
}
impl Into<Bytes> for TelnetIAC {
fn into(self) -> Bytes {
let mut buf = BytesMut::with_capacity(2);
buf.put_u8(255);
buf.put_u8(self.command);
buf.freeze()
}
}
impl Into<Vec<u8>> for TelnetIAC {
fn into(self) -> Vec<u8> {
let b: Bytes = self.into();
b.to_vec()
}
}
impl TelnetIAC {
pub fn new(command: u8) -> Self {
Self { command }
}
pub fn into_bytes(self) -> Vec<u8> {
self.into()
}
}
#[derive(Clone, Copy)]
pub struct TelnetNegotiation {
pub command: u8,
pub option: u8,
}
impl Into<Bytes> for TelnetNegotiation {
fn into(self) -> Bytes {
let data = [self.command, self.option];
let mut buf = BytesMut::with_capacity(3);
buf.put_u8(255);
buf.put(&data[..]);
buf.freeze()
}
}
impl Into<Vec<u8>> for TelnetNegotiation {
fn into(self) -> Vec<u8> {
let b: Bytes = self.into();
b.to_vec()
}
}
impl TelnetNegotiation {
pub fn new(command: u8, option: u8) -> Self {
Self { command, option }
}
pub fn into_bytes(self) -> Vec<u8> {
self.into()
}
}
#[derive(Clone)]
pub struct TelnetSubnegotiation {
pub option: u8,
pub buffer: Bytes,
}
impl Into<Bytes> for TelnetSubnegotiation {
fn into(self) -> Bytes {
let head: [u8; 3] = [255, 250, self.option];
let parsed = &Parser::escape_iac(self.buffer)[..];
let tail: [u8; 2] = [255, 240];
let mut buf = BytesMut::with_capacity(head.len() + parsed.len() + tail.len());
buf.put(&head[..]);
buf.put(&parsed[..]);
buf.put(&tail[..]);
buf.freeze()
}
}
impl Into<Vec<u8>> for TelnetSubnegotiation {
fn into(self) -> Vec<u8> {
let b: Bytes = self.into();
b.to_vec()
}
}
impl TelnetSubnegotiation {
pub fn new(option: u8, buffer: Bytes) -> Self {
Self { option, buffer }
}
pub fn into_bytes(self) -> Vec<u8> {
self.into()
}
}
#[derive(Clone)]
pub enum TelnetEvents {
IAC(TelnetIAC),
Negotiation(TelnetNegotiation),
Subnegotiation(TelnetSubnegotiation),
DataReceive(Bytes),
DataSend(Bytes),
DecompressImmediate(Bytes),
}
impl Into<Bytes> for TelnetEvents {
fn into(self) -> Bytes {
match self {
TelnetEvents::IAC(iac) => iac.into(),
TelnetEvents::Negotiation(neg) => neg.into(),
TelnetEvents::Subnegotiation(sub) => sub.into(),
TelnetEvents::DataReceive(data) => data,
TelnetEvents::DataSend(data) => data,
TelnetEvents::DecompressImmediate(data) => data,
}
}
}
impl TelnetEvents {
pub fn build_send(buffer: Bytes) -> Self {
TelnetEvents::DataSend(buffer)
}
pub fn build_receive(buffer: Bytes) -> 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: Bytes) -> Self {
TelnetEvents::Subnegotiation(TelnetSubnegotiation::new(option, buffer))
}
}