natfw/natfwstunturnclient/src/cstunclientimplementation.cpp
changeset 0 1bce908db942
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     1 /*
       
     2 * Copyright (c) 2006-2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:    
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 #include "natfwunsafserverresolver.h"
       
    22 #include "natfwunsafserverresolverobserver.h"
       
    23 #include "cnatfwunsaficmpreceiver.h"
       
    24 #include "natfwunsaflog.h"
       
    25 #include "casynccallback.h"
       
    26 #include "stunassert.h"
       
    27 #include "cstunclientimplementation.h"
       
    28 #include "cstunclientresolvingtcp.h"
       
    29 #include "cstunclientresolvingtls.h"
       
    30 #include "cstunclientresolvingudp.h"
       
    31 #include "cstunclientgetsharedsecret.h"
       
    32 #include "cstunclientready.h"
       
    33 #include "cstunclientrenewsharedsecret.h"
       
    34 #include "natfwstunbinding.h"
       
    35 #include "cstunbindingimplementation.h"
       
    36 #include "ctransactionidgenerator.h"
       
    37 #include "cstunsharedsecret.h"
       
    38 #include "cstuncredentials.h"
       
    39 #include "cstuncredentials.h"
       
    40 
       
    41 _LIT8( KUdp, "udp" );
       
    42 _LIT8( KTcp, "tcp" );
       
    43 _LIT8( KTls, "tls" );
       
    44 const TUint KTlsPort = 443;
       
    45 
       
    46 // ======== MEMBER FUNCTIONS ========
       
    47 
       
    48 // ---------------------------------------------------------------------------
       
    49 // CSTUNClientImplementation::NewL
       
    50 // ---------------------------------------------------------------------------
       
    51 //
       
    52 CSTUNClientImplementation*
       
    53 CSTUNClientImplementation::NewL( CSTUNClient& aClient,
       
    54                                  TInt aRetransmitInterval,
       
    55                                  const TDesC8& aServerAddress,
       
    56                                  TUint aServerPort,
       
    57                                  const TDesC8& aServiceName,
       
    58                                  RSocketServ& aSocketServ,
       
    59                                  RConnection& aConnection,
       
    60                                  CDeltaTimer& aTimer,
       
    61                                  MSTUNClientObserver& aObserver,
       
    62                                  TBool aObtainSharedSecret,
       
    63                                  TBool aFailIfNoSRVRecordsFound,
       
    64                                  TBool aIcmpReceiverUsed,
       
    65                                  MNcmConnectionMultiplexer* aMultiplexer,
       
    66                                  TTransportProtocol aTransportProtocol )
       
    67     {
       
    68     CSTUNClientImplementation* self =
       
    69         new ( ELeave ) CSTUNClientImplementation( aClient,
       
    70                                                   aRetransmitInterval,
       
    71                                                   aServerPort,
       
    72                                                   aSocketServ,
       
    73                                                   aConnection,
       
    74                                                   aTimer,
       
    75                                                   aObserver,
       
    76                                                   aObtainSharedSecret,
       
    77                                                   aMultiplexer,
       
    78                                                   aTransportProtocol );
       
    79     CleanupStack::PushL( self );
       
    80     self->ConstructL( aServerAddress, aServiceName, 
       
    81                       aFailIfNoSRVRecordsFound, aIcmpReceiverUsed );
       
    82     CleanupStack::Pop( self );
       
    83     return self;    
       
    84     }
       
    85 
       
    86 // ---------------------------------------------------------------------------
       
    87 // CSTUNClientImplementation::NewL - overloaded for ICE conn check usage
       
    88 // ---------------------------------------------------------------------------
       
    89 //
       
    90 CSTUNClientImplementation*
       
    91 CSTUNClientImplementation::NewL( CSTUNClient& aClient,
       
    92                                  TInt aRetransmitInterval,
       
    93                                  CDeltaTimer& aTimer,
       
    94                                  MSTUNClientObserver& aObserver,
       
    95                                  MNcmConnectionMultiplexer* aMultiplexer,
       
    96                                  TTransportProtocol aTransportProtocol )
       
    97     {
       
    98     CSTUNClientImplementation* self =
       
    99         new ( ELeave ) CSTUNClientImplementation( aClient,
       
   100                                                   aRetransmitInterval,
       
   101                                                   aTimer,
       
   102                                                   aObserver,
       
   103                                                   aMultiplexer,
       
   104                                                   aTransportProtocol );
       
   105     CleanupStack::PushL( self );
       
   106     self->ConstructL( );
       
   107     CleanupStack::Pop( self );
       
   108     return self;    
       
   109     }
       
   110 
       
   111 // ---------------------------------------------------------------------------
       
   112 // CSTUNClientImplementation::CSTUNClientImplementation
       
   113 // ---------------------------------------------------------------------------
       
   114 //
       
   115 CSTUNClientImplementation::CSTUNClientImplementation(
       
   116     CSTUNClient& aClient,
       
   117     TInt aRetransmitInterval,
       
   118     TUint aServerPort,
       
   119     RSocketServ& aSocketServ,
       
   120     RConnection& aConnection,
       
   121     CDeltaTimer& aTimer,
       
   122     MSTUNClientObserver& aObserver,
       
   123     TBool aObtainSharedSecret,
       
   124     MNcmConnectionMultiplexer* aMultiplexer,
       
   125     TTransportProtocol aTransportProtocol ) :
       
   126     CSTUNTimerUser( aTimer ),
       
   127     iClient( aClient ),
       
   128     iRetransmitInterval( aRetransmitInterval ),
       
   129     iServerPort( aServerPort ? aServerPort : EDefaultSTUNPort ),
       
   130     iSocketServer( aSocketServ ),
       
   131     iConnection( aConnection ),    
       
   132     iObserver( aObserver ),
       
   133     iObtainSharedSecret( aObtainSharedSecret ),
       
   134     iMultiplexer( aMultiplexer ),
       
   135     iTransportProtocol( aTransportProtocol )
       
   136 #ifdef TEST_EUNIT
       
   137     , iTcpAddresses( 1 ),
       
   138     iUdpAddresses( 1 )
       
   139 #endif
       
   140     {    
       
   141     }
       
   142 
       
   143 // ---------------------------------------------------------------------------
       
   144 // CSTUNClientImplementation::CSTUNClientImplementation - overloaded
       
   145 // ---------------------------------------------------------------------------
       
   146 //
       
   147 CSTUNClientImplementation::CSTUNClientImplementation(
       
   148     CSTUNClient& aClient,
       
   149     TInt aRetransmitInterval,
       
   150     CDeltaTimer& aTimer,
       
   151     MSTUNClientObserver& aObserver,
       
   152     MNcmConnectionMultiplexer* aMultiplexer,
       
   153     TTransportProtocol aTransportProtocol ) :
       
   154     CSTUNTimerUser( aTimer ),
       
   155     iClient( aClient ),
       
   156     iRetransmitInterval( aRetransmitInterval ),
       
   157     iSocketServer( *( RSocketServ* )0x1 ),
       
   158     iConnection( *( RConnection* )0x1 ),
       
   159     iObserver( aObserver ),
       
   160     iMultiplexer( aMultiplexer ),
       
   161     iTransportProtocol( aTransportProtocol )
       
   162     {
       
   163     }
       
   164         
       
   165 // ---------------------------------------------------------------------------
       
   166 // CSTUNClientImplementation::CSTUNClientImplementation
       
   167 // Dummy implementation. Default constructor is declared private and not used.
       
   168 // ---------------------------------------------------------------------------
       
   169 //
       
   170 CSTUNClientImplementation::CSTUNClientImplementation() :
       
   171     CSTUNTimerUser( *( CDeltaTimer* )0x1 ),        
       
   172     iClient( *( CSTUNClient* )0x1 ),
       
   173     iSocketServer( *( RSocketServ* )0x1 ),
       
   174     iConnection( *( RConnection* )0x1 ),
       
   175     iObserver( *( MSTUNClientObserver* )0x1 )
       
   176     {
       
   177     }
       
   178 
       
   179 // ---------------------------------------------------------------------------
       
   180 // CSTUNClientImplementation::CSTUNClientImplementation
       
   181 // Dummy implementation. Copy constructor is declared private and not used.
       
   182 // ---------------------------------------------------------------------------
       
   183 //
       
   184 CSTUNClientImplementation::CSTUNClientImplementation(
       
   185     const CSTUNClientImplementation& aImplementation ) :
       
   186     CSTUNTimerUser( *( CDeltaTimer* )0x1 ),
       
   187     iClient( aImplementation.iClient ),
       
   188     iSocketServer( aImplementation.iSocketServer ),
       
   189     iConnection( aImplementation.iConnection ),
       
   190     iObserver( aImplementation.iObserver )
       
   191     {
       
   192     }
       
   193 
       
   194 // ---------------------------------------------------------------------------
       
   195 // CSTUNClientImplementation::ConstructL
       
   196 // ---------------------------------------------------------------------------
       
   197 //
       
   198 void CSTUNClientImplementation::ConstructL( const TDesC8& aServerAddress,
       
   199                                             const TDesC8& aServiceName,
       
   200                                             TBool aFailIfNoSRVRecordsFound,
       
   201                                             TBool aIcmpReceiverUsed )
       
   202     {
       
   203     __STUNTURNCLIENT( "CSTUNClientImplementation::ConstructL IN" )
       
   204     __ASSERT_ALWAYS( aServerAddress.Length() > 0, 
       
   205         User::Leave( KErrArgument ) );
       
   206     
       
   207     __STUNTURNCLIENT_STR8( "CSTUNClientImplementation stunserver=",
       
   208         aServerAddress )
       
   209     iServerAddress = aServerAddress.AllocL();
       
   210     
       
   211     __STUNTURNCLIENT_STR8( "CSTUNClientImplementation servicename=",
       
   212         aServiceName )
       
   213     
       
   214     iServiceName = aServiceName.AllocL();
       
   215     iAsyncCallback = CAsyncCallback::NewL( iObserver );
       
   216         
       
   217     iResolver = CNATFWUNSAFServerResolver::NewL( iSocketServer,
       
   218                                                  iConnection,
       
   219                                                  *this,
       
   220                                                  aFailIfNoSRVRecordsFound );
       
   221                                                  
       
   222     iTransactionIDGenerator = CTransactionIDGenerator::NewL();
       
   223     
       
   224     if ( aIcmpReceiverUsed )
       
   225         {
       
   226         iIcmpReceiver = CIcmpReceiver::NewL( *this, iSocketServer );
       
   227         }
       
   228     
       
   229     iRenewSharedSecret = new ( ELeave ) CSTUNClientRenewSharedSecret;
       
   230     iReady = new ( ELeave ) CSTUNClientReady( *iRenewSharedSecret );
       
   231     iGetSharedSecret = new ( ELeave ) CSTUNClientGetSharedSecret( *iReady );
       
   232     iResolvingUDP = new ( ELeave ) CSTUNClientResolvingUDP( *iGetSharedSecret,
       
   233                                                             *iReady );
       
   234     if ( iObtainSharedSecret )
       
   235         {
       
   236         iResolvingTLS = new ( ELeave ) 
       
   237             CSTUNClientResolvingTLS( *iResolvingUDP );
       
   238         iResolvingTCP = new ( ELeave ) 
       
   239             CSTUNClientResolvingTCP( *iResolvingTLS, *iResolvingUDP );
       
   240         }
       
   241     else
       
   242         {
       
   243         iResolvingTCP = new ( ELeave )
       
   244             CSTUNClientResolvingTCP( *iResolvingUDP );
       
   245         }
       
   246     
       
   247     iRenewSharedSecret->SetNeighbourStates( *iReady );
       
   248 
       
   249     if ( iServiceName->Compare( KStunRelay ) == 0 && iObtainSharedSecret )
       
   250         {
       
   251         ChangeState( iResolvingTLS );
       
   252         ResolveAddressL( KTls, iTcpAddresses, KTlsPort, KStunRelay );
       
   253         }
       
   254     
       
   255     else if ( iObtainSharedSecret )
       
   256         {
       
   257         ChangeState( iResolvingTCP );
       
   258         ResolveAddressL( KTcp, iTcpAddresses, iServerPort, *iServiceName );
       
   259         }
       
   260     
       
   261     else
       
   262         {
       
   263         ChangeState( iResolvingUDP );
       
   264         ResolveUdpL();
       
   265         }
       
   266         
       
   267     __STUNTURNCLIENT( "CSTUNClientImplementation::ConstructL OUT" )
       
   268     }
       
   269 
       
   270 // ---------------------------------------------------------------------------
       
   271 // CSTUNClientImplementation::ConstructL - overloaded for ICE conn check usage
       
   272 // ---------------------------------------------------------------------------
       
   273 //
       
   274 void CSTUNClientImplementation::ConstructL( )
       
   275     {
       
   276     __STUNTURNCLIENT( "CSTUNClientImplementation::ConstructL for ICE" )
       
   277 
       
   278     TInetAddr defaultAddr;
       
   279     defaultAddr.SetAddress( KInetAddrAll );
       
   280     
       
   281     // Make state machine work with hack
       
   282     iUdpAddresses.AppendL( defaultAddr );
       
   283     iObtainSharedSecret = EFalse;
       
   284     iAsyncCallback = CAsyncCallback::NewL( iObserver );
       
   285         
       
   286     iTransactionIDGenerator = CTransactionIDGenerator::NewL();
       
   287     
       
   288     iRenewSharedSecret = new ( ELeave ) CSTUNClientRenewSharedSecret;
       
   289     iReady = new ( ELeave ) CSTUNClientReady( *iRenewSharedSecret );
       
   290     iGetSharedSecret = new ( ELeave ) CSTUNClientGetSharedSecret( *iReady );
       
   291     iResolvingUDP = new ( ELeave ) CSTUNClientResolvingUDP( *iGetSharedSecret,
       
   292                                                             *iReady );
       
   293     iResolvingTCP = new ( ELeave ) CSTUNClientResolvingTCP( *iResolvingUDP );
       
   294     
       
   295     iRenewSharedSecret->SetNeighbourStates( *iReady );
       
   296     ChangeState( iReady );
       
   297     // do not send notify, act as syncronous
       
   298     }
       
   299 
       
   300     
       
   301 // ---------------------------------------------------------------------------
       
   302 // CSTUNClientImplementation::~CSTUNClientImplementation
       
   303 // ---------------------------------------------------------------------------
       
   304 //
       
   305 CSTUNClientImplementation::~CSTUNClientImplementation()
       
   306     {
       
   307     __STUNTURNCLIENT( "CSTUNClientImplementation::~CSTUNClientImplementation" )
       
   308     TInt count( iBindings.Count() );
       
   309     if ( count > 0 )
       
   310         {
       
   311         for ( TInt i = 0; i < count; ++i ) // check that ++i
       
   312             {        
       
   313             iBindings[i]->Implementation().DetachClient();
       
   314             }
       
   315         }
       
   316     iBindings.Close();    
       
   317 
       
   318     FreeResolverResources();
       
   319 
       
   320     delete iAsyncCallback;
       
   321     delete iResolvingTLS;
       
   322     delete iResolvingTCP;
       
   323     delete iResolvingUDP;
       
   324     delete iGetSharedSecret;
       
   325     delete iReady;
       
   326     delete iRenewSharedSecret;
       
   327 
       
   328     delete iTransactionIDGenerator;
       
   329     delete iSharedSecret;
       
   330     delete iCredentials;
       
   331     delete iIcmpReceiver;
       
   332     
       
   333     delete iTimer;
       
   334     delete iServiceName;
       
   335 
       
   336     iTcpAddresses.Close();
       
   337     iUdpAddresses.Close();
       
   338     delete iServerAddress;
       
   339     iMultiplexer = NULL;
       
   340     iState = NULL;
       
   341     
       
   342     __STUNTURNCLIENT( "CSTUNClientImplementation::\
       
   343     ~CSTUNClientImplementation end" )
       
   344     }
       
   345 
       
   346 // ---------------------------------------------------------------------------
       
   347 // CSTUNClientImplementation::CompletedL
       
   348 // ---------------------------------------------------------------------------
       
   349 //
       
   350 void CSTUNClientImplementation::CompletedL()
       
   351     {
       
   352     __STUNTURNCLIENT( "CSTUNClientImplementation::CompletedL" )
       
   353     __TEST_INVARIANT;
       
   354 
       
   355     if ( iState )
       
   356         {
       
   357         iState->ResolvingCompletedL( *this, iObtainSharedSecret );
       
   358         }
       
   359         
       
   360     __TEST_INVARIANT;
       
   361     __STUNTURNCLIENT( "CSTUNClientImplementation::CompletedL end" )
       
   362     }
       
   363 
       
   364 // ---------------------------------------------------------------------------
       
   365 // CSTUNClientImplementation::ErrorOccured
       
   366 // ---------------------------------------------------------------------------
       
   367 //
       
   368 void CSTUNClientImplementation::ErrorOccured( TInt aError )
       
   369     {
       
   370     __STUNTURNCLIENT_INT1( "CSTUNClientImplementation::ErrorOccured: "
       
   371         , aError )
       
   372     __TEST_INVARIANT;
       
   373 
       
   374     if ( iState )
       
   375         {
       
   376         if ( aError != KErrNoMemory )
       
   377             {
       
   378             aError = KErrNATFWDnsFailure;
       
   379             }
       
   380         iState->ResolvingFailed( *this, aError );
       
   381         }
       
   382         
       
   383     __TEST_INVARIANT;    
       
   384     __STUNTURNCLIENT( "CSTUNClientImplementation::ErrorOccured end" )
       
   385     }
       
   386 
       
   387 // ---------------------------------------------------------------------------
       
   388 // CSTUNClientImplementation::IcmpError
       
   389 // Don't compare aAddress to iUdpAddresses, as some binding may still have an
       
   390 // address that has been removed from iUdpAddresses if another binding called
       
   391 // ObtainServerAddress.
       
   392 // ---------------------------------------------------------------------------
       
   393 //
       
   394 void CSTUNClientImplementation::IcmpError( const TInetAddr& aAddress )
       
   395     {
       
   396     __STUNTURNCLIENT( "CSTUNClientImplementation::IcmpError" )
       
   397     __TEST_INVARIANT;
       
   398 
       
   399     for ( TInt i = 0; i < iBindings.Count(); ++i )
       
   400         {
       
   401         iBindings[i]->Implementation().IcmpError( aAddress );
       
   402         }
       
   403     RemoveAddress( aAddress );
       
   404 
       
   405     __TEST_INVARIANT;
       
   406     __STUNTURNCLIENT( "CSTUNClientImplementation::IcmpError end" )
       
   407     }
       
   408 
       
   409 // ---------------------------------------------------------------------------
       
   410 // CSTUNClientImplementation::SharedSecretObtainedL
       
   411 // ---------------------------------------------------------------------------
       
   412 //
       
   413 void CSTUNClientImplementation::SharedSecretObtainedL()
       
   414     {
       
   415     __STUNTURNCLIENT( "CSTUNClientImplementation::SharedSecretObtainedL" )
       
   416     __TEST_INVARIANT;
       
   417 
       
   418     if ( iState )
       
   419         {        
       
   420         iState->SharedSecretReceivedL( *this );
       
   421         }
       
   422 
       
   423     __TEST_INVARIANT;
       
   424     __STUNTURNCLIENT( "CSTUNClientImplementation::SharedSecretObtainedL end" )
       
   425     }
       
   426 
       
   427 // ---------------------------------------------------------------------------
       
   428 // CSTUNClientImplementation::SharedSecretErrorL
       
   429 // ---------------------------------------------------------------------------
       
   430 //
       
   431 void CSTUNClientImplementation::SharedSecretErrorL( TInt aError )
       
   432     {
       
   433     __STUNTURNCLIENT( "CSTUNClientImplementation::SharedSecretErrorL" )
       
   434     __TEST_INVARIANT;
       
   435     
       
   436     if ( iState )
       
   437         {
       
   438         iState->SharedSecretErrorL( *this, aError );
       
   439         }
       
   440 
       
   441     __TEST_INVARIANT;
       
   442     __STUNTURNCLIENT( "CSTUNClientImplementation::SharedSecretErrorL end" )
       
   443     }
       
   444 
       
   445 // ---------------------------------------------------------------------------
       
   446 // CSTUNClientImplementation::STUNClient
       
   447 // ---------------------------------------------------------------------------
       
   448 //
       
   449 const CSTUNClient& CSTUNClientImplementation::STUNClient() const
       
   450     {
       
   451     __TEST_INVARIANT;
       
   452 
       
   453     return iClient;
       
   454     }
       
   455 
       
   456 // ---------------------------------------------------------------------------
       
   457 // CSTUNClientImplementation::TimerProvider
       
   458 // ---------------------------------------------------------------------------
       
   459 //
       
   460 CDeltaTimer& CSTUNClientImplementation::TimerProvider()
       
   461     {
       
   462     __TEST_INVARIANT;
       
   463 
       
   464     return DeltaTimer();
       
   465     }
       
   466 
       
   467 // ---------------------------------------------------------------------------
       
   468 // CSTUNClientImplementation::RetransmitInterval
       
   469 // ---------------------------------------------------------------------------
       
   470 //
       
   471 TInt CSTUNClientImplementation::RetransmitInterval() const
       
   472     {
       
   473     __TEST_INVARIANT;
       
   474 
       
   475     return iRetransmitInterval;
       
   476     }
       
   477 
       
   478 // ---------------------------------------------------------------------------
       
   479 // CSTUNClientImplementation::TransportProtocol
       
   480 // ---------------------------------------------------------------------------
       
   481 //
       
   482 TTransportProtocol CSTUNClientImplementation::TransportProtocol() const
       
   483     {
       
   484     __TEST_INVARIANT;
       
   485 
       
   486     return iTransportProtocol;
       
   487     }
       
   488     
       
   489 // ---------------------------------------------------------------------------
       
   490 // CSTUNClientImplementation::AddressResolvedL
       
   491 // ---------------------------------------------------------------------------
       
   492 //
       
   493 void CSTUNClientImplementation::AddressResolvedL(
       
   494     const CBinding& aBinding ) const
       
   495     {
       
   496     __TEST_INVARIANT;
       
   497 
       
   498     iAsyncCallback->MakeCallbackL( TSTUNCallbackInfo::EEventAddressResolvedL,
       
   499                                    &aBinding );
       
   500     }
       
   501 
       
   502 // ---------------------------------------------------------------------------
       
   503 // CSTUNClientImplementation::ObtainSharedSecretL
       
   504 // ---------------------------------------------------------------------------
       
   505 //
       
   506 void CSTUNClientImplementation::ObtainSharedSecretL( CBinding& aBinding )
       
   507     {
       
   508     __TEST_INVARIANT;
       
   509     __ASSERT_ALWAYS( iState != NULL, User::Leave( KErrServerTerminated ) );
       
   510 
       
   511     iState->ObtainSharedSecretL( *this, aBinding );
       
   512 
       
   513     __TEST_INVARIANT;
       
   514     }
       
   515 
       
   516 // ---------------------------------------------------------------------------
       
   517 // CSTUNClientImplementation::SharedSecretRejectedL
       
   518 // ---------------------------------------------------------------------------
       
   519 //
       
   520 TBool
       
   521 CSTUNClientImplementation::SharedSecretRejectedL( const CBinding& aBinding,
       
   522                                                   const TDesC8& aUsername,
       
   523                                                   const TDesC8& aPassword )
       
   524     {
       
   525     __TEST_INVARIANT;
       
   526 
       
   527     return iState && iState->SharedSecretRejectedL( *this,
       
   528                                                     aBinding,
       
   529                                                     aUsername,
       
   530                                                     aPassword );        
       
   531     }
       
   532 
       
   533 // ---------------------------------------------------------------------------
       
   534 // CSTUNClientImplementation::ObtainServerAddress
       
   535 // ---------------------------------------------------------------------------
       
   536 //
       
   537 TBool CSTUNClientImplementation::ObtainServerAddress( TInetAddr& aAddress )    
       
   538     {
       
   539     __STUNTURNCLIENT( "CSTUNClientImplementation::ObtainServerAddress" )
       
   540     __TEST_INVARIANT;
       
   541     #ifdef _DEBUG_EUNIT
       
   542         TInetAddr addr;
       
   543         addr.SetAddress( INET_ADDR( 10,32,194,251 ) );
       
   544         addr.SetFamily( KAfInet );
       
   545         iUdpAddresses.Append( addr );
       
   546     #endif
       
   547 
       
   548     if ( !aAddress.IsUnspecified() )
       
   549         {
       
   550         // Binding has tried aAddress and found it doesn't respond.
       
   551         RemoveAddress( aAddress );
       
   552         }
       
   553 
       
   554     if ( iUdpAddresses.Count() > 0 )
       
   555         {
       
   556         aAddress = iUdpAddresses[ 0 ];
       
   557         return ETrue;
       
   558         }
       
   559 
       
   560     __STUNTURNCLIENT( "CSTUNClientImplementation::ObtainServerAddress end" )
       
   561     return EFalse;
       
   562     }
       
   563 
       
   564 // ---------------------------------------------------------------------------
       
   565 // CSTUNClientImplementation::ObtainTransactionIDL
       
   566 // ---------------------------------------------------------------------------
       
   567 //
       
   568 void CSTUNClientImplementation::ObtainTransactionIDL(
       
   569     TNATFWUNSAFTransactionID& aTransactionID )
       
   570     {
       
   571     __STUNTURNCLIENT( "CSTUNClientImplementation::ObtainTransactionIDL" )
       
   572     __TEST_INVARIANT;
       
   573     #ifdef _DEBUG_EUNIT
       
   574         aTransactionID.Append(TUint8(0));
       
   575         aTransactionID.Append(TUint8(1));
       
   576         aTransactionID.Append(TUint8(2));
       
   577         aTransactionID.Append(TUint8(3));
       
   578         aTransactionID.Append(TUint8(4));
       
   579         aTransactionID.Append(TUint8(5));
       
   580         aTransactionID.Append(TUint8(6));
       
   581         aTransactionID.Append(TUint8(7));
       
   582         aTransactionID.Append(TUint8(8));
       
   583         aTransactionID.Append(TUint8(9));
       
   584         aTransactionID.Append(TUint8(0xa));
       
   585         aTransactionID.Append(TUint8(0xb));
       
   586     #else
       
   587         iTransactionIDGenerator->GetIDL( this, sizeof( *this ), aTransactionID );
       
   588     #endif
       
   589     __TEST_INVARIANT;
       
   590     __STUNTURNCLIENT( "CSTUNClientImplementation::ObtainTransactionIDL end" )
       
   591     }
       
   592 
       
   593 // ---------------------------------------------------------------------------
       
   594 // CSTUNClientImplementation::AttachBindingL
       
   595 // ---------------------------------------------------------------------------
       
   596 //
       
   597 void CSTUNClientImplementation::AttachBindingL( const CBinding& aBinding )
       
   598     {
       
   599     __STUNTURNCLIENT( "CSTUNClientImplementation::AttachBindingL" )
       
   600     __TEST_INVARIANT;
       
   601 
       
   602     iBindings.AppendL( &aBinding );
       
   603 
       
   604     __TEST_INVARIANT;
       
   605     __STUNTURNCLIENT( "CSTUNClientImplementation::AttachBindingL end" )
       
   606     }
       
   607                                      
       
   608 // ---------------------------------------------------------------------------
       
   609 // CSTUNClientImplementation::DetachBinding
       
   610 // ---------------------------------------------------------------------------
       
   611 //
       
   612 void CSTUNClientImplementation::DetachBinding( const CBinding& aBinding )
       
   613     {
       
   614     __STUNTURNCLIENT( "CSTUNClientImplementation::DetachBinding" )
       
   615     __TEST_INVARIANT;
       
   616 
       
   617     for ( TInt i = 0; i < iBindings.Count(); ++i )
       
   618         {
       
   619         if ( iBindings[ i ] == &aBinding )
       
   620             {
       
   621             iAsyncCallback->CancelCallback( *iBindings[i] );
       
   622             iBindings.Remove( i );
       
   623             __TEST_INVARIANT;
       
   624             return;
       
   625             }
       
   626         }
       
   627     __STUNTURNCLIENT( "CSTUNClientImplementation::DetachBinding end" )
       
   628     }
       
   629 
       
   630 // ---------------------------------------------------------------------------
       
   631 // CSTUNClientImplementation::BindingErrorL
       
   632 // ---------------------------------------------------------------------------
       
   633 //
       
   634 void CSTUNClientImplementation::BindingErrorL( const CBinding& aBinding,
       
   635                                                TInt aError, TBool aIsFatal )
       
   636     {
       
   637     __STUNTURNCLIENT( "CSTUNClientImplementation::BindingErrorL" )
       
   638     __TEST_INVARIANT;
       
   639     
       
   640     if ( aIsFatal )
       
   641         {
       
   642         iAsyncCallback->MakeCallbackL( 
       
   643             TSTUNCallbackInfo::EErrorOccurred, &aBinding, aError );
       
   644         }
       
   645     
       
   646     else
       
   647         {
       
   648         TInt count( iBindings.Count() );
       
   649         TInt index( KErrNotFound );
       
   650         
       
   651         for ( TInt i = 0; i < count; i++ )
       
   652             {
       
   653             if ( &aBinding == iBindings[i] )
       
   654                 {
       
   655                 index = i;
       
   656                 }
       
   657             }
       
   658         
       
   659         switch ( aError )
       
   660             {
       
   661             case E401Unauthorized:
       
   662                 if ( index >= 0 )
       
   663                     {
       
   664                     if ( iBindings[index]->RealmFromResponse() )
       
   665                         {
       
   666                         // Client should set long term credentials
       
   667                         iAsyncCallback->MakeCallbackL( 
       
   668                             TSTUNCallbackInfo::EErrorOccurred, 
       
   669                             &aBinding, aError );
       
   670                         }
       
   671                     else
       
   672                         {
       
   673                         RenewSharedSecretL();
       
   674                         }
       
   675                     }
       
   676             
       
   677             break;
       
   678             
       
   679             case E430StaleCredentials:
       
   680             break;
       
   681             
       
   682             case E436UnknownUsername:
       
   683                 if ( iCredentials && iSharedSecret )
       
   684                     {
       
   685                     // FATAL ERROR
       
   686                     iAsyncCallback->MakeCallbackL( 
       
   687                         TSTUNCallbackInfo::EErrorOccurred, &aBinding, aError );
       
   688                     }
       
   689                 else
       
   690                     {
       
   691                     RenewSharedSecretL();
       
   692                     }
       
   693             break;
       
   694             
       
   695             case E432MissingUsername:
       
   696             break;
       
   697             
       
   698             case E433UseTLS:
       
   699                 if ( iCredentials || iSharedSecret )
       
   700                     {
       
   701                     // FATAL ERROR
       
   702                     iAsyncCallback->MakeCallbackL( 
       
   703                         TSTUNCallbackInfo::EErrorOccurred, &aBinding, aError );
       
   704                     }
       
   705                 else
       
   706                     {
       
   707                     RenewSharedSecretL();
       
   708                     }
       
   709             break;
       
   710             
       
   711             case E434MissingRealm:
       
   712                 // client should decide if try again with credentials
       
   713                 iAsyncCallback->MakeCallbackL( 
       
   714                     TSTUNCallbackInfo::EErrorOccurred, &aBinding, aError );
       
   715             break;
       
   716             
       
   717             default:
       
   718                 // FATAL ERROR
       
   719                 iAsyncCallback->MakeCallbackL( 
       
   720                     TSTUNCallbackInfo::EErrorOccurred, 
       
   721                     &aBinding, KErrGeneral );
       
   722             break;
       
   723             }
       
   724         }
       
   725     
       
   726     __STUNTURNCLIENT( "CSTUNClientImplementation::BindingErrorL end" )
       
   727     }
       
   728 
       
   729 // ---------------------------------------------------------------------------
       
   730 // CSTUNClientImplementation::BindingEventOccurred
       
   731 // ---------------------------------------------------------------------------
       
   732 //
       
   733 void CSTUNClientImplementation::BindingEventOccurred( 
       
   734     const CBinding& aBinding, TSTUNCallbackInfo::TFunction aEvent )
       
   735     {
       
   736     __STUNTURNCLIENT( "CSTUNClientImplementation::BindingEventOccurred" )
       
   737     TRAP_IGNORE( iAsyncCallback->MakeCallbackL( aEvent,
       
   738                                                 &aBinding,
       
   739                                                 KErrNone ) );
       
   740                                    
       
   741     __STUNTURNCLIENT( "CSTUNClientImplementation::BindingEventOccurred exit" )
       
   742     }
       
   743 
       
   744 // ---------------------------------------------------------------------------
       
   745 // CSTUNClientImplementation::PassInitCompletedL
       
   746 // ---------------------------------------------------------------------------
       
   747 //
       
   748 void CSTUNClientImplementation::PassInitCompletedL( TInt aError ) const
       
   749     {
       
   750     __STUNTURNCLIENT( "CSTUNClientImplementation::PassInitCompletedL" )
       
   751     __TEST_INVARIANT;
       
   752 
       
   753     iAsyncCallback->MakeCallbackL( TSTUNCallbackInfo::EInitCompleted,
       
   754                                    NULL,
       
   755                                    aError,
       
   756                                    &iClient );
       
   757 
       
   758     __STUNTURNCLIENT( "CSTUNClientImplementation::PassInitCompletedL end" )
       
   759     }
       
   760 
       
   761 // ---------------------------------------------------------------------------
       
   762 // CSTUNClientImplementation::PassCredentialsRejectedL
       
   763 // ---------------------------------------------------------------------------
       
   764 //
       
   765 void CSTUNClientImplementation::PassCredentialsRejectedL(
       
   766     const CBinding& aBinding ) const
       
   767     {
       
   768     __STUNTURNCLIENT( "CSTUNClientImplementation::PassCredentialsRejectedL" )
       
   769     __TEST_INVARIANT;
       
   770 
       
   771     iAsyncCallback->MakeCallbackL( TSTUNCallbackInfo::EEventCredentialsRejected,
       
   772                                    &aBinding );
       
   773     __STUNTURNCLIENT( "CSTUNClientImplementation::PassCredentialsRejectedL end" )
       
   774     }
       
   775 
       
   776 // ---------------------------------------------------------------------------
       
   777 // CSTUNClientImplementation::IsInitialized
       
   778 // ---------------------------------------------------------------------------
       
   779 //
       
   780 TBool CSTUNClientImplementation::IsInitialized() const
       
   781     {
       
   782     __TEST_INVARIANT;
       
   783 
       
   784     return ( iState == iReady ) || ( iState == iRenewSharedSecret );
       
   785     }
       
   786 
       
   787 // ---------------------------------------------------------------------------
       
   788 // CSTUNClientImplementation::STUNServerAddrL
       
   789 // ---------------------------------------------------------------------------
       
   790 //
       
   791 const TInetAddr& CSTUNClientImplementation::STUNServerAddrL() const
       
   792     {
       
   793     __TEST_INVARIANT;
       
   794     __ASSERT_ALWAYS( IsInitialized(), User::Leave( KErrNotReady ) );
       
   795     __ASSERT_ALWAYS( iUdpAddresses.Count() > 0, User::Leave( KErrNotFound ) );
       
   796 
       
   797     return iUdpAddresses[ 0 ];
       
   798     }
       
   799 
       
   800 // ---------------------------------------------------------------------------
       
   801 // CSTUNClientImplementation::SetCredentialsL
       
   802 // iCredentials and iSharedSecret can both exist at the same time.
       
   803 // ---------------------------------------------------------------------------
       
   804 //    
       
   805 void CSTUNClientImplementation::SetCredentialsL( const TDesC8& aUsername,
       
   806                                                  const TDesC8& aPassword )
       
   807     {
       
   808     __STUNTURNCLIENT( "CSTUNClientImplementation::SetCredentialsL in" )
       
   809     __TEST_INVARIANT;
       
   810     __STUN_ASSERT_L( aUsername.Length(), KErrArgument );
       
   811     __STUN_ASSERT_L( aPassword.Length(), KErrArgument );
       
   812     
       
   813     CSTUNCredentials* credentials = CSTUNCredentials::NewL( aUsername,
       
   814                                                             aPassword );
       
   815     delete iCredentials;
       
   816     iCredentials = NULL;
       
   817     iCredentials = credentials;
       
   818 
       
   819     __TEST_INVARIANT;
       
   820     __STUNTURNCLIENT( "CSTUNClientImplementation::SetCredentialsL end" )
       
   821     }
       
   822 
       
   823 // -----------------------------------------------------------------------------
       
   824 // CSTUNClientImplementation::SharedSecretObtained
       
   825 // -----------------------------------------------------------------------------
       
   826 //
       
   827 TBool CSTUNClientImplementation::SharedSecretObtained() const
       
   828     {
       
   829     __STUNTURNCLIENT( "CSTUNClientImplementation::SharedSecretObtained" )
       
   830     return ( iSharedSecret && iSharedSecret->Username().Length() > 0 );
       
   831     }
       
   832 
       
   833 // -----------------------------------------------------------------------------
       
   834 // CSTUNClientImplementation::HasPresetCredentials
       
   835 // -----------------------------------------------------------------------------
       
   836 //
       
   837 TBool CSTUNClientImplementation::HasPresetCredentials() const
       
   838     {
       
   839     __TEST_INVARIANT;
       
   840     __STUNTURNCLIENT( "CSTUNClientImplementation::HasPresetCredentials" )
       
   841     return iCredentials != NULL;
       
   842     }
       
   843 
       
   844 // -----------------------------------------------------------------------------
       
   845 // CSTUNClientImplementation::ChangeState
       
   846 // -----------------------------------------------------------------------------
       
   847 //
       
   848 void CSTUNClientImplementation::ChangeState( const CSTUNClientState* aNewState )
       
   849     {
       
   850     __STUNTURNCLIENT( "CSTUNClientImplementation::ChangeState" )
       
   851     __TEST_INVARIANT;
       
   852 
       
   853     iState = aNewState;
       
   854 
       
   855     __TEST_INVARIANT;
       
   856     __STUNTURNCLIENT( "CSTUNClientImplementation::ChangeState end" )
       
   857     }
       
   858 
       
   859 // ---------------------------------------------------------------------------
       
   860 // CSTUNClientImplementation::Terminate
       
   861 // ---------------------------------------------------------------------------
       
   862 //
       
   863 void CSTUNClientImplementation::Terminate( TInt aError )
       
   864     {
       
   865     __STUNTURNCLIENT( "CSTUNClientImplementation::Terminate" )
       
   866     __TEST_INVARIANT;
       
   867     // Terminate only once    
       
   868     __STUN_ASSERT_RETURN( iState != NULL, KErrDied );    
       
   869     __STUN_ASSERT_RETURN( aError != KErrNone, KErrArgument );
       
   870 
       
   871     NATFWUNSAF_INTLOG( "STUNClient terminated, reason", aError )
       
   872 
       
   873     if ( !IsInitialized() )
       
   874         {
       
   875         TRAP_IGNORE( PassInitCompletedL( aError ) )
       
   876         }
       
   877     ChangeState( NULL );
       
   878 
       
   879     // A terminating binding calls CSTUNClientImplementation::BindingErrorL
       
   880     for ( TInt i = 0; i < iBindings.Count(); ++i )
       
   881         {
       
   882         iBindings[i]->Implementation().Terminate( aError );
       
   883         }
       
   884 
       
   885     __TEST_INVARIANT;
       
   886     __STUNTURNCLIENT( "CSTUNClientImplementation::Terminate end" )
       
   887     }
       
   888 
       
   889 // ---------------------------------------------------------------------------
       
   890 // CSTUNClientImplementation::TcpResolvedL
       
   891 // ---------------------------------------------------------------------------
       
   892 //
       
   893 void CSTUNClientImplementation::TcpResolvedL( TBool aObtainSharedSecret )
       
   894     {
       
   895     __STUNTURNCLIENT( "CSTUNClientImplementation::TcpResolvedL" )
       
   896     __TEST_INVARIANT;
       
   897 
       
   898     iObtainSharedSecret = aObtainSharedSecret;
       
   899     
       
   900     if ( iObtainSharedSecret )
       
   901         {
       
   902         ResolveAddressL( KTls, iTcpAddresses, KTlsPort, *iServiceName );
       
   903         }
       
   904     else
       
   905         {
       
   906         ResolveUdpL();
       
   907         }
       
   908 
       
   909     __TEST_INVARIANT;
       
   910     __STUNTURNCLIENT( "CSTUNClientImplementation::TcpResolvedL end" )
       
   911     }
       
   912 
       
   913 // ---------------------------------------------------------------------------
       
   914 // CSTUNClientImplementation::TlsResolvedL
       
   915 // ---------------------------------------------------------------------------
       
   916 //
       
   917 void CSTUNClientImplementation::TlsResolvedL( )
       
   918     {
       
   919     __STUNTURNCLIENT( "CSTUNClientImplementation::TlsResolvedL" )
       
   920 
       
   921     ResolveUdpL();
       
   922 
       
   923     __TEST_INVARIANT;
       
   924     __STUNTURNCLIENT( "CSTUNClientImplementation::TlsResolvedL end" )
       
   925     }
       
   926 
       
   927 // ---------------------------------------------------------------------------
       
   928 // CSTUNClientImplementation::FreeResolverResources
       
   929 // Don't use __TEST_INVARIANT, as this function is used from destructor.
       
   930 // ---------------------------------------------------------------------------
       
   931 //
       
   932 void CSTUNClientImplementation::FreeResolverResources()
       
   933     {
       
   934     __STUNTURNCLIENT( "CSTUNClientImplementation::FreeResolverResources" )
       
   935     if ( iResolver )
       
   936         {
       
   937         iResolver->CancelResolving();
       
   938         delete iResolver;
       
   939         iResolver = NULL;
       
   940         }
       
   941     __STUNTURNCLIENT( "CSTUNClientImplementation::FreeResolverResources end" )
       
   942     }
       
   943 
       
   944 // ---------------------------------------------------------------------------
       
   945 // CSTUNClientImplementation::ResolveUdpL
       
   946 // ---------------------------------------------------------------------------
       
   947 //
       
   948 void CSTUNClientImplementation::ResolveUdpL()
       
   949     {
       
   950     __STUNTURNCLIENT( "CSTUNClientImplementation::ResolveUdpL" )
       
   951     __TEST_INVARIANT;
       
   952 
       
   953     ResolveAddressL( KUdp, iUdpAddresses );
       
   954 
       
   955     __TEST_INVARIANT;
       
   956     __STUNTURNCLIENT( "CSTUNClientImplementation::ResolveUdpL end" )
       
   957     }
       
   958 
       
   959 // ---------------------------------------------------------------------------
       
   960 // CSTUNClientImplementation::ResolveAddressL
       
   961 // ---------------------------------------------------------------------------
       
   962 //
       
   963 void CSTUNClientImplementation::ResolveAddressL( const TDesC8& aProtocol,
       
   964                                                  RArray<TInetAddr>& aResult )
       
   965     {
       
   966     __STUNTURNCLIENT( "CSTUNClientImplementation::ResolveAddressL" )
       
   967     __TEST_INVARIANT;
       
   968     
       
   969     __STUNTURNCLIENT_STR8( "stunserveraddress: ", *iServerAddress )
       
   970     __STUNTURNCLIENT_INT1( "stunserverport: ", iServerPort )
       
   971     __STUNTURNCLIENT_STR8( "protocol: ", aProtocol )
       
   972     
       
   973     iResolver->ResolveL( *iServerAddress,
       
   974                          *iServiceName,
       
   975                          aProtocol,
       
   976                          iServerPort,
       
   977                          aResult );
       
   978     
       
   979     __TEST_INVARIANT;
       
   980     __STUNTURNCLIENT( "CSTUNClientImplementation::ResolveAddressL end" )
       
   981     }
       
   982 
       
   983 // ---------------------------------------------------------------------------
       
   984 // CSTUNClientImplementation::ResolveAddressL
       
   985 // ---------------------------------------------------------------------------
       
   986 //
       
   987 void CSTUNClientImplementation::ResolveAddressL( const TDesC8& aProtocol,
       
   988                                                  RArray<TInetAddr>& aResult,
       
   989                                                  TUint aPort,
       
   990                                                  const TDesC8& aService )
       
   991     {
       
   992     __STUNTURNCLIENT( "CSTUNClientImplementation::ResolveAddressL" )
       
   993     __TEST_INVARIANT;
       
   994     
       
   995     __STUNTURNCLIENT_STR8( "stunserveraddress: ", *iServerAddress )
       
   996     __STUNTURNCLIENT_INT1( "stunserverport: ", iServerPort )
       
   997     __STUNTURNCLIENT_STR8( "protocol: ", aProtocol )
       
   998     __STUNTURNCLIENT_STR8( "service: ", aService )
       
   999     
       
  1000     iResolver->ResolveL( *iServerAddress,
       
  1001                          aService,
       
  1002                          aProtocol,
       
  1003                          aPort,
       
  1004                          aResult );
       
  1005     
       
  1006     __TEST_INVARIANT;
       
  1007     __STUNTURNCLIENT( "CSTUNClientImplementation::ResolveAddressL end" )
       
  1008     }
       
  1009 
       
  1010 // ---------------------------------------------------------------------------
       
  1011 // CSTUNClientImplementation::GetSharedSecretFromServerL
       
  1012 // ---------------------------------------------------------------------------
       
  1013 //
       
  1014 void CSTUNClientImplementation::GetSharedSecretFromServerL()
       
  1015     {
       
  1016     __STUNTURNCLIENT( "CSTUNClientImplementation::\
       
  1017     GetSharedSecretFromServerL" )
       
  1018     __TEST_INVARIANT;
       
  1019 
       
  1020     __ASSERT_ALWAYS( iTcpAddresses.Count() > 0, User::Leave( KErrNotFound ) );
       
  1021 
       
  1022     delete iSharedSecret;
       
  1023     iSharedSecret = NULL;
       
  1024     
       
  1025     if ( iCredentials )
       
  1026         {
       
  1027         iSharedSecret = CSTUNSharedSecret::NewL( iSocketServer,
       
  1028                                              iConnection,
       
  1029                                              DeltaTimer(),
       
  1030                                              iTcpAddresses[0],
       
  1031                                              *iTransactionIDGenerator,
       
  1032                                              *this,
       
  1033                                              iCredentials->Username(),
       
  1034                                              iCredentials->Password() );
       
  1035         }
       
  1036     
       
  1037     else
       
  1038         {
       
  1039         iSharedSecret = CSTUNSharedSecret::NewL( iSocketServer,
       
  1040                                              iConnection,
       
  1041                                              DeltaTimer(),
       
  1042                                              iTcpAddresses[0],
       
  1043                                              *iTransactionIDGenerator,
       
  1044                                              *this,
       
  1045                                              KNullDesC8(),
       
  1046                                              KNullDesC8() );
       
  1047         }
       
  1048     
       
  1049     // Stop current shared secret's timer
       
  1050     StopTimer();
       
  1051 
       
  1052     __TEST_INVARIANT;
       
  1053     __STUNTURNCLIENT( "CSTUNClientImplementation::\
       
  1054     GetSharedSecretFromServerL end" )
       
  1055     }
       
  1056 
       
  1057 // ---------------------------------------------------------------------------
       
  1058 // CSTUNClientImplementation::PassSharedSecretToBindingL
       
  1059 // ---------------------------------------------------------------------------
       
  1060 //
       
  1061 void CSTUNClientImplementation::PassSharedSecretToBindingL(
       
  1062     CBinding& aBinding ) const
       
  1063     {
       
  1064     __STUNTURNCLIENT( "CSTUNClientImplementation::PassSharedSecretToBindingL" )
       
  1065     __TEST_INVARIANT;
       
  1066 
       
  1067     const TDesC8* username = &KNullDesC8();
       
  1068     const TDesC8* password = &KNullDesC8();
       
  1069     
       
  1070     if ( iSharedSecret )
       
  1071         {
       
  1072         if ( iObtainSharedSecret )
       
  1073             {
       
  1074             username = &iSharedSecret->Username();
       
  1075             password = &iSharedSecret->Password();
       
  1076             }
       
  1077         }
       
  1078         
       
  1079     else if ( iCredentials )
       
  1080         {
       
  1081         username = &iCredentials->Username();
       
  1082         password = &iCredentials->Password();
       
  1083         }
       
  1084     else
       
  1085         {
       
  1086         // if ... else if chain end
       
  1087         }  
       
  1088     aBinding.Implementation().SharedSecretObtainedL( 
       
  1089         *username, *password );
       
  1090     
       
  1091     __STUNTURNCLIENT( "CSTUNClientImplementation::PassSharedSecretToBindingL end" )
       
  1092     }
       
  1093 
       
  1094 // ---------------------------------------------------------------------------
       
  1095 // CSTUNClientImplementation::TimerExpiredL
       
  1096 // ---------------------------------------------------------------------------
       
  1097 //
       
  1098 void CSTUNClientImplementation::TimerExpiredL()
       
  1099     {
       
  1100     __STUNTURNCLIENT( "CSTUNClientImplementation::TimerExpiredL" )
       
  1101     __TEST_INVARIANT;
       
  1102 
       
  1103     GetSharedSecretFromServerL();
       
  1104     ChangeState( iRenewSharedSecret );
       
  1105 
       
  1106     __TEST_INVARIANT;
       
  1107     __STUNTURNCLIENT( "CSTUNClientImplementation::TimerExpiredL end" )
       
  1108     }
       
  1109 
       
  1110 // -----------------------------------------------------------------------------
       
  1111 // CSTUNClientImplementation::LeaveFromTimerExpired
       
  1112 // -----------------------------------------------------------------------------
       
  1113 //
       
  1114 void CSTUNClientImplementation::LeaveFromTimerExpired( TInt aError )
       
  1115     {
       
  1116     __STUNTURNCLIENT_INT1( "CSTUNClientImplementation::\
       
  1117     LeaveFromTimerExpired error:", aError )
       
  1118     __TEST_INVARIANT;
       
  1119     __STUN_ASSERT_RETURN( aError != KErrNone, KErrArgument );
       
  1120 
       
  1121     Terminate( aError );
       
  1122     }
       
  1123 
       
  1124 // ---------------------------------------------------------------------------
       
  1125 // CSTUNClientImplementation::StartSharedSecretTimer
       
  1126 // ---------------------------------------------------------------------------
       
  1127 //
       
  1128 void CSTUNClientImplementation::StartSharedSecretTimer()
       
  1129     {
       
  1130     __STUNTURNCLIENT( "CSTUNClientImplementation::StartSharedSecretTimer" )
       
  1131     __TEST_INVARIANT;
       
  1132 
       
  1133     const TUint KSharedSecretValidTime = 540000; // 9 minutes
       
  1134     StartTimer( KSharedSecretValidTime );
       
  1135 
       
  1136     __TEST_INVARIANT;
       
  1137     }
       
  1138 
       
  1139 // ---------------------------------------------------------------------------
       
  1140 // CSTUNClientImplementation::MultiplexerInstance
       
  1141 // ---------------------------------------------------------------------------
       
  1142 //
       
  1143 MNcmConnectionMultiplexer* 
       
  1144     CSTUNClientImplementation::MultiplexerInstance()
       
  1145     {
       
  1146     return iMultiplexer;
       
  1147     }
       
  1148 
       
  1149 // -----------------------------------------------------------------------------
       
  1150 // CSTUNClientImplementation::RenewSharedSecretL
       
  1151 // -----------------------------------------------------------------------------
       
  1152 //
       
  1153 void CSTUNClientImplementation::RenewSharedSecretL()
       
  1154     {
       
  1155     __STUNTURNCLIENT( "CSTUNClientImplementation::RenewSharedSecretL" )
       
  1156     if ( !iObtainSharedSecret )
       
  1157         {
       
  1158         delete iResolver;
       
  1159         iResolver = NULL;
       
  1160         iResolver = CNATFWUNSAFServerResolver::NewL( iSocketServer,
       
  1161                                                      iConnection,
       
  1162                                                      *this,
       
  1163                                                      ETrue );
       
  1164         iObtainSharedSecret = ETrue;
       
  1165         
       
  1166         __STUNTURNCLIENT_STR8( "servicename: ", *iServiceName )
       
  1167         
       
  1168         if ( !iResolvingTLS )
       
  1169             {
       
  1170             iResolvingTLS = new ( ELeave ) 
       
  1171                 CSTUNClientResolvingTLS( *iResolvingUDP );
       
  1172             // must set state to ensure that TCP state is not current state
       
  1173             ChangeState( iResolvingTLS );
       
  1174             delete iResolvingTCP;
       
  1175             iResolvingTCP = NULL;
       
  1176             
       
  1177             iResolvingTCP = new ( ELeave ) 
       
  1178                 CSTUNClientResolvingTCP( *iResolvingTLS, *iResolvingUDP );
       
  1179             }
       
  1180         
       
  1181         if ( iServiceName->Compare( KStunRelay ) == 0 )
       
  1182             {
       
  1183             ChangeState( iResolvingTLS );
       
  1184             ResolveAddressL( KTls, iTcpAddresses, KTlsPort, *iServiceName );
       
  1185             }
       
  1186         else
       
  1187             {
       
  1188             ChangeState( iResolvingTCP );
       
  1189             ResolveAddressL( KTcp, iTcpAddresses, iServerPort, *iServiceName );
       
  1190             }
       
  1191         }
       
  1192     
       
  1193     else
       
  1194         {
       
  1195         StopTimer();
       
  1196         __STUNTURNCLIENT( "timer stopped" )
       
  1197         TimerExpiredL();
       
  1198         }
       
  1199     }
       
  1200 
       
  1201 // ---------------------------------------------------------------------------
       
  1202 // CSTUNClientImplementation::UsenameForIndication
       
  1203 // ---------------------------------------------------------------------------
       
  1204 //
       
  1205 const TDesC8& CSTUNClientImplementation::UsernameForIndication()
       
  1206     {
       
  1207     if ( !iCredentials )
       
  1208         {
       
  1209         return KNullDesC8();
       
  1210         }
       
  1211     
       
  1212     return iCredentials->Username();
       
  1213     }
       
  1214 
       
  1215 // ---------------------------------------------------------------------------
       
  1216 // CSTUNClientImplementation::UsenameForIndication
       
  1217 // ---------------------------------------------------------------------------
       
  1218 //
       
  1219 const TDesC8& CSTUNClientImplementation::PasswordForIndication()
       
  1220     {
       
  1221     if ( !iCredentials )
       
  1222         {
       
  1223         return KNullDesC8();
       
  1224         }
       
  1225     
       
  1226     return iCredentials->Password();
       
  1227     }
       
  1228 
       
  1229 // ---------------------------------------------------------------------------
       
  1230 // CSTUNClientImplementation::InformWaitingBindingsL
       
  1231 // ---------------------------------------------------------------------------
       
  1232 //
       
  1233 void CSTUNClientImplementation::InformWaitingBindingsL() const
       
  1234     {
       
  1235     __STUNTURNCLIENT( "CSTUNClientImplementation::InformWaitingBindingsL" )
       
  1236     __TEST_INVARIANT;
       
  1237 
       
  1238     for ( TInt i = 0; i < iBindings.Count(); ++i )
       
  1239         {
       
  1240         if ( iBindings[i]->Implementation().IsWaitingSharedSecret() )
       
  1241             {
       
  1242             PassSharedSecretToBindingL( *iBindings[i] );
       
  1243             }
       
  1244         }
       
  1245     __STUNTURNCLIENT( "CSTUNClientImplementation::InformWaitingBindingsL end" )
       
  1246     }
       
  1247 
       
  1248 // ---------------------------------------------------------------------------
       
  1249 // CSTUNClientImplementation::HandleSharedSecretErrorL
       
  1250 // ---------------------------------------------------------------------------
       
  1251 //
       
  1252 void CSTUNClientImplementation::HandleSharedSecretErrorL( TInt aError )
       
  1253     {
       
  1254     __STUNTURNCLIENT( "CSTUNClientImplementation::HandleSharedSecretErrorL" )
       
  1255     __TEST_INVARIANT;
       
  1256     __STUN_ASSERT_L( aError != KErrNone, KErrArgument );
       
  1257 
       
  1258      if ( aError == KErrCouldNotConnect || aError == KErrTimedOut )
       
  1259          {
       
  1260          if ( iTcpAddresses.Count() > 1 )
       
  1261              {
       
  1262              // Remove the failed address. Remain in the current state.
       
  1263              iTcpAddresses.Remove( 0 );
       
  1264              GetSharedSecretFromServerL();
       
  1265              }
       
  1266          else
       
  1267              {
       
  1268              iObtainSharedSecret = EFalse;
       
  1269              if ( !IsInitialized() )
       
  1270                 {
       
  1271                 PassInitCompletedL( KErrNone );
       
  1272                 }
       
  1273              ChangeState( iReady );
       
  1274              }
       
  1275          }
       
  1276      else
       
  1277          {
       
  1278          Terminate( aError );
       
  1279          }
       
  1280  
       
  1281      __TEST_INVARIANT;
       
  1282     __STUNTURNCLIENT( "CSTUNClientImplementation::HandleSharedSecretErrorL end" )
       
  1283     }
       
  1284 
       
  1285 // ---------------------------------------------------------------------------
       
  1286 // CSTUNClientImplementation::DoesSharedSecretMatch
       
  1287 // Username and password can be empty.
       
  1288 // ---------------------------------------------------------------------------
       
  1289 //
       
  1290 TBool CSTUNClientImplementation::DoesSharedSecretMatch(
       
  1291     const TDesC8& aUsername,
       
  1292     const TDesC8& aPassword ) const
       
  1293     {
       
  1294     __TEST_INVARIANT;
       
  1295 
       
  1296     return ( iCredentials &&
       
  1297              iCredentials->Compare( aUsername, aPassword ) ) ||
       
  1298            ( iSharedSecret &&
       
  1299              aUsername.Compare( iSharedSecret->Username() ) == 0 &&
       
  1300              aPassword.Compare( iSharedSecret->Password() ) == 0 );
       
  1301     }
       
  1302 
       
  1303 // -----------------------------------------------------------------------------
       
  1304 // CSTUNClientImplementation::RemoveAddress
       
  1305 // -----------------------------------------------------------------------------
       
  1306 //
       
  1307 void CSTUNClientImplementation::RemoveAddress( const TInetAddr& aAddress )
       
  1308     {
       
  1309     __STUNTURNCLIENT( "CSTUNClientImplementation::RemoveAddress" )
       
  1310     __TEST_INVARIANT;
       
  1311 
       
  1312     for ( TInt i = 0; i < iUdpAddresses.Count(); ++i )
       
  1313         {
       
  1314         if ( iUdpAddresses[ i ].CmpAddr( aAddress ) )
       
  1315             {
       
  1316             iUdpAddresses.Remove( i );
       
  1317             }
       
  1318         }
       
  1319 
       
  1320     __TEST_INVARIANT;
       
  1321     __STUNTURNCLIENT( "CSTUNClientImplementation::RemoveAddress end" )
       
  1322     }
       
  1323 
       
  1324 // -----------------------------------------------------------------------------
       
  1325 // CSTUNClientImplementation::__DbgTestInvariant
       
  1326 // -----------------------------------------------------------------------------
       
  1327 //
       
  1328 void CSTUNClientImplementation::__DbgTestInvariant() const
       
  1329     {
       
  1330 #if defined( _DEBUG )
       
  1331     if ( ( iState == iResolvingTCP || iState == iResolvingUDP ) &&
       
  1332          ( !iResolver || !iServerAddress ) )
       
  1333         {
       
  1334         User::Invariant();
       
  1335         }
       
  1336     if ( !iTransactionIDGenerator ||
       
  1337          !iAsyncCallback ||
       
  1338          !iResolvingTCP ||
       
  1339          !iResolvingUDP ||
       
  1340          !iGetSharedSecret ||
       
  1341          !iReady )
       
  1342         {
       
  1343         User::Invariant();
       
  1344         }
       
  1345 #endif
       
  1346     }