natfw/natfwsettings/src/cnatfwstunsettings.cpp
changeset 0 1bce908db942
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     1 /*
       
     2 * Copyright (c) 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 <unsafprotocolscrkeys.h>
       
    22 #include "natsettingslogs.h"
       
    23 #include "cnatfwstunsettings.h"
       
    24 #include "cnatfwserversettings.h" 
       
    25 #include "cnatfwcenrephandler.h"
       
    26 #include "tnatfwsettingsparser.h"
       
    27 #include "natfwdefaultsettingsvalues.h"
       
    28 
       
    29 const TUint KDefaultGranularity = 2;
       
    30 
       
    31 
       
    32 // ======== MEMBER FUNCTIONS ========
       
    33 
       
    34 CNATFWStunSettings::CNATFWStunSettings()
       
    35     {
       
    36 
       
    37     }
       
    38 
       
    39 
       
    40 void CNATFWStunSettings::ConstructL( )
       
    41     {
       
    42     iServerArray = new ( ELeave ) CArrayPtrFlat<CNATFWServerSettings>(
       
    43         KDefaultGranularity );
       
    44     iLatestConnectedServerAddr = KNullDesC8().AllocL();
       
    45     }
       
    46 
       
    47 
       
    48 CNATFWStunSettings* CNATFWStunSettings::NewL()
       
    49     {
       
    50     CNATFWStunSettings* self = CNATFWStunSettings::NewLC();
       
    51     CleanupStack::Pop( self );
       
    52     return self;
       
    53     }
       
    54 
       
    55 
       
    56 CNATFWStunSettings* CNATFWStunSettings::NewLC()
       
    57     {
       
    58     CNATFWStunSettings* self = new( ELeave ) CNATFWStunSettings;
       
    59     CleanupStack::PushL( self );
       
    60     self->ConstructL();
       
    61     return self;
       
    62     }
       
    63 
       
    64 
       
    65 CNATFWStunSettings::~CNATFWStunSettings()
       
    66     {
       
    67     if ( iServerArray )
       
    68         {
       
    69         iServerArray->ResetAndDestroy();
       
    70         }
       
    71     delete iServerArray;
       
    72     delete iLatestConnectedServerAddr;
       
    73     }
       
    74 
       
    75 
       
    76 // ---------------------------------------------------------------------------
       
    77 // CNATFWStunSettings::SetStunServersL
       
    78 // ---------------------------------------------------------------------------
       
    79 //
       
    80 void CNATFWStunSettings::SetStunServersL(
       
    81     CArrayPtr<CNATFWServerSettings>* aServers )
       
    82     {
       
    83     __ASSERT_ALWAYS( NULL != aServers, User::Leave( KErrArgument ) );
       
    84     
       
    85     iServerArray->ResetAndDestroy();
       
    86     delete iServerArray;
       
    87     iServerArray = NULL;
       
    88     iServerArray = aServers;
       
    89     }
       
    90 
       
    91 
       
    92 // ---------------------------------------------------------------------------
       
    93 // CNATFWStunSettings::SetLatestConnectedServerAddrL
       
    94 // ---------------------------------------------------------------------------
       
    95 //
       
    96 void CNATFWStunSettings::SetLatestConnectedServerAddrL(
       
    97     const TDesC8& aServerAddr )
       
    98     {
       
    99     delete iLatestConnectedServerAddr;
       
   100     iLatestConnectedServerAddr = NULL;
       
   101     iLatestConnectedServerAddr = aServerAddr.AllocL();
       
   102     }
       
   103     
       
   104 
       
   105 // ---------------------------------------------------------------------------
       
   106 // CNATFWStunSettings::SetLatestConnectedServerPort
       
   107 // ---------------------------------------------------------------------------
       
   108 //
       
   109 void CNATFWStunSettings::SetLatestConnectedServerPort( TInt aServerPort )
       
   110     {
       
   111     iLatestConnectedServerPort = aServerPort;
       
   112     }
       
   113 
       
   114 
       
   115 // ---------------------------------------------------------------------------
       
   116 // CNATFWStunSettings::SetRetransmissionTimeout
       
   117 // ---------------------------------------------------------------------------
       
   118 //
       
   119 void CNATFWStunSettings::SetRetransmissionTimeout( TUint aRto )
       
   120     {
       
   121     iRto = aRto;
       
   122     }
       
   123 
       
   124 
       
   125 // ---------------------------------------------------------------------------
       
   126 // CNATFWStunSettings::ReadSettingsL
       
   127 // ---------------------------------------------------------------------------
       
   128 //  
       
   129 void CNATFWStunSettings::ReadSettingsL( TUint32 aDomainKey,
       
   130     const CNATFWCenRepHandler& aRepHandler )
       
   131     {
       
   132     __NATSETTINGS( "CNATFWStunSettings::ReadSettingsL" )
       
   133      
       
   134     RArray<TUint32> serverKeys;
       
   135     CleanupClosePushL( serverKeys );
       
   136     
       
   137     TBool sharedSecretNotEnabled;
       
   138     TBool sharedSecretEnabled;
       
   139     
       
   140     // read sharedsecret enabled
       
   141     if ( KErrNone != aRepHandler.Read( KUNSAFProtocolsDomainSharedSecretNotSupported |
       
   142         aDomainKey, sharedSecretNotEnabled ) )
       
   143         {
       
   144         sharedSecretEnabled = KNATFWDefaultUseSharedSecret;
       
   145         }
       
   146     else
       
   147         {
       
   148         sharedSecretEnabled = !sharedSecretNotEnabled;
       
   149         }
       
   150         
       
   151     // read server settings
       
   152     TInt err = aRepHandler.FindServerKeys(
       
   153         KUNSAFProtocolsSTUNServerTableMask | aDomainKey, serverKeys );
       
   154         
       
   155     TInt serverCount = serverKeys.Count();
       
   156     
       
   157     if ( KErrNone == err && serverCount > 0 )
       
   158         {
       
   159         for( TInt i( 0 ); i < serverCount; i++ )
       
   160             {
       
   161             CNATFWServerSettings* serverSettings = CNATFWServerSettings::NewLC(
       
   162                 KNullDesC8,
       
   163                 KNATFWDefaultSTUNServerPort,
       
   164                 KNullDesC8,
       
   165                 KNullDesC8,
       
   166                 sharedSecretEnabled );
       
   167 
       
   168             TUint32 key = serverKeys[i] | aDomainKey;
       
   169             serverSettings->ReadSettingsL( key, aRepHandler );
       
   170             iServerArray->AppendL( serverSettings );
       
   171             CleanupStack::Pop( serverSettings );
       
   172             }
       
   173         }
       
   174     else
       
   175         {
       
   176         if ( KErrNotFound == err )
       
   177             {
       
   178              __NATSETTINGS( "Server settings not found" )
       
   179             }
       
   180         else
       
   181             {
       
   182             User::Leave( err );
       
   183             }
       
   184         }
       
   185     CleanupStack::PopAndDestroy( &serverKeys );
       
   186     
       
   187     // read LatestConnectedStunServerAddress and possible port
       
   188     TInt port = 0;
       
   189     HBufC8* tempBufPointer = NULL;
       
   190     
       
   191     tempBufPointer = aRepHandler.ReadL(
       
   192         KUNSAFProtocolsLatestConnectedSTUNServerAddressMask | aDomainKey );
       
   193     if ( tempBufPointer ) 
       
   194         {
       
   195         CleanupStack::PushL( tempBufPointer );
       
   196         HBufC8* addr = TNATFWSettingsParser::ParseAddressLC(
       
   197             *tempBufPointer, port );
       
   198         SetLatestConnectedServerAddrL( *addr );
       
   199         SetLatestConnectedServerPort( port );
       
   200         CleanupStack::PopAndDestroy( addr );
       
   201         CleanupStack::PopAndDestroy( tempBufPointer );
       
   202         }
       
   203     else
       
   204         {
       
   205         SetLatestConnectedServerAddrL( KNullDesC8 );
       
   206         SetLatestConnectedServerPort( port );
       
   207         }
       
   208     
       
   209     // Read LatestConnectedStunServerPort, replaces previous data if found
       
   210     if ( KErrNone == aRepHandler.Read( KUNSAFProtocolsLatestConnectedSTUNServerPortMask |
       
   211         aDomainKey, port ) )
       
   212         {
       
   213         SetLatestConnectedServerPort( port );
       
   214         }
       
   215     
       
   216     // read retransmission timeout
       
   217     if ( KErrNone != aRepHandler.Read( KUNSAFProtocolsStunRetransmissionTimeoutMask |
       
   218         aDomainKey, iRto ) )
       
   219         {
       
   220         iRto = KNATFWDefaultRtoValue;
       
   221         }
       
   222     }
       
   223 
       
   224 
       
   225 // ---------------------------------------------------------------------------
       
   226 // From class MNATFWStunSettings.
       
   227 // CNATFWStunSettings::GetStunServerArrayL
       
   228 // ---------------------------------------------------------------------------
       
   229 //
       
   230 void CNATFWStunSettings::GetStunServerArrayL(
       
   231     RPointerArray<MNATFWServerSettings>& aServers ) const
       
   232     {
       
   233     TInt count = iServerArray->Count();
       
   234     
       
   235     for ( TInt i( 0 ); i < count; i++ )
       
   236         {
       
   237         aServers.AppendL( iServerArray->At( i ) );
       
   238         }
       
   239     }
       
   240 
       
   241 
       
   242 // ---------------------------------------------------------------------------
       
   243 // From class MNATFWStunSettings.
       
   244 // CNATFWStunSettings::RetransmissionTimeout
       
   245 // ---------------------------------------------------------------------------
       
   246 //
       
   247 TUint CNATFWStunSettings::RetransmissionTimeout() const
       
   248     {
       
   249     return iRto;
       
   250     }
       
   251  
       
   252     
       
   253 // ---------------------------------------------------------------------------
       
   254 // From class MNATFWStunSettings.
       
   255 // CNATFWStunSettings::LatestConnectedServerAddr
       
   256 // ---------------------------------------------------------------------------
       
   257 //
       
   258  const TDesC8& CNATFWStunSettings::LatestConnectedServerAddr() const
       
   259     {
       
   260     return *iLatestConnectedServerAddr;
       
   261     }
       
   262 
       
   263 
       
   264 // ---------------------------------------------------------------------------
       
   265 // From class MNATFWStunSettings.
       
   266 // CNATFWStunSettings::LatestConnectedServerPort
       
   267 // ---------------------------------------------------------------------------
       
   268 //
       
   269 TInt CNATFWStunSettings::LatestConnectedServerPort() const
       
   270     {
       
   271     return iLatestConnectedServerPort;
       
   272     }