ltelnet.pp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. { lTelnet CopyRight (C) 2004-2008 Ales Katona
  2. This library is Free software; you can rediStribute it and/or modify it
  3. under the terms of the GNU Library General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or (at your
  5. option) any later version.
  6. This program is diStributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; withOut even the implied warranty of MERCHANTABILITY or
  8. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  9. for more details.
  10. You should have received a Copy of the GNU Library General Public License
  11. along with This library; if not, Write to the Free Software Foundation,
  12. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  13. This license has been modified. See File LICENSE for more inFormation.
  14. Should you find these sources withOut a LICENSE File, please contact
  15. me at [email protected]
  16. }
  17. unit lTelnet;
  18. {$mode objfpc}{$H+}
  19. //{$define debug}
  20. interface
  21. uses
  22. Classes, lNet, lControlStack;
  23. const
  24. // Telnet printer signals
  25. TS_NUL = #0;
  26. TS_ECHO = #1;
  27. TS_SGA = #3; // Surpass go-ahead
  28. TS_BEL = #7;
  29. TS_BS = #8;
  30. TS_HT = #9;
  31. TS_LF = #10;
  32. TS_VT = #11;
  33. TS_FF = #12;
  34. TS_CR = #13;
  35. // Telnet control signals
  36. TS_NAWS = #31;
  37. TS_DATA_MARK = #128;
  38. TS_BREAK = #129;
  39. TS_HYI = #133; // Hide Your Input
  40. // Data types codes
  41. TS_STDTELNET = #160;
  42. TS_TRANSPARENT = #161;
  43. TS_EBCDIC = #162;
  44. // Control bytes
  45. TS_SE = #240;
  46. TS_NOP = #241;
  47. TS_GA = #249; // go ahead currently ignored(full duplex)
  48. TS_SB = #250;
  49. TS_WILL = #251;
  50. TS_WONT = #252;
  51. TS_DO = #253;
  52. TS_DONT = #254;
  53. // Mother of all power
  54. TS_IAC = #255;
  55. type
  56. TLTelnetClient = class;
  57. TLTelnetControlChars = set of Char;
  58. TLHowEnum = (TE_WILL = 251, TE_WONT, TE_DO, TE_DONW);
  59. { TLTelnet }
  60. TLTelnet = class(TLComponent, ILDirect)
  61. protected
  62. FStack: TLControlStack;
  63. FConnection: TLTcp;
  64. FPossible: TLTelnetControlChars;
  65. FActiveOpts: TLTelnetControlChars;
  66. FOutput: TMemoryStream;
  67. FOperation: Char;
  68. FCommandCharIndex: Byte;
  69. FOnReceive: TLSocketEvent;
  70. FOnConnect: TLSocketEvent;
  71. FOnDisconnect: TLSocketEvent;
  72. FOnError: TLSocketErrorEvent;
  73. FCommandArgs: string[3];
  74. FOrders: TLTelnetControlChars;
  75. FBuffer: array of Char;
  76. FBufferIndex: Integer;
  77. FBufferEnd: Integer;
  78. procedure InflateBuffer;
  79. function AddToBuffer(const aStr: string): Boolean; inline;
  80. function Question(const Command: Char; const Value: Boolean): Char;
  81. function GetConnected: Boolean;
  82. function GetTimeout: Integer;
  83. procedure SetTimeout(const Value: Integer);
  84. function GetSocketClass: TLSocketClass;
  85. procedure SetSocketClass(Value: TLSocketClass);
  86. function GetSession: TLSession;
  87. procedure SetSesssion(const AValue: TLSession);
  88. procedure SetCreator(AValue: TLComponent); override;
  89. procedure StackFull;
  90. procedure DoubleIAC(var s: string);
  91. function TelnetParse(const msg: string): Integer;
  92. procedure React(const Operation, Command: Char); virtual; abstract;
  93. procedure SendCommand(const Command: Char; const Value: Boolean); virtual; abstract;
  94. procedure OnCs(aSocket: TLSocket);
  95. public
  96. constructor Create(aOwner: TComponent); override;
  97. destructor Destroy; override;
  98. function Get(out aData; const aSize: Integer; aSocket: TLSocket = nil): Integer; virtual; abstract;
  99. function GetMessage(out msg: string; aSocket: TLSocket = nil): Integer; virtual; abstract;
  100. function Send(const aData; const aSize: Integer; aSocket: TLSocket = nil): Integer; virtual; abstract;
  101. function SendMessage(const msg: string; aSocket: TLSocket = nil): Integer; virtual; abstract;
  102. function OptionIsSet(const Option: Char): Boolean;
  103. function RegisterOption(const aOption: Char; const aCommand: Boolean): Boolean;
  104. procedure SetOption(const Option: Char);
  105. procedure UnSetOption(const Option: Char);
  106. procedure Disconnect(const Forced: Boolean = True); override;
  107. procedure SendCommand(const aCommand: Char; const How: TLHowEnum); virtual;
  108. public
  109. property Output: TMemoryStream read FOutput;
  110. property Connected: Boolean read GetConnected;
  111. property Timeout: Integer read GetTimeout write SetTimeout;
  112. property OnReceive: TLSocketEvent read FOnReceive write FOnReceive;
  113. property OnDisconnect: TLSocketEvent read FOnDisconnect write FOnDisconnect;
  114. property OnConnect: TLSocketEvent read FOnConnect write FOnConnect;
  115. property OnError: TLSocketErrorEvent read FOnError write FOnError;
  116. property Connection: TLTCP read FConnection;
  117. property SocketClass: TLSocketClass read GetSocketClass write SetSocketClass;
  118. property Session: TLSession read GetSession write SetSesssion;
  119. end;
  120. { TLTelnetClient }
  121. TLTelnetClient = class(TLTelnet, ILClient)
  122. protected
  123. FLocalEcho: Boolean;
  124. procedure OnEr(const msg: string; aSocket: TLSocket);
  125. procedure OnDs(aSocket: TLSocket);
  126. procedure OnRe(aSocket: TLSocket);
  127. procedure OnCo(aSocket: TLSocket);
  128. procedure React(const Operation, Command: Char); override;
  129. procedure SendCommand(const Command: Char; const Value: Boolean); override;
  130. public
  131. constructor Create(aOwner: TComponent); override;
  132. function Connect(const anAddress: string; const aPort: Word): Boolean;
  133. function Connect: Boolean;
  134. function Get(out aData; const aSize: Integer; aSocket: TLSocket = nil): Integer; override;
  135. function GetMessage(out msg: string; aSocket: TLSocket = nil): Integer; override;
  136. function Send(const aData; const aSize: Integer; aSocket: TLSocket = nil): Integer; override;
  137. function SendMessage(const msg: string; aSocket: TLSocket = nil): Integer; override;
  138. procedure CallAction; override;
  139. public
  140. property LocalEcho: Boolean read FLocalEcho write FLocalEcho;
  141. end;
  142. implementation
  143. uses
  144. SysUtils, Math;
  145. var
  146. zz: Char;
  147. TNames: array[Char] of string;
  148. //*******************************TLTelnetClient********************************
  149. constructor TLTelnet.Create(aOwner: TComponent);
  150. begin
  151. inherited Create(aOwner);
  152. FConnection := TLTCP.Create(nil);
  153. FConnection.Creator := Self;
  154. FConnection.OnCanSend := @OnCs;
  155. FOutput := TMemoryStream.Create;
  156. FCommandCharIndex := 0;
  157. FStack := TLControlStack.Create;
  158. FStack.OnFull := @StackFull;
  159. end;
  160. destructor TLTelnet.Destroy;
  161. begin
  162. Disconnect(True);
  163. FOutput.Free;
  164. FConnection.Free;
  165. FStack.Free;
  166. inherited Destroy;
  167. end;
  168. function TLTelnet.GetConnected: Boolean;
  169. begin
  170. Result := FConnection.Connected;
  171. end;
  172. function TLTelnet.GetSession: TLSession;
  173. begin
  174. Result := FConnection.Session;
  175. end;
  176. procedure TLTelnet.SetSesssion(const AValue: TLSession);
  177. begin
  178. FConnection.Session := aValue;
  179. end;
  180. procedure TLTelnet.SetCreator(AValue: TLComponent);
  181. begin
  182. inherited SetCreator(AValue);
  183. FConnection.Creator := aValue;
  184. end;
  185. procedure TLTelnet.InflateBuffer;
  186. var
  187. n: Integer;
  188. begin
  189. n := Max(Length(FBuffer), 25);
  190. SetLength(FBuffer, n * 10);
  191. end;
  192. function TLTelnet.AddToBuffer(const aStr: string): Boolean; inline;
  193. begin
  194. Result := False;
  195. while Length(aStr) + FBufferEnd > Length(FBuffer) do
  196. InflateBuffer;
  197. Move(aStr[1], FBuffer[FBufferEnd], Length(aStr));
  198. Inc(FBufferEnd, Length(aStr));
  199. end;
  200. function TLTelnet.Question(const Command: Char; const Value: Boolean): Char;
  201. begin
  202. Result := TS_NOP;
  203. if Value then begin
  204. if Command in FOrders then
  205. Result := TS_DO
  206. else
  207. Result := TS_WILL;
  208. end else begin
  209. if Command in FOrders then
  210. Result := TS_DONT
  211. else
  212. Result := TS_WONT;
  213. end;
  214. end;
  215. function TLTelnet.GetSocketClass: TLSocketClass;
  216. begin
  217. Result := FConnection.SocketClass;
  218. end;
  219. function TLTelnet.GetTimeout: Integer;
  220. begin
  221. Result := FConnection.Timeout;
  222. end;
  223. procedure TLTelnet.SetSocketClass(Value: TLSocketClass);
  224. begin
  225. FConnection.SocketClass := Value;
  226. end;
  227. procedure TLTelnet.SetTimeout(const Value: Integer);
  228. begin
  229. FConnection.Timeout := Value;
  230. end;
  231. procedure TLTelnet.StackFull;
  232. begin
  233. {$ifdef debug}
  234. Writeln('**STACKFULL**');
  235. {$endif}
  236. if FStack[1] = TS_IAC then
  237. begin
  238. FOutput.WriteByte(Byte(FStack[1]));
  239. FOutput.WriteByte(Byte(FStack[2]));
  240. end else React(FStack[1], FStack[2]);
  241. FStack.Clear;
  242. end;
  243. procedure TLTelnet.DoubleIAC(var s: string);
  244. var
  245. i: Longint;
  246. begin
  247. i := 0;
  248. if Length(s) > 0 then
  249. while i < Length(s) do begin
  250. Inc(i);
  251. if s[i] = TS_IAC then begin
  252. Insert(TS_IAC, s, i);
  253. Inc(i, 2);
  254. end;
  255. end;
  256. end;
  257. function TLTelnet.TelnetParse(const msg: string): Integer;
  258. var
  259. i: Longint;
  260. begin
  261. Result := 0;
  262. for i := 1 to Length(msg) do
  263. if (FStack.ItemIndex > 0) or (msg[i] = TS_IAC) then begin
  264. if msg[i] = TS_GA then
  265. FStack.Clear
  266. else
  267. FStack.Push(msg[i])
  268. end else begin
  269. FOutput.WriteByte(Byte(msg[i]));
  270. Inc(Result);
  271. end;
  272. end;
  273. procedure TLTelnet.OnCs(aSocket: TLSocket);
  274. var
  275. n: Integer;
  276. begin
  277. n := 1;
  278. while (n > 0) and (FBufferIndex < FBufferEnd) do begin
  279. n := FConnection.Send(FBuffer[FBufferIndex], FBufferEnd - FBufferIndex);
  280. if n > 0 then
  281. Inc(FBufferIndex, n);
  282. end;
  283. if FBufferEnd - FBufferIndex < FBufferIndex then begin // if we can move the "right" side of the buffer back to the left
  284. Move(FBuffer[FBufferIndex], FBuffer[0], FBufferEnd - FBufferIndex);
  285. FBufferEnd := FBufferEnd - FBufferIndex;
  286. FBufferIndex := 0;
  287. end;
  288. end;
  289. function TLTelnet.OptionIsSet(const Option: Char): Boolean;
  290. begin
  291. Result := False;
  292. Result := Option in FActiveOpts;
  293. end;
  294. function TLTelnet.RegisterOption(const aOption: Char;
  295. const aCommand: Boolean): Boolean;
  296. begin
  297. Result := False;
  298. if not (aOption in FPossible) then begin
  299. FPossible := FPossible + [aOption];
  300. if aCommand then
  301. FOrders := FOrders + [aOption];
  302. Result := True;
  303. end;
  304. end;
  305. procedure TLTelnet.SetOption(const Option: Char);
  306. begin
  307. if Option in FPossible then
  308. SendCommand(Option, True);
  309. end;
  310. procedure TLTelnet.UnSetOption(const Option: Char);
  311. begin
  312. if Option in FPossible then
  313. SendCommand(Option, False);
  314. end;
  315. procedure TLTelnet.Disconnect(const Forced: Boolean = True);
  316. begin
  317. FConnection.Disconnect(Forced);
  318. end;
  319. procedure TLTelnet.SendCommand(const aCommand: Char; const How: TLHowEnum);
  320. begin
  321. {$ifdef debug}
  322. Writeln('**SENT** ', TNames[Char(How)], ' ', TNames[aCommand]);
  323. {$endif}
  324. AddToBuffer(TS_IAC + Char(How) + aCommand);
  325. OnCs(nil);
  326. end;
  327. //****************************TLTelnetClient*****************************
  328. constructor TLTelnetClient.Create(aOwner: TComponent);
  329. begin
  330. inherited Create(aOwner);
  331. FConnection.OnError := @OnEr;
  332. FConnection.OnDisconnect := @OnDs;
  333. FConnection.OnReceive := @OnRe;
  334. FConnection.OnConnect := @OnCo;
  335. FPossible := [TS_ECHO, TS_HYI, TS_SGA];
  336. FActiveOpts := [];
  337. FOrders := [];
  338. end;
  339. procedure TLTelnetClient.OnEr(const msg: string; aSocket: TLSocket);
  340. begin
  341. if Assigned(FOnError) then
  342. FOnError(msg, aSocket)
  343. else
  344. FOutput.Write(Pointer(msg)^, Length(msg));
  345. end;
  346. procedure TLTelnetClient.OnDs(aSocket: TLSocket);
  347. begin
  348. if Assigned(FOnDisconnect) then
  349. FOnDisconnect(aSocket);
  350. end;
  351. procedure TLTelnetClient.OnRe(aSocket: TLSocket);
  352. var
  353. s: string;
  354. begin
  355. if aSocket.GetMessage(s) > 0 then
  356. if (TelnetParse(s) > 0) and Assigned(FOnReceive) then
  357. FOnReceive(aSocket);
  358. end;
  359. procedure TLTelnetClient.OnCo(aSocket: TLSocket);
  360. begin
  361. if Assigned(FOnConnect) then
  362. FOnConnect(aSocket);
  363. end;
  364. procedure TLTelnetClient.React(const Operation, Command: Char);
  365. procedure Accept(const Operation, Command: Char);
  366. begin
  367. FActiveOpts := FActiveOpts + [Command];
  368. {$ifdef debug}
  369. Writeln('**SENT** ', TNames[Operation], ' ', TNames[Command]);
  370. {$endif}
  371. AddToBuffer(TS_IAC + Operation + Command);
  372. OnCs(nil);
  373. end;
  374. procedure Refuse(const Operation, Command: Char);
  375. begin
  376. FActiveOpts := FActiveOpts - [Command];
  377. {$ifdef debug}
  378. Writeln('**SENT** ', TNames[Operation], ' ', TNames[Command]);
  379. {$endif}
  380. AddToBuffer(TS_IAC + Operation + Command);
  381. OnCs(nil);
  382. end;
  383. begin
  384. {$ifdef debug}
  385. Writeln('**GOT** ', TNames[Operation], ' ', TNames[Command]);
  386. {$endif}
  387. case Operation of
  388. TS_DO : if Command in FPossible then Accept(TS_WILL, Command)
  389. else Refuse(TS_WONT, Command);
  390. TS_DONT : if Command in FPossible then Refuse(TS_WONT, Command);
  391. TS_WILL : if Command in FPossible then FActiveOpts := FActiveOpts + [Command]
  392. else Refuse(TS_DONT, Command);
  393. TS_WONT : if Command in FPossible then FActiveOpts := FActiveOpts - [Command];
  394. end;
  395. end;
  396. procedure TLTelnetClient.SendCommand(const Command: Char; const Value: Boolean);
  397. begin
  398. if Connected then begin
  399. {$ifdef debug}
  400. Writeln('**SENT** ', TNames[Question(Command, Value)], ' ', TNames[Command]);
  401. {$endif}
  402. case Question(Command, Value) of
  403. TS_WILL : FActiveOpts := FActiveOpts + [Command];
  404. end;
  405. AddToBuffer(TS_IAC + Question(Command, Value) + Command);
  406. OnCs(nil);
  407. end;
  408. end;
  409. function TLTelnetClient.Connect(const anAddress: string; const aPort: Word): Boolean;
  410. begin
  411. Result := FConnection.Connect(anAddress, aPort);
  412. end;
  413. function TLTelnetClient.Connect: Boolean;
  414. begin
  415. Result := FConnection.Connect(FHost, FPort);
  416. end;
  417. function TLTelnetClient.Get(out aData; const aSize: Integer; aSocket: TLSocket): Integer;
  418. begin
  419. Result := FOutput.Read(aData, aSize);
  420. if FOutput.Position = FOutput.Size then
  421. FOutput.Clear;
  422. end;
  423. function TLTelnetClient.GetMessage(out msg: string; aSocket: TLSocket): Integer;
  424. begin
  425. Result := 0;
  426. msg := '';
  427. if FOutput.Size > 0 then begin
  428. FOutput.Position := 0;
  429. SetLength(msg, FOutput.Size);
  430. Result := FOutput.Read(PChar(msg)^, Length(msg));
  431. FOutput.Clear;
  432. end;
  433. end;
  434. function TLTelnetClient.Send(const aData; const aSize: Integer;
  435. aSocket: TLSocket): Integer;
  436. var
  437. Tmp: string;
  438. begin
  439. {$ifdef debug}
  440. Writeln('**SEND START** ');
  441. {$endif}
  442. Result := 0;
  443. if aSize > 0 then begin
  444. SetLength(Tmp, aSize);
  445. Move(aData, PChar(Tmp)^, aSize);
  446. DoubleIAC(Tmp);
  447. if LocalEcho and (not OptionIsSet(TS_ECHO)) and (not OptionIsSet(TS_HYI)) then
  448. FOutput.Write(PChar(Tmp)^, Length(Tmp));
  449. AddToBuffer(Tmp);
  450. OnCs(nil);
  451. Result := aSize;
  452. end;
  453. {$ifdef debug}
  454. Writeln('**SEND END** ');
  455. {$endif}
  456. end;
  457. function TLTelnetClient.SendMessage(const msg: string; aSocket: TLSocket
  458. ): Integer;
  459. begin
  460. Result := Send(PChar(msg)^, Length(msg));
  461. end;
  462. procedure TLTelnetClient.CallAction;
  463. begin
  464. FConnection.CallAction;
  465. end;
  466. initialization
  467. for zz := #0 to #255 do
  468. TNames[zz] := IntToStr(Ord(zz));
  469. TNames[#1] := 'TS_ECHO';
  470. TNames[#133] := 'TS_HYI';
  471. TNames[#251] := 'TS_WILL';
  472. TNames[#252] := 'TS_WONT';
  473. TNames[#253] := 'TS_DO';
  474. TNames[#254] := 'TS_DONT';
  475. end.