ltelnet.pp 13 KB

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