summaryrefslogtreecommitdiff
path: root/src/protocoltelnet.cpp
blob: b169a51e408efe2b70dda54390a55b1c85991ad0 (plain)
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
#include "protocoltelnet.h"
#include <string.h>

ProtocolTelnet::ProtocolTelnet()
{
	nTermType = termUnInited;
	bEchoOn = true;
}

ProtocolTelnet::~ProtocolTelnet()
{
}

bool ProtocolTelnet::onNewConnection()
{
	Connection *pCon = getConnection();

	pCon->appendOutput( (char)IAC );
	pCon->appendOutput( (char)WILL );
	pCon->appendOutput( (char)SUPPRESSGA );

	pCon->appendOutput( (char)IAC );
	pCon->appendOutput( (char)DO );
	pCon->appendOutput( (char)SUPPRESSGA );

	pCon->appendOutput( (char)IAC );
	pCon->appendOutput( (char)DONT );
	pCon->appendOutput( (char)TERMTYPE );

//	pCon->appendOutput( IAC );
//	pCon->appendOutput( SB );
//	pCon->appendOutput( TERMTYPE );
//	pCon->appendOutput( 1 );
//	pCon->appendOutput( IAC );
//	pCon->appendOutput( SE );

	pCon->appendOutput( (char)IAC );
	pCon->appendOutput( (char)DONT );
	pCon->appendOutput( (char)ECHO );

	pCon->appendOutput( (char)IAC );
	pCon->appendOutput( (char)WILL );
	pCon->appendOutput( (char)ECHO );

//	255(IAC),251(WILL),3
	return true;
}

