vpnengine/vpnmanager/src/cmmanagerutils.cpp
changeset 0 33413c0669b9
child 12 68dc8923de26
child 17 8962128a2656
equal deleted inserted replaced
-1:000000000000 0:33413c0669b9
       
     1 /*
       
     2 * Copyright (c) 2008-2009 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:   Utility methods for handling connection settings
       
    15 *
       
    16 */
       
    17 
       
    18 #include "cmmanagerutils.h"
       
    19 
       
    20 #include <cmconnectionmethoddef.h> // default ccm definitions
       
    21 #include <cmpluginvpndef.h> // vpn specific ccm definitions
       
    22 #include <cmdestinationext.h>
       
    23 #include <cmmanagerext.h>
       
    24 
       
    25 #include "eventlogger.h"
       
    26 #include "log_r6.h"
       
    27 
       
    28 
       
    29 static const TUint KMaxDestinationNameLength = 32;
       
    30 
       
    31 
       
    32 void CmManagerUtils::CreateVPNConnectionMethodToIntranetL(const TVpnPolicyInfo& aVpnPolicyInfo,
       
    33                                                           CEventLogger& aEventLogger)
       
    34     {
       
    35     LOG_("CmManagerUtils::CreateVPNConnectionMethodToIntranetL \n");
       
    36     
       
    37     using namespace CMManager;
       
    38     
       
    39     RCmManagerExt cmManagerExt;
       
    40     cmManagerExt.OpenL();        
       
    41     CleanupClosePushL( cmManagerExt );     
       
    42 
       
    43     //Makes sure that Internet Destination Exists
       
    44     RArray<TUint32> destinationArray;    
       
    45     cmManagerExt.AllDestinationsL( destinationArray );
       
    46     CleanupClosePushL(destinationArray);    
       
    47         
       
    48     TUint32 internetDestinationId = 0;
       
    49     for (TInt i = 0; i < destinationArray.Count(); ++i)
       
    50         {
       
    51         RCmDestinationExt destination = cmManagerExt.DestinationL( destinationArray[i] );
       
    52         CleanupClosePushL(destination);
       
    53         
       
    54         TUint32 purposeMetaData = destination.MetadataL( ESnapMetadataPurpose );
       
    55         if ( ESnapPurposeInternet ==  purposeMetaData )
       
    56             {
       
    57             internetDestinationId = destinationArray[i];
       
    58             CleanupStack::PopAndDestroy(); //destination
       
    59             break;
       
    60             }                
       
    61         CleanupStack::PopAndDestroy(); //destination
       
    62         }
       
    63     
       
    64     
       
    65     if ( internetDestinationId != 0)
       
    66         {                        
       
    67         //Generate name for the connection method
       
    68         HBufC* name = CreateConnectionMethodNameLC(cmManagerExt,
       
    69                                                    aVpnPolicyInfo.iName);
       
    70         
       
    71         //Create VPN connection method        
       
    72         RCmConnectionMethodExt vpnConnectionMethod =
       
    73             CreateNewConnectionMethodToIntranetL(cmManagerExt,
       
    74                                                  *name,
       
    75                                                  aVpnPolicyInfo.iId,
       
    76                                                  internetDestinationId);                        
       
    77             
       
    78         CleanupClosePushL( vpnConnectionMethod );
       
    79         TUint32 connectionMethodId = vpnConnectionMethod.GetIntAttributeL( ECmId );
       
    80         aEventLogger.LogEvent(R_VPN_MSG_CREATED_VPN_ACCESS_POINT_WITH_SNAP,
       
    81                               &(aVpnPolicyInfo.iId), NULL,
       
    82                               connectionMethodId, internetDestinationId);
       
    83         CleanupStack::PopAndDestroy(&vpnConnectionMethod);       
       
    84         CleanupStack::PopAndDestroy(name);
       
    85         }
       
    86     else
       
    87         {
       
    88         LOG_("No Internet destination: VPN connection method not created. \n");
       
    89         }
       
    90     
       
    91     CleanupStack::PopAndDestroy(); //destinationArray  
       
    92     CleanupStack::PopAndDestroy(); //cmManagerExt
       
    93     
       
    94     LOG_("CmManagerUtils::CreateVPNConnectionMethodToIntranetL - end \n");
       
    95     }
       
    96 
       
    97 void CmManagerUtils::SetVpnConnectionMethodAttributesL(RCmConnectionMethodExt& aConnectionMethod,
       
    98                                                        const TDesC& aConnectionMethodName,                                                       
       
    99                                                        const TVpnPolicyId aPolicyId,
       
   100                                                        const TUint32 aInternetDestinationId)
       
   101     {
       
   102     using namespace CMManager;
       
   103     
       
   104     LOG_("CmManagerUtils::SetVpnConnectionMethodAttributesL \n");                                                   
       
   105     
       
   106     aConnectionMethod.SetIntAttributeL( ECmNamingMethod, ENamingNothing );
       
   107     aConnectionMethod.SetStringAttributeL( ECmName, aConnectionMethodName );
       
   108     aConnectionMethod.SetStringAttributeL( EVpnServicePolicy, aPolicyId );      
       
   109     aConnectionMethod.SetIntAttributeL( ECmNextLayerSNAPId, aInternetDestinationId );
       
   110          
       
   111     LOG_("CmManagerUtils::SetVpnConnectionMethodAttributesL - end \n");
       
   112     }
       
   113 
       
   114 HBufC* CmManagerUtils::CreateConnectionMethodNameLC(RCmManagerExt& aCmManagerExt,
       
   115                                                     const TDesC& aPolicyName)
       
   116     {
       
   117     using namespace CMManager;
       
   118     
       
   119     LOG_("CmManagerUtils::CreateConnectionMethodNameLC \n");
       
   120     
       
   121     HBufC* name = HBufC::NewLC(KMaxDestinationNameLength);
       
   122     TPtr namePtr = name->Des();
       
   123     
       
   124     RArray<TUint32> connections;
       
   125     CleanupClosePushL( connections );    
       
   126     aCmManagerExt.ConnectionMethodL( connections, EFalse, EFalse, EFalse );
       
   127     
       
   128     TInt connectionMethodCount = connections.Count();    
       
   129     TBool matchFound = EFalse;
       
   130     TInt counter = 1;
       
   131     do 
       
   132         {       
       
   133         matchFound = EFalse;
       
   134         
       
   135         namePtr.Copy(aPolicyName.Left(KMaxDestinationNameLength));   
       
   136         if (counter > 1)
       
   137             {
       
   138             TBuf<10> numberValue;
       
   139             numberValue.Format(_L("(%d)"), counter);
       
   140             if (namePtr.MaxLength() < numberValue.Length() + namePtr.Length())                
       
   141                 {
       
   142                 namePtr.SetLength(namePtr.MaxLength() - numberValue.Length());
       
   143                 
       
   144                 }
       
   145             namePtr.Append(numberValue);
       
   146             }
       
   147              
       
   148         for (TInt i = 0; i < connectionMethodCount; i++)
       
   149             {
       
   150            RCmConnectionMethodExt connectionMethod = aCmManagerExt.ConnectionMethodL( connections[i] );
       
   151            CleanupClosePushL(connectionMethod);
       
   152            HBufC* existingName = connectionMethod.GetStringAttributeL( ECmName );
       
   153            CleanupStack::PopAndDestroy(); //connectionMethod
       
   154             
       
   155             if (name->Compare(*existingName) == 0)
       
   156                 {
       
   157                 delete existingName;
       
   158                 matchFound = ETrue;
       
   159                 break;
       
   160                 }                   
       
   161             delete existingName;                    
       
   162             }
       
   163         counter++;
       
   164         }while(matchFound);
       
   165         
       
   166     CleanupStack::PopAndDestroy(); // connections    
       
   167     LOG_("CmManagerUtils::CreateConnectionMethodNameLC - end \n");
       
   168     return name;
       
   169     }
       
   170 
       
   171 
       
   172 HBufC* CmManagerUtils::CreateIntranetDestinationNameLC(RCmManagerExt& aCmManagerExt)
       
   173     {
       
   174     _LIT(KDestinationNameBase, "Intranet");
       
   175     _LIT(KDestinationNumberFormat, "(%d)");
       
   176     
       
   177     HBufC* destinationName = HBufC::NewLC(KMaxDestinationNameLength);
       
   178     TPtr destinationNamePtr = destinationName->Des();
       
   179     
       
   180     RArray<TUint32> destArray;
       
   181     aCmManagerExt.AllDestinationsL(destArray);
       
   182     CleanupClosePushL(destArray);
       
   183    
       
   184     TUint counter = 1;    
       
   185     do
       
   186         {
       
   187         destinationNamePtr = KDestinationNameBase;
       
   188         if (counter > 1)
       
   189             {        
       
   190             TBuf<10> numberValue;
       
   191             numberValue.Format(KDestinationNumberFormat, counter);
       
   192             destinationNamePtr.Append(numberValue);
       
   193             }
       
   194     
       
   195         for (TInt i = 0; i < destArray.Count(); ++i)
       
   196             {
       
   197             RCmDestinationExt destination = aCmManagerExt.DestinationL(destArray[i]);
       
   198             CleanupClosePushL(destination);
       
   199             HBufC* name = destination.NameLC();
       
   200             
       
   201             if (name->Compare(*destinationName) == 0)
       
   202                 {
       
   203                 destinationNamePtr.Zero();
       
   204                 CleanupStack::PopAndDestroy(name);
       
   205                 CleanupStack::PopAndDestroy(); //destination
       
   206                 break;
       
   207                 }
       
   208             
       
   209             CleanupStack::PopAndDestroy(name);
       
   210             CleanupStack::PopAndDestroy(); //destination
       
   211             }
       
   212         counter++;
       
   213         }while(destinationName->Length() == 0);
       
   214     
       
   215     CleanupStack::PopAndDestroy(); //destArray
       
   216     return destinationName;
       
   217     }
       
   218 
       
   219 
       
   220 RCmConnectionMethodExt CmManagerUtils::CreateNewConnectionMethodToIntranetL(RCmManagerExt& aCmManagerExt,
       
   221                                                                             const TDesC& aConnectionMethodName,
       
   222                                                                             const TVpnPolicyId aPolicyId,
       
   223                                                                             const TUint32 aInternetDestinationId)
       
   224     {
       
   225     LOG_("CmManagerUtils::CreateNewConnectionMethodToIntranetL \n");
       
   226     using namespace CMManager;
       
   227 
       
   228     RCmDestinationExt intranetDestination;
       
   229     RCmConnectionMethodExt vpnConnectionMethod;
       
   230     TBool intranetSnapFound = EFalse;
       
   231     
       
   232     RArray<TUint32> destinationArray;    
       
   233     aCmManagerExt.AllDestinationsL( destinationArray );
       
   234     CleanupClosePushL(destinationArray);    
       
   235     
       
   236     //Try to find Intranet Destination using metadata.        
       
   237     for (TInt i = 0; i < destinationArray.Count(); ++i)
       
   238         {
       
   239         intranetDestination = aCmManagerExt.DestinationL( destinationArray[i] );
       
   240         CleanupClosePushL(intranetDestination);
       
   241         
       
   242         TUint32 purposeMetaData = intranetDestination.MetadataL( ESnapMetadataPurpose );
       
   243         if ( purposeMetaData ==  ESnapPurposeIntranet)
       
   244             {
       
   245             //Jumps out and leaves the destination 
       
   246             //in the clenaup stack.     
       
   247             intranetSnapFound = ETrue;
       
   248             // create new vpn record with default values to intranet
       
   249             vpnConnectionMethod = 
       
   250                 intranetDestination.CreateConnectionMethodL( KPluginVPNBearerTypeUid );
       
   251             SetVpnConnectionMethodAttributesL( vpnConnectionMethod,
       
   252                                                aConnectionMethodName,
       
   253                                                aPolicyId,
       
   254                                                aInternetDestinationId );
       
   255             intranetDestination.UpdateL();
       
   256             break;
       
   257             }                
       
   258         CleanupStack::PopAndDestroy(); //intranetDestination
       
   259         }            
       
   260                                                 
       
   261     if (!intranetSnapFound)
       
   262         {                      
       
   263         //If not found Create the Intranet Destination.
       
   264         //If the Intranet destination is not supported this leaves.
       
   265         HBufC* intranetSnapName = CreateIntranetDestinationNameLC(aCmManagerExt);            
       
   266         intranetDestination = 
       
   267             aCmManagerExt.CreateDestinationL( *intranetSnapName );
       
   268         CleanupStack::PopAndDestroy(intranetSnapName);
       
   269 
       
   270         CleanupClosePushL(intranetDestination);
       
   271         TRAPD(err, intranetDestination.SetMetadataL( ESnapMetadataPurpose, 
       
   272                                                      ESnapPurposeIntranet ));
       
   273 
       
   274         //Intranet metadata is not supported in all platforms.
       
   275         if (err == KErrNone)
       
   276             {
       
   277              // create new vpn record with default values  to intranet
       
   278             vpnConnectionMethod = 
       
   279                 intranetDestination.CreateConnectionMethodL( KPluginVPNBearerTypeUid );
       
   280             SetVpnConnectionMethodAttributesL( vpnConnectionMethod,
       
   281                                                aConnectionMethodName,
       
   282                                                aPolicyId,
       
   283                                                aInternetDestinationId );
       
   284             intranetDestination.UpdateL();
       
   285              }
       
   286         else
       
   287             {
       
   288  
       
   289             // create new vpn record with default values   
       
   290             vpnConnectionMethod = 
       
   291                 aCmManagerExt.CreateConnectionMethodL( KPluginVPNBearerTypeUid );
       
   292             SetVpnConnectionMethodAttributesL( vpnConnectionMethod,
       
   293                                                aConnectionMethodName,
       
   294                                                aPolicyId,
       
   295                                                aInternetDestinationId );
       
   296             vpnConnectionMethod.UpdateL();
       
   297             }
       
   298         }
       
   299 
       
   300     CleanupStack::PopAndDestroy(&intranetDestination); 
       
   301     CleanupStack::PopAndDestroy(&destinationArray); 
       
   302     
       
   303     LOG_("CmManagerUtils::CreateNewConnectionMethodToIntranetL - end \n");
       
   304     return vpnConnectionMethod;
       
   305     }