realtimenetprots/sipfw/SIP/ConnectionMgr/src/CSipConnection.cpp
changeset 0 307788aac0a8
child 19 b5e99d8877c7
equal deleted inserted replaced
-1:000000000000 0:307788aac0a8
       
     1 // Copyright (c) 2006-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          : CSipConnection.cpp
       
    15 // Part of       : ConnectionMgr
       
    16 // Version       : SIP/5.0
       
    17 //
       
    18 
       
    19 
       
    20 
       
    21 #include "CSipConnection.h"
       
    22 #include "CSipConnectionMgr.h"
       
    23 #include "SipLogs.h"
       
    24 #include "CTransportUdp.h"
       
    25 #include "CTransportTcp.h"
       
    26 #include "CTransportTls.h"
       
    27 #include "CIcmpReceiver.h"
       
    28 #include "MTransactionFinder.h"
       
    29 #include "CSIPServerResolver.h"
       
    30 #include "CTcpListeningPoint.h"
       
    31 #include "COutgoingData.h"
       
    32 #include "CTcpResSender.h"
       
    33 #include "MConnectionStateObserver.h"
       
    34 #include "CLocalAddrResolver.h"
       
    35 #include "MSigCompController.h"
       
    36 #include "SipAssert.h"
       
    37 #include "CConnectionContainer.h"
       
    38 #include "CNetworkManager.h"
       
    39 #include "CStateModel.h"
       
    40 #include "CommonConsts.h"
       
    41 #include "sipstrings.h"
       
    42 #include "sipstrconsts.h"
       
    43 #include "TSIPTransportParams.h"
       
    44 #include "COwnerSettingsList.h"
       
    45 #include "siperr.h"
       
    46 #include "NetworkInfo.h"
       
    47 #include "sipviaheader.h"
       
    48 #include "COutgoingRequestQueue.h"
       
    49 #include "sipuri.h"
       
    50 #include "uricontainer.h"
       
    51 #include "siphostport.h"
       
    52 #include "siprouteheader.h"
       
    53 #include "sipaddress.h"
       
    54 #include <uriutils.h>
       
    55 using namespace CommsDat;
       
    56 
       
    57 // Max length for an IP address string representation
       
    58 // (The format is either "d.d.d.d" or "h:h:h:h:h:h:h:h"
       
    59 //  depending on the address family, where "d" is an 8-bit decimal number
       
    60 //  and "h" is a 16-bit hexadecimal number. )
       
    61 const TUint KMaxAddressLength( 50 );
       
    62 
       
    63 const TInt CTransportBase::iOffset = _FOFF( CTransportBase, iLink );
       
    64 
       
    65 // -----------------------------------------------------------------------------
       
    66 // CSipConnection::NewLC
       
    67 // -----------------------------------------------------------------------------
       
    68 //
       
    69 CSipConnection* CSipConnection::NewLC(
       
    70 								MTransactionFinder& aFinder,
       
    71 								MServerTaFactory& aTaFactory,
       
    72 								MTimerManager& aTimer,
       
    73 								TUint aTOne,
       
    74 								const TSIPTransportParams& aParams,
       
    75 								RSocketServ& aServer,
       
    76 								MSigCompController& aSigCompHandler,
       
    77 								MConnectionStateObserver& aTUNetStatusObserver,
       
    78 								CNetworkManager& aNetworkManager,
       
    79 								MSipConnectionOwner& aOwner,
       
    80 								CNetworkInfo& aNetworkInfo,
       
    81 								CSIPNATTraversalController& aNATTraversal )
       
    82 	{
       
    83 	CSipConnection* self =
       
    84 		new( ELeave ) CSipConnection( aFinder, aTaFactory, aTimer, aTOne,
       
    85 									  aParams, aServer, aSigCompHandler,
       
    86 									  aTUNetStatusObserver, aNetworkManager,
       
    87 									  aOwner, aNetworkInfo, aNATTraversal );
       
    88 	CleanupStack::PushL( self );
       
    89 	self->ConstructL();
       
    90 	return self;
       
    91 
       
    92 	}
       
    93 
       
    94 // -----------------------------------------------------------------------------
       
    95 // CSipConnection::SetOptL
       
    96 // -----------------------------------------------------------------------------
       
    97 //	
       
    98 void CSipConnection::SetOptL( 
       
    99     const TSIPTransportParams& aParams,
       
   100     TUint aOptionName,
       
   101 	TUint aOptionLevel,
       
   102 	const TDesC8& aOption )
       
   103     {	
       
   104     // Remember opt, it will be used when this caller is using sockets
       
   105     iSettingsList->AddSettingsL( aParams, aOptionName, aOptionLevel, aOption );
       
   106     }
       
   107 
       
   108 // -----------------------------------------------------------------------------
       
   109 // CSipConnection::SetOptL
       
   110 // -----------------------------------------------------------------------------
       
   111 //		         	                         
       
   112 void CSipConnection::SetOptL( 
       
   113     const TSIPTransportParams& aParams,
       
   114     TUint aOptionName,
       
   115 	TUint aOptionLevel,
       
   116 	TInt aOption )
       
   117     {
       
   118     // Remember opt, it will be used when this caller is using sockets
       
   119     iSettingsList->AddSettingsL( aParams, aOptionName, aOptionLevel, aOption );
       
   120     }
       
   121 	
       
   122 // -----------------------------------------------------------------------------
       
   123 // CSipConnection::SendL
       
   124 // -----------------------------------------------------------------------------
       
   125 //
       
   126 void CSipConnection::SendL( 
       
   127     const TSIPTransportParams& aParams,
       
   128     CSIPResponse& aResponse,
       
   129 	TTransactionId aId,
       
   130 	TRequestStatus& aStatus )
       
   131 	{
       
   132 	__SIP_MESSAGE_LOG( "Connection Manager::SendL", aResponse )
       
   133 	iStateModel->SendL( aParams, aResponse, aId, aStatus );
       
   134 	}
       
   135 
       
   136 // -----------------------------------------------------------------------------
       
   137 // CSipConnection::SendL
       
   138 // -----------------------------------------------------------------------------
       
   139 //
       
   140 void CSipConnection::SendL( 
       
   141     const TSIPTransportParams& aParams,
       
   142     CSIPRequest& aRequest,
       
   143     TBool aForceUDP,
       
   144     const CUri8* aOutboundProxy,
       
   145 	const TInetAddr& aRemoteAddr,
       
   146 	TRequestStatus& aStatus )
       
   147 	{
       
   148 	__SIP_MESSAGE_LOG( "Connection Manager::SendL", aRequest )
       
   149 	if ( aRemoteAddr.IsMulticast() )
       
   150 		{
       
   151 		TRequestStatus* status = &aStatus;
       
   152 		User::RequestComplete( status, KErrNotSupported );
       
   153 		}
       
   154 	else
       
   155 		{
       
   156 		iStateModel->SendL( aParams, 
       
   157 		                    aRequest, 
       
   158 		                    aForceUDP,
       
   159 		                    aOutboundProxy, 
       
   160 		                    aRemoteAddr, 
       
   161 		                    aStatus );
       
   162 		}
       
   163 	}
       
   164 
       
   165 // -----------------------------------------------------------------------------
       
   166 // CSipConnection::CancelSend
       
   167 // -----------------------------------------------------------------------------
       
   168 //
       
   169 TInt CSipConnection::CancelSend( TRequestStatus &aStatus )
       
   170 	{
       
   171 	TBool cancelled = EFalse;
       
   172 	CTransportBase* listItem = 0;
       
   173 	iListIter.SetToFirst();
       
   174 	while ( ( listItem = iListIter++ ) != 0 )
       
   175 		{
       
   176 		cancelled = listItem->CancelSend( aStatus );
       
   177 		if(cancelled)
       
   178 			{
       
   179 			return KErrNone;
       
   180 			}
       
   181 		}
       
   182 
       
   183 	CTcpResponseSender* listsender( NULL );
       
   184 	iTcpSenderListIter.SetToFirst();
       
   185 	while ( ( listsender = iTcpSenderListIter++ ) != 0 )
       
   186 		{
       
   187 		if ( listsender->CancelSend( aStatus ) )
       
   188 			{
       
   189 			return KErrNone;
       
   190 			}
       
   191 		}
       
   192 		
       
   193     if ( iRequestQueue )
       
   194         {
       
   195         if ( iRequestQueue->CancelSend( aStatus ) )
       
   196             {
       
   197             return KErrNone;
       
   198             }
       
   199         }
       
   200 	return KErrNotFound;
       
   201 	}
       
   202 
       
   203 // -----------------------------------------------------------------------------
       
   204 // CSipConnection::CancelResponseSend
       
   205 // -----------------------------------------------------------------------------
       
   206 //
       
   207 TInt CSipConnection::CancelResponseSend( 
       
   208     TTransactionId aId, 
       
   209     TBool aCancelAlso2xxResponses )
       
   210 	{
       
   211 	TBool cancelled = EFalse;
       
   212 	CTransportBase* listItem = 0;
       
   213 	iListIter.SetToFirst();
       
   214 	while ( ( listItem = iListIter++ ) != 0 )
       
   215 		{
       
   216 		cancelled = listItem->CancelResponseSend( aId, aCancelAlso2xxResponses );
       
   217 		if ( cancelled )
       
   218 			{
       
   219 			return KErrNone;
       
   220 			}
       
   221 		}
       
   222 	return KErrNotFound;
       
   223 	}
       
   224 
       
   225 // -----------------------------------------------------------------------------
       
   226 // CSipConnection::TcpDisconnect
       
   227 // -----------------------------------------------------------------------------
       
   228 //
       
   229 void CSipConnection::TcpDisconnect( 
       
   230     const TSIPTransportParams& aParams,
       
   231     const TInetAddr& aRemoteAddress )
       
   232 	{
       
   233 	CTransportBase* listItem = 0;
       
   234 	iListIter.SetToFirst();
       
   235 
       
   236 	while ( ( listItem = iListIter++ ) != 0 )
       
   237 		{
       
   238 		if ( listItem->HasRemoteHost( aParams, 
       
   239 		                              aRemoteAddress, 
       
   240 		                              SIPStrings::StringF(SipStrConsts::ETCP) ) )
       
   241 			{
       
   242 			listItem->Disconnect( aRemoteAddress );
       
   243 			}
       
   244 		}
       
   245 	}
       
   246 
       
   247 // -----------------------------------------------------------------------------
       
   248 // CSipConnection::FreeResources
       
   249 // -----------------------------------------------------------------------------
       
   250 //	
       
   251 void CSipConnection::FreeResources( 
       
   252     MSIPNATBindingObserver& aSIPNATBindingObserver )
       
   253     {
       
   254     if ( iRequestQueue )
       
   255         {
       
   256         iRequestQueue->FreeResources( aSIPNATBindingObserver );
       
   257         }
       
   258     
       
   259     // Remove all matching persistent transports    
       
   260     CTransportBase* listItem;
       
   261     iListIter.SetToFirst();
       
   262     while ( ( listItem = iListIter++ ) != 0 )
       
   263         {   
       
   264         if ( listItem->Remove( aSIPNATBindingObserver ) )
       
   265             {
       
   266             RemoveTransport( listItem );
       
   267             }
       
   268         }
       
   269     }
       
   270 
       
   271 // -----------------------------------------------------------------------------
       
   272 // CSipConnection::SendToTransportL
       
   273 // -----------------------------------------------------------------------------
       
   274 //
       
   275 void CSipConnection::SendToTransportL(
       
   276     const TSIPTransportParams& aParams,
       
   277     CSIPResponse& aResponse,
       
   278 	TTransactionId aId,
       
   279 	TRequestStatus &aStatus )
       
   280 	{
       
   281 	CTransportBase* transport = 0;
       
   282 
       
   283 	CSIPViaHeader* viaHeader = TopViaHeader( &aResponse );
       
   284 	if ( viaHeader )
       
   285 	{
       
   286 		TBool isPortPresent = viaHeader->SentByHostPort().HasPort();
       
   287 		if (isPortPresent)
       
   288 		{
       
   289 			TUint sentByPort = viaHeader->SentByHostPort().Port();
       
   290 			if ((sentByPort > KMaxPortValue) || (sentByPort < KMinPortValue))
       
   291 			{
       
   292 				TRequestStatus* status = &aStatus;
       
   293 		    	__SIP_LOG( "Sent-by port is topmost viaheader is invalid, dropping response" )
       
   294 		    	User::RequestComplete( status, CSipConnectionMgr::EPortUnreachable );
       
   295 		    	return;
       
   296 			}
       
   297 		}
       
   298 	}
       
   299 	
       
   300 	TBool isTransportUdp = IsTransportUdp( &aResponse );
       
   301 
       
   302     transport = FindTransportL( aParams, &aResponse, aId, !isTransportUdp );
       
   303 
       
   304     if ( !isTransportUdp && !transport )
       
   305 		{
       
   306 		// When connected transport is used, there might be several connections
       
   307     	// with same remote address, in that case we had to search through all
       
   308     	// transports for matching the ta id. At this point any matching
       
   309     	// transport wasn't found, now we can take also remote address in count.
       
   310 		transport = FindTransportL( aParams, &aResponse, aId, EFalse );
       
   311 		
       
   312 		if ( !transport )
       
   313 		    {
       
   314     		// If there's saved settings regarding remote address, use them 
       
   315     		// instead of address passed from upper layers (probably only the
       
   316     		// port differs)
       
   317     		TInetAddr addrFromResponse;
       
   318             const TInetAddr* address;
       
   319             const TInetAddr* savedAddr = 
       
   320                 iSettingsList->SavedRemoteAddr( aParams.TransportId() );
       
   321             if ( savedAddr )
       
   322                 {
       
   323                 address = savedAddr;
       
   324                 }
       
   325             else
       
   326                 {
       
   327     			CTransportBase* listItem;
       
   328     			if ( !iList.IsEmpty() )
       
   329     				{
       
   330     				iListIter.SetToFirst();
       
   331     				listItem = iListIter;
       
   332     				listItem->GetAddrFromResponseL( aParams, 
       
   333     				                                aResponse, 
       
   334     				                                addrFromResponse );
       
   335     				}
       
   336                 address = &addrFromResponse;
       
   337                 }    
       
   338     		
       
   339     		if ( !address->IsWildAddr() )
       
   340     			{
       
   341         		CreateConnectedTransportPointL( aParams, aResponse, *address );
       
   342         		transport = FindTransportL( aParams, &aResponse, aId, EFalse );
       
   343     			}
       
   344     		}
       
   345 		}
       
   346 	
       
   347 	if ( transport )
       
   348 		{
       
   349 		transport->Send( aParams, aResponse, aId, aStatus );
       
   350 		}
       
   351 	else
       
   352 	    {	
       
   353         TRequestStatus* stat = &aStatus;
       
   354         User::RequestComplete( stat, KErrNotFound );
       
   355 	    }
       
   356 	}
       
   357 
       
   358 // -----------------------------------------------------------------------------
       
   359 // CSipConnection::SendToTransportL
       
   360 // -----------------------------------------------------------------------------
       
   361 //
       
   362 void CSipConnection::SendToTransportL(
       
   363     const TSIPTransportParams& aParams,
       
   364     CSIPRequest& aRequest,
       
   365     TBool aForceUDP,
       
   366     const CUri8* aOutboundProxy,
       
   367 	const TInetAddr& aAddr,
       
   368 	TRequestStatus &aStatus )
       
   369 	{
       
   370 	TUint origTransport = 
       
   371 	    UpdateTransportIfNeededL( aRequest, aParams, aForceUDP, aAddr );
       
   372 
       
   373     // If there's saved settings regarding remote address, use them instead of
       
   374     // address passed from upper layers (probably only the port differs)
       
   375     const TInetAddr* address = 
       
   376         iSettingsList->SavedRemoteAddr( aParams.TransportId() );
       
   377     if ( !address )
       
   378         {
       
   379         address = &aAddr;
       
   380         }
       
   381         
       
   382     TInetAddr localAddr;  
       
   383     GetLocalAddrL( localAddr, address );    
       
   384     iRequestQueue->NATTraverseL( aParams, 
       
   385                                  aRequest, 
       
   386                                  aForceUDP,
       
   387                                  *address, 
       
   388                                  aOutboundProxy,
       
   389                                  origTransport, 
       
   390                                  aStatus,
       
   391                                  localAddr );
       
   392 	}
       
   393 
       
   394 // -----------------------------------------------------------------------------
       
   395 // CSipConnection::ContinueSendToTransportL
       
   396 // -----------------------------------------------------------------------------
       
   397 //	
       
   398 void CSipConnection::ContinueSendToTransportL( 
       
   399     const TSIPTransportParams& aParams,
       
   400     CSIPRequest& aRequest,
       
   401     TBool aForceUDP,
       
   402     const TInetAddr& aRemoteAddr,
       
   403     TUint aLocalPort,
       
   404     TUint aOrigTransport,
       
   405     TRequestStatus& aStatus,
       
   406     TBool aPublicAddrResolved )
       
   407     {
       
   408     __ASSERT_ALWAYS( IsConnectionActive(), User::Leave( KErrNotReady ) );
       
   409 
       
   410 	CTransportBase* transport = 
       
   411 	    FindTransportL( aParams, &aRequest, aRemoteAddr, aLocalPort );
       
   412 	if ( transport != 0 )
       
   413 		{
       
   414 		aStatus = KRequestPending;
       
   415 		transport->Send( aParams, 
       
   416 		                 aRequest,
       
   417 		                 aForceUDP,
       
   418 		                 aRemoteAddr, 
       
   419 		                 aOrigTransport, 
       
   420 		                 aStatus, 
       
   421 		                 aPublicAddrResolved );
       
   422 		}
       
   423 	else
       
   424 		{
       
   425 		if ( aRequest.HasHeader( SIPStrings::StringF(SipStrConsts::EViaHeader) ) )
       
   426 			{
       
   427             if ( IsTransportUdp( &aRequest ) )
       
   428                 {
       
   429             	AddUdpListenerL( 
       
   430             	    TSIPTransportParams( KDefaultOwnerId, iIapId ),
       
   431             	    KDefaultSipPort );
       
   432             	}
       
   433             else
       
   434             	{	
       
   435             	CreateConnectedTransportPointL( aParams, aRequest, aRemoteAddr );
       
   436             	}
       
   437 
       
   438             transport = FindTransportL( aParams, &aRequest, aRemoteAddr );
       
   439             if ( transport )
       
   440                 {
       
   441                 aStatus = KRequestPending;
       
   442                 transport->Send( aParams, 
       
   443                                  aRequest,
       
   444                                  aForceUDP, 
       
   445                                  aRemoteAddr, 
       
   446                                  aOrigTransport, 
       
   447                                  aStatus,
       
   448                                  aPublicAddrResolved );
       
   449             	}
       
   450             else
       
   451             	{
       
   452             	TRequestStatus* stat = &aStatus;
       
   453             	User::RequestComplete( stat, KErrNotReady );
       
   454             	}	
       
   455 			}
       
   456 		}
       
   457     }
       
   458 
       
   459 // -----------------------------------------------------------------------------
       
   460 // CSipConnection::RequestFailed
       
   461 // -----------------------------------------------------------------------------
       
   462 //   
       
   463 void CSipConnection::RequestFailed( TRequestStatus& aStatus, TInt aError )
       
   464     {
       
   465     if ( aStatus == KRequestPending )
       
   466         {
       
   467         TRequestStatus* status = &aStatus;
       
   468 		User::RequestComplete( status, aError );
       
   469         }
       
   470     }
       
   471 
       
   472 // -----------------------------------------------------------------------------
       
   473 // CSipConnection::GetUdpSocketL
       
   474 // -----------------------------------------------------------------------------
       
   475 //    
       
   476 RSocket& CSipConnection::GetUdpSocketL( 
       
   477     const TSIPTransportParams& aParams, 
       
   478     const TInetAddr& aRemoteAddr,
       
   479     TUint& aLocalPort,
       
   480     MSocketUsagePermissionObserver* aObserver,
       
   481     TBool& aPermissionToUse )
       
   482     {
       
   483     CTransportBase* transport = 
       
   484         FindTransport( aParams, 
       
   485                        SIPStrings::StringF( SipStrConsts::EUDP ), 
       
   486                        aRemoteAddr,
       
   487                        aLocalPort,
       
   488                        EFalse );
       
   489     if ( !transport )
       
   490         {
       
   491         if ( !aLocalPort )
       
   492             {
       
   493             aLocalPort = iSettingsList->GetPrivatePortL( aParams );	
       
   494             }
       
   495         transport = AddUdpListenerL( aParams, aLocalPort, ETrue );
       
   496         }
       
   497 
       
   498     RSocket* udpSocket = 
       
   499         transport->AcquireUdpSocket( aObserver, aRemoteAddr, aPermissionToUse );
       
   500         
       
   501     __ASSERT_ALWAYS( udpSocket, User::Leave( KErrNotFound ) );
       
   502     
       
   503     return *udpSocket;
       
   504     }
       
   505 
       
   506 // -----------------------------------------------------------------------------
       
   507 // CSipConnection::UdpSocketFree
       
   508 // -----------------------------------------------------------------------------
       
   509 //       
       
   510 void CSipConnection::UdpSocketFree( 
       
   511     const TSIPTransportParams& aParams,
       
   512     const TInetAddr& aRemoteAddr,
       
   513     TUint aLocalPort,
       
   514     MSocketUsagePermissionObserver* aObserver,
       
   515     RSocket& aSocket )
       
   516     {
       
   517     CTransportBase* transport = 
       
   518         FindTransport( aParams, 
       
   519                        SIPStrings::StringF( SipStrConsts::EUDP ), 
       
   520                        aRemoteAddr,
       
   521                        aLocalPort,
       
   522                        EFalse );
       
   523     if ( transport )
       
   524         {
       
   525         transport->UdpSocketReleased( aObserver, aSocket );
       
   526         }
       
   527     }
       
   528 
       
   529 // -----------------------------------------------------------------------------
       
   530 // CSipConnection::UpdateTransportIfNeededL
       
   531 // -----------------------------------------------------------------------------
       
   532 //
       
   533 TUint CSipConnection::UpdateTransportIfNeededL( 
       
   534     CSIPMessage& aMessage,
       
   535     const TSIPTransportParams& aParams,
       
   536     TBool aForceUDP,
       
   537     const TInetAddr& aAddr )
       
   538 	{
       
   539     TUint origTransport = TransportProtocolL( aMessage );
       
   540 		
       
   541 	CSIPViaHeader* via = TopViaHeader( &aMessage );
       
   542 	
       
   543 	if ( via )
       
   544 	    {
       
   545     	TBool tlsForced = 
       
   546 	        iSettingsList->HasSavedTLSTransportInfo( aParams.TransportId() );
       
   547 	    
       
   548 		if ( tlsForced && origTransport != KProtocolTls )
       
   549 		    {
       
   550 		    via->SetTransportL( SIPStrings::StringF(SipStrConsts::ETLS) );
       
   551 		    
       
   552 		    // Upper layer may give incorrect port information in some 
       
   553 		    // situations when TLS is in use, ensure that at least
       
   554 		    // default TLS port is used.
       
   555 		    iSettingsList->SaveTLSRemoteL( aParams, aAddr );
       
   556 		    }
       
   557 	    else if ( origTransport == KProtocolInetUdp && !aForceUDP &&
       
   558 	            ( aMessage.EncodedHeaderPartLengthL() +
       
   559     		      aMessage.Content().Length() ) > KMaxUdpMessageSize )
       
   560 	        {
       
   561 	       	via->SetTransportL( SIPStrings::StringF(SipStrConsts::ETCP) );
       
   562 	        }
       
   563 	    else
       
   564 	        {
       
   565 	        }
       
   566 	    }
       
   567 	    
       
   568     return origTransport;
       
   569 	}
       
   570 
       
   571 // -----------------------------------------------------------------------------
       
   572 // CSipConnection::CreateConnectedTransportPointL
       
   573 // -----------------------------------------------------------------------------
       
   574 //
       
   575 void CSipConnection::CreateConnectedTransportPointL( 
       
   576     const TSIPTransportParams& aParams,
       
   577     CSIPMessage& aMessage, 
       
   578     const TInetAddr& aAddress )
       
   579 	{
       
   580 	TUint protocol = TransportProtocolL( aMessage );
       
   581 	switch ( protocol )
       
   582 	    {
       
   583 	    case KProtocolInetTcp:
       
   584 	        {
       
   585 	        CreateTcpTransportPointL( aParams, aAddress );
       
   586 	        break;
       
   587 	        }
       
   588 	    case KProtocolTls:
       
   589 	        {
       
   590 	        CreateTlsTransportPointL( aParams, aAddress );
       
   591 	        //Check if the SIP message is request.Below code sets the
       
   592 			//domain name into CTransportTls object which will be later
       
   593 			//used in domain name matching during TLS Negotiation
       
   594 	        HBufC8 *domainName = NULL;
       
   595 	        CTransportTls *transport;
       
   596 	        if(aMessage.IsRequest())
       
   597 	        {
       
   598 	        	//If the SIP request has Route header then the host part will be extracted
       
   599 	        	//from the Route header
       
   600 	        	const RStringF route = SIPStrings::StringF(SipStrConsts::ERouteHeader);	        	
       
   601 	        	if(aMessage.HasHeader(route))
       
   602 	        	{
       
   603 	        		TSglQueIter<CSIPHeaderBase> iter = aMessage.Headers( route );
       
   604 	        	    iter.SetToFirst();
       
   605 	        	    CSIPHeaderBase* header = iter;
       
   606 	        	    if ( header )
       
   607 	        	    {
       
   608 	        			CSIPRouteHeader* routeHeader =	static_cast<CSIPRouteHeader*>( header );
       
   609 	        			domainName = routeHeader->SIPAddress().URI().SIPURI()->HostPort().Host().AllocL();	        			
       
   610 	        		}	        
       
   611 	        	}
       
   612 	        	else
       
   613 	        	{
       
   614 	        		//If the Route header is absent from the SIP Request, request-line
       
   615 	        		//gets preference and host part is extracted from the request-line					
       
   616        				CSIPRequest &request = static_cast<CSIPRequest&>(aMessage ) ;
       
   617        				domainName = request.RequestURI()->SIPURI()->HostPort().Host().AllocL();
       
   618 	        	}	        	
       
   619 	        }
       
   620 	        else
       
   621 	        {
       
   622 	        	//Message is response. Host part will be extracted from the top
       
   623 	        	//Via header
       
   624 	            CSIPViaHeader* viaHeader = TopViaHeader( &aMessage );
       
   625 	            domainName = viaHeader->SentByHostPort().Host().AllocL();	            
       
   626 	        }
       
   627 	        transport = static_cast<CTransportTls*>(FindTransport( aParams, 
       
   628 	        	        								SIPStrings::StringF( SipStrConsts::ETLS ),
       
   629 	        	        								aAddress));
       
   630 			//If the domain name is of type "Text", set the domain name for the
       
   631 			//TLS object.
       
   632 			if ( ( UriUtils::HostType( *domainName ) == UriUtils::ETextHost ))
       
   633 		        transport->SetDomainNameL(domainName->Des());
       
   634 		    delete domainName;	
       
   635 	        break;
       
   636 	        }
       
   637 	    default:
       
   638 	        {
       
   639 	        User::Leave( KErrArgument );
       
   640 	        break;
       
   641 	        }
       
   642 	    }
       
   643 	}
       
   644 	
       
   645 // -----------------------------------------------------------------------------
       
   646 // CSipConnection::CreateTcpTransportPointL
       
   647 // -----------------------------------------------------------------------------
       
   648 //
       
   649 void CSipConnection::CreateTcpTransportPointL( 
       
   650     const TSIPTransportParams& aParams, 
       
   651     const TInetAddr& aAddress )
       
   652 	{
       
   653 	// Leave if tcp connection with remote address exists already
       
   654 	CTransportBase* listItem = 0;
       
   655 	iListIter.SetToFirst();
       
   656 	while( ( listItem = iListIter++ ) != 0 )
       
   657 		{
       
   658 		if( listItem->HasRemoteHost( aParams, 
       
   659 		                             aAddress, 
       
   660 		                             SIPStrings::StringF(SipStrConsts::ETCP) ) )
       
   661 		    {
       
   662 		    User::Leave( KErrSIPTransportFailure );
       
   663 		    }
       
   664 		}	
       
   665 	
       
   666 	CTransportBase* transport =	
       
   667 	    CTransportTcp::NewLC( 
       
   668 	        iServer, iFinder, iTaFactory,
       
   669 			*this, iTimer, aParams, aAddress, iTOne,
       
   670 			*iServerResolver, Connection(), iSigCompHandler, 
       
   671 			iSettingsList->SavedLocalSendPort( aParams.TransportId() ),
       
   672 			iNetworkInfo, *iSettingsList, iNATTraversal );        
       
   673 	AddTransportL( transport );
       
   674 	CleanupStack::Pop( transport );
       
   675 	}
       
   676 	
       
   677 // -----------------------------------------------------------------------------
       
   678 // CSipConnection::CreateTlsTransportPointL
       
   679 // -----------------------------------------------------------------------------
       
   680 //
       
   681 void CSipConnection::CreateTlsTransportPointL( 
       
   682     const TSIPTransportParams& aParams, 
       
   683     const TInetAddr& aAddress )
       
   684 	{
       
   685 	// Leave if tcp connection with remote address exists already
       
   686 	CTransportBase* listItem = 0;
       
   687 	iListIter.SetToFirst();
       
   688 	while( ( listItem = iListIter++ ) != 0 )
       
   689 		{
       
   690 		if( listItem->HasRemoteHost( aParams, 
       
   691 		                             aAddress,
       
   692 		                             SIPStrings::StringF(SipStrConsts::ETLS) ) )
       
   693 		    {
       
   694 		    User::Leave( KErrSIPTransportFailure );
       
   695 		    }
       
   696 		}	
       
   697 	
       
   698 	CTransportBase* transport =	
       
   699 	    CTransportTls::NewLC( 
       
   700 	        iServer, iFinder, iTaFactory,
       
   701 			*this, iTimer, aParams, aAddress, iTOne,
       
   702 			*iServerResolver, Connection(), iSigCompHandler, 
       
   703 			iNetworkInfo, *iSettingsList, iNATTraversal );        
       
   704 	AddTransportL( transport );
       
   705 	CleanupStack::Pop( transport );
       
   706 	}
       
   707 
       
   708 // -----------------------------------------------------------------------------
       
   709 // CSipConnection::IsTransportUdp
       
   710 // -----------------------------------------------------------------------------
       
   711 //
       
   712 TBool CSipConnection::IsTransportUdp( CSIPMessage* aMessage )
       
   713 	{
       
   714 	CSIPViaHeader* via = TopViaHeader( aMessage );
       
   715 	if( via )
       
   716 		{
       
   717 		if( via->Transport() == SIPStrings::StringF(SipStrConsts::EUDP) )
       
   718 			{
       
   719 			return ETrue;
       
   720 			}
       
   721 		}
       
   722 
       
   723 	return EFalse;
       
   724 	}
       
   725 
       
   726 // -----------------------------------------------------------------------------
       
   727 // CSipConnection::TransportProtocolL
       
   728 // -----------------------------------------------------------------------------
       
   729 //	
       
   730 TUint CSipConnection::TransportProtocolL( CSIPMessage& aMessage )
       
   731     {
       
   732     CSIPViaHeader* via = TopViaHeader( &aMessage );
       
   733     __ASSERT_ALWAYS( via, User::Leave( KErrNotFound ) );
       
   734 
       
   735 	RStringF transport = via->Transport(); 
       
   736 	if ( transport == SIPStrings::StringF( SipStrConsts::EUDP ) )
       
   737 		{
       
   738 		return KProtocolInetUdp;
       
   739 		}
       
   740     else if ( transport == SIPStrings::StringF( SipStrConsts::ETCP ) )
       
   741 		{
       
   742 		return KProtocolInetTcp;
       
   743 		}
       
   744     else if ( transport == SIPStrings::StringF( SipStrConsts::ETLS ) )
       
   745 		{
       
   746 		return KProtocolTls;
       
   747 		}
       
   748 	else
       
   749 	    {
       
   750 	    User::Leave( KErrNotSupported );
       
   751 	    }
       
   752 	return 0;
       
   753     }
       
   754 
       
   755 // -----------------------------------------------------------------------------
       
   756 // CSipConnection::ResetTransportsL
       
   757 // -----------------------------------------------------------------------------
       
   758 //
       
   759 void CSipConnection::ResetTransportsL()
       
   760 	{
       
   761 	RemoveAllTransports();
       
   762 	TSIPTransportParams defaultParams( KDefaultOwnerId, iIapId );
       
   763 	AddTcpListenerL( defaultParams, KDefaultSipPort );
       
   764 	AddUdpListenerL( defaultParams, KDefaultSipPort );
       
   765 	}
       
   766 
       
   767 // -----------------------------------------------------------------------------
       
   768 // CSipConnection::IsInList
       
   769 // -----------------------------------------------------------------------------
       
   770 //
       
   771 TBool CSipConnection::IsInList( CTransportBase* aTransport )
       
   772 	{
       
   773 	if( aTransport )
       
   774 		{
       
   775 		CTransportBase* listItem;
       
   776 		iListIter.SetToFirst();
       
   777 		while ( ( listItem = iListIter++ ) != 0 )
       
   778 			{
       
   779 			if( listItem == aTransport )
       
   780 				{
       
   781 				return ETrue;
       
   782 				}
       
   783 			}
       
   784 		}
       
   785 	return EFalse;
       
   786 	}
       
   787 
       
   788 // -----------------------------------------------------------------------------
       
   789 // CSipConnection::AddTransportL
       
   790 // -----------------------------------------------------------------------------
       
   791 //
       
   792 void CSipConnection::AddTransportL( CTransportBase* aTransport )
       
   793 	{
       
   794 	if( aTransport )
       
   795 		{
       
   796 		if( !IsInList( aTransport ) )
       
   797 			{
       
   798 			iList.AddLast( *aTransport );
       
   799 			}
       
   800 		else
       
   801 			{
       
   802 			User::Leave( KErrAlreadyExists );
       
   803 			}
       
   804 		}
       
   805 	else
       
   806 		{
       
   807 		User::Leave( KErrGeneral );
       
   808 		}
       
   809 	}
       
   810 
       
   811 // -----------------------------------------------------------------------------
       
   812 // CSipConnection::RemoveTransport
       
   813 // -----------------------------------------------------------------------------
       
   814 //
       
   815 TInt CSipConnection::RemoveTransport( CTransportBase* aTransport )
       
   816 	{
       
   817 	if( IsInList( aTransport ) )
       
   818 		{
       
   819 		iList.Remove( *aTransport );
       
   820 		delete aTransport;
       
   821 		return KErrNone;
       
   822 		}
       
   823 	else
       
   824 		{
       
   825 		return KErrNotFound;
       
   826 		}
       
   827 	}
       
   828 
       
   829 // -----------------------------------------------------------------------------
       
   830 // CSipConnection::RemoveAllTransports
       
   831 // -----------------------------------------------------------------------------
       
   832 //
       
   833 void CSipConnection::RemoveAllTransports()
       
   834 	{
       
   835 	CTransportBase* listItem( NULL );
       
   836 	iListIter.SetToFirst();
       
   837 	while ( ( listItem = iListIter++ ) != 0 )
       
   838 		{
       
   839 		iList.Remove( *listItem );
       
   840 		delete listItem;
       
   841 		}
       
   842 	}
       
   843 
       
   844 // -----------------------------------------------------------------------------
       
   845 // CSipConnection::FindTransportL
       
   846 // -----------------------------------------------------------------------------
       
   847 //
       
   848 CTransportBase* CSipConnection::FindTransportL( 
       
   849     const TSIPTransportParams& aParams,
       
   850     CSIPMessage* aMessage,
       
   851 	TTransactionId aId,
       
   852 	TBool aIsStrict )
       
   853 	{
       
   854 	iListIter.SetToFirst();
       
   855 
       
   856 	CTransportBase* listItem = NULL;
       
   857 	while ( ( listItem = iListIter++ ) != 0 )
       
   858 		{
       
   859 		if( listItem->HandleL( aParams, aMessage, aId, aIsStrict ) )
       
   860 			{
       
   861 			return listItem;
       
   862 			}
       
   863 		}
       
   864 	return 0;
       
   865 	}
       
   866 
       
   867 // -----------------------------------------------------------------------------
       
   868 // CSipConnection::FindTransportL
       
   869 // -----------------------------------------------------------------------------
       
   870 //
       
   871 CTransportBase* CSipConnection::FindTransportL( 
       
   872     const TSIPTransportParams& aParams,
       
   873     CSIPMessage* aMessage,
       
   874 	const TInetAddr& aRemoteAddr,
       
   875 	TUint aLocalPort,
       
   876 	TBool aIsStrict )
       
   877 	{
       
   878 	CTransportBase* listItem;
       
   879 	iListIter.SetToFirst();
       
   880 
       
   881 	while ( ( listItem = iListIter++ ) != 0 )
       
   882 		{
       
   883 		if ( listItem->HandleL( aParams, 
       
   884 		                        aMessage, 
       
   885 		                        aRemoteAddr, 
       
   886 		                        aLocalPort,
       
   887 		                        aIsStrict ) )
       
   888 			{
       
   889 			return listItem;
       
   890 			}
       
   891 		}
       
   892 	return 0;
       
   893 	}
       
   894 
       
   895 // -----------------------------------------------------------------------------
       
   896 // CSipConnection::FindTransportL
       
   897 // -----------------------------------------------------------------------------
       
   898 //
       
   899 CTransportBase* CSipConnection::FindTransport( 
       
   900     const TSIPTransportParams& aParams,
       
   901     RStringF aProtocol,
       
   902 	const TInetAddr& aRemoteAddr,
       
   903 	TUint aLocalPort,
       
   904 	TBool aIsStrict )
       
   905 	{
       
   906 	CTransportBase* listItem;
       
   907 	iListIter.SetToFirst();
       
   908 	while ( ( listItem = iListIter++ ) != 0 )
       
   909 		{
       
   910 		if ( listItem->HandleMessage( aParams, 
       
   911 		                              aProtocol, 
       
   912 		                              aRemoteAddr, 
       
   913 		                              aLocalPort,
       
   914 		                              aIsStrict ) )
       
   915 			{
       
   916 			return listItem;
       
   917 			}
       
   918 		}
       
   919 	return 0;
       
   920 	}
       
   921 	
       
   922 // -----------------------------------------------------------------------------
       
   923 // CSipConnection::RemoveTcpSender
       
   924 // -----------------------------------------------------------------------------
       
   925 //
       
   926 void CSipConnection::RemoveTcpSender( CTcpResponseSender* aThis )
       
   927 	{
       
   928 	if ( !iTcpSenderList.IsEmpty() )
       
   929 		{
       
   930 		iTcpSenderList.Remove( *aThis );
       
   931 		delete aThis;
       
   932 		}
       
   933 	}
       
   934 
       
   935 // -----------------------------------------------------------------------------
       
   936 // CSipConnection::RemoveAllTcpSender
       
   937 // -----------------------------------------------------------------------------
       
   938 //
       
   939 void CSipConnection::RemoveAllTcpSender()
       
   940 	{
       
   941 	CTcpResponseSender* listItem( NULL );
       
   942 	iTcpSenderListIter.SetToFirst();
       
   943 	while ( ( listItem = iTcpSenderListIter++ ) != 0 )
       
   944 		{
       
   945 		iTcpSenderList.Remove( *listItem );
       
   946 		delete listItem;
       
   947 		}
       
   948 	}
       
   949 
       
   950 // -----------------------------------------------------------------------------
       
   951 // CSipConnection::TcpConnected
       
   952 // -----------------------------------------------------------------------------
       
   953 //
       
   954 void CSipConnection::TcpConnected( const TInetAddr& aAddr )
       
   955 	{
       
   956 	CTcpResponseSender* listItem( NULL );
       
   957 	iTcpSenderListIter.SetToFirst();
       
   958 	while ( ( listItem = iTcpSenderListIter++ ) != 0 )
       
   959 		{
       
   960 		listItem->Connected( aAddr );
       
   961 		}
       
   962 	}
       
   963 
       
   964 // -----------------------------------------------------------------------------
       
   965 // CSipConnection::ReRouteL
       
   966 // -----------------------------------------------------------------------------
       
   967 //
       
   968 void CSipConnection::ReRouteL( TUint aProtocol, COutgoingData* aData )
       
   969 	{
       
   970 	__ASSERT_ALWAYS( aData, User::Leave( KErrArgument ) );
       
   971 
       
   972 	const TSIPTransportParams& tpParams = aData->TransportParams();
       
   973 	if ( aProtocol == KProtocolInetTcp &&
       
   974 		 aData->Message().IsRequest() )
       
   975 		{
       
   976 		CSIPViaHeader* via = TopViaHeader( &aData->Message() );
       
   977 		if(via)
       
   978 		{
       
   979 		    via->SetTransportL( SIPStrings::StringF(SipStrConsts::ETCP) );
       
   980 		}
       
   981 				
       
   982 		// Public address resolved value can be passed as true, since
       
   983 		// at this point all necessary via and contact header updates
       
   984 		// have been done (i.e. no need to update).
       
   985 		ContinueSendToTransportL(
       
   986 		    tpParams,
       
   987 		    static_cast<CSIPRequest&>( aData->Message() ),
       
   988 		    aData->ForceUDP(),
       
   989 			aData->Address(), 
       
   990 			0,
       
   991 			aData->OrigTransport(), 
       
   992 			*aData->Status(),
       
   993 			ETrue );
       
   994 		delete aData;
       
   995 		return;
       
   996 		}
       
   997 
       
   998 	if ( aProtocol == KProtocolInetTcp )
       
   999 		{
       
  1000 		CTcpResponseSender* tcpsender( NULL );
       
  1001 		while ( (tcpsender = iTcpSenderListIter++ ) != 0 )
       
  1002 			{
       
  1003 			if ( tcpsender->IsInProgress( *aData ) )
       
  1004 				{
       
  1005 				tcpsender->ConnectionFailedL();
       
  1006 				delete aData;
       
  1007 				return;
       
  1008 				}
       
  1009 			}
       
  1010 		tcpsender =
       
  1011 			new ( ELeave ) CTcpResponseSender( tpParams, 
       
  1012 			                                   *iServerResolver, 
       
  1013 			                                   *this );
       
  1014 		iTcpSenderList.AddLast( *tcpsender );
       
  1015 		tcpsender->ResolveL( *aData );
       
  1016 		tcpsender->SetData( aData );
       
  1017 		}
       
  1018 
       
  1019 	if ( aProtocol == KProtocolInetUdp &&
       
  1020 		 ( aData->Message().EncodedHeaderPartLengthL() +
       
  1021 		   aData->Message().Content().Length() ) > KMaxMessageLength )
       
  1022 		{
       
  1023 		TRequestStatus* stat = aData->Status();
       
  1024 		User::RequestComplete( stat, KErrCouldNotConnect );
       
  1025 		delete aData;
       
  1026 		return;
       
  1027 		}
       
  1028 
       
  1029 	if ( aProtocol == KProtocolInetUdp )
       
  1030 		{
       
  1031 		CSIPViaHeader* via = TopViaHeader( &aData->Message() );
       
  1032 		if ( via )
       
  1033 			{
       
  1034 			via->SetTransportL( SIPStrings::StringF(SipStrConsts::EUDP) );
       
  1035 			}
       
  1036 		CSender* send = GetSender( tpParams, aProtocol, aData->Address() );
       
  1037 		if ( send )
       
  1038 			{
       
  1039 			send->SendL( aData );
       
  1040 			}
       
  1041 		else
       
  1042 			{
       
  1043 			AddUdpListenerL( 
       
  1044 			    TSIPTransportParams( KDefaultOwnerId, iIapId ),
       
  1045 			    KDefaultSipPort );
       
  1046 			send = GetSender( tpParams, aProtocol, aData->Address() );
       
  1047 			if ( send )
       
  1048 				{
       
  1049 				send->SendL( aData );
       
  1050 				}
       
  1051 			else
       
  1052 				{
       
  1053 				TRequestStatus* stat = aData->Status();
       
  1054 				User::RequestComplete( stat, KErrCouldNotConnect );
       
  1055 				delete aData;
       
  1056 				}
       
  1057 			}
       
  1058 		}
       
  1059 	}
       
  1060 
       
  1061 // -----------------------------------------------------------------------------
       
  1062 // CSipConnection::GetSender
       
  1063 // -----------------------------------------------------------------------------
       
  1064 //
       
  1065 CSender* CSipConnection::GetSender( 
       
  1066     const TSIPTransportParams& aParams,
       
  1067     TUint aProtocol, 
       
  1068     const TInetAddr& aAddr )
       
  1069 	{
       
  1070 	CTransportBase* listItem;
       
  1071 	iListIter.SetToFirst();
       
  1072 
       
  1073 	CSender* sender = NULL;
       
  1074 	while( ( ( listItem = iListIter++ ) != 0 ) && !sender )
       
  1075 		{
       
  1076 		sender = listItem->Sender( aParams, aProtocol, aAddr );
       
  1077 		}
       
  1078 
       
  1079 	return sender;
       
  1080 	}
       
  1081 
       
  1082 // -----------------------------------------------------------------------------
       
  1083 // CSipConnection::AddListenerL
       
  1084 // -----------------------------------------------------------------------------
       
  1085 //
       
  1086 void CSipConnection::AddListenerL( 
       
  1087     const TSIPTransportParams& aParams,
       
  1088     TUint aProtocol, 
       
  1089     TUint aPort )
       
  1090 	{
       
  1091 	AddTcpListenerL( aParams, aPort );
       
  1092 	if( aProtocol != KProtocolInetTcp )
       
  1093 		{
       
  1094 		AddUdpListenerL( aParams, aPort );
       
  1095 		}
       
  1096 	}
       
  1097 
       
  1098 // -----------------------------------------------------------------------------
       
  1099 // CSipConnection::AddTcpListenerL
       
  1100 // -----------------------------------------------------------------------------
       
  1101 //
       
  1102 void CSipConnection::AddTcpListenerL( 
       
  1103     const TSIPTransportParams& aParams, 
       
  1104     TUint aPort,
       
  1105     TBool aIsReserved )
       
  1106 	{
       
  1107 	if( !IsListened( KProtocolInetTcp, aPort ) )
       
  1108 		{                       
       
  1109 		CTransportBase* transportTcp =
       
  1110 			CTcpListeningPoint::NewLC( iServer, iFinder, iTaFactory, *this,
       
  1111 									   iTimer, aPort, iTOne, *iServerResolver,
       
  1112 									   Connection(), iSigCompHandler,
       
  1113 									   iNetworkInfo, *iSettingsList, aParams,
       
  1114 									   iNATTraversal );
       
  1115 		AddTransportL( transportTcp );
       
  1116 		CleanupStack::Pop( transportTcp );
       
  1117 		transportTcp->WaitL();
       
  1118 		}
       
  1119     else
       
  1120         {
       
  1121         __ASSERT_ALWAYS( !aIsReserved, User::Leave( KErrSIPTransportFailure ) );
       
  1122         }
       
  1123 	}
       
  1124 
       
  1125 // -----------------------------------------------------------------------------
       
  1126 // CSipConnection::AddUdpListenerL
       
  1127 // -----------------------------------------------------------------------------
       
  1128 //
       
  1129 CTransportBase* CSipConnection::AddUdpListenerL( 
       
  1130     const TSIPTransportParams& aParams,
       
  1131     TUint aPort,
       
  1132     TBool aIsReserved,
       
  1133     TBool aOnlyForReceiving )
       
  1134 	{
       
  1135 	CTransportBase* transport( NULL );
       
  1136 	if( !IsListened( KProtocolInetUdp, aPort ) )
       
  1137 		{
       
  1138 		transport =
       
  1139 			CTransportUdp::NewLC( iServer, aPort, iFinder,
       
  1140 			                      iTaFactory, *this, iTimer, *iServerResolver,
       
  1141 			                      Connection(), iSigCompHandler, iTOne,
       
  1142 			                      iNetworkInfo, *iSettingsList, aParams,
       
  1143 			                      iNATTraversal, aOnlyForReceiving );
       
  1144 		AddTransportL( transport );
       
  1145 		CleanupStack::Pop( transport );
       
  1146 		transport->WaitL();
       
  1147 		}
       
  1148     else
       
  1149         {
       
  1150         __ASSERT_ALWAYS( !aIsReserved, User::Leave( KErrSIPTransportFailure ) );
       
  1151         }
       
  1152         
       
  1153     return transport;
       
  1154 	}
       
  1155 
       
  1156 // -----------------------------------------------------------------------------
       
  1157 // CSipConnection::AddTcpListenerUserL
       
  1158 // -----------------------------------------------------------------------------
       
  1159 //
       
  1160 void CSipConnection::AddTcpListenerUserL( 
       
  1161     const TSIPTransportParams& aOldParams,
       
  1162     const TSIPTransportParams& aParams,
       
  1163     TUint aReceivePort )
       
  1164     {
       
  1165     CTransportBase* listItem( NULL );
       
  1166 	iListIter.SetToFirst();
       
  1167 	
       
  1168 	TBool added( EFalse );
       
  1169    	while ( ( listItem = iListIter++ ) != 0 && !added )
       
  1170 		{
       
  1171 		if( listItem->Handle( aOldParams.TransportId() ) &&
       
  1172 		    listItem->ProtocolType() == KProtocolInetTcp )
       
  1173 			{
       
  1174 			added = listItem->AddUserL( aParams, aReceivePort );
       
  1175 			}
       
  1176 		}
       
  1177 	if ( !added )
       
  1178 	    {
       
  1179         User::Leave( KErrNotFound );
       
  1180 	    }
       
  1181     }
       
  1182     
       
  1183 // -----------------------------------------------------------------------------
       
  1184 // CSipConnection::AddUdpListenerUserL
       
  1185 // -----------------------------------------------------------------------------
       
  1186 //
       
  1187 void CSipConnection::AddUdpListenerUserL( 
       
  1188     const TSIPTransportParams& aOldParams,
       
  1189     const TSIPTransportParams& aParams,
       
  1190     TUint aReceivePort )
       
  1191     {
       
  1192     CTransportBase* listItem( NULL );
       
  1193 	iListIter.SetToFirst();
       
  1194 	
       
  1195 	TBool added( EFalse );
       
  1196    	while ( ( listItem = iListIter++ ) != 0 && !added )
       
  1197 		{
       
  1198 		if( listItem->Handle( aOldParams.TransportId() ) &&
       
  1199 		    listItem->ProtocolType() == KProtocolInetUdp )
       
  1200 			{
       
  1201 			added = listItem->AddUserL( aParams, aReceivePort );
       
  1202 			}
       
  1203 		}
       
  1204 	if ( !added )
       
  1205 	    {
       
  1206         User::Leave( KErrNotFound );
       
  1207 	    }
       
  1208     }
       
  1209     
       
  1210 // -----------------------------------------------------------------------------
       
  1211 // CSipConnection::IsListened
       
  1212 // -----------------------------------------------------------------------------
       
  1213 //
       
  1214 TBool CSipConnection::IsListened( TInt aProtocol, TUint aPort )
       
  1215 	{
       
  1216 	CTransportBase* listItem( NULL );
       
  1217 	iListIter.SetToFirst();
       
  1218 
       
  1219 	while ( ( listItem = iListIter++ ) != 0 )
       
  1220 		{
       
  1221 		if( listItem->IsWaiting( aProtocol, aPort ) )
       
  1222 			{
       
  1223 			return ETrue;
       
  1224 			}
       
  1225 		}
       
  1226 	return EFalse;
       
  1227 	}
       
  1228 
       
  1229 // -----------------------------------------------------------------------------
       
  1230 // CSipConnection::CancelAllRequests
       
  1231 // -----------------------------------------------------------------------------
       
  1232 //
       
  1233 void CSipConnection::CancelAllRequests( CSIPConnection::TState aReason )
       
  1234 	{
       
  1235 	CTransportBase* listItem( NULL );
       
  1236 	iListIter.SetToFirst();
       
  1237 	while ( ( listItem = iListIter++ ) != 0 )
       
  1238 		{
       
  1239 		listItem->CancelAllRequests( aReason );
       
  1240 		}
       
  1241 	}
       
  1242 
       
  1243 // -----------------------------------------------------------------------------
       
  1244 // CSipConnection::IcmpError
       
  1245 // -----------------------------------------------------------------------------
       
  1246 //
       
  1247 void CSipConnection::IcmpError( const TInetAddr& aAddress )
       
  1248 	{
       
  1249 	TRAP_IGNORE( NotifyIcmpErrorL( aAddress ) )
       
  1250 	}
       
  1251 
       
  1252 // -----------------------------------------------------------------------------
       
  1253 // CSipConnection::GetByURIL
       
  1254 // -----------------------------------------------------------------------------
       
  1255 //
       
  1256 void CSipConnection::GetByURIL( const TSIPTransportParams& aParams,
       
  1257                                 const CURIContainer& aURI,
       
  1258                                 RPointerArray<MSIPResolvingResult>& aResult,
       
  1259 		                        MSIPServerResolverObserver* aObserver )
       
  1260 	{
       
  1261 	__ASSERT_ALWAYS( iServerResolver, User::Leave( KErrNotReady ) );
       
  1262 	
       
  1263 	// Have to do const cast in order to get binding observer
       
  1264 	MSIPNATBindingObserver* bindingObserver = 
       
  1265 	    const_cast<TSIPTransportParams&>( aParams ).NATBindingObserver();
       
  1266 
       
  1267 	TBool persistentConnectionFound( EFalse );
       
  1268 	TInetAddr* remoteAddr = 0;
       
  1269 	TUint protocol = 0;
       
  1270 	
       
  1271 	CTransportBase* listItem;
       
  1272     iListIter.SetToFirst();
       
  1273 	while ( ( listItem = iListIter++ ) != 0 && !persistentConnectionFound )
       
  1274 		{
       
  1275 		protocol = listItem->ProtocolType();
       
  1276 		remoteAddr = listItem->PersistentRemoteAddr( bindingObserver );
       
  1277 		
       
  1278 		persistentConnectionFound = 
       
  1279 		    ( remoteAddr &&
       
  1280 		    ( protocol == KProtocolInetTcp || protocol == KProtocolTls ) );
       
  1281 	    }
       
  1282 	    
       
  1283 	TBool skipNaptrAndSrv = aParams.SkipNaptrAndSrv();    
       
  1284 	    
       
  1285 	if ( persistentConnectionFound )
       
  1286 	    {
       
  1287 	    CURIContainer* uriContainer = CURIContainer::NewLC( aURI );
       
  1288 		UpdateURIL( *uriContainer, *remoteAddr, protocol );
       
  1289 		iServerResolver->GetByURIL( *uriContainer, skipNaptrAndSrv, 
       
  1290 		                            aResult, aObserver );
       
  1291 		CleanupStack::PopAndDestroy( uriContainer );
       
  1292 		return;
       
  1293 	    }
       
  1294 
       
  1295     iServerResolver->GetByURIL( aURI, skipNaptrAndSrv, aResult, aObserver );
       
  1296 	}
       
  1297 
       
  1298 // -----------------------------------------------------------------------------
       
  1299 // CSipConnection::CancelGetByUri
       
  1300 // -----------------------------------------------------------------------------
       
  1301 //
       
  1302 void CSipConnection::CancelGetByUri( MSIPServerResolverObserver* aObserver )
       
  1303 	{
       
  1304 	if( iServerResolver )
       
  1305 	    { 
       
  1306     	iServerResolver->Cancel( aObserver );
       
  1307 	    }
       
  1308 	}
       
  1309 
       
  1310 // -----------------------------------------------------------------------------
       
  1311 // CSipConnection::GetLocalAddress
       
  1312 // -----------------------------------------------------------------------------
       
  1313 //
       
  1314 TInt CSipConnection::GetLocalAddress( TInetAddr& aAddr )
       
  1315 	{
       
  1316 	if ( !iLocalAddrResolver )
       
  1317 		{
       
  1318 		return KErrNotReady;
       
  1319 		}
       
  1320 
       
  1321 	TRAPD( err, GetLocalAddrL( aAddr, NULL ) );
       
  1322 		
       
  1323 	return err;
       
  1324 	}
       
  1325 
       
  1326 // -----------------------------------------------------------------------------
       
  1327 // CSipConnection::IsLocalAddr
       
  1328 // -----------------------------------------------------------------------------
       
  1329 //
       
  1330 TBool CSipConnection::IsLocalAddr( TInetAddr& aAddr ) const
       
  1331     {
       
  1332     return iLocalAddrResolver->IsLocalAddr( aAddr );
       
  1333     }
       
  1334 
       
  1335 // -----------------------------------------------------------------------------
       
  1336 // CSipConnection::GetLocalAddrL
       
  1337 // -----------------------------------------------------------------------------
       
  1338 //
       
  1339 void CSipConnection::GetLocalAddrL( 
       
  1340     TInetAddr& aAddr, 
       
  1341     const TInetAddr* aNextHop )
       
  1342 	{
       
  1343 	if( iLocalAddrResolver )
       
  1344 		{
       
  1345 	    iLocalAddrResolver->RefreshLocalAddressesL();
       
  1346 	    FillWithMatchingAddrFamily( aAddr, aNextHop );
       
  1347 		}
       
  1348 	}
       
  1349 
       
  1350 // -----------------------------------------------------------------------------
       
  1351 // CSipConnection::GetLocalIPAddr
       
  1352 // -----------------------------------------------------------------------------
       
  1353 //
       
  1354 TBool CSipConnection::GetLocalIPAddr( TUint aFamily, TInetAddr& aAddr )
       
  1355     {
       
  1356     TBool found( EFalse );
       
  1357     if( iLocalAddrResolver )
       
  1358 		{
       
  1359 		if ( aFamily == KAfInet )
       
  1360 		    {
       
  1361 		    if ( !iLocalAddrResolver->IPv4Addr().IsUnspecified() )
       
  1362 		        {
       
  1363         		aAddr = iLocalAddrResolver->IPv4Addr();
       
  1364         		found = ETrue;
       
  1365 		        }
       
  1366 		    }
       
  1367 		else
       
  1368 		    {
       
  1369 		    if ( !iLocalAddrResolver->IPv6Addr().IsUnspecified() )
       
  1370 		        {
       
  1371         		aAddr = iLocalAddrResolver->IPv6Addr();
       
  1372         		found = ETrue;
       
  1373 		        }
       
  1374 		    }
       
  1375 
       
  1376 		}
       
  1377 	return found;
       
  1378     }
       
  1379 
       
  1380 // -----------------------------------------------------------------------------
       
  1381 // CSipConnection::FillWithMatchingAddrFamily
       
  1382 // -----------------------------------------------------------------------------
       
  1383 //
       
  1384 void CSipConnection::FillWithMatchingAddrFamily( 
       
  1385     TInetAddr& aAddr,
       
  1386     const TInetAddr* aNextHop )
       
  1387     {
       
  1388     __SIP_ADDR_LOG("CSipConnection::FillWithMatchingAddrFamily: local IPv6 addr",
       
  1389                    iLocalAddrResolver->IPv6Addr())
       
  1390     __SIP_ADDR_LOG("CSipConnection::FillWithMatchingAddrFamily: local IPv4 addr",               
       
  1391                    iLocalAddrResolver->IPv4Addr())
       
  1392     
       
  1393     if ( !iLocalAddrResolver->IPv4Addr().IsUnspecified() )
       
  1394 	    {
       
  1395 	    aAddr = iLocalAddrResolver->IPv4Addr();
       
  1396 	    }
       
  1397 	else
       
  1398 	    {
       
  1399 	    aAddr = iLocalAddrResolver->IPv6Addr();
       
  1400 	    }
       
  1401     
       
  1402 	if ( aNextHop )
       
  1403 	    {
       
  1404         __SIP_ADDR_LOG("CSipConnection::FillWithMatchingAddrFamily: next hop addr",               
       
  1405                        *aNextHop)	    
       
  1406 	    
       
  1407                        
       
  1408 	    if ( aNextHop->Address() ) // Contains IPv4 address
       
  1409 	        {
       
  1410         	__SIP_INT_LOG1("CSipConnection::FillWithMatchingAddrFamily: next hop family",
       
  1411                        KAfInet)
       
  1412 	        if ( iLocalAddrResolver->IPv4Addr().IsUnspecified() &&
       
  1413 	             ( aAddr.IsV4Compat() || aAddr.IsV4Mapped() ) )
       
  1414 	            {
       
  1415 	            aAddr.ConvertToV4();
       
  1416 	            }
       
  1417 	        }
       
  1418 	    else
       
  1419 	        {
       
  1420         	__SIP_INT_LOG1("CSipConnection::FillWithMatchingAddrFamily: next hop family",
       
  1421                        KAfInet6)
       
  1422 	        if ( iLocalAddrResolver->IPv6Addr().IsUnspecified() )
       
  1423 	            {
       
  1424 	            aAddr = iLocalAddrResolver->IPv4Addr();
       
  1425 	            aAddr.ConvertToV4Mapped();
       
  1426 	            }
       
  1427 	        else
       
  1428 	            {
       
  1429 	            aAddr = iLocalAddrResolver->IPv6Addr();
       
  1430 	            }
       
  1431 	        }
       
  1432 	    }
       
  1433 	__SIP_ADDR_LOG("CSipConnection::FillWithMatchingAddrFamily returns",aAddr)
       
  1434     }
       
  1435 
       
  1436 // -----------------------------------------------------------------------------
       
  1437 // CSipConnection::DecreaseUserCount
       
  1438 // -----------------------------------------------------------------------------
       
  1439 //
       
  1440 TInt CSipConnection::DecreaseUserCount( const TSIPTransportParams& aParams )
       
  1441 	{
       
  1442 	__SIP_ASSERT_RETURN_VALUE( iUserCount > 0, KErrUnderflow );	
       
  1443 	
       
  1444 	iSettingsList->RemoveSettings( aParams );
       
  1445 	
       
  1446 	// Remove all related transports, default transports are not removed
       
  1447 	//
       
  1448    	CTransportBase* listItem;
       
  1449 	iListIter.SetToFirst();
       
  1450 
       
  1451 	while ( ( listItem = iListIter++ ) != 0 )
       
  1452 		{
       
  1453 		if ( listItem->Remove( aParams ) )
       
  1454 			{
       
  1455 			RemoveTransport( listItem );
       
  1456 			}
       
  1457 		}
       
  1458 		
       
  1459 	iUserCount--;	
       
  1460 	return iUserCount;
       
  1461 	}
       
  1462 
       
  1463 // -----------------------------------------------------------------------------
       
  1464 // CSipConnection::IncreaseUserCount
       
  1465 // -----------------------------------------------------------------------------
       
  1466 //
       
  1467 void CSipConnection::IncreaseUserCount()
       
  1468 	{
       
  1469 	iUserCount++;
       
  1470 	}
       
  1471 
       
  1472 // -----------------------------------------------------------------------------
       
  1473 // CSipConnection::Handle
       
  1474 // -----------------------------------------------------------------------------
       
  1475 //
       
  1476 TBool CSipConnection::Handle( TUint32 aIapId ) const
       
  1477 	{
       
  1478 	return ( aIapId == iIapId );
       
  1479 	}
       
  1480 
       
  1481 // -----------------------------------------------------------------------------
       
  1482 // CSipConnection::IapId
       
  1483 // -----------------------------------------------------------------------------
       
  1484 //	
       
  1485 TUint32 CSipConnection::IapId() const
       
  1486     { 
       
  1487     return iIapId;
       
  1488     }
       
  1489     
       
  1490 // -----------------------------------------------------------------------------
       
  1491 // CSipConnection::ReserveTransportL
       
  1492 // -----------------------------------------------------------------------------
       
  1493 //		
       
  1494 void CSipConnection::ReserveTransportL( 
       
  1495     const TSIPTransportParams& aOldTransportParams,
       
  1496     const TSIPTransportParams& aTransportParams,
       
  1497     TUint aLocalSendPort,
       
  1498 	TUint aLocalReceivePort,
       
  1499 	const TInetAddr& aRemoteAddr,
       
  1500 	TUint aRemoteSendPort )
       
  1501     {
       
  1502     // First reserve ports and save information about transport.
       
  1503     // - will leave with KErrSIPTransportFailure if some of the ports are 
       
  1504     // already reserved (if reservations for old transport id exist already,
       
  1505     // duplicate reservations are allowed).
       
  1506     
       
  1507     if ( !iSettingsList->SaveTransportInfoL( aOldTransportParams,
       
  1508                                              aTransportParams, 
       
  1509                                              aLocalSendPort, 
       
  1510                                              aLocalReceivePort,  
       
  1511                                              aRemoteAddr,
       
  1512                                              aRemoteSendPort ) )
       
  1513         {
       
  1514         // Because no previous settings matching aOldTransportParams were found
       
  1515         // also listeners don't exist yet, create them
       
  1516         //
       
  1517         AddTcpListenerL( aTransportParams, aLocalReceivePort, ETrue );
       
  1518     	
       
  1519     	AddUdpListenerL( aTransportParams, aLocalReceivePort, ETrue, ETrue );
       
  1520         }
       
  1521    else
       
  1522         {
       
  1523         // For existing tcp and udp listeners, add knowledge about second user
       
  1524         //
       
  1525         AddTcpListenerUserL( aOldTransportParams, 
       
  1526                              aTransportParams, 
       
  1527                              aLocalReceivePort );
       
  1528                              
       
  1529         AddUdpListenerUserL( aOldTransportParams, 
       
  1530                              aTransportParams, 
       
  1531                              aLocalReceivePort );                  
       
  1532         }
       
  1533         
       
  1534    AddUdpListenerL( aTransportParams, aLocalSendPort, ETrue );
       
  1535 	                                 
       
  1536    // TCP connection for outgoing requests is not created until sending occurs
       
  1537    
       
  1538    }
       
  1539 
       
  1540 // -----------------------------------------------------------------------------
       
  1541 // CSipConnection::ReserveTLSTransportL
       
  1542 // -----------------------------------------------------------------------------
       
  1543 //
       
  1544 void CSipConnection::ReserveTLSTransportL( 
       
  1545     TSIPTransportParams& aTransportParams )
       
  1546     {
       
  1547     iSettingsList->SaveTLSTransportInfoL( aTransportParams );
       
  1548     }
       
  1549     
       
  1550 // -----------------------------------------------------------------------------
       
  1551 // CSipConnection::RemoveTransport
       
  1552 // -----------------------------------------------------------------------------
       
  1553 //				                
       
  1554 TBool CSipConnection::RemoveReservedTransport( 
       
  1555     TUint32 aTransportId, 
       
  1556     MSIPTransportRemovalObserver* aRemovalObserver,
       
  1557     TInt& aNumAsyncRemovals )
       
  1558     {
       
  1559     // First remove corresponding settings
       
  1560     if( !iSettingsList->RemoveSettings( aTransportId ) )
       
  1561         {
       
  1562         return EFalse;
       
  1563         }
       
  1564     
       
  1565     // If some settings were removed there is also transports which has to 
       
  1566     // be removed
       
  1567    	CTransportBase* listItem;
       
  1568 	iListIter.SetToFirst();
       
  1569 
       
  1570 	while ( ( listItem = iListIter++ ) != 0 )
       
  1571 		{
       
  1572 		if ( listItem->Handle( aTransportId ) )
       
  1573 			{
       
  1574 			// Remove call will remove everything inside the transport related
       
  1575 			// to the transport id and if Remove() returns ETrue, transport
       
  1576 			// can be removed completely (if asynchronous shutdown is
       
  1577 			// not required)
       
  1578 			TBool lastUser = listItem->Remove( aTransportId );
       
  1579 			
       
  1580 			if ( listItem->Shutdown( aTransportId, aRemovalObserver ) )
       
  1581 			    {
       
  1582 			    // Caller has to wait asynchronous completion of transport
       
  1583 			    // removal
       
  1584 			    aNumAsyncRemovals++;
       
  1585 			    }
       
  1586 			else if ( lastUser )
       
  1587 			    {
       
  1588 			    RemoveTransport( listItem );
       
  1589 			    }
       
  1590 			else
       
  1591 			    {
       
  1592 			    }
       
  1593 			}
       
  1594 		}
       
  1595     
       
  1596     return ETrue;
       
  1597     }
       
  1598 
       
  1599 // -----------------------------------------------------------------------------
       
  1600 // CSipConnection::Match
       
  1601 // -----------------------------------------------------------------------------
       
  1602 //
       
  1603 TBool CSipConnection::Match( const TDesC8& aAddress )
       
  1604 	{
       
  1605 	TInetAddr address;
       
  1606 	GetLocalAddress( address );
       
  1607 
       
  1608 	TBuf16< KMaxAddressLength > addressout16;
       
  1609 	TBuf8< KMaxAddressLength > addressout8;
       
  1610 	address.Output( addressout16 );
       
  1611 	addressout8.Copy( addressout16 );
       
  1612 
       
  1613 	return ( addressout8 == aAddress );
       
  1614 	}
       
  1615 
       
  1616 // -----------------------------------------------------------------------------
       
  1617 // CSipConnection::CreateTransportResourcesL
       
  1618 // -----------------------------------------------------------------------------
       
  1619 //
       
  1620 void CSipConnection::CreateTransportResourcesL()
       
  1621 	{
       
  1622 	__SIP_LOG( "CreateTransportResourcesL" )
       
  1623 	//Resources must not exist already
       
  1624 	__SIP_ASSERT_LEAVE( !iIcmpReceiver, KErrAlreadyExists );
       
  1625 	__SIP_ASSERT_LEAVE( !iServerResolver, KErrAlreadyExists );
       
  1626 	__SIP_ASSERT_LEAVE( !iLocalAddrResolver, KErrAlreadyExists );
       
  1627 
       
  1628 	iIcmpReceiver = CIcmpReceiver::NewL( *this, iServer );
       
  1629 	iServerResolver = CSIPServerResolver::NewL( iServer, Connection() );
       
  1630 	iLocalAddrResolver = 
       
  1631 	    CLocalAddrResolver::NewL( iServer, Connection());
       
  1632 	iRequestQueue = COutgoingRequestQueue::NewL( *this, 
       
  1633 	                                             iNATTraversal, 
       
  1634 	                                             Connection(), 
       
  1635 	                                             iIapId );
       
  1636 	}
       
  1637 
       
  1638 // -----------------------------------------------------------------------------
       
  1639 // CSipConnection::CreateDefaultTransportsL
       
  1640 // -----------------------------------------------------------------------------
       
  1641 //
       
  1642 void CSipConnection::CreateDefaultTransportsL()
       
  1643     {
       
  1644     TSIPTransportParams defaultParams( KDefaultOwnerId, iIapId );
       
  1645     if ( !IsListened( KProtocolInetTcp, KDefaultSipPort ) )
       
  1646         {
       
  1647         AddTcpListenerL( defaultParams, KDefaultSipPort );
       
  1648         }
       
  1649     if ( !IsListened( KProtocolInetUdp, KDefaultSipPort ) )
       
  1650         {
       
  1651         AddUdpListenerL( defaultParams, KDefaultSipPort );
       
  1652         }
       
  1653     }
       
  1654 
       
  1655 // -----------------------------------------------------------------------------
       
  1656 // CSipConnection::ReleaseTransportResources
       
  1657 // -----------------------------------------------------------------------------
       
  1658 //
       
  1659 void CSipConnection::ReleaseTransportResources( TBool aReleaseAllResources )
       
  1660 	{
       
  1661 	__SIP_LOG( "ReleaseTransportResources" )
       
  1662 
       
  1663 	delete iIcmpReceiver;
       
  1664 	iIcmpReceiver = NULL;
       
  1665 	RemoveAllTransports();
       
  1666 	RemoveAllTcpSender();
       
  1667 	delete iServerResolver;
       
  1668 	iServerResolver = NULL;
       
  1669 	delete iLocalAddrResolver;
       
  1670 	iLocalAddrResolver = NULL;
       
  1671 	delete iRequestQueue;
       
  1672 	iRequestQueue = NULL;
       
  1673 
       
  1674     if ( aReleaseAllResources )
       
  1675         {
       
  1676 #if ( !defined( __WINS__ ) && !defined( __WINSCW__ ) )
       
  1677 	    // In HW RConnection must be closed when network coverage is lost	
       
  1678    	    CloseConnection();      
       
  1679 #endif
       
  1680         }
       
  1681 	}
       
  1682 
       
  1683 // -----------------------------------------------------------------------------
       
  1684 // CSipConnection::DoTransportResourcesExist
       
  1685 // -----------------------------------------------------------------------------
       
  1686 //
       
  1687 TBool CSipConnection::DoTransportResourcesExist() const
       
  1688 	{
       
  1689 	return ( iIcmpReceiver != 0 &&
       
  1690 			 iServerResolver != 0 &&
       
  1691 			 iLocalAddrResolver != 0 &&
       
  1692 			 iRequestQueue != 0 );
       
  1693 	}
       
  1694 
       
  1695 // -----------------------------------------------------------------------------
       
  1696 // CSipConnection::OpenConnection
       
  1697 // -----------------------------------------------------------------------------
       
  1698 //
       
  1699 TInt CSipConnection::OpenConnection()
       
  1700 	{
       
  1701 	return iConnection->OpenConnection( iIapId, iServer );
       
  1702 	}
       
  1703 
       
  1704 // -----------------------------------------------------------------------------
       
  1705 // CSipConnection::CloseConnection
       
  1706 // -----------------------------------------------------------------------------
       
  1707 //
       
  1708 void CSipConnection::CloseConnection()
       
  1709 	{
       
  1710 	if ( iConnection )
       
  1711 		{
       
  1712 		iConnection->CloseConnection();
       
  1713 		}
       
  1714 	}
       
  1715 
       
  1716 // -----------------------------------------------------------------------------
       
  1717 // CSipConnection::ResolveLocalAddress
       
  1718 // -----------------------------------------------------------------------------
       
  1719 //
       
  1720 void CSipConnection::ResolveLocalAddress( TInetAddr& aAddress )
       
  1721 	{
       
  1722 	GetLocalAddress( aAddress );
       
  1723 	}
       
  1724 
       
  1725 // -----------------------------------------------------------------------------
       
  1726 // CSipConnection::StartMonitoringL
       
  1727 // -----------------------------------------------------------------------------
       
  1728 //
       
  1729 void CSipConnection::StartMonitoringL( TInt aError )
       
  1730 	{
       
  1731 #ifdef CPPUNIT_TEST
       
  1732 	aError; // silence warning
       
  1733 #else
       
  1734 	// In unit testing hard coded IAPs cannot be expected 
       
  1735 	// to be valid in all environments. 
       
  1736 	// Not trying to load monitor plug-in by IAP in unit tests 
       
  1737     iNetworkManager.RegisterObserverL( *iStateModel, iIapId, aError );
       
  1738 #endif
       
  1739 	}
       
  1740 
       
  1741 // -----------------------------------------------------------------------------
       
  1742 // CSipConnection::StopMonitoring
       
  1743 // -----------------------------------------------------------------------------
       
  1744 //
       
  1745 void CSipConnection::StopMonitoring()
       
  1746 	{
       
  1747 	iNetworkManager.Deregister( *iStateModel );
       
  1748 	}
       
  1749 	
       
  1750 // -----------------------------------------------------------------------------
       
  1751 // CSipConnection::ContinueMonitoring
       
  1752 // -----------------------------------------------------------------------------
       
  1753 //
       
  1754 TBool CSipConnection::ContinueMonitoring( TInt aError )
       
  1755 	{
       
  1756 	return iNetworkManager.ContinueMonitoring( *iStateModel, aError );
       
  1757 	}
       
  1758 	
       
  1759 // -----------------------------------------------------------------------------
       
  1760 // CSipConnection::MonitorConnectionClosure
       
  1761 // -----------------------------------------------------------------------------
       
  1762 //	
       
  1763 TInt CSipConnection::MonitorConnectionClosure()
       
  1764     {
       
  1765     return iConnection->MonitorConnectionClosure( iServer );
       
  1766     }	
       
  1767 
       
  1768 // -----------------------------------------------------------------------------
       
  1769 // CSipConnection::StateModelChanged
       
  1770 // -----------------------------------------------------------------------------
       
  1771 //
       
  1772 void CSipConnection::StateModelChanged( CStateModel::TState aState )
       
  1773 	{
       
  1774 	CSIPConnection::TState oldState = iState;
       
  1775 	SetState( aState );
       
  1776 	if( oldState != iState )
       
  1777 		{
       
  1778 		TRAP_IGNORE( 
       
  1779 		    iTUNetStatusObserver.ConnectionStateChangedL( iIapId, iState ) )
       
  1780 		}
       
  1781 	}
       
  1782 	
       
  1783 // -----------------------------------------------------------------------------
       
  1784 // CSipConnection::Destroy
       
  1785 // -----------------------------------------------------------------------------
       
  1786 //	
       
  1787 void CSipConnection::Destroy()
       
  1788 	{
       
  1789 	iOwner.Destroy( iIapId );
       
  1790 	}
       
  1791 
       
  1792 // -----------------------------------------------------------------------------
       
  1793 // CSipConnection::DetermineConnectionTypeL
       
  1794 // -----------------------------------------------------------------------------
       
  1795 //
       
  1796 TInt CSipConnection::DetermineConnectionTypeL()
       
  1797 	{
       
  1798 	TInt bearer = KErrNotFound;
       
  1799   CMDBSession* cdb = CMDBSession::NewLC( KCDVersion1_1 ); 
       
  1800   cdb->SetAttributeMask( ECDHidden );
       
  1801   // Create an iap record
       
  1802   CCDIAPRecord* iapRecord = static_cast<CCDIAPRecord*>( 
       
  1803               CCDRecordBase::RecordFactoryL( KCDTIdIAPRecord ) );
       
  1804   CleanupStack::PushL( iapRecord );
       
  1805   iapRecord->SetRecordId( IapId() );
       
  1806   iapRecord->LoadL( *cdb );
       
  1807   TPtrC bearerType(iapRecord->iBearerType);
       
  1808   if(bearerType.CompareF(TPtrC(KCDTypeNameLANBearer)) == 0)
       
  1809 	bearer =  MSIPConnectionContext::ELanBearer;
       
  1810   else if(bearerType.CompareF(TPtrC(KCDTypeNameModemBearer)) == 0)
       
  1811 	bearer =  MSIPConnectionContext::EModemBearer;
       
  1812   CleanupStack::PopAndDestroy( iapRecord );
       
  1813   CleanupStack::PopAndDestroy( cdb );
       
  1814   return bearer;
       
  1815 	}
       
  1816 
       
  1817 
       
  1818 
       
  1819 // -----------------------------------------------------------------------------
       
  1820 // CSipConnection::State
       
  1821 // -----------------------------------------------------------------------------
       
  1822 //	
       
  1823 CSIPConnection::TState CSipConnection::State()
       
  1824 	{
       
  1825 	return iState;
       
  1826 	}
       
  1827 
       
  1828 // -----------------------------------------------------------------------------
       
  1829 // CSipConnection::SystemStarted
       
  1830 // -----------------------------------------------------------------------------
       
  1831 //	
       
  1832 void CSipConnection::SystemStarted()
       
  1833     {
       
  1834     iStateModel->EnterState( MStateModel::EInit, KErrNone );
       
  1835     }
       
  1836 
       
  1837 // -----------------------------------------------------------------------------
       
  1838 // CSipConnection::CellularUsageAllowed
       
  1839 // -----------------------------------------------------------------------------
       
  1840 //	
       
  1841 
       
  1842 void CSipConnection::CellularUsageAllowed()
       
  1843 {
       
  1844 	if(iWaitingForDataUsage)
       
  1845 	{
       
  1846 	iWaitingForDataUsage=EFalse;
       
  1847 	__SIP_LOG( "CSipConnection::CellularUsageAllowed Execution" )
       
  1848     iStateModel->EnterState( MStateModel::EInit, KErrNone );
       
  1849 	}
       
  1850 }
       
  1851 
       
  1852 
       
  1853 // -----------------------------------------------------------------------------
       
  1854 // CSipConnection::TopViaHeader
       
  1855 // -----------------------------------------------------------------------------
       
  1856 //
       
  1857 CSIPViaHeader* CSipConnection::TopViaHeader( CSIPMessage* aMessage )
       
  1858 	{
       
  1859 	CSIPViaHeader* via = NULL;
       
  1860 	if( aMessage->HasHeader( SIPStrings::StringF(SipStrConsts::EViaHeader) ) )
       
  1861 		{
       
  1862 		TSglQueIter< CSIPHeaderBase > iter =
       
  1863 			aMessage->Headers( SIPStrings::StringF(SipStrConsts::EViaHeader) );
       
  1864 		CSIPHeaderBase* header = iter;
       
  1865 		via = static_cast< CSIPViaHeader* >( header );
       
  1866 		}
       
  1867 	return via;
       
  1868 	}
       
  1869 
       
  1870 // -----------------------------------------------------------------------------
       
  1871 // CSipConnection::SetState
       
  1872 // -----------------------------------------------------------------------------
       
  1873 //	
       
  1874 void CSipConnection::SetState ( MStateModel::TState aState )
       
  1875 	{
       
  1876 	switch( aState )
       
  1877 		{
       
  1878 		case MStateModel::EInit:
       
  1879 			{
       
  1880 			iState = CSIPConnection::EInit;
       
  1881 			break;
       
  1882 			}
       
  1883 		case MStateModel::EInactiveMonitoring:
       
  1884 		case MStateModel::EInactiveConnecting:
       
  1885 			{
       
  1886 			iState = CSIPConnection::EInactive;
       
  1887 			break;
       
  1888 			}
       
  1889 		case MStateModel::EActiveResolving:
       
  1890 			{
       
  1891 			// Does not yet change the current SIP connection state.
       
  1892 			// The SIP connection state changes with the next state transition.
       
  1893 			break;	
       
  1894 			}			
       
  1895 		case MStateModel::EActiveResolved:
       
  1896 			{
       
  1897 			iState = CSIPConnection::EActive;
       
  1898 			break;
       
  1899 			}
       
  1900 		case MStateModel::ESuspended:
       
  1901 			{
       
  1902 			iState = CSIPConnection::ESuspended;
       
  1903 			break;
       
  1904 			}
       
  1905 		case MStateModel::EUnavailable:
       
  1906 		default:
       
  1907 			{
       
  1908 			iState = CSIPConnection::EUnavailable;
       
  1909 			break;
       
  1910 			}
       
  1911 		}	
       
  1912 	}
       
  1913 	
       
  1914 // -----------------------------------------------------------------------------
       
  1915 // CSipConnection::Connection
       
  1916 // -----------------------------------------------------------------------------
       
  1917 //
       
  1918 RConnection& CSipConnection::Connection()
       
  1919 	{
       
  1920 	return iConnection->Connection();
       
  1921 	}	
       
  1922 
       
  1923 // -----------------------------------------------------------------------------
       
  1924 // CSipConnection::NotifyIcmpErrorL
       
  1925 // -----------------------------------------------------------------------------
       
  1926 //	
       
  1927 void CSipConnection::NotifyIcmpErrorL( const TInetAddr& aAddress )
       
  1928     {
       
  1929     CSipConnectionMgr::TICMPError error = CSipConnectionMgr::ENetUnreachable;
       
  1930 	iFinder.IcmpErrorL( aAddress, error );
       
  1931 	CTransportBase* listItem;
       
  1932 	iListIter.SetToFirst();
       
  1933 	while ( ( listItem = iListIter++ ) != 0 )
       
  1934 		{
       
  1935 		listItem->ICMPErrorL( error, aAddress );
       
  1936 		}
       
  1937     }
       
  1938 
       
  1939 // -----------------------------------------------------------------------------
       
  1940 // CSipConnection::IsConnectionActive
       
  1941 // -----------------------------------------------------------------------------
       
  1942 //   
       
  1943 TBool CSipConnection::IsConnectionActive() const
       
  1944     {
       
  1945     return ( iState == CSIPConnection::EActive );
       
  1946     }
       
  1947 
       
  1948 // -----------------------------------------------------------------------------
       
  1949 // CSipConnection::UpdateURIL
       
  1950 // -----------------------------------------------------------------------------
       
  1951 // 
       
  1952 void CSipConnection::UpdateURIL( 
       
  1953     CURIContainer& aURI, 
       
  1954     const TInetAddr& aAddress,
       
  1955     TUint aProtocol ) const
       
  1956     {
       
  1957     __ASSERT_ALWAYS( aURI.IsSIPURI(), User::Leave( KErrSIPInvalidURIType ) );
       
  1958 	
       
  1959 	CSIPURI& uri = *aURI.SIPURI();
       
  1960 	
       
  1961 	RStringF transport;
       
  1962     if ( aProtocol == KProtocolInetTcp )
       
  1963         {
       
  1964         transport = SIPStrings::StringF( SipStrConsts::ETCP );
       
  1965         }
       
  1966     else if ( aProtocol == KProtocolTls )
       
  1967         {
       
  1968         transport = SIPStrings::StringF( SipStrConsts::ETLS );
       
  1969         }
       
  1970     else
       
  1971         {
       
  1972         User::Leave( KErrArgument );
       
  1973         }
       
  1974 	
       
  1975     uri.SetParamL( SIPStrings::StringF( SipStrConsts::ETransport ), transport );
       
  1976 
       
  1977     uri.HostPort().SetPort( aAddress.Port() );
       
  1978     
       
  1979     HBufC8* addressText = CTransport::ConvertInetAddrToTextL( aAddress );
       
  1980     CleanupStack::PushL( addressText );
       
  1981     uri.HostPort().SetHostL( *addressText );
       
  1982     CleanupStack::PopAndDestroy( addressText );
       
  1983     }
       
  1984     
       
  1985 // -----------------------------------------------------------------------------
       
  1986 // CSipConnection::CSipConnection
       
  1987 // -----------------------------------------------------------------------------
       
  1988 //
       
  1989 CSipConnection::CSipConnection( MTransactionFinder& aFinder,
       
  1990 								MServerTaFactory& aTaFactory,
       
  1991 								MTimerManager& aTimer,
       
  1992 								TUint aTOne,
       
  1993 								const TSIPTransportParams& aParams,
       
  1994 								RSocketServ& aServer,
       
  1995 								MSigCompController& aSigCompHandler,
       
  1996 								MConnectionStateObserver& aTUNetStatusObserver,
       
  1997 								CNetworkManager& aNetworkManager,
       
  1998 								MSipConnectionOwner& aOwner,
       
  1999 								CNetworkInfo& aNetworkInfo,
       
  2000 								CSIPNATTraversalController& aNATTraversal )
       
  2001 	: iFinder( aFinder ),
       
  2002 	  iTaFactory( aTaFactory ),
       
  2003 	  iTUNetStatusObserver( aTUNetStatusObserver ),
       
  2004 	  iSigCompHandler( aSigCompHandler ),
       
  2005 	  iServer( aServer ),
       
  2006 	  iList( CTransportBase::iOffset ),
       
  2007 	  iListIter( iList ),
       
  2008 	  iTcpSenderList( CTcpResponseSender::iSenderOffset ),
       
  2009 	  iTcpSenderListIter( iTcpSenderList ),
       
  2010 	  iTOne( aTOne ),
       
  2011 	  iNetworkManager( aNetworkManager ),
       
  2012 	  iTimer( aTimer ),
       
  2013 	  iOwner( aOwner ),
       
  2014 	  iIapId( aParams.IapId() ),
       
  2015 	  iState( CSIPConnection::EInit ),
       
  2016 	  iNetworkInfo( aNetworkInfo ),
       
  2017 	  iNATTraversal( aNATTraversal )
       
  2018 	{
       
  2019 	}
       
  2020 
       
  2021 // -----------------------------------------------------------------------------
       
  2022 // CSipConnection::~CSipConnection
       
  2023 // -----------------------------------------------------------------------------
       
  2024 //
       
  2025 CSipConnection::~CSipConnection()
       
  2026 	{	
       
  2027 	delete iSettingsList;
       
  2028 	iNetworkManager.Deregister( *iStateModel );
       
  2029 	delete iStateModel;
       
  2030      // Use scope operator for keeping PC-Lint happy
       
  2031 	CSipConnection::ReleaseTransportResources( ETrue );
       
  2032 	delete iConnection;	
       
  2033 	}
       
  2034 
       
  2035 // -----------------------------------------------------------------------------
       
  2036 // CSipConnection::ConstructL
       
  2037 // -----------------------------------------------------------------------------
       
  2038 //
       
  2039 void CSipConnection::ConstructL()
       
  2040 	{	
       
  2041 	iWaitingForDataUsage = EFalse;
       
  2042 	iSettingsList = COwnerSettingsList::NewL();	
       
  2043 	iStateModel = CStateModel::NewL( *this, iTimer );
       
  2044 	iConnection = CConnectionContainer::NewL( *iStateModel );	
       
  2045     if ( iOwner.IsSystemStarted())
       
  2046 		{ 
       
  2047 		if ((ConnectionType() == MSIPConnectionContext::EModemBearer) && (!iOwner.IsCellularDataUsageAllowed()) )
       
  2048 			{
       
  2049 			iWaitingForDataUsage = ETrue;
       
  2050 			__SIP_LOG( "CSipConnection::ConstructL() is blocking Connection Start Up..." )
       
  2051 			return;
       
  2052 			}
       
  2053 		else
       
  2054 			{
       
  2055 			__SIP_LOG( "CSipConnection::ConstructL() is Allowing Connection Start Up..." )
       
  2056 	        iStateModel->EnterState( MStateModel::EInit, KErrNone );
       
  2057 			}
       
  2058        	}
       
  2059 	}
       
  2060 
       
  2061 // -----------------------------------------------------------------------------
       
  2062 // CSipConnection::ConnectionError
       
  2063 // -----------------------------------------------------------------------------
       
  2064 //
       
  2065 TInt CSipConnection::ConnectionError()
       
  2066 	{
       
  2067 	return iStateModel->Error();
       
  2068 	}
       
  2069 
       
  2070 // -----------------------------------------------------------------------------
       
  2071 // CSipConnection::ConnectionType()
       
  2072 // -----------------------------------------------------------------------------
       
  2073 //
       
  2074 TInt CSipConnection::ConnectionType()
       
  2075 	{
       
  2076 	TInt type = MSIPConnectionContext::EModemBearer;
       
  2077 	TRAP_IGNORE( type = DetermineConnectionTypeL( ) )
       
  2078 	return type;
       
  2079 	}
       
  2080