voipplugins/voipadapters/voipxmlprovisioning/voipxmlprocessor/src/voipxmlnatfwhandler.cpp
changeset 2 7b872347d83b
child 8 7117cbf1600a
equal deleted inserted replaced
1:bfb1de3eac8e 2:7b872347d83b
       
     1 /*
       
     2 * Copyright (c) 2009-2010 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:  NAT/Firewall handler for VoIP XML processor
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <centralrepository.h>
       
    20 #include <unsafprotocolsinternalcrkeys.h>
       
    21 
       
    22 #include "voipxmlutils.h"
       
    23 #include "voipxmlnatfwhandler.h"
       
    24 #include "voipxmlprocessorlogger.h"
       
    25 //#include "voipxmlprocessordefaults.h"
       
    26 
       
    27 // ---------------------------------------------------------------------------
       
    28 // CVoipXmlNatFwHandler::CVoipXmlNatFwHandler
       
    29 // ---------------------------------------------------------------------------
       
    30 //
       
    31 CVoipXmlNatFwHandler::CVoipXmlNatFwHandler()
       
    32     {
       
    33     }
       
    34 
       
    35 // ---------------------------------------------------------------------------
       
    36 // CVoipXmlNatFwHandler::NewL
       
    37 // ---------------------------------------------------------------------------
       
    38 //
       
    39 CVoipXmlNatFwHandler* CVoipXmlNatFwHandler::NewL()
       
    40     {
       
    41     CVoipXmlNatFwHandler* self = new ( ELeave ) CVoipXmlNatFwHandler;
       
    42     CleanupStack::PushL( self );
       
    43     self->ConstructL();
       
    44     CleanupStack::Pop( self );
       
    45     return self;
       
    46     }
       
    47 
       
    48 // ---------------------------------------------------------------------------
       
    49 // CVoipXmlNatFwHandler::ConstructL
       
    50 // ---------------------------------------------------------------------------
       
    51 //
       
    52 void CVoipXmlNatFwHandler::ConstructL()
       
    53     {
       
    54     iDomain             = HBufC8::NewL( KMaxNodeValueLength );
       
    55     iStunSrvAddr        = HBufC8::NewL( KMaxNodeValueLength );
       
    56     iStunSrvUsername    = HBufC8::NewL( KMaxNodeValueLength );
       
    57     iStunSrvPassword    = HBufC8::NewL( KMaxNodeValueLength );
       
    58     iNatProtocol        = HBufC8::NewL( KMaxNodeValueLength );
       
    59     iNatProtocol->Des().Copy( KDefaultNatProtocol );
       
    60     iStunSrvPort        = KDefaultStunServerPort;
       
    61     iTcpRefreshInterval = KDefaultTcpRefreshInterval;
       
    62     iUdpRefreshInterval = KDefaultUdpRefreshInterval;
       
    63     iStartPortRange     = KDefaultStartPortRange;
       
    64     iEndPortRange       = KDefaultEndPortRange;
       
    65     iCurrentAdditionalStunServer.iStunSrvPort = KDefaultStunServerPort;
       
    66     }
       
    67 
       
    68 // ---------------------------------------------------------------------------
       
    69 // CVoipXmlNatFwHandler::~CVoipXmlNatFwHandler
       
    70 // ---------------------------------------------------------------------------
       
    71 //
       
    72 CVoipXmlNatFwHandler::~CVoipXmlNatFwHandler()
       
    73     {
       
    74     delete iDomain;
       
    75     delete iStunSrvAddr;
       
    76     delete iStunSrvUsername;
       
    77     delete iStunSrvPassword;
       
    78     delete iNatProtocol;
       
    79     iAdditionalStunServers.ResetAndDestroy();
       
    80     iAdditionalStunServers.Close();
       
    81     }
       
    82 
       
    83 // ---------------------------------------------------------------------------
       
    84 // Sets a NAT/Firewall setting.
       
    85 // ---------------------------------------------------------------------------
       
    86 //
       
    87 void CVoipXmlNatFwHandler::SetSetting( TInt aType, TInt aParam,
       
    88     const TDesC8& aValue )
       
    89     {
       
    90     // Ignore too long descriptors.
       
    91     if ( KMaxNodeValueLength < aValue.Length() )
       
    92         {
       
    93         return;
       
    94         }
       
    95 
       
    96     TInt intVal( KErrNotFound );
       
    97     switch ( aParam )
       
    98         {
       
    99         case EDomain:
       
   100             {
       
   101             iDomain->Des().Copy( aValue );
       
   102             iSettingsSet = ETrue;
       
   103             break;
       
   104             }
       
   105         case EUri:
       
   106             {
       
   107             if ( ENatFw == aType )
       
   108                 {
       
   109                 iStunSrvAddr->Des().Copy( aValue );
       
   110                 iSettingsSet = ETrue;
       
   111                 }
       
   112             else if ( EAdditionalStun == aType )
       
   113                 {
       
   114                 iCurrentAdditionalStunServer.iStunSrvAddr.Copy( aValue );
       
   115                 iSettingsSet = ETrue;
       
   116                 }
       
   117             break;
       
   118             }
       
   119         case EPort:
       
   120             {
       
   121             if ( ENatFw == aType && KErrNone == VoipXmlUtils::Des8ToInt( 
       
   122                 aValue, intVal ) )
       
   123                 {
       
   124                 iStunSrvPort = intVal;
       
   125                 iSettingsSet = ETrue;
       
   126                 }
       
   127             else if ( EAdditionalStun == aType && 
       
   128                 KErrNone == VoipXmlUtils::Des8ToInt( aValue, intVal ) )
       
   129                 {
       
   130                 iCurrentAdditionalStunServer.iStunSrvPort = intVal;
       
   131                 iSettingsSet = ETrue;
       
   132                 }
       
   133             break;
       
   134             }
       
   135         case ETcpRefreshInterval:
       
   136             {
       
   137             if ( KErrNone == VoipXmlUtils::Des8ToInt( aValue, intVal ) )
       
   138                 {
       
   139                 iTcpRefreshInterval = intVal;
       
   140                 iSettingsSet = ETrue;
       
   141                 }
       
   142             break;
       
   143             }
       
   144         case EUdpRefreshInterval:
       
   145             {
       
   146             if ( KErrNone == VoipXmlUtils::Des8ToInt( aValue, intVal ) )
       
   147                 {
       
   148                 iUdpRefreshInterval = intVal;
       
   149                 iSettingsSet = ETrue;
       
   150                 }
       
   151             break;
       
   152             }
       
   153         case ECrlfRefresh:
       
   154             {
       
   155             if ( KErrNone == VoipXmlUtils::Des8ToInt( aValue, intVal ) )
       
   156                 {
       
   157                 iCrlfRefresh = intVal;
       
   158                 iSettingsSet = ETrue;
       
   159                 }
       
   160             break;
       
   161             }
       
   162         case EUsername:
       
   163             {
       
   164             if ( ENatFw == aType )
       
   165                 {
       
   166                 iStunSrvUsername->Des().Copy( aValue );
       
   167                 iSettingsSet = ETrue;
       
   168                 }
       
   169             else if ( EAdditionalStun == aType )
       
   170                 {
       
   171                 iCurrentAdditionalStunServer.iStunSrvUsername.Copy( aValue );
       
   172                 iSettingsSet = ETrue;
       
   173                 }
       
   174             break;
       
   175             }
       
   176         case EPassword:
       
   177             {
       
   178             if ( ENatFw == aType )
       
   179                 {
       
   180                 iStunSrvPassword->Des().Copy( aValue );
       
   181                 iSettingsSet = ETrue;
       
   182                 }
       
   183             else if ( EAdditionalStun == aType )
       
   184                 {
       
   185                 iCurrentAdditionalStunServer.iStunSrvPassword.Copy( aValue );
       
   186                 iSettingsSet = ETrue;
       
   187                 }
       
   188             break;
       
   189             }
       
   190         case EStunSharedSecret:
       
   191             {
       
   192             if ( KErrNone == VoipXmlUtils::Des8ToInt( aValue, intVal ) )
       
   193                 {
       
   194                 iStunSharedSecret = intVal;
       
   195                 iSettingsSet = ETrue;
       
   196                 }
       
   197             break;
       
   198             }
       
   199         case EStartPort:
       
   200             {
       
   201             if ( KErrNone == VoipXmlUtils::Des8ToInt( aValue, intVal ) )
       
   202                 {
       
   203                 iStartPortRange = intVal;
       
   204                 iSettingsSet = ETrue;
       
   205                 }
       
   206             break;
       
   207             }
       
   208         case EEndPort:
       
   209             {
       
   210             if ( KErrNone == VoipXmlUtils::Des8ToInt( aValue, intVal ) )
       
   211                 {
       
   212                 iEndPortRange = intVal;
       
   213                 iSettingsSet = ETrue;
       
   214                 }
       
   215             break;
       
   216             }
       
   217         case EType:
       
   218             {
       
   219             iNatProtocol->Des().Copy( aValue );
       
   220             break;
       
   221             }
       
   222         default:
       
   223             break;
       
   224         }
       
   225     }
       
   226 
       
   227 // ---------------------------------------------------------------------------
       
   228 // Stores settings to Central Repository.
       
   229 // ---------------------------------------------------------------------------
       
   230 //
       
   231 TInt CVoipXmlNatFwHandler::StoreSettings()
       
   232     {
       
   233     if ( !iSettingsSet )
       
   234         {
       
   235         // No settings to be stored => method not supported.
       
   236         return KErrNotSupported;
       
   237         }
       
   238     TRAPD( err, StoreSettingsL() );
       
   239     if ( KErrNone != err )
       
   240         {
       
   241         err = KErrCompletion;
       
   242         }
       
   243     return err;
       
   244     }
       
   245 
       
   246 // ---------------------------------------------------------------------------
       
   247 // Appends currently modified additional STUN server to internal array.
       
   248 // ---------------------------------------------------------------------------
       
   249 //
       
   250 void CVoipXmlNatFwHandler::SettingsEnd( TInt aType )
       
   251     {
       
   252     if ( EAdditionalStun == aType )
       
   253         {
       
   254         TAdditionalStun* temp = new TAdditionalStun;
       
   255         temp->iStunSrvAddr.Copy( iCurrentAdditionalStunServer.iStunSrvAddr );
       
   256         temp->iStunSrvPort = iCurrentAdditionalStunServer.iStunSrvPort;
       
   257         temp->iStunSrvUsername.Copy( 
       
   258             iCurrentAdditionalStunServer.iStunSrvUsername );
       
   259         temp->iStunSrvPassword.Copy( 
       
   260             iCurrentAdditionalStunServer.iStunSrvPassword );
       
   261         iAdditionalStunServers.Append( temp );
       
   262         iCurrentAdditionalStunServer.iStunSrvAddr.Copy( KNullDesC8 );
       
   263         iCurrentAdditionalStunServer.iStunSrvPort = KDefaultStunServerPort;
       
   264         iCurrentAdditionalStunServer.iStunSrvUsername.Copy( KNullDesC8 );
       
   265         iCurrentAdditionalStunServer.iStunSrvPassword.Copy( KNullDesC8 );
       
   266         }
       
   267     }
       
   268 
       
   269 // ---------------------------------------------------------------------------
       
   270 // Commits Central Repository storage.
       
   271 // ---------------------------------------------------------------------------
       
   272 //
       
   273 void CVoipXmlNatFwHandler::StoreSettingsL()
       
   274     {
       
   275     CRepository* rep = CRepository::NewLC( KCRUidUNSAFProtocols ); // CS:1
       
   276 
       
   277     RArray<TUint32> keys;
       
   278     CleanupClosePushL( keys ); // CS:2
       
   279 
       
   280     // Get next free Domain key.
       
   281     rep->FindL( KUNSAFProtocolsDomainMask, 
       
   282         KUNSAFProtocolsFieldTypeMask, keys );
       
   283     TInt keyCount = keys.Count();
       
   284     TInt tmp = 0;
       
   285     if ( !keyCount )
       
   286         {
       
   287         tmp = KUNSAFProtocolsDomainTableMask;
       
   288         }
       
   289     else
       
   290         {
       
   291         tmp = keys[keyCount - 1] + 1;
       
   292         }
       
   293 
       
   294     rep->FindEqL( KUNSAFProtocolsDomainMask, KUNSAFProtocolsFieldTypeMask,
       
   295         iDomain->Des(), keys );
       
   296     if ( keys.Count() )
       
   297         {
       
   298         tmp = keys[0];
       
   299         }
       
   300 
       
   301     TUint32 currentKey = tmp|KUNSAFProtocolsDomainMask;
       
   302     currentKey &= KUNSAFProtocolsKeyMask;
       
   303 
       
   304     TUint32 currentDomainKey = tmp|KUNSAFProtocolsFieldTypeMask;
       
   305     currentDomainKey ^= KUNSAFProtocolsFieldTypeMask;
       
   306 
       
   307     TUint32 stunKey = KUNSAFProtocolsSubTableFieldTypeMask;
       
   308     stunKey ^= KUNSAFProtocolsSubTableFieldTypeMask;
       
   309     stunKey |= currentDomainKey;
       
   310 
       
   311     // Delete all existing additional STUN servers if there are any.
       
   312     RArray<TUint32> stunKeys;
       
   313     CleanupClosePushL( stunKeys ); // CS:3
       
   314     TInt err = rep->FindL( 
       
   315         currentDomainKey|KUNSAFProtocolsSTUNAddressMask, 
       
   316         KUNSAFProtocolsSubTableFieldTypeMask, stunKeys );
       
   317     const TInt stunKeyCount = stunKeys.Count();
       
   318     for ( TInt counter = 0 ; counter < stunKeyCount; counter++ )
       
   319         {
       
   320         TUint32 key = KUNSAFProtocolsSTUNAddressMask^
       
   321             stunKeys[counter];
       
   322         rep->Delete( key|KUNSAFProtocolsSTUNAddressMask );
       
   323         rep->Delete( key|KUNSAFProtocolsSTUNPortMask );
       
   324         rep->Delete( key|KUNSAFProtocolsSTUNUsernameMask );
       
   325         rep->Delete( key|KUNSAFProtocolsSTUNPasswordMask );
       
   326         }
       
   327     CleanupStack::PopAndDestroy( &stunKeys ); // CS:2
       
   328 
       
   329     // Set new keys.
       
   330 
       
   331     // Domain
       
   332     User::LeaveIfError( rep->Set( currentKey|KUNSAFProtocolsDomainMask,
       
   333         iDomain->Des() ));
       
   334 
       
   335     // STUN server address.
       
   336     User::LeaveIfError( rep->Set( 
       
   337         KUNSAFProtocolsSTUNServerMask|currentDomainKey, 
       
   338         iStunSrvAddr->Des() ) );
       
   339     // Set the same value into STUN server table.
       
   340     User::LeaveIfError( rep->Set( 
       
   341         KUNSAFProtocolsSTUNAddressMask|stunKey,
       
   342         iStunSrvAddr->Des() ) );
       
   343 
       
   344     // STUN server port.
       
   345     User::LeaveIfError( rep->Set( 
       
   346         KUNSAFProtocolsSTUNServerPortMask|currentDomainKey, 
       
   347         iStunSrvPort ) );
       
   348     // Set the same value into STUN server table.
       
   349     User::LeaveIfError( rep->Set( 
       
   350         KUNSAFProtocolsSTUNPortMask|stunKey,
       
   351         iStunSrvPort ) );
       
   352 
       
   353     // TCP refresh interval.
       
   354     User::LeaveIfError( rep->Set( 
       
   355         KUNSAFProtocolsDomainIntervalTCPMask|currentDomainKey, 
       
   356         iTcpRefreshInterval ) );
       
   357 
       
   358     // UDP refresh interval.
       
   359     User::LeaveIfError( rep->Set( 
       
   360         KUNSAFProtocolsDomainIntervalUDPMask|currentDomainKey, 
       
   361         iUdpRefreshInterval ) );
       
   362 
       
   363     // CRLF refresh.
       
   364     User::LeaveIfError( rep->Set( 
       
   365         KUNSAFProtocolsDomainEnableCRLFRefresh|currentDomainKey,
       
   366         iCrlfRefresh ) );
       
   367 
       
   368     // STUN server username
       
   369     User::LeaveIfError( rep->Set( 
       
   370         KUNSAFProtocolsSTUNUsernameMask|stunKey, 
       
   371         iStunSrvUsername->Des() ) );
       
   372 
       
   373     // STUN server password
       
   374     User::LeaveIfError( rep->Set( 
       
   375         KUNSAFProtocolsSTUNPasswordMask|stunKey, 
       
   376         iStunSrvPassword->Des() ) );
       
   377 
       
   378     // STUN shared secret
       
   379     User::LeaveIfError( rep->Set( currentDomainKey|
       
   380         KUNSAFProtocolsDomainSharedSecretNotSupported,
       
   381         !iStunSharedSecret ) );
       
   382 
       
   383     // Start port range
       
   384     User::LeaveIfError( rep->Set( 
       
   385         currentDomainKey|KUNSAFProtocolsPortPoolStartPortMask,
       
   386         iStartPortRange ) );
       
   387 
       
   388     // End port range.
       
   389     User::LeaveIfError( rep->Set( 
       
   390         currentDomainKey|KUNSAFProtocolsPortPoolEndPortMask,
       
   391         iEndPortRange ) );
       
   392 
       
   393     // Used NAT protocol.
       
   394     User::LeaveIfError( rep->Set(
       
   395         currentDomainKey|KUNSAFProtocolsUsedNATProtocolMask,
       
   396         iNatProtocol->Des() ) );
       
   397 
       
   398     // ==============================
       
   399     // Additional STUN servers
       
   400     // ==============================
       
   401     //
       
   402     const TInt count = iAdditionalStunServers.Count();
       
   403     for ( TInt counter = 0; counter < count; counter++ )
       
   404         {
       
   405         stunKey |= KUNSAFProtocolsSubTableFieldTypeMask;
       
   406         stunKey++;
       
   407         stunKey |= KUNSAFProtocolsSubTableFieldTypeMask;
       
   408         stunKey ^= KUNSAFProtocolsSubTableFieldTypeMask;
       
   409         stunKey |= currentDomainKey;
       
   410 
       
   411         // STUNServerAddress
       
   412         User::LeaveIfError( rep->Create( 
       
   413             KUNSAFProtocolsSTUNAddressMask|stunKey,
       
   414             iAdditionalStunServers[counter]->iStunSrvAddr ) );
       
   415 
       
   416         // STUNServerPort
       
   417         User::LeaveIfError( rep->Create( 
       
   418             KUNSAFProtocolsSTUNPortMask|stunKey,
       
   419             iAdditionalStunServers[counter]->iStunSrvPort ) );
       
   420 
       
   421         // STUNServerUsername
       
   422         User::LeaveIfError( rep->Create( 
       
   423             KUNSAFProtocolsSTUNUsernameMask|stunKey,
       
   424             iAdditionalStunServers[counter]->iStunSrvUsername ) );
       
   425 
       
   426         // STUNServerPassword
       
   427         User::LeaveIfError( rep->Create( 
       
   428             KUNSAFProtocolsSTUNPasswordMask|stunKey,
       
   429             iAdditionalStunServers[counter]->iStunSrvPassword ) );
       
   430         }
       
   431 
       
   432     // &keys, rep
       
   433     CleanupStack::PopAndDestroy( 2, rep ); // CS:0
       
   434     }
       
   435 
       
   436 // End of file.