Qore TelnetClient Module Reference  1.1
 All Classes Namespaces Functions Variables Groups Pages
TelnetClient.qm.dox.h
1 // -*- mode: c++; indent-tabs-mode: nil -*-
2 // @file TelnetClient.qm Telnet module definition
3 
4 /* TelnetClient.qm Copyright 2012 - 2014 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 
30 /* Version History
31  * 1.1: David Nichols <david@qore.org>: added socket instrumention support from Qore 0.8.9
32  * 1.0: David Nichols <david@qore.org>: updated to a user module
33  * 0.9: Pavol Potancok <ppotancok@gmail.com>: original author
34 */
35 
53 namespace TelnetClient {
58  // default telnet port
59  const DefaultTelnetPort = 23;
60 
61  // default connection timeout
62  const DefaultConnTimeout = 15s;
63 
64  // default poll timeout
65  const DefaultTimeout = 100ms;
66 
67  // default terminal type
68  const DefaultTerminalType = "xterm";
69 
70  // default terminal speed
71  const DefaultTerminalSpeed = 38400;
73 
77  const IAC = 255;
78  const DONT = 254;
79  const DO = 253;
80  const WONT = 252;
81  const WILL = 251;
82  const SE = 240;
83  const NOP = 241;
84  const DM = 242;
85  const BRK = 243;
86  const IP = 244;
87  const AO = 245;
88  const AYT = 246;
89  const EC = 247;
90  const EL = 248;
91  const GA = 249;
92  const SB = 250;
93 
95  const CmdMap = (
96  IAC : "IAC",
97  DONT : "DONT",
98  DO : "DO",
99  WONT : "WONT",
100  WILL: "WILL",
101  SE : "SubnegotiationEnd",
102  NOP : "NoOperation",
103  DM: "DataMark",
104  BRK : "Break",
105  IP : "Interruptprocess",
106  AO : "Abortoutput",
107  AYT : "AreYouThere",
108  EC : "EraseCharacter",
109  EL : "EraseLine",
110  GA : "GoAhead",
111  SB : "SubnegotiationBegin",
112  );
114 
115  const SUPDUP_OPT = 21; // SUPDUP_OPT display protocol
116 
120  const TOPT_BIN = 0;
121  const TOPT_ECHO = 1;
122  const TOPT_RECN = 2;
123  const TOPT_SUPP = 3;
124  const TOPT_APRX = 4;
125  const TOPT_STAT = 5;
126  const TOPT_TIM = 6;
127  const TOPT_REM = 7;
128  const TOPT_OLW = 8;
129  const TOPT_OPS = 9;
130  const TOPT_OCRD = 10;
131  const TOPT_OHT = 11;
132  const TOPT_OHTD = 12;
133  const TOPT_OFD = 13;
134  const TOPT_OVT = 14;
135  const TOPT_OVTD = 15;
136  const TOPT_OLD = 16;
137  const TOPT_EXT = 17;
138  const TOPT_LOGO = 18;
139  const TOPT_BYTE = 19;
140  const TOPT_DATA = 20;
141  const TOPT_SUP = 21;
142  const TOPT_SUPO = 22;
143  const TOPT_SNDL = 23;
144  const TOPT_TERM = 24;
145  const TOPT_EOR = 25;
146  const TOPT_TACACS = 26;
147  const TOPT_OM = 27;
148  const TOPT_TLN = 28;
149  const TOPT_3270 = 29;
150  const TOPT_X3 = 30;
151  const TOPT_NAWS = 31;
152  const TOPT_TS = 32;
153  const TOPT_RFC = 33;
154  const TOPT_LINE = 34;
155  const TOPT_XDL = 35;
156  const TOPT_ENVIR = 36;
157  const TOPT_AUTH = 37;
158  const TOPT_ENVIR_NEW = 39;
159  const TOPT_TN3270 = 40;
160  const TOPT_X_AUTH = 41;
161  const TOPT_CHARSET = 42;
162  const TOPT_RSP = 43;
163  const TOPT_COMPORT = 44;
164  const TOPT_SLE = 45;
165  const TOPT_STARTTLS = 46;
166  const TOPT_KERMIT = 47;
167  const TOPT_SEND_URL = 48;
168  const TOPT_EXTOP = 255;
169 
171  const OptMap = (
172  TOPT_BIN : "BinaryTransmission",
173  TOPT_ECHO : "Echo|Is|Send",
174  TOPT_RECN : "Reconnection",
175  TOPT_SUPP : "SuppressGoAhead",
176  TOPT_APRX : "ApproxMessageSizeNegotiation",
177  TOPT_STAT : "Status",
178  TOPT_TIM : "TimingMark",
179  TOPT_REM : "RemoteControlledTransandEcho",
180  TOPT_OLW : "OutputLineWidth",
181  TOPT_OPS : "OutputPageSize",
182  TOPT_OCRD : "OutputCarriage-ReturnDisposition",
183  TOPT_OHT : "OutputHorizontalTabstops",
184  TOPT_OHTD : "OutputHorizontalTabDisposition",
185  TOPT_OFD : "OutputFormfeedDisposition",
186  TOPT_OVT : "OutputVerticalTabstops",
187  TOPT_OVTD : "OutputVerticalTabDisposition",
188  TOPT_OLD : "OutputLinefeedDisposition",
189  TOPT_EXT : "ExtendedASCII",
190  TOPT_LOGO : "Logout",
191  TOPT_BYTE : "ByteMacro",
192  TOPT_DATA : "DataEntryTerminal",
193  TOPT_SUP : "SUPDUP",
194  TOPT_SUPO : "SUPDUPOutput",
195  TOPT_SNDL : "SendLocation",
196  TOPT_TERM : "TerminalType",
197  TOPT_EOR : "EndofRecord",
198  TOPT_TACACS : "TACACSUserIdent",
199  TOPT_OM : "OutputMarking",
200  TOPT_TLN : "TerminalLocationNumber",
201  TOPT_3270 : "Telnet3270Regime",
202  TOPT_X3 : "X.3PAD",
203  TOPT_NAWS : "NegotiateAboutWindowSize",
204  TOPT_TS : "TerminalSpeed",
205  TOPT_RFC : "RemoteFlowControl",
206  TOPT_LINE : "Linemode",
207  TOPT_XDL : "XDisplayLocation",
208  TOPT_ENVIR : "TelnetEnvironmentOption",
209  TOPT_AUTH : "TelnetAuthenticationOption",
210  TOPT_ENVIR_NEW : "TelnetNewEnvironmentOption",
211  TOPT_TN3270 : "TN3270Enhancements",
212  TOPT_X_AUTH : "TelnetXAUTH",
213  TOPT_CHARSET : "TelnetCHARSET",
214  TOPT_RSP : "TelnetRemoteSerialPort",
215  TOPT_COMPORT : "TelnetComPortControl",
216  TOPT_SLE : "TelnetSuppressLocalEcho",
217  TOPT_STARTTLS : "TelnetStartTLS",
218  TOPT_KERMIT : "TelnetKERMIT",
219  TOPT_SEND_URL : "Send-URL",
220  TOPT_EXTOP : "Extended-Options-List",
221  );
223 
225 
228  class TelnetClient {
229 
230 public:
232  private :
233  // the connection string
234  string connect;
235 
236  // the socket for all communication with the server
237  Socket conn();
238 
239  // to manage contention on the Socket
240  Mutex m();
241 
242  // login user
243  *string user;
244 
245  // logging closures
246  *code log_info;
247  *code log_debug;
248 
249  // hash of features the telnet server has asked for
250  hash fh;
251 
252 public:
254 
255  // no public members
256 private:
257 
258 public:
259 
261 
266  constructor(string host, softint port, *code log, *code dbgLog);
267 
268 
270 
274  constructor(string connect, *code log, *code dbglog);
275 
276 
278  destructor();
279 
280 
282  private log(string msg);
283 
284 
286  private logDbg(string msg);
287 
288 
290 
293  connect(timeout timeout = DefaultConnTimeout);
294 
295 
297  bool isConnected();
298 
299 
301 
303  setUser(*string user);
304 
305 
307  *string getUser();
308 
309 
311 
313  disconnect();
314 
315 
317 
329  sendData(softlist arr);
330 
331 
333 
341  sendTextData(string str);
342 
343 
345 
351  *string getAvailableData(timeout t = DefaultTimeout);
352 
353 
355 
363  bool hasFeature(int fc);
364 
365 
367 
370 
371 
373 
375  private *string getDisplay();
376 
377 
379 
381  private *string getTerminalType();
382 
383 
385 
387  private hash getEnvironment();
388 
389 
391 
393  private int getTerminalSpeed();
394 
395 
397 
404  private hash getWindowSize();
405 
406 
408 
417  nothing clearWarningQueue();
418 
419 
421 
451  nothing setWarningQueue(int warning_ms, int warning_bs, Queue queue, any arg, timeout min_ms = 1s);
452 
453 
455 
473  hash getUsageInfo();
474 
475 
477 
486  clearStats();
487 
488 
490  // don't override this method, fix/enhance it with a patch to the module
491  private final disconnectIntern();
492 
493 
494  private checkConnIntern();
495 
496 
497  // don't override this method, fix/enhance it with a patch to the module
498  private final sendDataIntern(softlist arr, bool double_aic = False);
499 
500 
501  private sendTextDataIntern(string str);
502 
503 
504  // don't override this method, fix/enhance it with a patch to the module
505  private final *string getAvailableDataIntern(timeout t = DefaultTimeout);
506 
507 
508  // don't override this method, fix/enhance it with a patch to the module
509  private final updateWindowSizeIntern(hash wh);
510 
511 
512  // don't override this method, fix/enhance it with a patch to the module
513  private final processCmd(reference rv);
514 
515 
516  // don't override this method, fix/enhance it with a patch to the module
517  private final doSubNegotiation();
518 
520  };
521 };
const TOPT_OCRD
OutputCarriage-ReturnDisposition.
Definition: TelnetClient.qm.dox.h:130
const TOPT_OHTD
OutputHorizontalTabDisposition.
Definition: TelnetClient.qm.dox.h:132
clearStats()
Clears performance statistics.
const BRK
Break.
Definition: TelnetClient.qm.dox.h:85
const TOPT_SUPO
SUPDUPOutput.
Definition: TelnetClient.qm.dox.h:142
const GA
Go Ahead.
Definition: TelnetClient.qm.dox.h:91
bool isConnected()
returns True if the client is connected to the server
const TOPT_EXTOP
Extended-Options-List.
Definition: TelnetClient.qm.dox.h:168
setUser(*string user)
sets or clears (in case passed with no value) the username parameter for logging in to the telnet ser...
const TOPT_APRX
ApproxMessageSizeNegotiation.
Definition: TelnetClient.qm.dox.h:124
*string getUser()
returns the current value of the username parameter
const TOPT_NAWS
NegotiateAboutWindowSize.
Definition: TelnetClient.qm.dox.h:151
const TOPT_OVTD
OutputVerticalTabDisposition.
Definition: TelnetClient.qm.dox.h:135
const TOPT_TS
TerminalSpeed.
Definition: TelnetClient.qm.dox.h:152
const TOPT_SUP
SUPDUP.
Definition: TelnetClient.qm.dox.h:141
private int getTerminalSpeed()
this method provides the value that will be given as the terminal speed in protocol negotiation ...
const TOPT_STAT
Status.
Definition: TelnetClient.qm.dox.h:125
constructor(string host, softint port, *code log, *code dbgLog)
creates the TelnetClient object
const TOPT_STARTTLS
TelnetStartTLS.
Definition: TelnetClient.qm.dox.h:165
const TOPT_SUPP
SuppressGoAhead.
Definition: TelnetClient.qm.dox.h:123
const TOPT_DATA
DataEntryTerminal.
Definition: TelnetClient.qm.dox.h:140
bool hasFeature(int fc)
returns True if the server has confirmed with a DO command that it supports the given feature ...
const TOPT_COMPORT
TelnetComPortControl.
Definition: TelnetClient.qm.dox.h:163
const WILL
WILL.
Definition: TelnetClient.qm.dox.h:81
const TOPT_X_AUTH
TelnetXAUTH.
Definition: TelnetClient.qm.dox.h:160
nothing clearWarningQueue()
Removes any warning Queue object from the Socket.
const EL
Erase Line.
Definition: TelnetClient.qm.dox.h:90
connect(timeout timeout=DefaultConnTimeout)
connects to the Telnet server
const False
const TOPT_OM
OutputMarking.
Definition: TelnetClient.qm.dox.h:147
const TOPT_CHARSET
TelnetCHARSET.
Definition: TelnetClient.qm.dox.h:161
windowSizeUpdated()
this method should be called externally when the window size has changed
const DO
DO.
Definition: TelnetClient.qm.dox.h:79
const TOPT_SNDL
SendLocation.
Definition: TelnetClient.qm.dox.h:143
The TelnetClient class allows communication with a telnet server; it can be used directly or subclass...
Definition: TelnetClient.qm.dox.h:228
const TOPT_OFD
OutputFormfeedDisposition.
Definition: TelnetClient.qm.dox.h:133
const TOPT_OPS
OutputPageSize.
Definition: TelnetClient.qm.dox.h:129
const SB
Subnegotiation Begin.
Definition: TelnetClient.qm.dox.h:92
const TOPT_REM
RemoteControlledTransandEcho.
Definition: TelnetClient.qm.dox.h:127
const TOPT_BIN
BinaryTransmission.
Definition: TelnetClient.qm.dox.h:120
const TOPT_LOGO
Logout.
Definition: TelnetClient.qm.dox.h:138
const EC
Erase Character.
Definition: TelnetClient.qm.dox.h:89
const TOPT_OLW
OutputLineWidth.
Definition: TelnetClient.qm.dox.h:128
hash getUsageInfo()
Returns performance statistics for the socket.
const AYT
Are You There.
Definition: TelnetClient.qm.dox.h:88
private hash getEnvironment()
this method provides the value that will be given as the user&#39;s environment in protocol negotiation ...
const TOPT_SEND_URL
Send-URL.
Definition: TelnetClient.qm.dox.h:167
const TOPT_TERM
TerminalType.
Definition: TelnetClient.qm.dox.h:144
sendData(softlist arr)
sends data to the server
private log(string msg)
logs the message to the log closure set with the constructor (if any)
private *string getTerminalType()
this method provides the value that will be given as the terminal type in protocol negotiation ...
const TOPT_ECHO
Echo|Is|Send.
Definition: TelnetClient.qm.dox.h:121
sendTextData(string str)
sends literal string data to the server; the text dat is converted to the socket&#39;s encoding if necess...
private *string getDisplay()
this method provides the value that will be given as the DISPLAY value in protocol negotiation ...
const TOPT_TACACS
TACACSUserIdent.
Definition: TelnetClient.qm.dox.h:146
const TOPT_TLN
TerminalLocationNumber.
Definition: TelnetClient.qm.dox.h:148
destructor()
disconnects from the server if connected and deletes the object
const CmdMap
command name map
Definition: TelnetClient.qm.dox.h:95
const TOPT_XDL
XDisplayLocation.
Definition: TelnetClient.qm.dox.h:155
const TOPT_TN3270
TN3270Enhancements.
Definition: TelnetClient.qm.dox.h:159
disconnect()
disconnects from the Telnet server
const TOPT_LINE
Linemode.
Definition: TelnetClient.qm.dox.h:154
const TOPT_AUTH
TelnetAuthenticationOption.
Definition: TelnetClient.qm.dox.h:157
const IP
Interrupt process.
Definition: TelnetClient.qm.dox.h:86
const TOPT_EOR
EndofRecord.
Definition: TelnetClient.qm.dox.h:145
const TOPT_ENVIR
TelnetEnvironmentOption.
Definition: TelnetClient.qm.dox.h:156
const TOPT_BYTE
ByteMacro.
Definition: TelnetClient.qm.dox.h:139
const TOPT_ENVIR_NEW
TelnetNewEnvironmentOption.
Definition: TelnetClient.qm.dox.h:158
const TOPT_OLD
OutputLinefeedDisposition.
Definition: TelnetClient.qm.dox.h:136
nothing setWarningQueue(int warning_ms, int warning_bs, Queue queue, any arg, timeout min_ms=1s)
Sets a Queue object to receive socket warnings.
const AO
Abort output.
Definition: TelnetClient.qm.dox.h:87
const TOPT_OVT
OutputVerticalTabstops.
Definition: TelnetClient.qm.dox.h:134
const NOP
No Operation.
Definition: TelnetClient.qm.dox.h:83
private logDbg(string msg)
logs the message to the debug log closure set with the constructor (if any)
const TOPT_X3
X.3PAD.
Definition: TelnetClient.qm.dox.h:150
hash hash(object obj)
const TOPT_OHT
OutputHorizontalTabstops.
Definition: TelnetClient.qm.dox.h:131
const IAC
IAC.
Definition: TelnetClient.qm.dox.h:77
const TOPT_RSP
TelnetRemoteSerialPort.
Definition: TelnetClient.qm.dox.h:162
const TOPT_SLE
TelnetSuppressLocalEcho.
Definition: TelnetClient.qm.dox.h:164
const TOPT_RFC
RemoteFlowControl.
Definition: TelnetClient.qm.dox.h:153
const WONT
WONT.
Definition: TelnetClient.qm.dox.h:80
const DONT
DONT.
Definition: TelnetClient.qm.dox.h:78
const TOPT_RECN
Reconnection.
Definition: TelnetClient.qm.dox.h:122
const SE
Subnegotiation End.
Definition: TelnetClient.qm.dox.h:82
const TOPT_KERMIT
TelnetKERMIT.
Definition: TelnetClient.qm.dox.h:166
const DM
Data Mark.
Definition: TelnetClient.qm.dox.h:84
private hash getWindowSize()
this method provides the value that will be given as the terminal window size in protocol negotiation...
const OptMap
option map, maps codes to text descriptions
Definition: TelnetClient.qm.dox.h:171
const TOPT_EXT
ExtendedASCII.
Definition: TelnetClient.qm.dox.h:137
const TOPT_3270
Telnet3270Regime.
Definition: TelnetClient.qm.dox.h:149
*string getAvailableData(timeout t=DefaultTimeout)
returns all data available as a string (which could be an empty string if no data is available in the...
const TOPT_TIM
TimingMark.
Definition: TelnetClient.qm.dox.h:126