vpnengine/kmdserver/src/ikepluginsessionhandler.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:  Handler of an IKE protocol plugin session
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <in_sock.h>
       
    20 
       
    21 #include "errorobserver.h"
       
    22 #include "iachangeobserver.h"
       
    23 #include "ikeconnectioninterface.h"
       
    24 #include "ikedebug.h"
       
    25 #include "ikepluginif.h"
       
    26 #include "ikepluginsessionif.h"
       
    27 #include "ikesocketdefs.h"
       
    28 #include "internaladdress.h"
       
    29 
       
    30 // CLASS HEADER
       
    31 #include "ikepluginsessionhandler.h"
       
    32 
       
    33 // ======== MEMBER FUNCTIONS ========
       
    34 
       
    35 // ---------------------------------------------------------------------------
       
    36 // Two-phased constructor.
       
    37 // ---------------------------------------------------------------------------
       
    38 //
       
    39 CIkePluginSessionHandler* CIkePluginSessionHandler::NewLC( TUint32 aVpnIapId,
       
    40                                                            TUint32 aVpnNetId,
       
    41                                                            TUint32 aVpnInterfaceIndex,
       
    42                                                            IkeSocket::TIkeMajorVersion aIkeVersion,
       
    43                                                            IkeSocket::TIpVersion aIpVersion,
       
    44                                                            const TInetAddr& aDnsServerAddr,
       
    45                                                            CIkeConnectionInterface& aConnection,
       
    46                                                            MIkePluginIf& aIkePlugin,            
       
    47                                                            MIkePluginSessionHandlerCallback& aCallback,
       
    48                                                            MIkeDebug& aDebug )
       
    49     {
       
    50     CIkePluginSessionHandler* self = new (ELeave) CIkePluginSessionHandler( aVpnIapId,
       
    51                                                                             aIkeVersion,
       
    52                                                                             aIpVersion,
       
    53                                                                             aCallback,
       
    54                                                                             aDebug );
       
    55     CleanupStack::PushL( self );
       
    56     self->ConstructL( aVpnNetId,
       
    57                       aVpnInterfaceIndex,
       
    58                       aDnsServerAddr,
       
    59                       aConnection,
       
    60                       aIkePlugin );
       
    61     return self;
       
    62     }
       
    63     
       
    64 // ---------------------------------------------------------------------------
       
    65 // Destructor.
       
    66 // ---------------------------------------------------------------------------
       
    67 //
       
    68 CIkePluginSessionHandler::~CIkePluginSessionHandler()
       
    69     {
       
    70     Cancel();
       
    71 
       
    72     delete iErrorObserver;
       
    73     delete iIaChangeObserver;
       
    74     delete iIkePluginSession;
       
    75     }
       
    76 
       
    77 // ---------------------------------------------------------------------------
       
    78 // Constructor.
       
    79 // ---------------------------------------------------------------------------
       
    80 //
       
    81 CIkePluginSessionHandler::CIkePluginSessionHandler( TUint32 aVpnIapId,
       
    82                                                     IkeSocket::TIkeMajorVersion aIkeVersion,
       
    83                                                     IkeSocket::TIpVersion aIpVersion,
       
    84                                                     MIkePluginSessionHandlerCallback& aCallback,
       
    85                                                     MIkeDebug& aDebug ) 
       
    86  : CActive( EPriorityStandard ),
       
    87    iVpnIapId( aVpnIapId ),
       
    88    iIkeVersion( aIkeVersion ),
       
    89    iIpVersion( aIpVersion ),
       
    90    iCallback( aCallback ),
       
    91    iDebug( aDebug )
       
    92     {
       
    93     CActiveScheduler::Add( this );
       
    94     }    
       
    95 
       
    96 // ---------------------------------------------------------------------------
       
    97 // Second phase construction.
       
    98 // ---------------------------------------------------------------------------
       
    99 //
       
   100 void CIkePluginSessionHandler::ConstructL( TUint32 aVpnNetId,
       
   101                                            TUint32 aVpnInterfaceIndex,
       
   102                                            const TInetAddr& aDnsServerAddr,
       
   103                                            CIkeConnectionInterface& aConnection,
       
   104                                            MIkePluginIf& aIkePlugin )
       
   105     {
       
   106     if ( iIkeDataInterface == NULL )
       
   107         {
       
   108         iIkeDataInterface = &aConnection.OpenDataInterfaceL( iIkeVersion,
       
   109                                                              iIpVersion );
       
   110         }    
       
   111     
       
   112     iIkePluginSession = aIkePlugin.CreateSessionL( iVpnIapId,
       
   113                                                    aVpnNetId,
       
   114                                                    aVpnInterfaceIndex,
       
   115                                                    *iIkeDataInterface );  
       
   116     
       
   117     iErrorObserver = CErrorObserver::NewL( *iIkePluginSession,
       
   118                                            iCallback,
       
   119                                            iDebug );
       
   120     
       
   121     iIaChangeObserver = CIaChangeObserver::NewL( iVpnIapId,
       
   122                                                  aDnsServerAddr,
       
   123                                                  *iIkePluginSession,
       
   124                                                  iDebug );
       
   125     
       
   126     DEBUG_LOG(_L("IKE plugin session created."));
       
   127     }       
       
   128 
       
   129 // ---------------------------------------------------------------------------
       
   130 // Starts negotiation with a remote host asynchronously.
       
   131 // ---------------------------------------------------------------------------
       
   132 //
       
   133 void CIkePluginSessionHandler::NegotiateWithHost( const CIkeData& aIkeData )
       
   134     {
       
   135     __ASSERT_DEBUG( iState == EIdle,
       
   136                     User::Invariant() );
       
   137     iIkePluginSession->NegotiateWithHost( aIkeData,
       
   138                                           iInternalAddress,
       
   139                                           iStatus );
       
   140     SetActive();
       
   141     iState = ENegotiatingWithHost;
       
   142     }
       
   143 
       
   144 // ---------------------------------------------------------------------------
       
   145 // Cancels negotiation request.
       
   146 // ---------------------------------------------------------------------------
       
   147 //
       
   148 void CIkePluginSessionHandler::CancelNegotiateWithHost()
       
   149     {
       
   150     if ( iState == ENegotiatingWithHost &&
       
   151          IsActive() )
       
   152         {
       
   153         Cancel();
       
   154         TVPNAddress empty;
       
   155         iCallback.NegotiationStarted( KErrCancel,
       
   156                                       empty );
       
   157         }
       
   158     }
       
   159 
       
   160 // ---------------------------------------------------------------------------
       
   161 // Deletes session. 
       
   162 // ---------------------------------------------------------------------------
       
   163 //
       
   164 void CIkePluginSessionHandler::DeleteSession( TBool aSilentClose )
       
   165     {
       
   166     iErrorObserver->Cancel();
       
   167     iIaChangeObserver->Cancel();
       
   168     
       
   169     if ( iState == ENegotiated )
       
   170         {
       
   171         // Delete session asynchronously.
       
   172         iIkePluginSession->DeleteSession( aSilentClose, iStatus );
       
   173         SetActive();
       
   174         iState = EDeletingSession;
       
   175         }
       
   176     else if ( iState == EDeletingSession )
       
   177         {
       
   178         // Session deletion is already in progress. Cancelling it deletes
       
   179         // session silently.
       
   180         CancelDeleteSession();
       
   181         }
       
   182     else if ( iState == EIdle )
       
   183         {
       
   184         // Session deletion can be requested, if negotiate request has failed.
       
   185         // In this case, it is enough to complete request.
       
   186         TRequestStatus* ownStatus = &iStatus;
       
   187         *ownStatus = KRequestPending;
       
   188         SetActive();
       
   189                 
       
   190         User::RequestComplete( ownStatus, KErrNone );
       
   191         }
       
   192     else
       
   193         {
       
   194         ASSERT( EFalse );
       
   195         }
       
   196     }
       
   197 
       
   198 // ---------------------------------------------------------------------------
       
   199 // Cancels session deletion request.
       
   200 // ---------------------------------------------------------------------------
       
   201 //
       
   202 void CIkePluginSessionHandler::CancelDeleteSession()
       
   203     {
       
   204     if ( IsActive() )
       
   205         {
       
   206         Cancel();
       
   207         iCallback.IkePluginSessionClosed( KErrCancel );
       
   208         }
       
   209     }
       
   210 
       
   211 // ---------------------------------------------------------------------------
       
   212 // Returns VPN IAP Id.
       
   213 // ---------------------------------------------------------------------------
       
   214 //
       
   215 TInt CIkePluginSessionHandler::VpnIapId() const
       
   216     {
       
   217     return iVpnIapId;
       
   218     }
       
   219 
       
   220 // ---------------------------------------------------------------------------
       
   221 // From class CActive
       
   222 // Handles completion of asynchronous request. 
       
   223 // ---------------------------------------------------------------------------
       
   224 //
       
   225 void CIkePluginSessionHandler::RunL()
       
   226     {
       
   227     DEBUG_LOG2( _L("CIkePluginSessionHandler::RunL, iState=%d, iStatus=%d"),
       
   228             iState, iStatus.Int() );
       
   229 
       
   230     switch ( iState )
       
   231         {
       
   232         case ENegotiatingWithHost:
       
   233             if ( iStatus.Int() == KErrNone )
       
   234                 {
       
   235                 iErrorObserver->StartObserving();
       
   236                 iIaChangeObserver->StartObserving();
       
   237                 iState = ENegotiated;
       
   238                 }
       
   239             else
       
   240                 {
       
   241                 iState = EIdle;
       
   242                 }
       
   243             // Ownership of internal address transferred.
       
   244             iCallback.NegotiationStarted( iStatus.Int(),
       
   245                                           iInternalAddress );
       
   246             break;
       
   247         case EDeletingSession: // Fall through
       
   248         case EIdle:        
       
   249             iState = EIdle;
       
   250             iCallback.IkePluginSessionClosed( iStatus.Int() );
       
   251             break;
       
   252         default:           
       
   253             ASSERT( EFalse );
       
   254             break;
       
   255         }        
       
   256     }
       
   257 
       
   258 // ---------------------------------------------------------------------------
       
   259 // From class CActive
       
   260 // Handles cancellation of asynchronous request. 
       
   261 // ---------------------------------------------------------------------------
       
   262 //
       
   263 void CIkePluginSessionHandler::DoCancel()
       
   264     {
       
   265     DEBUG_LOG1( _L("CIkePluginSessionHandler::DoCancel, iState=%d"),
       
   266             iState );
       
   267 
       
   268     switch ( iState )
       
   269         {
       
   270         case ENegotiatingWithHost: 
       
   271             iIkePluginSession->CancelNegotiateWithHost();
       
   272             iInternalAddress = TVPNAddress();
       
   273             // Session deletion is needed later, if negotiate request has
       
   274             // already been completed from IKE plugin session.            
       
   275             iState = ENegotiated;           
       
   276             break;
       
   277         case EDeletingSession:
       
   278             iIkePluginSession->CancelDeleteSession();
       
   279             iState = EIdle;
       
   280             break;
       
   281         case EIdle:                                    
       
   282             break;
       
   283         default:
       
   284             ASSERT( EFalse );
       
   285             break;
       
   286         }    
       
   287     }