realtimenetprots/sipfw/SIP/ConnectionMgr/src/COwnerSettingsList.cpp
changeset 0 307788aac0a8
equal deleted inserted replaced
-1:000000000000 0:307788aac0a8
       
     1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // Name        : COwnerSettingsList.cpp
       
    15 // Part of     : ConnectionMgr
       
    16 // See class definition below.
       
    17 // Version     : SIP/4.0
       
    18 //
       
    19 
       
    20 
       
    21 
       
    22 #include "COwnerSettingsList.h"
       
    23 #include "COwnerSettings.h"
       
    24 #include "CommonConsts.h"
       
    25 #include "siperr.h"
       
    26 
       
    27 const TInt KSettingsGranularity = 2;
       
    28 const TUint KFirstUnreservedPort = KDefaultSipPort+2;
       
    29 
       
    30 // -----------------------------------------------------------------------------
       
    31 // COwnerSettingsList::NewL
       
    32 // -----------------------------------------------------------------------------
       
    33 //
       
    34 COwnerSettingsList* COwnerSettingsList::NewL()
       
    35     {
       
    36     COwnerSettingsList* self = new ( ELeave ) COwnerSettingsList;
       
    37     return self;
       
    38     }
       
    39     
       
    40 // -----------------------------------------------------------------------------
       
    41 // COwnerSettingsList::~COwnerSettingsList
       
    42 // -----------------------------------------------------------------------------
       
    43 //
       
    44 COwnerSettingsList::~COwnerSettingsList()
       
    45     {
       
    46     iSettings.ResetAndDestroy();
       
    47     }
       
    48 
       
    49 // -----------------------------------------------------------------------------
       
    50 // COwnerSettingsList::COwnerSettingsList
       
    51 // -----------------------------------------------------------------------------
       
    52 //    
       
    53 COwnerSettingsList::COwnerSettingsList() :
       
    54     iSettings( KSettingsGranularity )
       
    55     {
       
    56     }
       
    57 
       
    58 // -----------------------------------------------------------------------------
       
    59 // COwnerSettingsList::AddSettingsL
       
    60 // -----------------------------------------------------------------------------
       
    61 //
       
    62 void COwnerSettingsList::AddSettingsL(     
       
    63     const TSIPTransportParams& aParams,
       
    64     TUint aOptionName,
       
    65 	TUint aOptionLevel,
       
    66 	TInt aOption )
       
    67     {
       
    68     COwnerSettings& settings = FindOwnerSettingsL( aParams );
       
    69     settings.AddOptL( aOptionName, aOptionLevel, aOption );
       
    70     }
       
    71     
       
    72 // -----------------------------------------------------------------------------
       
    73 // COwnerSettingsList::AddSettingsL
       
    74 // -----------------------------------------------------------------------------
       
    75 //
       
    76 void COwnerSettingsList::AddSettingsL(     
       
    77     const TSIPTransportParams& aParams,
       
    78     TUint aOptionName,
       
    79 	TUint aOptionLevel,
       
    80 	const TDesC8& aOption )
       
    81     {
       
    82     COwnerSettings& settings = FindOwnerSettingsL( aParams );
       
    83     settings.AddOptL( aOptionName, aOptionLevel, aOption );
       
    84     }
       
    85     
       
    86 // -----------------------------------------------------------------------------
       
    87 // COwnerSettingsList::RemoveSettings
       
    88 // -----------------------------------------------------------------------------
       
    89 //
       
    90 void COwnerSettingsList::RemoveSettings( const TSIPTransportParams& aParams )
       
    91     {
       
    92     // Remove all settings of this owner
       
    93     TInt count = iSettings.Count();
       
    94     for ( TInt i = ( count-1 ); i >= 0; i-- )
       
    95         {
       
    96         if ( iSettings[ i ]->TransportParams().OwnerId() == aParams.OwnerId() )
       
    97             {
       
    98             COwnerSettings* settings = iSettings[ i ];
       
    99             iSettings.Remove( i );
       
   100             delete settings;
       
   101             }
       
   102         }
       
   103     iSettings.Compress();
       
   104     }
       
   105     
       
   106 // -----------------------------------------------------------------------------
       
   107 // COwnerSettingsList::RemoveSettings
       
   108 // -----------------------------------------------------------------------------
       
   109 //
       
   110 TBool COwnerSettingsList::RemoveSettings( TUint32 aTransportId )
       
   111     {
       
   112     // Remove all settings containing passed transportId
       
   113     TBool removed( EFalse );
       
   114     TInt count = iSettings.Count();
       
   115     for ( TInt i = ( count-1 ); i >= 0; i-- )
       
   116         {
       
   117         if ( iSettings[ i ]->TransportParams().TransportId() == aTransportId )
       
   118             {
       
   119             COwnerSettings* settings = iSettings[ i ];
       
   120             iSettings.Remove( i );
       
   121             delete settings;
       
   122             removed = ETrue;
       
   123             }
       
   124         }
       
   125     iSettings.Compress();
       
   126     return removed;
       
   127     }
       
   128 
       
   129 // -----------------------------------------------------------------------------
       
   130 // COwnerSettingsList::IsRequestedOpts
       
   131 // -----------------------------------------------------------------------------
       
   132 //
       
   133 TBool COwnerSettingsList::IsRequestedOpts( 
       
   134     const TSIPTransportParams& aParams ) const
       
   135     {
       
   136     TInt count = iSettings.Count();
       
   137     for ( TInt i = 0; i < count; i++ )
       
   138         {
       
   139         if ( iSettings[ i ]->TransportParams().OwnerId() == aParams.OwnerId() &&
       
   140              iSettings[ i ]->IsOpts() )
       
   141             {
       
   142             return ETrue;
       
   143             }
       
   144         }
       
   145     return EFalse;
       
   146     }
       
   147 
       
   148 // -----------------------------------------------------------------------------
       
   149 // COwnerSettingsList::SetOpts
       
   150 // -----------------------------------------------------------------------------
       
   151 //    
       
   152 void COwnerSettingsList::SetOpts( 
       
   153     const TSIPTransportParams& aParams, 
       
   154     RSocket& aSocket )
       
   155     {
       
   156     // Apply to socket all opts this owner has set
       
   157     TInt count = iSettings.Count();
       
   158     for ( TInt i = 0; i < count; i++ )
       
   159         {
       
   160         if ( iSettings[ i ]->TransportParams().OwnerId() == aParams.OwnerId() )
       
   161             {
       
   162             iSettings[ i ]->SetOpts( aSocket );
       
   163             }
       
   164         }
       
   165     }
       
   166 
       
   167 
       
   168 // -----------------------------------------------------------------------------
       
   169 // COwnerSettingsList::ClearOpts
       
   170 // -----------------------------------------------------------------------------
       
   171 //    
       
   172 void COwnerSettingsList::ClearOpts( 
       
   173     const TSIPTransportParams& aParams, 
       
   174     RSocket& aSocket )
       
   175     {
       
   176     // Apply to socket all opts this owner has set
       
   177     TInt count = iSettings.Count();
       
   178     for ( TInt i = 0; i < count; i++ )
       
   179         {
       
   180         if ( iSettings[ i ]->TransportParams().OwnerId() == aParams.OwnerId() )
       
   181             {
       
   182             iSettings[ i ]->ClearOpts( aSocket );
       
   183             }
       
   184         }
       
   185     }
       
   186         
       
   187 // -----------------------------------------------------------------------------
       
   188 // COwnerSettingsList::SaveTransportInfoL
       
   189 // -----------------------------------------------------------------------------
       
   190 // 
       
   191 TBool COwnerSettingsList::SaveTransportInfoL( 
       
   192     const TSIPTransportParams& aOldParams, 
       
   193     const TSIPTransportParams& aParams, 
       
   194     TUint aLocalSendPort,
       
   195     TUint aLocalReceivePort,
       
   196     const TInetAddr& aRemoteAddr,
       
   197     TUint aRemoteSendPort )
       
   198     { 
       
   199     // If there is already settings matching old tp params and old transport id
       
   200     // is not default, it is allowed to have duplicate reservations between
       
   201     // those two transports since in this case new IPSec record is just changing
       
   202     // local sending port but sharing receiving port.
       
   203     
       
   204     TInt index( KErrNotFound );
       
   205     if ( IsReservedTransport( aOldParams.TransportId() ) )
       
   206         {
       
   207         index = FindOwnerSettingsIndex( aOldParams );
       
   208         }
       
   209 
       
   210     for ( TInt i = 0; i < iSettings.Count(); i++ )
       
   211         {
       
   212         if ( !iSettings[ i ]->IsUnreservedPort( aLocalSendPort ) ||
       
   213            ( i != index && !iSettings[ i ]->IsUnreservedPort( aLocalReceivePort ) ) )
       
   214             {
       
   215             User::Leave( KErrSIPTransportFailure );
       
   216             }
       
   217         }
       
   218             
       
   219     COwnerSettings& settings = FindOwnerSettingsL( aParams );
       
   220     
       
   221     settings.SaveTransportInfoL( aLocalSendPort,
       
   222                                  aLocalReceivePort,
       
   223                                  aRemoteAddr,
       
   224                                  aRemoteSendPort );
       
   225                             
       
   226     settings.AddReservedPortL( aLocalSendPort );
       
   227     settings.AddReservedPortL( aLocalReceivePort );
       
   228     
       
   229     return ( index != KErrNotFound );
       
   230     }
       
   231     
       
   232 // -----------------------------------------------------------------------------
       
   233 // COwnerSettingsList::SaveTLSTransportInfoL
       
   234 // -----------------------------------------------------------------------------
       
   235 // 
       
   236 void COwnerSettingsList::SaveTLSTransportInfoL( 
       
   237     const TSIPTransportParams& aParams )
       
   238     { 
       
   239     COwnerSettings& settings = FindOwnerSettingsL( aParams, EFalse );
       
   240     settings.SetUseTLS( ETrue );
       
   241     }
       
   242 
       
   243 // -----------------------------------------------------------------------------
       
   244 // COwnerSettingsList::ReservePrivatePortL
       
   245 // -----------------------------------------------------------------------------
       
   246 // 
       
   247 void COwnerSettingsList::ReservePrivatePortL( 
       
   248     const TSIPTransportParams& aParams, 
       
   249     TUint aPort )
       
   250     {  
       
   251     // Ignore attempt to reserve default sip port. Also ignore protected
       
   252     // transports since those use SaveTransportInfoL for reserving ports
       
   253     // (two SAs may share same private ports).  
       
   254     if ( aPort == KDefaultSipPort || 
       
   255          IsReservedTransport( aParams.TransportId() ) )
       
   256         {
       
   257         return;
       
   258         }
       
   259     
       
   260     // First check that port is not already reserved 
       
   261     // (it can be reserved for owner)
       
   262     TInt index = FindOwnerSettingsIndex( aParams );
       
   263 
       
   264     for ( TInt i = 0; i < iSettings.Count(); i++ )
       
   265         {
       
   266         if ( i != index && !iSettings[ i ]->IsUnreservedPort( aPort ) )
       
   267             {
       
   268             User::Leave( KErrSIPTransportFailure );
       
   269             }
       
   270         }
       
   271         
       
   272     // Then reserve it
       
   273     COwnerSettings& settings = FindOwnerSettingsL( aParams );   
       
   274     settings.AddReservedPortL( aPort );
       
   275     }
       
   276 
       
   277 // -----------------------------------------------------------------------------
       
   278 // COwnerSettingsList::GetPrivatePort
       
   279 // -----------------------------------------------------------------------------
       
   280 //        
       
   281 TUint COwnerSettingsList::GetPrivatePortL( const TSIPTransportParams& aParams )
       
   282     {
       
   283     // Find first unreserved port
       
   284     TBool found( EFalse );
       
   285     TUint unreservedport = KFirstUnreservedPort;
       
   286     TInt count = iSettings.Count();  
       
   287     for ( TInt i = 0; i < count && !found; i++ )
       
   288         {
       
   289         if ( iSettings[ i ]->IsUnreservedPort( unreservedport ) )
       
   290             {
       
   291             found = ETrue;
       
   292             }
       
   293         else
       
   294             {
       
   295             unreservedport++;
       
   296             }
       
   297         }
       
   298     // Then reserve it
       
   299     COwnerSettings& settings = FindOwnerSettingsL( aParams );   
       
   300     settings.AddReservedPortL( unreservedport );
       
   301     return unreservedport;
       
   302     }
       
   303 
       
   304 // -----------------------------------------------------------------------------
       
   305 // COwnerSettingsList::HasSavedTransportInfo
       
   306 // -----------------------------------------------------------------------------
       
   307 //       
       
   308 TBool COwnerSettingsList::HasSavedTransportInfo( 
       
   309     TUint32 aTransportId ) const
       
   310     {
       
   311     return ( FindOwnerSettingsIndex( aTransportId ) != KErrNotFound );
       
   312     }
       
   313 
       
   314 // -----------------------------------------------------------------------------
       
   315 // COwnerSettingsList::HasSavedTLSTransportInfo
       
   316 // -----------------------------------------------------------------------------
       
   317 //    
       
   318 TBool COwnerSettingsList::HasSavedTLSTransportInfo( TUint32 aTransportId ) const
       
   319     {
       
   320     TInt index( KErrNotFound );
       
   321     if ( ( index = FindOwnerSettingsIndex( aTransportId ) ) != KErrNotFound )
       
   322         {
       
   323         return iSettings[ index ]->UseTLS();
       
   324         }
       
   325     return EFalse;
       
   326     }
       
   327  
       
   328 // -----------------------------------------------------------------------------
       
   329 // COwnerSettingsList::SavedLocalSendPort
       
   330 // -----------------------------------------------------------------------------
       
   331 //      
       
   332 TUint COwnerSettingsList::SavedLocalSendPort( 
       
   333     TUint32 aTransportId ) const
       
   334     {
       
   335     TInt index( KErrNotFound );
       
   336     if ( ( index = FindOwnerSettingsIndex( aTransportId ) ) != KErrNotFound )
       
   337         {
       
   338         return iSettings[ index ]->LocalSendPort();
       
   339         }
       
   340     return 0;
       
   341     }
       
   342     
       
   343 // -----------------------------------------------------------------------------
       
   344 // COwnerSettingsList::SavedLocalReceivePort
       
   345 // -----------------------------------------------------------------------------
       
   346 //      
       
   347 TUint COwnerSettingsList::SavedLocalReceivePort( 
       
   348     TUint32 aTransportId ) const
       
   349     {
       
   350     TInt index( KErrNotFound );
       
   351     if ( ( index = FindOwnerSettingsIndex( aTransportId ) ) != KErrNotFound )
       
   352         {
       
   353         return iSettings[ index ]->LocalReceivePort();
       
   354         }
       
   355     return 0;
       
   356     }
       
   357     
       
   358 // -----------------------------------------------------------------------------
       
   359 // COwnerSettingsList::SavedRemoteAddr
       
   360 // -----------------------------------------------------------------------------
       
   361 //      
       
   362 const TInetAddr* COwnerSettingsList::SavedRemoteAddr( 
       
   363     TUint32 aTransportId ) const
       
   364     {
       
   365     TInt index( KErrNotFound );
       
   366     if ( ( index = FindOwnerSettingsIndex( aTransportId ) ) != KErrNotFound )
       
   367         {
       
   368         return iSettings[ index ]->RemoteAddr();
       
   369         }
       
   370     return 0;
       
   371     }
       
   372  
       
   373 // -----------------------------------------------------------------------------
       
   374 // COwnerSettingsList::IsReceivingAllowed
       
   375 // -----------------------------------------------------------------------------
       
   376 //      
       
   377 TBool COwnerSettingsList::IsReceivingAllowed( 
       
   378     const TSIPTransportParams& aParams, 
       
   379     const TInetAddr& aRemoteAddr,
       
   380     TUint aProtocol,
       
   381     TUint aLocalPort ) const
       
   382     {
       
   383     TBool reservedTransport = IsReservedTransport( aParams.TransportId() );
       
   384     
       
   385     // When TLS is used explicitly, there's no need to do remote address
       
   386     // specific checking
       
   387     if ( reservedTransport && 
       
   388          HasSavedTLSTransportInfo( aParams.TransportId() ) )
       
   389         {
       
   390         return ETrue;
       
   391         }
       
   392     
       
   393     // If transport id is not specified, receiving from protected (reserved)
       
   394     // ports is not allowed. If id is specified, receiving from unprotected
       
   395     // ports is not allowed
       
   396     TBool match( EFalse );
       
   397     for ( TInt i = 0; i < iSettings.Count() && !match; i++ )
       
   398         {
       
   399         if ( iSettings[ i ]->IsMatchingWithSavedInfo( aParams, 
       
   400                                                       aRemoteAddr, 
       
   401                                                       aProtocol,
       
   402                                                       aLocalPort ) )
       
   403             {
       
   404             match = ETrue;
       
   405             }
       
   406         }
       
   407         
       
   408     return ( reservedTransport && match ) || ( !reservedTransport && !match );
       
   409     }
       
   410 
       
   411 // -----------------------------------------------------------------------------
       
   412 // COwnerSettingsList::FindTransportId
       
   413 // -----------------------------------------------------------------------------
       
   414 //     
       
   415 TUint32 COwnerSettingsList::FindTransportId( const TInetAddr& aRemoteAddr ) const
       
   416     {
       
   417     for ( TInt i = 0; i < iSettings.Count(); i++ )
       
   418         {
       
   419         COwnerSettings* settings = iSettings[ i ];
       
   420         if ( settings->IsMatchingWithRemoteSendAddr( aRemoteAddr, ETrue ) )
       
   421             {
       
   422             return settings->TransportParams().TransportId();
       
   423             }
       
   424         }
       
   425     return 0;
       
   426     }
       
   427 
       
   428 // -----------------------------------------------------------------------------
       
   429 // COwnerSettingsList::SaveTLSRemoteL
       
   430 // -----------------------------------------------------------------------------
       
   431 //
       
   432 void COwnerSettingsList::SaveTLSRemoteL( 
       
   433     const TSIPTransportParams& aParams,
       
   434     const TInetAddr& aRemoteAddr )
       
   435     {
       
   436     TInt index( KErrNotFound );
       
   437     if ( ( index = FindOwnerSettingsIndex( 
       
   438             aParams.TransportId() ) ) != KErrNotFound )
       
   439         {
       
   440         iSettings[ index ]->SaveTLSRemoteL( aRemoteAddr );
       
   441         }
       
   442     }
       
   443     
       
   444 // -----------------------------------------------------------------------------
       
   445 // COwnerSettingsList::FindOwnerSettingsL
       
   446 // -----------------------------------------------------------------------------
       
   447 //        
       
   448 COwnerSettings& COwnerSettingsList::FindOwnerSettingsL( 
       
   449     const TSIPTransportParams& aParams, TBool aAllowExisting )
       
   450     {
       
   451     // Find existing settings for this owner or create new entry to the
       
   452     // settings list.
       
   453     TInt index( 0 );
       
   454     if ( ( index = FindOwnerSettingsIndex( aParams ) ) != KErrNotFound )
       
   455         {
       
   456         __ASSERT_ALWAYS( aAllowExisting, User::Leave( KErrAlreadyExists ) );
       
   457         return *iSettings[ index ];
       
   458         }
       
   459 
       
   460     COwnerSettings* settings = COwnerSettings::NewLC( aParams );
       
   461     iSettings.AppendL( settings );
       
   462     CleanupStack::Pop( settings );
       
   463     return *settings;
       
   464     }
       
   465  
       
   466 // -----------------------------------------------------------------------------
       
   467 // COwnerSettingsList::FindOwnerSettingsIndex
       
   468 // -----------------------------------------------------------------------------
       
   469 //         
       
   470 TInt COwnerSettingsList::FindOwnerSettingsIndex( 
       
   471     const TSIPTransportParams& aParams ) const
       
   472     {
       
   473     COwnerSettings* settings = 0;
       
   474     for ( TInt i = 0; i < iSettings.Count(); i++ )
       
   475         {
       
   476         settings = iSettings[ i ];
       
   477         if ( settings->TransportParams().OwnerId() == aParams.OwnerId() &&
       
   478              settings->TransportParams().TransportId() == aParams.TransportId() )
       
   479             {
       
   480             return i;
       
   481             }
       
   482         }
       
   483     return KErrNotFound;
       
   484     }
       
   485     
       
   486 // -----------------------------------------------------------------------------
       
   487 // COwnerSettingsList::FindOwnerSettingsIndex
       
   488 // -----------------------------------------------------------------------------
       
   489 //         
       
   490 TInt COwnerSettingsList::FindOwnerSettingsIndex( 
       
   491     TUint32 aTransportId ) const
       
   492     {
       
   493     if ( !IsReservedTransport( aTransportId ) )
       
   494         {
       
   495         return KErrNotFound;
       
   496         }
       
   497     
       
   498     COwnerSettings* settings = 0;
       
   499     for ( TInt i = 0; i < iSettings.Count(); i++ )
       
   500         {
       
   501         settings = iSettings[ i ];
       
   502         if ( settings->TransportParams().TransportId() == aTransportId )
       
   503             {
       
   504             return i;
       
   505             }
       
   506         }
       
   507     return KErrNotFound;
       
   508     }
       
   509  
       
   510 // -----------------------------------------------------------------------------
       
   511 // COwnerSettingsList::IsReservedTransport
       
   512 // -----------------------------------------------------------------------------
       
   513 //     
       
   514 TBool COwnerSettingsList::IsReservedTransport( TUint32 aTransportId ) const
       
   515     {
       
   516     return ( aTransportId > 0 );
       
   517     }
       
   518     
       
   519 // end of file