-
Notifications
You must be signed in to change notification settings - Fork 24
/
TelnetClient.cpp
261 lines (221 loc) · 6.44 KB
/
TelnetClient.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
/*
* Copyright 2017 Alessio Villa
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
#include "TelnetClient.h"
telnetClient::telnetClient(EthernetClient& client){
this->client = &client;
}
bool telnetClient::login(IPAddress serverIpAddress, const char* username, const char* password, uint8_t port){
this->print('\n');
this->print('\r');
DEBUG_PRINT(F("login|connecting..."));
if(this->client->connect(serverIpAddress, port)){
DEBUG_PRINT(F("login|connected!"));
//here there will be the initial negotiation
//listenUntil(':');
listen();
DEBUG_PRINT(F("login|sending username"));
if (!this->send(username, false)) return false;
listenUntil(':');
DEBUG_PRINT(F("login|sending password"));
if (!this->send(password, false)) return false;
#ifdef MT_VM
//mikrotik router with demo license
this->listenUntil('!');
this->send("", false);
#endif
return this->waitPrompt();
//listen();
//return true;
}
else{
DEBUG_PRINT(F("login|connection failed!"));
return false;
}
}
bool telnetClient::sendCommand(const char* cmd){
this->send(cmd);
//negotiation until the server show the command prompt again
if (strcmp(cmd, "exit") != 0){
return this->waitPrompt();
}
else{
this->disconnect();
}
}
void telnetClient::disconnect(){
this->client->stop();
}
bool telnetClient::send(const char* buf, bool waitEcho){
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;
}
l_size = strnlen(l_outBuffer, MAX_OUT_BUFFER_LENGTH);
for (uint8_t i = 0; i < l_size; ++i){
if(l_outBuffer[i] > 0){
this->client->write(l_outBuffer[i]);
this->print(l_outBuffer[i]);
if (waitEcho){
while (this->client->available () == 0) delay (1);
char inByte = this->client->read();
}
}
}
//this->print('\r');
return true;
}
void telnetClient::negotiate(){
byte verb, opt;
byte outBuf[3] = {255, 0, 0};
DEBUG_PRINT(F("negotiate|server:IAC"));
verb = this->client->read ();
if (verb == - 1) return;
switch (verb) {
case 255:
//...no it isn't!
DEBUG_PRINT(F("negotiate|server:IAC escape"));
this->print(char (verb));
break;
case 251:
//to a WILL statement...
DEBUG_PRINT(F("negotiate|server:WILL"));
opt = this->client->read();
if (opt == -1) break;
DEBUG_PRINT(F("negotiate|server opt: "));
DEBUG_PRINT(opt);
//always answer DO!
outBuf[1] = 253;
outBuf[2] = opt;
this->client->write(outBuf, 3);
this->client->flush();
DEBUG_PRINT(F("negotiate|client:IAC"));
DEBUG_PRINT(F("negotiate|client:DO"));
break;
case 252:
DEBUG_PRINT(F("negotiate|server:WONT"));
break;
case 253:
//to a DO request...
DEBUG_PRINT(F("negotiate|server:DO"));
opt = this->client->read();
if (opt == -1) break;
DEBUG_PRINT(F("negotiate|server opt: "));
DEBUG_PRINT(opt);
//alway answer WONT!
outBuf[1] = 252;
outBuf[2] = opt;
this->client->write(outBuf, 3);
this->client->flush();
DEBUG_PRINT(F("negotiate|client:IAC"));
DEBUG_PRINT(F("negotiate|client:WONT"));
break;
case 254:
DEBUG_PRINT(F("negotiate|server:DONT"));
break;
}
}
void telnetClient::listen(){
while (this->client->available() == 0) delay (1);
byte inByte;
unsigned long startMillis = millis();
while(1){
if (client->available() > 0){
startMillis = millis();
inByte = this->client->read ();
if (inByte <= 0){
//DEBUG_PRINT(F("listen|what?"));
}
else if(inByte == 255){
this->negotiate();
}
else{
//is stuff to be displayed
this->print(char(inByte));
}
}
else if (millis() - startMillis > LISTEN_TOUT){
DEBUG_PRINT(F("listen|TIMEOUT!!!"));
return;
}
}
}
bool telnetClient::listenUntil(char c){
byte inByte;
//listen incoming bytes untile one char in the array arrive
while (this->client->available() == 0) delay (1);
do {
if(this->client->available() > 0){
inByte = this->client->read();
if (inByte <= 0){
//DEBUG_PRINT(F("listen|what?"));
}
else if(inByte == 255){
this->negotiate();
}
else{
//is stuff to be displayed
this->print(char(inByte));
}
if (char(inByte) == c){
DEBUG_PRINT(F("listenUntil|TERMINATOR RECEIVED"));
return true;
}
}
}while (1);
}
bool telnetClient::waitPrompt(){
bool l_bLoop = false;
unsigned long startMillis = millis();
do
{
if (!this->listenUntil(m_promptChar)) return false;
char l_lastByte = this->client->read();
do
{
l_bLoop = this->client->available() > 0;
if (l_bLoop){
DEBUG_PRINT(F("waitPrompt|FALSE PROMPT DETECTED"));
this->print('\r');
//this->print('\n');
break;
}
}while(millis()-startMillis < PROMPT_REC_TOUT);
}while(l_bLoop);
//this->print('\n');
//this->print('\r');
DEBUG_PRINT(F("waitPrompt|END"));
return true;
}
void telnetClient::print(char c){
//edit this function if you want a different output!
Serial.print(c);
}
void telnetClient::setPromptChar(char c){
m_promptChar = c;
}