applayerprotocols/telnetengine/SRC/TELFSM.H
changeset 0 b16258d2340f
equal deleted inserted replaced
-1:000000000000 0:b16258d2340f
       
     1 /*
       
     2 * Copyright (c) 2003 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #ifndef _TELFSM_
       
    20 #define _TELFSM_
       
    21 
       
    22 #include <telsess.h>
       
    23 
       
    24 // RFC defined codes/commands 
       
    25 
       
    26 /**
       
    27 End of subnegotiation
       
    28 */
       
    29 static const TUint8 KTelnetSE	= 240;
       
    30 	
       
    31 /**
       
    32 No Operation
       
    33 */
       
    34 static const TUint8 KTelnetNOP	= 241;
       
    35 	
       
    36 /**
       
    37 Data Mark
       
    38 */
       
    39 static const TUint8 KTelnetDM	= 242;	
       
    40 /**
       
    41 NVT character Break
       
    42 */
       
    43 static const TUint8 KTelnetBRK	= 243;
       
    44 	
       
    45 /**
       
    46 NVT Interrupt Process
       
    47 */
       
    48 static const TUint8 KTelnetIP	= 244;	
       
    49 
       
    50 /**
       
    51 NVT Abort Output
       
    52 */
       
    53 static const TUint8 KTelnetAO	= 245;	
       
    54 
       
    55 /**
       
    56 NVT Are You There
       
    57 */
       
    58 static const TUint8 KTelnetAYT	= 246;	
       
    59 
       
    60 /**
       
    61 NVT Erase Character
       
    62 */
       
    63 static const TUint8 KTelnetEC	= 247;	
       
    64 
       
    65 /**
       
    66 NVT Erase Line
       
    67 */
       
    68 static const TUint8 KTelnetEL	= 248;
       
    69 	
       
    70 /**
       
    71 NVT Go Ahead Signal
       
    72 */
       
    73 static const TUint8 KTelnetGA	= 249;	
       
    74 
       
    75 /**
       
    76 Start of subnegotiation
       
    77 */
       
    78 static const TUint8 KTelnetSB	= 250;	
       
    79 
       
    80 /**
       
    81 Indicates the desire to begin/confirm option
       
    82 */
       
    83 static const TUint8 KTelnetWILL	= 251;
       
    84 	
       
    85 /**
       
    86 Indicates the refusal to begin/continue option
       
    87 */
       
    88 static const TUint8 KTelnetWONT	= 252;
       
    89 	
       
    90 /**
       
    91 Indicates the request that the other party begin/confirmation of option
       
    92 */
       
    93 static const TUint8 KTelnetDO	= 253;	
       
    94 
       
    95 /**
       
    96 Indicates the demand to stop/confirm that the option is no longer expected
       
    97 */
       
    98 static const TUint8 KTelnetDONT	= 254;
       
    99 	
       
   100 /**
       
   101 NVT Interpret As Command escape character
       
   102 */
       
   103 static const TUint8 KTelnetIAC	= 255;	
       
   104 
       
   105 static const TUint8 KTelnetCommandSEND	= 1;
       
   106 static const TUint8 KTelnetCommandIS	= 0;
       
   107 
       
   108 
       
   109 // Supported RFC defined options 
       
   110 
       
   111 static const TUint8 KTelnetProtOptionEcho			= 1;
       
   112 static const TUint8 KTelnetProtOptionSuppressGA		= 3;
       
   113 static const TUint8 KTelnetProtOptionBinary			= 0;
       
   114 static const TUint8 KTelnetProtOptionTerminalSpeed	= 32;
       
   115 static const TUint8 KTelnetProtOptionTerminalType	= 24;
       
   116 static const TUint8 KTelnetProtOptionWindowSize		= 31;
       
   117 static const TUint8 KTelnetProtOptionLogoff			= 18;
       
   118 static const TUint8 KTelnetProtOptionStatus			= 5;
       
   119 
       
   120 // RFC defined NVT Character constants 
       
   121 
       
   122 static const TUint8 KTelnetCR			= 0x0D;
       
   123 static const TUint8 KTelnetLF			= 0x0A;
       
   124 static const TUint8 KTelnetNULL			= 0x00;
       
   125 
       
   126 class MProtoEvent;
       
   127 
       
   128 class TOptionFSM
       
   129 /**
       
   130 RFC 1143 Bidirectional RFC option negotiation
       
   131 This class contains the core Us/Him state machine as defined in RFC 1143
       
   132 
       
   133 @internalComponent
       
   134 */
       
   135 {
       
   136 public:
       
   137 
       
   138 	/** Option Negotiation requests for both directions */
       
   139 	enum TRequests
       
   140 		{
       
   141 		ENoOption,EClientDo = 1,EClientWill = 2,EServerDo = 4, EServerWill = 8,
       
   142 		EClientWont,EClientDont,EServerWont,EServerDont
       
   143 		};
       
   144 
       
   145 	/** Return values from Request */
       
   146 	enum TRequestReturns
       
   147 		{
       
   148 		EErrNone,EProtocolError,EPermissionsError
       
   149 		};
       
   150 
       
   151 	/** Actions as a result of Request */
       
   152 	enum TActions
       
   153 		{
       
   154 		ENoAction,ESendDo,ESendWill,ESendDont,ESendWont
       
   155 		};
       
   156 
       
   157 	/** Events as a result of Request */
       
   158 	enum TEvents
       
   159 		{
       
   160 		ENoEvent,EServerEnabled = 1,EServerDisabled = 2,EClientEnabled = 4,EClientDisabled = 8
       
   161 		};
       
   162 	
       
   163 	// Key RFC 1143 method that takes requests from client or server side
       
   164 	TInt32	Request(const TInt32 aRequest,TInt32& aAction,TInt32& aEvent);
       
   165 	// Set the permissission on an option, maximum of :-
       
   166 	// EClientDo | EClientWill | EServerDo | EServerWill
       
   167 	void	SetPermission(const TInt32 aRequestPermissions){Allowed = aRequestPermissions;}
       
   168 	
       
   169 	// There are intermmediate WANT states but we only return enabled/disabled
       
   170 	TBool	ServerEnabled(){return(Him == EYes);}
       
   171 	TBool	ClientEnabled(){return(Us == EYes);}
       
   172 	// Reset the state machine
       
   173 	void	Reset();
       
   174 
       
   175 	TOptionFSM();
       
   176 	~TOptionFSM(){}
       
   177 
       
   178 private:
       
   179 	// Permissions, maximum of :-
       
   180 	// EClientDo | EClientWill | EServerDo | EServerWill
       
   181 	TInt32	Allowed;
       
   182 	// Defined in RFC 1143
       
   183 	TInt8	Us;
       
   184 	TInt8	UsQ;
       
   185 	TInt8	Him;
       
   186 	TInt8	HimQ;
       
   187 	
       
   188 	// values for HimQ,UsQ
       
   189 	enum TQueueStates
       
   190 		{
       
   191 		ENone,EEmpty,EOpposite
       
   192 		};
       
   193 											
       
   194 	// Values for Us,Him
       
   195 	enum TOptionStates
       
   196 		{
       
   197 		ENo,EYes,EWantYes,EWantNo
       
   198 		};
       
   199 };
       
   200 
       
   201 
       
   202 NONSHARABLE_CLASS(CUnknownOption)	:	public CBase
       
   203 /**
       
   204 Handles any unsupported RFC options
       
   205 
       
   206 @internalComponent
       
   207 */
       
   208 	{
       
   209 public:
       
   210 	CUnknownOption(){}
       
   211 	~CUnknownOption();
       
   212 	void RequestUnknown(TInt8 aOption,TInt32 aRequest,TDes8& aAction,TInt32& aEvent);
       
   213 	void Reset(){iFSM.Reset();}
       
   214 
       
   215 	static CUnknownOption* NewL();
       
   216 
       
   217 protected:
       
   218 	void ConstructL();
       
   219 
       
   220 	TOptionFSM iFSM;
       
   221 	};
       
   222 
       
   223 
       
   224 NONSHARABLE_CLASS(CRFCOptionBase)	:	public	CBase
       
   225 /**
       
   226 @internalComponent
       
   227 */
       
   228 	{
       
   229 public:
       
   230 	CRFCOptionBase(){}
       
   231 	~CRFCOptionBase();
       
   232 
       
   233 	static void CreateOptionResponse(TInt8 aOption,TInt32 aAction,TDes8& aBuffer);
       
   234 	void Reset(){iFSM.Reset();}
       
   235 	void SetRequestPermission(const TInt32 aAllowed){iFSM.SetPermission(aAllowed);}	
       
   236 protected:
       
   237 
       
   238 	virtual TInt32  RequestOption(const TInt32 aRequest,TDes8& aAction,TInt32& aEvent) = 0;
       
   239 	virtual void	GetTelnetOptionStatus(TDes8& aCurrentStatus) = 0;
       
   240 
       
   241 	TOptionFSM iFSM;
       
   242 
       
   243 	};
       
   244 
       
   245 NONSHARABLE_CLASS(CSuppressGAOption) :		public CRFCOptionBase
       
   246 /**
       
   247 TELNET Suppress Go Ahead RFC 858
       
   248 
       
   249 @internalComponent
       
   250 */
       
   251 	{
       
   252 public:
       
   253 	CSuppressGAOption(){}
       
   254 	~CSuppressGAOption(){}
       
   255 
       
   256 	static  CSuppressGAOption* NewL();
       
   257 	TInt32  RequestOption(const TInt32 aRequest,TDes8& aAction,TInt32& aEvent);
       
   258 	void	GetTelnetOptionStatus(TDes8& aCurrentStatus);
       
   259 
       
   260 protected:
       
   261 	void ConstructL();
       
   262 
       
   263 	};
       
   264 
       
   265 NONSHARABLE_CLASS(CStatusOption)	:	public CRFCOptionBase
       
   266 /**
       
   267 TELNET Status Option RFC 859
       
   268 
       
   269 @internalComponent
       
   270 */
       
   271 	{
       
   272 public:
       
   273 	CStatusOption(){}
       
   274 	~CStatusOption(){}
       
   275 
       
   276 	static CStatusOption* NewL();
       
   277 	TInt32  RequestOption(const TInt32 aRequest,TDes8& aAction,TInt32& aEvent);
       
   278 	void	GetTelnetOptionStatus(TDes8& aCurrentStatus);
       
   279 	TBool	ReceiveStatus(){return(iFSM.ServerEnabled());}
       
   280 	TBool	SendStatus(){return(iFSM.ClientEnabled());}
       
   281 protected:
       
   282 	void ConstructL();
       
   283 
       
   284 	};
       
   285 
       
   286 NONSHARABLE_CLASS(CSpeedOption)	:	public CRFCOptionBase
       
   287 /**
       
   288 TELNET Terminal Speed Option RFC 1079
       
   289 
       
   290 @internalComponent
       
   291 */
       
   292 	{
       
   293 public:
       
   294 	CSpeedOption(){}
       
   295 	~CSpeedOption(){}
       
   296 
       
   297 	static CSpeedOption* NewL();
       
   298 	TInt32  RequestOption(const TInt32 aRequest,TDes8& aAction,TInt32& aEvent);
       
   299 	void	GetTelnetOptionStatus(TDes8& aCurrentStatus);
       
   300 	void	GetTelnetSubOption(TDes8& aOutBuffer);
       
   301 	TBool	Set(const TDesC8& aSpeed);
       
   302 	TBool	SendSpeed(){return(iFSM.ClientEnabled());}
       
   303 
       
   304 protected:
       
   305 	void ConstructL();
       
   306 
       
   307 private:
       
   308 	TBuf8<20>	iTermSpeed;
       
   309 	};
       
   310 
       
   311 NONSHARABLE_CLASS(CLogoutOption)	:	public CRFCOptionBase
       
   312 /**
       
   313 TELNET Logout Option RFC 727
       
   314 
       
   315 @internalComponent
       
   316 */
       
   317 	{
       
   318 public:
       
   319 	CLogoutOption(){}
       
   320 	~CLogoutOption(){}
       
   321 
       
   322 	static  CLogoutOption* NewL();
       
   323 	TInt32  RequestOption(const TInt32 aRequest,TDes8& aAction,TInt32& aEvent);
       
   324 	void	GetTelnetOptionStatus(TDes8& aCurrentStatus);
       
   325 
       
   326 protected:
       
   327 	void ConstructL();
       
   328 	};
       
   329 
       
   330 
       
   331 NONSHARABLE_CLASS(CTerminalTypeOption)	:	public CRFCOptionBase
       
   332 /**
       
   333 TELNET Terminal Type Option RFC 1091
       
   334 
       
   335 @internalComponent
       
   336 */
       
   337 	{
       
   338 public:
       
   339 	CTerminalTypeOption(){}
       
   340 	~CTerminalTypeOption(){}
       
   341 
       
   342 	static CTerminalTypeOption* NewL();
       
   343 	TInt32  RequestOption(const TInt32 aRequest,TDes8& aAction,TInt32& aEvent);
       
   344 	void	GetTelnetOptionStatus(TDes8& aCurrentStatus);
       
   345 	void	GetTelnetSubOption(TDes8& aOutBuffer);
       
   346 	TBool	Set(const TDesC8& aType);
       
   347 	TBool	SendTerminalType(){return(iFSM.ClientEnabled());}
       
   348 	const TDesC8& TerminalType(){return(iTermType);}   
       
   349 
       
   350 protected:
       
   351 	void ConstructL();
       
   352 
       
   353 private:
       
   354 	TBuf8<40>	iTermType;
       
   355 	};
       
   356 
       
   357 NONSHARABLE_CLASS(CWindowSizeOption)	:	public CRFCOptionBase
       
   358 /**
       
   359 TELNET Window Size Option RFC 1073
       
   360 
       
   361 @internalComponent
       
   362 */
       
   363 	{
       
   364 public:
       
   365 	CWindowSizeOption(){}
       
   366 	~CWindowSizeOption(){}
       
   367 
       
   368 	static CWindowSizeOption* NewL();
       
   369 	TInt32  RequestOption(const TInt32 aRequest,TDes8& aAction,TInt32& aEvent);
       
   370 	void	GetTelnetOptionStatus(TDes8& aCurrentStatus);
       
   371 	void	GetTelnetSubOption(TDes8& aOutBuffer);
       
   372 	TBool	Set(const TTelnetConfig::TWindowSize& aSize);
       
   373 	TBool	SendWindowSize(){return(iFSM.ClientEnabled());}
       
   374 
       
   375 protected:
       
   376 	void ConstructL();
       
   377 
       
   378 private:
       
   379 	TTelnetConfig::TWindowSize iWindowSize;
       
   380 	};
       
   381 
       
   382 
       
   383 NONSHARABLE_CLASS(CEchoOption)	:	public CRFCOptionBase
       
   384 /**
       
   385 TELNET Echo Option RFC 857
       
   386 
       
   387 @internalComponent
       
   388 */
       
   389 	{
       
   390 public:
       
   391 	CEchoOption(){}
       
   392 	~CEchoOption(){}
       
   393 
       
   394 	static CEchoOption* NewL();
       
   395 	TInt32  RequestOption(const TInt32 aRequest,TDes8& aAction,TInt32& aEvent);
       
   396 	void	GetTelnetOptionStatus(TDes8& aCurrentStatus);
       
   397 	TBool	ReceiveEcho(){return(iFSM.ServerEnabled());}
       
   398 protected:
       
   399 	void ConstructL();
       
   400 
       
   401 	};
       
   402 
       
   403 NONSHARABLE_CLASS(CBinaryOption)	:	public CRFCOptionBase
       
   404 /**
       
   405 TELNET Binary Option RFC 856
       
   406 
       
   407 @internalComponent
       
   408 */
       
   409 	{
       
   410 public:
       
   411 	CBinaryOption(){}
       
   412 	~CBinaryOption(){}
       
   413 
       
   414 	static CBinaryOption* NewL();
       
   415 	TInt32  RequestOption(const TInt32 aRequest,TDes8& aAction,TInt32& aEvent);
       
   416 	void	GetTelnetOptionStatus(TDes8& aCurrentStatus);
       
   417 	TBool	ReceiveBinary(){return(iFSM.ServerEnabled());}
       
   418 	TBool	SendBinary(){return(iFSM.ClientEnabled());}
       
   419 
       
   420 protected:
       
   421 	void ConstructL();
       
   422 	};
       
   423 
       
   424 NONSHARABLE_CLASS(CProto) : public CBase
       
   425 /**
       
   426 The main TELNET RFC 854 coordinating class
       
   427 
       
   428 @internalComponent
       
   429 */
       
   430 	{
       
   431 public:
       
   432 	CProto();
       
   433 	~CProto();
       
   434 	static CProto* NewL(const TTelnetConfig& aConfig,MProtoEvent* aNotifier);
       
   435 	TInt ProtoWrite(const TDesC8& aInBuffer,TDes8& aOutBuffer);
       
   436 	TInt ProtoWrite(const TTelnetUserControl& aControlCode,TDes8& aBuffer,TDes8& aUrgentBuffer);
       
   437 	TInt ProtoRead(const TDesC8& aPortReadBuffer,TDes8& aClientReadBuffer,TDes8& aProtoBuffer);
       
   438 	TInt ClientRequestOption(TInt32 aOptionRequest,TDes8& aAction,TInt32& aEvent);
       
   439 	void ModifyConfig(const TTelnetConfig& aConfig,TDes8* aAction);
       
   440 	void ReceiveUrgent(TInt aUrgentData);
       
   441 	void OptionStatus(TOptionStatus& aStatus);
       
   442 	void ServerOptionStatus(TDes8& aActionBuffer);
       
   443 
       
   444 	void GetInitOptions(TDes8& aActionBuffer);
       
   445 	void Reset();
       
   446 
       
   447 	enum TLineEvents
       
   448 		{
       
   449 		EEventNone,
       
   450 		EEventUrgentData
       
   451 		};
       
   452 	
       
   453 private:
       
   454 	enum TReceiveState
       
   455 		{
       
   456 		ENormal,EIAC,ECommand,ESubOpt
       
   457 		};
       
   458 
       
   459 	enum TSubOptState
       
   460 		{
       
   461 		ESB,EOption,ERequest,EEndIAC
       
   462 		};
       
   463 
       
   464 	TBool					iUrgentFlag;
       
   465 	TInt					iUrgentData;
       
   466 
       
   467 	TReceiveState			iReceiveState;
       
   468 	TSubOptState			iSubOptState;
       
   469 
       
   470 	TBuf8<128>				iProtReadBuffer;
       
   471 	TBuf8<128>				iServerStatus;
       
   472 
       
   473 	CBinaryOption*			iBinary;
       
   474 	CEchoOption*			iEcho;
       
   475 	CWindowSizeOption*		iWindowSize;
       
   476 	CTerminalTypeOption*	iTerminalType;
       
   477 	CLogoutOption*			iLogout;
       
   478 	CSpeedOption*			iSpeed;
       
   479 	CSuppressGAOption*		iGA;
       
   480 	CStatusOption*			iStatus;
       
   481 	CUnknownOption*			iUnknown;
       
   482 
       
   483 	void GetOptionStatus(TDes8& aOutBuffer);
       
   484 
       
   485 	MProtoEvent* iNotifier;
       
   486 protected :
       
   487 	void ConstructL(const TTelnetConfig& aConfig,MProtoEvent* aNotifier);
       
   488 	};
       
   489 
       
   490 class MProtoEvent
       
   491 /**
       
   492 @internalComponent
       
   493 */
       
   494 	{
       
   495 public:
       
   496 	virtual void ProtoError(TInt aError) = 0;
       
   497 	virtual void ProtoEvent() = 0;
       
   498 	};
       
   499 
       
   500 #endif