forked from martydill/telnet
/
TelnetSession.cpp
414 lines (324 loc) · 7.92 KB
/
TelnetSession.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// TelnetSession.cpp
// Controls the telnet session
#include <iostream>
#include <string.h>
#include <conio.h>
#include "TelnetSession.h"
#include "TelnetProtocol.h"
#include "SendBuf.h"
// Default constructor
TelnetSession::TelnetSession()
{
debugLog.open("log.txt", ios::out);
}
// Default destructor
TelnetSession::~TelnetSession()
{
WSACleanup();
//if(console)
//delete console;
}
// Opens a connection
void TelnetSession::openConnection(char* address, int port)
{
WSADATA ws;
// Load the WinSock dll
long status = WSAStartup(0x0101,&ws);
if(status != 0)
throw "Unable to load WinSock!";
cout << "Opening connection to " << address << endl;
// Open the connection
connection = new Connection(port);
if(!connection->open(address))
throw "Unable to open connection!";
activeConnection = true;
// Set up the console
console = new Console;
return;
}
// The 'get input' thingy...
unsigned long WINAPI inputThread(void* data)
{
char* msg = new char[2];
msg[1] = '\0';
// Get a pointer to the TelnetSession object
TelnetSession* session = (TelnetSession*)data;
// Main loop...
while(session->getConnectionState() == true)
{
msg[0]= (char)getch();
try
{
session->sendString(msg);
}
catch(char* errorMessage) // Connection has been closed.
{
session->setConnectionState(false);
return 0;
}
}
delete[] msg;
// Kill this thread.
ExitThread(0);
return 0;
}
// Sends a single character
void TelnetSession::sendString(char* stringToSend)
{
connection->sendData(stringToSend);
}
// Checks to see if the connection is active
void TelnetSession::setConnectionState(bool state)
{
activeConnection = state;
}
// Checks to see if the connection is active
bool TelnetSession::getConnectionState(void)
{
return activeConnection;
}
// Enter the program's main loop
void TelnetSession::mainLoop(void)
{
unsigned char* receivedData = NULL;
HANDLE threadHandle;
// Create a thread for getting keyboard input
unsigned long threadId;
threadHandle = CreateThread(NULL, NULL, inputThread, this, NULL, &threadId);
// Main program loop
while(activeConnection)
{
try
{
// Download data
receivedData = (unsigned char*)connection->receive();
}
catch(char* errorMessage)
{
// Connection has been dropped
activeConnection = false;
CloseHandle(threadHandle);
return;
}
// Scan for command codes
unsigned char *pPos = receivedData;
while(*pPos != '\0')
{
// Telnet control code
if(*pPos == IAC)
{
int bytesRead = handleCommand(pPos);
pPos += bytesRead;
}
// VT100 control code
else if(*pPos == VT_ESC)
{
int bytesRead = handleVTCommand(pPos);
pPos += bytesRead;
}
// Just text
else
{
cout << *pPos;
cout.flush();
pPos++;
}
}
delete[] receivedData;
}
return;
}
// Deal with a single command
int TelnetSession::handleCommand(unsigned char* commandData)
{
unsigned char currentByte = *(++commandData);
commandData++;
if(currentByte == DO || currentByte == DONT)
respondToRequest(currentByte, *commandData);
else if(currentByte == WILL || currentByte == WONT)
respondToStatement(currentByte, *commandData);
//else
// throw "Unknown code!";
return 3;
}
// Respond to a DO/DONT request
void TelnetSession::respondToRequest(unsigned char command, unsigned char option)
{
SendBuf buf(3);
buf.add(IAC);
if(command == DONT || option == TERMINALTYPE || option == WINDOWSIZE || option == TERMINALSPEED || option == ENVIRONMENTOPTION || option == XDISPLAYLOCATION || option == ENVIRONMENTOPTION2)
buf.add(WONT);
else
buf.add(WILL);
buf.add(option);
SendBuffer(buf);
return;
}
// Respond to a WILL/WONT statement
void TelnetSession::respondToStatement(unsigned char command, unsigned char option)
{
SendBuf buf(3);
buf.add(IAC);
if(command == WONT)
buf.add(DONT);
else
buf.add(DO);
buf.add(option);
SendBuffer(buf);
return;
}
// Handle a VT100 command
// Ugh .. what a pain ...
int TelnetSession::handleVTCommand(unsigned char* commandData)
{
int bytesRead = 2;
unsigned char firstByte, secondByte, thirdByte;
commandData++;
// We have a '['
if(*commandData == VT_BRACE)
{
unsigned char* pTmp = commandData;
unsigned char currentByte;
int count = 0;
char strTmp[2];
// extract a number
do
{
currentByte = *(++pTmp);
strTmp[count] = currentByte;
count++;
}
while(isdigit(currentByte));
bytesRead += count;
// We have a ';'
if(currentByte == VT_SUB)
{
int firstVal = atoi(strTmp);
char strTmp2[2];
count = 0;
do
{
currentByte = *(++pTmp);
strTmp2[count] = currentByte;
count++;
}
while(isdigit(currentByte));
bytesRead += count;
int secondVal = atoi(strTmp2);
console->setCursorPos((unsigned char)secondVal - 1, (unsigned char)firstVal - 1);
debugLog << "Moving cursor to: " << ((unsigned char)secondVal - 1) << "," << ((unsigned char)firstVal -1 ) << endl;
}
// We have a '?'
else if(currentByte == VT_QUESTION)
{
bytesRead += 2;
}
// We are erasing stuff
else if(currentByte == VT_J)
{
int firstVal = atoi(strTmp);
if(firstVal == 0)
console->eraseFromCursorToEnd();
else if(firstVal == 1)
console->eraseFromStartToCursor();
else if(firstVal == 2)
console->clearScreen();
else
throw "Unimplemented VT_J";
}
// Also erasing stuff
else if(currentByte == VT_K)
{
int firstVal = atoi(strTmp);
if(firstVal == 0)
console->eraseToEndOfLine();
else if(firstVal == 1)
console->eraseFromStartOfLineToCursor();
else if(firstVal == 2)
console->eraseEntireLine();
else
throw "Unimplemented VT_K";
}
// Graphics attributes
else if(currentByte == VT_GRAPHICS_ATTRIBUTE)
{
int firstVal = atoi(strTmp);
if(firstVal == 7)
console->setInverseText();
else if(firstVal == 0)
console->setNormalText();
}
else if(currentByte == 'H')
{
console->setCursorPos(0, 0);
bytesRead = 1;
}
else if(currentByte == 'A')
{
//console->moveUp();
bytesRead = 1;
}
else if(currentByte == 'B')
{
//console->moveDown()
bytesRead = 1;
}
else
{
debugLog << "@@@" << currentByte << endl;
}
}
// We have a ( or a )
else if(*commandData == '(' || *commandData == ')')
{
bytesRead = 2;
debugLog << "** " << *commandData << " " << *(commandData + 1) << endl;
}
// Log the unimplemented stuff ...
else
{
if(*commandData == 'H')
// move cursor to home
debugLog << "home\n";
else if(*commandData == 'D')
// move the cursor to the left 1 character
debugLog << "move horizontal\n";
else if(*commandData == 'E')
// new line
debugLog << "new line\n";
else if(*commandData == 'M')
// move up one
debugLog << "move up\n";
else if(*commandData == 'c')
// reset
debugLog << "reset\n";
else if(*commandData == '7')
// save cursor and attributes
debugLog << "save cursor\n";
else if(*commandData == '8')
// restore cursor and attributes
debugLog << "restore cursor\n";
else if(*commandData == '>')
// set numeric keypad mode
debugLog << "numeric keypad\n";
else if(*commandData == '=')
// set application keyboard mode
debugLog << "application keyboard mode\n";
else if(*commandData == 'Z')
// identify
debugLog << "identify\n";
else
{
debugLog << "***" << *commandData << endl;
//throw "Unimplemented single byte code";
}
bytesRead = 2;
}
return bytesRead;
}
// Sends the data from a SendBuf
bool TelnetSession::SendBuffer(SendBuf buffer)
{
connection->sendData(buffer.getData());
return true;
}