bool ProtocolTelnet::onNewData()
{
	Connection *pCon = getConnection();
	if( !pCon->hasInput() )
	{
	    return true;
	}

	int nInSize = pCon->getInputAmnt();
	char *lpInStr = (char *)pCon->getInput();

	// Here we interpret the basic commands and un-encapsulate them, so to
	// speak.  We'll allow this, even if the terminal is in raw mode, we
	// just won't send anything in response...
	for( int j = 0; j < nInSize; j++ )
	{
		switch( (unsigned char)lpInStr[j] )
		{
		case '\r':
			fbEdited.appendData('\n');
			if( bEchoOn ) pCon->appendOutput("\n\r");
			break;
		
		case '\n':
			break;

		case '\177': // backspace
			if( fbEdited.getLength() > 0 )
			{
				fbEdited.usedData( -1 );  // Delete one char from the end
				if( bEchoOn ) pCon->appendOutput(ESC "[D"); // Move the cursor back one
				if( bEchoOn ) pCon->appendOutput(ESC "[P"); // Delete one character
			}
			break;

		case '\x1B': // escape sequence
			if( (unsigned char)lpInStr[j+1] == '[' )
			{
				switch( (unsigned char)lpInStr[j+2] )
				{
					case 'A': // Up
						break;

					case 'B': // Down
						break;

					case 'C': // Right
						break;

					case 'D': // Left
						break;
				}
				j+=2;
			}
			break;

		case 0:   // NOP: No operation
			break;

		case IAC: // IAC: Interpret as command
			switch( lpInStr[j+1] )
			{
			case SE: // SE: End of subnegotiation parameters.
				break;

			case NOP: // NOP: No operation
				break;

			case DM: // DM: Data mark. Indicates the position of a Synch event within the data stream. This should always be accompanied by a TCP urgent notification.
				break;

			case BRK: // BRK: Break. Indicates that the "break" or "attention" key was hit.
				break;

			case IP: // IP: Suspend, interrupt or abort the process to which the NVT is connected.
				break;

			case AO: // AO: Abort output. Allows the current process to run to completion but do not send its output to the user.
				break;

			case AYT: // AYT: Are you there. Send back to the NVT some visible evidence that the AYT was received.
				break;

			case EC: // EC: Erase character. The receiver should delete the last preceding undeleted character from the data stream.
				break;

			case EL: // EL: Erase line. Delete characters from the data stream back to but not including the previous CRLF.
				break;

			case GA: // GA: Go ahead. Used, under certain circumstances, to tell the other end that it can transmit.
				break;

			case SB: // SB: Subnegotiation of the indicated option follows.
				switch( lpInStr[j+2] )
				{
				case TERMTYPE:
					if( lpInStr[j+3] == 0 )
					{
						for( int k = 0; j+4+k < nInSize; k++ )
						{
							if( (unsigned char)lpInStr[j+4+k] == IAC &&
								(unsigned char)lpInStr[j+5+k] == SE )
							{
								lpInStr[j+4+k] = 0;
								//@TODO:  Do something with the term type...
								printf("Term type:  %s\n", &lpInStr[j+4] );
								j += 5+k;
							}
						}
					}
					else
					{
					}
					break;
					
				default:
					//printf("unknown subnegotiation parameters! (%d)\n", lpInStr[j+2] );
					break;
				}
				break;

			case WILL: // WILL: Indicates the desire to begin performing
				switch( lpInStr[j+2] )
				{
				case SUPPRESSGA:
					j += 2;
//					pCon->usedInput( 3 );
					break;

				case TERMTYPE:
					j += 2;
//					pCon->usedInput( 3 );
					break;

				case ECHO:
					j += 2;
//					pCon->usedInput( 3 );
					break;
					
				case NAWS:
				default:
					pCon->appendOutput( (char)ESC[0] );
					pCon->appendOutput( (char)DONT );
					pCon->appendOutput( lpInStr[j+2] );
					//printf("unknown will command used! (%d)\n", lpInStr[j+2] );
					j += 2;
					break;
				}
				break;

			case WONT: // WONT: Indicates the refusal to perform
				switch( lpInStr[j+2] )
				{
				case ECHO:
					j += 2;
//					pCon->usedInput( 3 );
					break;
					
				default:
					//printf("unknown wont command used! (%d)\n", lpInStr[j+2] );
					j += 2;
					break;
				}
				break;

			case DO: // DO: Indicates the request that the other party perform
				switch( lpInStr[j+2] )
				{
				case ECHO:
					j += 2;
					break;
					
				case SUPPRESSGA:
					j += 2;
					break;
					
				default:
					pCon->appendOutput( (char)ESC[0] );
					pCon->appendOutput( (char)DONT );
					pCon->appendOutput( lpInStr[j+2] );
					//printf("unknown do command used! (%d)\n", lpInStr[j+2] );
					j += 2;
					break;
				}
//				pCon->usedInput( 3 );
				break;

			case DONT: // DONT: Indicates the demand that the other party stop performing
				switch( lpInStr[j+2] )
				{
				case ECHO:
					j += 2;
//					pCon->usedInput( 3 );
					break;
				
				default:
					printf("unknown dont command used! (%d)\n", lpInStr[j+2] );
					j += 2;
					break;
				}
				break;
			}
			break;

		default:
			fbEdited.appendData( lpInStr[j] );
			if( bEchoOn ) pCon->appendOutput( lpInStr[j] );
			break;
		}
	}

	pCon->usedInput( pCon->getInputAmnt() );

    return true;
}

char *ProtocolTelnet::getLine( bool bFullOnly )
{
	int i = fbEdited.findChar('\n');

	if( i < 0 )
	{
		if( bFullOnly == false )
		{
			i = fbEdited.getLength();
		}
		else
		{
			return NULL;
		}
	}

	char *lpStr = new char[i+1];
	strncpy( lpStr, fbEdited.getData(), i );
	lpStr[i] = '\0';

	fbEdited.usedData( i+1 );

	return lpStr;
}

char *ProtocolTelnet::peekLine( bool bFullOnly )
{
	int i = fbEdited.findChar('\n');

	if( i < 0 )
	{
		if( bFullOnly == false )
		{
			i = fbEdited.getLength();
		}
		else
		{
			return NULL;
		}
	}

	char *lpStr = new char[i+1];
	strncpy( lpStr, fbEdited.getData(), i );
	lpStr[i] = '\0';

	return lpStr;
}

void ProtocolTelnet::setEcho( bool bEchoOn )
{
	this->bEchoOn = bEchoOn;
}