natfw/natfwicecandidatehandler/src/cicenatplugincontainer.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 "natfwpluginapi.h"
       
    22 #include "natfwcandidatepair.h"
       
    23 #include "natfwcandidate.h"
       
    24 #include <cnatfwsettingsapi.h>
       
    25 #include <mnatfwicesettings.h>
       
    26 #include <badesca.h>
       
    27 #include "cicenatplugincontainer.h"
       
    28 #include "mncmconnectionmultiplexer.h"
       
    29 #include "cicesessiondata.h"
       
    30 #include "cicehostresolver.h"
       
    31 #include "icecandidatehandlerdefs.h"
       
    32 #include "icecandidatehandlerlogs.h"
       
    33 
       
    34 const TInt KMaxNumberOfUtilityPlugins   = 2;
       
    35 
       
    36 // ======== MEMBER FUNCTIONS ========
       
    37 
       
    38 CIceNatPluginContainer::CIceNatPluginContainer( 
       
    39         const CIceSessionData& aSessionData,
       
    40         MNcmConnectionMultiplexer& aMultiplexer )
       
    41         :
       
    42         iInitParams( *this, aMultiplexer, 
       
    43             aSessionData.Domain(), aSessionData.IapId() )
       
    44     {
       
    45     }
       
    46 
       
    47 
       
    48 void CIceNatPluginContainer::ConstructL()
       
    49     {
       
    50     __ICEDP( "CIceNatPluginContainer::ConstructL" )
       
    51     
       
    52     TInt startInd = MIceNatPluginEventObs::EFirstPluginEventInd;
       
    53     TInt stopInd = MIceNatPluginEventObs::ELastPluginEventInd;
       
    54     for ( TInt i = startInd; i <= stopInd; ++i )
       
    55         {
       
    56         RPointerArray<MIceNatPluginEventObs>* arrayForEvent 
       
    57             = new ( ELeave ) RPointerArray<MIceNatPluginEventObs>;
       
    58         CleanupClosePushL( *arrayForEvent );
       
    59         iObserversForEvent.AppendL( arrayForEvent );
       
    60         CleanupStack::Pop( arrayForEvent ); // lint #429
       
    61         }
       
    62     
       
    63     ResolvePluginsL();
       
    64     }
       
    65 
       
    66 
       
    67 CIceNatPluginContainer* CIceNatPluginContainer::NewL( 
       
    68         const CIceSessionData& aSessionData,
       
    69         MNcmConnectionMultiplexer& aMultiplexer )
       
    70     {
       
    71     CIceNatPluginContainer* self 
       
    72         = CIceNatPluginContainer::NewLC( aSessionData, aMultiplexer );
       
    73     CleanupStack::Pop( self );
       
    74     return self;
       
    75     }
       
    76 
       
    77 
       
    78 CIceNatPluginContainer* CIceNatPluginContainer::NewLC( 
       
    79         const CIceSessionData& aSessionData,
       
    80         MNcmConnectionMultiplexer& aMultiplexer )
       
    81     {
       
    82     CIceNatPluginContainer* self 
       
    83         = new( ELeave ) CIceNatPluginContainer( aSessionData, aMultiplexer );
       
    84     CleanupStack::PushL( self );
       
    85     self->ConstructL();
       
    86     return self;
       
    87     }
       
    88 
       
    89 
       
    90 CIceNatPluginContainer::~CIceNatPluginContainer()
       
    91     {
       
    92     __ICEDP( "CIceNatPluginContainer::~CIceNatPluginContainer" )
       
    93     
       
    94     TInt count( iObserversForEvent.Count() );
       
    95     for ( TInt i = 0; i < count; ++i )
       
    96         {
       
    97         iObserversForEvent[i]->Close();
       
    98         }
       
    99     
       
   100     iObserversForEvent.ResetAndDestroy();
       
   101     
       
   102     iPlugins.ResetAndDestroy();
       
   103     
       
   104     REComSession::FinalClose();
       
   105     }
       
   106 
       
   107 
       
   108 // ---------------------------------------------------------------------------
       
   109 // CIceNatPluginContainer::ConnectPluginsL
       
   110 // ---------------------------------------------------------------------------
       
   111 //
       
   112 void CIceNatPluginContainer::ConnectPluginsL( const RSocketServ& aSocketServ,
       
   113         const TName& aConnectionName )
       
   114     {
       
   115     __ICEDP( "CIceNatPluginContainer::ConnectPluginsL" )
       
   116     
       
   117     for ( TInt i( iPlugins.Count() - 1 ); i >= 0; --i )
       
   118         {
       
   119         TRAPD( error,
       
   120             iPlugins[i]->ConnectServerL( aSocketServ, aConnectionName ) );
       
   121         
       
   122         if ( KErrNone != error )
       
   123             {
       
   124             delete iPlugins[i];
       
   125             iPlugins.Remove( i );
       
   126             }
       
   127         }
       
   128     
       
   129     __ASSERT_ALWAYS( iPlugins.Count(), User::Leave( KErrCouldNotConnect ) );
       
   130     }
       
   131 
       
   132 
       
   133 // ---------------------------------------------------------------------------
       
   134 // CIceNatPluginContainer::Count
       
   135 // ---------------------------------------------------------------------------
       
   136 //
       
   137 TInt CIceNatPluginContainer::Count() const
       
   138     {
       
   139     return iPlugins.Count();
       
   140     }
       
   141 
       
   142 
       
   143 // ---------------------------------------------------------------------------
       
   144 // CIceNatPluginContainer::RegObserverForEventL
       
   145 // ---------------------------------------------------------------------------
       
   146 //
       
   147 void CIceNatPluginContainer::RegObserverForEventL( 
       
   148         const MIceNatPluginEventObs& aObserver,
       
   149         MIceNatPluginEventObs::TNatPluginEvent aEvent )
       
   150     {
       
   151     __ICEDP( "CIceNatPluginContainer::RegObserverForEventL" )
       
   152     __ASSERT_DEBUG( aEvent < iObserversForEvent.Count(), 
       
   153         User::Leave( KErrArgument ) );
       
   154     
       
   155     iObserversForEvent[aEvent]->InsertInAddressOrderL( &aObserver );
       
   156     }
       
   157 
       
   158 
       
   159 // ---------------------------------------------------------------------------
       
   160 // CIceNatPluginContainer::UnregObserverForEvent
       
   161 // ---------------------------------------------------------------------------
       
   162 //
       
   163 void CIceNatPluginContainer::UnregObserverForEvent( 
       
   164         const MIceNatPluginEventObs& aObserver,
       
   165         MIceNatPluginEventObs::TNatPluginEvent aEvent )
       
   166     {
       
   167     __ICEDP( "CIceNatPluginContainer::UnregObserverForEvent" )
       
   168     ASSERT( aEvent < iObserversForEvent.Count() );
       
   169     
       
   170     TInt ind = iObserversForEvent[aEvent]->Find( &aObserver );
       
   171     if ( KErrNotFound != ind )
       
   172         {
       
   173         iObserversForEvent[aEvent]->Remove( ind );
       
   174         }
       
   175     else
       
   176         {
       
   177         ASSERT( EFalse );
       
   178         }
       
   179     }
       
   180 
       
   181 
       
   182 // ---------------------------------------------------------------------------
       
   183 // CIceNatPluginContainer::UnregObserver
       
   184 // Unregisters observer for all events.
       
   185 // ---------------------------------------------------------------------------
       
   186 //
       
   187 void CIceNatPluginContainer::UnregObserver( 
       
   188         const MIceNatPluginEventObs& aObserver )
       
   189     {
       
   190     __ICEDP( "CIceNatPluginContainer::UnregObserver" )
       
   191     
       
   192     TInt numOfEvents = iObserversForEvent.Count();
       
   193     for ( TInt i( numOfEvents - 1 ); i >= 0; --i )
       
   194         {
       
   195         TInt ind = iObserversForEvent[i]->Find( &aObserver );
       
   196         if ( KErrNotFound != ind )
       
   197             {
       
   198             iObserversForEvent[i]->Remove( ind );
       
   199             }
       
   200         }
       
   201     }
       
   202 
       
   203 
       
   204 // ---------------------------------------------------------------------------
       
   205 // CIceNatPluginContainer::ReleasePlugins
       
   206 // Host plugin is always preserved.
       
   207 // ---------------------------------------------------------------------------
       
   208 //
       
   209 void CIceNatPluginContainer::ReleasePlugins( TBool aRetainRelay )
       
   210     {
       
   211     __ICEDP_INT1( "CIceNatPluginContainer::ReleasePlugins, RETAINRELAY:",
       
   212         aRetainRelay )
       
   213     
       
   214     for ( TInt i( iPlugins.Count() - 1 ); i >= 0; --i )
       
   215         {
       
   216         TPtrC8 type = ParsePluginType( iPlugins[i]->PluginIdentifier() );
       
   217         
       
   218         if ( !( aRetainRelay && KNatPluginTypeTurn() == type )
       
   219             && ( KNatPluginTypeHost() != type ) )
       
   220             {
       
   221             delete iPlugins[i];
       
   222             iPlugins.Remove( i );
       
   223             }
       
   224         }
       
   225     }
       
   226 
       
   227 
       
   228 // ---------------------------------------------------------------------------
       
   229 // CIceNatPluginContainer::PluginByIdentifier
       
   230 // ---------------------------------------------------------------------------
       
   231 //
       
   232 CNATFWPluginApi* CIceNatPluginContainer::PluginByIdentifier( 
       
   233         const TDesC8& aIdentifier )
       
   234     {
       
   235     __ICEDP_STR( "CIceNatPluginContainer::PluginByIdentifier", aIdentifier )
       
   236     
       
   237     for ( TInt i( 0 ); i < iPlugins.Count(); ++i )
       
   238         {
       
   239         CNATFWPluginApi* item = iPlugins[i];
       
   240         if ( item->PluginIdentifier() == aIdentifier )
       
   241             {
       
   242             return item;
       
   243             }
       
   244         }
       
   245     
       
   246     return NULL;
       
   247     }
       
   248 
       
   249 
       
   250 // ---------------------------------------------------------------------------
       
   251 // CIceNatPluginContainer::PluginByType
       
   252 // ---------------------------------------------------------------------------
       
   253 //
       
   254 CNATFWPluginApi* CIceNatPluginContainer::PluginByType( 
       
   255         const TDesC8& aType )
       
   256     {
       
   257     __ICEDP_STR( "CIceNatPluginContainer::PluginByType", aType )
       
   258     
       
   259     TInt numOfPlugins = iPlugins.Count();
       
   260     for ( TInt i( 0 ); i < numOfPlugins; ++i )
       
   261         {
       
   262         CNATFWPluginApi* item = iPlugins[i];
       
   263         
       
   264         TPtrC8 type = ParsePluginType( item->PluginIdentifier() );
       
   265         if ( type == aType )
       
   266             {
       
   267             return item;
       
   268             }
       
   269         }
       
   270     
       
   271     return NULL;
       
   272     }
       
   273 
       
   274 
       
   275 // ---------------------------------------------------------------------------
       
   276 // From class MNATFWPluginObserver.
       
   277 // CIceNatPluginContainer::Error
       
   278 // ---------------------------------------------------------------------------
       
   279 //
       
   280 void CIceNatPluginContainer::Error( const CNATFWPluginApi& aPlugin,
       
   281         TUint aStreamId, TInt aErrorCode )
       
   282     {
       
   283     __ICEDP( "CIceNatPluginContainer::Error" )
       
   284     
       
   285     RPointerArray<MIceNatPluginEventObs>& observers = 
       
   286         *iObserversForEvent[MIceNatPluginEventObs::EGeneralError];
       
   287     
       
   288     TInt numOfObservers( observers.Count() );
       
   289     for ( TInt i = 0; i < numOfObservers; ++i )
       
   290         {
       
   291         observers[i]->PluginEventOccured( &aPlugin, aStreamId,
       
   292             MIceNatPluginEventObs::EGeneralError, 
       
   293             aErrorCode, NULL );
       
   294         }
       
   295     }
       
   296 
       
   297 
       
   298 // ---------------------------------------------------------------------------
       
   299 // From class MNATFWPluginObserver.
       
   300 // CIceNatPluginContainer::Notify
       
   301 // ---------------------------------------------------------------------------
       
   302 //
       
   303 void CIceNatPluginContainer::Notify( const CNATFWPluginApi& aPlugin,
       
   304         TUint aStreamId, TNATFWPluginEvent aEvent, TInt aErrCode )
       
   305     {
       
   306     __ICEDP( "CIceNatPluginContainer::Notify" )
       
   307     
       
   308     MIceNatPluginEventObs::TNatPluginEvent convertedEvent;
       
   309     TInt error = GetConvertedEvent( aEvent, convertedEvent );
       
   310     if ( KErrNone == error )
       
   311         {
       
   312         RPointerArray<MIceNatPluginEventObs>& observers = 
       
   313             *iObserversForEvent[convertedEvent];
       
   314         
       
   315         TInt numOfObservers = observers.Count();
       
   316         for ( TInt i = 0; i < numOfObservers; ++i )
       
   317             {
       
   318             observers[i]->PluginEventOccured( &aPlugin,
       
   319                 aStreamId, convertedEvent, aErrCode, NULL );
       
   320             }
       
   321         }
       
   322     }
       
   323 
       
   324 
       
   325 // ---------------------------------------------------------------------------
       
   326 // From class MNATFWPluginObserver.
       
   327 // CIceNatPluginContainer::NewCandidatePairFound
       
   328 // ---------------------------------------------------------------------------
       
   329 //
       
   330 void CIceNatPluginContainer::NewCandidatePairFound( 
       
   331         const CNATFWPluginApi& aPlugin,
       
   332         CNATFWCandidatePair* aPair )
       
   333     {
       
   334     __ICEDP( "CIceNatPluginContainer::NewCandidatePairFound" )
       
   335     
       
   336     if ( aPair )
       
   337         {
       
   338         RPointerArray<MIceNatPluginEventObs>& observers = 
       
   339             *iObserversForEvent[MIceNatPluginEventObs::ECandidatePairFound];
       
   340         
       
   341         TInt numOfObservers = observers.Count();
       
   342         for ( TInt i = 0; i < numOfObservers; ++i )
       
   343             {
       
   344             CNATFWCandidatePair* pair( NULL );
       
   345             TRAP_IGNORE( ( pair = CNATFWCandidatePair::NewL( *aPair ) ) )
       
   346             if ( pair )
       
   347                 {
       
   348                 observers[i]->PluginEventOccured( 
       
   349                     &aPlugin,
       
   350                     aPair->LocalCandidate().StreamId(),
       
   351                     MIceNatPluginEventObs::ECandidatePairFound, 
       
   352                     KErrNone, pair );
       
   353                 }
       
   354             }
       
   355         
       
   356         delete aPair;
       
   357         }
       
   358     else
       
   359         {
       
   360         __ICEDP( "CIceNatPluginContainer::NewCandidatePairFound, NULL" )
       
   361         }
       
   362     }
       
   363 
       
   364 
       
   365 // ---------------------------------------------------------------------------
       
   366 // From class MNATFWPluginObserver.
       
   367 // CIceNatPluginContainer::NewLocalCandidateFound
       
   368 // ---------------------------------------------------------------------------
       
   369 //
       
   370 void CIceNatPluginContainer::NewLocalCandidateFound( 
       
   371         const CNATFWPluginApi& aPlugin,
       
   372         CNATFWCandidate* aCandidate )
       
   373     {
       
   374     __ICEDP( "CIceNatPluginContainer::NewLocalCandidateFound" )
       
   375     
       
   376     if ( aCandidate )
       
   377         {
       
   378         RPointerArray<MIceNatPluginEventObs>& observers = 
       
   379             *iObserversForEvent[MIceNatPluginEventObs::ELocalCandidateFound];
       
   380         
       
   381         TInt numOfObservers = observers.Count();
       
   382         for ( TInt i = 0; i < numOfObservers; ++i )
       
   383             {
       
   384             CNATFWCandidate* candidate( NULL );
       
   385             TRAP_IGNORE( ( candidate = CNATFWCandidate::NewL( *aCandidate ) ) )
       
   386             
       
   387             if ( candidate )
       
   388                 {
       
   389                 observers[i]->PluginEventOccured( 
       
   390                     &aPlugin,
       
   391                     candidate->StreamId(),
       
   392                     MIceNatPluginEventObs::ELocalCandidateFound, 
       
   393                     KErrNone, candidate );
       
   394                 }
       
   395             }
       
   396         
       
   397         delete aCandidate;
       
   398         }
       
   399     else
       
   400         {
       
   401         __ICEDP( "CIceNatPluginContainer::NewLocalCandidateFound, NULL" )
       
   402         }
       
   403     }
       
   404 
       
   405 
       
   406 // ---------------------------------------------------------------------------
       
   407 // CIceNatPluginContainer::ResolvePluginsL
       
   408 // ---------------------------------------------------------------------------
       
   409 //
       
   410 void CIceNatPluginContainer::ResolvePluginsL()
       
   411     {
       
   412     __ICEDP( "CIceNatPluginContainer::ResolvePluginsL" )
       
   413     
       
   414     CNATFWNatSettingsApi* natSettings = CNATFWNatSettingsApi::NewL( 
       
   415         iInitParams.iDomain );
       
   416     CleanupStack::PushL( natSettings );
       
   417     const MNATFWIceSettings& settings = natSettings->IceSettingsL();
       
   418     
       
   419     
       
   420     TInt hostPreference( MNATFWIceSettings::KPrefValueDisabled );
       
   421     settings.GetPrefValue( MNATFWIceSettings::EPrefHostCandidate,
       
   422         hostPreference );
       
   423     if ( MNATFWIceSettings::KPrefValueDisabled != hostPreference )
       
   424         {
       
   425         // Create host plugin
       
   426         CNATFWPluginApi* hostPlugin 
       
   427             = CIceHostResolver::NewL( &iInitParams );
       
   428         CleanupStack::PushL( hostPlugin );
       
   429         iPlugins.AppendL( hostPlugin );
       
   430         CleanupStack::Pop( hostPlugin );
       
   431         }
       
   432     
       
   433     // instantiate utility plugins
       
   434     const CDesC8Array& utilityPluginIds = settings.NatUtilityPlugins();
       
   435     TInt idCount( utilityPluginIds.Count() );
       
   436     __ASSERT_ALWAYS( idCount <= KMaxNumberOfUtilityPlugins, 
       
   437         User::Leave( KErrCorrupt ) );
       
   438     
       
   439     TInt stunPreference( MNATFWIceSettings::KPrefValueDisabled );
       
   440     settings.GetPrefValue( 
       
   441         MNATFWIceSettings::EPrefServerReflexiveCandidate, stunPreference );
       
   442     TInt relayPreference( MNATFWIceSettings::KPrefValueDisabled );
       
   443     settings.GetPrefValue( 
       
   444         MNATFWIceSettings::EPrefRelayCandidate, relayPreference );
       
   445     
       
   446     for ( TInt i( 0 ); i < idCount; ++i )
       
   447         {
       
   448         TPtrC8 type = ParsePluginType( utilityPluginIds[i] );
       
   449         if ( ( type.CompareF( KNatPluginTypeStun ) == 0 
       
   450                 && MNATFWIceSettings::KPrefValueDisabled != stunPreference )
       
   451             || ( type.CompareF( KNatPluginTypeTurn ) == 0 
       
   452                 && MNATFWIceSettings::KPrefValueDisabled != relayPreference ) )
       
   453             {
       
   454             CNATFWPluginApi* plugin = CNATFWPluginApi::NewL( 
       
   455                 utilityPluginIds[i], iInitParams );
       
   456             CleanupStack::PushL( plugin );
       
   457             iPlugins.AppendL( plugin );
       
   458             CleanupStack::Pop( plugin );
       
   459             }        
       
   460         }
       
   461     
       
   462     CleanupStack::PopAndDestroy( natSettings );
       
   463     }
       
   464 
       
   465 
       
   466 // ---------------------------------------------------------------------------
       
   467 // CIceNatPluginContainer::GetConvertedEvent
       
   468 // ---------------------------------------------------------------------------
       
   469 //
       
   470 TInt CIceNatPluginContainer::GetConvertedEvent( TNATFWPluginEvent aEvent, 
       
   471         MIceNatPluginEventObs::TNatPluginEvent& aConvertedEvent ) const
       
   472     {
       
   473     switch ( aEvent )
       
   474         {
       
   475         case MNATFWPluginObserver::EServerConnected:
       
   476             aConvertedEvent = MIceNatPluginEventObs::EServerConnected;
       
   477             break;
       
   478         case MNATFWPluginObserver::EFetchingCompleted:
       
   479             aConvertedEvent = MIceNatPluginEventObs::EFetchingCompleted;
       
   480             break;
       
   481         case MNATFWPluginObserver::ETcpConnSetupCompleted:
       
   482             aConvertedEvent = MIceNatPluginEventObs::ETcpConnSetupCompleted;
       
   483             break;
       
   484         case MNATFWPluginObserver::EReceivingActivated:
       
   485             aConvertedEvent = MIceNatPluginEventObs::EReceivingActivated;
       
   486             break;
       
   487         case MNATFWPluginObserver::EReceivingDeactivated:
       
   488             aConvertedEvent = MIceNatPluginEventObs::EReceivingDeactivated;
       
   489             break;
       
   490         case MNATFWPluginObserver::ESendingActivated:
       
   491             aConvertedEvent = MIceNatPluginEventObs::ESendingActivated;
       
   492             break;
       
   493         case MNATFWPluginObserver::ESendingDeactivated:
       
   494             aConvertedEvent = MIceNatPluginEventObs::ESendingDeactivated;
       
   495             break;
       
   496         case MNATFWPluginObserver::EConnChecksCompleted:
       
   497             aConvertedEvent = MIceNatPluginEventObs::EConnChecksCompleted;
       
   498             break;
       
   499         default:
       
   500             __ICEDP_INT1( "CIceNatPluginContainer::GetConvertedEvent DEF:",
       
   501                 aEvent )
       
   502             
       
   503             return KErrArgument;
       
   504         }
       
   505     
       
   506     return KErrNone;
       
   507     }
       
   508 
       
   509 
       
   510 // ---------------------------------------------------------------------------
       
   511 // CIceNatPluginContainer::ParsePluginType
       
   512 // ---------------------------------------------------------------------------
       
   513 //
       
   514 TPtrC8 CIceNatPluginContainer::ParsePluginType( 
       
   515         const TDesC8& aIdentifier ) const
       
   516     {
       
   517     TLex8 lex( aIdentifier );
       
   518     while( !lex.Eos() )
       
   519         {
       
   520         if ( lex.Get() == '.' )
       
   521             {
       
   522             lex.Mark();
       
   523             }
       
   524         }
       
   525     
       
   526     return lex.RemainderFromMark();
       
   527     }