-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathTelnetConnection.cs
More file actions
515 lines (459 loc) · 19 KB
/
TelnetConnection.cs
File metadata and controls
515 lines (459 loc) · 19 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
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/*
RMLib: Nonvisual support classes used by multiple R&M Software programs
Copyright (C) Rick Parrish, R&M Software
This file is part of RMLib.
RMLib 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 3 of the License, or
any later version.
RMLib 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 RMLib. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Net.Sockets;
using System.Text;
namespace RandM.RMLib
{
public class TelnetConnection : TcpConnection
{
private bool[] _TelnetOptions = new bool[255];
private TelnetNegotiationState _TelnetNegotiationState = TelnetNegotiationState.Data;
private TelnetOption _SubnegotiationOption = TelnetOption.None;
private StringBuilder _SubnegotiationData = new StringBuilder();
// NB: The base class constructor calls InitSocket(), which means this method will run before this classes constructor, so
// everything accessed here needs to be initialized already (ie can't rely on the constructor to initialize it)
protected override void InitSocket()
{
base.InitSocket();
Array.Clear(_TelnetOptions, 0, _TelnetOptions.Length);
_TelnetNegotiationState = TelnetNegotiationState.Data;
}
protected override void NegotiateInbound(byte[] data, int numberOfBytes)
{
for (int i = 0; i < numberOfBytes; i++)
{
if (_TelnetNegotiationState == TelnetNegotiationState.Data)
{
if (data[i] == (byte)TelnetCommand.IAC)
{
_TelnetNegotiationState = TelnetNegotiationState.IAC;
}
else
{
AddToInputQueue(data[i]);
}
}
else if (_TelnetNegotiationState == TelnetNegotiationState.IAC)
{
if (data[i] == (byte)TelnetCommand.IAC)
{
_TelnetNegotiationState = TelnetNegotiationState.Data;
AddToInputQueue(data[i]);
}
else
{
switch ((TelnetCommand)data[i])
{
case TelnetCommand.NoOperation:
case TelnetCommand.DataMark:
case TelnetCommand.Break:
case TelnetCommand.InterruptProcess:
case TelnetCommand.AbortOutput:
case TelnetCommand.AreYouThere:
case TelnetCommand.EraseCharacter:
case TelnetCommand.EraseLine:
case TelnetCommand.GoAhead:
// TODO We recognize, but ignore these for now
_TelnetNegotiationState = TelnetNegotiationState.Data;
break;
case TelnetCommand.Do: _TelnetNegotiationState = TelnetNegotiationState.Do; break;
case TelnetCommand.Dont: _TelnetNegotiationState = TelnetNegotiationState.Dont; break;
case TelnetCommand.Will: _TelnetNegotiationState = TelnetNegotiationState.Will; break;
case TelnetCommand.Wont: _TelnetNegotiationState = TelnetNegotiationState.Wont; break;
case TelnetCommand.Subnegotiation:
_TelnetNegotiationState = TelnetNegotiationState.Subnegotiation;
_SubnegotiationData.Length = 0;
break;
default: _TelnetNegotiationState = TelnetNegotiationState.Data; break;
}
}
}
else if (_TelnetNegotiationState == TelnetNegotiationState.Do)
{
switch ((TelnetOption)data[i])
{
case TelnetOption.TransmitBinary: SendWill(data[i]); break;
case TelnetOption.Echo: SendWill(data[i]); break;
case TelnetOption.SuppressGoAhead: SendWill(data[i]); break;
case TelnetOption.WindowSize: SendWont(data[i]); break;
case TelnetOption.TerminalType: SendWont(data[i]); break;
case TelnetOption.LineMode: SendWont(data[i]); break;
default: SendWont(data[i]); break;
}
_TelnetNegotiationState = TelnetNegotiationState.Data;
}
else if (_TelnetNegotiationState == TelnetNegotiationState.Dont)
{
switch ((TelnetOption)data[i])
{
case TelnetOption.TransmitBinary: SendWill(data[i]); break;
case TelnetOption.Echo: SendWill(data[i]); break;
case TelnetOption.SuppressGoAhead: SendWill(data[i]); break;
case TelnetOption.WindowSize: SendWont(data[i]); break;
case TelnetOption.TerminalType: SendWont(data[i]); break;
case TelnetOption.LineMode: SendWont(data[i]); break;
default: SendWont(data[i]); break;
}
_TelnetNegotiationState = TelnetNegotiationState.Data;
}
else if (_TelnetNegotiationState == TelnetNegotiationState.Will)
{
switch ((TelnetOption)data[i])
{
case TelnetOption.TransmitBinary: SendDo(data[i]); break;
case TelnetOption.Echo: SendDont(data[i]); break;
case TelnetOption.SuppressGoAhead: SendDo(data[i]); break;
case TelnetOption.WindowSize: SendDont(data[i]); break;
case TelnetOption.TerminalType: SendSubnegotiate(data[i]); break;
case TelnetOption.LineMode: SendDont(data[i]); break;
default: SendDont(data[i]); break;
}
_TelnetNegotiationState = TelnetNegotiationState.Data;
}
else if (_TelnetNegotiationState == TelnetNegotiationState.Wont)
{
switch ((TelnetOption)data[i])
{
case TelnetOption.TransmitBinary: SendDont(data[i]); break;
case TelnetOption.Echo: SendDont(data[i]); break;
case TelnetOption.SuppressGoAhead: SendDo(data[i]); break;
case TelnetOption.WindowSize: SendDont(data[i]); break;
case TelnetOption.TerminalType: SendDont(data[i]); break;
case TelnetOption.LineMode: SendDont(data[i]); break;
default: SendDont(data[i]); break;
}
_TelnetNegotiationState = TelnetNegotiationState.Data;
}
// TODO Port to fTelnet, HtmlTerm
else if (_TelnetNegotiationState == TelnetNegotiationState.Subnegotiation)
{
// First byte of subnegotiation should be the option to negotiate
switch ((TelnetOption)data[i])
{
case TelnetOption.TerminalType:
case TelnetOption.WindowSize:
// Known option
_SubnegotiationOption = (TelnetOption)data[i];
break;
default:
// Unknown option
_SubnegotiationOption = TelnetOption.None;
break;
}
_TelnetNegotiationState = TelnetNegotiationState.SubnegotiationData;
}
else if (_TelnetNegotiationState == TelnetNegotiationState.SubnegotiationData)
{
// Add next byte to negotiation string, unless it's an IAC
switch ((TelnetCommand)data[i])
{
case TelnetCommand.IAC: _TelnetNegotiationState = TelnetNegotiationState.SubnegotiationIAC; break;
default: _SubnegotiationData.Append((char)data[i]); break;
}
}
else if (_TelnetNegotiationState == TelnetNegotiationState.SubnegotiationIAC)
{
// Check to see if negotiation has ended, or if it's just a doubled IAC
switch ((TelnetCommand)data[i])
{
case TelnetCommand.IAC:
// Doubled IAC means go back to data
_SubnegotiationData.Append((char)data[i]);
_TelnetNegotiationState = TelnetNegotiationState.SubnegotiationData;
break;
case TelnetCommand.EndSubnegotiation:
// Subnegotiation has ended
// TODO Do something with FSubnegotiationData based on FSubnegotiationOption
switch (_SubnegotiationOption)
{
case TelnetOption.TerminalType:
// TODO
break;
case TelnetOption.WindowSize:
// TODO
break;
}
_TelnetNegotiationState = TelnetNegotiationState.Data;
break;
default:
// Something unknown has happened
_TelnetNegotiationState = TelnetNegotiationState.Data;
break;
}
}
else
{
_TelnetNegotiationState = TelnetNegotiationState.Data;
}
}
}
protected override void NegotiateOutbound(byte[] data, int numberOfBytes)
{
for (int i = 0; i < numberOfBytes; i++)
{
// If there's an IAC, double it up
if (data[i] == (byte)TelnetCommand.IAC) _OutputBuffer.Enqueue((byte)TelnetCommand.IAC);
_OutputBuffer.Enqueue(data[i]);
}
}
public override bool Open(Socket socket)
{
if (base.Open(socket))
{
SendWill(TelnetOption.TransmitBinary);
SendDont(TelnetOption.Echo);
SendDo(TelnetOption.SuppressGoAhead);
SendDont(TelnetOption.WindowSize);
SendDont(TelnetOption.LineMode);
return true;
}
return false;
}
public void SendDo(byte option)
{
SendIAC(TelnetCommand.Do, option);
}
public void SendDo(TelnetOption option)
{
SendIAC(TelnetCommand.Do, option);
}
public void SendDont(byte option)
{
SendIAC(TelnetCommand.Dont, option);
}
public void SendDont(TelnetOption option)
{
SendIAC(TelnetCommand.Dont, option);
}
public void SendGoAhead()
{
if (Connected) base.WriteRaw(new byte[] { (byte)TelnetCommand.IAC, (byte)TelnetCommand.GoAhead });
}
private void SendIAC(TelnetCommand command, byte option)
{
if (Connected)
{
if (!_TelnetOptions[option])
{
_TelnetOptions[option] = true;
base.WriteRaw(new byte[] { (byte)TelnetCommand.IAC, (byte)command, option });
}
}
}
private void SendIAC(TelnetCommand command, TelnetOption option)
{
SendIAC(command, (byte)option);
}
public void SendSubnegotiate(TelnetOption option)
{
SendSubnegotiate((byte)option);
}
public void SendSubnegotiate(byte option)
{
base.WriteRaw(new byte[] { (byte)TelnetCommand.IAC,
(byte)TelnetCommand.Subnegotiation,
option,
1, // TODO This is for SEND in Terminal Type
(byte)TelnetCommand.IAC,
(byte)TelnetCommand.EndSubnegotiation });
}
public void SendWill(byte option)
{
SendIAC(TelnetCommand.Will, option);
}
public void SendWill(TelnetOption option)
{
SendIAC(TelnetCommand.Will, option);
}
public void SendWont(byte option)
{
SendIAC(TelnetCommand.Wont, option);
}
public void SendWont(TelnetOption option)
{
SendIAC(TelnetCommand.Wont, option);
}
}
/// <summary>
/// Commands the telnet negotiator will handle
/// </summary>
public enum TelnetCommand : byte
{
/// <summary>
/// SE: End of subnegotiation parameters.
/// </summary>
EndSubnegotiation = 240,
/// <summary>
/// NOP: No operation.
/// </summary>
NoOperation = 241,
/// <summary>
/// Data Mark: The data stream portion of a Synch. This should always be accompanied by a TCP Urgent notification.
/// </summary>
DataMark = 242,
/// <summary>
/// Break: NVT character BRK.
/// </summary>
Break = 243,
/// <summary>
/// Interrupt Process: The function IP.
/// </summary>
InterruptProcess = 244,
/// <summary>
/// Abort output: The function AO.
/// </summary>
AbortOutput = 245,
/// <summary>
/// Are You There: The function AYT.
/// </summary>
AreYouThere = 246,
/// <summary>
/// Erase character: The function EC.
/// </summary>
EraseCharacter = 247,
/// <summary>
/// Erase Line: The function EL.
/// </summary>
EraseLine = 248,
/// <summary>
/// Go ahead: The GA signal
/// </summary>
GoAhead = 249,
/// <summary>
/// SB: Indicates that what follows is subnegotiation of the indicated option.
/// </summary>
Subnegotiation = 250,
/// <summary>
/// WILL: Indicates the desire to begin performing, or confirmation that you are now performing, the indicated option.
/// </summary>
Will = 251,
/// <summary>
/// WON'T: Indicates the refusal to perform, or continue performing, the indicated option.
/// </summary>
Wont = 252,
/// <summary>
/// DO: Indicates the request that the other party perform, or confirmation that you are expecting the other party to perform, the indicated option.
/// </summary>
Do = 253,
/// <summary>
/// DON'T: Indicates the demand that the other party stop performing, or confirmation that you are no longer expecting the other party to perform, the indicated option.
/// </summary>
Dont = 254,
/// <summary>
/// IAC: Data Byte 255
/// </summary>
IAC = 255
}
/// <summary>
/// The possible states the telnet negotiator may find itself in
/// </summary>
public enum TelnetNegotiationState
{
/// <summary>
/// The default data state
/// </summary>
Data,
/// <summary>
/// The last received character was an IAC
/// </summary>
IAC,
/// <summary>
/// The last received character was a DO command
/// </summary>
Do,
/// <summary>
/// The last received character was a DONT command
/// </summary>
Dont,
/// <summary>
/// The last received character was a WILL command
/// </summary>
Will,
/// <summary>
/// The last received character was a WONT command
/// </summary>
Wont,
/// <summary>
/// The last received character was a SUBNEGOTIATION command
/// </summary>
Subnegotiation,
/// <summary>
/// The last received character was the option to be negotiated
/// </summary>
SubnegotiationData,
/// <summary>
/// The last received character was an IAC
/// </summary>
SubnegotiationIAC
}
/// <summary>
/// Options that can be enabled/disabled in a telnet connection
/// </summary>
public enum TelnetOption : sbyte
{
/// <summary>
/// Null option
/// </summary>
None = -1,
/// <summary>
/// When enabled, data is transmitted as 8-bit binary data.
/// </summary>
/// <remarks>
/// Defined in RFC 856
///
/// Default is to not transmit in binary.
/// </remarks>
TransmitBinary = 0,
/// <summary>
/// When enabled, the side performing the echoing transmits (echos) data characters it receives back to the sender of the data characters.
/// </summary>
/// <remarks>
/// Defined in RFC 857
///
/// Default is to not echo over the telnet connection.
/// </remarks>
Echo = 1,
/// <summary>
/// When enabled, the sender need not transmit GAs.
/// </summary>
/// <remarks>
/// Defined in RFC 858
///
/// Default is to not suppress go aheads.
/// </remarks>
SuppressGoAhead = 3,
TerminalType = 24,
/// <summary>
/// Allows the NAWS (negotiate about window size) subnegotiation command to be used if both sides agree
/// </summary>
/// <remarks>
/// Defined in RFC 1073
///
/// Default is to not allow the NAWS subnegotiation
/// </remarks>
WindowSize = 31,
/// <summary>
/// Linemode Telnet is a way of doing terminal character processing on the client side of a Telnet connection.
/// </summary>
/// <remarks>
/// Defined in RFC 1184
///
/// Default is to not allow the LINEMODE subnegotiation
/// </remarks>
LineMode = 34,
}
}