natplugins/natpnatfwsdpprovider/src/nspcontroller.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:  Controller class implementation
       
    15 *
       
    16 */
       
    17 
       
    18 #include <sdpdocument.h>
       
    19 #include <random.h>
       
    20 #include <centralrepository.h>
       
    21 #include <unsafprotocolscrkeys.h>
       
    22 #include <e32math.h>
       
    23 #include <badesca.h>
       
    24 #include "natfwconnectivityapi.h"
       
    25 #include "natfwcandidate.h"
       
    26 #include "natfwcandidatepair.h"
       
    27 #include "nspsessionobserver.h"
       
    28 #include "nspcontroller.h"
       
    29 #include "nspsession.h"
       
    30 #include "nspevents.h"
       
    31 #include "nspactive.h"
       
    32 #include "nspcontentparser.h"
       
    33 #include "nspdefs.h"
       
    34 
       
    35 // CONSTANTS
       
    36 //_LIT8( KStun, "stun" );
       
    37 //_LIT8( KTurn, "turn" );
       
    38 _LIT8( KIce, "ice" );
       
    39 
       
    40 _LIT8( KNSPCharStore,
       
    41 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/" );
       
    42 _LIT8( KNSPSingleDigit, "x" );
       
    43 
       
    44 const TText8* const KComma = _S8( "," );
       
    45 const TUint KCommaLength = 1;
       
    46 const TText8* const KDot = _S8( "." );
       
    47 
       
    48 const TUint KNSPSingleDigitLength = 1;
       
    49 const TUint KNSPUsernameMinLength = 4;
       
    50 const TUint KNSPPasswordMinLength = 22;
       
    51 const TUint KNSPVariationLength = 4;
       
    52 const TUint KDefaultUpdateSdpTimerValue = 10;
       
    53 
       
    54 static TDesC8& TrimAll( HBufC8& aDes )
       
    55     {
       
    56     TPtr8 ptr( aDes.Des() );
       
    57     ptr.TrimAll();
       
    58     return aDes;
       
    59     }
       
    60 
       
    61 
       
    62 // ======== MEMBER FUNCTIONS ========
       
    63 // ---------------------------------------------------------------------------
       
    64 // CNSPController::CNSPController
       
    65 // ---------------------------------------------------------------------------
       
    66 //
       
    67 CNSPController::CNSPController()
       
    68     {    
       
    69     }
       
    70 
       
    71 
       
    72 // ---------------------------------------------------------------------------
       
    73 // CNSPController::ConstructL
       
    74 // ---------------------------------------------------------------------------
       
    75 //
       
    76 void CNSPController::ConstructL()
       
    77     {
       
    78     NSPLOG_STR( "CNSPController::ConstructL(), Entry" )
       
    79     
       
    80     iInterface = CNATFWConnectivityApi::NewL();
       
    81     iInterface->RegisterObserverForEventsL(
       
    82         *this, MNATFWConnectivityObserver::EAllEvents );
       
    83     iContentParser = CNSPContentParser::NewL();
       
    84     iBase = Abs( static_cast<TInt>( Math::Random() ) );
       
    85     
       
    86     NSPLOG_STR( "CNSPController::ConstructL(), Exit" )
       
    87     }
       
    88 
       
    89 
       
    90 // ---------------------------------------------------------------------------
       
    91 // CNSPController::NewL
       
    92 // ---------------------------------------------------------------------------
       
    93 //
       
    94 CNSPController* CNSPController::NewL()
       
    95     {
       
    96     CNSPController* self = CNSPController::NewLC();
       
    97     CleanupStack::Pop( self );
       
    98     return self;
       
    99     }
       
   100 
       
   101 
       
   102 // ---------------------------------------------------------------------------
       
   103 // CNSPController::NewLC
       
   104 // ---------------------------------------------------------------------------
       
   105 //
       
   106 CNSPController* CNSPController::NewLC()
       
   107     {
       
   108     CNSPController* self = new ( ELeave ) CNSPController;
       
   109     CleanupStack::PushL( self );
       
   110     self->ConstructL();
       
   111     return self;
       
   112     }
       
   113 
       
   114 
       
   115 // ---------------------------------------------------------------------------
       
   116 // CNSPController::~CNSPController
       
   117 // ---------------------------------------------------------------------------
       
   118 //
       
   119 CNSPController::~CNSPController()
       
   120     {
       
   121     NSPLOG_STR( "CNSPController::~CNSPController(), Entry" )
       
   122     
       
   123     iActiveObjects.ResetAndDestroy();
       
   124     iActiveObjects.Close();    
       
   125     iSessionArray.ResetAndDestroy();
       
   126     iSessionArray.Close();
       
   127     iClosingSessionArray.ResetAndDestroy();
       
   128     iClosingSessionArray.Close();
       
   129     delete iContentParser;
       
   130     if ( iInterface )
       
   131         {
       
   132         iInterface->UnregisterObserverForEvents(
       
   133             *this, MNATFWConnectivityObserver::EAllEvents );
       
   134         }
       
   135     delete iInterface;
       
   136     
       
   137     NSPLOG_STR( "CNSPController::~CNSPController(), Exit" )
       
   138     }
       
   139 
       
   140 
       
   141 // ---------------------------------------------------------------------------
       
   142 // CNSPController::EventOccured
       
   143 // ---------------------------------------------------------------------------
       
   144 //
       
   145 void CNSPController::EventOccured( TUint aSessionId, TUint aStreamId,
       
   146         TNATFWConnectivityEvent aEvent, TInt aError, TAny* aEventData )
       
   147     {
       
   148     NSPLOG_STR( "CNSPController::EventOccured(), Entry" )
       
   149     
       
   150     TInt index = FindSession( aSessionId );
       
   151     
       
   152     if ( KErrNotFound != index )
       
   153         {
       
   154         TEventReturnStatus status = iSessionArray[index]->EventOccured(
       
   155         		aStreamId, aEvent, aError, aEventData );
       
   156         Callback( *iSessionArray[index], status );
       
   157         }
       
   158     else
       
   159         {
       
   160         index = FindClosingSession( aSessionId );
       
   161         
       
   162         if ( KErrNotFound != index )
       
   163             {
       
   164             TEventReturnStatus status = iClosingSessionArray[index]->EventOccured(
       
   165                     aStreamId, aEvent, aError, aEventData );
       
   166             
       
   167             if ( NSP_DELETE( status.iStatus ) || NSP_ERROR( status.iStatus ) )
       
   168                 {
       
   169                 delete iClosingSessionArray[index];
       
   170                 iClosingSessionArray.Remove(index);
       
   171                 iClosingSessionArray.Close();
       
   172                 RemoveActiveObjects( aSessionId );
       
   173                 }
       
   174             }
       
   175         else
       
   176             {
       
   177             NSPLOG_STR( "CNSPController::EventOccured(), KErrNotFound" )
       
   178             NSPLOG_INT( "CNSPController::EventOccured(), error:", aError )
       
   179             }
       
   180         }
       
   181     
       
   182     NSPLOG_STR( "CNSPController::EventOccured(), Exit" )
       
   183     }
       
   184 
       
   185 
       
   186 // ---------------------------------------------------------------------------
       
   187 // CNSPController::ContentParser
       
   188 // ---------------------------------------------------------------------------
       
   189 //
       
   190 const CNSPContentParser& CNSPController::ContentParser()
       
   191     {
       
   192     return (*iContentParser);
       
   193     }
       
   194 
       
   195 
       
   196 // ---------------------------------------------------------------------------
       
   197 // CNSPController::CreateSessionL
       
   198 // ---------------------------------------------------------------------------
       
   199 //
       
   200 TUint CNSPController::CreateSessionL( TUint32 aIapId, const TDesC8& aDomain )
       
   201     {
       
   202     return iInterface->CreateSessionL( aIapId, aDomain );
       
   203     }
       
   204 
       
   205 
       
   206 // ---------------------------------------------------------------------------
       
   207 // CNSPController::LoadPluginL
       
   208 // ---------------------------------------------------------------------------
       
   209 //
       
   210 void CNSPController::LoadPluginL( TUint aSessionId, const CDesC8Array& aPlugins,
       
   211         TInt& aPluginIndex )
       
   212     {
       
   213     iInterface->LoadPluginL( aSessionId, aPlugins, aPluginIndex );
       
   214     }
       
   215 
       
   216 
       
   217 // ---------------------------------------------------------------------------
       
   218 // CNSPController::CreateStreamL
       
   219 // ---------------------------------------------------------------------------
       
   220 //
       
   221 TUint CNSPController::CreateStreamL( TUint aSessionId, TUint aProtocol, TInt aQoS )
       
   222     {
       
   223     return iInterface->CreateStreamL( aSessionId, aProtocol, aQoS );
       
   224     }
       
   225 
       
   226 
       
   227 // ---------------------------------------------------------------------------
       
   228 // CNSPController::CreateWrapperL
       
   229 // ---------------------------------------------------------------------------
       
   230 //
       
   231 MNATFWSocketMediaConnWrapper& CNSPController::CreateWrapperL( 
       
   232         TUint aSessionId, TUint aStreamId )
       
   233     {
       
   234     return iInterface->CreateWrapperL( aSessionId, aStreamId );
       
   235     }
       
   236 
       
   237 
       
   238 // ---------------------------------------------------------------------------
       
   239 // CNSPController::FetchCandidateL
       
   240 // ---------------------------------------------------------------------------
       
   241 //
       
   242 void CNSPController::FetchCandidateL( TUint aSessionId, TUint aStreamId,
       
   243         TUint aAddrFamily )
       
   244     {
       
   245     iInterface->FetchCandidateL( aSessionId, aStreamId, aAddrFamily );
       
   246     }
       
   247 
       
   248 
       
   249 // ---------------------------------------------------------------------------
       
   250 // CNSPController::FetchCandidatesL
       
   251 // ---------------------------------------------------------------------------
       
   252 //
       
   253 void CNSPController::FetchCandidatesL( TUint aSessionId, TUint aStreamId,
       
   254         TUint aCollectionId, TUint aComponentId, TUint aAddrFamily )
       
   255     {
       
   256     iInterface->FetchCandidatesL( aSessionId, aStreamId,
       
   257             aCollectionId, aComponentId, aAddrFamily );
       
   258     }
       
   259 
       
   260 
       
   261 // ---------------------------------------------------------------------------
       
   262 // CNSPController::SetReceivingStateL
       
   263 // ---------------------------------------------------------------------------
       
   264 //
       
   265 void CNSPController::SetReceivingStateL( const CNATFWCandidate& aLocalCand,
       
   266         TNATFWStreamingState aState )
       
   267     {
       
   268     iInterface->SetReceivingStateL( aLocalCand, aState );
       
   269     }
       
   270 
       
   271 
       
   272 // ---------------------------------------------------------------------------
       
   273 // CNSPController::SetSendingStateL
       
   274 // ---------------------------------------------------------------------------
       
   275 //
       
   276 void CNSPController::SetSendingStateL( const CNATFWCandidate& aLocalCand,
       
   277         TNATFWStreamingState aState, const TDesC8& aDestAddr, TUint aPort )
       
   278     {
       
   279     iInterface->SetSendingStateL( aLocalCand, aState, aDestAddr, aPort );
       
   280     }
       
   281 
       
   282 
       
   283 // ---------------------------------------------------------------------------
       
   284 // CNSPController::SetRoleL
       
   285 // ---------------------------------------------------------------------------
       
   286 //
       
   287 void CNSPController::SetRoleL( TUint aSessionId, TNATFWIceRole aRole )
       
   288     {
       
   289     iInterface->SetRoleL( aSessionId, aRole );
       
   290     }
       
   291 
       
   292 
       
   293 // ---------------------------------------------------------------------------
       
   294 // CNSPController::SetCredentialsL
       
   295 // ---------------------------------------------------------------------------
       
   296 //
       
   297 void CNSPController::SetCredentialsL( TUint aSessionId, TUint aStreamId,
       
   298         const CNATFWCredentials& aCredentials )
       
   299     {
       
   300     CNATFWCandidate* dummyCand = CNATFWCandidate::NewLC();
       
   301     dummyCand->SetSessionId( aSessionId );
       
   302     dummyCand->SetStreamId( aStreamId );
       
   303     iInterface->SetCredentialsL( *dummyCand, aCredentials );
       
   304     CleanupStack::PopAndDestroy( dummyCand );
       
   305     }
       
   306 
       
   307 
       
   308 // ---------------------------------------------------------------------------
       
   309 // CNSPController::PerformCandidateChecksL
       
   310 // ---------------------------------------------------------------------------
       
   311 //
       
   312 void CNSPController::PerformCandidateChecksL( TUint aSessionId,
       
   313         RPointerArray<CNATFWCandidate>& aRemoteCandidates )
       
   314     {
       
   315     iInterface->PerformConnectivityChecksL( aSessionId, aRemoteCandidates );
       
   316     }
       
   317 
       
   318 
       
   319 // ---------------------------------------------------------------------------
       
   320 // CNSPController::UpdateIceProcessingL
       
   321 // ---------------------------------------------------------------------------
       
   322 //
       
   323 void CNSPController::UpdateIceProcessingL( TUint aSessionId,
       
   324         RPointerArray<CNATFWCandidate>& aNewRemoteCands )
       
   325     {
       
   326     iInterface->UpdateIceProcessingL( aSessionId, aNewRemoteCands );
       
   327     }
       
   328 
       
   329 
       
   330 // ---------------------------------------------------------------------------
       
   331 // CNSPController::CloseStreamL
       
   332 // ---------------------------------------------------------------------------
       
   333 //
       
   334 void CNSPController::CloseStreamL( TUint aSessionId, TUint aStreamId )
       
   335     {
       
   336     iInterface->CloseStreamL( aSessionId, aStreamId );
       
   337     }
       
   338 
       
   339 
       
   340 // ---------------------------------------------------------------------------
       
   341 // CNSPController::CloseSessionL
       
   342 // ---------------------------------------------------------------------------
       
   343 //
       
   344 void CNSPController::CloseSessionL( TUint aSessionId )
       
   345     {
       
   346     iInterface->CloseSessionL( aSessionId );
       
   347     }
       
   348 
       
   349 
       
   350 // ---------------------------------------------------------------------------
       
   351 // CNSPController::CreateUniqueId
       
   352 // ---------------------------------------------------------------------------
       
   353 //
       
   354 TUint CNSPController::CreateUniqueId()
       
   355     {
       
   356     return (iBase++);
       
   357     }
       
   358 
       
   359 
       
   360 // ---------------------------------------------------------------------------
       
   361 // CNSPController::OrderTimeoutL
       
   362 // ---------------------------------------------------------------------------
       
   363 //
       
   364 TUint CNSPController::OrderTimeoutL( TUint aSessionId, TUint aStreamId,
       
   365         MNATFWConnectivityObserver::TNATFWConnectivityEvent aEvent,
       
   366         TUint aTimerInMicroSeconds )
       
   367     {
       
   368     CNSPActive* callback = NULL;
       
   369     const TInt index = FindSession( aSessionId );
       
   370     const TUint transactionId = CreateUniqueId();
       
   371     
       
   372     if ( KErrNotFound != index )
       
   373         {
       
   374         TRAPD( error, callback = CNSPActive::NewL( *this,
       
   375                 aSessionId, aStreamId, transactionId, aEvent,
       
   376                 aTimerInMicroSeconds, NULL ) );
       
   377         
       
   378         User::LeaveIfError( error );
       
   379         error = iActiveObjects.Append( callback );
       
   380         
       
   381         if ( KErrNone != error )
       
   382             {
       
   383             delete callback;
       
   384             User::LeaveIfError( error );
       
   385             }
       
   386         }
       
   387     else
       
   388         {
       
   389         User::Leave( KErrNotFound );
       
   390         }
       
   391     
       
   392     return transactionId;
       
   393     }
       
   394 
       
   395 
       
   396 // ---------------------------------------------------------------------------
       
   397 // CNSPController::OrderUpdateSdpL
       
   398 // ---------------------------------------------------------------------------
       
   399 //
       
   400 TUint CNSPController::OrderUpdateSdpL( TUint aSessionId, CSdpDocument* aDoc )
       
   401     {
       
   402     CNSPActive* callback = NULL;
       
   403     const TInt index = FindSession( aSessionId );
       
   404     const TUint transactionId = CreateUniqueId();
       
   405     
       
   406     if ( KErrNotFound != index )
       
   407         {
       
   408         CleanupStack::PushL( aDoc );
       
   409         callback = CNSPActive::NewL( *this,
       
   410                 aSessionId, (TUint) KErrNone, transactionId,
       
   411                 (MNATFWConnectivityObserver::TNATFWConnectivityEvent) KErrNone,
       
   412                 KDefaultUpdateSdpTimerValue, aDoc );
       
   413         CleanupStack::Pop( aDoc );
       
   414         CleanupStack::PushL( callback );
       
   415         
       
   416         iActiveObjects.AppendL( callback );
       
   417         CleanupStack::Pop( callback );
       
   418         }
       
   419     else
       
   420         {
       
   421         User::Leave( KErrNotFound );
       
   422         }
       
   423     
       
   424     return transactionId;
       
   425     }
       
   426 
       
   427 
       
   428 // ---------------------------------------------------------------------------
       
   429 // CNSPController::Cancel
       
   430 // ---------------------------------------------------------------------------
       
   431 //
       
   432 void CNSPController::Cancel( TUint aTransactionId )
       
   433     {
       
   434     const TInt index = FindActiveObject( aTransactionId );
       
   435     
       
   436     if ( KErrNotFound != index )
       
   437         {
       
   438         iActiveObjects[index]->Cancel();
       
   439         delete (iActiveObjects[index]);
       
   440         iActiveObjects.Remove(index);
       
   441         iActiveObjects.Compress();
       
   442         }
       
   443     }
       
   444 
       
   445 
       
   446 // ---------------------------------------------------------------------------
       
   447 // CNSPController::GenerateUsernameL
       
   448 // ---------------------------------------------------------------------------
       
   449 //
       
   450 void CNSPController::GenerateUsernameL( TDes8& aUsername )
       
   451     {
       
   452     aUsername.Zero();
       
   453     
       
   454     TInt newlength = KNSPUsernameMinLength +
       
   455                      RandomByteL() % KNSPVariationLength;
       
   456     __ASSERT_ALWAYS( aUsername.MaxLength() >= newlength,
       
   457                      User::Leave( KErrArgument ) );
       
   458     
       
   459     for ( TInt index = 0; index < newlength; index++ )
       
   460         {
       
   461         aUsername.Append( KNSPCharStore()[
       
   462                 RandomByteL() % KNSPCharStore().Length() ] );
       
   463         }
       
   464     }
       
   465 
       
   466 
       
   467 // ---------------------------------------------------------------------------
       
   468 // CNSPController::GeneratePasswordL
       
   469 // ---------------------------------------------------------------------------
       
   470 //
       
   471 void CNSPController::GeneratePasswordL( TDes8& aPassword )
       
   472     {
       
   473     aPassword.Zero();
       
   474     
       
   475     TInt newlength = KNSPPasswordMinLength +
       
   476                      RandomByteL() % KNSPVariationLength;    
       
   477     __ASSERT_ALWAYS( aPassword.MaxLength() >= newlength,
       
   478                      User::Leave( KErrArgument ) );
       
   479     
       
   480     for ( TInt index = 0; index < newlength; index++ )
       
   481         {
       
   482         aPassword.Append( KNSPCharStore()[
       
   483                 RandomByteL() % KNSPCharStore().Length() ] );
       
   484         }
       
   485     }
       
   486 
       
   487 
       
   488 // ---------------------------------------------------------------------------
       
   489 // CNSPController::NewSessionL
       
   490 // ---------------------------------------------------------------------------
       
   491 //
       
   492 TUint CNSPController::NewSessionL( MNSPSessionObserver& aSessionObserver,
       
   493         TUint32 aIapId, const TDesC8& aDomain, TUint aProtocol )
       
   494     {
       
   495     NSPLOG_STR( "CNSPController::NewSessionL(), Entry" )
       
   496     
       
   497     CNSPSession* session = CNSPSession::NewLC( *this,
       
   498             aSessionObserver, aIapId, aDomain, aProtocol );
       
   499     session->SetUseIce( UseIceL( session->Plugins(), aDomain ) );
       
   500     User::LeaveIfError( iSessionArray.Append( session ) );
       
   501     CleanupStack::Pop( session );
       
   502     
       
   503     NSPLOG_STR( "CNSPController::NewSessionL(), Exit" )
       
   504     return session->SessionId();
       
   505     }
       
   506 
       
   507 
       
   508 // ---------------------------------------------------------------------------
       
   509 // CNSPController::CloseSessionL
       
   510 // ---------------------------------------------------------------------------
       
   511 //
       
   512 void CNSPController::ClosingSessionL( TUint aSessionId )
       
   513     {
       
   514     const TInt index = FindSession( aSessionId );
       
   515     
       
   516     if ( KErrNotFound != index )
       
   517         {
       
   518         CNSPSession* session = iSessionArray[index];
       
   519         iSessionArray.Remove(index);
       
   520         iSessionArray.Compress();
       
   521         
       
   522         CleanupStack::PushL( session );
       
   523         TNatReturnStatus status = session->CloseSessionL();
       
   524         
       
   525         if ( KNatReady == status )
       
   526             {
       
   527             CleanupStack::Pop( session );
       
   528             delete session;
       
   529             }
       
   530         else
       
   531             {
       
   532             iClosingSessionArray.AppendL( session );// ownership changed
       
   533             CleanupStack::Pop( session );
       
   534             }
       
   535         }
       
   536     else
       
   537         {
       
   538         NSPLOG_STR( "CNSPController::CloseSessionL(), KErrNotFound" )
       
   539         User::Leave( KErrNotFound );
       
   540         }
       
   541     }
       
   542 
       
   543 
       
   544 // ---------------------------------------------------------------------------
       
   545 // CNSPController::FindSessionObject
       
   546 // ---------------------------------------------------------------------------
       
   547 //
       
   548 CNSPSession& CNSPController::FindSessionObjectL( TUint aSessionId )
       
   549     {
       
   550     const TInt index = FindSession( aSessionId );
       
   551     
       
   552     if ( KErrNotFound == index )
       
   553         {
       
   554         NSPLOG_STR( "CNSPController::FindSessionObjectL(), KErrNotFound" )
       
   555         User::Leave( KErrNotFound );
       
   556         }
       
   557     
       
   558     return *iSessionArray[index];
       
   559     }
       
   560 
       
   561 
       
   562 // ---------------------------------------------------------------------------
       
   563 // CNSPController::FindSession
       
   564 // ---------------------------------------------------------------------------
       
   565 //
       
   566 TInt CNSPController::FindSession( TUint aSessionId )
       
   567     {
       
   568     const TInt sessionCount( iSessionArray.Count() );
       
   569     for ( TInt index = 0; index < sessionCount; index++ )
       
   570         {
       
   571         if ( iSessionArray[index]->SessionId() == aSessionId )
       
   572             {
       
   573             return index;
       
   574             }
       
   575         }
       
   576     
       
   577     return KErrNotFound;
       
   578     }
       
   579 
       
   580 
       
   581 // ---------------------------------------------------------------------------
       
   582 // CNSPController::FindClosingSession
       
   583 // ---------------------------------------------------------------------------
       
   584 //
       
   585 TInt CNSPController::FindClosingSession( TUint aSessionId )
       
   586     {
       
   587     const TInt sessionCount( iClosingSessionArray.Count() );
       
   588     for ( TInt index = 0; index < sessionCount; index++ )
       
   589         {
       
   590         if ( iClosingSessionArray[index]->SessionId() == aSessionId )
       
   591             {
       
   592             return index;
       
   593             }
       
   594         }
       
   595     
       
   596     return KErrNotFound;
       
   597     }
       
   598 
       
   599 
       
   600 // ---------------------------------------------------------------------------
       
   601 // CNSPController::FindActiveObject
       
   602 // ---------------------------------------------------------------------------
       
   603 //
       
   604 TInt CNSPController::FindActiveObject( TUint aTransactionId )
       
   605     {
       
   606     if( aTransactionId != 0 )
       
   607         {
       
   608         const TInt activeObjectsCount( iActiveObjects.Count() );
       
   609         for ( TInt index = 0 ; index < activeObjectsCount; index++ )
       
   610             {
       
   611             if ( iActiveObjects[index]->TransactionId() == aTransactionId )
       
   612                 {
       
   613                 return index;
       
   614                 }
       
   615             }
       
   616         }
       
   617     return KErrNotFound;
       
   618     }
       
   619 
       
   620 
       
   621 // ---------------------------------------------------------------------------
       
   622 // CNSPController::RemoveActiveObjects
       
   623 // ---------------------------------------------------------------------------
       
   624 //
       
   625 void CNSPController::RemoveActiveObjects( TUint aSessionId )
       
   626     {
       
   627     const TInt activeObjectsCount( iActiveObjects.Count() );
       
   628     for ( TInt index = 0 ; index < activeObjectsCount; index++ )
       
   629         {
       
   630         if ( iActiveObjects[index]->SessionId() == aSessionId )
       
   631             {
       
   632             iActiveObjects[index]->Cancel();
       
   633             delete (iActiveObjects[index]);
       
   634             iActiveObjects.Remove(index);
       
   635             iActiveObjects.Compress();
       
   636             }
       
   637         }
       
   638     }
       
   639 
       
   640 
       
   641 // ---------------------------------------------------------------------------
       
   642 // CNSPController::RandomByteL
       
   643 // ---------------------------------------------------------------------------
       
   644 //
       
   645 TUint8 CNSPController::RandomByteL() const
       
   646     {
       
   647     TBuf8< KNSPSingleDigitLength > buffer( KNSPSingleDigit );
       
   648     GenerateRandomBytesL( buffer ); // random byte received, as TDes8
       
   649     return static_cast< TUint8 >( buffer[0] );
       
   650     }
       
   651 
       
   652 
       
   653 // ---------------------------------------------------------------------------
       
   654 // CNSPController::UseIceL
       
   655 // ---------------------------------------------------------------------------
       
   656 //
       
   657 TBool CNSPController::UseIceL( CDesC8Array& aArray, const TDesC8& aDomain ) const
       
   658     {
       
   659     CRepository* cenrep = CRepository::NewLC( KCRUidUNSAFProtocols );
       
   660     
       
   661     RArray<TUint32> keys;
       
   662     CleanupClosePushL( keys );
       
   663     
       
   664     User::LeaveIfError( cenrep->FindEqL( KUNSAFProtocolsDomainMask,
       
   665             KUNSAFProtocolsFieldTypeMask, aDomain, keys ) );
       
   666     __ASSERT_ALWAYS( keys.Count(), User::Leave( KErrNotFound ) );
       
   667     
       
   668     TUint32 currentDomainKey = KErrNone;
       
   669     currentDomainKey = KUNSAFProtocolsDomainMask^keys[0];
       
   670     currentDomainKey |= KUNSAFProtocolsUsedNATProtocolMask;
       
   671     
       
   672     TBuf8<1> tmp;
       
   673     TInt actualLength( 0 );
       
   674     cenrep->Get( currentDomainKey, tmp, actualLength ); // ret KErrOverFlow
       
   675     
       
   676     if ( 0 < actualLength )
       
   677         {
       
   678         HBufC8* protocol = HBufC8::NewLC( actualLength );
       
   679         TPtr8 ptr( protocol->Des() );
       
   680         User::LeaveIfError( cenrep->Get( currentDomainKey, ptr ) );
       
   681         ReplaceArrayL( aArray, protocol->Des() ); // leave if empty
       
   682         CleanupStack::PopAndDestroy( protocol );
       
   683         }
       
   684     else
       
   685         {
       
   686         User::Leave( KErrNotFound );
       
   687         }
       
   688     
       
   689     CleanupStack::PopAndDestroy(); // keys
       
   690     CleanupStack::PopAndDestroy( cenrep );
       
   691     
       
   692     return IsIce( aArray.MdcaPoint( 0 ) );
       
   693     }
       
   694 
       
   695 
       
   696 // ---------------------------------------------------------------------------
       
   697 // CNSPController::ReplaceArrayL
       
   698 // ---------------------------------------------------------------------------
       
   699 //
       
   700 void CNSPController::ReplaceArrayL( CDesC8Array& aDesArray,
       
   701                                                 const TDesC8& aString ) const
       
   702     {
       
   703     aDesArray.Reset();
       
   704     TPtrC8 ptr( TrimAll( *( aString.AllocLC() ) ) );
       
   705     
       
   706     while ( ptr.Length() )
       
   707         {
       
   708         const TInt length = ptr.Length();
       
   709         const TInt index = ptr.Find( TPtrC8( KComma ) );
       
   710         const TInt left = KErrNotFound != index ? index : length;
       
   711         const TInt right = ( length - KCommaLength ) - left;
       
   712         aDesArray.AppendL( TrimAll( *( ptr.Left( left ).AllocLC() ) ) );
       
   713         CleanupStack::PopAndDestroy();
       
   714         ptr.Set( ptr.Right( 0 < right ? right : 0 ) );
       
   715         }
       
   716     
       
   717     CleanupStack::PopAndDestroy();
       
   718     User::LeaveIfError( aDesArray.Count() ? KErrNone : KErrArgument );
       
   719     }
       
   720 
       
   721 
       
   722 // ---------------------------------------------------------------------------
       
   723 // CNSPController::RemoveIce
       
   724 // ---------------------------------------------------------------------------
       
   725 //
       
   726 void CNSPController::RemoveIce( CDesC8Array& aDesArray ) const
       
   727     {
       
   728     for ( TInt index = 0; index < aDesArray.MdcaCount(); )
       
   729         {
       
   730         if ( IsIce( aDesArray.MdcaPoint( index ) ) )
       
   731             {
       
   732             aDesArray.Delete( index );
       
   733             aDesArray.Compress();
       
   734             }
       
   735         else
       
   736             {
       
   737             index++;
       
   738             }
       
   739         }
       
   740     }
       
   741 
       
   742 
       
   743 // ---------------------------------------------------------------------------
       
   744 // CNSPController::IsIce
       
   745 // ---------------------------------------------------------------------------
       
   746 //
       
   747 TBool CNSPController::IsIce( const TDesC8& aProtocol ) const
       
   748     {
       
   749     TInt index = aProtocol.Find( TPtrC8( KDot ) );
       
   750     
       
   751     if ( KErrNotFound !=  index )
       
   752         {
       
   753         const TInt right = ( aProtocol.Length() - index ) - KCommaLength;
       
   754         TPtrC8 ptr( aProtocol.Right( 0 < right ? right : 0 ) );
       
   755         return ( !ptr.CompareF( KIce() ) ? ETrue : EFalse );
       
   756         }
       
   757     else
       
   758         {
       
   759         return EFalse;
       
   760         }
       
   761     }
       
   762 
       
   763 
       
   764 // ---------------------------------------------------------------------------
       
   765 // CNSPController::Callback
       
   766 // ---------------------------------------------------------------------------
       
   767 //
       
   768 void CNSPController::Callback( const CNSPSession& aSession,
       
   769                                         TEventReturnStatus& aCallback ) const
       
   770 	{
       
   771 	switch ( aCallback.iType )
       
   772 		{
       
   773 		case TEventReturnStatus::ENone:
       
   774 			{
       
   775 			// NOP
       
   776 			break;
       
   777 			}
       
   778 		
       
   779 		case TEventReturnStatus::EInitialized:
       
   780 			{
       
   781 			ExecuteInitialized( aSession );
       
   782 			break;
       
   783 			}
       
   784 		
       
   785 		case TEventReturnStatus::EOfferReady:
       
   786 			{
       
   787 			ExecuteOfferReady( aSession, aCallback.iOffer );
       
   788 			break;
       
   789 			}
       
   790 		
       
   791 		case TEventReturnStatus::EAnswerReady:
       
   792 			{
       
   793 			ExecuteAnswerReady( aSession, aCallback.iAnswer );
       
   794 			break;
       
   795 			}
       
   796 		
       
   797 		case TEventReturnStatus::EUpdateSdp:
       
   798 			{
       
   799 			ExecuteUpdateSdp( aSession, aCallback.iOffer );
       
   800 			break;
       
   801 			}
       
   802 		
       
   803 		case TEventReturnStatus::EError:
       
   804 			{
       
   805             ExecuteErrorOccurred( aSession, aCallback.iStatus );
       
   806 			break;
       
   807 			}
       
   808 		
       
   809 		default:
       
   810 			{
       
   811 			// NOP
       
   812 			}
       
   813 		}
       
   814 	}
       
   815 
       
   816 
       
   817 // ---------------------------------------------------------------------------
       
   818 // CNSPController::ExecuteInitialized
       
   819 // ---------------------------------------------------------------------------
       
   820 //
       
   821 void CNSPController::ExecuteInitialized( const CNSPSession& aSession ) const
       
   822 	{
       
   823 	MNSPSessionObserver& observer = aSession.SessionObserver();
       
   824 	TUint sessionId = aSession.SessionId();
       
   825 	
       
   826 	NSP_OUTPUT_INITIALIZED
       
   827 	observer.Initialized( sessionId );
       
   828 	}
       
   829 
       
   830 
       
   831 // ---------------------------------------------------------------------------
       
   832 // CNSPController::ExecuteOfferReady
       
   833 // ---------------------------------------------------------------------------
       
   834 //
       
   835 void CNSPController::ExecuteOfferReady( const CNSPSession& aSession,
       
   836 		CSdpDocument* aOffer ) const
       
   837 	{
       
   838 	MNSPSessionObserver& observer = aSession.SessionObserver();
       
   839 	TUint sessionId = aSession.SessionId();
       
   840 	
       
   841 	if ( aOffer )
       
   842 		{
       
   843 		NSP_OUTPUT_OFFER( KNatReady, *aOffer )
       
   844 		observer.OfferReady( sessionId, aOffer );
       
   845 		}
       
   846 	else
       
   847 		{
       
   848 		ExecuteErrorOccurred( aSession, KErrTotalLossOfPrecision );
       
   849 		}
       
   850 	}
       
   851 
       
   852 
       
   853 // ---------------------------------------------------------------------------
       
   854 // CNSPController::ExecuteAnswerReady
       
   855 // ---------------------------------------------------------------------------
       
   856 //
       
   857 void CNSPController::ExecuteAnswerReady( const CNSPSession& aSession,
       
   858 		CSdpDocument* aAnswer ) const
       
   859 	{
       
   860 	MNSPSessionObserver& observer = aSession.SessionObserver();
       
   861 	TUint sessionId = aSession.SessionId();
       
   862 	
       
   863 	if ( aAnswer )
       
   864 		{
       
   865 		NSP_OUTPUT_ANSWER( KNatReady, *aAnswer )
       
   866 		observer.AnswerReady( sessionId, aAnswer );
       
   867 		}
       
   868 	else
       
   869 		{
       
   870 		ExecuteErrorOccurred( aSession, KErrTotalLossOfPrecision );
       
   871 		}
       
   872 	}
       
   873 
       
   874 
       
   875 // ---------------------------------------------------------------------------
       
   876 // CNSPController::ExecuteUpdateSdp
       
   877 // ---------------------------------------------------------------------------
       
   878 //
       
   879 void CNSPController::ExecuteUpdateSdp( const CNSPSession& aSession,
       
   880 		CSdpDocument* aOffer ) const
       
   881 	{
       
   882 	MNSPSessionObserver& observer = aSession.SessionObserver();
       
   883 	TUint sessionId = aSession.SessionId();
       
   884 	
       
   885 	if ( aOffer )
       
   886 		{
       
   887 		NSP_OUTPUT_UPDATESDP( KNatReady, *aOffer )
       
   888 		observer.UpdateSdp( sessionId, aOffer );
       
   889 		}
       
   890 	else
       
   891 		{
       
   892 		ExecuteErrorOccurred( aSession, KErrTotalLossOfPrecision );
       
   893 		}
       
   894 	}
       
   895 
       
   896 
       
   897 // ---------------------------------------------------------------------------
       
   898 // CNSPController::ExecuteErrorOccurred
       
   899 // ---------------------------------------------------------------------------
       
   900 //
       
   901 void CNSPController::ExecuteErrorOccurred(
       
   902         const CNSPSession& aSession, TInt aError ) const
       
   903 	{
       
   904 	MNSPSessionObserver& observer = aSession.SessionObserver();
       
   905 	TUint sessionId = aSession.SessionId();
       
   906 	
       
   907 	NSP_OUTPUT_ERROR( aError )
       
   908 	
       
   909 	if ( KErrCommsLineFail == aError )
       
   910 		{
       
   911 		observer.IcmpErrorOccurred( sessionId, aError );
       
   912 		}
       
   913 	else
       
   914 		{
       
   915 		observer.ErrorOccurred( sessionId, aError );
       
   916 		}
       
   917 	}
       
   918 
       
   919 // end of file