forked from blacktigersoftware/ESP8266RFCTelnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTelnet.h
146 lines (112 loc) · 3.91 KB
/
Telnet.h
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
/*
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
Currently supports:
RFC 854 - TELNET PROTOCOL SPEFICICATIONS
RFC 855 - TELNET OPTION SPECIFICATIONS
RFC 856 - TELNET BINARY TRANSMISSION
RFC 854 is the key spec, as it allows for extensible support, which
is represented by two virtual functions that can be handled.
This is a base class that receives incoming bytes from a WiFi client
and decodes the protocol.
*/
#ifndef _TELNET_h
#define _TELNET_h
#if defined(ARDUINO) && ARDUINO >= 100
#include "arduino.h"
#else
#include "WProgram.h"
#endif
#include <WiFiServer.h>
#include <WiFiClient.h>
#define TELNET_SE 240
#define TELNET_NOP 241
#define TELNET_DM 242
#define TELNET_BRK 243
#define TELNET_IP 244
#define TELNET_AO 245
#define TELNET_AYT 246
#define TELNET_EC 247
#define TELNET_EL 248
#define TELNET_GA 249
#define TELNET_SB 250
#define TELNET_WILL 251
#define TELNET_WONT 252
#define TELNET_DO 253
#define TELNET_DONT 254
#define TELNET_IAC 255
// telnet options
#define TELNET_OPTION_TRANSMIT_BINARY 0
#define TELNET_OPTION_ECHO 1
#define TELNET_OPTION_SUPPRESS_GA 3
class TelnetServer
{
public:
void begin();
void end();
void handleClient();
virtual ~TelnetServer();
protected:
TelnetServer();
TelnetServer(int port);
enum ClientState
{
Normal,
InTelnetOpt0,
InTelnetOpt1,
InTelnetSubNego0,
InTelnetSubNego1
};
// currently we only support one client, however,
// should the need arise to support multiple clients
// each client should have one of these.
struct ClientStruct
{
enum ClientState clientState;
byte opt0;
byte opt1;
byte echo;
byte noga;
uint8_t negoBuffer[128];
uint8_t negoBufferLen;
uint8_t buffer[1024];
uint8_t bufferLen;
};
// on 'false' the subnegotiation was not handled
virtual bool _processSubNegotiation(WiFiClient &client, struct ClientStruct &str);
/*
on 'false' the option was not processed,
for str.clientState of
Normal: opt0 is the incoming byte
InTelnetOpt0: opt0 is the command
InTelnetOpt1: opt0 is "DO/DONT/WILL/WONT", opt1 is the option
*/
virtual bool _processOption(WiFiClient &client, struct ClientStruct &str);
/*
initializes the client struct
*/
static void _initClient(struct ClientStruct &str);
/* our server */
WiFiServer _server;
/* our single client -- This can be easy extended to support multiple
clients --- but, consider. What would happen say if one client turned
on a motor and another client turned off a motor? Does a firmware board
really need to support multiple clients? However, nothing prevents the
multiple servers running on different ports for different reasons....
*/
WiFiClient _client;
/* holds are client data, should we handle multiple clients */
struct ClientStruct _clientStr;
};
#endif