vpnengine/kmdserver/src/ikepluginhandler.cpp
changeset 0 33413c0669b9
equal deleted inserted replaced
-1:000000000000 0:33413c0669b9
       
     1 /*
       
     2 * Copyright (c) 2008 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:  Handler of an IKE protocol plugin
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "ikedebug.h"
       
    20 #include "ikeplugindefs.h"
       
    21 #include "ikepluginif.h"
       
    22 #include "ikepluginsessionhandler.h"
       
    23 #include "kmdeventloggerif.h"
       
    24 
       
    25 // CLASS HEADER
       
    26 #include "ikepluginhandler.h"
       
    27 
       
    28 _LIT( KIkePluginPaths, "" ); // No additional paths.
       
    29 _LIT( KIkeV1Library, "ikev1lib" ); 
       
    30 _LIT( KIkeV2Library, "ikev2lib" ); 
       
    31 
       
    32 // ======== MEMBER FUNCTIONS ========
       
    33 
       
    34 // ---------------------------------------------------------------------------
       
    35 // Two-phased constructor.
       
    36 // ---------------------------------------------------------------------------
       
    37 //
       
    38 CIkePluginHandler* CIkePluginHandler::NewL( TInt aIkeVersion,
       
    39                                             MKmdEventLoggerIf& aEventLogger,
       
    40                                             MIkeDebug& aDebug )
       
    41     {
       
    42     CIkePluginHandler* self = new ( ELeave ) CIkePluginHandler( aIkeVersion,
       
    43                                                                 aEventLogger,
       
    44                                                                 aDebug );
       
    45     return self;
       
    46     }
       
    47     
       
    48 // ---------------------------------------------------------------------------
       
    49 // Destructor.
       
    50 // ---------------------------------------------------------------------------
       
    51 //
       
    52 CIkePluginHandler::~CIkePluginHandler()
       
    53     {
       
    54     __ASSERT_DEBUG( iIkePluginSessions.Count() == 0,
       
    55                     User::Invariant() );
       
    56     iIkePluginSessions.Close();    
       
    57     }
       
    58 
       
    59 // ---------------------------------------------------------------------------
       
    60 // Constructor.
       
    61 // ---------------------------------------------------------------------------
       
    62 //
       
    63 CIkePluginHandler::CIkePluginHandler( TInt aIkeVersion,
       
    64                                       MKmdEventLoggerIf& aEventLogger,
       
    65                                       MIkeDebug& aDebug ) 
       
    66  : iIkeVersion( aIkeVersion ),
       
    67    iEventLogger( aEventLogger ),
       
    68    iDebug( aDebug )
       
    69     {
       
    70     __ASSERT_DEBUG( ( iIkeVersion == KIkeV1 ||
       
    71                       iIkeVersion == KIkeV2 ),
       
    72                       User::Invariant() );
       
    73     }    
       
    74 
       
    75 // ---------------------------------------------------------------------------
       
    76 // Creates IKE plugin session.
       
    77 // ---------------------------------------------------------------------------
       
    78 //
       
    79 CIkePluginSessionHandler& CIkePluginHandler::CreateIkePluginSessionL( TUint32 aVpnIapId,
       
    80                                                                       TUint32 aVpnNetId,
       
    81                                                                       TUint32 aVpnInterfaceIndex,
       
    82                                                                       IkeSocket::TIpVersion aIpVersion,
       
    83                                                                       const TInetAddr& aDnsServer,
       
    84                                                                       CIkeConnectionInterface& aConnection,
       
    85                                                                       MIkePluginSessionHandlerCallback& aCallback )            
       
    86     {
       
    87     CIkePluginSessionHandler* sessionHandler = NULL;    
       
    88     TRAPD( err, sessionHandler = &DoCreateIkePluginSessionL( aVpnIapId,
       
    89                                                              aVpnNetId,
       
    90                                                              aVpnInterfaceIndex,
       
    91                                                              aIpVersion,
       
    92                                                              aDnsServer,
       
    93                                                              aConnection,
       
    94                                                              aCallback ) );
       
    95     
       
    96     if ( err != KErrNone )
       
    97         {        
       
    98         DeleteIkePluginSession( aVpnIapId );
       
    99         User::Leave( err );
       
   100         }
       
   101     
       
   102     return *sessionHandler;
       
   103     }
       
   104 
       
   105 // ---------------------------------------------------------------------------
       
   106 // Deletes IKE plugin session. IKE plugin is unloaded if there are no more
       
   107 // sessions.
       
   108 // ---------------------------------------------------------------------------
       
   109 //
       
   110 void CIkePluginHandler::DeleteIkePluginSession( TUint32 aVpnIapId )
       
   111     {
       
   112     TInt count = iIkePluginSessions.Count();
       
   113     
       
   114     for ( TInt i=0; i<count; i++ )
       
   115         {
       
   116         if ( iIkePluginSessions[i]->VpnIapId() == aVpnIapId )
       
   117             {
       
   118             CIkePluginSessionHandler* sessionHandler = iIkePluginSessions[i];
       
   119             iIkePluginSessions.Remove( i );
       
   120             delete sessionHandler;
       
   121             sessionHandler = NULL;            
       
   122             break;
       
   123             }
       
   124         }
       
   125     
       
   126     if ( iIkePluginSessions.Count() == 0 )
       
   127         {
       
   128         UnloadIkePlugin();
       
   129         }
       
   130     }
       
   131 	
       
   132 // ---------------------------------------------------------------------------
       
   133 // Creates IKE plugin session. IKE plugin is loaded if not yet loaded.
       
   134 // ---------------------------------------------------------------------------
       
   135 //
       
   136 CIkePluginSessionHandler& CIkePluginHandler::DoCreateIkePluginSessionL( TUint32 aVpnIapId,
       
   137                                                                         TUint32 aVpnNetId,
       
   138                                                                         TUint32 aVpnInterfaceIndex,
       
   139                                                                         IkeSocket::TIpVersion aIpVersion,
       
   140                                                                         const TInetAddr& aDnsServerAddr,
       
   141                                                                         CIkeConnectionInterface& aConnection,
       
   142                                                                         MIkePluginSessionHandlerCallback& aCallback )            
       
   143     {
       
   144     if ( iIkePluginSessions.Count() == 0 )
       
   145         {
       
   146         LoadIkePluginL();
       
   147         }
       
   148     
       
   149     CIkePluginSessionHandler* sessionHandler = CIkePluginSessionHandler::NewLC( aVpnIapId,
       
   150                                                                                 aVpnNetId,
       
   151                                                                                 aVpnInterfaceIndex,
       
   152                                                                                 IkeSocket::TIkeMajorVersion(iIkeVersion),
       
   153                                                                                 aIpVersion,
       
   154                                                                                 aDnsServerAddr,
       
   155                                                                                 aConnection,
       
   156                                                                                 *iIkePlugin,
       
   157                                                                                 aCallback,
       
   158                                                                                 iDebug );   
       
   159     iIkePluginSessions.AppendL( sessionHandler );
       
   160     CleanupStack::Pop( sessionHandler );
       
   161     
       
   162     return *sessionHandler;
       
   163     }
       
   164 
       
   165 // ---------------------------------------------------------------------------
       
   166 // Loads IKE plugin.
       
   167 // ---------------------------------------------------------------------------
       
   168 //
       
   169 void CIkePluginHandler::LoadIkePluginL()
       
   170     {	
       
   171     TInt status( KErrNone );
       
   172         
       
   173     switch( iIkeVersion )
       
   174         {
       
   175         case KIkeV1:
       
   176             {
       
   177             TUidType uidType( KIkePluginUid1, KIkePluginUid2, KIkeV1PluginUid3 );
       
   178             status = iLibrary.Load( KIkeV1Library, KIkePluginPaths, uidType );            
       
   179             break;
       
   180             }
       
   181         case KIkeV2:
       
   182             {
       
   183             TUidType uidType( KIkePluginUid1, KIkePluginUid2, KIkeV2PluginUid3 );
       
   184             status = iLibrary.Load( KIkeV2Library, KIkePluginPaths, uidType );            
       
   185             break;            
       
   186             }
       
   187         default:
       
   188             {
       
   189             status = KErrNotSupported;
       
   190             break;
       
   191             }        
       
   192         }
       
   193     
       
   194     DEBUG_LOG2( _L("Loading IKE plugin library, IKE version=%d, status=%d"), iIkeVersion, status );    
       
   195     User::LeaveIfError( status );
       
   196     
       
   197     CreateIkePluginL factoryMethodL  = reinterpret_cast<CreateIkePluginL>( iLibrary.Lookup(1) );    
       
   198     TRAPD( err, ( iIkePlugin = factoryMethodL( iEventLogger, iDebug ) ) );
       
   199     if ( err != KErrNone )
       
   200         {
       
   201         DEBUG_LOG1( _L("Could not create IKE plugin, err=%d"), err );
       
   202         UnloadIkePlugin();
       
   203         User::Leave( err );
       
   204         }    
       
   205     }
       
   206 
       
   207 // ---------------------------------------------------------------------------
       
   208 // Unloads IKE plugin.
       
   209 // ---------------------------------------------------------------------------
       
   210 //
       
   211 void CIkePluginHandler::UnloadIkePlugin()
       
   212     {
       
   213     DEBUG_LOG1( _L("Unloading IKE Plugin library, IKE version=%d"), iIkeVersion );
       
   214     delete iIkePlugin;
       
   215     iIkePlugin = NULL;
       
   216     iLibrary.Close();    
       
   217     }
       
   218