natfw/natfwicecandidatehandler/src/ciceplugin.cpp
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 #include <e32math.h>
       
    22 #include "natfwcandidate.h"
       
    23 #include "ciceplugin.h"
       
    24 #include "cicesessiondata.h"
       
    25 #include "cicelocalcandidatefinder.h"
       
    26 #include "ciceconnectionhandler.h"
       
    27 #include "cicecheckhandler.h"
       
    28 #include "cicestreamcollection.h"
       
    29 #include "cicenatplugincontainer.h"
       
    30 #include "icecandidatehandlerlogs.h"
       
    31 
       
    32 // ======== MEMBER FUNCTIONS ========
       
    33 
       
    34 CIcePlugin::CIcePlugin()
       
    35     {
       
    36     __ICEDP( "CIcePlugin::CIcePlugin" )
       
    37     }
       
    38 
       
    39 
       
    40 void CIcePlugin::ConstructL( TAny* aInitParams )
       
    41     {
       
    42     __ICEDP( "CIcePlugin::ConstructL" )
       
    43     __ASSERT_ALWAYS( aInitParams, User::Leave( KErrArgument ) );
       
    44     
       
    45     iPluginId = KNatPluginIdNokiaIce().AllocL();
       
    46     
       
    47     CNATFWPluginApi::TNATFWPluginInitParams* initParams = 
       
    48         static_cast<CNATFWPluginApi::TNATFWPluginInitParams*>
       
    49         ( aInitParams );
       
    50    
       
    51     iPluginObserver = &(initParams->iObserver);
       
    52     
       
    53     iSessionData = CIceSessionData::NewL(
       
    54         initParams->iDomain, initParams->iIapId );
       
    55     
       
    56     iPluginContainer = CIceNatPluginContainer::NewL( 
       
    57         *iSessionData, initParams->iMultiplexer );
       
    58     
       
    59     iPluginContainer->RegObserverForEventL( *this, 
       
    60         MIceNatPluginEventObs::EGeneralError );
       
    61     iPluginContainer->RegObserverForEventL( *this, 
       
    62         MIceNatPluginEventObs::EServerConnected );
       
    63     
       
    64     iCandidateFinder = CIceLocalCandidateFinder::NewL( 
       
    65         *this, *iSessionData, *iPluginContainer );
       
    66     
       
    67     iConnectionHandler = CIceConnectionHandler::NewL(
       
    68         initParams->iMultiplexer, *this, *iSessionData, 
       
    69         *iPluginContainer, *this );
       
    70     
       
    71     iCheckHandler = CIceCheckHandler::NewL( *iSessionData,
       
    72         initParams->iMultiplexer, *iConnectionHandler, *this );
       
    73     }
       
    74 
       
    75 
       
    76 CIcePlugin* CIcePlugin::NewL( TAny* aInitParams )
       
    77     {
       
    78     __ICEDP( "CIcePlugin::NewL" )
       
    79     
       
    80     CIcePlugin* self = new ( ELeave ) CIcePlugin();  
       
    81     CleanupStack::PushL(self);
       
    82     self->ConstructL( aInitParams );
       
    83     CleanupStack::Pop(self);
       
    84     
       
    85     return self;
       
    86     }
       
    87 
       
    88 
       
    89 CIcePlugin::~CIcePlugin()
       
    90     {
       
    91     __ICEDP( "CIcePlugin::~CIcePlugin" )
       
    92     
       
    93     delete iPluginId;
       
    94     delete iCheckHandler;
       
    95     delete iSessionData;
       
    96     
       
    97     if ( iPluginContainer )
       
    98         {
       
    99         iPluginContainer->UnregObserver( *this );
       
   100         delete iPluginContainer;
       
   101         }
       
   102     
       
   103     delete iCandidateFinder;
       
   104     delete iConnectionHandler;
       
   105     
       
   106     iPluginObserver = NULL;
       
   107     }
       
   108 
       
   109 
       
   110 // From CNATFWPluginApi
       
   111 
       
   112 // ---------------------------------------------------------------------------
       
   113 // From class CNATFWPluginApi.
       
   114 // ---------------------------------------------------------------------------
       
   115 //    
       
   116 void CIcePlugin::ConnectServerL( const RSocketServ& aSocketServ,
       
   117         const TName& aConnectionName )
       
   118     {
       
   119     __ICEDP( "CIcePlugin::ConnectServerL start" )
       
   120     iPluginContainer->ConnectPluginsL( aSocketServ, aConnectionName );
       
   121     __ICEDP( "CIcePlugin::ConnectServerL end" )
       
   122     }
       
   123 
       
   124 
       
   125 // ---------------------------------------------------------------------------
       
   126 // From class CNATFWPluginApi.
       
   127 // ---------------------------------------------------------------------------
       
   128 //
       
   129 void CIcePlugin::FetchCandidatesL( TUint aStreamCollectionId,
       
   130     TUint aStreamId, TUint aComponentId, TUint aAddrFamily )
       
   131     {
       
   132     __ICEDP( "CIcePlugin::FetchCandidatesL" )
       
   133 
       
   134     // Save collection identifiers
       
   135     iSessionData->CreateCollectionL( aStreamCollectionId );
       
   136     iSessionData->CollectionL( aStreamCollectionId )
       
   137         ->AddMediaComponentL( aStreamId, aComponentId );
       
   138 
       
   139     const TInt KTaTimerValue( 20 );
       
   140     iSessionData->SetTaTimerValue( KTaTimerValue );
       
   141 
       
   142     // Both ipv4 and ipv6 candidates will be fetched. Ipv6 candidates will be
       
   143     // fetched after ipv4 candidate fetching has completed
       
   144     if ( KAFUnspec == aAddrFamily )
       
   145         {
       
   146         TRAPD( err, iCandidateFinder->FetchCandidatesL(
       
   147             aStreamCollectionId, aStreamId, aComponentId, KAfInet, ETrue ) );
       
   148         if ( KErrNone != err )
       
   149             {
       
   150             iCandidateFinder->FetchCandidatesL( aStreamCollectionId,
       
   151                 aStreamId, aComponentId, KAfInet6, EFalse );
       
   152             }
       
   153         }
       
   154 
       
   155     // Desired address family only
       
   156     else
       
   157         {
       
   158         iCandidateFinder->FetchCandidatesL(
       
   159             aStreamCollectionId, aStreamId, aComponentId, aAddrFamily, EFalse );
       
   160         }
       
   161     }
       
   162 
       
   163 
       
   164 // ---------------------------------------------------------------------------
       
   165 // From class CNATFWPluginApi.
       
   166 // ---------------------------------------------------------------------------
       
   167 //
       
   168 void CIcePlugin::SetRoleL( TNATFWIceRole aRole )
       
   169     {
       
   170     __ICEDP( "CIcePlugin::SetRoleL" )
       
   171     __ASSERT_ALWAYS( 
       
   172         EIceRoleControlling == aRole || EIceRoleControlled == aRole,
       
   173         User::Leave( KErrArgument ) );
       
   174     
       
   175     iSessionData->SetRole( aRole );
       
   176     iSessionData->SetTieBreaker(
       
   177         TUint64( Math::Random() ) + TUint64( Math::Random() ) );
       
   178     }
       
   179 
       
   180 
       
   181 // ---------------------------------------------------------------------------
       
   182 // From class CNATFWPluginApi.
       
   183 // Set credentials for message signing and authentication.
       
   184 // This plugin supports only stream specific credentials.
       
   185 // ---------------------------------------------------------------------------
       
   186 //
       
   187 void CIcePlugin::SetCredentialsL( const CNATFWCandidate& aCandidate, 
       
   188         const CNATFWCredentials& aCredentials )
       
   189     {
       
   190     __ICEDP( "CIcePlugin::SetCredentialsL" )
       
   191     
       
   192     const_cast<CNATFWCredentials&>( aCredentials )
       
   193         .SetStreamId( aCandidate.StreamId() );
       
   194     
       
   195     iSessionData->AddCredentialsL( aCredentials );
       
   196     iCheckHandler->SetCredentialsL( aCredentials );
       
   197     }
       
   198 
       
   199 
       
   200 // ---------------------------------------------------------------------------
       
   201 // From class CNATFWPluginApi.
       
   202 // Credentials must be set before starting connectivity checks.
       
   203 // ---------------------------------------------------------------------------
       
   204 //
       
   205 void CIcePlugin::PerformConnectivityChecksL( 
       
   206         RPointerArray<CNATFWCandidate>& aRemoteCandidates )
       
   207     {
       
   208     __ICEDP( "CIcePlugin::PerformConnectivityChecksL" )
       
   209     
       
   210     iSessionData->RemoveRemoteCandidates();
       
   211     iSessionData->AddRemoteCandidatesL( aRemoteCandidates );
       
   212     
       
   213     const TInt KTaTimerValue( 20 );
       
   214     iSessionData->SetTaTimerValue( KTaTimerValue );
       
   215     
       
   216     iCheckHandler->PerformConnectivityChecksL();
       
   217     }
       
   218 
       
   219 
       
   220 // ---------------------------------------------------------------------------
       
   221 // From class CNATFWPluginApi.
       
   222 // Peer selected pairs should be known by local agent. Peer reflexive remote
       
   223 // candidate is added to the known remote candidate list when receiving check
       
   224 // from peer. Peer MUST not include new candidates when it has completed
       
   225 // ICE processing.
       
   226 // ---------------------------------------------------------------------------
       
   227 //
       
   228 void CIcePlugin::UpdateIceProcessingL( 
       
   229         RPointerArray<CNATFWCandidatePair>& aPeerSelectedPairs )
       
   230     {
       
   231     __ICEDP( "CIcePlugin::UpdateIceProcessingL, PEERSELECTEDPAIRS" )
       
   232     __ASSERT_ALWAYS( 0 != aPeerSelectedPairs.Count(),
       
   233         User::Leave( KErrArgument ) );
       
   234     
       
   235     iCheckHandler->UpdateIceProcessingL( aPeerSelectedPairs );
       
   236     }
       
   237 
       
   238 
       
   239 // ---------------------------------------------------------------------------
       
   240 // From class CNATFWPluginApi.
       
   241 // CIcePlugin::UpdateIceProcessingL
       
   242 // ---------------------------------------------------------------------------
       
   243 //
       
   244 void CIcePlugin::UpdateIceProcessingL( 
       
   245         RPointerArray<CNATFWCandidate>& aRemoteCands )
       
   246     {
       
   247     __ICEDP( "CIcePlugin::UpdateIceProcessingL, REMOTECANDS" )
       
   248     __ASSERT_ALWAYS( 0 != aRemoteCands.Count(),
       
   249         User::Leave( KErrArgument ) );
       
   250     
       
   251     iSessionData->AddRemoteCandidatesL( aRemoteCands );
       
   252     iCheckHandler->UpdateIceProcessingL( aRemoteCands );
       
   253     }
       
   254 
       
   255 
       
   256 // ---------------------------------------------------------------------------
       
   257 // From class CNATFWPluginApi.
       
   258 // CIcePlugin::SetReceivingStateL
       
   259 // ---------------------------------------------------------------------------
       
   260 //
       
   261 void CIcePlugin::SetReceivingStateL( 
       
   262         const CNATFWCandidate& aLocalCandidate,
       
   263         TNATFWStreamingState aState )
       
   264     {
       
   265     iConnectionHandler->SetReceivingStateL( aLocalCandidate, aState );
       
   266     }
       
   267 
       
   268 
       
   269 // ---------------------------------------------------------------------------
       
   270 // From class CNATFWPluginApi.
       
   271 // CIcePlugin::SetSendingStateL
       
   272 // ---------------------------------------------------------------------------
       
   273 //
       
   274 void CIcePlugin::SetSendingStateL( 
       
   275         const CNATFWCandidate& aLocalCandidate,
       
   276         TNATFWStreamingState aState, const TInetAddr& aDestAddr )
       
   277     {
       
   278     iConnectionHandler->SetSendingStateL( aLocalCandidate, aState, aDestAddr );
       
   279     }
       
   280 
       
   281 
       
   282 // ---------------------------------------------------------------------------
       
   283 // From class CNATFWPluginApi.
       
   284 // CIcePlugin::GetConnectionIdL
       
   285 // ---------------------------------------------------------------------------
       
   286 //
       
   287 void CIcePlugin::GetConnectionIdL( 
       
   288         const CNATFWCandidate& aLocalCandidate,
       
   289         TUint& aConnectionId )
       
   290     {
       
   291     iConnectionHandler->GetConnectionIdL( aLocalCandidate, aConnectionId );
       
   292     }
       
   293 
       
   294 
       
   295 // ---------------------------------------------------------------------------
       
   296 // From class CNATFWPluginApi.
       
   297 // CIcePlugin::PluginIdentifier
       
   298 // ---------------------------------------------------------------------------
       
   299 //
       
   300 const TDesC8& CIcePlugin::PluginIdentifier() const
       
   301     {
       
   302     return *iPluginId;
       
   303     }
       
   304 
       
   305 
       
   306 // ---------------------------------------------------------------------------
       
   307 // CIcePlugin::StreamClosed
       
   308 // Stream collection can be deleted right away, when one media component has
       
   309 // been removed.
       
   310 // ---------------------------------------------------------------------------
       
   311 //
       
   312 void CIcePlugin::StreamClosed( TUint aStreamId )
       
   313     {
       
   314     TUint collectionId( 0 );
       
   315     TInt err = iSessionData->GetCollectionIdForComponent( 
       
   316         aStreamId, collectionId );
       
   317     
       
   318     if ( KErrNone == err )
       
   319         {
       
   320         iSessionData->CleanupStreamData( aStreamId );
       
   321         iCheckHandler->CleanupCollectionData( collectionId );
       
   322         iSessionData->DeleteCollection( collectionId );
       
   323         }
       
   324     }
       
   325 
       
   326 // ---------------------------------------------------------------------------
       
   327 // From class MIceNatPluginEventObs.
       
   328 // Dispatches events from specialized event listeners to the ICE-plugin user.
       
   329 //
       
   330 // Listens to general errors on real NAT-plugins. If unexpected error within
       
   331 // a stream in some plugin occurs, we cannot do anything but close the stream.
       
   332 // ---------------------------------------------------------------------------
       
   333 //
       
   334 void CIcePlugin::PluginEventOccured( 
       
   335         #ifdef _DEBUG
       
   336         const CNATFWPluginApi* aPlugin,
       
   337         #else
       
   338         const CNATFWPluginApi* /*aPlugin*/,
       
   339         #endif
       
   340         TUint aStreamId, 
       
   341         MIceNatPluginEventObs::TNatPluginEvent aEventCode, 
       
   342         TInt aErrorCode, TAny* aEventData, TUint aStreamConnectionId,
       
   343         TUint aComponentId, TBool aIPv6After )
       
   344     {
       
   345     __ICEDP( "CIcePlugin::PluginEventOccured" )
       
   346     
       
   347     switch ( aEventCode )
       
   348         {
       
   349         case MIceNatPluginEventObs::EServerConnected:
       
   350             {
       
   351             ASSERT( NULL != aPlugin );
       
   352             
       
   353             iConnectedServerCount++;
       
   354             if ( iConnectedServerCount == iPluginContainer->Count() )
       
   355                 {
       
   356                 iPluginObserver->Notify( *this, aStreamId, 
       
   357                     MNATFWPluginObserver::EServerConnected, KErrNone );
       
   358                 }
       
   359             }
       
   360             break;
       
   361                     
       
   362         case MIceNatPluginEventObs::ELocalCandidateFound:
       
   363             {
       
   364             ASSERT( NULL == aPlugin && NULL != aEventData );
       
   365             
       
   366             iPluginObserver->NewLocalCandidateFound( *this, 
       
   367                 reinterpret_cast<CNATFWCandidate*>( aEventData ) );
       
   368             }
       
   369             break;
       
   370         
       
   371         case MIceNatPluginEventObs::EFetchingCompleted:
       
   372             {
       
   373             ASSERT( NULL == aPlugin );
       
   374 
       
   375             if ( aIPv6After )
       
   376                 {
       
   377                 if ( KErrNone == aErrorCode )
       
   378                     {
       
   379                     iIpv4Succeeded = ETrue;
       
   380                     }
       
   381                     
       
   382                 TRAPD( err, iCandidateFinder->FetchCandidatesL(
       
   383                     aStreamConnectionId, aStreamId, aComponentId, 
       
   384                     KAfInet6, EFalse ) );
       
   385                     
       
   386                 if ( KErrNone != err )
       
   387                     {
       
   388                     iPluginObserver->Notify( *this, aStreamId, 
       
   389                         MNATFWPluginObserver::EFetchingCompleted, aErrorCode );
       
   390                     }
       
   391                 }
       
   392                 
       
   393             else
       
   394                 {
       
   395                 if ( iIpv4Succeeded )
       
   396                     {
       
   397                     aErrorCode = KErrNone;
       
   398                     iIpv4Succeeded = EFalse;
       
   399                     }
       
   400 
       
   401                 iPluginObserver->Notify( *this, aStreamId,
       
   402                     MNATFWPluginObserver::EFetchingCompleted, aErrorCode );
       
   403                 }
       
   404             }
       
   405             break;
       
   406             
       
   407         case MIceNatPluginEventObs::EReceivingActivated:
       
   408             {
       
   409             ASSERT( NULL == aPlugin );
       
   410             
       
   411             iPluginObserver->Notify( *this, aStreamId, 
       
   412                 MNATFWPluginObserver::EReceivingActivated, aErrorCode );
       
   413             }
       
   414             break;
       
   415             
       
   416         case MIceNatPluginEventObs::EReceivingDeactivated:
       
   417             {
       
   418             ASSERT( NULL == aPlugin );
       
   419             
       
   420             iPluginObserver->Notify( *this, aStreamId, 
       
   421                 MNATFWPluginObserver::EReceivingDeactivated, aErrorCode );
       
   422             }
       
   423             break;            
       
   424 
       
   425         case MIceNatPluginEventObs::ESendingActivated:
       
   426             {
       
   427             ASSERT( NULL == aPlugin );
       
   428             
       
   429             iPluginObserver->Notify( *this, aStreamId, 
       
   430                 MNATFWPluginObserver::ESendingActivated, aErrorCode );
       
   431             }
       
   432             break;
       
   433         
       
   434         case MIceNatPluginEventObs::ESendingDeactivated:
       
   435             {
       
   436             ASSERT( NULL == aPlugin );
       
   437             
       
   438             iPluginObserver->Notify( *this, aStreamId, 
       
   439                 MNATFWPluginObserver::ESendingDeactivated, aErrorCode );
       
   440             }
       
   441             break;
       
   442         
       
   443         case MIceNatPluginEventObs::ECandidatePairFound:
       
   444             {
       
   445             ASSERT( NULL == aPlugin && NULL != aEventData );
       
   446             
       
   447             iPluginObserver->NewCandidatePairFound( *this, 
       
   448                 reinterpret_cast<CNATFWCandidatePair*>( aEventData ) );
       
   449             }
       
   450             break;
       
   451 
       
   452         case MIceNatPluginEventObs::EConnChecksCompleted:
       
   453             {
       
   454             ASSERT( NULL == aPlugin );
       
   455             
       
   456             iPluginObserver->Notify( *this, aStreamId, 
       
   457                 MNATFWPluginObserver::EConnChecksCompleted, aErrorCode );
       
   458             }
       
   459             break;
       
   460 
       
   461         case MIceNatPluginEventObs::EGeneralError:
       
   462             {
       
   463             // this is only event from real NAT-plugin
       
   464             ASSERT( NULL != aPlugin );
       
   465             ASSERT( KErrNone != aErrorCode );
       
   466             
       
   467             iPluginObserver->Error( *this, aStreamId, aErrorCode );
       
   468             }
       
   469             break;
       
   470         
       
   471         default:
       
   472             
       
   473             __ICEDP( "CIcePlugin::PluginEventOccured, DEFAULT" )
       
   474             ASSERT( EFalse );
       
   475         }
       
   476     }