-
Notifications
You must be signed in to change notification settings - Fork 2
/
Telnet.cpp
364 lines (322 loc) · 11.2 KB
/
Telnet.cpp
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
Telnet support for the ESP8266 Wifi.
Copyright (c) 2016 Kenneth S. Davis, All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Extends supports of TelnetServer base class:
RFC 857 - TELNET ECHO OPTION
RFC 858 - TELNET SUPPRESS GO AHEAD OPTION
RFC 1079 - TELNET TERMINAL SPEED OPTION
*/
#include "Telnet.h"
//#define DEBUG_TELNET Serial
TelnetServer::TelnetServer(int port) :
_server(port)
{
}
TelnetServer::TelnetServer() :
_server(23)
{
}
TelnetServer::~TelnetServer()
{
end();
}
void TelnetServer::begin()
{
#ifdef DEBUG_TELNET
DEBUG_TELNET.println("Telnet server started");
#endif
_server.begin();
}
void TelnetServer::end()
{
if (_client)
_client.stop();
_server.close();
}
void TelnetServer::handleClient()
{
// are we running?
if (_server.status() == CLOSED)
return;
// new client?
if (_server.hasClient())
{
if (_client)
{
// already have one, sorry
#ifdef DEBUG_TELNET
DEBUG_TELNET.println("Rejecting client");
#endif
WiFiClient rejectClient = _server.available();
rejectClient.stop();
}
else
{
// grab the new client
_client = _server.available();
_initClient(_clientStr);
#ifdef DEBUG_TELNET
DEBUG_TELNET.println("Accepted new client");
#endif
}
}
// so, do we still have a client?
if (_client)
{
// and is it connected?
if (!_client.connected())
{
#ifdef DEBUG_TELNET
DEBUG_TELNET.println("Existing client stopped");
#endif
_client.stop();
return;
}
// at this point, we have a client and it is connected
while (_client.available())
{
char c = _client.read();
#ifdef DEBUG_TELNET
DEBUG_TELNET.print(c, HEX);
DEBUG_TELNET.print(" ");
#endif
switch (_clientStr.clientState)
{
case Normal:
{
// don't pass this to sub-classes, just advance state
if (c == TELNET_IAC)
{
_clientStr.clientState = InTelnetOpt0;
return;
}
_clientStr.opt0 = c;
_processOption(_client, _clientStr);
return;
}
// Control
case InTelnetOpt0:
{
_clientStr.opt0 = c;
switch (c)
{
// don't pass this to sub-classes, just advance state
case TELNET_WILL:
case TELNET_DO:
case TELNET_WONT:
case TELNET_DONT:
{
_clientStr.clientState = InTelnetOpt1;
return;
}
case TELNET_IAC:
{
// this is an escaped 0xff, go back to normal mode
// and send to sub-classes as just a normal 0xff char
_clientStr.clientState = Normal;
_clientStr.opt0 = c;
_processOption(_client, _clientStr);
return;
}
case TELNET_SB: // start of sub-nego
{
// don't pass this to sub-classes, just advance state
_clientStr.clientState = InTelnetSubNego0;
_clientStr.negoBufferLen = 0;
return;
}
default:
{
// got one of these really, really old IAC commands
// for EL, EC, GA, etc, pass to client. Regardless
// change back to normal mode.
_processOption(_client, _clientStr);
_clientStr.clientState = Normal;
return;
}
}
}
// Option
case InTelnetOpt1:
{
_clientStr.opt1 = c;
// here, we let the sub-class determine handling of TELNET options
// If it handles it, we are done, otherwise we send appropriate DONT
// WONT. And back to normal mode.
if (_processOption(_client, _clientStr))
{
_clientStr.clientState = Normal;
return;
}
// default handling for unhandled requests
if (_clientStr.opt0 == TELNET_WILL)
{
// send DONT
_clientStr.buffer[_clientStr.bufferLen] = TELNET_IAC;
_clientStr.bufferLen++;
_clientStr.buffer[_clientStr.bufferLen] = TELNET_DONT;
_clientStr.bufferLen++;
_clientStr.buffer[_clientStr.bufferLen] = c;
_clientStr.bufferLen++;
}
else if (_clientStr.opt0 == TELNET_DO)
{
// send WONT
_clientStr.buffer[_clientStr.bufferLen] = TELNET_IAC;
_clientStr.bufferLen++;
_clientStr.buffer[_clientStr.bufferLen] = TELNET_WONT;
_clientStr.bufferLen++;
_clientStr.buffer[_clientStr.bufferLen] = c;
_clientStr.bufferLen++;
}
else
{
// something weird this way comes.. :<
#ifdef DEBUG_TELNET
DEBUG_TELNET.println("Unspecified opt");
#endif
}
_clientStr.clientState = Normal;
return;
}
// Sub Negoiations
case InTelnetSubNego0:
{
// In order to handle an embedded 0xff in subnegotiation mode,
// we get an extra state, InTelnetSubNego1.
if (c == TELNET_IAC)
_clientStr.clientState = InTelnetSubNego1;
else
{
_clientStr.negoBuffer[_clientStr.negoBufferLen] = c;
_clientStr.negoBufferLen++;
}
break;
}
case InTelnetSubNego1:
{
// in this state, we either get a second TELNET_IAC or otherwise
// we should get the TELNET_SE.
if (c == TELNET_IAC)
{
// they sent an esc'd 0xff, back to InTelnetSubNego0
_clientStr.clientState = InTelnetSubNego0;
_clientStr.negoBuffer[_clientStr.negoBufferLen] = TELNET_IAC;
_clientStr.negoBufferLen++;
}
else if (c == TELNET_SE)
{
// we got the completed subnegotiation, process it
_processSubNegotiation(_client, _clientStr);
_clientStr.clientState = Normal;
}
else
{
#ifdef DEBUG_TELNET
DEBUG_TELNET.println("");
DEBUG_TELNET.print("Unknown subneg option:");
DEBUG_TELNET.println(c, HEX);
#endif
}
break;
}
}
}
// Send outbound data
if (_clientStr.bufferLen > 0)
{
#ifdef DEBUG_TELNET
DEBUG_TELNET.println("");
DEBUG_TELNET.print("Sending bytes: ");
DEBUG_TELNET.println(_clientStr.bufferLen);
for (auto i = 0; i < _clientStr.bufferLen; i++)
{
DEBUG_TELNET.print(_clientStr.buffer[i], HEX);
DEBUG_TELNET.print(" ");
}
DEBUG_TELNET.println();
#endif
_client.write(&_clientStr.buffer[0], _clientStr.bufferLen);
_clientStr.bufferLen = 0;
}
}
}
void TelnetServer::_initClient(ClientStruct& str)
{
str.clientState = Normal;
str.echo = 0;
str.negoBufferLen = 0;
str.bufferLen = 0;
str.opt0 = 0;
str.opt1 = 0;
}
bool TelnetServer::_processOption(WiFiClient& client, ClientStruct& str)
{
if (str.clientState == InTelnetOpt0)
{
return false;
}
if (str.clientState == InTelnetOpt1)
{
switch (str.opt1)
{
case TELNET_OPTION_TRANSMIT_BINARY:
case TELNET_OPTION_ECHO:
case TELNET_OPTION_SUPPRESS_GA:
{
if (str.opt0 == TELNET_WILL)
{
str.buffer[str.bufferLen] = TELNET_IAC;
str.bufferLen++;
str.buffer[str.bufferLen] = TELNET_DO;
str.bufferLen++;
str.buffer[str.bufferLen] = str.opt1;
str.bufferLen++;
}
else if (_clientStr.opt0 == TELNET_DO)
{
str.buffer[str.bufferLen] = TELNET_IAC;
str.bufferLen++;
str.buffer[str.bufferLen] = TELNET_WILL;
str.bufferLen++;
str.buffer[str.bufferLen] = str.opt1;
str.bufferLen++;
}
if (str.opt1 == TELNET_OPTION_ECHO)
{
#ifdef DEBUG_TELNET
DEBUG_TELNET.println("Echo turned on");
#endif
str.echo = 1;
}
if (str.opt1 == TELNET_OPTION_SUPPRESS_GA)
{
#ifdef DEBUG_TELNET
DEBUG_TELNET.println("GA suppressed");
#endif
str.noga = 1;
}
str.clientState = Normal;
return true;
}
default:
return false;
}
}
return false;
}
bool TelnetServer::_processSubNegotiation(WiFiClient &client, struct ClientStruct &str)
{
return false;
}