multimediacommsengine/mmcesrv/mmceserver/src/mcesipmanager.cpp
changeset 0 1bce908db942
child 12 91f50911ea81
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     1 /*
       
     2 * Copyright (c) 2005 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 <badesca.h>
       
    22 #include <sip.h>
       
    23 #include <sipprofile.h>
       
    24 #include <sipprofileregistry.h>
       
    25 #include <siphttpdigest.h>
       
    26 #include <sipservertransaction.h>
       
    27 #include <siprequestelements.h>
       
    28 #include <siptoheader.h>
       
    29 #include <sipaddress.h>
       
    30 #include <sipmessageelements.h>
       
    31 #include <sipinvitedialogassoc.h>
       
    32 #include <sipnotifydialogassoc.h>
       
    33 #include <sipeventheader.h>
       
    34 #include <sipexpiresheader.h>
       
    35 #include <sipmanagedprofile.h>
       
    36 
       
    37 #include "mcesipstatemachine.h"
       
    38 #include "mcesipmanager.h"
       
    39 #include "mceservercore.h"
       
    40 #include "mcecssession.h"
       
    41 #include "mcecssessionimplementation.h"
       
    42 #include "mcesipsession.h"
       
    43 #include "mcesipconnection.h"
       
    44 #include "mcemediamanager.h"
       
    45 #include "mcenatpluginmanager.h"
       
    46 #include "mcenatsipsession.h"
       
    47 #include "mceclientserver.h"
       
    48 #include "mcesrvlogs.h"
       
    49 #include "mceclient.pan"
       
    50 #include "mcesipevent.h"
       
    51 #include "mceclientresolver.h"
       
    52 #include "mcetimermanager.h"
       
    53 #include "cleanupresetanddestroy.h"
       
    54 
       
    55 
       
    56 #define REJECT( transaction )\
       
    57         MCESRV_DEBUG("REJECTED");\
       
    58         MceSip::DiscardRequest( transaction );\
       
    59         return
       
    60 
       
    61 #define REJECT_IF_ERROR( error, transaction )\
       
    62     if ( error != KErrNone )\
       
    63         {\
       
    64         REJECT( transaction );\
       
    65         }
       
    66 
       
    67 // -----------------------------------------------------------------------------
       
    68 // CMceSipManager::NewL
       
    69 // -----------------------------------------------------------------------------
       
    70 //
       
    71 CMceSipManager* CMceSipManager::NewL (CMceServerCore& aServerCore )
       
    72 	{
       
    73     CMceSipManager* self = CMceSipManager::NewLC( aServerCore );
       
    74     CleanupStack::Pop(self);
       
    75     return self;
       
    76 	}
       
    77 
       
    78 // -----------------------------------------------------------------------------
       
    79 // CMceSipManager::NewLC
       
    80 // -----------------------------------------------------------------------------
       
    81 //
       
    82 CMceSipManager* CMceSipManager::NewLC ( CMceServerCore& aServerCore )
       
    83 	{
       
    84     CMceSipManager* self = new (ELeave) CMceSipManager(aServerCore);
       
    85     CleanupStack::PushL(self);
       
    86     self->ConstructL();
       
    87     return self;
       
    88 	}
       
    89 
       
    90 // -----------------------------------------------------------------------------
       
    91 // CMceSipManager::CMceSipManager
       
    92 // -----------------------------------------------------------------------------
       
    93 //
       
    94 CMceSipManager::CMceSipManager (CMceServerCore& aServerCore)
       
    95 : iServerCore (aServerCore),
       
    96   iNextDialogId( 0 )
       
    97 	{
       
    98 	}
       
    99 
       
   100 // -----------------------------------------------------------------------------
       
   101 // CMceSipManager::ConstructL
       
   102 // -----------------------------------------------------------------------------
       
   103 //
       
   104 void CMceSipManager::ConstructL ()
       
   105 	{
       
   106     MCESRV_DEBUG("CMceSipManager::ConstructL, Entry")
       
   107 	
       
   108     User::LeaveIfError( iSocketServ.Connect() );	
       
   109 	
       
   110 	iSIP = CSIP::NewL( KServerUid3, *this );
       
   111 	iProfileRegistry = CSIPProfileRegistry::NewL( *iSIP, *this );
       
   112                        
       
   113 	iMediaManager = CMceMediaManager::NewL( *this );
       
   114 	iStateMachine = CMceSipStateMachine::NewL();
       
   115 
       
   116 	iClientResolver = CMceClientResolver::NewL( *this );
       
   117 
       
   118 	iOrphanSubSessions = CMceCsSessionImplementation::NewL( *this, NULL );
       
   119 	
       
   120 	iTimerManager = CMceTimerManager::NewL();
       
   121 	
       
   122     MCESRV_DEBUG("CMceSipManager::ConstructL, Exit")
       
   123 	}
       
   124 
       
   125 // -----------------------------------------------------------------------------
       
   126 // CMceSipManager::~CMceSipManager
       
   127 // -----------------------------------------------------------------------------
       
   128 //
       
   129 CMceSipManager::~CMceSipManager()
       
   130 	{
       
   131     MCESRV_DEBUG("CMceSipManager::~CMceSipManager, Entry")
       
   132 	
       
   133     iSIPClientConnections.ResetAndDestroy();
       
   134     iSIPClientConnections.Close();
       
   135     iClients.Reset();
       
   136     iClients.Close();
       
   137 
       
   138     if ( iOrphanSubSessions )
       
   139         {
       
   140         iOrphanSubSessions->RemoveAllSubSessions( EFalse );    
       
   141         }
       
   142     delete iOrphanSubSessions;
       
   143 
       
   144     iProfiles.ResetAndDestroy();
       
   145     iProfiles.Close();
       
   146 
       
   147     delete iMediaManager;
       
   148     delete iStateMachine;
       
   149     delete iProfileRegistry;
       
   150 	delete iClientResolver;
       
   151 	delete iTimerManager;
       
   152     delete iSIP;
       
   153     
       
   154     iSocketServ.Close();
       
   155     
       
   156     MCESRV_DEBUG("CMceSipManager::~CMceSipManager, Exit")
       
   157 	}
       
   158 
       
   159 // -----------------------------------------------------------------------------
       
   160 // CMceSipManager::NextDialogId
       
   161 // -----------------------------------------------------------------------------
       
   162 //
       
   163 TUint32 CMceSipManager::NextDialogId()
       
   164     {
       
   165     iNextDialogId++;
       
   166     return iNextDialogId;
       
   167     }
       
   168 
       
   169 // -----------------------------------------------------------------------------
       
   170 // CMceSipManager::RegisterClientL
       
   171 // -----------------------------------------------------------------------------
       
   172 //
       
   173 void CMceSipManager::RegisterClientL( CMceCsSession& aClientSession )
       
   174     {
       
   175     MCESRV_DEBUG("CMceSipManager::RegisterClientL, Entry")
       
   176     MCESRV_DEBUG_DVALUE("UID", aClientSession.Uid().iUid )
       
   177 
       
   178 	iClientResolver->RegisterClientL( aClientSession.Uid() );
       
   179     iClients.AppendL( &aClientSession );
       
   180 
       
   181     MCESRV_DEBUG("CMceSipManager::RegisterClientL, Exit")
       
   182     }
       
   183 
       
   184 // -----------------------------------------------------------------------------
       
   185 // CMceSipManager::UnRegisterClient
       
   186 // -----------------------------------------------------------------------------
       
   187 //
       
   188 void CMceSipManager::UnRegisterClient( CMceCsSession& aClientSession )
       
   189     {
       
   190     MCESRV_DEBUG("CMceSipManager::UnRegisterClient, Entry")
       
   191 
       
   192 	iClientResolver->DeregisterClient( aClientSession.Uid() );
       
   193     
       
   194     TInt index = iClients.Find( &aClientSession );
       
   195     
       
   196     if ( index >= KErrNone )
       
   197         {
       
   198         iClients.Remove( index );
       
   199         }
       
   200     else
       
   201         {
       
   202         MCESRV_DEBUG("UnRegisterClient: CLIENT NOT FOUND")
       
   203         }
       
   204         
       
   205     MCESRV_DEBUG("CMceSipManager::UnRegisterClient, Exit")
       
   206     }
       
   207         
       
   208 // -----------------------------------------------------------------------------
       
   209 // CMceSipManager::MediaManager
       
   210 // -----------------------------------------------------------------------------
       
   211 //
       
   212 CMceMediaManager& CMceSipManager::MediaManager()
       
   213     {
       
   214     return *iMediaManager;
       
   215     }
       
   216         
       
   217 
       
   218 // -----------------------------------------------------------------------------
       
   219 // CMceSipManager::TimerManager
       
   220 // -----------------------------------------------------------------------------
       
   221 //
       
   222 CMceTimerManager& CMceSipManager::TimerManager()
       
   223     {
       
   224     return *iTimerManager;
       
   225     }
       
   226     
       
   227 // -----------------------------------------------------------------------------
       
   228 // CMceSipManager::MediaManager
       
   229 // -----------------------------------------------------------------------------
       
   230 //
       
   231 CSIP& CMceSipManager::SIPClient()
       
   232     {
       
   233     return *iSIP;
       
   234     }
       
   235 
       
   236 // -----------------------------------------------------------------------------
       
   237 // CMceSipManager::ProfileL
       
   238 // -----------------------------------------------------------------------------
       
   239 //
       
   240 CSIPProfile& CMceSipManager::ProfileL( TUint32 aProfileId )
       
   241     {
       
   242     CSIPProfile* profile = NULL;
       
   243     TInt status = KErrNotFound;
       
   244 
       
   245     TInt index = FindProfileById( iProfiles, aProfileId );
       
   246     if ( index != KErrNotFound )
       
   247         {
       
   248         profile = iProfiles[ index ];
       
   249         }
       
   250     else
       
   251         {
       
   252         RPointerArray<CSIPProfile> profiles;
       
   253         iProfileRegistry->ProfilesL( profiles );
       
   254         
       
   255         index = FindProfileById( profiles, aProfileId );
       
   256         if ( index != KErrNotFound )
       
   257             {
       
   258             profile = profiles[ index ];
       
   259             status = iProfiles.Append( profile );
       
   260             if ( status != KErrNone )
       
   261                 {
       
   262                 profile = NULL;
       
   263                 }
       
   264             else
       
   265                 {
       
   266                 profiles.Remove( index );
       
   267                 }
       
   268             }
       
   269         profiles.ResetAndDestroy();
       
   270         profiles.Close();
       
   271         }
       
   272     if (!profile )
       
   273         {
       
   274         User::Leave( status );
       
   275         }
       
   276         
       
   277     return *profile;
       
   278     }
       
   279 
       
   280 // -----------------------------------------------------------------------------
       
   281 // CMceSipManager::ProfileL
       
   282 // -----------------------------------------------------------------------------
       
   283 //
       
   284 CSIPProfile& CMceSipManager::ProfileL( TUint32 /*aIapId*/, 
       
   285                                        const CUri8& aRequestUri )
       
   286     {
       
   287     CSIPProfile* profile = NULL;
       
   288     TInt status = KErrNotFound;
       
   289     
       
   290     // Try to find in existing profiles 
       
   291     // Try matching with request URI
       
   292     TInt index = FindProfileByRequestUriL( iProfiles, aRequestUri );
       
   293     
       
   294     if ( index != KErrNotFound )
       
   295         {
       
   296         // found in existing profiles
       
   297         profile = iProfiles[ index ];
       
   298         }
       
   299     else
       
   300         {
       
   301         // Get all profiles
       
   302         RPointerArray<CSIPProfile> profiles;
       
   303         MceCleanupResetAndDestroyPushL( profiles );
       
   304         iProfileRegistry->ProfilesL( profiles );
       
   305         // Try matching with request URI
       
   306         index = FindProfileByRequestUriL( profiles, aRequestUri );
       
   307             
       
   308         if ( index != KErrNotFound )
       
   309             {
       
   310             profile = profiles[ index ];
       
   311             status = iProfiles.Append( profile );
       
   312             if ( status != KErrNone )
       
   313                 {
       
   314                 profile = NULL;
       
   315                 }
       
   316             else
       
   317                 {
       
   318                 profiles.Remove( index );
       
   319                 }
       
   320             }
       
   321         CleanupStack::PopAndDestroy( &profiles );
       
   322         }
       
   323     
       
   324     if (!profile )
       
   325         {
       
   326         User::Leave( status );
       
   327         }
       
   328     
       
   329     return *profile;
       
   330     }
       
   331 
       
   332 // -----------------------------------------------------------------------------
       
   333 // CMceSipManager::Cleanup
       
   334 // -----------------------------------------------------------------------------
       
   335 //
       
   336 void CMceSipManager::Cleanup()
       
   337     {
       
   338     MCESRV_DEBUG("CMceSipManager::Cleanup, Entry")
       
   339     
       
   340     for ( int i = 0; i < iOrphanSubSessions->SubSessions().Count(); i++ )
       
   341         {
       
   342         CMceCsSubSession* session = iOrphanSubSessions->SubSessions()[i];
       
   343         if ( session->CanDispose() ||
       
   344              session->Terminate() )
       
   345             {
       
   346             MCESRV_DEBUG_DVALUE("Cleanup: orphan removed. ID", 
       
   347                 MCE_SESSION_ID( session ) )
       
   348             iOrphanSubSessions->SubSessions().Remove( i );
       
   349             delete session;
       
   350             i--;
       
   351             }
       
   352         }
       
   353     
       
   354     // If number of orphan subsessions and number of CMceCsSession 
       
   355     // object's count are 0, stop MCE server after time out.
       
   356     iServerCore.InactiveServerCleanup();
       
   357     
       
   358     MCESRV_DEBUG("CMceSipManager::Cleanup, Exit")
       
   359     }
       
   360 
       
   361 // -----------------------------------------------------------------------------
       
   362 // CMceSipManager::ConnectionL
       
   363 // -----------------------------------------------------------------------------
       
   364 //
       
   365 CMceSipConnection& CMceSipManager::ConnectionL( CSIPProfile& aProfile )
       
   366     {
       
   367 	MCESRV_DEBUG("CMceSipManager::ConnectionL, Entry")
       
   368 	TUint32 iapId = 0;
       
   369 	aProfile.GetParameter( KSIPAccessPointId, iapId );
       
   370 
       
   371     CMceSipConnection* connection = NULL;
       
   372     
       
   373     TInt index = 0;
       
   374     while ( !connection && index < iSIPClientConnections.Count() )
       
   375         {
       
   376         if ( iSIPClientConnections[ index ]->AccessPointId() == iapId )
       
   377             {
       
   378             connection = iSIPClientConnections[ index ];
       
   379             }
       
   380         index++;            
       
   381         }
       
   382     	    
       
   383     if (!connection )
       
   384         {
       
   385         MCESRV_DEBUG("CMceSipManager::ConnectionL, no connection created")
       
   386         
       
   387         MCESRV_DEBUG("CMceSipManager::ConnectionL, Create MceSipConnection")
       
   388 		connection = 
       
   389 		    CMceSipConnection::NewLC( *this, aProfile, *iProfileRegistry );
       
   390         iSIPClientConnections.AppendL( connection );
       
   391         CleanupStack::Pop( connection );
       
   392         }
       
   393         
       
   394     if ( !iProfileRegistry->IsEnabled( aProfile ) )
       
   395     	{
       
   396     	iProfileRegistry->EnableL( aProfile, *connection );
       
   397         }
       
   398     	
       
   399     __ASSERT_ALWAYS( aProfile.IsContextActive(), 
       
   400                      User::Leave( KErrCouldNotConnect ) ); 
       
   401 
       
   402     MCESRV_DEBUG("CMceSipManager::ConnectionL, Exit")
       
   403     return *connection;
       
   404     }
       
   405 
       
   406 // -----------------------------------------------------------------------------
       
   407 // CMceSipManager::ConnectionL
       
   408 // -----------------------------------------------------------------------------
       
   409 //
       
   410 CMceSipConnection& CMceSipManager::ConnectionL( TUint32 aIAPId )
       
   411     {
       
   412     CMceSipConnection* connection = NULL;
       
   413 
       
   414     TInt index = 0;
       
   415     while ( !connection && index < iSIPClientConnections.Count() )
       
   416         {
       
   417         if ( iSIPClientConnections[ index ]->AccessPointId() == aIAPId )
       
   418             {
       
   419             connection = iSIPClientConnections[ index ];
       
   420             }
       
   421         index++;            
       
   422         }
       
   423 
       
   424     if ( !connection )
       
   425         {
       
   426         connection = CMceSipConnection::NewLC( *this, aIAPId );
       
   427         iSIPClientConnections.AppendL( connection );
       
   428         CleanupStack::Pop( connection );
       
   429         }
       
   430         
       
   431     return *connection;
       
   432     }
       
   433 
       
   434 // -----------------------------------------------------------------------------
       
   435 // CMceSipManager::CreateSubSessionL
       
   436 // -----------------------------------------------------------------------------
       
   437 //
       
   438 CMceCsSubSession* CMceSipManager::CreateSubSessionL( CMceCsSession& aSession,
       
   439                                                      TMceIds& aIds,
       
   440                                                      CDesC8Array& aParams )
       
   441     {
       
   442     MCESRV_DEBUG("CMceSipManager::CreateSubSessionL( by client ), Entry")
       
   443     
       
   444     Cleanup();
       
   445     
       
   446     TUint32 profileId = aIds.iProfileID;
       
   447     TMceCsSessionType sessionType = (TMceCsSessionType)aIds.iManagerType;
       
   448     TMceDialogType dialogType = (TMceDialogType)aIds.iDialogType;
       
   449     TUint32 dialogId = aIds.iDialogID;
       
   450 
       
   451     CMceSipConnection* connection = NULL;
       
   452     if ( profileId )
       
   453 	    {
       
   454 	    CSIPProfile& profile = ProfileL( profileId );
       
   455 	    
       
   456 	    connection = &ConnectionL( profile );   	
       
   457 	    }
       
   458 	else
       
   459 		{
       
   460 		TInt32 iapId = 0;
       
   461 		TLex8 accessPoint( aParams.MdcaPoint( KMceArrayIndexIAPId ) );
       
   462 		accessPoint.Val( iapId );
       
   463 	    connection = &ConnectionL( iapId );		
       
   464 		}
       
   465 		
       
   466     __ASSERT_ALWAYS( connection, User::Leave( KErrCouldNotConnect ) );    	
       
   467 
       
   468     CMceCsSubSession* subSession = NULL;
       
   469     
       
   470     aIds.iSpare1 = ( connection->State() == CSIPConnection::EActive );
       
   471     
       
   472     switch ( sessionType )
       
   473         {
       
   474         case KMceCSSIPSession:
       
   475             {
       
   476             if ( iMediaManager->NatPluginManager().NatEnabledL() )
       
   477                 {
       
   478                 if( profileId )
       
   479                 	{
       
   480                 	MCESRV_DEBUG("CMceSipManager::CreateSubSessionL, NAT SIP session with profile")
       
   481                 	subSession = CMceNatSipSession::NewLC( aSession,
       
   482                                                        *connection,
       
   483                                                        *iStateMachine,
       
   484                                                        ProfileL( profileId ),
       
   485                                                        &aParams );
       
   486                 	}
       
   487                 else
       
   488                 	{
       
   489                 	MCESRV_DEBUG("CMceSipManager::CreateSubSessionL, NAT SIP session without profile")
       
   490                 	subSession = CMceNatSipSession::NewLC( aSession,
       
   491                                                        *connection,
       
   492                                                        *iStateMachine,
       
   493                                                        &aParams );
       
   494                 	}
       
   495                 
       
   496                 }
       
   497             else
       
   498                 {
       
   499                 if( profileId )
       
   500                 	{
       
   501                     MCESRV_DEBUG("CMceSipManager::CreateSubSessionL, SIP session with profile")
       
   502                     subSession = CMceSipSession::NewLC( aSession,
       
   503                                                     *connection,
       
   504                                                     *iStateMachine,
       
   505                                                     ProfileL( profileId ) );
       
   506                 	}
       
   507                 else
       
   508                 	{
       
   509                 	MCESRV_DEBUG("CMceSipManager::CreateSubSessionL, SIP session without profile")
       
   510                 	subSession = CMceSipSession::NewLC( aSession,
       
   511                                                        *connection,
       
   512                                                        *iStateMachine );
       
   513                 	}
       
   514                 }
       
   515             break;
       
   516             }
       
   517         case KMceCSSIPEvent:
       
   518             {
       
   519             MCESRV_DEBUG("CreateSubSessionL: creating SIP event")
       
   520             subSession = CMceSipEvent::NewLC( aSession, 
       
   521             								  *connection, 
       
   522             								  ProfileL( profileId ) );
       
   523             break;
       
   524             }
       
   525         default:
       
   526             {
       
   527             MCESRV_DEBUG("CreateSubSessionL: NOT SUPPORTED")
       
   528             User::Leave( KErrNotSupported );
       
   529             }
       
   530         }
       
   531         
       
   532     const CMceCsSubSession* sharedSession = NULL;
       
   533     CSIPDialogAssocBase* dialog = NULL;
       
   534     
       
   535     if ( dialogId != KMceNotAssigned )
       
   536         {
       
   537         MCESRV_DEBUG_DVALUE( 
       
   538             "CreateSubSessionL: find subsession with dialogId", dialogId )
       
   539         sharedSession = aSession.Implementation().FindSubSession( dialogId );
       
   540         }
       
   541 
       
   542     if ( sharedSession )
       
   543         {
       
   544         MCESRV_DEBUG_DVALUE( 
       
   545             "CreateSubSessionL: creating dialog assoc to existing dialog. ID",
       
   546             dialogId )
       
   547         
       
   548         dialog = subSession->CreateOutgoingDialogL( 
       
   549                                         dialogType, 
       
   550                                         aParams, 
       
   551                                         sharedSession->Dialog()->Dialog() );
       
   552         subSession->SetDialog( CMceCsSubSession::EOutSession, 
       
   553                                dialog, 
       
   554                                sharedSession->DialogId() );
       
   555         }
       
   556     else
       
   557         {
       
   558         MCESRV_DEBUG( "CreateSubSessionL: creating new dialog assoc" )
       
   559         dialog = subSession->CreateOutgoingDialogL( dialogType, aParams );
       
   560         subSession->SetDialog( CMceCsSubSession::EOutSession, 
       
   561                                dialog, 
       
   562                                NextDialogId() );
       
   563         }
       
   564 
       
   565     connection->AttachL( *subSession );
       
   566     CleanupStack::Pop( subSession );
       
   567     
       
   568     MCESRV_DEBUG( 
       
   569         "CMceSipManager: CMceSipManager::CreateSubSessionL( by client ), Exit")
       
   570             
       
   571     return subSession;
       
   572     }
       
   573 
       
   574 // -----------------------------------------------------------------------------
       
   575 // CMceSipManager::CreateSubSessionL
       
   576 // -----------------------------------------------------------------------------
       
   577 //
       
   578 void CMceSipManager::CreateSubSessionL( CSIPServerTransaction* aTransaction,
       
   579                                         CMceSipConnection& aConnection )
       
   580     {
       
   581     MCESRV_DEBUG("CMceSipManager::CreateSubSessionL, Entry")
       
   582     
       
   583     User::LeaveIfError( Consumes( *aTransaction ) ?
       
   584                         KErrNone :
       
   585                         KErrNotSupported );
       
   586                         
       
   587     
       
   588     iClientResolver->RegisterIncomingRequestL( aTransaction, aConnection );
       
   589     
       
   590     MCESRV_DEBUG("CMceSipManager::CreateSubSessionL, Exit")
       
   591     }
       
   592 
       
   593 // -----------------------------------------------------------------------------
       
   594 // CMceSipManager::IncomingResolvedRequest
       
   595 // -----------------------------------------------------------------------------
       
   596 //
       
   597 void CMceSipManager::IncomingResolvedRequest( TUid aClientUid,
       
   598                                               CMceSipConnection& aConnection,
       
   599                                               CSIPServerTransaction* aRequest )
       
   600     {
       
   601     MCESRV_DEBUG("CMceSipManager::IncomingResolvedRequest, Entry")
       
   602     MCESRV_DEBUG_SVALUE("request", MceSip::Method( *aRequest ).DesC() )
       
   603 
       
   604     CMceCsSession* client = NULL;
       
   605     TInt i = 0;
       
   606     while ( !client && i < iClients.Count() )
       
   607         {
       
   608         client = iClients[ i++ ];
       
   609         if ( client->Uid() != aClientUid )
       
   610             {
       
   611             client = NULL;
       
   612             }
       
   613         }
       
   614 
       
   615     TInt error = KErrNone;
       
   616     if ( client )
       
   617         {
       
   618         TRAP( error, CreateSubSessionL( aRequest, *client, aConnection ) );
       
   619         }
       
   620     else
       
   621         {
       
   622         MCESRV_DEBUG("CLIENT NOT FOUND")
       
   623         }
       
   624 
       
   625     if ( !client || error != KErrNone )
       
   626         {
       
   627         MCESRV_DEBUG("IncomingResolvedRequest: discarding request")
       
   628         MCESRV_DEBUG_DVALUE( "ERROR:", error )
       
   629         MceSip::DiscardRequest( aRequest, error );
       
   630         }
       
   631     
       
   632     MCESRV_DEBUG("CMceSipManager::IncomingResolvedRequest, Exit")
       
   633     }
       
   634 
       
   635 // -----------------------------------------------------------------------------
       
   636 // CMceSipManager::CreateSubSessionL
       
   637 // -----------------------------------------------------------------------------
       
   638 //
       
   639 void CMceSipManager::CreateSubSessionL( CSIPServerTransaction* aTransaction,
       
   640                                         CMceCsSession& aClient,
       
   641                                         CMceSipConnection& aConnection,
       
   642                                         CMceCsSubSession* aAssocSession )
       
   643     {
       
   644     MCESRV_DEBUG("CMceSipManager::CreateSubSessionL( from network ), Entry")
       
   645     MCESRV_DEBUG_SVALUE("request", MceSip::Method( *aTransaction ).DesC() )
       
   646     
       
   647     __ASSERT_ALWAYS( Consumes( *aTransaction ), User::Leave( KErrArgument ) );
       
   648     
       
   649     CMceCsSubSession* session = NULL;
       
   650     CSIPProfile *profile = NULL;
       
   651     TUint32 profileId = 0;
       
   652 
       
   653     if ( aAssocSession != NULL )
       
   654     	{
       
   655     	User::LeaveIfError(
       
   656     	    aAssocSession->Profile().GetParameter( KSIPProfileId, profileId ) );
       
   657     	}
       
   658     else
       
   659     	{
       
   660     	const TDesC8* remoteContact =
       
   661     	    &aTransaction->RequestElements()->RemoteUri().Uri().UriDes();
       
   662     			
       
   663 		TInt len = remoteContact->Length();
       
   664 		HBufC8* editRemoteContact = HBufC8::NewLC( len );
       
   665 		TPtr8 desERC = editRemoteContact->Des();
       
   666 		desERC.Copy( *remoteContact );
       
   667 		TInt cut = desERC.Find( _L8( ":" ) );
       
   668 		
       
   669 		if ( cut != KErrNotFound )
       
   670 			{
       
   671 			desERC.Delete( 0, ++cut );
       
   672 			len -= cut;
       
   673 			cut = desERC.Find( _L8( "@" ) );
       
   674 			desERC.Delete( cut, len );
       
   675 			
       
   676 			RPointerArray<CSIPProfile> profiles;
       
   677 			iProfileRegistry->ProfilesL( profiles );
       
   678 
       
   679 			TInt error( KErrNone );
       
   680         	const TDesC8* localContact = NULL;
       
   681 			TBool found( EFalse );
       
   682 	    	for ( TInt i = 0; !found && i < profiles.Count(); i++ )
       
   683 	    		{
       
   684 	    		profile = profiles[ i ];
       
   685 	    		error = profile->GetParameter( KSIPContactHeaderUser, localContact );
       
   686 	    		if ( error == KErrNone )
       
   687 	    			{
       
   688 	    			if ( desERC.Compare( *localContact ) == 0 )
       
   689 	    				{
       
   690 	    				User::LeaveIfError( 
       
   691 	    				    profile->GetParameter( KSIPProfileId, profileId ) );
       
   692 	    				found = ETrue;
       
   693 	    				}
       
   694 	    			}
       
   695 	    		}
       
   696 	    	profiles.ResetAndDestroy();
       
   697         	profiles.Close();
       
   698 			}
       
   699 		CleanupStack::PopAndDestroy( editRemoteContact );
       
   700     	}
       
   701     profile = &ProfileL( profileId );
       
   702 
       
   703 
       
   704     RStringF method = MceSip::Method( *aTransaction );
       
   705     
       
   706     if ( method == SIPStrings::StringF( SipStrConsts::EInvite ) )
       
   707         {
       
   708         if ( iMediaManager->NatPluginManager().NatEnabledL() )
       
   709             {
       
   710             MCESRV_DEBUG("CMceSipManager::CreateSubSessionL, NAT SIP session")
       
   711             session = CMceNatSipSession::NewLC( aClient,
       
   712                                                 aConnection,
       
   713                                                 *iStateMachine,
       
   714                                                 ProfileL( profileId ) );
       
   715             }
       
   716         else
       
   717             {
       
   718             MCESRV_DEBUG("CMceSipManager::CreateSubSessionL, SIP session")
       
   719             // we can give to MceSipSession...
       
   720             session = CMceSipSession::NewLC( aClient,
       
   721                                              aConnection,
       
   722                                              *iStateMachine,
       
   723                                              *profile );
       
   724             }
       
   725         }
       
   726     else
       
   727         {
       
   728         MCESRV_DEBUG("IncomingResolvedRequest: creating SIP event")
       
   729         session = CMceSipEvent::NewLC( aClient, aConnection, *profile );
       
   730         }
       
   731         
       
   732     CSIPDialogAssocBase* dialog = 
       
   733         session->CreateIncomingDialogL( *aTransaction );
       
   734         
       
   735     TUint32 dialogId = aAssocSession ? 
       
   736         aAssocSession->DialogId() : NextDialogId();
       
   737         
       
   738     session->SetDialog( CMceCsSubSession::EInSession, dialog, dialogId );
       
   739     
       
   740     aConnection.AttachL( *session );
       
   741 
       
   742     session->InitializeIncomingDialogL( aTransaction );
       
   743     
       
   744     //check if session failed and is in terminated state already
       
   745     if ( !session->CanDispose() )
       
   746         {
       
   747         TInt error = aClient.Implementation().SubSessions().Append( session );
       
   748         if ( error != KErrNone )
       
   749             {
       
   750             MCESRV_DEBUG("CMceSipManager::CreateSubSessionL: Appending failed")
       
   751             session->PopRequest();
       
   752             User::Leave( error );
       
   753             }
       
   754         CleanupStack::Pop( session );
       
   755         }
       
   756     else
       
   757         {
       
   758         MCESRV_DEBUG("IncomingResolvedRequest: Initialization FAILED")
       
   759         CleanupStack::PopAndDestroy( session );
       
   760         }
       
   761         
       
   762     MCESRV_DEBUG("CMceSipManager::CreateSubSessionL( from network ), Exit")
       
   763     }
       
   764 
       
   765 // -----------------------------------------------------------------------------
       
   766 // CMceSipManager::SocketServ
       
   767 // -----------------------------------------------------------------------------
       
   768 //
       
   769 RSocketServ& CMceSipManager::SocketServ()
       
   770     {
       
   771     return iSocketServ;
       
   772     }
       
   773 
       
   774 // -----------------------------------------------------------------------------
       
   775 // CMceSipManager::Consumes
       
   776 // -----------------------------------------------------------------------------
       
   777 //
       
   778 TBool CMceSipManager::Consumes( CSIPServerTransaction& aTransaction )
       
   779     {
       
   780     RStringF method = MceSip::Method( aTransaction );
       
   781     
       
   782     return method == SIPStrings::StringF( SipStrConsts::EInvite ) ||
       
   783            method == SIPStrings::StringF( SipStrConsts::ESubscribe ) ||
       
   784            method == SIPStrings::StringF( SipStrConsts::ERefer );
       
   785     }
       
   786 	
       
   787 // -----------------------------------------------------------------------------
       
   788 // CMceSipManager::AddOrphanSubSessionL
       
   789 // -----------------------------------------------------------------------------
       
   790 //
       
   791 void CMceSipManager::AddOrphanSubSessionL( CMceCsSubSession* aSubSession )
       
   792     {
       
   793     MCESRV_DEBUG("CMceSipManager::AddOrphanSubSessionL, Entry")
       
   794     MCESRV_DEBUG_DVALUE("dialogid", aSubSession->DialogId() )
       
   795     
       
   796     aSubSession->SetClient( *iOrphanSubSessions );
       
   797     aSubSession->SetSessionReceiver( NULL );
       
   798     iOrphanSubSessions->SubSessions().AppendL( aSubSession );
       
   799     
       
   800     MCESRV_DEBUG("CMceSipManager::AddOrphanSubSessionL, Exit")
       
   801     }
       
   802 
       
   803 // -----------------------------------------------------------------------------
       
   804 // CMceSipManager::OrphanSubSessionCount
       
   805 // -----------------------------------------------------------------------------
       
   806 //
       
   807 TInt CMceSipManager::OrphanSubSessionCount()
       
   808     {
       
   809     return iOrphanSubSessions->SubSessions().Count();
       
   810     }
       
   811 
       
   812 // -----------------------------------------------------------------------------
       
   813 // CMceSipManager::UnRegisterSubSession
       
   814 // -----------------------------------------------------------------------------
       
   815 //
       
   816 void CMceSipManager::UnRegisterSubSession( CMceCsSubSession& aSubSession )
       
   817     {
       
   818     MCESRV_DEBUG("CMceSipManager::UnRegisterSubSession, Entry")
       
   819 
       
   820     CSIPProfile* profile = NULL;
       
   821     TBool profileUsed( EFalse );
       
   822     TInt err = KErrNone;
       
   823     TInt index = iSIPClientConnections.Find( &aSubSession.SIPConnection() );
       
   824  
       
   825     if ( index != KErrNotFound )
       
   826         {
       
   827         if ( aSubSession.SIPConnection().Detach( aSubSession ) == 0 )
       
   828             {
       
   829             // If no SubSessions related to the CMceSipConnection any more,
       
   830             // delete the CMceSipConnection instance.
       
   831             MCESRV_DEBUG("CMceSipManager::UnRegisterSubSession, remove connection")
       
   832             CMceSipConnection* connection = iSIPClientConnections[ index ];
       
   833             iSIPClientConnections.Remove( index );
       
   834             delete connection;
       
   835 			connection = NULL;
       
   836             }
       
   837         if ( aSubSession.ProfileConfigured() )
       
   838         	{
       
   839             MCESRV_DEBUG("CMceSipManager::UnRegisterSubSession, profile configured")
       
   840             TUint32 profileId = 0;
       
   841             aSubSession.Profile().GetParameter( KSIPProfileId, profileId );
       
   842 
       
   843         	TRAP( err, profile = &ProfileL( profileId ) );
       
   844 
       
   845         	if ( err == KErrNone )
       
   846         		{
       
   847         		for ( TInt i = 0; i < iSIPClientConnections.Count() && !profileUsed; i++ )
       
   848             		{
       
   849             		profileUsed = iSIPClientConnections[ i ]->IsProfileUsedInSession(
       
   850             														*profile );
       
   851             		}
       
   852         		}
       
   853             }
       
   854 
       
   855         // Delete the unused profile after un-register a SubSession.
       
   856         if ( !profileUsed )
       
   857             {
       
   858             MCESRV_DEBUG("CMceSipManager::UnRegisterSubSession, profile not used anymore")
       
   859             
       
   860 			// No need to loop downwards, as just one profile is removed
       
   861             for ( TInt j = 0; j < iProfiles.Count() && profile; j++ )
       
   862                 {
       
   863                 if ( iProfiles[ j ] == profile )
       
   864                     {
       
   865                     MCESRV_DEBUG("CMceSipManager::UnRegisterSubSession, deleting profile")
       
   866                     iProfiles.Remove( j );
       
   867                     delete profile;
       
   868                     profile = NULL;
       
   869                     }
       
   870                 }
       
   871             }
       
   872         }
       
   873     MCESRV_DEBUG("CMceSipManager::UnRegisterSubSession, Exit")
       
   874     }
       
   875 
       
   876 // -----------------------------------------------------------------------------
       
   877 // CMceSipManager::FindProfileByRequestUriL
       
   878 // -----------------------------------------------------------------------------
       
   879 //                                 
       
   880 TInt CMceSipManager::FindProfileByRequestUriL( 
       
   881     RPointerArray<CSIPProfile>& aProfiles, 
       
   882     const CUri8& aRequestUri )
       
   883     {
       
   884     TInt ret = KErrNotFound;
       
   885     
       
   886     // User name of request URI 
       
   887     const TDesC8& uriUser = aRequestUri.Uri().Extract( EUriUserinfo );
       
   888     
       
   889     CSIPProfile* profile = NULL;
       
   890     TInt profileInd = 0;
       
   891     
       
   892     while( ret == KErrNotFound && profileInd < aProfiles.Count() )
       
   893         {
       
   894         // Try to find a profile with matching contact header user part
       
   895         profile = aProfiles[ profileInd ];
       
   896 
       
   897         const TDesC8* contactUser = NULL;
       
   898         // get Contact-header's user-part
       
   899         User::LeaveIfError( profile->GetParameter( KSIPContactHeaderUser, contactUser ) );
       
   900 
       
   901         if ( uriUser.CompareF( *contactUser ) == 0 )
       
   902             {
       
   903             ret = profileInd;
       
   904             }
       
   905 
       
   906         profileInd++;
       
   907         }
       
   908 
       
   909     return ret;
       
   910     }   
       
   911 
       
   912 // -----------------------------------------------------------------------------
       
   913 // CMceSipManager::FindProfileById
       
   914 // -----------------------------------------------------------------------------
       
   915 //
       
   916 TInt CMceSipManager::FindProfileById( 
       
   917     RPointerArray<CSIPProfile>& aProfiles, 
       
   918     TUint32 profileId )
       
   919     {
       
   920     TInt index = 0;
       
   921     while ( index < aProfiles.Count() )
       
   922         {
       
   923         TUint32 macthId;
       
   924         TInt error = aProfiles[ index ]->GetParameter( KSIPProfileId, macthId );
       
   925         if ( error == KErrNone && macthId == profileId )
       
   926             {
       
   927             return index;
       
   928             }
       
   929         index++;            
       
   930         }
       
   931         
       
   932     return KErrNotFound;
       
   933     }
       
   934     
       
   935 // -----------------------------------------------------------------------------
       
   936 // CMceSipManager::IncomingRequest
       
   937 // -----------------------------------------------------------------------------
       
   938 //
       
   939 void CMceSipManager::IncomingRequest( 
       
   940     TUint32 aIapId,
       
   941     CSIPServerTransaction* aTransaction )	                 
       
   942     {
       
   943     MCESRV_DEBUG("CMceSipManager::IncomingRequest, Entry")
       
   944     MCESRV_DEBUG_SVALUE("request", MceSip::Method( *aTransaction ).DesC() )
       
   945     MCESRV_DEBUG_DVALUE("iapid", aIapId )
       
   946     
       
   947     CMceSipConnection* connection = NULL;
       
   948     CSIPProfile* profile = NULL;
       
   949     
       
   950     if ( !Consumes( *aTransaction ) )
       
   951         {
       
   952         REJECT( aTransaction );
       
   953         }
       
   954         
       
   955     const CSIPRequestElements* request = aTransaction->RequestElements();
       
   956     const CSIPToHeader* to = request->ToHeader();
       
   957     if ( !to )
       
   958         {
       
   959         MCESRV_DEBUG("IncomingRequest: no recipient: REJECT")
       
   960         REJECT( aTransaction );
       
   961         }
       
   962         
       
   963     const CUri8& reqUri = request->RemoteUri();
       
   964     
       
   965     TRAPD( error1, profile = &ProfileL( aIapId, reqUri ) );
       
   966     REJECT_IF_ERROR( error1, aTransaction )
       
   967     
       
   968     TRAPD( error2, connection = &ConnectionL( *profile ) );
       
   969     REJECT_IF_ERROR( error2, aTransaction )
       
   970 
       
   971     __ASSERT_ALWAYS( profile->IsContextActive(), 
       
   972         User::Leave( KErrCouldNotConnect ) );
       
   973     
       
   974     TRAPD( error3, CreateSubSessionL( aTransaction, *connection ) );
       
   975     REJECT_IF_ERROR( error3, aTransaction )
       
   976 
       
   977     MCESRV_DEBUG("CMceSipManager::IncomingRequest, Exit")
       
   978     }
       
   979 
       
   980 // -----------------------------------------------------------------------------
       
   981 // CMceSipManager::TimedOut
       
   982 // -----------------------------------------------------------------------------
       
   983 //
       
   984 void CMceSipManager::TimedOut( CSIPServerTransaction& /*aTransaction*/ )
       
   985     {
       
   986     MCESRV_DEBUG("CMceSipManager::TimedOut, Entry")
       
   987     MCESRV_DEBUG("-- NOT IMPLEMENTED --")
       
   988     MCESRV_DEBUG("CMceSipManager::TimedOut, Exit")
       
   989     }
       
   990 
       
   991 // -----------------------------------------------------------------------------
       
   992 // CMceSipManager::ProfileRegistryEventOccurred
       
   993 // If profile is updated, read its data from profile server and replace the
       
   994 // local copy of profile's data. As CSIPProfile's memory address changes, also
       
   995 // CMceCsSubSession::iProfile pointers must be updated.
       
   996 // -----------------------------------------------------------------------------
       
   997 //
       
   998 void CMceSipManager::ProfileRegistryEventOccurred( 
       
   999     TUint32 aProfileId, 
       
  1000     MSIPProfileRegistryObserver::TEvent aEvent )
       
  1001     {
       
  1002     MCESRV_DEBUG("CMceSipManager::ProfileRegistryEventOccurred(pid,event), Entry")
       
  1003     MCESRV_DEBUG_DVALUES( "profileId", aProfileId, "event", aEvent )
       
  1004 
       
  1005 	if ( aEvent == MSIPProfileRegistryObserver::EProfileUpdated )
       
  1006 		{
       
  1007 		CSIPProfile* profile( NULL );
       
  1008 		TRAPD( err, profile = iProfileRegistry->ProfileL( aProfileId ) );
       
  1009 
       
  1010 		if ( profile && err == KErrNone )
       
  1011 			{
       
  1012 			TInt index = FindProfileById( iProfiles, aProfileId );
       
  1013 			if ( index != KErrNotFound )
       
  1014 				{
       
  1015 				CSIPProfile* oldProfile = iProfiles[ index ];
       
  1016 				UpdateProfileToSubSessions( *profile, *oldProfile );
       
  1017 			    delete oldProfile;
       
  1018 			    // Replace pointer in iProfiles array
       
  1019 			    iProfiles[ index ] = profile;
       
  1020 				}
       
  1021 			else
       
  1022 				{
       
  1023 	    		err = iProfiles.Append( profile );
       
  1024 	    		if ( err != KErrNone )
       
  1025 	    			{
       
  1026 	    			delete profile;
       
  1027 	    			}
       
  1028 				}
       
  1029 			}
       
  1030         MCESRV_DEBUG_DVALUE( "err", err )
       
  1031 		}
       
  1032 	MCESRV_DEBUG("CMceSipManager::ProfileRegistryEventOccurred(pid,event), Exit")
       
  1033     }
       
  1034 
       
  1035 // -----------------------------------------------------------------------------
       
  1036 // CMceSipManager::ProfileRegistryErrorOccurred
       
  1037 // -----------------------------------------------------------------------------
       
  1038 //
       
  1039 void CMceSipManager::ProfileRegistryErrorOccurred( 
       
  1040     TUint32 /*aProfileId*/, 
       
  1041     TInt /*aError*/)
       
  1042     {
       
  1043     MCESRV_DEBUG("CMceSipManager::ProfileRegistryErrorOccurred(pid,error), Entry")
       
  1044     MCESRV_DEBUG("-- NOT IMPLEMENTED --")
       
  1045     MCESRV_DEBUG("CMceSipManager::ProfileRegistryErrorOccurred(pid,error), Exit")
       
  1046     }
       
  1047     
       
  1048 // -----------------------------------------------------------------------------
       
  1049 // CMceSipManager::MediaError
       
  1050 // -----------------------------------------------------------------------------
       
  1051 //
       
  1052 void CMceSipManager::MediaError( TInt /*aError*/ )
       
  1053     {
       
  1054     MCESRV_DEBUG("CMceSipManager::MediaError, Entry")
       
  1055     MCESRV_DEBUG("-- NOT IMPLEMENTED --")
       
  1056     MCESRV_DEBUG("CMceSipManager::MediaError, Exit")
       
  1057     }
       
  1058 
       
  1059 // -----------------------------------------------------------------------------
       
  1060 // CMceSipManager::UpdateProfileToSubSessions
       
  1061 // -----------------------------------------------------------------------------
       
  1062 //
       
  1063 void CMceSipManager::UpdateProfileToSubSessions( CSIPProfile& aNewProfile,
       
  1064 												 CSIPProfile& aOldProfile )
       
  1065 	{
       
  1066 	MCESRV_DEBUG("CMceSipManager::UpdateProfileToSubSessions, Entry")
       
  1067 
       
  1068 	TInt count = iClients.Count();
       
  1069 	for ( TInt i = 0; i < count; ++i )
       
  1070 		{
       
  1071 		CMceCsSessionImplementation& session = iClients[ i ]->Implementation();
       
  1072 		RPointerArray<CMceCsSubSession>& subSessions = session.SubSessions();
       
  1073 
       
  1074 		TInt subSessionCount = subSessions.Count();
       
  1075 		for ( TInt j = 0; j < subSessionCount; ++j )
       
  1076 			{
       
  1077 			CSIPProfile& subSessionProfile = subSessions[ j ]->Profile();
       
  1078 			if ( &subSessionProfile == &aOldProfile )
       
  1079 				{
       
  1080 				MCESRV_DEBUG_DVALUES("update subsession profile, i", i, "j", j )
       
  1081 				MCESRV_DEBUG_DVALUES("oldProfile", (TInt)&aOldProfile,
       
  1082 									 "newProfile", (TInt)&aNewProfile )
       
  1083 				subSessions[ j ]->SetProfile( aNewProfile );
       
  1084 				}
       
  1085 			}
       
  1086 		}
       
  1087 	MCESRV_DEBUG("CMceSipManager::UpdateProfileToSubSessions, Exit")
       
  1088 	}