natfw/natfwicecandidatehandler/inc/ciceconnectionhandler.h
changeset 0 1bce908db942
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     1 /*
       
     2 * Copyright (c) 2006-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 
       
    22 #ifndef CNATFWCONNECTIONHANDLER_H
       
    23 #define CNATFWCONNECTIONHANDLER_H
       
    24 
       
    25 
       
    26 #include <e32base.h>
       
    27 #include <in_sock.h>
       
    28 #include "mncmconnectionobserver.h"
       
    29 #include "natfwstunclientobserver.h"
       
    30 #include "mncmincomingconnectionobserver.h"
       
    31 #include "micenatplugineventobs.h"
       
    32 #include "icecandidatehandlerdefs.h"
       
    33 #include "natfwcandidate.h"
       
    34 #include "natfwinternaldefs.h"    //UNIT_TEST
       
    35 
       
    36 class TInetAddr;
       
    37 class CSTUNClient;
       
    38 class CSTUNBinding;
       
    39 class MNcmConnectionMultiplexer;
       
    40 class CIceSessionData;
       
    41 class MIceConnCheckObserver;
       
    42 class MIceConnHandlerObserver;
       
    43 class TBindingData;
       
    44 class CIceNatPluginContainer;
       
    45 class CNATFWCandidatePair;
       
    46 
       
    47 /**
       
    48  *  Hides connection handling related tasks from rest of ICE plugin.
       
    49  *
       
    50  *  @lib icecandidatehandler.dll
       
    51  *  @since S60 v3.2
       
    52  */
       
    53 class CIceConnectionHandler : public CBase,
       
    54                                 public MSTUNClientObserver,
       
    55                                 public MNcmIncomingConnectionObserver,
       
    56                                 public MNcmConnectionObserver,
       
    57                                 public MIceNatPluginEventObs
       
    58     {
       
    59     
       
    60     UNIT_TEST( UT_CIceConnectionHandler )
       
    61     
       
    62 public:
       
    63 
       
    64     /** ICE connection type */
       
    65     enum TIceConnectionType
       
    66         {
       
    67         EIceConnTypeHost    = 1,
       
    68         EIceConnTypeTurn    = 2
       
    69         };
       
    70         
       
    71     /**
       
    72      * Two-phased constructor.
       
    73      * @param   aMultiplexer        Multiplexer
       
    74      * @param   aEventObserver      Observer for events
       
    75      * @param   aSessionData        Session data
       
    76      * @param   aPluginContainer    Plugin container
       
    77      * @param   aConnHandlerObs     Observer for the connection handler
       
    78      */
       
    79     static CIceConnectionHandler* NewL(
       
    80         MNcmConnectionMultiplexer& aMultiplexer,
       
    81         MIceNatPluginEventObs& aEventObserver,
       
    82         CIceSessionData& aSessionData,
       
    83         CIceNatPluginContainer& aPluginContainer,
       
    84         MIceConnHandlerObserver& aConnHandlerObs );
       
    85 
       
    86     /**
       
    87      * Two-phased constructor.
       
    88      * @param   aMultiplexer        Multiplexer
       
    89      * @param   aEventObserver      Observer for events
       
    90      * @param   aSessionData        Session data
       
    91      * @param   aPluginContainer    Plugin container
       
    92      * @param   aConnHandlerObs     Observer for the connection handler
       
    93      */
       
    94     static CIceConnectionHandler* NewLC(
       
    95         MNcmConnectionMultiplexer& aMultiplexer,
       
    96         MIceNatPluginEventObs& aEventObserver,
       
    97         CIceSessionData& aSessionData,
       
    98         CIceNatPluginContainer& aPluginContainer,
       
    99         MIceConnHandlerObserver& aConnHandlerObs );
       
   100     
       
   101     /**
       
   102      * Destructor.
       
   103      */
       
   104     virtual ~CIceConnectionHandler();
       
   105 
       
   106     /**
       
   107      * Enables/disables media receiving for the specified local candidate.
       
   108      * 
       
   109      * @since   S60 v3.2
       
   110      * @param   aLocalCandidate The local end point for a media
       
   111      * @param   aState          The receiving state
       
   112      */
       
   113     void SetReceivingStateL( const CNATFWCandidate& aLocalCandidate,
       
   114         TNATFWStreamingState aState );
       
   115 
       
   116     /**
       
   117      * Enables/disables media sending from the specified local candidate.
       
   118      * 
       
   119      * @since   S60 v3.2
       
   120      * @param   aLocalCandidate The local end point for a media
       
   121      * @param   aState          The sending state
       
   122      * @param   aDestAddr       The remote end point for a media
       
   123      */        
       
   124     void SetSendingStateL( const CNATFWCandidate& aLocalCandidate,
       
   125         TNATFWStreamingState aState, const TInetAddr& aDestAddr );
       
   126 
       
   127     /**
       
   128      * Gets identifier for a connection that is associated with the
       
   129      * given local end point.
       
   130      * 
       
   131      * @since   S60 v3.2
       
   132      * @param   aLocalCandidate     The local end point for a media
       
   133      * @param   aConnectionId       Reference to found id
       
   134      */        
       
   135     void GetConnectionIdL( const CNATFWCandidate& aLocalCandidate,
       
   136         TUint& aConnectionId );     
       
   137     
       
   138     /**
       
   139      * Creates a new connection for the specified candidate pair.
       
   140      *
       
   141      * @since   S60 3.2
       
   142      * @param   aPair               The candidate pair
       
   143      * @return  Connection identifier
       
   144      */
       
   145     TUint CreateConnectionL( const CNATFWCandidatePair& aPair );
       
   146     
       
   147     /**
       
   148      * Removes specified connection.
       
   149      *
       
   150      * @since   S60 3.2
       
   151      * @param   aStreamId           The stream identifier
       
   152      * @param   aConnectionId       The connection identifier
       
   153      */
       
   154     void RemoveConnection( TUint aStreamId, TUint aConnectionId );
       
   155     
       
   156     /**
       
   157      * Performs connectivity check using specified connection.
       
   158      * MIceConnectionHandlerObserver::ConnCheckCompleted is
       
   159      * called after completion of check.
       
   160      *
       
   161      * @since   S60 3.2
       
   162      * @param   aStreamId       The stream identifier
       
   163      * @param   aConnectionId   The connection identifier
       
   164      * @param   aPriority       The value of PRIORITY attribute
       
   165      * @param   aUseCandidate   Whether to put USE-CANDIDATE to request
       
   166      * @param   aRtoValue       The retransmission timeout value in ms
       
   167      * @param   aDestAddr       The remote address where to send check
       
   168      * @param   aObserver       Connectivity check observer
       
   169      */
       
   170     void PerformConnCheckL( TUint aStreamId, TUint aConnectionId, 
       
   171         TUint aPriority, TBool aUseCandidate, TUint aRtoValue,
       
   172         const TInetAddr& aDestAddr,
       
   173         MIceConnCheckObserver& aObserver );
       
   174     
       
   175     /**
       
   176      * Cancels ongoing connectivity check.
       
   177      *
       
   178      * @since   S60 3.2
       
   179      * @param   aStreamId           The stream identifier
       
   180      * @param   aConnectionId       The connection identifier
       
   181      * @param   aDestAddr           Destination address of the check
       
   182      */
       
   183     void CancelCheck( TUint aStreamId, TUint aConnectionId,
       
   184         const TInetAddr& aDestAddr );
       
   185     
       
   186     /**
       
   187      * Starts fetching local(host, server reflexive, relay) candidates.
       
   188      *
       
   189      * @since S60 3.2
       
   190      * @param aStreamCollectionId   Stream collection identifier
       
   191      * @param aStreamId             Stream identifier
       
   192      * @param aComponentId          Stream collection identifier
       
   193      */
       
   194     void FetchCandidatesL( TUint aStreamCollectionId, TUint aStreamId, 
       
   195         TUint aComponentId );
       
   196 
       
   197     /**
       
   198      * Releases plugins.
       
   199      *
       
   200      * @since   S60 3.2
       
   201      * @param   aRetainRelay          Whether relay should be kept up
       
   202      */
       
   203     void ReleasePlugins( TBool aRetainRelay );
       
   204     
       
   205 // from base class MSTUNClientObserver
       
   206 
       
   207     /**
       
   208      * From MSTUNClientObserver.
       
   209      * Indicates that asynchronous initialization of the related
       
   210      * CSTUNClient has been completed and it can be used
       
   211      * for creating CSTUNBinding(s) or CSTUNRelayBinding(s).
       
   212      *
       
   213      * @param   aClient the client for which the initialization 
       
   214      *          was completed.
       
   215      * @param   aCompletionCode value KErrNone indicates that 
       
   216      *          the client instance is ready for creating CSTUNBinding(s).
       
   217      *          Any other completion code indicates that 
       
   218      *          the client instance cannot be used and should be deleted.
       
   219      */
       
   220     void STUNClientInitCompleted( 
       
   221         const CSTUNClient& aClient,
       
   222         TInt aCompletionCode );
       
   223 
       
   224     /**
       
   225      * From MSTUNClientObserver.
       
   226      * A STUN/STUN Relay binding related event occurred. After this callback
       
   227      * the aBinding can be used again by calling its SendRequestL or 
       
   228      * AllocateRequestL function.
       
   229      *
       
   230      * @param   aEvent the STUN binding related event
       
   231      * @param   aBinding the STUN binding that is the target for the event.
       
   232      */
       
   233     void STUNBindingEventOccurredL( 
       
   234         TSTUNBindingEvent aEvent,
       
   235         const CBinding& aBinding );
       
   236 
       
   237     /**
       
   238      * From MSTUNClientObserver.
       
   239      * Error occurred during IP address resolution.
       
   240      * This callback will also be called if STUNBindingEventOccuredL leaves.
       
   241      *
       
   242      * @param   aBinding a binding for which the error occured
       
   243      * @param   aError error code.
       
   244      */
       
   245     void STUNBindingErrorOccurred( 
       
   246         const CBinding& aBinding,
       
   247         TInt aError );
       
   248 
       
   249 // from base class MNcmConnectionObserver
       
   250 
       
   251     void ConnectionNotify( TUint aStreamId, TUint aConnectionId,
       
   252         MNcmConnectionObserver::TConnectionNotifyType aType, TInt aError );
       
   253         
       
   254 // from base class MNcmIncomingConnectionObserver
       
   255 
       
   256     void IncomingMessageL( TUint aStreamId, const TDesC8& aMessage,
       
   257         const TInetAddr& aLocalAddr, const TInetAddr& aFromAddr,
       
   258         const TInetAddr& aPeerRemoteAddress, TBool& aConsumed );
       
   259 
       
   260 // from base class MIceNatPluginEventObs
       
   261 
       
   262     void PluginEventOccured( const CNATFWPluginApi* aPlugin, 
       
   263         TUint aStreamId, TNatPluginEvent aEventCode, 
       
   264         TInt aErrorCode, TAny* aEventData, TUint aStreamConnectionId = 0,
       
   265         TUint aComponentId = 0, TBool aIPv6After = EFalse );
       
   266 
       
   267 private:
       
   268 
       
   269     CIceConnectionHandler(
       
   270         MNcmConnectionMultiplexer& aMultiplexer,
       
   271         MIceNatPluginEventObs& aEventObserver,
       
   272         CIceSessionData& aSessionData,
       
   273         CIceNatPluginContainer& aPluginContainer,
       
   274         MIceConnHandlerObserver& aConnHandlerObs );
       
   275     
       
   276     void ConstructL();
       
   277     
       
   278     TInt FindBindingIndex( TUint aStreamId, TUint aConnectionId ) const;
       
   279     
       
   280     TInt FindBindingIndex( const CBinding& aBinding ) const;
       
   281     
       
   282     TInt FindBindingIndex( TUint aStreamId, TUint aConnectionId,
       
   283         const TInetAddr& aDestAddr ) const;
       
   284 
       
   285     CNATFWPluginApi* SolvePluginL( const CNATFWCandidate& aLocalCandidate ); 
       
   286     
       
   287     void FreeResources();
       
   288     
       
   289     void ExecuteCallbackL( TInt aIndex, TInt aError );
       
   290     
       
   291     void DoRemoveConnection( TUint aStreamId, TUint aConnectionId, 
       
   292         TBool aRemoveMuxConn );
       
   293 
       
   294 private: // data
       
   295 
       
   296     /**
       
   297      * Multiplexer
       
   298      * Not own.
       
   299      */
       
   300     MNcmConnectionMultiplexer& iMultiplexer;
       
   301     
       
   302     /**
       
   303      * ICE plugin observer
       
   304      * Not own.
       
   305      */
       
   306     MIceNatPluginEventObs& iEventObserver;
       
   307     
       
   308     /**
       
   309      * The session data
       
   310      */
       
   311     CIceSessionData& iSessionData;
       
   312     
       
   313     /**
       
   314      * NAT protocol plugin container
       
   315      * Not own.
       
   316      */
       
   317     CIceNatPluginContainer& iPluginContainer;
       
   318     
       
   319     /**
       
   320      * Notifies about closed streams
       
   321      */
       
   322     MIceConnHandlerObserver& iConnHandlerObs;
       
   323     
       
   324     /**
       
   325      * Stun client.
       
   326      * Own.
       
   327      */
       
   328     CSTUNClient* iStunClient;
       
   329     
       
   330     /**
       
   331      * Instance for timer services.
       
   332      * Own.
       
   333      */
       
   334     CDeltaTimer* iTimerServices;
       
   335     
       
   336     /**
       
   337      * Binding data array
       
   338      */
       
   339     RArray<TBindingData> iBindings;
       
   340     };
       
   341 
       
   342 class TBindingData
       
   343     {
       
   344 public:
       
   345 // parametrized ctor
       
   346     inline TBindingData( CSTUNBinding* aBinding, 
       
   347         CIceConnectionHandler::TIceConnectionType aConnType,
       
   348         TUint aStreamId, TUint aConnectionId, 
       
   349         const TInetAddr& aDestAddr ) :
       
   350         iBinding( aBinding ), 
       
   351         iConnType( aConnType ),
       
   352         iStreamId( aStreamId ), 
       
   353         iConnectionId( aConnectionId ), iDestAddr( aDestAddr ),
       
   354         iObserver( NULL ),
       
   355         iStreamingStatus( EStreamingStatePassive ),
       
   356         iLocalAddr( KAFUnspec ), iFromAddr( KAFUnspec ),
       
   357         iPeerAddr( KAFUnspec ), iMappedAddr( KAFUnspec ),
       
   358         iCheckOngoing( EFalse ), iRtoValue( 0 ), iCancelled( EFalse )
       
   359         {
       
   360         }
       
   361 
       
   362 public:
       
   363     
       
   364     /**
       
   365      * Stun binding instance.
       
   366      * Own.
       
   367      */
       
   368     CSTUNBinding* iBinding;
       
   369     
       
   370     /**
       
   371      * Connection type.
       
   372      */
       
   373     CIceConnectionHandler::TIceConnectionType iConnType;
       
   374 
       
   375     /**
       
   376      * ID of used stream.
       
   377      */
       
   378     TUint iStreamId;
       
   379     
       
   380     /**
       
   381      * ID of used connection.
       
   382      */
       
   383     TUint iConnectionId;
       
   384     
       
   385     /**
       
   386      * Destination address, where to send.
       
   387      */
       
   388     TInetAddr iDestAddr;
       
   389     
       
   390     /**
       
   391      * For notifying client.
       
   392      */
       
   393     MIceConnCheckObserver* iObserver;
       
   394     
       
   395     /**
       
   396      * Stream status.
       
   397      */
       
   398     TNATFWStreamingState iStreamingStatus;
       
   399     
       
   400     /**
       
   401      * local address.
       
   402      */
       
   403     TInetAddr iLocalAddr;
       
   404     
       
   405     /**
       
   406      * Where message came from ( can be relay address ).
       
   407      */
       
   408     TInetAddr iFromAddr;
       
   409     
       
   410     /**
       
   411      * Peer address ( not relay, but real peer address ).
       
   412      */
       
   413     TInetAddr iPeerAddr;
       
   414 
       
   415     /**
       
   416      * Result of binding request, public ip address.
       
   417      */
       
   418     TInetAddr iMappedAddr;
       
   419     
       
   420     /**
       
   421      * Indicates whether check is in progress for this binding.
       
   422      */
       
   423     TBool iCheckOngoing;
       
   424     
       
   425     /**
       
   426      * Retransmission timeout for the binding.
       
   427      */
       
   428     TUint iRtoValue;
       
   429 
       
   430     /**
       
   431      * Indicates that check is cancelled.
       
   432      */    
       
   433     TBool iCancelled;
       
   434     };
       
   435     
       
   436 
       
   437 #endif // CNATFWCONNECTIONHANDLER_H