voipplugins/voipadapters/voipxmlprovisioning/voipxmlprocessor/src/voipxmlsiphandler.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:  SIP handler for VoIP XML processor
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32cmn.h>
       
    20 #include <coecntrl.h>
       
    21 #include <sipmanagedprofile.h>
       
    22 #include <sipmanagedprofileregistry.h>
       
    23 #include <sysutil.h>
       
    24 #include <wlaninternalpskeys.h>
       
    25 #include <pathinfo.h>
       
    26 #include <Authority16.h>
       
    27 #include <stringloader.h>
       
    28 #include <escapeutils.h>
       
    29 
       
    30 #include "voipxmlutils.h"
       
    31 #include "voipxmlsiphandler.h"
       
    32 #include "voipxmlprocessorlogger.h"
       
    33 #include "voipxmlprocessordefaults.h"
       
    34 
       
    35 // ---------------------------------------------------------------------------
       
    36 // CVoipXmlSipHandler::CVoipXmlSipHandler
       
    37 // ---------------------------------------------------------------------------
       
    38 //
       
    39 CVoipXmlSipHandler::CVoipXmlSipHandler()
       
    40     {
       
    41     }
       
    42 
       
    43 // ---------------------------------------------------------------------------
       
    44 // CVoipXmlSipHandler::NewL
       
    45 // ---------------------------------------------------------------------------
       
    46 //
       
    47 CVoipXmlSipHandler* CVoipXmlSipHandler::NewL()
       
    48     {
       
    49     CVoipXmlSipHandler* self = new ( ELeave ) CVoipXmlSipHandler;
       
    50     CleanupStack::PushL( self );
       
    51     self->ConstructL();
       
    52     CleanupStack::Pop( self );
       
    53     return self;
       
    54     }
       
    55 
       
    56 // ---------------------------------------------------------------------------
       
    57 // CVoipXmlSipHandler::ConstructL
       
    58 // ---------------------------------------------------------------------------
       
    59 //
       
    60 void CVoipXmlSipHandler::ConstructL()
       
    61     {
       
    62     DBG_PRINT( "CVoipXmlSipHandler::ConstructL begin" );
       
    63 
       
    64     // SIP Managed Profile Registry.
       
    65     iRegistry = CSIPManagedProfileRegistry::NewL( *this );
       
    66 
       
    67     // Create an empty SIP profile in which all settings will be set.
       
    68     iProfileType.iSIPProfileClass = TSIPProfileTypeInfo::EInternet;
       
    69     iProfileType.iSIPProfileName.Copy( KIetf() );
       
    70     iProfile = iRegistry->CreateL( iProfileType );
       
    71 
       
    72     iProxyUri           = HBufC8::NewL( KMaxNodeValueLength );
       
    73     iProxyTransport     = EAutomatic;
       
    74     iProxyPort          = KErrNotFound;
       
    75     iLr                 = EFalse;
       
    76     iRegistrarUri       = HBufC8::NewL( KMaxNodeValueLength );
       
    77     iRegistrarTransport = EAutomatic;
       
    78     iRegistrarPort      = KErrNotFound;
       
    79 
       
    80     const TInt ipTosShift( 2 );
       
    81     const TUint32 tosBits( KDefaultSigQos << ipTosShift );
       
    82     iProfile->SetParameter( KSIPSoIpTOS, tosBits );
       
    83 
       
    84     DBG_PRINT( "CVoipXmlSipHandler::ConstructL end" );
       
    85     }
       
    86 
       
    87 // ---------------------------------------------------------------------------
       
    88 // CVoipXmlSipHandler::~CVoipXmlSipHandler
       
    89 // ---------------------------------------------------------------------------
       
    90 //
       
    91 CVoipXmlSipHandler::~CVoipXmlSipHandler()
       
    92     {
       
    93     delete iProfile;
       
    94     delete iRegistry;
       
    95     delete iProxyUri;
       
    96     delete iRegistrarUri;
       
    97     }
       
    98 
       
    99 // ---------------------------------------------------------------------------
       
   100 // Sets SIP setting.
       
   101 // ---------------------------------------------------------------------------
       
   102 //
       
   103 void CVoipXmlSipHandler::SetSetting( TInt aType, TInt aParam, 
       
   104     const TDesC8& aValue )
       
   105     {
       
   106     // Ignore too long descriptors.
       
   107     if ( KMaxNodeValueLength < aValue.Length() )
       
   108         {
       
   109         return;
       
   110         }
       
   111 
       
   112     switch ( aParam )
       
   113         {
       
   114         case EName:
       
   115             {
       
   116             TRAPD( err, CreateProviderNameL( aValue ) );
       
   117             if ( KErrNone == err )
       
   118                 {
       
   119                 iSettingsSet = ETrue;
       
   120                 }
       
   121             break;
       
   122             }
       
   123         case ESignalingQos:
       
   124             {
       
   125             // We need to do bitshifting on the IP TOS, because it's the 
       
   126             // upper 6 bits that are set and settings provide us the IP TOS
       
   127             // as the lower 6 bits.
       
   128             // The lower 2 bits are reserver for explicit congestion
       
   129             // notification. 
       
   130             // See also more from:
       
   131             // Symbian Developer Library 
       
   132             //    => in_sock.h Global variables
       
   133             //       => KSoIpTOS
       
   134             const TInt ipTosShift( 2 );
       
   135             TInt value( KErrNotFound );
       
   136             VoipXmlUtils::Des8ToInt( aValue, value );
       
   137             if ( 0 <= value )
       
   138                 {
       
   139                 const TUint32 tosBits( value << ipTosShift );
       
   140                 iProfile->SetParameter( KSIPSoIpTOS, tosBits );
       
   141                 iSettingsSet = ETrue;
       
   142                 }
       
   143             break;
       
   144             }
       
   145         case EType:
       
   146             {
       
   147             TBuf8<KMaxNodeValueLength> value;
       
   148             value.Copy( aValue );
       
   149             value.UpperCase();
       
   150             if ( 0 == value.Compare( KIms() ) )
       
   151                 {
       
   152                 iProfileType.iSIPProfileClass = TSIPProfileTypeInfo::EIms;
       
   153                 value.LowerCase();
       
   154                 iProfileType.iSIPProfileName.Copy( value );
       
   155                 iProfile->SetType( iProfileType );
       
   156                 iSettingsSet = ETrue;
       
   157                 }
       
   158             else if ( 0 == value.Compare( KIetf() ) )
       
   159                 {
       
   160                 iProfileType.iSIPProfileClass = 
       
   161                     TSIPProfileTypeInfo::EInternet;
       
   162                 iProfileType.iSIPProfileName.Copy( value );
       
   163                 iProfile->SetType( iProfileType );
       
   164                 iSettingsSet = ETrue;
       
   165                 }
       
   166             else
       
   167                 {
       
   168                 iProfileType.iSIPProfileClass = TSIPProfileTypeInfo::EOther;
       
   169                 iProfileType.iSIPProfileName.Copy( aValue );
       
   170                 iProfile->SetType( iProfileType );
       
   171                 iSettingsSet = ETrue;
       
   172                 }
       
   173             break;
       
   174             }
       
   175         case EPublicUserId:
       
   176             {
       
   177             TBuf8<KMaxNodeValueLength> value;
       
   178             value.Copy( aValue );
       
   179             TRAPD( err, ModifyPuidL( value ) );
       
   180             if ( KErrNone == err )
       
   181                 {
       
   182                 iProfile->SetParameter( KSIPUserAor, value );
       
   183                 iSettingsSet = ETrue;
       
   184                 }
       
   185             break;
       
   186             }
       
   187         case ESignalCompression:
       
   188             {
       
   189             TInt value;
       
   190             if ( KErrNone == VoipXmlUtils::Des8ToInt( aValue, value ) )
       
   191                 {
       
   192                 iProfile->SetParameter( KSIPSigComp, (TBool)value );
       
   193                 iSettingsSet = ETrue;
       
   194                 }
       
   195             break;
       
   196             }
       
   197         case ESecurityNegotiation:
       
   198             {
       
   199             TInt value;
       
   200             if ( KErrNone == VoipXmlUtils::Des8ToInt( aValue, value ) )
       
   201                 {
       
   202                 iProfile->SetParameter( 
       
   203                     KSIPSecurityNegotiation, (TBool)value );
       
   204                 iSettingsSet = ETrue;
       
   205                 }
       
   206             break;
       
   207             }
       
   208         case EProfileLock:
       
   209             {
       
   210             TInt value;
       
   211             if ( KErrNone == VoipXmlUtils::Des8ToInt( aValue, value ) )
       
   212                 {
       
   213                 iProfile->SetParameter( KSIPProfileLocked, (TBool)value );
       
   214                 iSettingsSet = ETrue;
       
   215                 }
       
   216             break;
       
   217             }
       
   218         case EAutoRegistration:
       
   219             {
       
   220             TInt value;
       
   221             if ( KErrNone == VoipXmlUtils::Des8ToInt( aValue, value ) )
       
   222                 {
       
   223                 iProfile->SetParameter( KSIPAutoRegistration, (TBool)value );
       
   224                 iSettingsSet = ETrue;
       
   225                 }
       
   226             break;
       
   227             }
       
   228         case EUri:
       
   229             {
       
   230             TBuf8<KMaxNodeValueLength> tempBuf( KNullDesC8 );
       
   231             if ( 0 != aValue.Find( KSip() ) &&
       
   232                 0 != aValue.Find( KSips() ) )
       
   233                 {
       
   234                 tempBuf.Copy( KSip() );
       
   235                 }
       
   236             tempBuf.Append( aValue );
       
   237             if ( EProxy == aType )
       
   238                 {
       
   239                 iProxyUri->Des().Copy( tempBuf );
       
   240                 iSettingsSet = ETrue;
       
   241                 }
       
   242             else if ( ERegistrar == aType )
       
   243                 {
       
   244                 iRegistrarUri->Des().Copy( tempBuf );
       
   245                 iSettingsSet = ETrue;
       
   246                 }
       
   247             break;
       
   248             }
       
   249         case ETransport:
       
   250             {
       
   251             if ( EProxy == aType )
       
   252                 {
       
   253                 iProxyTransport = ValidateTransport( aValue );
       
   254                 iSettingsSet = ETrue;
       
   255                 }
       
   256             else if ( ERegistrar == aType )
       
   257                 {
       
   258                 iRegistrarTransport = ValidateTransport( aValue );
       
   259                 iSettingsSet = ETrue;
       
   260                 }
       
   261             break;
       
   262             }
       
   263         case EPort:
       
   264             {
       
   265             TInt value;
       
   266             TInt err = VoipXmlUtils::Des8ToInt( aValue, value );
       
   267             if ( EProxy == aType && KErrNone == err )
       
   268                 {
       
   269                 iProxyPort = value;
       
   270                 iSettingsSet = ETrue;
       
   271                 }
       
   272             else if ( ERegistrar == aType && KErrNone == err )
       
   273                 {
       
   274                 iRegistrarPort = value;
       
   275                 iSettingsSet = ETrue;
       
   276                 }
       
   277             break;
       
   278             }
       
   279         case ELooseRouting:
       
   280             {
       
   281             TInt value;
       
   282             if ( EProxy == aType && 
       
   283                 KErrNone == VoipXmlUtils::Des8ToInt( aValue, value ) )
       
   284                 {
       
   285                 iLr = (TBool)value;
       
   286                 iSettingsSet = ETrue;
       
   287                 }
       
   288             break;
       
   289             }
       
   290         case EUsername:
       
   291             {
       
   292             if ( EProxy == aType )
       
   293                 {
       
   294                 if ( TSIPProfileTypeInfo::EInternet == 
       
   295                     iProfileType.iSIPProfileClass )
       
   296                     {
       
   297                     iProfile->SetParameter( KSIPOutboundProxy, 
       
   298                         KSIPDigestUserName, aValue );
       
   299                     iSettingsSet = ETrue;
       
   300                     }
       
   301                 else
       
   302                     {
       
   303                     iProfile->SetParameter( KSIPPrivateIdentity, aValue );
       
   304                     iSettingsSet = ETrue;
       
   305                     }
       
   306                 }
       
   307             else if ( ERegistrar == aType )
       
   308                 {
       
   309                 if ( TSIPProfileTypeInfo::EInternet == 
       
   310                     iProfileType.iSIPProfileClass )
       
   311                     {
       
   312                     iProfile->SetParameter( KSIPRegistrar, KSIPDigestUserName,
       
   313                         aValue );
       
   314                     iSettingsSet = ETrue;
       
   315                     }
       
   316                 else
       
   317                     {
       
   318                     iProfile->SetParameter( KSIPPrivateIdentity, aValue );
       
   319                     iSettingsSet = ETrue;
       
   320                     }
       
   321                 }
       
   322             break;
       
   323             }
       
   324         case EPassword:
       
   325             {
       
   326             if ( EProxy == aType )
       
   327                 {
       
   328                 iProfile->SetParameter( KSIPOutboundProxy, KSIPDigestPassword,
       
   329                     aValue );
       
   330                 iSettingsSet = ETrue;
       
   331                 }
       
   332             else if ( ERegistrar == aType )
       
   333                 {
       
   334                 iProfile->SetParameter( KSIPRegistrar, KSIPDigestPassword,
       
   335                     aValue );
       
   336                 iSettingsSet = ETrue;
       
   337                 }
       
   338             break;
       
   339             }
       
   340         default:
       
   341             break;
       
   342         }
       
   343     }
       
   344 
       
   345 // ---------------------------------------------------------------------------
       
   346 // Stores settings to SIP managed profile registry.
       
   347 // ---------------------------------------------------------------------------
       
   348 //
       
   349 TInt CVoipXmlSipHandler::StoreSettings()
       
   350     {
       
   351     if ( !iSettingsSet )
       
   352         {
       
   353         // No settings to be stored => method not supported.
       
   354         return KErrNotSupported;
       
   355         }
       
   356 
       
   357     TBuf8<KMaxNodeValueLength> tempBuf( KNullDesC8 );
       
   358 
       
   359     // ==============================
       
   360     // Set components to proxy URI.
       
   361     // ==============================
       
   362     //
       
   363     if ( KErrNotFound != iProxyPort )
       
   364         {
       
   365         tempBuf.Copy( KColon() );
       
   366         tempBuf.AppendNum( iProxyPort );
       
   367         }
       
   368     if ( EUdp == iProxyTransport )
       
   369         {
       
   370         tempBuf.Append( KTransport() );
       
   371         tempBuf.Append( KUdp() );
       
   372         }
       
   373     else if ( ETcp == iProxyTransport )
       
   374         {
       
   375         tempBuf.Append( KTransport() );
       
   376         tempBuf.Append( KTcp() );
       
   377         }
       
   378     if ( iLr )
       
   379         {
       
   380         tempBuf.Append( KLr() );
       
   381         }
       
   382 
       
   383     if ( iProxyUri->Des().MaxLength() >= 
       
   384         ( iProxyUri->Des().Length() + tempBuf.Length() ) )
       
   385         {
       
   386         iProxyUri->Des().Append( tempBuf );
       
   387         }
       
   388     iProfile->SetParameter( KSIPOutboundProxy, KSIPServerAddress, 
       
   389         iProxyUri->Des() );
       
   390 
       
   391     // ==============================
       
   392     // Set components to registrar URI.
       
   393     // ==============================
       
   394     //
       
   395     tempBuf.Zero();
       
   396     if ( KErrNotFound != iRegistrarPort )
       
   397         {
       
   398         tempBuf.Copy( KColon() );
       
   399         tempBuf.AppendNum( iRegistrarPort );
       
   400         }
       
   401     if ( EUdp == iRegistrarTransport )
       
   402         {
       
   403         tempBuf.Append( KTransport() );
       
   404         tempBuf.Append( KUdp() );
       
   405         }
       
   406     else if ( ETcp == iRegistrarTransport )
       
   407         {
       
   408         tempBuf.Append( KTransport() );
       
   409         tempBuf.Append( KTcp() );
       
   410         }
       
   411     if ( iRegistrarUri->Des().MaxLength() >= 
       
   412         ( iRegistrarUri->Des().Length() + tempBuf.Length() ) )
       
   413         {
       
   414         iRegistrarUri->Des().Append( tempBuf );
       
   415         }
       
   416     iProfile->SetParameter( KSIPRegistrar, KSIPServerAddress, 
       
   417         iRegistrarUri->Des() );
       
   418 
       
   419     TRAPD( err, iRegistry->SaveL( *iProfile ) );
       
   420     if ( KErrNone != err )
       
   421         {
       
   422         // No need for specifying what went wrong because paramhandler
       
   423         // is only intrested in KErrNotSupported, KErrCompletion and KErrNone.
       
   424         err = KErrCompletion;
       
   425         }
       
   426     // Store SIP settings ID for later use.
       
   427     iProfile->GetParameter( KSIPProfileId, iProfileId );
       
   428     return err;
       
   429     }
       
   430 
       
   431 // ---------------------------------------------------------------------------
       
   432 // Returns the profile ID if the profile saved in StoreSettings.
       
   433 // ---------------------------------------------------------------------------
       
   434 //
       
   435 TUint32 CVoipXmlSipHandler::SettingsId()
       
   436     {
       
   437     return iProfileId;
       
   438     }
       
   439 
       
   440 // ---------------------------------------------------------------------------
       
   441 // Saves linkage information.
       
   442 // ---------------------------------------------------------------------------
       
   443 //
       
   444 void CVoipXmlSipHandler::LinkSettings( TInt aType, TUint32 aSettingsId )
       
   445     {
       
   446     if ( EDestination == aType && aSettingsId )
       
   447         {
       
   448         iProfile->SetParameter( KSIPSnapId, aSettingsId );
       
   449         }
       
   450     }
       
   451 
       
   452 // ---------------------------------------------------------------------------
       
   453 // Finalizes settings saving.
       
   454 // ---------------------------------------------------------------------------
       
   455 //
       
   456 TInt CVoipXmlSipHandler::FinalizeSettings()
       
   457     {
       
   458     TRAPD( err, iRegistry->SaveL( *iProfile ) );
       
   459     if ( KErrNone != err )
       
   460         {
       
   461         // ParamHandler is only intrested in KErrNone and KErrCompletion.
       
   462         err = KErrCompletion;
       
   463         }
       
   464     return err;
       
   465     }
       
   466 
       
   467 // ---------------------------------------------------------------------------
       
   468 // From class MSIPProfileRegistryObserver.
       
   469 // ---------------------------------------------------------------------------
       
   470 //
       
   471 void CVoipXmlSipHandler::ProfileRegistryEventOccurred( 
       
   472     TUint32 /*aSIPProfileId*/, TEvent /*aEvent*/ )
       
   473     {
       
   474     }
       
   475 
       
   476 // ---------------------------------------------------------------------------
       
   477 // From class MSIPProfileRegistryObserver. 
       
   478 // ---------------------------------------------------------------------------
       
   479 //
       
   480 void CVoipXmlSipHandler::ProfileRegistryErrorOccurred( 
       
   481     TUint32 /*aSIPProfileId*/, TInt /*aError*/ )
       
   482     {
       
   483     }
       
   484 
       
   485 // ---------------------------------------------------------------------------
       
   486 // CVoipXmlSipHandler::CreateProviderNameL
       
   487 // Checks if duplicate named SIP profiles. Renames if same.
       
   488 // ---------------------------------------------------------------------------
       
   489 //
       
   490 void CVoipXmlSipHandler::CreateProviderNameL( const TDesC8& aName )
       
   491     {
       
   492     DBG_PRINT( "CVoipXmlSipHandler::CreateProviderNameL begin" );
       
   493 
       
   494     RPointerArray<CSIPProfile> profiles;
       
   495     CleanupResetAndDestroyL( profiles ); // CS:1
       
   496 
       
   497     // Get all profiles based on profile types.
       
   498     iRegistry->ProfilesL( profiles );
       
   499     const TInt profileCount = profiles.Count();
       
   500 
       
   501     // Go through loaded profiles and check for name duplicates.
       
   502     HBufC8* name = HBufC8::NewLC( KMaxNodeNameLength ); // CS:2
       
   503     name->Des().Copy( aName );
       
   504     TUint i( 1 ); // Add number to the name if name already in use.
       
   505     const TInt count( profiles.Count() );
       
   506     for ( TInt counter = 0; counter < count; counter++ )
       
   507         {
       
   508         CSIPManagedProfile* profile = 
       
   509             static_cast<CSIPManagedProfile*>( profiles[counter] );
       
   510         const TDesC8* existingName;
       
   511         profile->GetParameter( KSIPProviderName, existingName );
       
   512         if ( 0 == existingName->Compare( *name ) )
       
   513             {
       
   514             name->Des().Copy( aName );
       
   515             name->Des().Append( KOpenParenthesis8() );
       
   516             name->Des().AppendNum( i );
       
   517             name->Des().Append( KClosedParenthesis8() );  
       
   518             counter = 0;
       
   519             i++;
       
   520             if ( KMaxProfileNames < i )
       
   521                 {
       
   522                 User::Leave( KErrBadName );
       
   523                 }
       
   524             }
       
   525         }
       
   526     iProfile->SetParameter( KSIPProviderName, name->Des() );
       
   527 
       
   528     // name, &profiles
       
   529     CleanupStack::PopAndDestroy( 2, &profiles ); // CS:0
       
   530     DBG_PRINT( "CVoipXmlSipHandler::CreateProviderNameL end" );
       
   531     }
       
   532 
       
   533 // ---------------------------------------------------------------------------
       
   534 // Modifies public user ID, i.e. escapes and adds sip: prefix if necessary.
       
   535 // ---------------------------------------------------------------------------
       
   536 //
       
   537 void CVoipXmlSipHandler::ModifyPuidL( TDes8& aValue )
       
   538     {
       
   539     if ( KErrNotFound == aValue.Find( KEscaped() ) )
       
   540         {
       
   541         HBufC8* tempPuid = EscapeUtils::EscapeEncodeL( aValue,
       
   542             EscapeUtils::EEscapeNormal );
       
   543         aValue.Copy( tempPuid->Des() );
       
   544         delete tempPuid;
       
   545         tempPuid = NULL;
       
   546         }
       
   547     if ( 0 != aValue.Find( KSip() ) && 0 != aValue.Find( KSips() ) )
       
   548         {
       
   549         if ( aValue.MaxLength() >= ( aValue.Length() + KSip().Length() ) )
       
   550             {
       
   551             aValue.Insert( 0, KSip() );
       
   552             }
       
   553         else
       
   554             {
       
   555             User::Leave( KErrTooBig );
       
   556             }
       
   557         }
       
   558     }
       
   559 
       
   560 // ---------------------------------------------------------------------------
       
   561 // Validates transport type.
       
   562 // ---------------------------------------------------------------------------
       
   563 //
       
   564 CVoipXmlSipHandler::TTransportType CVoipXmlSipHandler::ValidateTransport( 
       
   565     const TDesC8& aValue )
       
   566     {
       
   567     TBuf8<KMaxNodeValueLength> value;
       
   568     value.Copy( aValue );
       
   569     value.UpperCase();
       
   570     if ( 0 == value.Compare( KUdp() ) )
       
   571         {
       
   572         return EUdp;
       
   573         }
       
   574     else if ( 0 == value.Compare( KTcp() ) )
       
   575         {
       
   576         return ETcp;
       
   577         }
       
   578     else
       
   579         {
       
   580         return EAutomatic;
       
   581         }
       
   582     }
       
   583 
       
   584 // End of file.