voipplugins/voipadapters/voipxmlprovisioning/voipxmlprocessor/src/voipxmlnatfwhandler.cpp
branchRCL_3
changeset 21 f742655b05bf
parent 20 65a3ef1d5bd0
child 22 d38647835c2e
equal deleted inserted replaced
20:65a3ef1d5bd0 21:f742655b05bf
     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             //lint -e{960} No need for else statement here
       
   108             if ( ENatFw == aType )
       
   109                 {
       
   110                 iStunSrvAddr->Des().Copy( aValue );
       
   111                 iSettingsSet = ETrue;
       
   112                 }
       
   113             else if ( EAdditionalStun == aType )
       
   114                 {
       
   115                 iCurrentAdditionalStunServer.iStunSrvAddr.Copy( aValue );
       
   116                 iSettingsSet = ETrue;
       
   117                 }
       
   118             break;
       
   119             }
       
   120         case EPort:
       
   121             {
       
   122             //lint -e{960} No need for else statement here
       
   123             if ( ENatFw == aType && KErrNone == VoipXmlUtils::Des8ToInt( 
       
   124                 aValue, intVal ) )
       
   125                 {
       
   126                 iStunSrvPort = intVal;
       
   127                 iSettingsSet = ETrue;
       
   128                 }
       
   129             else if ( EAdditionalStun == aType && 
       
   130                 KErrNone == VoipXmlUtils::Des8ToInt( aValue, intVal ) )
       
   131                 {
       
   132                 iCurrentAdditionalStunServer.iStunSrvPort = intVal;
       
   133                 iSettingsSet = ETrue;
       
   134                 }
       
   135             break;
       
   136             }
       
   137         case ETcpRefreshInterval:
       
   138             {
       
   139             if ( KErrNone == VoipXmlUtils::Des8ToInt( aValue, intVal ) )
       
   140                 {
       
   141                 iTcpRefreshInterval = intVal;
       
   142                 iSettingsSet = ETrue;
       
   143                 }
       
   144             break;
       
   145             }
       
   146         case EUdpRefreshInterval:
       
   147             {
       
   148             if ( KErrNone == VoipXmlUtils::Des8ToInt( aValue, intVal ) )
       
   149                 {
       
   150                 iUdpRefreshInterval = intVal;
       
   151                 iSettingsSet = ETrue;
       
   152                 }
       
   153             break;
       
   154             }
       
   155         case ECrlfRefresh:
       
   156             {
       
   157             if ( KErrNone == VoipXmlUtils::Des8ToInt( aValue, intVal ) )
       
   158                 {
       
   159                 iCrlfRefresh = intVal;
       
   160                 iSettingsSet = ETrue;
       
   161                 }
       
   162             break;
       
   163             }
       
   164         case EUsername:
       
   165             {
       
   166             //lint -e{960} No need for else statement here
       
   167             if ( ENatFw == aType )
       
   168                 {
       
   169                 iStunSrvUsername->Des().Copy( aValue );
       
   170                 iSettingsSet = ETrue;
       
   171                 }
       
   172             else if ( EAdditionalStun == aType )
       
   173                 {
       
   174                 iCurrentAdditionalStunServer.iStunSrvUsername.Copy( aValue );
       
   175                 iSettingsSet = ETrue;
       
   176                 }
       
   177             break;
       
   178             }
       
   179         case EPassword:
       
   180             {
       
   181             //lint -e{960} No need for else statement here
       
   182             if ( ENatFw == aType )
       
   183                 {
       
   184                 iStunSrvPassword->Des().Copy( aValue );
       
   185                 iSettingsSet = ETrue;
       
   186                 }
       
   187             else if ( EAdditionalStun == aType )
       
   188                 {
       
   189                 iCurrentAdditionalStunServer.iStunSrvPassword.Copy( aValue );
       
   190                 iSettingsSet = ETrue;
       
   191                 }
       
   192             break;
       
   193             }
       
   194         case EStunSharedSecret:
       
   195             {
       
   196             if ( KErrNone == VoipXmlUtils::Des8ToInt( aValue, intVal ) )
       
   197                 {
       
   198                 iStunSharedSecret = intVal;
       
   199                 iSettingsSet = ETrue;
       
   200                 }
       
   201             break;
       
   202             }
       
   203         case EStartPort:
       
   204             {
       
   205             if ( KErrNone == VoipXmlUtils::Des8ToInt( aValue, intVal ) )
       
   206                 {
       
   207                 iStartPortRange = intVal;
       
   208                 iSettingsSet = ETrue;
       
   209                 }
       
   210             break;
       
   211             }
       
   212         case EEndPort:
       
   213             {
       
   214             if ( KErrNone == VoipXmlUtils::Des8ToInt( aValue, intVal ) )
       
   215                 {
       
   216                 iEndPortRange = intVal;
       
   217                 iSettingsSet = ETrue;
       
   218                 }
       
   219             break;
       
   220             }
       
   221         case EType:
       
   222             {
       
   223             iNatProtocol->Des().Copy( aValue );
       
   224             break;
       
   225             }
       
   226         default:
       
   227             break;
       
   228         }
       
   229     }
       
   230 
       
   231 // ---------------------------------------------------------------------------
       
   232 // Stores settings to Central Repository.
       
   233 // ---------------------------------------------------------------------------
       
   234 //
       
   235 TInt CVoipXmlNatFwHandler::StoreSettings()
       
   236     {
       
   237     if ( !iSettingsSet )
       
   238         {
       
   239         // No settings to be stored => method not supported.
       
   240         return KErrNotSupported;
       
   241         }
       
   242     TRAPD( err, StoreSettingsL() );
       
   243     if ( KErrNone != err )
       
   244         {
       
   245         err = KErrCompletion;
       
   246         }
       
   247     return err;
       
   248     }
       
   249 
       
   250 // ---------------------------------------------------------------------------
       
   251 // Appends currently modified additional STUN server to internal array.
       
   252 // ---------------------------------------------------------------------------
       
   253 //
       
   254 void CVoipXmlNatFwHandler::SettingsEnd( TInt aType )
       
   255     {
       
   256     if ( EAdditionalStun == aType )
       
   257         {
       
   258         TAdditionalStun* temp = new TAdditionalStun;
       
   259         temp->iStunSrvAddr.Copy( iCurrentAdditionalStunServer.iStunSrvAddr );
       
   260         temp->iStunSrvPort = iCurrentAdditionalStunServer.iStunSrvPort;
       
   261         temp->iStunSrvUsername.Copy( 
       
   262             iCurrentAdditionalStunServer.iStunSrvUsername );
       
   263         temp->iStunSrvPassword.Copy( 
       
   264             iCurrentAdditionalStunServer.iStunSrvPassword );
       
   265         iAdditionalStunServers.Append( temp );
       
   266         iCurrentAdditionalStunServer.iStunSrvAddr.Copy( KNullDesC8 );
       
   267         iCurrentAdditionalStunServer.iStunSrvPort = KDefaultStunServerPort;
       
   268         iCurrentAdditionalStunServer.iStunSrvUsername.Copy( KNullDesC8 );
       
   269         iCurrentAdditionalStunServer.iStunSrvPassword.Copy( KNullDesC8 );
       
   270         }
       
   271     }
       
   272 
       
   273 // ---------------------------------------------------------------------------
       
   274 // Commits Central Repository storage.
       
   275 // ---------------------------------------------------------------------------
       
   276 //
       
   277 void CVoipXmlNatFwHandler::StoreSettingsL()
       
   278     {
       
   279     CRepository* rep = CRepository::NewLC( KCRUidUNSAFProtocols ); // CS:1
       
   280 
       
   281     RArray<TUint32> keys;
       
   282     CleanupClosePushL( keys ); // CS:2
       
   283 
       
   284     // Get next free Domain key.
       
   285     rep->FindL( KUNSAFProtocolsDomainMask, 
       
   286         KUNSAFProtocolsFieldTypeMask, keys );
       
   287     TInt keyCount = keys.Count();
       
   288     TInt tmp = 0;
       
   289     if ( !keyCount )
       
   290         {
       
   291         tmp = KUNSAFProtocolsDomainTableMask;
       
   292         }
       
   293     else
       
   294         {
       
   295         tmp = keys[keyCount - 1] + 1;
       
   296         }
       
   297 
       
   298     rep->FindEqL( KUNSAFProtocolsDomainMask, KUNSAFProtocolsFieldTypeMask,
       
   299         iDomain->Des(), keys );
       
   300     if ( keys.Count() )
       
   301         {
       
   302         tmp = keys[0];
       
   303         }
       
   304 
       
   305     TUint32 currentKey = tmp|KUNSAFProtocolsDomainMask;
       
   306     currentKey &= KUNSAFProtocolsKeyMask;
       
   307 
       
   308     TUint32 currentDomainKey = tmp|KUNSAFProtocolsFieldTypeMask;
       
   309     currentDomainKey ^= KUNSAFProtocolsFieldTypeMask;
       
   310 
       
   311     TUint32 stunKey = KUNSAFProtocolsSubTableFieldTypeMask;
       
   312     stunKey ^= KUNSAFProtocolsSubTableFieldTypeMask;
       
   313     stunKey |= currentDomainKey;
       
   314 
       
   315     // Delete all existing additional STUN servers if there are any.
       
   316     RArray<TUint32> stunKeys;
       
   317     CleanupClosePushL( stunKeys ); // CS:3
       
   318     TInt err = rep->FindL( 
       
   319         currentDomainKey|KUNSAFProtocolsSTUNAddressMask, 
       
   320         KUNSAFProtocolsSubTableFieldTypeMask, stunKeys );
       
   321     const TInt stunKeyCount = stunKeys.Count();
       
   322     for ( TInt counter = 0 ; counter < stunKeyCount; counter++ )
       
   323         {
       
   324         TUint32 key = KUNSAFProtocolsSTUNAddressMask^
       
   325             stunKeys[counter];
       
   326         rep->Delete( key|KUNSAFProtocolsSTUNAddressMask );
       
   327         rep->Delete( key|KUNSAFProtocolsSTUNPortMask );
       
   328         rep->Delete( key|KUNSAFProtocolsSTUNUsernameMask );
       
   329         rep->Delete( key|KUNSAFProtocolsSTUNPasswordMask );
       
   330         }
       
   331     CleanupStack::PopAndDestroy( &stunKeys ); // CS:2
       
   332 
       
   333     // Set new keys.
       
   334 
       
   335     // Domain
       
   336     User::LeaveIfError( rep->Set( currentKey|KUNSAFProtocolsDomainMask,
       
   337         iDomain->Des() ));
       
   338 
       
   339     // STUN server address.
       
   340     User::LeaveIfError( rep->Set( 
       
   341         KUNSAFProtocolsSTUNServerMask|currentDomainKey, 
       
   342         iStunSrvAddr->Des() ) );
       
   343     // Set the same value into STUN server table.
       
   344     User::LeaveIfError( rep->Set( 
       
   345         KUNSAFProtocolsSTUNAddressMask|stunKey,
       
   346         iStunSrvAddr->Des() ) );
       
   347 
       
   348     // STUN server port.
       
   349     User::LeaveIfError( rep->Set( 
       
   350         KUNSAFProtocolsSTUNServerPortMask|currentDomainKey, 
       
   351         iStunSrvPort ) );
       
   352     // Set the same value into STUN server table.
       
   353     User::LeaveIfError( rep->Set( 
       
   354         KUNSAFProtocolsSTUNPortMask|stunKey,
       
   355         iStunSrvPort ) );
       
   356 
       
   357     // TCP refresh interval.
       
   358     User::LeaveIfError( rep->Set( 
       
   359         KUNSAFProtocolsDomainIntervalTCPMask|currentDomainKey, 
       
   360         iTcpRefreshInterval ) );
       
   361 
       
   362     // UDP refresh interval.
       
   363     User::LeaveIfError( rep->Set( 
       
   364         KUNSAFProtocolsDomainIntervalUDPMask|currentDomainKey, 
       
   365         iUdpRefreshInterval ) );
       
   366 
       
   367     // CRLF refresh.
       
   368     User::LeaveIfError( rep->Set( 
       
   369         KUNSAFProtocolsDomainEnableCRLFRefresh|currentDomainKey,
       
   370         iCrlfRefresh ) );
       
   371 
       
   372     // STUN server username
       
   373     User::LeaveIfError( rep->Set( 
       
   374         KUNSAFProtocolsSTUNUsernameMask|stunKey, 
       
   375         iStunSrvUsername->Des() ) );
       
   376 
       
   377     // STUN server password
       
   378     User::LeaveIfError( rep->Set( 
       
   379         KUNSAFProtocolsSTUNPasswordMask|stunKey, 
       
   380         iStunSrvPassword->Des() ) );
       
   381 
       
   382     // STUN shared secret
       
   383     User::LeaveIfError( rep->Set( currentDomainKey|
       
   384         KUNSAFProtocolsDomainSharedSecretNotSupported,
       
   385         !iStunSharedSecret ) );
       
   386 
       
   387     // Start port range
       
   388     User::LeaveIfError( rep->Set( 
       
   389         currentDomainKey|KUNSAFProtocolsPortPoolStartPortMask,
       
   390         iStartPortRange ) );
       
   391 
       
   392     // End port range.
       
   393     User::LeaveIfError( rep->Set( 
       
   394         currentDomainKey|KUNSAFProtocolsPortPoolEndPortMask,
       
   395         iEndPortRange ) );
       
   396 
       
   397     // Used NAT protocol.
       
   398     User::LeaveIfError( rep->Set(
       
   399         currentDomainKey|KUNSAFProtocolsUsedNATProtocolMask,
       
   400         iNatProtocol->Des() ) );
       
   401 
       
   402     // ==============================
       
   403     // Additional STUN servers
       
   404     // ==============================
       
   405     //
       
   406     const TInt count = iAdditionalStunServers.Count();
       
   407     for ( TInt counter = 0; counter < count; counter++ )
       
   408         {
       
   409         stunKey |= KUNSAFProtocolsSubTableFieldTypeMask;
       
   410         stunKey++;
       
   411         stunKey |= KUNSAFProtocolsSubTableFieldTypeMask;
       
   412         stunKey ^= KUNSAFProtocolsSubTableFieldTypeMask;
       
   413         stunKey |= currentDomainKey;
       
   414 
       
   415         // STUNServerAddress
       
   416         User::LeaveIfError( rep->Create( 
       
   417             KUNSAFProtocolsSTUNAddressMask|stunKey,
       
   418             iAdditionalStunServers[counter]->iStunSrvAddr ) );
       
   419 
       
   420         // STUNServerPort
       
   421         User::LeaveIfError( rep->Create( 
       
   422             KUNSAFProtocolsSTUNPortMask|stunKey,
       
   423             iAdditionalStunServers[counter]->iStunSrvPort ) );
       
   424 
       
   425         // STUNServerUsername
       
   426         User::LeaveIfError( rep->Create( 
       
   427             KUNSAFProtocolsSTUNUsernameMask|stunKey,
       
   428             iAdditionalStunServers[counter]->iStunSrvUsername ) );
       
   429 
       
   430         // STUNServerPassword
       
   431         User::LeaveIfError( rep->Create( 
       
   432             KUNSAFProtocolsSTUNPasswordMask|stunKey,
       
   433             iAdditionalStunServers[counter]->iStunSrvPassword ) );
       
   434         }
       
   435 
       
   436     // &keys, rep
       
   437     CleanupStack::PopAndDestroy( 2, rep ); // CS:0
       
   438     }
       
   439 
       
   440 // End of file.