Qore TelnetClient Module Reference  1.0
 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 
28 
29 /* Version History
30  * 1.1: David Nichols <david@qore.org>: added socket instrumention support from Qore 0.8.9
31  * 1.0: David Nichols <david@qore.org>: updated to a user module
32  * 0.9: Pavol Potancok <ppotancok@gmail.com>: original author
33 */
34 
52 namespace TelnetClient {
57  // default telnet port
58  const DefaultTelnetPort = 23;
59 
60  // default connection timeout
61  const DefaultConnTimeout = 15s;
62 
63  // default poll timeout
64  const DefaultTimeout = 100ms;
65 
66  // default terminal type
67  const DefaultTerminalType = "xterm";
68 
69  // default terminal speed
70  const DefaultTerminalSpeed = 38400;
72 
76  const IAC = 255;
77  const DONT = 254;
78  const DO = 253;
79  const WONT = 252;
80  const WILL = 251;
81  const SE = 240;
82  const NOP = 241;
83  const DM = 242;
84  const BRK = 243;
85  const IP = 244;
86  const AO = 245;
87  const AYT = 246;
88  const EC = 247;
89  const EL = 248;
90  const GA = 249;
91  const SB = 250;
92 
94  const CmdMap = (
95  IAC : "IAC",
96  DONT : "DONT",
97  DO : "DO",
98  WONT : "WONT",
99  WILL: "WILL",
100  SE : "SubnegotiationEnd",
101  NOP : "NoOperation",
102  DM: "DataMark",
103  BRK : "Break",
104  IP : "Interruptprocess",
105  AO : "Abortoutput",
106  AYT : "AreYouThere",
107  EC : "EraseCharacter",
108  EL : "EraseLine",
109  GA : "GoAhead",
110  SB : "SubnegotiationBegin",
111  );
113 
114  const SUPDUP_OPT = 21; // SUPDUP_OPT display protocol
115 
119  const TOPT_BIN = 0;
120  const TOPT_ECHO = 1;
121  const TOPT_RECN = 2;
122  const TOPT_SUPP = 3;
123  const TOPT_APRX = 4;
124  const TOPT_STAT = 5;
125  const TOPT_TIM = 6;
126  const TOPT_REM = 7;
127  const TOPT_OLW = 8;
128  const TOPT_OPS = 9;
129  const TOPT_OCRD = 10;
130  const TOPT_OHT = 11;
131  const TOPT_OHTD = 12;
132  const TOPT_OFD = 13;
133  const TOPT_OVT = 14;
134  const TOPT_OVTD = 15;
135  const TOPT_OLD = 16;
136  const TOPT_EXT = 17;
137  const TOPT_LOGO = 18;
138  const TOPT_BYTE = 19;
139  const TOPT_DATA = 20;
140  const TOPT_SUP = 21;
141  const TOPT_SUPO = 22;
142  const TOPT_SNDL = 23;
143  const TOPT_TERM = 24;
144  const TOPT_EOR = 25;
145  const TOPT_TACACS = 26;
146  const TOPT_OM = 27;
147  const TOPT_TLN = 28;
148  const TOPT_3270 = 29;
149  const TOPT_X3 = 30;
150  const TOPT_NAWS = 31;
151  const TOPT_TS = 32;
152  const TOPT_RFC = 33;
153  const TOPT_LINE = 34;
154  const TOPT_XDL = 35;
155  const TOPT_ENVIR = 36;
156  const TOPT_AUTH = 37;
157  const TOPT_ENVIR_NEW = 39;
158  const TOPT_TN3270 = 40;
159  const TOPT_X_AUTH = 41;
160  const TOPT_CHARSET = 42;
161  const TOPT_RSP = 43;
162  const TOPT_COMPORT = 44;
163  const TOPT_SLE = 45;
164  const TOPT_STARTTLS = 46;
165  const TOPT_KERMIT = 47;
166  const TOPT_SEND_URL = 48;
167  const TOPT_EXTOP = 255;
168 
170  const OptMap = (
171  TOPT_BIN : "BinaryTransmission",
172  TOPT_ECHO : "Echo|Is|Send",
173  TOPT_RECN : "Reconnection",
174  TOPT_SUPP : "SuppressGoAhead",
175  TOPT_APRX : "ApproxMessageSizeNegotiation",
176  TOPT_STAT : "Status",
177  TOPT_TIM : "TimingMark",
178  TOPT_REM : "RemoteControlledTransandEcho",
179  TOPT_OLW : "OutputLineWidth",
180  TOPT_OPS : "OutputPageSize",
181  TOPT_OCRD : "OutputCarriage-ReturnDisposition",
182  TOPT_OHT : "OutputHorizontalTabstops",
183  TOPT_OHTD : "OutputHorizontalTabDisposition",
184  TOPT_OFD : "OutputFormfeedDisposition",
185  TOPT_OVT : "OutputVerticalTabstops",
186  TOPT_OVTD : "OutputVerticalTabDisposition",
187  TOPT_OLD : "OutputLinefeedDisposition",
188  TOPT_EXT : "ExtendedASCII",
189  TOPT_LOGO : "Logout",
190  TOPT_BYTE : "ByteMacro",
191  TOPT_DATA : "DataEntryTerminal",
192  TOPT_SUP : "SUPDUP",
193  TOPT_SUPO : "SUPDUPOutput",
194  TOPT_SNDL : "SendLocation",
195  TOPT_TERM : "TerminalType",
196  TOPT_EOR : "EndofRecord",
197  TOPT_TACACS : "TACACSUserIdent",
198  TOPT_OM : "OutputMarking",
199  TOPT_TLN : "TerminalLocationNumber",
200  TOPT_3270 : "Telnet3270Regime",
201  TOPT_X3 : "X.3PAD",
202  TOPT_NAWS : "NegotiateAboutWindowSize",
203  TOPT_TS : "TerminalSpeed",
204  TOPT_RFC : "RemoteFlowControl",
205  TOPT_LINE : "Linemode",
206  TOPT_XDL : "XDisplayLocation",
207  TOPT_ENVIR : "TelnetEnvironmentOption",
208  TOPT_AUTH : "TelnetAuthenticationOption",
209  TOPT_ENVIR_NEW : "TelnetNewEnvironmentOption",
210  TOPT_TN3270 : "TN3270Enhancements",
211  TOPT_X_AUTH : "TelnetXAUTH",
212  TOPT_CHARSET : "TelnetCHARSET",
213  TOPT_RSP : "TelnetRemoteSerialPort",
214  TOPT_COMPORT : "TelnetComPortControl",
215  TOPT_SLE : "TelnetSuppressLocalEcho",
216  TOPT_STARTTLS : "TelnetStartTLS",
217  TOPT_KERMIT : "TelnetKERMIT",
218  TOPT_SEND_URL : "Send-URL",
219  TOPT_EXTOP : "Extended-Options-List",
220  );
222 
224 
227  class TelnetClient {
228 
229 public:
231  private :
232  // the connection string
233  string connect;
234 
235  // the socket for all communication with the server
236  Socket conn();
237 
238  // to manage contention on the Socket
239  Mutex m();
240 
241  // login user
242  *string user;
243 
244  // logging closures
245  *code log_info;
246  *code log_debug;
247 
248  // hash of features the telnet server has asked for
249  hash fh;
250 
251 public:
253 
254  // no public members
255 private:
256 
257 public:
258 
260 
265  constructor(string host, softint port, *code log, *code dbgLog);
266 
267 
269 
273  constructor(string connect, *code log, *code dbglog);
274 
275 
277  destructor();
278 
279 
281  private log(string msg);
282 
283 
285  private logDbg(string msg);
286 
287 
289 
292  connect(timeout timeout = DefaultConnTimeout);
293 
294 
296  bool isConnected();
297 
298 
300 
302  setUser(*string user);
303 
304 
306  *string getUser();
307 
308 
310 
312  disconnect();
313 
314 
316 
328  sendData(softlist arr);
329 
330 
332 
340  sendTextData(string str);
341 
342 
344 
350  *string getAvailableData(timeout t = DefaultTimeout);
351 
352 
354 
362  bool hasFeature(int fc);
363 
364 
366 
369 
370 
372 
374  private *string getDisplay();
375 
376 
378 
380  private *string getTerminalType();
381 
382 
384 
386  private hash getEnvironment();
387 
388 
390 
392  private int getTerminalSpeed();
393 
394 
396 
403  private hash getWindowSize();
404 
405 
407 
416  nothing clearWarningQueue();
417 
418 
420 
450  nothing setWarningQueue(int warning_ms, int warning_bs, Queue queue, any arg, timeout min_ms = 1s);
451 
452 
454 
472  hash getUsageInfo();
473 
474 
476 
485  clearStats();
486 
487 
489  // don't override this method, fix/enhance it with a patch to the module
490  private final disconnectIntern();
491 
492 
493  private checkConnIntern();
494 
495 
496  // don't override this method, fix/enhance it with a patch to the module
497  private final sendDataIntern(softlist arr, bool double_aic = False);
498 
499 
500  private sendTextDataIntern(string str);
501 
502 
503  // don't override this method, fix/enhance it with a patch to the module
504  private final *string getAvailableDataIntern(timeout t = DefaultTimeout);
505 
506 
507  // don't override this method, fix/enhance it with a patch to the module
508  private final updateWindowSizeIntern(hash wh);
509 
510 
511  // don't override this method, fix/enhance it with a patch to the module
512  private final processCmd(reference rv);
513 
514 
515  // don't override this method, fix/enhance it with a patch to the module
516  private final doSubNegotiation();
517 
519  };
520 };
const TOPT_OCRD
OutputCarriage-ReturnDisposition.
Definition: TelnetClient.qm.dox.h:129
const TOPT_OHTD
OutputHorizontalTabDisposition.
Definition: TelnetClient.qm.dox.h:131
clearStats()
Clears performance statistics.
const BRK
Break.
Definition: TelnetClient.qm.dox.h:84
const TOPT_SUPO
SUPDUPOutput.
Definition: TelnetClient.qm.dox.h:141
const GA
Go Ahead.
Definition: TelnetClient.qm.dox.h:90
bool isConnected()
returns True if the client is connected to the server
const TOPT_EXTOP
Extended-Options-List.
Definition: TelnetClient.qm.dox.h:167
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:123
*string getUser()
returns the current value of the username parameter
const TOPT_NAWS
NegotiateAboutWindowSize.
Definition: TelnetClient.qm.dox.h:150
const TOPT_OVTD
OutputVerticalTabDisposition.
Definition: TelnetClient.qm.dox.h:134
const TOPT_TS
TerminalSpeed.
Definition: TelnetClient.qm.dox.h:151
const TOPT_SUP
SUPDUP.
Definition: TelnetClient.qm.dox.h:140
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:124
constructor(string host, softint port, *code log, *code dbgLog)
creates the TelnetClient object
const TOPT_STARTTLS
TelnetStartTLS.
Definition: TelnetClient.qm.dox.h:164
const TOPT_SUPP
SuppressGoAhead.
Definition: TelnetClient.qm.dox.h:122
const TOPT_DATA
DataEntryTerminal.
Definition: TelnetClient.qm.dox.h:139
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:162
const WILL
WILL.
Definition: TelnetClient.qm.dox.h:80
const TOPT_X_AUTH
TelnetXAUTH.
Definition: TelnetClient.qm.dox.h:159
nothing clearWarningQueue()
Removes any warning Queue object from the Socket.
const EL
Erase Line.
Definition: TelnetClient.qm.dox.h:89
connect(timeout timeout=DefaultConnTimeout)
connects to the Telnet server
const False
const TOPT_OM
OutputMarking.
Definition: TelnetClient.qm.dox.h:146
const TOPT_CHARSET
TelnetCHARSET.
Definition: TelnetClient.qm.dox.h:160
windowSizeUpdated()
this method should be called externally when the window size has changed
const DO
DO.
Definition: TelnetClient.qm.dox.h:78
const TOPT_SNDL
SendLocation.
Definition: TelnetClient.qm.dox.h:142
The TelnetClient class allows communication with a telnet server; it can be used directly or subclass...
Definition: TelnetClient.qm.dox.h:227
const TOPT_OFD
OutputFormfeedDisposition.
Definition: TelnetClient.qm.dox.h:132
const TOPT_OPS
OutputPageSize.
Definition: TelnetClient.qm.dox.h:128
const SB
Subnegotiation Begin.
Definition: TelnetClient.qm.dox.h:91
const TOPT_REM
RemoteControlledTransandEcho.
Definition: TelnetClient.qm.dox.h:126
const TOPT_BIN
BinaryTransmission.
Definition: TelnetClient.qm.dox.h:119
const TOPT_LOGO
Logout.
Definition: TelnetClient.qm.dox.h:137
const EC
Erase Character.
Definition: TelnetClient.qm.dox.h:88
const TOPT_OLW
OutputLineWidth.
Definition: TelnetClient.qm.dox.h:127
hash getUsageInfo()
Returns performance statistics for the socket.
const AYT
Are You There.
Definition: TelnetClient.qm.dox.h:87
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:166
const TOPT_TERM
TerminalType.
Definition: TelnetClient.qm.dox.h:143
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:120
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:145
const TOPT_TLN
TerminalLocationNumber.
Definition: TelnetClient.qm.dox.h:147
destructor()
disconnects from the server if connected and deletes the object
const CmdMap
command name map
Definition: TelnetClient.qm.dox.h:94
const TOPT_XDL
XDisplayLocation.
Definition: TelnetClient.qm.dox.h:154
const TOPT_TN3270
TN3270Enhancements.
Definition: TelnetClient.qm.dox.h:158
disconnect()
disconnects from the Telnet server
const TOPT_LINE
Linemode.
Definition: TelnetClient.qm.dox.h:153
const TOPT_AUTH
TelnetAuthenticationOption.
Definition: TelnetClient.qm.dox.h:156
const IP
Interrupt process.
Definition: TelnetClient.qm.dox.h:85
const TOPT_EOR
EndofRecord.
Definition: TelnetClient.qm.dox.h:144
const TOPT_ENVIR
TelnetEnvironmentOption.
Definition: TelnetClient.qm.dox.h:155
const TOPT_BYTE
ByteMacro.
Definition: TelnetClient.qm.dox.h:138
const TOPT_ENVIR_NEW
TelnetNewEnvironmentOption.
Definition: TelnetClient.qm.dox.h:157
const TOPT_OLD
OutputLinefeedDisposition.
Definition: TelnetClient.qm.dox.h:135
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:86
const TOPT_OVT
OutputVerticalTabstops.
Definition: TelnetClient.qm.dox.h:133
const NOP
No Operation.
Definition: TelnetClient.qm.dox.h:82
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:149
hash hash(object obj)
const TOPT_OHT
OutputHorizontalTabstops.
Definition: TelnetClient.qm.dox.h:130
const IAC
IAC.
Definition: TelnetClient.qm.dox.h:76
const TOPT_RSP
TelnetRemoteSerialPort.
Definition: TelnetClient.qm.dox.h:161
const TOPT_SLE
TelnetSuppressLocalEcho.
Definition: TelnetClient.qm.dox.h:163
const TOPT_RFC
RemoteFlowControl.
Definition: TelnetClient.qm.dox.h:152
const WONT
WONT.
Definition: TelnetClient.qm.dox.h:79
const DONT
DONT.
Definition: TelnetClient.qm.dox.h:77
const TOPT_RECN
Reconnection.
Definition: TelnetClient.qm.dox.h:121
const SE
Subnegotiation End.
Definition: TelnetClient.qm.dox.h:81
const TOPT_KERMIT
TelnetKERMIT.
Definition: TelnetClient.qm.dox.h:165
const DM
Data Mark.
Definition: TelnetClient.qm.dox.h:83
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:170
const TOPT_EXT
ExtendedASCII.
Definition: TelnetClient.qm.dox.h:136
const TOPT_3270
Telnet3270Regime.
Definition: TelnetClient.qm.dox.h:148
*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:125