natfw/natfwicecandidatehandler/src/ciceconnectionhandler.cpp
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 #include <e32base.h>
       
    22 #include "natfwcredentials.h"
       
    23 #include "natfwcandidatepair.h"
       
    24 #include "mnatfwpluginobserver.h"
       
    25 #include "mncmconnectionmultiplexer.h"
       
    26 #include "ciceconnectionhandler.h"
       
    27 #include "natfwstunclient.h"
       
    28 #include "natfwstunbinding.h"
       
    29 #include "cicesessiondata.h"
       
    30 #include "miceconncheckobserver.h"
       
    31 #include "miceconnhandlerobserver.h"
       
    32 #include "ticenatplugincontaineriter.h"
       
    33 #include "icecandidatehandlerlogs.h"
       
    34 
       
    35 // CONSTANTS
       
    36 const TInt KRetransmitIntervalInMs = 250;
       
    37 
       
    38 // ======== MEMBER FUNCTIONS ========
       
    39 
       
    40 
       
    41 // ---------------------------------------------------------------------------
       
    42 // CIceConnectionHandler::CIceConnectionHandler
       
    43 // ---------------------------------------------------------------------------
       
    44 //
       
    45 CIceConnectionHandler::CIceConnectionHandler(
       
    46         MNcmConnectionMultiplexer& aMultiplexer,
       
    47         MIceNatPluginEventObs& aEventObserver,
       
    48         CIceSessionData& aSessionData,
       
    49         CIceNatPluginContainer& aPluginContainer,
       
    50         MIceConnHandlerObserver& aConnHandlerObs )
       
    51     :
       
    52     iMultiplexer( aMultiplexer ),
       
    53     iEventObserver( aEventObserver ),
       
    54     iSessionData( aSessionData ),
       
    55     iPluginContainer( aPluginContainer ),
       
    56     iConnHandlerObs( aConnHandlerObs )
       
    57     {
       
    58     __ICEDP( "CIceConnectionHandler::CIceConnectionHandler" )
       
    59     }
       
    60 
       
    61 
       
    62 // ---------------------------------------------------------------------------
       
    63 // CIceConnectionHandler::ConstructL
       
    64 // ---------------------------------------------------------------------------
       
    65 //
       
    66 void CIceConnectionHandler::ConstructL()
       
    67     {
       
    68     __ICEDP( "CIceConnectionHandler::ConstructL" )
       
    69     
       
    70     iTimerServices = CDeltaTimer::NewL( CActive::EPriorityStandard );
       
    71     
       
    72     // RTO value given here will be overridden when sending request
       
    73     iStunClient = CSTUNClient::NewL( KRetransmitIntervalInMs,
       
    74                                      *iTimerServices,
       
    75                                      *this,
       
    76                                      &iMultiplexer );
       
    77     
       
    78     iPluginContainer.RegObserverForEventL( *this, 
       
    79         MIceNatPluginEventObs::ESendingActivated );
       
    80     iPluginContainer.RegObserverForEventL( *this, 
       
    81         MIceNatPluginEventObs::ESendingDeactivated );
       
    82         
       
    83     iPluginContainer.RegObserverForEventL( *this, 
       
    84         MIceNatPluginEventObs::EReceivingActivated );
       
    85     iPluginContainer.RegObserverForEventL( *this, 
       
    86         MIceNatPluginEventObs::EReceivingDeactivated );        
       
    87     }
       
    88 
       
    89 
       
    90 // ---------------------------------------------------------------------------
       
    91 // CIceConnectionHandler::NewL
       
    92 // ---------------------------------------------------------------------------
       
    93 //
       
    94 CIceConnectionHandler* CIceConnectionHandler::NewL(
       
    95         MNcmConnectionMultiplexer& aMultiplexer,
       
    96         MIceNatPluginEventObs& aEventObserver,
       
    97         CIceSessionData& aSessionData,
       
    98         CIceNatPluginContainer& aPluginContainer,
       
    99         MIceConnHandlerObserver& aConnHandlerObs )
       
   100     {
       
   101     __ICEDP( "CIceConnectionHandler::NewL" )
       
   102 
       
   103     CIceConnectionHandler* self 
       
   104         = CIceConnectionHandler::NewLC( 
       
   105             aMultiplexer, aEventObserver, aSessionData, aPluginContainer,
       
   106             aConnHandlerObs );
       
   107             
       
   108     CleanupStack::Pop( self );
       
   109 
       
   110     return self;
       
   111     }
       
   112 
       
   113 
       
   114 // ---------------------------------------------------------------------------
       
   115 // CIceConnectionHandler::NewLC
       
   116 // ---------------------------------------------------------------------------
       
   117 //
       
   118 CIceConnectionHandler* CIceConnectionHandler::NewLC(
       
   119         MNcmConnectionMultiplexer& aMultiplexer,
       
   120         MIceNatPluginEventObs& aEventObserver,
       
   121         CIceSessionData& aSessionData,
       
   122         CIceNatPluginContainer& aPluginContainer,
       
   123         MIceConnHandlerObserver& aConnHandlerObs )
       
   124     {
       
   125     __ICEDP( "CIceConnectionHandler::NewLC" )
       
   126     
       
   127     CIceConnectionHandler* self 
       
   128         = new( ELeave ) CIceConnectionHandler( 
       
   129             aMultiplexer, aEventObserver, aSessionData, 
       
   130             aPluginContainer, aConnHandlerObs );
       
   131     CleanupStack::PushL( self );
       
   132     self->ConstructL();
       
   133     
       
   134     return self;
       
   135     }
       
   136 
       
   137 
       
   138 // ---------------------------------------------------------------------------
       
   139 // CIceConnectionHandler::~CIceConnectionHandler
       
   140 // ---------------------------------------------------------------------------
       
   141 //
       
   142 CIceConnectionHandler::~CIceConnectionHandler()
       
   143     {
       
   144     __ICEDP( "CIceConnectionHandler::~CIceConnectionHandler" )
       
   145     
       
   146     FreeResources();
       
   147     delete iStunClient;
       
   148     
       
   149     // timer services must be deleted AFTER STUN Client
       
   150     delete iTimerServices;
       
   151     
       
   152     iPluginContainer.UnregObserver( *this );
       
   153     }
       
   154 
       
   155 
       
   156 // ---------------------------------------------------------------------------
       
   157 // CIceConnectionHandler::SetReceivingStateL
       
   158 // ---------------------------------------------------------------------------
       
   159 //
       
   160 void CIceConnectionHandler::SetReceivingStateL( 
       
   161         const CNATFWCandidate& aLocalCandidate,
       
   162         TNATFWStreamingState aState )
       
   163     {
       
   164     __ICEDP( "CIceConnectionHandler::SetReceivingStateL" )
       
   165     
       
   166     CNATFWPluginApi* item = SolvePluginL( aLocalCandidate );
       
   167     item->SetReceivingStateL( aLocalCandidate, aState );
       
   168     }
       
   169 
       
   170 
       
   171 // ---------------------------------------------------------------------------
       
   172 // CIceConnectionHandler::SetSendingStateL
       
   173 // ---------------------------------------------------------------------------
       
   174 //
       
   175 void CIceConnectionHandler::SetSendingStateL( 
       
   176         const CNATFWCandidate& aLocalCandidate,
       
   177         TNATFWStreamingState aState, const TInetAddr& aDestAddr )
       
   178     {
       
   179     __ICEDP( "CIceConnectionHandler::SetSendingStateL" )
       
   180     
       
   181     CNATFWPluginApi* item = SolvePluginL( aLocalCandidate );
       
   182     item->SetSendingStateL( aLocalCandidate, aState, aDestAddr );
       
   183     }
       
   184 
       
   185 
       
   186 // ---------------------------------------------------------------------------
       
   187 // CIceConnectionHandler::GetConnectionIdL
       
   188 // ---------------------------------------------------------------------------
       
   189 //
       
   190 void CIceConnectionHandler::GetConnectionIdL( 
       
   191         const CNATFWCandidate& aLocalCandidate,
       
   192         TUint& aConnectionId )
       
   193     {
       
   194     __ICEDP( "CIceConnectionHandler::GetConnectionIdL" )
       
   195     
       
   196     CNATFWPluginApi* item = SolvePluginL( aLocalCandidate );
       
   197     item->GetConnectionIdL( aLocalCandidate, aConnectionId );
       
   198     }
       
   199 
       
   200 
       
   201 // ---------------------------------------------------------------------------
       
   202 // CIceConnectionHandler::CreateConnectionL
       
   203 // ---------------------------------------------------------------------------
       
   204 //
       
   205 TUint CIceConnectionHandler::CreateConnectionL( 
       
   206         const CNATFWCandidatePair& aPair )
       
   207     {
       
   208     __ICEDP( "CIceConnectionHandler::CreateConnectionL" )
       
   209     const CNATFWCandidate& localCandidate( aPair.LocalCandidate() );
       
   210     TUint streamId( localCandidate.StreamId() );
       
   211     
       
   212     iMultiplexer.RegisterConnectionObserverL( streamId, *this );
       
   213     iMultiplexer.RegisterIncomingConnectionObserverL( streamId, *this );
       
   214     
       
   215     TUint connectionId( 0 );
       
   216     if ( CNATFWCandidate::ERelay == localCandidate.Type() )
       
   217         {
       
   218         // turnconnectionid is used when utilizing TURN-plugin
       
   219         CNATFWPluginApi* plugin 
       
   220             = iPluginContainer.PluginByType( KNatPluginTypeTurn() );
       
   221         __ASSERT_ALWAYS( plugin, User::Leave( KErrNotFound ) );
       
   222         
       
   223         plugin->GetConnectionIdL( localCandidate, connectionId );
       
   224         }
       
   225     else
       
   226         {
       
   227         // create a new host connection for the candidate pair
       
   228         connectionId = iMultiplexer.CreateConnectionL( 
       
   229             streamId, localCandidate.TransportAddr() );
       
   230         }
       
   231     
       
   232     CSTUNBinding* binding = 
       
   233         CSTUNBinding::NewLC( *iStunClient, streamId, connectionId );
       
   234     
       
   235     TIceConnectionType type = 
       
   236         CNATFWCandidate::ERelay == aPair.LocalCandidate().Type() ?
       
   237             EIceConnTypeTurn : EIceConnTypeHost;
       
   238     
       
   239     TBindingData bindingData( binding, type,
       
   240         streamId, connectionId, aPair.RemoteCandidate().TransportAddr() );
       
   241     iBindings.AppendL( bindingData );
       
   242     CleanupStack::Pop( binding );
       
   243     
       
   244     return connectionId;
       
   245     }
       
   246 
       
   247 
       
   248 // ---------------------------------------------------------------------------
       
   249 // CIceConnectionHandler::RemoveConnection
       
   250 // ---------------------------------------------------------------------------
       
   251 //
       
   252 void CIceConnectionHandler::RemoveConnection( 
       
   253         TUint aStreamId, TUint aConnectionId )
       
   254     {
       
   255     __ICEDP( "CIceConnectionHandler::RemoveConnection" )
       
   256     
       
   257     DoRemoveConnection( aStreamId, aConnectionId, ETrue );
       
   258     }
       
   259 
       
   260 
       
   261 // ---------------------------------------------------------------------------
       
   262 // CIceConnectionHandler::PerformConnCheckL
       
   263 // ---------------------------------------------------------------------------
       
   264 //
       
   265 void CIceConnectionHandler::PerformConnCheckL( TUint aStreamId,
       
   266         TUint aConnectionId, TUint aPriority, TBool aUseCandidate,
       
   267         TUint aRtoValue, const TInetAddr& aDestAddr,
       
   268         MIceConnCheckObserver& aObserver )
       
   269     {
       
   270     __ICEDP( "CIceConnectionHandler::PerformConnCheckL" )
       
   271     
       
   272     const CNATFWCredentials* credential = 
       
   273         iSessionData.Credentials( 
       
   274             aStreamId, CNATFWCredentials::EOutbound );
       
   275     __ASSERT_ALWAYS( NULL != credential, User::Leave( KErrNotReady ) );
       
   276     __ASSERT_ALWAYS(
       
   277         credential->Username().Length(), User::Leave( KErrNotReady ) );
       
   278     __ASSERT_ALWAYS(
       
   279         credential->Password().Length(), User::Leave( KErrNotReady ) );
       
   280     
       
   281     iStunClient->SetCredentialsL( 
       
   282         credential->Username(), credential->Password() );
       
   283     
       
   284     TInt index( FindBindingIndex( aStreamId, aConnectionId, aDestAddr ) );
       
   285     __ASSERT_ALWAYS( KErrNotFound != index, User::Leave( index ) );
       
   286     iBindings[index].iObserver = &aObserver;
       
   287     
       
   288     TICEAttributes attributes( 
       
   289         aPriority, aUseCandidate, 
       
   290         ( EIceRoleControlled == iSessionData.Role() )
       
   291             ? iSessionData.TieBreaker() : TUint64( 0 ),
       
   292         ( EIceRoleControlling == iSessionData.Role() )
       
   293             ? iSessionData.TieBreaker() : TUint64( 0 ) );
       
   294     
       
   295     CSTUNBinding* stunBinding =
       
   296         static_cast<CSTUNBinding*>( iBindings[index].iBinding );
       
   297         
       
   298     stunBinding->SetICESpecificAttributes( attributes );
       
   299     iBindings[index].iCheckOngoing = ETrue;
       
   300     iBindings[index].iRtoValue = aRtoValue;
       
   301     
       
   302     if ( EStreamingStateActive != iBindings[index].iStreamingStatus )
       
   303         {
       
   304         if ( CIceConnectionHandler::EIceConnTypeTurn 
       
   305                 != iBindings[index].iConnType )
       
   306             {
       
   307             iMultiplexer.SetReceivingStateL( iBindings[index].iStreamId,
       
   308                 iBindings[index].iConnectionId, EStreamingStateActive );
       
   309             
       
   310             iMultiplexer.SetSendingStateL( iBindings[index].iStreamId, 
       
   311                 iBindings[index].iConnectionId,
       
   312                 iBindings[index].iDestAddr, EStreamingStateActive );
       
   313             // Wait for notify from multiplexer and after that call 
       
   314             // SendRequestL for this connection.
       
   315             }
       
   316         else
       
   317             {
       
   318             stunBinding->SendRequestL( 
       
   319                 iBindings[index].iDestAddr, ETrue, 
       
   320                 iBindings[index].iRtoValue );
       
   321             }
       
   322         }
       
   323     else
       
   324         {
       
   325         stunBinding->SendRequestL( 
       
   326             iBindings[index].iDestAddr, ETrue, iBindings[index].iRtoValue  );
       
   327         }
       
   328     }
       
   329 
       
   330 
       
   331 // ---------------------------------------------------------------------------
       
   332 // CIceConnectionHandler::CancelCheck
       
   333 // ---------------------------------------------------------------------------
       
   334 //
       
   335 void CIceConnectionHandler::CancelCheck( TUint aStreamId, 
       
   336         TUint aConnectionId, const TInetAddr& aDestAddr )
       
   337     {
       
   338     __ICEDP( "CIceConnectionHandler::CancelCheck" )
       
   339     
       
   340     TInt index( FindBindingIndex( aStreamId, aConnectionId, aDestAddr ) );
       
   341     if ( KErrNotFound != index  )
       
   342         {
       
   343         CSTUNBinding* stunBinding =
       
   344             static_cast<CSTUNBinding*>( iBindings[index].iBinding );
       
   345         if ( EStreamingStateActive == iBindings[index].iStreamingStatus ||
       
   346              EIceConnTypeTurn == iBindings[index].iConnType )
       
   347             {
       
   348             // Previous transaction is fully terminated because STUN binding
       
   349             // does not currently support simultaneous transactions.
       
   350             stunBinding->CancelRequest();
       
   351             }
       
   352         else
       
   353             {
       
   354             // mux connection not yet activated
       
   355             __ICEDP( "CIceConnectionHandler::CancelCheck, NO ACTIVE CONN" )
       
   356             iBindings[index].iCancelled = ETrue;
       
   357             }
       
   358         }
       
   359     else
       
   360         {
       
   361         __ICEDP( "CIceConnectionHandler::CancelCheck, ASSERT" )
       
   362         ASSERT( EFalse );
       
   363         }
       
   364     }
       
   365 
       
   366 
       
   367 // ---------------------------------------------------------------------------
       
   368 // CIceConnectionHandler::ReleasePlugins
       
   369 // ---------------------------------------------------------------------------
       
   370 //
       
   371 void CIceConnectionHandler::ReleasePlugins( TBool aRetainRelay )
       
   372     {
       
   373     __ICEDP( "CIceConnectionHandler::ReleasePlugins" )
       
   374     
       
   375     iPluginContainer.ReleasePlugins( aRetainRelay );
       
   376     }
       
   377 
       
   378 
       
   379 // ---------------------------------------------------------------------------
       
   380 // CIceConnectionHandler::STUNClientInitCompleted
       
   381 // ---------------------------------------------------------------------------
       
   382 //
       
   383 void CIceConnectionHandler::STUNClientInitCompleted( 
       
   384         const CSTUNClient& /*aClient*/,
       
   385         TInt /*aCompletionCode*/ )
       
   386     {
       
   387     __ICEDP( "CIceConnectionHandler::STUNClientInitCompleted" )
       
   388     // SHOULD NOT GET CALLED IN THIS IMPLEMENTATION
       
   389     ASSERT( EFalse );
       
   390     }
       
   391 
       
   392 
       
   393 // ---------------------------------------------------------------------------
       
   394 // CIceConnectionHandler::STUNBindingEventOccurredL
       
   395 // ---------------------------------------------------------------------------
       
   396 //
       
   397 void CIceConnectionHandler::STUNBindingEventOccurredL( 
       
   398         TSTUNBindingEvent aEvent, const CBinding& aBinding )
       
   399     {
       
   400     __ICEDP( "CIceConnectionHandler::STUNBindingEventOccurredL" )
       
   401     
       
   402     TInt index = FindBindingIndex( aBinding );
       
   403     User::LeaveIfError( index );
       
   404     
       
   405     // notify client about success of connectivity check with data got
       
   406     // in incoming message
       
   407     if ( EPublicAddressResolved == aEvent )
       
   408         {
       
   409         __ICEDP_ADDRLOG( "MAPPEDADDR:", aBinding.PublicAddr() )
       
   410         iBindings[index].iMappedAddr = aBinding.PublicAddr();
       
   411         ExecuteCallbackL( index, KErrNone );
       
   412         }
       
   413     else if ( ECredentialsRejected == aEvent )
       
   414         {
       
   415         iBindings[index].iMappedAddr.SetAddress( KAFUnspec );
       
   416         ExecuteCallbackL( index, KErrPermissionDenied );
       
   417         }
       
   418     else
       
   419         {
       
   420         __ICEDP( "CIceConnectionHandler::STUNBindingEventOccurredL, ELSE" )
       
   421         ASSERT( EFalse );
       
   422         }
       
   423     }
       
   424 
       
   425 
       
   426 // ---------------------------------------------------------------------------
       
   427 // CIceConnectionHandler::STUNBindingErrorOccurred
       
   428 // ---------------------------------------------------------------------------
       
   429 //
       
   430 void CIceConnectionHandler::STUNBindingErrorOccurred( 
       
   431         const CBinding& aBinding, TInt aError )
       
   432     {
       
   433     __ICEDP( "CIceConnectionHandler::STUNBindingErrorOccurred" )
       
   434     
       
   435     TInt index = FindBindingIndex( aBinding );
       
   436     if ( KErrNotFound != index )
       
   437         {
       
   438         TRAP_IGNORE( ExecuteCallbackL( index, aError ) )
       
   439         }
       
   440     else
       
   441         {
       
   442         __ICEDP( "CIceConnectionHandler::STUNBindingErrorOccurred, ELSE" )
       
   443         ASSERT( EFalse );
       
   444         }
       
   445     }
       
   446 
       
   447 
       
   448 // ---------------------------------------------------------------------------
       
   449 // CIceConnectionHandler::ConnectionNotify
       
   450 // ---------------------------------------------------------------------------
       
   451 //
       
   452 void CIceConnectionHandler::ConnectionNotify( 
       
   453         TUint aStreamId, TUint aConnectionId,
       
   454         MNcmConnectionObserver::TConnectionNotifyType aType, 
       
   455         TInt aError )
       
   456     {
       
   457     __ICEDP_INT1( 
       
   458         "CIceConnectionHandler::ConnectionNotify, ERR:", aError )
       
   459     
       
   460     TInt index( FindBindingIndex( aStreamId, aConnectionId ) );
       
   461     if ( KErrNotFound != index 
       
   462             && EIceConnTypeHost == iBindings[index].iConnType )
       
   463         {
       
   464         switch ( aType )
       
   465             {
       
   466             case MNcmConnectionObserver::EConnectionRemoved:
       
   467                 {
       
   468                 DoRemoveConnection( aStreamId, aConnectionId, EFalse );
       
   469                 iConnHandlerObs.StreamClosed( aStreamId );
       
   470                 }
       
   471                 break;
       
   472             
       
   473             case MNcmConnectionObserver::EConnectionError:
       
   474                 {
       
   475                 // Handled with error handling below
       
   476                 }
       
   477                 break;
       
   478             
       
   479             case MNcmConnectionObserver::EReceivingActivated:
       
   480                 {
       
   481                 // Wait for MNcmConnectionObserver::ESendingActivated
       
   482                 }
       
   483                 break;
       
   484             
       
   485             case MNcmConnectionObserver::EReceivingDeactivated:
       
   486                 {
       
   487                 // Wait for MNcmConnectionObserver::ESendingDeactivated
       
   488                 }
       
   489                 break;
       
   490            
       
   491             case MNcmConnectionObserver::ESendingActivated:
       
   492                 {
       
   493                 iBindings[index].iStreamingStatus = EStreamingStateActive;
       
   494                 
       
   495                 if ( iBindings[index].iCancelled )
       
   496                     {
       
   497                     // Connectivity check is now 'cancelled' and new request
       
   498                     // can be received.
       
   499                     iBindings[index].iCancelled = EFalse;
       
   500                     }
       
   501                 else
       
   502                     {
       
   503                     CSTUNBinding* stunBinding =
       
   504                         static_cast<CSTUNBinding*>( iBindings[index].iBinding );
       
   505                     TRAP_IGNORE( stunBinding->SendRequestL( 
       
   506                         iBindings[index].iDestAddr, ETrue, 
       
   507                         iBindings[index].iRtoValue ) )
       
   508                     }
       
   509                 }
       
   510                 break;
       
   511             
       
   512             case MNcmConnectionObserver::ESendingDeactivated:
       
   513                 {
       
   514                 iBindings[index].iStreamingStatus = EStreamingStatePassive;
       
   515                 }
       
   516                 break;
       
   517                 
       
   518             case MNcmConnectionObserver::EFirstMediaPacketSent:
       
   519                 {
       
   520                 }
       
   521                 break;    
       
   522             
       
   523             default:
       
   524                 ASSERT( EFalse );
       
   525             }
       
   526         
       
   527         if ( aError )
       
   528             {
       
   529             if ( iBindings[index].iCheckOngoing )
       
   530                 {
       
   531                 iBindings[index].iCheckOngoing = EFalse;
       
   532                 TRAP_IGNORE( ExecuteCallbackL( index, KErrUnknown ) )
       
   533                 }
       
   534             
       
   535             DoRemoveConnection( aStreamId, aConnectionId, ETrue );
       
   536             }
       
   537         }
       
   538     }
       
   539 
       
   540 
       
   541 // ---------------------------------------------------------------------------
       
   542 // CIceConnectionHandler::IncomingMessageL
       
   543 // For protocol message receiving.
       
   544 // ---------------------------------------------------------------------------
       
   545 //
       
   546 void CIceConnectionHandler::IncomingMessageL( TUint aStreamId,
       
   547         const TDesC8& aMessage, const TInetAddr& aLocalAddr,
       
   548         const TInetAddr& aFromAddr, const TInetAddr& aPeerAddr,
       
   549         TBool& aConsumed )
       
   550     {
       
   551     __ICEDP_ADDRLOG( 
       
   552         "CIceConnectionHandler::IncomingMessageL FROMADDR", aFromAddr )
       
   553     __ICEDP_ADDRLOG( 
       
   554         "CIceConnectionHandler::IncomingMessageL PEERADDR", aPeerAddr )
       
   555     
       
   556     TInt index( iBindings.Count() - 1 );
       
   557     __ASSERT_ALWAYS( 0 <= index, User::Leave( KErrNotReady ) );
       
   558     
       
   559     TInetAddr peerAddrFromIndication( KAFUnspec );
       
   560     HBufC8* indicationData( NULL );
       
   561     TInt consumingBindingInd( KErrNotFound );
       
   562     
       
   563     // offer message for every binding in the stream until consumed
       
   564     while ( ( KErrNotFound == consumingBindingInd ) && ( 0 <= index ) )
       
   565         {
       
   566         if ( iBindings[index].iStreamId == aStreamId )
       
   567             {
       
   568             CSTUNBinding* stunBinding = NULL;
       
   569             stunBinding = 
       
   570                 static_cast<CSTUNBinding*>( iBindings[index].iBinding );
       
   571             
       
   572             TBool consumed( EFalse );
       
   573             if ( indicationData )
       
   574                 {
       
   575                 stunBinding->HandleDataL( 
       
   576                     *indicationData, consumed, peerAddrFromIndication );
       
   577                 }
       
   578             else
       
   579                 {
       
   580                 indicationData = stunBinding->HandleDataL( 
       
   581                     aMessage, consumed, peerAddrFromIndication );
       
   582                 }
       
   583             
       
   584             if ( consumed )
       
   585                 {
       
   586                 consumingBindingInd = index;
       
   587                 aConsumed = ETrue;
       
   588                 }
       
   589             }
       
   590         
       
   591         index--;
       
   592         }
       
   593     
       
   594     if ( KErrNotFound != consumingBindingInd )
       
   595         {
       
   596         iBindings[consumingBindingInd].iLocalAddr = aLocalAddr;
       
   597         iBindings[consumingBindingInd].iFromAddr = aFromAddr;
       
   598         
       
   599         if ( aPeerAddr.IsUnspecified() )
       
   600             {
       
   601             if ( peerAddrFromIndication.IsUnspecified() )
       
   602                 {
       
   603                 iBindings[consumingBindingInd].iPeerAddr = aFromAddr;
       
   604                 }
       
   605             else
       
   606                 {
       
   607                 iBindings[consumingBindingInd].iPeerAddr 
       
   608                     = peerAddrFromIndication;
       
   609                 }
       
   610             }
       
   611         else
       
   612             {
       
   613             iBindings[consumingBindingInd].iPeerAddr = aPeerAddr;
       
   614             }
       
   615         
       
   616         __ICEDP_ADDRLOG( 
       
   617             "CIceConnectionHandler::IncomingMessageL BINDING PEERADDR",
       
   618             iBindings[consumingBindingInd].iPeerAddr )
       
   619         
       
   620         // mapped address is filled when binding event occurs
       
   621         }
       
   622     
       
   623     delete indicationData;
       
   624     }
       
   625 
       
   626 
       
   627 // ---------------------------------------------------------------------------
       
   628 // From class MIceNatPluginEventObs.
       
   629 // CIceConnectionHandler::PluginEventOccured
       
   630 // ---------------------------------------------------------------------------
       
   631 //
       
   632 void CIceConnectionHandler::PluginEventOccured( 
       
   633         const CNATFWPluginApi* /*aPlugin*/, TUint aStreamId, 
       
   634         MIceNatPluginEventObs::TNatPluginEvent aEventCode,
       
   635         TInt aErrorCode, TAny* aEventData, TUint /*aStreamConnectionId*/,
       
   636         TUint /*aComponentId*/, TBool /*aIPv6After*/ )
       
   637     {
       
   638     __ICEDP( "CIceConnectionHandler::PluginEventOccured" )
       
   639     
       
   640     switch ( aEventCode )
       
   641         {
       
   642         case MIceNatPluginEventObs::ESendingActivated:
       
   643         case MIceNatPluginEventObs::ESendingDeactivated:
       
   644         case MIceNatPluginEventObs::EReceivingActivated:
       
   645         case MIceNatPluginEventObs::EReceivingDeactivated:
       
   646             {
       
   647             iEventObserver.PluginEventOccured( NULL, aStreamId, 
       
   648                 aEventCode, aErrorCode, aEventData );
       
   649             }
       
   650             break;
       
   651         
       
   652         default:
       
   653             __ICEDP( "CIceConnectionHandler::PluginEventOccured, DEFAULT" )
       
   654             ASSERT( EFalse );
       
   655         }
       
   656     }
       
   657 
       
   658 
       
   659 // ---------------------------------------------------------------------------
       
   660 // CIceConnectionHandler::FindBindingIndex
       
   661 // Private. For finding the right binding.
       
   662 // ---------------------------------------------------------------------------
       
   663 //
       
   664 TInt CIceConnectionHandler::FindBindingIndex( 
       
   665         TUint aStreamId, TUint aConnectionId ) const
       
   666     {
       
   667     __ICEDP( "CIceConnectionHandler::FindBindingIndex" )
       
   668     
       
   669     TInt index( iBindings.Count() - 1 );
       
   670     while ( ( index >= 0 ) 
       
   671             && !( ( iBindings[index].iStreamId == aStreamId )
       
   672             && ( iBindings[index].iConnectionId == aConnectionId ) ) )
       
   673         {
       
   674         index--;
       
   675         }
       
   676     
       
   677     return index;
       
   678     }
       
   679 
       
   680 
       
   681 // ---------------------------------------------------------------------------
       
   682 // CIceConnectionHandler::FindBindingIndex
       
   683 // Private. For finding the right binding.
       
   684 // ---------------------------------------------------------------------------
       
   685 //
       
   686 TInt CIceConnectionHandler::FindBindingIndex( 
       
   687         const CBinding& aBinding ) const
       
   688     {
       
   689     TInt index( iBindings.Count() - 1 );
       
   690     while ( ( index >= 0 ) && ( iBindings[index].iBinding != &aBinding ) )
       
   691         {
       
   692         index--;
       
   693         }
       
   694     
       
   695     return index;
       
   696     }
       
   697 
       
   698 
       
   699 // ---------------------------------------------------------------------------
       
   700 // CIceConnectionHandler::FindBindingIndex
       
   701 // Private. For finding the right binding.
       
   702 // ---------------------------------------------------------------------------
       
   703 //
       
   704 TInt CIceConnectionHandler::FindBindingIndex( 
       
   705         TUint aStreamId, TUint aConnectionId, 
       
   706         const TInetAddr& aDestAddr ) const
       
   707     {
       
   708     __ICEDP( "CIceConnectionHandler::FindBindingIndex" )
       
   709     
       
   710     TInt foundConnectionInd( KErrNotFound );
       
   711     TInt index( iBindings.Count() - 1 );
       
   712     while ( KErrNotFound == foundConnectionInd && 0 <= index )
       
   713         {
       
   714         if ( ( iBindings[index].iStreamId == aStreamId ) &&
       
   715              ( iBindings[index].iConnectionId == aConnectionId ) &&
       
   716              ( TIceUtils::MatchAddresses( 
       
   717                    iBindings[index].iDestAddr, aDestAddr ) ) )
       
   718             {
       
   719             foundConnectionInd = index;
       
   720             }
       
   721         
       
   722         index--;
       
   723         }
       
   724     
       
   725     return foundConnectionInd;
       
   726     }
       
   727 
       
   728 
       
   729 // ---------------------------------------------------------------------------
       
   730 // CIceConnectionHandler::SolvePluginL
       
   731 // Private. Solves plugin owning given candidate.
       
   732 // ---------------------------------------------------------------------------
       
   733 //
       
   734 CNATFWPluginApi* CIceConnectionHandler::SolvePluginL( 
       
   735         const CNATFWCandidate& aLocalCandidate )
       
   736     {
       
   737     CNATFWPluginApi* item( NULL );
       
   738     switch ( aLocalCandidate.Type() )
       
   739         {
       
   740         case CNATFWCandidate::EHost:
       
   741         case CNATFWCandidate::EPeerReflexive:
       
   742             // Host plugin is ICE instantiated object and can be resolved
       
   743             // with hardcoded id. Host plugin binds peer reflexive candidate
       
   744             // to the host connection when candidate is given to the plugin 
       
   745             // first time.
       
   746             item = iPluginContainer.PluginByIdentifier( 
       
   747                 KNatPluginIdNokiaHost() );
       
   748             break;
       
   749         
       
   750         case CNATFWCandidate::EServerReflexive:
       
   751             item = iPluginContainer.PluginByType( KNatPluginTypeStun() );
       
   752             break;
       
   753         
       
   754         case CNATFWCandidate::ERelay:
       
   755             item = iPluginContainer.PluginByType( KNatPluginTypeTurn() );
       
   756             break;
       
   757        
       
   758         default:
       
   759             ASSERT( EFalse );
       
   760         }
       
   761     
       
   762     __ASSERT_ALWAYS( NULL != item, User::Leave( KErrNotFound ) );
       
   763     return item;
       
   764     }
       
   765 
       
   766 
       
   767 // ---------------------------------------------------------------------------
       
   768 // CIceConnectionHandler::FreeResources
       
   769 // Private. Remove data from binding array.
       
   770 // ---------------------------------------------------------------------------
       
   771 //
       
   772 void CIceConnectionHandler::FreeResources()
       
   773     {
       
   774     __ICEDP( "CIceConnectionHandler::FreeResources" )
       
   775     
       
   776     TInt count( iBindings.Count() );
       
   777     for ( TInt i( 0 ); i < count; ++i )
       
   778         {
       
   779         delete iBindings[i].iBinding;
       
   780         iBindings[i].iBinding = NULL;
       
   781         
       
   782         TRAP_IGNORE( 
       
   783             iMultiplexer.UnregisterConnectionObserverL( 
       
   784                 iBindings[i].iStreamId, *this ) )
       
   785         TRAP_IGNORE(
       
   786             iMultiplexer.UnregisterIncomingConnectionObserverL( 
       
   787                 iBindings[i].iStreamId, *this ) )
       
   788         
       
   789         if ( EIceConnTypeHost == iBindings[i].iConnType )
       
   790             {
       
   791             TRAP_IGNORE(
       
   792                 iMultiplexer.RemoveConnectionL( 
       
   793                     iBindings[i].iStreamId, iBindings[i].iConnectionId ) )
       
   794             }
       
   795         }
       
   796     
       
   797     iBindings.Close();
       
   798     }
       
   799 
       
   800 
       
   801 // ---------------------------------------------------------------------------
       
   802 // CIceConnectionHandler::ExecuteCallbackL
       
   803 // Private.
       
   804 // ---------------------------------------------------------------------------
       
   805 //
       
   806 void CIceConnectionHandler::ExecuteCallbackL( TInt aIndex, TInt aError )
       
   807     {
       
   808     __ICEDP( "CIceConnectionHandler::ExecuteCallbackL" )
       
   809     
       
   810     iBindings[aIndex].iCheckOngoing = EFalse;
       
   811     __ASSERT_ALWAYS( NULL != iBindings[aIndex].iObserver, 
       
   812         User::Leave( KErrNotReady ) );
       
   813     
       
   814     iBindings[aIndex].iObserver->ConnCheckCompletedL( 
       
   815         aError,
       
   816         iBindings[aIndex].iLocalAddr,
       
   817         iBindings[aIndex].iPeerAddr,
       
   818         iBindings[aIndex].iMappedAddr );
       
   819     }
       
   820 
       
   821 
       
   822 // ---------------------------------------------------------------------------
       
   823 // CIceConnectionHandler::DoRemoveConnection
       
   824 // ---------------------------------------------------------------------------
       
   825 //
       
   826 void CIceConnectionHandler::DoRemoveConnection( 
       
   827         TUint aStreamId, TUint aConnectionId, TBool aRemoveMuxConn )
       
   828     {
       
   829     __ICEDP_INT1( "CIceConnectionHandler::DoRemoveConnection, start:",
       
   830         aRemoveMuxConn )
       
   831     
       
   832     TInt index( FindBindingIndex( aStreamId, aConnectionId ) );
       
   833     if ( KErrNotFound != index )
       
   834         {
       
   835         if ( aRemoveMuxConn && 
       
   836                 EIceConnTypeHost == iBindings[index].iConnType )
       
   837             {
       
   838             TRAP_IGNORE(
       
   839                 iMultiplexer.RemoveConnectionL( 
       
   840                     iBindings[index].iStreamId, 
       
   841                     iBindings[index].iConnectionId ) )
       
   842             }
       
   843         
       
   844         delete iBindings[index].iBinding;
       
   845         iBindings.Remove( index );
       
   846         }
       
   847     
       
   848     __ICEDP( "CIceConnectionHandler::DoRemoveConnection, end" )
       
   849     }