-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTelnet.p
429 lines (381 loc) · 11.9 KB
/
Telnet.p
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
{ Segments: Telnet_1 }
unit Telnet;
interface
uses
Processes, AppleTalk, ADSP, Serial, Sound, TCPTypes, Initial, NodePrefs, InpOut4;
procedure DoTelnetNegotiation;
implementation
const
TelnetTimeout = 60; { ticks }
{ Telnet sequences. }
telnetSE = 240;
telnetNOP = 241;
telnetDataMark = 242;
telnetBreak = 243;
telnetInterruptProcess = 244;
telnetAbortOutput = 245;
telnetAreYouThere = 246;
telnetEraseCharacter = 247;
telnetEraseLine = 248;
telnetGoAhead = 249;
telnetSB = 250;
telnetWILL = 251;
telnetWONT = 252;
telnetDO = 253;
telnetDONT = 254;
telnetIAC = 255;
{ The telnet options we process. }
TRANSMIT_BINARY = $00;
ECHO = $01;
SUPPRESS_GO_AHEAD = $03;
type
{ Telnet state. }
eTelnetState = (waitingIAC, waitingCommand, waitingCode);
{ Q Method types. }
eUsHim = (NO, WANTNO, WANTYES, YES);
eUsQHimQ = (EMPTY, OPPOSITE);
qOptionPtr = ^qOption;
qOption = record
us: eUsHim;
usq: eUsQHimQ;
him: eUsHim;
himq: eUsQHimQ;
end;
qOptionListPtr = ^qOptionList;
qOptionList = record
options: array[0..3] of qOption;
end;
{$S Telnet_1}
function GetTelnetString (command, code: byte): Str255;
var
tempString, tempString2: Str255;
begin
tempString := '<IAC ';
if command = telnetWILL then
tempString := concat(tempString, 'WILL ')
else if command = telnetWONT then
tempString := concat(tempString, 'WONT ')
else if command = telnetDO then
tempString := concat(tempString, 'DO ')
else if command = telnetDONT then
tempString := concat(tempString, 'DONT ');
if code = TRANSMIT_BINARY then
tempString := concat(tempString, 'TRANSMIT-BINARY')
else if code = ECHO then
tempString := concat(tempString, 'ECHO')
else if code = SUPPRESS_GO_AHEAD then
tempString := concat(tempString, 'SUPPRESS-GO-AHEAD')
else
begin
NumToString(code, tempString2);
tempString := concat(tempString, '#', tempString2);
end;
tempString := concat(tempString, '>');
GetTelnetString := tempString;
end;
procedure LogTelnet (logStr: Str255);
var
path: str255;
logRef: integer;
logStrSize: longInt;
result: OSerr;
begin
{ Display to the SysOp. }
OutLineSysop(logStr, true);
{ Write to the file if requested. }
if InitSystHand^^.DebugTelnetToFile then
begin
path := concat(sharedPath, 'Misc:Telnet Log');
result := FSOpen(path, 0, logRef);
if result <> noErr then
begin
result := FSDelete(path, 0);
result := Create(path, 0, 'HRMS', 'TEXT');
result := FSOpen(path, 0, LogRef);
end;
if result = noErr then
begin
logStr := concat(logStr, char(13));
result := SetFPos(logRef, fsFromLEOF, 0);
logStrSize := length(logStr);
result := FSWrite(logRef, logStrSize, @logStr[1]);
result := FSClose(logRef);
end;
end;
end;
procedure SendTelnet (command, code: byte);
var
{ Temporary vars. }
result: OSErr;
{ General vars. }
writeBytes: packed array[0..2] of byte;
begin
with curGlobs^ do
begin
if InitSystHand^^.DebugTelnet then
LogTelnet(concat('Telnet: sending ', GetTelnetString(command, code)));
{ Prepare our sequence. }
writeBytes[0] := telnetIAC;
writeBytes[1] := command;
writeBytes[2] := code;
{ Send the sequence. }
nodeTCP.tcpWDSPtr^.size := 3;
nodeTCP.tcpWDSPtr^.buffer := @writeBytes;
nodeTCP.tcpWDSPtr^.term := 0;
with nodeTCP.tcpPBPtr^ do
begin
ioResult := 1;
ioCompletion := nil;
ioCRefNum := ippDrvrRefNum;
csCode := TCPcsSend;
tcpStream := nodeTCP.tcpStreamPtr;
send.ulpTimeoutValue := 0;
send.ulpTimeoutAction := -1;
send.validityFlags := $c0;
send.pushFlag := 1;
send.urgentFlag := 0;
send.wds := nodeTCP.tcpWDSPtr;
send.userDataPtr := nil;
end;
result := PBControl(ParmBlkPtr(nodeTCP.tcpPBPtr), false);
end;
end;
procedure DoTelnetNegotiation;
label
2;
var
{ Temporary vars. }
result: OSErr;
tempString: Str255;
{ General variables. }
readCnt: integer;
readBytes: packed array[0..1] of byte;
cb: TCPControlBlock;
opt: qOptionPtr;
useANSI: Boolean;
{ State variables. }
options: qOptionListPtr;
state: eTelnetState;
curCommand: byte;
begin
with curGlobs^ do
begin
{ crossint7 is the current telnet command. crossint8 is the state variable. }
{ crosslong is used to hold the options pointer. }
curCommand := crossint7;
state := eTelnetState(crossint8);
options := qOptionListPtr(crosslong);
case crossint of
1: { Enter telnet negotiation stage. }
begin
if InitSystHand^^.DebugTelnet then
LogTelnet('Telnet: beginning telnet negotiation..');
{ Initialize our telnet state and set our timeout counter. If we don't receive any telnet}
{ negotiation codes in TelnetTimeout seconds, then we conclude negotiation. }
state := waitingIAC;
lastKeyPressed := TickCount;
{ Initialize our option state. We only will process options that we know about. }
{ Everything else gets ignored. }
options := qOptionListPtr(NewPtrClear(sizeof(qOptionList)));
{ Things we ask for. }
options^.options[TRANSMIT_BINARY].us := YES;
options^.options[TRANSMIT_BINARY].usq := EMPTY;
options^.options[TRANSMIT_BINARY].him := WANTYES;
options^.options[TRANSMIT_BINARY].himq := EMPTY;
SendTelnet(telnetDO, TRANSMIT_BINARY);
options^.options[ECHO].us := WANTYES;
options^.options[ECHO].usq := EMPTY;
options^.options[ECHO].him := NO;
options^.options[ECHO].himq := EMPTY;
SendTelnet(telnetWILL, ECHO);
{ Things we are asked. }
options^.options[SUPPRESS_GO_AHEAD].us := YES;
options^.options[SUPPRESS_GO_AHEAD].usq := EMPTY;
options^.options[SUPPRESS_GO_AHEAD].him := NO;
options^.options[SUPPRESS_GO_AHEAD].himq := EMPTY;
{ Start reading characters. }
crossint := 2;
end;
2:
begin { Telnet negotiation loop. }
2: { Only continue if there is data to read. }
readCnt := TCPBytesToRead(@nodeTCP);
if readCnt <> 0 then
begin
{ Read a byte. }
with cb do
begin
ioResult := 1;
ioCompletion := nil;
ioCRefNum := ippDrvrRefNum;
csCode := TCPcsRcv;
tcpStream := nodeTCP.tcpStreamPtr;
receive.commandTimeoutValue := 0;
receive.markFlag := 0;
receive.urgentFlag := 0;
receive.rcvBuff := @readBytes;
receive.rcvBuffLength := 1;
receive.userDataPtr := nil;
end; { with cb }
result := PBControl(ParmBlkPtr(@cb), false);
if result = noErr then
begin
{ Reset our telnet timeout. }
lastKeyPressed := TickCount;
{ Store the amount of data read. }
readCnt := cb.receive.rcvBuffLength;
if InitSystHand^^.DebugTelnet then
begin
NumToString(readBytes[0], tempString);
LogTelnet(concat('Telnet: received character #', tempString));
end;
{ Process this byte through our state machine. }
case state of
waitingIAC:
case readBytes[0] of
telnetIAC:
begin
{ Look for a telnet command. }
state := waitingCommand;
end;
otherwise
begin
{ We received an out of state character; telnet negotiation must be over. }
crossint := 99;
if InitSystHand^^.DebugTelnet then
LogTelnet('Telnet: out of state character; concluding negotiation.');
end;
end;
waitingCommand:
begin
curCommand := readBytes[0];
if (readBytes[0] = telnetWILL) or (readBytes[0] = telnetWONT) or (readBytes[0] = telnetDO) or (readBytes[0] = telnetDONT) then
state := waitingCode
else
begin
{ We received an out of state character; telnet negotiation must be over. }
crossint := 99;
if InitSystHand^^.DebugTelnet then
LogTelnet('Telnet: out of state character; concluding negotiation.');
end;
end;
waitingCode:
begin
if InitSystHand^^.DebugTelnet then
LogTelnet(concat('Telnet: received ', GetTelnetString(curCommand, readBytes[0])));
if (readBytes[0] = TRANSMIT_BINARY) or (readBytes[0] = ECHO) or (readBytes[0] = SUPPRESS_GO_AHEAD) then
begin
{ Process this code according to the command. }
opt := @options^.options[readBytes[0]];
if InitSystHand^^.DebugTelnet then
begin
tempString := 'Telnet:';
if opt^.us = YES then
tempString := concat(tempString, ' us=YES;')
else
tempString := concat(tempString, ' us=NO;');
if opt^.usq = EMPTY then
tempString := concat(tempString, ' usq=EMPTY;')
else
tempString := concat(tempString, ' usq=OPPOSITE;');
if opt^.him = YES then
tempString := concat(tempString, ' him=YES;')
else
tempString := concat(tempString, ' him=NO;');
if opt^.himq = EMPTY then
tempString := concat(tempString, ' himq=EMPTY;')
else
tempString := concat(tempString, ' himq=OPPOSITE;');
LogTelnet(tempString);
end;
case curCommand of
telnetWILL:
begin
if opt^.him = NO then
begin
if opt^.us = YES then
begin
opt^.him := YES;
SendTelnet(telnetDO, readBytes[0]);
end
else
SendTelnet(telnetDONT, readBytes[0]);
end
else if opt^.him = YES then
{ ignore }
else if (opt^.him = WANTNO) and (opt^.himq = EMPTY) then
begin
opt^.him := NO;
if InitSystHand^^.DebugTelnet then
LogTelnet('Telnet: error; DONT answered by WILL.')
end
else if (opt^.him = WANTNO) and (opt^.himq = OPPOSITE) then
begin
opt^.him := YES;
opt^.himq := EMPTY;
if InitSystHand^^.DebugTelnet then
LogTelnet('Telnet: error; DONT answered by WILL.');
end
else if (opt^.him = WANTYES) and (opt^.himq = EMPTY) then
begin
opt^.him := YES;
end
else if (opt^.him = WANTNO) and (opt^.himq = OPPOSITE) then
begin
opt^.him := WANTNO;
opt^.himq := EMPTY;
SendTelnet(telnetDONT, readBytes[0]);
end;
end;
end;
end
else
begin
{ Unknown code. }
if InitSystHand^^.DebugTelnet then
LogTelnet('Telnet: unknown telnet code; ignoring.');
end;
{ Wait for the next code. }
state := waitingIAC;
end;
otherwise
begin
{ We received random data; telnet negotiation must be over. }
crossint := 99;
if InitSystHand^^.DebugTelnet then
LogTelnet('Telnet: unknown character received; concluding negotiation.');
end;
end; { case state }
end; { result = noErr }
end
else { readCnt <> 0 }
begin
if TickCount > (lastKeyPressed + TelnetTimeout) then
begin
{ We received random data; telnet negotiation must be over. }
crossint := 99;
if InitSystHand^^.DebugTelnet then
LogTelnet('Telnet: telnet negotiation timed out; concluding negotiation.');
end;
end;
end;
99: { Telnet negotiation complete. }
begin
{ Determine if this user supports ANSI. }
useANSI := true;
{ Free our option pointer. }
DisposPtr(Ptr(options));
{ Log the user in. }
if InitSystHand^^.DebugTelnet then
LogTelnet('Telnet: telnet negotiation complete.');
DoLogon(useANSI);
end;
end;
{ crossint7 is the current telnet command. crossint8 is the state variable. }
{ crosslong is used to hold the options pointer. }
crossint7 := curCommand;
crossint8 := integer(state);
crosslong := longint(options);
end;
end;
end.