ltelnet.pp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. { lTelnet CopyRight (C) 2004-2006 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. FActive: 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. FConnected: Boolean;
  76. function Question(const Command: Char; const Value: Boolean): Char;
  77. function GetTimeout: DWord;
  78. procedure SetTimeout(const Value: DWord);
  79. function GetSocketClass: TLSocketClass;
  80. procedure SetSocketClass(Value: TLSocketClass);
  81. procedure StackFull;
  82. procedure DoubleIAC(var s: string);
  83. procedure TelnetParse(const msg: string);
  84. procedure React(const Operation, Command: Char); virtual; abstract;
  85. procedure SendCommand(const Command: Char; const Value: Boolean); virtual; abstract;
  86. public
  87. constructor Create(aOwner: TComponent); override;
  88. destructor Destroy; override;
  89. function Get(var aData; const aSize: Integer; aSocket: TLSocket = nil): Integer; virtual; abstract;
  90. function GetMessage(out msg: string; aSocket: TLSocket = nil): Integer; virtual; abstract;
  91. function Send(const aData; const aSize: Integer; aSocket: TLSocket = nil): Integer; virtual; abstract;
  92. function SendMessage(const msg: string; aSocket: TLSocket = nil): Integer; virtual; abstract;
  93. function OptionIsSet(const Option: Char): Boolean;
  94. function RegisterOption(const aOption: Char; const aCommand: Boolean): Boolean;
  95. procedure SetOption(const Option: Char);
  96. procedure UnSetOption(const Option: Char);
  97. procedure Disconnect; override;
  98. procedure SendCommand(const aCommand: Char; const How: TLHowEnum); virtual;
  99. public
  100. property Output: TMemoryStream read FOutput;
  101. property Connected: Boolean read FConnected;
  102. property Timeout: DWord read GetTimeout write SetTimeout;
  103. property OnReceive: TLSocketEvent read FOnReceive write FOnReceive;
  104. property OnDisconnect: TLSocketEvent read FOnDisconnect write FOnDisconnect;
  105. property OnConnect: TLSocketEvent read FOnConnect write FOnConnect;
  106. property OnError: TLSocketErrorEvent read FOnError write FOnError;
  107. property Connection: TLTCP read FConnection;
  108. property SocketClass: TLSocketClass read GetSocketClass write SetSocketClass;
  109. end;
  110. { TLTelnetClient }
  111. { TLTelnetClient }
  112. TLTelnetClient = class(TLTelnet, ILClient)
  113. protected
  114. FLocalEcho: Boolean;
  115. procedure OnEr(const msg: string; aSocket: TLSocket);
  116. procedure OnDs(aSocket: TLSocket);
  117. procedure OnRe(aSocket: TLSocket);
  118. procedure OnCo(aSocket: TLSocket);
  119. procedure React(const Operation, Command: Char); override;
  120. procedure SendCommand(const Command: Char; const Value: Boolean); override;
  121. public
  122. constructor Create(aOwner: TComponent); override;
  123. function Connect(const anAddress: string; const aPort: Word): Boolean;
  124. function Connect: Boolean;
  125. function Get(var aData; const aSize: Integer; aSocket: TLSocket = nil): Integer; override;
  126. function GetMessage(out msg: string; aSocket: TLSocket = nil): Integer; override;
  127. function Send(const aData; const aSize: Integer; aSocket: TLSocket = nil): Integer; override;
  128. function SendMessage(const msg: string; aSocket: TLSocket = nil): Integer; override;
  129. procedure CallAction; override;
  130. public
  131. property LocalEcho: Boolean read FLocalEcho write FLocalEcho;
  132. end;
  133. implementation
  134. uses
  135. SysUtils;
  136. var
  137. zz: Char;
  138. TNames: array[Char] of string;
  139. //*******************************TLTelnetClient********************************
  140. constructor TLTelnet.Create(aOwner: TComponent);
  141. begin
  142. inherited Create(aOwner);
  143. FConnection := TLTCP.Create(aOwner);
  144. FOutput := TMemoryStream.Create;
  145. FCommandCharIndex := 0;
  146. FStack := TLControlStack.Create;
  147. FStack.OnFull := @StackFull;
  148. end;
  149. destructor TLTelnet.Destroy;
  150. begin
  151. Disconnect;
  152. FOutput.Free;
  153. FConnection.Free;
  154. FStack.Free;
  155. inherited Destroy;
  156. end;
  157. function TLTelnet.Question(const Command: Char; const Value: Boolean): Char;
  158. begin
  159. Result := TS_NOP;
  160. if Value then begin
  161. if Command in FOrders then
  162. Result := TS_DO
  163. else
  164. Result := TS_WILL;
  165. end else begin
  166. if Command in FOrders then
  167. Result := TS_DONT
  168. else
  169. Result := TS_WONT;
  170. end;
  171. end;
  172. function TLTelnet.GetSocketClass: TLSocketClass;
  173. begin
  174. Result := FConnection.SocketClass;
  175. end;
  176. function TLTelnet.GetTimeout: DWord;
  177. begin
  178. Result := FConnection.Timeout;
  179. end;
  180. procedure TLTelnet.SetSocketClass(Value: TLSocketClass);
  181. begin
  182. FConnection.SocketClass := Value;
  183. end;
  184. procedure TLTelnet.SetTimeout(const Value: DWord);
  185. begin
  186. FConnection.Timeout := Value;
  187. end;
  188. procedure TLTelnet.StackFull;
  189. begin
  190. {$ifdef debug}
  191. Writeln('**STACKFULL**');
  192. {$endif}
  193. if FStack[1] = TS_IAC then
  194. begin
  195. FOutput.WriteByte(Byte(FStack[1]));
  196. FOutput.WriteByte(Byte(FStack[2]));
  197. end else React(FStack[1], FStack[2]);
  198. FStack.Clear;
  199. end;
  200. procedure TLTelnet.DoubleIAC(var s: string);
  201. var
  202. i: Longint;
  203. begin
  204. i := 0;
  205. if Length(s) > 0 then
  206. while i < Length(s) do begin
  207. Inc(i);
  208. if s[i] = TS_IAC then begin
  209. Insert(TS_IAC, s, i);
  210. Inc(i, 2);
  211. end;
  212. end;
  213. end;
  214. procedure TLTelnet.TelnetParse(const msg: string);
  215. var
  216. i: Longint;
  217. begin
  218. for i := 1 to Length(msg) do
  219. if (FStack.ItemIndex > 0) or (msg[i] = TS_IAC) then begin
  220. if msg[i] = TS_GA then
  221. FStack.Clear
  222. else
  223. FStack.Push(msg[i])
  224. end else
  225. FOutput.WriteByte(Byte(msg[i]));
  226. end;
  227. function TLTelnet.OptionIsSet(const Option: Char): Boolean;
  228. begin
  229. Result := False;
  230. Result := Option in FActive;
  231. end;
  232. function TLTelnet.RegisterOption(const aOption: Char;
  233. const aCommand: Boolean): Boolean;
  234. begin
  235. Result := False;
  236. if not (aOption in FPossible) then begin
  237. FPossible := FPossible + [aOption];
  238. if aCommand then
  239. FOrders := FOrders + [aOption];
  240. Result := True;
  241. end;
  242. end;
  243. procedure TLTelnet.SetOption(const Option: Char);
  244. begin
  245. if Option in FPossible then
  246. SendCommand(Option, True);
  247. end;
  248. procedure TLTelnet.UnSetOption(const Option: Char);
  249. begin
  250. if Option in FPossible then
  251. SendCommand(Option, False);
  252. end;
  253. procedure TLTelnet.Disconnect;
  254. begin
  255. FConnection.Disconnect;
  256. FConnected := False;
  257. end;
  258. procedure TLTelnet.SendCommand(const aCommand: Char; const How: TLHowEnum);
  259. begin
  260. {$ifdef debug}
  261. Writeln('**SENT** ', TNames[Char(How)], ' ', TNames[aCommand]);
  262. {$endif}
  263. FConnection.SendMessage(TS_IAC + Char(How) + aCommand);
  264. end;
  265. //****************************TLTelnetClient*****************************
  266. constructor TLTelnetClient.Create(aOwner: TComponent);
  267. begin
  268. inherited Create(aOwner);
  269. FConnection.OnError := @OnEr;
  270. FConnection.OnDisconnect := @OnDs;
  271. FConnection.OnReceive := @OnRe;
  272. FConnection.OnConnect := @OnCo;
  273. FConnected := False;
  274. FPossible := [TS_ECHO, TS_HYI, TS_SGA];
  275. FActive := [];
  276. FOrders := [];
  277. end;
  278. procedure TLTelnetClient.OnEr(const msg: string; aSocket: TLSocket);
  279. begin
  280. if Assigned(FOnError) then
  281. FOnError(msg, aSocket)
  282. else
  283. FOutput.Write(Pointer(msg)^, Length(msg));
  284. end;
  285. procedure TLTelnetClient.OnDs(aSocket: TLSocket);
  286. begin
  287. if Assigned(FOnDisconnect) then
  288. FOnDisconnect(aSocket);
  289. end;
  290. procedure TLTelnetClient.OnRe(aSocket: TLSocket);
  291. var
  292. s: string;
  293. begin
  294. if aSocket.GetMessage(s) > 0 then begin
  295. TelnetParse(s);
  296. if Assigned(FOnReceive) then
  297. FOnReceive(aSocket);
  298. end;
  299. end;
  300. procedure TLTelnetClient.OnCo(aSocket: TLSocket);
  301. begin
  302. FConnected := True;
  303. if Assigned(FOnConnect) then
  304. FOnConnect(aSocket);
  305. end;
  306. procedure TLTelnetClient.React(const Operation, Command: Char);
  307. procedure Accept(const Operation, Command: Char);
  308. begin
  309. FActive := FActive + [Command];
  310. {$ifdef debug}
  311. Writeln('**SENT** ', TNames[Operation], ' ', TNames[Command]);
  312. {$endif}
  313. FConnection.SendMessage(TS_IAC + Operation + Command);
  314. end;
  315. procedure Refuse(const Operation, Command: Char);
  316. begin
  317. FActive := FActive - [Command];
  318. {$ifdef debug}
  319. Writeln('**SENT** ', TNames[Operation], ' ', TNames[Command]);
  320. {$endif}
  321. FConnection.SendMessage(TS_IAC + Operation + Command);
  322. end;
  323. begin
  324. {$ifdef debug}
  325. Writeln('**GOT** ', TNames[Operation], ' ', TNames[Command]);
  326. {$endif}
  327. case Operation of
  328. TS_DO : if Command in FPossible then Accept(TS_WILL, Command)
  329. else Refuse(TS_WONT, Command);
  330. TS_DONT : if Command in FPossible then Refuse(TS_WONT, Command);
  331. TS_WILL : if Command in FPossible then FActive := FActive + [Command]
  332. else Refuse(TS_DONT, Command);
  333. TS_WONT : if Command in FPossible then FActive := FActive - [Command];
  334. end;
  335. end;
  336. procedure TLTelnetClient.SendCommand(const Command: Char; const Value: Boolean);
  337. begin
  338. if FConnected then begin
  339. {$ifdef debug}
  340. Writeln('**SENT** ', TNames[Question(Command, Value)], ' ', TNames[Command]);
  341. {$endif}
  342. case Question(Command, Value) of
  343. TS_WILL : FActive := FActive + [Command];
  344. end;
  345. FConnection.SendMessage(TS_IAC + Question(Command, Value) + Command);
  346. end;
  347. end;
  348. function TLTelnetClient.Connect(const anAddress: string; const aPort: Word): Boolean;
  349. begin
  350. Result := FConnection.Connect(anAddress, aPort);
  351. end;
  352. function TLTelnetClient.Connect: Boolean;
  353. begin
  354. Result := FConnection.Connect(FHost, FPort);
  355. end;
  356. function TLTelnetClient.Get(var aData; const aSize: Integer; aSocket: TLSocket): Integer;
  357. begin
  358. Result := FOutput.Read(aData, aSize);
  359. if FOutput.Position = FOutput.Size then
  360. FOutput.Clear;
  361. end;
  362. function TLTelnetClient.GetMessage(out msg: string; aSocket: TLSocket): Integer;
  363. begin
  364. Result := 0;
  365. msg := '';
  366. if FOutput.Size > 0 then begin
  367. FOutput.Position := 0;
  368. SetLength(msg, FOutput.Size);
  369. Result := FOutput.Read(PChar(msg)^, Length(msg));
  370. FOutput.Clear;
  371. end;
  372. end;
  373. function TLTelnetClient.Send(const aData; const aSize: Integer;
  374. aSocket: TLSocket): Integer;
  375. var
  376. Tmp: string;
  377. begin
  378. {$ifdef debug}
  379. Writeln('**SEND START** ');
  380. {$endif}
  381. Result := 0;
  382. if aSize > 0 then begin
  383. SetLength(Tmp, aSize);
  384. Move(aData, PChar(Tmp)^, aSize);
  385. DoubleIAC(Tmp);
  386. if LocalEcho and (not OptionIsSet(TS_ECHO)) and (not OptionIsSet(TS_HYI)) then
  387. FOutput.Write(PChar(Tmp)^, Length(Tmp));
  388. Result := FConnection.SendMessage(Tmp);
  389. end;
  390. {$ifdef debug}
  391. Writeln('**SEND END** ');
  392. {$endif}
  393. end;
  394. function TLTelnetClient.SendMessage(const msg: string; aSocket: TLSocket
  395. ): Integer;
  396. begin
  397. Result := Send(PChar(msg)^, Length(msg));
  398. end;
  399. procedure TLTelnetClient.CallAction;
  400. begin
  401. FConnection.CallAction;
  402. end;
  403. initialization
  404. for zz := #0 to #255 do
  405. TNames[zz] := IntToStr(Ord(zz));
  406. TNames[#1] := 'TS_ECHO';
  407. TNames[#133] := 'TS_HYI';
  408. TNames[#251] := 'TS_WILL';
  409. TNames[#252] := 'TS_WONT';
  410. TNames[#253] := 'TS_DO';
  411. TNames[#254] := 'TS_DONT';
  412. end.