Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Class Members | File Members

telnet.h

Go to the documentation of this file.
00001 /*
00002  * telnet.h
00003  *
00004  * TELNET Socket class.
00005  *
00006  * Portable Windows Library
00007  *
00008  * Copyright (c) 1993-2002 Equivalence Pty. Ltd.
00009  *
00010  * The contents of this file are subject to the Mozilla Public License
00011  * Version 1.0 (the "License"); you may not use this file except in
00012  * compliance with the License. You may obtain a copy of the License at
00013  * http://www.mozilla.org/MPL/
00014  *
00015  * Software distributed under the License is distributed on an "AS IS"
00016  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
00017  * the License for the specific language governing rights and limitations
00018  * under the License.
00019  *
00020  * The Original Code is Portable Windows Library.
00021  *
00022  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
00023  *
00024  * Contributor(s): ______________________________________.
00025  *
00026  * $Log: telnet.h,v $
00027  * Revision 1.23  2002/11/06 22:47:24  robertj
00028  * Fixed header comment (copyright etc)
00029  *
00030  * Revision 1.22  2002/09/16 01:08:59  robertj
00031  * Added #define so can select if #pragma interface/implementation is used on
00032  *   platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan.
00033  *
00034  * Revision 1.21  1999/03/09 08:01:47  robertj
00035  * Changed comments for doc++ support (more to come).
00036  *
00037  * Revision 1.20  1999/02/16 08:07:10  robertj
00038  * MSVC 6.0 compatibility changes.
00039  *
00040  * Revision 1.19  1998/11/30 02:50:56  robertj
00041  * New directory structure
00042  *
00043  * Revision 1.18  1998/09/23 06:20:04  robertj
00044  * Added open source copyright license.
00045  *
00046  * Revision 1.17  1996/08/08 10:08:54  robertj
00047  * Directory structure changes for common files.
00048  *
00049  * Revision 1.16  1995/06/17 11:13:32  robertj
00050  * Documentation update.
00051  *
00052  * Revision 1.15  1995/06/17 00:47:38  robertj
00053  * Changed overloaded Open() calls to 3 separate function names.
00054  * More logical design of port numbers and service names.
00055  *
00056  * Revision 1.14  1995/06/04 12:46:26  robertj
00057  * Slight redesign of port numbers on sockets.
00058  *
00059  * Revision 1.13  1995/04/25 11:12:30  robertj
00060  * Fixed functions hiding ancestor virtuals.
00061  *
00062  * Revision 1.12  1995/04/01 08:32:10  robertj
00063  * Finally got a working TELNET.
00064  *
00065  * Revision 1.11  1995/03/18 06:27:50  robertj
00066  * Rewrite of telnet socket protocol according to RFC1143.
00067  *
00068  * Revision 1.10  1995/03/14  12:42:47  robertj
00069  * Updated documentation to use HTML codes.
00070  *
00071  * Revision 1.9  1995/02/21  11:25:33  robertj
00072  * Further implementation of telnet socket, feature complete now.
00073  *
00074  * Revision 1.8  1995/01/03  09:36:23  robertj
00075  * Documentation.
00076  *
00077  * Revision 1.7  1995/01/01  01:07:33  robertj
00078  * More implementation.
00079  *
00080  * Revision 1.6  1994/11/28  12:38:59  robertj
00081  * Added DONT and WONT states.
00082  *
00083  * Revision 1.5  1994/08/23  11:32:52  robertj
00084  * Oops
00085  *
00086  * Revision 1.4  1994/08/22  00:46:48  robertj
00087  * Added pragma fro GNU C++ compiler.
00088  *
00089  * Revision 1.3  1994/08/21  23:43:02  robertj
00090  * Changed type of socket port number for better portability.
00091  *
00092  * Revision 1.2  1994/07/25  03:36:03  robertj
00093  * Added sockets to common, normalising to same comment standard.
00094  *
00095  */
00096 
00097 #ifndef _PTELNETSOCKET
00098 #define _PTELNETSOCKET
00099 
00100 #ifdef P_USE_PRAGMA
00101 #pragma interface
00102 #endif
00103 
00104 #include <ptlib/sockets.h>
00105 
00106 
00109 class PTelnetSocket : public PTCPSocket
00110 {
00111   PCLASSINFO(PTelnetSocket, PTCPSocket)
00112 
00113   public:
00114     PTelnetSocket();
00115     // Create an unopened TELNET socket.
00116 
00117     PTelnetSocket(
00118       const PString & address  // Address of remote machine to connect to.
00119     );
00120     // Create an opened TELNET socket.
00121 
00122 
00123   // Overrides from class PChannel
00139     BOOL Read(
00140       void * buf,   // Pointer to a block of memory to receive the read bytes.
00141       PINDEX len    // Maximum number of bytes to read into the buffer.
00142     );
00143 
00157     BOOL Write(
00158       const void * buf, // Pointer to a block of memory to write.
00159       PINDEX len        // Number of bytes to write.
00160     );
00161 
00162 
00174     virtual BOOL Connect(
00175       const PString & address   // Address of remote machine to connect to.
00176     );
00177 
00178 
00193     virtual BOOL Accept(
00194       PSocket & socket          // Listening socket making the connection.
00195     );
00196 
00197 
00204     virtual void OnOutOfBand(
00205       const void * buf,   // Data to be received as URGENT TCP data.
00206       PINDEX len          // Number of bytes pointed to by <CODE>buf</CODE>.
00207     );
00208 
00209 
00210   // New functions
00211     enum Command {
00212       IAC           = 255,    // Interpret As Command - escape character.
00213       DONT          = 254,    // You are not to use option.
00214       DO            = 253,    // Request to use option.
00215       WONT          = 252,    // Refuse use of option.
00216       WILL          = 251,    // Accept the use of option.
00217       SB            = 250,    // Subnegotiation begin.
00218       GoAhead       = 249,    // Function GA, you may reverse the line.
00219       EraseLine     = 248,    // Function EL, erase the current line.
00220       EraseChar     = 247,    // Function EC, erase the current character.
00221       AreYouThere   = 246,    // Function AYT, are you there?
00222       AbortOutput   = 245,    // Function AO, abort output stream.
00223       InterruptProcess = 244, // Function IP, interrupt process, permanently.
00224       Break         = 243,    // NVT character break.
00225       DataMark      = 242,    // Marker for connection cleaning.
00226       NOP           = 241,    // No operation.
00227       SE            = 240,    // Subnegotiation end.
00228       EndOfReccord  = 239,    // End of record for transparent mode.
00229       AbortProcess  = 238,    // Abort the entire process
00230       SuspendProcess= 237,    // Suspend the process.
00231       EndOfFile     = 236     // End of file marker.
00232     };
00233     // Defined telnet commands codes
00234 
00255     BOOL SendCommand(
00256       Command cmd,  // Command code to send
00257       int opt = 0  // Option for command code.
00258     );
00259 
00260 
00261     enum Options {
00262       TransmitBinary      = 0,    // Assume binary 8 bit data is transferred.
00263       EchoOption          = 1,    // Automatically echo characters sent.
00264       ReconnectOption     = 2,    // Prepare to reconnect
00265       SuppressGoAhead     = 3,    // Do not use the GA protocol.
00266       MessageSizeOption   = 4,    // Negatiate approximate message size
00267       StatusOption        = 5,    // Status packets are understood.
00268       TimingMark          = 6,    // Marker for synchronisation.
00269       RCTEOption          = 7,    // Remote controlled transmission and echo.
00270       OutputLineWidth     = 8,    // Negotiate about output line width.
00271       OutputPageSize      = 9,    // Negotiate about output page size.
00272       CRDisposition       = 10,   // Negotiate about CR disposition.
00273       HorizontalTabsStops = 11,   // Negotiate about horizontal tabstops.
00274       HorizTabDisposition = 12,   // Negotiate about horizontal tab disposition
00275       FormFeedDisposition = 13,   // Negotiate about formfeed disposition.
00276       VerticalTabStops    = 14,   // Negotiate about vertical tab stops.
00277       VertTabDisposition  = 15,   // Negotiate about vertical tab disposition.
00278       LineFeedDisposition = 16,   // Negotiate about output LF disposition.
00279       ExtendedASCII       = 17,   // Extended ascic character set.
00280       ForceLogout         = 18,   // Force logout.
00281       ByteMacroOption     = 19,   // Byte macro.
00282       DataEntryTerminal   = 20,   // data entry terminal.
00283       SupDupProtocol      = 21,   // supdup protocol.
00284       SupDupOutput        = 22,   // supdup output.
00285       SendLocation        = 23,   // Send location.
00286       TerminalType        = 24,   // Provide terminal type information.
00287       EndOfRecordOption   = 25,   // Record boundary marker.
00288       TACACSUID           = 26,   // TACACS user identification.
00289       OutputMark          = 27,   // Output marker or banner text.
00290       TerminalLocation    = 28,   // Terminals physical location infromation.
00291       Use3270RegimeOption = 29,   // 3270 regime.
00292       UseX3PADOption      = 30,   // X.3 PAD
00293       WindowSize          = 31,   // NAWS - Negotiate About Window Size.
00294       TerminalSpeed       = 32,   // Provide terminal speed information.
00295       FlowControl         = 33,   // Remote flow control.
00296       LineModeOption      = 34,   // Terminal in line mode option.
00297       XDisplayLocation    = 35,   // X Display location.
00298       EnvironmentOption   = 36,   // Provide environment information.
00299       AuthenticateOption  = 37,   // Authenticate option.
00300       EncriptionOption    = 38,   // Encryption option.
00301       ExtendedOptionsList = 255,  // Code for extended options.
00302       MaxOptions
00303     };
00304     // Defined TELNET options.
00305 
00306 
00312     virtual BOOL SendDo(
00313       BYTE option    // Option to DO
00314     );
00315 
00321     virtual BOOL SendDont(
00322       BYTE option    // Option to DONT
00323     );
00324 
00330     virtual BOOL SendWill(
00331       BYTE option    // Option to WILL
00332     );
00333 
00339     virtual BOOL SendWont(
00340       BYTE option    // Option to WONT
00341     );
00342 
00343     enum SubOptionCodes {
00344       SubOptionIs       = 0,  // Sub-option is...
00345       SubOptionSend     = 1,  // Request to send option.
00346     };
00347     // Codes for sub option negotiation.
00348 
00354     BOOL SendSubOption(
00355       BYTE code,          // Suboptions option code.
00356       const BYTE * info,  // Information to send.
00357       PINDEX len,         // Length of information.
00358       int subCode = -1    // Suboptions sub-code, -1 indicates no sub-code.
00359     );
00360 
00364     void SetOurOption(
00365       BYTE code,          // Option to check.
00366       BOOL state = TRUE   // New state for for option.
00367     ) { option[code].weCan = state; }
00368 
00372     void SetTheirOption(
00373       BYTE code,          // Option to check.
00374       BOOL state = TRUE  // New state for for option.
00375     ) { option[code].theyShould = state; }
00376 
00382     BOOL IsOurOption(
00383       BYTE code    // Option to check.
00384     ) const { return option[code].ourState == OptionInfo::IsYes; }
00385 
00391     BOOL IsTheirOption(
00392       BYTE code    // Option to check.
00393     ) const { return option[code].theirState == OptionInfo::IsYes; }
00394 
00395     void SetTerminalType(
00396       const PString & newType   // New terminal type description string.
00397     );
00398     // Set the terminal type description string for TELNET protocol.
00399 
00400     const PString & GetTerminalType() const { return terminalType; }
00401     // Get the terminal type description string for TELNET protocol.
00402 
00403     void SetWindowSize(
00404       WORD width,   // New window width.
00405       WORD height   // New window height.
00406     );
00407     // Set the width and height of the Network Virtual Terminal window.
00408 
00409     void GetWindowSize(
00410       WORD & width,   // Old window width.
00411       WORD & height   // Old window height.
00412     ) const;
00413     // Get the width and height of the Network Virtual Terminal window.
00414 
00415 
00416   protected:
00417     void Construct();
00418     // Common construct code for TELNET socket channel.
00419 
00429     virtual void OnDo(
00430       BYTE option   // Option to DO
00431     );
00432 
00439     virtual void OnDont(
00440       BYTE option   // Option to DONT
00441     );
00442 
00449     virtual void OnWill(
00450       BYTE option   // Option to WILL
00451     );
00452 
00459     virtual void OnWont(
00460       BYTE option   // Option to WONT
00461     );
00462 
00466     virtual void OnSubOption(
00467       BYTE code,          // Option code for sub-option data.
00468       const BYTE * info,  // Extra information being sent in the sub-option.
00469       PINDEX len          // Number of extra bytes.
00470     );
00471 
00472 
00482     virtual BOOL OnCommand(
00483       BYTE code  // Code received that could not be precessed.
00484     );
00485 
00486 
00487   // Member variables.
00488     struct OptionInfo {
00489       enum {
00490         IsNo, IsYes, WantNo, WantNoQueued, WantYes, WantYesQueued
00491       };
00492       unsigned weCan:1;      // We can do the option if they want us to do.
00493       unsigned ourState:3;
00494       unsigned theyShould:1; // They should if they will.
00495       unsigned theirState:3;
00496     };
00497     
00498     OptionInfo option[MaxOptions];
00499     // Information on protocol options.
00500 
00501     PString terminalType;
00502     // Type of terminal connected to telnet socket, defaults to "UNKNOWN"
00503 
00504     WORD windowWidth, windowHeight;
00505     // Size of the "window" used by the NVT.
00506 
00507     BOOL debug;
00508     // Debug socket, output messages to PError stream.
00509 
00510 
00511   private:
00512     enum State {
00513       StateNormal,
00514       StateCarriageReturn,
00515       StateIAC,
00516       StateDo,
00517       StateDont,
00518       StateWill,
00519       StateWont,
00520       StateSubNegotiations,
00521       StateEndNegotiations
00522     };
00523     // Internal states for the TELNET decoder
00524 
00525     State state;
00526     // Current state of incoming characters.
00527 
00528     PBYTEArray subOption;
00529     // Storage for sub-negotiated options
00530 
00531     unsigned synchronising;
00532 
00533     BOOL StartSend(const char * which, BYTE code);
00534 };
00535 
00536 
00537 #endif
00538 
00539 
00540 // End Of File ///////////////////////////////////////////////////////////////
00541 

Generated on Sat Dec 22 16:46:51 2007 for PWLib by  doxygen 1.4.2