-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelnet.c
268 lines (219 loc) · 4.64 KB
/
telnet.c
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
* This handles telnet "negotiation", etc.
* We don't actually negotiate anything but we at least strip
* the telnet commands.
*/
#pragma noroot
#include "telnet.h"
extern unsigned char buffer[];
extern unsigned buffer_size;
extern void send(const unsigned char *, unsigned);
enum {
st_data = 0,
st_iac,
st_do,
st_dont,
st_will,
st_wont,
st_sb,
st_sb_iac,
st_cr
};
static int state = 0;
static unsigned do_bits[3];
static unsigned dont_bits[3];
static unsigned will_bits[3];
static unsigned wont_bits[3];
#define set_bit(type, x) \
type ## _bits[(x) >> 4] |= 1 << ((x) & 0x0f)
#define has_bit(type, x) \
(type ## _bits[(x) >> 4] & 1 << ((x) & 0x0f))
void telnet_init(void) {
static unsigned char msg[] = {
IAC, DO, TELOPT_SGA,
IAC, DO, TELOPT_ECHO,
IAC, WONT, TELOPT_ECHO,
IAC, DO, TELOPT_BINARY,
IAC, DONT, TELOPT_LINEMODE,
IAC, WILL, TELOPT_TTYPE,
IAC, WILL, TELOPT_NAWS,
IAC, WILL, TELOPT_TSPEED,
};
do_bits[0] = 0;
do_bits[1] = 0;
do_bits[2] = 0;
dont_bits[0] = 0;
dont_bits[1] = 0;
dont_bits[2] = 0;
will_bits[0] = 0;
will_bits[1] = 0;
will_bits[2] = 0;
wont_bits[0] = 0;
wont_bits[1] = 0;
wont_bits[2] = 0;
state = st_data;
#if 0
set_bit(do, TELOPT_SGA);
set_bit(do, TELOPT_ECHO);
set_bit(do, TELOPT_BINARY);
set_bit(dont, TELOPT_LINEMODE);
set_bit(will, TELOPT_TTYPE);
set_bit(will, TELOPT_NAWS);
set_bit(will, TELOPT_TSPEED);
#endif
send(msg, sizeof(msg));
}
void telnet_process(void) {
unsigned i, j;
unsigned char cmd[3];
static unsigned char telopt_naws[] = {
IAC, SB, TELOPT_NAWS,
0, 80, 0, 24,
IAC, SE
};
static unsigned char telopt_tspeed[] = {
IAC, SB, TELOPT_TSPEED,
'9', '6', '0', '0', ',', '9', '6', '0', '0',
IAC, SE
};
static unsigned char telopt_ttype_vt100[] = {
IAC, SB, TELOPT_TTYPE, TELQUAL_IS,
'V', 'T', '1', '0', '0',
IAC, SE
};
static unsigned char telopt_ttype_vt52[] = {
IAC, SB, TELOPT_TTYPE, TELQUAL_IS,
'V', 'T', '5', '2',
IAC, SE
};
/* don't need to process if no state, no IACs in buffer */
if (!state) {
/* check for an IAC */
for (i = 0; i < buffer_size; ++i) {
if (buffer[i] == IAC) break;
}
if (i == buffer_size) return;
}
for (i = 0, j = 0; i < buffer_size; ++i) {
unsigned char c = buffer[i];
switch(state) {
#if 0
// ignoring this for now since vt100 ignores 0x00 anyhow.
// also, would only run if other IAC detected.
case st_cr: // 0x0d 0x00 < drop the 0x00
if (c == 0x00) {
state = st_data;
break;
}
/* drop through */
#endif
case st_data:
if (c == IAC) {
state = st_iac;
}
else buffer[j++] = c;
//if (c == 0x0d) state = st_cr;
break;
case st_iac:
switch (c) {
case IAC: // IAC IAC -> IAC
state = st_data;
buffer[j++] = c;
break;
case AYT:
send("\r\n[Yes]\r\n", 9);
state = st_data;
break;
case DO: state = st_do; break;
case DONT: state = st_dont; break;
case WILL: state = st_will; break;
case WONT: state = st_wont; break;
case SB: state = st_sb; break;
default: state = st_data; break;
}
break;
case st_do:
cmd[0] = IAC;
cmd[1] = WILL;
cmd[2] = c;
switch(c) {
case TELOPT_SGA:
case TELOPT_BINARY:
// already said will do them, no reply needed.
break;
case TELOPT_NAWS:
set_bit(do, TELOPT_NAWS);
send(telopt_naws, sizeof(telopt_naws));
break;
case TELOPT_TSPEED:
send(telopt_tspeed, sizeof(telopt_tspeed));
break;
case TELOPT_TTYPE:
send(telopt_ttype_vt100, sizeof(telopt_ttype_vt100));
break;
default:
case TELOPT_ECHO:
cmd[1] = WONT;
send(cmd, 3);
}
state = st_data;
break;
case st_dont:
cmd[0] = IAC;
cmd[1] = WONT;
cmd[2] = c;
#if 0
switch(c) {
case TELOPT_SGA:
cmd[1] = WILL;
break;
default:
}
#endif
send(cmd, 3);
state = st_data;
break;
case st_will:
cmd[0] = IAC;
cmd[1] = DO;
cmd[2] = c;
switch(c) {
case TELOPT_SGA:
case TELOPT_BINARY:
case TELOPT_ECHO:
case TELOPT_NAWS:
break;
default:
cmd[1] = DONT;
send(cmd, 3);
}
state = st_data;
break;
case st_wont:
#if 0
cmd[0] = IAC;
cmd[1] = DONT;
cmd[2] = c;
switch(c) {
case TELOPT_SGA:
case TELOPT_LINEMODE:
cmp[1] = DO;
break;
default:
}
send(cmd, 3);
#endif
state = st_data;
break;
case st_sb:
if (c == IAC) state = st_sb_iac;
break;
case st_sb_iac:
// IAC | IAC or IAC | SE
if (c == SE) state = st_data;
else state = st_sb;
break;
}
}
buffer_size = j;
}