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  * $Revision: 20385 $
00027  * $Author: rjongbloed $
00028  * $Date: 2008-06-04 10:40:38 +0000 (Wed, 04 Jun 2008) $
00029  */
00030 
00031 #ifndef _PTELNETSOCKET
00032 #define _PTELNETSOCKET
00033 
00034 #ifdef P_USE_PRAGMA
00035 #pragma interface
00036 #endif
00037 
00038 #include <ptlib/sockets.h>
00039 
00040 
00043 class PTelnetSocket : public PTCPSocket
00044 {
00045   PCLASSINFO(PTelnetSocket, PTCPSocket)
00046 
00047   public:
00048     PTelnetSocket();
00049     // Create an unopened TELNET socket.
00050 
00051     PTelnetSocket(
00052       const PString & address  
00053     );
00054     // Create an opened TELNET socket.
00055 
00056 
00057   // Overrides from class PChannel
00073     PBoolean Read(
00074       void * buf,   
00075       PINDEX len    
00076     );
00077 
00091     PBoolean Write(
00092       const void * buf, 
00093       PINDEX len        
00094     );
00095 
00096 
00108     virtual PBoolean Connect(
00109       const PString & address   
00110     );
00111 
00112 
00127     virtual PBoolean Accept(
00128       PSocket & socket          
00129     );
00130 
00131 
00138     virtual void OnOutOfBand(
00139       const void * buf,   
00140       PINDEX len          
00141     );
00142 
00143 
00144   // New functions
00145     enum Command {
00146       IAC           = 255,    
00147       DONT          = 254,    
00148       DO            = 253,    
00149       WONT          = 252,    
00150       WILL          = 251,    
00151       SB            = 250,    
00152       GoAhead       = 249,    
00153       EraseLine     = 248,    
00154       EraseChar     = 247,    
00155       AreYouThere   = 246,    
00156       AbortOutput   = 245,    
00157       InterruptProcess = 244, 
00158       Break         = 243,    
00159       DataMark      = 242,    
00160       NOP           = 241,    
00161       SE            = 240,    
00162       EndOfReccord  = 239,    
00163       AbortProcess  = 238,    
00164       SuspendProcess= 237,    
00165       EndOfFile     = 236     
00166     };
00167     // Defined telnet commands codes
00168 
00189     PBoolean SendCommand(
00190       Command cmd,  
00191       int opt = 0  
00192     );
00193 
00194 
00195     enum Options {
00196       TransmitBinary      = 0,    
00197       EchoOption          = 1,    
00198       ReconnectOption     = 2,    
00199       SuppressGoAhead     = 3,    
00200       MessageSizeOption   = 4,    
00201       StatusOption        = 5,    
00202       TimingMark          = 6,    
00203       RCTEOption          = 7,    
00204       OutputLineWidth     = 8,    
00205       OutputPageSize      = 9,    
00206       CRDisposition       = 10,   
00207       HorizontalTabsStops = 11,   
00208       HorizTabDisposition = 12,   
00209       FormFeedDisposition = 13,   
00210       VerticalTabStops    = 14,   
00211       VertTabDisposition  = 15,   
00212       LineFeedDisposition = 16,   
00213       ExtendedASCII       = 17,   
00214       ForceLogout         = 18,   
00215       ByteMacroOption     = 19,   
00216       DataEntryTerminal   = 20,   
00217       SupDupProtocol      = 21,   
00218       SupDupOutput        = 22,   
00219       SendLocation        = 23,   
00220       TerminalType        = 24,   
00221       EndOfRecordOption   = 25,   
00222       TACACSUID           = 26,   
00223       OutputMark          = 27,   
00224       TerminalLocation    = 28,   
00225       Use3270RegimeOption = 29,   
00226       UseX3PADOption      = 30,   
00227       WindowSize          = 31,   
00228       TerminalSpeed       = 32,   
00229       FlowControl         = 33,   
00230       LineModeOption      = 34,   
00231       XDisplayLocation    = 35,   
00232       EnvironmentOption   = 36,   
00233       AuthenticateOption  = 37,   
00234       EncriptionOption    = 38,   
00235       EncryptionOption    = 38,   
00236       ExtendedOptionsList = 255,  
00237       MaxOptions
00238     };
00239     // Defined TELNET options.
00240 
00241 
00247     virtual PBoolean SendDo(
00248       BYTE option    
00249     );
00250 
00256     virtual PBoolean SendDont(
00257       BYTE option    
00258     );
00259 
00265     virtual PBoolean SendWill(
00266       BYTE option    
00267     );
00268 
00274     virtual PBoolean SendWont(
00275       BYTE option    
00276     );
00277 
00278     enum SubOptionCodes {
00279       SubOptionIs       = 0,  
00280       SubOptionSend     = 1,  
00281     };
00282     // Codes for sub option negotiation.
00283 
00289     PBoolean SendSubOption(
00290       BYTE code,          
00291       const BYTE * info,  
00292       PINDEX len,         
00293       int subCode = -1    
00294     );
00295 
00299     void SetOurOption(
00300       BYTE code,          
00301       PBoolean state = PTrue   
00302     ) { option[code].weCan = state; }
00303 
00307     void SetTheirOption(
00308       BYTE code,          
00309       PBoolean state = PTrue  
00310     ) { option[code].theyShould = state; }
00311 
00317     PBoolean IsOurOption(
00318       BYTE code    
00319     ) const { return option[code].ourState == OptionInfo::IsYes; }
00320 
00326     PBoolean IsTheirOption(
00327       BYTE code    
00328     ) const { return option[code].theirState == OptionInfo::IsYes; }
00329 
00330     void SetTerminalType(
00331       const PString & newType   
00332     );
00333     // Set the terminal type description string for TELNET protocol.
00334 
00335     const PString & GetTerminalType() const { return terminalType; }
00336     // Get the terminal type description string for TELNET protocol.
00337 
00338     void SetWindowSize(
00339       WORD width,   
00340       WORD height   
00341     );
00342     // Set the width and height of the Network Virtual Terminal window.
00343 
00344     void GetWindowSize(
00345       WORD & width,   
00346       WORD & height   
00347     ) const;
00348     // Get the width and height of the Network Virtual Terminal window.
00349 
00350 
00351   protected:
00352     void Construct();
00353     // Common construct code for TELNET socket channel.
00354 
00364     virtual void OnDo(
00365       BYTE option   
00366     );
00367 
00374     virtual void OnDont(
00375       BYTE option   
00376     );
00377 
00384     virtual void OnWill(
00385       BYTE option   
00386     );
00387 
00394     virtual void OnWont(
00395       BYTE option   
00396     );
00397 
00401     virtual void OnSubOption(
00402       BYTE code,          
00403       const BYTE * info,  
00404       PINDEX len          
00405     );
00406 
00407 
00417     virtual PBoolean OnCommand(
00418       BYTE code  
00419     );
00420 
00421 
00422   // Member variables.
00423     struct OptionInfo {
00424       enum {
00425         IsNo, IsYes, WantNo, WantNoQueued, WantYes, WantYesQueued
00426       };
00427       unsigned weCan:1;      // We can do the option if they want us to do.
00428       unsigned ourState:3;
00429       unsigned theyShould:1; // They should if they will.
00430       unsigned theirState:3;
00431     };
00432     
00433     OptionInfo option[MaxOptions];
00434     // Information on protocol options.
00435 
00436     PString terminalType;
00437     // Type of terminal connected to telnet socket, defaults to "UNKNOWN"
00438 
00439     WORD windowWidth, windowHeight;
00440     // Size of the "window" used by the NVT.
00441 
00442     PBoolean debug;
00443     // Debug socket, output messages to PError stream.
00444 
00445 
00446   private:
00447     enum State {
00448       StateNormal,
00449       StateCarriageReturn,
00450       StateIAC,
00451       StateDo,
00452       StateDont,
00453       StateWill,
00454       StateWont,
00455       StateSubNegotiations,
00456       StateEndNegotiations
00457     };
00458     // Internal states for the TELNET decoder
00459 
00460     State state;
00461     // Current state of incoming characters.
00462 
00463     PBYTEArray subOption;
00464     // Storage for sub-negotiated options
00465 
00466     unsigned synchronising;
00467 
00468     PBoolean StartSend(const char * which, BYTE code);
00469 };
00470 
00471 
00472 #endif
00473 
00474 
00475 // End Of File ///////////////////////////////////////////////////////////////
00476 

Generated on Mon Sep 15 01:21:35 2008 for PTLib by  doxygen 1.5.1