tcpiputils/dhcp/include/DHCPStateMachine.h
changeset 0 af10295192d8
child 20 7e41d162e158
equal deleted inserted replaced
-1:000000000000 0:af10295192d8
       
     1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // The DHCPStateMachine header file
       
    15 // 
       
    16 //
       
    17 
       
    18 /**
       
    19  @file DHCPStateMachine.h
       
    20 */
       
    21 
       
    22 #ifndef DHCPSTATEMACHINE_H
       
    23 #define DHCPSTATEMACHINE_H
       
    24 
       
    25 #include <e32base.h>
       
    26 #include <comms-infras/statemachine.h>
       
    27 
       
    28 #include "MsgSender.h"
       
    29 #include "DHCP_Std.h"
       
    30 
       
    31 class CExpireTimer;
       
    32 class CDHCPMessageHeader;
       
    33 class CDHCPState;
       
    34 #ifdef SYMBIAN_NETWORKING_ADDRESS_PROVISION
       
    35 class CDhcpHwAddrManager;
       
    36 #endif //SYMBIAN_NETWORKING_ADDRESS_PROVISION
       
    37 
       
    38 #ifdef SYMBIAN_NETWORKING_DHCPSERVER
       
    39 enum TSvrState
       
    40 	{
       
    41 	ESvrBinding,
       
    42 	ESvrWaitForAnyDHCPMsgs,
       
    43 	ESvrWaitForDiscoverInform,
       
    44 	ESvrEnd
       
    45 	};
       
    46 
       
    47 enum TSvrSpecificState
       
    48 	{
       
    49 	ESvrDiscoverInProgress,
       
    50 	ESvrInformInProgress,
       
    51 	ESvrRenewInProgress,
       
    52 	ESvrRebindInProgress,
       
    53 	ESvrDeclineInProgress,
       
    54 	ESvrReleaseInProgress,
       
    55 	};
       
    56 #endif // SYMBIAN_NETWORKING_DHCPSERVER	
       
    57 	
       
    58 class CDHCPStateMachine : public CStateMachine, public MMSListener
       
    59 /**
       
    60   * Implements helper function abstractions
       
    61   *
       
    62   * @internalTechnology
       
    63   */
       
    64 	{
       
    65 friend class CDHCPControl;
       
    66 friend class CDHCPDb;
       
    67 friend class CDHCPAddressAcquisition;
       
    68 friend class CDHCPSelect;
       
    69 friend class CDHCPRebootConfirm;
       
    70 friend class CDHCPInformationConfig;
       
    71 friend class CDHCPRequest;
       
    72 friend class CDHCPRenew;
       
    73 friend class CDHCPRebind;
       
    74 friend class CDHCPWaitForClientMsgs;
       
    75 
       
    76 public:
       
    77    enum EAddressType
       
    78       {
       
    79       EAllAvailableServers,
       
    80       EUnicast
       
    81       };
       
    82 
       
    83 	enum EInitialisationContext
       
    84 		{
       
    85 		EFirstCall,
       
    86 		ESubsequentCalls
       
    87 		};
       
    88 
       
    89 public:
       
    90 	~CDHCPStateMachine();
       
    91 
       
    92 	void Cancel();
       
    93 
       
    94 	virtual void GetServerAddress( TInetAddr& aAddress ) = 0;
       
    95   	virtual void SetCurrentAddress(const TInetAddr& aCurrentAddress, const TInetAddr& aSubnetMask) = 0;
       
    96 	void SetCurrentAddress(const TInetAddr& aCurrentAddress);
       
    97 #ifdef SYMBIAN_NETWORKING_DHCPSERVER	
       
    98 	virtual void GetClientAddress(TInetAddr& aAddress);
       
    99 	virtual void InitServerStateMachineL(MStateMachineNotify* aStateMachineNotify);
       
   100 	virtual void InitServerBinding(MStateMachineNotify* aStateMachineNotify);	
       
   101 	virtual void CloseNSendServerMsgL(TRequestStatus& aStatus, CDHCPStateMachine::EAddressType aEAddressType);
       
   102 	void CloseNSendSvrMsgL(TTimeIntervalSeconds aSecs, TInt aMaxRetryCount, CDHCPStateMachine::EAddressType aEAddressType);
       
   103 	virtual CDHCPState* ReceiveOnPort67L( TRequestStatus* aStatus ) = 0;
       
   104 	virtual void InitialiseServerSocketL() = 0;
       
   105    	virtual void CreateOfferMsgL() = 0;
       
   106    	virtual void HandleRequestMsgL() = 0;
       
   107    	virtual void HandleInformMsgL() = 0;   	
       
   108    	void SetClientIdentified(TBool aClientIdentified);
       
   109    	TBool IsClientIdentified();
       
   110    	void SetServerState(TBool aServerImpl);
       
   111    	void FetchServerAddressL();
       
   112 	TBool CheckNetworkId();
       
   113 	TInt BindServerInterface();
       
   114 	TInetAddr GetInterfaceServerGlobalAddress();
       
   115 	void SetDNSInformation(TDes8* aDNSInfo);
       
   116 #endif // SYMBIAN_NETWORKING_DHCPSERVER	
       
   117 	// returns unspecified address if no global address present
       
   118 	TInetAddr GetInterfaceGlobalAddress();
       
   119 	
       
   120 	TBool DoesInterfaceKnowAnyDNSServers();
       
   121 
       
   122    //dhcpIPX tasks
       
   123    virtual void StartInitL(MStateMachineNotify* aStateMachineNotify, EInitialisationContext aInitialisationContext, TInt aUserTimeOut = 0 ) = 0;
       
   124    virtual void StartInformL(MStateMachineNotify* aStateMachineNotify, TBool aStaticAddress) = 0;
       
   125    virtual void StartRebootL(MStateMachineNotify* aStateMachineNotify) = 0;
       
   126    virtual void StartRenewL(MStateMachineNotify* aStateMachineNotify, TInt aUserTimeOut) = 0;
       
   127    virtual void StartDeclineL(MStateMachineNotify* aStateMachineNotify) = 0;
       
   128    virtual void StartReleaseL(MStateMachineNotify* aStateMachineNotify) = 0;
       
   129    virtual void StartRebindL(MStateMachineNotify* aStateMachineNotify) = 0;
       
   130 
       
   131    virtual void RemoveConfiguredAddress(const TInetAddr *aInetAddr = NULL ) = 0;
       
   132    void RemoveConfiguredAddress( const TSoInet6InterfaceInfo& aSoInet6InterfaceInfo );
       
   133 
       
   134    //interface used by CDHCPXXStates derivatives
       
   135 	virtual void CloseNSendMsgL(TRequestStatus& aStatus, CDHCPStateMachine::EAddressType aEAddressType);
       
   136    void CloseNSendMsgL(TTimeIntervalSeconds aSecs, TInt aMaxRetryCount, CDHCPStateMachine::EAddressType aEAddressType);
       
   137    virtual CDHCPState* ReceiveL( TRequestStatus* aStatus ) = 0;
       
   138    void CancelMessageSender();
       
   139 
       
   140    virtual void InitialiseSocketL() = 0;
       
   141    virtual void CreateDiscoverMsgL() = 0;                //discover IP4 solicit IP6
       
   142    virtual void CreateOfferAcceptanceRequestMsgL() = 0;  //request after offer IP4 advertise IP6
       
   143    virtual void CreateRebootRequestMsgL() = 0;           //reboot IP4 confirm IP6
       
   144    virtual void CreateInformMsgL() = 0;                  //no IP address is required (static address IP4 stateless neg. IP6)
       
   145    virtual void CreateDeclineMsgL() = 0;
       
   146    virtual void CreateRenewRequestMsgL() = 0;
       
   147    virtual void CreateRebindRequestMsgL() = 0;
       
   148    virtual void CreateReleaseMsgL() = 0;
       
   149    virtual void HandleOfferL() = 0;                      //after discover IP4 solicit IP6 or rebind(IP4/IP6)
       
   150    virtual CDHCPState* HandleReplyL( TRequestStatus* aStatus ) = 0;//after request
       
   151    TInt BindToSource();
       
   152    virtual void BindSocketForUnicastL() = 0;
       
   153 
       
   154    //interface used by both CDHCPXXStates derivatives & CDHCPXXControl
       
   155    void StartTimer(TTimeIntervalSeconds aSeconds, MExpireTimer& aExpireTimer);
       
   156    void StartTimer( TTimeIntervalMicroSeconds32 aMicroSeconds, MExpireTimer& aExpireTimer);
       
   157    void CancelTimer();
       
   158    TBool TimerActive() const;
       
   159    TBool IsGettingCfgInfoOnly() const;
       
   160    void SetAsyncCancelHandler(CDHCPState* aStateOwner);
       
   161 
       
   162    virtual TInt MSReportError(TInt aError);
       
   163    
       
   164 	CDHCPMessageHeader* Message() const;
       
   165 	const TName& InterfaceName() const
       
   166 		{
       
   167 		return iInterfaceName;
       
   168 		}
       
   169 	void SetIdle( TBool aIdle );
       
   170 	TBool Idle();
       
   171 	void SetFastTimeoutDuringInform();
       
   172 	TBool FastTimeoutDuringInform();
       
   173 	void SetCompleteClientRequestTrue();
       
   174 	void SetCompleteClientRequestFalse();
       
   175 	TBool CompleteClientRequest();
       
   176 #ifdef SYMBIAN_DNS_PROXY	
       
   177 	TInetAddr GetListenerAddress();
       
   178 #endif // SYMBIAN_DNS_PROXY
       
   179 	
       
   180 protected:
       
   181    
       
   182 #ifdef SYMBIAN_NETWORKING_DHCPSERVER
       
   183 #ifdef SYMBIAN_NETWORKING_ADDRESS_PROVISION
       
   184    CDHCPStateMachine(RSocketServ& aEsock, RConnection& aConnection, const TName& aInterfaceName, CDhcpHwAddrManager* aDhcpHwAddrManager, TBool aDHCPServerImpl);
       
   185 #else
       
   186    CDHCPStateMachine(RSocketServ& aEsock, RConnection& aConnection, const TName& aInterfaceName,TBool aDHCPServerImpl);
       
   187 #endif //SYMBIAN_NETWORKING_ADDRESS_PROVISION
       
   188 #else   
       
   189    CDHCPStateMachine(RSocketServ& aEsock, RConnection& aConnection, const TName& aInterfaceName);
       
   190 #endif //  SYMBIAN_NETWORKING_DHCPSERVER
       
   191    void ConstructL();
       
   192    void Start(MStateMachineNotify* aStateMachineNotify);
       
   193 
       
   194    virtual void DoCancel();
       
   195 
       
   196    virtual void AssembleClientIDsL() = 0;
       
   197    void FetchHWAddress();
       
   198 
       
   199    void ConfigureInterfaceL( const TSoInet6InterfaceInfo& aInterfaceInfo );
       
   200    virtual void PrepareToSendL(CDHCPStateMachine::EAddressType aEAddressType) = 0;
       
   201 #ifdef SYMBIAN_NETWORKING_DHCPSERVER   
       
   202    	virtual void PrepareToSendServerMsgL(CDHCPStateMachine::EAddressType aEAddressType) = 0;
       
   203 	void AddScopeToClientAddrL(TInetAddr& addr);
       
   204 #endif // SYMBIAN_NETWORKING_DHCPSERVER	
       
   205    virtual void AssignAddresses( TInetAddr& aDest, const TInetAddr& aSrc ) const = 0;
       
   206    TUint32 GetNetworkIdL() const;
       
   207    void AddScopeToAddrL(TInetAddr& addr);
       
   208 
       
   209 protected:
       
   210 	const TName& iInterfaceName;
       
   211 	RSocketServ& iEsock;
       
   212 	RConnection& iConnection;
       
   213 	CDHCPMessageHeader* iDhcpMessage;
       
   214 	CMessageSender* iMessageSender;
       
   215 	CExpireTimer* iTimer;
       
   216 
       
   217 	TSockAddr iHardwareAddr;
       
   218 #ifdef SYMBIAN_NETWORKING_DHCPSERVER	
       
   219 	TSockAddr iClientHWAddr;
       
   220 	TInetAddr iInformClientAddr;
       
   221 #ifdef SYMBIAN_NETWORKING_ADDRESS_PROVISION
       
   222 	CDhcpHwAddrManager* iDhcpHwAddrManager;
       
   223 #endif //SYMBIAN_NETWORKING_ADDRESS_PROVISION
       
   224 	
       
   225 #endif // SYMBIAN_NETWORKING_DHCPSERVER	
       
   226 	CDHCPState* iAsyncCancelHandler; //iAsyncCancelHandler is set by the DHCP State that assumes ownership of the RequestStatus object of CDHCPStateMachine 
       
   227 
       
   228 public: 
       
   229    //accessed by CDHCPIP6MessageReader
       
   230 	RSocket iSocket;
       
   231 	TInetAddr iSocketAddr;
       
   232 #ifdef SYMBIAN_NETWORKING_DHCPSERVER	
       
   233 	RSocket iSvrSocket;//For the Server Impl
       
   234 	TInetAddr iSrvSocketAddr;
       
   235 	TSvrState iSvrState;
       
   236 	TSvrSpecificState iSvrSpecificState;
       
   237 	HBufC8* 	iDNSInformation;
       
   238 #endif // SYMBIAN_NETWORKING_DHCPSERVER	
       
   239 
       
   240 #ifdef SYMBIAN_DNS_PROXY
       
   241 	TBuf8<KMaxName>   iProxyHostName;
       
   242 	TBuf8<KMaxName>   iProxyDomainName;
       
   243 	TInetAddr   iProxyDnsSrvAddr;
       
   244 	TInetAddr   iClientStaticAddr;
       
   245 	TUint32     iOfferedAddress;
       
   246 	TBuf8<KMaxName>     iClientHostName;
       
   247 #endif // SYMBIAN_DNS_PROXY
       
   248 		
       
   249 protected:
       
   250 	TDhcpRnd iXid;
       
   251 	TBool iCfgInfoOnly; //if ETrue then:
       
   252 							  //for IP4 indicates that a static address is used
       
   253 							  //for IP6 if set during negotiation it indicates the same as for IP4
       
   254 							  //			if set by CDHCPIP6Reconfigure states it indicates that the server wants us
       
   255 							  //			to start inform-request rather than renew request
       
   256 	TBool iMakeIdle;		  // used only for IP6, when RA recvd, with both 'M' and 'O' flags as false, this flag makes statemachine idle
       
   257 	TBool iFastTimeout;
       
   258 	TBool iCompleteClientRequest;	//used only for IP6, set to ETrue when 'O' flag in RA is set
       
   259    
       
   260 	//data read from CommDb &&|| stored in persistent storage
       
   261 	//the "public" should be replaced by some more inteligent way of dealing with persistent & init data
       
   262 	//common for IP4 & IP6(maybe EPOC wide persistent storage?)
       
   263     //for IP6 it's the first address from the first IA option in the reply message
       
   264   	TInetAddr iCurrentAddress;
       
   265 	TTime iTaskStartedAt;			// Time that task started. If sucessful it is copied to iStartedAquisitionAt
       
   266 	TTime iStartedAquisitionAt;	// Time that lease period started
       
   267 
       
   268 	TInetAddr iDefGateway;
       
   269 
       
   270 	TBool iNameServerAddressesFromServer;
       
   271 	TInetAddr iNameServer1;
       
   272 	TInetAddr iNameServer2;
       
   273 
       
   274 	//DNS client names
       
   275 	HBufC8*   iHostName;
       
   276 	HBufC8*   iDomainName;
       
   277 
       
   278 	TUint32 iRenewalTimeT1;			// number of seconds after iStartedAquisitionAt when we send a renew request
       
   279 	TUint32 iRebindTimeT2;			// number of seconds after iStartedAquisitionAt when we send a rebind request (only if any renew fails)
       
   280 	TUint32 iLeaseTime;				// number of seconds after iStartedAquisitionAt when the lease expires
       
   281 
       
   282 	TBool iReceiving;				//ETrue if we are waiting to receive data from socket
       
   283 
       
   284 	CDHCPState* iFirstState; //accessed by CDHCPIP6ListenToNeighbor
       
   285 	RBuf8 iClientId;  //accessed from CDhcpDb
       
   286 	
       
   287 	TInt iMaxRetryCount;	//IPv6 calculates its own retry values..so iMaxRetryCount is ignored
       
   288 	TInt iRetryDhcpIpCount;	//number of retry for DHCP IP in case a DAD is detected.
       
   289 
       
   290 #ifdef SYMBIAN_NETWORKING_DHCPSERVER
       
   291 	TUint32 iYiaddr;
       
   292 	TUint32 iCiaddr;
       
   293 	TUint16 iFlag;							  
       
   294 	TUint8 iMesgType;							  
       
   295 	TBool iClientIdentified;
       
   296 	TBool iServerImpl;
       
   297 #endif // 	SYMBIAN_NETWORKING_DHCPSERVER	
       
   298 #ifdef SYMBIAN_NETWORKING_DHCP_MSG_HEADERS
       
   299 public:	
       
   300 	TBool iDhcpInformAckPending;
       
   301 	RBuf8 iSavedExtraParameters;
       
   302 #endif //SYMBIAN_NETWORKING_DHCP_MSG_HEADERS	
       
   303 	};
       
   304 	
       
   305 #ifdef SYMBIAN_NETWORKING_DHCPSERVER
       
   306 #ifndef SYMBIAN_NETWORKING_ADDRESS_PROVISION
       
   307 inline CDHCPStateMachine::CDHCPStateMachine(RSocketServ& aEsock, RConnection& aConnection, const TName& aInterfaceName,TBool aDHCPServerImpl):
       
   308                      iInterfaceName(aInterfaceName),
       
   309                      iEsock(aEsock),
       
   310                      iConnection(aConnection),
       
   311                      iCfgInfoOnly( EFalse ),iMaxRetryCount(KInfinity),
       
   312 					 iMakeIdle (EFalse),
       
   313 					 iFastTimeout (EFalse),
       
   314 					 iServerImpl(aDHCPServerImpl),
       
   315 					 iRetryDhcpIpCount(0)
       
   316 #else
       
   317 inline CDHCPStateMachine::CDHCPStateMachine(RSocketServ& aEsock, RConnection& aConnection, const TName& aInterfaceName, CDhcpHwAddrManager* aDhcpHwAddrManager, TBool aDHCPServerImpl):
       
   318                      iInterfaceName(aInterfaceName),
       
   319                      iEsock(aEsock),
       
   320                      iConnection(aConnection),
       
   321                      iCfgInfoOnly( EFalse ),iMaxRetryCount(KInfinity),
       
   322 					 iMakeIdle (EFalse),
       
   323 					 iFastTimeout (EFalse),
       
   324 					 iServerImpl(aDHCPServerImpl),
       
   325 					 iRetryDhcpIpCount(0),
       
   326 					 iDhcpHwAddrManager(aDhcpHwAddrManager)
       
   327 #endif //SYMBIAN_NETWORKING_ADDRESS_PROVISION
       
   328 /**
       
   329   * Constructor of the DHCPStateMachine
       
   330   *
       
   331   * @internalTechnology
       
   332   *
       
   333   */
       
   334 	{
       
   335 	}
       
   336 #else 
       
   337 
       
   338 inline CDHCPStateMachine::CDHCPStateMachine(RSocketServ& aEsock, RConnection& aConnection, const TName& aInterfaceName):
       
   339 	iInterfaceName(aInterfaceName),
       
   340 	iEsock(aEsock),
       
   341 	iConnection(aConnection),
       
   342 	iCfgInfoOnly( EFalse ),iMaxRetryCount(KInfinity),
       
   343 	iMessageSender(NULL),
       
   344 	iMakeIdle(EFalse),
       
   345 	iFastTimeout(EFalse),
       
   346 	iRetryDhcpIpCount(0)
       
   347 /**
       
   348   * Constructor of the DHCPStateMachine
       
   349   *
       
   350   * @internalTechnology
       
   351   *
       
   352   */
       
   353 	{
       
   354 	}
       
   355 #endif // SYMBIAN_NETWORKING_DHCPSERVER
       
   356 inline CDHCPMessageHeader* CDHCPStateMachine::Message() const
       
   357 	{
       
   358 	return iDhcpMessage;
       
   359 	}
       
   360 	
       
   361 inline void CDHCPStateMachine::CancelMessageSender()
       
   362    {
       
   363 	if (iMessageSender != NULL)
       
   364 		iMessageSender->Cancel();
       
   365    }
       
   366 
       
   367 inline TBool CDHCPStateMachine::TimerActive() const
       
   368 	{
       
   369 	return iTimer != NULL ? iTimer->IsActive() : EFalse;
       
   370 	}
       
   371 
       
   372 inline void CDHCPStateMachine::CancelTimer()
       
   373 	{
       
   374 	__CFLOG_VAR((KLogSubSysDHCP, KLogCode, _L8("CDHCPStateMachine::CancelTimer")));
       
   375 
       
   376 	if (iTimer != NULL)
       
   377 		iTimer->Cancel();
       
   378 	}
       
   379 
       
   380 inline TBool CDHCPStateMachine::IsGettingCfgInfoOnly() const
       
   381 	{
       
   382 	return iCfgInfoOnly;
       
   383 	}
       
   384 
       
   385 inline void CDHCPStateMachine::SetAsyncCancelHandler(CDHCPState* aStateOwner) 
       
   386 	{	
       
   387 	iAsyncCancelHandler =  aStateOwner;
       
   388 	}	
       
   389 
       
   390 inline void CDHCPStateMachine::SetCurrentAddress(const TInetAddr& aCurrentAddress)
       
   391 /**
       
   392   * The SetCurrentAddress function
       
   393   *
       
   394   * Stores ip address
       
   395   *
       
   396   * @internalTechnology
       
   397   */
       
   398 	{
       
   399 	iCurrentAddress = aCurrentAddress;
       
   400 	}
       
   401 
       
   402 inline void CDHCPStateMachine::SetIdle ( TBool aIdle )
       
   403 	{
       
   404 	iMakeIdle = aIdle;
       
   405 	}
       
   406 
       
   407 inline TBool CDHCPStateMachine::Idle ()
       
   408 	{
       
   409 	return iMakeIdle;
       
   410 	}
       
   411 
       
   412 inline void CDHCPStateMachine::SetFastTimeoutDuringInform()
       
   413 	{
       
   414 	iFastTimeout = ETrue;
       
   415 	}
       
   416 
       
   417 inline TBool CDHCPStateMachine::FastTimeoutDuringInform()
       
   418 	{
       
   419 	return iFastTimeout;
       
   420 	}
       
   421 
       
   422 
       
   423 inline void CDHCPStateMachine::SetCompleteClientRequestTrue()
       
   424 	{
       
   425 	iCompleteClientRequest = ETrue;
       
   426 	}
       
   427 
       
   428 inline void CDHCPStateMachine::SetCompleteClientRequestFalse()
       
   429 	{
       
   430 	iCompleteClientRequest = EFalse;
       
   431 	}
       
   432 
       
   433 inline TBool CDHCPStateMachine::CompleteClientRequest()
       
   434 	{
       
   435 	return iCompleteClientRequest;
       
   436 	}
       
   437 
       
   438 
       
   439 #ifdef SYMBIAN_NETWORKING_DHCPSERVER	
       
   440 inline void CDHCPStateMachine::SetClientIdentified(TBool aClientIdentified)
       
   441 	{
       
   442 	iClientIdentified = aClientIdentified;
       
   443 	}
       
   444 
       
   445 inline TBool CDHCPStateMachine::IsClientIdentified()
       
   446 	{
       
   447 	return iClientIdentified;
       
   448 	}
       
   449 
       
   450 #endif 	// SYMBIAN_NETWORKING_DHCPSERVER	
       
   451 
       
   452 #endif
       
   453