forked from blacktigersoftware/ESP8266RFCTelnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleTelnetServer.cpp
215 lines (187 loc) · 6.2 KB
/
SimpleTelnetServer.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
/*
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 1079 - TELNET TERMINAL SPEED OPTION
*/
#include "SimpleTelnetServer.h"
SimpleTelnetServer::SimpleTelnetServer()
{
recvBufLen = 0;
}
SimpleTelnetServer::~SimpleTelnetServer()
{
end();
}
/*
This example extends TelnetServer with additional RFC's.
*/
bool SimpleTelnetServer::_processOption(WiFiClient& client, ClientStruct& str)
{
// check with the base class first.....
if (TelnetServer::_processOption(client, str))
return true;
if (str.clientState == Normal)
{
if (str.echo)
{
str.buffer[str.bufferLen] = str.opt0;
str.bufferLen++;
}
recvBuffer[recvBufLen] = str.opt0;
recvBufLen++;
return true;
}
if (str.clientState == InTelnetOpt0)
{
switch (str.opt0)
{
case TELNET_AYT:
{
// send a '.'
str.buffer[str.bufferLen] = '.';
str.bufferLen++;
str.clientState = Normal;
return true;
}
case TELNET_BRK: // break cmd
case TELNET_AO: // abort output
case TELNET_NOP: // Nop
case TELNET_DM: // Data mark
case TELNET_GA: // Go-ahead flow-control
{
#ifdef DEBUG_TELNET
DEBUG_TELNET.print("Unsupported telnet option:");
DEBUG_TELNET.println(str.opt0, HEX);
#endif
return false;
}
case TELNET_EC: // erase last character
{
if (str.echo)
{
if (str.buffer[str.bufferLen] > 0)
{
str.buffer[str.bufferLen] = 0;
str.bufferLen--;
}
}
_clientStr.clientState = Normal;
return true;
}
case TELNET_EL: // erase current line
{
#ifdef DEBUG_TELNET
DEBUG_TELNET.print("Unsupported telnet option:");
DEBUG_TELNET.println(str.opt0, HEX);
#endif
_clientStr.clientState = Normal;
return true;
}
case TELNET_IP: // interrupt process ---- what to do...?
{
#ifdef DEBUG_TELNET
DEBUG_TELNET.print("Unsupported telnet option:");
DEBUG_TELNET.println(str.opt0, HEX);
#endif
_clientStr.clientState = Normal;
return true;
}
default:
{
#ifdef DEBUG_TELNET
DEBUG_TELNET.print("Unknown telnet option:");
DEBUG_TELNET.println(str.opt0, HEX);
#endif
return false;
}
}
}
if (str.clientState == InTelnetOpt1)
{
switch (str.opt1)
{
// extend for RFC 1079 - TELNET TERMINAL SPEED OPTION, also needed
// to add subnegotiation support for this.
case TELNET_OPTION_TERMINAL_SPEED:
{
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++;
}
str.clientState = Normal;
return true;
}
default:
{
return false;
}
}
}
return false;
}
bool SimpleTelnetServer::_processSubNegotiation(WiFiClient &client, struct ClientStruct &str)
{
// we have captured a sb block:
// IAC SB [ captured ] IAC SE
if (TelnetServer::_processSubNegotiation(client, str))
return true;
switch (str.negoBuffer[0])
{
case TELNET_OPTION_TERMINAL_SPEED:
{
int txSpeed = 9600;
int rxSpeed = 9600;
if (str.negoBuffer[1] == 1)
{
str.buffer[str.bufferLen++] = TELNET_IAC;
str.buffer[str.bufferLen++] = TELNET_SB;
str.buffer[str.bufferLen++] = TELNET_OPTION_TERMINAL_SPEED;
str.buffer[str.bufferLen++] = 0;
str.bufferLen += sprintf((char*)&str.buffer[str.bufferLen], "%d,%d", txSpeed, rxSpeed);
str.buffer[str.bufferLen++] = TELNET_IAC;
str.buffer[str.bufferLen] = TELNET_SE;
#ifdef DEBUG_TELNET
DEBUG_TELNET.println("SB TERMINAL SPEED");
for (int i = 0; i < str.bufferLen; i++)
{
DEBUG_TELNET.print(str.buffer[i], HEX);
DEBUG_TELNET.print(" ");
}
DEBUG_TELNET.println();
#endif
str.negoBufferLen = 0;
return true;
}
break;
}
default:
return false;
}
return false;
}