vpnengine/kmdserver/src/kmdserver.cpp
changeset 0 33413c0669b9
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:  KMD server
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INTERNAL INCLUDES
       
    20 #include "ikedebug.h"
       
    21 #include "ikeplugindefs.h"
       
    22 #include "ikepluginhandler.h"
       
    23 #include "kmddebuglogger.h"
       
    24 #include "kmdeventlogger.h"
       
    25 #include "kmdsession.h"
       
    26 #include "secpolreader.h"
       
    27 #include "vpnconnection.h"
       
    28 
       
    29 // CLASS HEADER
       
    30 #include "kmdserver.h"
       
    31 
       
    32 const TUint CKmdServer::iKmdServerRangeCount = 2;
       
    33   
       
    34 const TInt CKmdServer::iKmdServerRanges[iKmdServerRangeCount] = 
       
    35     {
       
    36     CKmdServer::KKmdStartConnection,
       
    37     CKmdServer::KKmdCancelResolveAddress+1
       
    38     };
       
    39 
       
    40 const TUint8 CKmdServer::iKmdServerElementIndex[iKmdServerRangeCount] = 
       
    41     {
       
    42     0,
       
    43     CPolicyServer::ENotSupported
       
    44     };
       
    45 
       
    46 const CPolicyServer::TPolicyElement CKmdServer::iKmdServerElements[] =
       
    47     {
       
    48     {_INIT_SECURITY_POLICY_C1(ECapabilityNetworkControl), 
       
    49       CPolicyServer::EFailClient},
       
    50     };
       
    51 
       
    52 const CPolicyServer::TPolicy CKmdServer::iKmdServerPolicy =
       
    53     {
       
    54     0,                      // All connect attempts are checked
       
    55     iKmdServerRangeCount,   // Count of ranges
       
    56     iKmdServerRanges,       // 0-6, 7...
       
    57     iKmdServerElementIndex, // Only range 0-6 are checked
       
    58     iKmdServerElements      // The list of policy elements
       
    59     };
       
    60 
       
    61 const TInt KIkeV1PluginHandlerIndex( 0 );
       
    62 const TInt KIkeV2PluginHandlerIndex( 1 );
       
    63 
       
    64 // ======== MEMBER FUNCTIONS ========
       
    65 
       
    66 // ---------------------------------------------------------------------------
       
    67 // Two-phased constructor.
       
    68 // ---------------------------------------------------------------------------
       
    69 //
       
    70 CKmdServer* CKmdServer::NewL()
       
    71     {
       
    72     CKmdServer* self = new ( ELeave ) CKmdServer;
       
    73     CleanupStack::PushL( self );
       
    74     self->ConstructL();    
       
    75     CleanupStack::Pop( self );
       
    76     return self;
       
    77     }
       
    78 
       
    79 // ---------------------------------------------------------------------------
       
    80 // Destructor.
       
    81 // ---------------------------------------------------------------------------
       
    82 //
       
    83 CKmdServer::~CKmdServer()
       
    84     {    
       
    85     iIpsecPolicyServ.Close();
       
    86     iVpnConnections.ResetAndDestroy();
       
    87     iVpnConnections.Close();    
       
    88     delete iIkePluginHandlers[0];
       
    89     delete iIkePluginHandlers[1];
       
    90     delete iSecpolReader;
       
    91     delete iEventLogger;
       
    92     
       
    93 #ifdef _DEBUG
       
    94     if ( iDebugLogger != NULL )
       
    95         {
       
    96         iDebugLogger->LogWrite(_L("KMD server stopped."));
       
    97         }
       
    98 #endif        
       
    99     delete iDebugLogger;
       
   100     }
       
   101 
       
   102 // ---------------------------------------------------------------------------
       
   103 // Constructor.
       
   104 // ---------------------------------------------------------------------------
       
   105 //
       
   106 CKmdServer::CKmdServer() : 
       
   107  CPolicyServer( EPriorityStandard, iKmdServerPolicy )
       
   108     {
       
   109     }
       
   110 
       
   111 // ---------------------------------------------------------------------------
       
   112 // Second phase construction.
       
   113 // ---------------------------------------------------------------------------
       
   114 //
       
   115 void CKmdServer::ConstructL()
       
   116     {
       
   117     iDebugLogger = CKmdDebugLogger::NewL();
       
   118     
       
   119 #ifdef _DEBUG
       
   120     iDebugLogger->LogWrite(_L("------------------------------------------------------"));
       
   121     iDebugLogger->LogWrite(_L("Starting KMD server."));
       
   122     iDebugLogger->LogWrite(_L("------------------------------------------------------"));
       
   123 #endif    
       
   124 
       
   125     User::LeaveIfError( iIpsecPolicyServ.Connect() );        
       
   126     iEventLogger = CKmdEventLogger::NewL( *iDebugLogger );
       
   127     iSecpolReader = CSecpolReader::NewL( *iEventLogger,
       
   128                                          *iDebugLogger );
       
   129     iIkePluginHandlers[KIkeV1PluginHandlerIndex] = CIkePluginHandler::NewL( KIkeV1,
       
   130                                                                             *iEventLogger,
       
   131                                                                             *iDebugLogger );
       
   132     iIkePluginHandlers[KIkeV2PluginHandlerIndex] = CIkePluginHandler::NewL( KIkeV2,
       
   133                                                                             *iEventLogger,
       
   134                                                                             *iDebugLogger );
       
   135     
       
   136     StartL( KKmdServerName );
       
   137     
       
   138 #ifdef _DEBUG
       
   139     iDebugLogger->LogWrite(_L("KMD server started."));
       
   140 #endif        
       
   141     }
       
   142 
       
   143 // ---------------------------------------------------------------------------
       
   144 // Information that KMD session has been closed.
       
   145 // ---------------------------------------------------------------------------
       
   146 //
       
   147 void CKmdServer::KmdSessionClosed()
       
   148     {
       
   149     iSessionCount--;    
       
   150     __ASSERT_DEBUG(iSessionCount >= 0, User::Invariant());
       
   151     
       
   152     StopKmdServer();
       
   153     }
       
   154 
       
   155 // ---------------------------------------------------------------------------
       
   156 // Creates VPN connection object.
       
   157 // ---------------------------------------------------------------------------
       
   158 //
       
   159 CVpnConnection& CKmdServer::CreateVpnConnectionL(TUint32 aVpnIapId)
       
   160     {
       
   161 #ifndef _DEBUG
       
   162     for ( TInt i = 0; i < iVpnConnections.Count(); ++i )
       
   163         {
       
   164         __ASSERT_DEBUG( iVpnConnections[i]->VpnIapId() != aVpnIapId, User::Invariant() );
       
   165         }
       
   166 #endif //_DEBUG    
       
   167     
       
   168     CVpnConnection* newConnection = CVpnConnection::NewLC( aVpnIapId,
       
   169                                                            *this,
       
   170                                                            *iDebugLogger );
       
   171     User::LeaveIfError( iVpnConnections.Append( newConnection ) );
       
   172     CleanupStack::Pop( newConnection );
       
   173     
       
   174     return *newConnection;        
       
   175     }
       
   176 
       
   177 // ---------------------------------------------------------------------------
       
   178 // Gets VPN connection object.
       
   179 // ---------------------------------------------------------------------------
       
   180 //
       
   181 CVpnConnection& CKmdServer::GetVpnConnectionL( TUint32 aVpnIapId )
       
   182     {
       
   183     CVpnConnection* connection = NULL;
       
   184     for ( TInt i = 0; i < iVpnConnections.Count(); ++i )
       
   185         {
       
   186         CVpnConnection* c = iVpnConnections[i];
       
   187         if ( c->VpnIapId() == aVpnIapId )
       
   188             {
       
   189             connection = c;
       
   190             break;
       
   191             }
       
   192         }
       
   193     
       
   194     if ( connection == NULL )
       
   195         {
       
   196         User::Leave( KErrNotFound );
       
   197         }
       
   198     return *connection;            
       
   199     }
       
   200 
       
   201 // ---------------------------------------------------------------------------
       
   202 // Deletes VPN connection object.
       
   203 // ---------------------------------------------------------------------------
       
   204 //
       
   205 void CKmdServer::DeleteVpnConnection( TUint32 aVpnIapId )
       
   206     {    
       
   207     TInt connectionCount = iVpnConnections.Count();
       
   208     for ( TInt i = 0; i < connectionCount; ++i )
       
   209         {
       
   210         CVpnConnection* c = iVpnConnections[i];
       
   211         if ( c->VpnIapId() == aVpnIapId )
       
   212             {
       
   213             iVpnConnections.Remove( i );
       
   214             delete c;
       
   215             break;
       
   216             }
       
   217         }
       
   218     
       
   219     StopKmdServer();
       
   220     }
       
   221 
       
   222 // ---------------------------------------------------------------------------
       
   223 // Creates IKE plugin session.
       
   224 // ---------------------------------------------------------------------------
       
   225 //
       
   226 CIkePluginSessionHandler& CKmdServer::CreateIkePluginSessionL( TInt aIkeVersion,
       
   227                                                                IkeSocket::TIpVersion aIpVersion,
       
   228                                                                CIkeConnectionInterface& aConnection,
       
   229                                                                TUint32 aVpnIapId,
       
   230                                                                TUint32 aVpnNetId,
       
   231                                                                TUint32 aVpnInterfaceIndex,
       
   232                                                                const TInetAddr& aDnsServerAddr,
       
   233                                                                MIkePluginSessionHandlerCallback& aCallback )
       
   234     {
       
   235     TInt index( 0 );
       
   236     switch ( aIkeVersion )
       
   237         {
       
   238         case KIkeV1:
       
   239             {
       
   240             index = KIkeV1PluginHandlerIndex;
       
   241             break;
       
   242             }
       
   243         case KIkeV2:
       
   244             {
       
   245             index = KIkeV2PluginHandlerIndex;
       
   246             break;
       
   247             }
       
   248         default:
       
   249             {
       
   250             ASSERT( EFalse );
       
   251             }        
       
   252         }
       
   253     return iIkePluginHandlers[index]->CreateIkePluginSessionL( aVpnIapId,
       
   254                                                                aVpnNetId,
       
   255                                                                aVpnInterfaceIndex,
       
   256                                                                aIpVersion,
       
   257                                                                aDnsServerAddr,
       
   258                                                                aConnection,
       
   259                                                                aCallback );
       
   260     }
       
   261 
       
   262 // ---------------------------------------------------------------------------
       
   263 // Deletes IKE plugin session.
       
   264 // ---------------------------------------------------------------------------
       
   265 //
       
   266 void CKmdServer::DeleteIkePluginSession( TInt aIkeVersion,
       
   267                                          TUint32 aVpnIapId )
       
   268     {
       
   269     TInt index( 0 );
       
   270     switch ( aIkeVersion )
       
   271         {
       
   272         case KIkeV1:
       
   273             {
       
   274             index = KIkeV1PluginHandlerIndex;
       
   275             break;
       
   276             }
       
   277         case KIkeV2:
       
   278             {
       
   279             index = KIkeV2PluginHandlerIndex;
       
   280             break;
       
   281             }
       
   282         default:
       
   283             {
       
   284             ASSERT( EFalse );
       
   285             break;
       
   286             }        
       
   287         }
       
   288     iIkePluginHandlers[index]->DeleteIkePluginSession( aVpnIapId );
       
   289     }
       
   290 
       
   291 // ---------------------------------------------------------------------------
       
   292 // Returns debug trace interface.
       
   293 // ---------------------------------------------------------------------------
       
   294 //
       
   295 MIkeDebug& CKmdServer::Debug()
       
   296     {
       
   297     __ASSERT_DEBUG( iDebugLogger != NULL,
       
   298                     User::Invariant() );
       
   299     
       
   300     return *iDebugLogger;
       
   301     }
       
   302 
       
   303 // ---------------------------------------------------------------------------
       
   304 // Creates a server-side session object.
       
   305 // ---------------------------------------------------------------------------
       
   306 //
       
   307 CSession2* CKmdServer::NewSessionL( const TVersion& /*aVersion*/,
       
   308                                     const RMessage2& /*aMessage*/) const
       
   309     {
       
   310     CKmdSession* session = CKmdSession::NewL( *const_cast<CKmdServer*>( this ),
       
   311                                               *iDebugLogger );
       
   312     iSessionCount++;
       
   313     return session;
       
   314     }
       
   315 
       
   316 // ---------------------------------------------------------------------------
       
   317 // Stops KMD server if there exist neither client sessions nor active
       
   318 // connections.
       
   319 // ---------------------------------------------------------------------------
       
   320 //
       
   321 void CKmdServer::StopKmdServer()
       
   322     {
       
   323     if ( iSessionCount == 0 &&
       
   324          iVpnConnections.Count() == 0 )   
       
   325         {
       
   326         // Stop scheduluder that was started by RunServerL().
       
   327         CActiveScheduler::Stop();        
       
   328         }
       
   329     }