-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtelnet.cpp
More file actions
executable file
·185 lines (166 loc) · 5.57 KB
/
telnet.cpp
File metadata and controls
executable file
·185 lines (166 loc) · 5.57 KB
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
#include "telnet.h"
telnetClient::telnetClient(EthernetClient& client){
this->client = &client;
}
bool telnetClient::login(IPAddress serverIpAddress, const char* username, const char* password, uint8_t port){
DEBUG_PRINT(F("login..."));
DEBUG_PRINT(F("connecting..."));
if(this->client->connect(serverIpAddress, port)){
DEBUG_PRINT(F("connected!"));
//negotation
this->receive();
//we should be asked for the login username at this point...
DEBUG_PRINT(F("sending username"));
if (!this->send(username, true)) return false;
//receive until the server show the password prompt
this->listenUntil(':');
DEBUG_PRINT(F("sending password"));
if (!this->send(password, false)) return false;
//receive until the server show the command prompt or timeout
return this->listenUntil('$', '>');
}
else{
DEBUG_PRINT(F("connection failed!"));
return false;
}
}
bool telnetClient::sendCommand(const char* cmd){
this->send(cmd, true);
//receive until the server show the command prompt again
if (strcmp(cmd, "exit") != 0){
return this->listenUntil('$', '>', '#');
}
}
void telnetClient::disconnect(){
this->client->stop();
}
bool telnetClient::send(const char* buf, bool checkEcho){
uint8_t l_size = strnlen(buf, MAX_OUT_BUFFER_LENGTH);
if(l_size == MAX_OUT_BUFFER_LENGTH){
DEBUG_PRINT(F("send: bad input"));
return false;
}
char l_outBuffer[MAX_OUT_BUFFER_LENGTH];
strlcpy(l_outBuffer, buf, MAX_OUT_BUFFER_LENGTH);
if(strlcat(l_outBuffer, "\r\n", MAX_OUT_BUFFER_LENGTH) >= MAX_OUT_BUFFER_LENGTH){
DEBUG_PRINT(F("send: bad input"));
return false;
}
if(checkEcho){
l_size = strnlen(l_outBuffer, MAX_OUT_BUFFER_LENGTH);
for (uint8_t i = 0; i < l_size - 1; ++i){
if(l_outBuffer[i] > 0){
this->client->write(l_outBuffer[i]);
this->print(l_outBuffer[i]);
while (this->client->available () == 0) delay (1);
byte inByte = this->client->read();
if((char)inByte != l_outBuffer[i]){
DEBUG_PRINT(F("send: wrong echo from server"));
return false;
}
}
}
}
else{
this->client->write(l_outBuffer);
}
this->print('\r');
return true;
}
void telnetClient::receive(){
while (this->client->available() == 0) delay (1);
do {
this->parse();
delay(100);
}
while(this->client->available() > 0);
}
void telnetClient::parse(){
int inByte, verb, opt;
while (this->client->available () > 0) {
inByte = this->client->read ();
switch (inByte) {
case -1:
DEBUG_PRINT(F("what?"));
break;
case 255:
//is stuff to negotiate with the server
DEBUG_PRINT(F("server:IAC"));
verb = this->client->read ();
if (verb == - 1) break;
switch (verb) {
case 255:
//...no it isn't!
DEBUG_PRINT(F("server:IAC escape"));
this->print(char (verb));
break;
case 251:
//to a WILL statement...
DEBUG_PRINT(F("server:WILL"));
opt = this->client->read();
if (opt == -1) break;
DEBUG_PRINT(F("server opt: "));
DEBUG_PRINT(opt);
//always answer DO!
DEBUG_PRINT(F("client:IAC"));
this->client->write (255);
DEBUG_PRINT(F("client:DO"));
this->client->write (253);
this->client->write(opt);
break;
case 252:
DEBUG_PRINT(F("server:WONT"));
break;
case 253:
//to a DO request...
DEBUG_PRINT(F("server:DO"));
opt = this->client->read();
if (opt == -1) break;
DEBUG_PRINT(F("server opt: "));
DEBUG_PRINT(opt);
//always answer WONT!
this->client->write(255);
DEBUG_PRINT(F("client:IAC"));
this->client->write(252);
DEBUG_PRINT(F("client:WONT"));
this->client->write(opt);
break;
case 254:
DEBUG_PRINT(F("server:DONT"));
break;
}
break;
default:
//is stuff to be displayed
this->print(char(inByte));
break;
}
}
}
bool telnetClient::listenUntil(char c1, char c2, char c3, char c4){
char l_lastByte = 0;
unsigned long startMillis = millis();
while (this->client->available() == 0) delay (1);
do {
if(this->client->available() > 0){
l_lastByte = this->client->read();
if(l_lastByte > 0)this->print(l_lastByte);
}
}while (l_lastByte != c1 && l_lastByte != c2 && l_lastByte != c3 && l_lastByte != c4 && this->client->connected() && (millis()-startMillis < LISTEN_TOUT));
if(millis()-startMillis >= LISTEN_TOUT){
DEBUG_PRINT(F("timeout occourred"));
return false;
}
//empty the input buffer
while (this->client->available() > 0){
l_lastByte = this->client->read();
this->print(l_lastByte);
}
return true;
}
void telnetClient::print(char c){
//override this function if you want a different output!
//Serial.print(c);
//START::print(c);
START::writeOnScreen(c);
}