natfw/natfwicecandidatehandler/src/ciceconnectivitycheck.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:    Performs connectivity check
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include "natfwcredentials.h"
       
    21 #include "natfwcandidatepair.h"
       
    22 #include "natfwcandidate.h"
       
    23 #include "ciceconnectivitycheck.h"
       
    24 #include "miceconnchecklistener.h"
       
    25 #include "natfwbindingbase.h"
       
    26 #include "ciceconnectionhandler.h"
       
    27 #include "cicesessiondata.h"
       
    28 
       
    29 #include "icecandidatehandlerlogs.h"
       
    30 
       
    31 // ======== MEMBER FUNCTIONS ========
       
    32 
       
    33 CIceConnectivityCheck::CIceConnectivityCheck(
       
    34         MIceConnCheckListener& aClient,
       
    35         CIceConnectionHandler& aConnHandler,
       
    36         CIceSessionData& aSessionData )
       
    37     :
       
    38     iState( EIceCheckFrozen ),
       
    39     iClient( aClient ),
       
    40     iConnHandler( aConnHandler ),
       
    41     iSessionData( aSessionData )
       
    42     {
       
    43     }
       
    44 
       
    45 
       
    46 void CIceConnectivityCheck::ConstructL(
       
    47         const CNATFWCandidate& aLocalCand,
       
    48         const CNATFWCandidate& aRemoteCand )
       
    49     {
       
    50     __ICEDP( "CIceConnectivityCheck::ConstructL" )
       
    51     
       
    52     __ASSERT_ALWAYS( aLocalCand.ComponentId() == aRemoteCand.ComponentId(),
       
    53         User::Leave( KErrArgument ) );
       
    54     __ASSERT_ALWAYS( 
       
    55         aLocalCand.StreamCollectionId() == aRemoteCand.StreamCollectionId(),
       
    56         User::Leave( KErrArgument ) );
       
    57     
       
    58     iCandidatePair = CNATFWCandidatePair::NewL( aLocalCand, aRemoteCand );
       
    59     }
       
    60 
       
    61 
       
    62 CIceConnectivityCheck* CIceConnectivityCheck::NewL(
       
    63         MIceConnCheckListener& aClient,
       
    64         const CNATFWCandidate& aLocalCand,
       
    65         const CNATFWCandidate& aRemoteCand,
       
    66         CIceConnectionHandler& aConnHandler,
       
    67         CIceSessionData& aSessionData )
       
    68     {
       
    69     CIceConnectivityCheck* self = CIceConnectivityCheck::NewLC(
       
    70         aClient, aLocalCand, aRemoteCand, aConnHandler, aSessionData );
       
    71     CleanupStack::Pop( self );
       
    72     return self;
       
    73     }
       
    74 
       
    75 
       
    76 CIceConnectivityCheck* CIceConnectivityCheck::NewLC(
       
    77         MIceConnCheckListener& aClient,
       
    78         const CNATFWCandidate& aLocalCand,
       
    79         const CNATFWCandidate& aRemoteCand,
       
    80         CIceConnectionHandler& aConnHandler,
       
    81         CIceSessionData& aSessionData )
       
    82     {
       
    83     CIceConnectivityCheck* self = new( ELeave ) 
       
    84         CIceConnectivityCheck( aClient, aConnHandler, aSessionData );
       
    85     CleanupStack::PushL( self );
       
    86     self->ConstructL( aLocalCand, aRemoteCand );
       
    87     return self;
       
    88     }
       
    89 
       
    90 
       
    91 CIceConnectivityCheck::~CIceConnectivityCheck()
       
    92     {
       
    93     __ICEDP( "CIceConnectivityCheck::~CIceConnectivityCheck" )
       
    94     
       
    95     delete iValidatedPair;
       
    96     if ( iCandidatePair )
       
    97         {
       
    98         iConnHandler.RemoveConnection( 
       
    99             iCandidatePair->LocalCandidate().StreamId(),
       
   100             iConnectionId );
       
   101         }
       
   102     
       
   103     delete iCandidatePair;
       
   104     }
       
   105 
       
   106 
       
   107 // ---------------------------------------------------------------------------
       
   108 // CIceConnectivityCheck::ComparePriorities
       
   109 // Implements TLinearOrder (descending order).
       
   110 // ---------------------------------------------------------------------------
       
   111 //
       
   112 TInt CIceConnectivityCheck::ComparePriorities( 
       
   113         const CIceConnectivityCheck& aA, 
       
   114         const CIceConnectivityCheck& aB )
       
   115     {
       
   116     return static_cast<TInt>( 
       
   117         aB.CandidatePair().Priority() - aA.CandidatePair().Priority() );
       
   118     }
       
   119 
       
   120 
       
   121 // ---------------------------------------------------------------------------
       
   122 // CIceConnectivityCheck::MatchAddresses
       
   123 // Implements TIdentityRelation.
       
   124 // ---------------------------------------------------------------------------
       
   125 //
       
   126 TBool CIceConnectivityCheck::MatchAddresses( 
       
   127         const CIceConnectivityCheck& aItem1, 
       
   128         const CIceConnectivityCheck& aItem2 )
       
   129     {
       
   130     const TInetAddr& localAddrA( 
       
   131         aItem1.CandidatePair().LocalCandidate().TransportAddr() );
       
   132     const TInetAddr& localAddrB( 
       
   133         aItem2.CandidatePair().LocalCandidate().TransportAddr() );
       
   134     const TInetAddr& remoteAddrA( 
       
   135         aItem1.CandidatePair().RemoteCandidate().TransportAddr() );
       
   136     const TInetAddr& remoteAddrB( 
       
   137         aItem2.CandidatePair().RemoteCandidate().TransportAddr() );
       
   138     
       
   139     return ( ( TIceUtils::MatchAddresses( localAddrA, localAddrB ) ) 
       
   140         && ( TIceUtils::MatchAddresses( remoteAddrA, remoteAddrB ) ) );
       
   141     }
       
   142 
       
   143 // Non-derived function
       
   144 
       
   145 // ---------------------------------------------------------------------------
       
   146 // CIceConnectivityCheck::Initialize
       
   147 // ---------------------------------------------------------------------------
       
   148 //
       
   149 void CIceConnectivityCheck::Initialize()
       
   150     {
       
   151     __ICEDP( "CIceConnectivityCheck::Initialize" )
       
   152     
       
   153     iIsNominated = EFalse;
       
   154     iResendInProgress = EFalse;
       
   155     
       
   156     iState = EIceCheckWaiting;
       
   157     }
       
   158 
       
   159 
       
   160 // ---------------------------------------------------------------------------
       
   161 // CIceConnectivityCheck::PerformConnCheckL
       
   162 // USERNAME and MESSAGE-INTEGRITY -attributes are set by connection handler.
       
   163 // ---------------------------------------------------------------------------
       
   164 //
       
   165 void CIceConnectivityCheck::PerformConnCheckL( 
       
   166         TIceCheckType aType, 
       
   167         TUint aPriority,
       
   168         TUint aRtoValue )
       
   169     {
       
   170     __ICEDP( "CIceConnectivityCheck::PerformConnCheckL" )
       
   171     __ICEDP_ADDRLOG( "LOCAL_ADDR", 
       
   172         iCandidatePair->LocalCandidate().TransportAddr() )
       
   173     __ICEDP_ADDRLOG( "PEER_ADDR", 
       
   174         iCandidatePair->RemoteCandidate().TransportAddr() )
       
   175     
       
   176     __ASSERT_ALWAYS( 
       
   177         ( EIceCheckWaiting == iState || EIceCheckFrozen == iState ),
       
   178         User::Leave( KErrNotReady ) );
       
   179         
       
   180     __ASSERT_ALWAYS( ( iCandidatePair->LocalCandidate().Type() ==
       
   181         CNATFWCandidate::ERelay || iCandidatePair->LocalCandidate().Type() ==
       
   182         CNATFWCandidate::EHost ), User::Leave( KErrArgument ) );
       
   183     
       
   184     iType = aType;
       
   185     iPriority = aPriority;
       
   186     
       
   187     if ( 0 == iConnectionId )
       
   188         {
       
   189         iConnectionId = iConnHandler.CreateConnectionL( *iCandidatePair );
       
   190         }
       
   191     
       
   192     iConnHandler.PerformConnCheckL( 
       
   193         StreamId(), iConnectionId, iPriority, EFalse, 
       
   194         aRtoValue, iCandidatePair->RemoteCandidate().TransportAddr(),
       
   195         *this );
       
   196     
       
   197     iState = EIceCheckInProgress;
       
   198     }
       
   199 
       
   200 
       
   201 // ---------------------------------------------------------------------------
       
   202 // CIceConnectivityCheck::PerformNominationL
       
   203 // ---------------------------------------------------------------------------
       
   204 //
       
   205 void CIceConnectivityCheck::PerformNominationL( TUint aRtoValue )
       
   206     {
       
   207     __ICEDP( "CIceConnectivityCheck::PerformNominationL" )
       
   208     __ICEDP_ADDRLOG( "LOCAL_ADDR", 
       
   209         iCandidatePair->LocalCandidate().TransportAddr() )
       
   210     __ICEDP_ADDRLOG( "PEER_ADDR", 
       
   211         iCandidatePair->RemoteCandidate().TransportAddr() )
       
   212     
       
   213     __ASSERT_ALWAYS( 
       
   214         EIceCheckSucceed == iState || EIceCheckInProgress == iState, 
       
   215         User::Leave( KErrNotReady ) );
       
   216     
       
   217     iIsNominated = ETrue;
       
   218     
       
   219     if ( EIceCheckSucceed == iState )
       
   220         {
       
   221         iConnHandler.PerformConnCheckL( 
       
   222             StreamId(), iConnectionId, iPriority, ETrue, aRtoValue,
       
   223             iCandidatePair->RemoteCandidate().TransportAddr(), *this );
       
   224         iResendInProgress = ETrue;
       
   225         iState = EIceCheckInProgress;
       
   226         }
       
   227     else
       
   228         {
       
   229         ASSERT( EFalse );
       
   230         }
       
   231     }
       
   232 
       
   233 
       
   234 // ---------------------------------------------------------------------------
       
   235 // CIceConnectivityCheck::Nominated
       
   236 // ---------------------------------------------------------------------------
       
   237 //
       
   238 TBool CIceConnectivityCheck::Nominated() const
       
   239     {
       
   240     __ICEDP_INT1( "CIceConnectivityCheck::Nominated:", iIsNominated )
       
   241     
       
   242     return iIsNominated;
       
   243     }
       
   244 
       
   245 
       
   246 // ---------------------------------------------------------------------------
       
   247 // CIceConnectivityCheck::SetRemoteCheckInfo
       
   248 // ---------------------------------------------------------------------------
       
   249 //
       
   250 void CIceConnectivityCheck::SetRemoteCheckInfo( 
       
   251         TUint aPriority, TBool aFavored )
       
   252     {
       
   253     __ICEDP( "CIceConnectivityCheck::SetRemoteCheckInfo" )
       
   254     
       
   255     iRemotePriority = aPriority;
       
   256     iRemoteFavored = aFavored;
       
   257     }
       
   258 
       
   259 
       
   260 // ---------------------------------------------------------------------------
       
   261 // CIceConnectivityCheck::Cancel
       
   262 // ---------------------------------------------------------------------------
       
   263 //
       
   264 void CIceConnectivityCheck::Cancel()
       
   265     {
       
   266     iConnHandler.CancelCheck( StreamId(), iConnectionId, 
       
   267         iCandidatePair->RemoteCandidate().TransportAddr() );
       
   268     }
       
   269 
       
   270 
       
   271 // ---------------------------------------------------------------------------
       
   272 // CIceConnectivityCheck::StreamId
       
   273 // ---------------------------------------------------------------------------
       
   274 //
       
   275 TUint CIceConnectivityCheck::StreamId() const
       
   276     {
       
   277     return iCandidatePair->LocalCandidate().StreamId();
       
   278     }
       
   279 
       
   280 
       
   281 // ---------------------------------------------------------------------------
       
   282 // CIceConnectivityCheck::ComponentId
       
   283 // ---------------------------------------------------------------------------
       
   284 //
       
   285 TUint CIceConnectivityCheck::ComponentId() const
       
   286     {
       
   287     return iCandidatePair->LocalCandidate().ComponentId();
       
   288     }
       
   289 
       
   290 
       
   291 // ---------------------------------------------------------------------------
       
   292 // CIceConnectivityCheck::State
       
   293 // ---------------------------------------------------------------------------
       
   294 //
       
   295 CIceConnectivityCheck::TIceCheckState CIceConnectivityCheck::State() const
       
   296     {
       
   297     return iState;
       
   298     }
       
   299 
       
   300 
       
   301 // ---------------------------------------------------------------------------
       
   302 // CIceConnectivityCheck::StreamCollectionId
       
   303 // ---------------------------------------------------------------------------
       
   304 //
       
   305 TUint CIceConnectivityCheck::StreamCollectionId() const
       
   306     {
       
   307     return iCandidatePair->LocalCandidate().StreamCollectionId();
       
   308     }
       
   309 
       
   310 
       
   311 // ---------------------------------------------------------------------------
       
   312 // CIceConnectivityCheck::ValidatedPair
       
   313 // ---------------------------------------------------------------------------
       
   314 //
       
   315 const CNATFWCandidatePair* CIceConnectivityCheck::ValidatedPair() const
       
   316     {
       
   317     return iValidatedPair;
       
   318     }
       
   319 
       
   320 
       
   321 // ---------------------------------------------------------------------------
       
   322 // CIceConnectivityCheck::CandidatePair
       
   323 // ---------------------------------------------------------------------------
       
   324 //
       
   325 const CNATFWCandidatePair& CIceConnectivityCheck::CandidatePair() const
       
   326     {
       
   327     return *iCandidatePair;
       
   328     }
       
   329 
       
   330 
       
   331 // ---------------------------------------------------------------------------
       
   332 // CIceConnectivityCheck::ConnCheckCompletedL
       
   333 // ---------------------------------------------------------------------------
       
   334 //
       
   335 void CIceConnectivityCheck::ConnCheckCompletedL(
       
   336     TInt aCompletionCode,
       
   337 #ifdef _DEBUG
       
   338     const TInetAddr& aLocalAddr,
       
   339 #else
       
   340     const TInetAddr& /*aLocalAddr*/,
       
   341 #endif
       
   342     const TInetAddr& aPeerAddr,
       
   343     const TInetAddr& aMappedAddr )
       
   344     {
       
   345     __ICEDP_INT1( "CIceConnectivityCheck::ConnCheckCompletedL, COMPLCODE",
       
   346         aCompletionCode )
       
   347     
       
   348     __ICEDP_ADDRLOG( "LOCAL_ADDR", aLocalAddr )
       
   349     __ICEDP_ADDRLOG( "PEER_ADDR", aPeerAddr )
       
   350     __ICEDP_ADDRLOG( "MAPPED_ADDR", aMappedAddr )
       
   351     
       
   352     __ASSERT_DEBUG( EIceCheckInProgress == iState, 
       
   353         User::Leave( KErrNotReady ) );
       
   354     
       
   355     TInt completionCode = aCompletionCode;
       
   356     if ( !TIceUtils::MatchAddresses( aPeerAddr, 
       
   357             iCandidatePair->RemoteCandidate().TransportAddr() ) )
       
   358         {
       
   359         completionCode = KErrCouldNotConnect;
       
   360         }
       
   361     
       
   362     if ( KErrNone != completionCode )
       
   363         {
       
   364         iState = EIceCheckFailed;
       
   365         if ( iResendInProgress )
       
   366             {
       
   367             iResendInProgress = EFalse;
       
   368             iClient.NominationCompletedL( 
       
   369                 completionCode, *this, *iValidatedPair );
       
   370             }
       
   371         else
       
   372             {
       
   373             iClient.CheckCompletedL( completionCode, *this, NULL );
       
   374             }
       
   375         
       
   376         return;
       
   377         }
       
   378     
       
   379     if ( iResendInProgress )
       
   380         {
       
   381         iResendInProgress = EFalse;
       
   382         if ( iIsNominated )
       
   383             {
       
   384             iValidatedPair->SetSelected( ETrue );
       
   385             iCandidatePair->SetSelected( ETrue );
       
   386             }
       
   387         
       
   388         iClient.NominationCompletedL( completionCode, *this, 
       
   389             *iValidatedPair );
       
   390         }
       
   391     else
       
   392         {
       
   393         iState = EIceCheckSucceed;
       
   394         iValidatedPair = CreateValidatedPairL( aMappedAddr );
       
   395         
       
   396         CNATFWCandidatePair* validatedPair
       
   397             = CNATFWCandidatePair::NewL( *iValidatedPair );
       
   398         iClient.CheckCompletedL( completionCode, *this, 
       
   399             validatedPair );
       
   400         }
       
   401     }
       
   402 
       
   403 
       
   404 // ---------------------------------------------------------------------------
       
   405 // CIceConnectivityCheck::CreateValidatedPairL
       
   406 // ICE-17, 7.1.2.2.2, Constructing a Valid Pair
       
   407 // ---------------------------------------------------------------------------
       
   408 //
       
   409 CNATFWCandidatePair* CIceConnectivityCheck::CreateValidatedPairL(
       
   410         const TInetAddr& aMappedAddr )
       
   411     {
       
   412     __ICEDP( "CIceConnectivityCheck::CreateValidatedPairL" )
       
   413     
       
   414     const CNATFWCandidate* knownLocalCand 
       
   415         = iSessionData.FindLocalCandidate( aMappedAddr );
       
   416     
       
   417     CNATFWCandidate* validatedLocalCand = NULL;
       
   418     if ( knownLocalCand )
       
   419         {
       
   420         validatedLocalCand = CNATFWCandidate::NewLC( *knownLocalCand );
       
   421         }
       
   422     else
       
   423         {
       
   424         // Peer reflexive candidate
       
   425         validatedLocalCand = CNATFWCandidate::NewLC( 
       
   426             iCandidatePair->LocalCandidate() );
       
   427         validatedLocalCand->SetTransportAddrL( aMappedAddr );
       
   428         validatedLocalCand->SetType( CNATFWCandidate::EPeerReflexive );
       
   429         validatedLocalCand->SetPriority( iPriority );
       
   430         }
       
   431     
       
   432     CNATFWCandidate* remoteCand 
       
   433         = CNATFWCandidate::NewLC( iCandidatePair->RemoteCandidate() );
       
   434     if ( EIceCheckTypeTriggered == iType )
       
   435         {
       
   436         remoteCand->SetPriority( iRemotePriority );
       
   437         }
       
   438     
       
   439     CNATFWCandidatePair* validatedPair = CNATFWCandidatePair::NewLC(
       
   440         *validatedLocalCand, *remoteCand );
       
   441     
       
   442     if ( iIsNominated )
       
   443         {
       
   444         // ICE-17, 7.1.2.2.4. Updating the Nominated Flag
       
   445         validatedPair->SetSelected( ETrue );
       
   446         iCandidatePair->SetSelected( ETrue );
       
   447         }
       
   448     else
       
   449         {
       
   450         if ( EIceCheckTypeTriggered == iType && iRemoteFavored )
       
   451             {
       
   452             // ICE-17, 7.2.1.5. Updating the Nominated Flag
       
   453             validatedPair->SetSelected( ETrue );
       
   454             iCandidatePair->SetSelected( ETrue );
       
   455             }
       
   456         }
       
   457     
       
   458     CleanupStack::Pop( validatedPair );
       
   459     CleanupStack::PopAndDestroy( remoteCand );
       
   460     CleanupStack::PopAndDestroy( validatedLocalCand );
       
   461     return validatedPair;
       
   462     }