Qore TelnetClient Module Reference  1.2
TelnetClient.qm.dox.h
1 // -*- mode: c++; indent-tabs-mode: nil -*-
2 // @file TelnetClient.qm Telnet module definition
3 
4 /* TelnetClient.qm Copyright 2012 - 2015 Qore Technologies sro
5 
6  Original Author: Pavol Potancok
7 
8  Permission is hereby granted, free of charge, to any person obtaining a
9  copy of this software and associated documentation files (the "Software"),
10  to deal in the Software without restriction, including without limitation
11  the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  and/or sell copies of the Software, and to permit persons to whom the
13  Software is furnished to do so, subject to the following conditions:
14 
15  The above copyright notice and this permission notice shall be included in
16  all copies or substantial portions of the Software.
17 
18  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  DEALINGS IN THE SOFTWARE.
25 */
26 
27 // minimum required Qore version
28 
29 // assume local var scope, do not use "$" for vars, members, and method calls
30 
31 
32 /* Version History
33  * 1.2: David Nichols <david@qore.org>: converted to new-style
34  * 1.1: David Nichols <david@qore.org>: added socket instrumention support from Qore 0.8.9
35  * 1.0: David Nichols <david@qore.org>: updated to a user module
36  * 0.9: Pavol Potancok <ppotancok@gmail.com>: original author
37 */
38 
56 namespace TelnetClient {
61  // default telnet port
62  const DefaultTelnetPort = 23;
63 
64  // default connection timeout
65  const DefaultConnTimeout = 15s;
66 
67  // default poll timeout
68  const DefaultTimeout = 100ms;
69 
70  // default terminal type
71  const DefaultTerminalType = "xterm";
72 
73  // default terminal speed
74  const DefaultTerminalSpeed = 38400;
76 
80  const IAC = 255;
81  const DONT = 254;
82  const DO = 253;
83  const WONT = 252;
84  const WILL = 251;
85  const SE = 240;
86  const NOP = 241;
87  const DM = 242;
88  const BRK = 243;
89  const IP = 244;
90  const AO = 245;
91  const AYT = 246;
92  const EC = 247;
93  const EL = 248;
94  const GA = 249;
95  const SB = 250;
96 
98  const CmdMap = (
99  IAC : "IAC",
100  DONT : "DONT",
101  DO : "DO",
102  WONT : "WONT",
103  WILL: "WILL",
104  SE : "SubnegotiationEnd",
105  NOP : "NoOperation",
106  DM: "DataMark",
107  BRK : "Break",
108  IP : "Interruptprocess",
109  AO : "Abortoutput",
110  AYT : "AreYouThere",
111  EC : "EraseCharacter",
112  EL : "EraseLine",
113  GA : "GoAhead",
114  SB : "SubnegotiationBegin",
115  );
117 
118  const SUPDUP_OPT = 21; // SUPDUP_OPT display protocol
119 
123  const TOPT_BIN = 0;
124  const TOPT_ECHO = 1;
125  const TOPT_RECN = 2;
126  const TOPT_SUPP = 3;
127  const TOPT_APRX = 4;
128  const TOPT_STAT = 5;
129  const TOPT_TIM = 6;
130  const TOPT_REM = 7;
131  const TOPT_OLW = 8;
132  const TOPT_OPS = 9;
133  const TOPT_OCRD = 10;
134  const TOPT_OHT = 11;
135  const TOPT_OHTD = 12;
136  const TOPT_OFD = 13;
137  const TOPT_OVT = 14;
138  const TOPT_OVTD = 15;
139  const TOPT_OLD = 16;
140  const TOPT_EXT = 17;
141  const TOPT_LOGO = 18;
142  const TOPT_BYTE = 19;
143  const TOPT_DATA = 20;
144  const TOPT_SUP = 21;
145  const TOPT_SUPO = 22;
146  const TOPT_SNDL = 23;
147  const TOPT_TERM = 24;
148  const TOPT_EOR = 25;
149  const TOPT_TACACS = 26;
150  const TOPT_OM = 27;
151  const TOPT_TLN = 28;
152  const TOPT_3270 = 29;
153  const TOPT_X3 = 30;
154  const TOPT_NAWS = 31;
155  const TOPT_TS = 32;
156  const TOPT_RFC = 33;
157  const TOPT_LINE = 34;
158  const TOPT_XDL = 35;
159  const TOPT_ENVIR = 36;
160  const TOPT_AUTH = 37;
161  const TOPT_ENVIR_NEW = 39;
162  const TOPT_TN3270 = 40;
163  const TOPT_X_AUTH = 41;
164  const TOPT_CHARSET = 42;
165  const TOPT_RSP = 43;
166  const TOPT_COMPORT = 44;
167  const TOPT_SLE = 45;
168  const TOPT_STARTTLS = 46;
169  const TOPT_KERMIT = 47;
170  const TOPT_SEND_URL = 48;
171  const TOPT_EXTOP = 255;
172 
174  const OptMap = (
175  TOPT_BIN : "BinaryTransmission",
176  TOPT_ECHO : "Echo|Is|Send",
177  TOPT_RECN : "Reconnection",
178  TOPT_SUPP : "SuppressGoAhead",
179  TOPT_APRX : "ApproxMessageSizeNegotiation",
180  TOPT_STAT : "Status",
181  TOPT_TIM : "TimingMark",
182  TOPT_REM : "RemoteControlledTransandEcho",
183  TOPT_OLW : "OutputLineWidth",
184  TOPT_OPS : "OutputPageSize",
185  TOPT_OCRD : "OutputCarriage-ReturnDisposition",
186  TOPT_OHT : "OutputHorizontalTabstops",
187  TOPT_OHTD : "OutputHorizontalTabDisposition",
188  TOPT_OFD : "OutputFormfeedDisposition",
189  TOPT_OVT : "OutputVerticalTabstops",
190  TOPT_OVTD : "OutputVerticalTabDisposition",
191  TOPT_OLD : "OutputLinefeedDisposition",
192  TOPT_EXT : "ExtendedASCII",
193  TOPT_LOGO : "Logout",
194  TOPT_BYTE : "ByteMacro",
195  TOPT_DATA : "DataEntryTerminal",
196  TOPT_SUP : "SUPDUP",
197  TOPT_SUPO : "SUPDUPOutput",
198  TOPT_SNDL : "SendLocation",
199  TOPT_TERM : "TerminalType",
200  TOPT_EOR : "EndofRecord",
201  TOPT_TACACS : "TACACSUserIdent",
202  TOPT_OM : "OutputMarking",
203  TOPT_TLN : "TerminalLocationNumber",
204  TOPT_3270 : "Telnet3270Regime",
205  TOPT_X3 : "X.3PAD",
206  TOPT_NAWS : "NegotiateAboutWindowSize",
207  TOPT_TS : "TerminalSpeed",
208  TOPT_RFC : "RemoteFlowControl",
209  TOPT_LINE : "Linemode",
210  TOPT_XDL : "XDisplayLocation",
211  TOPT_ENVIR : "TelnetEnvironmentOption",
212  TOPT_AUTH : "TelnetAuthenticationOption",
213  TOPT_ENVIR_NEW : "TelnetNewEnvironmentOption",
214  TOPT_TN3270 : "TN3270Enhancements",
215  TOPT_X_AUTH : "TelnetXAUTH",
216  TOPT_CHARSET : "TelnetCHARSET",
217  TOPT_RSP : "TelnetRemoteSerialPort",
218  TOPT_COMPORT : "TelnetComPortControl",
219  TOPT_SLE : "TelnetSuppressLocalEcho",
220  TOPT_STARTTLS : "TelnetStartTLS",
221  TOPT_KERMIT : "TelnetKERMIT",
222  TOPT_SEND_URL : "Send-URL",
223  TOPT_EXTOP : "Extended-Options-List",
224  );
226 
228 
231  class TelnetClient {
232 
233 public:
235  private :
236  // the connection string
237  string connect;
238 
239  // the socket for all communication with the server
240  Socket conn();
241 
242  // to manage contention on the Socket
243  Mutex m();
244 
245  // login user
246  *string user;
247 
248  // logging closures
249  *code log_info;
250  *code log_debug;
251 
252  // hash of features the telnet server has asked for
253  hash fh;
254 
255 public:
257 
258  // no public members
259 private:
260 
261 public:
262 
264 
269  constructor(string host, softint port, *code log, *code dbgLog);
270 
271 
273 
277  constructor(string n_connect, *code n_log, *code n_dbglog);
278 
279 
281  destructor();
282 
283 
285  private log(string msg);
286 
287 
289  private logDbg(string msg);
290 
291 
293 
296  connect(timeout timeout = DefaultConnTimeout);
297 
298 
300  bool isConnected();
301 
302 
304 
308  setUser(*string n_user);
309 
310 
312  *string getUser();
313 
314 
316 
318  disconnect();
319 
320 
322 
334  sendData(softlist arr);
335 
336 
338 
346  sendTextData(string str);
347 
348 
350 
356  *string getAvailableData(timeout t = DefaultTimeout);
357 
358 
360 
368  bool hasFeature(int fc);
369 
370 
372 
374  windowSizeUpdated();
375 
376 
378 
380  private *string getDisplay();
381 
382 
384 
386  private *string getTerminalType();
387 
388 
390 
392  private hash getEnvironment();
393 
394 
396 
398  private int getTerminalSpeed();
399 
400 
402 
409  private hash getWindowSize();
410 
411 
413 
422  nothing clearWarningQueue();
423 
424 
426 
456  nothing setWarningQueue(int warning_ms, int warning_bs, Queue queue, any arg, timeout min_ms = 1s);
457 
458 
460 
478  hash getUsageInfo();
479 
480 
482 
491  clearStats();
492 
493 
495  // don't override this method, fix/enhance it with a patch to the module
496  private final disconnectIntern();
497 
498 
499  private checkConnIntern();
500 
501 
502  // don't override this method, fix/enhance it with a patch to the module
503  private final sendDataIntern(softlist arr, bool double_aic = False);
504 
505 
506  private sendTextDataIntern(string str);
507 
508 
509  // don't override this method, fix/enhance it with a patch to the module
510  private final *string getAvailableDataIntern(timeout t = DefaultTimeout);
511 
512 
513  // don't override this method, fix/enhance it with a patch to the module
514  private final updateWindowSizeIntern(hash wh);
515 
516 
517  // don't override this method, fix/enhance it with a patch to the module
518  private final processCmd(reference rv);
519 
520 
521  // don't override this method, fix/enhance it with a patch to the module
522  private final doSubNegotiation();
523 
525  };
526 };
const TOPT_OCRD
OutputCarriage-ReturnDisposition.
Definition: TelnetClient.qm.dox.h:133
const TOPT_OHTD
OutputHorizontalTabDisposition.
Definition: TelnetClient.qm.dox.h:135
const BRK
Break.
Definition: TelnetClient.qm.dox.h:88
const TOPT_SUPO
SUPDUPOutput.
Definition: TelnetClient.qm.dox.h:145
const GA
Go Ahead.
Definition: TelnetClient.qm.dox.h:94
const TOPT_EXTOP
Extended-Options-List.
Definition: TelnetClient.qm.dox.h:171
const TOPT_APRX
ApproxMessageSizeNegotiation.
Definition: TelnetClient.qm.dox.h:127
const TOPT_NAWS
NegotiateAboutWindowSize.
Definition: TelnetClient.qm.dox.h:154
const TOPT_OVTD
OutputVerticalTabDisposition.
Definition: TelnetClient.qm.dox.h:138
const TOPT_TS
TerminalSpeed.
Definition: TelnetClient.qm.dox.h:155
const TOPT_SUP
SUPDUP.
Definition: TelnetClient.qm.dox.h:144
contains constant definitions and the definition of the TelnetClient class
Definition: TelnetClient.qm.dox.h:57
const TOPT_STAT
Status.
Definition: TelnetClient.qm.dox.h:128
const TOPT_STARTTLS
TelnetStartTLS.
Definition: TelnetClient.qm.dox.h:168
const TOPT_SUPP
SuppressGoAhead.
Definition: TelnetClient.qm.dox.h:126
const TOPT_DATA
DataEntryTerminal.
Definition: TelnetClient.qm.dox.h:143
const TOPT_COMPORT
TelnetComPortControl.
Definition: TelnetClient.qm.dox.h:166
const WILL
WILL.
Definition: TelnetClient.qm.dox.h:84
const TOPT_X_AUTH
TelnetXAUTH.
Definition: TelnetClient.qm.dox.h:163
const EL
Erase Line.
Definition: TelnetClient.qm.dox.h:93
const False
const TOPT_OM
OutputMarking.
Definition: TelnetClient.qm.dox.h:150
const TOPT_CHARSET
TelnetCHARSET.
Definition: TelnetClient.qm.dox.h:164
const DO
DO.
Definition: TelnetClient.qm.dox.h:82
const TOPT_SNDL
SendLocation.
Definition: TelnetClient.qm.dox.h:146
const TOPT_OFD
OutputFormfeedDisposition.
Definition: TelnetClient.qm.dox.h:136
const TOPT_OPS
OutputPageSize.
Definition: TelnetClient.qm.dox.h:132
const SB
Subnegotiation Begin.
Definition: TelnetClient.qm.dox.h:95
const TOPT_REM
RemoteControlledTransandEcho.
Definition: TelnetClient.qm.dox.h:130
const TOPT_BIN
BinaryTransmission.
Definition: TelnetClient.qm.dox.h:123
const TOPT_LOGO
Logout.
Definition: TelnetClient.qm.dox.h:141
const EC
Erase Character.
Definition: TelnetClient.qm.dox.h:92
const TOPT_OLW
OutputLineWidth.
Definition: TelnetClient.qm.dox.h:131
const AYT
Are You There.
Definition: TelnetClient.qm.dox.h:91
const TOPT_SEND_URL
Send-URL.
Definition: TelnetClient.qm.dox.h:170
const TOPT_TERM
TerminalType.
Definition: TelnetClient.qm.dox.h:147
const TOPT_ECHO
Echo|Is|Send.
Definition: TelnetClient.qm.dox.h:124
const TOPT_TACACS
TACACSUserIdent.
Definition: TelnetClient.qm.dox.h:149
const DefaultTimeout
const TOPT_TLN
TerminalLocationNumber.
Definition: TelnetClient.qm.dox.h:151
const CmdMap
command name map
Definition: TelnetClient.qm.dox.h:98
const TOPT_XDL
XDisplayLocation.
Definition: TelnetClient.qm.dox.h:158
const TOPT_TN3270
TN3270Enhancements.
Definition: TelnetClient.qm.dox.h:162
const TOPT_LINE
Linemode.
Definition: TelnetClient.qm.dox.h:157
const TOPT_AUTH
TelnetAuthenticationOption.
Definition: TelnetClient.qm.dox.h:160
const IP
Interrupt process.
Definition: TelnetClient.qm.dox.h:89
const TOPT_EOR
EndofRecord.
Definition: TelnetClient.qm.dox.h:148
const TOPT_ENVIR
TelnetEnvironmentOption.
Definition: TelnetClient.qm.dox.h:159
const TOPT_BYTE
ByteMacro.
Definition: TelnetClient.qm.dox.h:142
const TOPT_ENVIR_NEW
TelnetNewEnvironmentOption.
Definition: TelnetClient.qm.dox.h:161
const TOPT_OLD
OutputLinefeedDisposition.
Definition: TelnetClient.qm.dox.h:139
const AO
Abort output.
Definition: TelnetClient.qm.dox.h:90
const TOPT_OVT
OutputVerticalTabstops.
Definition: TelnetClient.qm.dox.h:137
const NOP
No Operation.
Definition: TelnetClient.qm.dox.h:86
const TOPT_X3
X.3PAD.
Definition: TelnetClient.qm.dox.h:153
hash hash(object obj)
const TOPT_OHT
OutputHorizontalTabstops.
Definition: TelnetClient.qm.dox.h:134
const IAC
IAC.
Definition: TelnetClient.qm.dox.h:80
const TOPT_RSP
TelnetRemoteSerialPort.
Definition: TelnetClient.qm.dox.h:165
const TOPT_SLE
TelnetSuppressLocalEcho.
Definition: TelnetClient.qm.dox.h:167
const TOPT_RFC
RemoteFlowControl.
Definition: TelnetClient.qm.dox.h:156
const WONT
WONT.
Definition: TelnetClient.qm.dox.h:83
const DONT
DONT.
Definition: TelnetClient.qm.dox.h:81
const TOPT_RECN
Reconnection.
Definition: TelnetClient.qm.dox.h:125
const SE
Subnegotiation End.
Definition: TelnetClient.qm.dox.h:85
const TOPT_KERMIT
TelnetKERMIT.
Definition: TelnetClient.qm.dox.h:169
const DM
Data Mark.
Definition: TelnetClient.qm.dox.h:87
const OptMap
option map, maps codes to text descriptions
Definition: TelnetClient.qm.dox.h:174
const TOPT_EXT
ExtendedASCII.
Definition: TelnetClient.qm.dox.h:140
const TOPT_3270
Telnet3270Regime.
Definition: TelnetClient.qm.dox.h:152
const TOPT_TIM
TimingMark.
Definition: TelnetClient.qm.dox.h:129