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 Pavol Potancok, David Nichols
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23 */
24 
25 
26 /* Version History
27  * 1.0: updated by David Nichols <david@qore.org> to a user module
28  * 0.9: original author Pavol Potancok <ppotancok@gmail.com>
29 */
30 
46 
47 namespace TelnetClient {
51  // default telnet port
52  const DefaultTelnetPort = 23;
53 
54  // default connection timeout
55  const DefaultConnTimeout = 15s;
56 
57  // default poll timeout
58  const DefaultTimeout = 100ms;
59 
60  // default terminal type
61  const DefaultTerminalType = "xterm";
62 
63  // default terminal speed
64  const DefaultTerminalSpeed = 38400;
66 
70  const IAC = 255;
71  const DONT = 254;
72  const DO = 253;
73  const WONT = 252;
74  const WILL = 251;
75  const SE = 240;
76  const NOP = 241;
77  const DM = 242;
78  const BRK = 243;
79  const IP = 244;
80  const AO = 245;
81  const AYT = 246;
82  const EC = 247;
83  const EL = 248;
84  const GA = 249;
85  const SB = 250;
86 
88  const CmdMap = (
89  IAC : "IAC",
90  DONT : "DONT",
91  DO : "DO",
92  WONT : "WONT",
93  WILL: "WILL",
94  SE : "SubnegotiationEnd",
95  NOP : "NoOperation",
96  DM: "DataMark",
97  BRK : "Break",
98  IP : "Interruptprocess",
99  AO : "Abortoutput",
100  AYT : "AreYouThere",
101  EC : "EraseCharacter",
102  EL : "EraseLine",
103  GA : "GoAhead",
104  SB : "SubnegotiationBegin",
105  );
107 
108  const SUPDUP_OPT = 21; // SUPDUP_OPT display protocol
109 
113  const TOPT_BIN = 0;
114  const TOPT_ECHO = 1;
115  const TOPT_RECN = 2;
116  const TOPT_SUPP = 3;
117  const TOPT_APRX = 4;
118  const TOPT_STAT = 5;
119  const TOPT_TIM = 6;
120  const TOPT_REM = 7;
121  const TOPT_OLW = 8;
122  const TOPT_OPS = 9;
123  const TOPT_OCRD = 10;
124  const TOPT_OHT = 11;
125  const TOPT_OHTD = 12;
126  const TOPT_OFD = 13;
127  const TOPT_OVT = 14;
128  const TOPT_OVTD = 15;
129  const TOPT_OLD = 16;
130  const TOPT_EXT = 17;
131  const TOPT_LOGO = 18;
132  const TOPT_BYTE = 19;
133  const TOPT_DATA = 20;
134  const TOPT_SUP = 21;
135  const TOPT_SUPO = 22;
136  const TOPT_SNDL = 23;
137  const TOPT_TERM = 24;
138  const TOPT_EOR = 25;
139  const TOPT_TACACS = 26;
140  const TOPT_OM = 27;
141  const TOPT_TLN = 28;
142  const TOPT_3270 = 29;
143  const TOPT_X3 = 30;
144  const TOPT_NAWS = 31;
145  const TOPT_TS = 32;
146  const TOPT_RFC = 33;
147  const TOPT_LINE = 34;
148  const TOPT_XDL = 35;
149  const TOPT_ENVIR = 36;
150  const TOPT_AUTH = 37;
151  const TOPT_ENVIR_NEW = 39;
152  const TOPT_TN3270 = 40;
153  const TOPT_X_AUTH = 41;
154  const TOPT_CHARSET = 42;
155  const TOPT_RSP = 43;
156  const TOPT_COMPORT = 44;
157  const TOPT_SLE = 45;
158  const TOPT_STARTTLS = 46;
159  const TOPT_KERMIT = 47;
160  const TOPT_SEND_URL = 48;
161  const TOPT_EXTOP = 255;
162 
164  const OptMap = (
165  TOPT_BIN : "BinaryTransmission",
166  TOPT_ECHO : "Echo|Is|Send",
167  TOPT_RECN : "Reconnection",
168  TOPT_SUPP : "SuppressGoAhead",
169  TOPT_APRX : "ApproxMessageSizeNegotiation",
170  TOPT_STAT : "Status",
171  TOPT_TIM : "TimingMark",
172  TOPT_REM : "RemoteControlledTransandEcho",
173  TOPT_OLW : "OutputLineWidth",
174  TOPT_OPS : "OutputPageSize",
175  TOPT_OCRD : "OutputCarriage-ReturnDisposition",
176  TOPT_OHT : "OutputHorizontalTabstops",
177  TOPT_OHTD : "OutputHorizontalTabDisposition",
178  TOPT_OFD : "OutputFormfeedDisposition",
179  TOPT_OVT : "OutputVerticalTabstops",
180  TOPT_OVTD : "OutputVerticalTabDisposition",
181  TOPT_OLD : "OutputLinefeedDisposition",
182  TOPT_EXT : "ExtendedASCII",
183  TOPT_LOGO : "Logout",
184  TOPT_BYTE : "ByteMacro",
185  TOPT_DATA : "DataEntryTerminal",
186  TOPT_SUP : "SUPDUP",
187  TOPT_SUPO : "SUPDUPOutput",
188  TOPT_SNDL : "SendLocation",
189  TOPT_TERM : "TerminalType",
190  TOPT_EOR : "EndofRecord",
191  TOPT_TACACS : "TACACSUserIdent",
192  TOPT_OM : "OutputMarking",
193  TOPT_TLN : "TerminalLocationNumber",
194  TOPT_3270 : "Telnet3270Regime",
195  TOPT_X3 : "X.3PAD",
196  TOPT_NAWS : "NegotiateAboutWindowSize",
197  TOPT_TS : "TerminalSpeed",
198  TOPT_RFC : "RemoteFlowControl",
199  TOPT_LINE : "Linemode",
200  TOPT_XDL : "XDisplayLocation",
201  TOPT_ENVIR : "TelnetEnvironmentOption",
202  TOPT_AUTH : "TelnetAuthenticationOption",
203  TOPT_ENVIR_NEW : "TelnetNewEnvironmentOption",
204  TOPT_TN3270 : "TN3270Enhancements",
205  TOPT_X_AUTH : "TelnetXAUTH",
206  TOPT_CHARSET : "TelnetCHARSET",
207  TOPT_RSP : "TelnetRemoteSerialPort",
208  TOPT_COMPORT : "TelnetComPortControl",
209  TOPT_SLE : "TelnetSuppressLocalEcho",
210  TOPT_STARTTLS : "TelnetStartTLS",
211  TOPT_KERMIT : "TelnetKERMIT",
212  TOPT_SEND_URL : "Send-URL",
213  TOPT_EXTOP : "Extended-Options-List",
214  );
216 
218 
221  class TelnetClient {
222 
223 public:
225  private :
226  // the connection string
227  string connect;
228 
229  // the socket for all communication with the server
230  Socket conn();
231 
232  // to manage contention on the Socket
233  Mutex m();
234 
235  // login user
236  *string user;
237 
238  // logging closures
239  *code log_info;
240  *code log_debug;
241 
242  // hash of features the telnet server has asked for
243  hash fh;
244 
245 public:
247 
248  // no public members
249 private:
250 
251 public:
252 
254 
259  constructor(string host, softint port, *code log, *code dbgLog);
260 
261 
263 
267  constructor(string connect, *code log, *code dbglog);
268 
269 
271  destructor();
272 
273 
275 
276 private:
277  log(string msg);
278 public:
279 
280 
282 
283 private:
284  logDbg(string msg);
285 public:
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 
375 private:
376  *string getDisplay();
377 public:
378 
379 
381 
384 private:
385  *string getTerminalType();
386 public:
387 
388 
390 
393 private:
395 public:
396 
397 
399 
402 private:
403  int getTerminalSpeed();
404 public:
405 
406 
408 
416 private:
418 public:
419 
420 
422  // don't override this method, fix/enhance it with a patch to the module
423 
424 private:
425  final disconnectIntern();
426 public:
427 
428 
429 
430 private:
431  checkConnIntern();
432 public:
433 
434 
435  // don't override this method, fix/enhance it with a patch to the module
436 
437 private:
438  final sendDataIntern(softlist arr, bool double_aic = False);
439 public:
440 
441 
442 
443 private:
444  sendTextDataIntern(string str);
445 public:
446 
447 
448  // don't override this method, fix/enhance it with a patch to the module
449 
450 private:
451  final *string getAvailableDataIntern(timeout t = DefaultTimeout);
452 public:
453 
454 
455  // don't override this method, fix/enhance it with a patch to the module
456 
457 private:
458  final updateWindowSizeIntern(hash wh);
459 public:
460 
461 
462  // don't override this method, fix/enhance it with a patch to the module
463 
464 private:
465  final processCmd(reference rv);
466 public:
467 
468 
469  // don't override this method, fix/enhance it with a patch to the module
470 
471 private:
472  final doSubNegotiation();
473 public:
474 
476  };
477 };