natfw/natfwstunturnclient/inc/cstunrelaybindingimplementation.h
changeset 0 1bce908db942
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     1 /*
       
     2 * Copyright (c) 2007 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 
       
    20 
       
    21 #ifndef C_CSTUNRELAYBINDINGIMPLEMENTATION_H
       
    22 #define C_CSTUNRELAYBINDINGIMPLEMENTATION_H
       
    23 
       
    24 // INCLUDES
       
    25 #include <e32base.h>
       
    26 #include <es_sock.h>
       
    27 #include <in_sock.h>
       
    28 #include "mstuntransactionobserver.h"
       
    29 #include "cstuntimeruser.h"
       
    30 #include "natfwbindingimplementationbase.h"
       
    31 #include "stunturnclientlogs.h"
       
    32 #include "natfwunsaftcprelaypacket.h"
       
    33 
       
    34 // FORWARD DECLARATIONS
       
    35 class MSTUNBindingObserver;
       
    36 class MNcmConnectionMultiplexer;
       
    37 
       
    38 // CLASS DECLARATION
       
    39 /**
       
    40 * Hides the implementation of STUN binding for the user of STUN client.
       
    41 * @lib  natfwstunclient.dll
       
    42 */
       
    43 class CSTUNRelayBindingImplementation : public CBindingImplementation
       
    44     {
       
    45     friend class ut_cstunrelaybindingimplementation;
       
    46     
       
    47     public:
       
    48     
       
    49     enum TRequestType
       
    50         {
       
    51         EUnknown                        = 0,
       
    52         ESendRequest                    = 1,
       
    53         EAllocateRequest                = 2,
       
    54         ETCPConnectRequest              = 3,
       
    55         ESetActiveDestinationRequest    = 4
       
    56         };
       
    57         
       
    58     public:  // Constructors and destructor
       
    59 
       
    60         /**
       
    61          * Creates a new instance of CSTUNRelayBindingImplementation.
       
    62          * Associates the local socket with the binding.
       
    63          * The public IP address is available when
       
    64          * MSTUNClientObserver::STUNBindingEventOccuredL gets called.
       
    65          *
       
    66          * @since   s60 3.2
       
    67          * @pre aClient.IsInitialized()
       
    68          * @param aBinding Associated binding interface
       
    69          * @param aClient An initialised STUN client
       
    70          * @param aSocket An opened socket that has the local port set.
       
    71          * @return a new instance, the ownership is transferred.
       
    72          */
       
    73         static CSTUNRelayBindingImplementation* NewL( CBinding& aBinding,
       
    74                                                  MSTUNBindingObserver& aClient,
       
    75                                                  RSocket& aSocket );
       
    76         
       
    77         static CSTUNRelayBindingImplementation* NewL( CBinding& aBinding,
       
    78                                          MSTUNBindingObserver& aClient,
       
    79                                          TUint aStreamId,
       
    80                                          TUint aConnection,
       
    81                                          MNcmConnectionMultiplexer* aMux );
       
    82         /**
       
    83          * Destructor.
       
    84          */
       
    85         ~CSTUNRelayBindingImplementation();
       
    86 
       
    87     public: // From CSTUNTimerUser
       
    88 
       
    89         void TimerExpiredL();
       
    90 
       
    91         void LeaveFromTimerExpired( TInt aError );
       
    92 
       
    93     public: // From MSTUNTransactionObserver
       
    94 
       
    95         void PublicAddressObtainedL( const TInetAddr& aAddress );
       
    96         
       
    97         void PublicAddressObtainedL( const TInetAddr& aReflexiveAddr,
       
    98                                      const TInetAddr& aRelayAddr );
       
    99 
       
   100         void TransactionError( TInt aError,
       
   101                                CNATFWUNSAFUnknownAttributesAttribute* aUnknownAttr );
       
   102         
       
   103         void TransactionEventOccurredL( TSTUNCallbackInfo::TFunction aEvent );
       
   104 
       
   105     public: // New functions
       
   106 
       
   107         /**
       
   108          * Refreshes the binding causing a new Binding Request to be sent
       
   109          * to the STUN relay server. The public IP address is available when 
       
   110          * MSTUNClientObserver gets called.
       
   111          *
       
   112          * @since   s60 3.2
       
   113          * @param   aRtoValue Retransmission timeout(=0 if default to be used)
       
   114          * @return  void
       
   115          */
       
   116         void AllocateRequestL( TUint aRtoValue );
       
   117 
       
   118         /**
       
   119          * Cancels a previously issued call to SendRequestL.
       
   120          *
       
   121          * @since   s60 3.2
       
   122          * @return  void
       
   123          */
       
   124         void CancelRequest();
       
   125 
       
   126         /**
       
   127          * Gets the socket used for obtaining the public address.
       
   128          *
       
   129          * @since    s60 3.2
       
   130          * @return socket
       
   131          */
       
   132         const RSocket& Socket() const;    
       
   133 
       
   134         /**
       
   135          * Checks whether the public address has a value set
       
   136          *
       
   137          * @since   s60 3.2
       
   138          * @return ETrue if the public address has been obtained, 
       
   139          *         otherwise EFalse. 
       
   140          */
       
   141         TBool AddressResolved() const;    
       
   142 
       
   143         /**
       
   144          * Gets the public address for this binding as seen in public network.
       
   145          *
       
   146          * @since   s60 3.2
       
   147          * @return public address
       
   148          */
       
   149         const TInetAddr& PublicAddr() const;
       
   150         
       
   151         /**
       
   152          * Returns the relayed address allocated for this binding
       
   153          * 
       
   154          * @since   s60 3.2
       
   155          * @return relay address
       
   156          */
       
   157         const TInetAddr& RelayAddr() const; 
       
   158 
       
   159         /**
       
   160          * Returns alternate server transport address.
       
   161          *
       
   162          * @since   s60 3.2
       
   163          * @return alternate server address
       
   164          */
       
   165         const TInetAddr& AlternateServerAddr() const;
       
   166         
       
   167         /**
       
   168          * Returns realm value from error response.
       
   169          *
       
   170          * @since   s60 3.2
       
   171          * @return realm from error response         
       
   172          */
       
   173         const HBufC8* RealmFromResponse() const;         
       
   174         
       
   175         /**
       
   176          * The client should call this function when it has received data
       
   177          * from the socket shared with this binding and 
       
   178          * the client cannot itself handle the data.
       
   179          *  
       
   180          * @since   s60 3.2
       
   181          * @param   aData data received
       
   182          * @param   aConsumed on return ETrue if the data was accepted,
       
   183          *          otherwise EFalse.
       
   184          * @param   aRemoteAddr remote address from remote address attribute
       
   185          * @return  Decoded data if not consumed and is not unsaf message,
       
   186          *          otherwise NULL.
       
   187          */
       
   188         HBufC8* HandleDataL( 
       
   189             const TDesC8& aData, TBool& aConsumed, TInetAddr& aRemoteAddr );
       
   190 
       
   191         /**
       
   192          * Returns the associated CSTUNClient instance.
       
   193          *
       
   194          * @since   s60 3.2
       
   195          * @return CSTUNClient, or NULL if the CSTUNClient has been deleted        
       
   196          */
       
   197         const CSTUNClient* STUNClient() const;
       
   198 
       
   199         /**
       
   200          * Get a STUN server address from STUN client
       
   201          *
       
   202          * @since   s60 3.2
       
   203          * @return  void
       
   204          */
       
   205         void GetServerAddressL();
       
   206 
       
   207         /**
       
   208          * Get a fresh a shared secret.
       
   209          *
       
   210          * @since   s60 3.2
       
   211          * @return  void     
       
   212          */
       
   213         void GetSharedSecretL();
       
   214 
       
   215         /**
       
   216          * Clears the pointer to CSTUNClient.
       
   217          *
       
   218          * @since   s60 3.2
       
   219          * @return  void
       
   220          * @post iClient == NULL        
       
   221          */
       
   222         void DetachClient();
       
   223 
       
   224         /**
       
   225          * Store the obtained public address and inform application.
       
   226          * Shouldn't be called in relay implementation.
       
   227          *
       
   228          * @since   s60 3.2
       
   229          * @param aPublicAddress Public address
       
   230          * @return  void
       
   231          */        
       
   232         void StoreAddressL( const TInetAddr& aPublicAddress );
       
   233 
       
   234         /**
       
   235          * Store the obtained addresses and inform application.
       
   236          *
       
   237          * @since   s60 3.2
       
   238          * @param   aReflexiveAddr  public IP address (server reflexive)
       
   239          * @param   aRelayAddr      public relay IP address
       
   240          * return   void
       
   241          */
       
   242         virtual void StoreAddressL( const TInetAddr& aReflexiveAddr,
       
   243                                     const TInetAddr& aRelayAddr );
       
   244                                     
       
   245         /**
       
   246          * Change the object's state.
       
   247          *
       
   248          * @since   s60 3.2
       
   249          * @param aNewState State to enter.
       
   250          * @return  void
       
   251          */
       
   252         void ChangeState( CSTUNBindingState& aNewState );
       
   253 
       
   254         /**
       
   255          * Binding encountered an error and enters terminated state. The function
       
   256          * MSTUNClientObserver::BindingErrorL is used to inform application.
       
   257          *
       
   258          * @since   s60 3.2
       
   259          * @pre aError != KErrNone
       
   260          * @param aError Error code
       
   261          * @return  void
       
   262          */
       
   263         void Terminate( TInt aError );        
       
   264 
       
   265         /**
       
   266          * Checks if this binding is waiting for shared secret.
       
   267          *
       
   268          * @since   s60 3.2
       
   269          * @return ETrue BInding is waiting shared secret, EFalse otherwise
       
   270          */
       
   271         TBool IsWaitingSharedSecret() const;
       
   272 
       
   273         /**
       
   274          * STUN client uses this function to give the username and password, that
       
   275          * the binding requested earlier with function
       
   276          * MSTUNBindingObserver::ObtainSharedSecretL.
       
   277          *
       
   278          * @since   s60 3.2
       
   279          * @pre aUsername and aPassword must either both have a descriptor that
       
   280          *      is not empty, or then both must have an empty descritor.
       
   281          * @param aUsername Username or an empty descriptor if the shared secret
       
   282          *                    is not used.
       
   283          * @param aPassword Password or an empty descriptor if the shared secret
       
   284          *                    is not used.
       
   285          * @return  void
       
   286          */
       
   287         void SharedSecretObtainedL( const TDesC8& aUsername,
       
   288                                     const TDesC8& aPassword );
       
   289 
       
   290         /**
       
   291          * Creates or updates a Binding Request message, using the given username
       
   292          * and password.
       
   293          *
       
   294          * @since   s60 3.2
       
   295          * @param aUsername Username
       
   296          * @param aPassword Password
       
   297          * @return  void
       
   298          */
       
   299         void CreateBindingRequestL( const TDesC8& aUsername,
       
   300                                     const TDesC8& aPassword );
       
   301 
       
   302         /**
       
   303          * Creates a transaction for sending Binding Request message
       
   304          *
       
   305          * @since   s60 3.2
       
   306          * @pre iRequest != NULL
       
   307          * @return  void     
       
   308          */                          
       
   309         void SendBindingRequestL();
       
   310 
       
   311         /**
       
   312          * Take actions based on the reason why transaction failed. For certain
       
   313          * errors, it is possible to send the Binding Request again.
       
   314          *
       
   315          * @since   s60 3.2
       
   316          * @pre aError != KErrNone
       
   317          * @param aError Error reason
       
   318          * @return ETrue Request can be retried
       
   319          *          EFalse Binding enters Terminated-state
       
   320          */
       
   321         TBool HandleTransactionError( TInt aError );
       
   322 
       
   323         /**
       
   324          * Forward the ICMP error to transaction for processing.
       
   325          *
       
   326          * @since   s60 3.2
       
   327          * @param aAddress Address where the ICMP error occurred
       
   328          * @return  void    
       
   329          */
       
   330         void IcmpError( const TInetAddr& aAddress );
       
   331 
       
   332         /**
       
   333          * Wait before retring to send a Binding Request
       
   334          *
       
   335          * @since   s60 3.2
       
   336          * @return  void  
       
   337          */
       
   338         void WaitBeforeRetrying();
       
   339         
       
   340         
       
   341         // From CBindingImplementation
       
   342         
       
   343         void SetICESpecificAttributes( const TICEAttributes& aAttributes );
       
   344         
       
   345         void SetActiveDestinationRequestL( const TInetAddr& aRemoteAddr, 
       
   346             TUint32& aTimerValue );
       
   347         
       
   348         void SendIndicationL( const TInetAddr& aRemoteAddr, 
       
   349             const TDesC8& aData, TBool aAddFingerprint );
       
   350         
       
   351         void ConnectRequestL( const TInetAddr& aRemoteAddr );
       
   352 
       
   353         TUint StreamId() const;
       
   354         
       
   355         TUint ConnectionId() const;
       
   356         
       
   357         
       
   358         const CBinding& Binding();
       
   359              
       
   360     private: // Constructors, for internal use
       
   361 
       
   362         CSTUNRelayBindingImplementation( CBinding& aBinding,        
       
   363                                     MSTUNBindingObserver& aClient,
       
   364                                     RSocket& aSocket );
       
   365         
       
   366         CSTUNRelayBindingImplementation( CBinding& aBinding,
       
   367                                     MSTUNBindingObserver& aStunClient, 
       
   368                                     TUint aStreamId,
       
   369                                     TUint aSubstreamId,
       
   370                                     MNcmConnectionMultiplexer* aMux );
       
   371 
       
   372         void ConstructL();
       
   373         CSTUNRelayBindingImplementation();
       
   374 
       
   375     private: // New functions, for internal use
       
   376 
       
   377         /**
       
   378          * Release the memory of the Binding Request related data. This data is
       
   379          * only needed for sending a Binding Request and receiving a response.
       
   380          *
       
   381          * @since   s60 3.2
       
   382          * @return  void
       
   383          */
       
   384         void FreeRequestData();
       
   385 
       
   386         /**
       
   387          * Return STUN client handle.
       
   388          *
       
   389          * @since   s60 3.2
       
   390          * @pre iClient != NULL
       
   391          * @return STUN client
       
   392          * @leave KErrNotFound If the STUN client has been deleted
       
   393          */
       
   394         MSTUNBindingObserver& ClientL() const;
       
   395 
       
   396         /**
       
   397          * Return the current username, or an empty descriptor if username
       
   398          * does not exist.
       
   399          *
       
   400          * @since   s60 3.2
       
   401          * @return Username
       
   402          */
       
   403         const TDesC8& Username() const;
       
   404 
       
   405         /**
       
   406          * Checks if the STUN server has requested client to use a shared secret.
       
   407          *
       
   408          * @since   s60 3.2
       
   409          * @return ETrue if STUN server requires the use of a shared secret
       
   410          *          EFalse otherwise
       
   411          */
       
   412         TBool IsSharedSecretRequired() const;
       
   413 
       
   414         /**
       
   415          * Decodes a byte stream and return the decoded UNSAF message.
       
   416          *
       
   417          * @since   s60 3.2
       
   418          * @param aData Byte stream to decode
       
   419          * @return Decoded message, ownership is transferred.
       
   420          *          NULL if the stream didn't contain an UNSAF message.
       
   421          */
       
   422         CNATFWUNSAFMessage* DecodeMessageL( const TDesC8& aData ) const;
       
   423         
       
   424         /**
       
   425          * Decodes a byte stream and returns a decoded TCP relay message.
       
   426          *
       
   427          * @since   s60 3.2
       
   428          * @param aData Byte stream to decode
       
   429          * @return A new instance of CNATFWUNSAFTcpRelayPacket.
       
   430          *         The ownership is transferred
       
   431          */
       
   432         CNATFWUNSAFTcpRelayPacket* DecodeTcpRelayMessageL( const 
       
   433             TDesC8& aData ) const;        
       
   434         
       
   435         /**
       
   436          * Checks message type.
       
   437          *
       
   438          * @since   s60 3.2
       
   439          * @param   aMsg pointer to message
       
   440          * @return  ETrue if message type is valid, else EFalse
       
   441          */
       
   442         TBool ValidateMsgType( CNATFWUNSAFMessage* aMsg ) const;
       
   443 
       
   444     private: // Data
       
   445         
       
   446         // Holds copy of remote address for connect request and  for set active
       
   447         // destination request
       
   448         TInetAddr iRemoteAddr;
       
   449         
       
   450         // Type of request
       
   451         TInt iRequestType;
       
   452         
       
   453         // Pointer to timer value
       
   454         TUint32* iTimerValue;
       
   455         
       
   456     private: // For testing purposes
       
   457 
       
   458 #ifdef TEST_EUNIT
       
   459         friend class CSTUNClientTest;
       
   460         friend class CSTUNClientImplementationTest;
       
   461         friend class CSTUNBindingTest;
       
   462         friend class CSTUNBindingImplementationTest;
       
   463 #endif
       
   464     };
       
   465 
       
   466 #endif // CSTUNRELAYBINDINGIMPLEMENTATION_H
       
   467 
       
   468 // End of File