vpnengine/ikev1lib/src/ikev1keepalive.cpp
changeset 0 33413c0669b9
equal deleted inserted replaced
-1:000000000000 0:33413c0669b9
       
     1 /*
       
     2 * Copyright (c) 2003-2007 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:  IKEv1 keep alive object
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <eikenv.h>
       
    20 #include <es_sock.h>
       
    21 #include <e32math.h>
       
    22 #include "ikedebug.h"
       
    23 #include "ikev1keepalive.h"
       
    24 #include "ikev1pluginsession.h"
       
    25 
       
    26 _LIT8(KMsgContent, "\xff");
       
    27 
       
    28 CIkeV1KeepAlive* CIkeV1KeepAlive::NewL( CIkev1PluginSession& aPluginSession,
       
    29                                         TInt aPort,
       
    30                                         TInetAddr& aDestAddr, 
       
    31                                         TInt NatKeepAlive,
       
    32                                         TInt DpdKeepAlive,
       
    33                                         MDpdHeartBeatEventHandler* aHandler,
       
    34                                         TUint8 aDscp )
       
    35     {
       
    36 	CIkeV1KeepAlive* self = new (ELeave) CIkeV1KeepAlive( aPluginSession,
       
    37 	                                                      aPort,
       
    38 	                                                      aDestAddr,
       
    39 	                                                      NatKeepAlive, 
       
    40 	                                                      DpdKeepAlive,
       
    41 	                                                      aHandler,
       
    42 	                                                      aDscp );
       
    43 	CleanupStack::PushL( self );					   	
       
    44 	self->ConstructL();
       
    45 	CleanupStack::Pop( self );
       
    46 	return self;
       
    47     }
       
    48 
       
    49 
       
    50 
       
    51 CIkeV1KeepAlive::CIkeV1KeepAlive( CIkev1PluginSession& aPluginSession,
       
    52                                   TInt aPort,
       
    53                                   TInetAddr& aDestAddr, 
       
    54                                   TInt aNatKeepAlive,
       
    55                                   TInt aDpdKeepAlive, 
       
    56                                   MDpdHeartBeatEventHandler* aHandler,
       
    57                                   TUint8 aDscp ) 
       
    58  : CTimer( EPriorityStandard ),
       
    59    iPluginSession( aPluginSession ),
       
    60    iPort( aPort ),
       
    61    iMsg( KMsgContent )
       
    62     {
       
    63 	iDestAddr     = aDestAddr;
       
    64 	iNatKeepAlive = aNatKeepAlive;
       
    65 	iDpdKeepAlive = aDpdKeepAlive;
       
    66 	iCallback     = aHandler;
       
    67 	iDscp         = aDscp;
       
    68     CActiveScheduler::Add( this );
       
    69     }
       
    70 
       
    71 //Destructor
       
    72 CIkeV1KeepAlive::~CIkeV1KeepAlive()
       
    73     {
       
    74     Cancel();
       
    75     }
       
    76 
       
    77 
       
    78 void CIkeV1KeepAlive::ConstructL()
       
    79     {
       
    80     //
       
    81     // If NAT keepalive timoeut allocate (and initialize) a TIkeXmitBfr data buffer for
       
    82     // NAT keepalive. Allocate a data buffer for special "Echo request"
       
    83     // keepalive message, if proprietary "Nokia NAT Traversal is used".
       
    84     // Start keep alive timer
       
    85     //
       
    86     CTimer::ConstructL();
       
    87    
       
    88     if ( (iNatKeepAlive >= iDpdKeepAlive) && (iDpdKeepAlive != 0) && iCallback )
       
    89         iNatKeepAlive	= 0;  // Not needed
       
    90 
       
    91     if ( iNatKeepAlive )
       
    92         {
       
    93         iDestAddr.SetPort(iPort);
       
    94 	   
       
    95         iRemainingTime = iNatKeepAlive;
       
    96         if ( iCallback )
       
    97             iCurrDPDTimeLeft = iDpdKeepAlive - iNatKeepAlive;
       
    98         else iDpdKeepAlive = 0;
       
    99         }
       
   100     else
       
   101         {
       
   102         iRemainingTime = iDpdKeepAlive;
       
   103         }	    
       
   104 
       
   105     StartTimer();
       
   106     }
       
   107 
       
   108 
       
   109 void CIkeV1KeepAlive::DoCancel()
       
   110     {
       
   111     CTimer::DoCancel();
       
   112     }
       
   113 
       
   114 void CIkeV1KeepAlive::RunL()
       
   115     {
       
   116 	if ( iRemainingTime == 0 ) 
       
   117 	    {
       
   118 	    TBool DpdEvent = EFalse;	
       
   119 	    if ( iDpdKeepAlive )
       
   120 	        {
       
   121 	        if ( ( iCurrDPDTimeLeft == 0 ) && iCallback )
       
   122 	            {
       
   123 	            //
       
   124 	            // Call DPD event handler 
       
   125 	            //
       
   126 	            DpdEvent = ETrue; 
       
   127 	            iCallback->EventHandlerL();
       
   128 	            iRemainingTime = iDpdKeepAlive;				 			  
       
   129 	            }		 
       
   130 	        }	   
       
   131 
       
   132 	    if ( iNatKeepAlive )
       
   133 	        {
       
   134 	        //
       
   135 	        // Send a NAT keepalive message(s)
       
   136 	        //
       
   137 	        if ( !DpdEvent )
       
   138 	            {	   
       
   139 	            iPluginSession.SendNatKeepAliveL( iDestAddr,
       
   140 	                                              iMsg,
       
   141 	                                              iDscp );
       
   142 	            }
       
   143 		   
       
   144 	        iRemainingTime = iNatKeepAlive;
       
   145 		   
       
   146 	        if ( iDpdKeepAlive )
       
   147 	            {
       
   148 	            if ( DpdEvent )
       
   149 	                {	  
       
   150 	                iCurrDPDTimeLeft = iDpdKeepAlive - iNatKeepAlive;
       
   151 	                }	 
       
   152 	            else
       
   153 	                {
       
   154 	                if ( iCurrDPDTimeLeft < iNatKeepAlive )
       
   155 	                    {
       
   156 	                    iRemainingTime   = iCurrDPDTimeLeft; 
       
   157 	                    iCurrDPDTimeLeft = 0;					 
       
   158 	                    }	 
       
   159 	                else iCurrDPDTimeLeft -= iNatKeepAlive;
       
   160 	                }	  
       
   161 	            }
       
   162 	        }	   
       
   163 	    }
       
   164 	
       
   165 	StartTimer();	
       
   166     }
       
   167 
       
   168 TInt CIkeV1KeepAlive::RunError(TInt /*aError*/)
       
   169     {
       
   170     return KErrNone;
       
   171     }
       
   172 
       
   173 void CIkeV1KeepAlive::StartTimer()
       
   174     {	
       
   175     if ( iRemainingTime > KMaxTInt/1000000 )   //To avoid overflowing the Timer
       
   176         {
       
   177         iRemainingTime -= KMaxTInt/1000000;
       
   178 		After(KMaxTInt);
       
   179         }
       
   180     else    //No overflow
       
   181         {
       
   182 		if ( iRemainingTime )
       
   183 		    {
       
   184 		    After(iRemainingTime*1000000);
       
   185 		    }
       
   186 		iRemainingTime = 0;
       
   187         }
       
   188     }
       
   189