natfw/natfwconnectionmultiplexer/src/cncmsession.cpp
changeset 0 1bce908db942
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     1 /*
       
     2 * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:    Connection multiplexer session abstraction.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 #include <e32math.h> 
       
    22 #include <es_sock.h>
       
    23 #include <es_sock_partner.h>
       
    24 
       
    25 #include "cncmsession.h"
       
    26 #include "cncmstream.h"
       
    27 #include "cncmconnection.h"
       
    28 #include "cncmconnectionobserverhandler.h"
       
    29 #include "cncmlocaladdressresolver.h"
       
    30 #include "ncmconnectionmultiplexerlogs.h"
       
    31 #include "cncmportstore.h"
       
    32 #include "mncmsessionobserver.h"
       
    33 #include "mncmconnectionmultiplexerobserver.h"
       
    34 #include "natfwmediawrapper.h"
       
    35 #include "cncmicmpv4receiver.h"
       
    36 #include "cncmicmpv6receiver.h"
       
    37 
       
    38 const TUint KESockMessageSlots = 24;
       
    39 
       
    40 // ---------------------------------------------------------------------------
       
    41 // CNcmSessionSession::CNcmSession
       
    42 // ---------------------------------------------------------------------------
       
    43 //
       
    44 CNcmSession::CNcmSession( TUint aSessionId,
       
    45     TUint32 aIapId,
       
    46     MNcmConnectionMultiplexerObserver& aObserver,
       
    47     MNcmSessionObserver& aSessionObserver ) :
       
    48     CActive( EPriorityStandard ),
       
    49     iSessionId( aSessionId ), iIapId( aIapId ), iObserver( aObserver ),
       
    50     iSessionObserver( aSessionObserver )
       
    51     {
       
    52     CActiveScheduler::Add( this );
       
    53     }
       
    54 
       
    55 
       
    56 // ---------------------------------------------------------------------------
       
    57 // CNcmSession::ConstructL
       
    58 // ---------------------------------------------------------------------------
       
    59 //
       
    60 void CNcmSession::ConstructL( TUint aPortRangeStart, TUint aPortRangeStop )
       
    61     {
       
    62     iPortStore = CNcmPortStore::NewL( aPortRangeStart, aPortRangeStop );
       
    63 
       
    64     User::LeaveIfError( iSocketServ.Connect( KESockMessageSlots ) );
       
    65 
       
    66     iConnPref.SetDialogPreference( ECommDbDialogPrefDoNotPrompt );
       
    67     iConnPref.SetIapId( iIapId );  
       
    68                                                  
       
    69     User::LeaveIfError( iConnection.Open( iSocketServ ) );
       
    70     
       
    71     TSecurityPolicy policy( ECapabilityNetworkServices );
       
    72     TSecurityPolicyBuf securityBuf(policy);
       
    73     User::LeaveIfError( iConnection.Control( 
       
    74         KCOLConnection, KCoEnableCloneOpen, securityBuf ) );
       
    75 
       
    76     iConnection.Start( iConnPref, iStatus );
       
    77     SetActive();                                  
       
    78     }
       
    79 
       
    80 
       
    81 // ---------------------------------------------------------------------------
       
    82 // CNcmSession::NewL
       
    83 // ---------------------------------------------------------------------------
       
    84 //
       
    85 CNcmSession* CNcmSession::NewL( TUint aSessionId,
       
    86     TUint32 aIapId, TUint aPortRangeStart, TUint aPortRangeStop,
       
    87     MNcmConnectionMultiplexerObserver& aObserver,
       
    88     MNcmSessionObserver& aSessionObserver )
       
    89     {   
       
    90     CNcmSession* self =
       
    91         CNcmSession::NewLC( aSessionId, aIapId, aPortRangeStart,
       
    92             aPortRangeStop, aObserver, aSessionObserver );
       
    93             
       
    94     CleanupStack::Pop( self );
       
    95     return self;
       
    96     }
       
    97 
       
    98 
       
    99 // ---------------------------------------------------------------------------
       
   100 // CNcmSession::NewLC
       
   101 // ---------------------------------------------------------------------------
       
   102 //
       
   103 CNcmSession* CNcmSession::NewLC( TUint aSessionId,
       
   104     TUint32 aIapId, TUint aPortRangeStart, TUint aPortRangeStop,
       
   105     MNcmConnectionMultiplexerObserver& aObserver,
       
   106     MNcmSessionObserver& aSessionObserver )
       
   107     {   
       
   108     CNcmSession* self =
       
   109         new( ELeave ) CNcmSession( aSessionId, aIapId,
       
   110             aObserver, aSessionObserver );
       
   111                     
       
   112     CleanupStack::PushL( self );
       
   113     self->ConstructL( aPortRangeStart, aPortRangeStop );
       
   114     return self;
       
   115     }
       
   116 
       
   117 
       
   118 // ---------------------------------------------------------------------------
       
   119 // CNcmSession::~CNcmSession
       
   120 // ---------------------------------------------------------------------------
       
   121 //
       
   122 CNcmSession::~CNcmSession()
       
   123     {
       
   124     delete iIcmpV4receiver;
       
   125     delete iIcmpV6receiver;
       
   126     delete iPortStore;
       
   127     
       
   128     TInt ind( iStreams.Count() );
       
   129     
       
   130     while ( ind-- )
       
   131         {
       
   132         TRAP_IGNORE( RemoveStreamL( iStreams[ind]->StreamId() ) )
       
   133         }   
       
   134     
       
   135     iConnection.Close();    
       
   136     Cancel();
       
   137 
       
   138     iSocketServ.Close();
       
   139     iStreams.Close();
       
   140     }
       
   141 
       
   142 
       
   143 // ---------------------------------------------------------------------------
       
   144 // CNcmSession::CreateStreamL
       
   145 // ---------------------------------------------------------------------------
       
   146 //
       
   147 TUint CNcmSession::CreateStreamL( TInt aQos,
       
   148     TUint aProtocol )
       
   149     {
       
   150     TUint streamId( GenerateStreamId() );
       
   151 
       
   152     CNcmStream* item = CNcmStream::NewLC(
       
   153         iSessionId, streamId, aQos, aProtocol );
       
   154 
       
   155     iStreams.AppendL( item ); 
       
   156     CleanupStack::Pop( item );
       
   157     
       
   158     return item->StreamId();
       
   159     }
       
   160 
       
   161 
       
   162 // ---------------------------------------------------------------------------
       
   163 // CNcmSession::RemoveStreamL
       
   164 // ---------------------------------------------------------------------------
       
   165 //
       
   166 void CNcmSession::RemoveStreamL( TUint aStreamId )
       
   167     {    
       
   168     TUint streamIndex( StreamIndexL( aStreamId ) );
       
   169     
       
   170     delete iStreams[streamIndex];
       
   171     iStreams.Remove( streamIndex );
       
   172     }
       
   173     
       
   174 
       
   175 // ---------------------------------------------------------------------------
       
   176 // CNcmSession::ConnectionId
       
   177 // ---------------------------------------------------------------------------
       
   178 //
       
   179 TUint32 CNcmSession::IapId() const
       
   180     {
       
   181     return iIapId;
       
   182     }
       
   183 
       
   184     
       
   185 // ---------------------------------------------------------------------------
       
   186 // CNcmSession::ConnectionId
       
   187 // ---------------------------------------------------------------------------
       
   188 //
       
   189 TUint CNcmSession::SessionId() const
       
   190     {
       
   191     return iSessionId;
       
   192     }
       
   193 
       
   194 
       
   195 // ---------------------------------------------------------------------------
       
   196 // From class CActive
       
   197 //
       
   198 // CNcmSession::RunL
       
   199 // ---------------------------------------------------------------------------
       
   200 //
       
   201 void CNcmSession::RunL()
       
   202     {
       
   203     __CONNECTIONMULTIPLEXER_INT1(
       
   204         "CNcmSession::RunL - iStatus: ", iStatus.Int() )
       
   205                    
       
   206     if ( iStatus != KErrNone )
       
   207         {
       
   208         iObserver.Notify( iSessionId, 0,
       
   209             MNcmConnectionMultiplexerObserver::ESessionCreated,
       
   210             iStatus.Int() );
       
   211             
       
   212         iSessionObserver.SessionCreationFailed( iSessionId );
       
   213         }
       
   214     else
       
   215         {
       
   216         iInitialized = ETrue;
       
   217 
       
   218         CNcmLocalAddressResolver* localAddressResolver(
       
   219             CNcmLocalAddressResolver::NewLC( iSocketServ ) );
       
   220             
       
   221         localAddressResolver->GetLocalAddrL( iIpv4Addr, iIpv6Addr, iIapId );
       
   222         CleanupStack::PopAndDestroy( localAddressResolver );
       
   223         
       
   224         if ( !iIpv4Addr.IsUnspecified() )
       
   225             {
       
   226             iIcmpV4receiver =
       
   227                 CNcmIcmpV4Receiver::NewL( iSocketServ, iConnection, *this );
       
   228             }
       
   229 
       
   230         if ( !iIpv6Addr.IsUnspecified() )
       
   231             {
       
   232             iIcmpV6receiver =
       
   233                 CNcmIcmpV6Receiver::NewL( iSocketServ,iConnection, *this );
       
   234             }      
       
   235                 
       
   236         iObserver.Notify( iSessionId, 0,
       
   237             MNcmConnectionMultiplexerObserver::ESessionCreated,
       
   238             iStatus.Int() );
       
   239         }
       
   240     }
       
   241 
       
   242 
       
   243 // ---------------------------------------------------------------------------
       
   244 // From class CActive
       
   245 //
       
   246 // DoCancel
       
   247 // ---------------------------------------------------------------------------
       
   248 //
       
   249 void CNcmSession::DoCancel()
       
   250     {    
       
   251     // RConnection doesn't cancel an outstanding request at Close()
       
   252     // so we'll have to it "manually" here
       
   253     if( iStatus.Int() == KRequestPending )
       
   254         {
       
   255         TRequestStatus* status = &iStatus;
       
   256         User::RequestComplete( status, KErrCancel );
       
   257         }
       
   258     }
       
   259 
       
   260 
       
   261 // -----------------------------------------------------------------------------
       
   262 // From class CActive
       
   263 //
       
   264 // CNcmSession::RunError
       
   265 // -----------------------------------------------------------------------------
       
   266 //
       
   267 TInt CNcmSession::RunError( TInt aError )
       
   268     {
       
   269     __CONNECTIONMULTIPLEXER_INT1(
       
   270         "CNcmSession::RunError aError: ", aError )
       
   271   
       
   272     iObserver.Notify( iSessionId, 0,
       
   273         MNcmConnectionMultiplexerObserver::ESessionCreated,
       
   274         aError );
       
   275         
       
   276     iSessionObserver.SessionCreationFailed( iSessionId );
       
   277 
       
   278     aError = aError;
       
   279     return KErrNone;
       
   280     }
       
   281     
       
   282         
       
   283 // ---------------------------------------------------------------------------
       
   284 // CNcmSession::StreamByIdL
       
   285 // ---------------------------------------------------------------------------
       
   286 //
       
   287 CNcmStream* CNcmSession::StreamByIdL(
       
   288     TUint aStreamId )
       
   289     {
       
   290     TInt ind( iStreams.Count() );
       
   291     
       
   292     while ( ind-- )
       
   293         {
       
   294         if ( iStreams[ind]->StreamId() == aStreamId )
       
   295             {
       
   296             return iStreams[ind];
       
   297             }
       
   298         }
       
   299     
       
   300     return NULL;
       
   301     }
       
   302 
       
   303     
       
   304 // ---------------------------------------------------------------------------
       
   305 // CNcmSession::StreamIndexL
       
   306 // ---------------------------------------------------------------------------
       
   307 //
       
   308 TUint CNcmSession::StreamIndexL( TUint aStreamId )
       
   309     {   
       
   310     TInt streamCount( iStreams.Count() );
       
   311     
       
   312     for ( TInt i = 0; i < streamCount; i++ )
       
   313         {
       
   314         if ( iStreams[i]->StreamId() == aStreamId )
       
   315             {
       
   316             return i;
       
   317             }
       
   318         }
       
   319     
       
   320     User::Leave( KErrNotFound );
       
   321     return KErrNone; // To remove a compiler warning
       
   322     }
       
   323 
       
   324     
       
   325 // ---------------------------------------------------------------------------
       
   326 // CNcmSession::SocketServer
       
   327 // ---------------------------------------------------------------------------
       
   328 //
       
   329 RSocketServ& CNcmSession::SocketServer()
       
   330     {
       
   331     return iSocketServ;
       
   332     }
       
   333 
       
   334 
       
   335 // ---------------------------------------------------------------------------
       
   336 // CNcmSession::Connection
       
   337 // ---------------------------------------------------------------------------
       
   338 //
       
   339 RConnection& CNcmSession::Connection()
       
   340     {  
       
   341     return iConnection;
       
   342     }
       
   343 
       
   344     
       
   345 // ---------------------------------------------------------------------------
       
   346 // CNcmSession::GetConnectionName
       
   347 // ---------------------------------------------------------------------------
       
   348 //
       
   349 TInt CNcmSession::GetConnectionName( TName& aConnectionName )
       
   350     {  
       
   351     return iConnection.Name( aConnectionName );
       
   352     }
       
   353 
       
   354 
       
   355 // -----------------------------------------------------------------------------
       
   356 // CNcmSession::GenerateStreamId
       
   357 // -----------------------------------------------------------------------------
       
   358 //
       
   359 TUint CNcmSession::GenerateStreamId() const
       
   360     {
       
   361     return ( static_cast<TUint>( Math::Random() ) );
       
   362     }
       
   363 
       
   364    
       
   365 // ---------------------------------------------------------------------------
       
   366 // CNcmSession::PortStore
       
   367 // ---------------------------------------------------------------------------
       
   368 //
       
   369 CNcmPortStore& CNcmSession::PortStore() const
       
   370     {
       
   371     __CONNECTIONMULTIPLEXER( "CNcmSession::PortStore" )
       
   372 
       
   373     return *iPortStore;
       
   374     }
       
   375 
       
   376 
       
   377 // ---------------------------------------------------------------------------
       
   378 // From class MNcmIcmpObserver
       
   379 //
       
   380 // Called as a result for ICMP error received 
       
   381 // ---------------------------------------------------------------------------
       
   382 //        
       
   383 void CNcmSession::IcmpError( const TInetAddr& aAddress,
       
   384     TInetAddr& aLocalAddress, TInetAddr& aRemoteAddress )
       
   385     {
       
   386     TRAP_IGNORE( this->HandleIcmpErrorL( aAddress, aLocalAddress,
       
   387         aRemoteAddress ) )
       
   388     }
       
   389     
       
   390 // ---------------------------------------------------------------------------
       
   391 // CNcmSession::HandleIcmpErrorL
       
   392 // ---------------------------------------------------------------------------
       
   393 void CNcmSession::HandleIcmpErrorL( const TInetAddr& aAddress,
       
   394     TInetAddr& aLocalAddress, TInetAddr& aRemoteAddress )
       
   395     {  
       
   396     __CONNECTIONMULTIPLEXER( "CNcmSession::HandleIcmpErrorL" )
       
   397     
       
   398     TInt ind( iStreams.Count() );
       
   399     TUint mediaConnectionId( 0 );
       
   400     
       
   401     while ( ind-- )
       
   402         { 
       
   403         mediaConnectionId = iStreams[ind]->MediaConnectionId();
       
   404 
       
   405         if ( iStreams[ind]->ConnectionL( mediaConnectionId )->
       
   406             CheckSenderValidityToSending( aAddress ) )
       
   407             {
       
   408             __CONNECTIONMULTIPLEXER(
       
   409                 "CNcmSession::HandleIcmpErrorL - ICMP ERROR VALID" )
       
   410             
       
   411             iStreams[ind]->WrapperL()->GetAddresses(
       
   412                 aLocalAddress, aRemoteAddress );
       
   413             return;
       
   414             }
       
   415         }
       
   416         
       
   417     __CONNECTIONMULTIPLEXER(
       
   418         "CNcmSession::HandleIcmpErrorL - ICMP ERROR NOT VALID" )
       
   419     User::Leave( KErrNotFound );
       
   420     }
       
   421 
       
   422 
       
   423 // ---------------------------------------------------------------------------
       
   424 // CNcmSession::Initialized
       
   425 // ---------------------------------------------------------------------------
       
   426 //
       
   427 TBool CNcmSession::Initialized() const
       
   428     {
       
   429     return iInitialized;
       
   430     }
       
   431 
       
   432     
       
   433 // ---------------------------------------------------------------------------
       
   434 // CNcmSession::ResolveDestinationAddressL
       
   435 // ---------------------------------------------------------------------------
       
   436 //
       
   437 void CNcmSession::ResolveDestinationAddressL( const TDesC8& aAddress,
       
   438     TUint aPort, TInetAddr& aResult )
       
   439     {
       
   440     __CONNECTIONMULTIPLEXER_STR(
       
   441         "CNcmSession::ResolveDestinationAddressL, FQDN", aAddress )
       
   442 
       
   443     HBufC* addrBuf = HBufC::NewLC( aAddress.Length() );
       
   444     TPtr addrPtr( addrBuf->Des() );
       
   445     User::LeaveIfError(
       
   446         CnvUtfConverter::ConvertToUnicodeFromUtf8( addrPtr, aAddress ) );
       
   447 
       
   448     RHostResolver resolver;
       
   449     CleanupClosePushL( resolver );
       
   450     User::LeaveIfError( resolver.Open(
       
   451         iSocketServ, KAfInet, KProtocolInetUdp, iConnection ) );
       
   452     TNameEntry entry;
       
   453     User::LeaveIfError( resolver.GetByName( *addrBuf, entry ) );
       
   454 
       
   455     CleanupStack::PopAndDestroy( 2, addrBuf );
       
   456 
       
   457     aResult = TInetAddr::Cast( entry().iAddr );
       
   458     aResult.SetPort( aPort );
       
   459     
       
   460     __CONNECTIONMULTIPLEXER_ADDRLOG(
       
   461         "CNcmSession::ResolveDestinationAddressL, IP", aResult )
       
   462     }
       
   463  
       
   464     
       
   465 // ---------------------------------------------------------------------------
       
   466 // CNcmSession::LocalIPv4Address
       
   467 // ---------------------------------------------------------------------------
       
   468 //
       
   469 TInetAddr& CNcmSession::LocalIPv4Address()
       
   470     {
       
   471     return iIpv4Addr;
       
   472     }
       
   473 
       
   474 
       
   475 // ---------------------------------------------------------------------------
       
   476 // CNcmSession::LocalIPv6Address
       
   477 // ---------------------------------------------------------------------------
       
   478 //
       
   479 TInetAddr& CNcmSession::LocalIPv6Address()
       
   480     {
       
   481     return iIpv6Addr;
       
   482     }